88ab164d5b458233c438ea1c48b8281bb82338a0
[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 /* Return a type that is a generic pointer type, the size of which
425 matches the address size given in the compilation unit header for
426 this CU. */
427 struct type *addr_type () const;
428
429 /* Find an integer type the same size as the address size given in
430 the compilation unit header for this CU. UNSIGNED_P controls if
431 the integer is unsigned or not. */
432 struct type *addr_sized_int_type (bool unsigned_p) const;
433
434 /* The header of the compilation unit. */
435 struct comp_unit_head header {};
436
437 /* Base address of this compilation unit. */
438 gdb::optional<CORE_ADDR> base_address;
439
440 /* The language we are debugging. */
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
443
444 const char *producer = nullptr;
445
446 private:
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
449 std::unique_ptr<buildsym_compunit> m_builder;
450
451 public:
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
461 struct pending **list_in_scope = nullptr;
462
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
465 htab_t partial_dies = nullptr;
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
469 auto_obstack comp_unit_obstack;
470
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
476
477 /* Backlink to our per_cu entry. */
478 struct dwarf2_per_cu_data *per_cu;
479
480 /* The dwarf2_per_objfile that owns this. */
481 struct dwarf2_per_objfile *per_objfile;
482
483 /* How many compilation units ago was this CU last referenced? */
484 int last_used = 0;
485
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
488 htab_t die_hash = nullptr;
489
490 /* Full DIEs if read in. */
491 struct die_info *dies = nullptr;
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
496 htab_t dependencies = nullptr;
497
498 /* Header data from the line table, during full symbol processing. */
499 struct line_header *line_header = nullptr;
500 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
501 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
502 this is the DW_TAG_compile_unit die for this CU. We'll hold on
503 to the line header as long as this DIE is being processed. See
504 process_die_scope. */
505 die_info *line_header_die_owner = nullptr;
506
507 /* A list of methods which need to have physnames computed
508 after all type information has been read. */
509 std::vector<delayed_method_info> method_list;
510
511 /* To be copied to symtab->call_site_htab. */
512 htab_t call_site_htab = nullptr;
513
514 /* Non-NULL if this CU came from a DWO file.
515 There is an invariant here that is important to remember:
516 Except for attributes copied from the top level DIE in the "main"
517 (or "stub") file in preparation for reading the DWO file
518 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
519 Either there isn't a DWO file (in which case this is NULL and the point
520 is moot), or there is and either we're not going to read it (in which
521 case this is NULL) or there is and we are reading it (in which case this
522 is non-NULL). */
523 struct dwo_unit *dwo_unit = nullptr;
524
525 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
526 Note this value comes from the Fission stub CU/TU's DIE. */
527 gdb::optional<ULONGEST> addr_base;
528
529 /* The DW_AT_rnglists_base attribute if present.
530 Note this value comes from the Fission stub CU/TU's DIE.
531 Also note that the value is zero in the non-DWO case so this value can
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
536 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
538 ULONGEST ranges_base = 0;
539
540 /* The DW_AT_loclists_base attribute if present. */
541 ULONGEST loclist_base = 0;
542
543 /* When reading debug info generated by older versions of rustc, we
544 have to rewrite some union types to be struct types with a
545 variant part. This rewriting must be done after the CU is fully
546 read in, because otherwise at the point of rewriting some struct
547 type might not have been fully processed. So, we keep a list of
548 all such types here and process them after expansion. */
549 std::vector<struct type *> rust_unions;
550
551 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
552 files, the value is implicitly zero. For DWARF 5 version DWO files, the
553 value is often implicit and is the size of the header of
554 .debug_str_offsets section (8 or 4, depending on the address size). */
555 gdb::optional<ULONGEST> str_offsets_base;
556
557 /* Mark used when releasing cached dies. */
558 bool mark : 1;
559
560 /* This CU references .debug_loc. See the symtab->locations_valid field.
561 This test is imperfect as there may exist optimized debug code not using
562 any location list and still facing inlining issues if handled as
563 unoptimized code. For a future better test see GCC PR other/32998. */
564 bool has_loclist : 1;
565
566 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
567 if all the producer_is_* fields are valid. This information is cached
568 because profiling CU expansion showed excessive time spent in
569 producer_is_gxx_lt_4_6. */
570 bool checked_producer : 1;
571 bool producer_is_gxx_lt_4_6 : 1;
572 bool producer_is_gcc_lt_4_3 : 1;
573 bool producer_is_icc : 1;
574 bool producer_is_icc_lt_14 : 1;
575 bool producer_is_codewarrior : 1;
576
577 /* When true, the file that we're processing is known to have
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
581 bool processing_has_namespace_info : 1;
582
583 struct partial_die_info *find_partial_die (sect_offset sect_off);
584
585 /* If this CU was inherited by another CU (via specification,
586 abstract_origin, etc), this is the ancestor CU. */
587 dwarf2_cu *ancestor;
588
589 /* Get the buildsym_compunit for this CU. */
590 buildsym_compunit *get_builder ()
591 {
592 /* If this CU has a builder associated with it, use that. */
593 if (m_builder != nullptr)
594 return m_builder.get ();
595
596 /* Otherwise, search ancestors for a valid builder. */
597 if (ancestor != nullptr)
598 return ancestor->get_builder ();
599
600 return nullptr;
601 }
602 };
603
604 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
605 This includes type_unit_group and quick_file_names. */
606
607 struct stmt_list_hash
608 {
609 /* The DWO unit this table is from or NULL if there is none. */
610 struct dwo_unit *dwo_unit;
611
612 /* Offset in .debug_line or .debug_line.dwo. */
613 sect_offset line_sect_off;
614 };
615
616 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
617 an object of this type. */
618
619 struct type_unit_group
620 {
621 /* dwarf2read.c's main "handle" on a TU symtab.
622 To simplify things we create an artificial CU that "includes" all the
623 type units using this stmt_list so that the rest of the code still has
624 a "per_cu" handle on the symtab. */
625 struct dwarf2_per_cu_data per_cu;
626
627 /* The TUs that share this DW_AT_stmt_list entry.
628 This is added to while parsing type units to build partial symtabs,
629 and is deleted afterwards and not used again. */
630 std::vector<signatured_type *> *tus;
631
632 /* The compunit symtab.
633 Type units in a group needn't all be defined in the same source file,
634 so we create an essentially anonymous symtab as the compunit symtab. */
635 struct compunit_symtab *compunit_symtab;
636
637 /* The data used to construct the hash key. */
638 struct stmt_list_hash hash;
639
640 /* The symbol tables for this TU (obtained from the files listed in
641 DW_AT_stmt_list).
642 WARNING: The order of entries here must match the order of entries
643 in the line header. After the first TU using this type_unit_group, the
644 line header for the subsequent TUs is recreated from this. This is done
645 because we need to use the same symtabs for each TU using the same
646 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
647 there's no guarantee the line header doesn't have duplicate entries. */
648 struct symtab **symtabs;
649 };
650
651 /* These sections are what may appear in a (real or virtual) DWO file. */
652
653 struct dwo_sections
654 {
655 struct dwarf2_section_info abbrev;
656 struct dwarf2_section_info line;
657 struct dwarf2_section_info loc;
658 struct dwarf2_section_info loclists;
659 struct dwarf2_section_info macinfo;
660 struct dwarf2_section_info macro;
661 struct dwarf2_section_info str;
662 struct dwarf2_section_info str_offsets;
663 /* In the case of a virtual DWO file, these two are unused. */
664 struct dwarf2_section_info info;
665 std::vector<dwarf2_section_info> types;
666 };
667
668 /* CUs/TUs in DWP/DWO files. */
669
670 struct dwo_unit
671 {
672 /* Backlink to the containing struct dwo_file. */
673 struct dwo_file *dwo_file;
674
675 /* The "id" that distinguishes this CU/TU.
676 .debug_info calls this "dwo_id", .debug_types calls this "signature".
677 Since signatures came first, we stick with it for consistency. */
678 ULONGEST signature;
679
680 /* The section this CU/TU lives in, in the DWO file. */
681 struct dwarf2_section_info *section;
682
683 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
684 sect_offset sect_off;
685 unsigned int length;
686
687 /* For types, offset in the type's DIE of the type defined by this TU. */
688 cu_offset type_offset_in_tu;
689 };
690
691 /* include/dwarf2.h defines the DWP section codes.
692 It defines a max value but it doesn't define a min value, which we
693 use for error checking, so provide one. */
694
695 enum dwp_v2_section_ids
696 {
697 DW_SECT_MIN = 1
698 };
699
700 /* Data for one DWO file.
701
702 This includes virtual DWO files (a virtual DWO file is a DWO file as it
703 appears in a DWP file). DWP files don't really have DWO files per se -
704 comdat folding of types "loses" the DWO file they came from, and from
705 a high level view DWP files appear to contain a mass of random types.
706 However, to maintain consistency with the non-DWP case we pretend DWP
707 files contain virtual DWO files, and we assign each TU with one virtual
708 DWO file (generally based on the line and abbrev section offsets -
709 a heuristic that seems to work in practice). */
710
711 struct dwo_file
712 {
713 dwo_file () = default;
714 DISABLE_COPY_AND_ASSIGN (dwo_file);
715
716 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
717 For virtual DWO files the name is constructed from the section offsets
718 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
719 from related CU+TUs. */
720 const char *dwo_name = nullptr;
721
722 /* The DW_AT_comp_dir attribute. */
723 const char *comp_dir = nullptr;
724
725 /* The bfd, when the file is open. Otherwise this is NULL.
726 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
727 gdb_bfd_ref_ptr dbfd;
728
729 /* The sections that make up this DWO file.
730 Remember that for virtual DWO files in DWP V2, these are virtual
731 sections (for lack of a better name). */
732 struct dwo_sections sections {};
733
734 /* The CUs in the file.
735 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
736 an extension to handle LLVM's Link Time Optimization output (where
737 multiple source files may be compiled into a single object/dwo pair). */
738 htab_up cus;
739
740 /* Table of TUs in the file.
741 Each element is a struct dwo_unit. */
742 htab_up tus;
743 };
744
745 /* These sections are what may appear in a DWP file. */
746
747 struct dwp_sections
748 {
749 /* These are used by both DWP version 1 and 2. */
750 struct dwarf2_section_info str;
751 struct dwarf2_section_info cu_index;
752 struct dwarf2_section_info tu_index;
753
754 /* These are only used by DWP version 2 files.
755 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
756 sections are referenced by section number, and are not recorded here.
757 In DWP version 2 there is at most one copy of all these sections, each
758 section being (effectively) comprised of the concatenation of all of the
759 individual sections that exist in the version 1 format.
760 To keep the code simple we treat each of these concatenated pieces as a
761 section itself (a virtual section?). */
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info info;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 struct dwarf2_section_info types;
770 };
771
772 /* These sections are what may appear in a virtual DWO file in DWP version 1.
773 A virtual DWO file is a DWO file as it appears in a DWP file. */
774
775 struct virtual_v1_dwo_sections
776 {
777 struct dwarf2_section_info abbrev;
778 struct dwarf2_section_info line;
779 struct dwarf2_section_info loc;
780 struct dwarf2_section_info macinfo;
781 struct dwarf2_section_info macro;
782 struct dwarf2_section_info str_offsets;
783 /* Each DWP hash table entry records one CU or one TU.
784 That is recorded here, and copied to dwo_unit.section. */
785 struct dwarf2_section_info info_or_types;
786 };
787
788 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
789 In version 2, the sections of the DWO files are concatenated together
790 and stored in one section of that name. Thus each ELF section contains
791 several "virtual" sections. */
792
793 struct virtual_v2_dwo_sections
794 {
795 bfd_size_type abbrev_offset;
796 bfd_size_type abbrev_size;
797
798 bfd_size_type line_offset;
799 bfd_size_type line_size;
800
801 bfd_size_type loc_offset;
802 bfd_size_type loc_size;
803
804 bfd_size_type macinfo_offset;
805 bfd_size_type macinfo_size;
806
807 bfd_size_type macro_offset;
808 bfd_size_type macro_size;
809
810 bfd_size_type str_offsets_offset;
811 bfd_size_type str_offsets_size;
812
813 /* Each DWP hash table entry records one CU or one TU.
814 That is recorded here, and copied to dwo_unit.section. */
815 bfd_size_type info_or_types_offset;
816 bfd_size_type info_or_types_size;
817 };
818
819 /* Contents of DWP hash tables. */
820
821 struct dwp_hash_table
822 {
823 uint32_t version, nr_columns;
824 uint32_t nr_units, nr_slots;
825 const gdb_byte *hash_table, *unit_table;
826 union
827 {
828 struct
829 {
830 const gdb_byte *indices;
831 } v1;
832 struct
833 {
834 /* This is indexed by column number and gives the id of the section
835 in that column. */
836 #define MAX_NR_V2_DWO_SECTIONS \
837 (1 /* .debug_info or .debug_types */ \
838 + 1 /* .debug_abbrev */ \
839 + 1 /* .debug_line */ \
840 + 1 /* .debug_loc */ \
841 + 1 /* .debug_str_offsets */ \
842 + 1 /* .debug_macro or .debug_macinfo */)
843 int section_ids[MAX_NR_V2_DWO_SECTIONS];
844 const gdb_byte *offsets;
845 const gdb_byte *sizes;
846 } v2;
847 } section_pool;
848 };
849
850 /* Data for one DWP file. */
851
852 struct dwp_file
853 {
854 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
855 : name (name_),
856 dbfd (std::move (abfd))
857 {
858 }
859
860 /* Name of the file. */
861 const char *name;
862
863 /* File format version. */
864 int version = 0;
865
866 /* The bfd. */
867 gdb_bfd_ref_ptr dbfd;
868
869 /* Section info for this file. */
870 struct dwp_sections sections {};
871
872 /* Table of CUs in the file. */
873 const struct dwp_hash_table *cus = nullptr;
874
875 /* Table of TUs in the file. */
876 const struct dwp_hash_table *tus = nullptr;
877
878 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
879 htab_up loaded_cus;
880 htab_up loaded_tus;
881
882 /* Table to map ELF section numbers to their sections.
883 This is only needed for the DWP V1 file format. */
884 unsigned int num_sections = 0;
885 asection **elf_sections = nullptr;
886 };
887
888 /* Struct used to pass misc. parameters to read_die_and_children, et
889 al. which are used for both .debug_info and .debug_types dies.
890 All parameters here are unchanging for the life of the call. This
891 struct exists to abstract away the constant parameters of die reading. */
892
893 struct die_reader_specs
894 {
895 /* The bfd of die_section. */
896 bfd* abfd;
897
898 /* The CU of the DIE we are parsing. */
899 struct dwarf2_cu *cu;
900
901 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
902 struct dwo_file *dwo_file;
903
904 /* The section the die comes from.
905 This is either .debug_info or .debug_types, or the .dwo variants. */
906 struct dwarf2_section_info *die_section;
907
908 /* die_section->buffer. */
909 const gdb_byte *buffer;
910
911 /* The end of the buffer. */
912 const gdb_byte *buffer_end;
913
914 /* The abbreviation table to use when reading the DIEs. */
915 struct abbrev_table *abbrev_table;
916 };
917
918 /* A subclass of die_reader_specs that holds storage and has complex
919 constructor and destructor behavior. */
920
921 class cutu_reader : public die_reader_specs
922 {
923 public:
924
925 cutu_reader (dwarf2_per_cu_data *this_cu,
926 dwarf2_per_objfile *per_objfile,
927 struct abbrev_table *abbrev_table,
928 int use_existing_cu,
929 bool skip_partial);
930
931 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
932 dwarf2_per_objfile *per_objfile,
933 struct dwarf2_cu *parent_cu = nullptr,
934 struct dwo_file *dwo_file = nullptr);
935
936 DISABLE_COPY_AND_ASSIGN (cutu_reader);
937
938 const gdb_byte *info_ptr = nullptr;
939 struct die_info *comp_unit_die = nullptr;
940 bool dummy_p = false;
941
942 /* Release the new CU, putting it on the chain. This cannot be done
943 for dummy CUs. */
944 void keep ();
945
946 private:
947 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
948 dwarf2_per_objfile *per_objfile,
949 int use_existing_cu);
950
951 struct dwarf2_per_cu_data *m_this_cu;
952 std::unique_ptr<dwarf2_cu> m_new_cu;
953
954 /* The ordinary abbreviation table. */
955 abbrev_table_up m_abbrev_table_holder;
956
957 /* The DWO abbreviation table. */
958 abbrev_table_up m_dwo_abbrev_table;
959 };
960
961 /* When we construct a partial symbol table entry we only
962 need this much information. */
963 struct partial_die_info : public allocate_on_obstack
964 {
965 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
966
967 /* Disable assign but still keep copy ctor, which is needed
968 load_partial_dies. */
969 partial_die_info& operator=(const partial_die_info& rhs) = delete;
970
971 /* Adjust the partial die before generating a symbol for it. This
972 function may set the is_external flag or change the DIE's
973 name. */
974 void fixup (struct dwarf2_cu *cu);
975
976 /* Read a minimal amount of information into the minimal die
977 structure. */
978 const gdb_byte *read (const struct die_reader_specs *reader,
979 const struct abbrev_info &abbrev,
980 const gdb_byte *info_ptr);
981
982 /* Offset of this DIE. */
983 const sect_offset sect_off;
984
985 /* DWARF-2 tag for this DIE. */
986 const ENUM_BITFIELD(dwarf_tag) tag : 16;
987
988 /* Assorted flags describing the data found in this DIE. */
989 const unsigned int has_children : 1;
990
991 unsigned int is_external : 1;
992 unsigned int is_declaration : 1;
993 unsigned int has_type : 1;
994 unsigned int has_specification : 1;
995 unsigned int has_pc_info : 1;
996 unsigned int may_be_inlined : 1;
997
998 /* This DIE has been marked DW_AT_main_subprogram. */
999 unsigned int main_subprogram : 1;
1000
1001 /* Flag set if the SCOPE field of this structure has been
1002 computed. */
1003 unsigned int scope_set : 1;
1004
1005 /* Flag set if the DIE has a byte_size attribute. */
1006 unsigned int has_byte_size : 1;
1007
1008 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1009 unsigned int has_const_value : 1;
1010
1011 /* Flag set if any of the DIE's children are template arguments. */
1012 unsigned int has_template_arguments : 1;
1013
1014 /* Flag set if fixup has been called on this die. */
1015 unsigned int fixup_called : 1;
1016
1017 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1018 unsigned int is_dwz : 1;
1019
1020 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1021 unsigned int spec_is_dwz : 1;
1022
1023 /* The name of this DIE. Normally the value of DW_AT_name, but
1024 sometimes a default name for unnamed DIEs. */
1025 const char *name = nullptr;
1026
1027 /* The linkage name, if present. */
1028 const char *linkage_name = nullptr;
1029
1030 /* The scope to prepend to our children. This is generally
1031 allocated on the comp_unit_obstack, so will disappear
1032 when this compilation unit leaves the cache. */
1033 const char *scope = nullptr;
1034
1035 /* Some data associated with the partial DIE. The tag determines
1036 which field is live. */
1037 union
1038 {
1039 /* The location description associated with this DIE, if any. */
1040 struct dwarf_block *locdesc;
1041 /* The offset of an import, for DW_TAG_imported_unit. */
1042 sect_offset sect_off;
1043 } d {};
1044
1045 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1046 CORE_ADDR lowpc = 0;
1047 CORE_ADDR highpc = 0;
1048
1049 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1050 DW_AT_sibling, if any. */
1051 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1052 could return DW_AT_sibling values to its caller load_partial_dies. */
1053 const gdb_byte *sibling = nullptr;
1054
1055 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1056 DW_AT_specification (or DW_AT_abstract_origin or
1057 DW_AT_extension). */
1058 sect_offset spec_offset {};
1059
1060 /* Pointers to this DIE's parent, first child, and next sibling,
1061 if any. */
1062 struct partial_die_info *die_parent = nullptr;
1063 struct partial_die_info *die_child = nullptr;
1064 struct partial_die_info *die_sibling = nullptr;
1065
1066 friend struct partial_die_info *
1067 dwarf2_cu::find_partial_die (sect_offset sect_off);
1068
1069 private:
1070 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1071 partial_die_info (sect_offset sect_off)
1072 : partial_die_info (sect_off, DW_TAG_padding, 0)
1073 {
1074 }
1075
1076 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1077 int has_children_)
1078 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1079 {
1080 is_external = 0;
1081 is_declaration = 0;
1082 has_type = 0;
1083 has_specification = 0;
1084 has_pc_info = 0;
1085 may_be_inlined = 0;
1086 main_subprogram = 0;
1087 scope_set = 0;
1088 has_byte_size = 0;
1089 has_const_value = 0;
1090 has_template_arguments = 0;
1091 fixup_called = 0;
1092 is_dwz = 0;
1093 spec_is_dwz = 0;
1094 }
1095 };
1096
1097 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1098 but this would require a corresponding change in unpack_field_as_long
1099 and friends. */
1100 static int bits_per_byte = 8;
1101
1102 struct variant_part_builder;
1103
1104 /* When reading a variant, we track a bit more information about the
1105 field, and store it in an object of this type. */
1106
1107 struct variant_field
1108 {
1109 int first_field = -1;
1110 int last_field = -1;
1111
1112 /* A variant can contain other variant parts. */
1113 std::vector<variant_part_builder> variant_parts;
1114
1115 /* If we see a DW_TAG_variant, then this will be set if this is the
1116 default branch. */
1117 bool default_branch = false;
1118 /* If we see a DW_AT_discr_value, then this will be the discriminant
1119 value. */
1120 ULONGEST discriminant_value = 0;
1121 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1122 data. */
1123 struct dwarf_block *discr_list_data = nullptr;
1124 };
1125
1126 /* This represents a DW_TAG_variant_part. */
1127
1128 struct variant_part_builder
1129 {
1130 /* The offset of the discriminant field. */
1131 sect_offset discriminant_offset {};
1132
1133 /* Variants that are direct children of this variant part. */
1134 std::vector<variant_field> variants;
1135
1136 /* True if we're currently reading a variant. */
1137 bool processing_variant = false;
1138 };
1139
1140 struct nextfield
1141 {
1142 int accessibility = 0;
1143 int virtuality = 0;
1144 /* Variant parts need to find the discriminant, which is a DIE
1145 reference. We track the section offset of each field to make
1146 this link. */
1147 sect_offset offset;
1148 struct field field {};
1149 };
1150
1151 struct fnfieldlist
1152 {
1153 const char *name = nullptr;
1154 std::vector<struct fn_field> fnfields;
1155 };
1156
1157 /* The routines that read and process dies for a C struct or C++ class
1158 pass lists of data member fields and lists of member function fields
1159 in an instance of a field_info structure, as defined below. */
1160 struct field_info
1161 {
1162 /* List of data member and baseclasses fields. */
1163 std::vector<struct nextfield> fields;
1164 std::vector<struct nextfield> baseclasses;
1165
1166 /* Set if the accessibility of one of the fields is not public. */
1167 int non_public_fields = 0;
1168
1169 /* Member function fieldlist array, contains name of possibly overloaded
1170 member function, number of overloaded member functions and a pointer
1171 to the head of the member function field chain. */
1172 std::vector<struct fnfieldlist> fnfieldlists;
1173
1174 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1175 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1176 std::vector<struct decl_field> typedef_field_list;
1177
1178 /* Nested types defined by this class and the number of elements in this
1179 list. */
1180 std::vector<struct decl_field> nested_types_list;
1181
1182 /* If non-null, this is the variant part we are currently
1183 reading. */
1184 variant_part_builder *current_variant_part = nullptr;
1185 /* This holds all the top-level variant parts attached to the type
1186 we're reading. */
1187 std::vector<variant_part_builder> variant_parts;
1188
1189 /* Return the total number of fields (including baseclasses). */
1190 int nfields () const
1191 {
1192 return fields.size () + baseclasses.size ();
1193 }
1194 };
1195
1196 /* Loaded secondary compilation units are kept in memory until they
1197 have not been referenced for the processing of this many
1198 compilation units. Set this to zero to disable caching. Cache
1199 sizes of up to at least twenty will improve startup time for
1200 typical inter-CU-reference binaries, at an obvious memory cost. */
1201 static int dwarf_max_cache_age = 5;
1202 static void
1203 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1204 struct cmd_list_element *c, const char *value)
1205 {
1206 fprintf_filtered (file, _("The upper bound on the age of cached "
1207 "DWARF compilation units is %s.\n"),
1208 value);
1209 }
1210 \f
1211 /* local function prototypes */
1212
1213 static void dwarf2_find_base_address (struct die_info *die,
1214 struct dwarf2_cu *cu);
1215
1216 static dwarf2_psymtab *create_partial_symtab
1217 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1218 const char *name);
1219
1220 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1221 const gdb_byte *info_ptr,
1222 struct die_info *type_unit_die);
1223
1224 static void dwarf2_build_psymtabs_hard
1225 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1226
1227 static void scan_partial_symbols (struct partial_die_info *,
1228 CORE_ADDR *, CORE_ADDR *,
1229 int, struct dwarf2_cu *);
1230
1231 static void add_partial_symbol (struct partial_die_info *,
1232 struct dwarf2_cu *);
1233
1234 static void add_partial_namespace (struct partial_die_info *pdi,
1235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1236 int set_addrmap, struct dwarf2_cu *cu);
1237
1238 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1239 CORE_ADDR *highpc, int set_addrmap,
1240 struct dwarf2_cu *cu);
1241
1242 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1243 struct dwarf2_cu *cu);
1244
1245 static void add_partial_subprogram (struct partial_die_info *pdi,
1246 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1247 int need_pc, struct dwarf2_cu *cu);
1248
1249 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1250
1251 static struct partial_die_info *load_partial_dies
1252 (const struct die_reader_specs *, const gdb_byte *, int);
1253
1254 /* A pair of partial_die_info and compilation unit. */
1255 struct cu_partial_die_info
1256 {
1257 /* The compilation unit of the partial_die_info. */
1258 struct dwarf2_cu *cu;
1259 /* A partial_die_info. */
1260 struct partial_die_info *pdi;
1261
1262 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1263 : cu (cu),
1264 pdi (pdi)
1265 { /* Nothing. */ }
1266
1267 private:
1268 cu_partial_die_info () = delete;
1269 };
1270
1271 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1272 struct dwarf2_cu *);
1273
1274 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1275 struct attribute *, struct attr_abbrev *,
1276 const gdb_byte *, bool *need_reprocess);
1277
1278 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1279 struct attribute *attr);
1280
1281 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1282
1283 static sect_offset read_abbrev_offset
1284 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1285 struct dwarf2_section_info *, sect_offset);
1286
1287 static const char *read_indirect_string
1288 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1289 const struct comp_unit_head *, unsigned int *);
1290
1291 static const char *read_indirect_string_at_offset
1292 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1293
1294 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1295 const gdb_byte *,
1296 unsigned int *);
1297
1298 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1299 ULONGEST str_index);
1300
1301 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1302 ULONGEST str_index);
1303
1304 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1305
1306 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1307 struct dwarf2_cu *);
1308
1309 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1310 struct dwarf2_cu *cu);
1311
1312 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1313
1314 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1315 struct dwarf2_cu *cu);
1316
1317 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1318
1319 static struct die_info *die_specification (struct die_info *die,
1320 struct dwarf2_cu **);
1321
1322 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1323 struct dwarf2_cu *cu);
1324
1325 static void dwarf_decode_lines (struct line_header *, const char *,
1326 struct dwarf2_cu *, dwarf2_psymtab *,
1327 CORE_ADDR, int decode_mapping);
1328
1329 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1330 const char *);
1331
1332 static struct symbol *new_symbol (struct die_info *, struct type *,
1333 struct dwarf2_cu *, struct symbol * = NULL);
1334
1335 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1336 struct dwarf2_cu *);
1337
1338 static void dwarf2_const_value_attr (const struct attribute *attr,
1339 struct type *type,
1340 const char *name,
1341 struct obstack *obstack,
1342 struct dwarf2_cu *cu, LONGEST *value,
1343 const gdb_byte **bytes,
1344 struct dwarf2_locexpr_baton **baton);
1345
1346 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1347
1348 static int need_gnat_info (struct dwarf2_cu *);
1349
1350 static struct type *die_descriptive_type (struct die_info *,
1351 struct dwarf2_cu *);
1352
1353 static void set_descriptive_type (struct type *, struct die_info *,
1354 struct dwarf2_cu *);
1355
1356 static struct type *die_containing_type (struct die_info *,
1357 struct dwarf2_cu *);
1358
1359 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1360 struct dwarf2_cu *);
1361
1362 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1363
1364 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1365
1366 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1367
1368 static char *typename_concat (struct obstack *obs, const char *prefix,
1369 const char *suffix, int physname,
1370 struct dwarf2_cu *cu);
1371
1372 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1373
1374 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1375
1376 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1377
1378 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1379
1380 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1381
1382 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1383
1384 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1385 struct dwarf2_cu *, dwarf2_psymtab *);
1386
1387 /* Return the .debug_loclists section to use for cu. */
1388 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1389
1390 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1391 values. Keep the items ordered with increasing constraints compliance. */
1392 enum pc_bounds_kind
1393 {
1394 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1395 PC_BOUNDS_NOT_PRESENT,
1396
1397 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1398 were present but they do not form a valid range of PC addresses. */
1399 PC_BOUNDS_INVALID,
1400
1401 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1402 PC_BOUNDS_RANGES,
1403
1404 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1405 PC_BOUNDS_HIGH_LOW,
1406 };
1407
1408 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1409 CORE_ADDR *, CORE_ADDR *,
1410 struct dwarf2_cu *,
1411 dwarf2_psymtab *);
1412
1413 static void get_scope_pc_bounds (struct die_info *,
1414 CORE_ADDR *, CORE_ADDR *,
1415 struct dwarf2_cu *);
1416
1417 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1418 CORE_ADDR, struct dwarf2_cu *);
1419
1420 static void dwarf2_add_field (struct field_info *, struct die_info *,
1421 struct dwarf2_cu *);
1422
1423 static void dwarf2_attach_fields_to_type (struct field_info *,
1424 struct type *, struct dwarf2_cu *);
1425
1426 static void dwarf2_add_member_fn (struct field_info *,
1427 struct die_info *, struct type *,
1428 struct dwarf2_cu *);
1429
1430 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1431 struct type *,
1432 struct dwarf2_cu *);
1433
1434 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1435
1436 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1437
1438 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1439
1440 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1441
1442 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1443
1444 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1445
1446 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1447
1448 static struct type *read_module_type (struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
1451 static const char *namespace_name (struct die_info *die,
1452 int *is_anonymous, struct dwarf2_cu *);
1453
1454 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1455
1456 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1457 bool * = nullptr);
1458
1459 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1460 struct dwarf2_cu *);
1461
1462 static struct die_info *read_die_and_siblings_1
1463 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1464 struct die_info *);
1465
1466 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1467 const gdb_byte *info_ptr,
1468 const gdb_byte **new_info_ptr,
1469 struct die_info *parent);
1470
1471 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1472 struct die_info **, const gdb_byte *,
1473 int);
1474
1475 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1476 struct die_info **, const gdb_byte *);
1477
1478 static void process_die (struct die_info *, struct dwarf2_cu *);
1479
1480 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1481 struct objfile *);
1482
1483 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1484
1485 static const char *dwarf2_full_name (const char *name,
1486 struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
1489 static const char *dwarf2_physname (const char *name, struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
1492 static struct die_info *dwarf2_extension (struct die_info *die,
1493 struct dwarf2_cu **);
1494
1495 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1496
1497 static void dump_die_for_error (struct die_info *);
1498
1499 static void dump_die_1 (struct ui_file *, int level, int max_level,
1500 struct die_info *);
1501
1502 /*static*/ void dump_die (struct die_info *, int max_level);
1503
1504 static void store_in_ref_table (struct die_info *,
1505 struct dwarf2_cu *);
1506
1507 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1508 const struct attribute *,
1509 struct dwarf2_cu **);
1510
1511 static struct die_info *follow_die_ref (struct die_info *,
1512 const struct attribute *,
1513 struct dwarf2_cu **);
1514
1515 static struct die_info *follow_die_sig (struct die_info *,
1516 const struct attribute *,
1517 struct dwarf2_cu **);
1518
1519 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1520 struct dwarf2_cu *);
1521
1522 static struct type *get_DW_AT_signature_type (struct die_info *,
1523 const struct attribute *,
1524 struct dwarf2_cu *);
1525
1526 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1527 dwarf2_per_objfile *per_objfile);
1528
1529 static void read_signatured_type (signatured_type *sig_type,
1530 dwarf2_per_objfile *per_objfile);
1531
1532 static int attr_to_dynamic_prop (const struct attribute *attr,
1533 struct die_info *die, struct dwarf2_cu *cu,
1534 struct dynamic_prop *prop, struct type *type);
1535
1536 /* memory allocation interface */
1537
1538 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1539
1540 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1541
1542 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1543
1544 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1545 struct dwarf2_loclist_baton *baton,
1546 const struct attribute *attr);
1547
1548 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1549 struct symbol *sym,
1550 struct dwarf2_cu *cu,
1551 int is_block);
1552
1553 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1554 const gdb_byte *info_ptr,
1555 struct abbrev_info *abbrev);
1556
1557 static hashval_t partial_die_hash (const void *item);
1558
1559 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1560
1561 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1562 (sect_offset sect_off, unsigned int offset_in_dwz,
1563 struct dwarf2_per_objfile *dwarf2_per_objfile);
1564
1565 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1566 struct die_info *comp_unit_die,
1567 enum language pretend_language);
1568
1569 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1570
1571 static void free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
1572 dwarf2_per_objfile *per_objfile);
1573
1574 static struct type *set_die_type (struct die_info *, struct type *,
1575 struct dwarf2_cu *);
1576
1577 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1578
1579 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1580
1581 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1582 dwarf2_per_objfile *per_objfile,
1583 bool skip_partial,
1584 enum language pretend_language);
1585
1586 static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1587 dwarf2_per_objfile *per_objfile,
1588 enum language pretend_language);
1589
1590 static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile,
1592 enum language pretend_language);
1593
1594 static void dwarf2_add_dependence (struct dwarf2_cu *,
1595 struct dwarf2_per_cu_data *);
1596
1597 static void dwarf2_mark (struct dwarf2_cu *);
1598
1599 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1600
1601 static struct type *get_die_type_at_offset (sect_offset,
1602 dwarf2_per_cu_data *per_cu,
1603 dwarf2_per_objfile *per_objfile);
1604
1605 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1606
1607 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1608 dwarf2_per_objfile *per_objfile,
1609 enum language pretend_language);
1610
1611 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1612
1613 /* Class, the destructor of which frees all allocated queue entries. This
1614 will only have work to do if an error was thrown while processing the
1615 dwarf. If no error was thrown then the queue entries should have all
1616 been processed, and freed, as we went along. */
1617
1618 class dwarf2_queue_guard
1619 {
1620 public:
1621 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1622 : m_per_objfile (per_objfile)
1623 {
1624 }
1625
1626 /* Free any entries remaining on the queue. There should only be
1627 entries left if we hit an error while processing the dwarf. */
1628 ~dwarf2_queue_guard ()
1629 {
1630 /* Ensure that no memory is allocated by the queue. */
1631 std::queue<dwarf2_queue_item> empty;
1632 std::swap (m_per_objfile->per_bfd->queue, empty);
1633 }
1634
1635 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1636
1637 private:
1638 dwarf2_per_objfile *m_per_objfile;
1639 };
1640
1641 dwarf2_queue_item::~dwarf2_queue_item ()
1642 {
1643 /* Anything still marked queued is likely to be in an
1644 inconsistent state, so discard it. */
1645 if (per_cu->queued)
1646 {
1647 if (per_cu->cu != NULL)
1648 free_one_cached_comp_unit (per_cu, per_objfile);
1649 per_cu->queued = 0;
1650 }
1651 }
1652
1653 /* The return type of find_file_and_directory. Note, the enclosed
1654 string pointers are only valid while this object is valid. */
1655
1656 struct file_and_directory
1657 {
1658 /* The filename. This is never NULL. */
1659 const char *name;
1660
1661 /* The compilation directory. NULL if not known. If we needed to
1662 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1663 points directly to the DW_AT_comp_dir string attribute owned by
1664 the obstack that owns the DIE. */
1665 const char *comp_dir;
1666
1667 /* If we needed to build a new string for comp_dir, this is what
1668 owns the storage. */
1669 std::string comp_dir_storage;
1670 };
1671
1672 static file_and_directory find_file_and_directory (struct die_info *die,
1673 struct dwarf2_cu *cu);
1674
1675 static htab_up allocate_signatured_type_table ();
1676
1677 static htab_up allocate_dwo_unit_table ();
1678
1679 static struct dwo_unit *lookup_dwo_unit_in_dwp
1680 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1681 struct dwp_file *dwp_file, const char *comp_dir,
1682 ULONGEST signature, int is_debug_types);
1683
1684 static struct dwp_file *get_dwp_file
1685 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1686
1687 static struct dwo_unit *lookup_dwo_comp_unit
1688 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1689 ULONGEST signature);
1690
1691 static struct dwo_unit *lookup_dwo_type_unit
1692 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1693
1694 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1695
1696 /* A unique pointer to a dwo_file. */
1697
1698 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1699
1700 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1701
1702 static void check_producer (struct dwarf2_cu *cu);
1703
1704 static void free_line_header_voidp (void *arg);
1705 \f
1706 /* Various complaints about symbol reading that don't abort the process. */
1707
1708 static void
1709 dwarf2_debug_line_missing_file_complaint (void)
1710 {
1711 complaint (_(".debug_line section has line data without a file"));
1712 }
1713
1714 static void
1715 dwarf2_debug_line_missing_end_sequence_complaint (void)
1716 {
1717 complaint (_(".debug_line section has line "
1718 "program sequence without an end"));
1719 }
1720
1721 static void
1722 dwarf2_complex_location_expr_complaint (void)
1723 {
1724 complaint (_("location expression too complex"));
1725 }
1726
1727 static void
1728 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1729 int arg3)
1730 {
1731 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1732 arg1, arg2, arg3);
1733 }
1734
1735 static void
1736 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1737 {
1738 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1739 arg1, arg2);
1740 }
1741
1742 /* Hash function for line_header_hash. */
1743
1744 static hashval_t
1745 line_header_hash (const struct line_header *ofs)
1746 {
1747 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1748 }
1749
1750 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1751
1752 static hashval_t
1753 line_header_hash_voidp (const void *item)
1754 {
1755 const struct line_header *ofs = (const struct line_header *) item;
1756
1757 return line_header_hash (ofs);
1758 }
1759
1760 /* Equality function for line_header_hash. */
1761
1762 static int
1763 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1764 {
1765 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1766 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1767
1768 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1769 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1770 }
1771
1772 \f
1773
1774 /* See declaration. */
1775
1776 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1777 bool can_copy_)
1778 : obfd (obfd),
1779 can_copy (can_copy_)
1780 {
1781 if (names == NULL)
1782 names = &dwarf2_elf_names;
1783
1784 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1785 locate_sections (obfd, sec, *names);
1786 }
1787
1788 dwarf2_per_bfd::~dwarf2_per_bfd ()
1789 {
1790 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1791 free_cached_comp_units ();
1792
1793 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1794 per_cu->imported_symtabs_free ();
1795
1796 for (signatured_type *sig_type : all_type_units)
1797 sig_type->per_cu.imported_symtabs_free ();
1798
1799 /* Everything else should be on this->obstack. */
1800 }
1801
1802 /* See declaration. */
1803
1804 void
1805 dwarf2_per_bfd::free_cached_comp_units ()
1806 {
1807 dwarf2_per_cu_data *per_cu = read_in_chain;
1808 dwarf2_per_cu_data **last_chain = &read_in_chain;
1809 while (per_cu != NULL)
1810 {
1811 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1812
1813 delete per_cu->cu;
1814 *last_chain = next_cu;
1815 per_cu = next_cu;
1816 }
1817 }
1818
1819 /* A helper class that calls free_cached_comp_units on
1820 destruction. */
1821
1822 class free_cached_comp_units
1823 {
1824 public:
1825
1826 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1827 : m_per_objfile (per_objfile)
1828 {
1829 }
1830
1831 ~free_cached_comp_units ()
1832 {
1833 m_per_objfile->per_bfd->free_cached_comp_units ();
1834 }
1835
1836 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1837
1838 private:
1839
1840 dwarf2_per_objfile *m_per_objfile;
1841 };
1842
1843 /* See read.h. */
1844
1845 bool
1846 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1847 {
1848 gdb_assert (per_cu->index < this->m_symtabs.size ());
1849
1850 return this->m_symtabs[per_cu->index] != nullptr;
1851 }
1852
1853 /* See read.h. */
1854
1855 compunit_symtab *
1856 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1857 {
1858 gdb_assert (per_cu->index < this->m_symtabs.size ());
1859
1860 return this->m_symtabs[per_cu->index];
1861 }
1862
1863 /* See read.h. */
1864
1865 void
1866 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1867 compunit_symtab *symtab)
1868 {
1869 gdb_assert (per_cu->index < this->m_symtabs.size ());
1870 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1871
1872 this->m_symtabs[per_cu->index] = symtab;
1873 }
1874
1875 /* Try to locate the sections we need for DWARF 2 debugging
1876 information and return true if we have enough to do something.
1877 NAMES points to the dwarf2 section names, or is NULL if the standard
1878 ELF names are used. CAN_COPY is true for formats where symbol
1879 interposition is possible and so symbol values must follow copy
1880 relocation rules. */
1881
1882 int
1883 dwarf2_has_info (struct objfile *objfile,
1884 const struct dwarf2_debug_sections *names,
1885 bool can_copy)
1886 {
1887 if (objfile->flags & OBJF_READNEVER)
1888 return 0;
1889
1890 struct dwarf2_per_objfile *dwarf2_per_objfile
1891 = get_dwarf2_per_objfile (objfile);
1892
1893 if (dwarf2_per_objfile == NULL)
1894 {
1895 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1896 sharing yet). */
1897 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1898
1899 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1900 }
1901
1902 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1903 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1904 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1905 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1906 }
1907
1908 /* When loading sections, we look either for uncompressed section or for
1909 compressed section names. */
1910
1911 static int
1912 section_is_p (const char *section_name,
1913 const struct dwarf2_section_names *names)
1914 {
1915 if (names->normal != NULL
1916 && strcmp (section_name, names->normal) == 0)
1917 return 1;
1918 if (names->compressed != NULL
1919 && strcmp (section_name, names->compressed) == 0)
1920 return 1;
1921 return 0;
1922 }
1923
1924 /* See declaration. */
1925
1926 void
1927 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1928 const dwarf2_debug_sections &names)
1929 {
1930 flagword aflag = bfd_section_flags (sectp);
1931
1932 if ((aflag & SEC_HAS_CONTENTS) == 0)
1933 {
1934 }
1935 else if (elf_section_data (sectp)->this_hdr.sh_size
1936 > bfd_get_file_size (abfd))
1937 {
1938 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1939 warning (_("Discarding section %s which has a section size (%s"
1940 ") larger than the file size [in module %s]"),
1941 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1942 bfd_get_filename (abfd));
1943 }
1944 else if (section_is_p (sectp->name, &names.info))
1945 {
1946 this->info.s.section = sectp;
1947 this->info.size = bfd_section_size (sectp);
1948 }
1949 else if (section_is_p (sectp->name, &names.abbrev))
1950 {
1951 this->abbrev.s.section = sectp;
1952 this->abbrev.size = bfd_section_size (sectp);
1953 }
1954 else if (section_is_p (sectp->name, &names.line))
1955 {
1956 this->line.s.section = sectp;
1957 this->line.size = bfd_section_size (sectp);
1958 }
1959 else if (section_is_p (sectp->name, &names.loc))
1960 {
1961 this->loc.s.section = sectp;
1962 this->loc.size = bfd_section_size (sectp);
1963 }
1964 else if (section_is_p (sectp->name, &names.loclists))
1965 {
1966 this->loclists.s.section = sectp;
1967 this->loclists.size = bfd_section_size (sectp);
1968 }
1969 else if (section_is_p (sectp->name, &names.macinfo))
1970 {
1971 this->macinfo.s.section = sectp;
1972 this->macinfo.size = bfd_section_size (sectp);
1973 }
1974 else if (section_is_p (sectp->name, &names.macro))
1975 {
1976 this->macro.s.section = sectp;
1977 this->macro.size = bfd_section_size (sectp);
1978 }
1979 else if (section_is_p (sectp->name, &names.str))
1980 {
1981 this->str.s.section = sectp;
1982 this->str.size = bfd_section_size (sectp);
1983 }
1984 else if (section_is_p (sectp->name, &names.str_offsets))
1985 {
1986 this->str_offsets.s.section = sectp;
1987 this->str_offsets.size = bfd_section_size (sectp);
1988 }
1989 else if (section_is_p (sectp->name, &names.line_str))
1990 {
1991 this->line_str.s.section = sectp;
1992 this->line_str.size = bfd_section_size (sectp);
1993 }
1994 else if (section_is_p (sectp->name, &names.addr))
1995 {
1996 this->addr.s.section = sectp;
1997 this->addr.size = bfd_section_size (sectp);
1998 }
1999 else if (section_is_p (sectp->name, &names.frame))
2000 {
2001 this->frame.s.section = sectp;
2002 this->frame.size = bfd_section_size (sectp);
2003 }
2004 else if (section_is_p (sectp->name, &names.eh_frame))
2005 {
2006 this->eh_frame.s.section = sectp;
2007 this->eh_frame.size = bfd_section_size (sectp);
2008 }
2009 else if (section_is_p (sectp->name, &names.ranges))
2010 {
2011 this->ranges.s.section = sectp;
2012 this->ranges.size = bfd_section_size (sectp);
2013 }
2014 else if (section_is_p (sectp->name, &names.rnglists))
2015 {
2016 this->rnglists.s.section = sectp;
2017 this->rnglists.size = bfd_section_size (sectp);
2018 }
2019 else if (section_is_p (sectp->name, &names.types))
2020 {
2021 struct dwarf2_section_info type_section;
2022
2023 memset (&type_section, 0, sizeof (type_section));
2024 type_section.s.section = sectp;
2025 type_section.size = bfd_section_size (sectp);
2026
2027 this->types.push_back (type_section);
2028 }
2029 else if (section_is_p (sectp->name, &names.gdb_index))
2030 {
2031 this->gdb_index.s.section = sectp;
2032 this->gdb_index.size = bfd_section_size (sectp);
2033 }
2034 else if (section_is_p (sectp->name, &names.debug_names))
2035 {
2036 this->debug_names.s.section = sectp;
2037 this->debug_names.size = bfd_section_size (sectp);
2038 }
2039 else if (section_is_p (sectp->name, &names.debug_aranges))
2040 {
2041 this->debug_aranges.s.section = sectp;
2042 this->debug_aranges.size = bfd_section_size (sectp);
2043 }
2044
2045 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2046 && bfd_section_vma (sectp) == 0)
2047 this->has_section_at_zero = true;
2048 }
2049
2050 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2051 SECTION_NAME. */
2052
2053 void
2054 dwarf2_get_section_info (struct objfile *objfile,
2055 enum dwarf2_section_enum sect,
2056 asection **sectp, const gdb_byte **bufp,
2057 bfd_size_type *sizep)
2058 {
2059 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2060 struct dwarf2_section_info *info;
2061
2062 /* We may see an objfile without any DWARF, in which case we just
2063 return nothing. */
2064 if (data == NULL)
2065 {
2066 *sectp = NULL;
2067 *bufp = NULL;
2068 *sizep = 0;
2069 return;
2070 }
2071 switch (sect)
2072 {
2073 case DWARF2_DEBUG_FRAME:
2074 info = &data->per_bfd->frame;
2075 break;
2076 case DWARF2_EH_FRAME:
2077 info = &data->per_bfd->eh_frame;
2078 break;
2079 default:
2080 gdb_assert_not_reached ("unexpected section");
2081 }
2082
2083 info->read (objfile);
2084
2085 *sectp = info->get_bfd_section ();
2086 *bufp = info->buffer;
2087 *sizep = info->size;
2088 }
2089
2090 /* A helper function to find the sections for a .dwz file. */
2091
2092 static void
2093 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2094 {
2095 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2096
2097 /* Note that we only support the standard ELF names, because .dwz
2098 is ELF-only (at the time of writing). */
2099 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2100 {
2101 dwz_file->abbrev.s.section = sectp;
2102 dwz_file->abbrev.size = bfd_section_size (sectp);
2103 }
2104 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2105 {
2106 dwz_file->info.s.section = sectp;
2107 dwz_file->info.size = bfd_section_size (sectp);
2108 }
2109 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2110 {
2111 dwz_file->str.s.section = sectp;
2112 dwz_file->str.size = bfd_section_size (sectp);
2113 }
2114 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2115 {
2116 dwz_file->line.s.section = sectp;
2117 dwz_file->line.size = bfd_section_size (sectp);
2118 }
2119 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2120 {
2121 dwz_file->macro.s.section = sectp;
2122 dwz_file->macro.size = bfd_section_size (sectp);
2123 }
2124 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2125 {
2126 dwz_file->gdb_index.s.section = sectp;
2127 dwz_file->gdb_index.size = bfd_section_size (sectp);
2128 }
2129 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2130 {
2131 dwz_file->debug_names.s.section = sectp;
2132 dwz_file->debug_names.size = bfd_section_size (sectp);
2133 }
2134 }
2135
2136 /* See dwarf2read.h. */
2137
2138 struct dwz_file *
2139 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2140 {
2141 const char *filename;
2142 bfd_size_type buildid_len_arg;
2143 size_t buildid_len;
2144 bfd_byte *buildid;
2145
2146 if (per_bfd->dwz_file != NULL)
2147 return per_bfd->dwz_file.get ();
2148
2149 bfd_set_error (bfd_error_no_error);
2150 gdb::unique_xmalloc_ptr<char> data
2151 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2152 &buildid_len_arg, &buildid));
2153 if (data == NULL)
2154 {
2155 if (bfd_get_error () == bfd_error_no_error)
2156 return NULL;
2157 error (_("could not read '.gnu_debugaltlink' section: %s"),
2158 bfd_errmsg (bfd_get_error ()));
2159 }
2160
2161 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2162
2163 buildid_len = (size_t) buildid_len_arg;
2164
2165 filename = data.get ();
2166
2167 std::string abs_storage;
2168 if (!IS_ABSOLUTE_PATH (filename))
2169 {
2170 gdb::unique_xmalloc_ptr<char> abs
2171 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2172
2173 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2174 filename = abs_storage.c_str ();
2175 }
2176
2177 /* First try the file name given in the section. If that doesn't
2178 work, try to use the build-id instead. */
2179 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2180 if (dwz_bfd != NULL)
2181 {
2182 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2183 dwz_bfd.reset (nullptr);
2184 }
2185
2186 if (dwz_bfd == NULL)
2187 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2188
2189 if (dwz_bfd == nullptr)
2190 {
2191 gdb::unique_xmalloc_ptr<char> alt_filename;
2192 const char *origname = bfd_get_filename (per_bfd->obfd);
2193
2194 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2195 buildid_len,
2196 origname,
2197 &alt_filename));
2198
2199 if (fd.get () >= 0)
2200 {
2201 /* File successfully retrieved from server. */
2202 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2203
2204 if (dwz_bfd == nullptr)
2205 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2206 alt_filename.get ());
2207 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2208 dwz_bfd.reset (nullptr);
2209 }
2210 }
2211
2212 if (dwz_bfd == NULL)
2213 error (_("could not find '.gnu_debugaltlink' file for %s"),
2214 bfd_get_filename (per_bfd->obfd));
2215
2216 std::unique_ptr<struct dwz_file> result
2217 (new struct dwz_file (std::move (dwz_bfd)));
2218
2219 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2220 result.get ());
2221
2222 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2223 per_bfd->dwz_file = std::move (result);
2224 return per_bfd->dwz_file.get ();
2225 }
2226 \f
2227 /* DWARF quick_symbols_functions support. */
2228
2229 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2230 unique line tables, so we maintain a separate table of all .debug_line
2231 derived entries to support the sharing.
2232 All the quick functions need is the list of file names. We discard the
2233 line_header when we're done and don't need to record it here. */
2234 struct quick_file_names
2235 {
2236 /* The data used to construct the hash key. */
2237 struct stmt_list_hash hash;
2238
2239 /* The number of entries in file_names, real_names. */
2240 unsigned int num_file_names;
2241
2242 /* The file names from the line table, after being run through
2243 file_full_name. */
2244 const char **file_names;
2245
2246 /* The file names from the line table after being run through
2247 gdb_realpath. These are computed lazily. */
2248 const char **real_names;
2249 };
2250
2251 /* When using the index (and thus not using psymtabs), each CU has an
2252 object of this type. This is used to hold information needed by
2253 the various "quick" methods. */
2254 struct dwarf2_per_cu_quick_data
2255 {
2256 /* The file table. This can be NULL if there was no file table
2257 or it's currently not read in.
2258 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2259 struct quick_file_names *file_names;
2260
2261 /* A temporary mark bit used when iterating over all CUs in
2262 expand_symtabs_matching. */
2263 unsigned int mark : 1;
2264
2265 /* True if we've tried to read the file table and found there isn't one.
2266 There will be no point in trying to read it again next time. */
2267 unsigned int no_file_data : 1;
2268 };
2269
2270 /* Utility hash function for a stmt_list_hash. */
2271
2272 static hashval_t
2273 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2274 {
2275 hashval_t v = 0;
2276
2277 if (stmt_list_hash->dwo_unit != NULL)
2278 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2279 v += to_underlying (stmt_list_hash->line_sect_off);
2280 return v;
2281 }
2282
2283 /* Utility equality function for a stmt_list_hash. */
2284
2285 static int
2286 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2287 const struct stmt_list_hash *rhs)
2288 {
2289 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2290 return 0;
2291 if (lhs->dwo_unit != NULL
2292 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2293 return 0;
2294
2295 return lhs->line_sect_off == rhs->line_sect_off;
2296 }
2297
2298 /* Hash function for a quick_file_names. */
2299
2300 static hashval_t
2301 hash_file_name_entry (const void *e)
2302 {
2303 const struct quick_file_names *file_data
2304 = (const struct quick_file_names *) e;
2305
2306 return hash_stmt_list_entry (&file_data->hash);
2307 }
2308
2309 /* Equality function for a quick_file_names. */
2310
2311 static int
2312 eq_file_name_entry (const void *a, const void *b)
2313 {
2314 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2315 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2316
2317 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2318 }
2319
2320 /* Delete function for a quick_file_names. */
2321
2322 static void
2323 delete_file_name_entry (void *e)
2324 {
2325 struct quick_file_names *file_data = (struct quick_file_names *) e;
2326 int i;
2327
2328 for (i = 0; i < file_data->num_file_names; ++i)
2329 {
2330 xfree ((void*) file_data->file_names[i]);
2331 if (file_data->real_names)
2332 xfree ((void*) file_data->real_names[i]);
2333 }
2334
2335 /* The space for the struct itself lives on the obstack, so we don't
2336 free it here. */
2337 }
2338
2339 /* Create a quick_file_names hash table. */
2340
2341 static htab_up
2342 create_quick_file_names_table (unsigned int nr_initial_entries)
2343 {
2344 return htab_up (htab_create_alloc (nr_initial_entries,
2345 hash_file_name_entry, eq_file_name_entry,
2346 delete_file_name_entry, xcalloc, xfree));
2347 }
2348
2349 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2350 function is unrelated to symtabs, symtab would have to be created afterwards.
2351 You should call age_cached_comp_units after processing the CU. */
2352
2353 static void
2354 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2355 bool skip_partial)
2356 {
2357 if (per_cu->is_debug_types)
2358 load_full_type_unit (per_cu, per_objfile);
2359 else
2360 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2361
2362 if (per_cu->cu == NULL)
2363 return; /* Dummy CU. */
2364
2365 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2366 }
2367
2368 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2369
2370 static void
2371 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2372 dwarf2_per_objfile *dwarf2_per_objfile,
2373 bool skip_partial)
2374 {
2375 /* Skip type_unit_groups, reading the type units they contain
2376 is handled elsewhere. */
2377 if (per_cu->type_unit_group_p ())
2378 return;
2379
2380 /* The destructor of dwarf2_queue_guard frees any entries left on
2381 the queue. After this point we're guaranteed to leave this function
2382 with the dwarf queue empty. */
2383 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2384
2385 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2386 {
2387 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
2388 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2389
2390 /* If we just loaded a CU from a DWO, and we're working with an index
2391 that may badly handle TUs, load all the TUs in that DWO as well.
2392 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2393 if (!per_cu->is_debug_types
2394 && per_cu->cu != NULL
2395 && per_cu->cu->dwo_unit != NULL
2396 && dwarf2_per_objfile->per_bfd->index_table != NULL
2397 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2398 /* DWP files aren't supported yet. */
2399 && get_dwp_file (dwarf2_per_objfile) == NULL)
2400 queue_and_load_all_dwo_tus (per_cu);
2401 }
2402
2403 process_queue (dwarf2_per_objfile);
2404
2405 /* Age the cache, releasing compilation units that have not
2406 been used recently. */
2407 age_cached_comp_units (dwarf2_per_objfile);
2408 }
2409
2410 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2411 the per-objfile for which this symtab is instantiated.
2412
2413 Returns the resulting symbol table. */
2414
2415 static struct compunit_symtab *
2416 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2417 dwarf2_per_objfile *dwarf2_per_objfile,
2418 bool skip_partial)
2419 {
2420 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2421
2422 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2423 {
2424 free_cached_comp_units freer (dwarf2_per_objfile);
2425 scoped_restore decrementer = increment_reading_symtab ();
2426 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2427 process_cu_includes (dwarf2_per_objfile);
2428 }
2429
2430 return dwarf2_per_objfile->get_symtab (per_cu);
2431 }
2432
2433 /* See declaration. */
2434
2435 dwarf2_per_cu_data *
2436 dwarf2_per_bfd::get_cutu (int index)
2437 {
2438 if (index >= this->all_comp_units.size ())
2439 {
2440 index -= this->all_comp_units.size ();
2441 gdb_assert (index < this->all_type_units.size ());
2442 return &this->all_type_units[index]->per_cu;
2443 }
2444
2445 return this->all_comp_units[index];
2446 }
2447
2448 /* See declaration. */
2449
2450 dwarf2_per_cu_data *
2451 dwarf2_per_bfd::get_cu (int index)
2452 {
2453 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2454
2455 return this->all_comp_units[index];
2456 }
2457
2458 /* See declaration. */
2459
2460 signatured_type *
2461 dwarf2_per_bfd::get_tu (int index)
2462 {
2463 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2464
2465 return this->all_type_units[index];
2466 }
2467
2468 /* See read.h. */
2469
2470 dwarf2_per_cu_data *
2471 dwarf2_per_bfd::allocate_per_cu ()
2472 {
2473 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2474 result->per_bfd = this;
2475 result->index = m_num_psymtabs++;
2476 return result;
2477 }
2478
2479 /* See read.h. */
2480
2481 signatured_type *
2482 dwarf2_per_bfd::allocate_signatured_type ()
2483 {
2484 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2485 result->per_cu.per_bfd = this;
2486 result->per_cu.index = m_num_psymtabs++;
2487 return result;
2488 }
2489
2490 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2491 obstack, and constructed with the specified field values. */
2492
2493 static dwarf2_per_cu_data *
2494 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2495 struct dwarf2_section_info *section,
2496 int is_dwz,
2497 sect_offset sect_off, ULONGEST length)
2498 {
2499 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2500 the_cu->sect_off = sect_off;
2501 the_cu->length = length;
2502 the_cu->section = section;
2503 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2504 struct dwarf2_per_cu_quick_data);
2505 the_cu->is_dwz = is_dwz;
2506 return the_cu;
2507 }
2508
2509 /* A helper for create_cus_from_index that handles a given list of
2510 CUs. */
2511
2512 static void
2513 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2514 const gdb_byte *cu_list, offset_type n_elements,
2515 struct dwarf2_section_info *section,
2516 int is_dwz)
2517 {
2518 for (offset_type i = 0; i < n_elements; i += 2)
2519 {
2520 gdb_static_assert (sizeof (ULONGEST) >= 8);
2521
2522 sect_offset sect_off
2523 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2524 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2525 cu_list += 2 * 8;
2526
2527 dwarf2_per_cu_data *per_cu
2528 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2529 sect_off, length);
2530 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2531 }
2532 }
2533
2534 /* Read the CU list from the mapped index, and use it to create all
2535 the CU objects for this objfile. */
2536
2537 static void
2538 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2539 const gdb_byte *cu_list, offset_type cu_list_elements,
2540 const gdb_byte *dwz_list, offset_type dwz_elements)
2541 {
2542 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2543 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2544 ((cu_list_elements + dwz_elements) / 2);
2545
2546 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2547 &dwarf2_per_objfile->per_bfd->info, 0);
2548
2549 if (dwz_elements == 0)
2550 return;
2551
2552 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2553 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2554 &dwz->info, 1);
2555 }
2556
2557 /* Create the signatured type hash table from the index. */
2558
2559 static void
2560 create_signatured_type_table_from_index
2561 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2562 struct dwarf2_section_info *section,
2563 const gdb_byte *bytes,
2564 offset_type elements)
2565 {
2566 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2567 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2568
2569 htab_up sig_types_hash = allocate_signatured_type_table ();
2570
2571 for (offset_type i = 0; i < elements; i += 3)
2572 {
2573 struct signatured_type *sig_type;
2574 ULONGEST signature;
2575 void **slot;
2576 cu_offset type_offset_in_tu;
2577
2578 gdb_static_assert (sizeof (ULONGEST) >= 8);
2579 sect_offset sect_off
2580 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2581 type_offset_in_tu
2582 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2583 BFD_ENDIAN_LITTLE);
2584 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2585 bytes += 3 * 8;
2586
2587 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2588 sig_type->signature = signature;
2589 sig_type->type_offset_in_tu = type_offset_in_tu;
2590 sig_type->per_cu.is_debug_types = 1;
2591 sig_type->per_cu.section = section;
2592 sig_type->per_cu.sect_off = sect_off;
2593 sig_type->per_cu.v.quick
2594 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2595 struct dwarf2_per_cu_quick_data);
2596
2597 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2598 *slot = sig_type;
2599
2600 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2601 }
2602
2603 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2604 }
2605
2606 /* Create the signatured type hash table from .debug_names. */
2607
2608 static void
2609 create_signatured_type_table_from_debug_names
2610 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2611 const mapped_debug_names &map,
2612 struct dwarf2_section_info *section,
2613 struct dwarf2_section_info *abbrev_section)
2614 {
2615 struct objfile *objfile = dwarf2_per_objfile->objfile;
2616
2617 section->read (objfile);
2618 abbrev_section->read (objfile);
2619
2620 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2621 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2622
2623 htab_up sig_types_hash = allocate_signatured_type_table ();
2624
2625 for (uint32_t i = 0; i < map.tu_count; ++i)
2626 {
2627 struct signatured_type *sig_type;
2628 void **slot;
2629
2630 sect_offset sect_off
2631 = (sect_offset) (extract_unsigned_integer
2632 (map.tu_table_reordered + i * map.offset_size,
2633 map.offset_size,
2634 map.dwarf5_byte_order));
2635
2636 comp_unit_head cu_header;
2637 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2638 abbrev_section,
2639 section->buffer + to_underlying (sect_off),
2640 rcuh_kind::TYPE);
2641
2642 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2643 sig_type->signature = cu_header.signature;
2644 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2645 sig_type->per_cu.is_debug_types = 1;
2646 sig_type->per_cu.section = section;
2647 sig_type->per_cu.sect_off = sect_off;
2648 sig_type->per_cu.v.quick
2649 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2650 struct dwarf2_per_cu_quick_data);
2651
2652 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2653 *slot = sig_type;
2654
2655 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2656 }
2657
2658 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2659 }
2660
2661 /* Read the address map data from the mapped index, and use it to
2662 populate the objfile's psymtabs_addrmap. */
2663
2664 static void
2665 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2666 struct mapped_index *index)
2667 {
2668 struct objfile *objfile = dwarf2_per_objfile->objfile;
2669 struct gdbarch *gdbarch = objfile->arch ();
2670 const gdb_byte *iter, *end;
2671 struct addrmap *mutable_map;
2672 CORE_ADDR baseaddr;
2673
2674 auto_obstack temp_obstack;
2675
2676 mutable_map = addrmap_create_mutable (&temp_obstack);
2677
2678 iter = index->address_table.data ();
2679 end = iter + index->address_table.size ();
2680
2681 baseaddr = objfile->text_section_offset ();
2682
2683 while (iter < end)
2684 {
2685 ULONGEST hi, lo, cu_index;
2686 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2687 iter += 8;
2688 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2689 iter += 8;
2690 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2691 iter += 4;
2692
2693 if (lo > hi)
2694 {
2695 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2696 hex_string (lo), hex_string (hi));
2697 continue;
2698 }
2699
2700 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2701 {
2702 complaint (_(".gdb_index address table has invalid CU number %u"),
2703 (unsigned) cu_index);
2704 continue;
2705 }
2706
2707 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2708 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2709 addrmap_set_empty (mutable_map, lo, hi - 1,
2710 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2711 }
2712
2713 objfile->partial_symtabs->psymtabs_addrmap
2714 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2715 }
2716
2717 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2718 populate the objfile's psymtabs_addrmap. */
2719
2720 static void
2721 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2722 struct dwarf2_section_info *section)
2723 {
2724 struct objfile *objfile = dwarf2_per_objfile->objfile;
2725 bfd *abfd = objfile->obfd;
2726 struct gdbarch *gdbarch = objfile->arch ();
2727 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2728
2729 auto_obstack temp_obstack;
2730 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2731
2732 std::unordered_map<sect_offset,
2733 dwarf2_per_cu_data *,
2734 gdb::hash_enum<sect_offset>>
2735 debug_info_offset_to_per_cu;
2736 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2737 {
2738 const auto insertpair
2739 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2740 if (!insertpair.second)
2741 {
2742 warning (_("Section .debug_aranges in %s has duplicate "
2743 "debug_info_offset %s, ignoring .debug_aranges."),
2744 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2745 return;
2746 }
2747 }
2748
2749 section->read (objfile);
2750
2751 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2752
2753 const gdb_byte *addr = section->buffer;
2754
2755 while (addr < section->buffer + section->size)
2756 {
2757 const gdb_byte *const entry_addr = addr;
2758 unsigned int bytes_read;
2759
2760 const LONGEST entry_length = read_initial_length (abfd, addr,
2761 &bytes_read);
2762 addr += bytes_read;
2763
2764 const gdb_byte *const entry_end = addr + entry_length;
2765 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2766 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2767 if (addr + entry_length > section->buffer + section->size)
2768 {
2769 warning (_("Section .debug_aranges in %s entry at offset %s "
2770 "length %s exceeds section length %s, "
2771 "ignoring .debug_aranges."),
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer),
2774 plongest (bytes_read + entry_length),
2775 pulongest (section->size));
2776 return;
2777 }
2778
2779 /* The version number. */
2780 const uint16_t version = read_2_bytes (abfd, addr);
2781 addr += 2;
2782 if (version != 2)
2783 {
2784 warning (_("Section .debug_aranges in %s entry at offset %s "
2785 "has unsupported version %d, ignoring .debug_aranges."),
2786 objfile_name (objfile),
2787 plongest (entry_addr - section->buffer), version);
2788 return;
2789 }
2790
2791 const uint64_t debug_info_offset
2792 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2793 addr += offset_size;
2794 const auto per_cu_it
2795 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2796 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2797 {
2798 warning (_("Section .debug_aranges in %s entry at offset %s "
2799 "debug_info_offset %s does not exists, "
2800 "ignoring .debug_aranges."),
2801 objfile_name (objfile),
2802 plongest (entry_addr - section->buffer),
2803 pulongest (debug_info_offset));
2804 return;
2805 }
2806 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2807
2808 const uint8_t address_size = *addr++;
2809 if (address_size < 1 || address_size > 8)
2810 {
2811 warning (_("Section .debug_aranges in %s entry at offset %s "
2812 "address_size %u is invalid, ignoring .debug_aranges."),
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer), address_size);
2815 return;
2816 }
2817
2818 const uint8_t segment_selector_size = *addr++;
2819 if (segment_selector_size != 0)
2820 {
2821 warning (_("Section .debug_aranges in %s entry at offset %s "
2822 "segment_selector_size %u is not supported, "
2823 "ignoring .debug_aranges."),
2824 objfile_name (objfile),
2825 plongest (entry_addr - section->buffer),
2826 segment_selector_size);
2827 return;
2828 }
2829
2830 /* Must pad to an alignment boundary that is twice the address
2831 size. It is undocumented by the DWARF standard but GCC does
2832 use it. */
2833 for (size_t padding = ((-(addr - section->buffer))
2834 & (2 * address_size - 1));
2835 padding > 0; padding--)
2836 if (*addr++ != 0)
2837 {
2838 warning (_("Section .debug_aranges in %s entry at offset %s "
2839 "padding is not zero, ignoring .debug_aranges."),
2840 objfile_name (objfile),
2841 plongest (entry_addr - section->buffer));
2842 return;
2843 }
2844
2845 for (;;)
2846 {
2847 if (addr + 2 * address_size > entry_end)
2848 {
2849 warning (_("Section .debug_aranges in %s entry at offset %s "
2850 "address list is not properly terminated, "
2851 "ignoring .debug_aranges."),
2852 objfile_name (objfile),
2853 plongest (entry_addr - section->buffer));
2854 return;
2855 }
2856 ULONGEST start = extract_unsigned_integer (addr, address_size,
2857 dwarf5_byte_order);
2858 addr += address_size;
2859 ULONGEST length = extract_unsigned_integer (addr, address_size,
2860 dwarf5_byte_order);
2861 addr += address_size;
2862 if (start == 0 && length == 0)
2863 break;
2864 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2865 {
2866 /* Symbol was eliminated due to a COMDAT group. */
2867 continue;
2868 }
2869 ULONGEST end = start + length;
2870 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2871 - baseaddr);
2872 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2873 - baseaddr);
2874 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2875 }
2876 }
2877
2878 objfile->partial_symtabs->psymtabs_addrmap
2879 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2880 }
2881
2882 /* Find a slot in the mapped index INDEX for the object named NAME.
2883 If NAME is found, set *VEC_OUT to point to the CU vector in the
2884 constant pool and return true. If NAME cannot be found, return
2885 false. */
2886
2887 static bool
2888 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2889 offset_type **vec_out)
2890 {
2891 offset_type hash;
2892 offset_type slot, step;
2893 int (*cmp) (const char *, const char *);
2894
2895 gdb::unique_xmalloc_ptr<char> without_params;
2896 if (current_language->la_language == language_cplus
2897 || current_language->la_language == language_fortran
2898 || current_language->la_language == language_d)
2899 {
2900 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2901 not contain any. */
2902
2903 if (strchr (name, '(') != NULL)
2904 {
2905 without_params = cp_remove_params (name);
2906
2907 if (without_params != NULL)
2908 name = without_params.get ();
2909 }
2910 }
2911
2912 /* Index version 4 did not support case insensitive searches. But the
2913 indices for case insensitive languages are built in lowercase, therefore
2914 simulate our NAME being searched is also lowercased. */
2915 hash = mapped_index_string_hash ((index->version == 4
2916 && case_sensitivity == case_sensitive_off
2917 ? 5 : index->version),
2918 name);
2919
2920 slot = hash & (index->symbol_table.size () - 1);
2921 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2922 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2923
2924 for (;;)
2925 {
2926 const char *str;
2927
2928 const auto &bucket = index->symbol_table[slot];
2929 if (bucket.name == 0 && bucket.vec == 0)
2930 return false;
2931
2932 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2933 if (!cmp (name, str))
2934 {
2935 *vec_out = (offset_type *) (index->constant_pool
2936 + MAYBE_SWAP (bucket.vec));
2937 return true;
2938 }
2939
2940 slot = (slot + step) & (index->symbol_table.size () - 1);
2941 }
2942 }
2943
2944 /* A helper function that reads the .gdb_index from BUFFER and fills
2945 in MAP. FILENAME is the name of the file containing the data;
2946 it is used for error reporting. DEPRECATED_OK is true if it is
2947 ok to use deprecated sections.
2948
2949 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2950 out parameters that are filled in with information about the CU and
2951 TU lists in the section.
2952
2953 Returns true if all went well, false otherwise. */
2954
2955 static bool
2956 read_gdb_index_from_buffer (const char *filename,
2957 bool deprecated_ok,
2958 gdb::array_view<const gdb_byte> buffer,
2959 struct mapped_index *map,
2960 const gdb_byte **cu_list,
2961 offset_type *cu_list_elements,
2962 const gdb_byte **types_list,
2963 offset_type *types_list_elements)
2964 {
2965 const gdb_byte *addr = &buffer[0];
2966
2967 /* Version check. */
2968 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2969 /* Versions earlier than 3 emitted every copy of a psymbol. This
2970 causes the index to behave very poorly for certain requests. Version 3
2971 contained incomplete addrmap. So, it seems better to just ignore such
2972 indices. */
2973 if (version < 4)
2974 {
2975 static int warning_printed = 0;
2976 if (!warning_printed)
2977 {
2978 warning (_("Skipping obsolete .gdb_index section in %s."),
2979 filename);
2980 warning_printed = 1;
2981 }
2982 return 0;
2983 }
2984 /* Index version 4 uses a different hash function than index version
2985 5 and later.
2986
2987 Versions earlier than 6 did not emit psymbols for inlined
2988 functions. Using these files will cause GDB not to be able to
2989 set breakpoints on inlined functions by name, so we ignore these
2990 indices unless the user has done
2991 "set use-deprecated-index-sections on". */
2992 if (version < 6 && !deprecated_ok)
2993 {
2994 static int warning_printed = 0;
2995 if (!warning_printed)
2996 {
2997 warning (_("\
2998 Skipping deprecated .gdb_index section in %s.\n\
2999 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3000 to use the section anyway."),
3001 filename);
3002 warning_printed = 1;
3003 }
3004 return 0;
3005 }
3006 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3007 of the TU (for symbols coming from TUs),
3008 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3009 Plus gold-generated indices can have duplicate entries for global symbols,
3010 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3011 These are just performance bugs, and we can't distinguish gdb-generated
3012 indices from gold-generated ones, so issue no warning here. */
3013
3014 /* Indexes with higher version than the one supported by GDB may be no
3015 longer backward compatible. */
3016 if (version > 8)
3017 return 0;
3018
3019 map->version = version;
3020
3021 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3022
3023 int i = 0;
3024 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3025 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3026 / 8);
3027 ++i;
3028
3029 *types_list = addr + MAYBE_SWAP (metadata[i]);
3030 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3031 - MAYBE_SWAP (metadata[i]))
3032 / 8);
3033 ++i;
3034
3035 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3036 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3037 map->address_table
3038 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3039 ++i;
3040
3041 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3042 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3043 map->symbol_table
3044 = gdb::array_view<mapped_index::symbol_table_slot>
3045 ((mapped_index::symbol_table_slot *) symbol_table,
3046 (mapped_index::symbol_table_slot *) symbol_table_end);
3047
3048 ++i;
3049 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3050
3051 return 1;
3052 }
3053
3054 /* Callback types for dwarf2_read_gdb_index. */
3055
3056 typedef gdb::function_view
3057 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3058 get_gdb_index_contents_ftype;
3059 typedef gdb::function_view
3060 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3061 get_gdb_index_contents_dwz_ftype;
3062
3063 /* Read .gdb_index. If everything went ok, initialize the "quick"
3064 elements of all the CUs and return 1. Otherwise, return 0. */
3065
3066 static int
3067 dwarf2_read_gdb_index
3068 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3069 get_gdb_index_contents_ftype get_gdb_index_contents,
3070 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3071 {
3072 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3073 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3074 struct dwz_file *dwz;
3075 struct objfile *objfile = dwarf2_per_objfile->objfile;
3076
3077 gdb::array_view<const gdb_byte> main_index_contents
3078 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3079
3080 if (main_index_contents.empty ())
3081 return 0;
3082
3083 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3084 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3085 use_deprecated_index_sections,
3086 main_index_contents, map.get (), &cu_list,
3087 &cu_list_elements, &types_list,
3088 &types_list_elements))
3089 return 0;
3090
3091 /* Don't use the index if it's empty. */
3092 if (map->symbol_table.empty ())
3093 return 0;
3094
3095 /* If there is a .dwz file, read it so we can get its CU list as
3096 well. */
3097 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3098 if (dwz != NULL)
3099 {
3100 struct mapped_index dwz_map;
3101 const gdb_byte *dwz_types_ignore;
3102 offset_type dwz_types_elements_ignore;
3103
3104 gdb::array_view<const gdb_byte> dwz_index_content
3105 = get_gdb_index_contents_dwz (objfile, dwz);
3106
3107 if (dwz_index_content.empty ())
3108 return 0;
3109
3110 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3111 1, dwz_index_content, &dwz_map,
3112 &dwz_list, &dwz_list_elements,
3113 &dwz_types_ignore,
3114 &dwz_types_elements_ignore))
3115 {
3116 warning (_("could not read '.gdb_index' section from %s; skipping"),
3117 bfd_get_filename (dwz->dwz_bfd.get ()));
3118 return 0;
3119 }
3120 }
3121
3122 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3123 dwz_list, dwz_list_elements);
3124
3125 if (types_list_elements)
3126 {
3127 /* We can only handle a single .debug_types when we have an
3128 index. */
3129 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3130 return 0;
3131
3132 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3133
3134 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3135 types_list, types_list_elements);
3136 }
3137
3138 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3139
3140 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3141 dwarf2_per_objfile->per_bfd->using_index = 1;
3142 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3143 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3144
3145 return 1;
3146 }
3147
3148 /* die_reader_func for dw2_get_file_names. */
3149
3150 static void
3151 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3152 const gdb_byte *info_ptr,
3153 struct die_info *comp_unit_die)
3154 {
3155 struct dwarf2_cu *cu = reader->cu;
3156 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3157 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3158 struct dwarf2_per_cu_data *lh_cu;
3159 struct attribute *attr;
3160 void **slot;
3161 struct quick_file_names *qfn;
3162
3163 gdb_assert (! this_cu->is_debug_types);
3164
3165 /* Our callers never want to match partial units -- instead they
3166 will match the enclosing full CU. */
3167 if (comp_unit_die->tag == DW_TAG_partial_unit)
3168 {
3169 this_cu->v.quick->no_file_data = 1;
3170 return;
3171 }
3172
3173 lh_cu = this_cu;
3174 slot = NULL;
3175
3176 line_header_up lh;
3177 sect_offset line_offset {};
3178
3179 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3180 if (attr != nullptr)
3181 {
3182 struct quick_file_names find_entry;
3183
3184 line_offset = (sect_offset) DW_UNSND (attr);
3185
3186 /* We may have already read in this line header (TU line header sharing).
3187 If we have we're done. */
3188 find_entry.hash.dwo_unit = cu->dwo_unit;
3189 find_entry.hash.line_sect_off = line_offset;
3190 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3191 &find_entry, INSERT);
3192 if (*slot != NULL)
3193 {
3194 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3195 return;
3196 }
3197
3198 lh = dwarf_decode_line_header (line_offset, cu);
3199 }
3200 if (lh == NULL)
3201 {
3202 lh_cu->v.quick->no_file_data = 1;
3203 return;
3204 }
3205
3206 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3207 qfn->hash.dwo_unit = cu->dwo_unit;
3208 qfn->hash.line_sect_off = line_offset;
3209 gdb_assert (slot != NULL);
3210 *slot = qfn;
3211
3212 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3213
3214 int offset = 0;
3215 if (strcmp (fnd.name, "<unknown>") != 0)
3216 ++offset;
3217
3218 qfn->num_file_names = offset + lh->file_names_size ();
3219 qfn->file_names =
3220 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3221 qfn->num_file_names);
3222 if (offset != 0)
3223 qfn->file_names[0] = xstrdup (fnd.name);
3224 for (int i = 0; i < lh->file_names_size (); ++i)
3225 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3226 fnd.comp_dir).release ();
3227 qfn->real_names = NULL;
3228
3229 lh_cu->v.quick->file_names = qfn;
3230 }
3231
3232 /* A helper for the "quick" functions which attempts to read the line
3233 table for THIS_CU. */
3234
3235 static struct quick_file_names *
3236 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3237 dwarf2_per_objfile *per_objfile)
3238 {
3239 /* This should never be called for TUs. */
3240 gdb_assert (! this_cu->is_debug_types);
3241 /* Nor type unit groups. */
3242 gdb_assert (! this_cu->type_unit_group_p ());
3243
3244 if (this_cu->v.quick->file_names != NULL)
3245 return this_cu->v.quick->file_names;
3246 /* If we know there is no line data, no point in looking again. */
3247 if (this_cu->v.quick->no_file_data)
3248 return NULL;
3249
3250 cutu_reader reader (this_cu, per_objfile);
3251 if (!reader.dummy_p)
3252 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3253
3254 if (this_cu->v.quick->no_file_data)
3255 return NULL;
3256 return this_cu->v.quick->file_names;
3257 }
3258
3259 /* A helper for the "quick" functions which computes and caches the
3260 real path for a given file name from the line table. */
3261
3262 static const char *
3263 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3264 struct quick_file_names *qfn, int index)
3265 {
3266 if (qfn->real_names == NULL)
3267 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3268 qfn->num_file_names, const char *);
3269
3270 if (qfn->real_names[index] == NULL)
3271 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3272
3273 return qfn->real_names[index];
3274 }
3275
3276 static struct symtab *
3277 dw2_find_last_source_symtab (struct objfile *objfile)
3278 {
3279 struct dwarf2_per_objfile *dwarf2_per_objfile
3280 = get_dwarf2_per_objfile (objfile);
3281 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3282 compunit_symtab *cust
3283 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3284
3285 if (cust == NULL)
3286 return NULL;
3287
3288 return compunit_primary_filetab (cust);
3289 }
3290
3291 /* Traversal function for dw2_forget_cached_source_info. */
3292
3293 static int
3294 dw2_free_cached_file_names (void **slot, void *info)
3295 {
3296 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3297
3298 if (file_data->real_names)
3299 {
3300 int i;
3301
3302 for (i = 0; i < file_data->num_file_names; ++i)
3303 {
3304 xfree ((void*) file_data->real_names[i]);
3305 file_data->real_names[i] = NULL;
3306 }
3307 }
3308
3309 return 1;
3310 }
3311
3312 static void
3313 dw2_forget_cached_source_info (struct objfile *objfile)
3314 {
3315 struct dwarf2_per_objfile *dwarf2_per_objfile
3316 = get_dwarf2_per_objfile (objfile);
3317
3318 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3319 dw2_free_cached_file_names, NULL);
3320 }
3321
3322 /* Helper function for dw2_map_symtabs_matching_filename that expands
3323 the symtabs and calls the iterator. */
3324
3325 static int
3326 dw2_map_expand_apply (struct objfile *objfile,
3327 struct dwarf2_per_cu_data *per_cu,
3328 const char *name, const char *real_path,
3329 gdb::function_view<bool (symtab *)> callback)
3330 {
3331 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3332
3333 /* Don't visit already-expanded CUs. */
3334 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3335 if (per_objfile->symtab_set_p (per_cu))
3336 return 0;
3337
3338 /* This may expand more than one symtab, and we want to iterate over
3339 all of them. */
3340 dw2_instantiate_symtab (per_cu, per_objfile, false);
3341
3342 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3343 last_made, callback);
3344 }
3345
3346 /* Implementation of the map_symtabs_matching_filename method. */
3347
3348 static bool
3349 dw2_map_symtabs_matching_filename
3350 (struct objfile *objfile, const char *name, const char *real_path,
3351 gdb::function_view<bool (symtab *)> callback)
3352 {
3353 const char *name_basename = lbasename (name);
3354 struct dwarf2_per_objfile *dwarf2_per_objfile
3355 = get_dwarf2_per_objfile (objfile);
3356
3357 /* The rule is CUs specify all the files, including those used by
3358 any TU, so there's no need to scan TUs here. */
3359
3360 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3361 {
3362 /* We only need to look at symtabs not already expanded. */
3363 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3364 continue;
3365
3366 quick_file_names *file_data
3367 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3368 if (file_data == NULL)
3369 continue;
3370
3371 for (int j = 0; j < file_data->num_file_names; ++j)
3372 {
3373 const char *this_name = file_data->file_names[j];
3374 const char *this_real_name;
3375
3376 if (compare_filenames_for_search (this_name, name))
3377 {
3378 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3379 callback))
3380 return true;
3381 continue;
3382 }
3383
3384 /* Before we invoke realpath, which can get expensive when many
3385 files are involved, do a quick comparison of the basenames. */
3386 if (! basenames_may_differ
3387 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3388 continue;
3389
3390 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3391 file_data, j);
3392 if (compare_filenames_for_search (this_real_name, name))
3393 {
3394 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3395 callback))
3396 return true;
3397 continue;
3398 }
3399
3400 if (real_path != NULL)
3401 {
3402 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3403 gdb_assert (IS_ABSOLUTE_PATH (name));
3404 if (this_real_name != NULL
3405 && FILENAME_CMP (real_path, this_real_name) == 0)
3406 {
3407 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3408 callback))
3409 return true;
3410 continue;
3411 }
3412 }
3413 }
3414 }
3415
3416 return false;
3417 }
3418
3419 /* Struct used to manage iterating over all CUs looking for a symbol. */
3420
3421 struct dw2_symtab_iterator
3422 {
3423 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3424 struct dwarf2_per_objfile *dwarf2_per_objfile;
3425 /* If set, only look for symbols that match that block. Valid values are
3426 GLOBAL_BLOCK and STATIC_BLOCK. */
3427 gdb::optional<block_enum> block_index;
3428 /* The kind of symbol we're looking for. */
3429 domain_enum domain;
3430 /* The list of CUs from the index entry of the symbol,
3431 or NULL if not found. */
3432 offset_type *vec;
3433 /* The next element in VEC to look at. */
3434 int next;
3435 /* The number of elements in VEC, or zero if there is no match. */
3436 int length;
3437 /* Have we seen a global version of the symbol?
3438 If so we can ignore all further global instances.
3439 This is to work around gold/15646, inefficient gold-generated
3440 indices. */
3441 int global_seen;
3442 };
3443
3444 /* Initialize the index symtab iterator ITER. */
3445
3446 static void
3447 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3448 struct dwarf2_per_objfile *dwarf2_per_objfile,
3449 gdb::optional<block_enum> block_index,
3450 domain_enum domain,
3451 const char *name)
3452 {
3453 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3454 iter->block_index = block_index;
3455 iter->domain = domain;
3456 iter->next = 0;
3457 iter->global_seen = 0;
3458
3459 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3460
3461 /* index is NULL if OBJF_READNOW. */
3462 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3463 iter->length = MAYBE_SWAP (*iter->vec);
3464 else
3465 {
3466 iter->vec = NULL;
3467 iter->length = 0;
3468 }
3469 }
3470
3471 /* Return the next matching CU or NULL if there are no more. */
3472
3473 static struct dwarf2_per_cu_data *
3474 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3475 {
3476 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3477
3478 for ( ; iter->next < iter->length; ++iter->next)
3479 {
3480 offset_type cu_index_and_attrs =
3481 MAYBE_SWAP (iter->vec[iter->next + 1]);
3482 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3483 gdb_index_symbol_kind symbol_kind =
3484 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3485 /* Only check the symbol attributes if they're present.
3486 Indices prior to version 7 don't record them,
3487 and indices >= 7 may elide them for certain symbols
3488 (gold does this). */
3489 int attrs_valid =
3490 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3491 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3492
3493 /* Don't crash on bad data. */
3494 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3495 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3496 {
3497 complaint (_(".gdb_index entry has bad CU index"
3498 " [in module %s]"),
3499 objfile_name (dwarf2_per_objfile->objfile));
3500 continue;
3501 }
3502
3503 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3504
3505 /* Skip if already read in. */
3506 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3507 continue;
3508
3509 /* Check static vs global. */
3510 if (attrs_valid)
3511 {
3512 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3513
3514 if (iter->block_index.has_value ())
3515 {
3516 bool want_static = *iter->block_index == STATIC_BLOCK;
3517
3518 if (is_static != want_static)
3519 continue;
3520 }
3521
3522 /* Work around gold/15646. */
3523 if (!is_static && iter->global_seen)
3524 continue;
3525 if (!is_static)
3526 iter->global_seen = 1;
3527 }
3528
3529 /* Only check the symbol's kind if it has one. */
3530 if (attrs_valid)
3531 {
3532 switch (iter->domain)
3533 {
3534 case VAR_DOMAIN:
3535 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3536 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3537 /* Some types are also in VAR_DOMAIN. */
3538 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3539 continue;
3540 break;
3541 case STRUCT_DOMAIN:
3542 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3543 continue;
3544 break;
3545 case LABEL_DOMAIN:
3546 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3547 continue;
3548 break;
3549 case MODULE_DOMAIN:
3550 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3551 continue;
3552 break;
3553 default:
3554 break;
3555 }
3556 }
3557
3558 ++iter->next;
3559 return per_cu;
3560 }
3561
3562 return NULL;
3563 }
3564
3565 static struct compunit_symtab *
3566 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3567 const char *name, domain_enum domain)
3568 {
3569 struct compunit_symtab *stab_best = NULL;
3570 struct dwarf2_per_objfile *dwarf2_per_objfile
3571 = get_dwarf2_per_objfile (objfile);
3572
3573 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3574
3575 struct dw2_symtab_iterator iter;
3576 struct dwarf2_per_cu_data *per_cu;
3577
3578 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3579
3580 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3581 {
3582 struct symbol *sym, *with_opaque = NULL;
3583 struct compunit_symtab *stab
3584 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3585 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3586 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3587
3588 sym = block_find_symbol (block, name, domain,
3589 block_find_non_opaque_type_preferred,
3590 &with_opaque);
3591
3592 /* Some caution must be observed with overloaded functions
3593 and methods, since the index will not contain any overload
3594 information (but NAME might contain it). */
3595
3596 if (sym != NULL
3597 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3598 return stab;
3599 if (with_opaque != NULL
3600 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3601 stab_best = stab;
3602
3603 /* Keep looking through other CUs. */
3604 }
3605
3606 return stab_best;
3607 }
3608
3609 static void
3610 dw2_print_stats (struct objfile *objfile)
3611 {
3612 struct dwarf2_per_objfile *dwarf2_per_objfile
3613 = get_dwarf2_per_objfile (objfile);
3614 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3615 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3616 int count = 0;
3617
3618 for (int i = 0; i < total; ++i)
3619 {
3620 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3621
3622 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3623 ++count;
3624 }
3625 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3626 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3627 }
3628
3629 /* This dumps minimal information about the index.
3630 It is called via "mt print objfiles".
3631 One use is to verify .gdb_index has been loaded by the
3632 gdb.dwarf2/gdb-index.exp testcase. */
3633
3634 static void
3635 dw2_dump (struct objfile *objfile)
3636 {
3637 struct dwarf2_per_objfile *dwarf2_per_objfile
3638 = get_dwarf2_per_objfile (objfile);
3639
3640 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3641 printf_filtered (".gdb_index:");
3642 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3643 {
3644 printf_filtered (" version %d\n",
3645 dwarf2_per_objfile->per_bfd->index_table->version);
3646 }
3647 else
3648 printf_filtered (" faked for \"readnow\"\n");
3649 printf_filtered ("\n");
3650 }
3651
3652 static void
3653 dw2_expand_symtabs_for_function (struct objfile *objfile,
3654 const char *func_name)
3655 {
3656 struct dwarf2_per_objfile *dwarf2_per_objfile
3657 = get_dwarf2_per_objfile (objfile);
3658
3659 struct dw2_symtab_iterator iter;
3660 struct dwarf2_per_cu_data *per_cu;
3661
3662 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3663
3664 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3665 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3666
3667 }
3668
3669 static void
3670 dw2_expand_all_symtabs (struct objfile *objfile)
3671 {
3672 struct dwarf2_per_objfile *dwarf2_per_objfile
3673 = get_dwarf2_per_objfile (objfile);
3674 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3675 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3676
3677 for (int i = 0; i < total_units; ++i)
3678 {
3679 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3680
3681 /* We don't want to directly expand a partial CU, because if we
3682 read it with the wrong language, then assertion failures can
3683 be triggered later on. See PR symtab/23010. So, tell
3684 dw2_instantiate_symtab to skip partial CUs -- any important
3685 partial CU will be read via DW_TAG_imported_unit anyway. */
3686 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3687 }
3688 }
3689
3690 static void
3691 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3692 const char *fullname)
3693 {
3694 struct dwarf2_per_objfile *dwarf2_per_objfile
3695 = get_dwarf2_per_objfile (objfile);
3696
3697 /* We don't need to consider type units here.
3698 This is only called for examining code, e.g. expand_line_sal.
3699 There can be an order of magnitude (or more) more type units
3700 than comp units, and we avoid them if we can. */
3701
3702 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3703 {
3704 /* We only need to look at symtabs not already expanded. */
3705 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3706 continue;
3707
3708 quick_file_names *file_data
3709 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3710 if (file_data == NULL)
3711 continue;
3712
3713 for (int j = 0; j < file_data->num_file_names; ++j)
3714 {
3715 const char *this_fullname = file_data->file_names[j];
3716
3717 if (filename_cmp (this_fullname, fullname) == 0)
3718 {
3719 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3720 break;
3721 }
3722 }
3723 }
3724 }
3725
3726 static void
3727 dw2_expand_symtabs_matching_symbol
3728 (mapped_index_base &index,
3729 const lookup_name_info &lookup_name_in,
3730 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3731 enum search_domain kind,
3732 gdb::function_view<bool (offset_type)> match_callback);
3733
3734 static void
3735 dw2_expand_symtabs_matching_one
3736 (dwarf2_per_cu_data *per_cu,
3737 dwarf2_per_objfile *per_objfile,
3738 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3739 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3740
3741 static void
3742 dw2_map_matching_symbols
3743 (struct objfile *objfile,
3744 const lookup_name_info &name, domain_enum domain,
3745 int global,
3746 gdb::function_view<symbol_found_callback_ftype> callback,
3747 symbol_compare_ftype *ordered_compare)
3748 {
3749 /* Used for Ada. */
3750 struct dwarf2_per_objfile *dwarf2_per_objfile
3751 = get_dwarf2_per_objfile (objfile);
3752
3753 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3754
3755 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3756 {
3757 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3758 here though if the current language is Ada for a non-Ada objfile
3759 using GNU index. */
3760 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3761
3762 const char *match_name = name.ada ().lookup_name ().c_str ();
3763 auto matcher = [&] (const char *symname)
3764 {
3765 if (ordered_compare == nullptr)
3766 return true;
3767 return ordered_compare (symname, match_name) == 0;
3768 };
3769
3770 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3771 [&] (offset_type namei)
3772 {
3773 struct dw2_symtab_iterator iter;
3774 struct dwarf2_per_cu_data *per_cu;
3775
3776 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3777 match_name);
3778 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3779 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3780 nullptr);
3781 return true;
3782 });
3783 }
3784 else
3785 {
3786 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3787 proceed assuming all symtabs have been read in. */
3788 }
3789
3790 for (compunit_symtab *cust : objfile->compunits ())
3791 {
3792 const struct block *block;
3793
3794 if (cust == NULL)
3795 continue;
3796 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3797 if (!iterate_over_symbols_terminated (block, name,
3798 domain, callback))
3799 return;
3800 }
3801 }
3802
3803 /* Starting from a search name, return the string that finds the upper
3804 bound of all strings that start with SEARCH_NAME in a sorted name
3805 list. Returns the empty string to indicate that the upper bound is
3806 the end of the list. */
3807
3808 static std::string
3809 make_sort_after_prefix_name (const char *search_name)
3810 {
3811 /* When looking to complete "func", we find the upper bound of all
3812 symbols that start with "func" by looking for where we'd insert
3813 the closest string that would follow "func" in lexicographical
3814 order. Usually, that's "func"-with-last-character-incremented,
3815 i.e. "fund". Mind non-ASCII characters, though. Usually those
3816 will be UTF-8 multi-byte sequences, but we can't be certain.
3817 Especially mind the 0xff character, which is a valid character in
3818 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3819 rule out compilers allowing it in identifiers. Note that
3820 conveniently, strcmp/strcasecmp are specified to compare
3821 characters interpreted as unsigned char. So what we do is treat
3822 the whole string as a base 256 number composed of a sequence of
3823 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3824 to 0, and carries 1 to the following more-significant position.
3825 If the very first character in SEARCH_NAME ends up incremented
3826 and carries/overflows, then the upper bound is the end of the
3827 list. The string after the empty string is also the empty
3828 string.
3829
3830 Some examples of this operation:
3831
3832 SEARCH_NAME => "+1" RESULT
3833
3834 "abc" => "abd"
3835 "ab\xff" => "ac"
3836 "\xff" "a" "\xff" => "\xff" "b"
3837 "\xff" => ""
3838 "\xff\xff" => ""
3839 "" => ""
3840
3841 Then, with these symbols for example:
3842
3843 func
3844 func1
3845 fund
3846
3847 completing "func" looks for symbols between "func" and
3848 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3849 which finds "func" and "func1", but not "fund".
3850
3851 And with:
3852
3853 funcÿ (Latin1 'ÿ' [0xff])
3854 funcÿ1
3855 fund
3856
3857 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3858 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3859
3860 And with:
3861
3862 ÿÿ (Latin1 'ÿ' [0xff])
3863 ÿÿ1
3864
3865 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3866 the end of the list.
3867 */
3868 std::string after = search_name;
3869 while (!after.empty () && (unsigned char) after.back () == 0xff)
3870 after.pop_back ();
3871 if (!after.empty ())
3872 after.back () = (unsigned char) after.back () + 1;
3873 return after;
3874 }
3875
3876 /* See declaration. */
3877
3878 std::pair<std::vector<name_component>::const_iterator,
3879 std::vector<name_component>::const_iterator>
3880 mapped_index_base::find_name_components_bounds
3881 (const lookup_name_info &lookup_name_without_params, language lang) const
3882 {
3883 auto *name_cmp
3884 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3885
3886 const char *lang_name
3887 = lookup_name_without_params.language_lookup_name (lang);
3888
3889 /* Comparison function object for lower_bound that matches against a
3890 given symbol name. */
3891 auto lookup_compare_lower = [&] (const name_component &elem,
3892 const char *name)
3893 {
3894 const char *elem_qualified = this->symbol_name_at (elem.idx);
3895 const char *elem_name = elem_qualified + elem.name_offset;
3896 return name_cmp (elem_name, name) < 0;
3897 };
3898
3899 /* Comparison function object for upper_bound that matches against a
3900 given symbol name. */
3901 auto lookup_compare_upper = [&] (const char *name,
3902 const name_component &elem)
3903 {
3904 const char *elem_qualified = this->symbol_name_at (elem.idx);
3905 const char *elem_name = elem_qualified + elem.name_offset;
3906 return name_cmp (name, elem_name) < 0;
3907 };
3908
3909 auto begin = this->name_components.begin ();
3910 auto end = this->name_components.end ();
3911
3912 /* Find the lower bound. */
3913 auto lower = [&] ()
3914 {
3915 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3916 return begin;
3917 else
3918 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3919 } ();
3920
3921 /* Find the upper bound. */
3922 auto upper = [&] ()
3923 {
3924 if (lookup_name_without_params.completion_mode ())
3925 {
3926 /* In completion mode, we want UPPER to point past all
3927 symbols names that have the same prefix. I.e., with
3928 these symbols, and completing "func":
3929
3930 function << lower bound
3931 function1
3932 other_function << upper bound
3933
3934 We find the upper bound by looking for the insertion
3935 point of "func"-with-last-character-incremented,
3936 i.e. "fund". */
3937 std::string after = make_sort_after_prefix_name (lang_name);
3938 if (after.empty ())
3939 return end;
3940 return std::lower_bound (lower, end, after.c_str (),
3941 lookup_compare_lower);
3942 }
3943 else
3944 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3945 } ();
3946
3947 return {lower, upper};
3948 }
3949
3950 /* See declaration. */
3951
3952 void
3953 mapped_index_base::build_name_components ()
3954 {
3955 if (!this->name_components.empty ())
3956 return;
3957
3958 this->name_components_casing = case_sensitivity;
3959 auto *name_cmp
3960 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3961
3962 /* The code below only knows how to break apart components of C++
3963 symbol names (and other languages that use '::' as
3964 namespace/module separator) and Ada symbol names. */
3965 auto count = this->symbol_name_count ();
3966 for (offset_type idx = 0; idx < count; idx++)
3967 {
3968 if (this->symbol_name_slot_invalid (idx))
3969 continue;
3970
3971 const char *name = this->symbol_name_at (idx);
3972
3973 /* Add each name component to the name component table. */
3974 unsigned int previous_len = 0;
3975
3976 if (strstr (name, "::") != nullptr)
3977 {
3978 for (unsigned int current_len = cp_find_first_component (name);
3979 name[current_len] != '\0';
3980 current_len += cp_find_first_component (name + current_len))
3981 {
3982 gdb_assert (name[current_len] == ':');
3983 this->name_components.push_back ({previous_len, idx});
3984 /* Skip the '::'. */
3985 current_len += 2;
3986 previous_len = current_len;
3987 }
3988 }
3989 else
3990 {
3991 /* Handle the Ada encoded (aka mangled) form here. */
3992 for (const char *iter = strstr (name, "__");
3993 iter != nullptr;
3994 iter = strstr (iter, "__"))
3995 {
3996 this->name_components.push_back ({previous_len, idx});
3997 iter += 2;
3998 previous_len = iter - name;
3999 }
4000 }
4001
4002 this->name_components.push_back ({previous_len, idx});
4003 }
4004
4005 /* Sort name_components elements by name. */
4006 auto name_comp_compare = [&] (const name_component &left,
4007 const name_component &right)
4008 {
4009 const char *left_qualified = this->symbol_name_at (left.idx);
4010 const char *right_qualified = this->symbol_name_at (right.idx);
4011
4012 const char *left_name = left_qualified + left.name_offset;
4013 const char *right_name = right_qualified + right.name_offset;
4014
4015 return name_cmp (left_name, right_name) < 0;
4016 };
4017
4018 std::sort (this->name_components.begin (),
4019 this->name_components.end (),
4020 name_comp_compare);
4021 }
4022
4023 /* Helper for dw2_expand_symtabs_matching that works with a
4024 mapped_index_base instead of the containing objfile. This is split
4025 to a separate function in order to be able to unit test the
4026 name_components matching using a mock mapped_index_base. For each
4027 symbol name that matches, calls MATCH_CALLBACK, passing it the
4028 symbol's index in the mapped_index_base symbol table. */
4029
4030 static void
4031 dw2_expand_symtabs_matching_symbol
4032 (mapped_index_base &index,
4033 const lookup_name_info &lookup_name_in,
4034 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4035 enum search_domain kind,
4036 gdb::function_view<bool (offset_type)> match_callback)
4037 {
4038 lookup_name_info lookup_name_without_params
4039 = lookup_name_in.make_ignore_params ();
4040
4041 /* Build the symbol name component sorted vector, if we haven't
4042 yet. */
4043 index.build_name_components ();
4044
4045 /* The same symbol may appear more than once in the range though.
4046 E.g., if we're looking for symbols that complete "w", and we have
4047 a symbol named "w1::w2", we'll find the two name components for
4048 that same symbol in the range. To be sure we only call the
4049 callback once per symbol, we first collect the symbol name
4050 indexes that matched in a temporary vector and ignore
4051 duplicates. */
4052 std::vector<offset_type> matches;
4053
4054 struct name_and_matcher
4055 {
4056 symbol_name_matcher_ftype *matcher;
4057 const char *name;
4058
4059 bool operator== (const name_and_matcher &other) const
4060 {
4061 return matcher == other.matcher && strcmp (name, other.name) == 0;
4062 }
4063 };
4064
4065 /* A vector holding all the different symbol name matchers, for all
4066 languages. */
4067 std::vector<name_and_matcher> matchers;
4068
4069 for (int i = 0; i < nr_languages; i++)
4070 {
4071 enum language lang_e = (enum language) i;
4072
4073 const language_defn *lang = language_def (lang_e);
4074 symbol_name_matcher_ftype *name_matcher
4075 = get_symbol_name_matcher (lang, lookup_name_without_params);
4076
4077 name_and_matcher key {
4078 name_matcher,
4079 lookup_name_without_params.language_lookup_name (lang_e)
4080 };
4081
4082 /* Don't insert the same comparison routine more than once.
4083 Note that we do this linear walk. This is not a problem in
4084 practice because the number of supported languages is
4085 low. */
4086 if (std::find (matchers.begin (), matchers.end (), key)
4087 != matchers.end ())
4088 continue;
4089 matchers.push_back (std::move (key));
4090
4091 auto bounds
4092 = index.find_name_components_bounds (lookup_name_without_params,
4093 lang_e);
4094
4095 /* Now for each symbol name in range, check to see if we have a name
4096 match, and if so, call the MATCH_CALLBACK callback. */
4097
4098 for (; bounds.first != bounds.second; ++bounds.first)
4099 {
4100 const char *qualified = index.symbol_name_at (bounds.first->idx);
4101
4102 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4103 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4104 continue;
4105
4106 matches.push_back (bounds.first->idx);
4107 }
4108 }
4109
4110 std::sort (matches.begin (), matches.end ());
4111
4112 /* Finally call the callback, once per match. */
4113 ULONGEST prev = -1;
4114 for (offset_type idx : matches)
4115 {
4116 if (prev != idx)
4117 {
4118 if (!match_callback (idx))
4119 break;
4120 prev = idx;
4121 }
4122 }
4123
4124 /* Above we use a type wider than idx's for 'prev', since 0 and
4125 (offset_type)-1 are both possible values. */
4126 static_assert (sizeof (prev) > sizeof (offset_type), "");
4127 }
4128
4129 #if GDB_SELF_TEST
4130
4131 namespace selftests { namespace dw2_expand_symtabs_matching {
4132
4133 /* A mock .gdb_index/.debug_names-like name index table, enough to
4134 exercise dw2_expand_symtabs_matching_symbol, which works with the
4135 mapped_index_base interface. Builds an index from the symbol list
4136 passed as parameter to the constructor. */
4137 class mock_mapped_index : public mapped_index_base
4138 {
4139 public:
4140 mock_mapped_index (gdb::array_view<const char *> symbols)
4141 : m_symbol_table (symbols)
4142 {}
4143
4144 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4145
4146 /* Return the number of names in the symbol table. */
4147 size_t symbol_name_count () const override
4148 {
4149 return m_symbol_table.size ();
4150 }
4151
4152 /* Get the name of the symbol at IDX in the symbol table. */
4153 const char *symbol_name_at (offset_type idx) const override
4154 {
4155 return m_symbol_table[idx];
4156 }
4157
4158 private:
4159 gdb::array_view<const char *> m_symbol_table;
4160 };
4161
4162 /* Convenience function that converts a NULL pointer to a "<null>"
4163 string, to pass to print routines. */
4164
4165 static const char *
4166 string_or_null (const char *str)
4167 {
4168 return str != NULL ? str : "<null>";
4169 }
4170
4171 /* Check if a lookup_name_info built from
4172 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4173 index. EXPECTED_LIST is the list of expected matches, in expected
4174 matching order. If no match expected, then an empty list is
4175 specified. Returns true on success. On failure prints a warning
4176 indicating the file:line that failed, and returns false. */
4177
4178 static bool
4179 check_match (const char *file, int line,
4180 mock_mapped_index &mock_index,
4181 const char *name, symbol_name_match_type match_type,
4182 bool completion_mode,
4183 std::initializer_list<const char *> expected_list)
4184 {
4185 lookup_name_info lookup_name (name, match_type, completion_mode);
4186
4187 bool matched = true;
4188
4189 auto mismatch = [&] (const char *expected_str,
4190 const char *got)
4191 {
4192 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4193 "expected=\"%s\", got=\"%s\"\n"),
4194 file, line,
4195 (match_type == symbol_name_match_type::FULL
4196 ? "FULL" : "WILD"),
4197 name, string_or_null (expected_str), string_or_null (got));
4198 matched = false;
4199 };
4200
4201 auto expected_it = expected_list.begin ();
4202 auto expected_end = expected_list.end ();
4203
4204 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4205 NULL, ALL_DOMAIN,
4206 [&] (offset_type idx)
4207 {
4208 const char *matched_name = mock_index.symbol_name_at (idx);
4209 const char *expected_str
4210 = expected_it == expected_end ? NULL : *expected_it++;
4211
4212 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4213 mismatch (expected_str, matched_name);
4214 return true;
4215 });
4216
4217 const char *expected_str
4218 = expected_it == expected_end ? NULL : *expected_it++;
4219 if (expected_str != NULL)
4220 mismatch (expected_str, NULL);
4221
4222 return matched;
4223 }
4224
4225 /* The symbols added to the mock mapped_index for testing (in
4226 canonical form). */
4227 static const char *test_symbols[] = {
4228 "function",
4229 "std::bar",
4230 "std::zfunction",
4231 "std::zfunction2",
4232 "w1::w2",
4233 "ns::foo<char*>",
4234 "ns::foo<int>",
4235 "ns::foo<long>",
4236 "ns2::tmpl<int>::foo2",
4237 "(anonymous namespace)::A::B::C",
4238
4239 /* These are used to check that the increment-last-char in the
4240 matching algorithm for completion doesn't match "t1_fund" when
4241 completing "t1_func". */
4242 "t1_func",
4243 "t1_func1",
4244 "t1_fund",
4245 "t1_fund1",
4246
4247 /* A UTF-8 name with multi-byte sequences to make sure that
4248 cp-name-parser understands this as a single identifier ("função"
4249 is "function" in PT). */
4250 u8"u8função",
4251
4252 /* \377 (0xff) is Latin1 'ÿ'. */
4253 "yfunc\377",
4254
4255 /* \377 (0xff) is Latin1 'ÿ'. */
4256 "\377",
4257 "\377\377123",
4258
4259 /* A name with all sorts of complications. Starts with "z" to make
4260 it easier for the completion tests below. */
4261 #define Z_SYM_NAME \
4262 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4263 "::tuple<(anonymous namespace)::ui*, " \
4264 "std::default_delete<(anonymous namespace)::ui>, void>"
4265
4266 Z_SYM_NAME
4267 };
4268
4269 /* Returns true if the mapped_index_base::find_name_component_bounds
4270 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4271 in completion mode. */
4272
4273 static bool
4274 check_find_bounds_finds (mapped_index_base &index,
4275 const char *search_name,
4276 gdb::array_view<const char *> expected_syms)
4277 {
4278 lookup_name_info lookup_name (search_name,
4279 symbol_name_match_type::FULL, true);
4280
4281 auto bounds = index.find_name_components_bounds (lookup_name,
4282 language_cplus);
4283
4284 size_t distance = std::distance (bounds.first, bounds.second);
4285 if (distance != expected_syms.size ())
4286 return false;
4287
4288 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4289 {
4290 auto nc_elem = bounds.first + exp_elem;
4291 const char *qualified = index.symbol_name_at (nc_elem->idx);
4292 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4293 return false;
4294 }
4295
4296 return true;
4297 }
4298
4299 /* Test the lower-level mapped_index::find_name_component_bounds
4300 method. */
4301
4302 static void
4303 test_mapped_index_find_name_component_bounds ()
4304 {
4305 mock_mapped_index mock_index (test_symbols);
4306
4307 mock_index.build_name_components ();
4308
4309 /* Test the lower-level mapped_index::find_name_component_bounds
4310 method in completion mode. */
4311 {
4312 static const char *expected_syms[] = {
4313 "t1_func",
4314 "t1_func1",
4315 };
4316
4317 SELF_CHECK (check_find_bounds_finds (mock_index,
4318 "t1_func", expected_syms));
4319 }
4320
4321 /* Check that the increment-last-char in the name matching algorithm
4322 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4323 {
4324 static const char *expected_syms1[] = {
4325 "\377",
4326 "\377\377123",
4327 };
4328 SELF_CHECK (check_find_bounds_finds (mock_index,
4329 "\377", expected_syms1));
4330
4331 static const char *expected_syms2[] = {
4332 "\377\377123",
4333 };
4334 SELF_CHECK (check_find_bounds_finds (mock_index,
4335 "\377\377", expected_syms2));
4336 }
4337 }
4338
4339 /* Test dw2_expand_symtabs_matching_symbol. */
4340
4341 static void
4342 test_dw2_expand_symtabs_matching_symbol ()
4343 {
4344 mock_mapped_index mock_index (test_symbols);
4345
4346 /* We let all tests run until the end even if some fails, for debug
4347 convenience. */
4348 bool any_mismatch = false;
4349
4350 /* Create the expected symbols list (an initializer_list). Needed
4351 because lists have commas, and we need to pass them to CHECK,
4352 which is a macro. */
4353 #define EXPECT(...) { __VA_ARGS__ }
4354
4355 /* Wrapper for check_match that passes down the current
4356 __FILE__/__LINE__. */
4357 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4358 any_mismatch |= !check_match (__FILE__, __LINE__, \
4359 mock_index, \
4360 NAME, MATCH_TYPE, COMPLETION_MODE, \
4361 EXPECTED_LIST)
4362
4363 /* Identity checks. */
4364 for (const char *sym : test_symbols)
4365 {
4366 /* Should be able to match all existing symbols. */
4367 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4368 EXPECT (sym));
4369
4370 /* Should be able to match all existing symbols with
4371 parameters. */
4372 std::string with_params = std::string (sym) + "(int)";
4373 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4374 EXPECT (sym));
4375
4376 /* Should be able to match all existing symbols with
4377 parameters and qualifiers. */
4378 with_params = std::string (sym) + " ( int ) const";
4379 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4380 EXPECT (sym));
4381
4382 /* This should really find sym, but cp-name-parser.y doesn't
4383 know about lvalue/rvalue qualifiers yet. */
4384 with_params = std::string (sym) + " ( int ) &&";
4385 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4386 {});
4387 }
4388
4389 /* Check that the name matching algorithm for completion doesn't get
4390 confused with Latin1 'ÿ' / 0xff. */
4391 {
4392 static const char str[] = "\377";
4393 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4394 EXPECT ("\377", "\377\377123"));
4395 }
4396
4397 /* Check that the increment-last-char in the matching algorithm for
4398 completion doesn't match "t1_fund" when completing "t1_func". */
4399 {
4400 static const char str[] = "t1_func";
4401 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4402 EXPECT ("t1_func", "t1_func1"));
4403 }
4404
4405 /* Check that completion mode works at each prefix of the expected
4406 symbol name. */
4407 {
4408 static const char str[] = "function(int)";
4409 size_t len = strlen (str);
4410 std::string lookup;
4411
4412 for (size_t i = 1; i < len; i++)
4413 {
4414 lookup.assign (str, i);
4415 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4416 EXPECT ("function"));
4417 }
4418 }
4419
4420 /* While "w" is a prefix of both components, the match function
4421 should still only be called once. */
4422 {
4423 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4424 EXPECT ("w1::w2"));
4425 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4426 EXPECT ("w1::w2"));
4427 }
4428
4429 /* Same, with a "complicated" symbol. */
4430 {
4431 static const char str[] = Z_SYM_NAME;
4432 size_t len = strlen (str);
4433 std::string lookup;
4434
4435 for (size_t i = 1; i < len; i++)
4436 {
4437 lookup.assign (str, i);
4438 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4439 EXPECT (Z_SYM_NAME));
4440 }
4441 }
4442
4443 /* In FULL mode, an incomplete symbol doesn't match. */
4444 {
4445 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4446 {});
4447 }
4448
4449 /* A complete symbol with parameters matches any overload, since the
4450 index has no overload info. */
4451 {
4452 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4453 EXPECT ("std::zfunction", "std::zfunction2"));
4454 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4455 EXPECT ("std::zfunction", "std::zfunction2"));
4456 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4457 EXPECT ("std::zfunction", "std::zfunction2"));
4458 }
4459
4460 /* Check that whitespace is ignored appropriately. A symbol with a
4461 template argument list. */
4462 {
4463 static const char expected[] = "ns::foo<int>";
4464 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4465 EXPECT (expected));
4466 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4467 EXPECT (expected));
4468 }
4469
4470 /* Check that whitespace is ignored appropriately. A symbol with a
4471 template argument list that includes a pointer. */
4472 {
4473 static const char expected[] = "ns::foo<char*>";
4474 /* Try both completion and non-completion modes. */
4475 static const bool completion_mode[2] = {false, true};
4476 for (size_t i = 0; i < 2; i++)
4477 {
4478 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4479 completion_mode[i], EXPECT (expected));
4480 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4481 completion_mode[i], EXPECT (expected));
4482
4483 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4484 completion_mode[i], EXPECT (expected));
4485 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4486 completion_mode[i], EXPECT (expected));
4487 }
4488 }
4489
4490 {
4491 /* Check method qualifiers are ignored. */
4492 static const char expected[] = "ns::foo<char*>";
4493 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4494 symbol_name_match_type::FULL, true, EXPECT (expected));
4495 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4496 symbol_name_match_type::FULL, true, EXPECT (expected));
4497 CHECK_MATCH ("foo < char * > ( int ) const",
4498 symbol_name_match_type::WILD, true, EXPECT (expected));
4499 CHECK_MATCH ("foo < char * > ( int ) &&",
4500 symbol_name_match_type::WILD, true, EXPECT (expected));
4501 }
4502
4503 /* Test lookup names that don't match anything. */
4504 {
4505 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4506 {});
4507
4508 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4509 {});
4510 }
4511
4512 /* Some wild matching tests, exercising "(anonymous namespace)",
4513 which should not be confused with a parameter list. */
4514 {
4515 static const char *syms[] = {
4516 "A::B::C",
4517 "B::C",
4518 "C",
4519 "A :: B :: C ( int )",
4520 "B :: C ( int )",
4521 "C ( int )",
4522 };
4523
4524 for (const char *s : syms)
4525 {
4526 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4527 EXPECT ("(anonymous namespace)::A::B::C"));
4528 }
4529 }
4530
4531 {
4532 static const char expected[] = "ns2::tmpl<int>::foo2";
4533 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4534 EXPECT (expected));
4535 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4536 EXPECT (expected));
4537 }
4538
4539 SELF_CHECK (!any_mismatch);
4540
4541 #undef EXPECT
4542 #undef CHECK_MATCH
4543 }
4544
4545 static void
4546 run_test ()
4547 {
4548 test_mapped_index_find_name_component_bounds ();
4549 test_dw2_expand_symtabs_matching_symbol ();
4550 }
4551
4552 }} // namespace selftests::dw2_expand_symtabs_matching
4553
4554 #endif /* GDB_SELF_TEST */
4555
4556 /* If FILE_MATCHER is NULL or if PER_CU has
4557 dwarf2_per_cu_quick_data::MARK set (see
4558 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4559 EXPANSION_NOTIFY on it. */
4560
4561 static void
4562 dw2_expand_symtabs_matching_one
4563 (dwarf2_per_cu_data *per_cu,
4564 dwarf2_per_objfile *per_objfile,
4565 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4566 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4567 {
4568 if (file_matcher == NULL || per_cu->v.quick->mark)
4569 {
4570 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4571
4572 compunit_symtab *symtab
4573 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4574 gdb_assert (symtab != nullptr);
4575
4576 if (expansion_notify != NULL && symtab_was_null)
4577 expansion_notify (symtab);
4578 }
4579 }
4580
4581 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4582 matched, to expand corresponding CUs that were marked. IDX is the
4583 index of the symbol name that matched. */
4584
4585 static void
4586 dw2_expand_marked_cus
4587 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4588 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4589 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4590 search_domain kind)
4591 {
4592 offset_type *vec, vec_len, vec_idx;
4593 bool global_seen = false;
4594 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4595
4596 vec = (offset_type *) (index.constant_pool
4597 + MAYBE_SWAP (index.symbol_table[idx].vec));
4598 vec_len = MAYBE_SWAP (vec[0]);
4599 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4600 {
4601 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4602 /* This value is only valid for index versions >= 7. */
4603 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4604 gdb_index_symbol_kind symbol_kind =
4605 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4606 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4607 /* Only check the symbol attributes if they're present.
4608 Indices prior to version 7 don't record them,
4609 and indices >= 7 may elide them for certain symbols
4610 (gold does this). */
4611 int attrs_valid =
4612 (index.version >= 7
4613 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4614
4615 /* Work around gold/15646. */
4616 if (attrs_valid)
4617 {
4618 if (!is_static && global_seen)
4619 continue;
4620 if (!is_static)
4621 global_seen = true;
4622 }
4623
4624 /* Only check the symbol's kind if it has one. */
4625 if (attrs_valid)
4626 {
4627 switch (kind)
4628 {
4629 case VARIABLES_DOMAIN:
4630 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4631 continue;
4632 break;
4633 case FUNCTIONS_DOMAIN:
4634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4635 continue;
4636 break;
4637 case TYPES_DOMAIN:
4638 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4639 continue;
4640 break;
4641 case MODULES_DOMAIN:
4642 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4643 continue;
4644 break;
4645 default:
4646 break;
4647 }
4648 }
4649
4650 /* Don't crash on bad data. */
4651 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4652 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4653 {
4654 complaint (_(".gdb_index entry has bad CU index"
4655 " [in module %s]"),
4656 objfile_name (dwarf2_per_objfile->objfile));
4657 continue;
4658 }
4659
4660 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4661 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4662 expansion_notify);
4663 }
4664 }
4665
4666 /* If FILE_MATCHER is non-NULL, set all the
4667 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4668 that match FILE_MATCHER. */
4669
4670 static void
4671 dw_expand_symtabs_matching_file_matcher
4672 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4673 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4674 {
4675 if (file_matcher == NULL)
4676 return;
4677
4678 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4679 htab_eq_pointer,
4680 NULL, xcalloc, xfree));
4681 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4682 htab_eq_pointer,
4683 NULL, xcalloc, xfree));
4684
4685 /* The rule is CUs specify all the files, including those used by
4686 any TU, so there's no need to scan TUs here. */
4687
4688 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4689 {
4690 QUIT;
4691
4692 per_cu->v.quick->mark = 0;
4693
4694 /* We only need to look at symtabs not already expanded. */
4695 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4696 continue;
4697
4698 quick_file_names *file_data
4699 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4700 if (file_data == NULL)
4701 continue;
4702
4703 if (htab_find (visited_not_found.get (), file_data) != NULL)
4704 continue;
4705 else if (htab_find (visited_found.get (), file_data) != NULL)
4706 {
4707 per_cu->v.quick->mark = 1;
4708 continue;
4709 }
4710
4711 for (int j = 0; j < file_data->num_file_names; ++j)
4712 {
4713 const char *this_real_name;
4714
4715 if (file_matcher (file_data->file_names[j], false))
4716 {
4717 per_cu->v.quick->mark = 1;
4718 break;
4719 }
4720
4721 /* Before we invoke realpath, which can get expensive when many
4722 files are involved, do a quick comparison of the basenames. */
4723 if (!basenames_may_differ
4724 && !file_matcher (lbasename (file_data->file_names[j]),
4725 true))
4726 continue;
4727
4728 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4729 file_data, j);
4730 if (file_matcher (this_real_name, false))
4731 {
4732 per_cu->v.quick->mark = 1;
4733 break;
4734 }
4735 }
4736
4737 void **slot = htab_find_slot (per_cu->v.quick->mark
4738 ? visited_found.get ()
4739 : visited_not_found.get (),
4740 file_data, INSERT);
4741 *slot = file_data;
4742 }
4743 }
4744
4745 static void
4746 dw2_expand_symtabs_matching
4747 (struct objfile *objfile,
4748 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4749 const lookup_name_info *lookup_name,
4750 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4751 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4752 enum search_domain kind)
4753 {
4754 struct dwarf2_per_objfile *dwarf2_per_objfile
4755 = get_dwarf2_per_objfile (objfile);
4756
4757 /* index_table is NULL if OBJF_READNOW. */
4758 if (!dwarf2_per_objfile->per_bfd->index_table)
4759 return;
4760
4761 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4762
4763 if (symbol_matcher == NULL && lookup_name == NULL)
4764 {
4765 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4766 {
4767 QUIT;
4768
4769 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4770 file_matcher, expansion_notify);
4771 }
4772 return;
4773 }
4774
4775 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4776
4777 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4778 symbol_matcher,
4779 kind, [&] (offset_type idx)
4780 {
4781 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4782 expansion_notify, kind);
4783 return true;
4784 });
4785 }
4786
4787 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4788 symtab. */
4789
4790 static struct compunit_symtab *
4791 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4792 CORE_ADDR pc)
4793 {
4794 int i;
4795
4796 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4797 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4798 return cust;
4799
4800 if (cust->includes == NULL)
4801 return NULL;
4802
4803 for (i = 0; cust->includes[i]; ++i)
4804 {
4805 struct compunit_symtab *s = cust->includes[i];
4806
4807 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4808 if (s != NULL)
4809 return s;
4810 }
4811
4812 return NULL;
4813 }
4814
4815 static struct compunit_symtab *
4816 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4817 struct bound_minimal_symbol msymbol,
4818 CORE_ADDR pc,
4819 struct obj_section *section,
4820 int warn_if_readin)
4821 {
4822 struct dwarf2_per_cu_data *data;
4823 struct compunit_symtab *result;
4824
4825 if (!objfile->partial_symtabs->psymtabs_addrmap)
4826 return NULL;
4827
4828 CORE_ADDR baseaddr = objfile->text_section_offset ();
4829 data = (struct dwarf2_per_cu_data *) addrmap_find
4830 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4831 if (!data)
4832 return NULL;
4833
4834 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4835 if (warn_if_readin && per_objfile->symtab_set_p (data))
4836 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4837 paddress (objfile->arch (), pc));
4838
4839 result = recursively_find_pc_sect_compunit_symtab
4840 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4841
4842 gdb_assert (result != NULL);
4843 return result;
4844 }
4845
4846 static void
4847 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4848 void *data, int need_fullname)
4849 {
4850 struct dwarf2_per_objfile *dwarf2_per_objfile
4851 = get_dwarf2_per_objfile (objfile);
4852
4853 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4854 {
4855 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4856
4857 htab_up visited (htab_create_alloc (10,
4858 htab_hash_pointer, htab_eq_pointer,
4859 NULL, xcalloc, xfree));
4860
4861 /* The rule is CUs specify all the files, including those used
4862 by any TU, so there's no need to scan TUs here. We can
4863 ignore file names coming from already-expanded CUs. */
4864
4865 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4866 {
4867 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4868 {
4869 void **slot = htab_find_slot (visited.get (),
4870 per_cu->v.quick->file_names,
4871 INSERT);
4872
4873 *slot = per_cu->v.quick->file_names;
4874 }
4875 }
4876
4877 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4878 {
4879 /* We only need to look at symtabs not already expanded. */
4880 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4881 continue;
4882
4883 quick_file_names *file_data
4884 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4885 if (file_data == NULL)
4886 continue;
4887
4888 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4889 if (*slot)
4890 {
4891 /* Already visited. */
4892 continue;
4893 }
4894 *slot = file_data;
4895
4896 for (int j = 0; j < file_data->num_file_names; ++j)
4897 {
4898 const char *filename = file_data->file_names[j];
4899 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4900 }
4901 }
4902 }
4903
4904 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4905 {
4906 gdb::unique_xmalloc_ptr<char> this_real_name;
4907
4908 if (need_fullname)
4909 this_real_name = gdb_realpath (filename);
4910 (*fun) (filename, this_real_name.get (), data);
4911 });
4912 }
4913
4914 static int
4915 dw2_has_symbols (struct objfile *objfile)
4916 {
4917 return 1;
4918 }
4919
4920 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4921 {
4922 dw2_has_symbols,
4923 dw2_find_last_source_symtab,
4924 dw2_forget_cached_source_info,
4925 dw2_map_symtabs_matching_filename,
4926 dw2_lookup_symbol,
4927 NULL,
4928 dw2_print_stats,
4929 dw2_dump,
4930 dw2_expand_symtabs_for_function,
4931 dw2_expand_all_symtabs,
4932 dw2_expand_symtabs_with_fullname,
4933 dw2_map_matching_symbols,
4934 dw2_expand_symtabs_matching,
4935 dw2_find_pc_sect_compunit_symtab,
4936 NULL,
4937 dw2_map_symbol_filenames
4938 };
4939
4940 /* DWARF-5 debug_names reader. */
4941
4942 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4943 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4944
4945 /* A helper function that reads the .debug_names section in SECTION
4946 and fills in MAP. FILENAME is the name of the file containing the
4947 section; it is used for error reporting.
4948
4949 Returns true if all went well, false otherwise. */
4950
4951 static bool
4952 read_debug_names_from_section (struct objfile *objfile,
4953 const char *filename,
4954 struct dwarf2_section_info *section,
4955 mapped_debug_names &map)
4956 {
4957 if (section->empty ())
4958 return false;
4959
4960 /* Older elfutils strip versions could keep the section in the main
4961 executable while splitting it for the separate debug info file. */
4962 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4963 return false;
4964
4965 section->read (objfile);
4966
4967 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4968
4969 const gdb_byte *addr = section->buffer;
4970
4971 bfd *const abfd = section->get_bfd_owner ();
4972
4973 unsigned int bytes_read;
4974 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4975 addr += bytes_read;
4976
4977 map.dwarf5_is_dwarf64 = bytes_read != 4;
4978 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4979 if (bytes_read + length != section->size)
4980 {
4981 /* There may be multiple per-CU indices. */
4982 warning (_("Section .debug_names in %s length %s does not match "
4983 "section length %s, ignoring .debug_names."),
4984 filename, plongest (bytes_read + length),
4985 pulongest (section->size));
4986 return false;
4987 }
4988
4989 /* The version number. */
4990 uint16_t version = read_2_bytes (abfd, addr);
4991 addr += 2;
4992 if (version != 5)
4993 {
4994 warning (_("Section .debug_names in %s has unsupported version %d, "
4995 "ignoring .debug_names."),
4996 filename, version);
4997 return false;
4998 }
4999
5000 /* Padding. */
5001 uint16_t padding = read_2_bytes (abfd, addr);
5002 addr += 2;
5003 if (padding != 0)
5004 {
5005 warning (_("Section .debug_names in %s has unsupported padding %d, "
5006 "ignoring .debug_names."),
5007 filename, padding);
5008 return false;
5009 }
5010
5011 /* comp_unit_count - The number of CUs in the CU list. */
5012 map.cu_count = read_4_bytes (abfd, addr);
5013 addr += 4;
5014
5015 /* local_type_unit_count - The number of TUs in the local TU
5016 list. */
5017 map.tu_count = read_4_bytes (abfd, addr);
5018 addr += 4;
5019
5020 /* foreign_type_unit_count - The number of TUs in the foreign TU
5021 list. */
5022 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5023 addr += 4;
5024 if (foreign_tu_count != 0)
5025 {
5026 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5027 "ignoring .debug_names."),
5028 filename, static_cast<unsigned long> (foreign_tu_count));
5029 return false;
5030 }
5031
5032 /* bucket_count - The number of hash buckets in the hash lookup
5033 table. */
5034 map.bucket_count = read_4_bytes (abfd, addr);
5035 addr += 4;
5036
5037 /* name_count - The number of unique names in the index. */
5038 map.name_count = read_4_bytes (abfd, addr);
5039 addr += 4;
5040
5041 /* abbrev_table_size - The size in bytes of the abbreviations
5042 table. */
5043 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5044 addr += 4;
5045
5046 /* augmentation_string_size - The size in bytes of the augmentation
5047 string. This value is rounded up to a multiple of 4. */
5048 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5049 addr += 4;
5050 map.augmentation_is_gdb = ((augmentation_string_size
5051 == sizeof (dwarf5_augmentation))
5052 && memcmp (addr, dwarf5_augmentation,
5053 sizeof (dwarf5_augmentation)) == 0);
5054 augmentation_string_size += (-augmentation_string_size) & 3;
5055 addr += augmentation_string_size;
5056
5057 /* List of CUs */
5058 map.cu_table_reordered = addr;
5059 addr += map.cu_count * map.offset_size;
5060
5061 /* List of Local TUs */
5062 map.tu_table_reordered = addr;
5063 addr += map.tu_count * map.offset_size;
5064
5065 /* Hash Lookup Table */
5066 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5067 addr += map.bucket_count * 4;
5068 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5069 addr += map.name_count * 4;
5070
5071 /* Name Table */
5072 map.name_table_string_offs_reordered = addr;
5073 addr += map.name_count * map.offset_size;
5074 map.name_table_entry_offs_reordered = addr;
5075 addr += map.name_count * map.offset_size;
5076
5077 const gdb_byte *abbrev_table_start = addr;
5078 for (;;)
5079 {
5080 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5081 addr += bytes_read;
5082 if (index_num == 0)
5083 break;
5084
5085 const auto insertpair
5086 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5087 if (!insertpair.second)
5088 {
5089 warning (_("Section .debug_names in %s has duplicate index %s, "
5090 "ignoring .debug_names."),
5091 filename, pulongest (index_num));
5092 return false;
5093 }
5094 mapped_debug_names::index_val &indexval = insertpair.first->second;
5095 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5096 addr += bytes_read;
5097
5098 for (;;)
5099 {
5100 mapped_debug_names::index_val::attr attr;
5101 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5102 addr += bytes_read;
5103 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5104 addr += bytes_read;
5105 if (attr.form == DW_FORM_implicit_const)
5106 {
5107 attr.implicit_const = read_signed_leb128 (abfd, addr,
5108 &bytes_read);
5109 addr += bytes_read;
5110 }
5111 if (attr.dw_idx == 0 && attr.form == 0)
5112 break;
5113 indexval.attr_vec.push_back (std::move (attr));
5114 }
5115 }
5116 if (addr != abbrev_table_start + abbrev_table_size)
5117 {
5118 warning (_("Section .debug_names in %s has abbreviation_table "
5119 "of size %s vs. written as %u, ignoring .debug_names."),
5120 filename, plongest (addr - abbrev_table_start),
5121 abbrev_table_size);
5122 return false;
5123 }
5124 map.entry_pool = addr;
5125
5126 return true;
5127 }
5128
5129 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5130 list. */
5131
5132 static void
5133 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5134 const mapped_debug_names &map,
5135 dwarf2_section_info &section,
5136 bool is_dwz)
5137 {
5138 if (!map.augmentation_is_gdb)
5139 {
5140 for (uint32_t i = 0; i < map.cu_count; ++i)
5141 {
5142 sect_offset sect_off
5143 = (sect_offset) (extract_unsigned_integer
5144 (map.cu_table_reordered + i * map.offset_size,
5145 map.offset_size,
5146 map.dwarf5_byte_order));
5147 /* We don't know the length of the CU, because the CU list in a
5148 .debug_names index can be incomplete, so we can't use the start of
5149 the next CU as end of this CU. We create the CUs here with length 0,
5150 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5151 dwarf2_per_cu_data *per_cu
5152 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5153 sect_off, 0);
5154 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5155 }
5156 }
5157
5158 sect_offset sect_off_prev;
5159 for (uint32_t i = 0; i <= map.cu_count; ++i)
5160 {
5161 sect_offset sect_off_next;
5162 if (i < map.cu_count)
5163 {
5164 sect_off_next
5165 = (sect_offset) (extract_unsigned_integer
5166 (map.cu_table_reordered + i * map.offset_size,
5167 map.offset_size,
5168 map.dwarf5_byte_order));
5169 }
5170 else
5171 sect_off_next = (sect_offset) section.size;
5172 if (i >= 1)
5173 {
5174 const ULONGEST length = sect_off_next - sect_off_prev;
5175 dwarf2_per_cu_data *per_cu
5176 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5177 sect_off_prev, length);
5178 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5179 }
5180 sect_off_prev = sect_off_next;
5181 }
5182 }
5183
5184 /* Read the CU list from the mapped index, and use it to create all
5185 the CU objects for this dwarf2_per_objfile. */
5186
5187 static void
5188 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5189 const mapped_debug_names &map,
5190 const mapped_debug_names &dwz_map)
5191 {
5192 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5193 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5194
5195 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5196 dwarf2_per_objfile->per_bfd->info,
5197 false /* is_dwz */);
5198
5199 if (dwz_map.cu_count == 0)
5200 return;
5201
5202 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5203 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5204 true /* is_dwz */);
5205 }
5206
5207 /* Read .debug_names. If everything went ok, initialize the "quick"
5208 elements of all the CUs and return true. Otherwise, return false. */
5209
5210 static bool
5211 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5212 {
5213 std::unique_ptr<mapped_debug_names> map
5214 (new mapped_debug_names (dwarf2_per_objfile));
5215 mapped_debug_names dwz_map (dwarf2_per_objfile);
5216 struct objfile *objfile = dwarf2_per_objfile->objfile;
5217
5218 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5219 &dwarf2_per_objfile->per_bfd->debug_names,
5220 *map))
5221 return false;
5222
5223 /* Don't use the index if it's empty. */
5224 if (map->name_count == 0)
5225 return false;
5226
5227 /* If there is a .dwz file, read it so we can get its CU list as
5228 well. */
5229 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5230 if (dwz != NULL)
5231 {
5232 if (!read_debug_names_from_section (objfile,
5233 bfd_get_filename (dwz->dwz_bfd.get ()),
5234 &dwz->debug_names, dwz_map))
5235 {
5236 warning (_("could not read '.debug_names' section from %s; skipping"),
5237 bfd_get_filename (dwz->dwz_bfd.get ()));
5238 return false;
5239 }
5240 }
5241
5242 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5243
5244 if (map->tu_count != 0)
5245 {
5246 /* We can only handle a single .debug_types when we have an
5247 index. */
5248 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5249 return false;
5250
5251 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5252
5253 create_signatured_type_table_from_debug_names
5254 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5255 }
5256
5257 create_addrmap_from_aranges (dwarf2_per_objfile,
5258 &dwarf2_per_objfile->per_bfd->debug_aranges);
5259
5260 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5261 dwarf2_per_objfile->per_bfd->using_index = 1;
5262 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5263 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5264
5265 return true;
5266 }
5267
5268 /* Type used to manage iterating over all CUs looking for a symbol for
5269 .debug_names. */
5270
5271 class dw2_debug_names_iterator
5272 {
5273 public:
5274 dw2_debug_names_iterator (const mapped_debug_names &map,
5275 gdb::optional<block_enum> block_index,
5276 domain_enum domain,
5277 const char *name)
5278 : m_map (map), m_block_index (block_index), m_domain (domain),
5279 m_addr (find_vec_in_debug_names (map, name))
5280 {}
5281
5282 dw2_debug_names_iterator (const mapped_debug_names &map,
5283 search_domain search, uint32_t namei)
5284 : m_map (map),
5285 m_search (search),
5286 m_addr (find_vec_in_debug_names (map, namei))
5287 {}
5288
5289 dw2_debug_names_iterator (const mapped_debug_names &map,
5290 block_enum block_index, domain_enum domain,
5291 uint32_t namei)
5292 : m_map (map), m_block_index (block_index), m_domain (domain),
5293 m_addr (find_vec_in_debug_names (map, namei))
5294 {}
5295
5296 /* Return the next matching CU or NULL if there are no more. */
5297 dwarf2_per_cu_data *next ();
5298
5299 private:
5300 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5301 const char *name);
5302 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5303 uint32_t namei);
5304
5305 /* The internalized form of .debug_names. */
5306 const mapped_debug_names &m_map;
5307
5308 /* If set, only look for symbols that match that block. Valid values are
5309 GLOBAL_BLOCK and STATIC_BLOCK. */
5310 const gdb::optional<block_enum> m_block_index;
5311
5312 /* The kind of symbol we're looking for. */
5313 const domain_enum m_domain = UNDEF_DOMAIN;
5314 const search_domain m_search = ALL_DOMAIN;
5315
5316 /* The list of CUs from the index entry of the symbol, or NULL if
5317 not found. */
5318 const gdb_byte *m_addr;
5319 };
5320
5321 const char *
5322 mapped_debug_names::namei_to_name (uint32_t namei) const
5323 {
5324 const ULONGEST namei_string_offs
5325 = extract_unsigned_integer ((name_table_string_offs_reordered
5326 + namei * offset_size),
5327 offset_size,
5328 dwarf5_byte_order);
5329 return read_indirect_string_at_offset (dwarf2_per_objfile,
5330 namei_string_offs);
5331 }
5332
5333 /* Find a slot in .debug_names for the object named NAME. If NAME is
5334 found, return pointer to its pool data. If NAME cannot be found,
5335 return NULL. */
5336
5337 const gdb_byte *
5338 dw2_debug_names_iterator::find_vec_in_debug_names
5339 (const mapped_debug_names &map, const char *name)
5340 {
5341 int (*cmp) (const char *, const char *);
5342
5343 gdb::unique_xmalloc_ptr<char> without_params;
5344 if (current_language->la_language == language_cplus
5345 || current_language->la_language == language_fortran
5346 || current_language->la_language == language_d)
5347 {
5348 /* NAME is already canonical. Drop any qualifiers as
5349 .debug_names does not contain any. */
5350
5351 if (strchr (name, '(') != NULL)
5352 {
5353 without_params = cp_remove_params (name);
5354 if (without_params != NULL)
5355 name = without_params.get ();
5356 }
5357 }
5358
5359 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5360
5361 const uint32_t full_hash = dwarf5_djb_hash (name);
5362 uint32_t namei
5363 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5364 (map.bucket_table_reordered
5365 + (full_hash % map.bucket_count)), 4,
5366 map.dwarf5_byte_order);
5367 if (namei == 0)
5368 return NULL;
5369 --namei;
5370 if (namei >= map.name_count)
5371 {
5372 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5373 "[in module %s]"),
5374 namei, map.name_count,
5375 objfile_name (map.dwarf2_per_objfile->objfile));
5376 return NULL;
5377 }
5378
5379 for (;;)
5380 {
5381 const uint32_t namei_full_hash
5382 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5383 (map.hash_table_reordered + namei), 4,
5384 map.dwarf5_byte_order);
5385 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5386 return NULL;
5387
5388 if (full_hash == namei_full_hash)
5389 {
5390 const char *const namei_string = map.namei_to_name (namei);
5391
5392 #if 0 /* An expensive sanity check. */
5393 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5394 {
5395 complaint (_("Wrong .debug_names hash for string at index %u "
5396 "[in module %s]"),
5397 namei, objfile_name (dwarf2_per_objfile->objfile));
5398 return NULL;
5399 }
5400 #endif
5401
5402 if (cmp (namei_string, name) == 0)
5403 {
5404 const ULONGEST namei_entry_offs
5405 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5406 + namei * map.offset_size),
5407 map.offset_size, map.dwarf5_byte_order);
5408 return map.entry_pool + namei_entry_offs;
5409 }
5410 }
5411
5412 ++namei;
5413 if (namei >= map.name_count)
5414 return NULL;
5415 }
5416 }
5417
5418 const gdb_byte *
5419 dw2_debug_names_iterator::find_vec_in_debug_names
5420 (const mapped_debug_names &map, uint32_t namei)
5421 {
5422 if (namei >= map.name_count)
5423 {
5424 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5425 "[in module %s]"),
5426 namei, map.name_count,
5427 objfile_name (map.dwarf2_per_objfile->objfile));
5428 return NULL;
5429 }
5430
5431 const ULONGEST namei_entry_offs
5432 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5433 + namei * map.offset_size),
5434 map.offset_size, map.dwarf5_byte_order);
5435 return map.entry_pool + namei_entry_offs;
5436 }
5437
5438 /* See dw2_debug_names_iterator. */
5439
5440 dwarf2_per_cu_data *
5441 dw2_debug_names_iterator::next ()
5442 {
5443 if (m_addr == NULL)
5444 return NULL;
5445
5446 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5447 struct objfile *objfile = dwarf2_per_objfile->objfile;
5448 bfd *const abfd = objfile->obfd;
5449
5450 again:
5451
5452 unsigned int bytes_read;
5453 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5454 m_addr += bytes_read;
5455 if (abbrev == 0)
5456 return NULL;
5457
5458 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5459 if (indexval_it == m_map.abbrev_map.cend ())
5460 {
5461 complaint (_("Wrong .debug_names undefined abbrev code %s "
5462 "[in module %s]"),
5463 pulongest (abbrev), objfile_name (objfile));
5464 return NULL;
5465 }
5466 const mapped_debug_names::index_val &indexval = indexval_it->second;
5467 enum class symbol_linkage {
5468 unknown,
5469 static_,
5470 extern_,
5471 } symbol_linkage_ = symbol_linkage::unknown;
5472 dwarf2_per_cu_data *per_cu = NULL;
5473 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5474 {
5475 ULONGEST ull;
5476 switch (attr.form)
5477 {
5478 case DW_FORM_implicit_const:
5479 ull = attr.implicit_const;
5480 break;
5481 case DW_FORM_flag_present:
5482 ull = 1;
5483 break;
5484 case DW_FORM_udata:
5485 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5486 m_addr += bytes_read;
5487 break;
5488 case DW_FORM_ref4:
5489 ull = read_4_bytes (abfd, m_addr);
5490 m_addr += 4;
5491 break;
5492 case DW_FORM_ref8:
5493 ull = read_8_bytes (abfd, m_addr);
5494 m_addr += 8;
5495 break;
5496 case DW_FORM_ref_sig8:
5497 ull = read_8_bytes (abfd, m_addr);
5498 m_addr += 8;
5499 break;
5500 default:
5501 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5502 dwarf_form_name (attr.form),
5503 objfile_name (objfile));
5504 return NULL;
5505 }
5506 switch (attr.dw_idx)
5507 {
5508 case DW_IDX_compile_unit:
5509 /* Don't crash on bad data. */
5510 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5511 {
5512 complaint (_(".debug_names entry has bad CU index %s"
5513 " [in module %s]"),
5514 pulongest (ull),
5515 objfile_name (dwarf2_per_objfile->objfile));
5516 continue;
5517 }
5518 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5519 break;
5520 case DW_IDX_type_unit:
5521 /* Don't crash on bad data. */
5522 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5523 {
5524 complaint (_(".debug_names entry has bad TU index %s"
5525 " [in module %s]"),
5526 pulongest (ull),
5527 objfile_name (dwarf2_per_objfile->objfile));
5528 continue;
5529 }
5530 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5531 break;
5532 case DW_IDX_die_offset:
5533 /* In a per-CU index (as opposed to a per-module index), index
5534 entries without CU attribute implicitly refer to the single CU. */
5535 if (per_cu == NULL)
5536 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5537 break;
5538 case DW_IDX_GNU_internal:
5539 if (!m_map.augmentation_is_gdb)
5540 break;
5541 symbol_linkage_ = symbol_linkage::static_;
5542 break;
5543 case DW_IDX_GNU_external:
5544 if (!m_map.augmentation_is_gdb)
5545 break;
5546 symbol_linkage_ = symbol_linkage::extern_;
5547 break;
5548 }
5549 }
5550
5551 /* Skip if already read in. */
5552 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5553 goto again;
5554
5555 /* Check static vs global. */
5556 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5557 {
5558 const bool want_static = *m_block_index == STATIC_BLOCK;
5559 const bool symbol_is_static =
5560 symbol_linkage_ == symbol_linkage::static_;
5561 if (want_static != symbol_is_static)
5562 goto again;
5563 }
5564
5565 /* Match dw2_symtab_iter_next, symbol_kind
5566 and debug_names::psymbol_tag. */
5567 switch (m_domain)
5568 {
5569 case VAR_DOMAIN:
5570 switch (indexval.dwarf_tag)
5571 {
5572 case DW_TAG_variable:
5573 case DW_TAG_subprogram:
5574 /* Some types are also in VAR_DOMAIN. */
5575 case DW_TAG_typedef:
5576 case DW_TAG_structure_type:
5577 break;
5578 default:
5579 goto again;
5580 }
5581 break;
5582 case STRUCT_DOMAIN:
5583 switch (indexval.dwarf_tag)
5584 {
5585 case DW_TAG_typedef:
5586 case DW_TAG_structure_type:
5587 break;
5588 default:
5589 goto again;
5590 }
5591 break;
5592 case LABEL_DOMAIN:
5593 switch (indexval.dwarf_tag)
5594 {
5595 case 0:
5596 case DW_TAG_variable:
5597 break;
5598 default:
5599 goto again;
5600 }
5601 break;
5602 case MODULE_DOMAIN:
5603 switch (indexval.dwarf_tag)
5604 {
5605 case DW_TAG_module:
5606 break;
5607 default:
5608 goto again;
5609 }
5610 break;
5611 default:
5612 break;
5613 }
5614
5615 /* Match dw2_expand_symtabs_matching, symbol_kind and
5616 debug_names::psymbol_tag. */
5617 switch (m_search)
5618 {
5619 case VARIABLES_DOMAIN:
5620 switch (indexval.dwarf_tag)
5621 {
5622 case DW_TAG_variable:
5623 break;
5624 default:
5625 goto again;
5626 }
5627 break;
5628 case FUNCTIONS_DOMAIN:
5629 switch (indexval.dwarf_tag)
5630 {
5631 case DW_TAG_subprogram:
5632 break;
5633 default:
5634 goto again;
5635 }
5636 break;
5637 case TYPES_DOMAIN:
5638 switch (indexval.dwarf_tag)
5639 {
5640 case DW_TAG_typedef:
5641 case DW_TAG_structure_type:
5642 break;
5643 default:
5644 goto again;
5645 }
5646 break;
5647 case MODULES_DOMAIN:
5648 switch (indexval.dwarf_tag)
5649 {
5650 case DW_TAG_module:
5651 break;
5652 default:
5653 goto again;
5654 }
5655 default:
5656 break;
5657 }
5658
5659 return per_cu;
5660 }
5661
5662 static struct compunit_symtab *
5663 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5664 const char *name, domain_enum domain)
5665 {
5666 struct dwarf2_per_objfile *dwarf2_per_objfile
5667 = get_dwarf2_per_objfile (objfile);
5668
5669 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5670 if (!mapp)
5671 {
5672 /* index is NULL if OBJF_READNOW. */
5673 return NULL;
5674 }
5675 const auto &map = *mapp;
5676
5677 dw2_debug_names_iterator iter (map, block_index, domain, name);
5678
5679 struct compunit_symtab *stab_best = NULL;
5680 struct dwarf2_per_cu_data *per_cu;
5681 while ((per_cu = iter.next ()) != NULL)
5682 {
5683 struct symbol *sym, *with_opaque = NULL;
5684 compunit_symtab *stab
5685 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5686 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5687 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5688
5689 sym = block_find_symbol (block, name, domain,
5690 block_find_non_opaque_type_preferred,
5691 &with_opaque);
5692
5693 /* Some caution must be observed with overloaded functions and
5694 methods, since the index will not contain any overload
5695 information (but NAME might contain it). */
5696
5697 if (sym != NULL
5698 && strcmp_iw (sym->search_name (), name) == 0)
5699 return stab;
5700 if (with_opaque != NULL
5701 && strcmp_iw (with_opaque->search_name (), name) == 0)
5702 stab_best = stab;
5703
5704 /* Keep looking through other CUs. */
5705 }
5706
5707 return stab_best;
5708 }
5709
5710 /* This dumps minimal information about .debug_names. It is called
5711 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5712 uses this to verify that .debug_names has been loaded. */
5713
5714 static void
5715 dw2_debug_names_dump (struct objfile *objfile)
5716 {
5717 struct dwarf2_per_objfile *dwarf2_per_objfile
5718 = get_dwarf2_per_objfile (objfile);
5719
5720 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5721 printf_filtered (".debug_names:");
5722 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5723 printf_filtered (" exists\n");
5724 else
5725 printf_filtered (" faked for \"readnow\"\n");
5726 printf_filtered ("\n");
5727 }
5728
5729 static void
5730 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5731 const char *func_name)
5732 {
5733 struct dwarf2_per_objfile *dwarf2_per_objfile
5734 = get_dwarf2_per_objfile (objfile);
5735
5736 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5737 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5738 {
5739 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5740
5741 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5742
5743 struct dwarf2_per_cu_data *per_cu;
5744 while ((per_cu = iter.next ()) != NULL)
5745 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5746 }
5747 }
5748
5749 static void
5750 dw2_debug_names_map_matching_symbols
5751 (struct objfile *objfile,
5752 const lookup_name_info &name, domain_enum domain,
5753 int global,
5754 gdb::function_view<symbol_found_callback_ftype> callback,
5755 symbol_compare_ftype *ordered_compare)
5756 {
5757 struct dwarf2_per_objfile *dwarf2_per_objfile
5758 = get_dwarf2_per_objfile (objfile);
5759
5760 /* debug_names_table is NULL if OBJF_READNOW. */
5761 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5762 return;
5763
5764 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5765 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5766
5767 const char *match_name = name.ada ().lookup_name ().c_str ();
5768 auto matcher = [&] (const char *symname)
5769 {
5770 if (ordered_compare == nullptr)
5771 return true;
5772 return ordered_compare (symname, match_name) == 0;
5773 };
5774
5775 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5776 [&] (offset_type namei)
5777 {
5778 /* The name was matched, now expand corresponding CUs that were
5779 marked. */
5780 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5781
5782 struct dwarf2_per_cu_data *per_cu;
5783 while ((per_cu = iter.next ()) != NULL)
5784 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5785 nullptr);
5786 return true;
5787 });
5788
5789 /* It's a shame we couldn't do this inside the
5790 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5791 that have already been expanded. Instead, this loop matches what
5792 the psymtab code does. */
5793 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5794 {
5795 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5796 if (symtab != nullptr)
5797 {
5798 const struct block *block
5799 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5800 if (!iterate_over_symbols_terminated (block, name,
5801 domain, callback))
5802 break;
5803 }
5804 }
5805 }
5806
5807 static void
5808 dw2_debug_names_expand_symtabs_matching
5809 (struct objfile *objfile,
5810 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5811 const lookup_name_info *lookup_name,
5812 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5813 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5814 enum search_domain kind)
5815 {
5816 struct dwarf2_per_objfile *dwarf2_per_objfile
5817 = get_dwarf2_per_objfile (objfile);
5818
5819 /* debug_names_table is NULL if OBJF_READNOW. */
5820 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5821 return;
5822
5823 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5824
5825 if (symbol_matcher == NULL && lookup_name == NULL)
5826 {
5827 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5828 {
5829 QUIT;
5830
5831 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5832 file_matcher, expansion_notify);
5833 }
5834 return;
5835 }
5836
5837 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5838
5839 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5840 symbol_matcher,
5841 kind, [&] (offset_type namei)
5842 {
5843 /* The name was matched, now expand corresponding CUs that were
5844 marked. */
5845 dw2_debug_names_iterator iter (map, kind, namei);
5846
5847 struct dwarf2_per_cu_data *per_cu;
5848 while ((per_cu = iter.next ()) != NULL)
5849 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5850 file_matcher, expansion_notify);
5851 return true;
5852 });
5853 }
5854
5855 const struct quick_symbol_functions dwarf2_debug_names_functions =
5856 {
5857 dw2_has_symbols,
5858 dw2_find_last_source_symtab,
5859 dw2_forget_cached_source_info,
5860 dw2_map_symtabs_matching_filename,
5861 dw2_debug_names_lookup_symbol,
5862 NULL,
5863 dw2_print_stats,
5864 dw2_debug_names_dump,
5865 dw2_debug_names_expand_symtabs_for_function,
5866 dw2_expand_all_symtabs,
5867 dw2_expand_symtabs_with_fullname,
5868 dw2_debug_names_map_matching_symbols,
5869 dw2_debug_names_expand_symtabs_matching,
5870 dw2_find_pc_sect_compunit_symtab,
5871 NULL,
5872 dw2_map_symbol_filenames
5873 };
5874
5875 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5876 to either a dwarf2_per_bfd or dwz_file object. */
5877
5878 template <typename T>
5879 static gdb::array_view<const gdb_byte>
5880 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5881 {
5882 dwarf2_section_info *section = &section_owner->gdb_index;
5883
5884 if (section->empty ())
5885 return {};
5886
5887 /* Older elfutils strip versions could keep the section in the main
5888 executable while splitting it for the separate debug info file. */
5889 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5890 return {};
5891
5892 section->read (obj);
5893
5894 /* dwarf2_section_info::size is a bfd_size_type, while
5895 gdb::array_view works with size_t. On 32-bit hosts, with
5896 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5897 is 32-bit. So we need an explicit narrowing conversion here.
5898 This is fine, because it's impossible to allocate or mmap an
5899 array/buffer larger than what size_t can represent. */
5900 return gdb::make_array_view (section->buffer, section->size);
5901 }
5902
5903 /* Lookup the index cache for the contents of the index associated to
5904 DWARF2_OBJ. */
5905
5906 static gdb::array_view<const gdb_byte>
5907 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5908 {
5909 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5910 if (build_id == nullptr)
5911 return {};
5912
5913 return global_index_cache.lookup_gdb_index (build_id,
5914 &dwarf2_per_bfd->index_cache_res);
5915 }
5916
5917 /* Same as the above, but for DWZ. */
5918
5919 static gdb::array_view<const gdb_byte>
5920 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5921 {
5922 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5923 if (build_id == nullptr)
5924 return {};
5925
5926 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5927 }
5928
5929 /* See symfile.h. */
5930
5931 bool
5932 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5933 {
5934 struct dwarf2_per_objfile *dwarf2_per_objfile
5935 = get_dwarf2_per_objfile (objfile);
5936
5937 /* If we're about to read full symbols, don't bother with the
5938 indices. In this case we also don't care if some other debug
5939 format is making psymtabs, because they are all about to be
5940 expanded anyway. */
5941 if ((objfile->flags & OBJF_READNOW))
5942 {
5943 dwarf2_per_objfile->per_bfd->using_index = 1;
5944 create_all_comp_units (dwarf2_per_objfile);
5945 create_all_type_units (dwarf2_per_objfile);
5946 dwarf2_per_objfile->per_bfd->quick_file_names_table
5947 = create_quick_file_names_table
5948 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5949 dwarf2_per_objfile->resize_symtabs ();
5950
5951 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5952 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5953 {
5954 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5955
5956 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5957 struct dwarf2_per_cu_quick_data);
5958 }
5959
5960 /* Return 1 so that gdb sees the "quick" functions. However,
5961 these functions will be no-ops because we will have expanded
5962 all symtabs. */
5963 *index_kind = dw_index_kind::GDB_INDEX;
5964 return true;
5965 }
5966
5967 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5968 {
5969 *index_kind = dw_index_kind::DEBUG_NAMES;
5970 dwarf2_per_objfile->resize_symtabs ();
5971 return true;
5972 }
5973
5974 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5975 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5976 get_gdb_index_contents_from_section<dwz_file>))
5977 {
5978 *index_kind = dw_index_kind::GDB_INDEX;
5979 dwarf2_per_objfile->resize_symtabs ();
5980 return true;
5981 }
5982
5983 /* ... otherwise, try to find the index in the index cache. */
5984 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5985 get_gdb_index_contents_from_cache,
5986 get_gdb_index_contents_from_cache_dwz))
5987 {
5988 global_index_cache.hit ();
5989 *index_kind = dw_index_kind::GDB_INDEX;
5990 dwarf2_per_objfile->resize_symtabs ();
5991 return true;
5992 }
5993
5994 global_index_cache.miss ();
5995 return false;
5996 }
5997
5998 \f
5999
6000 /* Build a partial symbol table. */
6001
6002 void
6003 dwarf2_build_psymtabs (struct objfile *objfile)
6004 {
6005 struct dwarf2_per_objfile *dwarf2_per_objfile
6006 = get_dwarf2_per_objfile (objfile);
6007
6008 init_psymbol_list (objfile, 1024);
6009
6010 try
6011 {
6012 /* This isn't really ideal: all the data we allocate on the
6013 objfile's obstack is still uselessly kept around. However,
6014 freeing it seems unsafe. */
6015 psymtab_discarder psymtabs (objfile);
6016 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6017 psymtabs.keep ();
6018
6019 dwarf2_per_objfile->resize_symtabs ();
6020
6021 /* (maybe) store an index in the cache. */
6022 global_index_cache.store (dwarf2_per_objfile);
6023 }
6024 catch (const gdb_exception_error &except)
6025 {
6026 exception_print (gdb_stderr, except);
6027 }
6028 }
6029
6030 /* Find the base address of the compilation unit for range lists and
6031 location lists. It will normally be specified by DW_AT_low_pc.
6032 In DWARF-3 draft 4, the base address could be overridden by
6033 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6034 compilation units with discontinuous ranges. */
6035
6036 static void
6037 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6038 {
6039 struct attribute *attr;
6040
6041 cu->base_address.reset ();
6042
6043 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6044 if (attr != nullptr)
6045 cu->base_address = attr->value_as_address ();
6046 else
6047 {
6048 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6049 if (attr != nullptr)
6050 cu->base_address = attr->value_as_address ();
6051 }
6052 }
6053
6054 /* Helper function that returns the proper abbrev section for
6055 THIS_CU. */
6056
6057 static struct dwarf2_section_info *
6058 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6059 {
6060 struct dwarf2_section_info *abbrev;
6061 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6062
6063 if (this_cu->is_dwz)
6064 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6065 else
6066 abbrev = &per_bfd->abbrev;
6067
6068 return abbrev;
6069 }
6070
6071 /* Fetch the abbreviation table offset from a comp or type unit header. */
6072
6073 static sect_offset
6074 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6075 struct dwarf2_section_info *section,
6076 sect_offset sect_off)
6077 {
6078 bfd *abfd = section->get_bfd_owner ();
6079 const gdb_byte *info_ptr;
6080 unsigned int initial_length_size, offset_size;
6081 uint16_t version;
6082
6083 section->read (dwarf2_per_objfile->objfile);
6084 info_ptr = section->buffer + to_underlying (sect_off);
6085 read_initial_length (abfd, info_ptr, &initial_length_size);
6086 offset_size = initial_length_size == 4 ? 4 : 8;
6087 info_ptr += initial_length_size;
6088
6089 version = read_2_bytes (abfd, info_ptr);
6090 info_ptr += 2;
6091 if (version >= 5)
6092 {
6093 /* Skip unit type and address size. */
6094 info_ptr += 2;
6095 }
6096
6097 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6098 }
6099
6100 /* A partial symtab that is used only for include files. */
6101 struct dwarf2_include_psymtab : public partial_symtab
6102 {
6103 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6104 : partial_symtab (filename, objfile)
6105 {
6106 }
6107
6108 void read_symtab (struct objfile *objfile) override
6109 {
6110 /* It's an include file, no symbols to read for it.
6111 Everything is in the includer symtab. */
6112
6113 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6114 expansion of the includer psymtab. We use the dependencies[0] field to
6115 model the includer. But if we go the regular route of calling
6116 expand_psymtab here, and having expand_psymtab call expand_dependencies
6117 to expand the includer, we'll only use expand_psymtab on the includer
6118 (making it a non-toplevel psymtab), while if we expand the includer via
6119 another path, we'll use read_symtab (making it a toplevel psymtab).
6120 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6121 psymtab, and trigger read_symtab on the includer here directly. */
6122 includer ()->read_symtab (objfile);
6123 }
6124
6125 void expand_psymtab (struct objfile *objfile) override
6126 {
6127 /* This is not called by read_symtab, and should not be called by any
6128 expand_dependencies. */
6129 gdb_assert (false);
6130 }
6131
6132 bool readin_p (struct objfile *objfile) const override
6133 {
6134 return includer ()->readin_p (objfile);
6135 }
6136
6137 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6138 {
6139 return nullptr;
6140 }
6141
6142 private:
6143 partial_symtab *includer () const
6144 {
6145 /* An include psymtab has exactly one dependency: the psymtab that
6146 includes it. */
6147 gdb_assert (this->number_of_dependencies == 1);
6148 return this->dependencies[0];
6149 }
6150 };
6151
6152 /* Allocate a new partial symtab for file named NAME and mark this new
6153 partial symtab as being an include of PST. */
6154
6155 static void
6156 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6157 struct objfile *objfile)
6158 {
6159 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6160
6161 if (!IS_ABSOLUTE_PATH (subpst->filename))
6162 subpst->dirname = pst->dirname;
6163
6164 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6165 subpst->dependencies[0] = pst;
6166 subpst->number_of_dependencies = 1;
6167 }
6168
6169 /* Read the Line Number Program data and extract the list of files
6170 included by the source file represented by PST. Build an include
6171 partial symtab for each of these included files. */
6172
6173 static void
6174 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6175 struct die_info *die,
6176 dwarf2_psymtab *pst)
6177 {
6178 line_header_up lh;
6179 struct attribute *attr;
6180
6181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6182 if (attr != nullptr)
6183 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6184 if (lh == NULL)
6185 return; /* No linetable, so no includes. */
6186
6187 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6188 that we pass in the raw text_low here; that is ok because we're
6189 only decoding the line table to make include partial symtabs, and
6190 so the addresses aren't really used. */
6191 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6192 pst->raw_text_low (), 1);
6193 }
6194
6195 static hashval_t
6196 hash_signatured_type (const void *item)
6197 {
6198 const struct signatured_type *sig_type
6199 = (const struct signatured_type *) item;
6200
6201 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6202 return sig_type->signature;
6203 }
6204
6205 static int
6206 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6207 {
6208 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6209 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6210
6211 return lhs->signature == rhs->signature;
6212 }
6213
6214 /* Allocate a hash table for signatured types. */
6215
6216 static htab_up
6217 allocate_signatured_type_table ()
6218 {
6219 return htab_up (htab_create_alloc (41,
6220 hash_signatured_type,
6221 eq_signatured_type,
6222 NULL, xcalloc, xfree));
6223 }
6224
6225 /* A helper function to add a signatured type CU to a table. */
6226
6227 static int
6228 add_signatured_type_cu_to_table (void **slot, void *datum)
6229 {
6230 struct signatured_type *sigt = (struct signatured_type *) *slot;
6231 std::vector<signatured_type *> *all_type_units
6232 = (std::vector<signatured_type *> *) datum;
6233
6234 all_type_units->push_back (sigt);
6235
6236 return 1;
6237 }
6238
6239 /* A helper for create_debug_types_hash_table. Read types from SECTION
6240 and fill them into TYPES_HTAB. It will process only type units,
6241 therefore DW_UT_type. */
6242
6243 static void
6244 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6245 struct dwo_file *dwo_file,
6246 dwarf2_section_info *section, htab_up &types_htab,
6247 rcuh_kind section_kind)
6248 {
6249 struct objfile *objfile = dwarf2_per_objfile->objfile;
6250 struct dwarf2_section_info *abbrev_section;
6251 bfd *abfd;
6252 const gdb_byte *info_ptr, *end_ptr;
6253
6254 abbrev_section = (dwo_file != NULL
6255 ? &dwo_file->sections.abbrev
6256 : &dwarf2_per_objfile->per_bfd->abbrev);
6257
6258 if (dwarf_read_debug)
6259 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6260 section->get_name (),
6261 abbrev_section->get_file_name ());
6262
6263 section->read (objfile);
6264 info_ptr = section->buffer;
6265
6266 if (info_ptr == NULL)
6267 return;
6268
6269 /* We can't set abfd until now because the section may be empty or
6270 not present, in which case the bfd is unknown. */
6271 abfd = section->get_bfd_owner ();
6272
6273 /* We don't use cutu_reader here because we don't need to read
6274 any dies: the signature is in the header. */
6275
6276 end_ptr = info_ptr + section->size;
6277 while (info_ptr < end_ptr)
6278 {
6279 struct signatured_type *sig_type;
6280 struct dwo_unit *dwo_tu;
6281 void **slot;
6282 const gdb_byte *ptr = info_ptr;
6283 struct comp_unit_head header;
6284 unsigned int length;
6285
6286 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6287
6288 /* Initialize it due to a false compiler warning. */
6289 header.signature = -1;
6290 header.type_cu_offset_in_tu = (cu_offset) -1;
6291
6292 /* We need to read the type's signature in order to build the hash
6293 table, but we don't need anything else just yet. */
6294
6295 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6296 abbrev_section, ptr, section_kind);
6297
6298 length = header.get_length ();
6299
6300 /* Skip dummy type units. */
6301 if (ptr >= info_ptr + length
6302 || peek_abbrev_code (abfd, ptr) == 0
6303 || header.unit_type != DW_UT_type)
6304 {
6305 info_ptr += length;
6306 continue;
6307 }
6308
6309 if (types_htab == NULL)
6310 {
6311 if (dwo_file)
6312 types_htab = allocate_dwo_unit_table ();
6313 else
6314 types_htab = allocate_signatured_type_table ();
6315 }
6316
6317 if (dwo_file)
6318 {
6319 sig_type = NULL;
6320 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6321 struct dwo_unit);
6322 dwo_tu->dwo_file = dwo_file;
6323 dwo_tu->signature = header.signature;
6324 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6325 dwo_tu->section = section;
6326 dwo_tu->sect_off = sect_off;
6327 dwo_tu->length = length;
6328 }
6329 else
6330 {
6331 /* N.B.: type_offset is not usable if this type uses a DWO file.
6332 The real type_offset is in the DWO file. */
6333 dwo_tu = NULL;
6334 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6335 sig_type->signature = header.signature;
6336 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6337 sig_type->per_cu.is_debug_types = 1;
6338 sig_type->per_cu.section = section;
6339 sig_type->per_cu.sect_off = sect_off;
6340 sig_type->per_cu.length = length;
6341 }
6342
6343 slot = htab_find_slot (types_htab.get (),
6344 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6345 INSERT);
6346 gdb_assert (slot != NULL);
6347 if (*slot != NULL)
6348 {
6349 sect_offset dup_sect_off;
6350
6351 if (dwo_file)
6352 {
6353 const struct dwo_unit *dup_tu
6354 = (const struct dwo_unit *) *slot;
6355
6356 dup_sect_off = dup_tu->sect_off;
6357 }
6358 else
6359 {
6360 const struct signatured_type *dup_tu
6361 = (const struct signatured_type *) *slot;
6362
6363 dup_sect_off = dup_tu->per_cu.sect_off;
6364 }
6365
6366 complaint (_("debug type entry at offset %s is duplicate to"
6367 " the entry at offset %s, signature %s"),
6368 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6369 hex_string (header.signature));
6370 }
6371 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6372
6373 if (dwarf_read_debug > 1)
6374 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6375 sect_offset_str (sect_off),
6376 hex_string (header.signature));
6377
6378 info_ptr += length;
6379 }
6380 }
6381
6382 /* Create the hash table of all entries in the .debug_types
6383 (or .debug_types.dwo) section(s).
6384 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6385 otherwise it is NULL.
6386
6387 The result is a pointer to the hash table or NULL if there are no types.
6388
6389 Note: This function processes DWO files only, not DWP files. */
6390
6391 static void
6392 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6393 struct dwo_file *dwo_file,
6394 gdb::array_view<dwarf2_section_info> type_sections,
6395 htab_up &types_htab)
6396 {
6397 for (dwarf2_section_info &section : type_sections)
6398 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6399 types_htab, rcuh_kind::TYPE);
6400 }
6401
6402 /* Create the hash table of all entries in the .debug_types section,
6403 and initialize all_type_units.
6404 The result is zero if there is an error (e.g. missing .debug_types section),
6405 otherwise non-zero. */
6406
6407 static int
6408 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6409 {
6410 htab_up types_htab;
6411
6412 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6413 &dwarf2_per_objfile->per_bfd->info, types_htab,
6414 rcuh_kind::COMPILE);
6415 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6416 dwarf2_per_objfile->per_bfd->types, types_htab);
6417 if (types_htab == NULL)
6418 {
6419 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6420 return 0;
6421 }
6422
6423 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6424
6425 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6426 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6427 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6428
6429 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6430 add_signatured_type_cu_to_table,
6431 &dwarf2_per_objfile->per_bfd->all_type_units);
6432
6433 return 1;
6434 }
6435
6436 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6437 If SLOT is non-NULL, it is the entry to use in the hash table.
6438 Otherwise we find one. */
6439
6440 static struct signatured_type *
6441 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6442 void **slot)
6443 {
6444 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6445 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6446 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6447
6448 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6449
6450 dwarf2_per_objfile->resize_symtabs ();
6451
6452 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6453 sig_type->signature = sig;
6454 sig_type->per_cu.is_debug_types = 1;
6455 if (dwarf2_per_objfile->per_bfd->using_index)
6456 {
6457 sig_type->per_cu.v.quick =
6458 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6459 struct dwarf2_per_cu_quick_data);
6460 }
6461
6462 if (slot == NULL)
6463 {
6464 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6465 sig_type, INSERT);
6466 }
6467 gdb_assert (*slot == NULL);
6468 *slot = sig_type;
6469 /* The rest of sig_type must be filled in by the caller. */
6470 return sig_type;
6471 }
6472
6473 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6474 Fill in SIG_ENTRY with DWO_ENTRY. */
6475
6476 static void
6477 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6478 struct signatured_type *sig_entry,
6479 struct dwo_unit *dwo_entry)
6480 {
6481 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6482
6483 /* Make sure we're not clobbering something we don't expect to. */
6484 gdb_assert (! sig_entry->per_cu.queued);
6485 gdb_assert (sig_entry->per_cu.cu == NULL);
6486 if (per_bfd->using_index)
6487 {
6488 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6489 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6490 }
6491 else
6492 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6493 gdb_assert (sig_entry->signature == dwo_entry->signature);
6494 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6495 gdb_assert (sig_entry->type_unit_group == NULL);
6496 gdb_assert (sig_entry->dwo_unit == NULL);
6497
6498 sig_entry->per_cu.section = dwo_entry->section;
6499 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6500 sig_entry->per_cu.length = dwo_entry->length;
6501 sig_entry->per_cu.reading_dwo_directly = 1;
6502 sig_entry->per_cu.per_bfd = per_bfd;
6503 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6504 sig_entry->dwo_unit = dwo_entry;
6505 }
6506
6507 /* Subroutine of lookup_signatured_type.
6508 If we haven't read the TU yet, create the signatured_type data structure
6509 for a TU to be read in directly from a DWO file, bypassing the stub.
6510 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6511 using .gdb_index, then when reading a CU we want to stay in the DWO file
6512 containing that CU. Otherwise we could end up reading several other DWO
6513 files (due to comdat folding) to process the transitive closure of all the
6514 mentioned TUs, and that can be slow. The current DWO file will have every
6515 type signature that it needs.
6516 We only do this for .gdb_index because in the psymtab case we already have
6517 to read all the DWOs to build the type unit groups. */
6518
6519 static struct signatured_type *
6520 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6521 {
6522 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6523 struct dwo_file *dwo_file;
6524 struct dwo_unit find_dwo_entry, *dwo_entry;
6525 struct signatured_type find_sig_entry, *sig_entry;
6526 void **slot;
6527
6528 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6529
6530 /* If TU skeletons have been removed then we may not have read in any
6531 TUs yet. */
6532 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6533 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6534
6535 /* We only ever need to read in one copy of a signatured type.
6536 Use the global signatured_types array to do our own comdat-folding
6537 of types. If this is the first time we're reading this TU, and
6538 the TU has an entry in .gdb_index, replace the recorded data from
6539 .gdb_index with this TU. */
6540
6541 find_sig_entry.signature = sig;
6542 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6543 &find_sig_entry, INSERT);
6544 sig_entry = (struct signatured_type *) *slot;
6545
6546 /* We can get here with the TU already read, *or* in the process of being
6547 read. Don't reassign the global entry to point to this DWO if that's
6548 the case. Also note that if the TU is already being read, it may not
6549 have come from a DWO, the program may be a mix of Fission-compiled
6550 code and non-Fission-compiled code. */
6551
6552 /* Have we already tried to read this TU?
6553 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6554 needn't exist in the global table yet). */
6555 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6556 return sig_entry;
6557
6558 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6559 dwo_unit of the TU itself. */
6560 dwo_file = cu->dwo_unit->dwo_file;
6561
6562 /* Ok, this is the first time we're reading this TU. */
6563 if (dwo_file->tus == NULL)
6564 return NULL;
6565 find_dwo_entry.signature = sig;
6566 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6567 &find_dwo_entry);
6568 if (dwo_entry == NULL)
6569 return NULL;
6570
6571 /* If the global table doesn't have an entry for this TU, add one. */
6572 if (sig_entry == NULL)
6573 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6574
6575 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6576 sig_entry->per_cu.tu_read = 1;
6577 return sig_entry;
6578 }
6579
6580 /* Subroutine of lookup_signatured_type.
6581 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6582 then try the DWP file. If the TU stub (skeleton) has been removed then
6583 it won't be in .gdb_index. */
6584
6585 static struct signatured_type *
6586 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6587 {
6588 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6589 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6590 struct dwo_unit *dwo_entry;
6591 struct signatured_type find_sig_entry, *sig_entry;
6592 void **slot;
6593
6594 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6595 gdb_assert (dwp_file != NULL);
6596
6597 /* If TU skeletons have been removed then we may not have read in any
6598 TUs yet. */
6599 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6600 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6601
6602 find_sig_entry.signature = sig;
6603 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6604 &find_sig_entry, INSERT);
6605 sig_entry = (struct signatured_type *) *slot;
6606
6607 /* Have we already tried to read this TU?
6608 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6609 needn't exist in the global table yet). */
6610 if (sig_entry != NULL)
6611 return sig_entry;
6612
6613 if (dwp_file->tus == NULL)
6614 return NULL;
6615 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6616 sig, 1 /* is_debug_types */);
6617 if (dwo_entry == NULL)
6618 return NULL;
6619
6620 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6621 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6622
6623 return sig_entry;
6624 }
6625
6626 /* Lookup a signature based type for DW_FORM_ref_sig8.
6627 Returns NULL if signature SIG is not present in the table.
6628 It is up to the caller to complain about this. */
6629
6630 static struct signatured_type *
6631 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6632 {
6633 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6634
6635 if (cu->dwo_unit
6636 && dwarf2_per_objfile->per_bfd->using_index)
6637 {
6638 /* We're in a DWO/DWP file, and we're using .gdb_index.
6639 These cases require special processing. */
6640 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6641 return lookup_dwo_signatured_type (cu, sig);
6642 else
6643 return lookup_dwp_signatured_type (cu, sig);
6644 }
6645 else
6646 {
6647 struct signatured_type find_entry, *entry;
6648
6649 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6650 return NULL;
6651 find_entry.signature = sig;
6652 entry = ((struct signatured_type *)
6653 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6654 &find_entry));
6655 return entry;
6656 }
6657 }
6658
6659 /* Low level DIE reading support. */
6660
6661 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6662
6663 static void
6664 init_cu_die_reader (struct die_reader_specs *reader,
6665 struct dwarf2_cu *cu,
6666 struct dwarf2_section_info *section,
6667 struct dwo_file *dwo_file,
6668 struct abbrev_table *abbrev_table)
6669 {
6670 gdb_assert (section->readin && section->buffer != NULL);
6671 reader->abfd = section->get_bfd_owner ();
6672 reader->cu = cu;
6673 reader->dwo_file = dwo_file;
6674 reader->die_section = section;
6675 reader->buffer = section->buffer;
6676 reader->buffer_end = section->buffer + section->size;
6677 reader->abbrev_table = abbrev_table;
6678 }
6679
6680 /* Subroutine of cutu_reader to simplify it.
6681 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6682 There's just a lot of work to do, and cutu_reader is big enough
6683 already.
6684
6685 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6686 from it to the DIE in the DWO. If NULL we are skipping the stub.
6687 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6688 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6689 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6690 STUB_COMP_DIR may be non-NULL.
6691 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6692 are filled in with the info of the DIE from the DWO file.
6693 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6694 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6695 kept around for at least as long as *RESULT_READER.
6696
6697 The result is non-zero if a valid (non-dummy) DIE was found. */
6698
6699 static int
6700 read_cutu_die_from_dwo (dwarf2_cu *cu,
6701 struct dwo_unit *dwo_unit,
6702 struct die_info *stub_comp_unit_die,
6703 const char *stub_comp_dir,
6704 struct die_reader_specs *result_reader,
6705 const gdb_byte **result_info_ptr,
6706 struct die_info **result_comp_unit_die,
6707 abbrev_table_up *result_dwo_abbrev_table)
6708 {
6709 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6710 dwarf2_per_cu_data *per_cu = cu->per_cu;
6711 struct objfile *objfile = dwarf2_per_objfile->objfile;
6712 bfd *abfd;
6713 const gdb_byte *begin_info_ptr, *info_ptr;
6714 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6715 int i,num_extra_attrs;
6716 struct dwarf2_section_info *dwo_abbrev_section;
6717 struct die_info *comp_unit_die;
6718
6719 /* At most one of these may be provided. */
6720 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6721
6722 /* These attributes aren't processed until later:
6723 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6724 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6725 referenced later. However, these attributes are found in the stub
6726 which we won't have later. In order to not impose this complication
6727 on the rest of the code, we read them here and copy them to the
6728 DWO CU/TU die. */
6729
6730 stmt_list = NULL;
6731 low_pc = NULL;
6732 high_pc = NULL;
6733 ranges = NULL;
6734 comp_dir = NULL;
6735
6736 if (stub_comp_unit_die != NULL)
6737 {
6738 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6739 DWO file. */
6740 if (!per_cu->is_debug_types)
6741 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6742 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6743 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6744 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6745 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6746
6747 cu->addr_base = stub_comp_unit_die->addr_base ();
6748
6749 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6750 here (if needed). We need the value before we can process
6751 DW_AT_ranges. */
6752 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6753 }
6754 else if (stub_comp_dir != NULL)
6755 {
6756 /* Reconstruct the comp_dir attribute to simplify the code below. */
6757 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6758 comp_dir->name = DW_AT_comp_dir;
6759 comp_dir->form = DW_FORM_string;
6760 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6761 DW_STRING (comp_dir) = stub_comp_dir;
6762 }
6763
6764 /* Set up for reading the DWO CU/TU. */
6765 cu->dwo_unit = dwo_unit;
6766 dwarf2_section_info *section = dwo_unit->section;
6767 section->read (objfile);
6768 abfd = section->get_bfd_owner ();
6769 begin_info_ptr = info_ptr = (section->buffer
6770 + to_underlying (dwo_unit->sect_off));
6771 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6772
6773 if (per_cu->is_debug_types)
6774 {
6775 signatured_type *sig_type = (struct signatured_type *) per_cu;
6776
6777 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6778 &cu->header, section,
6779 dwo_abbrev_section,
6780 info_ptr, rcuh_kind::TYPE);
6781 /* This is not an assert because it can be caused by bad debug info. */
6782 if (sig_type->signature != cu->header.signature)
6783 {
6784 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6785 " TU at offset %s [in module %s]"),
6786 hex_string (sig_type->signature),
6787 hex_string (cu->header.signature),
6788 sect_offset_str (dwo_unit->sect_off),
6789 bfd_get_filename (abfd));
6790 }
6791 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6792 /* For DWOs coming from DWP files, we don't know the CU length
6793 nor the type's offset in the TU until now. */
6794 dwo_unit->length = cu->header.get_length ();
6795 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6796
6797 /* Establish the type offset that can be used to lookup the type.
6798 For DWO files, we don't know it until now. */
6799 sig_type->type_offset_in_section
6800 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6801 }
6802 else
6803 {
6804 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6805 &cu->header, section,
6806 dwo_abbrev_section,
6807 info_ptr, rcuh_kind::COMPILE);
6808 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6809 /* For DWOs coming from DWP files, we don't know the CU length
6810 until now. */
6811 dwo_unit->length = cu->header.get_length ();
6812 }
6813
6814 *result_dwo_abbrev_table
6815 = abbrev_table::read (objfile, dwo_abbrev_section,
6816 cu->header.abbrev_sect_off);
6817 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6818 result_dwo_abbrev_table->get ());
6819
6820 /* Read in the die, but leave space to copy over the attributes
6821 from the stub. This has the benefit of simplifying the rest of
6822 the code - all the work to maintain the illusion of a single
6823 DW_TAG_{compile,type}_unit DIE is done here. */
6824 num_extra_attrs = ((stmt_list != NULL)
6825 + (low_pc != NULL)
6826 + (high_pc != NULL)
6827 + (ranges != NULL)
6828 + (comp_dir != NULL));
6829 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6830 num_extra_attrs);
6831
6832 /* Copy over the attributes from the stub to the DIE we just read in. */
6833 comp_unit_die = *result_comp_unit_die;
6834 i = comp_unit_die->num_attrs;
6835 if (stmt_list != NULL)
6836 comp_unit_die->attrs[i++] = *stmt_list;
6837 if (low_pc != NULL)
6838 comp_unit_die->attrs[i++] = *low_pc;
6839 if (high_pc != NULL)
6840 comp_unit_die->attrs[i++] = *high_pc;
6841 if (ranges != NULL)
6842 comp_unit_die->attrs[i++] = *ranges;
6843 if (comp_dir != NULL)
6844 comp_unit_die->attrs[i++] = *comp_dir;
6845 comp_unit_die->num_attrs += num_extra_attrs;
6846
6847 if (dwarf_die_debug)
6848 {
6849 fprintf_unfiltered (gdb_stdlog,
6850 "Read die from %s@0x%x of %s:\n",
6851 section->get_name (),
6852 (unsigned) (begin_info_ptr - section->buffer),
6853 bfd_get_filename (abfd));
6854 dump_die (comp_unit_die, dwarf_die_debug);
6855 }
6856
6857 /* Skip dummy compilation units. */
6858 if (info_ptr >= begin_info_ptr + dwo_unit->length
6859 || peek_abbrev_code (abfd, info_ptr) == 0)
6860 return 0;
6861
6862 *result_info_ptr = info_ptr;
6863 return 1;
6864 }
6865
6866 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6867 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6868 signature is part of the header. */
6869 static gdb::optional<ULONGEST>
6870 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6871 {
6872 if (cu->header.version >= 5)
6873 return cu->header.signature;
6874 struct attribute *attr;
6875 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6876 if (attr == nullptr)
6877 return gdb::optional<ULONGEST> ();
6878 return DW_UNSND (attr);
6879 }
6880
6881 /* Subroutine of cutu_reader to simplify it.
6882 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6883 Returns NULL if the specified DWO unit cannot be found. */
6884
6885 static struct dwo_unit *
6886 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6887 {
6888 dwarf2_per_cu_data *per_cu = cu->per_cu;
6889 struct dwo_unit *dwo_unit;
6890 const char *comp_dir;
6891
6892 gdb_assert (cu != NULL);
6893
6894 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6895 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6896 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6897
6898 if (per_cu->is_debug_types)
6899 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6900 else
6901 {
6902 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6903
6904 if (!signature.has_value ())
6905 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6906 " [in module %s]"),
6907 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6908
6909 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6910 }
6911
6912 return dwo_unit;
6913 }
6914
6915 /* Subroutine of cutu_reader to simplify it.
6916 See it for a description of the parameters.
6917 Read a TU directly from a DWO file, bypassing the stub. */
6918
6919 void
6920 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6921 dwarf2_per_objfile *per_objfile,
6922 int use_existing_cu)
6923 {
6924 struct signatured_type *sig_type;
6925
6926 /* Verify we can do the following downcast, and that we have the
6927 data we need. */
6928 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6929 sig_type = (struct signatured_type *) this_cu;
6930 gdb_assert (sig_type->dwo_unit != NULL);
6931
6932 if (use_existing_cu && this_cu->cu != NULL)
6933 {
6934 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6935 /* There's no need to do the rereading_dwo_cu handling that
6936 cutu_reader does since we don't read the stub. */
6937 }
6938 else
6939 {
6940 /* If !use_existing_cu, this_cu->cu must be NULL. */
6941 gdb_assert (this_cu->cu == NULL);
6942 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6943 }
6944
6945 /* A future optimization, if needed, would be to use an existing
6946 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6947 could share abbrev tables. */
6948
6949 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
6950 NULL /* stub_comp_unit_die */,
6951 sig_type->dwo_unit->dwo_file->comp_dir,
6952 this, &info_ptr,
6953 &comp_unit_die,
6954 &m_dwo_abbrev_table) == 0)
6955 {
6956 /* Dummy die. */
6957 dummy_p = true;
6958 }
6959 }
6960
6961 /* Initialize a CU (or TU) and read its DIEs.
6962 If the CU defers to a DWO file, read the DWO file as well.
6963
6964 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6965 Otherwise the table specified in the comp unit header is read in and used.
6966 This is an optimization for when we already have the abbrev table.
6967
6968 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6969 Otherwise, a new CU is allocated with xmalloc. */
6970
6971 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6972 dwarf2_per_objfile *dwarf2_per_objfile,
6973 struct abbrev_table *abbrev_table,
6974 int use_existing_cu,
6975 bool skip_partial)
6976 : die_reader_specs {},
6977 m_this_cu (this_cu)
6978 {
6979 struct objfile *objfile = dwarf2_per_objfile->objfile;
6980 struct dwarf2_section_info *section = this_cu->section;
6981 bfd *abfd = section->get_bfd_owner ();
6982 struct dwarf2_cu *cu;
6983 const gdb_byte *begin_info_ptr;
6984 struct signatured_type *sig_type = NULL;
6985 struct dwarf2_section_info *abbrev_section;
6986 /* Non-zero if CU currently points to a DWO file and we need to
6987 reread it. When this happens we need to reread the skeleton die
6988 before we can reread the DWO file (this only applies to CUs, not TUs). */
6989 int rereading_dwo_cu = 0;
6990
6991 if (dwarf_die_debug)
6992 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6993 this_cu->is_debug_types ? "type" : "comp",
6994 sect_offset_str (this_cu->sect_off));
6995
6996 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6997 file (instead of going through the stub), short-circuit all of this. */
6998 if (this_cu->reading_dwo_directly)
6999 {
7000 /* Narrow down the scope of possibilities to have to understand. */
7001 gdb_assert (this_cu->is_debug_types);
7002 gdb_assert (abbrev_table == NULL);
7003 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
7004 return;
7005 }
7006
7007 /* This is cheap if the section is already read in. */
7008 section->read (objfile);
7009
7010 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7011
7012 abbrev_section = get_abbrev_section_for_cu (this_cu);
7013
7014 if (use_existing_cu && this_cu->cu != NULL)
7015 {
7016 cu = this_cu->cu;
7017 /* If this CU is from a DWO file we need to start over, we need to
7018 refetch the attributes from the skeleton CU.
7019 This could be optimized by retrieving those attributes from when we
7020 were here the first time: the previous comp_unit_die was stored in
7021 comp_unit_obstack. But there's no data yet that we need this
7022 optimization. */
7023 if (cu->dwo_unit != NULL)
7024 rereading_dwo_cu = 1;
7025 }
7026 else
7027 {
7028 /* If !use_existing_cu, this_cu->cu must be NULL. */
7029 gdb_assert (this_cu->cu == NULL);
7030 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7031 cu = m_new_cu.get ();
7032 }
7033
7034 /* Get the header. */
7035 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7036 {
7037 /* We already have the header, there's no need to read it in again. */
7038 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7039 }
7040 else
7041 {
7042 if (this_cu->is_debug_types)
7043 {
7044 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7045 &cu->header, section,
7046 abbrev_section, info_ptr,
7047 rcuh_kind::TYPE);
7048
7049 /* Since per_cu is the first member of struct signatured_type,
7050 we can go from a pointer to one to a pointer to the other. */
7051 sig_type = (struct signatured_type *) this_cu;
7052 gdb_assert (sig_type->signature == cu->header.signature);
7053 gdb_assert (sig_type->type_offset_in_tu
7054 == cu->header.type_cu_offset_in_tu);
7055 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7056
7057 /* LENGTH has not been set yet for type units if we're
7058 using .gdb_index. */
7059 this_cu->length = cu->header.get_length ();
7060
7061 /* Establish the type offset that can be used to lookup the type. */
7062 sig_type->type_offset_in_section =
7063 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7064
7065 this_cu->dwarf_version = cu->header.version;
7066 }
7067 else
7068 {
7069 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7070 &cu->header, section,
7071 abbrev_section,
7072 info_ptr,
7073 rcuh_kind::COMPILE);
7074
7075 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7076 if (this_cu->length == 0)
7077 this_cu->length = cu->header.get_length ();
7078 else
7079 gdb_assert (this_cu->length == cu->header.get_length ());
7080 this_cu->dwarf_version = cu->header.version;
7081 }
7082 }
7083
7084 /* Skip dummy compilation units. */
7085 if (info_ptr >= begin_info_ptr + this_cu->length
7086 || peek_abbrev_code (abfd, info_ptr) == 0)
7087 {
7088 dummy_p = true;
7089 return;
7090 }
7091
7092 /* If we don't have them yet, read the abbrevs for this compilation unit.
7093 And if we need to read them now, make sure they're freed when we're
7094 done. */
7095 if (abbrev_table != NULL)
7096 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7097 else
7098 {
7099 m_abbrev_table_holder
7100 = abbrev_table::read (objfile, abbrev_section,
7101 cu->header.abbrev_sect_off);
7102 abbrev_table = m_abbrev_table_holder.get ();
7103 }
7104
7105 /* Read the top level CU/TU die. */
7106 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7107 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7108
7109 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7110 {
7111 dummy_p = true;
7112 return;
7113 }
7114
7115 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7116 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7117 table from the DWO file and pass the ownership over to us. It will be
7118 referenced from READER, so we must make sure to free it after we're done
7119 with READER.
7120
7121 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7122 DWO CU, that this test will fail (the attribute will not be present). */
7123 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7124 if (dwo_name != nullptr)
7125 {
7126 struct dwo_unit *dwo_unit;
7127 struct die_info *dwo_comp_unit_die;
7128
7129 if (comp_unit_die->has_children)
7130 {
7131 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7132 " has children (offset %s) [in module %s]"),
7133 sect_offset_str (this_cu->sect_off),
7134 bfd_get_filename (abfd));
7135 }
7136 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7137 if (dwo_unit != NULL)
7138 {
7139 if (read_cutu_die_from_dwo (cu, dwo_unit,
7140 comp_unit_die, NULL,
7141 this, &info_ptr,
7142 &dwo_comp_unit_die,
7143 &m_dwo_abbrev_table) == 0)
7144 {
7145 /* Dummy die. */
7146 dummy_p = true;
7147 return;
7148 }
7149 comp_unit_die = dwo_comp_unit_die;
7150 }
7151 else
7152 {
7153 /* Yikes, we couldn't find the rest of the DIE, we only have
7154 the stub. A complaint has already been logged. There's
7155 not much more we can do except pass on the stub DIE to
7156 die_reader_func. We don't want to throw an error on bad
7157 debug info. */
7158 }
7159 }
7160 }
7161
7162 void
7163 cutu_reader::keep ()
7164 {
7165 /* Done, clean up. */
7166 gdb_assert (!dummy_p);
7167 if (m_new_cu != NULL)
7168 {
7169 /* We know that m_this_cu->cu is set, since we are in the process of
7170 parsing the CU. */
7171 gdb_assert (m_this_cu->cu != nullptr);
7172 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7173
7174 /* Link this CU into read_in_chain. */
7175 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7176 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7177 /* The chain owns it now. */
7178 m_new_cu.release ();
7179 }
7180 }
7181
7182 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7183 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7184 assumed to have already done the lookup to find the DWO file).
7185
7186 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7187 THIS_CU->is_debug_types, but nothing else.
7188
7189 We fill in THIS_CU->length.
7190
7191 THIS_CU->cu is always freed when done.
7192 This is done in order to not leave THIS_CU->cu in a state where we have
7193 to care whether it refers to the "main" CU or the DWO CU.
7194
7195 When parent_cu is passed, it is used to provide a default value for
7196 str_offsets_base and addr_base from the parent. */
7197
7198 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7199 dwarf2_per_objfile *dwarf2_per_objfile,
7200 struct dwarf2_cu *parent_cu,
7201 struct dwo_file *dwo_file)
7202 : die_reader_specs {},
7203 m_this_cu (this_cu)
7204 {
7205 struct objfile *objfile = dwarf2_per_objfile->objfile;
7206 struct dwarf2_section_info *section = this_cu->section;
7207 bfd *abfd = section->get_bfd_owner ();
7208 struct dwarf2_section_info *abbrev_section;
7209 const gdb_byte *begin_info_ptr, *info_ptr;
7210
7211 if (dwarf_die_debug)
7212 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7213 this_cu->is_debug_types ? "type" : "comp",
7214 sect_offset_str (this_cu->sect_off));
7215
7216 gdb_assert (this_cu->cu == NULL);
7217
7218 abbrev_section = (dwo_file != NULL
7219 ? &dwo_file->sections.abbrev
7220 : get_abbrev_section_for_cu (this_cu));
7221
7222 /* This is cheap if the section is already read in. */
7223 section->read (objfile);
7224
7225 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7226
7227 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7228 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7229 &m_new_cu->header, section,
7230 abbrev_section, info_ptr,
7231 (this_cu->is_debug_types
7232 ? rcuh_kind::TYPE
7233 : rcuh_kind::COMPILE));
7234
7235 if (parent_cu != nullptr)
7236 {
7237 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7238 m_new_cu->addr_base = parent_cu->addr_base;
7239 }
7240 this_cu->length = m_new_cu->header.get_length ();
7241
7242 /* Skip dummy compilation units. */
7243 if (info_ptr >= begin_info_ptr + this_cu->length
7244 || peek_abbrev_code (abfd, info_ptr) == 0)
7245 {
7246 dummy_p = true;
7247 return;
7248 }
7249
7250 m_abbrev_table_holder
7251 = abbrev_table::read (objfile, abbrev_section,
7252 m_new_cu->header.abbrev_sect_off);
7253
7254 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7255 m_abbrev_table_holder.get ());
7256 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7257 }
7258
7259 \f
7260 /* Type Unit Groups.
7261
7262 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7263 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7264 so that all types coming from the same compilation (.o file) are grouped
7265 together. A future step could be to put the types in the same symtab as
7266 the CU the types ultimately came from. */
7267
7268 static hashval_t
7269 hash_type_unit_group (const void *item)
7270 {
7271 const struct type_unit_group *tu_group
7272 = (const struct type_unit_group *) item;
7273
7274 return hash_stmt_list_entry (&tu_group->hash);
7275 }
7276
7277 static int
7278 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7279 {
7280 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7281 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7282
7283 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7284 }
7285
7286 /* Allocate a hash table for type unit groups. */
7287
7288 static htab_up
7289 allocate_type_unit_groups_table ()
7290 {
7291 return htab_up (htab_create_alloc (3,
7292 hash_type_unit_group,
7293 eq_type_unit_group,
7294 NULL, xcalloc, xfree));
7295 }
7296
7297 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7298 partial symtabs. We combine several TUs per psymtab to not let the size
7299 of any one psymtab grow too big. */
7300 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7301 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7302
7303 /* Helper routine for get_type_unit_group.
7304 Create the type_unit_group object used to hold one or more TUs. */
7305
7306 static struct type_unit_group *
7307 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7308 {
7309 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7310 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7311 struct dwarf2_per_cu_data *per_cu;
7312 struct type_unit_group *tu_group;
7313
7314 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7315 struct type_unit_group);
7316 per_cu = &tu_group->per_cu;
7317 per_cu->per_bfd = per_bfd;
7318
7319 if (per_bfd->using_index)
7320 {
7321 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7322 struct dwarf2_per_cu_quick_data);
7323 }
7324 else
7325 {
7326 unsigned int line_offset = to_underlying (line_offset_struct);
7327 dwarf2_psymtab *pst;
7328 std::string name;
7329
7330 /* Give the symtab a useful name for debug purposes. */
7331 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7332 name = string_printf ("<type_units_%d>",
7333 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7334 else
7335 name = string_printf ("<type_units_at_0x%x>", line_offset);
7336
7337 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7338 pst->anonymous = true;
7339 }
7340
7341 tu_group->hash.dwo_unit = cu->dwo_unit;
7342 tu_group->hash.line_sect_off = line_offset_struct;
7343
7344 return tu_group;
7345 }
7346
7347 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7348 STMT_LIST is a DW_AT_stmt_list attribute. */
7349
7350 static struct type_unit_group *
7351 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7352 {
7353 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7354 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7355 struct type_unit_group *tu_group;
7356 void **slot;
7357 unsigned int line_offset;
7358 struct type_unit_group type_unit_group_for_lookup;
7359
7360 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7361 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7362
7363 /* Do we need to create a new group, or can we use an existing one? */
7364
7365 if (stmt_list)
7366 {
7367 line_offset = DW_UNSND (stmt_list);
7368 ++tu_stats->nr_symtab_sharers;
7369 }
7370 else
7371 {
7372 /* Ugh, no stmt_list. Rare, but we have to handle it.
7373 We can do various things here like create one group per TU or
7374 spread them over multiple groups to split up the expansion work.
7375 To avoid worst case scenarios (too many groups or too large groups)
7376 we, umm, group them in bunches. */
7377 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7378 | (tu_stats->nr_stmt_less_type_units
7379 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7380 ++tu_stats->nr_stmt_less_type_units;
7381 }
7382
7383 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7384 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7385 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7386 &type_unit_group_for_lookup, INSERT);
7387 if (*slot != NULL)
7388 {
7389 tu_group = (struct type_unit_group *) *slot;
7390 gdb_assert (tu_group != NULL);
7391 }
7392 else
7393 {
7394 sect_offset line_offset_struct = (sect_offset) line_offset;
7395 tu_group = create_type_unit_group (cu, line_offset_struct);
7396 *slot = tu_group;
7397 ++tu_stats->nr_symtabs;
7398 }
7399
7400 return tu_group;
7401 }
7402 \f
7403 /* Partial symbol tables. */
7404
7405 /* Create a psymtab named NAME and assign it to PER_CU.
7406
7407 The caller must fill in the following details:
7408 dirname, textlow, texthigh. */
7409
7410 static dwarf2_psymtab *
7411 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7412 dwarf2_per_objfile *per_objfile,
7413 const char *name)
7414 {
7415 struct objfile *objfile = per_objfile->objfile;
7416 dwarf2_psymtab *pst;
7417
7418 pst = new dwarf2_psymtab (name, objfile, per_cu);
7419
7420 pst->psymtabs_addrmap_supported = true;
7421
7422 /* This is the glue that links PST into GDB's symbol API. */
7423 per_cu->v.psymtab = pst;
7424
7425 return pst;
7426 }
7427
7428 /* DIE reader function for process_psymtab_comp_unit. */
7429
7430 static void
7431 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7432 const gdb_byte *info_ptr,
7433 struct die_info *comp_unit_die,
7434 enum language pretend_language)
7435 {
7436 struct dwarf2_cu *cu = reader->cu;
7437 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7438 struct objfile *objfile = per_objfile->objfile;
7439 struct gdbarch *gdbarch = objfile->arch ();
7440 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7441 CORE_ADDR baseaddr;
7442 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7443 dwarf2_psymtab *pst;
7444 enum pc_bounds_kind cu_bounds_kind;
7445 const char *filename;
7446
7447 gdb_assert (! per_cu->is_debug_types);
7448
7449 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7450
7451 /* Allocate a new partial symbol table structure. */
7452 gdb::unique_xmalloc_ptr<char> debug_filename;
7453 static const char artificial[] = "<artificial>";
7454 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7455 if (filename == NULL)
7456 filename = "";
7457 else if (strcmp (filename, artificial) == 0)
7458 {
7459 debug_filename.reset (concat (artificial, "@",
7460 sect_offset_str (per_cu->sect_off),
7461 (char *) NULL));
7462 filename = debug_filename.get ();
7463 }
7464
7465 pst = create_partial_symtab (per_cu, per_objfile, filename);
7466
7467 /* This must be done before calling dwarf2_build_include_psymtabs. */
7468 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7469
7470 baseaddr = objfile->text_section_offset ();
7471
7472 dwarf2_find_base_address (comp_unit_die, cu);
7473
7474 /* Possibly set the default values of LOWPC and HIGHPC from
7475 `DW_AT_ranges'. */
7476 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7477 &best_highpc, cu, pst);
7478 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7479 {
7480 CORE_ADDR low
7481 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7482 - baseaddr);
7483 CORE_ADDR high
7484 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7485 - baseaddr - 1);
7486 /* Store the contiguous range if it is not empty; it can be
7487 empty for CUs with no code. */
7488 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7489 low, high, pst);
7490 }
7491
7492 /* Check if comp unit has_children.
7493 If so, read the rest of the partial symbols from this comp unit.
7494 If not, there's no more debug_info for this comp unit. */
7495 if (comp_unit_die->has_children)
7496 {
7497 struct partial_die_info *first_die;
7498 CORE_ADDR lowpc, highpc;
7499
7500 lowpc = ((CORE_ADDR) -1);
7501 highpc = ((CORE_ADDR) 0);
7502
7503 first_die = load_partial_dies (reader, info_ptr, 1);
7504
7505 scan_partial_symbols (first_die, &lowpc, &highpc,
7506 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7507
7508 /* If we didn't find a lowpc, set it to highpc to avoid
7509 complaints from `maint check'. */
7510 if (lowpc == ((CORE_ADDR) -1))
7511 lowpc = highpc;
7512
7513 /* If the compilation unit didn't have an explicit address range,
7514 then use the information extracted from its child dies. */
7515 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7516 {
7517 best_lowpc = lowpc;
7518 best_highpc = highpc;
7519 }
7520 }
7521 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7522 best_lowpc + baseaddr)
7523 - baseaddr);
7524 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7525 best_highpc + baseaddr)
7526 - baseaddr);
7527
7528 end_psymtab_common (objfile, pst);
7529
7530 if (!cu->per_cu->imported_symtabs_empty ())
7531 {
7532 int i;
7533 int len = cu->per_cu->imported_symtabs_size ();
7534
7535 /* Fill in 'dependencies' here; we fill in 'users' in a
7536 post-pass. */
7537 pst->number_of_dependencies = len;
7538 pst->dependencies
7539 = objfile->partial_symtabs->allocate_dependencies (len);
7540 for (i = 0; i < len; ++i)
7541 {
7542 pst->dependencies[i]
7543 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7544 }
7545
7546 cu->per_cu->imported_symtabs_free ();
7547 }
7548
7549 /* Get the list of files included in the current compilation unit,
7550 and build a psymtab for each of them. */
7551 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7552
7553 if (dwarf_read_debug)
7554 fprintf_unfiltered (gdb_stdlog,
7555 "Psymtab for %s unit @%s: %s - %s"
7556 ", %d global, %d static syms\n",
7557 per_cu->is_debug_types ? "type" : "comp",
7558 sect_offset_str (per_cu->sect_off),
7559 paddress (gdbarch, pst->text_low (objfile)),
7560 paddress (gdbarch, pst->text_high (objfile)),
7561 pst->n_global_syms, pst->n_static_syms);
7562 }
7563
7564 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7565 Process compilation unit THIS_CU for a psymtab. */
7566
7567 static void
7568 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7569 dwarf2_per_objfile *per_objfile,
7570 bool want_partial_unit,
7571 enum language pretend_language)
7572 {
7573 /* If this compilation unit was already read in, free the
7574 cached copy in order to read it in again. This is
7575 necessary because we skipped some symbols when we first
7576 read in the compilation unit (see load_partial_dies).
7577 This problem could be avoided, but the benefit is unclear. */
7578 if (this_cu->cu != NULL)
7579 free_one_cached_comp_unit (this_cu, per_objfile);
7580
7581 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7582
7583 switch (reader.comp_unit_die->tag)
7584 {
7585 case DW_TAG_compile_unit:
7586 this_cu->unit_type = DW_UT_compile;
7587 break;
7588 case DW_TAG_partial_unit:
7589 this_cu->unit_type = DW_UT_partial;
7590 break;
7591 default:
7592 abort ();
7593 }
7594
7595 if (reader.dummy_p)
7596 {
7597 /* Nothing. */
7598 }
7599 else if (this_cu->is_debug_types)
7600 build_type_psymtabs_reader (&reader, reader.info_ptr,
7601 reader.comp_unit_die);
7602 else if (want_partial_unit
7603 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7604 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7605 reader.comp_unit_die,
7606 pretend_language);
7607
7608 this_cu->lang = this_cu->cu->language;
7609
7610 /* Age out any secondary CUs. */
7611 age_cached_comp_units (per_objfile);
7612 }
7613
7614 /* Reader function for build_type_psymtabs. */
7615
7616 static void
7617 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7618 const gdb_byte *info_ptr,
7619 struct die_info *type_unit_die)
7620 {
7621 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7622 struct objfile *objfile = dwarf2_per_objfile->objfile;
7623 struct dwarf2_cu *cu = reader->cu;
7624 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7625 struct signatured_type *sig_type;
7626 struct type_unit_group *tu_group;
7627 struct attribute *attr;
7628 struct partial_die_info *first_die;
7629 CORE_ADDR lowpc, highpc;
7630 dwarf2_psymtab *pst;
7631
7632 gdb_assert (per_cu->is_debug_types);
7633 sig_type = (struct signatured_type *) per_cu;
7634
7635 if (! type_unit_die->has_children)
7636 return;
7637
7638 attr = type_unit_die->attr (DW_AT_stmt_list);
7639 tu_group = get_type_unit_group (cu, attr);
7640
7641 if (tu_group->tus == nullptr)
7642 tu_group->tus = new std::vector<signatured_type *>;
7643 tu_group->tus->push_back (sig_type);
7644
7645 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7646 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7647 pst->anonymous = true;
7648
7649 first_die = load_partial_dies (reader, info_ptr, 1);
7650
7651 lowpc = (CORE_ADDR) -1;
7652 highpc = (CORE_ADDR) 0;
7653 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7654
7655 end_psymtab_common (objfile, pst);
7656 }
7657
7658 /* Struct used to sort TUs by their abbreviation table offset. */
7659
7660 struct tu_abbrev_offset
7661 {
7662 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7663 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7664 {}
7665
7666 signatured_type *sig_type;
7667 sect_offset abbrev_offset;
7668 };
7669
7670 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7671
7672 static bool
7673 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7674 const struct tu_abbrev_offset &b)
7675 {
7676 return a.abbrev_offset < b.abbrev_offset;
7677 }
7678
7679 /* Efficiently read all the type units.
7680 This does the bulk of the work for build_type_psymtabs.
7681
7682 The efficiency is because we sort TUs by the abbrev table they use and
7683 only read each abbrev table once. In one program there are 200K TUs
7684 sharing 8K abbrev tables.
7685
7686 The main purpose of this function is to support building the
7687 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7688 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7689 can collapse the search space by grouping them by stmt_list.
7690 The savings can be significant, in the same program from above the 200K TUs
7691 share 8K stmt_list tables.
7692
7693 FUNC is expected to call get_type_unit_group, which will create the
7694 struct type_unit_group if necessary and add it to
7695 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7696
7697 static void
7698 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7699 {
7700 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7701 abbrev_table_up abbrev_table;
7702 sect_offset abbrev_offset;
7703
7704 /* It's up to the caller to not call us multiple times. */
7705 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7706
7707 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7708 return;
7709
7710 /* TUs typically share abbrev tables, and there can be way more TUs than
7711 abbrev tables. Sort by abbrev table to reduce the number of times we
7712 read each abbrev table in.
7713 Alternatives are to punt or to maintain a cache of abbrev tables.
7714 This is simpler and efficient enough for now.
7715
7716 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7717 symtab to use). Typically TUs with the same abbrev offset have the same
7718 stmt_list value too so in practice this should work well.
7719
7720 The basic algorithm here is:
7721
7722 sort TUs by abbrev table
7723 for each TU with same abbrev table:
7724 read abbrev table if first user
7725 read TU top level DIE
7726 [IWBN if DWO skeletons had DW_AT_stmt_list]
7727 call FUNC */
7728
7729 if (dwarf_read_debug)
7730 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7731
7732 /* Sort in a separate table to maintain the order of all_type_units
7733 for .gdb_index: TU indices directly index all_type_units. */
7734 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7735 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7736
7737 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7738 sorted_by_abbrev.emplace_back
7739 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7740 sig_type->per_cu.section,
7741 sig_type->per_cu.sect_off));
7742
7743 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7744 sort_tu_by_abbrev_offset);
7745
7746 abbrev_offset = (sect_offset) ~(unsigned) 0;
7747
7748 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7749 {
7750 /* Switch to the next abbrev table if necessary. */
7751 if (abbrev_table == NULL
7752 || tu.abbrev_offset != abbrev_offset)
7753 {
7754 abbrev_offset = tu.abbrev_offset;
7755 abbrev_table =
7756 abbrev_table::read (dwarf2_per_objfile->objfile,
7757 &dwarf2_per_objfile->per_bfd->abbrev,
7758 abbrev_offset);
7759 ++tu_stats->nr_uniq_abbrev_tables;
7760 }
7761
7762 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7763 abbrev_table.get (), 0, false);
7764 if (!reader.dummy_p)
7765 build_type_psymtabs_reader (&reader, reader.info_ptr,
7766 reader.comp_unit_die);
7767 }
7768 }
7769
7770 /* Print collected type unit statistics. */
7771
7772 static void
7773 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7774 {
7775 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7776
7777 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7778 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7779 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7780 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7781 tu_stats->nr_uniq_abbrev_tables);
7782 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7783 tu_stats->nr_symtabs);
7784 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7785 tu_stats->nr_symtab_sharers);
7786 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7787 tu_stats->nr_stmt_less_type_units);
7788 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7789 tu_stats->nr_all_type_units_reallocs);
7790 }
7791
7792 /* Traversal function for build_type_psymtabs. */
7793
7794 static int
7795 build_type_psymtab_dependencies (void **slot, void *info)
7796 {
7797 struct dwarf2_per_objfile *dwarf2_per_objfile
7798 = (struct dwarf2_per_objfile *) info;
7799 struct objfile *objfile = dwarf2_per_objfile->objfile;
7800 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7801 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7802 dwarf2_psymtab *pst = per_cu->v.psymtab;
7803 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7804 int i;
7805
7806 gdb_assert (len > 0);
7807 gdb_assert (per_cu->type_unit_group_p ());
7808
7809 pst->number_of_dependencies = len;
7810 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7811 for (i = 0; i < len; ++i)
7812 {
7813 struct signatured_type *iter = tu_group->tus->at (i);
7814 gdb_assert (iter->per_cu.is_debug_types);
7815 pst->dependencies[i] = iter->per_cu.v.psymtab;
7816 iter->type_unit_group = tu_group;
7817 }
7818
7819 delete tu_group->tus;
7820 tu_group->tus = nullptr;
7821
7822 return 1;
7823 }
7824
7825 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7826 Build partial symbol tables for the .debug_types comp-units. */
7827
7828 static void
7829 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7830 {
7831 if (! create_all_type_units (dwarf2_per_objfile))
7832 return;
7833
7834 build_type_psymtabs_1 (dwarf2_per_objfile);
7835 }
7836
7837 /* Traversal function for process_skeletonless_type_unit.
7838 Read a TU in a DWO file and build partial symbols for it. */
7839
7840 static int
7841 process_skeletonless_type_unit (void **slot, void *info)
7842 {
7843 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7844 struct dwarf2_per_objfile *dwarf2_per_objfile
7845 = (struct dwarf2_per_objfile *) info;
7846 struct signatured_type find_entry, *entry;
7847
7848 /* If this TU doesn't exist in the global table, add it and read it in. */
7849
7850 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7851 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7852
7853 find_entry.signature = dwo_unit->signature;
7854 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7855 &find_entry, INSERT);
7856 /* If we've already seen this type there's nothing to do. What's happening
7857 is we're doing our own version of comdat-folding here. */
7858 if (*slot != NULL)
7859 return 1;
7860
7861 /* This does the job that create_all_type_units would have done for
7862 this TU. */
7863 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7864 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7865 *slot = entry;
7866
7867 /* This does the job that build_type_psymtabs_1 would have done. */
7868 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7869 if (!reader.dummy_p)
7870 build_type_psymtabs_reader (&reader, reader.info_ptr,
7871 reader.comp_unit_die);
7872
7873 return 1;
7874 }
7875
7876 /* Traversal function for process_skeletonless_type_units. */
7877
7878 static int
7879 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7880 {
7881 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7882
7883 if (dwo_file->tus != NULL)
7884 htab_traverse_noresize (dwo_file->tus.get (),
7885 process_skeletonless_type_unit, info);
7886
7887 return 1;
7888 }
7889
7890 /* Scan all TUs of DWO files, verifying we've processed them.
7891 This is needed in case a TU was emitted without its skeleton.
7892 Note: This can't be done until we know what all the DWO files are. */
7893
7894 static void
7895 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7896 {
7897 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7898 if (get_dwp_file (dwarf2_per_objfile) == NULL
7899 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7900 {
7901 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7902 process_dwo_file_for_skeletonless_type_units,
7903 dwarf2_per_objfile);
7904 }
7905 }
7906
7907 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7908
7909 static void
7910 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7911 {
7912 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7913 {
7914 dwarf2_psymtab *pst = per_cu->v.psymtab;
7915
7916 if (pst == NULL)
7917 continue;
7918
7919 for (int j = 0; j < pst->number_of_dependencies; ++j)
7920 {
7921 /* Set the 'user' field only if it is not already set. */
7922 if (pst->dependencies[j]->user == NULL)
7923 pst->dependencies[j]->user = pst;
7924 }
7925 }
7926 }
7927
7928 /* Build the partial symbol table by doing a quick pass through the
7929 .debug_info and .debug_abbrev sections. */
7930
7931 static void
7932 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7933 {
7934 struct objfile *objfile = dwarf2_per_objfile->objfile;
7935
7936 if (dwarf_read_debug)
7937 {
7938 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7939 objfile_name (objfile));
7940 }
7941
7942 scoped_restore restore_reading_psyms
7943 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7944 true);
7945
7946 dwarf2_per_objfile->per_bfd->info.read (objfile);
7947
7948 /* Any cached compilation units will be linked by the per-objfile
7949 read_in_chain. Make sure to free them when we're done. */
7950 free_cached_comp_units freer (dwarf2_per_objfile);
7951
7952 build_type_psymtabs (dwarf2_per_objfile);
7953
7954 create_all_comp_units (dwarf2_per_objfile);
7955
7956 /* Create a temporary address map on a temporary obstack. We later
7957 copy this to the final obstack. */
7958 auto_obstack temp_obstack;
7959
7960 scoped_restore save_psymtabs_addrmap
7961 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7962 addrmap_create_mutable (&temp_obstack));
7963
7964 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7965 {
7966 if (per_cu->v.psymtab != NULL)
7967 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7968 continue;
7969 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7970 language_minimal);
7971 }
7972
7973 /* This has to wait until we read the CUs, we need the list of DWOs. */
7974 process_skeletonless_type_units (dwarf2_per_objfile);
7975
7976 /* Now that all TUs have been processed we can fill in the dependencies. */
7977 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7978 {
7979 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7980 build_type_psymtab_dependencies, dwarf2_per_objfile);
7981 }
7982
7983 if (dwarf_read_debug)
7984 print_tu_stats (dwarf2_per_objfile);
7985
7986 set_partial_user (dwarf2_per_objfile);
7987
7988 objfile->partial_symtabs->psymtabs_addrmap
7989 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7990 objfile->partial_symtabs->obstack ());
7991 /* At this point we want to keep the address map. */
7992 save_psymtabs_addrmap.release ();
7993
7994 if (dwarf_read_debug)
7995 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7996 objfile_name (objfile));
7997 }
7998
7999 /* Load the partial DIEs for a secondary CU into memory.
8000 This is also used when rereading a primary CU with load_all_dies. */
8001
8002 static void
8003 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8004 dwarf2_per_objfile *per_objfile)
8005 {
8006 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
8007
8008 if (!reader.dummy_p)
8009 {
8010 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8011 language_minimal);
8012
8013 /* Check if comp unit has_children.
8014 If so, read the rest of the partial symbols from this comp unit.
8015 If not, there's no more debug_info for this comp unit. */
8016 if (reader.comp_unit_die->has_children)
8017 load_partial_dies (&reader, reader.info_ptr, 0);
8018
8019 reader.keep ();
8020 }
8021 }
8022
8023 static void
8024 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8025 struct dwarf2_section_info *section,
8026 struct dwarf2_section_info *abbrev_section,
8027 unsigned int is_dwz)
8028 {
8029 const gdb_byte *info_ptr;
8030 struct objfile *objfile = dwarf2_per_objfile->objfile;
8031
8032 if (dwarf_read_debug)
8033 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8034 section->get_name (),
8035 section->get_file_name ());
8036
8037 section->read (objfile);
8038
8039 info_ptr = section->buffer;
8040
8041 while (info_ptr < section->buffer + section->size)
8042 {
8043 struct dwarf2_per_cu_data *this_cu;
8044
8045 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8046
8047 comp_unit_head cu_header;
8048 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8049 abbrev_section, info_ptr,
8050 rcuh_kind::COMPILE);
8051
8052 /* Save the compilation unit for later lookup. */
8053 if (cu_header.unit_type != DW_UT_type)
8054 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8055 else
8056 {
8057 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8058 sig_type->signature = cu_header.signature;
8059 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8060 this_cu = &sig_type->per_cu;
8061 }
8062 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8063 this_cu->sect_off = sect_off;
8064 this_cu->length = cu_header.length + cu_header.initial_length_size;
8065 this_cu->is_dwz = is_dwz;
8066 this_cu->section = section;
8067
8068 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8069
8070 info_ptr = info_ptr + this_cu->length;
8071 }
8072 }
8073
8074 /* Create a list of all compilation units in OBJFILE.
8075 This is only done for -readnow and building partial symtabs. */
8076
8077 static void
8078 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8079 {
8080 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8081 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8082 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8083
8084 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8085 if (dwz != NULL)
8086 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8087 1);
8088 }
8089
8090 /* Process all loaded DIEs for compilation unit CU, starting at
8091 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8092 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8093 DW_AT_ranges). See the comments of add_partial_subprogram on how
8094 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8095
8096 static void
8097 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8098 CORE_ADDR *highpc, int set_addrmap,
8099 struct dwarf2_cu *cu)
8100 {
8101 struct partial_die_info *pdi;
8102
8103 /* Now, march along the PDI's, descending into ones which have
8104 interesting children but skipping the children of the other ones,
8105 until we reach the end of the compilation unit. */
8106
8107 pdi = first_die;
8108
8109 while (pdi != NULL)
8110 {
8111 pdi->fixup (cu);
8112
8113 /* Anonymous namespaces or modules have no name but have interesting
8114 children, so we need to look at them. Ditto for anonymous
8115 enums. */
8116
8117 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8118 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8119 || pdi->tag == DW_TAG_imported_unit
8120 || pdi->tag == DW_TAG_inlined_subroutine)
8121 {
8122 switch (pdi->tag)
8123 {
8124 case DW_TAG_subprogram:
8125 case DW_TAG_inlined_subroutine:
8126 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8127 break;
8128 case DW_TAG_constant:
8129 case DW_TAG_variable:
8130 case DW_TAG_typedef:
8131 case DW_TAG_union_type:
8132 if (!pdi->is_declaration
8133 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8134 {
8135 add_partial_symbol (pdi, cu);
8136 }
8137 break;
8138 case DW_TAG_class_type:
8139 case DW_TAG_interface_type:
8140 case DW_TAG_structure_type:
8141 if (!pdi->is_declaration)
8142 {
8143 add_partial_symbol (pdi, cu);
8144 }
8145 if ((cu->language == language_rust
8146 || cu->language == language_cplus) && pdi->has_children)
8147 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8148 set_addrmap, cu);
8149 break;
8150 case DW_TAG_enumeration_type:
8151 if (!pdi->is_declaration)
8152 add_partial_enumeration (pdi, cu);
8153 break;
8154 case DW_TAG_base_type:
8155 case DW_TAG_subrange_type:
8156 /* File scope base type definitions are added to the partial
8157 symbol table. */
8158 add_partial_symbol (pdi, cu);
8159 break;
8160 case DW_TAG_namespace:
8161 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8162 break;
8163 case DW_TAG_module:
8164 if (!pdi->is_declaration)
8165 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8166 break;
8167 case DW_TAG_imported_unit:
8168 {
8169 struct dwarf2_per_cu_data *per_cu;
8170
8171 /* For now we don't handle imported units in type units. */
8172 if (cu->per_cu->is_debug_types)
8173 {
8174 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8175 " supported in type units [in module %s]"),
8176 objfile_name (cu->per_objfile->objfile));
8177 }
8178
8179 per_cu = dwarf2_find_containing_comp_unit
8180 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8181
8182 /* Go read the partial unit, if needed. */
8183 if (per_cu->v.psymtab == NULL)
8184 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8185 cu->language);
8186
8187 cu->per_cu->imported_symtabs_push (per_cu);
8188 }
8189 break;
8190 case DW_TAG_imported_declaration:
8191 add_partial_symbol (pdi, cu);
8192 break;
8193 default:
8194 break;
8195 }
8196 }
8197
8198 /* If the die has a sibling, skip to the sibling. */
8199
8200 pdi = pdi->die_sibling;
8201 }
8202 }
8203
8204 /* Functions used to compute the fully scoped name of a partial DIE.
8205
8206 Normally, this is simple. For C++, the parent DIE's fully scoped
8207 name is concatenated with "::" and the partial DIE's name.
8208 Enumerators are an exception; they use the scope of their parent
8209 enumeration type, i.e. the name of the enumeration type is not
8210 prepended to the enumerator.
8211
8212 There are two complexities. One is DW_AT_specification; in this
8213 case "parent" means the parent of the target of the specification,
8214 instead of the direct parent of the DIE. The other is compilers
8215 which do not emit DW_TAG_namespace; in this case we try to guess
8216 the fully qualified name of structure types from their members'
8217 linkage names. This must be done using the DIE's children rather
8218 than the children of any DW_AT_specification target. We only need
8219 to do this for structures at the top level, i.e. if the target of
8220 any DW_AT_specification (if any; otherwise the DIE itself) does not
8221 have a parent. */
8222
8223 /* Compute the scope prefix associated with PDI's parent, in
8224 compilation unit CU. The result will be allocated on CU's
8225 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8226 field. NULL is returned if no prefix is necessary. */
8227 static const char *
8228 partial_die_parent_scope (struct partial_die_info *pdi,
8229 struct dwarf2_cu *cu)
8230 {
8231 const char *grandparent_scope;
8232 struct partial_die_info *parent, *real_pdi;
8233
8234 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8235 then this means the parent of the specification DIE. */
8236
8237 real_pdi = pdi;
8238 while (real_pdi->has_specification)
8239 {
8240 auto res = find_partial_die (real_pdi->spec_offset,
8241 real_pdi->spec_is_dwz, cu);
8242 real_pdi = res.pdi;
8243 cu = res.cu;
8244 }
8245
8246 parent = real_pdi->die_parent;
8247 if (parent == NULL)
8248 return NULL;
8249
8250 if (parent->scope_set)
8251 return parent->scope;
8252
8253 parent->fixup (cu);
8254
8255 grandparent_scope = partial_die_parent_scope (parent, cu);
8256
8257 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8258 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8259 Work around this problem here. */
8260 if (cu->language == language_cplus
8261 && parent->tag == DW_TAG_namespace
8262 && strcmp (parent->name, "::") == 0
8263 && grandparent_scope == NULL)
8264 {
8265 parent->scope = NULL;
8266 parent->scope_set = 1;
8267 return NULL;
8268 }
8269
8270 /* Nested subroutines in Fortran get a prefix. */
8271 if (pdi->tag == DW_TAG_enumerator)
8272 /* Enumerators should not get the name of the enumeration as a prefix. */
8273 parent->scope = grandparent_scope;
8274 else if (parent->tag == DW_TAG_namespace
8275 || parent->tag == DW_TAG_module
8276 || parent->tag == DW_TAG_structure_type
8277 || parent->tag == DW_TAG_class_type
8278 || parent->tag == DW_TAG_interface_type
8279 || parent->tag == DW_TAG_union_type
8280 || parent->tag == DW_TAG_enumeration_type
8281 || (cu->language == language_fortran
8282 && parent->tag == DW_TAG_subprogram
8283 && pdi->tag == DW_TAG_subprogram))
8284 {
8285 if (grandparent_scope == NULL)
8286 parent->scope = parent->name;
8287 else
8288 parent->scope = typename_concat (&cu->comp_unit_obstack,
8289 grandparent_scope,
8290 parent->name, 0, cu);
8291 }
8292 else
8293 {
8294 /* FIXME drow/2004-04-01: What should we be doing with
8295 function-local names? For partial symbols, we should probably be
8296 ignoring them. */
8297 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8298 dwarf_tag_name (parent->tag),
8299 sect_offset_str (pdi->sect_off));
8300 parent->scope = grandparent_scope;
8301 }
8302
8303 parent->scope_set = 1;
8304 return parent->scope;
8305 }
8306
8307 /* Return the fully scoped name associated with PDI, from compilation unit
8308 CU. The result will be allocated with malloc. */
8309
8310 static gdb::unique_xmalloc_ptr<char>
8311 partial_die_full_name (struct partial_die_info *pdi,
8312 struct dwarf2_cu *cu)
8313 {
8314 const char *parent_scope;
8315
8316 /* If this is a template instantiation, we can not work out the
8317 template arguments from partial DIEs. So, unfortunately, we have
8318 to go through the full DIEs. At least any work we do building
8319 types here will be reused if full symbols are loaded later. */
8320 if (pdi->has_template_arguments)
8321 {
8322 pdi->fixup (cu);
8323
8324 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8325 {
8326 struct die_info *die;
8327 struct attribute attr;
8328 struct dwarf2_cu *ref_cu = cu;
8329
8330 /* DW_FORM_ref_addr is using section offset. */
8331 attr.name = (enum dwarf_attribute) 0;
8332 attr.form = DW_FORM_ref_addr;
8333 attr.u.unsnd = to_underlying (pdi->sect_off);
8334 die = follow_die_ref (NULL, &attr, &ref_cu);
8335
8336 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8337 }
8338 }
8339
8340 parent_scope = partial_die_parent_scope (pdi, cu);
8341 if (parent_scope == NULL)
8342 return NULL;
8343 else
8344 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8345 pdi->name, 0, cu));
8346 }
8347
8348 static void
8349 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8350 {
8351 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8352 struct objfile *objfile = dwarf2_per_objfile->objfile;
8353 struct gdbarch *gdbarch = objfile->arch ();
8354 CORE_ADDR addr = 0;
8355 const char *actual_name = NULL;
8356 CORE_ADDR baseaddr;
8357
8358 baseaddr = objfile->text_section_offset ();
8359
8360 gdb::unique_xmalloc_ptr<char> built_actual_name
8361 = partial_die_full_name (pdi, cu);
8362 if (built_actual_name != NULL)
8363 actual_name = built_actual_name.get ();
8364
8365 if (actual_name == NULL)
8366 actual_name = pdi->name;
8367
8368 partial_symbol psymbol;
8369 memset (&psymbol, 0, sizeof (psymbol));
8370 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8371 psymbol.ginfo.section = -1;
8372
8373 /* The code below indicates that the psymbol should be installed by
8374 setting this. */
8375 gdb::optional<psymbol_placement> where;
8376
8377 switch (pdi->tag)
8378 {
8379 case DW_TAG_inlined_subroutine:
8380 case DW_TAG_subprogram:
8381 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8382 - baseaddr);
8383 if (pdi->is_external
8384 || cu->language == language_ada
8385 || (cu->language == language_fortran
8386 && pdi->die_parent != NULL
8387 && pdi->die_parent->tag == DW_TAG_subprogram))
8388 {
8389 /* Normally, only "external" DIEs are part of the global scope.
8390 But in Ada and Fortran, we want to be able to access nested
8391 procedures globally. So all Ada and Fortran subprograms are
8392 stored in the global scope. */
8393 where = psymbol_placement::GLOBAL;
8394 }
8395 else
8396 where = psymbol_placement::STATIC;
8397
8398 psymbol.domain = VAR_DOMAIN;
8399 psymbol.aclass = LOC_BLOCK;
8400 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8401 psymbol.ginfo.value.address = addr;
8402
8403 if (pdi->main_subprogram && actual_name != NULL)
8404 set_objfile_main_name (objfile, actual_name, cu->language);
8405 break;
8406 case DW_TAG_constant:
8407 psymbol.domain = VAR_DOMAIN;
8408 psymbol.aclass = LOC_STATIC;
8409 where = (pdi->is_external
8410 ? psymbol_placement::GLOBAL
8411 : psymbol_placement::STATIC);
8412 break;
8413 case DW_TAG_variable:
8414 if (pdi->d.locdesc)
8415 addr = decode_locdesc (pdi->d.locdesc, cu);
8416
8417 if (pdi->d.locdesc
8418 && addr == 0
8419 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8420 {
8421 /* A global or static variable may also have been stripped
8422 out by the linker if unused, in which case its address
8423 will be nullified; do not add such variables into partial
8424 symbol table then. */
8425 }
8426 else if (pdi->is_external)
8427 {
8428 /* Global Variable.
8429 Don't enter into the minimal symbol tables as there is
8430 a minimal symbol table entry from the ELF symbols already.
8431 Enter into partial symbol table if it has a location
8432 descriptor or a type.
8433 If the location descriptor is missing, new_symbol will create
8434 a LOC_UNRESOLVED symbol, the address of the variable will then
8435 be determined from the minimal symbol table whenever the variable
8436 is referenced.
8437 The address for the partial symbol table entry is not
8438 used by GDB, but it comes in handy for debugging partial symbol
8439 table building. */
8440
8441 if (pdi->d.locdesc || pdi->has_type)
8442 {
8443 psymbol.domain = VAR_DOMAIN;
8444 psymbol.aclass = LOC_STATIC;
8445 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8446 psymbol.ginfo.value.address = addr;
8447 where = psymbol_placement::GLOBAL;
8448 }
8449 }
8450 else
8451 {
8452 int has_loc = pdi->d.locdesc != NULL;
8453
8454 /* Static Variable. Skip symbols whose value we cannot know (those
8455 without location descriptors or constant values). */
8456 if (!has_loc && !pdi->has_const_value)
8457 return;
8458
8459 psymbol.domain = VAR_DOMAIN;
8460 psymbol.aclass = LOC_STATIC;
8461 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8462 if (has_loc)
8463 psymbol.ginfo.value.address = addr;
8464 where = psymbol_placement::STATIC;
8465 }
8466 break;
8467 case DW_TAG_typedef:
8468 case DW_TAG_base_type:
8469 case DW_TAG_subrange_type:
8470 psymbol.domain = VAR_DOMAIN;
8471 psymbol.aclass = LOC_TYPEDEF;
8472 where = psymbol_placement::STATIC;
8473 break;
8474 case DW_TAG_imported_declaration:
8475 case DW_TAG_namespace:
8476 psymbol.domain = VAR_DOMAIN;
8477 psymbol.aclass = LOC_TYPEDEF;
8478 where = psymbol_placement::GLOBAL;
8479 break;
8480 case DW_TAG_module:
8481 /* With Fortran 77 there might be a "BLOCK DATA" module
8482 available without any name. If so, we skip the module as it
8483 doesn't bring any value. */
8484 if (actual_name != nullptr)
8485 {
8486 psymbol.domain = MODULE_DOMAIN;
8487 psymbol.aclass = LOC_TYPEDEF;
8488 where = psymbol_placement::GLOBAL;
8489 }
8490 break;
8491 case DW_TAG_class_type:
8492 case DW_TAG_interface_type:
8493 case DW_TAG_structure_type:
8494 case DW_TAG_union_type:
8495 case DW_TAG_enumeration_type:
8496 /* Skip external references. The DWARF standard says in the section
8497 about "Structure, Union, and Class Type Entries": "An incomplete
8498 structure, union or class type is represented by a structure,
8499 union or class entry that does not have a byte size attribute
8500 and that has a DW_AT_declaration attribute." */
8501 if (!pdi->has_byte_size && pdi->is_declaration)
8502 return;
8503
8504 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8505 static vs. global. */
8506 psymbol.domain = STRUCT_DOMAIN;
8507 psymbol.aclass = LOC_TYPEDEF;
8508 where = (cu->language == language_cplus
8509 ? psymbol_placement::GLOBAL
8510 : psymbol_placement::STATIC);
8511 break;
8512 case DW_TAG_enumerator:
8513 psymbol.domain = VAR_DOMAIN;
8514 psymbol.aclass = LOC_CONST;
8515 where = (cu->language == language_cplus
8516 ? psymbol_placement::GLOBAL
8517 : psymbol_placement::STATIC);
8518 break;
8519 default:
8520 break;
8521 }
8522
8523 if (where.has_value ())
8524 {
8525 if (built_actual_name != nullptr)
8526 actual_name = objfile->intern (actual_name);
8527 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8528 psymbol.ginfo.set_linkage_name (actual_name);
8529 else
8530 {
8531 psymbol.ginfo.set_demangled_name (actual_name,
8532 &objfile->objfile_obstack);
8533 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8534 }
8535 add_psymbol_to_list (psymbol, *where, objfile);
8536 }
8537 }
8538
8539 /* Read a partial die corresponding to a namespace; also, add a symbol
8540 corresponding to that namespace to the symbol table. NAMESPACE is
8541 the name of the enclosing namespace. */
8542
8543 static void
8544 add_partial_namespace (struct partial_die_info *pdi,
8545 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8546 int set_addrmap, struct dwarf2_cu *cu)
8547 {
8548 /* Add a symbol for the namespace. */
8549
8550 add_partial_symbol (pdi, cu);
8551
8552 /* Now scan partial symbols in that namespace. */
8553
8554 if (pdi->has_children)
8555 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8556 }
8557
8558 /* Read a partial die corresponding to a Fortran module. */
8559
8560 static void
8561 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8562 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8563 {
8564 /* Add a symbol for the namespace. */
8565
8566 add_partial_symbol (pdi, cu);
8567
8568 /* Now scan partial symbols in that module. */
8569
8570 if (pdi->has_children)
8571 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8572 }
8573
8574 /* Read a partial die corresponding to a subprogram or an inlined
8575 subprogram and create a partial symbol for that subprogram.
8576 When the CU language allows it, this routine also defines a partial
8577 symbol for each nested subprogram that this subprogram contains.
8578 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8579 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8580
8581 PDI may also be a lexical block, in which case we simply search
8582 recursively for subprograms defined inside that lexical block.
8583 Again, this is only performed when the CU language allows this
8584 type of definitions. */
8585
8586 static void
8587 add_partial_subprogram (struct partial_die_info *pdi,
8588 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8589 int set_addrmap, struct dwarf2_cu *cu)
8590 {
8591 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8592 {
8593 if (pdi->has_pc_info)
8594 {
8595 if (pdi->lowpc < *lowpc)
8596 *lowpc = pdi->lowpc;
8597 if (pdi->highpc > *highpc)
8598 *highpc = pdi->highpc;
8599 if (set_addrmap)
8600 {
8601 struct objfile *objfile = cu->per_objfile->objfile;
8602 struct gdbarch *gdbarch = objfile->arch ();
8603 CORE_ADDR baseaddr;
8604 CORE_ADDR this_highpc;
8605 CORE_ADDR this_lowpc;
8606
8607 baseaddr = objfile->text_section_offset ();
8608 this_lowpc
8609 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8610 pdi->lowpc + baseaddr)
8611 - baseaddr);
8612 this_highpc
8613 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8614 pdi->highpc + baseaddr)
8615 - baseaddr);
8616 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8617 this_lowpc, this_highpc - 1,
8618 cu->per_cu->v.psymtab);
8619 }
8620 }
8621
8622 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8623 {
8624 if (!pdi->is_declaration)
8625 /* Ignore subprogram DIEs that do not have a name, they are
8626 illegal. Do not emit a complaint at this point, we will
8627 do so when we convert this psymtab into a symtab. */
8628 if (pdi->name)
8629 add_partial_symbol (pdi, cu);
8630 }
8631 }
8632
8633 if (! pdi->has_children)
8634 return;
8635
8636 if (cu->language == language_ada || cu->language == language_fortran)
8637 {
8638 pdi = pdi->die_child;
8639 while (pdi != NULL)
8640 {
8641 pdi->fixup (cu);
8642 if (pdi->tag == DW_TAG_subprogram
8643 || pdi->tag == DW_TAG_inlined_subroutine
8644 || pdi->tag == DW_TAG_lexical_block)
8645 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8646 pdi = pdi->die_sibling;
8647 }
8648 }
8649 }
8650
8651 /* Read a partial die corresponding to an enumeration type. */
8652
8653 static void
8654 add_partial_enumeration (struct partial_die_info *enum_pdi,
8655 struct dwarf2_cu *cu)
8656 {
8657 struct partial_die_info *pdi;
8658
8659 if (enum_pdi->name != NULL)
8660 add_partial_symbol (enum_pdi, cu);
8661
8662 pdi = enum_pdi->die_child;
8663 while (pdi)
8664 {
8665 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8666 complaint (_("malformed enumerator DIE ignored"));
8667 else
8668 add_partial_symbol (pdi, cu);
8669 pdi = pdi->die_sibling;
8670 }
8671 }
8672
8673 /* Return the initial uleb128 in the die at INFO_PTR. */
8674
8675 static unsigned int
8676 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8677 {
8678 unsigned int bytes_read;
8679
8680 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8681 }
8682
8683 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8684 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8685
8686 Return the corresponding abbrev, or NULL if the number is zero (indicating
8687 an empty DIE). In either case *BYTES_READ will be set to the length of
8688 the initial number. */
8689
8690 static struct abbrev_info *
8691 peek_die_abbrev (const die_reader_specs &reader,
8692 const gdb_byte *info_ptr, unsigned int *bytes_read)
8693 {
8694 dwarf2_cu *cu = reader.cu;
8695 bfd *abfd = cu->per_objfile->objfile->obfd;
8696 unsigned int abbrev_number
8697 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8698
8699 if (abbrev_number == 0)
8700 return NULL;
8701
8702 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8703 if (!abbrev)
8704 {
8705 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8706 " at offset %s [in module %s]"),
8707 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8708 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8709 }
8710
8711 return abbrev;
8712 }
8713
8714 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8715 Returns a pointer to the end of a series of DIEs, terminated by an empty
8716 DIE. Any children of the skipped DIEs will also be skipped. */
8717
8718 static const gdb_byte *
8719 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8720 {
8721 while (1)
8722 {
8723 unsigned int bytes_read;
8724 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8725
8726 if (abbrev == NULL)
8727 return info_ptr + bytes_read;
8728 else
8729 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8730 }
8731 }
8732
8733 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8734 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8735 abbrev corresponding to that skipped uleb128 should be passed in
8736 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8737 children. */
8738
8739 static const gdb_byte *
8740 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8741 struct abbrev_info *abbrev)
8742 {
8743 unsigned int bytes_read;
8744 struct attribute attr;
8745 bfd *abfd = reader->abfd;
8746 struct dwarf2_cu *cu = reader->cu;
8747 const gdb_byte *buffer = reader->buffer;
8748 const gdb_byte *buffer_end = reader->buffer_end;
8749 unsigned int form, i;
8750
8751 for (i = 0; i < abbrev->num_attrs; i++)
8752 {
8753 /* The only abbrev we care about is DW_AT_sibling. */
8754 if (abbrev->attrs[i].name == DW_AT_sibling)
8755 {
8756 bool ignored;
8757 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8758 &ignored);
8759 if (attr.form == DW_FORM_ref_addr)
8760 complaint (_("ignoring absolute DW_AT_sibling"));
8761 else
8762 {
8763 sect_offset off = attr.get_ref_die_offset ();
8764 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8765
8766 if (sibling_ptr < info_ptr)
8767 complaint (_("DW_AT_sibling points backwards"));
8768 else if (sibling_ptr > reader->buffer_end)
8769 reader->die_section->overflow_complaint ();
8770 else
8771 return sibling_ptr;
8772 }
8773 }
8774
8775 /* If it isn't DW_AT_sibling, skip this attribute. */
8776 form = abbrev->attrs[i].form;
8777 skip_attribute:
8778 switch (form)
8779 {
8780 case DW_FORM_ref_addr:
8781 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8782 and later it is offset sized. */
8783 if (cu->header.version == 2)
8784 info_ptr += cu->header.addr_size;
8785 else
8786 info_ptr += cu->header.offset_size;
8787 break;
8788 case DW_FORM_GNU_ref_alt:
8789 info_ptr += cu->header.offset_size;
8790 break;
8791 case DW_FORM_addr:
8792 info_ptr += cu->header.addr_size;
8793 break;
8794 case DW_FORM_data1:
8795 case DW_FORM_ref1:
8796 case DW_FORM_flag:
8797 case DW_FORM_strx1:
8798 info_ptr += 1;
8799 break;
8800 case DW_FORM_flag_present:
8801 case DW_FORM_implicit_const:
8802 break;
8803 case DW_FORM_data2:
8804 case DW_FORM_ref2:
8805 case DW_FORM_strx2:
8806 info_ptr += 2;
8807 break;
8808 case DW_FORM_strx3:
8809 info_ptr += 3;
8810 break;
8811 case DW_FORM_data4:
8812 case DW_FORM_ref4:
8813 case DW_FORM_strx4:
8814 info_ptr += 4;
8815 break;
8816 case DW_FORM_data8:
8817 case DW_FORM_ref8:
8818 case DW_FORM_ref_sig8:
8819 info_ptr += 8;
8820 break;
8821 case DW_FORM_data16:
8822 info_ptr += 16;
8823 break;
8824 case DW_FORM_string:
8825 read_direct_string (abfd, info_ptr, &bytes_read);
8826 info_ptr += bytes_read;
8827 break;
8828 case DW_FORM_sec_offset:
8829 case DW_FORM_strp:
8830 case DW_FORM_GNU_strp_alt:
8831 info_ptr += cu->header.offset_size;
8832 break;
8833 case DW_FORM_exprloc:
8834 case DW_FORM_block:
8835 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8836 info_ptr += bytes_read;
8837 break;
8838 case DW_FORM_block1:
8839 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8840 break;
8841 case DW_FORM_block2:
8842 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8843 break;
8844 case DW_FORM_block4:
8845 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8846 break;
8847 case DW_FORM_addrx:
8848 case DW_FORM_strx:
8849 case DW_FORM_sdata:
8850 case DW_FORM_udata:
8851 case DW_FORM_ref_udata:
8852 case DW_FORM_GNU_addr_index:
8853 case DW_FORM_GNU_str_index:
8854 case DW_FORM_rnglistx:
8855 case DW_FORM_loclistx:
8856 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8857 break;
8858 case DW_FORM_indirect:
8859 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8860 info_ptr += bytes_read;
8861 /* We need to continue parsing from here, so just go back to
8862 the top. */
8863 goto skip_attribute;
8864
8865 default:
8866 error (_("Dwarf Error: Cannot handle %s "
8867 "in DWARF reader [in module %s]"),
8868 dwarf_form_name (form),
8869 bfd_get_filename (abfd));
8870 }
8871 }
8872
8873 if (abbrev->has_children)
8874 return skip_children (reader, info_ptr);
8875 else
8876 return info_ptr;
8877 }
8878
8879 /* Locate ORIG_PDI's sibling.
8880 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8881
8882 static const gdb_byte *
8883 locate_pdi_sibling (const struct die_reader_specs *reader,
8884 struct partial_die_info *orig_pdi,
8885 const gdb_byte *info_ptr)
8886 {
8887 /* Do we know the sibling already? */
8888
8889 if (orig_pdi->sibling)
8890 return orig_pdi->sibling;
8891
8892 /* Are there any children to deal with? */
8893
8894 if (!orig_pdi->has_children)
8895 return info_ptr;
8896
8897 /* Skip the children the long way. */
8898
8899 return skip_children (reader, info_ptr);
8900 }
8901
8902 /* Expand this partial symbol table into a full symbol table. SELF is
8903 not NULL. */
8904
8905 void
8906 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8907 {
8908 struct dwarf2_per_objfile *dwarf2_per_objfile
8909 = get_dwarf2_per_objfile (objfile);
8910
8911 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8912
8913 /* If this psymtab is constructed from a debug-only objfile, the
8914 has_section_at_zero flag will not necessarily be correct. We
8915 can get the correct value for this flag by looking at the data
8916 associated with the (presumably stripped) associated objfile. */
8917 if (objfile->separate_debug_objfile_backlink)
8918 {
8919 struct dwarf2_per_objfile *dpo_backlink
8920 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8921
8922 dwarf2_per_objfile->per_bfd->has_section_at_zero
8923 = dpo_backlink->per_bfd->has_section_at_zero;
8924 }
8925
8926 expand_psymtab (objfile);
8927
8928 process_cu_includes (dwarf2_per_objfile);
8929 }
8930 \f
8931 /* Reading in full CUs. */
8932
8933 /* Add PER_CU to the queue. */
8934
8935 static void
8936 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8937 dwarf2_per_objfile *per_objfile,
8938 enum language pretend_language)
8939 {
8940 per_cu->queued = 1;
8941 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8942 }
8943
8944 /* If PER_CU is not yet queued, add it to the queue.
8945 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8946 dependency.
8947 The result is non-zero if PER_CU was queued, otherwise the result is zero
8948 meaning either PER_CU is already queued or it is already loaded.
8949
8950 N.B. There is an invariant here that if a CU is queued then it is loaded.
8951 The caller is required to load PER_CU if we return non-zero. */
8952
8953 static int
8954 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8955 dwarf2_per_cu_data *per_cu,
8956 dwarf2_per_objfile *per_objfile,
8957 enum language pretend_language)
8958 {
8959 /* We may arrive here during partial symbol reading, if we need full
8960 DIEs to process an unusual case (e.g. template arguments). Do
8961 not queue PER_CU, just tell our caller to load its DIEs. */
8962 if (per_cu->per_bfd->reading_partial_symbols)
8963 {
8964 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8965 return 1;
8966 return 0;
8967 }
8968
8969 /* Mark the dependence relation so that we don't flush PER_CU
8970 too early. */
8971 if (dependent_cu != NULL)
8972 dwarf2_add_dependence (dependent_cu, per_cu);
8973
8974 /* If it's already on the queue, we have nothing to do. */
8975 if (per_cu->queued)
8976 return 0;
8977
8978 /* If the compilation unit is already loaded, just mark it as
8979 used. */
8980 if (per_cu->cu != NULL)
8981 {
8982 per_cu->cu->last_used = 0;
8983 return 0;
8984 }
8985
8986 /* Add it to the queue. */
8987 queue_comp_unit (per_cu, per_objfile, pretend_language);
8988
8989 return 1;
8990 }
8991
8992 /* Process the queue. */
8993
8994 static void
8995 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8996 {
8997 if (dwarf_read_debug)
8998 {
8999 fprintf_unfiltered (gdb_stdlog,
9000 "Expanding one or more symtabs of objfile %s ...\n",
9001 objfile_name (dwarf2_per_objfile->objfile));
9002 }
9003
9004 /* The queue starts out with one item, but following a DIE reference
9005 may load a new CU, adding it to the end of the queue. */
9006 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
9007 {
9008 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
9009
9010 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
9011 /* Skip dummy CUs. */
9012 && item.per_cu->cu != NULL)
9013 {
9014 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9015 unsigned int debug_print_threshold;
9016 char buf[100];
9017
9018 if (per_cu->is_debug_types)
9019 {
9020 struct signatured_type *sig_type =
9021 (struct signatured_type *) per_cu;
9022
9023 sprintf (buf, "TU %s at offset %s",
9024 hex_string (sig_type->signature),
9025 sect_offset_str (per_cu->sect_off));
9026 /* There can be 100s of TUs.
9027 Only print them in verbose mode. */
9028 debug_print_threshold = 2;
9029 }
9030 else
9031 {
9032 sprintf (buf, "CU at offset %s",
9033 sect_offset_str (per_cu->sect_off));
9034 debug_print_threshold = 1;
9035 }
9036
9037 if (dwarf_read_debug >= debug_print_threshold)
9038 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9039
9040 if (per_cu->is_debug_types)
9041 process_full_type_unit (per_cu, dwarf2_per_objfile,
9042 item.pretend_language);
9043 else
9044 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9045 item.pretend_language);
9046
9047 if (dwarf_read_debug >= debug_print_threshold)
9048 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9049 }
9050
9051 item.per_cu->queued = 0;
9052 dwarf2_per_objfile->per_bfd->queue.pop ();
9053 }
9054
9055 if (dwarf_read_debug)
9056 {
9057 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9058 objfile_name (dwarf2_per_objfile->objfile));
9059 }
9060 }
9061
9062 /* Read in full symbols for PST, and anything it depends on. */
9063
9064 void
9065 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9066 {
9067 gdb_assert (!readin_p (objfile));
9068
9069 expand_dependencies (objfile);
9070
9071 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9072 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9073 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9074 }
9075
9076 /* See psympriv.h. */
9077
9078 bool
9079 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9080 {
9081 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9082 return per_objfile->symtab_set_p (per_cu_data);
9083 }
9084
9085 /* See psympriv.h. */
9086
9087 compunit_symtab *
9088 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9089 {
9090 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9091 return per_objfile->get_symtab (per_cu_data);
9092 }
9093
9094 /* Trivial hash function for die_info: the hash value of a DIE
9095 is its offset in .debug_info for this objfile. */
9096
9097 static hashval_t
9098 die_hash (const void *item)
9099 {
9100 const struct die_info *die = (const struct die_info *) item;
9101
9102 return to_underlying (die->sect_off);
9103 }
9104
9105 /* Trivial comparison function for die_info structures: two DIEs
9106 are equal if they have the same offset. */
9107
9108 static int
9109 die_eq (const void *item_lhs, const void *item_rhs)
9110 {
9111 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9112 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9113
9114 return die_lhs->sect_off == die_rhs->sect_off;
9115 }
9116
9117 /* Load the DIEs associated with PER_CU into memory. */
9118
9119 static void
9120 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9121 dwarf2_per_objfile *per_objfile,
9122 bool skip_partial,
9123 enum language pretend_language)
9124 {
9125 gdb_assert (! this_cu->is_debug_types);
9126
9127 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9128 if (reader.dummy_p)
9129 return;
9130
9131 struct dwarf2_cu *cu = reader.cu;
9132 const gdb_byte *info_ptr = reader.info_ptr;
9133
9134 gdb_assert (cu->die_hash == NULL);
9135 cu->die_hash =
9136 htab_create_alloc_ex (cu->header.length / 12,
9137 die_hash,
9138 die_eq,
9139 NULL,
9140 &cu->comp_unit_obstack,
9141 hashtab_obstack_allocate,
9142 dummy_obstack_deallocate);
9143
9144 if (reader.comp_unit_die->has_children)
9145 reader.comp_unit_die->child
9146 = read_die_and_siblings (&reader, reader.info_ptr,
9147 &info_ptr, reader.comp_unit_die);
9148 cu->dies = reader.comp_unit_die;
9149 /* comp_unit_die is not stored in die_hash, no need. */
9150
9151 /* We try not to read any attributes in this function, because not
9152 all CUs needed for references have been loaded yet, and symbol
9153 table processing isn't initialized. But we have to set the CU language,
9154 or we won't be able to build types correctly.
9155 Similarly, if we do not read the producer, we can not apply
9156 producer-specific interpretation. */
9157 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9158
9159 reader.keep ();
9160 }
9161
9162 /* Add a DIE to the delayed physname list. */
9163
9164 static void
9165 add_to_method_list (struct type *type, int fnfield_index, int index,
9166 const char *name, struct die_info *die,
9167 struct dwarf2_cu *cu)
9168 {
9169 struct delayed_method_info mi;
9170 mi.type = type;
9171 mi.fnfield_index = fnfield_index;
9172 mi.index = index;
9173 mi.name = name;
9174 mi.die = die;
9175 cu->method_list.push_back (mi);
9176 }
9177
9178 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9179 "const" / "volatile". If so, decrements LEN by the length of the
9180 modifier and return true. Otherwise return false. */
9181
9182 template<size_t N>
9183 static bool
9184 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9185 {
9186 size_t mod_len = sizeof (mod) - 1;
9187 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9188 {
9189 len -= mod_len;
9190 return true;
9191 }
9192 return false;
9193 }
9194
9195 /* Compute the physnames of any methods on the CU's method list.
9196
9197 The computation of method physnames is delayed in order to avoid the
9198 (bad) condition that one of the method's formal parameters is of an as yet
9199 incomplete type. */
9200
9201 static void
9202 compute_delayed_physnames (struct dwarf2_cu *cu)
9203 {
9204 /* Only C++ delays computing physnames. */
9205 if (cu->method_list.empty ())
9206 return;
9207 gdb_assert (cu->language == language_cplus);
9208
9209 for (const delayed_method_info &mi : cu->method_list)
9210 {
9211 const char *physname;
9212 struct fn_fieldlist *fn_flp
9213 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9214 physname = dwarf2_physname (mi.name, mi.die, cu);
9215 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9216 = physname ? physname : "";
9217
9218 /* Since there's no tag to indicate whether a method is a
9219 const/volatile overload, extract that information out of the
9220 demangled name. */
9221 if (physname != NULL)
9222 {
9223 size_t len = strlen (physname);
9224
9225 while (1)
9226 {
9227 if (physname[len] == ')') /* shortcut */
9228 break;
9229 else if (check_modifier (physname, len, " const"))
9230 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9231 else if (check_modifier (physname, len, " volatile"))
9232 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9233 else
9234 break;
9235 }
9236 }
9237 }
9238
9239 /* The list is no longer needed. */
9240 cu->method_list.clear ();
9241 }
9242
9243 /* Go objects should be embedded in a DW_TAG_module DIE,
9244 and it's not clear if/how imported objects will appear.
9245 To keep Go support simple until that's worked out,
9246 go back through what we've read and create something usable.
9247 We could do this while processing each DIE, and feels kinda cleaner,
9248 but that way is more invasive.
9249 This is to, for example, allow the user to type "p var" or "b main"
9250 without having to specify the package name, and allow lookups
9251 of module.object to work in contexts that use the expression
9252 parser. */
9253
9254 static void
9255 fixup_go_packaging (struct dwarf2_cu *cu)
9256 {
9257 gdb::unique_xmalloc_ptr<char> package_name;
9258 struct pending *list;
9259 int i;
9260
9261 for (list = *cu->get_builder ()->get_global_symbols ();
9262 list != NULL;
9263 list = list->next)
9264 {
9265 for (i = 0; i < list->nsyms; ++i)
9266 {
9267 struct symbol *sym = list->symbol[i];
9268
9269 if (sym->language () == language_go
9270 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9271 {
9272 gdb::unique_xmalloc_ptr<char> this_package_name
9273 (go_symbol_package_name (sym));
9274
9275 if (this_package_name == NULL)
9276 continue;
9277 if (package_name == NULL)
9278 package_name = std::move (this_package_name);
9279 else
9280 {
9281 struct objfile *objfile = cu->per_objfile->objfile;
9282 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9283 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9284 (symbol_symtab (sym) != NULL
9285 ? symtab_to_filename_for_display
9286 (symbol_symtab (sym))
9287 : objfile_name (objfile)),
9288 this_package_name.get (), package_name.get ());
9289 }
9290 }
9291 }
9292 }
9293
9294 if (package_name != NULL)
9295 {
9296 struct objfile *objfile = cu->per_objfile->objfile;
9297 const char *saved_package_name = objfile->intern (package_name.get ());
9298 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9299 saved_package_name);
9300 struct symbol *sym;
9301
9302 sym = new (&objfile->objfile_obstack) symbol;
9303 sym->set_language (language_go, &objfile->objfile_obstack);
9304 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9305 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9306 e.g., "main" finds the "main" module and not C's main(). */
9307 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9308 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9309 SYMBOL_TYPE (sym) = type;
9310
9311 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9312 }
9313 }
9314
9315 /* Allocate a fully-qualified name consisting of the two parts on the
9316 obstack. */
9317
9318 static const char *
9319 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9320 {
9321 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9322 }
9323
9324 /* A helper that allocates a variant part to attach to a Rust enum
9325 type. OBSTACK is where the results should be allocated. TYPE is
9326 the type we're processing. DISCRIMINANT_INDEX is the index of the
9327 discriminant. It must be the index of one of the fields of TYPE.
9328 DEFAULT_INDEX is the index of the default field; or -1 if there is
9329 no default. RANGES is indexed by "effective" field number (the
9330 field index, but omitting the discriminant and default fields) and
9331 must hold the discriminant values used by the variants. Note that
9332 RANGES must have a lifetime at least as long as OBSTACK -- either
9333 already allocated on it, or static. */
9334
9335 static void
9336 alloc_rust_variant (struct obstack *obstack, struct type *type,
9337 int discriminant_index, int default_index,
9338 gdb::array_view<discriminant_range> ranges)
9339 {
9340 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9341 must be handled by the caller. */
9342 gdb_assert (discriminant_index >= 0
9343 && discriminant_index < type->num_fields ());
9344 gdb_assert (default_index == -1
9345 || (default_index >= 0 && default_index < type->num_fields ()));
9346
9347 /* We have one variant for each non-discriminant field. */
9348 int n_variants = type->num_fields () - 1;
9349
9350 variant *variants = new (obstack) variant[n_variants];
9351 int var_idx = 0;
9352 int range_idx = 0;
9353 for (int i = 0; i < type->num_fields (); ++i)
9354 {
9355 if (i == discriminant_index)
9356 continue;
9357
9358 variants[var_idx].first_field = i;
9359 variants[var_idx].last_field = i + 1;
9360
9361 /* The default field does not need a range, but other fields do.
9362 We skipped the discriminant above. */
9363 if (i != default_index)
9364 {
9365 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9366 ++range_idx;
9367 }
9368
9369 ++var_idx;
9370 }
9371
9372 gdb_assert (range_idx == ranges.size ());
9373 gdb_assert (var_idx == n_variants);
9374
9375 variant_part *part = new (obstack) variant_part;
9376 part->discriminant_index = discriminant_index;
9377 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9378 discriminant_index));
9379 part->variants = gdb::array_view<variant> (variants, n_variants);
9380
9381 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9382 gdb::array_view<variant_part> *prop_value
9383 = new (storage) gdb::array_view<variant_part> (part, 1);
9384
9385 struct dynamic_prop prop;
9386 prop.kind = PROP_VARIANT_PARTS;
9387 prop.data.variant_parts = prop_value;
9388
9389 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9390 }
9391
9392 /* Some versions of rustc emitted enums in an unusual way.
9393
9394 Ordinary enums were emitted as unions. The first element of each
9395 structure in the union was named "RUST$ENUM$DISR". This element
9396 held the discriminant.
9397
9398 These versions of Rust also implemented the "non-zero"
9399 optimization. When the enum had two values, and one is empty and
9400 the other holds a pointer that cannot be zero, the pointer is used
9401 as the discriminant, with a zero value meaning the empty variant.
9402 Here, the union's first member is of the form
9403 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9404 where the fieldnos are the indices of the fields that should be
9405 traversed in order to find the field (which may be several fields deep)
9406 and the variantname is the name of the variant of the case when the
9407 field is zero.
9408
9409 This function recognizes whether TYPE is of one of these forms,
9410 and, if so, smashes it to be a variant type. */
9411
9412 static void
9413 quirk_rust_enum (struct type *type, struct objfile *objfile)
9414 {
9415 gdb_assert (type->code () == TYPE_CODE_UNION);
9416
9417 /* We don't need to deal with empty enums. */
9418 if (type->num_fields () == 0)
9419 return;
9420
9421 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9422 if (type->num_fields () == 1
9423 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9424 {
9425 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9426
9427 /* Decode the field name to find the offset of the
9428 discriminant. */
9429 ULONGEST bit_offset = 0;
9430 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9431 while (name[0] >= '0' && name[0] <= '9')
9432 {
9433 char *tail;
9434 unsigned long index = strtoul (name, &tail, 10);
9435 name = tail;
9436 if (*name != '$'
9437 || index >= field_type->num_fields ()
9438 || (TYPE_FIELD_LOC_KIND (field_type, index)
9439 != FIELD_LOC_KIND_BITPOS))
9440 {
9441 complaint (_("Could not parse Rust enum encoding string \"%s\""
9442 "[in module %s]"),
9443 TYPE_FIELD_NAME (type, 0),
9444 objfile_name (objfile));
9445 return;
9446 }
9447 ++name;
9448
9449 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9450 field_type = TYPE_FIELD_TYPE (field_type, index);
9451 }
9452
9453 /* Smash this type to be a structure type. We have to do this
9454 because the type has already been recorded. */
9455 type->set_code (TYPE_CODE_STRUCT);
9456 type->set_num_fields (3);
9457 /* Save the field we care about. */
9458 struct field saved_field = type->field (0);
9459 type->set_fields
9460 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9461
9462 /* Put the discriminant at index 0. */
9463 TYPE_FIELD_TYPE (type, 0) = field_type;
9464 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9465 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9466 SET_FIELD_BITPOS (type->field (0), bit_offset);
9467
9468 /* The order of fields doesn't really matter, so put the real
9469 field at index 1 and the data-less field at index 2. */
9470 type->field (1) = saved_field;
9471 TYPE_FIELD_NAME (type, 1)
9472 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9473 TYPE_FIELD_TYPE (type, 1)->set_name
9474 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9475 TYPE_FIELD_NAME (type, 1)));
9476
9477 const char *dataless_name
9478 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9479 name);
9480 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9481 dataless_name);
9482 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9483 /* NAME points into the original discriminant name, which
9484 already has the correct lifetime. */
9485 TYPE_FIELD_NAME (type, 2) = name;
9486 SET_FIELD_BITPOS (type->field (2), 0);
9487
9488 /* Indicate that this is a variant type. */
9489 static discriminant_range ranges[1] = { { 0, 0 } };
9490 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9491 }
9492 /* A union with a single anonymous field is probably an old-style
9493 univariant enum. */
9494 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9495 {
9496 /* Smash this type to be a structure type. We have to do this
9497 because the type has already been recorded. */
9498 type->set_code (TYPE_CODE_STRUCT);
9499
9500 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9501 const char *variant_name
9502 = rust_last_path_segment (field_type->name ());
9503 TYPE_FIELD_NAME (type, 0) = variant_name;
9504 field_type->set_name
9505 (rust_fully_qualify (&objfile->objfile_obstack,
9506 type->name (), variant_name));
9507 }
9508 else
9509 {
9510 struct type *disr_type = nullptr;
9511 for (int i = 0; i < type->num_fields (); ++i)
9512 {
9513 disr_type = TYPE_FIELD_TYPE (type, i);
9514
9515 if (disr_type->code () != TYPE_CODE_STRUCT)
9516 {
9517 /* All fields of a true enum will be structs. */
9518 return;
9519 }
9520 else if (disr_type->num_fields () == 0)
9521 {
9522 /* Could be data-less variant, so keep going. */
9523 disr_type = nullptr;
9524 }
9525 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9526 "RUST$ENUM$DISR") != 0)
9527 {
9528 /* Not a Rust enum. */
9529 return;
9530 }
9531 else
9532 {
9533 /* Found one. */
9534 break;
9535 }
9536 }
9537
9538 /* If we got here without a discriminant, then it's probably
9539 just a union. */
9540 if (disr_type == nullptr)
9541 return;
9542
9543 /* Smash this type to be a structure type. We have to do this
9544 because the type has already been recorded. */
9545 type->set_code (TYPE_CODE_STRUCT);
9546
9547 /* Make space for the discriminant field. */
9548 struct field *disr_field = &disr_type->field (0);
9549 field *new_fields
9550 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9551 * sizeof (struct field)));
9552 memcpy (new_fields + 1, type->fields (),
9553 type->num_fields () * sizeof (struct field));
9554 type->set_fields (new_fields);
9555 type->set_num_fields (type->num_fields () + 1);
9556
9557 /* Install the discriminant at index 0 in the union. */
9558 type->field (0) = *disr_field;
9559 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9560 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9561
9562 /* We need a way to find the correct discriminant given a
9563 variant name. For convenience we build a map here. */
9564 struct type *enum_type = FIELD_TYPE (*disr_field);
9565 std::unordered_map<std::string, ULONGEST> discriminant_map;
9566 for (int i = 0; i < enum_type->num_fields (); ++i)
9567 {
9568 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9569 {
9570 const char *name
9571 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9572 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9573 }
9574 }
9575
9576 int n_fields = type->num_fields ();
9577 /* We don't need a range entry for the discriminant, but we do
9578 need one for every other field, as there is no default
9579 variant. */
9580 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9581 discriminant_range,
9582 n_fields - 1);
9583 /* Skip the discriminant here. */
9584 for (int i = 1; i < n_fields; ++i)
9585 {
9586 /* Find the final word in the name of this variant's type.
9587 That name can be used to look up the correct
9588 discriminant. */
9589 const char *variant_name
9590 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9591
9592 auto iter = discriminant_map.find (variant_name);
9593 if (iter != discriminant_map.end ())
9594 {
9595 ranges[i].low = iter->second;
9596 ranges[i].high = iter->second;
9597 }
9598
9599 /* Remove the discriminant field, if it exists. */
9600 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9601 if (sub_type->num_fields () > 0)
9602 {
9603 sub_type->set_num_fields (sub_type->num_fields () - 1);
9604 sub_type->set_fields (sub_type->fields () + 1);
9605 }
9606 TYPE_FIELD_NAME (type, i) = variant_name;
9607 sub_type->set_name
9608 (rust_fully_qualify (&objfile->objfile_obstack,
9609 type->name (), variant_name));
9610 }
9611
9612 /* Indicate that this is a variant type. */
9613 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9614 gdb::array_view<discriminant_range> (ranges,
9615 n_fields - 1));
9616 }
9617 }
9618
9619 /* Rewrite some Rust unions to be structures with variants parts. */
9620
9621 static void
9622 rust_union_quirks (struct dwarf2_cu *cu)
9623 {
9624 gdb_assert (cu->language == language_rust);
9625 for (type *type_ : cu->rust_unions)
9626 quirk_rust_enum (type_, cu->per_objfile->objfile);
9627 /* We don't need this any more. */
9628 cu->rust_unions.clear ();
9629 }
9630
9631 /* A helper function for computing the list of all symbol tables
9632 included by PER_CU. */
9633
9634 static void
9635 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9636 htab_t all_children, htab_t all_type_symtabs,
9637 dwarf2_per_cu_data *per_cu,
9638 dwarf2_per_objfile *per_objfile,
9639 struct compunit_symtab *immediate_parent)
9640 {
9641 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9642 if (*slot != NULL)
9643 {
9644 /* This inclusion and its children have been processed. */
9645 return;
9646 }
9647
9648 *slot = per_cu;
9649
9650 /* Only add a CU if it has a symbol table. */
9651 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9652 if (cust != NULL)
9653 {
9654 /* If this is a type unit only add its symbol table if we haven't
9655 seen it yet (type unit per_cu's can share symtabs). */
9656 if (per_cu->is_debug_types)
9657 {
9658 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9659 if (*slot == NULL)
9660 {
9661 *slot = cust;
9662 result->push_back (cust);
9663 if (cust->user == NULL)
9664 cust->user = immediate_parent;
9665 }
9666 }
9667 else
9668 {
9669 result->push_back (cust);
9670 if (cust->user == NULL)
9671 cust->user = immediate_parent;
9672 }
9673 }
9674
9675 if (!per_cu->imported_symtabs_empty ())
9676 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9677 {
9678 recursively_compute_inclusions (result, all_children,
9679 all_type_symtabs, ptr, per_objfile,
9680 cust);
9681 }
9682 }
9683
9684 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9685 PER_CU. */
9686
9687 static void
9688 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9689 dwarf2_per_objfile *per_objfile)
9690 {
9691 gdb_assert (! per_cu->is_debug_types);
9692
9693 if (!per_cu->imported_symtabs_empty ())
9694 {
9695 int len;
9696 std::vector<compunit_symtab *> result_symtabs;
9697 htab_t all_children, all_type_symtabs;
9698 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9699
9700 /* If we don't have a symtab, we can just skip this case. */
9701 if (cust == NULL)
9702 return;
9703
9704 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9705 NULL, xcalloc, xfree);
9706 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9707 NULL, xcalloc, xfree);
9708
9709 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9710 {
9711 recursively_compute_inclusions (&result_symtabs, all_children,
9712 all_type_symtabs, ptr, per_objfile,
9713 cust);
9714 }
9715
9716 /* Now we have a transitive closure of all the included symtabs. */
9717 len = result_symtabs.size ();
9718 cust->includes
9719 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9720 struct compunit_symtab *, len + 1);
9721 memcpy (cust->includes, result_symtabs.data (),
9722 len * sizeof (compunit_symtab *));
9723 cust->includes[len] = NULL;
9724
9725 htab_delete (all_children);
9726 htab_delete (all_type_symtabs);
9727 }
9728 }
9729
9730 /* Compute the 'includes' field for the symtabs of all the CUs we just
9731 read. */
9732
9733 static void
9734 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9735 {
9736 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9737 {
9738 if (! iter->is_debug_types)
9739 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9740 }
9741
9742 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9743 }
9744
9745 /* Generate full symbol information for PER_CU, whose DIEs have
9746 already been loaded into memory. */
9747
9748 static void
9749 process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9750 dwarf2_per_objfile *dwarf2_per_objfile,
9751 enum language pretend_language)
9752 {
9753 struct dwarf2_cu *cu = per_cu->cu;
9754 struct objfile *objfile = dwarf2_per_objfile->objfile;
9755 struct gdbarch *gdbarch = objfile->arch ();
9756 CORE_ADDR lowpc, highpc;
9757 struct compunit_symtab *cust;
9758 CORE_ADDR baseaddr;
9759 struct block *static_block;
9760 CORE_ADDR addr;
9761
9762 baseaddr = objfile->text_section_offset ();
9763
9764 /* Clear the list here in case something was left over. */
9765 cu->method_list.clear ();
9766
9767 cu->language = pretend_language;
9768 cu->language_defn = language_def (cu->language);
9769
9770 /* Do line number decoding in read_file_scope () */
9771 process_die (cu->dies, cu);
9772
9773 /* For now fudge the Go package. */
9774 if (cu->language == language_go)
9775 fixup_go_packaging (cu);
9776
9777 /* Now that we have processed all the DIEs in the CU, all the types
9778 should be complete, and it should now be safe to compute all of the
9779 physnames. */
9780 compute_delayed_physnames (cu);
9781
9782 if (cu->language == language_rust)
9783 rust_union_quirks (cu);
9784
9785 /* Some compilers don't define a DW_AT_high_pc attribute for the
9786 compilation unit. If the DW_AT_high_pc is missing, synthesize
9787 it, by scanning the DIE's below the compilation unit. */
9788 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9789
9790 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9791 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9792
9793 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9794 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9795 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9796 addrmap to help ensure it has an accurate map of pc values belonging to
9797 this comp unit. */
9798 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9799
9800 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9801 SECT_OFF_TEXT (objfile),
9802 0);
9803
9804 if (cust != NULL)
9805 {
9806 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9807
9808 /* Set symtab language to language from DW_AT_language. If the
9809 compilation is from a C file generated by language preprocessors, do
9810 not set the language if it was already deduced by start_subfile. */
9811 if (!(cu->language == language_c
9812 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9813 COMPUNIT_FILETABS (cust)->language = cu->language;
9814
9815 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9816 produce DW_AT_location with location lists but it can be possibly
9817 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9818 there were bugs in prologue debug info, fixed later in GCC-4.5
9819 by "unwind info for epilogues" patch (which is not directly related).
9820
9821 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9822 needed, it would be wrong due to missing DW_AT_producer there.
9823
9824 Still one can confuse GDB by using non-standard GCC compilation
9825 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9826 */
9827 if (cu->has_loclist && gcc_4_minor >= 5)
9828 cust->locations_valid = 1;
9829
9830 if (gcc_4_minor >= 5)
9831 cust->epilogue_unwind_valid = 1;
9832
9833 cust->call_site_htab = cu->call_site_htab;
9834 }
9835
9836 dwarf2_per_objfile->set_symtab (per_cu, cust);
9837
9838 /* Push it for inclusion processing later. */
9839 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9840
9841 /* Not needed any more. */
9842 cu->reset_builder ();
9843 }
9844
9845 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9846 already been loaded into memory. */
9847
9848 static void
9849 process_full_type_unit (dwarf2_per_cu_data *per_cu,
9850 dwarf2_per_objfile *dwarf2_per_objfile,
9851 enum language pretend_language)
9852 {
9853 struct dwarf2_cu *cu = per_cu->cu;
9854 struct objfile *objfile = dwarf2_per_objfile->objfile;
9855 struct compunit_symtab *cust;
9856 struct signatured_type *sig_type;
9857
9858 gdb_assert (per_cu->is_debug_types);
9859 sig_type = (struct signatured_type *) per_cu;
9860
9861 /* Clear the list here in case something was left over. */
9862 cu->method_list.clear ();
9863
9864 cu->language = pretend_language;
9865 cu->language_defn = language_def (cu->language);
9866
9867 /* The symbol tables are set up in read_type_unit_scope. */
9868 process_die (cu->dies, cu);
9869
9870 /* For now fudge the Go package. */
9871 if (cu->language == language_go)
9872 fixup_go_packaging (cu);
9873
9874 /* Now that we have processed all the DIEs in the CU, all the types
9875 should be complete, and it should now be safe to compute all of the
9876 physnames. */
9877 compute_delayed_physnames (cu);
9878
9879 if (cu->language == language_rust)
9880 rust_union_quirks (cu);
9881
9882 /* TUs share symbol tables.
9883 If this is the first TU to use this symtab, complete the construction
9884 of it with end_expandable_symtab. Otherwise, complete the addition of
9885 this TU's symbols to the existing symtab. */
9886 if (sig_type->type_unit_group->compunit_symtab == NULL)
9887 {
9888 buildsym_compunit *builder = cu->get_builder ();
9889 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9890 sig_type->type_unit_group->compunit_symtab = cust;
9891
9892 if (cust != NULL)
9893 {
9894 /* Set symtab language to language from DW_AT_language. If the
9895 compilation is from a C file generated by language preprocessors,
9896 do not set the language if it was already deduced by
9897 start_subfile. */
9898 if (!(cu->language == language_c
9899 && COMPUNIT_FILETABS (cust)->language != language_c))
9900 COMPUNIT_FILETABS (cust)->language = cu->language;
9901 }
9902 }
9903 else
9904 {
9905 cu->get_builder ()->augment_type_symtab ();
9906 cust = sig_type->type_unit_group->compunit_symtab;
9907 }
9908
9909 dwarf2_per_objfile->set_symtab (per_cu, cust);
9910
9911 /* Not needed any more. */
9912 cu->reset_builder ();
9913 }
9914
9915 /* Process an imported unit DIE. */
9916
9917 static void
9918 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9919 {
9920 struct attribute *attr;
9921
9922 /* For now we don't handle imported units in type units. */
9923 if (cu->per_cu->is_debug_types)
9924 {
9925 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9926 " supported in type units [in module %s]"),
9927 objfile_name (cu->per_objfile->objfile));
9928 }
9929
9930 attr = dwarf2_attr (die, DW_AT_import, cu);
9931 if (attr != NULL)
9932 {
9933 sect_offset sect_off = attr->get_ref_die_offset ();
9934 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9935 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9936 dwarf2_per_cu_data *per_cu
9937 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9938
9939 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9940 into another compilation unit, at root level. Regard this as a hint,
9941 and ignore it. */
9942 if (die->parent && die->parent->parent == NULL
9943 && per_cu->unit_type == DW_UT_compile
9944 && per_cu->lang == language_cplus)
9945 return;
9946
9947 /* If necessary, add it to the queue and load its DIEs. */
9948 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9949 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9950
9951 cu->per_cu->imported_symtabs_push (per_cu);
9952 }
9953 }
9954
9955 /* RAII object that represents a process_die scope: i.e.,
9956 starts/finishes processing a DIE. */
9957 class process_die_scope
9958 {
9959 public:
9960 process_die_scope (die_info *die, dwarf2_cu *cu)
9961 : m_die (die), m_cu (cu)
9962 {
9963 /* We should only be processing DIEs not already in process. */
9964 gdb_assert (!m_die->in_process);
9965 m_die->in_process = true;
9966 }
9967
9968 ~process_die_scope ()
9969 {
9970 m_die->in_process = false;
9971
9972 /* If we're done processing the DIE for the CU that owns the line
9973 header, we don't need the line header anymore. */
9974 if (m_cu->line_header_die_owner == m_die)
9975 {
9976 delete m_cu->line_header;
9977 m_cu->line_header = NULL;
9978 m_cu->line_header_die_owner = NULL;
9979 }
9980 }
9981
9982 private:
9983 die_info *m_die;
9984 dwarf2_cu *m_cu;
9985 };
9986
9987 /* Process a die and its children. */
9988
9989 static void
9990 process_die (struct die_info *die, struct dwarf2_cu *cu)
9991 {
9992 process_die_scope scope (die, cu);
9993
9994 switch (die->tag)
9995 {
9996 case DW_TAG_padding:
9997 break;
9998 case DW_TAG_compile_unit:
9999 case DW_TAG_partial_unit:
10000 read_file_scope (die, cu);
10001 break;
10002 case DW_TAG_type_unit:
10003 read_type_unit_scope (die, cu);
10004 break;
10005 case DW_TAG_subprogram:
10006 /* Nested subprograms in Fortran get a prefix. */
10007 if (cu->language == language_fortran
10008 && die->parent != NULL
10009 && die->parent->tag == DW_TAG_subprogram)
10010 cu->processing_has_namespace_info = true;
10011 /* Fall through. */
10012 case DW_TAG_inlined_subroutine:
10013 read_func_scope (die, cu);
10014 break;
10015 case DW_TAG_lexical_block:
10016 case DW_TAG_try_block:
10017 case DW_TAG_catch_block:
10018 read_lexical_block_scope (die, cu);
10019 break;
10020 case DW_TAG_call_site:
10021 case DW_TAG_GNU_call_site:
10022 read_call_site_scope (die, cu);
10023 break;
10024 case DW_TAG_class_type:
10025 case DW_TAG_interface_type:
10026 case DW_TAG_structure_type:
10027 case DW_TAG_union_type:
10028 process_structure_scope (die, cu);
10029 break;
10030 case DW_TAG_enumeration_type:
10031 process_enumeration_scope (die, cu);
10032 break;
10033
10034 /* These dies have a type, but processing them does not create
10035 a symbol or recurse to process the children. Therefore we can
10036 read them on-demand through read_type_die. */
10037 case DW_TAG_subroutine_type:
10038 case DW_TAG_set_type:
10039 case DW_TAG_array_type:
10040 case DW_TAG_pointer_type:
10041 case DW_TAG_ptr_to_member_type:
10042 case DW_TAG_reference_type:
10043 case DW_TAG_rvalue_reference_type:
10044 case DW_TAG_string_type:
10045 break;
10046
10047 case DW_TAG_base_type:
10048 case DW_TAG_subrange_type:
10049 case DW_TAG_typedef:
10050 /* Add a typedef symbol for the type definition, if it has a
10051 DW_AT_name. */
10052 new_symbol (die, read_type_die (die, cu), cu);
10053 break;
10054 case DW_TAG_common_block:
10055 read_common_block (die, cu);
10056 break;
10057 case DW_TAG_common_inclusion:
10058 break;
10059 case DW_TAG_namespace:
10060 cu->processing_has_namespace_info = true;
10061 read_namespace (die, cu);
10062 break;
10063 case DW_TAG_module:
10064 cu->processing_has_namespace_info = true;
10065 read_module (die, cu);
10066 break;
10067 case DW_TAG_imported_declaration:
10068 cu->processing_has_namespace_info = true;
10069 if (read_namespace_alias (die, cu))
10070 break;
10071 /* The declaration is not a global namespace alias. */
10072 /* Fall through. */
10073 case DW_TAG_imported_module:
10074 cu->processing_has_namespace_info = true;
10075 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10076 || cu->language != language_fortran))
10077 complaint (_("Tag '%s' has unexpected children"),
10078 dwarf_tag_name (die->tag));
10079 read_import_statement (die, cu);
10080 break;
10081
10082 case DW_TAG_imported_unit:
10083 process_imported_unit_die (die, cu);
10084 break;
10085
10086 case DW_TAG_variable:
10087 read_variable (die, cu);
10088 break;
10089
10090 default:
10091 new_symbol (die, NULL, cu);
10092 break;
10093 }
10094 }
10095 \f
10096 /* DWARF name computation. */
10097
10098 /* A helper function for dwarf2_compute_name which determines whether DIE
10099 needs to have the name of the scope prepended to the name listed in the
10100 die. */
10101
10102 static int
10103 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10104 {
10105 struct attribute *attr;
10106
10107 switch (die->tag)
10108 {
10109 case DW_TAG_namespace:
10110 case DW_TAG_typedef:
10111 case DW_TAG_class_type:
10112 case DW_TAG_interface_type:
10113 case DW_TAG_structure_type:
10114 case DW_TAG_union_type:
10115 case DW_TAG_enumeration_type:
10116 case DW_TAG_enumerator:
10117 case DW_TAG_subprogram:
10118 case DW_TAG_inlined_subroutine:
10119 case DW_TAG_member:
10120 case DW_TAG_imported_declaration:
10121 return 1;
10122
10123 case DW_TAG_variable:
10124 case DW_TAG_constant:
10125 /* We only need to prefix "globally" visible variables. These include
10126 any variable marked with DW_AT_external or any variable that
10127 lives in a namespace. [Variables in anonymous namespaces
10128 require prefixing, but they are not DW_AT_external.] */
10129
10130 if (dwarf2_attr (die, DW_AT_specification, cu))
10131 {
10132 struct dwarf2_cu *spec_cu = cu;
10133
10134 return die_needs_namespace (die_specification (die, &spec_cu),
10135 spec_cu);
10136 }
10137
10138 attr = dwarf2_attr (die, DW_AT_external, cu);
10139 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10140 && die->parent->tag != DW_TAG_module)
10141 return 0;
10142 /* A variable in a lexical block of some kind does not need a
10143 namespace, even though in C++ such variables may be external
10144 and have a mangled name. */
10145 if (die->parent->tag == DW_TAG_lexical_block
10146 || die->parent->tag == DW_TAG_try_block
10147 || die->parent->tag == DW_TAG_catch_block
10148 || die->parent->tag == DW_TAG_subprogram)
10149 return 0;
10150 return 1;
10151
10152 default:
10153 return 0;
10154 }
10155 }
10156
10157 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10158 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10159 defined for the given DIE. */
10160
10161 static struct attribute *
10162 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10163 {
10164 struct attribute *attr;
10165
10166 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10167 if (attr == NULL)
10168 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10169
10170 return attr;
10171 }
10172
10173 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10174 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10175 defined for the given DIE. */
10176
10177 static const char *
10178 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10179 {
10180 const char *linkage_name;
10181
10182 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10183 if (linkage_name == NULL)
10184 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10185
10186 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10187 See https://github.com/rust-lang/rust/issues/32925. */
10188 if (cu->language == language_rust && linkage_name != NULL
10189 && strchr (linkage_name, '{') != NULL)
10190 linkage_name = NULL;
10191
10192 return linkage_name;
10193 }
10194
10195 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10196 compute the physname for the object, which include a method's:
10197 - formal parameters (C++),
10198 - receiver type (Go),
10199
10200 The term "physname" is a bit confusing.
10201 For C++, for example, it is the demangled name.
10202 For Go, for example, it's the mangled name.
10203
10204 For Ada, return the DIE's linkage name rather than the fully qualified
10205 name. PHYSNAME is ignored..
10206
10207 The result is allocated on the objfile->per_bfd's obstack and
10208 canonicalized. */
10209
10210 static const char *
10211 dwarf2_compute_name (const char *name,
10212 struct die_info *die, struct dwarf2_cu *cu,
10213 int physname)
10214 {
10215 struct objfile *objfile = cu->per_objfile->objfile;
10216
10217 if (name == NULL)
10218 name = dwarf2_name (die, cu);
10219
10220 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10221 but otherwise compute it by typename_concat inside GDB.
10222 FIXME: Actually this is not really true, or at least not always true.
10223 It's all very confusing. compute_and_set_names doesn't try to demangle
10224 Fortran names because there is no mangling standard. So new_symbol
10225 will set the demangled name to the result of dwarf2_full_name, and it is
10226 the demangled name that GDB uses if it exists. */
10227 if (cu->language == language_ada
10228 || (cu->language == language_fortran && physname))
10229 {
10230 /* For Ada unit, we prefer the linkage name over the name, as
10231 the former contains the exported name, which the user expects
10232 to be able to reference. Ideally, we want the user to be able
10233 to reference this entity using either natural or linkage name,
10234 but we haven't started looking at this enhancement yet. */
10235 const char *linkage_name = dw2_linkage_name (die, cu);
10236
10237 if (linkage_name != NULL)
10238 return linkage_name;
10239 }
10240
10241 /* These are the only languages we know how to qualify names in. */
10242 if (name != NULL
10243 && (cu->language == language_cplus
10244 || cu->language == language_fortran || cu->language == language_d
10245 || cu->language == language_rust))
10246 {
10247 if (die_needs_namespace (die, cu))
10248 {
10249 const char *prefix;
10250 const char *canonical_name = NULL;
10251
10252 string_file buf;
10253
10254 prefix = determine_prefix (die, cu);
10255 if (*prefix != '\0')
10256 {
10257 gdb::unique_xmalloc_ptr<char> prefixed_name
10258 (typename_concat (NULL, prefix, name, physname, cu));
10259
10260 buf.puts (prefixed_name.get ());
10261 }
10262 else
10263 buf.puts (name);
10264
10265 /* Template parameters may be specified in the DIE's DW_AT_name, or
10266 as children with DW_TAG_template_type_param or
10267 DW_TAG_value_type_param. If the latter, add them to the name
10268 here. If the name already has template parameters, then
10269 skip this step; some versions of GCC emit both, and
10270 it is more efficient to use the pre-computed name.
10271
10272 Something to keep in mind about this process: it is very
10273 unlikely, or in some cases downright impossible, to produce
10274 something that will match the mangled name of a function.
10275 If the definition of the function has the same debug info,
10276 we should be able to match up with it anyway. But fallbacks
10277 using the minimal symbol, for instance to find a method
10278 implemented in a stripped copy of libstdc++, will not work.
10279 If we do not have debug info for the definition, we will have to
10280 match them up some other way.
10281
10282 When we do name matching there is a related problem with function
10283 templates; two instantiated function templates are allowed to
10284 differ only by their return types, which we do not add here. */
10285
10286 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10287 {
10288 struct attribute *attr;
10289 struct die_info *child;
10290 int first = 1;
10291
10292 die->building_fullname = 1;
10293
10294 for (child = die->child; child != NULL; child = child->sibling)
10295 {
10296 struct type *type;
10297 LONGEST value;
10298 const gdb_byte *bytes;
10299 struct dwarf2_locexpr_baton *baton;
10300 struct value *v;
10301
10302 if (child->tag != DW_TAG_template_type_param
10303 && child->tag != DW_TAG_template_value_param)
10304 continue;
10305
10306 if (first)
10307 {
10308 buf.puts ("<");
10309 first = 0;
10310 }
10311 else
10312 buf.puts (", ");
10313
10314 attr = dwarf2_attr (child, DW_AT_type, cu);
10315 if (attr == NULL)
10316 {
10317 complaint (_("template parameter missing DW_AT_type"));
10318 buf.puts ("UNKNOWN_TYPE");
10319 continue;
10320 }
10321 type = die_type (child, cu);
10322
10323 if (child->tag == DW_TAG_template_type_param)
10324 {
10325 c_print_type (type, "", &buf, -1, 0, cu->language,
10326 &type_print_raw_options);
10327 continue;
10328 }
10329
10330 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10331 if (attr == NULL)
10332 {
10333 complaint (_("template parameter missing "
10334 "DW_AT_const_value"));
10335 buf.puts ("UNKNOWN_VALUE");
10336 continue;
10337 }
10338
10339 dwarf2_const_value_attr (attr, type, name,
10340 &cu->comp_unit_obstack, cu,
10341 &value, &bytes, &baton);
10342
10343 if (TYPE_NOSIGN (type))
10344 /* GDB prints characters as NUMBER 'CHAR'. If that's
10345 changed, this can use value_print instead. */
10346 c_printchar (value, type, &buf);
10347 else
10348 {
10349 struct value_print_options opts;
10350
10351 if (baton != NULL)
10352 v = dwarf2_evaluate_loc_desc (type, NULL,
10353 baton->data,
10354 baton->size,
10355 baton->per_cu,
10356 baton->per_objfile);
10357 else if (bytes != NULL)
10358 {
10359 v = allocate_value (type);
10360 memcpy (value_contents_writeable (v), bytes,
10361 TYPE_LENGTH (type));
10362 }
10363 else
10364 v = value_from_longest (type, value);
10365
10366 /* Specify decimal so that we do not depend on
10367 the radix. */
10368 get_formatted_print_options (&opts, 'd');
10369 opts.raw = 1;
10370 value_print (v, &buf, &opts);
10371 release_value (v);
10372 }
10373 }
10374
10375 die->building_fullname = 0;
10376
10377 if (!first)
10378 {
10379 /* Close the argument list, with a space if necessary
10380 (nested templates). */
10381 if (!buf.empty () && buf.string ().back () == '>')
10382 buf.puts (" >");
10383 else
10384 buf.puts (">");
10385 }
10386 }
10387
10388 /* For C++ methods, append formal parameter type
10389 information, if PHYSNAME. */
10390
10391 if (physname && die->tag == DW_TAG_subprogram
10392 && cu->language == language_cplus)
10393 {
10394 struct type *type = read_type_die (die, cu);
10395
10396 c_type_print_args (type, &buf, 1, cu->language,
10397 &type_print_raw_options);
10398
10399 if (cu->language == language_cplus)
10400 {
10401 /* Assume that an artificial first parameter is
10402 "this", but do not crash if it is not. RealView
10403 marks unnamed (and thus unused) parameters as
10404 artificial; there is no way to differentiate
10405 the two cases. */
10406 if (type->num_fields () > 0
10407 && TYPE_FIELD_ARTIFICIAL (type, 0)
10408 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10409 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10410 0))))
10411 buf.puts (" const");
10412 }
10413 }
10414
10415 const std::string &intermediate_name = buf.string ();
10416
10417 if (cu->language == language_cplus)
10418 canonical_name
10419 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10420 objfile);
10421
10422 /* If we only computed INTERMEDIATE_NAME, or if
10423 INTERMEDIATE_NAME is already canonical, then we need to
10424 intern it. */
10425 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10426 name = objfile->intern (intermediate_name);
10427 else
10428 name = canonical_name;
10429 }
10430 }
10431
10432 return name;
10433 }
10434
10435 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10436 If scope qualifiers are appropriate they will be added. The result
10437 will be allocated on the storage_obstack, or NULL if the DIE does
10438 not have a name. NAME may either be from a previous call to
10439 dwarf2_name or NULL.
10440
10441 The output string will be canonicalized (if C++). */
10442
10443 static const char *
10444 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10445 {
10446 return dwarf2_compute_name (name, die, cu, 0);
10447 }
10448
10449 /* Construct a physname for the given DIE in CU. NAME may either be
10450 from a previous call to dwarf2_name or NULL. The result will be
10451 allocated on the objfile_objstack or NULL if the DIE does not have a
10452 name.
10453
10454 The output string will be canonicalized (if C++). */
10455
10456 static const char *
10457 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10458 {
10459 struct objfile *objfile = cu->per_objfile->objfile;
10460 const char *retval, *mangled = NULL, *canon = NULL;
10461 int need_copy = 1;
10462
10463 /* In this case dwarf2_compute_name is just a shortcut not building anything
10464 on its own. */
10465 if (!die_needs_namespace (die, cu))
10466 return dwarf2_compute_name (name, die, cu, 1);
10467
10468 if (cu->language != language_rust)
10469 mangled = dw2_linkage_name (die, cu);
10470
10471 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10472 has computed. */
10473 gdb::unique_xmalloc_ptr<char> demangled;
10474 if (mangled != NULL)
10475 {
10476
10477 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10478 {
10479 /* Do nothing (do not demangle the symbol name). */
10480 }
10481 else if (cu->language == language_go)
10482 {
10483 /* This is a lie, but we already lie to the caller new_symbol.
10484 new_symbol assumes we return the mangled name.
10485 This just undoes that lie until things are cleaned up. */
10486 }
10487 else
10488 {
10489 /* Use DMGL_RET_DROP for C++ template functions to suppress
10490 their return type. It is easier for GDB users to search
10491 for such functions as `name(params)' than `long name(params)'.
10492 In such case the minimal symbol names do not match the full
10493 symbol names but for template functions there is never a need
10494 to look up their definition from their declaration so
10495 the only disadvantage remains the minimal symbol variant
10496 `long name(params)' does not have the proper inferior type. */
10497 demangled.reset (gdb_demangle (mangled,
10498 (DMGL_PARAMS | DMGL_ANSI
10499 | DMGL_RET_DROP)));
10500 }
10501 if (demangled)
10502 canon = demangled.get ();
10503 else
10504 {
10505 canon = mangled;
10506 need_copy = 0;
10507 }
10508 }
10509
10510 if (canon == NULL || check_physname)
10511 {
10512 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10513
10514 if (canon != NULL && strcmp (physname, canon) != 0)
10515 {
10516 /* It may not mean a bug in GDB. The compiler could also
10517 compute DW_AT_linkage_name incorrectly. But in such case
10518 GDB would need to be bug-to-bug compatible. */
10519
10520 complaint (_("Computed physname <%s> does not match demangled <%s> "
10521 "(from linkage <%s>) - DIE at %s [in module %s]"),
10522 physname, canon, mangled, sect_offset_str (die->sect_off),
10523 objfile_name (objfile));
10524
10525 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10526 is available here - over computed PHYSNAME. It is safer
10527 against both buggy GDB and buggy compilers. */
10528
10529 retval = canon;
10530 }
10531 else
10532 {
10533 retval = physname;
10534 need_copy = 0;
10535 }
10536 }
10537 else
10538 retval = canon;
10539
10540 if (need_copy)
10541 retval = objfile->intern (retval);
10542
10543 return retval;
10544 }
10545
10546 /* Inspect DIE in CU for a namespace alias. If one exists, record
10547 a new symbol for it.
10548
10549 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10550
10551 static int
10552 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10553 {
10554 struct attribute *attr;
10555
10556 /* If the die does not have a name, this is not a namespace
10557 alias. */
10558 attr = dwarf2_attr (die, DW_AT_name, cu);
10559 if (attr != NULL)
10560 {
10561 int num;
10562 struct die_info *d = die;
10563 struct dwarf2_cu *imported_cu = cu;
10564
10565 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10566 keep inspecting DIEs until we hit the underlying import. */
10567 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10568 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10569 {
10570 attr = dwarf2_attr (d, DW_AT_import, cu);
10571 if (attr == NULL)
10572 break;
10573
10574 d = follow_die_ref (d, attr, &imported_cu);
10575 if (d->tag != DW_TAG_imported_declaration)
10576 break;
10577 }
10578
10579 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10580 {
10581 complaint (_("DIE at %s has too many recursively imported "
10582 "declarations"), sect_offset_str (d->sect_off));
10583 return 0;
10584 }
10585
10586 if (attr != NULL)
10587 {
10588 struct type *type;
10589 sect_offset sect_off = attr->get_ref_die_offset ();
10590
10591 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10592 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10593 {
10594 /* This declaration is a global namespace alias. Add
10595 a symbol for it whose type is the aliased namespace. */
10596 new_symbol (die, type, cu);
10597 return 1;
10598 }
10599 }
10600 }
10601
10602 return 0;
10603 }
10604
10605 /* Return the using directives repository (global or local?) to use in the
10606 current context for CU.
10607
10608 For Ada, imported declarations can materialize renamings, which *may* be
10609 global. However it is impossible (for now?) in DWARF to distinguish
10610 "external" imported declarations and "static" ones. As all imported
10611 declarations seem to be static in all other languages, make them all CU-wide
10612 global only in Ada. */
10613
10614 static struct using_direct **
10615 using_directives (struct dwarf2_cu *cu)
10616 {
10617 if (cu->language == language_ada
10618 && cu->get_builder ()->outermost_context_p ())
10619 return cu->get_builder ()->get_global_using_directives ();
10620 else
10621 return cu->get_builder ()->get_local_using_directives ();
10622 }
10623
10624 /* Read the import statement specified by the given die and record it. */
10625
10626 static void
10627 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10628 {
10629 struct objfile *objfile = cu->per_objfile->objfile;
10630 struct attribute *import_attr;
10631 struct die_info *imported_die, *child_die;
10632 struct dwarf2_cu *imported_cu;
10633 const char *imported_name;
10634 const char *imported_name_prefix;
10635 const char *canonical_name;
10636 const char *import_alias;
10637 const char *imported_declaration = NULL;
10638 const char *import_prefix;
10639 std::vector<const char *> excludes;
10640
10641 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10642 if (import_attr == NULL)
10643 {
10644 complaint (_("Tag '%s' has no DW_AT_import"),
10645 dwarf_tag_name (die->tag));
10646 return;
10647 }
10648
10649 imported_cu = cu;
10650 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10651 imported_name = dwarf2_name (imported_die, imported_cu);
10652 if (imported_name == NULL)
10653 {
10654 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10655
10656 The import in the following code:
10657 namespace A
10658 {
10659 typedef int B;
10660 }
10661
10662 int main ()
10663 {
10664 using A::B;
10665 B b;
10666 return b;
10667 }
10668
10669 ...
10670 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10671 <52> DW_AT_decl_file : 1
10672 <53> DW_AT_decl_line : 6
10673 <54> DW_AT_import : <0x75>
10674 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10675 <59> DW_AT_name : B
10676 <5b> DW_AT_decl_file : 1
10677 <5c> DW_AT_decl_line : 2
10678 <5d> DW_AT_type : <0x6e>
10679 ...
10680 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10681 <76> DW_AT_byte_size : 4
10682 <77> DW_AT_encoding : 5 (signed)
10683
10684 imports the wrong die ( 0x75 instead of 0x58 ).
10685 This case will be ignored until the gcc bug is fixed. */
10686 return;
10687 }
10688
10689 /* Figure out the local name after import. */
10690 import_alias = dwarf2_name (die, cu);
10691
10692 /* Figure out where the statement is being imported to. */
10693 import_prefix = determine_prefix (die, cu);
10694
10695 /* Figure out what the scope of the imported die is and prepend it
10696 to the name of the imported die. */
10697 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10698
10699 if (imported_die->tag != DW_TAG_namespace
10700 && imported_die->tag != DW_TAG_module)
10701 {
10702 imported_declaration = imported_name;
10703 canonical_name = imported_name_prefix;
10704 }
10705 else if (strlen (imported_name_prefix) > 0)
10706 canonical_name = obconcat (&objfile->objfile_obstack,
10707 imported_name_prefix,
10708 (cu->language == language_d ? "." : "::"),
10709 imported_name, (char *) NULL);
10710 else
10711 canonical_name = imported_name;
10712
10713 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10714 for (child_die = die->child; child_die && child_die->tag;
10715 child_die = child_die->sibling)
10716 {
10717 /* DWARF-4: A Fortran use statement with a “rename list” may be
10718 represented by an imported module entry with an import attribute
10719 referring to the module and owned entries corresponding to those
10720 entities that are renamed as part of being imported. */
10721
10722 if (child_die->tag != DW_TAG_imported_declaration)
10723 {
10724 complaint (_("child DW_TAG_imported_declaration expected "
10725 "- DIE at %s [in module %s]"),
10726 sect_offset_str (child_die->sect_off),
10727 objfile_name (objfile));
10728 continue;
10729 }
10730
10731 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10732 if (import_attr == NULL)
10733 {
10734 complaint (_("Tag '%s' has no DW_AT_import"),
10735 dwarf_tag_name (child_die->tag));
10736 continue;
10737 }
10738
10739 imported_cu = cu;
10740 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10741 &imported_cu);
10742 imported_name = dwarf2_name (imported_die, imported_cu);
10743 if (imported_name == NULL)
10744 {
10745 complaint (_("child DW_TAG_imported_declaration has unknown "
10746 "imported name - DIE at %s [in module %s]"),
10747 sect_offset_str (child_die->sect_off),
10748 objfile_name (objfile));
10749 continue;
10750 }
10751
10752 excludes.push_back (imported_name);
10753
10754 process_die (child_die, cu);
10755 }
10756
10757 add_using_directive (using_directives (cu),
10758 import_prefix,
10759 canonical_name,
10760 import_alias,
10761 imported_declaration,
10762 excludes,
10763 0,
10764 &objfile->objfile_obstack);
10765 }
10766
10767 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10768 types, but gives them a size of zero. Starting with version 14,
10769 ICC is compatible with GCC. */
10770
10771 static bool
10772 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10773 {
10774 if (!cu->checked_producer)
10775 check_producer (cu);
10776
10777 return cu->producer_is_icc_lt_14;
10778 }
10779
10780 /* ICC generates a DW_AT_type for C void functions. This was observed on
10781 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10782 which says that void functions should not have a DW_AT_type. */
10783
10784 static bool
10785 producer_is_icc (struct dwarf2_cu *cu)
10786 {
10787 if (!cu->checked_producer)
10788 check_producer (cu);
10789
10790 return cu->producer_is_icc;
10791 }
10792
10793 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10794 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10795 this, it was first present in GCC release 4.3.0. */
10796
10797 static bool
10798 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10799 {
10800 if (!cu->checked_producer)
10801 check_producer (cu);
10802
10803 return cu->producer_is_gcc_lt_4_3;
10804 }
10805
10806 static file_and_directory
10807 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10808 {
10809 file_and_directory res;
10810
10811 /* Find the filename. Do not use dwarf2_name here, since the filename
10812 is not a source language identifier. */
10813 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10814 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10815
10816 if (res.comp_dir == NULL
10817 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10818 && IS_ABSOLUTE_PATH (res.name))
10819 {
10820 res.comp_dir_storage = ldirname (res.name);
10821 if (!res.comp_dir_storage.empty ())
10822 res.comp_dir = res.comp_dir_storage.c_str ();
10823 }
10824 if (res.comp_dir != NULL)
10825 {
10826 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10827 directory, get rid of it. */
10828 const char *cp = strchr (res.comp_dir, ':');
10829
10830 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10831 res.comp_dir = cp + 1;
10832 }
10833
10834 if (res.name == NULL)
10835 res.name = "<unknown>";
10836
10837 return res;
10838 }
10839
10840 /* Handle DW_AT_stmt_list for a compilation unit.
10841 DIE is the DW_TAG_compile_unit die for CU.
10842 COMP_DIR is the compilation directory. LOWPC is passed to
10843 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10844
10845 static void
10846 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10847 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10848 {
10849 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10850 struct attribute *attr;
10851 struct line_header line_header_local;
10852 hashval_t line_header_local_hash;
10853 void **slot;
10854 int decode_mapping;
10855
10856 gdb_assert (! cu->per_cu->is_debug_types);
10857
10858 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10859 if (attr == NULL)
10860 return;
10861
10862 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10863
10864 /* The line header hash table is only created if needed (it exists to
10865 prevent redundant reading of the line table for partial_units).
10866 If we're given a partial_unit, we'll need it. If we're given a
10867 compile_unit, then use the line header hash table if it's already
10868 created, but don't create one just yet. */
10869
10870 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10871 && die->tag == DW_TAG_partial_unit)
10872 {
10873 dwarf2_per_objfile->per_bfd->line_header_hash
10874 .reset (htab_create_alloc (127, line_header_hash_voidp,
10875 line_header_eq_voidp,
10876 free_line_header_voidp,
10877 xcalloc, xfree));
10878 }
10879
10880 line_header_local.sect_off = line_offset;
10881 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10882 line_header_local_hash = line_header_hash (&line_header_local);
10883 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10884 {
10885 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10886 &line_header_local,
10887 line_header_local_hash, NO_INSERT);
10888
10889 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10890 is not present in *SLOT (since if there is something in *SLOT then
10891 it will be for a partial_unit). */
10892 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10893 {
10894 gdb_assert (*slot != NULL);
10895 cu->line_header = (struct line_header *) *slot;
10896 return;
10897 }
10898 }
10899
10900 /* dwarf_decode_line_header does not yet provide sufficient information.
10901 We always have to call also dwarf_decode_lines for it. */
10902 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10903 if (lh == NULL)
10904 return;
10905
10906 cu->line_header = lh.release ();
10907 cu->line_header_die_owner = die;
10908
10909 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10910 slot = NULL;
10911 else
10912 {
10913 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10914 &line_header_local,
10915 line_header_local_hash, INSERT);
10916 gdb_assert (slot != NULL);
10917 }
10918 if (slot != NULL && *slot == NULL)
10919 {
10920 /* This newly decoded line number information unit will be owned
10921 by line_header_hash hash table. */
10922 *slot = cu->line_header;
10923 cu->line_header_die_owner = NULL;
10924 }
10925 else
10926 {
10927 /* We cannot free any current entry in (*slot) as that struct line_header
10928 may be already used by multiple CUs. Create only temporary decoded
10929 line_header for this CU - it may happen at most once for each line
10930 number information unit. And if we're not using line_header_hash
10931 then this is what we want as well. */
10932 gdb_assert (die->tag != DW_TAG_partial_unit);
10933 }
10934 decode_mapping = (die->tag != DW_TAG_partial_unit);
10935 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10936 decode_mapping);
10937
10938 }
10939
10940 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10941
10942 static void
10943 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10944 {
10945 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10946 struct objfile *objfile = dwarf2_per_objfile->objfile;
10947 struct gdbarch *gdbarch = objfile->arch ();
10948 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10949 CORE_ADDR highpc = ((CORE_ADDR) 0);
10950 struct attribute *attr;
10951 struct die_info *child_die;
10952 CORE_ADDR baseaddr;
10953
10954 prepare_one_comp_unit (cu, die, cu->language);
10955 baseaddr = objfile->text_section_offset ();
10956
10957 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10958
10959 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10960 from finish_block. */
10961 if (lowpc == ((CORE_ADDR) -1))
10962 lowpc = highpc;
10963 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10964
10965 file_and_directory fnd = find_file_and_directory (die, cu);
10966
10967 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10968 standardised yet. As a workaround for the language detection we fall
10969 back to the DW_AT_producer string. */
10970 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10971 cu->language = language_opencl;
10972
10973 /* Similar hack for Go. */
10974 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10975 set_cu_language (DW_LANG_Go, cu);
10976
10977 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10978
10979 /* Decode line number information if present. We do this before
10980 processing child DIEs, so that the line header table is available
10981 for DW_AT_decl_file. */
10982 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10983
10984 /* Process all dies in compilation unit. */
10985 if (die->child != NULL)
10986 {
10987 child_die = die->child;
10988 while (child_die && child_die->tag)
10989 {
10990 process_die (child_die, cu);
10991 child_die = child_die->sibling;
10992 }
10993 }
10994
10995 /* Decode macro information, if present. Dwarf 2 macro information
10996 refers to information in the line number info statement program
10997 header, so we can only read it if we've read the header
10998 successfully. */
10999 attr = dwarf2_attr (die, DW_AT_macros, cu);
11000 if (attr == NULL)
11001 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11002 if (attr && cu->line_header)
11003 {
11004 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11005 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11006
11007 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11008 }
11009 else
11010 {
11011 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11012 if (attr && cu->line_header)
11013 {
11014 unsigned int macro_offset = DW_UNSND (attr);
11015
11016 dwarf_decode_macros (cu, macro_offset, 0);
11017 }
11018 }
11019 }
11020
11021 void
11022 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11023 {
11024 struct type_unit_group *tu_group;
11025 int first_time;
11026 struct attribute *attr;
11027 unsigned int i;
11028 struct signatured_type *sig_type;
11029
11030 gdb_assert (per_cu->is_debug_types);
11031 sig_type = (struct signatured_type *) per_cu;
11032
11033 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11034
11035 /* If we're using .gdb_index (includes -readnow) then
11036 per_cu->type_unit_group may not have been set up yet. */
11037 if (sig_type->type_unit_group == NULL)
11038 sig_type->type_unit_group = get_type_unit_group (this, attr);
11039 tu_group = sig_type->type_unit_group;
11040
11041 /* If we've already processed this stmt_list there's no real need to
11042 do it again, we could fake it and just recreate the part we need
11043 (file name,index -> symtab mapping). If data shows this optimization
11044 is useful we can do it then. */
11045 first_time = tu_group->compunit_symtab == NULL;
11046
11047 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11048 debug info. */
11049 line_header_up lh;
11050 if (attr != NULL)
11051 {
11052 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11053 lh = dwarf_decode_line_header (line_offset, this);
11054 }
11055 if (lh == NULL)
11056 {
11057 if (first_time)
11058 start_symtab ("", NULL, 0);
11059 else
11060 {
11061 gdb_assert (tu_group->symtabs == NULL);
11062 gdb_assert (m_builder == nullptr);
11063 struct compunit_symtab *cust = tu_group->compunit_symtab;
11064 m_builder.reset (new struct buildsym_compunit
11065 (COMPUNIT_OBJFILE (cust), "",
11066 COMPUNIT_DIRNAME (cust),
11067 compunit_language (cust),
11068 0, cust));
11069 list_in_scope = get_builder ()->get_file_symbols ();
11070 }
11071 return;
11072 }
11073
11074 line_header = lh.release ();
11075 line_header_die_owner = die;
11076
11077 if (first_time)
11078 {
11079 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11080
11081 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11082 still initializing it, and our caller (a few levels up)
11083 process_full_type_unit still needs to know if this is the first
11084 time. */
11085
11086 tu_group->symtabs
11087 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11088 struct symtab *, line_header->file_names_size ());
11089
11090 auto &file_names = line_header->file_names ();
11091 for (i = 0; i < file_names.size (); ++i)
11092 {
11093 file_entry &fe = file_names[i];
11094 dwarf2_start_subfile (this, fe.name,
11095 fe.include_dir (line_header));
11096 buildsym_compunit *b = get_builder ();
11097 if (b->get_current_subfile ()->symtab == NULL)
11098 {
11099 /* NOTE: start_subfile will recognize when it's been
11100 passed a file it has already seen. So we can't
11101 assume there's a simple mapping from
11102 cu->line_header->file_names to subfiles, plus
11103 cu->line_header->file_names may contain dups. */
11104 b->get_current_subfile ()->symtab
11105 = allocate_symtab (cust, b->get_current_subfile ()->name);
11106 }
11107
11108 fe.symtab = b->get_current_subfile ()->symtab;
11109 tu_group->symtabs[i] = fe.symtab;
11110 }
11111 }
11112 else
11113 {
11114 gdb_assert (m_builder == nullptr);
11115 struct compunit_symtab *cust = tu_group->compunit_symtab;
11116 m_builder.reset (new struct buildsym_compunit
11117 (COMPUNIT_OBJFILE (cust), "",
11118 COMPUNIT_DIRNAME (cust),
11119 compunit_language (cust),
11120 0, cust));
11121 list_in_scope = get_builder ()->get_file_symbols ();
11122
11123 auto &file_names = line_header->file_names ();
11124 for (i = 0; i < file_names.size (); ++i)
11125 {
11126 file_entry &fe = file_names[i];
11127 fe.symtab = tu_group->symtabs[i];
11128 }
11129 }
11130
11131 /* The main symtab is allocated last. Type units don't have DW_AT_name
11132 so they don't have a "real" (so to speak) symtab anyway.
11133 There is later code that will assign the main symtab to all symbols
11134 that don't have one. We need to handle the case of a symbol with a
11135 missing symtab (DW_AT_decl_file) anyway. */
11136 }
11137
11138 /* Process DW_TAG_type_unit.
11139 For TUs we want to skip the first top level sibling if it's not the
11140 actual type being defined by this TU. In this case the first top
11141 level sibling is there to provide context only. */
11142
11143 static void
11144 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11145 {
11146 struct die_info *child_die;
11147
11148 prepare_one_comp_unit (cu, die, language_minimal);
11149
11150 /* Initialize (or reinitialize) the machinery for building symtabs.
11151 We do this before processing child DIEs, so that the line header table
11152 is available for DW_AT_decl_file. */
11153 cu->setup_type_unit_groups (die);
11154
11155 if (die->child != NULL)
11156 {
11157 child_die = die->child;
11158 while (child_die && child_die->tag)
11159 {
11160 process_die (child_die, cu);
11161 child_die = child_die->sibling;
11162 }
11163 }
11164 }
11165 \f
11166 /* DWO/DWP files.
11167
11168 http://gcc.gnu.org/wiki/DebugFission
11169 http://gcc.gnu.org/wiki/DebugFissionDWP
11170
11171 To simplify handling of both DWO files ("object" files with the DWARF info)
11172 and DWP files (a file with the DWOs packaged up into one file), we treat
11173 DWP files as having a collection of virtual DWO files. */
11174
11175 static hashval_t
11176 hash_dwo_file (const void *item)
11177 {
11178 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11179 hashval_t hash;
11180
11181 hash = htab_hash_string (dwo_file->dwo_name);
11182 if (dwo_file->comp_dir != NULL)
11183 hash += htab_hash_string (dwo_file->comp_dir);
11184 return hash;
11185 }
11186
11187 static int
11188 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11189 {
11190 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11191 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11192
11193 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11194 return 0;
11195 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11196 return lhs->comp_dir == rhs->comp_dir;
11197 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11198 }
11199
11200 /* Allocate a hash table for DWO files. */
11201
11202 static htab_up
11203 allocate_dwo_file_hash_table ()
11204 {
11205 auto delete_dwo_file = [] (void *item)
11206 {
11207 struct dwo_file *dwo_file = (struct dwo_file *) item;
11208
11209 delete dwo_file;
11210 };
11211
11212 return htab_up (htab_create_alloc (41,
11213 hash_dwo_file,
11214 eq_dwo_file,
11215 delete_dwo_file,
11216 xcalloc, xfree));
11217 }
11218
11219 /* Lookup DWO file DWO_NAME. */
11220
11221 static void **
11222 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11223 const char *dwo_name,
11224 const char *comp_dir)
11225 {
11226 struct dwo_file find_entry;
11227 void **slot;
11228
11229 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11230 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11231
11232 find_entry.dwo_name = dwo_name;
11233 find_entry.comp_dir = comp_dir;
11234 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11235 INSERT);
11236
11237 return slot;
11238 }
11239
11240 static hashval_t
11241 hash_dwo_unit (const void *item)
11242 {
11243 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11244
11245 /* This drops the top 32 bits of the id, but is ok for a hash. */
11246 return dwo_unit->signature;
11247 }
11248
11249 static int
11250 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11251 {
11252 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11253 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11254
11255 /* The signature is assumed to be unique within the DWO file.
11256 So while object file CU dwo_id's always have the value zero,
11257 that's OK, assuming each object file DWO file has only one CU,
11258 and that's the rule for now. */
11259 return lhs->signature == rhs->signature;
11260 }
11261
11262 /* Allocate a hash table for DWO CUs,TUs.
11263 There is one of these tables for each of CUs,TUs for each DWO file. */
11264
11265 static htab_up
11266 allocate_dwo_unit_table ()
11267 {
11268 /* Start out with a pretty small number.
11269 Generally DWO files contain only one CU and maybe some TUs. */
11270 return htab_up (htab_create_alloc (3,
11271 hash_dwo_unit,
11272 eq_dwo_unit,
11273 NULL, xcalloc, xfree));
11274 }
11275
11276 /* die_reader_func for create_dwo_cu. */
11277
11278 static void
11279 create_dwo_cu_reader (const struct die_reader_specs *reader,
11280 const gdb_byte *info_ptr,
11281 struct die_info *comp_unit_die,
11282 struct dwo_file *dwo_file,
11283 struct dwo_unit *dwo_unit)
11284 {
11285 struct dwarf2_cu *cu = reader->cu;
11286 sect_offset sect_off = cu->per_cu->sect_off;
11287 struct dwarf2_section_info *section = cu->per_cu->section;
11288
11289 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11290 if (!signature.has_value ())
11291 {
11292 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11293 " its dwo_id [in module %s]"),
11294 sect_offset_str (sect_off), dwo_file->dwo_name);
11295 return;
11296 }
11297
11298 dwo_unit->dwo_file = dwo_file;
11299 dwo_unit->signature = *signature;
11300 dwo_unit->section = section;
11301 dwo_unit->sect_off = sect_off;
11302 dwo_unit->length = cu->per_cu->length;
11303
11304 if (dwarf_read_debug)
11305 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11306 sect_offset_str (sect_off),
11307 hex_string (dwo_unit->signature));
11308 }
11309
11310 /* Create the dwo_units for the CUs in a DWO_FILE.
11311 Note: This function processes DWO files only, not DWP files. */
11312
11313 static void
11314 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11315 dwarf2_cu *cu, struct dwo_file &dwo_file,
11316 dwarf2_section_info &section, htab_up &cus_htab)
11317 {
11318 struct objfile *objfile = dwarf2_per_objfile->objfile;
11319 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11320 const gdb_byte *info_ptr, *end_ptr;
11321
11322 section.read (objfile);
11323 info_ptr = section.buffer;
11324
11325 if (info_ptr == NULL)
11326 return;
11327
11328 if (dwarf_read_debug)
11329 {
11330 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11331 section.get_name (),
11332 section.get_file_name ());
11333 }
11334
11335 end_ptr = info_ptr + section.size;
11336 while (info_ptr < end_ptr)
11337 {
11338 struct dwarf2_per_cu_data per_cu;
11339 struct dwo_unit read_unit {};
11340 struct dwo_unit *dwo_unit;
11341 void **slot;
11342 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11343
11344 memset (&per_cu, 0, sizeof (per_cu));
11345 per_cu.per_bfd = per_bfd;
11346 per_cu.is_debug_types = 0;
11347 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11348 per_cu.section = &section;
11349
11350 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11351 if (!reader.dummy_p)
11352 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11353 &dwo_file, &read_unit);
11354 info_ptr += per_cu.length;
11355
11356 // If the unit could not be parsed, skip it.
11357 if (read_unit.dwo_file == NULL)
11358 continue;
11359
11360 if (cus_htab == NULL)
11361 cus_htab = allocate_dwo_unit_table ();
11362
11363 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11364 struct dwo_unit);
11365 *dwo_unit = read_unit;
11366 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11367 gdb_assert (slot != NULL);
11368 if (*slot != NULL)
11369 {
11370 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11371 sect_offset dup_sect_off = dup_cu->sect_off;
11372
11373 complaint (_("debug cu entry at offset %s is duplicate to"
11374 " the entry at offset %s, signature %s"),
11375 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11376 hex_string (dwo_unit->signature));
11377 }
11378 *slot = (void *)dwo_unit;
11379 }
11380 }
11381
11382 /* DWP file .debug_{cu,tu}_index section format:
11383 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11384
11385 DWP Version 1:
11386
11387 Both index sections have the same format, and serve to map a 64-bit
11388 signature to a set of section numbers. Each section begins with a header,
11389 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11390 indexes, and a pool of 32-bit section numbers. The index sections will be
11391 aligned at 8-byte boundaries in the file.
11392
11393 The index section header consists of:
11394
11395 V, 32 bit version number
11396 -, 32 bits unused
11397 N, 32 bit number of compilation units or type units in the index
11398 M, 32 bit number of slots in the hash table
11399
11400 Numbers are recorded using the byte order of the application binary.
11401
11402 The hash table begins at offset 16 in the section, and consists of an array
11403 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11404 order of the application binary). Unused slots in the hash table are 0.
11405 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11406
11407 The parallel table begins immediately after the hash table
11408 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11409 array of 32-bit indexes (using the byte order of the application binary),
11410 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11411 table contains a 32-bit index into the pool of section numbers. For unused
11412 hash table slots, the corresponding entry in the parallel table will be 0.
11413
11414 The pool of section numbers begins immediately following the hash table
11415 (at offset 16 + 12 * M from the beginning of the section). The pool of
11416 section numbers consists of an array of 32-bit words (using the byte order
11417 of the application binary). Each item in the array is indexed starting
11418 from 0. The hash table entry provides the index of the first section
11419 number in the set. Additional section numbers in the set follow, and the
11420 set is terminated by a 0 entry (section number 0 is not used in ELF).
11421
11422 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11423 section must be the first entry in the set, and the .debug_abbrev.dwo must
11424 be the second entry. Other members of the set may follow in any order.
11425
11426 ---
11427
11428 DWP Version 2:
11429
11430 DWP Version 2 combines all the .debug_info, etc. sections into one,
11431 and the entries in the index tables are now offsets into these sections.
11432 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11433 section.
11434
11435 Index Section Contents:
11436 Header
11437 Hash Table of Signatures dwp_hash_table.hash_table
11438 Parallel Table of Indices dwp_hash_table.unit_table
11439 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11440 Table of Section Sizes dwp_hash_table.v2.sizes
11441
11442 The index section header consists of:
11443
11444 V, 32 bit version number
11445 L, 32 bit number of columns in the table of section offsets
11446 N, 32 bit number of compilation units or type units in the index
11447 M, 32 bit number of slots in the hash table
11448
11449 Numbers are recorded using the byte order of the application binary.
11450
11451 The hash table has the same format as version 1.
11452 The parallel table of indices has the same format as version 1,
11453 except that the entries are origin-1 indices into the table of sections
11454 offsets and the table of section sizes.
11455
11456 The table of offsets begins immediately following the parallel table
11457 (at offset 16 + 12 * M from the beginning of the section). The table is
11458 a two-dimensional array of 32-bit words (using the byte order of the
11459 application binary), with L columns and N+1 rows, in row-major order.
11460 Each row in the array is indexed starting from 0. The first row provides
11461 a key to the remaining rows: each column in this row provides an identifier
11462 for a debug section, and the offsets in the same column of subsequent rows
11463 refer to that section. The section identifiers are:
11464
11465 DW_SECT_INFO 1 .debug_info.dwo
11466 DW_SECT_TYPES 2 .debug_types.dwo
11467 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11468 DW_SECT_LINE 4 .debug_line.dwo
11469 DW_SECT_LOC 5 .debug_loc.dwo
11470 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11471 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11472 DW_SECT_MACRO 8 .debug_macro.dwo
11473
11474 The offsets provided by the CU and TU index sections are the base offsets
11475 for the contributions made by each CU or TU to the corresponding section
11476 in the package file. Each CU and TU header contains an abbrev_offset
11477 field, used to find the abbreviations table for that CU or TU within the
11478 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11479 be interpreted as relative to the base offset given in the index section.
11480 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11481 should be interpreted as relative to the base offset for .debug_line.dwo,
11482 and offsets into other debug sections obtained from DWARF attributes should
11483 also be interpreted as relative to the corresponding base offset.
11484
11485 The table of sizes begins immediately following the table of offsets.
11486 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11487 with L columns and N rows, in row-major order. Each row in the array is
11488 indexed starting from 1 (row 0 is shared by the two tables).
11489
11490 ---
11491
11492 Hash table lookup is handled the same in version 1 and 2:
11493
11494 We assume that N and M will not exceed 2^32 - 1.
11495 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11496
11497 Given a 64-bit compilation unit signature or a type signature S, an entry
11498 in the hash table is located as follows:
11499
11500 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11501 the low-order k bits all set to 1.
11502
11503 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11504
11505 3) If the hash table entry at index H matches the signature, use that
11506 entry. If the hash table entry at index H is unused (all zeroes),
11507 terminate the search: the signature is not present in the table.
11508
11509 4) Let H = (H + H') modulo M. Repeat at Step 3.
11510
11511 Because M > N and H' and M are relatively prime, the search is guaranteed
11512 to stop at an unused slot or find the match. */
11513
11514 /* Create a hash table to map DWO IDs to their CU/TU entry in
11515 .debug_{info,types}.dwo in DWP_FILE.
11516 Returns NULL if there isn't one.
11517 Note: This function processes DWP files only, not DWO files. */
11518
11519 static struct dwp_hash_table *
11520 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11521 struct dwp_file *dwp_file, int is_debug_types)
11522 {
11523 struct objfile *objfile = dwarf2_per_objfile->objfile;
11524 bfd *dbfd = dwp_file->dbfd.get ();
11525 const gdb_byte *index_ptr, *index_end;
11526 struct dwarf2_section_info *index;
11527 uint32_t version, nr_columns, nr_units, nr_slots;
11528 struct dwp_hash_table *htab;
11529
11530 if (is_debug_types)
11531 index = &dwp_file->sections.tu_index;
11532 else
11533 index = &dwp_file->sections.cu_index;
11534
11535 if (index->empty ())
11536 return NULL;
11537 index->read (objfile);
11538
11539 index_ptr = index->buffer;
11540 index_end = index_ptr + index->size;
11541
11542 version = read_4_bytes (dbfd, index_ptr);
11543 index_ptr += 4;
11544 if (version == 2)
11545 nr_columns = read_4_bytes (dbfd, index_ptr);
11546 else
11547 nr_columns = 0;
11548 index_ptr += 4;
11549 nr_units = read_4_bytes (dbfd, index_ptr);
11550 index_ptr += 4;
11551 nr_slots = read_4_bytes (dbfd, index_ptr);
11552 index_ptr += 4;
11553
11554 if (version != 1 && version != 2)
11555 {
11556 error (_("Dwarf Error: unsupported DWP file version (%s)"
11557 " [in module %s]"),
11558 pulongest (version), dwp_file->name);
11559 }
11560 if (nr_slots != (nr_slots & -nr_slots))
11561 {
11562 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11563 " is not power of 2 [in module %s]"),
11564 pulongest (nr_slots), dwp_file->name);
11565 }
11566
11567 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11568 htab->version = version;
11569 htab->nr_columns = nr_columns;
11570 htab->nr_units = nr_units;
11571 htab->nr_slots = nr_slots;
11572 htab->hash_table = index_ptr;
11573 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11574
11575 /* Exit early if the table is empty. */
11576 if (nr_slots == 0 || nr_units == 0
11577 || (version == 2 && nr_columns == 0))
11578 {
11579 /* All must be zero. */
11580 if (nr_slots != 0 || nr_units != 0
11581 || (version == 2 && nr_columns != 0))
11582 {
11583 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11584 " all zero [in modules %s]"),
11585 dwp_file->name);
11586 }
11587 return htab;
11588 }
11589
11590 if (version == 1)
11591 {
11592 htab->section_pool.v1.indices =
11593 htab->unit_table + sizeof (uint32_t) * nr_slots;
11594 /* It's harder to decide whether the section is too small in v1.
11595 V1 is deprecated anyway so we punt. */
11596 }
11597 else
11598 {
11599 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11600 int *ids = htab->section_pool.v2.section_ids;
11601 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11602 /* Reverse map for error checking. */
11603 int ids_seen[DW_SECT_MAX + 1];
11604 int i;
11605
11606 if (nr_columns < 2)
11607 {
11608 error (_("Dwarf Error: bad DWP hash table, too few columns"
11609 " in section table [in module %s]"),
11610 dwp_file->name);
11611 }
11612 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11613 {
11614 error (_("Dwarf Error: bad DWP hash table, too many columns"
11615 " in section table [in module %s]"),
11616 dwp_file->name);
11617 }
11618 memset (ids, 255, sizeof_ids);
11619 memset (ids_seen, 255, sizeof (ids_seen));
11620 for (i = 0; i < nr_columns; ++i)
11621 {
11622 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11623
11624 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11625 {
11626 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11627 " in section table [in module %s]"),
11628 id, dwp_file->name);
11629 }
11630 if (ids_seen[id] != -1)
11631 {
11632 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11633 " id %d in section table [in module %s]"),
11634 id, dwp_file->name);
11635 }
11636 ids_seen[id] = i;
11637 ids[i] = id;
11638 }
11639 /* Must have exactly one info or types section. */
11640 if (((ids_seen[DW_SECT_INFO] != -1)
11641 + (ids_seen[DW_SECT_TYPES] != -1))
11642 != 1)
11643 {
11644 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11645 " DWO info/types section [in module %s]"),
11646 dwp_file->name);
11647 }
11648 /* Must have an abbrev section. */
11649 if (ids_seen[DW_SECT_ABBREV] == -1)
11650 {
11651 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11652 " section [in module %s]"),
11653 dwp_file->name);
11654 }
11655 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11656 htab->section_pool.v2.sizes =
11657 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11658 * nr_units * nr_columns);
11659 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11660 * nr_units * nr_columns))
11661 > index_end)
11662 {
11663 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11664 " [in module %s]"),
11665 dwp_file->name);
11666 }
11667 }
11668
11669 return htab;
11670 }
11671
11672 /* Update SECTIONS with the data from SECTP.
11673
11674 This function is like the other "locate" section routines that are
11675 passed to bfd_map_over_sections, but in this context the sections to
11676 read comes from the DWP V1 hash table, not the full ELF section table.
11677
11678 The result is non-zero for success, or zero if an error was found. */
11679
11680 static int
11681 locate_v1_virtual_dwo_sections (asection *sectp,
11682 struct virtual_v1_dwo_sections *sections)
11683 {
11684 const struct dwop_section_names *names = &dwop_section_names;
11685
11686 if (section_is_p (sectp->name, &names->abbrev_dwo))
11687 {
11688 /* There can be only one. */
11689 if (sections->abbrev.s.section != NULL)
11690 return 0;
11691 sections->abbrev.s.section = sectp;
11692 sections->abbrev.size = bfd_section_size (sectp);
11693 }
11694 else if (section_is_p (sectp->name, &names->info_dwo)
11695 || section_is_p (sectp->name, &names->types_dwo))
11696 {
11697 /* There can be only one. */
11698 if (sections->info_or_types.s.section != NULL)
11699 return 0;
11700 sections->info_or_types.s.section = sectp;
11701 sections->info_or_types.size = bfd_section_size (sectp);
11702 }
11703 else if (section_is_p (sectp->name, &names->line_dwo))
11704 {
11705 /* There can be only one. */
11706 if (sections->line.s.section != NULL)
11707 return 0;
11708 sections->line.s.section = sectp;
11709 sections->line.size = bfd_section_size (sectp);
11710 }
11711 else if (section_is_p (sectp->name, &names->loc_dwo))
11712 {
11713 /* There can be only one. */
11714 if (sections->loc.s.section != NULL)
11715 return 0;
11716 sections->loc.s.section = sectp;
11717 sections->loc.size = bfd_section_size (sectp);
11718 }
11719 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11720 {
11721 /* There can be only one. */
11722 if (sections->macinfo.s.section != NULL)
11723 return 0;
11724 sections->macinfo.s.section = sectp;
11725 sections->macinfo.size = bfd_section_size (sectp);
11726 }
11727 else if (section_is_p (sectp->name, &names->macro_dwo))
11728 {
11729 /* There can be only one. */
11730 if (sections->macro.s.section != NULL)
11731 return 0;
11732 sections->macro.s.section = sectp;
11733 sections->macro.size = bfd_section_size (sectp);
11734 }
11735 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11736 {
11737 /* There can be only one. */
11738 if (sections->str_offsets.s.section != NULL)
11739 return 0;
11740 sections->str_offsets.s.section = sectp;
11741 sections->str_offsets.size = bfd_section_size (sectp);
11742 }
11743 else
11744 {
11745 /* No other kind of section is valid. */
11746 return 0;
11747 }
11748
11749 return 1;
11750 }
11751
11752 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11753 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11754 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11755 This is for DWP version 1 files. */
11756
11757 static struct dwo_unit *
11758 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11759 struct dwp_file *dwp_file,
11760 uint32_t unit_index,
11761 const char *comp_dir,
11762 ULONGEST signature, int is_debug_types)
11763 {
11764 const struct dwp_hash_table *dwp_htab =
11765 is_debug_types ? dwp_file->tus : dwp_file->cus;
11766 bfd *dbfd = dwp_file->dbfd.get ();
11767 const char *kind = is_debug_types ? "TU" : "CU";
11768 struct dwo_file *dwo_file;
11769 struct dwo_unit *dwo_unit;
11770 struct virtual_v1_dwo_sections sections;
11771 void **dwo_file_slot;
11772 int i;
11773
11774 gdb_assert (dwp_file->version == 1);
11775
11776 if (dwarf_read_debug)
11777 {
11778 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11779 kind,
11780 pulongest (unit_index), hex_string (signature),
11781 dwp_file->name);
11782 }
11783
11784 /* Fetch the sections of this DWO unit.
11785 Put a limit on the number of sections we look for so that bad data
11786 doesn't cause us to loop forever. */
11787
11788 #define MAX_NR_V1_DWO_SECTIONS \
11789 (1 /* .debug_info or .debug_types */ \
11790 + 1 /* .debug_abbrev */ \
11791 + 1 /* .debug_line */ \
11792 + 1 /* .debug_loc */ \
11793 + 1 /* .debug_str_offsets */ \
11794 + 1 /* .debug_macro or .debug_macinfo */ \
11795 + 1 /* trailing zero */)
11796
11797 memset (&sections, 0, sizeof (sections));
11798
11799 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11800 {
11801 asection *sectp;
11802 uint32_t section_nr =
11803 read_4_bytes (dbfd,
11804 dwp_htab->section_pool.v1.indices
11805 + (unit_index + i) * sizeof (uint32_t));
11806
11807 if (section_nr == 0)
11808 break;
11809 if (section_nr >= dwp_file->num_sections)
11810 {
11811 error (_("Dwarf Error: bad DWP hash table, section number too large"
11812 " [in module %s]"),
11813 dwp_file->name);
11814 }
11815
11816 sectp = dwp_file->elf_sections[section_nr];
11817 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11818 {
11819 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11820 " [in module %s]"),
11821 dwp_file->name);
11822 }
11823 }
11824
11825 if (i < 2
11826 || sections.info_or_types.empty ()
11827 || sections.abbrev.empty ())
11828 {
11829 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11830 " [in module %s]"),
11831 dwp_file->name);
11832 }
11833 if (i == MAX_NR_V1_DWO_SECTIONS)
11834 {
11835 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11836 " [in module %s]"),
11837 dwp_file->name);
11838 }
11839
11840 /* It's easier for the rest of the code if we fake a struct dwo_file and
11841 have dwo_unit "live" in that. At least for now.
11842
11843 The DWP file can be made up of a random collection of CUs and TUs.
11844 However, for each CU + set of TUs that came from the same original DWO
11845 file, we can combine them back into a virtual DWO file to save space
11846 (fewer struct dwo_file objects to allocate). Remember that for really
11847 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11848
11849 std::string virtual_dwo_name =
11850 string_printf ("virtual-dwo/%d-%d-%d-%d",
11851 sections.abbrev.get_id (),
11852 sections.line.get_id (),
11853 sections.loc.get_id (),
11854 sections.str_offsets.get_id ());
11855 /* Can we use an existing virtual DWO file? */
11856 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11857 virtual_dwo_name.c_str (),
11858 comp_dir);
11859 /* Create one if necessary. */
11860 if (*dwo_file_slot == NULL)
11861 {
11862 if (dwarf_read_debug)
11863 {
11864 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11865 virtual_dwo_name.c_str ());
11866 }
11867 dwo_file = new struct dwo_file;
11868 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11869 dwo_file->comp_dir = comp_dir;
11870 dwo_file->sections.abbrev = sections.abbrev;
11871 dwo_file->sections.line = sections.line;
11872 dwo_file->sections.loc = sections.loc;
11873 dwo_file->sections.macinfo = sections.macinfo;
11874 dwo_file->sections.macro = sections.macro;
11875 dwo_file->sections.str_offsets = sections.str_offsets;
11876 /* The "str" section is global to the entire DWP file. */
11877 dwo_file->sections.str = dwp_file->sections.str;
11878 /* The info or types section is assigned below to dwo_unit,
11879 there's no need to record it in dwo_file.
11880 Also, we can't simply record type sections in dwo_file because
11881 we record a pointer into the vector in dwo_unit. As we collect more
11882 types we'll grow the vector and eventually have to reallocate space
11883 for it, invalidating all copies of pointers into the previous
11884 contents. */
11885 *dwo_file_slot = dwo_file;
11886 }
11887 else
11888 {
11889 if (dwarf_read_debug)
11890 {
11891 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11892 virtual_dwo_name.c_str ());
11893 }
11894 dwo_file = (struct dwo_file *) *dwo_file_slot;
11895 }
11896
11897 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11898 dwo_unit->dwo_file = dwo_file;
11899 dwo_unit->signature = signature;
11900 dwo_unit->section =
11901 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11902 *dwo_unit->section = sections.info_or_types;
11903 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11904
11905 return dwo_unit;
11906 }
11907
11908 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11909 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11910 piece within that section used by a TU/CU, return a virtual section
11911 of just that piece. */
11912
11913 static struct dwarf2_section_info
11914 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11915 struct dwarf2_section_info *section,
11916 bfd_size_type offset, bfd_size_type size)
11917 {
11918 struct dwarf2_section_info result;
11919 asection *sectp;
11920
11921 gdb_assert (section != NULL);
11922 gdb_assert (!section->is_virtual);
11923
11924 memset (&result, 0, sizeof (result));
11925 result.s.containing_section = section;
11926 result.is_virtual = true;
11927
11928 if (size == 0)
11929 return result;
11930
11931 sectp = section->get_bfd_section ();
11932
11933 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11934 bounds of the real section. This is a pretty-rare event, so just
11935 flag an error (easier) instead of a warning and trying to cope. */
11936 if (sectp == NULL
11937 || offset + size > bfd_section_size (sectp))
11938 {
11939 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11940 " in section %s [in module %s]"),
11941 sectp ? bfd_section_name (sectp) : "<unknown>",
11942 objfile_name (dwarf2_per_objfile->objfile));
11943 }
11944
11945 result.virtual_offset = offset;
11946 result.size = size;
11947 return result;
11948 }
11949
11950 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11951 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11952 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11953 This is for DWP version 2 files. */
11954
11955 static struct dwo_unit *
11956 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11957 struct dwp_file *dwp_file,
11958 uint32_t unit_index,
11959 const char *comp_dir,
11960 ULONGEST signature, int is_debug_types)
11961 {
11962 const struct dwp_hash_table *dwp_htab =
11963 is_debug_types ? dwp_file->tus : dwp_file->cus;
11964 bfd *dbfd = dwp_file->dbfd.get ();
11965 const char *kind = is_debug_types ? "TU" : "CU";
11966 struct dwo_file *dwo_file;
11967 struct dwo_unit *dwo_unit;
11968 struct virtual_v2_dwo_sections sections;
11969 void **dwo_file_slot;
11970 int i;
11971
11972 gdb_assert (dwp_file->version == 2);
11973
11974 if (dwarf_read_debug)
11975 {
11976 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11977 kind,
11978 pulongest (unit_index), hex_string (signature),
11979 dwp_file->name);
11980 }
11981
11982 /* Fetch the section offsets of this DWO unit. */
11983
11984 memset (&sections, 0, sizeof (sections));
11985
11986 for (i = 0; i < dwp_htab->nr_columns; ++i)
11987 {
11988 uint32_t offset = read_4_bytes (dbfd,
11989 dwp_htab->section_pool.v2.offsets
11990 + (((unit_index - 1) * dwp_htab->nr_columns
11991 + i)
11992 * sizeof (uint32_t)));
11993 uint32_t size = read_4_bytes (dbfd,
11994 dwp_htab->section_pool.v2.sizes
11995 + (((unit_index - 1) * dwp_htab->nr_columns
11996 + i)
11997 * sizeof (uint32_t)));
11998
11999 switch (dwp_htab->section_pool.v2.section_ids[i])
12000 {
12001 case DW_SECT_INFO:
12002 case DW_SECT_TYPES:
12003 sections.info_or_types_offset = offset;
12004 sections.info_or_types_size = size;
12005 break;
12006 case DW_SECT_ABBREV:
12007 sections.abbrev_offset = offset;
12008 sections.abbrev_size = size;
12009 break;
12010 case DW_SECT_LINE:
12011 sections.line_offset = offset;
12012 sections.line_size = size;
12013 break;
12014 case DW_SECT_LOC:
12015 sections.loc_offset = offset;
12016 sections.loc_size = size;
12017 break;
12018 case DW_SECT_STR_OFFSETS:
12019 sections.str_offsets_offset = offset;
12020 sections.str_offsets_size = size;
12021 break;
12022 case DW_SECT_MACINFO:
12023 sections.macinfo_offset = offset;
12024 sections.macinfo_size = size;
12025 break;
12026 case DW_SECT_MACRO:
12027 sections.macro_offset = offset;
12028 sections.macro_size = size;
12029 break;
12030 }
12031 }
12032
12033 /* It's easier for the rest of the code if we fake a struct dwo_file and
12034 have dwo_unit "live" in that. At least for now.
12035
12036 The DWP file can be made up of a random collection of CUs and TUs.
12037 However, for each CU + set of TUs that came from the same original DWO
12038 file, we can combine them back into a virtual DWO file to save space
12039 (fewer struct dwo_file objects to allocate). Remember that for really
12040 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12041
12042 std::string virtual_dwo_name =
12043 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12044 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12045 (long) (sections.line_size ? sections.line_offset : 0),
12046 (long) (sections.loc_size ? sections.loc_offset : 0),
12047 (long) (sections.str_offsets_size
12048 ? sections.str_offsets_offset : 0));
12049 /* Can we use an existing virtual DWO file? */
12050 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12051 virtual_dwo_name.c_str (),
12052 comp_dir);
12053 /* Create one if necessary. */
12054 if (*dwo_file_slot == NULL)
12055 {
12056 if (dwarf_read_debug)
12057 {
12058 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12059 virtual_dwo_name.c_str ());
12060 }
12061 dwo_file = new struct dwo_file;
12062 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12063 dwo_file->comp_dir = comp_dir;
12064 dwo_file->sections.abbrev =
12065 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12066 sections.abbrev_offset, sections.abbrev_size);
12067 dwo_file->sections.line =
12068 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12069 sections.line_offset, sections.line_size);
12070 dwo_file->sections.loc =
12071 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12072 sections.loc_offset, sections.loc_size);
12073 dwo_file->sections.macinfo =
12074 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12075 sections.macinfo_offset, sections.macinfo_size);
12076 dwo_file->sections.macro =
12077 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12078 sections.macro_offset, sections.macro_size);
12079 dwo_file->sections.str_offsets =
12080 create_dwp_v2_section (dwarf2_per_objfile,
12081 &dwp_file->sections.str_offsets,
12082 sections.str_offsets_offset,
12083 sections.str_offsets_size);
12084 /* The "str" section is global to the entire DWP file. */
12085 dwo_file->sections.str = dwp_file->sections.str;
12086 /* The info or types section is assigned below to dwo_unit,
12087 there's no need to record it in dwo_file.
12088 Also, we can't simply record type sections in dwo_file because
12089 we record a pointer into the vector in dwo_unit. As we collect more
12090 types we'll grow the vector and eventually have to reallocate space
12091 for it, invalidating all copies of pointers into the previous
12092 contents. */
12093 *dwo_file_slot = dwo_file;
12094 }
12095 else
12096 {
12097 if (dwarf_read_debug)
12098 {
12099 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12100 virtual_dwo_name.c_str ());
12101 }
12102 dwo_file = (struct dwo_file *) *dwo_file_slot;
12103 }
12104
12105 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12106 dwo_unit->dwo_file = dwo_file;
12107 dwo_unit->signature = signature;
12108 dwo_unit->section =
12109 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12110 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12111 is_debug_types
12112 ? &dwp_file->sections.types
12113 : &dwp_file->sections.info,
12114 sections.info_or_types_offset,
12115 sections.info_or_types_size);
12116 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12117
12118 return dwo_unit;
12119 }
12120
12121 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12122 Returns NULL if the signature isn't found. */
12123
12124 static struct dwo_unit *
12125 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12126 struct dwp_file *dwp_file, const char *comp_dir,
12127 ULONGEST signature, int is_debug_types)
12128 {
12129 const struct dwp_hash_table *dwp_htab =
12130 is_debug_types ? dwp_file->tus : dwp_file->cus;
12131 bfd *dbfd = dwp_file->dbfd.get ();
12132 uint32_t mask = dwp_htab->nr_slots - 1;
12133 uint32_t hash = signature & mask;
12134 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12135 unsigned int i;
12136 void **slot;
12137 struct dwo_unit find_dwo_cu;
12138
12139 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12140 find_dwo_cu.signature = signature;
12141 slot = htab_find_slot (is_debug_types
12142 ? dwp_file->loaded_tus.get ()
12143 : dwp_file->loaded_cus.get (),
12144 &find_dwo_cu, INSERT);
12145
12146 if (*slot != NULL)
12147 return (struct dwo_unit *) *slot;
12148
12149 /* Use a for loop so that we don't loop forever on bad debug info. */
12150 for (i = 0; i < dwp_htab->nr_slots; ++i)
12151 {
12152 ULONGEST signature_in_table;
12153
12154 signature_in_table =
12155 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12156 if (signature_in_table == signature)
12157 {
12158 uint32_t unit_index =
12159 read_4_bytes (dbfd,
12160 dwp_htab->unit_table + hash * sizeof (uint32_t));
12161
12162 if (dwp_file->version == 1)
12163 {
12164 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12165 dwp_file, unit_index,
12166 comp_dir, signature,
12167 is_debug_types);
12168 }
12169 else
12170 {
12171 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12172 dwp_file, unit_index,
12173 comp_dir, signature,
12174 is_debug_types);
12175 }
12176 return (struct dwo_unit *) *slot;
12177 }
12178 if (signature_in_table == 0)
12179 return NULL;
12180 hash = (hash + hash2) & mask;
12181 }
12182
12183 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12184 " [in module %s]"),
12185 dwp_file->name);
12186 }
12187
12188 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12189 Open the file specified by FILE_NAME and hand it off to BFD for
12190 preliminary analysis. Return a newly initialized bfd *, which
12191 includes a canonicalized copy of FILE_NAME.
12192 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12193 SEARCH_CWD is true if the current directory is to be searched.
12194 It will be searched before debug-file-directory.
12195 If successful, the file is added to the bfd include table of the
12196 objfile's bfd (see gdb_bfd_record_inclusion).
12197 If unable to find/open the file, return NULL.
12198 NOTE: This function is derived from symfile_bfd_open. */
12199
12200 static gdb_bfd_ref_ptr
12201 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12202 const char *file_name, int is_dwp, int search_cwd)
12203 {
12204 int desc;
12205 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12206 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12207 to debug_file_directory. */
12208 const char *search_path;
12209 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12210
12211 gdb::unique_xmalloc_ptr<char> search_path_holder;
12212 if (search_cwd)
12213 {
12214 if (*debug_file_directory != '\0')
12215 {
12216 search_path_holder.reset (concat (".", dirname_separator_string,
12217 debug_file_directory,
12218 (char *) NULL));
12219 search_path = search_path_holder.get ();
12220 }
12221 else
12222 search_path = ".";
12223 }
12224 else
12225 search_path = debug_file_directory;
12226
12227 openp_flags flags = OPF_RETURN_REALPATH;
12228 if (is_dwp)
12229 flags |= OPF_SEARCH_IN_PATH;
12230
12231 gdb::unique_xmalloc_ptr<char> absolute_name;
12232 desc = openp (search_path, flags, file_name,
12233 O_RDONLY | O_BINARY, &absolute_name);
12234 if (desc < 0)
12235 return NULL;
12236
12237 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12238 gnutarget, desc));
12239 if (sym_bfd == NULL)
12240 return NULL;
12241 bfd_set_cacheable (sym_bfd.get (), 1);
12242
12243 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12244 return NULL;
12245
12246 /* Success. Record the bfd as having been included by the objfile's bfd.
12247 This is important because things like demangled_names_hash lives in the
12248 objfile's per_bfd space and may have references to things like symbol
12249 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12250 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12251
12252 return sym_bfd;
12253 }
12254
12255 /* Try to open DWO file FILE_NAME.
12256 COMP_DIR is the DW_AT_comp_dir attribute.
12257 The result is the bfd handle of the file.
12258 If there is a problem finding or opening the file, return NULL.
12259 Upon success, the canonicalized path of the file is stored in the bfd,
12260 same as symfile_bfd_open. */
12261
12262 static gdb_bfd_ref_ptr
12263 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12264 const char *file_name, const char *comp_dir)
12265 {
12266 if (IS_ABSOLUTE_PATH (file_name))
12267 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12268 0 /*is_dwp*/, 0 /*search_cwd*/);
12269
12270 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12271
12272 if (comp_dir != NULL)
12273 {
12274 gdb::unique_xmalloc_ptr<char> path_to_try
12275 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12276
12277 /* NOTE: If comp_dir is a relative path, this will also try the
12278 search path, which seems useful. */
12279 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12280 path_to_try.get (),
12281 0 /*is_dwp*/,
12282 1 /*search_cwd*/));
12283 if (abfd != NULL)
12284 return abfd;
12285 }
12286
12287 /* That didn't work, try debug-file-directory, which, despite its name,
12288 is a list of paths. */
12289
12290 if (*debug_file_directory == '\0')
12291 return NULL;
12292
12293 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12294 0 /*is_dwp*/, 1 /*search_cwd*/);
12295 }
12296
12297 /* This function is mapped across the sections and remembers the offset and
12298 size of each of the DWO debugging sections we are interested in. */
12299
12300 static void
12301 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12302 {
12303 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12304 const struct dwop_section_names *names = &dwop_section_names;
12305
12306 if (section_is_p (sectp->name, &names->abbrev_dwo))
12307 {
12308 dwo_sections->abbrev.s.section = sectp;
12309 dwo_sections->abbrev.size = bfd_section_size (sectp);
12310 }
12311 else if (section_is_p (sectp->name, &names->info_dwo))
12312 {
12313 dwo_sections->info.s.section = sectp;
12314 dwo_sections->info.size = bfd_section_size (sectp);
12315 }
12316 else if (section_is_p (sectp->name, &names->line_dwo))
12317 {
12318 dwo_sections->line.s.section = sectp;
12319 dwo_sections->line.size = bfd_section_size (sectp);
12320 }
12321 else if (section_is_p (sectp->name, &names->loc_dwo))
12322 {
12323 dwo_sections->loc.s.section = sectp;
12324 dwo_sections->loc.size = bfd_section_size (sectp);
12325 }
12326 else if (section_is_p (sectp->name, &names->loclists_dwo))
12327 {
12328 dwo_sections->loclists.s.section = sectp;
12329 dwo_sections->loclists.size = bfd_section_size (sectp);
12330 }
12331 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12332 {
12333 dwo_sections->macinfo.s.section = sectp;
12334 dwo_sections->macinfo.size = bfd_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->macro_dwo))
12337 {
12338 dwo_sections->macro.s.section = sectp;
12339 dwo_sections->macro.size = bfd_section_size (sectp);
12340 }
12341 else if (section_is_p (sectp->name, &names->str_dwo))
12342 {
12343 dwo_sections->str.s.section = sectp;
12344 dwo_sections->str.size = bfd_section_size (sectp);
12345 }
12346 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12347 {
12348 dwo_sections->str_offsets.s.section = sectp;
12349 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12350 }
12351 else if (section_is_p (sectp->name, &names->types_dwo))
12352 {
12353 struct dwarf2_section_info type_section;
12354
12355 memset (&type_section, 0, sizeof (type_section));
12356 type_section.s.section = sectp;
12357 type_section.size = bfd_section_size (sectp);
12358 dwo_sections->types.push_back (type_section);
12359 }
12360 }
12361
12362 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12363 by PER_CU. This is for the non-DWP case.
12364 The result is NULL if DWO_NAME can't be found. */
12365
12366 static struct dwo_file *
12367 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12368 const char *comp_dir)
12369 {
12370 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12371
12372 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12373 if (dbfd == NULL)
12374 {
12375 if (dwarf_read_debug)
12376 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12377 return NULL;
12378 }
12379
12380 dwo_file_up dwo_file (new struct dwo_file);
12381 dwo_file->dwo_name = dwo_name;
12382 dwo_file->comp_dir = comp_dir;
12383 dwo_file->dbfd = std::move (dbfd);
12384
12385 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12386 &dwo_file->sections);
12387
12388 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12389 dwo_file->sections.info, dwo_file->cus);
12390
12391 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12392 dwo_file->sections.types, dwo_file->tus);
12393
12394 if (dwarf_read_debug)
12395 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12396
12397 return dwo_file.release ();
12398 }
12399
12400 /* This function is mapped across the sections and remembers the offset and
12401 size of each of the DWP debugging sections common to version 1 and 2 that
12402 we are interested in. */
12403
12404 static void
12405 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12406 void *dwp_file_ptr)
12407 {
12408 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12409 const struct dwop_section_names *names = &dwop_section_names;
12410 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12411
12412 /* Record the ELF section number for later lookup: this is what the
12413 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12414 gdb_assert (elf_section_nr < dwp_file->num_sections);
12415 dwp_file->elf_sections[elf_section_nr] = sectp;
12416
12417 /* Look for specific sections that we need. */
12418 if (section_is_p (sectp->name, &names->str_dwo))
12419 {
12420 dwp_file->sections.str.s.section = sectp;
12421 dwp_file->sections.str.size = bfd_section_size (sectp);
12422 }
12423 else if (section_is_p (sectp->name, &names->cu_index))
12424 {
12425 dwp_file->sections.cu_index.s.section = sectp;
12426 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12427 }
12428 else if (section_is_p (sectp->name, &names->tu_index))
12429 {
12430 dwp_file->sections.tu_index.s.section = sectp;
12431 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12432 }
12433 }
12434
12435 /* This function is mapped across the sections and remembers the offset and
12436 size of each of the DWP version 2 debugging sections that we are interested
12437 in. This is split into a separate function because we don't know if we
12438 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12439
12440 static void
12441 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12442 {
12443 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12444 const struct dwop_section_names *names = &dwop_section_names;
12445 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12446
12447 /* Record the ELF section number for later lookup: this is what the
12448 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12449 gdb_assert (elf_section_nr < dwp_file->num_sections);
12450 dwp_file->elf_sections[elf_section_nr] = sectp;
12451
12452 /* Look for specific sections that we need. */
12453 if (section_is_p (sectp->name, &names->abbrev_dwo))
12454 {
12455 dwp_file->sections.abbrev.s.section = sectp;
12456 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12457 }
12458 else if (section_is_p (sectp->name, &names->info_dwo))
12459 {
12460 dwp_file->sections.info.s.section = sectp;
12461 dwp_file->sections.info.size = bfd_section_size (sectp);
12462 }
12463 else if (section_is_p (sectp->name, &names->line_dwo))
12464 {
12465 dwp_file->sections.line.s.section = sectp;
12466 dwp_file->sections.line.size = bfd_section_size (sectp);
12467 }
12468 else if (section_is_p (sectp->name, &names->loc_dwo))
12469 {
12470 dwp_file->sections.loc.s.section = sectp;
12471 dwp_file->sections.loc.size = bfd_section_size (sectp);
12472 }
12473 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12474 {
12475 dwp_file->sections.macinfo.s.section = sectp;
12476 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12477 }
12478 else if (section_is_p (sectp->name, &names->macro_dwo))
12479 {
12480 dwp_file->sections.macro.s.section = sectp;
12481 dwp_file->sections.macro.size = bfd_section_size (sectp);
12482 }
12483 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12484 {
12485 dwp_file->sections.str_offsets.s.section = sectp;
12486 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12487 }
12488 else if (section_is_p (sectp->name, &names->types_dwo))
12489 {
12490 dwp_file->sections.types.s.section = sectp;
12491 dwp_file->sections.types.size = bfd_section_size (sectp);
12492 }
12493 }
12494
12495 /* Hash function for dwp_file loaded CUs/TUs. */
12496
12497 static hashval_t
12498 hash_dwp_loaded_cutus (const void *item)
12499 {
12500 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12501
12502 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12503 return dwo_unit->signature;
12504 }
12505
12506 /* Equality function for dwp_file loaded CUs/TUs. */
12507
12508 static int
12509 eq_dwp_loaded_cutus (const void *a, const void *b)
12510 {
12511 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12512 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12513
12514 return dua->signature == dub->signature;
12515 }
12516
12517 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12518
12519 static htab_up
12520 allocate_dwp_loaded_cutus_table ()
12521 {
12522 return htab_up (htab_create_alloc (3,
12523 hash_dwp_loaded_cutus,
12524 eq_dwp_loaded_cutus,
12525 NULL, xcalloc, xfree));
12526 }
12527
12528 /* Try to open DWP file FILE_NAME.
12529 The result is the bfd handle of the file.
12530 If there is a problem finding or opening the file, return NULL.
12531 Upon success, the canonicalized path of the file is stored in the bfd,
12532 same as symfile_bfd_open. */
12533
12534 static gdb_bfd_ref_ptr
12535 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12536 const char *file_name)
12537 {
12538 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12539 1 /*is_dwp*/,
12540 1 /*search_cwd*/));
12541 if (abfd != NULL)
12542 return abfd;
12543
12544 /* Work around upstream bug 15652.
12545 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12546 [Whether that's a "bug" is debatable, but it is getting in our way.]
12547 We have no real idea where the dwp file is, because gdb's realpath-ing
12548 of the executable's path may have discarded the needed info.
12549 [IWBN if the dwp file name was recorded in the executable, akin to
12550 .gnu_debuglink, but that doesn't exist yet.]
12551 Strip the directory from FILE_NAME and search again. */
12552 if (*debug_file_directory != '\0')
12553 {
12554 /* Don't implicitly search the current directory here.
12555 If the user wants to search "." to handle this case,
12556 it must be added to debug-file-directory. */
12557 return try_open_dwop_file (dwarf2_per_objfile,
12558 lbasename (file_name), 1 /*is_dwp*/,
12559 0 /*search_cwd*/);
12560 }
12561
12562 return NULL;
12563 }
12564
12565 /* Initialize the use of the DWP file for the current objfile.
12566 By convention the name of the DWP file is ${objfile}.dwp.
12567 The result is NULL if it can't be found. */
12568
12569 static std::unique_ptr<struct dwp_file>
12570 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12571 {
12572 struct objfile *objfile = dwarf2_per_objfile->objfile;
12573
12574 /* Try to find first .dwp for the binary file before any symbolic links
12575 resolving. */
12576
12577 /* If the objfile is a debug file, find the name of the real binary
12578 file and get the name of dwp file from there. */
12579 std::string dwp_name;
12580 if (objfile->separate_debug_objfile_backlink != NULL)
12581 {
12582 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12583 const char *backlink_basename = lbasename (backlink->original_name);
12584
12585 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12586 }
12587 else
12588 dwp_name = objfile->original_name;
12589
12590 dwp_name += ".dwp";
12591
12592 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12593 if (dbfd == NULL
12594 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12595 {
12596 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12597 dwp_name = objfile_name (objfile);
12598 dwp_name += ".dwp";
12599 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12600 }
12601
12602 if (dbfd == NULL)
12603 {
12604 if (dwarf_read_debug)
12605 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12606 return std::unique_ptr<dwp_file> ();
12607 }
12608
12609 const char *name = bfd_get_filename (dbfd.get ());
12610 std::unique_ptr<struct dwp_file> dwp_file
12611 (new struct dwp_file (name, std::move (dbfd)));
12612
12613 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12614 dwp_file->elf_sections =
12615 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12616 dwp_file->num_sections, asection *);
12617
12618 bfd_map_over_sections (dwp_file->dbfd.get (),
12619 dwarf2_locate_common_dwp_sections,
12620 dwp_file.get ());
12621
12622 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12623 0);
12624
12625 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12626 1);
12627
12628 /* The DWP file version is stored in the hash table. Oh well. */
12629 if (dwp_file->cus && dwp_file->tus
12630 && dwp_file->cus->version != dwp_file->tus->version)
12631 {
12632 /* Technically speaking, we should try to limp along, but this is
12633 pretty bizarre. We use pulongest here because that's the established
12634 portability solution (e.g, we cannot use %u for uint32_t). */
12635 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12636 " TU version %s [in DWP file %s]"),
12637 pulongest (dwp_file->cus->version),
12638 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12639 }
12640
12641 if (dwp_file->cus)
12642 dwp_file->version = dwp_file->cus->version;
12643 else if (dwp_file->tus)
12644 dwp_file->version = dwp_file->tus->version;
12645 else
12646 dwp_file->version = 2;
12647
12648 if (dwp_file->version == 2)
12649 bfd_map_over_sections (dwp_file->dbfd.get (),
12650 dwarf2_locate_v2_dwp_sections,
12651 dwp_file.get ());
12652
12653 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12654 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12655
12656 if (dwarf_read_debug)
12657 {
12658 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12659 fprintf_unfiltered (gdb_stdlog,
12660 " %s CUs, %s TUs\n",
12661 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12662 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12663 }
12664
12665 return dwp_file;
12666 }
12667
12668 /* Wrapper around open_and_init_dwp_file, only open it once. */
12669
12670 static struct dwp_file *
12671 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12672 {
12673 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12674 {
12675 dwarf2_per_objfile->per_bfd->dwp_file
12676 = open_and_init_dwp_file (dwarf2_per_objfile);
12677 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12678 }
12679 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12680 }
12681
12682 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12683 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12684 or in the DWP file for the objfile, referenced by THIS_UNIT.
12685 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12686 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12687
12688 This is called, for example, when wanting to read a variable with a
12689 complex location. Therefore we don't want to do file i/o for every call.
12690 Therefore we don't want to look for a DWO file on every call.
12691 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12692 then we check if we've already seen DWO_NAME, and only THEN do we check
12693 for a DWO file.
12694
12695 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12696 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12697
12698 static struct dwo_unit *
12699 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12700 ULONGEST signature, int is_debug_types)
12701 {
12702 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12703 struct objfile *objfile = dwarf2_per_objfile->objfile;
12704 const char *kind = is_debug_types ? "TU" : "CU";
12705 void **dwo_file_slot;
12706 struct dwo_file *dwo_file;
12707 struct dwp_file *dwp_file;
12708
12709 /* First see if there's a DWP file.
12710 If we have a DWP file but didn't find the DWO inside it, don't
12711 look for the original DWO file. It makes gdb behave differently
12712 depending on whether one is debugging in the build tree. */
12713
12714 dwp_file = get_dwp_file (dwarf2_per_objfile);
12715 if (dwp_file != NULL)
12716 {
12717 const struct dwp_hash_table *dwp_htab =
12718 is_debug_types ? dwp_file->tus : dwp_file->cus;
12719
12720 if (dwp_htab != NULL)
12721 {
12722 struct dwo_unit *dwo_cutu =
12723 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12724 signature, is_debug_types);
12725
12726 if (dwo_cutu != NULL)
12727 {
12728 if (dwarf_read_debug)
12729 {
12730 fprintf_unfiltered (gdb_stdlog,
12731 "Virtual DWO %s %s found: @%s\n",
12732 kind, hex_string (signature),
12733 host_address_to_string (dwo_cutu));
12734 }
12735 return dwo_cutu;
12736 }
12737 }
12738 }
12739 else
12740 {
12741 /* No DWP file, look for the DWO file. */
12742
12743 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12744 dwo_name, comp_dir);
12745 if (*dwo_file_slot == NULL)
12746 {
12747 /* Read in the file and build a table of the CUs/TUs it contains. */
12748 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12749 }
12750 /* NOTE: This will be NULL if unable to open the file. */
12751 dwo_file = (struct dwo_file *) *dwo_file_slot;
12752
12753 if (dwo_file != NULL)
12754 {
12755 struct dwo_unit *dwo_cutu = NULL;
12756
12757 if (is_debug_types && dwo_file->tus)
12758 {
12759 struct dwo_unit find_dwo_cutu;
12760
12761 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12762 find_dwo_cutu.signature = signature;
12763 dwo_cutu
12764 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12765 &find_dwo_cutu);
12766 }
12767 else if (!is_debug_types && dwo_file->cus)
12768 {
12769 struct dwo_unit find_dwo_cutu;
12770
12771 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12772 find_dwo_cutu.signature = signature;
12773 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12774 &find_dwo_cutu);
12775 }
12776
12777 if (dwo_cutu != NULL)
12778 {
12779 if (dwarf_read_debug)
12780 {
12781 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12782 kind, dwo_name, hex_string (signature),
12783 host_address_to_string (dwo_cutu));
12784 }
12785 return dwo_cutu;
12786 }
12787 }
12788 }
12789
12790 /* We didn't find it. This could mean a dwo_id mismatch, or
12791 someone deleted the DWO/DWP file, or the search path isn't set up
12792 correctly to find the file. */
12793
12794 if (dwarf_read_debug)
12795 {
12796 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12797 kind, dwo_name, hex_string (signature));
12798 }
12799
12800 /* This is a warning and not a complaint because it can be caused by
12801 pilot error (e.g., user accidentally deleting the DWO). */
12802 {
12803 /* Print the name of the DWP file if we looked there, helps the user
12804 better diagnose the problem. */
12805 std::string dwp_text;
12806
12807 if (dwp_file != NULL)
12808 dwp_text = string_printf (" [in DWP file %s]",
12809 lbasename (dwp_file->name));
12810
12811 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12812 " [in module %s]"),
12813 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12814 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12815 }
12816 return NULL;
12817 }
12818
12819 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12820 See lookup_dwo_cutu_unit for details. */
12821
12822 static struct dwo_unit *
12823 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12824 ULONGEST signature)
12825 {
12826 gdb_assert (!cu->per_cu->is_debug_types);
12827
12828 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12829 }
12830
12831 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12832 See lookup_dwo_cutu_unit for details. */
12833
12834 static struct dwo_unit *
12835 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12836 {
12837 gdb_assert (cu->per_cu->is_debug_types);
12838
12839 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12840
12841 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12842 }
12843
12844 /* Traversal function for queue_and_load_all_dwo_tus. */
12845
12846 static int
12847 queue_and_load_dwo_tu (void **slot, void *info)
12848 {
12849 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12850 dwarf2_cu *cu = (dwarf2_cu *) info;
12851 ULONGEST signature = dwo_unit->signature;
12852 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12853
12854 if (sig_type != NULL)
12855 {
12856 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12857
12858 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12859 a real dependency of PER_CU on SIG_TYPE. That is detected later
12860 while processing PER_CU. */
12861 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12862 load_full_type_unit (sig_cu, cu->per_objfile);
12863 cu->per_cu->imported_symtabs_push (sig_cu);
12864 }
12865
12866 return 1;
12867 }
12868
12869 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12870 The DWO may have the only definition of the type, though it may not be
12871 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12872 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12873
12874 static void
12875 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12876 {
12877 struct dwo_unit *dwo_unit;
12878 struct dwo_file *dwo_file;
12879
12880 gdb_assert (!per_cu->is_debug_types);
12881 gdb_assert (per_cu->cu != NULL);
12882 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
12883
12884 dwo_unit = per_cu->cu->dwo_unit;
12885 gdb_assert (dwo_unit != NULL);
12886
12887 dwo_file = dwo_unit->dwo_file;
12888 if (dwo_file->tus != NULL)
12889 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12890 per_cu->cu);
12891 }
12892
12893 /* Read in various DIEs. */
12894
12895 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12896 Inherit only the children of the DW_AT_abstract_origin DIE not being
12897 already referenced by DW_AT_abstract_origin from the children of the
12898 current DIE. */
12899
12900 static void
12901 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12902 {
12903 struct die_info *child_die;
12904 sect_offset *offsetp;
12905 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12906 struct die_info *origin_die;
12907 /* Iterator of the ORIGIN_DIE children. */
12908 struct die_info *origin_child_die;
12909 struct attribute *attr;
12910 struct dwarf2_cu *origin_cu;
12911 struct pending **origin_previous_list_in_scope;
12912
12913 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12914 if (!attr)
12915 return;
12916
12917 /* Note that following die references may follow to a die in a
12918 different cu. */
12919
12920 origin_cu = cu;
12921 origin_die = follow_die_ref (die, attr, &origin_cu);
12922
12923 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12924 symbols in. */
12925 origin_previous_list_in_scope = origin_cu->list_in_scope;
12926 origin_cu->list_in_scope = cu->list_in_scope;
12927
12928 if (die->tag != origin_die->tag
12929 && !(die->tag == DW_TAG_inlined_subroutine
12930 && origin_die->tag == DW_TAG_subprogram))
12931 complaint (_("DIE %s and its abstract origin %s have different tags"),
12932 sect_offset_str (die->sect_off),
12933 sect_offset_str (origin_die->sect_off));
12934
12935 std::vector<sect_offset> offsets;
12936
12937 for (child_die = die->child;
12938 child_die && child_die->tag;
12939 child_die = child_die->sibling)
12940 {
12941 struct die_info *child_origin_die;
12942 struct dwarf2_cu *child_origin_cu;
12943
12944 /* We are trying to process concrete instance entries:
12945 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12946 it's not relevant to our analysis here. i.e. detecting DIEs that are
12947 present in the abstract instance but not referenced in the concrete
12948 one. */
12949 if (child_die->tag == DW_TAG_call_site
12950 || child_die->tag == DW_TAG_GNU_call_site)
12951 continue;
12952
12953 /* For each CHILD_DIE, find the corresponding child of
12954 ORIGIN_DIE. If there is more than one layer of
12955 DW_AT_abstract_origin, follow them all; there shouldn't be,
12956 but GCC versions at least through 4.4 generate this (GCC PR
12957 40573). */
12958 child_origin_die = child_die;
12959 child_origin_cu = cu;
12960 while (1)
12961 {
12962 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12963 child_origin_cu);
12964 if (attr == NULL)
12965 break;
12966 child_origin_die = follow_die_ref (child_origin_die, attr,
12967 &child_origin_cu);
12968 }
12969
12970 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12971 counterpart may exist. */
12972 if (child_origin_die != child_die)
12973 {
12974 if (child_die->tag != child_origin_die->tag
12975 && !(child_die->tag == DW_TAG_inlined_subroutine
12976 && child_origin_die->tag == DW_TAG_subprogram))
12977 complaint (_("Child DIE %s and its abstract origin %s have "
12978 "different tags"),
12979 sect_offset_str (child_die->sect_off),
12980 sect_offset_str (child_origin_die->sect_off));
12981 if (child_origin_die->parent != origin_die)
12982 complaint (_("Child DIE %s and its abstract origin %s have "
12983 "different parents"),
12984 sect_offset_str (child_die->sect_off),
12985 sect_offset_str (child_origin_die->sect_off));
12986 else
12987 offsets.push_back (child_origin_die->sect_off);
12988 }
12989 }
12990 std::sort (offsets.begin (), offsets.end ());
12991 sect_offset *offsets_end = offsets.data () + offsets.size ();
12992 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12993 if (offsetp[-1] == *offsetp)
12994 complaint (_("Multiple children of DIE %s refer "
12995 "to DIE %s as their abstract origin"),
12996 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12997
12998 offsetp = offsets.data ();
12999 origin_child_die = origin_die->child;
13000 while (origin_child_die && origin_child_die->tag)
13001 {
13002 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13003 while (offsetp < offsets_end
13004 && *offsetp < origin_child_die->sect_off)
13005 offsetp++;
13006 if (offsetp >= offsets_end
13007 || *offsetp > origin_child_die->sect_off)
13008 {
13009 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13010 Check whether we're already processing ORIGIN_CHILD_DIE.
13011 This can happen with mutually referenced abstract_origins.
13012 PR 16581. */
13013 if (!origin_child_die->in_process)
13014 process_die (origin_child_die, origin_cu);
13015 }
13016 origin_child_die = origin_child_die->sibling;
13017 }
13018 origin_cu->list_in_scope = origin_previous_list_in_scope;
13019
13020 if (cu != origin_cu)
13021 compute_delayed_physnames (origin_cu);
13022 }
13023
13024 static void
13025 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13026 {
13027 struct objfile *objfile = cu->per_objfile->objfile;
13028 struct gdbarch *gdbarch = objfile->arch ();
13029 struct context_stack *newobj;
13030 CORE_ADDR lowpc;
13031 CORE_ADDR highpc;
13032 struct die_info *child_die;
13033 struct attribute *attr, *call_line, *call_file;
13034 const char *name;
13035 CORE_ADDR baseaddr;
13036 struct block *block;
13037 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13038 std::vector<struct symbol *> template_args;
13039 struct template_symbol *templ_func = NULL;
13040
13041 if (inlined_func)
13042 {
13043 /* If we do not have call site information, we can't show the
13044 caller of this inlined function. That's too confusing, so
13045 only use the scope for local variables. */
13046 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13047 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13048 if (call_line == NULL || call_file == NULL)
13049 {
13050 read_lexical_block_scope (die, cu);
13051 return;
13052 }
13053 }
13054
13055 baseaddr = objfile->text_section_offset ();
13056
13057 name = dwarf2_name (die, cu);
13058
13059 /* Ignore functions with missing or empty names. These are actually
13060 illegal according to the DWARF standard. */
13061 if (name == NULL)
13062 {
13063 complaint (_("missing name for subprogram DIE at %s"),
13064 sect_offset_str (die->sect_off));
13065 return;
13066 }
13067
13068 /* Ignore functions with missing or invalid low and high pc attributes. */
13069 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13070 <= PC_BOUNDS_INVALID)
13071 {
13072 attr = dwarf2_attr (die, DW_AT_external, cu);
13073 if (!attr || !DW_UNSND (attr))
13074 complaint (_("cannot get low and high bounds "
13075 "for subprogram DIE at %s"),
13076 sect_offset_str (die->sect_off));
13077 return;
13078 }
13079
13080 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13081 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13082
13083 /* If we have any template arguments, then we must allocate a
13084 different sort of symbol. */
13085 for (child_die = die->child; child_die; child_die = child_die->sibling)
13086 {
13087 if (child_die->tag == DW_TAG_template_type_param
13088 || child_die->tag == DW_TAG_template_value_param)
13089 {
13090 templ_func = new (&objfile->objfile_obstack) template_symbol;
13091 templ_func->subclass = SYMBOL_TEMPLATE;
13092 break;
13093 }
13094 }
13095
13096 newobj = cu->get_builder ()->push_context (0, lowpc);
13097 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13098 (struct symbol *) templ_func);
13099
13100 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13101 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13102 cu->language);
13103
13104 /* If there is a location expression for DW_AT_frame_base, record
13105 it. */
13106 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13107 if (attr != nullptr)
13108 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13109
13110 /* If there is a location for the static link, record it. */
13111 newobj->static_link = NULL;
13112 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13113 if (attr != nullptr)
13114 {
13115 newobj->static_link
13116 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13117 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13118 cu->addr_type ());
13119 }
13120
13121 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13122
13123 if (die->child != NULL)
13124 {
13125 child_die = die->child;
13126 while (child_die && child_die->tag)
13127 {
13128 if (child_die->tag == DW_TAG_template_type_param
13129 || child_die->tag == DW_TAG_template_value_param)
13130 {
13131 struct symbol *arg = new_symbol (child_die, NULL, cu);
13132
13133 if (arg != NULL)
13134 template_args.push_back (arg);
13135 }
13136 else
13137 process_die (child_die, cu);
13138 child_die = child_die->sibling;
13139 }
13140 }
13141
13142 inherit_abstract_dies (die, cu);
13143
13144 /* If we have a DW_AT_specification, we might need to import using
13145 directives from the context of the specification DIE. See the
13146 comment in determine_prefix. */
13147 if (cu->language == language_cplus
13148 && dwarf2_attr (die, DW_AT_specification, cu))
13149 {
13150 struct dwarf2_cu *spec_cu = cu;
13151 struct die_info *spec_die = die_specification (die, &spec_cu);
13152
13153 while (spec_die)
13154 {
13155 child_die = spec_die->child;
13156 while (child_die && child_die->tag)
13157 {
13158 if (child_die->tag == DW_TAG_imported_module)
13159 process_die (child_die, spec_cu);
13160 child_die = child_die->sibling;
13161 }
13162
13163 /* In some cases, GCC generates specification DIEs that
13164 themselves contain DW_AT_specification attributes. */
13165 spec_die = die_specification (spec_die, &spec_cu);
13166 }
13167 }
13168
13169 struct context_stack cstk = cu->get_builder ()->pop_context ();
13170 /* Make a block for the local symbols within. */
13171 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13172 cstk.static_link, lowpc, highpc);
13173
13174 /* For C++, set the block's scope. */
13175 if ((cu->language == language_cplus
13176 || cu->language == language_fortran
13177 || cu->language == language_d
13178 || cu->language == language_rust)
13179 && cu->processing_has_namespace_info)
13180 block_set_scope (block, determine_prefix (die, cu),
13181 &objfile->objfile_obstack);
13182
13183 /* If we have address ranges, record them. */
13184 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13185
13186 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13187
13188 /* Attach template arguments to function. */
13189 if (!template_args.empty ())
13190 {
13191 gdb_assert (templ_func != NULL);
13192
13193 templ_func->n_template_arguments = template_args.size ();
13194 templ_func->template_arguments
13195 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13196 templ_func->n_template_arguments);
13197 memcpy (templ_func->template_arguments,
13198 template_args.data (),
13199 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13200
13201 /* Make sure that the symtab is set on the new symbols. Even
13202 though they don't appear in this symtab directly, other parts
13203 of gdb assume that symbols do, and this is reasonably
13204 true. */
13205 for (symbol *sym : template_args)
13206 symbol_set_symtab (sym, symbol_symtab (templ_func));
13207 }
13208
13209 /* In C++, we can have functions nested inside functions (e.g., when
13210 a function declares a class that has methods). This means that
13211 when we finish processing a function scope, we may need to go
13212 back to building a containing block's symbol lists. */
13213 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13214 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13215
13216 /* If we've finished processing a top-level function, subsequent
13217 symbols go in the file symbol list. */
13218 if (cu->get_builder ()->outermost_context_p ())
13219 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13220 }
13221
13222 /* Process all the DIES contained within a lexical block scope. Start
13223 a new scope, process the dies, and then close the scope. */
13224
13225 static void
13226 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13227 {
13228 struct objfile *objfile = cu->per_objfile->objfile;
13229 struct gdbarch *gdbarch = objfile->arch ();
13230 CORE_ADDR lowpc, highpc;
13231 struct die_info *child_die;
13232 CORE_ADDR baseaddr;
13233
13234 baseaddr = objfile->text_section_offset ();
13235
13236 /* Ignore blocks with missing or invalid low and high pc attributes. */
13237 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13238 as multiple lexical blocks? Handling children in a sane way would
13239 be nasty. Might be easier to properly extend generic blocks to
13240 describe ranges. */
13241 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13242 {
13243 case PC_BOUNDS_NOT_PRESENT:
13244 /* DW_TAG_lexical_block has no attributes, process its children as if
13245 there was no wrapping by that DW_TAG_lexical_block.
13246 GCC does no longer produces such DWARF since GCC r224161. */
13247 for (child_die = die->child;
13248 child_die != NULL && child_die->tag;
13249 child_die = child_die->sibling)
13250 {
13251 /* We might already be processing this DIE. This can happen
13252 in an unusual circumstance -- where a subroutine A
13253 appears lexically in another subroutine B, but A actually
13254 inlines B. The recursion is broken here, rather than in
13255 inherit_abstract_dies, because it seems better to simply
13256 drop concrete children here. */
13257 if (!child_die->in_process)
13258 process_die (child_die, cu);
13259 }
13260 return;
13261 case PC_BOUNDS_INVALID:
13262 return;
13263 }
13264 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13265 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13266
13267 cu->get_builder ()->push_context (0, lowpc);
13268 if (die->child != NULL)
13269 {
13270 child_die = die->child;
13271 while (child_die && child_die->tag)
13272 {
13273 process_die (child_die, cu);
13274 child_die = child_die->sibling;
13275 }
13276 }
13277 inherit_abstract_dies (die, cu);
13278 struct context_stack cstk = cu->get_builder ()->pop_context ();
13279
13280 if (*cu->get_builder ()->get_local_symbols () != NULL
13281 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13282 {
13283 struct block *block
13284 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13285 cstk.start_addr, highpc);
13286
13287 /* Note that recording ranges after traversing children, as we
13288 do here, means that recording a parent's ranges entails
13289 walking across all its children's ranges as they appear in
13290 the address map, which is quadratic behavior.
13291
13292 It would be nicer to record the parent's ranges before
13293 traversing its children, simply overriding whatever you find
13294 there. But since we don't even decide whether to create a
13295 block until after we've traversed its children, that's hard
13296 to do. */
13297 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13298 }
13299 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13300 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13301 }
13302
13303 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13304
13305 static void
13306 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13307 {
13308 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13309 struct objfile *objfile = per_objfile->objfile;
13310 struct gdbarch *gdbarch = objfile->arch ();
13311 CORE_ADDR pc, baseaddr;
13312 struct attribute *attr;
13313 struct call_site *call_site, call_site_local;
13314 void **slot;
13315 int nparams;
13316 struct die_info *child_die;
13317
13318 baseaddr = objfile->text_section_offset ();
13319
13320 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13321 if (attr == NULL)
13322 {
13323 /* This was a pre-DWARF-5 GNU extension alias
13324 for DW_AT_call_return_pc. */
13325 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13326 }
13327 if (!attr)
13328 {
13329 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13330 "DIE %s [in module %s]"),
13331 sect_offset_str (die->sect_off), objfile_name (objfile));
13332 return;
13333 }
13334 pc = attr->value_as_address () + baseaddr;
13335 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13336
13337 if (cu->call_site_htab == NULL)
13338 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13339 NULL, &objfile->objfile_obstack,
13340 hashtab_obstack_allocate, NULL);
13341 call_site_local.pc = pc;
13342 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13343 if (*slot != NULL)
13344 {
13345 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13346 "DIE %s [in module %s]"),
13347 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13348 objfile_name (objfile));
13349 return;
13350 }
13351
13352 /* Count parameters at the caller. */
13353
13354 nparams = 0;
13355 for (child_die = die->child; child_die && child_die->tag;
13356 child_die = child_die->sibling)
13357 {
13358 if (child_die->tag != DW_TAG_call_site_parameter
13359 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13360 {
13361 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13362 "DW_TAG_call_site child DIE %s [in module %s]"),
13363 child_die->tag, sect_offset_str (child_die->sect_off),
13364 objfile_name (objfile));
13365 continue;
13366 }
13367
13368 nparams++;
13369 }
13370
13371 call_site
13372 = ((struct call_site *)
13373 obstack_alloc (&objfile->objfile_obstack,
13374 sizeof (*call_site)
13375 + (sizeof (*call_site->parameter) * (nparams - 1))));
13376 *slot = call_site;
13377 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13378 call_site->pc = pc;
13379
13380 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13381 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13382 {
13383 struct die_info *func_die;
13384
13385 /* Skip also over DW_TAG_inlined_subroutine. */
13386 for (func_die = die->parent;
13387 func_die && func_die->tag != DW_TAG_subprogram
13388 && func_die->tag != DW_TAG_subroutine_type;
13389 func_die = func_die->parent);
13390
13391 /* DW_AT_call_all_calls is a superset
13392 of DW_AT_call_all_tail_calls. */
13393 if (func_die
13394 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13395 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13396 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13397 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13398 {
13399 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13400 not complete. But keep CALL_SITE for look ups via call_site_htab,
13401 both the initial caller containing the real return address PC and
13402 the final callee containing the current PC of a chain of tail
13403 calls do not need to have the tail call list complete. But any
13404 function candidate for a virtual tail call frame searched via
13405 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13406 determined unambiguously. */
13407 }
13408 else
13409 {
13410 struct type *func_type = NULL;
13411
13412 if (func_die)
13413 func_type = get_die_type (func_die, cu);
13414 if (func_type != NULL)
13415 {
13416 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13417
13418 /* Enlist this call site to the function. */
13419 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13420 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13421 }
13422 else
13423 complaint (_("Cannot find function owning DW_TAG_call_site "
13424 "DIE %s [in module %s]"),
13425 sect_offset_str (die->sect_off), objfile_name (objfile));
13426 }
13427 }
13428
13429 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13430 if (attr == NULL)
13431 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13432 if (attr == NULL)
13433 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13434 if (attr == NULL)
13435 {
13436 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13437 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13438 }
13439 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13440 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13441 /* Keep NULL DWARF_BLOCK. */;
13442 else if (attr->form_is_block ())
13443 {
13444 struct dwarf2_locexpr_baton *dlbaton;
13445
13446 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13447 dlbaton->data = DW_BLOCK (attr)->data;
13448 dlbaton->size = DW_BLOCK (attr)->size;
13449 dlbaton->per_objfile = per_objfile;
13450 dlbaton->per_cu = cu->per_cu;
13451
13452 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13453 }
13454 else if (attr->form_is_ref ())
13455 {
13456 struct dwarf2_cu *target_cu = cu;
13457 struct die_info *target_die;
13458
13459 target_die = follow_die_ref (die, attr, &target_cu);
13460 gdb_assert (target_cu->per_objfile->objfile == objfile);
13461 if (die_is_declaration (target_die, target_cu))
13462 {
13463 const char *target_physname;
13464
13465 /* Prefer the mangled name; otherwise compute the demangled one. */
13466 target_physname = dw2_linkage_name (target_die, target_cu);
13467 if (target_physname == NULL)
13468 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13469 if (target_physname == NULL)
13470 complaint (_("DW_AT_call_target target DIE has invalid "
13471 "physname, for referencing DIE %s [in module %s]"),
13472 sect_offset_str (die->sect_off), objfile_name (objfile));
13473 else
13474 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13475 }
13476 else
13477 {
13478 CORE_ADDR lowpc;
13479
13480 /* DW_AT_entry_pc should be preferred. */
13481 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13482 <= PC_BOUNDS_INVALID)
13483 complaint (_("DW_AT_call_target target DIE has invalid "
13484 "low pc, for referencing DIE %s [in module %s]"),
13485 sect_offset_str (die->sect_off), objfile_name (objfile));
13486 else
13487 {
13488 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13489 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13490 }
13491 }
13492 }
13493 else
13494 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13495 "block nor reference, for DIE %s [in module %s]"),
13496 sect_offset_str (die->sect_off), objfile_name (objfile));
13497
13498 call_site->per_cu = cu->per_cu;
13499 call_site->per_objfile = per_objfile;
13500
13501 for (child_die = die->child;
13502 child_die && child_die->tag;
13503 child_die = child_die->sibling)
13504 {
13505 struct call_site_parameter *parameter;
13506 struct attribute *loc, *origin;
13507
13508 if (child_die->tag != DW_TAG_call_site_parameter
13509 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13510 {
13511 /* Already printed the complaint above. */
13512 continue;
13513 }
13514
13515 gdb_assert (call_site->parameter_count < nparams);
13516 parameter = &call_site->parameter[call_site->parameter_count];
13517
13518 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13519 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13520 register is contained in DW_AT_call_value. */
13521
13522 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13523 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13524 if (origin == NULL)
13525 {
13526 /* This was a pre-DWARF-5 GNU extension alias
13527 for DW_AT_call_parameter. */
13528 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13529 }
13530 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13531 {
13532 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13533
13534 sect_offset sect_off = origin->get_ref_die_offset ();
13535 if (!cu->header.offset_in_cu_p (sect_off))
13536 {
13537 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13538 binding can be done only inside one CU. Such referenced DIE
13539 therefore cannot be even moved to DW_TAG_partial_unit. */
13540 complaint (_("DW_AT_call_parameter offset is not in CU for "
13541 "DW_TAG_call_site child DIE %s [in module %s]"),
13542 sect_offset_str (child_die->sect_off),
13543 objfile_name (objfile));
13544 continue;
13545 }
13546 parameter->u.param_cu_off
13547 = (cu_offset) (sect_off - cu->header.sect_off);
13548 }
13549 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13550 {
13551 complaint (_("No DW_FORM_block* DW_AT_location for "
13552 "DW_TAG_call_site child DIE %s [in module %s]"),
13553 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13554 continue;
13555 }
13556 else
13557 {
13558 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13559 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13560 if (parameter->u.dwarf_reg != -1)
13561 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13562 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13563 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13564 &parameter->u.fb_offset))
13565 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13566 else
13567 {
13568 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13569 "for DW_FORM_block* DW_AT_location is supported for "
13570 "DW_TAG_call_site child DIE %s "
13571 "[in module %s]"),
13572 sect_offset_str (child_die->sect_off),
13573 objfile_name (objfile));
13574 continue;
13575 }
13576 }
13577
13578 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13579 if (attr == NULL)
13580 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13581 if (attr == NULL || !attr->form_is_block ())
13582 {
13583 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13584 "DW_TAG_call_site child DIE %s [in module %s]"),
13585 sect_offset_str (child_die->sect_off),
13586 objfile_name (objfile));
13587 continue;
13588 }
13589 parameter->value = DW_BLOCK (attr)->data;
13590 parameter->value_size = DW_BLOCK (attr)->size;
13591
13592 /* Parameters are not pre-cleared by memset above. */
13593 parameter->data_value = NULL;
13594 parameter->data_value_size = 0;
13595 call_site->parameter_count++;
13596
13597 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13598 if (attr == NULL)
13599 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13600 if (attr != nullptr)
13601 {
13602 if (!attr->form_is_block ())
13603 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13604 "DW_TAG_call_site child DIE %s [in module %s]"),
13605 sect_offset_str (child_die->sect_off),
13606 objfile_name (objfile));
13607 else
13608 {
13609 parameter->data_value = DW_BLOCK (attr)->data;
13610 parameter->data_value_size = DW_BLOCK (attr)->size;
13611 }
13612 }
13613 }
13614 }
13615
13616 /* Helper function for read_variable. If DIE represents a virtual
13617 table, then return the type of the concrete object that is
13618 associated with the virtual table. Otherwise, return NULL. */
13619
13620 static struct type *
13621 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13622 {
13623 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13624 if (attr == NULL)
13625 return NULL;
13626
13627 /* Find the type DIE. */
13628 struct die_info *type_die = NULL;
13629 struct dwarf2_cu *type_cu = cu;
13630
13631 if (attr->form_is_ref ())
13632 type_die = follow_die_ref (die, attr, &type_cu);
13633 if (type_die == NULL)
13634 return NULL;
13635
13636 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13637 return NULL;
13638 return die_containing_type (type_die, type_cu);
13639 }
13640
13641 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13642
13643 static void
13644 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13645 {
13646 struct rust_vtable_symbol *storage = NULL;
13647
13648 if (cu->language == language_rust)
13649 {
13650 struct type *containing_type = rust_containing_type (die, cu);
13651
13652 if (containing_type != NULL)
13653 {
13654 struct objfile *objfile = cu->per_objfile->objfile;
13655
13656 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13657 storage->concrete_type = containing_type;
13658 storage->subclass = SYMBOL_RUST_VTABLE;
13659 }
13660 }
13661
13662 struct symbol *res = new_symbol (die, NULL, cu, storage);
13663 struct attribute *abstract_origin
13664 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13665 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13666 if (res == NULL && loc && abstract_origin)
13667 {
13668 /* We have a variable without a name, but with a location and an abstract
13669 origin. This may be a concrete instance of an abstract variable
13670 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13671 later. */
13672 struct dwarf2_cu *origin_cu = cu;
13673 struct die_info *origin_die
13674 = follow_die_ref (die, abstract_origin, &origin_cu);
13675 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13676 per_objfile->per_bfd->abstract_to_concrete
13677 [origin_die->sect_off].push_back (die->sect_off);
13678 }
13679 }
13680
13681 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13682 reading .debug_rnglists.
13683 Callback's type should be:
13684 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13685 Return true if the attributes are present and valid, otherwise,
13686 return false. */
13687
13688 template <typename Callback>
13689 static bool
13690 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13691 Callback &&callback)
13692 {
13693 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13694 struct objfile *objfile = dwarf2_per_objfile->objfile;
13695 bfd *obfd = objfile->obfd;
13696 /* Base address selection entry. */
13697 gdb::optional<CORE_ADDR> base;
13698 const gdb_byte *buffer;
13699 CORE_ADDR baseaddr;
13700 bool overflow = false;
13701
13702 base = cu->base_address;
13703
13704 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13705 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13706 {
13707 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13708 offset);
13709 return false;
13710 }
13711 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13712
13713 baseaddr = objfile->text_section_offset ();
13714
13715 while (1)
13716 {
13717 /* Initialize it due to a false compiler warning. */
13718 CORE_ADDR range_beginning = 0, range_end = 0;
13719 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13720 + dwarf2_per_objfile->per_bfd->rnglists.size);
13721 unsigned int bytes_read;
13722
13723 if (buffer == buf_end)
13724 {
13725 overflow = true;
13726 break;
13727 }
13728 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13729 switch (rlet)
13730 {
13731 case DW_RLE_end_of_list:
13732 break;
13733 case DW_RLE_base_address:
13734 if (buffer + cu->header.addr_size > buf_end)
13735 {
13736 overflow = true;
13737 break;
13738 }
13739 base = cu->header.read_address (obfd, buffer, &bytes_read);
13740 buffer += bytes_read;
13741 break;
13742 case DW_RLE_start_length:
13743 if (buffer + cu->header.addr_size > buf_end)
13744 {
13745 overflow = true;
13746 break;
13747 }
13748 range_beginning = cu->header.read_address (obfd, buffer,
13749 &bytes_read);
13750 buffer += bytes_read;
13751 range_end = (range_beginning
13752 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13753 buffer += bytes_read;
13754 if (buffer > buf_end)
13755 {
13756 overflow = true;
13757 break;
13758 }
13759 break;
13760 case DW_RLE_offset_pair:
13761 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13762 buffer += bytes_read;
13763 if (buffer > buf_end)
13764 {
13765 overflow = true;
13766 break;
13767 }
13768 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13769 buffer += bytes_read;
13770 if (buffer > buf_end)
13771 {
13772 overflow = true;
13773 break;
13774 }
13775 break;
13776 case DW_RLE_start_end:
13777 if (buffer + 2 * cu->header.addr_size > buf_end)
13778 {
13779 overflow = true;
13780 break;
13781 }
13782 range_beginning = cu->header.read_address (obfd, buffer,
13783 &bytes_read);
13784 buffer += bytes_read;
13785 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13786 buffer += bytes_read;
13787 break;
13788 default:
13789 complaint (_("Invalid .debug_rnglists data (no base address)"));
13790 return false;
13791 }
13792 if (rlet == DW_RLE_end_of_list || overflow)
13793 break;
13794 if (rlet == DW_RLE_base_address)
13795 continue;
13796
13797 if (!base.has_value ())
13798 {
13799 /* We have no valid base address for the ranges
13800 data. */
13801 complaint (_("Invalid .debug_rnglists data (no base address)"));
13802 return false;
13803 }
13804
13805 if (range_beginning > range_end)
13806 {
13807 /* Inverted range entries are invalid. */
13808 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13809 return false;
13810 }
13811
13812 /* Empty range entries have no effect. */
13813 if (range_beginning == range_end)
13814 continue;
13815
13816 range_beginning += *base;
13817 range_end += *base;
13818
13819 /* A not-uncommon case of bad debug info.
13820 Don't pollute the addrmap with bad data. */
13821 if (range_beginning + baseaddr == 0
13822 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13823 {
13824 complaint (_(".debug_rnglists entry has start address of zero"
13825 " [in module %s]"), objfile_name (objfile));
13826 continue;
13827 }
13828
13829 callback (range_beginning, range_end);
13830 }
13831
13832 if (overflow)
13833 {
13834 complaint (_("Offset %d is not terminated "
13835 "for DW_AT_ranges attribute"),
13836 offset);
13837 return false;
13838 }
13839
13840 return true;
13841 }
13842
13843 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13844 Callback's type should be:
13845 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13846 Return 1 if the attributes are present and valid, otherwise, return 0. */
13847
13848 template <typename Callback>
13849 static int
13850 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13851 Callback &&callback)
13852 {
13853 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13854 struct objfile *objfile = per_objfile->objfile;
13855 struct comp_unit_head *cu_header = &cu->header;
13856 bfd *obfd = objfile->obfd;
13857 unsigned int addr_size = cu_header->addr_size;
13858 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13859 /* Base address selection entry. */
13860 gdb::optional<CORE_ADDR> base;
13861 unsigned int dummy;
13862 const gdb_byte *buffer;
13863 CORE_ADDR baseaddr;
13864
13865 if (cu_header->version >= 5)
13866 return dwarf2_rnglists_process (offset, cu, callback);
13867
13868 base = cu->base_address;
13869
13870 per_objfile->per_bfd->ranges.read (objfile);
13871 if (offset >= per_objfile->per_bfd->ranges.size)
13872 {
13873 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13874 offset);
13875 return 0;
13876 }
13877 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13878
13879 baseaddr = objfile->text_section_offset ();
13880
13881 while (1)
13882 {
13883 CORE_ADDR range_beginning, range_end;
13884
13885 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13886 buffer += addr_size;
13887 range_end = cu->header.read_address (obfd, buffer, &dummy);
13888 buffer += addr_size;
13889 offset += 2 * addr_size;
13890
13891 /* An end of list marker is a pair of zero addresses. */
13892 if (range_beginning == 0 && range_end == 0)
13893 /* Found the end of list entry. */
13894 break;
13895
13896 /* Each base address selection entry is a pair of 2 values.
13897 The first is the largest possible address, the second is
13898 the base address. Check for a base address here. */
13899 if ((range_beginning & mask) == mask)
13900 {
13901 /* If we found the largest possible address, then we already
13902 have the base address in range_end. */
13903 base = range_end;
13904 continue;
13905 }
13906
13907 if (!base.has_value ())
13908 {
13909 /* We have no valid base address for the ranges
13910 data. */
13911 complaint (_("Invalid .debug_ranges data (no base address)"));
13912 return 0;
13913 }
13914
13915 if (range_beginning > range_end)
13916 {
13917 /* Inverted range entries are invalid. */
13918 complaint (_("Invalid .debug_ranges data (inverted range)"));
13919 return 0;
13920 }
13921
13922 /* Empty range entries have no effect. */
13923 if (range_beginning == range_end)
13924 continue;
13925
13926 range_beginning += *base;
13927 range_end += *base;
13928
13929 /* A not-uncommon case of bad debug info.
13930 Don't pollute the addrmap with bad data. */
13931 if (range_beginning + baseaddr == 0
13932 && !per_objfile->per_bfd->has_section_at_zero)
13933 {
13934 complaint (_(".debug_ranges entry has start address of zero"
13935 " [in module %s]"), objfile_name (objfile));
13936 continue;
13937 }
13938
13939 callback (range_beginning, range_end);
13940 }
13941
13942 return 1;
13943 }
13944
13945 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13946 Return 1 if the attributes are present and valid, otherwise, return 0.
13947 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13948
13949 static int
13950 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13951 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13952 dwarf2_psymtab *ranges_pst)
13953 {
13954 struct objfile *objfile = cu->per_objfile->objfile;
13955 struct gdbarch *gdbarch = objfile->arch ();
13956 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13957 int low_set = 0;
13958 CORE_ADDR low = 0;
13959 CORE_ADDR high = 0;
13960 int retval;
13961
13962 retval = dwarf2_ranges_process (offset, cu,
13963 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13964 {
13965 if (ranges_pst != NULL)
13966 {
13967 CORE_ADDR lowpc;
13968 CORE_ADDR highpc;
13969
13970 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13971 range_beginning + baseaddr)
13972 - baseaddr);
13973 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13974 range_end + baseaddr)
13975 - baseaddr);
13976 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13977 lowpc, highpc - 1, ranges_pst);
13978 }
13979
13980 /* FIXME: This is recording everything as a low-high
13981 segment of consecutive addresses. We should have a
13982 data structure for discontiguous block ranges
13983 instead. */
13984 if (! low_set)
13985 {
13986 low = range_beginning;
13987 high = range_end;
13988 low_set = 1;
13989 }
13990 else
13991 {
13992 if (range_beginning < low)
13993 low = range_beginning;
13994 if (range_end > high)
13995 high = range_end;
13996 }
13997 });
13998 if (!retval)
13999 return 0;
14000
14001 if (! low_set)
14002 /* If the first entry is an end-of-list marker, the range
14003 describes an empty scope, i.e. no instructions. */
14004 return 0;
14005
14006 if (low_return)
14007 *low_return = low;
14008 if (high_return)
14009 *high_return = high;
14010 return 1;
14011 }
14012
14013 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14014 definition for the return value. *LOWPC and *HIGHPC are set iff
14015 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14016
14017 static enum pc_bounds_kind
14018 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14019 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14020 dwarf2_psymtab *pst)
14021 {
14022 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14023 struct attribute *attr;
14024 struct attribute *attr_high;
14025 CORE_ADDR low = 0;
14026 CORE_ADDR high = 0;
14027 enum pc_bounds_kind ret;
14028
14029 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14030 if (attr_high)
14031 {
14032 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14033 if (attr != nullptr)
14034 {
14035 low = attr->value_as_address ();
14036 high = attr_high->value_as_address ();
14037 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14038 high += low;
14039 }
14040 else
14041 /* Found high w/o low attribute. */
14042 return PC_BOUNDS_INVALID;
14043
14044 /* Found consecutive range of addresses. */
14045 ret = PC_BOUNDS_HIGH_LOW;
14046 }
14047 else
14048 {
14049 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14050 if (attr != NULL)
14051 {
14052 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14053 We take advantage of the fact that DW_AT_ranges does not appear
14054 in DW_TAG_compile_unit of DWO files. */
14055 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14056 unsigned int ranges_offset = (DW_UNSND (attr)
14057 + (need_ranges_base
14058 ? cu->ranges_base
14059 : 0));
14060
14061 /* Value of the DW_AT_ranges attribute is the offset in the
14062 .debug_ranges section. */
14063 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14064 return PC_BOUNDS_INVALID;
14065 /* Found discontinuous range of addresses. */
14066 ret = PC_BOUNDS_RANGES;
14067 }
14068 else
14069 return PC_BOUNDS_NOT_PRESENT;
14070 }
14071
14072 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14073 if (high <= low)
14074 return PC_BOUNDS_INVALID;
14075
14076 /* When using the GNU linker, .gnu.linkonce. sections are used to
14077 eliminate duplicate copies of functions and vtables and such.
14078 The linker will arbitrarily choose one and discard the others.
14079 The AT_*_pc values for such functions refer to local labels in
14080 these sections. If the section from that file was discarded, the
14081 labels are not in the output, so the relocs get a value of 0.
14082 If this is a discarded function, mark the pc bounds as invalid,
14083 so that GDB will ignore it. */
14084 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14085 return PC_BOUNDS_INVALID;
14086
14087 *lowpc = low;
14088 if (highpc)
14089 *highpc = high;
14090 return ret;
14091 }
14092
14093 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14094 its low and high PC addresses. Do nothing if these addresses could not
14095 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14096 and HIGHPC to the high address if greater than HIGHPC. */
14097
14098 static void
14099 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14100 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14101 struct dwarf2_cu *cu)
14102 {
14103 CORE_ADDR low, high;
14104 struct die_info *child = die->child;
14105
14106 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14107 {
14108 *lowpc = std::min (*lowpc, low);
14109 *highpc = std::max (*highpc, high);
14110 }
14111
14112 /* If the language does not allow nested subprograms (either inside
14113 subprograms or lexical blocks), we're done. */
14114 if (cu->language != language_ada)
14115 return;
14116
14117 /* Check all the children of the given DIE. If it contains nested
14118 subprograms, then check their pc bounds. Likewise, we need to
14119 check lexical blocks as well, as they may also contain subprogram
14120 definitions. */
14121 while (child && child->tag)
14122 {
14123 if (child->tag == DW_TAG_subprogram
14124 || child->tag == DW_TAG_lexical_block)
14125 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14126 child = child->sibling;
14127 }
14128 }
14129
14130 /* Get the low and high pc's represented by the scope DIE, and store
14131 them in *LOWPC and *HIGHPC. If the correct values can't be
14132 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14133
14134 static void
14135 get_scope_pc_bounds (struct die_info *die,
14136 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14137 struct dwarf2_cu *cu)
14138 {
14139 CORE_ADDR best_low = (CORE_ADDR) -1;
14140 CORE_ADDR best_high = (CORE_ADDR) 0;
14141 CORE_ADDR current_low, current_high;
14142
14143 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14144 >= PC_BOUNDS_RANGES)
14145 {
14146 best_low = current_low;
14147 best_high = current_high;
14148 }
14149 else
14150 {
14151 struct die_info *child = die->child;
14152
14153 while (child && child->tag)
14154 {
14155 switch (child->tag) {
14156 case DW_TAG_subprogram:
14157 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14158 break;
14159 case DW_TAG_namespace:
14160 case DW_TAG_module:
14161 /* FIXME: carlton/2004-01-16: Should we do this for
14162 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14163 that current GCC's always emit the DIEs corresponding
14164 to definitions of methods of classes as children of a
14165 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14166 the DIEs giving the declarations, which could be
14167 anywhere). But I don't see any reason why the
14168 standards says that they have to be there. */
14169 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14170
14171 if (current_low != ((CORE_ADDR) -1))
14172 {
14173 best_low = std::min (best_low, current_low);
14174 best_high = std::max (best_high, current_high);
14175 }
14176 break;
14177 default:
14178 /* Ignore. */
14179 break;
14180 }
14181
14182 child = child->sibling;
14183 }
14184 }
14185
14186 *lowpc = best_low;
14187 *highpc = best_high;
14188 }
14189
14190 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14191 in DIE. */
14192
14193 static void
14194 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14195 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14196 {
14197 struct objfile *objfile = cu->per_objfile->objfile;
14198 struct gdbarch *gdbarch = objfile->arch ();
14199 struct attribute *attr;
14200 struct attribute *attr_high;
14201
14202 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14203 if (attr_high)
14204 {
14205 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14206 if (attr != nullptr)
14207 {
14208 CORE_ADDR low = attr->value_as_address ();
14209 CORE_ADDR high = attr_high->value_as_address ();
14210
14211 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14212 high += low;
14213
14214 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14215 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14216 cu->get_builder ()->record_block_range (block, low, high - 1);
14217 }
14218 }
14219
14220 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14221 if (attr != nullptr)
14222 {
14223 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14224 We take advantage of the fact that DW_AT_ranges does not appear
14225 in DW_TAG_compile_unit of DWO files. */
14226 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14227
14228 /* The value of the DW_AT_ranges attribute is the offset of the
14229 address range list in the .debug_ranges section. */
14230 unsigned long offset = (DW_UNSND (attr)
14231 + (need_ranges_base ? cu->ranges_base : 0));
14232
14233 std::vector<blockrange> blockvec;
14234 dwarf2_ranges_process (offset, cu,
14235 [&] (CORE_ADDR start, CORE_ADDR end)
14236 {
14237 start += baseaddr;
14238 end += baseaddr;
14239 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14240 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14241 cu->get_builder ()->record_block_range (block, start, end - 1);
14242 blockvec.emplace_back (start, end);
14243 });
14244
14245 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14246 }
14247 }
14248
14249 /* Check whether the producer field indicates either of GCC < 4.6, or the
14250 Intel C/C++ compiler, and cache the result in CU. */
14251
14252 static void
14253 check_producer (struct dwarf2_cu *cu)
14254 {
14255 int major, minor;
14256
14257 if (cu->producer == NULL)
14258 {
14259 /* For unknown compilers expect their behavior is DWARF version
14260 compliant.
14261
14262 GCC started to support .debug_types sections by -gdwarf-4 since
14263 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14264 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14265 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14266 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14267 }
14268 else if (producer_is_gcc (cu->producer, &major, &minor))
14269 {
14270 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14271 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14272 }
14273 else if (producer_is_icc (cu->producer, &major, &minor))
14274 {
14275 cu->producer_is_icc = true;
14276 cu->producer_is_icc_lt_14 = major < 14;
14277 }
14278 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14279 cu->producer_is_codewarrior = true;
14280 else
14281 {
14282 /* For other non-GCC compilers, expect their behavior is DWARF version
14283 compliant. */
14284 }
14285
14286 cu->checked_producer = true;
14287 }
14288
14289 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14290 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14291 during 4.6.0 experimental. */
14292
14293 static bool
14294 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14295 {
14296 if (!cu->checked_producer)
14297 check_producer (cu);
14298
14299 return cu->producer_is_gxx_lt_4_6;
14300 }
14301
14302
14303 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14304 with incorrect is_stmt attributes. */
14305
14306 static bool
14307 producer_is_codewarrior (struct dwarf2_cu *cu)
14308 {
14309 if (!cu->checked_producer)
14310 check_producer (cu);
14311
14312 return cu->producer_is_codewarrior;
14313 }
14314
14315 /* Return the default accessibility type if it is not overridden by
14316 DW_AT_accessibility. */
14317
14318 static enum dwarf_access_attribute
14319 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14320 {
14321 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14322 {
14323 /* The default DWARF 2 accessibility for members is public, the default
14324 accessibility for inheritance is private. */
14325
14326 if (die->tag != DW_TAG_inheritance)
14327 return DW_ACCESS_public;
14328 else
14329 return DW_ACCESS_private;
14330 }
14331 else
14332 {
14333 /* DWARF 3+ defines the default accessibility a different way. The same
14334 rules apply now for DW_TAG_inheritance as for the members and it only
14335 depends on the container kind. */
14336
14337 if (die->parent->tag == DW_TAG_class_type)
14338 return DW_ACCESS_private;
14339 else
14340 return DW_ACCESS_public;
14341 }
14342 }
14343
14344 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14345 offset. If the attribute was not found return 0, otherwise return
14346 1. If it was found but could not properly be handled, set *OFFSET
14347 to 0. */
14348
14349 static int
14350 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14351 LONGEST *offset)
14352 {
14353 struct attribute *attr;
14354
14355 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14356 if (attr != NULL)
14357 {
14358 *offset = 0;
14359
14360 /* Note that we do not check for a section offset first here.
14361 This is because DW_AT_data_member_location is new in DWARF 4,
14362 so if we see it, we can assume that a constant form is really
14363 a constant and not a section offset. */
14364 if (attr->form_is_constant ())
14365 *offset = attr->constant_value (0);
14366 else if (attr->form_is_section_offset ())
14367 dwarf2_complex_location_expr_complaint ();
14368 else if (attr->form_is_block ())
14369 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14370 else
14371 dwarf2_complex_location_expr_complaint ();
14372
14373 return 1;
14374 }
14375
14376 return 0;
14377 }
14378
14379 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14380
14381 static void
14382 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14383 struct field *field)
14384 {
14385 struct attribute *attr;
14386
14387 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14388 if (attr != NULL)
14389 {
14390 if (attr->form_is_constant ())
14391 {
14392 LONGEST offset = attr->constant_value (0);
14393 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14394 }
14395 else if (attr->form_is_section_offset ())
14396 dwarf2_complex_location_expr_complaint ();
14397 else if (attr->form_is_block ())
14398 {
14399 bool handled;
14400 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14401 if (handled)
14402 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14403 else
14404 {
14405 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14406 struct objfile *objfile = per_objfile->objfile;
14407 struct dwarf2_locexpr_baton *dlbaton
14408 = XOBNEW (&objfile->objfile_obstack,
14409 struct dwarf2_locexpr_baton);
14410 dlbaton->data = DW_BLOCK (attr)->data;
14411 dlbaton->size = DW_BLOCK (attr)->size;
14412 /* When using this baton, we want to compute the address
14413 of the field, not the value. This is why
14414 is_reference is set to false here. */
14415 dlbaton->is_reference = false;
14416 dlbaton->per_objfile = per_objfile;
14417 dlbaton->per_cu = cu->per_cu;
14418
14419 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14420 }
14421 }
14422 else
14423 dwarf2_complex_location_expr_complaint ();
14424 }
14425 }
14426
14427 /* Add an aggregate field to the field list. */
14428
14429 static void
14430 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14431 struct dwarf2_cu *cu)
14432 {
14433 struct objfile *objfile = cu->per_objfile->objfile;
14434 struct gdbarch *gdbarch = objfile->arch ();
14435 struct nextfield *new_field;
14436 struct attribute *attr;
14437 struct field *fp;
14438 const char *fieldname = "";
14439
14440 if (die->tag == DW_TAG_inheritance)
14441 {
14442 fip->baseclasses.emplace_back ();
14443 new_field = &fip->baseclasses.back ();
14444 }
14445 else
14446 {
14447 fip->fields.emplace_back ();
14448 new_field = &fip->fields.back ();
14449 }
14450
14451 new_field->offset = die->sect_off;
14452
14453 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14454 if (attr != nullptr)
14455 new_field->accessibility = DW_UNSND (attr);
14456 else
14457 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14458 if (new_field->accessibility != DW_ACCESS_public)
14459 fip->non_public_fields = 1;
14460
14461 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14462 if (attr != nullptr)
14463 new_field->virtuality = DW_UNSND (attr);
14464 else
14465 new_field->virtuality = DW_VIRTUALITY_none;
14466
14467 fp = &new_field->field;
14468
14469 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14470 {
14471 /* Data member other than a C++ static data member. */
14472
14473 /* Get type of field. */
14474 fp->type = die_type (die, cu);
14475
14476 SET_FIELD_BITPOS (*fp, 0);
14477
14478 /* Get bit size of field (zero if none). */
14479 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14480 if (attr != nullptr)
14481 {
14482 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14483 }
14484 else
14485 {
14486 FIELD_BITSIZE (*fp) = 0;
14487 }
14488
14489 /* Get bit offset of field. */
14490 handle_data_member_location (die, cu, fp);
14491 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14492 if (attr != nullptr)
14493 {
14494 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14495 {
14496 /* For big endian bits, the DW_AT_bit_offset gives the
14497 additional bit offset from the MSB of the containing
14498 anonymous object to the MSB of the field. We don't
14499 have to do anything special since we don't need to
14500 know the size of the anonymous object. */
14501 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14502 }
14503 else
14504 {
14505 /* For little endian bits, compute the bit offset to the
14506 MSB of the anonymous object, subtract off the number of
14507 bits from the MSB of the field to the MSB of the
14508 object, and then subtract off the number of bits of
14509 the field itself. The result is the bit offset of
14510 the LSB of the field. */
14511 int anonymous_size;
14512 int bit_offset = DW_UNSND (attr);
14513
14514 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14515 if (attr != nullptr)
14516 {
14517 /* The size of the anonymous object containing
14518 the bit field is explicit, so use the
14519 indicated size (in bytes). */
14520 anonymous_size = DW_UNSND (attr);
14521 }
14522 else
14523 {
14524 /* The size of the anonymous object containing
14525 the bit field must be inferred from the type
14526 attribute of the data member containing the
14527 bit field. */
14528 anonymous_size = TYPE_LENGTH (fp->type);
14529 }
14530 SET_FIELD_BITPOS (*fp,
14531 (FIELD_BITPOS (*fp)
14532 + anonymous_size * bits_per_byte
14533 - bit_offset - FIELD_BITSIZE (*fp)));
14534 }
14535 }
14536 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14537 if (attr != NULL)
14538 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14539 + attr->constant_value (0)));
14540
14541 /* Get name of field. */
14542 fieldname = dwarf2_name (die, cu);
14543 if (fieldname == NULL)
14544 fieldname = "";
14545
14546 /* The name is already allocated along with this objfile, so we don't
14547 need to duplicate it for the type. */
14548 fp->name = fieldname;
14549
14550 /* Change accessibility for artificial fields (e.g. virtual table
14551 pointer or virtual base class pointer) to private. */
14552 if (dwarf2_attr (die, DW_AT_artificial, cu))
14553 {
14554 FIELD_ARTIFICIAL (*fp) = 1;
14555 new_field->accessibility = DW_ACCESS_private;
14556 fip->non_public_fields = 1;
14557 }
14558 }
14559 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14560 {
14561 /* C++ static member. */
14562
14563 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14564 is a declaration, but all versions of G++ as of this writing
14565 (so through at least 3.2.1) incorrectly generate
14566 DW_TAG_variable tags. */
14567
14568 const char *physname;
14569
14570 /* Get name of field. */
14571 fieldname = dwarf2_name (die, cu);
14572 if (fieldname == NULL)
14573 return;
14574
14575 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14576 if (attr
14577 /* Only create a symbol if this is an external value.
14578 new_symbol checks this and puts the value in the global symbol
14579 table, which we want. If it is not external, new_symbol
14580 will try to put the value in cu->list_in_scope which is wrong. */
14581 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14582 {
14583 /* A static const member, not much different than an enum as far as
14584 we're concerned, except that we can support more types. */
14585 new_symbol (die, NULL, cu);
14586 }
14587
14588 /* Get physical name. */
14589 physname = dwarf2_physname (fieldname, die, cu);
14590
14591 /* The name is already allocated along with this objfile, so we don't
14592 need to duplicate it for the type. */
14593 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14594 FIELD_TYPE (*fp) = die_type (die, cu);
14595 FIELD_NAME (*fp) = fieldname;
14596 }
14597 else if (die->tag == DW_TAG_inheritance)
14598 {
14599 /* C++ base class field. */
14600 handle_data_member_location (die, cu, fp);
14601 FIELD_BITSIZE (*fp) = 0;
14602 FIELD_TYPE (*fp) = die_type (die, cu);
14603 FIELD_NAME (*fp) = fp->type->name ();
14604 }
14605 else
14606 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14607 }
14608
14609 /* Can the type given by DIE define another type? */
14610
14611 static bool
14612 type_can_define_types (const struct die_info *die)
14613 {
14614 switch (die->tag)
14615 {
14616 case DW_TAG_typedef:
14617 case DW_TAG_class_type:
14618 case DW_TAG_structure_type:
14619 case DW_TAG_union_type:
14620 case DW_TAG_enumeration_type:
14621 return true;
14622
14623 default:
14624 return false;
14625 }
14626 }
14627
14628 /* Add a type definition defined in the scope of the FIP's class. */
14629
14630 static void
14631 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14632 struct dwarf2_cu *cu)
14633 {
14634 struct decl_field fp;
14635 memset (&fp, 0, sizeof (fp));
14636
14637 gdb_assert (type_can_define_types (die));
14638
14639 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14640 fp.name = dwarf2_name (die, cu);
14641 fp.type = read_type_die (die, cu);
14642
14643 /* Save accessibility. */
14644 enum dwarf_access_attribute accessibility;
14645 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14646 if (attr != NULL)
14647 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14648 else
14649 accessibility = dwarf2_default_access_attribute (die, cu);
14650 switch (accessibility)
14651 {
14652 case DW_ACCESS_public:
14653 /* The assumed value if neither private nor protected. */
14654 break;
14655 case DW_ACCESS_private:
14656 fp.is_private = 1;
14657 break;
14658 case DW_ACCESS_protected:
14659 fp.is_protected = 1;
14660 break;
14661 default:
14662 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14663 }
14664
14665 if (die->tag == DW_TAG_typedef)
14666 fip->typedef_field_list.push_back (fp);
14667 else
14668 fip->nested_types_list.push_back (fp);
14669 }
14670
14671 /* A convenience typedef that's used when finding the discriminant
14672 field for a variant part. */
14673 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14674 offset_map_type;
14675
14676 /* Compute the discriminant range for a given variant. OBSTACK is
14677 where the results will be stored. VARIANT is the variant to
14678 process. IS_UNSIGNED indicates whether the discriminant is signed
14679 or unsigned. */
14680
14681 static const gdb::array_view<discriminant_range>
14682 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14683 bool is_unsigned)
14684 {
14685 std::vector<discriminant_range> ranges;
14686
14687 if (variant.default_branch)
14688 return {};
14689
14690 if (variant.discr_list_data == nullptr)
14691 {
14692 discriminant_range r
14693 = {variant.discriminant_value, variant.discriminant_value};
14694 ranges.push_back (r);
14695 }
14696 else
14697 {
14698 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14699 variant.discr_list_data->size);
14700 while (!data.empty ())
14701 {
14702 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14703 {
14704 complaint (_("invalid discriminant marker: %d"), data[0]);
14705 break;
14706 }
14707 bool is_range = data[0] == DW_DSC_range;
14708 data = data.slice (1);
14709
14710 ULONGEST low, high;
14711 unsigned int bytes_read;
14712
14713 if (data.empty ())
14714 {
14715 complaint (_("DW_AT_discr_list missing low value"));
14716 break;
14717 }
14718 if (is_unsigned)
14719 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14720 else
14721 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14722 &bytes_read);
14723 data = data.slice (bytes_read);
14724
14725 if (is_range)
14726 {
14727 if (data.empty ())
14728 {
14729 complaint (_("DW_AT_discr_list missing high value"));
14730 break;
14731 }
14732 if (is_unsigned)
14733 high = read_unsigned_leb128 (nullptr, data.data (),
14734 &bytes_read);
14735 else
14736 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14737 &bytes_read);
14738 data = data.slice (bytes_read);
14739 }
14740 else
14741 high = low;
14742
14743 ranges.push_back ({ low, high });
14744 }
14745 }
14746
14747 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14748 ranges.size ());
14749 std::copy (ranges.begin (), ranges.end (), result);
14750 return gdb::array_view<discriminant_range> (result, ranges.size ());
14751 }
14752
14753 static const gdb::array_view<variant_part> create_variant_parts
14754 (struct obstack *obstack,
14755 const offset_map_type &offset_map,
14756 struct field_info *fi,
14757 const std::vector<variant_part_builder> &variant_parts);
14758
14759 /* Fill in a "struct variant" for a given variant field. RESULT is
14760 the variant to fill in. OBSTACK is where any needed allocations
14761 will be done. OFFSET_MAP holds the mapping from section offsets to
14762 fields for the type. FI describes the fields of the type we're
14763 processing. FIELD is the variant field we're converting. */
14764
14765 static void
14766 create_one_variant (variant &result, struct obstack *obstack,
14767 const offset_map_type &offset_map,
14768 struct field_info *fi, const variant_field &field)
14769 {
14770 result.discriminants = convert_variant_range (obstack, field, false);
14771 result.first_field = field.first_field + fi->baseclasses.size ();
14772 result.last_field = field.last_field + fi->baseclasses.size ();
14773 result.parts = create_variant_parts (obstack, offset_map, fi,
14774 field.variant_parts);
14775 }
14776
14777 /* Fill in a "struct variant_part" for a given variant part. RESULT
14778 is the variant part to fill in. OBSTACK is where any needed
14779 allocations will be done. OFFSET_MAP holds the mapping from
14780 section offsets to fields for the type. FI describes the fields of
14781 the type we're processing. BUILDER is the variant part to be
14782 converted. */
14783
14784 static void
14785 create_one_variant_part (variant_part &result,
14786 struct obstack *obstack,
14787 const offset_map_type &offset_map,
14788 struct field_info *fi,
14789 const variant_part_builder &builder)
14790 {
14791 auto iter = offset_map.find (builder.discriminant_offset);
14792 if (iter == offset_map.end ())
14793 {
14794 result.discriminant_index = -1;
14795 /* Doesn't matter. */
14796 result.is_unsigned = false;
14797 }
14798 else
14799 {
14800 result.discriminant_index = iter->second;
14801 result.is_unsigned
14802 = TYPE_UNSIGNED (FIELD_TYPE
14803 (fi->fields[result.discriminant_index].field));
14804 }
14805
14806 size_t n = builder.variants.size ();
14807 variant *output = new (obstack) variant[n];
14808 for (size_t i = 0; i < n; ++i)
14809 create_one_variant (output[i], obstack, offset_map, fi,
14810 builder.variants[i]);
14811
14812 result.variants = gdb::array_view<variant> (output, n);
14813 }
14814
14815 /* Create a vector of variant parts that can be attached to a type.
14816 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14817 holds the mapping from section offsets to fields for the type. FI
14818 describes the fields of the type we're processing. VARIANT_PARTS
14819 is the vector to convert. */
14820
14821 static const gdb::array_view<variant_part>
14822 create_variant_parts (struct obstack *obstack,
14823 const offset_map_type &offset_map,
14824 struct field_info *fi,
14825 const std::vector<variant_part_builder> &variant_parts)
14826 {
14827 if (variant_parts.empty ())
14828 return {};
14829
14830 size_t n = variant_parts.size ();
14831 variant_part *result = new (obstack) variant_part[n];
14832 for (size_t i = 0; i < n; ++i)
14833 create_one_variant_part (result[i], obstack, offset_map, fi,
14834 variant_parts[i]);
14835
14836 return gdb::array_view<variant_part> (result, n);
14837 }
14838
14839 /* Compute the variant part vector for FIP, attaching it to TYPE when
14840 done. */
14841
14842 static void
14843 add_variant_property (struct field_info *fip, struct type *type,
14844 struct dwarf2_cu *cu)
14845 {
14846 /* Map section offsets of fields to their field index. Note the
14847 field index here does not take the number of baseclasses into
14848 account. */
14849 offset_map_type offset_map;
14850 for (int i = 0; i < fip->fields.size (); ++i)
14851 offset_map[fip->fields[i].offset] = i;
14852
14853 struct objfile *objfile = cu->per_objfile->objfile;
14854 gdb::array_view<variant_part> parts
14855 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14856 fip->variant_parts);
14857
14858 struct dynamic_prop prop;
14859 prop.kind = PROP_VARIANT_PARTS;
14860 prop.data.variant_parts
14861 = ((gdb::array_view<variant_part> *)
14862 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14863
14864 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14865 }
14866
14867 /* Create the vector of fields, and attach it to the type. */
14868
14869 static void
14870 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14871 struct dwarf2_cu *cu)
14872 {
14873 int nfields = fip->nfields ();
14874
14875 /* Record the field count, allocate space for the array of fields,
14876 and create blank accessibility bitfields if necessary. */
14877 type->set_num_fields (nfields);
14878 type->set_fields
14879 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14880
14881 if (fip->non_public_fields && cu->language != language_ada)
14882 {
14883 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14884
14885 TYPE_FIELD_PRIVATE_BITS (type) =
14886 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14887 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14888
14889 TYPE_FIELD_PROTECTED_BITS (type) =
14890 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14891 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14892
14893 TYPE_FIELD_IGNORE_BITS (type) =
14894 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14895 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14896 }
14897
14898 /* If the type has baseclasses, allocate and clear a bit vector for
14899 TYPE_FIELD_VIRTUAL_BITS. */
14900 if (!fip->baseclasses.empty () && cu->language != language_ada)
14901 {
14902 int num_bytes = B_BYTES (fip->baseclasses.size ());
14903 unsigned char *pointer;
14904
14905 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14906 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14907 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14908 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14909 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14910 }
14911
14912 if (!fip->variant_parts.empty ())
14913 add_variant_property (fip, type, cu);
14914
14915 /* Copy the saved-up fields into the field vector. */
14916 for (int i = 0; i < nfields; ++i)
14917 {
14918 struct nextfield &field
14919 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14920 : fip->fields[i - fip->baseclasses.size ()]);
14921
14922 type->field (i) = field.field;
14923 switch (field.accessibility)
14924 {
14925 case DW_ACCESS_private:
14926 if (cu->language != language_ada)
14927 SET_TYPE_FIELD_PRIVATE (type, i);
14928 break;
14929
14930 case DW_ACCESS_protected:
14931 if (cu->language != language_ada)
14932 SET_TYPE_FIELD_PROTECTED (type, i);
14933 break;
14934
14935 case DW_ACCESS_public:
14936 break;
14937
14938 default:
14939 /* Unknown accessibility. Complain and treat it as public. */
14940 {
14941 complaint (_("unsupported accessibility %d"),
14942 field.accessibility);
14943 }
14944 break;
14945 }
14946 if (i < fip->baseclasses.size ())
14947 {
14948 switch (field.virtuality)
14949 {
14950 case DW_VIRTUALITY_virtual:
14951 case DW_VIRTUALITY_pure_virtual:
14952 if (cu->language == language_ada)
14953 error (_("unexpected virtuality in component of Ada type"));
14954 SET_TYPE_FIELD_VIRTUAL (type, i);
14955 break;
14956 }
14957 }
14958 }
14959 }
14960
14961 /* Return true if this member function is a constructor, false
14962 otherwise. */
14963
14964 static int
14965 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14966 {
14967 const char *fieldname;
14968 const char *type_name;
14969 int len;
14970
14971 if (die->parent == NULL)
14972 return 0;
14973
14974 if (die->parent->tag != DW_TAG_structure_type
14975 && die->parent->tag != DW_TAG_union_type
14976 && die->parent->tag != DW_TAG_class_type)
14977 return 0;
14978
14979 fieldname = dwarf2_name (die, cu);
14980 type_name = dwarf2_name (die->parent, cu);
14981 if (fieldname == NULL || type_name == NULL)
14982 return 0;
14983
14984 len = strlen (fieldname);
14985 return (strncmp (fieldname, type_name, len) == 0
14986 && (type_name[len] == '\0' || type_name[len] == '<'));
14987 }
14988
14989 /* Check if the given VALUE is a recognized enum
14990 dwarf_defaulted_attribute constant according to DWARF5 spec,
14991 Table 7.24. */
14992
14993 static bool
14994 is_valid_DW_AT_defaulted (ULONGEST value)
14995 {
14996 switch (value)
14997 {
14998 case DW_DEFAULTED_no:
14999 case DW_DEFAULTED_in_class:
15000 case DW_DEFAULTED_out_of_class:
15001 return true;
15002 }
15003
15004 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15005 return false;
15006 }
15007
15008 /* Add a member function to the proper fieldlist. */
15009
15010 static void
15011 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15012 struct type *type, struct dwarf2_cu *cu)
15013 {
15014 struct objfile *objfile = cu->per_objfile->objfile;
15015 struct attribute *attr;
15016 int i;
15017 struct fnfieldlist *flp = nullptr;
15018 struct fn_field *fnp;
15019 const char *fieldname;
15020 struct type *this_type;
15021 enum dwarf_access_attribute accessibility;
15022
15023 if (cu->language == language_ada)
15024 error (_("unexpected member function in Ada type"));
15025
15026 /* Get name of member function. */
15027 fieldname = dwarf2_name (die, cu);
15028 if (fieldname == NULL)
15029 return;
15030
15031 /* Look up member function name in fieldlist. */
15032 for (i = 0; i < fip->fnfieldlists.size (); i++)
15033 {
15034 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15035 {
15036 flp = &fip->fnfieldlists[i];
15037 break;
15038 }
15039 }
15040
15041 /* Create a new fnfieldlist if necessary. */
15042 if (flp == nullptr)
15043 {
15044 fip->fnfieldlists.emplace_back ();
15045 flp = &fip->fnfieldlists.back ();
15046 flp->name = fieldname;
15047 i = fip->fnfieldlists.size () - 1;
15048 }
15049
15050 /* Create a new member function field and add it to the vector of
15051 fnfieldlists. */
15052 flp->fnfields.emplace_back ();
15053 fnp = &flp->fnfields.back ();
15054
15055 /* Delay processing of the physname until later. */
15056 if (cu->language == language_cplus)
15057 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15058 die, cu);
15059 else
15060 {
15061 const char *physname = dwarf2_physname (fieldname, die, cu);
15062 fnp->physname = physname ? physname : "";
15063 }
15064
15065 fnp->type = alloc_type (objfile);
15066 this_type = read_type_die (die, cu);
15067 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15068 {
15069 int nparams = this_type->num_fields ();
15070
15071 /* TYPE is the domain of this method, and THIS_TYPE is the type
15072 of the method itself (TYPE_CODE_METHOD). */
15073 smash_to_method_type (fnp->type, type,
15074 TYPE_TARGET_TYPE (this_type),
15075 this_type->fields (),
15076 this_type->num_fields (),
15077 TYPE_VARARGS (this_type));
15078
15079 /* Handle static member functions.
15080 Dwarf2 has no clean way to discern C++ static and non-static
15081 member functions. G++ helps GDB by marking the first
15082 parameter for non-static member functions (which is the this
15083 pointer) as artificial. We obtain this information from
15084 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15085 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15086 fnp->voffset = VOFFSET_STATIC;
15087 }
15088 else
15089 complaint (_("member function type missing for '%s'"),
15090 dwarf2_full_name (fieldname, die, cu));
15091
15092 /* Get fcontext from DW_AT_containing_type if present. */
15093 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15094 fnp->fcontext = die_containing_type (die, cu);
15095
15096 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15097 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15098
15099 /* Get accessibility. */
15100 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15101 if (attr != nullptr)
15102 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15103 else
15104 accessibility = dwarf2_default_access_attribute (die, cu);
15105 switch (accessibility)
15106 {
15107 case DW_ACCESS_private:
15108 fnp->is_private = 1;
15109 break;
15110 case DW_ACCESS_protected:
15111 fnp->is_protected = 1;
15112 break;
15113 }
15114
15115 /* Check for artificial methods. */
15116 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15117 if (attr && DW_UNSND (attr) != 0)
15118 fnp->is_artificial = 1;
15119
15120 /* Check for defaulted methods. */
15121 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15122 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15123 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15124
15125 /* Check for deleted methods. */
15126 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15127 if (attr != nullptr && DW_UNSND (attr) != 0)
15128 fnp->is_deleted = 1;
15129
15130 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15131
15132 /* Get index in virtual function table if it is a virtual member
15133 function. For older versions of GCC, this is an offset in the
15134 appropriate virtual table, as specified by DW_AT_containing_type.
15135 For everyone else, it is an expression to be evaluated relative
15136 to the object address. */
15137
15138 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15139 if (attr != nullptr)
15140 {
15141 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15142 {
15143 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15144 {
15145 /* Old-style GCC. */
15146 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15147 }
15148 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15149 || (DW_BLOCK (attr)->size > 1
15150 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15151 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15152 {
15153 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15154 if ((fnp->voffset % cu->header.addr_size) != 0)
15155 dwarf2_complex_location_expr_complaint ();
15156 else
15157 fnp->voffset /= cu->header.addr_size;
15158 fnp->voffset += 2;
15159 }
15160 else
15161 dwarf2_complex_location_expr_complaint ();
15162
15163 if (!fnp->fcontext)
15164 {
15165 /* If there is no `this' field and no DW_AT_containing_type,
15166 we cannot actually find a base class context for the
15167 vtable! */
15168 if (this_type->num_fields () == 0
15169 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15170 {
15171 complaint (_("cannot determine context for virtual member "
15172 "function \"%s\" (offset %s)"),
15173 fieldname, sect_offset_str (die->sect_off));
15174 }
15175 else
15176 {
15177 fnp->fcontext
15178 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15179 }
15180 }
15181 }
15182 else if (attr->form_is_section_offset ())
15183 {
15184 dwarf2_complex_location_expr_complaint ();
15185 }
15186 else
15187 {
15188 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15189 fieldname);
15190 }
15191 }
15192 else
15193 {
15194 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15195 if (attr && DW_UNSND (attr))
15196 {
15197 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15198 complaint (_("Member function \"%s\" (offset %s) is virtual "
15199 "but the vtable offset is not specified"),
15200 fieldname, sect_offset_str (die->sect_off));
15201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15202 TYPE_CPLUS_DYNAMIC (type) = 1;
15203 }
15204 }
15205 }
15206
15207 /* Create the vector of member function fields, and attach it to the type. */
15208
15209 static void
15210 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15211 struct dwarf2_cu *cu)
15212 {
15213 if (cu->language == language_ada)
15214 error (_("unexpected member functions in Ada type"));
15215
15216 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15217 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15218 TYPE_ALLOC (type,
15219 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15220
15221 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15222 {
15223 struct fnfieldlist &nf = fip->fnfieldlists[i];
15224 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15225
15226 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15227 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15228 fn_flp->fn_fields = (struct fn_field *)
15229 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15230
15231 for (int k = 0; k < nf.fnfields.size (); ++k)
15232 fn_flp->fn_fields[k] = nf.fnfields[k];
15233 }
15234
15235 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15236 }
15237
15238 /* Returns non-zero if NAME is the name of a vtable member in CU's
15239 language, zero otherwise. */
15240 static int
15241 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15242 {
15243 static const char vptr[] = "_vptr";
15244
15245 /* Look for the C++ form of the vtable. */
15246 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15247 return 1;
15248
15249 return 0;
15250 }
15251
15252 /* GCC outputs unnamed structures that are really pointers to member
15253 functions, with the ABI-specified layout. If TYPE describes
15254 such a structure, smash it into a member function type.
15255
15256 GCC shouldn't do this; it should just output pointer to member DIEs.
15257 This is GCC PR debug/28767. */
15258
15259 static void
15260 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15261 {
15262 struct type *pfn_type, *self_type, *new_type;
15263
15264 /* Check for a structure with no name and two children. */
15265 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15266 return;
15267
15268 /* Check for __pfn and __delta members. */
15269 if (TYPE_FIELD_NAME (type, 0) == NULL
15270 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15271 || TYPE_FIELD_NAME (type, 1) == NULL
15272 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15273 return;
15274
15275 /* Find the type of the method. */
15276 pfn_type = TYPE_FIELD_TYPE (type, 0);
15277 if (pfn_type == NULL
15278 || pfn_type->code () != TYPE_CODE_PTR
15279 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15280 return;
15281
15282 /* Look for the "this" argument. */
15283 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15284 if (pfn_type->num_fields () == 0
15285 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15286 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15287 return;
15288
15289 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15290 new_type = alloc_type (objfile);
15291 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15292 pfn_type->fields (), pfn_type->num_fields (),
15293 TYPE_VARARGS (pfn_type));
15294 smash_to_methodptr_type (type, new_type);
15295 }
15296
15297 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15298 appropriate error checking and issuing complaints if there is a
15299 problem. */
15300
15301 static ULONGEST
15302 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15303 {
15304 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15305
15306 if (attr == nullptr)
15307 return 0;
15308
15309 if (!attr->form_is_constant ())
15310 {
15311 complaint (_("DW_AT_alignment must have constant form"
15312 " - DIE at %s [in module %s]"),
15313 sect_offset_str (die->sect_off),
15314 objfile_name (cu->per_objfile->objfile));
15315 return 0;
15316 }
15317
15318 ULONGEST align;
15319 if (attr->form == DW_FORM_sdata)
15320 {
15321 LONGEST val = DW_SND (attr);
15322 if (val < 0)
15323 {
15324 complaint (_("DW_AT_alignment value must not be negative"
15325 " - DIE at %s [in module %s]"),
15326 sect_offset_str (die->sect_off),
15327 objfile_name (cu->per_objfile->objfile));
15328 return 0;
15329 }
15330 align = val;
15331 }
15332 else
15333 align = DW_UNSND (attr);
15334
15335 if (align == 0)
15336 {
15337 complaint (_("DW_AT_alignment value must not be zero"
15338 " - DIE at %s [in module %s]"),
15339 sect_offset_str (die->sect_off),
15340 objfile_name (cu->per_objfile->objfile));
15341 return 0;
15342 }
15343 if ((align & (align - 1)) != 0)
15344 {
15345 complaint (_("DW_AT_alignment value must be a power of 2"
15346 " - DIE at %s [in module %s]"),
15347 sect_offset_str (die->sect_off),
15348 objfile_name (cu->per_objfile->objfile));
15349 return 0;
15350 }
15351
15352 return align;
15353 }
15354
15355 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15356 the alignment for TYPE. */
15357
15358 static void
15359 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15360 struct type *type)
15361 {
15362 if (!set_type_align (type, get_alignment (cu, die)))
15363 complaint (_("DW_AT_alignment value too large"
15364 " - DIE at %s [in module %s]"),
15365 sect_offset_str (die->sect_off),
15366 objfile_name (cu->per_objfile->objfile));
15367 }
15368
15369 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15370 constant for a type, according to DWARF5 spec, Table 5.5. */
15371
15372 static bool
15373 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15374 {
15375 switch (value)
15376 {
15377 case DW_CC_normal:
15378 case DW_CC_pass_by_reference:
15379 case DW_CC_pass_by_value:
15380 return true;
15381
15382 default:
15383 complaint (_("unrecognized DW_AT_calling_convention value "
15384 "(%s) for a type"), pulongest (value));
15385 return false;
15386 }
15387 }
15388
15389 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15390 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15391 also according to GNU-specific values (see include/dwarf2.h). */
15392
15393 static bool
15394 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15395 {
15396 switch (value)
15397 {
15398 case DW_CC_normal:
15399 case DW_CC_program:
15400 case DW_CC_nocall:
15401 return true;
15402
15403 case DW_CC_GNU_renesas_sh:
15404 case DW_CC_GNU_borland_fastcall_i386:
15405 case DW_CC_GDB_IBM_OpenCL:
15406 return true;
15407
15408 default:
15409 complaint (_("unrecognized DW_AT_calling_convention value "
15410 "(%s) for a subroutine"), pulongest (value));
15411 return false;
15412 }
15413 }
15414
15415 /* Called when we find the DIE that starts a structure or union scope
15416 (definition) to create a type for the structure or union. Fill in
15417 the type's name and general properties; the members will not be
15418 processed until process_structure_scope. A symbol table entry for
15419 the type will also not be done until process_structure_scope (assuming
15420 the type has a name).
15421
15422 NOTE: we need to call these functions regardless of whether or not the
15423 DIE has a DW_AT_name attribute, since it might be an anonymous
15424 structure or union. This gets the type entered into our set of
15425 user defined types. */
15426
15427 static struct type *
15428 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15429 {
15430 struct objfile *objfile = cu->per_objfile->objfile;
15431 struct type *type;
15432 struct attribute *attr;
15433 const char *name;
15434
15435 /* If the definition of this type lives in .debug_types, read that type.
15436 Don't follow DW_AT_specification though, that will take us back up
15437 the chain and we want to go down. */
15438 attr = die->attr (DW_AT_signature);
15439 if (attr != nullptr)
15440 {
15441 type = get_DW_AT_signature_type (die, attr, cu);
15442
15443 /* The type's CU may not be the same as CU.
15444 Ensure TYPE is recorded with CU in die_type_hash. */
15445 return set_die_type (die, type, cu);
15446 }
15447
15448 type = alloc_type (objfile);
15449 INIT_CPLUS_SPECIFIC (type);
15450
15451 name = dwarf2_name (die, cu);
15452 if (name != NULL)
15453 {
15454 if (cu->language == language_cplus
15455 || cu->language == language_d
15456 || cu->language == language_rust)
15457 {
15458 const char *full_name = dwarf2_full_name (name, die, cu);
15459
15460 /* dwarf2_full_name might have already finished building the DIE's
15461 type. If so, there is no need to continue. */
15462 if (get_die_type (die, cu) != NULL)
15463 return get_die_type (die, cu);
15464
15465 type->set_name (full_name);
15466 }
15467 else
15468 {
15469 /* The name is already allocated along with this objfile, so
15470 we don't need to duplicate it for the type. */
15471 type->set_name (name);
15472 }
15473 }
15474
15475 if (die->tag == DW_TAG_structure_type)
15476 {
15477 type->set_code (TYPE_CODE_STRUCT);
15478 }
15479 else if (die->tag == DW_TAG_union_type)
15480 {
15481 type->set_code (TYPE_CODE_UNION);
15482 }
15483 else
15484 {
15485 type->set_code (TYPE_CODE_STRUCT);
15486 }
15487
15488 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15489 TYPE_DECLARED_CLASS (type) = 1;
15490
15491 /* Store the calling convention in the type if it's available in
15492 the die. Otherwise the calling convention remains set to
15493 the default value DW_CC_normal. */
15494 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15495 if (attr != nullptr
15496 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15497 {
15498 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15499 TYPE_CPLUS_CALLING_CONVENTION (type)
15500 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15501 }
15502
15503 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15504 if (attr != nullptr)
15505 {
15506 if (attr->form_is_constant ())
15507 TYPE_LENGTH (type) = DW_UNSND (attr);
15508 else
15509 {
15510 struct dynamic_prop prop;
15511 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15512 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15513 TYPE_LENGTH (type) = 0;
15514 }
15515 }
15516 else
15517 {
15518 TYPE_LENGTH (type) = 0;
15519 }
15520
15521 maybe_set_alignment (cu, die, type);
15522
15523 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15524 {
15525 /* ICC<14 does not output the required DW_AT_declaration on
15526 incomplete types, but gives them a size of zero. */
15527 TYPE_STUB (type) = 1;
15528 }
15529 else
15530 TYPE_STUB_SUPPORTED (type) = 1;
15531
15532 if (die_is_declaration (die, cu))
15533 TYPE_STUB (type) = 1;
15534 else if (attr == NULL && die->child == NULL
15535 && producer_is_realview (cu->producer))
15536 /* RealView does not output the required DW_AT_declaration
15537 on incomplete types. */
15538 TYPE_STUB (type) = 1;
15539
15540 /* We need to add the type field to the die immediately so we don't
15541 infinitely recurse when dealing with pointers to the structure
15542 type within the structure itself. */
15543 set_die_type (die, type, cu);
15544
15545 /* set_die_type should be already done. */
15546 set_descriptive_type (type, die, cu);
15547
15548 return type;
15549 }
15550
15551 static void handle_struct_member_die
15552 (struct die_info *child_die,
15553 struct type *type,
15554 struct field_info *fi,
15555 std::vector<struct symbol *> *template_args,
15556 struct dwarf2_cu *cu);
15557
15558 /* A helper for handle_struct_member_die that handles
15559 DW_TAG_variant_part. */
15560
15561 static void
15562 handle_variant_part (struct die_info *die, struct type *type,
15563 struct field_info *fi,
15564 std::vector<struct symbol *> *template_args,
15565 struct dwarf2_cu *cu)
15566 {
15567 variant_part_builder *new_part;
15568 if (fi->current_variant_part == nullptr)
15569 {
15570 fi->variant_parts.emplace_back ();
15571 new_part = &fi->variant_parts.back ();
15572 }
15573 else if (!fi->current_variant_part->processing_variant)
15574 {
15575 complaint (_("nested DW_TAG_variant_part seen "
15576 "- DIE at %s [in module %s]"),
15577 sect_offset_str (die->sect_off),
15578 objfile_name (cu->per_objfile->objfile));
15579 return;
15580 }
15581 else
15582 {
15583 variant_field &current = fi->current_variant_part->variants.back ();
15584 current.variant_parts.emplace_back ();
15585 new_part = &current.variant_parts.back ();
15586 }
15587
15588 /* When we recurse, we want callees to add to this new variant
15589 part. */
15590 scoped_restore save_current_variant_part
15591 = make_scoped_restore (&fi->current_variant_part, new_part);
15592
15593 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15594 if (discr == NULL)
15595 {
15596 /* It's a univariant form, an extension we support. */
15597 }
15598 else if (discr->form_is_ref ())
15599 {
15600 struct dwarf2_cu *target_cu = cu;
15601 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15602
15603 new_part->discriminant_offset = target_die->sect_off;
15604 }
15605 else
15606 {
15607 complaint (_("DW_AT_discr does not have DIE reference form"
15608 " - DIE at %s [in module %s]"),
15609 sect_offset_str (die->sect_off),
15610 objfile_name (cu->per_objfile->objfile));
15611 }
15612
15613 for (die_info *child_die = die->child;
15614 child_die != NULL;
15615 child_die = child_die->sibling)
15616 handle_struct_member_die (child_die, type, fi, template_args, cu);
15617 }
15618
15619 /* A helper for handle_struct_member_die that handles
15620 DW_TAG_variant. */
15621
15622 static void
15623 handle_variant (struct die_info *die, struct type *type,
15624 struct field_info *fi,
15625 std::vector<struct symbol *> *template_args,
15626 struct dwarf2_cu *cu)
15627 {
15628 if (fi->current_variant_part == nullptr)
15629 {
15630 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15631 "- DIE at %s [in module %s]"),
15632 sect_offset_str (die->sect_off),
15633 objfile_name (cu->per_objfile->objfile));
15634 return;
15635 }
15636 if (fi->current_variant_part->processing_variant)
15637 {
15638 complaint (_("nested DW_TAG_variant seen "
15639 "- DIE at %s [in module %s]"),
15640 sect_offset_str (die->sect_off),
15641 objfile_name (cu->per_objfile->objfile));
15642 return;
15643 }
15644
15645 scoped_restore save_processing_variant
15646 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15647 true);
15648
15649 fi->current_variant_part->variants.emplace_back ();
15650 variant_field &variant = fi->current_variant_part->variants.back ();
15651 variant.first_field = fi->fields.size ();
15652
15653 /* In a variant we want to get the discriminant and also add a
15654 field for our sole member child. */
15655 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15656 if (discr == nullptr)
15657 {
15658 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15659 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15660 variant.default_branch = true;
15661 else
15662 variant.discr_list_data = DW_BLOCK (discr);
15663 }
15664 else
15665 variant.discriminant_value = DW_UNSND (discr);
15666
15667 for (die_info *variant_child = die->child;
15668 variant_child != NULL;
15669 variant_child = variant_child->sibling)
15670 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15671
15672 variant.last_field = fi->fields.size ();
15673 }
15674
15675 /* A helper for process_structure_scope that handles a single member
15676 DIE. */
15677
15678 static void
15679 handle_struct_member_die (struct die_info *child_die, struct type *type,
15680 struct field_info *fi,
15681 std::vector<struct symbol *> *template_args,
15682 struct dwarf2_cu *cu)
15683 {
15684 if (child_die->tag == DW_TAG_member
15685 || child_die->tag == DW_TAG_variable)
15686 {
15687 /* NOTE: carlton/2002-11-05: A C++ static data member
15688 should be a DW_TAG_member that is a declaration, but
15689 all versions of G++ as of this writing (so through at
15690 least 3.2.1) incorrectly generate DW_TAG_variable
15691 tags for them instead. */
15692 dwarf2_add_field (fi, child_die, cu);
15693 }
15694 else if (child_die->tag == DW_TAG_subprogram)
15695 {
15696 /* Rust doesn't have member functions in the C++ sense.
15697 However, it does emit ordinary functions as children
15698 of a struct DIE. */
15699 if (cu->language == language_rust)
15700 read_func_scope (child_die, cu);
15701 else
15702 {
15703 /* C++ member function. */
15704 dwarf2_add_member_fn (fi, child_die, type, cu);
15705 }
15706 }
15707 else if (child_die->tag == DW_TAG_inheritance)
15708 {
15709 /* C++ base class field. */
15710 dwarf2_add_field (fi, child_die, cu);
15711 }
15712 else if (type_can_define_types (child_die))
15713 dwarf2_add_type_defn (fi, child_die, cu);
15714 else if (child_die->tag == DW_TAG_template_type_param
15715 || child_die->tag == DW_TAG_template_value_param)
15716 {
15717 struct symbol *arg = new_symbol (child_die, NULL, cu);
15718
15719 if (arg != NULL)
15720 template_args->push_back (arg);
15721 }
15722 else if (child_die->tag == DW_TAG_variant_part)
15723 handle_variant_part (child_die, type, fi, template_args, cu);
15724 else if (child_die->tag == DW_TAG_variant)
15725 handle_variant (child_die, type, fi, template_args, cu);
15726 }
15727
15728 /* Finish creating a structure or union type, including filling in
15729 its members and creating a symbol for it. */
15730
15731 static void
15732 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15733 {
15734 struct objfile *objfile = cu->per_objfile->objfile;
15735 struct die_info *child_die;
15736 struct type *type;
15737
15738 type = get_die_type (die, cu);
15739 if (type == NULL)
15740 type = read_structure_type (die, cu);
15741
15742 bool has_template_parameters = false;
15743 if (die->child != NULL && ! die_is_declaration (die, cu))
15744 {
15745 struct field_info fi;
15746 std::vector<struct symbol *> template_args;
15747
15748 child_die = die->child;
15749
15750 while (child_die && child_die->tag)
15751 {
15752 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15753 child_die = child_die->sibling;
15754 }
15755
15756 /* Attach template arguments to type. */
15757 if (!template_args.empty ())
15758 {
15759 has_template_parameters = true;
15760 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15761 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15762 TYPE_TEMPLATE_ARGUMENTS (type)
15763 = XOBNEWVEC (&objfile->objfile_obstack,
15764 struct symbol *,
15765 TYPE_N_TEMPLATE_ARGUMENTS (type));
15766 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15767 template_args.data (),
15768 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15769 * sizeof (struct symbol *)));
15770 }
15771
15772 /* Attach fields and member functions to the type. */
15773 if (fi.nfields () > 0)
15774 dwarf2_attach_fields_to_type (&fi, type, cu);
15775 if (!fi.fnfieldlists.empty ())
15776 {
15777 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15778
15779 /* Get the type which refers to the base class (possibly this
15780 class itself) which contains the vtable pointer for the current
15781 class from the DW_AT_containing_type attribute. This use of
15782 DW_AT_containing_type is a GNU extension. */
15783
15784 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15785 {
15786 struct type *t = die_containing_type (die, cu);
15787
15788 set_type_vptr_basetype (type, t);
15789 if (type == t)
15790 {
15791 int i;
15792
15793 /* Our own class provides vtbl ptr. */
15794 for (i = t->num_fields () - 1;
15795 i >= TYPE_N_BASECLASSES (t);
15796 --i)
15797 {
15798 const char *fieldname = TYPE_FIELD_NAME (t, i);
15799
15800 if (is_vtable_name (fieldname, cu))
15801 {
15802 set_type_vptr_fieldno (type, i);
15803 break;
15804 }
15805 }
15806
15807 /* Complain if virtual function table field not found. */
15808 if (i < TYPE_N_BASECLASSES (t))
15809 complaint (_("virtual function table pointer "
15810 "not found when defining class '%s'"),
15811 type->name () ? type->name () : "");
15812 }
15813 else
15814 {
15815 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15816 }
15817 }
15818 else if (cu->producer
15819 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15820 {
15821 /* The IBM XLC compiler does not provide direct indication
15822 of the containing type, but the vtable pointer is
15823 always named __vfp. */
15824
15825 int i;
15826
15827 for (i = type->num_fields () - 1;
15828 i >= TYPE_N_BASECLASSES (type);
15829 --i)
15830 {
15831 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15832 {
15833 set_type_vptr_fieldno (type, i);
15834 set_type_vptr_basetype (type, type);
15835 break;
15836 }
15837 }
15838 }
15839 }
15840
15841 /* Copy fi.typedef_field_list linked list elements content into the
15842 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15843 if (!fi.typedef_field_list.empty ())
15844 {
15845 int count = fi.typedef_field_list.size ();
15846
15847 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15848 TYPE_TYPEDEF_FIELD_ARRAY (type)
15849 = ((struct decl_field *)
15850 TYPE_ALLOC (type,
15851 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15852 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15853
15854 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15855 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15856 }
15857
15858 /* Copy fi.nested_types_list linked list elements content into the
15859 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15860 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15861 {
15862 int count = fi.nested_types_list.size ();
15863
15864 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15865 TYPE_NESTED_TYPES_ARRAY (type)
15866 = ((struct decl_field *)
15867 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15868 TYPE_NESTED_TYPES_COUNT (type) = count;
15869
15870 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15871 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15872 }
15873 }
15874
15875 quirk_gcc_member_function_pointer (type, objfile);
15876 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15877 cu->rust_unions.push_back (type);
15878
15879 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15880 snapshots) has been known to create a die giving a declaration
15881 for a class that has, as a child, a die giving a definition for a
15882 nested class. So we have to process our children even if the
15883 current die is a declaration. Normally, of course, a declaration
15884 won't have any children at all. */
15885
15886 child_die = die->child;
15887
15888 while (child_die != NULL && child_die->tag)
15889 {
15890 if (child_die->tag == DW_TAG_member
15891 || child_die->tag == DW_TAG_variable
15892 || child_die->tag == DW_TAG_inheritance
15893 || child_die->tag == DW_TAG_template_value_param
15894 || child_die->tag == DW_TAG_template_type_param)
15895 {
15896 /* Do nothing. */
15897 }
15898 else
15899 process_die (child_die, cu);
15900
15901 child_die = child_die->sibling;
15902 }
15903
15904 /* Do not consider external references. According to the DWARF standard,
15905 these DIEs are identified by the fact that they have no byte_size
15906 attribute, and a declaration attribute. */
15907 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15908 || !die_is_declaration (die, cu)
15909 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15910 {
15911 struct symbol *sym = new_symbol (die, type, cu);
15912
15913 if (has_template_parameters)
15914 {
15915 struct symtab *symtab;
15916 if (sym != nullptr)
15917 symtab = symbol_symtab (sym);
15918 else if (cu->line_header != nullptr)
15919 {
15920 /* Any related symtab will do. */
15921 symtab
15922 = cu->line_header->file_names ()[0].symtab;
15923 }
15924 else
15925 {
15926 symtab = nullptr;
15927 complaint (_("could not find suitable "
15928 "symtab for template parameter"
15929 " - DIE at %s [in module %s]"),
15930 sect_offset_str (die->sect_off),
15931 objfile_name (objfile));
15932 }
15933
15934 if (symtab != nullptr)
15935 {
15936 /* Make sure that the symtab is set on the new symbols.
15937 Even though they don't appear in this symtab directly,
15938 other parts of gdb assume that symbols do, and this is
15939 reasonably true. */
15940 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15941 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15942 }
15943 }
15944 }
15945 }
15946
15947 /* Assuming DIE is an enumeration type, and TYPE is its associated
15948 type, update TYPE using some information only available in DIE's
15949 children. In particular, the fields are computed. */
15950
15951 static void
15952 update_enumeration_type_from_children (struct die_info *die,
15953 struct type *type,
15954 struct dwarf2_cu *cu)
15955 {
15956 struct die_info *child_die;
15957 int unsigned_enum = 1;
15958 int flag_enum = 1;
15959
15960 auto_obstack obstack;
15961 std::vector<struct field> fields;
15962
15963 for (child_die = die->child;
15964 child_die != NULL && child_die->tag;
15965 child_die = child_die->sibling)
15966 {
15967 struct attribute *attr;
15968 LONGEST value;
15969 const gdb_byte *bytes;
15970 struct dwarf2_locexpr_baton *baton;
15971 const char *name;
15972
15973 if (child_die->tag != DW_TAG_enumerator)
15974 continue;
15975
15976 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15977 if (attr == NULL)
15978 continue;
15979
15980 name = dwarf2_name (child_die, cu);
15981 if (name == NULL)
15982 name = "<anonymous enumerator>";
15983
15984 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15985 &value, &bytes, &baton);
15986 if (value < 0)
15987 {
15988 unsigned_enum = 0;
15989 flag_enum = 0;
15990 }
15991 else
15992 {
15993 if (count_one_bits_ll (value) >= 2)
15994 flag_enum = 0;
15995 }
15996
15997 fields.emplace_back ();
15998 struct field &field = fields.back ();
15999 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16000 SET_FIELD_ENUMVAL (field, value);
16001 }
16002
16003 if (!fields.empty ())
16004 {
16005 type->set_num_fields (fields.size ());
16006 type->set_fields
16007 ((struct field *)
16008 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16009 memcpy (type->fields (), fields.data (),
16010 sizeof (struct field) * fields.size ());
16011 }
16012
16013 if (unsigned_enum)
16014 TYPE_UNSIGNED (type) = 1;
16015 if (flag_enum)
16016 TYPE_FLAG_ENUM (type) = 1;
16017 }
16018
16019 /* Given a DW_AT_enumeration_type die, set its type. We do not
16020 complete the type's fields yet, or create any symbols. */
16021
16022 static struct type *
16023 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16024 {
16025 struct objfile *objfile = cu->per_objfile->objfile;
16026 struct type *type;
16027 struct attribute *attr;
16028 const char *name;
16029
16030 /* If the definition of this type lives in .debug_types, read that type.
16031 Don't follow DW_AT_specification though, that will take us back up
16032 the chain and we want to go down. */
16033 attr = die->attr (DW_AT_signature);
16034 if (attr != nullptr)
16035 {
16036 type = get_DW_AT_signature_type (die, attr, cu);
16037
16038 /* The type's CU may not be the same as CU.
16039 Ensure TYPE is recorded with CU in die_type_hash. */
16040 return set_die_type (die, type, cu);
16041 }
16042
16043 type = alloc_type (objfile);
16044
16045 type->set_code (TYPE_CODE_ENUM);
16046 name = dwarf2_full_name (NULL, die, cu);
16047 if (name != NULL)
16048 type->set_name (name);
16049
16050 attr = dwarf2_attr (die, DW_AT_type, cu);
16051 if (attr != NULL)
16052 {
16053 struct type *underlying_type = die_type (die, cu);
16054
16055 TYPE_TARGET_TYPE (type) = underlying_type;
16056 }
16057
16058 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16059 if (attr != nullptr)
16060 {
16061 TYPE_LENGTH (type) = DW_UNSND (attr);
16062 }
16063 else
16064 {
16065 TYPE_LENGTH (type) = 0;
16066 }
16067
16068 maybe_set_alignment (cu, die, type);
16069
16070 /* The enumeration DIE can be incomplete. In Ada, any type can be
16071 declared as private in the package spec, and then defined only
16072 inside the package body. Such types are known as Taft Amendment
16073 Types. When another package uses such a type, an incomplete DIE
16074 may be generated by the compiler. */
16075 if (die_is_declaration (die, cu))
16076 TYPE_STUB (type) = 1;
16077
16078 /* If this type has an underlying type that is not a stub, then we
16079 may use its attributes. We always use the "unsigned" attribute
16080 in this situation, because ordinarily we guess whether the type
16081 is unsigned -- but the guess can be wrong and the underlying type
16082 can tell us the reality. However, we defer to a local size
16083 attribute if one exists, because this lets the compiler override
16084 the underlying type if needed. */
16085 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16086 {
16087 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16088 underlying_type = check_typedef (underlying_type);
16089 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16090 if (TYPE_LENGTH (type) == 0)
16091 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16092 if (TYPE_RAW_ALIGN (type) == 0
16093 && TYPE_RAW_ALIGN (underlying_type) != 0)
16094 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16095 }
16096
16097 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16098
16099 set_die_type (die, type, cu);
16100
16101 /* Finish the creation of this type by using the enum's children.
16102 Note that, as usual, this must come after set_die_type to avoid
16103 infinite recursion when trying to compute the names of the
16104 enumerators. */
16105 update_enumeration_type_from_children (die, type, cu);
16106
16107 return type;
16108 }
16109
16110 /* Given a pointer to a die which begins an enumeration, process all
16111 the dies that define the members of the enumeration, and create the
16112 symbol for the enumeration type.
16113
16114 NOTE: We reverse the order of the element list. */
16115
16116 static void
16117 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16118 {
16119 struct type *this_type;
16120
16121 this_type = get_die_type (die, cu);
16122 if (this_type == NULL)
16123 this_type = read_enumeration_type (die, cu);
16124
16125 if (die->child != NULL)
16126 {
16127 struct die_info *child_die;
16128 const char *name;
16129
16130 child_die = die->child;
16131 while (child_die && child_die->tag)
16132 {
16133 if (child_die->tag != DW_TAG_enumerator)
16134 {
16135 process_die (child_die, cu);
16136 }
16137 else
16138 {
16139 name = dwarf2_name (child_die, cu);
16140 if (name)
16141 new_symbol (child_die, this_type, cu);
16142 }
16143
16144 child_die = child_die->sibling;
16145 }
16146 }
16147
16148 /* If we are reading an enum from a .debug_types unit, and the enum
16149 is a declaration, and the enum is not the signatured type in the
16150 unit, then we do not want to add a symbol for it. Adding a
16151 symbol would in some cases obscure the true definition of the
16152 enum, giving users an incomplete type when the definition is
16153 actually available. Note that we do not want to do this for all
16154 enums which are just declarations, because C++0x allows forward
16155 enum declarations. */
16156 if (cu->per_cu->is_debug_types
16157 && die_is_declaration (die, cu))
16158 {
16159 struct signatured_type *sig_type;
16160
16161 sig_type = (struct signatured_type *) cu->per_cu;
16162 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16163 if (sig_type->type_offset_in_section != die->sect_off)
16164 return;
16165 }
16166
16167 new_symbol (die, this_type, cu);
16168 }
16169
16170 /* Extract all information from a DW_TAG_array_type DIE and put it in
16171 the DIE's type field. For now, this only handles one dimensional
16172 arrays. */
16173
16174 static struct type *
16175 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16176 {
16177 struct objfile *objfile = cu->per_objfile->objfile;
16178 struct die_info *child_die;
16179 struct type *type;
16180 struct type *element_type, *range_type, *index_type;
16181 struct attribute *attr;
16182 const char *name;
16183 struct dynamic_prop *byte_stride_prop = NULL;
16184 unsigned int bit_stride = 0;
16185
16186 element_type = die_type (die, cu);
16187
16188 /* The die_type call above may have already set the type for this DIE. */
16189 type = get_die_type (die, cu);
16190 if (type)
16191 return type;
16192
16193 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16194 if (attr != NULL)
16195 {
16196 int stride_ok;
16197 struct type *prop_type = cu->addr_sized_int_type (false);
16198
16199 byte_stride_prop
16200 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16201 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16202 prop_type);
16203 if (!stride_ok)
16204 {
16205 complaint (_("unable to read array DW_AT_byte_stride "
16206 " - DIE at %s [in module %s]"),
16207 sect_offset_str (die->sect_off),
16208 objfile_name (cu->per_objfile->objfile));
16209 /* Ignore this attribute. We will likely not be able to print
16210 arrays of this type correctly, but there is little we can do
16211 to help if we cannot read the attribute's value. */
16212 byte_stride_prop = NULL;
16213 }
16214 }
16215
16216 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16217 if (attr != NULL)
16218 bit_stride = DW_UNSND (attr);
16219
16220 /* Irix 6.2 native cc creates array types without children for
16221 arrays with unspecified length. */
16222 if (die->child == NULL)
16223 {
16224 index_type = objfile_type (objfile)->builtin_int;
16225 range_type = create_static_range_type (NULL, index_type, 0, -1);
16226 type = create_array_type_with_stride (NULL, element_type, range_type,
16227 byte_stride_prop, bit_stride);
16228 return set_die_type (die, type, cu);
16229 }
16230
16231 std::vector<struct type *> range_types;
16232 child_die = die->child;
16233 while (child_die && child_die->tag)
16234 {
16235 if (child_die->tag == DW_TAG_subrange_type)
16236 {
16237 struct type *child_type = read_type_die (child_die, cu);
16238
16239 if (child_type != NULL)
16240 {
16241 /* The range type was succesfully read. Save it for the
16242 array type creation. */
16243 range_types.push_back (child_type);
16244 }
16245 }
16246 child_die = child_die->sibling;
16247 }
16248
16249 /* Dwarf2 dimensions are output from left to right, create the
16250 necessary array types in backwards order. */
16251
16252 type = element_type;
16253
16254 if (read_array_order (die, cu) == DW_ORD_col_major)
16255 {
16256 int i = 0;
16257
16258 while (i < range_types.size ())
16259 type = create_array_type_with_stride (NULL, type, range_types[i++],
16260 byte_stride_prop, bit_stride);
16261 }
16262 else
16263 {
16264 size_t ndim = range_types.size ();
16265 while (ndim-- > 0)
16266 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16267 byte_stride_prop, bit_stride);
16268 }
16269
16270 /* Understand Dwarf2 support for vector types (like they occur on
16271 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16272 array type. This is not part of the Dwarf2/3 standard yet, but a
16273 custom vendor extension. The main difference between a regular
16274 array and the vector variant is that vectors are passed by value
16275 to functions. */
16276 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16277 if (attr != nullptr)
16278 make_vector_type (type);
16279
16280 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16281 implementation may choose to implement triple vectors using this
16282 attribute. */
16283 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16284 if (attr != nullptr)
16285 {
16286 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16287 TYPE_LENGTH (type) = DW_UNSND (attr);
16288 else
16289 complaint (_("DW_AT_byte_size for array type smaller "
16290 "than the total size of elements"));
16291 }
16292
16293 name = dwarf2_name (die, cu);
16294 if (name)
16295 type->set_name (name);
16296
16297 maybe_set_alignment (cu, die, type);
16298
16299 /* Install the type in the die. */
16300 set_die_type (die, type, cu);
16301
16302 /* set_die_type should be already done. */
16303 set_descriptive_type (type, die, cu);
16304
16305 return type;
16306 }
16307
16308 static enum dwarf_array_dim_ordering
16309 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16310 {
16311 struct attribute *attr;
16312
16313 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16314
16315 if (attr != nullptr)
16316 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16317
16318 /* GNU F77 is a special case, as at 08/2004 array type info is the
16319 opposite order to the dwarf2 specification, but data is still
16320 laid out as per normal fortran.
16321
16322 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16323 version checking. */
16324
16325 if (cu->language == language_fortran
16326 && cu->producer && strstr (cu->producer, "GNU F77"))
16327 {
16328 return DW_ORD_row_major;
16329 }
16330
16331 switch (cu->language_defn->la_array_ordering)
16332 {
16333 case array_column_major:
16334 return DW_ORD_col_major;
16335 case array_row_major:
16336 default:
16337 return DW_ORD_row_major;
16338 };
16339 }
16340
16341 /* Extract all information from a DW_TAG_set_type DIE and put it in
16342 the DIE's type field. */
16343
16344 static struct type *
16345 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16346 {
16347 struct type *domain_type, *set_type;
16348 struct attribute *attr;
16349
16350 domain_type = die_type (die, cu);
16351
16352 /* The die_type call above may have already set the type for this DIE. */
16353 set_type = get_die_type (die, cu);
16354 if (set_type)
16355 return set_type;
16356
16357 set_type = create_set_type (NULL, domain_type);
16358
16359 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16360 if (attr != nullptr)
16361 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16362
16363 maybe_set_alignment (cu, die, set_type);
16364
16365 return set_die_type (die, set_type, cu);
16366 }
16367
16368 /* A helper for read_common_block that creates a locexpr baton.
16369 SYM is the symbol which we are marking as computed.
16370 COMMON_DIE is the DIE for the common block.
16371 COMMON_LOC is the location expression attribute for the common
16372 block itself.
16373 MEMBER_LOC is the location expression attribute for the particular
16374 member of the common block that we are processing.
16375 CU is the CU from which the above come. */
16376
16377 static void
16378 mark_common_block_symbol_computed (struct symbol *sym,
16379 struct die_info *common_die,
16380 struct attribute *common_loc,
16381 struct attribute *member_loc,
16382 struct dwarf2_cu *cu)
16383 {
16384 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16385 struct objfile *objfile = per_objfile->objfile;
16386 struct dwarf2_locexpr_baton *baton;
16387 gdb_byte *ptr;
16388 unsigned int cu_off;
16389 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16390 LONGEST offset = 0;
16391
16392 gdb_assert (common_loc && member_loc);
16393 gdb_assert (common_loc->form_is_block ());
16394 gdb_assert (member_loc->form_is_block ()
16395 || member_loc->form_is_constant ());
16396
16397 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16398 baton->per_objfile = per_objfile;
16399 baton->per_cu = cu->per_cu;
16400 gdb_assert (baton->per_cu);
16401
16402 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16403
16404 if (member_loc->form_is_constant ())
16405 {
16406 offset = member_loc->constant_value (0);
16407 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16408 }
16409 else
16410 baton->size += DW_BLOCK (member_loc)->size;
16411
16412 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16413 baton->data = ptr;
16414
16415 *ptr++ = DW_OP_call4;
16416 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16417 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16418 ptr += 4;
16419
16420 if (member_loc->form_is_constant ())
16421 {
16422 *ptr++ = DW_OP_addr;
16423 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16424 ptr += cu->header.addr_size;
16425 }
16426 else
16427 {
16428 /* We have to copy the data here, because DW_OP_call4 will only
16429 use a DW_AT_location attribute. */
16430 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16431 ptr += DW_BLOCK (member_loc)->size;
16432 }
16433
16434 *ptr++ = DW_OP_plus;
16435 gdb_assert (ptr - baton->data == baton->size);
16436
16437 SYMBOL_LOCATION_BATON (sym) = baton;
16438 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16439 }
16440
16441 /* Create appropriate locally-scoped variables for all the
16442 DW_TAG_common_block entries. Also create a struct common_block
16443 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16444 is used to separate the common blocks name namespace from regular
16445 variable names. */
16446
16447 static void
16448 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16449 {
16450 struct attribute *attr;
16451
16452 attr = dwarf2_attr (die, DW_AT_location, cu);
16453 if (attr != nullptr)
16454 {
16455 /* Support the .debug_loc offsets. */
16456 if (attr->form_is_block ())
16457 {
16458 /* Ok. */
16459 }
16460 else if (attr->form_is_section_offset ())
16461 {
16462 dwarf2_complex_location_expr_complaint ();
16463 attr = NULL;
16464 }
16465 else
16466 {
16467 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16468 "common block member");
16469 attr = NULL;
16470 }
16471 }
16472
16473 if (die->child != NULL)
16474 {
16475 struct objfile *objfile = cu->per_objfile->objfile;
16476 struct die_info *child_die;
16477 size_t n_entries = 0, size;
16478 struct common_block *common_block;
16479 struct symbol *sym;
16480
16481 for (child_die = die->child;
16482 child_die && child_die->tag;
16483 child_die = child_die->sibling)
16484 ++n_entries;
16485
16486 size = (sizeof (struct common_block)
16487 + (n_entries - 1) * sizeof (struct symbol *));
16488 common_block
16489 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16490 size);
16491 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16492 common_block->n_entries = 0;
16493
16494 for (child_die = die->child;
16495 child_die && child_die->tag;
16496 child_die = child_die->sibling)
16497 {
16498 /* Create the symbol in the DW_TAG_common_block block in the current
16499 symbol scope. */
16500 sym = new_symbol (child_die, NULL, cu);
16501 if (sym != NULL)
16502 {
16503 struct attribute *member_loc;
16504
16505 common_block->contents[common_block->n_entries++] = sym;
16506
16507 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16508 cu);
16509 if (member_loc)
16510 {
16511 /* GDB has handled this for a long time, but it is
16512 not specified by DWARF. It seems to have been
16513 emitted by gfortran at least as recently as:
16514 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16515 complaint (_("Variable in common block has "
16516 "DW_AT_data_member_location "
16517 "- DIE at %s [in module %s]"),
16518 sect_offset_str (child_die->sect_off),
16519 objfile_name (objfile));
16520
16521 if (member_loc->form_is_section_offset ())
16522 dwarf2_complex_location_expr_complaint ();
16523 else if (member_loc->form_is_constant ()
16524 || member_loc->form_is_block ())
16525 {
16526 if (attr != nullptr)
16527 mark_common_block_symbol_computed (sym, die, attr,
16528 member_loc, cu);
16529 }
16530 else
16531 dwarf2_complex_location_expr_complaint ();
16532 }
16533 }
16534 }
16535
16536 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16537 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16538 }
16539 }
16540
16541 /* Create a type for a C++ namespace. */
16542
16543 static struct type *
16544 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16545 {
16546 struct objfile *objfile = cu->per_objfile->objfile;
16547 const char *previous_prefix, *name;
16548 int is_anonymous;
16549 struct type *type;
16550
16551 /* For extensions, reuse the type of the original namespace. */
16552 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16553 {
16554 struct die_info *ext_die;
16555 struct dwarf2_cu *ext_cu = cu;
16556
16557 ext_die = dwarf2_extension (die, &ext_cu);
16558 type = read_type_die (ext_die, ext_cu);
16559
16560 /* EXT_CU may not be the same as CU.
16561 Ensure TYPE is recorded with CU in die_type_hash. */
16562 return set_die_type (die, type, cu);
16563 }
16564
16565 name = namespace_name (die, &is_anonymous, cu);
16566
16567 /* Now build the name of the current namespace. */
16568
16569 previous_prefix = determine_prefix (die, cu);
16570 if (previous_prefix[0] != '\0')
16571 name = typename_concat (&objfile->objfile_obstack,
16572 previous_prefix, name, 0, cu);
16573
16574 /* Create the type. */
16575 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16576
16577 return set_die_type (die, type, cu);
16578 }
16579
16580 /* Read a namespace scope. */
16581
16582 static void
16583 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16584 {
16585 struct objfile *objfile = cu->per_objfile->objfile;
16586 int is_anonymous;
16587
16588 /* Add a symbol associated to this if we haven't seen the namespace
16589 before. Also, add a using directive if it's an anonymous
16590 namespace. */
16591
16592 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16593 {
16594 struct type *type;
16595
16596 type = read_type_die (die, cu);
16597 new_symbol (die, type, cu);
16598
16599 namespace_name (die, &is_anonymous, cu);
16600 if (is_anonymous)
16601 {
16602 const char *previous_prefix = determine_prefix (die, cu);
16603
16604 std::vector<const char *> excludes;
16605 add_using_directive (using_directives (cu),
16606 previous_prefix, type->name (), NULL,
16607 NULL, excludes, 0, &objfile->objfile_obstack);
16608 }
16609 }
16610
16611 if (die->child != NULL)
16612 {
16613 struct die_info *child_die = die->child;
16614
16615 while (child_die && child_die->tag)
16616 {
16617 process_die (child_die, cu);
16618 child_die = child_die->sibling;
16619 }
16620 }
16621 }
16622
16623 /* Read a Fortran module as type. This DIE can be only a declaration used for
16624 imported module. Still we need that type as local Fortran "use ... only"
16625 declaration imports depend on the created type in determine_prefix. */
16626
16627 static struct type *
16628 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16629 {
16630 struct objfile *objfile = cu->per_objfile->objfile;
16631 const char *module_name;
16632 struct type *type;
16633
16634 module_name = dwarf2_name (die, cu);
16635 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16636
16637 return set_die_type (die, type, cu);
16638 }
16639
16640 /* Read a Fortran module. */
16641
16642 static void
16643 read_module (struct die_info *die, struct dwarf2_cu *cu)
16644 {
16645 struct die_info *child_die = die->child;
16646 struct type *type;
16647
16648 type = read_type_die (die, cu);
16649 new_symbol (die, type, cu);
16650
16651 while (child_die && child_die->tag)
16652 {
16653 process_die (child_die, cu);
16654 child_die = child_die->sibling;
16655 }
16656 }
16657
16658 /* Return the name of the namespace represented by DIE. Set
16659 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16660 namespace. */
16661
16662 static const char *
16663 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16664 {
16665 struct die_info *current_die;
16666 const char *name = NULL;
16667
16668 /* Loop through the extensions until we find a name. */
16669
16670 for (current_die = die;
16671 current_die != NULL;
16672 current_die = dwarf2_extension (die, &cu))
16673 {
16674 /* We don't use dwarf2_name here so that we can detect the absence
16675 of a name -> anonymous namespace. */
16676 name = dwarf2_string_attr (die, DW_AT_name, cu);
16677
16678 if (name != NULL)
16679 break;
16680 }
16681
16682 /* Is it an anonymous namespace? */
16683
16684 *is_anonymous = (name == NULL);
16685 if (*is_anonymous)
16686 name = CP_ANONYMOUS_NAMESPACE_STR;
16687
16688 return name;
16689 }
16690
16691 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16692 the user defined type vector. */
16693
16694 static struct type *
16695 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16696 {
16697 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16698 struct comp_unit_head *cu_header = &cu->header;
16699 struct type *type;
16700 struct attribute *attr_byte_size;
16701 struct attribute *attr_address_class;
16702 int byte_size, addr_class;
16703 struct type *target_type;
16704
16705 target_type = die_type (die, cu);
16706
16707 /* The die_type call above may have already set the type for this DIE. */
16708 type = get_die_type (die, cu);
16709 if (type)
16710 return type;
16711
16712 type = lookup_pointer_type (target_type);
16713
16714 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16715 if (attr_byte_size)
16716 byte_size = DW_UNSND (attr_byte_size);
16717 else
16718 byte_size = cu_header->addr_size;
16719
16720 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16721 if (attr_address_class)
16722 addr_class = DW_UNSND (attr_address_class);
16723 else
16724 addr_class = DW_ADDR_none;
16725
16726 ULONGEST alignment = get_alignment (cu, die);
16727
16728 /* If the pointer size, alignment, or address class is different
16729 than the default, create a type variant marked as such and set
16730 the length accordingly. */
16731 if (TYPE_LENGTH (type) != byte_size
16732 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16733 && alignment != TYPE_RAW_ALIGN (type))
16734 || addr_class != DW_ADDR_none)
16735 {
16736 if (gdbarch_address_class_type_flags_p (gdbarch))
16737 {
16738 int type_flags;
16739
16740 type_flags = gdbarch_address_class_type_flags
16741 (gdbarch, byte_size, addr_class);
16742 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16743 == 0);
16744 type = make_type_with_address_space (type, type_flags);
16745 }
16746 else if (TYPE_LENGTH (type) != byte_size)
16747 {
16748 complaint (_("invalid pointer size %d"), byte_size);
16749 }
16750 else if (TYPE_RAW_ALIGN (type) != alignment)
16751 {
16752 complaint (_("Invalid DW_AT_alignment"
16753 " - DIE at %s [in module %s]"),
16754 sect_offset_str (die->sect_off),
16755 objfile_name (cu->per_objfile->objfile));
16756 }
16757 else
16758 {
16759 /* Should we also complain about unhandled address classes? */
16760 }
16761 }
16762
16763 TYPE_LENGTH (type) = byte_size;
16764 set_type_align (type, alignment);
16765 return set_die_type (die, type, cu);
16766 }
16767
16768 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16769 the user defined type vector. */
16770
16771 static struct type *
16772 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16773 {
16774 struct type *type;
16775 struct type *to_type;
16776 struct type *domain;
16777
16778 to_type = die_type (die, cu);
16779 domain = die_containing_type (die, cu);
16780
16781 /* The calls above may have already set the type for this DIE. */
16782 type = get_die_type (die, cu);
16783 if (type)
16784 return type;
16785
16786 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16787 type = lookup_methodptr_type (to_type);
16788 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16789 {
16790 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16791
16792 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16793 to_type->fields (), to_type->num_fields (),
16794 TYPE_VARARGS (to_type));
16795 type = lookup_methodptr_type (new_type);
16796 }
16797 else
16798 type = lookup_memberptr_type (to_type, domain);
16799
16800 return set_die_type (die, type, cu);
16801 }
16802
16803 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16804 the user defined type vector. */
16805
16806 static struct type *
16807 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16808 enum type_code refcode)
16809 {
16810 struct comp_unit_head *cu_header = &cu->header;
16811 struct type *type, *target_type;
16812 struct attribute *attr;
16813
16814 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16815
16816 target_type = die_type (die, cu);
16817
16818 /* The die_type call above may have already set the type for this DIE. */
16819 type = get_die_type (die, cu);
16820 if (type)
16821 return type;
16822
16823 type = lookup_reference_type (target_type, refcode);
16824 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16825 if (attr != nullptr)
16826 {
16827 TYPE_LENGTH (type) = DW_UNSND (attr);
16828 }
16829 else
16830 {
16831 TYPE_LENGTH (type) = cu_header->addr_size;
16832 }
16833 maybe_set_alignment (cu, die, type);
16834 return set_die_type (die, type, cu);
16835 }
16836
16837 /* Add the given cv-qualifiers to the element type of the array. GCC
16838 outputs DWARF type qualifiers that apply to an array, not the
16839 element type. But GDB relies on the array element type to carry
16840 the cv-qualifiers. This mimics section 6.7.3 of the C99
16841 specification. */
16842
16843 static struct type *
16844 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16845 struct type *base_type, int cnst, int voltl)
16846 {
16847 struct type *el_type, *inner_array;
16848
16849 base_type = copy_type (base_type);
16850 inner_array = base_type;
16851
16852 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16853 {
16854 TYPE_TARGET_TYPE (inner_array) =
16855 copy_type (TYPE_TARGET_TYPE (inner_array));
16856 inner_array = TYPE_TARGET_TYPE (inner_array);
16857 }
16858
16859 el_type = TYPE_TARGET_TYPE (inner_array);
16860 cnst |= TYPE_CONST (el_type);
16861 voltl |= TYPE_VOLATILE (el_type);
16862 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16863
16864 return set_die_type (die, base_type, cu);
16865 }
16866
16867 static struct type *
16868 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16869 {
16870 struct type *base_type, *cv_type;
16871
16872 base_type = die_type (die, cu);
16873
16874 /* The die_type call above may have already set the type for this DIE. */
16875 cv_type = get_die_type (die, cu);
16876 if (cv_type)
16877 return cv_type;
16878
16879 /* In case the const qualifier is applied to an array type, the element type
16880 is so qualified, not the array type (section 6.7.3 of C99). */
16881 if (base_type->code () == TYPE_CODE_ARRAY)
16882 return add_array_cv_type (die, cu, base_type, 1, 0);
16883
16884 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16885 return set_die_type (die, cv_type, cu);
16886 }
16887
16888 static struct type *
16889 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16890 {
16891 struct type *base_type, *cv_type;
16892
16893 base_type = die_type (die, cu);
16894
16895 /* The die_type call above may have already set the type for this DIE. */
16896 cv_type = get_die_type (die, cu);
16897 if (cv_type)
16898 return cv_type;
16899
16900 /* In case the volatile qualifier is applied to an array type, the
16901 element type is so qualified, not the array type (section 6.7.3
16902 of C99). */
16903 if (base_type->code () == TYPE_CODE_ARRAY)
16904 return add_array_cv_type (die, cu, base_type, 0, 1);
16905
16906 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16907 return set_die_type (die, cv_type, cu);
16908 }
16909
16910 /* Handle DW_TAG_restrict_type. */
16911
16912 static struct type *
16913 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16914 {
16915 struct type *base_type, *cv_type;
16916
16917 base_type = die_type (die, cu);
16918
16919 /* The die_type call above may have already set the type for this DIE. */
16920 cv_type = get_die_type (die, cu);
16921 if (cv_type)
16922 return cv_type;
16923
16924 cv_type = make_restrict_type (base_type);
16925 return set_die_type (die, cv_type, cu);
16926 }
16927
16928 /* Handle DW_TAG_atomic_type. */
16929
16930 static struct type *
16931 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16932 {
16933 struct type *base_type, *cv_type;
16934
16935 base_type = die_type (die, cu);
16936
16937 /* The die_type call above may have already set the type for this DIE. */
16938 cv_type = get_die_type (die, cu);
16939 if (cv_type)
16940 return cv_type;
16941
16942 cv_type = make_atomic_type (base_type);
16943 return set_die_type (die, cv_type, cu);
16944 }
16945
16946 /* Extract all information from a DW_TAG_string_type DIE and add to
16947 the user defined type vector. It isn't really a user defined type,
16948 but it behaves like one, with other DIE's using an AT_user_def_type
16949 attribute to reference it. */
16950
16951 static struct type *
16952 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16953 {
16954 struct objfile *objfile = cu->per_objfile->objfile;
16955 struct gdbarch *gdbarch = objfile->arch ();
16956 struct type *type, *range_type, *index_type, *char_type;
16957 struct attribute *attr;
16958 struct dynamic_prop prop;
16959 bool length_is_constant = true;
16960 LONGEST length;
16961
16962 /* There are a couple of places where bit sizes might be made use of
16963 when parsing a DW_TAG_string_type, however, no producer that we know
16964 of make use of these. Handling bit sizes that are a multiple of the
16965 byte size is easy enough, but what about other bit sizes? Lets deal
16966 with that problem when we have to. Warn about these attributes being
16967 unsupported, then parse the type and ignore them like we always
16968 have. */
16969 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16970 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16971 {
16972 static bool warning_printed = false;
16973 if (!warning_printed)
16974 {
16975 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16976 "currently supported on DW_TAG_string_type."));
16977 warning_printed = true;
16978 }
16979 }
16980
16981 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16982 if (attr != nullptr && !attr->form_is_constant ())
16983 {
16984 /* The string length describes the location at which the length of
16985 the string can be found. The size of the length field can be
16986 specified with one of the attributes below. */
16987 struct type *prop_type;
16988 struct attribute *len
16989 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16990 if (len == nullptr)
16991 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16992 if (len != nullptr && len->form_is_constant ())
16993 {
16994 /* Pass 0 as the default as we know this attribute is constant
16995 and the default value will not be returned. */
16996 LONGEST sz = len->constant_value (0);
16997 prop_type = cu->per_objfile->int_type (sz, true);
16998 }
16999 else
17000 {
17001 /* If the size is not specified then we assume it is the size of
17002 an address on this target. */
17003 prop_type = cu->addr_sized_int_type (true);
17004 }
17005
17006 /* Convert the attribute into a dynamic property. */
17007 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17008 length = 1;
17009 else
17010 length_is_constant = false;
17011 }
17012 else if (attr != nullptr)
17013 {
17014 /* This DW_AT_string_length just contains the length with no
17015 indirection. There's no need to create a dynamic property in this
17016 case. Pass 0 for the default value as we know it will not be
17017 returned in this case. */
17018 length = attr->constant_value (0);
17019 }
17020 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17021 {
17022 /* We don't currently support non-constant byte sizes for strings. */
17023 length = attr->constant_value (1);
17024 }
17025 else
17026 {
17027 /* Use 1 as a fallback length if we have nothing else. */
17028 length = 1;
17029 }
17030
17031 index_type = objfile_type (objfile)->builtin_int;
17032 if (length_is_constant)
17033 range_type = create_static_range_type (NULL, index_type, 1, length);
17034 else
17035 {
17036 struct dynamic_prop low_bound;
17037
17038 low_bound.kind = PROP_CONST;
17039 low_bound.data.const_val = 1;
17040 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17041 }
17042 char_type = language_string_char_type (cu->language_defn, gdbarch);
17043 type = create_string_type (NULL, char_type, range_type);
17044
17045 return set_die_type (die, type, cu);
17046 }
17047
17048 /* Assuming that DIE corresponds to a function, returns nonzero
17049 if the function is prototyped. */
17050
17051 static int
17052 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17053 {
17054 struct attribute *attr;
17055
17056 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17057 if (attr && (DW_UNSND (attr) != 0))
17058 return 1;
17059
17060 /* The DWARF standard implies that the DW_AT_prototyped attribute
17061 is only meaningful for C, but the concept also extends to other
17062 languages that allow unprototyped functions (Eg: Objective C).
17063 For all other languages, assume that functions are always
17064 prototyped. */
17065 if (cu->language != language_c
17066 && cu->language != language_objc
17067 && cu->language != language_opencl)
17068 return 1;
17069
17070 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17071 prototyped and unprototyped functions; default to prototyped,
17072 since that is more common in modern code (and RealView warns
17073 about unprototyped functions). */
17074 if (producer_is_realview (cu->producer))
17075 return 1;
17076
17077 return 0;
17078 }
17079
17080 /* Handle DIES due to C code like:
17081
17082 struct foo
17083 {
17084 int (*funcp)(int a, long l);
17085 int b;
17086 };
17087
17088 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17089
17090 static struct type *
17091 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17092 {
17093 struct objfile *objfile = cu->per_objfile->objfile;
17094 struct type *type; /* Type that this function returns. */
17095 struct type *ftype; /* Function that returns above type. */
17096 struct attribute *attr;
17097
17098 type = die_type (die, cu);
17099
17100 /* The die_type call above may have already set the type for this DIE. */
17101 ftype = get_die_type (die, cu);
17102 if (ftype)
17103 return ftype;
17104
17105 ftype = lookup_function_type (type);
17106
17107 if (prototyped_function_p (die, cu))
17108 TYPE_PROTOTYPED (ftype) = 1;
17109
17110 /* Store the calling convention in the type if it's available in
17111 the subroutine die. Otherwise set the calling convention to
17112 the default value DW_CC_normal. */
17113 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17114 if (attr != nullptr
17115 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17116 TYPE_CALLING_CONVENTION (ftype)
17117 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17118 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17119 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17120 else
17121 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17122
17123 /* Record whether the function returns normally to its caller or not
17124 if the DWARF producer set that information. */
17125 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17126 if (attr && (DW_UNSND (attr) != 0))
17127 TYPE_NO_RETURN (ftype) = 1;
17128
17129 /* We need to add the subroutine type to the die immediately so
17130 we don't infinitely recurse when dealing with parameters
17131 declared as the same subroutine type. */
17132 set_die_type (die, ftype, cu);
17133
17134 if (die->child != NULL)
17135 {
17136 struct type *void_type = objfile_type (objfile)->builtin_void;
17137 struct die_info *child_die;
17138 int nparams, iparams;
17139
17140 /* Count the number of parameters.
17141 FIXME: GDB currently ignores vararg functions, but knows about
17142 vararg member functions. */
17143 nparams = 0;
17144 child_die = die->child;
17145 while (child_die && child_die->tag)
17146 {
17147 if (child_die->tag == DW_TAG_formal_parameter)
17148 nparams++;
17149 else if (child_die->tag == DW_TAG_unspecified_parameters)
17150 TYPE_VARARGS (ftype) = 1;
17151 child_die = child_die->sibling;
17152 }
17153
17154 /* Allocate storage for parameters and fill them in. */
17155 ftype->set_num_fields (nparams);
17156 ftype->set_fields
17157 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17158
17159 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17160 even if we error out during the parameters reading below. */
17161 for (iparams = 0; iparams < nparams; iparams++)
17162 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17163
17164 iparams = 0;
17165 child_die = die->child;
17166 while (child_die && child_die->tag)
17167 {
17168 if (child_die->tag == DW_TAG_formal_parameter)
17169 {
17170 struct type *arg_type;
17171
17172 /* DWARF version 2 has no clean way to discern C++
17173 static and non-static member functions. G++ helps
17174 GDB by marking the first parameter for non-static
17175 member functions (which is the this pointer) as
17176 artificial. We pass this information to
17177 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17178
17179 DWARF version 3 added DW_AT_object_pointer, which GCC
17180 4.5 does not yet generate. */
17181 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17182 if (attr != nullptr)
17183 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17184 else
17185 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17186 arg_type = die_type (child_die, cu);
17187
17188 /* RealView does not mark THIS as const, which the testsuite
17189 expects. GCC marks THIS as const in method definitions,
17190 but not in the class specifications (GCC PR 43053). */
17191 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17192 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17193 {
17194 int is_this = 0;
17195 struct dwarf2_cu *arg_cu = cu;
17196 const char *name = dwarf2_name (child_die, cu);
17197
17198 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17199 if (attr != nullptr)
17200 {
17201 /* If the compiler emits this, use it. */
17202 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17203 is_this = 1;
17204 }
17205 else if (name && strcmp (name, "this") == 0)
17206 /* Function definitions will have the argument names. */
17207 is_this = 1;
17208 else if (name == NULL && iparams == 0)
17209 /* Declarations may not have the names, so like
17210 elsewhere in GDB, assume an artificial first
17211 argument is "this". */
17212 is_this = 1;
17213
17214 if (is_this)
17215 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17216 arg_type, 0);
17217 }
17218
17219 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17220 iparams++;
17221 }
17222 child_die = child_die->sibling;
17223 }
17224 }
17225
17226 return ftype;
17227 }
17228
17229 static struct type *
17230 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17231 {
17232 struct objfile *objfile = cu->per_objfile->objfile;
17233 const char *name = NULL;
17234 struct type *this_type, *target_type;
17235
17236 name = dwarf2_full_name (NULL, die, cu);
17237 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17238 TYPE_TARGET_STUB (this_type) = 1;
17239 set_die_type (die, this_type, cu);
17240 target_type = die_type (die, cu);
17241 if (target_type != this_type)
17242 TYPE_TARGET_TYPE (this_type) = target_type;
17243 else
17244 {
17245 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17246 spec and cause infinite loops in GDB. */
17247 complaint (_("Self-referential DW_TAG_typedef "
17248 "- DIE at %s [in module %s]"),
17249 sect_offset_str (die->sect_off), objfile_name (objfile));
17250 TYPE_TARGET_TYPE (this_type) = NULL;
17251 }
17252 if (name == NULL)
17253 {
17254 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17255 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17256 Handle these by just returning the target type, rather than
17257 constructing an anonymous typedef type and trying to handle this
17258 elsewhere. */
17259 set_die_type (die, target_type, cu);
17260 return target_type;
17261 }
17262 return this_type;
17263 }
17264
17265 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17266 (which may be different from NAME) to the architecture back-end to allow
17267 it to guess the correct format if necessary. */
17268
17269 static struct type *
17270 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17271 const char *name_hint, enum bfd_endian byte_order)
17272 {
17273 struct gdbarch *gdbarch = objfile->arch ();
17274 const struct floatformat **format;
17275 struct type *type;
17276
17277 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17278 if (format)
17279 type = init_float_type (objfile, bits, name, format, byte_order);
17280 else
17281 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17282
17283 return type;
17284 }
17285
17286 /* Allocate an integer type of size BITS and name NAME. */
17287
17288 static struct type *
17289 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17290 int bits, int unsigned_p, const char *name)
17291 {
17292 struct type *type;
17293
17294 /* Versions of Intel's C Compiler generate an integer type called "void"
17295 instead of using DW_TAG_unspecified_type. This has been seen on
17296 at least versions 14, 17, and 18. */
17297 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17298 && strcmp (name, "void") == 0)
17299 type = objfile_type (objfile)->builtin_void;
17300 else
17301 type = init_integer_type (objfile, bits, unsigned_p, name);
17302
17303 return type;
17304 }
17305
17306 /* Initialise and return a floating point type of size BITS suitable for
17307 use as a component of a complex number. The NAME_HINT is passed through
17308 when initialising the floating point type and is the name of the complex
17309 type.
17310
17311 As DWARF doesn't currently provide an explicit name for the components
17312 of a complex number, but it can be helpful to have these components
17313 named, we try to select a suitable name based on the size of the
17314 component. */
17315 static struct type *
17316 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17317 struct objfile *objfile,
17318 int bits, const char *name_hint,
17319 enum bfd_endian byte_order)
17320 {
17321 gdbarch *gdbarch = objfile->arch ();
17322 struct type *tt = nullptr;
17323
17324 /* Try to find a suitable floating point builtin type of size BITS.
17325 We're going to use the name of this type as the name for the complex
17326 target type that we are about to create. */
17327 switch (cu->language)
17328 {
17329 case language_fortran:
17330 switch (bits)
17331 {
17332 case 32:
17333 tt = builtin_f_type (gdbarch)->builtin_real;
17334 break;
17335 case 64:
17336 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17337 break;
17338 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17339 case 128:
17340 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17341 break;
17342 }
17343 break;
17344 default:
17345 switch (bits)
17346 {
17347 case 32:
17348 tt = builtin_type (gdbarch)->builtin_float;
17349 break;
17350 case 64:
17351 tt = builtin_type (gdbarch)->builtin_double;
17352 break;
17353 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17354 case 128:
17355 tt = builtin_type (gdbarch)->builtin_long_double;
17356 break;
17357 }
17358 break;
17359 }
17360
17361 /* If the type we found doesn't match the size we were looking for, then
17362 pretend we didn't find a type at all, the complex target type we
17363 create will then be nameless. */
17364 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17365 tt = nullptr;
17366
17367 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17368 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17369 }
17370
17371 /* Find a representation of a given base type and install
17372 it in the TYPE field of the die. */
17373
17374 static struct type *
17375 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17376 {
17377 struct objfile *objfile = cu->per_objfile->objfile;
17378 struct type *type;
17379 struct attribute *attr;
17380 int encoding = 0, bits = 0;
17381 const char *name;
17382 gdbarch *arch;
17383
17384 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17385 if (attr != nullptr)
17386 encoding = DW_UNSND (attr);
17387 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17388 if (attr != nullptr)
17389 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17390 name = dwarf2_name (die, cu);
17391 if (!name)
17392 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17393
17394 arch = objfile->arch ();
17395 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17396
17397 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17398 if (attr)
17399 {
17400 int endianity = DW_UNSND (attr);
17401
17402 switch (endianity)
17403 {
17404 case DW_END_big:
17405 byte_order = BFD_ENDIAN_BIG;
17406 break;
17407 case DW_END_little:
17408 byte_order = BFD_ENDIAN_LITTLE;
17409 break;
17410 default:
17411 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17412 break;
17413 }
17414 }
17415
17416 switch (encoding)
17417 {
17418 case DW_ATE_address:
17419 /* Turn DW_ATE_address into a void * pointer. */
17420 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17421 type = init_pointer_type (objfile, bits, name, type);
17422 break;
17423 case DW_ATE_boolean:
17424 type = init_boolean_type (objfile, bits, 1, name);
17425 break;
17426 case DW_ATE_complex_float:
17427 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17428 byte_order);
17429 if (type->code () == TYPE_CODE_ERROR)
17430 {
17431 if (name == nullptr)
17432 {
17433 struct obstack *obstack
17434 = &cu->per_objfile->objfile->objfile_obstack;
17435 name = obconcat (obstack, "_Complex ", type->name (),
17436 nullptr);
17437 }
17438 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17439 }
17440 else
17441 type = init_complex_type (name, type);
17442 break;
17443 case DW_ATE_decimal_float:
17444 type = init_decfloat_type (objfile, bits, name);
17445 break;
17446 case DW_ATE_float:
17447 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17448 break;
17449 case DW_ATE_signed:
17450 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17451 break;
17452 case DW_ATE_unsigned:
17453 if (cu->language == language_fortran
17454 && name
17455 && startswith (name, "character("))
17456 type = init_character_type (objfile, bits, 1, name);
17457 else
17458 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17459 break;
17460 case DW_ATE_signed_char:
17461 if (cu->language == language_ada || cu->language == language_m2
17462 || cu->language == language_pascal
17463 || cu->language == language_fortran)
17464 type = init_character_type (objfile, bits, 0, name);
17465 else
17466 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17467 break;
17468 case DW_ATE_unsigned_char:
17469 if (cu->language == language_ada || cu->language == language_m2
17470 || cu->language == language_pascal
17471 || cu->language == language_fortran
17472 || cu->language == language_rust)
17473 type = init_character_type (objfile, bits, 1, name);
17474 else
17475 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17476 break;
17477 case DW_ATE_UTF:
17478 {
17479 if (bits == 16)
17480 type = builtin_type (arch)->builtin_char16;
17481 else if (bits == 32)
17482 type = builtin_type (arch)->builtin_char32;
17483 else
17484 {
17485 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17486 bits);
17487 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17488 }
17489 return set_die_type (die, type, cu);
17490 }
17491 break;
17492
17493 default:
17494 complaint (_("unsupported DW_AT_encoding: '%s'"),
17495 dwarf_type_encoding_name (encoding));
17496 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17497 break;
17498 }
17499
17500 if (name && strcmp (name, "char") == 0)
17501 TYPE_NOSIGN (type) = 1;
17502
17503 maybe_set_alignment (cu, die, type);
17504
17505 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17506
17507 return set_die_type (die, type, cu);
17508 }
17509
17510 /* Parse dwarf attribute if it's a block, reference or constant and put the
17511 resulting value of the attribute into struct bound_prop.
17512 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17513
17514 static int
17515 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17516 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17517 struct type *default_type)
17518 {
17519 struct dwarf2_property_baton *baton;
17520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17521 struct objfile *objfile = per_objfile->objfile;
17522 struct obstack *obstack = &objfile->objfile_obstack;
17523
17524 gdb_assert (default_type != NULL);
17525
17526 if (attr == NULL || prop == NULL)
17527 return 0;
17528
17529 if (attr->form_is_block ())
17530 {
17531 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17532 baton->property_type = default_type;
17533 baton->locexpr.per_cu = cu->per_cu;
17534 baton->locexpr.per_objfile = per_objfile;
17535 baton->locexpr.size = DW_BLOCK (attr)->size;
17536 baton->locexpr.data = DW_BLOCK (attr)->data;
17537 switch (attr->name)
17538 {
17539 case DW_AT_string_length:
17540 baton->locexpr.is_reference = true;
17541 break;
17542 default:
17543 baton->locexpr.is_reference = false;
17544 break;
17545 }
17546 prop->data.baton = baton;
17547 prop->kind = PROP_LOCEXPR;
17548 gdb_assert (prop->data.baton != NULL);
17549 }
17550 else if (attr->form_is_ref ())
17551 {
17552 struct dwarf2_cu *target_cu = cu;
17553 struct die_info *target_die;
17554 struct attribute *target_attr;
17555
17556 target_die = follow_die_ref (die, attr, &target_cu);
17557 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17558 if (target_attr == NULL)
17559 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17560 target_cu);
17561 if (target_attr == NULL)
17562 return 0;
17563
17564 switch (target_attr->name)
17565 {
17566 case DW_AT_location:
17567 if (target_attr->form_is_section_offset ())
17568 {
17569 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17570 baton->property_type = die_type (target_die, target_cu);
17571 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17572 prop->data.baton = baton;
17573 prop->kind = PROP_LOCLIST;
17574 gdb_assert (prop->data.baton != NULL);
17575 }
17576 else if (target_attr->form_is_block ())
17577 {
17578 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17579 baton->property_type = die_type (target_die, target_cu);
17580 baton->locexpr.per_cu = cu->per_cu;
17581 baton->locexpr.per_objfile = per_objfile;
17582 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17583 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17584 baton->locexpr.is_reference = true;
17585 prop->data.baton = baton;
17586 prop->kind = PROP_LOCEXPR;
17587 gdb_assert (prop->data.baton != NULL);
17588 }
17589 else
17590 {
17591 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17592 "dynamic property");
17593 return 0;
17594 }
17595 break;
17596 case DW_AT_data_member_location:
17597 {
17598 LONGEST offset;
17599
17600 if (!handle_data_member_location (target_die, target_cu,
17601 &offset))
17602 return 0;
17603
17604 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17605 baton->property_type = read_type_die (target_die->parent,
17606 target_cu);
17607 baton->offset_info.offset = offset;
17608 baton->offset_info.type = die_type (target_die, target_cu);
17609 prop->data.baton = baton;
17610 prop->kind = PROP_ADDR_OFFSET;
17611 break;
17612 }
17613 }
17614 }
17615 else if (attr->form_is_constant ())
17616 {
17617 prop->data.const_val = attr->constant_value (0);
17618 prop->kind = PROP_CONST;
17619 }
17620 else
17621 {
17622 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17623 dwarf2_name (die, cu));
17624 return 0;
17625 }
17626
17627 return 1;
17628 }
17629
17630 /* See read.h. */
17631
17632 struct type *
17633 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17634 {
17635 struct type *int_type;
17636
17637 /* Helper macro to examine the various builtin types. */
17638 #define TRY_TYPE(F) \
17639 int_type = (unsigned_p \
17640 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17641 : objfile_type (objfile)->builtin_ ## F); \
17642 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17643 return int_type
17644
17645 TRY_TYPE (char);
17646 TRY_TYPE (short);
17647 TRY_TYPE (int);
17648 TRY_TYPE (long);
17649 TRY_TYPE (long_long);
17650
17651 #undef TRY_TYPE
17652
17653 gdb_assert_not_reached ("unable to find suitable integer type");
17654 }
17655
17656 /* See read.h. */
17657
17658 struct type *
17659 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17660 {
17661 int addr_size = this->per_cu->addr_size ();
17662 return this->per_objfile->int_type (addr_size, unsigned_p);
17663 }
17664
17665 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17666 present (which is valid) then compute the default type based on the
17667 compilation units address size. */
17668
17669 static struct type *
17670 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17671 {
17672 struct type *index_type = die_type (die, cu);
17673
17674 /* Dwarf-2 specifications explicitly allows to create subrange types
17675 without specifying a base type.
17676 In that case, the base type must be set to the type of
17677 the lower bound, upper bound or count, in that order, if any of these
17678 three attributes references an object that has a type.
17679 If no base type is found, the Dwarf-2 specifications say that
17680 a signed integer type of size equal to the size of an address should
17681 be used.
17682 For the following C code: `extern char gdb_int [];'
17683 GCC produces an empty range DIE.
17684 FIXME: muller/2010-05-28: Possible references to object for low bound,
17685 high bound or count are not yet handled by this code. */
17686 if (index_type->code () == TYPE_CODE_VOID)
17687 index_type = cu->addr_sized_int_type (false);
17688
17689 return index_type;
17690 }
17691
17692 /* Read the given DW_AT_subrange DIE. */
17693
17694 static struct type *
17695 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17696 {
17697 struct type *base_type, *orig_base_type;
17698 struct type *range_type;
17699 struct attribute *attr;
17700 struct dynamic_prop low, high;
17701 int low_default_is_valid;
17702 int high_bound_is_count = 0;
17703 const char *name;
17704 ULONGEST negative_mask;
17705
17706 orig_base_type = read_subrange_index_type (die, cu);
17707
17708 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17709 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17710 creating the range type, but we use the result of check_typedef
17711 when examining properties of the type. */
17712 base_type = check_typedef (orig_base_type);
17713
17714 /* The die_type call above may have already set the type for this DIE. */
17715 range_type = get_die_type (die, cu);
17716 if (range_type)
17717 return range_type;
17718
17719 low.kind = PROP_CONST;
17720 high.kind = PROP_CONST;
17721 high.data.const_val = 0;
17722
17723 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17724 omitting DW_AT_lower_bound. */
17725 switch (cu->language)
17726 {
17727 case language_c:
17728 case language_cplus:
17729 low.data.const_val = 0;
17730 low_default_is_valid = 1;
17731 break;
17732 case language_fortran:
17733 low.data.const_val = 1;
17734 low_default_is_valid = 1;
17735 break;
17736 case language_d:
17737 case language_objc:
17738 case language_rust:
17739 low.data.const_val = 0;
17740 low_default_is_valid = (cu->header.version >= 4);
17741 break;
17742 case language_ada:
17743 case language_m2:
17744 case language_pascal:
17745 low.data.const_val = 1;
17746 low_default_is_valid = (cu->header.version >= 4);
17747 break;
17748 default:
17749 low.data.const_val = 0;
17750 low_default_is_valid = 0;
17751 break;
17752 }
17753
17754 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17755 if (attr != nullptr)
17756 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17757 else if (!low_default_is_valid)
17758 complaint (_("Missing DW_AT_lower_bound "
17759 "- DIE at %s [in module %s]"),
17760 sect_offset_str (die->sect_off),
17761 objfile_name (cu->per_objfile->objfile));
17762
17763 struct attribute *attr_ub, *attr_count;
17764 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17765 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17766 {
17767 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17768 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17769 {
17770 /* If bounds are constant do the final calculation here. */
17771 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17772 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17773 else
17774 high_bound_is_count = 1;
17775 }
17776 else
17777 {
17778 if (attr_ub != NULL)
17779 complaint (_("Unresolved DW_AT_upper_bound "
17780 "- DIE at %s [in module %s]"),
17781 sect_offset_str (die->sect_off),
17782 objfile_name (cu->per_objfile->objfile));
17783 if (attr_count != NULL)
17784 complaint (_("Unresolved DW_AT_count "
17785 "- DIE at %s [in module %s]"),
17786 sect_offset_str (die->sect_off),
17787 objfile_name (cu->per_objfile->objfile));
17788 }
17789 }
17790
17791 LONGEST bias = 0;
17792 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17793 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17794 bias = bias_attr->constant_value (0);
17795
17796 /* Normally, the DWARF producers are expected to use a signed
17797 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17798 But this is unfortunately not always the case, as witnessed
17799 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17800 is used instead. To work around that ambiguity, we treat
17801 the bounds as signed, and thus sign-extend their values, when
17802 the base type is signed. */
17803 negative_mask =
17804 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17805 if (low.kind == PROP_CONST
17806 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17807 low.data.const_val |= negative_mask;
17808 if (high.kind == PROP_CONST
17809 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17810 high.data.const_val |= negative_mask;
17811
17812 /* Check for bit and byte strides. */
17813 struct dynamic_prop byte_stride_prop;
17814 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17815 if (attr_byte_stride != nullptr)
17816 {
17817 struct type *prop_type = cu->addr_sized_int_type (false);
17818 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17819 prop_type);
17820 }
17821
17822 struct dynamic_prop bit_stride_prop;
17823 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17824 if (attr_bit_stride != nullptr)
17825 {
17826 /* It only makes sense to have either a bit or byte stride. */
17827 if (attr_byte_stride != nullptr)
17828 {
17829 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17830 "- DIE at %s [in module %s]"),
17831 sect_offset_str (die->sect_off),
17832 objfile_name (cu->per_objfile->objfile));
17833 attr_bit_stride = nullptr;
17834 }
17835 else
17836 {
17837 struct type *prop_type = cu->addr_sized_int_type (false);
17838 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17839 prop_type);
17840 }
17841 }
17842
17843 if (attr_byte_stride != nullptr
17844 || attr_bit_stride != nullptr)
17845 {
17846 bool byte_stride_p = (attr_byte_stride != nullptr);
17847 struct dynamic_prop *stride
17848 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17849
17850 range_type
17851 = create_range_type_with_stride (NULL, orig_base_type, &low,
17852 &high, bias, stride, byte_stride_p);
17853 }
17854 else
17855 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17856
17857 if (high_bound_is_count)
17858 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17859
17860 /* Ada expects an empty array on no boundary attributes. */
17861 if (attr == NULL && cu->language != language_ada)
17862 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17863
17864 name = dwarf2_name (die, cu);
17865 if (name)
17866 range_type->set_name (name);
17867
17868 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17869 if (attr != nullptr)
17870 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17871
17872 maybe_set_alignment (cu, die, range_type);
17873
17874 set_die_type (die, range_type, cu);
17875
17876 /* set_die_type should be already done. */
17877 set_descriptive_type (range_type, die, cu);
17878
17879 return range_type;
17880 }
17881
17882 static struct type *
17883 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17884 {
17885 struct type *type;
17886
17887 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17888 type->set_name (dwarf2_name (die, cu));
17889
17890 /* In Ada, an unspecified type is typically used when the description
17891 of the type is deferred to a different unit. When encountering
17892 such a type, we treat it as a stub, and try to resolve it later on,
17893 when needed. */
17894 if (cu->language == language_ada)
17895 TYPE_STUB (type) = 1;
17896
17897 return set_die_type (die, type, cu);
17898 }
17899
17900 /* Read a single die and all its descendents. Set the die's sibling
17901 field to NULL; set other fields in the die correctly, and set all
17902 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17903 location of the info_ptr after reading all of those dies. PARENT
17904 is the parent of the die in question. */
17905
17906 static struct die_info *
17907 read_die_and_children (const struct die_reader_specs *reader,
17908 const gdb_byte *info_ptr,
17909 const gdb_byte **new_info_ptr,
17910 struct die_info *parent)
17911 {
17912 struct die_info *die;
17913 const gdb_byte *cur_ptr;
17914
17915 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17916 if (die == NULL)
17917 {
17918 *new_info_ptr = cur_ptr;
17919 return NULL;
17920 }
17921 store_in_ref_table (die, reader->cu);
17922
17923 if (die->has_children)
17924 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17925 else
17926 {
17927 die->child = NULL;
17928 *new_info_ptr = cur_ptr;
17929 }
17930
17931 die->sibling = NULL;
17932 die->parent = parent;
17933 return die;
17934 }
17935
17936 /* Read a die, all of its descendents, and all of its siblings; set
17937 all of the fields of all of the dies correctly. Arguments are as
17938 in read_die_and_children. */
17939
17940 static struct die_info *
17941 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17942 const gdb_byte *info_ptr,
17943 const gdb_byte **new_info_ptr,
17944 struct die_info *parent)
17945 {
17946 struct die_info *first_die, *last_sibling;
17947 const gdb_byte *cur_ptr;
17948
17949 cur_ptr = info_ptr;
17950 first_die = last_sibling = NULL;
17951
17952 while (1)
17953 {
17954 struct die_info *die
17955 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17956
17957 if (die == NULL)
17958 {
17959 *new_info_ptr = cur_ptr;
17960 return first_die;
17961 }
17962
17963 if (!first_die)
17964 first_die = die;
17965 else
17966 last_sibling->sibling = die;
17967
17968 last_sibling = die;
17969 }
17970 }
17971
17972 /* Read a die, all of its descendents, and all of its siblings; set
17973 all of the fields of all of the dies correctly. Arguments are as
17974 in read_die_and_children.
17975 This the main entry point for reading a DIE and all its children. */
17976
17977 static struct die_info *
17978 read_die_and_siblings (const struct die_reader_specs *reader,
17979 const gdb_byte *info_ptr,
17980 const gdb_byte **new_info_ptr,
17981 struct die_info *parent)
17982 {
17983 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17984 new_info_ptr, parent);
17985
17986 if (dwarf_die_debug)
17987 {
17988 fprintf_unfiltered (gdb_stdlog,
17989 "Read die from %s@0x%x of %s:\n",
17990 reader->die_section->get_name (),
17991 (unsigned) (info_ptr - reader->die_section->buffer),
17992 bfd_get_filename (reader->abfd));
17993 dump_die (die, dwarf_die_debug);
17994 }
17995
17996 return die;
17997 }
17998
17999 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18000 attributes.
18001 The caller is responsible for filling in the extra attributes
18002 and updating (*DIEP)->num_attrs.
18003 Set DIEP to point to a newly allocated die with its information,
18004 except for its child, sibling, and parent fields. */
18005
18006 static const gdb_byte *
18007 read_full_die_1 (const struct die_reader_specs *reader,
18008 struct die_info **diep, const gdb_byte *info_ptr,
18009 int num_extra_attrs)
18010 {
18011 unsigned int abbrev_number, bytes_read, i;
18012 struct abbrev_info *abbrev;
18013 struct die_info *die;
18014 struct dwarf2_cu *cu = reader->cu;
18015 bfd *abfd = reader->abfd;
18016
18017 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18018 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18019 info_ptr += bytes_read;
18020 if (!abbrev_number)
18021 {
18022 *diep = NULL;
18023 return info_ptr;
18024 }
18025
18026 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18027 if (!abbrev)
18028 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18029 abbrev_number,
18030 bfd_get_filename (abfd));
18031
18032 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18033 die->sect_off = sect_off;
18034 die->tag = abbrev->tag;
18035 die->abbrev = abbrev_number;
18036 die->has_children = abbrev->has_children;
18037
18038 /* Make the result usable.
18039 The caller needs to update num_attrs after adding the extra
18040 attributes. */
18041 die->num_attrs = abbrev->num_attrs;
18042
18043 std::vector<int> indexes_that_need_reprocess;
18044 for (i = 0; i < abbrev->num_attrs; ++i)
18045 {
18046 bool need_reprocess;
18047 info_ptr =
18048 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18049 info_ptr, &need_reprocess);
18050 if (need_reprocess)
18051 indexes_that_need_reprocess.push_back (i);
18052 }
18053
18054 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18055 if (attr != nullptr)
18056 cu->str_offsets_base = DW_UNSND (attr);
18057
18058 attr = die->attr (DW_AT_loclists_base);
18059 if (attr != nullptr)
18060 cu->loclist_base = DW_UNSND (attr);
18061
18062 auto maybe_addr_base = die->addr_base ();
18063 if (maybe_addr_base.has_value ())
18064 cu->addr_base = *maybe_addr_base;
18065 for (int index : indexes_that_need_reprocess)
18066 read_attribute_reprocess (reader, &die->attrs[index]);
18067 *diep = die;
18068 return info_ptr;
18069 }
18070
18071 /* Read a die and all its attributes.
18072 Set DIEP to point to a newly allocated die with its information,
18073 except for its child, sibling, and parent fields. */
18074
18075 static const gdb_byte *
18076 read_full_die (const struct die_reader_specs *reader,
18077 struct die_info **diep, const gdb_byte *info_ptr)
18078 {
18079 const gdb_byte *result;
18080
18081 result = read_full_die_1 (reader, diep, info_ptr, 0);
18082
18083 if (dwarf_die_debug)
18084 {
18085 fprintf_unfiltered (gdb_stdlog,
18086 "Read die from %s@0x%x of %s:\n",
18087 reader->die_section->get_name (),
18088 (unsigned) (info_ptr - reader->die_section->buffer),
18089 bfd_get_filename (reader->abfd));
18090 dump_die (*diep, dwarf_die_debug);
18091 }
18092
18093 return result;
18094 }
18095 \f
18096
18097 /* Returns nonzero if TAG represents a type that we might generate a partial
18098 symbol for. */
18099
18100 static int
18101 is_type_tag_for_partial (int tag)
18102 {
18103 switch (tag)
18104 {
18105 #if 0
18106 /* Some types that would be reasonable to generate partial symbols for,
18107 that we don't at present. */
18108 case DW_TAG_array_type:
18109 case DW_TAG_file_type:
18110 case DW_TAG_ptr_to_member_type:
18111 case DW_TAG_set_type:
18112 case DW_TAG_string_type:
18113 case DW_TAG_subroutine_type:
18114 #endif
18115 case DW_TAG_base_type:
18116 case DW_TAG_class_type:
18117 case DW_TAG_interface_type:
18118 case DW_TAG_enumeration_type:
18119 case DW_TAG_structure_type:
18120 case DW_TAG_subrange_type:
18121 case DW_TAG_typedef:
18122 case DW_TAG_union_type:
18123 return 1;
18124 default:
18125 return 0;
18126 }
18127 }
18128
18129 /* Load all DIEs that are interesting for partial symbols into memory. */
18130
18131 static struct partial_die_info *
18132 load_partial_dies (const struct die_reader_specs *reader,
18133 const gdb_byte *info_ptr, int building_psymtab)
18134 {
18135 struct dwarf2_cu *cu = reader->cu;
18136 struct objfile *objfile = cu->per_objfile->objfile;
18137 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18138 unsigned int bytes_read;
18139 unsigned int load_all = 0;
18140 int nesting_level = 1;
18141
18142 parent_die = NULL;
18143 last_die = NULL;
18144
18145 gdb_assert (cu->per_cu != NULL);
18146 if (cu->per_cu->load_all_dies)
18147 load_all = 1;
18148
18149 cu->partial_dies
18150 = htab_create_alloc_ex (cu->header.length / 12,
18151 partial_die_hash,
18152 partial_die_eq,
18153 NULL,
18154 &cu->comp_unit_obstack,
18155 hashtab_obstack_allocate,
18156 dummy_obstack_deallocate);
18157
18158 while (1)
18159 {
18160 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18161
18162 /* A NULL abbrev means the end of a series of children. */
18163 if (abbrev == NULL)
18164 {
18165 if (--nesting_level == 0)
18166 return first_die;
18167
18168 info_ptr += bytes_read;
18169 last_die = parent_die;
18170 parent_die = parent_die->die_parent;
18171 continue;
18172 }
18173
18174 /* Check for template arguments. We never save these; if
18175 they're seen, we just mark the parent, and go on our way. */
18176 if (parent_die != NULL
18177 && cu->language == language_cplus
18178 && (abbrev->tag == DW_TAG_template_type_param
18179 || abbrev->tag == DW_TAG_template_value_param))
18180 {
18181 parent_die->has_template_arguments = 1;
18182
18183 if (!load_all)
18184 {
18185 /* We don't need a partial DIE for the template argument. */
18186 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18187 continue;
18188 }
18189 }
18190
18191 /* We only recurse into c++ subprograms looking for template arguments.
18192 Skip their other children. */
18193 if (!load_all
18194 && cu->language == language_cplus
18195 && parent_die != NULL
18196 && parent_die->tag == DW_TAG_subprogram)
18197 {
18198 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18199 continue;
18200 }
18201
18202 /* Check whether this DIE is interesting enough to save. Normally
18203 we would not be interested in members here, but there may be
18204 later variables referencing them via DW_AT_specification (for
18205 static members). */
18206 if (!load_all
18207 && !is_type_tag_for_partial (abbrev->tag)
18208 && abbrev->tag != DW_TAG_constant
18209 && abbrev->tag != DW_TAG_enumerator
18210 && abbrev->tag != DW_TAG_subprogram
18211 && abbrev->tag != DW_TAG_inlined_subroutine
18212 && abbrev->tag != DW_TAG_lexical_block
18213 && abbrev->tag != DW_TAG_variable
18214 && abbrev->tag != DW_TAG_namespace
18215 && abbrev->tag != DW_TAG_module
18216 && abbrev->tag != DW_TAG_member
18217 && abbrev->tag != DW_TAG_imported_unit
18218 && abbrev->tag != DW_TAG_imported_declaration)
18219 {
18220 /* Otherwise we skip to the next sibling, if any. */
18221 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18222 continue;
18223 }
18224
18225 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18226 abbrev);
18227
18228 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18229
18230 /* This two-pass algorithm for processing partial symbols has a
18231 high cost in cache pressure. Thus, handle some simple cases
18232 here which cover the majority of C partial symbols. DIEs
18233 which neither have specification tags in them, nor could have
18234 specification tags elsewhere pointing at them, can simply be
18235 processed and discarded.
18236
18237 This segment is also optional; scan_partial_symbols and
18238 add_partial_symbol will handle these DIEs if we chain
18239 them in normally. When compilers which do not emit large
18240 quantities of duplicate debug information are more common,
18241 this code can probably be removed. */
18242
18243 /* Any complete simple types at the top level (pretty much all
18244 of them, for a language without namespaces), can be processed
18245 directly. */
18246 if (parent_die == NULL
18247 && pdi.has_specification == 0
18248 && pdi.is_declaration == 0
18249 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18250 || pdi.tag == DW_TAG_base_type
18251 || pdi.tag == DW_TAG_subrange_type))
18252 {
18253 if (building_psymtab && pdi.name != NULL)
18254 add_psymbol_to_list (pdi.name, false,
18255 VAR_DOMAIN, LOC_TYPEDEF, -1,
18256 psymbol_placement::STATIC,
18257 0, cu->language, objfile);
18258 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18259 continue;
18260 }
18261
18262 /* The exception for DW_TAG_typedef with has_children above is
18263 a workaround of GCC PR debug/47510. In the case of this complaint
18264 type_name_or_error will error on such types later.
18265
18266 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18267 it could not find the child DIEs referenced later, this is checked
18268 above. In correct DWARF DW_TAG_typedef should have no children. */
18269
18270 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18271 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18272 "- DIE at %s [in module %s]"),
18273 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18274
18275 /* If we're at the second level, and we're an enumerator, and
18276 our parent has no specification (meaning possibly lives in a
18277 namespace elsewhere), then we can add the partial symbol now
18278 instead of queueing it. */
18279 if (pdi.tag == DW_TAG_enumerator
18280 && parent_die != NULL
18281 && parent_die->die_parent == NULL
18282 && parent_die->tag == DW_TAG_enumeration_type
18283 && parent_die->has_specification == 0)
18284 {
18285 if (pdi.name == NULL)
18286 complaint (_("malformed enumerator DIE ignored"));
18287 else if (building_psymtab)
18288 add_psymbol_to_list (pdi.name, false,
18289 VAR_DOMAIN, LOC_CONST, -1,
18290 cu->language == language_cplus
18291 ? psymbol_placement::GLOBAL
18292 : psymbol_placement::STATIC,
18293 0, cu->language, objfile);
18294
18295 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18296 continue;
18297 }
18298
18299 struct partial_die_info *part_die
18300 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18301
18302 /* We'll save this DIE so link it in. */
18303 part_die->die_parent = parent_die;
18304 part_die->die_sibling = NULL;
18305 part_die->die_child = NULL;
18306
18307 if (last_die && last_die == parent_die)
18308 last_die->die_child = part_die;
18309 else if (last_die)
18310 last_die->die_sibling = part_die;
18311
18312 last_die = part_die;
18313
18314 if (first_die == NULL)
18315 first_die = part_die;
18316
18317 /* Maybe add the DIE to the hash table. Not all DIEs that we
18318 find interesting need to be in the hash table, because we
18319 also have the parent/sibling/child chains; only those that we
18320 might refer to by offset later during partial symbol reading.
18321
18322 For now this means things that might have be the target of a
18323 DW_AT_specification, DW_AT_abstract_origin, or
18324 DW_AT_extension. DW_AT_extension will refer only to
18325 namespaces; DW_AT_abstract_origin refers to functions (and
18326 many things under the function DIE, but we do not recurse
18327 into function DIEs during partial symbol reading) and
18328 possibly variables as well; DW_AT_specification refers to
18329 declarations. Declarations ought to have the DW_AT_declaration
18330 flag. It happens that GCC forgets to put it in sometimes, but
18331 only for functions, not for types.
18332
18333 Adding more things than necessary to the hash table is harmless
18334 except for the performance cost. Adding too few will result in
18335 wasted time in find_partial_die, when we reread the compilation
18336 unit with load_all_dies set. */
18337
18338 if (load_all
18339 || abbrev->tag == DW_TAG_constant
18340 || abbrev->tag == DW_TAG_subprogram
18341 || abbrev->tag == DW_TAG_variable
18342 || abbrev->tag == DW_TAG_namespace
18343 || part_die->is_declaration)
18344 {
18345 void **slot;
18346
18347 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18348 to_underlying (part_die->sect_off),
18349 INSERT);
18350 *slot = part_die;
18351 }
18352
18353 /* For some DIEs we want to follow their children (if any). For C
18354 we have no reason to follow the children of structures; for other
18355 languages we have to, so that we can get at method physnames
18356 to infer fully qualified class names, for DW_AT_specification,
18357 and for C++ template arguments. For C++, we also look one level
18358 inside functions to find template arguments (if the name of the
18359 function does not already contain the template arguments).
18360
18361 For Ada and Fortran, we need to scan the children of subprograms
18362 and lexical blocks as well because these languages allow the
18363 definition of nested entities that could be interesting for the
18364 debugger, such as nested subprograms for instance. */
18365 if (last_die->has_children
18366 && (load_all
18367 || last_die->tag == DW_TAG_namespace
18368 || last_die->tag == DW_TAG_module
18369 || last_die->tag == DW_TAG_enumeration_type
18370 || (cu->language == language_cplus
18371 && last_die->tag == DW_TAG_subprogram
18372 && (last_die->name == NULL
18373 || strchr (last_die->name, '<') == NULL))
18374 || (cu->language != language_c
18375 && (last_die->tag == DW_TAG_class_type
18376 || last_die->tag == DW_TAG_interface_type
18377 || last_die->tag == DW_TAG_structure_type
18378 || last_die->tag == DW_TAG_union_type))
18379 || ((cu->language == language_ada
18380 || cu->language == language_fortran)
18381 && (last_die->tag == DW_TAG_subprogram
18382 || last_die->tag == DW_TAG_lexical_block))))
18383 {
18384 nesting_level++;
18385 parent_die = last_die;
18386 continue;
18387 }
18388
18389 /* Otherwise we skip to the next sibling, if any. */
18390 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18391
18392 /* Back to the top, do it again. */
18393 }
18394 }
18395
18396 partial_die_info::partial_die_info (sect_offset sect_off_,
18397 struct abbrev_info *abbrev)
18398 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18399 {
18400 }
18401
18402 /* Read a minimal amount of information into the minimal die structure.
18403 INFO_PTR should point just after the initial uleb128 of a DIE. */
18404
18405 const gdb_byte *
18406 partial_die_info::read (const struct die_reader_specs *reader,
18407 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18408 {
18409 struct dwarf2_cu *cu = reader->cu;
18410 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18411 unsigned int i;
18412 int has_low_pc_attr = 0;
18413 int has_high_pc_attr = 0;
18414 int high_pc_relative = 0;
18415
18416 for (i = 0; i < abbrev.num_attrs; ++i)
18417 {
18418 attribute attr;
18419 bool need_reprocess;
18420 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18421 info_ptr, &need_reprocess);
18422 /* String and address offsets that need to do the reprocessing have
18423 already been read at this point, so there is no need to wait until
18424 the loop terminates to do the reprocessing. */
18425 if (need_reprocess)
18426 read_attribute_reprocess (reader, &attr);
18427 /* Store the data if it is of an attribute we want to keep in a
18428 partial symbol table. */
18429 switch (attr.name)
18430 {
18431 case DW_AT_name:
18432 switch (tag)
18433 {
18434 case DW_TAG_compile_unit:
18435 case DW_TAG_partial_unit:
18436 case DW_TAG_type_unit:
18437 /* Compilation units have a DW_AT_name that is a filename, not
18438 a source language identifier. */
18439 case DW_TAG_enumeration_type:
18440 case DW_TAG_enumerator:
18441 /* These tags always have simple identifiers already; no need
18442 to canonicalize them. */
18443 name = DW_STRING (&attr);
18444 break;
18445 default:
18446 {
18447 struct objfile *objfile = dwarf2_per_objfile->objfile;
18448
18449 name
18450 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18451 }
18452 break;
18453 }
18454 break;
18455 case DW_AT_linkage_name:
18456 case DW_AT_MIPS_linkage_name:
18457 /* Note that both forms of linkage name might appear. We
18458 assume they will be the same, and we only store the last
18459 one we see. */
18460 linkage_name = attr.value_as_string ();
18461 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18462 See https://github.com/rust-lang/rust/issues/32925. */
18463 if (cu->language == language_rust && linkage_name != NULL
18464 && strchr (linkage_name, '{') != NULL)
18465 linkage_name = NULL;
18466 break;
18467 case DW_AT_low_pc:
18468 has_low_pc_attr = 1;
18469 lowpc = attr.value_as_address ();
18470 break;
18471 case DW_AT_high_pc:
18472 has_high_pc_attr = 1;
18473 highpc = attr.value_as_address ();
18474 if (cu->header.version >= 4 && attr.form_is_constant ())
18475 high_pc_relative = 1;
18476 break;
18477 case DW_AT_location:
18478 /* Support the .debug_loc offsets. */
18479 if (attr.form_is_block ())
18480 {
18481 d.locdesc = DW_BLOCK (&attr);
18482 }
18483 else if (attr.form_is_section_offset ())
18484 {
18485 dwarf2_complex_location_expr_complaint ();
18486 }
18487 else
18488 {
18489 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18490 "partial symbol information");
18491 }
18492 break;
18493 case DW_AT_external:
18494 is_external = DW_UNSND (&attr);
18495 break;
18496 case DW_AT_declaration:
18497 is_declaration = DW_UNSND (&attr);
18498 break;
18499 case DW_AT_type:
18500 has_type = 1;
18501 break;
18502 case DW_AT_abstract_origin:
18503 case DW_AT_specification:
18504 case DW_AT_extension:
18505 has_specification = 1;
18506 spec_offset = attr.get_ref_die_offset ();
18507 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18508 || cu->per_cu->is_dwz);
18509 break;
18510 case DW_AT_sibling:
18511 /* Ignore absolute siblings, they might point outside of
18512 the current compile unit. */
18513 if (attr.form == DW_FORM_ref_addr)
18514 complaint (_("ignoring absolute DW_AT_sibling"));
18515 else
18516 {
18517 const gdb_byte *buffer = reader->buffer;
18518 sect_offset off = attr.get_ref_die_offset ();
18519 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18520
18521 if (sibling_ptr < info_ptr)
18522 complaint (_("DW_AT_sibling points backwards"));
18523 else if (sibling_ptr > reader->buffer_end)
18524 reader->die_section->overflow_complaint ();
18525 else
18526 sibling = sibling_ptr;
18527 }
18528 break;
18529 case DW_AT_byte_size:
18530 has_byte_size = 1;
18531 break;
18532 case DW_AT_const_value:
18533 has_const_value = 1;
18534 break;
18535 case DW_AT_calling_convention:
18536 /* DWARF doesn't provide a way to identify a program's source-level
18537 entry point. DW_AT_calling_convention attributes are only meant
18538 to describe functions' calling conventions.
18539
18540 However, because it's a necessary piece of information in
18541 Fortran, and before DWARF 4 DW_CC_program was the only
18542 piece of debugging information whose definition refers to
18543 a 'main program' at all, several compilers marked Fortran
18544 main programs with DW_CC_program --- even when those
18545 functions use the standard calling conventions.
18546
18547 Although DWARF now specifies a way to provide this
18548 information, we support this practice for backward
18549 compatibility. */
18550 if (DW_UNSND (&attr) == DW_CC_program
18551 && cu->language == language_fortran)
18552 main_subprogram = 1;
18553 break;
18554 case DW_AT_inline:
18555 if (DW_UNSND (&attr) == DW_INL_inlined
18556 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18557 may_be_inlined = 1;
18558 break;
18559
18560 case DW_AT_import:
18561 if (tag == DW_TAG_imported_unit)
18562 {
18563 d.sect_off = attr.get_ref_die_offset ();
18564 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18565 || cu->per_cu->is_dwz);
18566 }
18567 break;
18568
18569 case DW_AT_main_subprogram:
18570 main_subprogram = DW_UNSND (&attr);
18571 break;
18572
18573 case DW_AT_ranges:
18574 {
18575 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18576 but that requires a full DIE, so instead we just
18577 reimplement it. */
18578 int need_ranges_base = tag != DW_TAG_compile_unit;
18579 unsigned int ranges_offset = (DW_UNSND (&attr)
18580 + (need_ranges_base
18581 ? cu->ranges_base
18582 : 0));
18583
18584 /* Value of the DW_AT_ranges attribute is the offset in the
18585 .debug_ranges section. */
18586 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18587 nullptr))
18588 has_pc_info = 1;
18589 }
18590 break;
18591
18592 default:
18593 break;
18594 }
18595 }
18596
18597 /* For Ada, if both the name and the linkage name appear, we prefer
18598 the latter. This lets "catch exception" work better, regardless
18599 of the order in which the name and linkage name were emitted.
18600 Really, though, this is just a workaround for the fact that gdb
18601 doesn't store both the name and the linkage name. */
18602 if (cu->language == language_ada && linkage_name != nullptr)
18603 name = linkage_name;
18604
18605 if (high_pc_relative)
18606 highpc += lowpc;
18607
18608 if (has_low_pc_attr && has_high_pc_attr)
18609 {
18610 /* When using the GNU linker, .gnu.linkonce. sections are used to
18611 eliminate duplicate copies of functions and vtables and such.
18612 The linker will arbitrarily choose one and discard the others.
18613 The AT_*_pc values for such functions refer to local labels in
18614 these sections. If the section from that file was discarded, the
18615 labels are not in the output, so the relocs get a value of 0.
18616 If this is a discarded function, mark the pc bounds as invalid,
18617 so that GDB will ignore it. */
18618 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18619 {
18620 struct objfile *objfile = dwarf2_per_objfile->objfile;
18621 struct gdbarch *gdbarch = objfile->arch ();
18622
18623 complaint (_("DW_AT_low_pc %s is zero "
18624 "for DIE at %s [in module %s]"),
18625 paddress (gdbarch, lowpc),
18626 sect_offset_str (sect_off),
18627 objfile_name (objfile));
18628 }
18629 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18630 else if (lowpc >= highpc)
18631 {
18632 struct objfile *objfile = dwarf2_per_objfile->objfile;
18633 struct gdbarch *gdbarch = objfile->arch ();
18634
18635 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18636 "for DIE at %s [in module %s]"),
18637 paddress (gdbarch, lowpc),
18638 paddress (gdbarch, highpc),
18639 sect_offset_str (sect_off),
18640 objfile_name (objfile));
18641 }
18642 else
18643 has_pc_info = 1;
18644 }
18645
18646 return info_ptr;
18647 }
18648
18649 /* Find a cached partial DIE at OFFSET in CU. */
18650
18651 struct partial_die_info *
18652 dwarf2_cu::find_partial_die (sect_offset sect_off)
18653 {
18654 struct partial_die_info *lookup_die = NULL;
18655 struct partial_die_info part_die (sect_off);
18656
18657 lookup_die = ((struct partial_die_info *)
18658 htab_find_with_hash (partial_dies, &part_die,
18659 to_underlying (sect_off)));
18660
18661 return lookup_die;
18662 }
18663
18664 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18665 except in the case of .debug_types DIEs which do not reference
18666 outside their CU (they do however referencing other types via
18667 DW_FORM_ref_sig8). */
18668
18669 static const struct cu_partial_die_info
18670 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18671 {
18672 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18673 struct objfile *objfile = dwarf2_per_objfile->objfile;
18674 struct dwarf2_per_cu_data *per_cu = NULL;
18675 struct partial_die_info *pd = NULL;
18676
18677 if (offset_in_dwz == cu->per_cu->is_dwz
18678 && cu->header.offset_in_cu_p (sect_off))
18679 {
18680 pd = cu->find_partial_die (sect_off);
18681 if (pd != NULL)
18682 return { cu, pd };
18683 /* We missed recording what we needed.
18684 Load all dies and try again. */
18685 per_cu = cu->per_cu;
18686 }
18687 else
18688 {
18689 /* TUs don't reference other CUs/TUs (except via type signatures). */
18690 if (cu->per_cu->is_debug_types)
18691 {
18692 error (_("Dwarf Error: Type Unit at offset %s contains"
18693 " external reference to offset %s [in module %s].\n"),
18694 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18695 bfd_get_filename (objfile->obfd));
18696 }
18697 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18698 dwarf2_per_objfile);
18699
18700 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18701 load_partial_comp_unit (per_cu, cu->per_objfile);
18702
18703 per_cu->cu->last_used = 0;
18704 pd = per_cu->cu->find_partial_die (sect_off);
18705 }
18706
18707 /* If we didn't find it, and not all dies have been loaded,
18708 load them all and try again. */
18709
18710 if (pd == NULL && per_cu->load_all_dies == 0)
18711 {
18712 per_cu->load_all_dies = 1;
18713
18714 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18715 THIS_CU->cu may already be in use. So we can't just free it and
18716 replace its DIEs with the ones we read in. Instead, we leave those
18717 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18718 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18719 set. */
18720 load_partial_comp_unit (per_cu, cu->per_objfile);
18721
18722 pd = per_cu->cu->find_partial_die (sect_off);
18723 }
18724
18725 if (pd == NULL)
18726 internal_error (__FILE__, __LINE__,
18727 _("could not find partial DIE %s "
18728 "in cache [from module %s]\n"),
18729 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18730 return { per_cu->cu, pd };
18731 }
18732
18733 /* See if we can figure out if the class lives in a namespace. We do
18734 this by looking for a member function; its demangled name will
18735 contain namespace info, if there is any. */
18736
18737 static void
18738 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18739 struct dwarf2_cu *cu)
18740 {
18741 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18742 what template types look like, because the demangler
18743 frequently doesn't give the same name as the debug info. We
18744 could fix this by only using the demangled name to get the
18745 prefix (but see comment in read_structure_type). */
18746
18747 struct partial_die_info *real_pdi;
18748 struct partial_die_info *child_pdi;
18749
18750 /* If this DIE (this DIE's specification, if any) has a parent, then
18751 we should not do this. We'll prepend the parent's fully qualified
18752 name when we create the partial symbol. */
18753
18754 real_pdi = struct_pdi;
18755 while (real_pdi->has_specification)
18756 {
18757 auto res = find_partial_die (real_pdi->spec_offset,
18758 real_pdi->spec_is_dwz, cu);
18759 real_pdi = res.pdi;
18760 cu = res.cu;
18761 }
18762
18763 if (real_pdi->die_parent != NULL)
18764 return;
18765
18766 for (child_pdi = struct_pdi->die_child;
18767 child_pdi != NULL;
18768 child_pdi = child_pdi->die_sibling)
18769 {
18770 if (child_pdi->tag == DW_TAG_subprogram
18771 && child_pdi->linkage_name != NULL)
18772 {
18773 gdb::unique_xmalloc_ptr<char> actual_class_name
18774 (language_class_name_from_physname (cu->language_defn,
18775 child_pdi->linkage_name));
18776 if (actual_class_name != NULL)
18777 {
18778 struct objfile *objfile = cu->per_objfile->objfile;
18779 struct_pdi->name = objfile->intern (actual_class_name.get ());
18780 }
18781 break;
18782 }
18783 }
18784 }
18785
18786 /* Return true if a DIE with TAG may have the DW_AT_const_value
18787 attribute. */
18788
18789 static bool
18790 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18791 {
18792 switch (tag)
18793 {
18794 case DW_TAG_constant:
18795 case DW_TAG_enumerator:
18796 case DW_TAG_formal_parameter:
18797 case DW_TAG_template_value_param:
18798 case DW_TAG_variable:
18799 return true;
18800 }
18801
18802 return false;
18803 }
18804
18805 void
18806 partial_die_info::fixup (struct dwarf2_cu *cu)
18807 {
18808 /* Once we've fixed up a die, there's no point in doing so again.
18809 This also avoids a memory leak if we were to call
18810 guess_partial_die_structure_name multiple times. */
18811 if (fixup_called)
18812 return;
18813
18814 /* If we found a reference attribute and the DIE has no name, try
18815 to find a name in the referred to DIE. */
18816
18817 if (name == NULL && has_specification)
18818 {
18819 struct partial_die_info *spec_die;
18820
18821 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18822 spec_die = res.pdi;
18823 cu = res.cu;
18824
18825 spec_die->fixup (cu);
18826
18827 if (spec_die->name)
18828 {
18829 name = spec_die->name;
18830
18831 /* Copy DW_AT_external attribute if it is set. */
18832 if (spec_die->is_external)
18833 is_external = spec_die->is_external;
18834 }
18835 }
18836
18837 if (!has_const_value && has_specification
18838 && can_have_DW_AT_const_value_p (tag))
18839 {
18840 struct partial_die_info *spec_die;
18841
18842 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18843 spec_die = res.pdi;
18844 cu = res.cu;
18845
18846 spec_die->fixup (cu);
18847
18848 if (spec_die->has_const_value)
18849 {
18850 /* Copy DW_AT_const_value attribute if it is set. */
18851 has_const_value = spec_die->has_const_value;
18852 }
18853 }
18854
18855 /* Set default names for some unnamed DIEs. */
18856
18857 if (name == NULL && tag == DW_TAG_namespace)
18858 name = CP_ANONYMOUS_NAMESPACE_STR;
18859
18860 /* If there is no parent die to provide a namespace, and there are
18861 children, see if we can determine the namespace from their linkage
18862 name. */
18863 if (cu->language == language_cplus
18864 && !cu->per_objfile->per_bfd->types.empty ()
18865 && die_parent == NULL
18866 && has_children
18867 && (tag == DW_TAG_class_type
18868 || tag == DW_TAG_structure_type
18869 || tag == DW_TAG_union_type))
18870 guess_partial_die_structure_name (this, cu);
18871
18872 /* GCC might emit a nameless struct or union that has a linkage
18873 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18874 if (name == NULL
18875 && (tag == DW_TAG_class_type
18876 || tag == DW_TAG_interface_type
18877 || tag == DW_TAG_structure_type
18878 || tag == DW_TAG_union_type)
18879 && linkage_name != NULL)
18880 {
18881 gdb::unique_xmalloc_ptr<char> demangled
18882 (gdb_demangle (linkage_name, DMGL_TYPES));
18883 if (demangled != nullptr)
18884 {
18885 const char *base;
18886
18887 /* Strip any leading namespaces/classes, keep only the base name.
18888 DW_AT_name for named DIEs does not contain the prefixes. */
18889 base = strrchr (demangled.get (), ':');
18890 if (base && base > demangled.get () && base[-1] == ':')
18891 base++;
18892 else
18893 base = demangled.get ();
18894
18895 struct objfile *objfile = cu->per_objfile->objfile;
18896 name = objfile->intern (base);
18897 }
18898 }
18899
18900 fixup_called = 1;
18901 }
18902
18903 /* Read the .debug_loclists header contents from the given SECTION in the
18904 HEADER. */
18905 static void
18906 read_loclist_header (struct loclist_header *header,
18907 struct dwarf2_section_info *section)
18908 {
18909 unsigned int bytes_read;
18910 bfd *abfd = section->get_bfd_owner ();
18911 const gdb_byte *info_ptr = section->buffer;
18912 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18913 info_ptr += bytes_read;
18914 header->version = read_2_bytes (abfd, info_ptr);
18915 info_ptr += 2;
18916 header->addr_size = read_1_byte (abfd, info_ptr);
18917 info_ptr += 1;
18918 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18919 info_ptr += 1;
18920 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18921 }
18922
18923 /* Return the DW_AT_loclists_base value for the CU. */
18924 static ULONGEST
18925 lookup_loclist_base (struct dwarf2_cu *cu)
18926 {
18927 /* For the .dwo unit, the loclist_base points to the first offset following
18928 the header. The header consists of the following entities-
18929 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18930 bit format)
18931 2. version (2 bytes)
18932 3. address size (1 byte)
18933 4. segment selector size (1 byte)
18934 5. offset entry count (4 bytes)
18935 These sizes are derived as per the DWARFv5 standard. */
18936 if (cu->dwo_unit != nullptr)
18937 {
18938 if (cu->header.initial_length_size == 4)
18939 return LOCLIST_HEADER_SIZE32;
18940 return LOCLIST_HEADER_SIZE64;
18941 }
18942 return cu->loclist_base;
18943 }
18944
18945 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18946 array of offsets in the .debug_loclists section. */
18947 static CORE_ADDR
18948 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18949 {
18950 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18951 struct objfile *objfile = dwarf2_per_objfile->objfile;
18952 bfd *abfd = objfile->obfd;
18953 ULONGEST loclist_base = lookup_loclist_base (cu);
18954 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18955
18956 section->read (objfile);
18957 if (section->buffer == NULL)
18958 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18959 "section [in module %s]"), objfile_name (objfile));
18960 struct loclist_header header;
18961 read_loclist_header (&header, section);
18962 if (loclist_index >= header.offset_entry_count)
18963 complaint (_("DW_FORM_loclistx pointing outside of "
18964 ".debug_loclists offset array [in module %s]"),
18965 objfile_name (objfile));
18966 if (loclist_base + loclist_index * cu->header.offset_size
18967 >= section->size)
18968 complaint (_("DW_FORM_loclistx pointing outside of "
18969 ".debug_loclists section [in module %s]"),
18970 objfile_name (objfile));
18971 const gdb_byte *info_ptr
18972 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18973
18974 if (cu->header.offset_size == 4)
18975 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18976 else
18977 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18978 }
18979
18980 /* Process the attributes that had to be skipped in the first round. These
18981 attributes are the ones that need str_offsets_base or addr_base attributes.
18982 They could not have been processed in the first round, because at the time
18983 the values of str_offsets_base or addr_base may not have been known. */
18984 static void
18985 read_attribute_reprocess (const struct die_reader_specs *reader,
18986 struct attribute *attr)
18987 {
18988 struct dwarf2_cu *cu = reader->cu;
18989 switch (attr->form)
18990 {
18991 case DW_FORM_addrx:
18992 case DW_FORM_GNU_addr_index:
18993 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18994 break;
18995 case DW_FORM_loclistx:
18996 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18997 break;
18998 case DW_FORM_strx:
18999 case DW_FORM_strx1:
19000 case DW_FORM_strx2:
19001 case DW_FORM_strx3:
19002 case DW_FORM_strx4:
19003 case DW_FORM_GNU_str_index:
19004 {
19005 unsigned int str_index = DW_UNSND (attr);
19006 if (reader->dwo_file != NULL)
19007 {
19008 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19009 DW_STRING_IS_CANONICAL (attr) = 0;
19010 }
19011 else
19012 {
19013 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19014 DW_STRING_IS_CANONICAL (attr) = 0;
19015 }
19016 break;
19017 }
19018 default:
19019 gdb_assert_not_reached (_("Unexpected DWARF form."));
19020 }
19021 }
19022
19023 /* Read an attribute value described by an attribute form. */
19024
19025 static const gdb_byte *
19026 read_attribute_value (const struct die_reader_specs *reader,
19027 struct attribute *attr, unsigned form,
19028 LONGEST implicit_const, const gdb_byte *info_ptr,
19029 bool *need_reprocess)
19030 {
19031 struct dwarf2_cu *cu = reader->cu;
19032 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19033 struct objfile *objfile = dwarf2_per_objfile->objfile;
19034 bfd *abfd = reader->abfd;
19035 struct comp_unit_head *cu_header = &cu->header;
19036 unsigned int bytes_read;
19037 struct dwarf_block *blk;
19038 *need_reprocess = false;
19039
19040 attr->form = (enum dwarf_form) form;
19041 switch (form)
19042 {
19043 case DW_FORM_ref_addr:
19044 if (cu->header.version == 2)
19045 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19046 &bytes_read);
19047 else
19048 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19049 &bytes_read);
19050 info_ptr += bytes_read;
19051 break;
19052 case DW_FORM_GNU_ref_alt:
19053 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19054 info_ptr += bytes_read;
19055 break;
19056 case DW_FORM_addr:
19057 {
19058 struct gdbarch *gdbarch = objfile->arch ();
19059 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19060 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19061 info_ptr += bytes_read;
19062 }
19063 break;
19064 case DW_FORM_block2:
19065 blk = dwarf_alloc_block (cu);
19066 blk->size = read_2_bytes (abfd, info_ptr);
19067 info_ptr += 2;
19068 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19069 info_ptr += blk->size;
19070 DW_BLOCK (attr) = blk;
19071 break;
19072 case DW_FORM_block4:
19073 blk = dwarf_alloc_block (cu);
19074 blk->size = read_4_bytes (abfd, info_ptr);
19075 info_ptr += 4;
19076 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19077 info_ptr += blk->size;
19078 DW_BLOCK (attr) = blk;
19079 break;
19080 case DW_FORM_data2:
19081 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19082 info_ptr += 2;
19083 break;
19084 case DW_FORM_data4:
19085 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19086 info_ptr += 4;
19087 break;
19088 case DW_FORM_data8:
19089 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19090 info_ptr += 8;
19091 break;
19092 case DW_FORM_data16:
19093 blk = dwarf_alloc_block (cu);
19094 blk->size = 16;
19095 blk->data = read_n_bytes (abfd, info_ptr, 16);
19096 info_ptr += 16;
19097 DW_BLOCK (attr) = blk;
19098 break;
19099 case DW_FORM_sec_offset:
19100 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19101 info_ptr += bytes_read;
19102 break;
19103 case DW_FORM_loclistx:
19104 {
19105 *need_reprocess = true;
19106 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19107 info_ptr += bytes_read;
19108 }
19109 break;
19110 case DW_FORM_string:
19111 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19112 DW_STRING_IS_CANONICAL (attr) = 0;
19113 info_ptr += bytes_read;
19114 break;
19115 case DW_FORM_strp:
19116 if (!cu->per_cu->is_dwz)
19117 {
19118 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19119 abfd, info_ptr, cu_header,
19120 &bytes_read);
19121 DW_STRING_IS_CANONICAL (attr) = 0;
19122 info_ptr += bytes_read;
19123 break;
19124 }
19125 /* FALLTHROUGH */
19126 case DW_FORM_line_strp:
19127 if (!cu->per_cu->is_dwz)
19128 {
19129 DW_STRING (attr)
19130 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19131 &bytes_read);
19132 DW_STRING_IS_CANONICAL (attr) = 0;
19133 info_ptr += bytes_read;
19134 break;
19135 }
19136 /* FALLTHROUGH */
19137 case DW_FORM_GNU_strp_alt:
19138 {
19139 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19140 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19141 &bytes_read);
19142
19143 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19144 DW_STRING_IS_CANONICAL (attr) = 0;
19145 info_ptr += bytes_read;
19146 }
19147 break;
19148 case DW_FORM_exprloc:
19149 case DW_FORM_block:
19150 blk = dwarf_alloc_block (cu);
19151 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19152 info_ptr += bytes_read;
19153 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19154 info_ptr += blk->size;
19155 DW_BLOCK (attr) = blk;
19156 break;
19157 case DW_FORM_block1:
19158 blk = dwarf_alloc_block (cu);
19159 blk->size = read_1_byte (abfd, info_ptr);
19160 info_ptr += 1;
19161 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19162 info_ptr += blk->size;
19163 DW_BLOCK (attr) = blk;
19164 break;
19165 case DW_FORM_data1:
19166 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19167 info_ptr += 1;
19168 break;
19169 case DW_FORM_flag:
19170 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19171 info_ptr += 1;
19172 break;
19173 case DW_FORM_flag_present:
19174 DW_UNSND (attr) = 1;
19175 break;
19176 case DW_FORM_sdata:
19177 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19178 info_ptr += bytes_read;
19179 break;
19180 case DW_FORM_udata:
19181 case DW_FORM_rnglistx:
19182 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19183 info_ptr += bytes_read;
19184 break;
19185 case DW_FORM_ref1:
19186 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19187 + read_1_byte (abfd, info_ptr));
19188 info_ptr += 1;
19189 break;
19190 case DW_FORM_ref2:
19191 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19192 + read_2_bytes (abfd, info_ptr));
19193 info_ptr += 2;
19194 break;
19195 case DW_FORM_ref4:
19196 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19197 + read_4_bytes (abfd, info_ptr));
19198 info_ptr += 4;
19199 break;
19200 case DW_FORM_ref8:
19201 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19202 + read_8_bytes (abfd, info_ptr));
19203 info_ptr += 8;
19204 break;
19205 case DW_FORM_ref_sig8:
19206 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19207 info_ptr += 8;
19208 break;
19209 case DW_FORM_ref_udata:
19210 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19211 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19212 info_ptr += bytes_read;
19213 break;
19214 case DW_FORM_indirect:
19215 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19216 info_ptr += bytes_read;
19217 if (form == DW_FORM_implicit_const)
19218 {
19219 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19220 info_ptr += bytes_read;
19221 }
19222 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19223 info_ptr, need_reprocess);
19224 break;
19225 case DW_FORM_implicit_const:
19226 DW_SND (attr) = implicit_const;
19227 break;
19228 case DW_FORM_addrx:
19229 case DW_FORM_GNU_addr_index:
19230 *need_reprocess = true;
19231 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19232 info_ptr += bytes_read;
19233 break;
19234 case DW_FORM_strx:
19235 case DW_FORM_strx1:
19236 case DW_FORM_strx2:
19237 case DW_FORM_strx3:
19238 case DW_FORM_strx4:
19239 case DW_FORM_GNU_str_index:
19240 {
19241 ULONGEST str_index;
19242 if (form == DW_FORM_strx1)
19243 {
19244 str_index = read_1_byte (abfd, info_ptr);
19245 info_ptr += 1;
19246 }
19247 else if (form == DW_FORM_strx2)
19248 {
19249 str_index = read_2_bytes (abfd, info_ptr);
19250 info_ptr += 2;
19251 }
19252 else if (form == DW_FORM_strx3)
19253 {
19254 str_index = read_3_bytes (abfd, info_ptr);
19255 info_ptr += 3;
19256 }
19257 else if (form == DW_FORM_strx4)
19258 {
19259 str_index = read_4_bytes (abfd, info_ptr);
19260 info_ptr += 4;
19261 }
19262 else
19263 {
19264 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19265 info_ptr += bytes_read;
19266 }
19267 *need_reprocess = true;
19268 DW_UNSND (attr) = str_index;
19269 }
19270 break;
19271 default:
19272 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19273 dwarf_form_name (form),
19274 bfd_get_filename (abfd));
19275 }
19276
19277 /* Super hack. */
19278 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19279 attr->form = DW_FORM_GNU_ref_alt;
19280
19281 /* We have seen instances where the compiler tried to emit a byte
19282 size attribute of -1 which ended up being encoded as an unsigned
19283 0xffffffff. Although 0xffffffff is technically a valid size value,
19284 an object of this size seems pretty unlikely so we can relatively
19285 safely treat these cases as if the size attribute was invalid and
19286 treat them as zero by default. */
19287 if (attr->name == DW_AT_byte_size
19288 && form == DW_FORM_data4
19289 && DW_UNSND (attr) >= 0xffffffff)
19290 {
19291 complaint
19292 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19293 hex_string (DW_UNSND (attr)));
19294 DW_UNSND (attr) = 0;
19295 }
19296
19297 return info_ptr;
19298 }
19299
19300 /* Read an attribute described by an abbreviated attribute. */
19301
19302 static const gdb_byte *
19303 read_attribute (const struct die_reader_specs *reader,
19304 struct attribute *attr, struct attr_abbrev *abbrev,
19305 const gdb_byte *info_ptr, bool *need_reprocess)
19306 {
19307 attr->name = abbrev->name;
19308 return read_attribute_value (reader, attr, abbrev->form,
19309 abbrev->implicit_const, info_ptr,
19310 need_reprocess);
19311 }
19312
19313 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19314
19315 static const char *
19316 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19317 LONGEST str_offset)
19318 {
19319 return dwarf2_per_objfile->per_bfd->str.read_string
19320 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19321 }
19322
19323 /* Return pointer to string at .debug_str offset as read from BUF.
19324 BUF is assumed to be in a compilation unit described by CU_HEADER.
19325 Return *BYTES_READ_PTR count of bytes read from BUF. */
19326
19327 static const char *
19328 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19329 const gdb_byte *buf,
19330 const struct comp_unit_head *cu_header,
19331 unsigned int *bytes_read_ptr)
19332 {
19333 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19334
19335 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19336 }
19337
19338 /* See read.h. */
19339
19340 const char *
19341 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19342 const struct comp_unit_head *cu_header,
19343 unsigned int *bytes_read_ptr)
19344 {
19345 bfd *abfd = objfile->obfd;
19346 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19347
19348 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19349 }
19350
19351 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19352 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19353 ADDR_SIZE is the size of addresses from the CU header. */
19354
19355 static CORE_ADDR
19356 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19357 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19358 int addr_size)
19359 {
19360 struct objfile *objfile = dwarf2_per_objfile->objfile;
19361 bfd *abfd = objfile->obfd;
19362 const gdb_byte *info_ptr;
19363 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19364
19365 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19366 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19367 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19368 objfile_name (objfile));
19369 if (addr_base_or_zero + addr_index * addr_size
19370 >= dwarf2_per_objfile->per_bfd->addr.size)
19371 error (_("DW_FORM_addr_index pointing outside of "
19372 ".debug_addr section [in module %s]"),
19373 objfile_name (objfile));
19374 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19375 + addr_base_or_zero + addr_index * addr_size);
19376 if (addr_size == 4)
19377 return bfd_get_32 (abfd, info_ptr);
19378 else
19379 return bfd_get_64 (abfd, info_ptr);
19380 }
19381
19382 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19383
19384 static CORE_ADDR
19385 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19386 {
19387 return read_addr_index_1 (cu->per_objfile, addr_index,
19388 cu->addr_base, cu->header.addr_size);
19389 }
19390
19391 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19392
19393 static CORE_ADDR
19394 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19395 unsigned int *bytes_read)
19396 {
19397 bfd *abfd = cu->per_objfile->objfile->obfd;
19398 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19399
19400 return read_addr_index (cu, addr_index);
19401 }
19402
19403 /* See read.h. */
19404
19405 CORE_ADDR
19406 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19407 dwarf2_per_objfile *dwarf2_per_objfile,
19408 unsigned int addr_index)
19409 {
19410 struct dwarf2_cu *cu = per_cu->cu;
19411 gdb::optional<ULONGEST> addr_base;
19412 int addr_size;
19413
19414 /* We need addr_base and addr_size.
19415 If we don't have PER_CU->cu, we have to get it.
19416 Nasty, but the alternative is storing the needed info in PER_CU,
19417 which at this point doesn't seem justified: it's not clear how frequently
19418 it would get used and it would increase the size of every PER_CU.
19419 Entry points like dwarf2_per_cu_addr_size do a similar thing
19420 so we're not in uncharted territory here.
19421 Alas we need to be a bit more complicated as addr_base is contained
19422 in the DIE.
19423
19424 We don't need to read the entire CU(/TU).
19425 We just need the header and top level die.
19426
19427 IWBN to use the aging mechanism to let us lazily later discard the CU.
19428 For now we skip this optimization. */
19429
19430 if (cu != NULL)
19431 {
19432 addr_base = cu->addr_base;
19433 addr_size = cu->header.addr_size;
19434 }
19435 else
19436 {
19437 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19438 addr_base = reader.cu->addr_base;
19439 addr_size = reader.cu->header.addr_size;
19440 }
19441
19442 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19443 addr_size);
19444 }
19445
19446 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19447 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19448 DWO file. */
19449
19450 static const char *
19451 read_str_index (struct dwarf2_cu *cu,
19452 struct dwarf2_section_info *str_section,
19453 struct dwarf2_section_info *str_offsets_section,
19454 ULONGEST str_offsets_base, ULONGEST str_index)
19455 {
19456 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19457 struct objfile *objfile = dwarf2_per_objfile->objfile;
19458 const char *objf_name = objfile_name (objfile);
19459 bfd *abfd = objfile->obfd;
19460 const gdb_byte *info_ptr;
19461 ULONGEST str_offset;
19462 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19463
19464 str_section->read (objfile);
19465 str_offsets_section->read (objfile);
19466 if (str_section->buffer == NULL)
19467 error (_("%s used without %s section"
19468 " in CU at offset %s [in module %s]"),
19469 form_name, str_section->get_name (),
19470 sect_offset_str (cu->header.sect_off), objf_name);
19471 if (str_offsets_section->buffer == NULL)
19472 error (_("%s used without %s section"
19473 " in CU at offset %s [in module %s]"),
19474 form_name, str_section->get_name (),
19475 sect_offset_str (cu->header.sect_off), objf_name);
19476 info_ptr = (str_offsets_section->buffer
19477 + str_offsets_base
19478 + str_index * cu->header.offset_size);
19479 if (cu->header.offset_size == 4)
19480 str_offset = bfd_get_32 (abfd, info_ptr);
19481 else
19482 str_offset = bfd_get_64 (abfd, info_ptr);
19483 if (str_offset >= str_section->size)
19484 error (_("Offset from %s pointing outside of"
19485 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19486 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19487 return (const char *) (str_section->buffer + str_offset);
19488 }
19489
19490 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19491
19492 static const char *
19493 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19494 {
19495 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19496 ? reader->cu->header.addr_size : 0;
19497 return read_str_index (reader->cu,
19498 &reader->dwo_file->sections.str,
19499 &reader->dwo_file->sections.str_offsets,
19500 str_offsets_base, str_index);
19501 }
19502
19503 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19504
19505 static const char *
19506 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19507 {
19508 struct objfile *objfile = cu->per_objfile->objfile;
19509 const char *objf_name = objfile_name (objfile);
19510 static const char form_name[] = "DW_FORM_GNU_str_index";
19511 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19512
19513 if (!cu->str_offsets_base.has_value ())
19514 error (_("%s used in Fission stub without %s"
19515 " in CU at offset 0x%lx [in module %s]"),
19516 form_name, str_offsets_attr_name,
19517 (long) cu->header.offset_size, objf_name);
19518
19519 return read_str_index (cu,
19520 &cu->per_objfile->per_bfd->str,
19521 &cu->per_objfile->per_bfd->str_offsets,
19522 *cu->str_offsets_base, str_index);
19523 }
19524
19525 /* Return the length of an LEB128 number in BUF. */
19526
19527 static int
19528 leb128_size (const gdb_byte *buf)
19529 {
19530 const gdb_byte *begin = buf;
19531 gdb_byte byte;
19532
19533 while (1)
19534 {
19535 byte = *buf++;
19536 if ((byte & 128) == 0)
19537 return buf - begin;
19538 }
19539 }
19540
19541 static void
19542 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19543 {
19544 switch (lang)
19545 {
19546 case DW_LANG_C89:
19547 case DW_LANG_C99:
19548 case DW_LANG_C11:
19549 case DW_LANG_C:
19550 case DW_LANG_UPC:
19551 cu->language = language_c;
19552 break;
19553 case DW_LANG_Java:
19554 case DW_LANG_C_plus_plus:
19555 case DW_LANG_C_plus_plus_11:
19556 case DW_LANG_C_plus_plus_14:
19557 cu->language = language_cplus;
19558 break;
19559 case DW_LANG_D:
19560 cu->language = language_d;
19561 break;
19562 case DW_LANG_Fortran77:
19563 case DW_LANG_Fortran90:
19564 case DW_LANG_Fortran95:
19565 case DW_LANG_Fortran03:
19566 case DW_LANG_Fortran08:
19567 cu->language = language_fortran;
19568 break;
19569 case DW_LANG_Go:
19570 cu->language = language_go;
19571 break;
19572 case DW_LANG_Mips_Assembler:
19573 cu->language = language_asm;
19574 break;
19575 case DW_LANG_Ada83:
19576 case DW_LANG_Ada95:
19577 cu->language = language_ada;
19578 break;
19579 case DW_LANG_Modula2:
19580 cu->language = language_m2;
19581 break;
19582 case DW_LANG_Pascal83:
19583 cu->language = language_pascal;
19584 break;
19585 case DW_LANG_ObjC:
19586 cu->language = language_objc;
19587 break;
19588 case DW_LANG_Rust:
19589 case DW_LANG_Rust_old:
19590 cu->language = language_rust;
19591 break;
19592 case DW_LANG_Cobol74:
19593 case DW_LANG_Cobol85:
19594 default:
19595 cu->language = language_minimal;
19596 break;
19597 }
19598 cu->language_defn = language_def (cu->language);
19599 }
19600
19601 /* Return the named attribute or NULL if not there. */
19602
19603 static struct attribute *
19604 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19605 {
19606 for (;;)
19607 {
19608 unsigned int i;
19609 struct attribute *spec = NULL;
19610
19611 for (i = 0; i < die->num_attrs; ++i)
19612 {
19613 if (die->attrs[i].name == name)
19614 return &die->attrs[i];
19615 if (die->attrs[i].name == DW_AT_specification
19616 || die->attrs[i].name == DW_AT_abstract_origin)
19617 spec = &die->attrs[i];
19618 }
19619
19620 if (!spec)
19621 break;
19622
19623 die = follow_die_ref (die, spec, &cu);
19624 }
19625
19626 return NULL;
19627 }
19628
19629 /* Return the string associated with a string-typed attribute, or NULL if it
19630 is either not found or is of an incorrect type. */
19631
19632 static const char *
19633 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19634 {
19635 struct attribute *attr;
19636 const char *str = NULL;
19637
19638 attr = dwarf2_attr (die, name, cu);
19639
19640 if (attr != NULL)
19641 {
19642 str = attr->value_as_string ();
19643 if (str == nullptr)
19644 complaint (_("string type expected for attribute %s for "
19645 "DIE at %s in module %s"),
19646 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19647 objfile_name (cu->per_objfile->objfile));
19648 }
19649
19650 return str;
19651 }
19652
19653 /* Return the dwo name or NULL if not present. If present, it is in either
19654 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19655 static const char *
19656 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19657 {
19658 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19659 if (dwo_name == nullptr)
19660 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19661 return dwo_name;
19662 }
19663
19664 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19665 and holds a non-zero value. This function should only be used for
19666 DW_FORM_flag or DW_FORM_flag_present attributes. */
19667
19668 static int
19669 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19670 {
19671 struct attribute *attr = dwarf2_attr (die, name, cu);
19672
19673 return (attr && DW_UNSND (attr));
19674 }
19675
19676 static int
19677 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19678 {
19679 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19680 which value is non-zero. However, we have to be careful with
19681 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19682 (via dwarf2_flag_true_p) follows this attribute. So we may
19683 end up accidently finding a declaration attribute that belongs
19684 to a different DIE referenced by the specification attribute,
19685 even though the given DIE does not have a declaration attribute. */
19686 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19687 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19688 }
19689
19690 /* Return the die giving the specification for DIE, if there is
19691 one. *SPEC_CU is the CU containing DIE on input, and the CU
19692 containing the return value on output. If there is no
19693 specification, but there is an abstract origin, that is
19694 returned. */
19695
19696 static struct die_info *
19697 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19698 {
19699 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19700 *spec_cu);
19701
19702 if (spec_attr == NULL)
19703 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19704
19705 if (spec_attr == NULL)
19706 return NULL;
19707 else
19708 return follow_die_ref (die, spec_attr, spec_cu);
19709 }
19710
19711 /* Stub for free_line_header to match void * callback types. */
19712
19713 static void
19714 free_line_header_voidp (void *arg)
19715 {
19716 struct line_header *lh = (struct line_header *) arg;
19717
19718 delete lh;
19719 }
19720
19721 /* A convenience function to find the proper .debug_line section for a CU. */
19722
19723 static struct dwarf2_section_info *
19724 get_debug_line_section (struct dwarf2_cu *cu)
19725 {
19726 struct dwarf2_section_info *section;
19727 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19728
19729 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19730 DWO file. */
19731 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19732 section = &cu->dwo_unit->dwo_file->sections.line;
19733 else if (cu->per_cu->is_dwz)
19734 {
19735 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19736
19737 section = &dwz->line;
19738 }
19739 else
19740 section = &dwarf2_per_objfile->per_bfd->line;
19741
19742 return section;
19743 }
19744
19745 /* Read the statement program header starting at OFFSET in
19746 .debug_line, or .debug_line.dwo. Return a pointer
19747 to a struct line_header, allocated using xmalloc.
19748 Returns NULL if there is a problem reading the header, e.g., if it
19749 has a version we don't understand.
19750
19751 NOTE: the strings in the include directory and file name tables of
19752 the returned object point into the dwarf line section buffer,
19753 and must not be freed. */
19754
19755 static line_header_up
19756 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19757 {
19758 struct dwarf2_section_info *section;
19759 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19760
19761 section = get_debug_line_section (cu);
19762 section->read (dwarf2_per_objfile->objfile);
19763 if (section->buffer == NULL)
19764 {
19765 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19766 complaint (_("missing .debug_line.dwo section"));
19767 else
19768 complaint (_("missing .debug_line section"));
19769 return 0;
19770 }
19771
19772 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19773 dwarf2_per_objfile, section,
19774 &cu->header);
19775 }
19776
19777 /* Subroutine of dwarf_decode_lines to simplify it.
19778 Return the file name of the psymtab for the given file_entry.
19779 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19780 If space for the result is malloc'd, *NAME_HOLDER will be set.
19781 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19782
19783 static const char *
19784 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19785 const dwarf2_psymtab *pst,
19786 const char *comp_dir,
19787 gdb::unique_xmalloc_ptr<char> *name_holder)
19788 {
19789 const char *include_name = fe.name;
19790 const char *include_name_to_compare = include_name;
19791 const char *pst_filename;
19792 int file_is_pst;
19793
19794 const char *dir_name = fe.include_dir (lh);
19795
19796 gdb::unique_xmalloc_ptr<char> hold_compare;
19797 if (!IS_ABSOLUTE_PATH (include_name)
19798 && (dir_name != NULL || comp_dir != NULL))
19799 {
19800 /* Avoid creating a duplicate psymtab for PST.
19801 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19802 Before we do the comparison, however, we need to account
19803 for DIR_NAME and COMP_DIR.
19804 First prepend dir_name (if non-NULL). If we still don't
19805 have an absolute path prepend comp_dir (if non-NULL).
19806 However, the directory we record in the include-file's
19807 psymtab does not contain COMP_DIR (to match the
19808 corresponding symtab(s)).
19809
19810 Example:
19811
19812 bash$ cd /tmp
19813 bash$ gcc -g ./hello.c
19814 include_name = "hello.c"
19815 dir_name = "."
19816 DW_AT_comp_dir = comp_dir = "/tmp"
19817 DW_AT_name = "./hello.c"
19818
19819 */
19820
19821 if (dir_name != NULL)
19822 {
19823 name_holder->reset (concat (dir_name, SLASH_STRING,
19824 include_name, (char *) NULL));
19825 include_name = name_holder->get ();
19826 include_name_to_compare = include_name;
19827 }
19828 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19829 {
19830 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19831 include_name, (char *) NULL));
19832 include_name_to_compare = hold_compare.get ();
19833 }
19834 }
19835
19836 pst_filename = pst->filename;
19837 gdb::unique_xmalloc_ptr<char> copied_name;
19838 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19839 {
19840 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19841 pst_filename, (char *) NULL));
19842 pst_filename = copied_name.get ();
19843 }
19844
19845 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19846
19847 if (file_is_pst)
19848 return NULL;
19849 return include_name;
19850 }
19851
19852 /* State machine to track the state of the line number program. */
19853
19854 class lnp_state_machine
19855 {
19856 public:
19857 /* Initialize a machine state for the start of a line number
19858 program. */
19859 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19860 bool record_lines_p);
19861
19862 file_entry *current_file ()
19863 {
19864 /* lh->file_names is 0-based, but the file name numbers in the
19865 statement program are 1-based. */
19866 return m_line_header->file_name_at (m_file);
19867 }
19868
19869 /* Record the line in the state machine. END_SEQUENCE is true if
19870 we're processing the end of a sequence. */
19871 void record_line (bool end_sequence);
19872
19873 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19874 nop-out rest of the lines in this sequence. */
19875 void check_line_address (struct dwarf2_cu *cu,
19876 const gdb_byte *line_ptr,
19877 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19878
19879 void handle_set_discriminator (unsigned int discriminator)
19880 {
19881 m_discriminator = discriminator;
19882 m_line_has_non_zero_discriminator |= discriminator != 0;
19883 }
19884
19885 /* Handle DW_LNE_set_address. */
19886 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19887 {
19888 m_op_index = 0;
19889 address += baseaddr;
19890 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19891 }
19892
19893 /* Handle DW_LNS_advance_pc. */
19894 void handle_advance_pc (CORE_ADDR adjust);
19895
19896 /* Handle a special opcode. */
19897 void handle_special_opcode (unsigned char op_code);
19898
19899 /* Handle DW_LNS_advance_line. */
19900 void handle_advance_line (int line_delta)
19901 {
19902 advance_line (line_delta);
19903 }
19904
19905 /* Handle DW_LNS_set_file. */
19906 void handle_set_file (file_name_index file);
19907
19908 /* Handle DW_LNS_negate_stmt. */
19909 void handle_negate_stmt ()
19910 {
19911 m_is_stmt = !m_is_stmt;
19912 }
19913
19914 /* Handle DW_LNS_const_add_pc. */
19915 void handle_const_add_pc ();
19916
19917 /* Handle DW_LNS_fixed_advance_pc. */
19918 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19919 {
19920 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19921 m_op_index = 0;
19922 }
19923
19924 /* Handle DW_LNS_copy. */
19925 void handle_copy ()
19926 {
19927 record_line (false);
19928 m_discriminator = 0;
19929 }
19930
19931 /* Handle DW_LNE_end_sequence. */
19932 void handle_end_sequence ()
19933 {
19934 m_currently_recording_lines = true;
19935 }
19936
19937 private:
19938 /* Advance the line by LINE_DELTA. */
19939 void advance_line (int line_delta)
19940 {
19941 m_line += line_delta;
19942
19943 if (line_delta != 0)
19944 m_line_has_non_zero_discriminator = m_discriminator != 0;
19945 }
19946
19947 struct dwarf2_cu *m_cu;
19948
19949 gdbarch *m_gdbarch;
19950
19951 /* True if we're recording lines.
19952 Otherwise we're building partial symtabs and are just interested in
19953 finding include files mentioned by the line number program. */
19954 bool m_record_lines_p;
19955
19956 /* The line number header. */
19957 line_header *m_line_header;
19958
19959 /* These are part of the standard DWARF line number state machine,
19960 and initialized according to the DWARF spec. */
19961
19962 unsigned char m_op_index = 0;
19963 /* The line table index of the current file. */
19964 file_name_index m_file = 1;
19965 unsigned int m_line = 1;
19966
19967 /* These are initialized in the constructor. */
19968
19969 CORE_ADDR m_address;
19970 bool m_is_stmt;
19971 unsigned int m_discriminator;
19972
19973 /* Additional bits of state we need to track. */
19974
19975 /* The last file that we called dwarf2_start_subfile for.
19976 This is only used for TLLs. */
19977 unsigned int m_last_file = 0;
19978 /* The last file a line number was recorded for. */
19979 struct subfile *m_last_subfile = NULL;
19980
19981 /* When true, record the lines we decode. */
19982 bool m_currently_recording_lines = false;
19983
19984 /* The last line number that was recorded, used to coalesce
19985 consecutive entries for the same line. This can happen, for
19986 example, when discriminators are present. PR 17276. */
19987 unsigned int m_last_line = 0;
19988 bool m_line_has_non_zero_discriminator = false;
19989 };
19990
19991 void
19992 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19993 {
19994 CORE_ADDR addr_adj = (((m_op_index + adjust)
19995 / m_line_header->maximum_ops_per_instruction)
19996 * m_line_header->minimum_instruction_length);
19997 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19998 m_op_index = ((m_op_index + adjust)
19999 % m_line_header->maximum_ops_per_instruction);
20000 }
20001
20002 void
20003 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20004 {
20005 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20006 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20007 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20008 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20009 / m_line_header->maximum_ops_per_instruction)
20010 * m_line_header->minimum_instruction_length);
20011 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20012 m_op_index = ((m_op_index + adj_opcode_d)
20013 % m_line_header->maximum_ops_per_instruction);
20014
20015 int line_delta = m_line_header->line_base + adj_opcode_r;
20016 advance_line (line_delta);
20017 record_line (false);
20018 m_discriminator = 0;
20019 }
20020
20021 void
20022 lnp_state_machine::handle_set_file (file_name_index file)
20023 {
20024 m_file = file;
20025
20026 const file_entry *fe = current_file ();
20027 if (fe == NULL)
20028 dwarf2_debug_line_missing_file_complaint ();
20029 else if (m_record_lines_p)
20030 {
20031 const char *dir = fe->include_dir (m_line_header);
20032
20033 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20034 m_line_has_non_zero_discriminator = m_discriminator != 0;
20035 dwarf2_start_subfile (m_cu, fe->name, dir);
20036 }
20037 }
20038
20039 void
20040 lnp_state_machine::handle_const_add_pc ()
20041 {
20042 CORE_ADDR adjust
20043 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20044
20045 CORE_ADDR addr_adj
20046 = (((m_op_index + adjust)
20047 / m_line_header->maximum_ops_per_instruction)
20048 * m_line_header->minimum_instruction_length);
20049
20050 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20051 m_op_index = ((m_op_index + adjust)
20052 % m_line_header->maximum_ops_per_instruction);
20053 }
20054
20055 /* Return non-zero if we should add LINE to the line number table.
20056 LINE is the line to add, LAST_LINE is the last line that was added,
20057 LAST_SUBFILE is the subfile for LAST_LINE.
20058 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20059 had a non-zero discriminator.
20060
20061 We have to be careful in the presence of discriminators.
20062 E.g., for this line:
20063
20064 for (i = 0; i < 100000; i++);
20065
20066 clang can emit four line number entries for that one line,
20067 each with a different discriminator.
20068 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20069
20070 However, we want gdb to coalesce all four entries into one.
20071 Otherwise the user could stepi into the middle of the line and
20072 gdb would get confused about whether the pc really was in the
20073 middle of the line.
20074
20075 Things are further complicated by the fact that two consecutive
20076 line number entries for the same line is a heuristic used by gcc
20077 to denote the end of the prologue. So we can't just discard duplicate
20078 entries, we have to be selective about it. The heuristic we use is
20079 that we only collapse consecutive entries for the same line if at least
20080 one of those entries has a non-zero discriminator. PR 17276.
20081
20082 Note: Addresses in the line number state machine can never go backwards
20083 within one sequence, thus this coalescing is ok. */
20084
20085 static int
20086 dwarf_record_line_p (struct dwarf2_cu *cu,
20087 unsigned int line, unsigned int last_line,
20088 int line_has_non_zero_discriminator,
20089 struct subfile *last_subfile)
20090 {
20091 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20092 return 1;
20093 if (line != last_line)
20094 return 1;
20095 /* Same line for the same file that we've seen already.
20096 As a last check, for pr 17276, only record the line if the line
20097 has never had a non-zero discriminator. */
20098 if (!line_has_non_zero_discriminator)
20099 return 1;
20100 return 0;
20101 }
20102
20103 /* Use the CU's builder to record line number LINE beginning at
20104 address ADDRESS in the line table of subfile SUBFILE. */
20105
20106 static void
20107 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20108 unsigned int line, CORE_ADDR address, bool is_stmt,
20109 struct dwarf2_cu *cu)
20110 {
20111 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20112
20113 if (dwarf_line_debug)
20114 {
20115 fprintf_unfiltered (gdb_stdlog,
20116 "Recording line %u, file %s, address %s\n",
20117 line, lbasename (subfile->name),
20118 paddress (gdbarch, address));
20119 }
20120
20121 if (cu != nullptr)
20122 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20123 }
20124
20125 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20126 Mark the end of a set of line number records.
20127 The arguments are the same as for dwarf_record_line_1.
20128 If SUBFILE is NULL the request is ignored. */
20129
20130 static void
20131 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20132 CORE_ADDR address, struct dwarf2_cu *cu)
20133 {
20134 if (subfile == NULL)
20135 return;
20136
20137 if (dwarf_line_debug)
20138 {
20139 fprintf_unfiltered (gdb_stdlog,
20140 "Finishing current line, file %s, address %s\n",
20141 lbasename (subfile->name),
20142 paddress (gdbarch, address));
20143 }
20144
20145 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20146 }
20147
20148 void
20149 lnp_state_machine::record_line (bool end_sequence)
20150 {
20151 if (dwarf_line_debug)
20152 {
20153 fprintf_unfiltered (gdb_stdlog,
20154 "Processing actual line %u: file %u,"
20155 " address %s, is_stmt %u, discrim %u%s\n",
20156 m_line, m_file,
20157 paddress (m_gdbarch, m_address),
20158 m_is_stmt, m_discriminator,
20159 (end_sequence ? "\t(end sequence)" : ""));
20160 }
20161
20162 file_entry *fe = current_file ();
20163
20164 if (fe == NULL)
20165 dwarf2_debug_line_missing_file_complaint ();
20166 /* For now we ignore lines not starting on an instruction boundary.
20167 But not when processing end_sequence for compatibility with the
20168 previous version of the code. */
20169 else if (m_op_index == 0 || end_sequence)
20170 {
20171 fe->included_p = 1;
20172 if (m_record_lines_p)
20173 {
20174 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20175 || end_sequence)
20176 {
20177 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20178 m_currently_recording_lines ? m_cu : nullptr);
20179 }
20180
20181 if (!end_sequence)
20182 {
20183 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20184
20185 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20186 m_line_has_non_zero_discriminator,
20187 m_last_subfile))
20188 {
20189 buildsym_compunit *builder = m_cu->get_builder ();
20190 dwarf_record_line_1 (m_gdbarch,
20191 builder->get_current_subfile (),
20192 m_line, m_address, is_stmt,
20193 m_currently_recording_lines ? m_cu : nullptr);
20194 }
20195 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20196 m_last_line = m_line;
20197 }
20198 }
20199 }
20200 }
20201
20202 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20203 line_header *lh, bool record_lines_p)
20204 {
20205 m_cu = cu;
20206 m_gdbarch = arch;
20207 m_record_lines_p = record_lines_p;
20208 m_line_header = lh;
20209
20210 m_currently_recording_lines = true;
20211
20212 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20213 was a line entry for it so that the backend has a chance to adjust it
20214 and also record it in case it needs it. This is currently used by MIPS
20215 code, cf. `mips_adjust_dwarf2_line'. */
20216 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20217 m_is_stmt = lh->default_is_stmt;
20218 m_discriminator = 0;
20219 }
20220
20221 void
20222 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20223 const gdb_byte *line_ptr,
20224 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20225 {
20226 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20227 the pc range of the CU. However, we restrict the test to only ADDRESS
20228 values of zero to preserve GDB's previous behaviour which is to handle
20229 the specific case of a function being GC'd by the linker. */
20230
20231 if (address == 0 && address < unrelocated_lowpc)
20232 {
20233 /* This line table is for a function which has been
20234 GCd by the linker. Ignore it. PR gdb/12528 */
20235
20236 struct objfile *objfile = cu->per_objfile->objfile;
20237 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20238
20239 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20240 line_offset, objfile_name (objfile));
20241 m_currently_recording_lines = false;
20242 /* Note: m_currently_recording_lines is left as false until we see
20243 DW_LNE_end_sequence. */
20244 }
20245 }
20246
20247 /* Subroutine of dwarf_decode_lines to simplify it.
20248 Process the line number information in LH.
20249 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20250 program in order to set included_p for every referenced header. */
20251
20252 static void
20253 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20254 const int decode_for_pst_p, CORE_ADDR lowpc)
20255 {
20256 const gdb_byte *line_ptr, *extended_end;
20257 const gdb_byte *line_end;
20258 unsigned int bytes_read, extended_len;
20259 unsigned char op_code, extended_op;
20260 CORE_ADDR baseaddr;
20261 struct objfile *objfile = cu->per_objfile->objfile;
20262 bfd *abfd = objfile->obfd;
20263 struct gdbarch *gdbarch = objfile->arch ();
20264 /* True if we're recording line info (as opposed to building partial
20265 symtabs and just interested in finding include files mentioned by
20266 the line number program). */
20267 bool record_lines_p = !decode_for_pst_p;
20268
20269 baseaddr = objfile->text_section_offset ();
20270
20271 line_ptr = lh->statement_program_start;
20272 line_end = lh->statement_program_end;
20273
20274 /* Read the statement sequences until there's nothing left. */
20275 while (line_ptr < line_end)
20276 {
20277 /* The DWARF line number program state machine. Reset the state
20278 machine at the start of each sequence. */
20279 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20280 bool end_sequence = false;
20281
20282 if (record_lines_p)
20283 {
20284 /* Start a subfile for the current file of the state
20285 machine. */
20286 const file_entry *fe = state_machine.current_file ();
20287
20288 if (fe != NULL)
20289 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20290 }
20291
20292 /* Decode the table. */
20293 while (line_ptr < line_end && !end_sequence)
20294 {
20295 op_code = read_1_byte (abfd, line_ptr);
20296 line_ptr += 1;
20297
20298 if (op_code >= lh->opcode_base)
20299 {
20300 /* Special opcode. */
20301 state_machine.handle_special_opcode (op_code);
20302 }
20303 else switch (op_code)
20304 {
20305 case DW_LNS_extended_op:
20306 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20307 &bytes_read);
20308 line_ptr += bytes_read;
20309 extended_end = line_ptr + extended_len;
20310 extended_op = read_1_byte (abfd, line_ptr);
20311 line_ptr += 1;
20312 switch (extended_op)
20313 {
20314 case DW_LNE_end_sequence:
20315 state_machine.handle_end_sequence ();
20316 end_sequence = true;
20317 break;
20318 case DW_LNE_set_address:
20319 {
20320 CORE_ADDR address
20321 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20322 line_ptr += bytes_read;
20323
20324 state_machine.check_line_address (cu, line_ptr,
20325 lowpc - baseaddr, address);
20326 state_machine.handle_set_address (baseaddr, address);
20327 }
20328 break;
20329 case DW_LNE_define_file:
20330 {
20331 const char *cur_file;
20332 unsigned int mod_time, length;
20333 dir_index dindex;
20334
20335 cur_file = read_direct_string (abfd, line_ptr,
20336 &bytes_read);
20337 line_ptr += bytes_read;
20338 dindex = (dir_index)
20339 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20340 line_ptr += bytes_read;
20341 mod_time =
20342 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20343 line_ptr += bytes_read;
20344 length =
20345 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20346 line_ptr += bytes_read;
20347 lh->add_file_name (cur_file, dindex, mod_time, length);
20348 }
20349 break;
20350 case DW_LNE_set_discriminator:
20351 {
20352 /* The discriminator is not interesting to the
20353 debugger; just ignore it. We still need to
20354 check its value though:
20355 if there are consecutive entries for the same
20356 (non-prologue) line we want to coalesce them.
20357 PR 17276. */
20358 unsigned int discr
20359 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20360 line_ptr += bytes_read;
20361
20362 state_machine.handle_set_discriminator (discr);
20363 }
20364 break;
20365 default:
20366 complaint (_("mangled .debug_line section"));
20367 return;
20368 }
20369 /* Make sure that we parsed the extended op correctly. If e.g.
20370 we expected a different address size than the producer used,
20371 we may have read the wrong number of bytes. */
20372 if (line_ptr != extended_end)
20373 {
20374 complaint (_("mangled .debug_line section"));
20375 return;
20376 }
20377 break;
20378 case DW_LNS_copy:
20379 state_machine.handle_copy ();
20380 break;
20381 case DW_LNS_advance_pc:
20382 {
20383 CORE_ADDR adjust
20384 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20385 line_ptr += bytes_read;
20386
20387 state_machine.handle_advance_pc (adjust);
20388 }
20389 break;
20390 case DW_LNS_advance_line:
20391 {
20392 int line_delta
20393 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20394 line_ptr += bytes_read;
20395
20396 state_machine.handle_advance_line (line_delta);
20397 }
20398 break;
20399 case DW_LNS_set_file:
20400 {
20401 file_name_index file
20402 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20403 &bytes_read);
20404 line_ptr += bytes_read;
20405
20406 state_machine.handle_set_file (file);
20407 }
20408 break;
20409 case DW_LNS_set_column:
20410 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20411 line_ptr += bytes_read;
20412 break;
20413 case DW_LNS_negate_stmt:
20414 state_machine.handle_negate_stmt ();
20415 break;
20416 case DW_LNS_set_basic_block:
20417 break;
20418 /* Add to the address register of the state machine the
20419 address increment value corresponding to special opcode
20420 255. I.e., this value is scaled by the minimum
20421 instruction length since special opcode 255 would have
20422 scaled the increment. */
20423 case DW_LNS_const_add_pc:
20424 state_machine.handle_const_add_pc ();
20425 break;
20426 case DW_LNS_fixed_advance_pc:
20427 {
20428 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20429 line_ptr += 2;
20430
20431 state_machine.handle_fixed_advance_pc (addr_adj);
20432 }
20433 break;
20434 default:
20435 {
20436 /* Unknown standard opcode, ignore it. */
20437 int i;
20438
20439 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20440 {
20441 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20442 line_ptr += bytes_read;
20443 }
20444 }
20445 }
20446 }
20447
20448 if (!end_sequence)
20449 dwarf2_debug_line_missing_end_sequence_complaint ();
20450
20451 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20452 in which case we still finish recording the last line). */
20453 state_machine.record_line (true);
20454 }
20455 }
20456
20457 /* Decode the Line Number Program (LNP) for the given line_header
20458 structure and CU. The actual information extracted and the type
20459 of structures created from the LNP depends on the value of PST.
20460
20461 1. If PST is NULL, then this procedure uses the data from the program
20462 to create all necessary symbol tables, and their linetables.
20463
20464 2. If PST is not NULL, this procedure reads the program to determine
20465 the list of files included by the unit represented by PST, and
20466 builds all the associated partial symbol tables.
20467
20468 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20469 It is used for relative paths in the line table.
20470 NOTE: When processing partial symtabs (pst != NULL),
20471 comp_dir == pst->dirname.
20472
20473 NOTE: It is important that psymtabs have the same file name (via strcmp)
20474 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20475 symtab we don't use it in the name of the psymtabs we create.
20476 E.g. expand_line_sal requires this when finding psymtabs to expand.
20477 A good testcase for this is mb-inline.exp.
20478
20479 LOWPC is the lowest address in CU (or 0 if not known).
20480
20481 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20482 for its PC<->lines mapping information. Otherwise only the filename
20483 table is read in. */
20484
20485 static void
20486 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20487 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20488 CORE_ADDR lowpc, int decode_mapping)
20489 {
20490 struct objfile *objfile = cu->per_objfile->objfile;
20491 const int decode_for_pst_p = (pst != NULL);
20492
20493 if (decode_mapping)
20494 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20495
20496 if (decode_for_pst_p)
20497 {
20498 /* Now that we're done scanning the Line Header Program, we can
20499 create the psymtab of each included file. */
20500 for (auto &file_entry : lh->file_names ())
20501 if (file_entry.included_p == 1)
20502 {
20503 gdb::unique_xmalloc_ptr<char> name_holder;
20504 const char *include_name =
20505 psymtab_include_file_name (lh, file_entry, pst,
20506 comp_dir, &name_holder);
20507 if (include_name != NULL)
20508 dwarf2_create_include_psymtab (include_name, pst, objfile);
20509 }
20510 }
20511 else
20512 {
20513 /* Make sure a symtab is created for every file, even files
20514 which contain only variables (i.e. no code with associated
20515 line numbers). */
20516 buildsym_compunit *builder = cu->get_builder ();
20517 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20518
20519 for (auto &fe : lh->file_names ())
20520 {
20521 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20522 if (builder->get_current_subfile ()->symtab == NULL)
20523 {
20524 builder->get_current_subfile ()->symtab
20525 = allocate_symtab (cust,
20526 builder->get_current_subfile ()->name);
20527 }
20528 fe.symtab = builder->get_current_subfile ()->symtab;
20529 }
20530 }
20531 }
20532
20533 /* Start a subfile for DWARF. FILENAME is the name of the file and
20534 DIRNAME the name of the source directory which contains FILENAME
20535 or NULL if not known.
20536 This routine tries to keep line numbers from identical absolute and
20537 relative file names in a common subfile.
20538
20539 Using the `list' example from the GDB testsuite, which resides in
20540 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20541 of /srcdir/list0.c yields the following debugging information for list0.c:
20542
20543 DW_AT_name: /srcdir/list0.c
20544 DW_AT_comp_dir: /compdir
20545 files.files[0].name: list0.h
20546 files.files[0].dir: /srcdir
20547 files.files[1].name: list0.c
20548 files.files[1].dir: /srcdir
20549
20550 The line number information for list0.c has to end up in a single
20551 subfile, so that `break /srcdir/list0.c:1' works as expected.
20552 start_subfile will ensure that this happens provided that we pass the
20553 concatenation of files.files[1].dir and files.files[1].name as the
20554 subfile's name. */
20555
20556 static void
20557 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20558 const char *dirname)
20559 {
20560 gdb::unique_xmalloc_ptr<char> copy;
20561
20562 /* In order not to lose the line information directory,
20563 we concatenate it to the filename when it makes sense.
20564 Note that the Dwarf3 standard says (speaking of filenames in line
20565 information): ``The directory index is ignored for file names
20566 that represent full path names''. Thus ignoring dirname in the
20567 `else' branch below isn't an issue. */
20568
20569 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20570 {
20571 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20572 filename = copy.get ();
20573 }
20574
20575 cu->get_builder ()->start_subfile (filename);
20576 }
20577
20578 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20579 buildsym_compunit constructor. */
20580
20581 struct compunit_symtab *
20582 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20583 CORE_ADDR low_pc)
20584 {
20585 gdb_assert (m_builder == nullptr);
20586
20587 m_builder.reset (new struct buildsym_compunit
20588 (this->per_objfile->objfile,
20589 name, comp_dir, language, low_pc));
20590
20591 list_in_scope = get_builder ()->get_file_symbols ();
20592
20593 get_builder ()->record_debugformat ("DWARF 2");
20594 get_builder ()->record_producer (producer);
20595
20596 processing_has_namespace_info = false;
20597
20598 return get_builder ()->get_compunit_symtab ();
20599 }
20600
20601 static void
20602 var_decode_location (struct attribute *attr, struct symbol *sym,
20603 struct dwarf2_cu *cu)
20604 {
20605 struct objfile *objfile = cu->per_objfile->objfile;
20606 struct comp_unit_head *cu_header = &cu->header;
20607
20608 /* NOTE drow/2003-01-30: There used to be a comment and some special
20609 code here to turn a symbol with DW_AT_external and a
20610 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20611 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20612 with some versions of binutils) where shared libraries could have
20613 relocations against symbols in their debug information - the
20614 minimal symbol would have the right address, but the debug info
20615 would not. It's no longer necessary, because we will explicitly
20616 apply relocations when we read in the debug information now. */
20617
20618 /* A DW_AT_location attribute with no contents indicates that a
20619 variable has been optimized away. */
20620 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20621 {
20622 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20623 return;
20624 }
20625
20626 /* Handle one degenerate form of location expression specially, to
20627 preserve GDB's previous behavior when section offsets are
20628 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20629 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20630
20631 if (attr->form_is_block ()
20632 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20633 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20634 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20635 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20636 && (DW_BLOCK (attr)->size
20637 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20638 {
20639 unsigned int dummy;
20640
20641 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20642 SET_SYMBOL_VALUE_ADDRESS
20643 (sym, cu->header.read_address (objfile->obfd,
20644 DW_BLOCK (attr)->data + 1,
20645 &dummy));
20646 else
20647 SET_SYMBOL_VALUE_ADDRESS
20648 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20649 &dummy));
20650 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20651 fixup_symbol_section (sym, objfile);
20652 SET_SYMBOL_VALUE_ADDRESS
20653 (sym,
20654 SYMBOL_VALUE_ADDRESS (sym)
20655 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20656 return;
20657 }
20658
20659 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20660 expression evaluator, and use LOC_COMPUTED only when necessary
20661 (i.e. when the value of a register or memory location is
20662 referenced, or a thread-local block, etc.). Then again, it might
20663 not be worthwhile. I'm assuming that it isn't unless performance
20664 or memory numbers show me otherwise. */
20665
20666 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20667
20668 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20669 cu->has_loclist = true;
20670 }
20671
20672 /* Given a pointer to a DWARF information entry, figure out if we need
20673 to make a symbol table entry for it, and if so, create a new entry
20674 and return a pointer to it.
20675 If TYPE is NULL, determine symbol type from the die, otherwise
20676 used the passed type.
20677 If SPACE is not NULL, use it to hold the new symbol. If it is
20678 NULL, allocate a new symbol on the objfile's obstack. */
20679
20680 static struct symbol *
20681 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20682 struct symbol *space)
20683 {
20684 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20685 struct objfile *objfile = dwarf2_per_objfile->objfile;
20686 struct gdbarch *gdbarch = objfile->arch ();
20687 struct symbol *sym = NULL;
20688 const char *name;
20689 struct attribute *attr = NULL;
20690 struct attribute *attr2 = NULL;
20691 CORE_ADDR baseaddr;
20692 struct pending **list_to_add = NULL;
20693
20694 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20695
20696 baseaddr = objfile->text_section_offset ();
20697
20698 name = dwarf2_name (die, cu);
20699 if (name)
20700 {
20701 int suppress_add = 0;
20702
20703 if (space)
20704 sym = space;
20705 else
20706 sym = new (&objfile->objfile_obstack) symbol;
20707 OBJSTAT (objfile, n_syms++);
20708
20709 /* Cache this symbol's name and the name's demangled form (if any). */
20710 sym->set_language (cu->language, &objfile->objfile_obstack);
20711 /* Fortran does not have mangling standard and the mangling does differ
20712 between gfortran, iFort etc. */
20713 const char *physname
20714 = (cu->language == language_fortran
20715 ? dwarf2_full_name (name, die, cu)
20716 : dwarf2_physname (name, die, cu));
20717 const char *linkagename = dw2_linkage_name (die, cu);
20718
20719 if (linkagename == nullptr || cu->language == language_ada)
20720 sym->set_linkage_name (physname);
20721 else
20722 {
20723 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20724 sym->set_linkage_name (linkagename);
20725 }
20726
20727 /* Default assumptions.
20728 Use the passed type or decode it from the die. */
20729 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20730 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20731 if (type != NULL)
20732 SYMBOL_TYPE (sym) = type;
20733 else
20734 SYMBOL_TYPE (sym) = die_type (die, cu);
20735 attr = dwarf2_attr (die,
20736 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20737 cu);
20738 if (attr != nullptr)
20739 {
20740 SYMBOL_LINE (sym) = DW_UNSND (attr);
20741 }
20742
20743 attr = dwarf2_attr (die,
20744 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20745 cu);
20746 if (attr != nullptr)
20747 {
20748 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20749 struct file_entry *fe;
20750
20751 if (cu->line_header != NULL)
20752 fe = cu->line_header->file_name_at (file_index);
20753 else
20754 fe = NULL;
20755
20756 if (fe == NULL)
20757 complaint (_("file index out of range"));
20758 else
20759 symbol_set_symtab (sym, fe->symtab);
20760 }
20761
20762 switch (die->tag)
20763 {
20764 case DW_TAG_label:
20765 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20766 if (attr != nullptr)
20767 {
20768 CORE_ADDR addr;
20769
20770 addr = attr->value_as_address ();
20771 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20772 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20773 }
20774 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20775 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20776 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20777 add_symbol_to_list (sym, cu->list_in_scope);
20778 break;
20779 case DW_TAG_subprogram:
20780 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20781 finish_block. */
20782 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20783 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20784 if ((attr2 && (DW_UNSND (attr2) != 0))
20785 || cu->language == language_ada
20786 || cu->language == language_fortran)
20787 {
20788 /* Subprograms marked external are stored as a global symbol.
20789 Ada and Fortran subprograms, whether marked external or
20790 not, are always stored as a global symbol, because we want
20791 to be able to access them globally. For instance, we want
20792 to be able to break on a nested subprogram without having
20793 to specify the context. */
20794 list_to_add = cu->get_builder ()->get_global_symbols ();
20795 }
20796 else
20797 {
20798 list_to_add = cu->list_in_scope;
20799 }
20800 break;
20801 case DW_TAG_inlined_subroutine:
20802 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20803 finish_block. */
20804 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20805 SYMBOL_INLINED (sym) = 1;
20806 list_to_add = cu->list_in_scope;
20807 break;
20808 case DW_TAG_template_value_param:
20809 suppress_add = 1;
20810 /* Fall through. */
20811 case DW_TAG_constant:
20812 case DW_TAG_variable:
20813 case DW_TAG_member:
20814 /* Compilation with minimal debug info may result in
20815 variables with missing type entries. Change the
20816 misleading `void' type to something sensible. */
20817 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20818 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20819
20820 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20821 /* In the case of DW_TAG_member, we should only be called for
20822 static const members. */
20823 if (die->tag == DW_TAG_member)
20824 {
20825 /* dwarf2_add_field uses die_is_declaration,
20826 so we do the same. */
20827 gdb_assert (die_is_declaration (die, cu));
20828 gdb_assert (attr);
20829 }
20830 if (attr != nullptr)
20831 {
20832 dwarf2_const_value (attr, sym, cu);
20833 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20834 if (!suppress_add)
20835 {
20836 if (attr2 && (DW_UNSND (attr2) != 0))
20837 list_to_add = cu->get_builder ()->get_global_symbols ();
20838 else
20839 list_to_add = cu->list_in_scope;
20840 }
20841 break;
20842 }
20843 attr = dwarf2_attr (die, DW_AT_location, cu);
20844 if (attr != nullptr)
20845 {
20846 var_decode_location (attr, sym, cu);
20847 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20848
20849 /* Fortran explicitly imports any global symbols to the local
20850 scope by DW_TAG_common_block. */
20851 if (cu->language == language_fortran && die->parent
20852 && die->parent->tag == DW_TAG_common_block)
20853 attr2 = NULL;
20854
20855 if (SYMBOL_CLASS (sym) == LOC_STATIC
20856 && SYMBOL_VALUE_ADDRESS (sym) == 0
20857 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20858 {
20859 /* When a static variable is eliminated by the linker,
20860 the corresponding debug information is not stripped
20861 out, but the variable address is set to null;
20862 do not add such variables into symbol table. */
20863 }
20864 else if (attr2 && (DW_UNSND (attr2) != 0))
20865 {
20866 if (SYMBOL_CLASS (sym) == LOC_STATIC
20867 && (objfile->flags & OBJF_MAINLINE) == 0
20868 && dwarf2_per_objfile->per_bfd->can_copy)
20869 {
20870 /* A global static variable might be subject to
20871 copy relocation. We first check for a local
20872 minsym, though, because maybe the symbol was
20873 marked hidden, in which case this would not
20874 apply. */
20875 bound_minimal_symbol found
20876 = (lookup_minimal_symbol_linkage
20877 (sym->linkage_name (), objfile));
20878 if (found.minsym != nullptr)
20879 sym->maybe_copied = 1;
20880 }
20881
20882 /* A variable with DW_AT_external is never static,
20883 but it may be block-scoped. */
20884 list_to_add
20885 = ((cu->list_in_scope
20886 == cu->get_builder ()->get_file_symbols ())
20887 ? cu->get_builder ()->get_global_symbols ()
20888 : cu->list_in_scope);
20889 }
20890 else
20891 list_to_add = cu->list_in_scope;
20892 }
20893 else
20894 {
20895 /* We do not know the address of this symbol.
20896 If it is an external symbol and we have type information
20897 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20898 The address of the variable will then be determined from
20899 the minimal symbol table whenever the variable is
20900 referenced. */
20901 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20902
20903 /* Fortran explicitly imports any global symbols to the local
20904 scope by DW_TAG_common_block. */
20905 if (cu->language == language_fortran && die->parent
20906 && die->parent->tag == DW_TAG_common_block)
20907 {
20908 /* SYMBOL_CLASS doesn't matter here because
20909 read_common_block is going to reset it. */
20910 if (!suppress_add)
20911 list_to_add = cu->list_in_scope;
20912 }
20913 else if (attr2 && (DW_UNSND (attr2) != 0)
20914 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20915 {
20916 /* A variable with DW_AT_external is never static, but it
20917 may be block-scoped. */
20918 list_to_add
20919 = ((cu->list_in_scope
20920 == cu->get_builder ()->get_file_symbols ())
20921 ? cu->get_builder ()->get_global_symbols ()
20922 : cu->list_in_scope);
20923
20924 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20925 }
20926 else if (!die_is_declaration (die, cu))
20927 {
20928 /* Use the default LOC_OPTIMIZED_OUT class. */
20929 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20930 if (!suppress_add)
20931 list_to_add = cu->list_in_scope;
20932 }
20933 }
20934 break;
20935 case DW_TAG_formal_parameter:
20936 {
20937 /* If we are inside a function, mark this as an argument. If
20938 not, we might be looking at an argument to an inlined function
20939 when we do not have enough information to show inlined frames;
20940 pretend it's a local variable in that case so that the user can
20941 still see it. */
20942 struct context_stack *curr
20943 = cu->get_builder ()->get_current_context_stack ();
20944 if (curr != nullptr && curr->name != nullptr)
20945 SYMBOL_IS_ARGUMENT (sym) = 1;
20946 attr = dwarf2_attr (die, DW_AT_location, cu);
20947 if (attr != nullptr)
20948 {
20949 var_decode_location (attr, sym, cu);
20950 }
20951 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20952 if (attr != nullptr)
20953 {
20954 dwarf2_const_value (attr, sym, cu);
20955 }
20956
20957 list_to_add = cu->list_in_scope;
20958 }
20959 break;
20960 case DW_TAG_unspecified_parameters:
20961 /* From varargs functions; gdb doesn't seem to have any
20962 interest in this information, so just ignore it for now.
20963 (FIXME?) */
20964 break;
20965 case DW_TAG_template_type_param:
20966 suppress_add = 1;
20967 /* Fall through. */
20968 case DW_TAG_class_type:
20969 case DW_TAG_interface_type:
20970 case DW_TAG_structure_type:
20971 case DW_TAG_union_type:
20972 case DW_TAG_set_type:
20973 case DW_TAG_enumeration_type:
20974 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20975 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20976
20977 {
20978 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20979 really ever be static objects: otherwise, if you try
20980 to, say, break of a class's method and you're in a file
20981 which doesn't mention that class, it won't work unless
20982 the check for all static symbols in lookup_symbol_aux
20983 saves you. See the OtherFileClass tests in
20984 gdb.c++/namespace.exp. */
20985
20986 if (!suppress_add)
20987 {
20988 buildsym_compunit *builder = cu->get_builder ();
20989 list_to_add
20990 = (cu->list_in_scope == builder->get_file_symbols ()
20991 && cu->language == language_cplus
20992 ? builder->get_global_symbols ()
20993 : cu->list_in_scope);
20994
20995 /* The semantics of C++ state that "struct foo {
20996 ... }" also defines a typedef for "foo". */
20997 if (cu->language == language_cplus
20998 || cu->language == language_ada
20999 || cu->language == language_d
21000 || cu->language == language_rust)
21001 {
21002 /* The symbol's name is already allocated along
21003 with this objfile, so we don't need to
21004 duplicate it for the type. */
21005 if (SYMBOL_TYPE (sym)->name () == 0)
21006 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21007 }
21008 }
21009 }
21010 break;
21011 case DW_TAG_typedef:
21012 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21013 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21014 list_to_add = cu->list_in_scope;
21015 break;
21016 case DW_TAG_base_type:
21017 case DW_TAG_subrange_type:
21018 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21019 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21020 list_to_add = cu->list_in_scope;
21021 break;
21022 case DW_TAG_enumerator:
21023 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21024 if (attr != nullptr)
21025 {
21026 dwarf2_const_value (attr, sym, cu);
21027 }
21028 {
21029 /* NOTE: carlton/2003-11-10: See comment above in the
21030 DW_TAG_class_type, etc. block. */
21031
21032 list_to_add
21033 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21034 && cu->language == language_cplus
21035 ? cu->get_builder ()->get_global_symbols ()
21036 : cu->list_in_scope);
21037 }
21038 break;
21039 case DW_TAG_imported_declaration:
21040 case DW_TAG_namespace:
21041 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21042 list_to_add = cu->get_builder ()->get_global_symbols ();
21043 break;
21044 case DW_TAG_module:
21045 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21046 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21047 list_to_add = cu->get_builder ()->get_global_symbols ();
21048 break;
21049 case DW_TAG_common_block:
21050 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21051 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21052 add_symbol_to_list (sym, cu->list_in_scope);
21053 break;
21054 default:
21055 /* Not a tag we recognize. Hopefully we aren't processing
21056 trash data, but since we must specifically ignore things
21057 we don't recognize, there is nothing else we should do at
21058 this point. */
21059 complaint (_("unsupported tag: '%s'"),
21060 dwarf_tag_name (die->tag));
21061 break;
21062 }
21063
21064 if (suppress_add)
21065 {
21066 sym->hash_next = objfile->template_symbols;
21067 objfile->template_symbols = sym;
21068 list_to_add = NULL;
21069 }
21070
21071 if (list_to_add != NULL)
21072 add_symbol_to_list (sym, list_to_add);
21073
21074 /* For the benefit of old versions of GCC, check for anonymous
21075 namespaces based on the demangled name. */
21076 if (!cu->processing_has_namespace_info
21077 && cu->language == language_cplus)
21078 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21079 }
21080 return (sym);
21081 }
21082
21083 /* Given an attr with a DW_FORM_dataN value in host byte order,
21084 zero-extend it as appropriate for the symbol's type. The DWARF
21085 standard (v4) is not entirely clear about the meaning of using
21086 DW_FORM_dataN for a constant with a signed type, where the type is
21087 wider than the data. The conclusion of a discussion on the DWARF
21088 list was that this is unspecified. We choose to always zero-extend
21089 because that is the interpretation long in use by GCC. */
21090
21091 static gdb_byte *
21092 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21093 struct dwarf2_cu *cu, LONGEST *value, int bits)
21094 {
21095 struct objfile *objfile = cu->per_objfile->objfile;
21096 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21097 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21098 LONGEST l = DW_UNSND (attr);
21099
21100 if (bits < sizeof (*value) * 8)
21101 {
21102 l &= ((LONGEST) 1 << bits) - 1;
21103 *value = l;
21104 }
21105 else if (bits == sizeof (*value) * 8)
21106 *value = l;
21107 else
21108 {
21109 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21110 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21111 return bytes;
21112 }
21113
21114 return NULL;
21115 }
21116
21117 /* Read a constant value from an attribute. Either set *VALUE, or if
21118 the value does not fit in *VALUE, set *BYTES - either already
21119 allocated on the objfile obstack, or newly allocated on OBSTACK,
21120 or, set *BATON, if we translated the constant to a location
21121 expression. */
21122
21123 static void
21124 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21125 const char *name, struct obstack *obstack,
21126 struct dwarf2_cu *cu,
21127 LONGEST *value, const gdb_byte **bytes,
21128 struct dwarf2_locexpr_baton **baton)
21129 {
21130 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21131 struct objfile *objfile = per_objfile->objfile;
21132 struct comp_unit_head *cu_header = &cu->header;
21133 struct dwarf_block *blk;
21134 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21135 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21136
21137 *value = 0;
21138 *bytes = NULL;
21139 *baton = NULL;
21140
21141 switch (attr->form)
21142 {
21143 case DW_FORM_addr:
21144 case DW_FORM_addrx:
21145 case DW_FORM_GNU_addr_index:
21146 {
21147 gdb_byte *data;
21148
21149 if (TYPE_LENGTH (type) != cu_header->addr_size)
21150 dwarf2_const_value_length_mismatch_complaint (name,
21151 cu_header->addr_size,
21152 TYPE_LENGTH (type));
21153 /* Symbols of this form are reasonably rare, so we just
21154 piggyback on the existing location code rather than writing
21155 a new implementation of symbol_computed_ops. */
21156 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21157 (*baton)->per_objfile = per_objfile;
21158 (*baton)->per_cu = cu->per_cu;
21159 gdb_assert ((*baton)->per_cu);
21160
21161 (*baton)->size = 2 + cu_header->addr_size;
21162 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21163 (*baton)->data = data;
21164
21165 data[0] = DW_OP_addr;
21166 store_unsigned_integer (&data[1], cu_header->addr_size,
21167 byte_order, DW_ADDR (attr));
21168 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21169 }
21170 break;
21171 case DW_FORM_string:
21172 case DW_FORM_strp:
21173 case DW_FORM_strx:
21174 case DW_FORM_GNU_str_index:
21175 case DW_FORM_GNU_strp_alt:
21176 /* DW_STRING is already allocated on the objfile obstack, point
21177 directly to it. */
21178 *bytes = (const gdb_byte *) DW_STRING (attr);
21179 break;
21180 case DW_FORM_block1:
21181 case DW_FORM_block2:
21182 case DW_FORM_block4:
21183 case DW_FORM_block:
21184 case DW_FORM_exprloc:
21185 case DW_FORM_data16:
21186 blk = DW_BLOCK (attr);
21187 if (TYPE_LENGTH (type) != blk->size)
21188 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21189 TYPE_LENGTH (type));
21190 *bytes = blk->data;
21191 break;
21192
21193 /* The DW_AT_const_value attributes are supposed to carry the
21194 symbol's value "represented as it would be on the target
21195 architecture." By the time we get here, it's already been
21196 converted to host endianness, so we just need to sign- or
21197 zero-extend it as appropriate. */
21198 case DW_FORM_data1:
21199 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21200 break;
21201 case DW_FORM_data2:
21202 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21203 break;
21204 case DW_FORM_data4:
21205 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21206 break;
21207 case DW_FORM_data8:
21208 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21209 break;
21210
21211 case DW_FORM_sdata:
21212 case DW_FORM_implicit_const:
21213 *value = DW_SND (attr);
21214 break;
21215
21216 case DW_FORM_udata:
21217 *value = DW_UNSND (attr);
21218 break;
21219
21220 default:
21221 complaint (_("unsupported const value attribute form: '%s'"),
21222 dwarf_form_name (attr->form));
21223 *value = 0;
21224 break;
21225 }
21226 }
21227
21228
21229 /* Copy constant value from an attribute to a symbol. */
21230
21231 static void
21232 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21233 struct dwarf2_cu *cu)
21234 {
21235 struct objfile *objfile = cu->per_objfile->objfile;
21236 LONGEST value;
21237 const gdb_byte *bytes;
21238 struct dwarf2_locexpr_baton *baton;
21239
21240 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21241 sym->print_name (),
21242 &objfile->objfile_obstack, cu,
21243 &value, &bytes, &baton);
21244
21245 if (baton != NULL)
21246 {
21247 SYMBOL_LOCATION_BATON (sym) = baton;
21248 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21249 }
21250 else if (bytes != NULL)
21251 {
21252 SYMBOL_VALUE_BYTES (sym) = bytes;
21253 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21254 }
21255 else
21256 {
21257 SYMBOL_VALUE (sym) = value;
21258 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21259 }
21260 }
21261
21262 /* Return the type of the die in question using its DW_AT_type attribute. */
21263
21264 static struct type *
21265 die_type (struct die_info *die, struct dwarf2_cu *cu)
21266 {
21267 struct attribute *type_attr;
21268
21269 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21270 if (!type_attr)
21271 {
21272 struct objfile *objfile = cu->per_objfile->objfile;
21273 /* A missing DW_AT_type represents a void type. */
21274 return objfile_type (objfile)->builtin_void;
21275 }
21276
21277 return lookup_die_type (die, type_attr, cu);
21278 }
21279
21280 /* True iff CU's producer generates GNAT Ada auxiliary information
21281 that allows to find parallel types through that information instead
21282 of having to do expensive parallel lookups by type name. */
21283
21284 static int
21285 need_gnat_info (struct dwarf2_cu *cu)
21286 {
21287 /* Assume that the Ada compiler was GNAT, which always produces
21288 the auxiliary information. */
21289 return (cu->language == language_ada);
21290 }
21291
21292 /* Return the auxiliary type of the die in question using its
21293 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21294 attribute is not present. */
21295
21296 static struct type *
21297 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21298 {
21299 struct attribute *type_attr;
21300
21301 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21302 if (!type_attr)
21303 return NULL;
21304
21305 return lookup_die_type (die, type_attr, cu);
21306 }
21307
21308 /* If DIE has a descriptive_type attribute, then set the TYPE's
21309 descriptive type accordingly. */
21310
21311 static void
21312 set_descriptive_type (struct type *type, struct die_info *die,
21313 struct dwarf2_cu *cu)
21314 {
21315 struct type *descriptive_type = die_descriptive_type (die, cu);
21316
21317 if (descriptive_type)
21318 {
21319 ALLOCATE_GNAT_AUX_TYPE (type);
21320 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21321 }
21322 }
21323
21324 /* Return the containing type of the die in question using its
21325 DW_AT_containing_type attribute. */
21326
21327 static struct type *
21328 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21329 {
21330 struct attribute *type_attr;
21331 struct objfile *objfile = cu->per_objfile->objfile;
21332
21333 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21334 if (!type_attr)
21335 error (_("Dwarf Error: Problem turning containing type into gdb type "
21336 "[in module %s]"), objfile_name (objfile));
21337
21338 return lookup_die_type (die, type_attr, cu);
21339 }
21340
21341 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21342
21343 static struct type *
21344 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21345 {
21346 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21347 struct objfile *objfile = dwarf2_per_objfile->objfile;
21348 char *saved;
21349
21350 std::string message
21351 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21352 objfile_name (objfile),
21353 sect_offset_str (cu->header.sect_off),
21354 sect_offset_str (die->sect_off));
21355 saved = obstack_strdup (&objfile->objfile_obstack, message);
21356
21357 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21358 }
21359
21360 /* Look up the type of DIE in CU using its type attribute ATTR.
21361 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21362 DW_AT_containing_type.
21363 If there is no type substitute an error marker. */
21364
21365 static struct type *
21366 lookup_die_type (struct die_info *die, const struct attribute *attr,
21367 struct dwarf2_cu *cu)
21368 {
21369 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21370 struct objfile *objfile = dwarf2_per_objfile->objfile;
21371 struct type *this_type;
21372
21373 gdb_assert (attr->name == DW_AT_type
21374 || attr->name == DW_AT_GNAT_descriptive_type
21375 || attr->name == DW_AT_containing_type);
21376
21377 /* First see if we have it cached. */
21378
21379 if (attr->form == DW_FORM_GNU_ref_alt)
21380 {
21381 struct dwarf2_per_cu_data *per_cu;
21382 sect_offset sect_off = attr->get_ref_die_offset ();
21383
21384 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21385 dwarf2_per_objfile);
21386 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
21387 }
21388 else if (attr->form_is_ref ())
21389 {
21390 sect_offset sect_off = attr->get_ref_die_offset ();
21391
21392 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21393 dwarf2_per_objfile);
21394 }
21395 else if (attr->form == DW_FORM_ref_sig8)
21396 {
21397 ULONGEST signature = DW_SIGNATURE (attr);
21398
21399 return get_signatured_type (die, signature, cu);
21400 }
21401 else
21402 {
21403 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21404 " at %s [in module %s]"),
21405 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21406 objfile_name (objfile));
21407 return build_error_marker_type (cu, die);
21408 }
21409
21410 /* If not cached we need to read it in. */
21411
21412 if (this_type == NULL)
21413 {
21414 struct die_info *type_die = NULL;
21415 struct dwarf2_cu *type_cu = cu;
21416
21417 if (attr->form_is_ref ())
21418 type_die = follow_die_ref (die, attr, &type_cu);
21419 if (type_die == NULL)
21420 return build_error_marker_type (cu, die);
21421 /* If we find the type now, it's probably because the type came
21422 from an inter-CU reference and the type's CU got expanded before
21423 ours. */
21424 this_type = read_type_die (type_die, type_cu);
21425 }
21426
21427 /* If we still don't have a type use an error marker. */
21428
21429 if (this_type == NULL)
21430 return build_error_marker_type (cu, die);
21431
21432 return this_type;
21433 }
21434
21435 /* Return the type in DIE, CU.
21436 Returns NULL for invalid types.
21437
21438 This first does a lookup in die_type_hash,
21439 and only reads the die in if necessary.
21440
21441 NOTE: This can be called when reading in partial or full symbols. */
21442
21443 static struct type *
21444 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21445 {
21446 struct type *this_type;
21447
21448 this_type = get_die_type (die, cu);
21449 if (this_type)
21450 return this_type;
21451
21452 return read_type_die_1 (die, cu);
21453 }
21454
21455 /* Read the type in DIE, CU.
21456 Returns NULL for invalid types. */
21457
21458 static struct type *
21459 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21460 {
21461 struct type *this_type = NULL;
21462
21463 switch (die->tag)
21464 {
21465 case DW_TAG_class_type:
21466 case DW_TAG_interface_type:
21467 case DW_TAG_structure_type:
21468 case DW_TAG_union_type:
21469 this_type = read_structure_type (die, cu);
21470 break;
21471 case DW_TAG_enumeration_type:
21472 this_type = read_enumeration_type (die, cu);
21473 break;
21474 case DW_TAG_subprogram:
21475 case DW_TAG_subroutine_type:
21476 case DW_TAG_inlined_subroutine:
21477 this_type = read_subroutine_type (die, cu);
21478 break;
21479 case DW_TAG_array_type:
21480 this_type = read_array_type (die, cu);
21481 break;
21482 case DW_TAG_set_type:
21483 this_type = read_set_type (die, cu);
21484 break;
21485 case DW_TAG_pointer_type:
21486 this_type = read_tag_pointer_type (die, cu);
21487 break;
21488 case DW_TAG_ptr_to_member_type:
21489 this_type = read_tag_ptr_to_member_type (die, cu);
21490 break;
21491 case DW_TAG_reference_type:
21492 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21493 break;
21494 case DW_TAG_rvalue_reference_type:
21495 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21496 break;
21497 case DW_TAG_const_type:
21498 this_type = read_tag_const_type (die, cu);
21499 break;
21500 case DW_TAG_volatile_type:
21501 this_type = read_tag_volatile_type (die, cu);
21502 break;
21503 case DW_TAG_restrict_type:
21504 this_type = read_tag_restrict_type (die, cu);
21505 break;
21506 case DW_TAG_string_type:
21507 this_type = read_tag_string_type (die, cu);
21508 break;
21509 case DW_TAG_typedef:
21510 this_type = read_typedef (die, cu);
21511 break;
21512 case DW_TAG_subrange_type:
21513 this_type = read_subrange_type (die, cu);
21514 break;
21515 case DW_TAG_base_type:
21516 this_type = read_base_type (die, cu);
21517 break;
21518 case DW_TAG_unspecified_type:
21519 this_type = read_unspecified_type (die, cu);
21520 break;
21521 case DW_TAG_namespace:
21522 this_type = read_namespace_type (die, cu);
21523 break;
21524 case DW_TAG_module:
21525 this_type = read_module_type (die, cu);
21526 break;
21527 case DW_TAG_atomic_type:
21528 this_type = read_tag_atomic_type (die, cu);
21529 break;
21530 default:
21531 complaint (_("unexpected tag in read_type_die: '%s'"),
21532 dwarf_tag_name (die->tag));
21533 break;
21534 }
21535
21536 return this_type;
21537 }
21538
21539 /* See if we can figure out if the class lives in a namespace. We do
21540 this by looking for a member function; its demangled name will
21541 contain namespace info, if there is any.
21542 Return the computed name or NULL.
21543 Space for the result is allocated on the objfile's obstack.
21544 This is the full-die version of guess_partial_die_structure_name.
21545 In this case we know DIE has no useful parent. */
21546
21547 static const char *
21548 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21549 {
21550 struct die_info *spec_die;
21551 struct dwarf2_cu *spec_cu;
21552 struct die_info *child;
21553 struct objfile *objfile = cu->per_objfile->objfile;
21554
21555 spec_cu = cu;
21556 spec_die = die_specification (die, &spec_cu);
21557 if (spec_die != NULL)
21558 {
21559 die = spec_die;
21560 cu = spec_cu;
21561 }
21562
21563 for (child = die->child;
21564 child != NULL;
21565 child = child->sibling)
21566 {
21567 if (child->tag == DW_TAG_subprogram)
21568 {
21569 const char *linkage_name = dw2_linkage_name (child, cu);
21570
21571 if (linkage_name != NULL)
21572 {
21573 gdb::unique_xmalloc_ptr<char> actual_name
21574 (language_class_name_from_physname (cu->language_defn,
21575 linkage_name));
21576 const char *name = NULL;
21577
21578 if (actual_name != NULL)
21579 {
21580 const char *die_name = dwarf2_name (die, cu);
21581
21582 if (die_name != NULL
21583 && strcmp (die_name, actual_name.get ()) != 0)
21584 {
21585 /* Strip off the class name from the full name.
21586 We want the prefix. */
21587 int die_name_len = strlen (die_name);
21588 int actual_name_len = strlen (actual_name.get ());
21589 const char *ptr = actual_name.get ();
21590
21591 /* Test for '::' as a sanity check. */
21592 if (actual_name_len > die_name_len + 2
21593 && ptr[actual_name_len - die_name_len - 1] == ':')
21594 name = obstack_strndup (
21595 &objfile->per_bfd->storage_obstack,
21596 ptr, actual_name_len - die_name_len - 2);
21597 }
21598 }
21599 return name;
21600 }
21601 }
21602 }
21603
21604 return NULL;
21605 }
21606
21607 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21608 prefix part in such case. See
21609 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21610
21611 static const char *
21612 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21613 {
21614 struct attribute *attr;
21615 const char *base;
21616
21617 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21618 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21619 return NULL;
21620
21621 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21622 return NULL;
21623
21624 attr = dw2_linkage_name_attr (die, cu);
21625 if (attr == NULL || DW_STRING (attr) == NULL)
21626 return NULL;
21627
21628 /* dwarf2_name had to be already called. */
21629 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21630
21631 /* Strip the base name, keep any leading namespaces/classes. */
21632 base = strrchr (DW_STRING (attr), ':');
21633 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21634 return "";
21635
21636 struct objfile *objfile = cu->per_objfile->objfile;
21637 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21638 DW_STRING (attr),
21639 &base[-1] - DW_STRING (attr));
21640 }
21641
21642 /* Return the name of the namespace/class that DIE is defined within,
21643 or "" if we can't tell. The caller should not xfree the result.
21644
21645 For example, if we're within the method foo() in the following
21646 code:
21647
21648 namespace N {
21649 class C {
21650 void foo () {
21651 }
21652 };
21653 }
21654
21655 then determine_prefix on foo's die will return "N::C". */
21656
21657 static const char *
21658 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21659 {
21660 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21661 struct die_info *parent, *spec_die;
21662 struct dwarf2_cu *spec_cu;
21663 struct type *parent_type;
21664 const char *retval;
21665
21666 if (cu->language != language_cplus
21667 && cu->language != language_fortran && cu->language != language_d
21668 && cu->language != language_rust)
21669 return "";
21670
21671 retval = anonymous_struct_prefix (die, cu);
21672 if (retval)
21673 return retval;
21674
21675 /* We have to be careful in the presence of DW_AT_specification.
21676 For example, with GCC 3.4, given the code
21677
21678 namespace N {
21679 void foo() {
21680 // Definition of N::foo.
21681 }
21682 }
21683
21684 then we'll have a tree of DIEs like this:
21685
21686 1: DW_TAG_compile_unit
21687 2: DW_TAG_namespace // N
21688 3: DW_TAG_subprogram // declaration of N::foo
21689 4: DW_TAG_subprogram // definition of N::foo
21690 DW_AT_specification // refers to die #3
21691
21692 Thus, when processing die #4, we have to pretend that we're in
21693 the context of its DW_AT_specification, namely the contex of die
21694 #3. */
21695 spec_cu = cu;
21696 spec_die = die_specification (die, &spec_cu);
21697 if (spec_die == NULL)
21698 parent = die->parent;
21699 else
21700 {
21701 parent = spec_die->parent;
21702 cu = spec_cu;
21703 }
21704
21705 if (parent == NULL)
21706 return "";
21707 else if (parent->building_fullname)
21708 {
21709 const char *name;
21710 const char *parent_name;
21711
21712 /* It has been seen on RealView 2.2 built binaries,
21713 DW_TAG_template_type_param types actually _defined_ as
21714 children of the parent class:
21715
21716 enum E {};
21717 template class <class Enum> Class{};
21718 Class<enum E> class_e;
21719
21720 1: DW_TAG_class_type (Class)
21721 2: DW_TAG_enumeration_type (E)
21722 3: DW_TAG_enumerator (enum1:0)
21723 3: DW_TAG_enumerator (enum2:1)
21724 ...
21725 2: DW_TAG_template_type_param
21726 DW_AT_type DW_FORM_ref_udata (E)
21727
21728 Besides being broken debug info, it can put GDB into an
21729 infinite loop. Consider:
21730
21731 When we're building the full name for Class<E>, we'll start
21732 at Class, and go look over its template type parameters,
21733 finding E. We'll then try to build the full name of E, and
21734 reach here. We're now trying to build the full name of E,
21735 and look over the parent DIE for containing scope. In the
21736 broken case, if we followed the parent DIE of E, we'd again
21737 find Class, and once again go look at its template type
21738 arguments, etc., etc. Simply don't consider such parent die
21739 as source-level parent of this die (it can't be, the language
21740 doesn't allow it), and break the loop here. */
21741 name = dwarf2_name (die, cu);
21742 parent_name = dwarf2_name (parent, cu);
21743 complaint (_("template param type '%s' defined within parent '%s'"),
21744 name ? name : "<unknown>",
21745 parent_name ? parent_name : "<unknown>");
21746 return "";
21747 }
21748 else
21749 switch (parent->tag)
21750 {
21751 case DW_TAG_namespace:
21752 parent_type = read_type_die (parent, cu);
21753 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21754 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21755 Work around this problem here. */
21756 if (cu->language == language_cplus
21757 && strcmp (parent_type->name (), "::") == 0)
21758 return "";
21759 /* We give a name to even anonymous namespaces. */
21760 return parent_type->name ();
21761 case DW_TAG_class_type:
21762 case DW_TAG_interface_type:
21763 case DW_TAG_structure_type:
21764 case DW_TAG_union_type:
21765 case DW_TAG_module:
21766 parent_type = read_type_die (parent, cu);
21767 if (parent_type->name () != NULL)
21768 return parent_type->name ();
21769 else
21770 /* An anonymous structure is only allowed non-static data
21771 members; no typedefs, no member functions, et cetera.
21772 So it does not need a prefix. */
21773 return "";
21774 case DW_TAG_compile_unit:
21775 case DW_TAG_partial_unit:
21776 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21777 if (cu->language == language_cplus
21778 && !dwarf2_per_objfile->per_bfd->types.empty ()
21779 && die->child != NULL
21780 && (die->tag == DW_TAG_class_type
21781 || die->tag == DW_TAG_structure_type
21782 || die->tag == DW_TAG_union_type))
21783 {
21784 const char *name = guess_full_die_structure_name (die, cu);
21785 if (name != NULL)
21786 return name;
21787 }
21788 return "";
21789 case DW_TAG_subprogram:
21790 /* Nested subroutines in Fortran get a prefix with the name
21791 of the parent's subroutine. */
21792 if (cu->language == language_fortran)
21793 {
21794 if ((die->tag == DW_TAG_subprogram)
21795 && (dwarf2_name (parent, cu) != NULL))
21796 return dwarf2_name (parent, cu);
21797 }
21798 return determine_prefix (parent, cu);
21799 case DW_TAG_enumeration_type:
21800 parent_type = read_type_die (parent, cu);
21801 if (TYPE_DECLARED_CLASS (parent_type))
21802 {
21803 if (parent_type->name () != NULL)
21804 return parent_type->name ();
21805 return "";
21806 }
21807 /* Fall through. */
21808 default:
21809 return determine_prefix (parent, cu);
21810 }
21811 }
21812
21813 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21814 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21815 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21816 an obconcat, otherwise allocate storage for the result. The CU argument is
21817 used to determine the language and hence, the appropriate separator. */
21818
21819 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21820
21821 static char *
21822 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21823 int physname, struct dwarf2_cu *cu)
21824 {
21825 const char *lead = "";
21826 const char *sep;
21827
21828 if (suffix == NULL || suffix[0] == '\0'
21829 || prefix == NULL || prefix[0] == '\0')
21830 sep = "";
21831 else if (cu->language == language_d)
21832 {
21833 /* For D, the 'main' function could be defined in any module, but it
21834 should never be prefixed. */
21835 if (strcmp (suffix, "D main") == 0)
21836 {
21837 prefix = "";
21838 sep = "";
21839 }
21840 else
21841 sep = ".";
21842 }
21843 else if (cu->language == language_fortran && physname)
21844 {
21845 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21846 DW_AT_MIPS_linkage_name is preferred and used instead. */
21847
21848 lead = "__";
21849 sep = "_MOD_";
21850 }
21851 else
21852 sep = "::";
21853
21854 if (prefix == NULL)
21855 prefix = "";
21856 if (suffix == NULL)
21857 suffix = "";
21858
21859 if (obs == NULL)
21860 {
21861 char *retval
21862 = ((char *)
21863 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21864
21865 strcpy (retval, lead);
21866 strcat (retval, prefix);
21867 strcat (retval, sep);
21868 strcat (retval, suffix);
21869 return retval;
21870 }
21871 else
21872 {
21873 /* We have an obstack. */
21874 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21875 }
21876 }
21877
21878 /* Get name of a die, return NULL if not found. */
21879
21880 static const char *
21881 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21882 struct objfile *objfile)
21883 {
21884 if (name && cu->language == language_cplus)
21885 {
21886 gdb::unique_xmalloc_ptr<char> canon_name
21887 = cp_canonicalize_string (name);
21888
21889 if (canon_name != nullptr)
21890 name = objfile->intern (canon_name.get ());
21891 }
21892
21893 return name;
21894 }
21895
21896 /* Get name of a die, return NULL if not found.
21897 Anonymous namespaces are converted to their magic string. */
21898
21899 static const char *
21900 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21901 {
21902 struct attribute *attr;
21903 struct objfile *objfile = cu->per_objfile->objfile;
21904
21905 attr = dwarf2_attr (die, DW_AT_name, cu);
21906 if ((!attr || !DW_STRING (attr))
21907 && die->tag != DW_TAG_namespace
21908 && die->tag != DW_TAG_class_type
21909 && die->tag != DW_TAG_interface_type
21910 && die->tag != DW_TAG_structure_type
21911 && die->tag != DW_TAG_union_type)
21912 return NULL;
21913
21914 switch (die->tag)
21915 {
21916 case DW_TAG_compile_unit:
21917 case DW_TAG_partial_unit:
21918 /* Compilation units have a DW_AT_name that is a filename, not
21919 a source language identifier. */
21920 case DW_TAG_enumeration_type:
21921 case DW_TAG_enumerator:
21922 /* These tags always have simple identifiers already; no need
21923 to canonicalize them. */
21924 return DW_STRING (attr);
21925
21926 case DW_TAG_namespace:
21927 if (attr != NULL && DW_STRING (attr) != NULL)
21928 return DW_STRING (attr);
21929 return CP_ANONYMOUS_NAMESPACE_STR;
21930
21931 case DW_TAG_class_type:
21932 case DW_TAG_interface_type:
21933 case DW_TAG_structure_type:
21934 case DW_TAG_union_type:
21935 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21936 structures or unions. These were of the form "._%d" in GCC 4.1,
21937 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21938 and GCC 4.4. We work around this problem by ignoring these. */
21939 if (attr && DW_STRING (attr)
21940 && (startswith (DW_STRING (attr), "._")
21941 || startswith (DW_STRING (attr), "<anonymous")))
21942 return NULL;
21943
21944 /* GCC might emit a nameless typedef that has a linkage name. See
21945 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21946 if (!attr || DW_STRING (attr) == NULL)
21947 {
21948 attr = dw2_linkage_name_attr (die, cu);
21949 if (attr == NULL || DW_STRING (attr) == NULL)
21950 return NULL;
21951
21952 /* Avoid demangling DW_STRING (attr) the second time on a second
21953 call for the same DIE. */
21954 if (!DW_STRING_IS_CANONICAL (attr))
21955 {
21956 gdb::unique_xmalloc_ptr<char> demangled
21957 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21958 if (demangled == nullptr)
21959 return nullptr;
21960
21961 DW_STRING (attr) = objfile->intern (demangled.get ());
21962 DW_STRING_IS_CANONICAL (attr) = 1;
21963 }
21964
21965 /* Strip any leading namespaces/classes, keep only the base name.
21966 DW_AT_name for named DIEs does not contain the prefixes. */
21967 const char *base = strrchr (DW_STRING (attr), ':');
21968 if (base && base > DW_STRING (attr) && base[-1] == ':')
21969 return &base[1];
21970 else
21971 return DW_STRING (attr);
21972 }
21973 break;
21974
21975 default:
21976 break;
21977 }
21978
21979 if (!DW_STRING_IS_CANONICAL (attr))
21980 {
21981 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21982 objfile);
21983 DW_STRING_IS_CANONICAL (attr) = 1;
21984 }
21985 return DW_STRING (attr);
21986 }
21987
21988 /* Return the die that this die in an extension of, or NULL if there
21989 is none. *EXT_CU is the CU containing DIE on input, and the CU
21990 containing the return value on output. */
21991
21992 static struct die_info *
21993 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21994 {
21995 struct attribute *attr;
21996
21997 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21998 if (attr == NULL)
21999 return NULL;
22000
22001 return follow_die_ref (die, attr, ext_cu);
22002 }
22003
22004 static void
22005 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22006 {
22007 unsigned int i;
22008
22009 print_spaces (indent, f);
22010 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22011 dwarf_tag_name (die->tag), die->abbrev,
22012 sect_offset_str (die->sect_off));
22013
22014 if (die->parent != NULL)
22015 {
22016 print_spaces (indent, f);
22017 fprintf_unfiltered (f, " parent at offset: %s\n",
22018 sect_offset_str (die->parent->sect_off));
22019 }
22020
22021 print_spaces (indent, f);
22022 fprintf_unfiltered (f, " has children: %s\n",
22023 dwarf_bool_name (die->child != NULL));
22024
22025 print_spaces (indent, f);
22026 fprintf_unfiltered (f, " attributes:\n");
22027
22028 for (i = 0; i < die->num_attrs; ++i)
22029 {
22030 print_spaces (indent, f);
22031 fprintf_unfiltered (f, " %s (%s) ",
22032 dwarf_attr_name (die->attrs[i].name),
22033 dwarf_form_name (die->attrs[i].form));
22034
22035 switch (die->attrs[i].form)
22036 {
22037 case DW_FORM_addr:
22038 case DW_FORM_addrx:
22039 case DW_FORM_GNU_addr_index:
22040 fprintf_unfiltered (f, "address: ");
22041 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22042 break;
22043 case DW_FORM_block2:
22044 case DW_FORM_block4:
22045 case DW_FORM_block:
22046 case DW_FORM_block1:
22047 fprintf_unfiltered (f, "block: size %s",
22048 pulongest (DW_BLOCK (&die->attrs[i])->size));
22049 break;
22050 case DW_FORM_exprloc:
22051 fprintf_unfiltered (f, "expression: size %s",
22052 pulongest (DW_BLOCK (&die->attrs[i])->size));
22053 break;
22054 case DW_FORM_data16:
22055 fprintf_unfiltered (f, "constant of 16 bytes");
22056 break;
22057 case DW_FORM_ref_addr:
22058 fprintf_unfiltered (f, "ref address: ");
22059 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22060 break;
22061 case DW_FORM_GNU_ref_alt:
22062 fprintf_unfiltered (f, "alt ref address: ");
22063 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22064 break;
22065 case DW_FORM_ref1:
22066 case DW_FORM_ref2:
22067 case DW_FORM_ref4:
22068 case DW_FORM_ref8:
22069 case DW_FORM_ref_udata:
22070 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22071 (long) (DW_UNSND (&die->attrs[i])));
22072 break;
22073 case DW_FORM_data1:
22074 case DW_FORM_data2:
22075 case DW_FORM_data4:
22076 case DW_FORM_data8:
22077 case DW_FORM_udata:
22078 case DW_FORM_sdata:
22079 fprintf_unfiltered (f, "constant: %s",
22080 pulongest (DW_UNSND (&die->attrs[i])));
22081 break;
22082 case DW_FORM_sec_offset:
22083 fprintf_unfiltered (f, "section offset: %s",
22084 pulongest (DW_UNSND (&die->attrs[i])));
22085 break;
22086 case DW_FORM_ref_sig8:
22087 fprintf_unfiltered (f, "signature: %s",
22088 hex_string (DW_SIGNATURE (&die->attrs[i])));
22089 break;
22090 case DW_FORM_string:
22091 case DW_FORM_strp:
22092 case DW_FORM_line_strp:
22093 case DW_FORM_strx:
22094 case DW_FORM_GNU_str_index:
22095 case DW_FORM_GNU_strp_alt:
22096 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22097 DW_STRING (&die->attrs[i])
22098 ? DW_STRING (&die->attrs[i]) : "",
22099 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22100 break;
22101 case DW_FORM_flag:
22102 if (DW_UNSND (&die->attrs[i]))
22103 fprintf_unfiltered (f, "flag: TRUE");
22104 else
22105 fprintf_unfiltered (f, "flag: FALSE");
22106 break;
22107 case DW_FORM_flag_present:
22108 fprintf_unfiltered (f, "flag: TRUE");
22109 break;
22110 case DW_FORM_indirect:
22111 /* The reader will have reduced the indirect form to
22112 the "base form" so this form should not occur. */
22113 fprintf_unfiltered (f,
22114 "unexpected attribute form: DW_FORM_indirect");
22115 break;
22116 case DW_FORM_implicit_const:
22117 fprintf_unfiltered (f, "constant: %s",
22118 plongest (DW_SND (&die->attrs[i])));
22119 break;
22120 default:
22121 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22122 die->attrs[i].form);
22123 break;
22124 }
22125 fprintf_unfiltered (f, "\n");
22126 }
22127 }
22128
22129 static void
22130 dump_die_for_error (struct die_info *die)
22131 {
22132 dump_die_shallow (gdb_stderr, 0, die);
22133 }
22134
22135 static void
22136 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22137 {
22138 int indent = level * 4;
22139
22140 gdb_assert (die != NULL);
22141
22142 if (level >= max_level)
22143 return;
22144
22145 dump_die_shallow (f, indent, die);
22146
22147 if (die->child != NULL)
22148 {
22149 print_spaces (indent, f);
22150 fprintf_unfiltered (f, " Children:");
22151 if (level + 1 < max_level)
22152 {
22153 fprintf_unfiltered (f, "\n");
22154 dump_die_1 (f, level + 1, max_level, die->child);
22155 }
22156 else
22157 {
22158 fprintf_unfiltered (f,
22159 " [not printed, max nesting level reached]\n");
22160 }
22161 }
22162
22163 if (die->sibling != NULL && level > 0)
22164 {
22165 dump_die_1 (f, level, max_level, die->sibling);
22166 }
22167 }
22168
22169 /* This is called from the pdie macro in gdbinit.in.
22170 It's not static so gcc will keep a copy callable from gdb. */
22171
22172 void
22173 dump_die (struct die_info *die, int max_level)
22174 {
22175 dump_die_1 (gdb_stdlog, 0, max_level, die);
22176 }
22177
22178 static void
22179 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22180 {
22181 void **slot;
22182
22183 slot = htab_find_slot_with_hash (cu->die_hash, die,
22184 to_underlying (die->sect_off),
22185 INSERT);
22186
22187 *slot = die;
22188 }
22189
22190 /* Follow reference or signature attribute ATTR of SRC_DIE.
22191 On entry *REF_CU is the CU of SRC_DIE.
22192 On exit *REF_CU is the CU of the result. */
22193
22194 static struct die_info *
22195 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22196 struct dwarf2_cu **ref_cu)
22197 {
22198 struct die_info *die;
22199
22200 if (attr->form_is_ref ())
22201 die = follow_die_ref (src_die, attr, ref_cu);
22202 else if (attr->form == DW_FORM_ref_sig8)
22203 die = follow_die_sig (src_die, attr, ref_cu);
22204 else
22205 {
22206 dump_die_for_error (src_die);
22207 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22208 objfile_name ((*ref_cu)->per_objfile->objfile));
22209 }
22210
22211 return die;
22212 }
22213
22214 /* Follow reference OFFSET.
22215 On entry *REF_CU is the CU of the source die referencing OFFSET.
22216 On exit *REF_CU is the CU of the result.
22217 Returns NULL if OFFSET is invalid. */
22218
22219 static struct die_info *
22220 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22221 struct dwarf2_cu **ref_cu)
22222 {
22223 struct die_info temp_die;
22224 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22225 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22226
22227 gdb_assert (cu->per_cu != NULL);
22228
22229 target_cu = cu;
22230
22231 if (cu->per_cu->is_debug_types)
22232 {
22233 /* .debug_types CUs cannot reference anything outside their CU.
22234 If they need to, they have to reference a signatured type via
22235 DW_FORM_ref_sig8. */
22236 if (!cu->header.offset_in_cu_p (sect_off))
22237 return NULL;
22238 }
22239 else if (offset_in_dwz != cu->per_cu->is_dwz
22240 || !cu->header.offset_in_cu_p (sect_off))
22241 {
22242 struct dwarf2_per_cu_data *per_cu;
22243
22244 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22245 dwarf2_per_objfile);
22246
22247 /* If necessary, add it to the queue and load its DIEs. */
22248 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22249 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22250
22251 target_cu = per_cu->cu;
22252 }
22253 else if (cu->dies == NULL)
22254 {
22255 /* We're loading full DIEs during partial symbol reading. */
22256 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22257 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22258 language_minimal);
22259 }
22260
22261 *ref_cu = target_cu;
22262 temp_die.sect_off = sect_off;
22263
22264 if (target_cu != cu)
22265 target_cu->ancestor = cu;
22266
22267 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22268 &temp_die,
22269 to_underlying (sect_off));
22270 }
22271
22272 /* Follow reference attribute ATTR of SRC_DIE.
22273 On entry *REF_CU is the CU of SRC_DIE.
22274 On exit *REF_CU is the CU of the result. */
22275
22276 static struct die_info *
22277 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22278 struct dwarf2_cu **ref_cu)
22279 {
22280 sect_offset sect_off = attr->get_ref_die_offset ();
22281 struct dwarf2_cu *cu = *ref_cu;
22282 struct die_info *die;
22283
22284 die = follow_die_offset (sect_off,
22285 (attr->form == DW_FORM_GNU_ref_alt
22286 || cu->per_cu->is_dwz),
22287 ref_cu);
22288 if (!die)
22289 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22290 "at %s [in module %s]"),
22291 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22292 objfile_name (cu->per_objfile->objfile));
22293
22294 return die;
22295 }
22296
22297 /* See read.h. */
22298
22299 struct dwarf2_locexpr_baton
22300 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22301 dwarf2_per_cu_data *per_cu,
22302 dwarf2_per_objfile *dwarf2_per_objfile,
22303 CORE_ADDR (*get_frame_pc) (void *baton),
22304 void *baton, bool resolve_abstract_p)
22305 {
22306 struct dwarf2_cu *cu;
22307 struct die_info *die;
22308 struct attribute *attr;
22309 struct dwarf2_locexpr_baton retval;
22310 struct objfile *objfile = dwarf2_per_objfile->objfile;
22311
22312 if (per_cu->cu == NULL)
22313 load_cu (per_cu, dwarf2_per_objfile, false);
22314 cu = per_cu->cu;
22315 if (cu == NULL)
22316 {
22317 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22318 Instead just throw an error, not much else we can do. */
22319 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22320 sect_offset_str (sect_off), objfile_name (objfile));
22321 }
22322
22323 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22324 if (!die)
22325 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22326 sect_offset_str (sect_off), objfile_name (objfile));
22327
22328 attr = dwarf2_attr (die, DW_AT_location, cu);
22329 if (!attr && resolve_abstract_p
22330 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22331 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22332 {
22333 CORE_ADDR pc = (*get_frame_pc) (baton);
22334 CORE_ADDR baseaddr = objfile->text_section_offset ();
22335 struct gdbarch *gdbarch = objfile->arch ();
22336
22337 for (const auto &cand_off
22338 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22339 {
22340 struct dwarf2_cu *cand_cu = cu;
22341 struct die_info *cand
22342 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22343 if (!cand
22344 || !cand->parent
22345 || cand->parent->tag != DW_TAG_subprogram)
22346 continue;
22347
22348 CORE_ADDR pc_low, pc_high;
22349 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22350 if (pc_low == ((CORE_ADDR) -1))
22351 continue;
22352 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22353 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22354 if (!(pc_low <= pc && pc < pc_high))
22355 continue;
22356
22357 die = cand;
22358 attr = dwarf2_attr (die, DW_AT_location, cu);
22359 break;
22360 }
22361 }
22362
22363 if (!attr)
22364 {
22365 /* DWARF: "If there is no such attribute, then there is no effect.".
22366 DATA is ignored if SIZE is 0. */
22367
22368 retval.data = NULL;
22369 retval.size = 0;
22370 }
22371 else if (attr->form_is_section_offset ())
22372 {
22373 struct dwarf2_loclist_baton loclist_baton;
22374 CORE_ADDR pc = (*get_frame_pc) (baton);
22375 size_t size;
22376
22377 fill_in_loclist_baton (cu, &loclist_baton, attr);
22378
22379 retval.data = dwarf2_find_location_expression (&loclist_baton,
22380 &size, pc);
22381 retval.size = size;
22382 }
22383 else
22384 {
22385 if (!attr->form_is_block ())
22386 error (_("Dwarf Error: DIE at %s referenced in module %s "
22387 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22388 sect_offset_str (sect_off), objfile_name (objfile));
22389
22390 retval.data = DW_BLOCK (attr)->data;
22391 retval.size = DW_BLOCK (attr)->size;
22392 }
22393 retval.per_objfile = dwarf2_per_objfile;
22394 retval.per_cu = cu->per_cu;
22395
22396 age_cached_comp_units (dwarf2_per_objfile);
22397
22398 return retval;
22399 }
22400
22401 /* See read.h. */
22402
22403 struct dwarf2_locexpr_baton
22404 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22405 dwarf2_per_cu_data *per_cu,
22406 dwarf2_per_objfile *per_objfile,
22407 CORE_ADDR (*get_frame_pc) (void *baton),
22408 void *baton)
22409 {
22410 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22411
22412 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22413 get_frame_pc, baton);
22414 }
22415
22416 /* Write a constant of a given type as target-ordered bytes into
22417 OBSTACK. */
22418
22419 static const gdb_byte *
22420 write_constant_as_bytes (struct obstack *obstack,
22421 enum bfd_endian byte_order,
22422 struct type *type,
22423 ULONGEST value,
22424 LONGEST *len)
22425 {
22426 gdb_byte *result;
22427
22428 *len = TYPE_LENGTH (type);
22429 result = (gdb_byte *) obstack_alloc (obstack, *len);
22430 store_unsigned_integer (result, *len, byte_order, value);
22431
22432 return result;
22433 }
22434
22435 /* See read.h. */
22436
22437 const gdb_byte *
22438 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22439 dwarf2_per_cu_data *per_cu,
22440 dwarf2_per_objfile *per_objfile,
22441 obstack *obstack,
22442 LONGEST *len)
22443 {
22444 struct dwarf2_cu *cu;
22445 struct die_info *die;
22446 struct attribute *attr;
22447 const gdb_byte *result = NULL;
22448 struct type *type;
22449 LONGEST value;
22450 enum bfd_endian byte_order;
22451 struct objfile *objfile = per_objfile->objfile;
22452
22453 if (per_cu->cu == NULL)
22454 load_cu (per_cu, per_objfile, false);
22455 cu = per_cu->cu;
22456 if (cu == NULL)
22457 {
22458 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22459 Instead just throw an error, not much else we can do. */
22460 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22461 sect_offset_str (sect_off), objfile_name (objfile));
22462 }
22463
22464 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22465 if (!die)
22466 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22467 sect_offset_str (sect_off), objfile_name (objfile));
22468
22469 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22470 if (attr == NULL)
22471 return NULL;
22472
22473 byte_order = (bfd_big_endian (objfile->obfd)
22474 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22475
22476 switch (attr->form)
22477 {
22478 case DW_FORM_addr:
22479 case DW_FORM_addrx:
22480 case DW_FORM_GNU_addr_index:
22481 {
22482 gdb_byte *tem;
22483
22484 *len = cu->header.addr_size;
22485 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22486 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22487 result = tem;
22488 }
22489 break;
22490 case DW_FORM_string:
22491 case DW_FORM_strp:
22492 case DW_FORM_strx:
22493 case DW_FORM_GNU_str_index:
22494 case DW_FORM_GNU_strp_alt:
22495 /* DW_STRING is already allocated on the objfile obstack, point
22496 directly to it. */
22497 result = (const gdb_byte *) DW_STRING (attr);
22498 *len = strlen (DW_STRING (attr));
22499 break;
22500 case DW_FORM_block1:
22501 case DW_FORM_block2:
22502 case DW_FORM_block4:
22503 case DW_FORM_block:
22504 case DW_FORM_exprloc:
22505 case DW_FORM_data16:
22506 result = DW_BLOCK (attr)->data;
22507 *len = DW_BLOCK (attr)->size;
22508 break;
22509
22510 /* The DW_AT_const_value attributes are supposed to carry the
22511 symbol's value "represented as it would be on the target
22512 architecture." By the time we get here, it's already been
22513 converted to host endianness, so we just need to sign- or
22514 zero-extend it as appropriate. */
22515 case DW_FORM_data1:
22516 type = die_type (die, cu);
22517 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22518 if (result == NULL)
22519 result = write_constant_as_bytes (obstack, byte_order,
22520 type, value, len);
22521 break;
22522 case DW_FORM_data2:
22523 type = die_type (die, cu);
22524 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22525 if (result == NULL)
22526 result = write_constant_as_bytes (obstack, byte_order,
22527 type, value, len);
22528 break;
22529 case DW_FORM_data4:
22530 type = die_type (die, cu);
22531 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22532 if (result == NULL)
22533 result = write_constant_as_bytes (obstack, byte_order,
22534 type, value, len);
22535 break;
22536 case DW_FORM_data8:
22537 type = die_type (die, cu);
22538 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22539 if (result == NULL)
22540 result = write_constant_as_bytes (obstack, byte_order,
22541 type, value, len);
22542 break;
22543
22544 case DW_FORM_sdata:
22545 case DW_FORM_implicit_const:
22546 type = die_type (die, cu);
22547 result = write_constant_as_bytes (obstack, byte_order,
22548 type, DW_SND (attr), len);
22549 break;
22550
22551 case DW_FORM_udata:
22552 type = die_type (die, cu);
22553 result = write_constant_as_bytes (obstack, byte_order,
22554 type, DW_UNSND (attr), len);
22555 break;
22556
22557 default:
22558 complaint (_("unsupported const value attribute form: '%s'"),
22559 dwarf_form_name (attr->form));
22560 break;
22561 }
22562
22563 return result;
22564 }
22565
22566 /* See read.h. */
22567
22568 struct type *
22569 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22570 dwarf2_per_cu_data *per_cu,
22571 dwarf2_per_objfile *per_objfile)
22572 {
22573 struct dwarf2_cu *cu;
22574 struct die_info *die;
22575
22576 if (per_cu->cu == NULL)
22577 load_cu (per_cu, per_objfile, false);
22578 cu = per_cu->cu;
22579 if (!cu)
22580 return NULL;
22581
22582 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22583 if (!die)
22584 return NULL;
22585
22586 return die_type (die, cu);
22587 }
22588
22589 /* See read.h. */
22590
22591 struct type *
22592 dwarf2_get_die_type (cu_offset die_offset,
22593 dwarf2_per_cu_data *per_cu,
22594 dwarf2_per_objfile *per_objfile)
22595 {
22596 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22597 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22598 }
22599
22600 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22601 On entry *REF_CU is the CU of SRC_DIE.
22602 On exit *REF_CU is the CU of the result.
22603 Returns NULL if the referenced DIE isn't found. */
22604
22605 static struct die_info *
22606 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22607 struct dwarf2_cu **ref_cu)
22608 {
22609 struct die_info temp_die;
22610 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22611 struct die_info *die;
22612 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22613
22614
22615 /* While it might be nice to assert sig_type->type == NULL here,
22616 we can get here for DW_AT_imported_declaration where we need
22617 the DIE not the type. */
22618
22619 /* If necessary, add it to the queue and load its DIEs. */
22620
22621 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22622 language_minimal))
22623 read_signatured_type (sig_type, dwarf2_per_objfile);
22624
22625 sig_cu = sig_type->per_cu.cu;
22626 gdb_assert (sig_cu != NULL);
22627 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22628 temp_die.sect_off = sig_type->type_offset_in_section;
22629 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22630 to_underlying (temp_die.sect_off));
22631 if (die)
22632 {
22633 /* For .gdb_index version 7 keep track of included TUs.
22634 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22635 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22636 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22637 {
22638 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22639 }
22640
22641 *ref_cu = sig_cu;
22642 if (sig_cu != cu)
22643 sig_cu->ancestor = cu;
22644
22645 return die;
22646 }
22647
22648 return NULL;
22649 }
22650
22651 /* Follow signatured type referenced by ATTR in SRC_DIE.
22652 On entry *REF_CU is the CU of SRC_DIE.
22653 On exit *REF_CU is the CU of the result.
22654 The result is the DIE of the type.
22655 If the referenced type cannot be found an error is thrown. */
22656
22657 static struct die_info *
22658 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22659 struct dwarf2_cu **ref_cu)
22660 {
22661 ULONGEST signature = DW_SIGNATURE (attr);
22662 struct signatured_type *sig_type;
22663 struct die_info *die;
22664
22665 gdb_assert (attr->form == DW_FORM_ref_sig8);
22666
22667 sig_type = lookup_signatured_type (*ref_cu, signature);
22668 /* sig_type will be NULL if the signatured type is missing from
22669 the debug info. */
22670 if (sig_type == NULL)
22671 {
22672 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22673 " from DIE at %s [in module %s]"),
22674 hex_string (signature), sect_offset_str (src_die->sect_off),
22675 objfile_name ((*ref_cu)->per_objfile->objfile));
22676 }
22677
22678 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22679 if (die == NULL)
22680 {
22681 dump_die_for_error (src_die);
22682 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22683 " from DIE at %s [in module %s]"),
22684 hex_string (signature), sect_offset_str (src_die->sect_off),
22685 objfile_name ((*ref_cu)->per_objfile->objfile));
22686 }
22687
22688 return die;
22689 }
22690
22691 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22692 reading in and processing the type unit if necessary. */
22693
22694 static struct type *
22695 get_signatured_type (struct die_info *die, ULONGEST signature,
22696 struct dwarf2_cu *cu)
22697 {
22698 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22699 struct signatured_type *sig_type;
22700 struct dwarf2_cu *type_cu;
22701 struct die_info *type_die;
22702 struct type *type;
22703
22704 sig_type = lookup_signatured_type (cu, signature);
22705 /* sig_type will be NULL if the signatured type is missing from
22706 the debug info. */
22707 if (sig_type == NULL)
22708 {
22709 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22710 " from DIE at %s [in module %s]"),
22711 hex_string (signature), sect_offset_str (die->sect_off),
22712 objfile_name (dwarf2_per_objfile->objfile));
22713 return build_error_marker_type (cu, die);
22714 }
22715
22716 /* If we already know the type we're done. */
22717 if (sig_type->type != NULL)
22718 return sig_type->type;
22719
22720 type_cu = cu;
22721 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22722 if (type_die != NULL)
22723 {
22724 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22725 is created. This is important, for example, because for c++ classes
22726 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22727 type = read_type_die (type_die, type_cu);
22728 if (type == NULL)
22729 {
22730 complaint (_("Dwarf Error: Cannot build signatured type %s"
22731 " referenced from DIE at %s [in module %s]"),
22732 hex_string (signature), sect_offset_str (die->sect_off),
22733 objfile_name (dwarf2_per_objfile->objfile));
22734 type = build_error_marker_type (cu, die);
22735 }
22736 }
22737 else
22738 {
22739 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22740 " from DIE at %s [in module %s]"),
22741 hex_string (signature), sect_offset_str (die->sect_off),
22742 objfile_name (dwarf2_per_objfile->objfile));
22743 type = build_error_marker_type (cu, die);
22744 }
22745 sig_type->type = type;
22746
22747 return type;
22748 }
22749
22750 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22751 reading in and processing the type unit if necessary. */
22752
22753 static struct type *
22754 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22755 struct dwarf2_cu *cu) /* ARI: editCase function */
22756 {
22757 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22758 if (attr->form_is_ref ())
22759 {
22760 struct dwarf2_cu *type_cu = cu;
22761 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22762
22763 return read_type_die (type_die, type_cu);
22764 }
22765 else if (attr->form == DW_FORM_ref_sig8)
22766 {
22767 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22768 }
22769 else
22770 {
22771 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22772
22773 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22774 " at %s [in module %s]"),
22775 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22776 objfile_name (dwarf2_per_objfile->objfile));
22777 return build_error_marker_type (cu, die);
22778 }
22779 }
22780
22781 /* Load the DIEs associated with type unit PER_CU into memory. */
22782
22783 static void
22784 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22785 dwarf2_per_objfile *per_objfile)
22786 {
22787 struct signatured_type *sig_type;
22788
22789 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22790 gdb_assert (! per_cu->type_unit_group_p ());
22791
22792 /* We have the per_cu, but we need the signatured_type.
22793 Fortunately this is an easy translation. */
22794 gdb_assert (per_cu->is_debug_types);
22795 sig_type = (struct signatured_type *) per_cu;
22796
22797 gdb_assert (per_cu->cu == NULL);
22798
22799 read_signatured_type (sig_type, per_objfile);
22800
22801 gdb_assert (per_cu->cu != NULL);
22802 }
22803
22804 /* Read in a signatured type and build its CU and DIEs.
22805 If the type is a stub for the real type in a DWO file,
22806 read in the real type from the DWO file as well. */
22807
22808 static void
22809 read_signatured_type (signatured_type *sig_type,
22810 dwarf2_per_objfile *per_objfile)
22811 {
22812 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22813
22814 gdb_assert (per_cu->is_debug_types);
22815 gdb_assert (per_cu->cu == NULL);
22816
22817 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22818
22819 if (!reader.dummy_p)
22820 {
22821 struct dwarf2_cu *cu = reader.cu;
22822 const gdb_byte *info_ptr = reader.info_ptr;
22823
22824 gdb_assert (cu->die_hash == NULL);
22825 cu->die_hash =
22826 htab_create_alloc_ex (cu->header.length / 12,
22827 die_hash,
22828 die_eq,
22829 NULL,
22830 &cu->comp_unit_obstack,
22831 hashtab_obstack_allocate,
22832 dummy_obstack_deallocate);
22833
22834 if (reader.comp_unit_die->has_children)
22835 reader.comp_unit_die->child
22836 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22837 reader.comp_unit_die);
22838 cu->dies = reader.comp_unit_die;
22839 /* comp_unit_die is not stored in die_hash, no need. */
22840
22841 /* We try not to read any attributes in this function, because
22842 not all CUs needed for references have been loaded yet, and
22843 symbol table processing isn't initialized. But we have to
22844 set the CU language, or we won't be able to build types
22845 correctly. Similarly, if we do not read the producer, we can
22846 not apply producer-specific interpretation. */
22847 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22848
22849 reader.keep ();
22850 }
22851
22852 sig_type->per_cu.tu_read = 1;
22853 }
22854
22855 /* Decode simple location descriptions.
22856 Given a pointer to a dwarf block that defines a location, compute
22857 the location and return the value. If COMPUTED is non-null, it is
22858 set to true to indicate that decoding was successful, and false
22859 otherwise. If COMPUTED is null, then this function may emit a
22860 complaint. */
22861
22862 static CORE_ADDR
22863 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22864 {
22865 struct objfile *objfile = cu->per_objfile->objfile;
22866 size_t i;
22867 size_t size = blk->size;
22868 const gdb_byte *data = blk->data;
22869 CORE_ADDR stack[64];
22870 int stacki;
22871 unsigned int bytes_read, unsnd;
22872 gdb_byte op;
22873
22874 if (computed != nullptr)
22875 *computed = false;
22876
22877 i = 0;
22878 stacki = 0;
22879 stack[stacki] = 0;
22880 stack[++stacki] = 0;
22881
22882 while (i < size)
22883 {
22884 op = data[i++];
22885 switch (op)
22886 {
22887 case DW_OP_lit0:
22888 case DW_OP_lit1:
22889 case DW_OP_lit2:
22890 case DW_OP_lit3:
22891 case DW_OP_lit4:
22892 case DW_OP_lit5:
22893 case DW_OP_lit6:
22894 case DW_OP_lit7:
22895 case DW_OP_lit8:
22896 case DW_OP_lit9:
22897 case DW_OP_lit10:
22898 case DW_OP_lit11:
22899 case DW_OP_lit12:
22900 case DW_OP_lit13:
22901 case DW_OP_lit14:
22902 case DW_OP_lit15:
22903 case DW_OP_lit16:
22904 case DW_OP_lit17:
22905 case DW_OP_lit18:
22906 case DW_OP_lit19:
22907 case DW_OP_lit20:
22908 case DW_OP_lit21:
22909 case DW_OP_lit22:
22910 case DW_OP_lit23:
22911 case DW_OP_lit24:
22912 case DW_OP_lit25:
22913 case DW_OP_lit26:
22914 case DW_OP_lit27:
22915 case DW_OP_lit28:
22916 case DW_OP_lit29:
22917 case DW_OP_lit30:
22918 case DW_OP_lit31:
22919 stack[++stacki] = op - DW_OP_lit0;
22920 break;
22921
22922 case DW_OP_reg0:
22923 case DW_OP_reg1:
22924 case DW_OP_reg2:
22925 case DW_OP_reg3:
22926 case DW_OP_reg4:
22927 case DW_OP_reg5:
22928 case DW_OP_reg6:
22929 case DW_OP_reg7:
22930 case DW_OP_reg8:
22931 case DW_OP_reg9:
22932 case DW_OP_reg10:
22933 case DW_OP_reg11:
22934 case DW_OP_reg12:
22935 case DW_OP_reg13:
22936 case DW_OP_reg14:
22937 case DW_OP_reg15:
22938 case DW_OP_reg16:
22939 case DW_OP_reg17:
22940 case DW_OP_reg18:
22941 case DW_OP_reg19:
22942 case DW_OP_reg20:
22943 case DW_OP_reg21:
22944 case DW_OP_reg22:
22945 case DW_OP_reg23:
22946 case DW_OP_reg24:
22947 case DW_OP_reg25:
22948 case DW_OP_reg26:
22949 case DW_OP_reg27:
22950 case DW_OP_reg28:
22951 case DW_OP_reg29:
22952 case DW_OP_reg30:
22953 case DW_OP_reg31:
22954 stack[++stacki] = op - DW_OP_reg0;
22955 if (i < size)
22956 {
22957 if (computed == nullptr)
22958 dwarf2_complex_location_expr_complaint ();
22959 else
22960 return 0;
22961 }
22962 break;
22963
22964 case DW_OP_regx:
22965 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22966 i += bytes_read;
22967 stack[++stacki] = unsnd;
22968 if (i < size)
22969 {
22970 if (computed == nullptr)
22971 dwarf2_complex_location_expr_complaint ();
22972 else
22973 return 0;
22974 }
22975 break;
22976
22977 case DW_OP_addr:
22978 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22979 &bytes_read);
22980 i += bytes_read;
22981 break;
22982
22983 case DW_OP_const1u:
22984 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22985 i += 1;
22986 break;
22987
22988 case DW_OP_const1s:
22989 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22990 i += 1;
22991 break;
22992
22993 case DW_OP_const2u:
22994 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22995 i += 2;
22996 break;
22997
22998 case DW_OP_const2s:
22999 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23000 i += 2;
23001 break;
23002
23003 case DW_OP_const4u:
23004 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23005 i += 4;
23006 break;
23007
23008 case DW_OP_const4s:
23009 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23010 i += 4;
23011 break;
23012
23013 case DW_OP_const8u:
23014 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23015 i += 8;
23016 break;
23017
23018 case DW_OP_constu:
23019 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23020 &bytes_read);
23021 i += bytes_read;
23022 break;
23023
23024 case DW_OP_consts:
23025 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23026 i += bytes_read;
23027 break;
23028
23029 case DW_OP_dup:
23030 stack[stacki + 1] = stack[stacki];
23031 stacki++;
23032 break;
23033
23034 case DW_OP_plus:
23035 stack[stacki - 1] += stack[stacki];
23036 stacki--;
23037 break;
23038
23039 case DW_OP_plus_uconst:
23040 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23041 &bytes_read);
23042 i += bytes_read;
23043 break;
23044
23045 case DW_OP_minus:
23046 stack[stacki - 1] -= stack[stacki];
23047 stacki--;
23048 break;
23049
23050 case DW_OP_deref:
23051 /* If we're not the last op, then we definitely can't encode
23052 this using GDB's address_class enum. This is valid for partial
23053 global symbols, although the variable's address will be bogus
23054 in the psymtab. */
23055 if (i < size)
23056 {
23057 if (computed == nullptr)
23058 dwarf2_complex_location_expr_complaint ();
23059 else
23060 return 0;
23061 }
23062 break;
23063
23064 case DW_OP_GNU_push_tls_address:
23065 case DW_OP_form_tls_address:
23066 /* The top of the stack has the offset from the beginning
23067 of the thread control block at which the variable is located. */
23068 /* Nothing should follow this operator, so the top of stack would
23069 be returned. */
23070 /* This is valid for partial global symbols, but the variable's
23071 address will be bogus in the psymtab. Make it always at least
23072 non-zero to not look as a variable garbage collected by linker
23073 which have DW_OP_addr 0. */
23074 if (i < size)
23075 {
23076 if (computed == nullptr)
23077 dwarf2_complex_location_expr_complaint ();
23078 else
23079 return 0;
23080 }
23081 stack[stacki]++;
23082 break;
23083
23084 case DW_OP_GNU_uninit:
23085 if (computed != nullptr)
23086 return 0;
23087 break;
23088
23089 case DW_OP_addrx:
23090 case DW_OP_GNU_addr_index:
23091 case DW_OP_GNU_const_index:
23092 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23093 &bytes_read);
23094 i += bytes_read;
23095 break;
23096
23097 default:
23098 if (computed == nullptr)
23099 {
23100 const char *name = get_DW_OP_name (op);
23101
23102 if (name)
23103 complaint (_("unsupported stack op: '%s'"),
23104 name);
23105 else
23106 complaint (_("unsupported stack op: '%02x'"),
23107 op);
23108 }
23109
23110 return (stack[stacki]);
23111 }
23112
23113 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23114 outside of the allocated space. Also enforce minimum>0. */
23115 if (stacki >= ARRAY_SIZE (stack) - 1)
23116 {
23117 if (computed == nullptr)
23118 complaint (_("location description stack overflow"));
23119 return 0;
23120 }
23121
23122 if (stacki <= 0)
23123 {
23124 if (computed == nullptr)
23125 complaint (_("location description stack underflow"));
23126 return 0;
23127 }
23128 }
23129
23130 if (computed != nullptr)
23131 *computed = true;
23132 return (stack[stacki]);
23133 }
23134
23135 /* memory allocation interface */
23136
23137 static struct dwarf_block *
23138 dwarf_alloc_block (struct dwarf2_cu *cu)
23139 {
23140 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23141 }
23142
23143 static struct die_info *
23144 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23145 {
23146 struct die_info *die;
23147 size_t size = sizeof (struct die_info);
23148
23149 if (num_attrs > 1)
23150 size += (num_attrs - 1) * sizeof (struct attribute);
23151
23152 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23153 memset (die, 0, sizeof (struct die_info));
23154 return (die);
23155 }
23156
23157 \f
23158
23159 /* Macro support. */
23160
23161 /* An overload of dwarf_decode_macros that finds the correct section
23162 and ensures it is read in before calling the other overload. */
23163
23164 static void
23165 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23166 int section_is_gnu)
23167 {
23168 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23169 struct objfile *objfile = dwarf2_per_objfile->objfile;
23170 const struct line_header *lh = cu->line_header;
23171 unsigned int offset_size = cu->header.offset_size;
23172 struct dwarf2_section_info *section;
23173 const char *section_name;
23174
23175 if (cu->dwo_unit != nullptr)
23176 {
23177 if (section_is_gnu)
23178 {
23179 section = &cu->dwo_unit->dwo_file->sections.macro;
23180 section_name = ".debug_macro.dwo";
23181 }
23182 else
23183 {
23184 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23185 section_name = ".debug_macinfo.dwo";
23186 }
23187 }
23188 else
23189 {
23190 if (section_is_gnu)
23191 {
23192 section = &dwarf2_per_objfile->per_bfd->macro;
23193 section_name = ".debug_macro";
23194 }
23195 else
23196 {
23197 section = &dwarf2_per_objfile->per_bfd->macinfo;
23198 section_name = ".debug_macinfo";
23199 }
23200 }
23201
23202 section->read (objfile);
23203 if (section->buffer == nullptr)
23204 {
23205 complaint (_("missing %s section"), section_name);
23206 return;
23207 }
23208
23209 buildsym_compunit *builder = cu->get_builder ();
23210
23211 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23212 offset_size, offset, section_is_gnu);
23213 }
23214
23215 /* Return the .debug_loc section to use for CU.
23216 For DWO files use .debug_loc.dwo. */
23217
23218 static struct dwarf2_section_info *
23219 cu_debug_loc_section (struct dwarf2_cu *cu)
23220 {
23221 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23222
23223 if (cu->dwo_unit)
23224 {
23225 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23226
23227 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23228 }
23229 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23230 : &dwarf2_per_objfile->per_bfd->loc);
23231 }
23232
23233 /* A helper function that fills in a dwarf2_loclist_baton. */
23234
23235 static void
23236 fill_in_loclist_baton (struct dwarf2_cu *cu,
23237 struct dwarf2_loclist_baton *baton,
23238 const struct attribute *attr)
23239 {
23240 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23241 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23242
23243 section->read (dwarf2_per_objfile->objfile);
23244
23245 baton->per_objfile = dwarf2_per_objfile;
23246 baton->per_cu = cu->per_cu;
23247 gdb_assert (baton->per_cu);
23248 /* We don't know how long the location list is, but make sure we
23249 don't run off the edge of the section. */
23250 baton->size = section->size - DW_UNSND (attr);
23251 baton->data = section->buffer + DW_UNSND (attr);
23252 if (cu->base_address.has_value ())
23253 baton->base_address = *cu->base_address;
23254 else
23255 baton->base_address = 0;
23256 baton->from_dwo = cu->dwo_unit != NULL;
23257 }
23258
23259 static void
23260 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23261 struct dwarf2_cu *cu, int is_block)
23262 {
23263 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23264 struct objfile *objfile = dwarf2_per_objfile->objfile;
23265 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23266
23267 if (attr->form_is_section_offset ()
23268 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23269 the section. If so, fall through to the complaint in the
23270 other branch. */
23271 && DW_UNSND (attr) < section->get_size (objfile))
23272 {
23273 struct dwarf2_loclist_baton *baton;
23274
23275 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23276
23277 fill_in_loclist_baton (cu, baton, attr);
23278
23279 if (!cu->base_address.has_value ())
23280 complaint (_("Location list used without "
23281 "specifying the CU base address."));
23282
23283 SYMBOL_ACLASS_INDEX (sym) = (is_block
23284 ? dwarf2_loclist_block_index
23285 : dwarf2_loclist_index);
23286 SYMBOL_LOCATION_BATON (sym) = baton;
23287 }
23288 else
23289 {
23290 struct dwarf2_locexpr_baton *baton;
23291
23292 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23293 baton->per_objfile = dwarf2_per_objfile;
23294 baton->per_cu = cu->per_cu;
23295 gdb_assert (baton->per_cu);
23296
23297 if (attr->form_is_block ())
23298 {
23299 /* Note that we're just copying the block's data pointer
23300 here, not the actual data. We're still pointing into the
23301 info_buffer for SYM's objfile; right now we never release
23302 that buffer, but when we do clean up properly this may
23303 need to change. */
23304 baton->size = DW_BLOCK (attr)->size;
23305 baton->data = DW_BLOCK (attr)->data;
23306 }
23307 else
23308 {
23309 dwarf2_invalid_attrib_class_complaint ("location description",
23310 sym->natural_name ());
23311 baton->size = 0;
23312 }
23313
23314 SYMBOL_ACLASS_INDEX (sym) = (is_block
23315 ? dwarf2_locexpr_block_index
23316 : dwarf2_locexpr_index);
23317 SYMBOL_LOCATION_BATON (sym) = baton;
23318 }
23319 }
23320
23321 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23322 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23323 CU_HEADERP first. */
23324
23325 static const struct comp_unit_head *
23326 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23327 const struct dwarf2_per_cu_data *per_cu)
23328 {
23329 const gdb_byte *info_ptr;
23330
23331 if (per_cu->cu)
23332 return &per_cu->cu->header;
23333
23334 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23335
23336 memset (cu_headerp, 0, sizeof (*cu_headerp));
23337 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23338 rcuh_kind::COMPILE);
23339
23340 return cu_headerp;
23341 }
23342
23343 /* See read.h. */
23344
23345 int
23346 dwarf2_per_cu_data::addr_size () const
23347 {
23348 struct comp_unit_head cu_header_local;
23349 const struct comp_unit_head *cu_headerp;
23350
23351 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23352
23353 return cu_headerp->addr_size;
23354 }
23355
23356 /* See read.h. */
23357
23358 int
23359 dwarf2_per_cu_data::offset_size () const
23360 {
23361 struct comp_unit_head cu_header_local;
23362 const struct comp_unit_head *cu_headerp;
23363
23364 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23365
23366 return cu_headerp->offset_size;
23367 }
23368
23369 /* See read.h. */
23370
23371 int
23372 dwarf2_per_cu_data::ref_addr_size () const
23373 {
23374 struct comp_unit_head cu_header_local;
23375 const struct comp_unit_head *cu_headerp;
23376
23377 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23378
23379 if (cu_headerp->version == 2)
23380 return cu_headerp->addr_size;
23381 else
23382 return cu_headerp->offset_size;
23383 }
23384
23385 /* See read.h. */
23386
23387 struct type *
23388 dwarf2_cu::addr_type () const
23389 {
23390 struct objfile *objfile = this->per_objfile->objfile;
23391 struct type *void_type = objfile_type (objfile)->builtin_void;
23392 struct type *addr_type = lookup_pointer_type (void_type);
23393 int addr_size = this->per_cu->addr_size ();
23394
23395 if (TYPE_LENGTH (addr_type) == addr_size)
23396 return addr_type;
23397
23398 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23399 return addr_type;
23400 }
23401
23402 /* A helper function for dwarf2_find_containing_comp_unit that returns
23403 the index of the result, and that searches a vector. It will
23404 return a result even if the offset in question does not actually
23405 occur in any CU. This is separate so that it can be unit
23406 tested. */
23407
23408 static int
23409 dwarf2_find_containing_comp_unit
23410 (sect_offset sect_off,
23411 unsigned int offset_in_dwz,
23412 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23413 {
23414 int low, high;
23415
23416 low = 0;
23417 high = all_comp_units.size () - 1;
23418 while (high > low)
23419 {
23420 struct dwarf2_per_cu_data *mid_cu;
23421 int mid = low + (high - low) / 2;
23422
23423 mid_cu = all_comp_units[mid];
23424 if (mid_cu->is_dwz > offset_in_dwz
23425 || (mid_cu->is_dwz == offset_in_dwz
23426 && mid_cu->sect_off + mid_cu->length > sect_off))
23427 high = mid;
23428 else
23429 low = mid + 1;
23430 }
23431 gdb_assert (low == high);
23432 return low;
23433 }
23434
23435 /* Locate the .debug_info compilation unit from CU's objfile which contains
23436 the DIE at OFFSET. Raises an error on failure. */
23437
23438 static struct dwarf2_per_cu_data *
23439 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23440 unsigned int offset_in_dwz,
23441 struct dwarf2_per_objfile *dwarf2_per_objfile)
23442 {
23443 int low
23444 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23445 dwarf2_per_objfile->per_bfd->all_comp_units);
23446 struct dwarf2_per_cu_data *this_cu
23447 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23448
23449 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23450 {
23451 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23452 error (_("Dwarf Error: could not find partial DIE containing "
23453 "offset %s [in module %s]"),
23454 sect_offset_str (sect_off),
23455 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23456
23457 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23458 <= sect_off);
23459 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23460 }
23461 else
23462 {
23463 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23464 && sect_off >= this_cu->sect_off + this_cu->length)
23465 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23466 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23467 return this_cu;
23468 }
23469 }
23470
23471 #if GDB_SELF_TEST
23472
23473 namespace selftests {
23474 namespace find_containing_comp_unit {
23475
23476 static void
23477 run_test ()
23478 {
23479 struct dwarf2_per_cu_data one {};
23480 struct dwarf2_per_cu_data two {};
23481 struct dwarf2_per_cu_data three {};
23482 struct dwarf2_per_cu_data four {};
23483
23484 one.length = 5;
23485 two.sect_off = sect_offset (one.length);
23486 two.length = 7;
23487
23488 three.length = 5;
23489 three.is_dwz = 1;
23490 four.sect_off = sect_offset (three.length);
23491 four.length = 7;
23492 four.is_dwz = 1;
23493
23494 std::vector<dwarf2_per_cu_data *> units;
23495 units.push_back (&one);
23496 units.push_back (&two);
23497 units.push_back (&three);
23498 units.push_back (&four);
23499
23500 int result;
23501
23502 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23503 SELF_CHECK (units[result] == &one);
23504 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23505 SELF_CHECK (units[result] == &one);
23506 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23507 SELF_CHECK (units[result] == &two);
23508
23509 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23510 SELF_CHECK (units[result] == &three);
23511 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23512 SELF_CHECK (units[result] == &three);
23513 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23514 SELF_CHECK (units[result] == &four);
23515 }
23516
23517 }
23518 }
23519
23520 #endif /* GDB_SELF_TEST */
23521
23522 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23523
23524 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23525 dwarf2_per_objfile *per_objfile)
23526 : per_cu (per_cu),
23527 per_objfile (per_objfile),
23528 mark (false),
23529 has_loclist (false),
23530 checked_producer (false),
23531 producer_is_gxx_lt_4_6 (false),
23532 producer_is_gcc_lt_4_3 (false),
23533 producer_is_icc (false),
23534 producer_is_icc_lt_14 (false),
23535 producer_is_codewarrior (false),
23536 processing_has_namespace_info (false)
23537 {
23538 per_cu->cu = this;
23539 }
23540
23541 /* Destroy a dwarf2_cu. */
23542
23543 dwarf2_cu::~dwarf2_cu ()
23544 {
23545 per_cu->cu = NULL;
23546 }
23547
23548 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23549
23550 static void
23551 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23552 enum language pretend_language)
23553 {
23554 struct attribute *attr;
23555
23556 /* Set the language we're debugging. */
23557 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23558 if (attr != nullptr)
23559 set_cu_language (DW_UNSND (attr), cu);
23560 else
23561 {
23562 cu->language = pretend_language;
23563 cu->language_defn = language_def (cu->language);
23564 }
23565
23566 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23567 }
23568
23569 /* Increase the age counter on each cached compilation unit, and free
23570 any that are too old. */
23571
23572 static void
23573 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23574 {
23575 struct dwarf2_per_cu_data *per_cu, **last_chain;
23576
23577 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23578 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23579 while (per_cu != NULL)
23580 {
23581 per_cu->cu->last_used ++;
23582 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23583 dwarf2_mark (per_cu->cu);
23584 per_cu = per_cu->cu->read_in_chain;
23585 }
23586
23587 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23588 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23589 while (per_cu != NULL)
23590 {
23591 struct dwarf2_per_cu_data *next_cu;
23592
23593 next_cu = per_cu->cu->read_in_chain;
23594
23595 if (!per_cu->cu->mark)
23596 {
23597 delete per_cu->cu;
23598 *last_chain = next_cu;
23599 }
23600 else
23601 last_chain = &per_cu->cu->read_in_chain;
23602
23603 per_cu = next_cu;
23604 }
23605 }
23606
23607 /* Remove a single compilation unit from the cache. */
23608
23609 static void
23610 free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23611 dwarf2_per_objfile *dwarf2_per_objfile)
23612 {
23613 struct dwarf2_per_cu_data *per_cu, **last_chain;
23614
23615 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23616 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23617 while (per_cu != NULL)
23618 {
23619 struct dwarf2_per_cu_data *next_cu;
23620
23621 next_cu = per_cu->cu->read_in_chain;
23622
23623 if (per_cu == target_per_cu)
23624 {
23625 delete per_cu->cu;
23626 per_cu->cu = NULL;
23627 *last_chain = next_cu;
23628 break;
23629 }
23630 else
23631 last_chain = &per_cu->cu->read_in_chain;
23632
23633 per_cu = next_cu;
23634 }
23635 }
23636
23637 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23638 We store these in a hash table separate from the DIEs, and preserve them
23639 when the DIEs are flushed out of cache.
23640
23641 The CU "per_cu" pointer is needed because offset alone is not enough to
23642 uniquely identify the type. A file may have multiple .debug_types sections,
23643 or the type may come from a DWO file. Furthermore, while it's more logical
23644 to use per_cu->section+offset, with Fission the section with the data is in
23645 the DWO file but we don't know that section at the point we need it.
23646 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23647 because we can enter the lookup routine, get_die_type_at_offset, from
23648 outside this file, and thus won't necessarily have PER_CU->cu.
23649 Fortunately, PER_CU is stable for the life of the objfile. */
23650
23651 struct dwarf2_per_cu_offset_and_type
23652 {
23653 const struct dwarf2_per_cu_data *per_cu;
23654 sect_offset sect_off;
23655 struct type *type;
23656 };
23657
23658 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23659
23660 static hashval_t
23661 per_cu_offset_and_type_hash (const void *item)
23662 {
23663 const struct dwarf2_per_cu_offset_and_type *ofs
23664 = (const struct dwarf2_per_cu_offset_and_type *) item;
23665
23666 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23667 }
23668
23669 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23670
23671 static int
23672 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23673 {
23674 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23675 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23676 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23677 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23678
23679 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23680 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23681 }
23682
23683 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23684 table if necessary. For convenience, return TYPE.
23685
23686 The DIEs reading must have careful ordering to:
23687 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23688 reading current DIE.
23689 * Not trying to dereference contents of still incompletely read in types
23690 while reading in other DIEs.
23691 * Enable referencing still incompletely read in types just by a pointer to
23692 the type without accessing its fields.
23693
23694 Therefore caller should follow these rules:
23695 * Try to fetch any prerequisite types we may need to build this DIE type
23696 before building the type and calling set_die_type.
23697 * After building type call set_die_type for current DIE as soon as
23698 possible before fetching more types to complete the current type.
23699 * Make the type as complete as possible before fetching more types. */
23700
23701 static struct type *
23702 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23703 {
23704 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23705 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23706 struct objfile *objfile = dwarf2_per_objfile->objfile;
23707 struct attribute *attr;
23708 struct dynamic_prop prop;
23709
23710 /* For Ada types, make sure that the gnat-specific data is always
23711 initialized (if not already set). There are a few types where
23712 we should not be doing so, because the type-specific area is
23713 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23714 where the type-specific area is used to store the floatformat).
23715 But this is not a problem, because the gnat-specific information
23716 is actually not needed for these types. */
23717 if (need_gnat_info (cu)
23718 && type->code () != TYPE_CODE_FUNC
23719 && type->code () != TYPE_CODE_FLT
23720 && type->code () != TYPE_CODE_METHODPTR
23721 && type->code () != TYPE_CODE_MEMBERPTR
23722 && type->code () != TYPE_CODE_METHOD
23723 && !HAVE_GNAT_AUX_INFO (type))
23724 INIT_GNAT_SPECIFIC (type);
23725
23726 /* Read DW_AT_allocated and set in type. */
23727 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23728 if (attr != NULL && attr->form_is_block ())
23729 {
23730 struct type *prop_type = cu->addr_sized_int_type (false);
23731 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23732 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23733 }
23734 else if (attr != NULL)
23735 {
23736 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23737 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23738 sect_offset_str (die->sect_off));
23739 }
23740
23741 /* Read DW_AT_associated and set in type. */
23742 attr = dwarf2_attr (die, DW_AT_associated, cu);
23743 if (attr != NULL && attr->form_is_block ())
23744 {
23745 struct type *prop_type = cu->addr_sized_int_type (false);
23746 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23747 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23748 }
23749 else if (attr != NULL)
23750 {
23751 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23752 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23753 sect_offset_str (die->sect_off));
23754 }
23755
23756 /* Read DW_AT_data_location and set in type. */
23757 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23758 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23759 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23760
23761 if (dwarf2_per_objfile->die_type_hash == NULL)
23762 dwarf2_per_objfile->die_type_hash
23763 = htab_up (htab_create_alloc (127,
23764 per_cu_offset_and_type_hash,
23765 per_cu_offset_and_type_eq,
23766 NULL, xcalloc, xfree));
23767
23768 ofs.per_cu = cu->per_cu;
23769 ofs.sect_off = die->sect_off;
23770 ofs.type = type;
23771 slot = (struct dwarf2_per_cu_offset_and_type **)
23772 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23773 if (*slot)
23774 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23775 sect_offset_str (die->sect_off));
23776 *slot = XOBNEW (&objfile->objfile_obstack,
23777 struct dwarf2_per_cu_offset_and_type);
23778 **slot = ofs;
23779 return type;
23780 }
23781
23782 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23783 or return NULL if the die does not have a saved type. */
23784
23785 static struct type *
23786 get_die_type_at_offset (sect_offset sect_off,
23787 dwarf2_per_cu_data *per_cu,
23788 dwarf2_per_objfile *dwarf2_per_objfile)
23789 {
23790 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23791
23792 if (dwarf2_per_objfile->die_type_hash == NULL)
23793 return NULL;
23794
23795 ofs.per_cu = per_cu;
23796 ofs.sect_off = sect_off;
23797 slot = ((struct dwarf2_per_cu_offset_and_type *)
23798 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23799 if (slot)
23800 return slot->type;
23801 else
23802 return NULL;
23803 }
23804
23805 /* Look up the type for DIE in CU in die_type_hash,
23806 or return NULL if DIE does not have a saved type. */
23807
23808 static struct type *
23809 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23810 {
23811 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23812 }
23813
23814 /* Add a dependence relationship from CU to REF_PER_CU. */
23815
23816 static void
23817 dwarf2_add_dependence (struct dwarf2_cu *cu,
23818 struct dwarf2_per_cu_data *ref_per_cu)
23819 {
23820 void **slot;
23821
23822 if (cu->dependencies == NULL)
23823 cu->dependencies
23824 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23825 NULL, &cu->comp_unit_obstack,
23826 hashtab_obstack_allocate,
23827 dummy_obstack_deallocate);
23828
23829 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23830 if (*slot == NULL)
23831 *slot = ref_per_cu;
23832 }
23833
23834 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23835 Set the mark field in every compilation unit in the
23836 cache that we must keep because we are keeping CU. */
23837
23838 static int
23839 dwarf2_mark_helper (void **slot, void *data)
23840 {
23841 struct dwarf2_per_cu_data *per_cu;
23842
23843 per_cu = (struct dwarf2_per_cu_data *) *slot;
23844
23845 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23846 reading of the chain. As such dependencies remain valid it is not much
23847 useful to track and undo them during QUIT cleanups. */
23848 if (per_cu->cu == NULL)
23849 return 1;
23850
23851 if (per_cu->cu->mark)
23852 return 1;
23853 per_cu->cu->mark = true;
23854
23855 if (per_cu->cu->dependencies != NULL)
23856 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23857
23858 return 1;
23859 }
23860
23861 /* Set the mark field in CU and in every other compilation unit in the
23862 cache that we must keep because we are keeping CU. */
23863
23864 static void
23865 dwarf2_mark (struct dwarf2_cu *cu)
23866 {
23867 if (cu->mark)
23868 return;
23869 cu->mark = true;
23870 if (cu->dependencies != NULL)
23871 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23872 }
23873
23874 static void
23875 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23876 {
23877 while (per_cu)
23878 {
23879 per_cu->cu->mark = false;
23880 per_cu = per_cu->cu->read_in_chain;
23881 }
23882 }
23883
23884 /* Trivial hash function for partial_die_info: the hash value of a DIE
23885 is its offset in .debug_info for this objfile. */
23886
23887 static hashval_t
23888 partial_die_hash (const void *item)
23889 {
23890 const struct partial_die_info *part_die
23891 = (const struct partial_die_info *) item;
23892
23893 return to_underlying (part_die->sect_off);
23894 }
23895
23896 /* Trivial comparison function for partial_die_info structures: two DIEs
23897 are equal if they have the same offset. */
23898
23899 static int
23900 partial_die_eq (const void *item_lhs, const void *item_rhs)
23901 {
23902 const struct partial_die_info *part_die_lhs
23903 = (const struct partial_die_info *) item_lhs;
23904 const struct partial_die_info *part_die_rhs
23905 = (const struct partial_die_info *) item_rhs;
23906
23907 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23908 }
23909
23910 struct cmd_list_element *set_dwarf_cmdlist;
23911 struct cmd_list_element *show_dwarf_cmdlist;
23912
23913 static void
23914 show_check_physname (struct ui_file *file, int from_tty,
23915 struct cmd_list_element *c, const char *value)
23916 {
23917 fprintf_filtered (file,
23918 _("Whether to check \"physname\" is %s.\n"),
23919 value);
23920 }
23921
23922 void _initialize_dwarf2_read ();
23923 void
23924 _initialize_dwarf2_read ()
23925 {
23926 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23927 Set DWARF specific variables.\n\
23928 Configure DWARF variables such as the cache size."),
23929 &set_dwarf_cmdlist, "maintenance set dwarf ",
23930 0/*allow-unknown*/, &maintenance_set_cmdlist);
23931
23932 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23933 Show DWARF specific variables.\n\
23934 Show DWARF variables such as the cache size."),
23935 &show_dwarf_cmdlist, "maintenance show dwarf ",
23936 0/*allow-unknown*/, &maintenance_show_cmdlist);
23937
23938 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23939 &dwarf_max_cache_age, _("\
23940 Set the upper bound on the age of cached DWARF compilation units."), _("\
23941 Show the upper bound on the age of cached DWARF compilation units."), _("\
23942 A higher limit means that cached compilation units will be stored\n\
23943 in memory longer, and more total memory will be used. Zero disables\n\
23944 caching, which can slow down startup."),
23945 NULL,
23946 show_dwarf_max_cache_age,
23947 &set_dwarf_cmdlist,
23948 &show_dwarf_cmdlist);
23949
23950 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23951 Set debugging of the DWARF reader."), _("\
23952 Show debugging of the DWARF reader."), _("\
23953 When enabled (non-zero), debugging messages are printed during DWARF\n\
23954 reading and symtab expansion. A value of 1 (one) provides basic\n\
23955 information. A value greater than 1 provides more verbose information."),
23956 NULL,
23957 NULL,
23958 &setdebuglist, &showdebuglist);
23959
23960 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23961 Set debugging of the DWARF DIE reader."), _("\
23962 Show debugging of the DWARF DIE reader."), _("\
23963 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23964 The value is the maximum depth to print."),
23965 NULL,
23966 NULL,
23967 &setdebuglist, &showdebuglist);
23968
23969 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23970 Set debugging of the dwarf line reader."), _("\
23971 Show debugging of the dwarf line reader."), _("\
23972 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23973 A value of 1 (one) provides basic information.\n\
23974 A value greater than 1 provides more verbose information."),
23975 NULL,
23976 NULL,
23977 &setdebuglist, &showdebuglist);
23978
23979 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23980 Set cross-checking of \"physname\" code against demangler."), _("\
23981 Show cross-checking of \"physname\" code against demangler."), _("\
23982 When enabled, GDB's internal \"physname\" code is checked against\n\
23983 the demangler."),
23984 NULL, show_check_physname,
23985 &setdebuglist, &showdebuglist);
23986
23987 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23988 no_class, &use_deprecated_index_sections, _("\
23989 Set whether to use deprecated gdb_index sections."), _("\
23990 Show whether to use deprecated gdb_index sections."), _("\
23991 When enabled, deprecated .gdb_index sections are used anyway.\n\
23992 Normally they are ignored either because of a missing feature or\n\
23993 performance issue.\n\
23994 Warning: This option must be enabled before gdb reads the file."),
23995 NULL,
23996 NULL,
23997 &setlist, &showlist);
23998
23999 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24000 &dwarf2_locexpr_funcs);
24001 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24002 &dwarf2_loclist_funcs);
24003
24004 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24005 &dwarf2_block_frame_base_locexpr_funcs);
24006 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24007 &dwarf2_block_frame_base_loclist_funcs);
24008
24009 #if GDB_SELF_TEST
24010 selftests::register_test ("dw2_expand_symtabs_matching",
24011 selftests::dw2_expand_symtabs_matching::run_test);
24012 selftests::register_test ("dwarf2_find_containing_comp_unit",
24013 selftests::find_containing_comp_unit::run_test);
24014 #endif
24015 }
This page took 0.573925 seconds and 4 git commands to generate.