Split type_unit_group
[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. This contains elements of type unit groups
618 that can be shared across objfiles. The non-shareable parts are in
619 type_unit_group_unshareable. */
620
621 struct type_unit_group
622 {
623 /* dwarf2read.c's main "handle" on a TU symtab.
624 To simplify things we create an artificial CU that "includes" all the
625 type units using this stmt_list so that the rest of the code still has
626 a "per_cu" handle on the symtab. */
627 struct dwarf2_per_cu_data per_cu;
628
629 /* The TUs that share this DW_AT_stmt_list entry.
630 This is added to while parsing type units to build partial symtabs,
631 and is deleted afterwards and not used again. */
632 std::vector<signatured_type *> *tus;
633
634 /* The data used to construct the hash key. */
635 struct stmt_list_hash hash;
636 };
637
638 /* These sections are what may appear in a (real or virtual) DWO file. */
639
640 struct dwo_sections
641 {
642 struct dwarf2_section_info abbrev;
643 struct dwarf2_section_info line;
644 struct dwarf2_section_info loc;
645 struct dwarf2_section_info loclists;
646 struct dwarf2_section_info macinfo;
647 struct dwarf2_section_info macro;
648 struct dwarf2_section_info str;
649 struct dwarf2_section_info str_offsets;
650 /* In the case of a virtual DWO file, these two are unused. */
651 struct dwarf2_section_info info;
652 std::vector<dwarf2_section_info> types;
653 };
654
655 /* CUs/TUs in DWP/DWO files. */
656
657 struct dwo_unit
658 {
659 /* Backlink to the containing struct dwo_file. */
660 struct dwo_file *dwo_file;
661
662 /* The "id" that distinguishes this CU/TU.
663 .debug_info calls this "dwo_id", .debug_types calls this "signature".
664 Since signatures came first, we stick with it for consistency. */
665 ULONGEST signature;
666
667 /* The section this CU/TU lives in, in the DWO file. */
668 struct dwarf2_section_info *section;
669
670 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
671 sect_offset sect_off;
672 unsigned int length;
673
674 /* For types, offset in the type's DIE of the type defined by this TU. */
675 cu_offset type_offset_in_tu;
676 };
677
678 /* include/dwarf2.h defines the DWP section codes.
679 It defines a max value but it doesn't define a min value, which we
680 use for error checking, so provide one. */
681
682 enum dwp_v2_section_ids
683 {
684 DW_SECT_MIN = 1
685 };
686
687 /* Data for one DWO file.
688
689 This includes virtual DWO files (a virtual DWO file is a DWO file as it
690 appears in a DWP file). DWP files don't really have DWO files per se -
691 comdat folding of types "loses" the DWO file they came from, and from
692 a high level view DWP files appear to contain a mass of random types.
693 However, to maintain consistency with the non-DWP case we pretend DWP
694 files contain virtual DWO files, and we assign each TU with one virtual
695 DWO file (generally based on the line and abbrev section offsets -
696 a heuristic that seems to work in practice). */
697
698 struct dwo_file
699 {
700 dwo_file () = default;
701 DISABLE_COPY_AND_ASSIGN (dwo_file);
702
703 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
704 For virtual DWO files the name is constructed from the section offsets
705 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
706 from related CU+TUs. */
707 const char *dwo_name = nullptr;
708
709 /* The DW_AT_comp_dir attribute. */
710 const char *comp_dir = nullptr;
711
712 /* The bfd, when the file is open. Otherwise this is NULL.
713 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
714 gdb_bfd_ref_ptr dbfd;
715
716 /* The sections that make up this DWO file.
717 Remember that for virtual DWO files in DWP V2, these are virtual
718 sections (for lack of a better name). */
719 struct dwo_sections sections {};
720
721 /* The CUs in the file.
722 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
723 an extension to handle LLVM's Link Time Optimization output (where
724 multiple source files may be compiled into a single object/dwo pair). */
725 htab_up cus;
726
727 /* Table of TUs in the file.
728 Each element is a struct dwo_unit. */
729 htab_up tus;
730 };
731
732 /* These sections are what may appear in a DWP file. */
733
734 struct dwp_sections
735 {
736 /* These are used by both DWP version 1 and 2. */
737 struct dwarf2_section_info str;
738 struct dwarf2_section_info cu_index;
739 struct dwarf2_section_info tu_index;
740
741 /* These are only used by DWP version 2 files.
742 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
743 sections are referenced by section number, and are not recorded here.
744 In DWP version 2 there is at most one copy of all these sections, each
745 section being (effectively) comprised of the concatenation of all of the
746 individual sections that exist in the version 1 format.
747 To keep the code simple we treat each of these concatenated pieces as a
748 section itself (a virtual section?). */
749 struct dwarf2_section_info abbrev;
750 struct dwarf2_section_info info;
751 struct dwarf2_section_info line;
752 struct dwarf2_section_info loc;
753 struct dwarf2_section_info macinfo;
754 struct dwarf2_section_info macro;
755 struct dwarf2_section_info str_offsets;
756 struct dwarf2_section_info types;
757 };
758
759 /* These sections are what may appear in a virtual DWO file in DWP version 1.
760 A virtual DWO file is a DWO file as it appears in a DWP file. */
761
762 struct virtual_v1_dwo_sections
763 {
764 struct dwarf2_section_info abbrev;
765 struct dwarf2_section_info line;
766 struct dwarf2_section_info loc;
767 struct dwarf2_section_info macinfo;
768 struct dwarf2_section_info macro;
769 struct dwarf2_section_info str_offsets;
770 /* Each DWP hash table entry records one CU or one TU.
771 That is recorded here, and copied to dwo_unit.section. */
772 struct dwarf2_section_info info_or_types;
773 };
774
775 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
776 In version 2, the sections of the DWO files are concatenated together
777 and stored in one section of that name. Thus each ELF section contains
778 several "virtual" sections. */
779
780 struct virtual_v2_dwo_sections
781 {
782 bfd_size_type abbrev_offset;
783 bfd_size_type abbrev_size;
784
785 bfd_size_type line_offset;
786 bfd_size_type line_size;
787
788 bfd_size_type loc_offset;
789 bfd_size_type loc_size;
790
791 bfd_size_type macinfo_offset;
792 bfd_size_type macinfo_size;
793
794 bfd_size_type macro_offset;
795 bfd_size_type macro_size;
796
797 bfd_size_type str_offsets_offset;
798 bfd_size_type str_offsets_size;
799
800 /* Each DWP hash table entry records one CU or one TU.
801 That is recorded here, and copied to dwo_unit.section. */
802 bfd_size_type info_or_types_offset;
803 bfd_size_type info_or_types_size;
804 };
805
806 /* Contents of DWP hash tables. */
807
808 struct dwp_hash_table
809 {
810 uint32_t version, nr_columns;
811 uint32_t nr_units, nr_slots;
812 const gdb_byte *hash_table, *unit_table;
813 union
814 {
815 struct
816 {
817 const gdb_byte *indices;
818 } v1;
819 struct
820 {
821 /* This is indexed by column number and gives the id of the section
822 in that column. */
823 #define MAX_NR_V2_DWO_SECTIONS \
824 (1 /* .debug_info or .debug_types */ \
825 + 1 /* .debug_abbrev */ \
826 + 1 /* .debug_line */ \
827 + 1 /* .debug_loc */ \
828 + 1 /* .debug_str_offsets */ \
829 + 1 /* .debug_macro or .debug_macinfo */)
830 int section_ids[MAX_NR_V2_DWO_SECTIONS];
831 const gdb_byte *offsets;
832 const gdb_byte *sizes;
833 } v2;
834 } section_pool;
835 };
836
837 /* Data for one DWP file. */
838
839 struct dwp_file
840 {
841 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
842 : name (name_),
843 dbfd (std::move (abfd))
844 {
845 }
846
847 /* Name of the file. */
848 const char *name;
849
850 /* File format version. */
851 int version = 0;
852
853 /* The bfd. */
854 gdb_bfd_ref_ptr dbfd;
855
856 /* Section info for this file. */
857 struct dwp_sections sections {};
858
859 /* Table of CUs in the file. */
860 const struct dwp_hash_table *cus = nullptr;
861
862 /* Table of TUs in the file. */
863 const struct dwp_hash_table *tus = nullptr;
864
865 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
866 htab_up loaded_cus;
867 htab_up loaded_tus;
868
869 /* Table to map ELF section numbers to their sections.
870 This is only needed for the DWP V1 file format. */
871 unsigned int num_sections = 0;
872 asection **elf_sections = nullptr;
873 };
874
875 /* Struct used to pass misc. parameters to read_die_and_children, et
876 al. which are used for both .debug_info and .debug_types dies.
877 All parameters here are unchanging for the life of the call. This
878 struct exists to abstract away the constant parameters of die reading. */
879
880 struct die_reader_specs
881 {
882 /* The bfd of die_section. */
883 bfd* abfd;
884
885 /* The CU of the DIE we are parsing. */
886 struct dwarf2_cu *cu;
887
888 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
889 struct dwo_file *dwo_file;
890
891 /* The section the die comes from.
892 This is either .debug_info or .debug_types, or the .dwo variants. */
893 struct dwarf2_section_info *die_section;
894
895 /* die_section->buffer. */
896 const gdb_byte *buffer;
897
898 /* The end of the buffer. */
899 const gdb_byte *buffer_end;
900
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
903 };
904
905 /* A subclass of die_reader_specs that holds storage and has complex
906 constructor and destructor behavior. */
907
908 class cutu_reader : public die_reader_specs
909 {
910 public:
911
912 cutu_reader (dwarf2_per_cu_data *this_cu,
913 dwarf2_per_objfile *per_objfile,
914 struct abbrev_table *abbrev_table,
915 int use_existing_cu,
916 bool skip_partial);
917
918 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
919 dwarf2_per_objfile *per_objfile,
920 struct dwarf2_cu *parent_cu = nullptr,
921 struct dwo_file *dwo_file = nullptr);
922
923 DISABLE_COPY_AND_ASSIGN (cutu_reader);
924
925 const gdb_byte *info_ptr = nullptr;
926 struct die_info *comp_unit_die = nullptr;
927 bool dummy_p = false;
928
929 /* Release the new CU, putting it on the chain. This cannot be done
930 for dummy CUs. */
931 void keep ();
932
933 private:
934 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
935 dwarf2_per_objfile *per_objfile,
936 int use_existing_cu);
937
938 struct dwarf2_per_cu_data *m_this_cu;
939 std::unique_ptr<dwarf2_cu> m_new_cu;
940
941 /* The ordinary abbreviation table. */
942 abbrev_table_up m_abbrev_table_holder;
943
944 /* The DWO abbreviation table. */
945 abbrev_table_up m_dwo_abbrev_table;
946 };
947
948 /* When we construct a partial symbol table entry we only
949 need this much information. */
950 struct partial_die_info : public allocate_on_obstack
951 {
952 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
953
954 /* Disable assign but still keep copy ctor, which is needed
955 load_partial_dies. */
956 partial_die_info& operator=(const partial_die_info& rhs) = delete;
957
958 /* Adjust the partial die before generating a symbol for it. This
959 function may set the is_external flag or change the DIE's
960 name. */
961 void fixup (struct dwarf2_cu *cu);
962
963 /* Read a minimal amount of information into the minimal die
964 structure. */
965 const gdb_byte *read (const struct die_reader_specs *reader,
966 const struct abbrev_info &abbrev,
967 const gdb_byte *info_ptr);
968
969 /* Offset of this DIE. */
970 const sect_offset sect_off;
971
972 /* DWARF-2 tag for this DIE. */
973 const ENUM_BITFIELD(dwarf_tag) tag : 16;
974
975 /* Assorted flags describing the data found in this DIE. */
976 const unsigned int has_children : 1;
977
978 unsigned int is_external : 1;
979 unsigned int is_declaration : 1;
980 unsigned int has_type : 1;
981 unsigned int has_specification : 1;
982 unsigned int has_pc_info : 1;
983 unsigned int may_be_inlined : 1;
984
985 /* This DIE has been marked DW_AT_main_subprogram. */
986 unsigned int main_subprogram : 1;
987
988 /* Flag set if the SCOPE field of this structure has been
989 computed. */
990 unsigned int scope_set : 1;
991
992 /* Flag set if the DIE has a byte_size attribute. */
993 unsigned int has_byte_size : 1;
994
995 /* Flag set if the DIE has a DW_AT_const_value attribute. */
996 unsigned int has_const_value : 1;
997
998 /* Flag set if any of the DIE's children are template arguments. */
999 unsigned int has_template_arguments : 1;
1000
1001 /* Flag set if fixup has been called on this die. */
1002 unsigned int fixup_called : 1;
1003
1004 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1005 unsigned int is_dwz : 1;
1006
1007 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1008 unsigned int spec_is_dwz : 1;
1009
1010 /* The name of this DIE. Normally the value of DW_AT_name, but
1011 sometimes a default name for unnamed DIEs. */
1012 const char *name = nullptr;
1013
1014 /* The linkage name, if present. */
1015 const char *linkage_name = nullptr;
1016
1017 /* The scope to prepend to our children. This is generally
1018 allocated on the comp_unit_obstack, so will disappear
1019 when this compilation unit leaves the cache. */
1020 const char *scope = nullptr;
1021
1022 /* Some data associated with the partial DIE. The tag determines
1023 which field is live. */
1024 union
1025 {
1026 /* The location description associated with this DIE, if any. */
1027 struct dwarf_block *locdesc;
1028 /* The offset of an import, for DW_TAG_imported_unit. */
1029 sect_offset sect_off;
1030 } d {};
1031
1032 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1033 CORE_ADDR lowpc = 0;
1034 CORE_ADDR highpc = 0;
1035
1036 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1037 DW_AT_sibling, if any. */
1038 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1039 could return DW_AT_sibling values to its caller load_partial_dies. */
1040 const gdb_byte *sibling = nullptr;
1041
1042 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1043 DW_AT_specification (or DW_AT_abstract_origin or
1044 DW_AT_extension). */
1045 sect_offset spec_offset {};
1046
1047 /* Pointers to this DIE's parent, first child, and next sibling,
1048 if any. */
1049 struct partial_die_info *die_parent = nullptr;
1050 struct partial_die_info *die_child = nullptr;
1051 struct partial_die_info *die_sibling = nullptr;
1052
1053 friend struct partial_die_info *
1054 dwarf2_cu::find_partial_die (sect_offset sect_off);
1055
1056 private:
1057 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1058 partial_die_info (sect_offset sect_off)
1059 : partial_die_info (sect_off, DW_TAG_padding, 0)
1060 {
1061 }
1062
1063 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1064 int has_children_)
1065 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1066 {
1067 is_external = 0;
1068 is_declaration = 0;
1069 has_type = 0;
1070 has_specification = 0;
1071 has_pc_info = 0;
1072 may_be_inlined = 0;
1073 main_subprogram = 0;
1074 scope_set = 0;
1075 has_byte_size = 0;
1076 has_const_value = 0;
1077 has_template_arguments = 0;
1078 fixup_called = 0;
1079 is_dwz = 0;
1080 spec_is_dwz = 0;
1081 }
1082 };
1083
1084 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1085 but this would require a corresponding change in unpack_field_as_long
1086 and friends. */
1087 static int bits_per_byte = 8;
1088
1089 struct variant_part_builder;
1090
1091 /* When reading a variant, we track a bit more information about the
1092 field, and store it in an object of this type. */
1093
1094 struct variant_field
1095 {
1096 int first_field = -1;
1097 int last_field = -1;
1098
1099 /* A variant can contain other variant parts. */
1100 std::vector<variant_part_builder> variant_parts;
1101
1102 /* If we see a DW_TAG_variant, then this will be set if this is the
1103 default branch. */
1104 bool default_branch = false;
1105 /* If we see a DW_AT_discr_value, then this will be the discriminant
1106 value. */
1107 ULONGEST discriminant_value = 0;
1108 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1109 data. */
1110 struct dwarf_block *discr_list_data = nullptr;
1111 };
1112
1113 /* This represents a DW_TAG_variant_part. */
1114
1115 struct variant_part_builder
1116 {
1117 /* The offset of the discriminant field. */
1118 sect_offset discriminant_offset {};
1119
1120 /* Variants that are direct children of this variant part. */
1121 std::vector<variant_field> variants;
1122
1123 /* True if we're currently reading a variant. */
1124 bool processing_variant = false;
1125 };
1126
1127 struct nextfield
1128 {
1129 int accessibility = 0;
1130 int virtuality = 0;
1131 /* Variant parts need to find the discriminant, which is a DIE
1132 reference. We track the section offset of each field to make
1133 this link. */
1134 sect_offset offset;
1135 struct field field {};
1136 };
1137
1138 struct fnfieldlist
1139 {
1140 const char *name = nullptr;
1141 std::vector<struct fn_field> fnfields;
1142 };
1143
1144 /* The routines that read and process dies for a C struct or C++ class
1145 pass lists of data member fields and lists of member function fields
1146 in an instance of a field_info structure, as defined below. */
1147 struct field_info
1148 {
1149 /* List of data member and baseclasses fields. */
1150 std::vector<struct nextfield> fields;
1151 std::vector<struct nextfield> baseclasses;
1152
1153 /* Set if the accessibility of one of the fields is not public. */
1154 int non_public_fields = 0;
1155
1156 /* Member function fieldlist array, contains name of possibly overloaded
1157 member function, number of overloaded member functions and a pointer
1158 to the head of the member function field chain. */
1159 std::vector<struct fnfieldlist> fnfieldlists;
1160
1161 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1162 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1163 std::vector<struct decl_field> typedef_field_list;
1164
1165 /* Nested types defined by this class and the number of elements in this
1166 list. */
1167 std::vector<struct decl_field> nested_types_list;
1168
1169 /* If non-null, this is the variant part we are currently
1170 reading. */
1171 variant_part_builder *current_variant_part = nullptr;
1172 /* This holds all the top-level variant parts attached to the type
1173 we're reading. */
1174 std::vector<variant_part_builder> variant_parts;
1175
1176 /* Return the total number of fields (including baseclasses). */
1177 int nfields () const
1178 {
1179 return fields.size () + baseclasses.size ();
1180 }
1181 };
1182
1183 /* Loaded secondary compilation units are kept in memory until they
1184 have not been referenced for the processing of this many
1185 compilation units. Set this to zero to disable caching. Cache
1186 sizes of up to at least twenty will improve startup time for
1187 typical inter-CU-reference binaries, at an obvious memory cost. */
1188 static int dwarf_max_cache_age = 5;
1189 static void
1190 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1191 struct cmd_list_element *c, const char *value)
1192 {
1193 fprintf_filtered (file, _("The upper bound on the age of cached "
1194 "DWARF compilation units is %s.\n"),
1195 value);
1196 }
1197 \f
1198 /* local function prototypes */
1199
1200 static void dwarf2_find_base_address (struct die_info *die,
1201 struct dwarf2_cu *cu);
1202
1203 static dwarf2_psymtab *create_partial_symtab
1204 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1205 const char *name);
1206
1207 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1208 const gdb_byte *info_ptr,
1209 struct die_info *type_unit_die);
1210
1211 static void dwarf2_build_psymtabs_hard
1212 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1213
1214 static void scan_partial_symbols (struct partial_die_info *,
1215 CORE_ADDR *, CORE_ADDR *,
1216 int, struct dwarf2_cu *);
1217
1218 static void add_partial_symbol (struct partial_die_info *,
1219 struct dwarf2_cu *);
1220
1221 static void add_partial_namespace (struct partial_die_info *pdi,
1222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1223 int set_addrmap, struct dwarf2_cu *cu);
1224
1225 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1226 CORE_ADDR *highpc, int set_addrmap,
1227 struct dwarf2_cu *cu);
1228
1229 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1230 struct dwarf2_cu *cu);
1231
1232 static void add_partial_subprogram (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1234 int need_pc, struct dwarf2_cu *cu);
1235
1236 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1237
1238 static struct partial_die_info *load_partial_dies
1239 (const struct die_reader_specs *, const gdb_byte *, int);
1240
1241 /* A pair of partial_die_info and compilation unit. */
1242 struct cu_partial_die_info
1243 {
1244 /* The compilation unit of the partial_die_info. */
1245 struct dwarf2_cu *cu;
1246 /* A partial_die_info. */
1247 struct partial_die_info *pdi;
1248
1249 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1250 : cu (cu),
1251 pdi (pdi)
1252 { /* Nothing. */ }
1253
1254 private:
1255 cu_partial_die_info () = delete;
1256 };
1257
1258 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1259 struct dwarf2_cu *);
1260
1261 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1262 struct attribute *, struct attr_abbrev *,
1263 const gdb_byte *, bool *need_reprocess);
1264
1265 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1266 struct attribute *attr);
1267
1268 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1269
1270 static sect_offset read_abbrev_offset
1271 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1272 struct dwarf2_section_info *, sect_offset);
1273
1274 static const char *read_indirect_string
1275 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1276 const struct comp_unit_head *, unsigned int *);
1277
1278 static const char *read_indirect_string_at_offset
1279 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1280
1281 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1282 const gdb_byte *,
1283 unsigned int *);
1284
1285 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1286 ULONGEST str_index);
1287
1288 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1289 ULONGEST str_index);
1290
1291 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1292
1293 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1294 struct dwarf2_cu *);
1295
1296 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1297 struct dwarf2_cu *cu);
1298
1299 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1300
1301 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1302 struct dwarf2_cu *cu);
1303
1304 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1305
1306 static struct die_info *die_specification (struct die_info *die,
1307 struct dwarf2_cu **);
1308
1309 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1310 struct dwarf2_cu *cu);
1311
1312 static void dwarf_decode_lines (struct line_header *, const char *,
1313 struct dwarf2_cu *, dwarf2_psymtab *,
1314 CORE_ADDR, int decode_mapping);
1315
1316 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1317 const char *);
1318
1319 static struct symbol *new_symbol (struct die_info *, struct type *,
1320 struct dwarf2_cu *, struct symbol * = NULL);
1321
1322 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1323 struct dwarf2_cu *);
1324
1325 static void dwarf2_const_value_attr (const struct attribute *attr,
1326 struct type *type,
1327 const char *name,
1328 struct obstack *obstack,
1329 struct dwarf2_cu *cu, LONGEST *value,
1330 const gdb_byte **bytes,
1331 struct dwarf2_locexpr_baton **baton);
1332
1333 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1334
1335 static int need_gnat_info (struct dwarf2_cu *);
1336
1337 static struct type *die_descriptive_type (struct die_info *,
1338 struct dwarf2_cu *);
1339
1340 static void set_descriptive_type (struct type *, struct die_info *,
1341 struct dwarf2_cu *);
1342
1343 static struct type *die_containing_type (struct die_info *,
1344 struct dwarf2_cu *);
1345
1346 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1347 struct dwarf2_cu *);
1348
1349 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1350
1351 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1352
1353 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1354
1355 static char *typename_concat (struct obstack *obs, const char *prefix,
1356 const char *suffix, int physname,
1357 struct dwarf2_cu *cu);
1358
1359 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1360
1361 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1362
1363 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1364
1365 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1366
1367 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1368
1369 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1370
1371 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1372 struct dwarf2_cu *, dwarf2_psymtab *);
1373
1374 /* Return the .debug_loclists section to use for cu. */
1375 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1376
1377 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1378 values. Keep the items ordered with increasing constraints compliance. */
1379 enum pc_bounds_kind
1380 {
1381 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1382 PC_BOUNDS_NOT_PRESENT,
1383
1384 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1385 were present but they do not form a valid range of PC addresses. */
1386 PC_BOUNDS_INVALID,
1387
1388 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1389 PC_BOUNDS_RANGES,
1390
1391 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1392 PC_BOUNDS_HIGH_LOW,
1393 };
1394
1395 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *,
1398 dwarf2_psymtab *);
1399
1400 static void get_scope_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *);
1403
1404 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1405 CORE_ADDR, struct dwarf2_cu *);
1406
1407 static void dwarf2_add_field (struct field_info *, struct die_info *,
1408 struct dwarf2_cu *);
1409
1410 static void dwarf2_attach_fields_to_type (struct field_info *,
1411 struct type *, struct dwarf2_cu *);
1412
1413 static void dwarf2_add_member_fn (struct field_info *,
1414 struct die_info *, struct type *,
1415 struct dwarf2_cu *);
1416
1417 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1418 struct type *,
1419 struct dwarf2_cu *);
1420
1421 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1422
1423 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1424
1425 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1426
1427 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1428
1429 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1430
1431 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1432
1433 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1434
1435 static struct type *read_module_type (struct die_info *die,
1436 struct dwarf2_cu *cu);
1437
1438 static const char *namespace_name (struct die_info *die,
1439 int *is_anonymous, struct dwarf2_cu *);
1440
1441 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1442
1443 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1444 bool * = nullptr);
1445
1446 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1447 struct dwarf2_cu *);
1448
1449 static struct die_info *read_die_and_siblings_1
1450 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1451 struct die_info *);
1452
1453 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1454 const gdb_byte *info_ptr,
1455 const gdb_byte **new_info_ptr,
1456 struct die_info *parent);
1457
1458 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1459 struct die_info **, const gdb_byte *,
1460 int);
1461
1462 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1463 struct die_info **, const gdb_byte *);
1464
1465 static void process_die (struct die_info *, struct dwarf2_cu *);
1466
1467 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1468 struct objfile *);
1469
1470 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1471
1472 static const char *dwarf2_full_name (const char *name,
1473 struct die_info *die,
1474 struct dwarf2_cu *cu);
1475
1476 static const char *dwarf2_physname (const char *name, struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
1479 static struct die_info *dwarf2_extension (struct die_info *die,
1480 struct dwarf2_cu **);
1481
1482 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1483
1484 static void dump_die_for_error (struct die_info *);
1485
1486 static void dump_die_1 (struct ui_file *, int level, int max_level,
1487 struct die_info *);
1488
1489 /*static*/ void dump_die (struct die_info *, int max_level);
1490
1491 static void store_in_ref_table (struct die_info *,
1492 struct dwarf2_cu *);
1493
1494 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1495 const struct attribute *,
1496 struct dwarf2_cu **);
1497
1498 static struct die_info *follow_die_ref (struct die_info *,
1499 const struct attribute *,
1500 struct dwarf2_cu **);
1501
1502 static struct die_info *follow_die_sig (struct die_info *,
1503 const struct attribute *,
1504 struct dwarf2_cu **);
1505
1506 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509 static struct type *get_DW_AT_signature_type (struct die_info *,
1510 const struct attribute *,
1511 struct dwarf2_cu *);
1512
1513 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1514 dwarf2_per_objfile *per_objfile);
1515
1516 static void read_signatured_type (signatured_type *sig_type,
1517 dwarf2_per_objfile *per_objfile);
1518
1519 static int attr_to_dynamic_prop (const struct attribute *attr,
1520 struct die_info *die, struct dwarf2_cu *cu,
1521 struct dynamic_prop *prop, struct type *type);
1522
1523 /* memory allocation interface */
1524
1525 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1526
1527 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1528
1529 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1530
1531 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1532 struct dwarf2_loclist_baton *baton,
1533 const struct attribute *attr);
1534
1535 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1536 struct symbol *sym,
1537 struct dwarf2_cu *cu,
1538 int is_block);
1539
1540 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1541 const gdb_byte *info_ptr,
1542 struct abbrev_info *abbrev);
1543
1544 static hashval_t partial_die_hash (const void *item);
1545
1546 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1547
1548 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1549 (sect_offset sect_off, unsigned int offset_in_dwz,
1550 struct dwarf2_per_objfile *dwarf2_per_objfile);
1551
1552 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1553 struct die_info *comp_unit_die,
1554 enum language pretend_language);
1555
1556 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1557
1558 static void free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
1559 dwarf2_per_objfile *per_objfile);
1560
1561 static struct type *set_die_type (struct die_info *, struct type *,
1562 struct dwarf2_cu *);
1563
1564 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1565
1566 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1567
1568 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1569 dwarf2_per_objfile *per_objfile,
1570 bool skip_partial,
1571 enum language pretend_language);
1572
1573 static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 enum language pretend_language);
1576
1577 static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1578 dwarf2_per_objfile *per_objfile,
1579 enum language pretend_language);
1580
1581 static void dwarf2_add_dependence (struct dwarf2_cu *,
1582 struct dwarf2_per_cu_data *);
1583
1584 static void dwarf2_mark (struct dwarf2_cu *);
1585
1586 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1587
1588 static struct type *get_die_type_at_offset (sect_offset,
1589 dwarf2_per_cu_data *per_cu,
1590 dwarf2_per_objfile *per_objfile);
1591
1592 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1593
1594 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1595 dwarf2_per_objfile *per_objfile,
1596 enum language pretend_language);
1597
1598 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1599
1600 /* Class, the destructor of which frees all allocated queue entries. This
1601 will only have work to do if an error was thrown while processing the
1602 dwarf. If no error was thrown then the queue entries should have all
1603 been processed, and freed, as we went along. */
1604
1605 class dwarf2_queue_guard
1606 {
1607 public:
1608 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1609 : m_per_objfile (per_objfile)
1610 {
1611 }
1612
1613 /* Free any entries remaining on the queue. There should only be
1614 entries left if we hit an error while processing the dwarf. */
1615 ~dwarf2_queue_guard ()
1616 {
1617 /* Ensure that no memory is allocated by the queue. */
1618 std::queue<dwarf2_queue_item> empty;
1619 std::swap (m_per_objfile->per_bfd->queue, empty);
1620 }
1621
1622 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1623
1624 private:
1625 dwarf2_per_objfile *m_per_objfile;
1626 };
1627
1628 dwarf2_queue_item::~dwarf2_queue_item ()
1629 {
1630 /* Anything still marked queued is likely to be in an
1631 inconsistent state, so discard it. */
1632 if (per_cu->queued)
1633 {
1634 if (per_cu->cu != NULL)
1635 free_one_cached_comp_unit (per_cu, per_objfile);
1636 per_cu->queued = 0;
1637 }
1638 }
1639
1640 /* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643 struct file_and_directory
1644 {
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657 };
1658
1659 static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
1661
1662 static htab_up allocate_signatured_type_table ();
1663
1664 static htab_up allocate_dwo_unit_table ();
1665
1666 static struct dwo_unit *lookup_dwo_unit_in_dwp
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1668 struct dwp_file *dwp_file, const char *comp_dir,
1669 ULONGEST signature, int is_debug_types);
1670
1671 static struct dwp_file *get_dwp_file
1672 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1673
1674 static struct dwo_unit *lookup_dwo_comp_unit
1675 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1676 ULONGEST signature);
1677
1678 static struct dwo_unit *lookup_dwo_type_unit
1679 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1680
1681 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1682
1683 /* A unique pointer to a dwo_file. */
1684
1685 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1686
1687 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1688
1689 static void check_producer (struct dwarf2_cu *cu);
1690
1691 static void free_line_header_voidp (void *arg);
1692 \f
1693 /* Various complaints about symbol reading that don't abort the process. */
1694
1695 static void
1696 dwarf2_debug_line_missing_file_complaint (void)
1697 {
1698 complaint (_(".debug_line section has line data without a file"));
1699 }
1700
1701 static void
1702 dwarf2_debug_line_missing_end_sequence_complaint (void)
1703 {
1704 complaint (_(".debug_line section has line "
1705 "program sequence without an end"));
1706 }
1707
1708 static void
1709 dwarf2_complex_location_expr_complaint (void)
1710 {
1711 complaint (_("location expression too complex"));
1712 }
1713
1714 static void
1715 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1716 int arg3)
1717 {
1718 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1719 arg1, arg2, arg3);
1720 }
1721
1722 static void
1723 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1724 {
1725 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1726 arg1, arg2);
1727 }
1728
1729 /* Hash function for line_header_hash. */
1730
1731 static hashval_t
1732 line_header_hash (const struct line_header *ofs)
1733 {
1734 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1735 }
1736
1737 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1738
1739 static hashval_t
1740 line_header_hash_voidp (const void *item)
1741 {
1742 const struct line_header *ofs = (const struct line_header *) item;
1743
1744 return line_header_hash (ofs);
1745 }
1746
1747 /* Equality function for line_header_hash. */
1748
1749 static int
1750 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1751 {
1752 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1753 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1754
1755 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1756 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1757 }
1758
1759 \f
1760
1761 /* See declaration. */
1762
1763 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1764 bool can_copy_)
1765 : obfd (obfd),
1766 can_copy (can_copy_)
1767 {
1768 if (names == NULL)
1769 names = &dwarf2_elf_names;
1770
1771 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1772 locate_sections (obfd, sec, *names);
1773 }
1774
1775 dwarf2_per_bfd::~dwarf2_per_bfd ()
1776 {
1777 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1778 free_cached_comp_units ();
1779
1780 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1781 per_cu->imported_symtabs_free ();
1782
1783 for (signatured_type *sig_type : all_type_units)
1784 sig_type->per_cu.imported_symtabs_free ();
1785
1786 /* Everything else should be on this->obstack. */
1787 }
1788
1789 /* See declaration. */
1790
1791 void
1792 dwarf2_per_bfd::free_cached_comp_units ()
1793 {
1794 dwarf2_per_cu_data *per_cu = read_in_chain;
1795 dwarf2_per_cu_data **last_chain = &read_in_chain;
1796 while (per_cu != NULL)
1797 {
1798 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1799
1800 delete per_cu->cu;
1801 *last_chain = next_cu;
1802 per_cu = next_cu;
1803 }
1804 }
1805
1806 /* A helper class that calls free_cached_comp_units on
1807 destruction. */
1808
1809 class free_cached_comp_units
1810 {
1811 public:
1812
1813 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1814 : m_per_objfile (per_objfile)
1815 {
1816 }
1817
1818 ~free_cached_comp_units ()
1819 {
1820 m_per_objfile->per_bfd->free_cached_comp_units ();
1821 }
1822
1823 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1824
1825 private:
1826
1827 dwarf2_per_objfile *m_per_objfile;
1828 };
1829
1830 /* See read.h. */
1831
1832 bool
1833 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1834 {
1835 gdb_assert (per_cu->index < this->m_symtabs.size ());
1836
1837 return this->m_symtabs[per_cu->index] != nullptr;
1838 }
1839
1840 /* See read.h. */
1841
1842 compunit_symtab *
1843 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1844 {
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846
1847 return this->m_symtabs[per_cu->index];
1848 }
1849
1850 /* See read.h. */
1851
1852 void
1853 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1854 compunit_symtab *symtab)
1855 {
1856 gdb_assert (per_cu->index < this->m_symtabs.size ());
1857 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1858
1859 this->m_symtabs[per_cu->index] = symtab;
1860 }
1861
1862 /* Try to locate the sections we need for DWARF 2 debugging
1863 information and return true if we have enough to do something.
1864 NAMES points to the dwarf2 section names, or is NULL if the standard
1865 ELF names are used. CAN_COPY is true for formats where symbol
1866 interposition is possible and so symbol values must follow copy
1867 relocation rules. */
1868
1869 int
1870 dwarf2_has_info (struct objfile *objfile,
1871 const struct dwarf2_debug_sections *names,
1872 bool can_copy)
1873 {
1874 if (objfile->flags & OBJF_READNEVER)
1875 return 0;
1876
1877 struct dwarf2_per_objfile *dwarf2_per_objfile
1878 = get_dwarf2_per_objfile (objfile);
1879
1880 if (dwarf2_per_objfile == NULL)
1881 {
1882 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1883 sharing yet). */
1884 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885
1886 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1887 }
1888
1889 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1890 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1891 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1892 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1893 }
1894
1895 /* When loading sections, we look either for uncompressed section or for
1896 compressed section names. */
1897
1898 static int
1899 section_is_p (const char *section_name,
1900 const struct dwarf2_section_names *names)
1901 {
1902 if (names->normal != NULL
1903 && strcmp (section_name, names->normal) == 0)
1904 return 1;
1905 if (names->compressed != NULL
1906 && strcmp (section_name, names->compressed) == 0)
1907 return 1;
1908 return 0;
1909 }
1910
1911 /* See declaration. */
1912
1913 void
1914 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1915 const dwarf2_debug_sections &names)
1916 {
1917 flagword aflag = bfd_section_flags (sectp);
1918
1919 if ((aflag & SEC_HAS_CONTENTS) == 0)
1920 {
1921 }
1922 else if (elf_section_data (sectp)->this_hdr.sh_size
1923 > bfd_get_file_size (abfd))
1924 {
1925 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1926 warning (_("Discarding section %s which has a section size (%s"
1927 ") larger than the file size [in module %s]"),
1928 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1929 bfd_get_filename (abfd));
1930 }
1931 else if (section_is_p (sectp->name, &names.info))
1932 {
1933 this->info.s.section = sectp;
1934 this->info.size = bfd_section_size (sectp);
1935 }
1936 else if (section_is_p (sectp->name, &names.abbrev))
1937 {
1938 this->abbrev.s.section = sectp;
1939 this->abbrev.size = bfd_section_size (sectp);
1940 }
1941 else if (section_is_p (sectp->name, &names.line))
1942 {
1943 this->line.s.section = sectp;
1944 this->line.size = bfd_section_size (sectp);
1945 }
1946 else if (section_is_p (sectp->name, &names.loc))
1947 {
1948 this->loc.s.section = sectp;
1949 this->loc.size = bfd_section_size (sectp);
1950 }
1951 else if (section_is_p (sectp->name, &names.loclists))
1952 {
1953 this->loclists.s.section = sectp;
1954 this->loclists.size = bfd_section_size (sectp);
1955 }
1956 else if (section_is_p (sectp->name, &names.macinfo))
1957 {
1958 this->macinfo.s.section = sectp;
1959 this->macinfo.size = bfd_section_size (sectp);
1960 }
1961 else if (section_is_p (sectp->name, &names.macro))
1962 {
1963 this->macro.s.section = sectp;
1964 this->macro.size = bfd_section_size (sectp);
1965 }
1966 else if (section_is_p (sectp->name, &names.str))
1967 {
1968 this->str.s.section = sectp;
1969 this->str.size = bfd_section_size (sectp);
1970 }
1971 else if (section_is_p (sectp->name, &names.str_offsets))
1972 {
1973 this->str_offsets.s.section = sectp;
1974 this->str_offsets.size = bfd_section_size (sectp);
1975 }
1976 else if (section_is_p (sectp->name, &names.line_str))
1977 {
1978 this->line_str.s.section = sectp;
1979 this->line_str.size = bfd_section_size (sectp);
1980 }
1981 else if (section_is_p (sectp->name, &names.addr))
1982 {
1983 this->addr.s.section = sectp;
1984 this->addr.size = bfd_section_size (sectp);
1985 }
1986 else if (section_is_p (sectp->name, &names.frame))
1987 {
1988 this->frame.s.section = sectp;
1989 this->frame.size = bfd_section_size (sectp);
1990 }
1991 else if (section_is_p (sectp->name, &names.eh_frame))
1992 {
1993 this->eh_frame.s.section = sectp;
1994 this->eh_frame.size = bfd_section_size (sectp);
1995 }
1996 else if (section_is_p (sectp->name, &names.ranges))
1997 {
1998 this->ranges.s.section = sectp;
1999 this->ranges.size = bfd_section_size (sectp);
2000 }
2001 else if (section_is_p (sectp->name, &names.rnglists))
2002 {
2003 this->rnglists.s.section = sectp;
2004 this->rnglists.size = bfd_section_size (sectp);
2005 }
2006 else if (section_is_p (sectp->name, &names.types))
2007 {
2008 struct dwarf2_section_info type_section;
2009
2010 memset (&type_section, 0, sizeof (type_section));
2011 type_section.s.section = sectp;
2012 type_section.size = bfd_section_size (sectp);
2013
2014 this->types.push_back (type_section);
2015 }
2016 else if (section_is_p (sectp->name, &names.gdb_index))
2017 {
2018 this->gdb_index.s.section = sectp;
2019 this->gdb_index.size = bfd_section_size (sectp);
2020 }
2021 else if (section_is_p (sectp->name, &names.debug_names))
2022 {
2023 this->debug_names.s.section = sectp;
2024 this->debug_names.size = bfd_section_size (sectp);
2025 }
2026 else if (section_is_p (sectp->name, &names.debug_aranges))
2027 {
2028 this->debug_aranges.s.section = sectp;
2029 this->debug_aranges.size = bfd_section_size (sectp);
2030 }
2031
2032 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2033 && bfd_section_vma (sectp) == 0)
2034 this->has_section_at_zero = true;
2035 }
2036
2037 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2038 SECTION_NAME. */
2039
2040 void
2041 dwarf2_get_section_info (struct objfile *objfile,
2042 enum dwarf2_section_enum sect,
2043 asection **sectp, const gdb_byte **bufp,
2044 bfd_size_type *sizep)
2045 {
2046 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2047 struct dwarf2_section_info *info;
2048
2049 /* We may see an objfile without any DWARF, in which case we just
2050 return nothing. */
2051 if (data == NULL)
2052 {
2053 *sectp = NULL;
2054 *bufp = NULL;
2055 *sizep = 0;
2056 return;
2057 }
2058 switch (sect)
2059 {
2060 case DWARF2_DEBUG_FRAME:
2061 info = &data->per_bfd->frame;
2062 break;
2063 case DWARF2_EH_FRAME:
2064 info = &data->per_bfd->eh_frame;
2065 break;
2066 default:
2067 gdb_assert_not_reached ("unexpected section");
2068 }
2069
2070 info->read (objfile);
2071
2072 *sectp = info->get_bfd_section ();
2073 *bufp = info->buffer;
2074 *sizep = info->size;
2075 }
2076
2077 /* A helper function to find the sections for a .dwz file. */
2078
2079 static void
2080 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2081 {
2082 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2083
2084 /* Note that we only support the standard ELF names, because .dwz
2085 is ELF-only (at the time of writing). */
2086 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2087 {
2088 dwz_file->abbrev.s.section = sectp;
2089 dwz_file->abbrev.size = bfd_section_size (sectp);
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2092 {
2093 dwz_file->info.s.section = sectp;
2094 dwz_file->info.size = bfd_section_size (sectp);
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2097 {
2098 dwz_file->str.s.section = sectp;
2099 dwz_file->str.size = bfd_section_size (sectp);
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2102 {
2103 dwz_file->line.s.section = sectp;
2104 dwz_file->line.size = bfd_section_size (sectp);
2105 }
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2107 {
2108 dwz_file->macro.s.section = sectp;
2109 dwz_file->macro.size = bfd_section_size (sectp);
2110 }
2111 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2112 {
2113 dwz_file->gdb_index.s.section = sectp;
2114 dwz_file->gdb_index.size = bfd_section_size (sectp);
2115 }
2116 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2117 {
2118 dwz_file->debug_names.s.section = sectp;
2119 dwz_file->debug_names.size = bfd_section_size (sectp);
2120 }
2121 }
2122
2123 /* See dwarf2read.h. */
2124
2125 struct dwz_file *
2126 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2127 {
2128 const char *filename;
2129 bfd_size_type buildid_len_arg;
2130 size_t buildid_len;
2131 bfd_byte *buildid;
2132
2133 if (per_bfd->dwz_file != NULL)
2134 return per_bfd->dwz_file.get ();
2135
2136 bfd_set_error (bfd_error_no_error);
2137 gdb::unique_xmalloc_ptr<char> data
2138 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2139 &buildid_len_arg, &buildid));
2140 if (data == NULL)
2141 {
2142 if (bfd_get_error () == bfd_error_no_error)
2143 return NULL;
2144 error (_("could not read '.gnu_debugaltlink' section: %s"),
2145 bfd_errmsg (bfd_get_error ()));
2146 }
2147
2148 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2149
2150 buildid_len = (size_t) buildid_len_arg;
2151
2152 filename = data.get ();
2153
2154 std::string abs_storage;
2155 if (!IS_ABSOLUTE_PATH (filename))
2156 {
2157 gdb::unique_xmalloc_ptr<char> abs
2158 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2159
2160 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2161 filename = abs_storage.c_str ();
2162 }
2163
2164 /* First try the file name given in the section. If that doesn't
2165 work, try to use the build-id instead. */
2166 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2167 if (dwz_bfd != NULL)
2168 {
2169 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2170 dwz_bfd.reset (nullptr);
2171 }
2172
2173 if (dwz_bfd == NULL)
2174 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2175
2176 if (dwz_bfd == nullptr)
2177 {
2178 gdb::unique_xmalloc_ptr<char> alt_filename;
2179 const char *origname = bfd_get_filename (per_bfd->obfd);
2180
2181 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2182 buildid_len,
2183 origname,
2184 &alt_filename));
2185
2186 if (fd.get () >= 0)
2187 {
2188 /* File successfully retrieved from server. */
2189 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2190
2191 if (dwz_bfd == nullptr)
2192 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2193 alt_filename.get ());
2194 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2195 dwz_bfd.reset (nullptr);
2196 }
2197 }
2198
2199 if (dwz_bfd == NULL)
2200 error (_("could not find '.gnu_debugaltlink' file for %s"),
2201 bfd_get_filename (per_bfd->obfd));
2202
2203 std::unique_ptr<struct dwz_file> result
2204 (new struct dwz_file (std::move (dwz_bfd)));
2205
2206 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2207 result.get ());
2208
2209 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2210 per_bfd->dwz_file = std::move (result);
2211 return per_bfd->dwz_file.get ();
2212 }
2213 \f
2214 /* DWARF quick_symbols_functions support. */
2215
2216 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2217 unique line tables, so we maintain a separate table of all .debug_line
2218 derived entries to support the sharing.
2219 All the quick functions need is the list of file names. We discard the
2220 line_header when we're done and don't need to record it here. */
2221 struct quick_file_names
2222 {
2223 /* The data used to construct the hash key. */
2224 struct stmt_list_hash hash;
2225
2226 /* The number of entries in file_names, real_names. */
2227 unsigned int num_file_names;
2228
2229 /* The file names from the line table, after being run through
2230 file_full_name. */
2231 const char **file_names;
2232
2233 /* The file names from the line table after being run through
2234 gdb_realpath. These are computed lazily. */
2235 const char **real_names;
2236 };
2237
2238 /* When using the index (and thus not using psymtabs), each CU has an
2239 object of this type. This is used to hold information needed by
2240 the various "quick" methods. */
2241 struct dwarf2_per_cu_quick_data
2242 {
2243 /* The file table. This can be NULL if there was no file table
2244 or it's currently not read in.
2245 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2246 struct quick_file_names *file_names;
2247
2248 /* A temporary mark bit used when iterating over all CUs in
2249 expand_symtabs_matching. */
2250 unsigned int mark : 1;
2251
2252 /* True if we've tried to read the file table and found there isn't one.
2253 There will be no point in trying to read it again next time. */
2254 unsigned int no_file_data : 1;
2255 };
2256
2257 /* Utility hash function for a stmt_list_hash. */
2258
2259 static hashval_t
2260 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2261 {
2262 hashval_t v = 0;
2263
2264 if (stmt_list_hash->dwo_unit != NULL)
2265 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2266 v += to_underlying (stmt_list_hash->line_sect_off);
2267 return v;
2268 }
2269
2270 /* Utility equality function for a stmt_list_hash. */
2271
2272 static int
2273 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2274 const struct stmt_list_hash *rhs)
2275 {
2276 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2277 return 0;
2278 if (lhs->dwo_unit != NULL
2279 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2280 return 0;
2281
2282 return lhs->line_sect_off == rhs->line_sect_off;
2283 }
2284
2285 /* Hash function for a quick_file_names. */
2286
2287 static hashval_t
2288 hash_file_name_entry (const void *e)
2289 {
2290 const struct quick_file_names *file_data
2291 = (const struct quick_file_names *) e;
2292
2293 return hash_stmt_list_entry (&file_data->hash);
2294 }
2295
2296 /* Equality function for a quick_file_names. */
2297
2298 static int
2299 eq_file_name_entry (const void *a, const void *b)
2300 {
2301 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2302 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2303
2304 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2305 }
2306
2307 /* Delete function for a quick_file_names. */
2308
2309 static void
2310 delete_file_name_entry (void *e)
2311 {
2312 struct quick_file_names *file_data = (struct quick_file_names *) e;
2313 int i;
2314
2315 for (i = 0; i < file_data->num_file_names; ++i)
2316 {
2317 xfree ((void*) file_data->file_names[i]);
2318 if (file_data->real_names)
2319 xfree ((void*) file_data->real_names[i]);
2320 }
2321
2322 /* The space for the struct itself lives on the obstack, so we don't
2323 free it here. */
2324 }
2325
2326 /* Create a quick_file_names hash table. */
2327
2328 static htab_up
2329 create_quick_file_names_table (unsigned int nr_initial_entries)
2330 {
2331 return htab_up (htab_create_alloc (nr_initial_entries,
2332 hash_file_name_entry, eq_file_name_entry,
2333 delete_file_name_entry, xcalloc, xfree));
2334 }
2335
2336 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2337 function is unrelated to symtabs, symtab would have to be created afterwards.
2338 You should call age_cached_comp_units after processing the CU. */
2339
2340 static void
2341 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2342 bool skip_partial)
2343 {
2344 if (per_cu->is_debug_types)
2345 load_full_type_unit (per_cu, per_objfile);
2346 else
2347 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2348
2349 if (per_cu->cu == NULL)
2350 return; /* Dummy CU. */
2351
2352 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2353 }
2354
2355 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2356
2357 static void
2358 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2359 dwarf2_per_objfile *dwarf2_per_objfile,
2360 bool skip_partial)
2361 {
2362 /* Skip type_unit_groups, reading the type units they contain
2363 is handled elsewhere. */
2364 if (per_cu->type_unit_group_p ())
2365 return;
2366
2367 /* The destructor of dwarf2_queue_guard frees any entries left on
2368 the queue. After this point we're guaranteed to leave this function
2369 with the dwarf queue empty. */
2370 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2371
2372 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2373 {
2374 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
2375 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2376
2377 /* If we just loaded a CU from a DWO, and we're working with an index
2378 that may badly handle TUs, load all the TUs in that DWO as well.
2379 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2380 if (!per_cu->is_debug_types
2381 && per_cu->cu != NULL
2382 && per_cu->cu->dwo_unit != NULL
2383 && dwarf2_per_objfile->per_bfd->index_table != NULL
2384 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2385 /* DWP files aren't supported yet. */
2386 && get_dwp_file (dwarf2_per_objfile) == NULL)
2387 queue_and_load_all_dwo_tus (per_cu);
2388 }
2389
2390 process_queue (dwarf2_per_objfile);
2391
2392 /* Age the cache, releasing compilation units that have not
2393 been used recently. */
2394 age_cached_comp_units (dwarf2_per_objfile);
2395 }
2396
2397 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2398 the per-objfile for which this symtab is instantiated.
2399
2400 Returns the resulting symbol table. */
2401
2402 static struct compunit_symtab *
2403 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2404 dwarf2_per_objfile *dwarf2_per_objfile,
2405 bool skip_partial)
2406 {
2407 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2408
2409 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2410 {
2411 free_cached_comp_units freer (dwarf2_per_objfile);
2412 scoped_restore decrementer = increment_reading_symtab ();
2413 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2414 process_cu_includes (dwarf2_per_objfile);
2415 }
2416
2417 return dwarf2_per_objfile->get_symtab (per_cu);
2418 }
2419
2420 /* See declaration. */
2421
2422 dwarf2_per_cu_data *
2423 dwarf2_per_bfd::get_cutu (int index)
2424 {
2425 if (index >= this->all_comp_units.size ())
2426 {
2427 index -= this->all_comp_units.size ();
2428 gdb_assert (index < this->all_type_units.size ());
2429 return &this->all_type_units[index]->per_cu;
2430 }
2431
2432 return this->all_comp_units[index];
2433 }
2434
2435 /* See declaration. */
2436
2437 dwarf2_per_cu_data *
2438 dwarf2_per_bfd::get_cu (int index)
2439 {
2440 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2441
2442 return this->all_comp_units[index];
2443 }
2444
2445 /* See declaration. */
2446
2447 signatured_type *
2448 dwarf2_per_bfd::get_tu (int index)
2449 {
2450 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2451
2452 return this->all_type_units[index];
2453 }
2454
2455 /* See read.h. */
2456
2457 dwarf2_per_cu_data *
2458 dwarf2_per_bfd::allocate_per_cu ()
2459 {
2460 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2461 result->per_bfd = this;
2462 result->index = m_num_psymtabs++;
2463 return result;
2464 }
2465
2466 /* See read.h. */
2467
2468 signatured_type *
2469 dwarf2_per_bfd::allocate_signatured_type ()
2470 {
2471 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2472 result->per_cu.per_bfd = this;
2473 result->per_cu.index = m_num_psymtabs++;
2474 return result;
2475 }
2476
2477 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2478 obstack, and constructed with the specified field values. */
2479
2480 static dwarf2_per_cu_data *
2481 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2482 struct dwarf2_section_info *section,
2483 int is_dwz,
2484 sect_offset sect_off, ULONGEST length)
2485 {
2486 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2487 the_cu->sect_off = sect_off;
2488 the_cu->length = length;
2489 the_cu->section = section;
2490 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2491 struct dwarf2_per_cu_quick_data);
2492 the_cu->is_dwz = is_dwz;
2493 return the_cu;
2494 }
2495
2496 /* A helper for create_cus_from_index that handles a given list of
2497 CUs. */
2498
2499 static void
2500 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2501 const gdb_byte *cu_list, offset_type n_elements,
2502 struct dwarf2_section_info *section,
2503 int is_dwz)
2504 {
2505 for (offset_type i = 0; i < n_elements; i += 2)
2506 {
2507 gdb_static_assert (sizeof (ULONGEST) >= 8);
2508
2509 sect_offset sect_off
2510 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2511 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2512 cu_list += 2 * 8;
2513
2514 dwarf2_per_cu_data *per_cu
2515 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2516 sect_off, length);
2517 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2518 }
2519 }
2520
2521 /* Read the CU list from the mapped index, and use it to create all
2522 the CU objects for this objfile. */
2523
2524 static void
2525 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2526 const gdb_byte *cu_list, offset_type cu_list_elements,
2527 const gdb_byte *dwz_list, offset_type dwz_elements)
2528 {
2529 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2530 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2531 ((cu_list_elements + dwz_elements) / 2);
2532
2533 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2534 &dwarf2_per_objfile->per_bfd->info, 0);
2535
2536 if (dwz_elements == 0)
2537 return;
2538
2539 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2540 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2541 &dwz->info, 1);
2542 }
2543
2544 /* Create the signatured type hash table from the index. */
2545
2546 static void
2547 create_signatured_type_table_from_index
2548 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2549 struct dwarf2_section_info *section,
2550 const gdb_byte *bytes,
2551 offset_type elements)
2552 {
2553 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2554 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2555
2556 htab_up sig_types_hash = allocate_signatured_type_table ();
2557
2558 for (offset_type i = 0; i < elements; i += 3)
2559 {
2560 struct signatured_type *sig_type;
2561 ULONGEST signature;
2562 void **slot;
2563 cu_offset type_offset_in_tu;
2564
2565 gdb_static_assert (sizeof (ULONGEST) >= 8);
2566 sect_offset sect_off
2567 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2568 type_offset_in_tu
2569 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2570 BFD_ENDIAN_LITTLE);
2571 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2572 bytes += 3 * 8;
2573
2574 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2575 sig_type->signature = signature;
2576 sig_type->type_offset_in_tu = type_offset_in_tu;
2577 sig_type->per_cu.is_debug_types = 1;
2578 sig_type->per_cu.section = section;
2579 sig_type->per_cu.sect_off = sect_off;
2580 sig_type->per_cu.v.quick
2581 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2582 struct dwarf2_per_cu_quick_data);
2583
2584 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2585 *slot = sig_type;
2586
2587 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2588 }
2589
2590 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2591 }
2592
2593 /* Create the signatured type hash table from .debug_names. */
2594
2595 static void
2596 create_signatured_type_table_from_debug_names
2597 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2598 const mapped_debug_names &map,
2599 struct dwarf2_section_info *section,
2600 struct dwarf2_section_info *abbrev_section)
2601 {
2602 struct objfile *objfile = dwarf2_per_objfile->objfile;
2603
2604 section->read (objfile);
2605 abbrev_section->read (objfile);
2606
2607 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2608 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2609
2610 htab_up sig_types_hash = allocate_signatured_type_table ();
2611
2612 for (uint32_t i = 0; i < map.tu_count; ++i)
2613 {
2614 struct signatured_type *sig_type;
2615 void **slot;
2616
2617 sect_offset sect_off
2618 = (sect_offset) (extract_unsigned_integer
2619 (map.tu_table_reordered + i * map.offset_size,
2620 map.offset_size,
2621 map.dwarf5_byte_order));
2622
2623 comp_unit_head cu_header;
2624 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2625 abbrev_section,
2626 section->buffer + to_underlying (sect_off),
2627 rcuh_kind::TYPE);
2628
2629 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2630 sig_type->signature = cu_header.signature;
2631 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2632 sig_type->per_cu.is_debug_types = 1;
2633 sig_type->per_cu.section = section;
2634 sig_type->per_cu.sect_off = sect_off;
2635 sig_type->per_cu.v.quick
2636 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2637 struct dwarf2_per_cu_quick_data);
2638
2639 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2640 *slot = sig_type;
2641
2642 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2643 }
2644
2645 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2646 }
2647
2648 /* Read the address map data from the mapped index, and use it to
2649 populate the objfile's psymtabs_addrmap. */
2650
2651 static void
2652 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2653 struct mapped_index *index)
2654 {
2655 struct objfile *objfile = dwarf2_per_objfile->objfile;
2656 struct gdbarch *gdbarch = objfile->arch ();
2657 const gdb_byte *iter, *end;
2658 struct addrmap *mutable_map;
2659 CORE_ADDR baseaddr;
2660
2661 auto_obstack temp_obstack;
2662
2663 mutable_map = addrmap_create_mutable (&temp_obstack);
2664
2665 iter = index->address_table.data ();
2666 end = iter + index->address_table.size ();
2667
2668 baseaddr = objfile->text_section_offset ();
2669
2670 while (iter < end)
2671 {
2672 ULONGEST hi, lo, cu_index;
2673 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2674 iter += 8;
2675 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2676 iter += 8;
2677 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2678 iter += 4;
2679
2680 if (lo > hi)
2681 {
2682 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2683 hex_string (lo), hex_string (hi));
2684 continue;
2685 }
2686
2687 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2688 {
2689 complaint (_(".gdb_index address table has invalid CU number %u"),
2690 (unsigned) cu_index);
2691 continue;
2692 }
2693
2694 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2695 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2696 addrmap_set_empty (mutable_map, lo, hi - 1,
2697 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2698 }
2699
2700 objfile->partial_symtabs->psymtabs_addrmap
2701 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2702 }
2703
2704 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2705 populate the objfile's psymtabs_addrmap. */
2706
2707 static void
2708 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2709 struct dwarf2_section_info *section)
2710 {
2711 struct objfile *objfile = dwarf2_per_objfile->objfile;
2712 bfd *abfd = objfile->obfd;
2713 struct gdbarch *gdbarch = objfile->arch ();
2714 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2715
2716 auto_obstack temp_obstack;
2717 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2718
2719 std::unordered_map<sect_offset,
2720 dwarf2_per_cu_data *,
2721 gdb::hash_enum<sect_offset>>
2722 debug_info_offset_to_per_cu;
2723 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2724 {
2725 const auto insertpair
2726 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2727 if (!insertpair.second)
2728 {
2729 warning (_("Section .debug_aranges in %s has duplicate "
2730 "debug_info_offset %s, ignoring .debug_aranges."),
2731 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2732 return;
2733 }
2734 }
2735
2736 section->read (objfile);
2737
2738 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2739
2740 const gdb_byte *addr = section->buffer;
2741
2742 while (addr < section->buffer + section->size)
2743 {
2744 const gdb_byte *const entry_addr = addr;
2745 unsigned int bytes_read;
2746
2747 const LONGEST entry_length = read_initial_length (abfd, addr,
2748 &bytes_read);
2749 addr += bytes_read;
2750
2751 const gdb_byte *const entry_end = addr + entry_length;
2752 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2753 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2754 if (addr + entry_length > section->buffer + section->size)
2755 {
2756 warning (_("Section .debug_aranges in %s entry at offset %s "
2757 "length %s exceeds section length %s, "
2758 "ignoring .debug_aranges."),
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
2761 plongest (bytes_read + entry_length),
2762 pulongest (section->size));
2763 return;
2764 }
2765
2766 /* The version number. */
2767 const uint16_t version = read_2_bytes (abfd, addr);
2768 addr += 2;
2769 if (version != 2)
2770 {
2771 warning (_("Section .debug_aranges in %s entry at offset %s "
2772 "has unsupported version %d, ignoring .debug_aranges."),
2773 objfile_name (objfile),
2774 plongest (entry_addr - section->buffer), version);
2775 return;
2776 }
2777
2778 const uint64_t debug_info_offset
2779 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2780 addr += offset_size;
2781 const auto per_cu_it
2782 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2783 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2784 {
2785 warning (_("Section .debug_aranges in %s entry at offset %s "
2786 "debug_info_offset %s does not exists, "
2787 "ignoring .debug_aranges."),
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer),
2790 pulongest (debug_info_offset));
2791 return;
2792 }
2793 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2794
2795 const uint8_t address_size = *addr++;
2796 if (address_size < 1 || address_size > 8)
2797 {
2798 warning (_("Section .debug_aranges in %s entry at offset %s "
2799 "address_size %u is invalid, ignoring .debug_aranges."),
2800 objfile_name (objfile),
2801 plongest (entry_addr - section->buffer), address_size);
2802 return;
2803 }
2804
2805 const uint8_t segment_selector_size = *addr++;
2806 if (segment_selector_size != 0)
2807 {
2808 warning (_("Section .debug_aranges in %s entry at offset %s "
2809 "segment_selector_size %u is not supported, "
2810 "ignoring .debug_aranges."),
2811 objfile_name (objfile),
2812 plongest (entry_addr - section->buffer),
2813 segment_selector_size);
2814 return;
2815 }
2816
2817 /* Must pad to an alignment boundary that is twice the address
2818 size. It is undocumented by the DWARF standard but GCC does
2819 use it. */
2820 for (size_t padding = ((-(addr - section->buffer))
2821 & (2 * address_size - 1));
2822 padding > 0; padding--)
2823 if (*addr++ != 0)
2824 {
2825 warning (_("Section .debug_aranges in %s entry at offset %s "
2826 "padding is not zero, ignoring .debug_aranges."),
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer));
2829 return;
2830 }
2831
2832 for (;;)
2833 {
2834 if (addr + 2 * address_size > entry_end)
2835 {
2836 warning (_("Section .debug_aranges in %s entry at offset %s "
2837 "address list is not properly terminated, "
2838 "ignoring .debug_aranges."),
2839 objfile_name (objfile),
2840 plongest (entry_addr - section->buffer));
2841 return;
2842 }
2843 ULONGEST start = extract_unsigned_integer (addr, address_size,
2844 dwarf5_byte_order);
2845 addr += address_size;
2846 ULONGEST length = extract_unsigned_integer (addr, address_size,
2847 dwarf5_byte_order);
2848 addr += address_size;
2849 if (start == 0 && length == 0)
2850 break;
2851 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2852 {
2853 /* Symbol was eliminated due to a COMDAT group. */
2854 continue;
2855 }
2856 ULONGEST end = start + length;
2857 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2858 - baseaddr);
2859 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2860 - baseaddr);
2861 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2862 }
2863 }
2864
2865 objfile->partial_symtabs->psymtabs_addrmap
2866 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2867 }
2868
2869 /* Find a slot in the mapped index INDEX for the object named NAME.
2870 If NAME is found, set *VEC_OUT to point to the CU vector in the
2871 constant pool and return true. If NAME cannot be found, return
2872 false. */
2873
2874 static bool
2875 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2876 offset_type **vec_out)
2877 {
2878 offset_type hash;
2879 offset_type slot, step;
2880 int (*cmp) (const char *, const char *);
2881
2882 gdb::unique_xmalloc_ptr<char> without_params;
2883 if (current_language->la_language == language_cplus
2884 || current_language->la_language == language_fortran
2885 || current_language->la_language == language_d)
2886 {
2887 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2888 not contain any. */
2889
2890 if (strchr (name, '(') != NULL)
2891 {
2892 without_params = cp_remove_params (name);
2893
2894 if (without_params != NULL)
2895 name = without_params.get ();
2896 }
2897 }
2898
2899 /* Index version 4 did not support case insensitive searches. But the
2900 indices for case insensitive languages are built in lowercase, therefore
2901 simulate our NAME being searched is also lowercased. */
2902 hash = mapped_index_string_hash ((index->version == 4
2903 && case_sensitivity == case_sensitive_off
2904 ? 5 : index->version),
2905 name);
2906
2907 slot = hash & (index->symbol_table.size () - 1);
2908 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2909 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2910
2911 for (;;)
2912 {
2913 const char *str;
2914
2915 const auto &bucket = index->symbol_table[slot];
2916 if (bucket.name == 0 && bucket.vec == 0)
2917 return false;
2918
2919 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2920 if (!cmp (name, str))
2921 {
2922 *vec_out = (offset_type *) (index->constant_pool
2923 + MAYBE_SWAP (bucket.vec));
2924 return true;
2925 }
2926
2927 slot = (slot + step) & (index->symbol_table.size () - 1);
2928 }
2929 }
2930
2931 /* A helper function that reads the .gdb_index from BUFFER and fills
2932 in MAP. FILENAME is the name of the file containing the data;
2933 it is used for error reporting. DEPRECATED_OK is true if it is
2934 ok to use deprecated sections.
2935
2936 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2937 out parameters that are filled in with information about the CU and
2938 TU lists in the section.
2939
2940 Returns true if all went well, false otherwise. */
2941
2942 static bool
2943 read_gdb_index_from_buffer (const char *filename,
2944 bool deprecated_ok,
2945 gdb::array_view<const gdb_byte> buffer,
2946 struct mapped_index *map,
2947 const gdb_byte **cu_list,
2948 offset_type *cu_list_elements,
2949 const gdb_byte **types_list,
2950 offset_type *types_list_elements)
2951 {
2952 const gdb_byte *addr = &buffer[0];
2953
2954 /* Version check. */
2955 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2956 /* Versions earlier than 3 emitted every copy of a psymbol. This
2957 causes the index to behave very poorly for certain requests. Version 3
2958 contained incomplete addrmap. So, it seems better to just ignore such
2959 indices. */
2960 if (version < 4)
2961 {
2962 static int warning_printed = 0;
2963 if (!warning_printed)
2964 {
2965 warning (_("Skipping obsolete .gdb_index section in %s."),
2966 filename);
2967 warning_printed = 1;
2968 }
2969 return 0;
2970 }
2971 /* Index version 4 uses a different hash function than index version
2972 5 and later.
2973
2974 Versions earlier than 6 did not emit psymbols for inlined
2975 functions. Using these files will cause GDB not to be able to
2976 set breakpoints on inlined functions by name, so we ignore these
2977 indices unless the user has done
2978 "set use-deprecated-index-sections on". */
2979 if (version < 6 && !deprecated_ok)
2980 {
2981 static int warning_printed = 0;
2982 if (!warning_printed)
2983 {
2984 warning (_("\
2985 Skipping deprecated .gdb_index section in %s.\n\
2986 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2987 to use the section anyway."),
2988 filename);
2989 warning_printed = 1;
2990 }
2991 return 0;
2992 }
2993 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2994 of the TU (for symbols coming from TUs),
2995 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2996 Plus gold-generated indices can have duplicate entries for global symbols,
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2998 These are just performance bugs, and we can't distinguish gdb-generated
2999 indices from gold-generated ones, so issue no warning here. */
3000
3001 /* Indexes with higher version than the one supported by GDB may be no
3002 longer backward compatible. */
3003 if (version > 8)
3004 return 0;
3005
3006 map->version = version;
3007
3008 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3009
3010 int i = 0;
3011 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3012 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3013 / 8);
3014 ++i;
3015
3016 *types_list = addr + MAYBE_SWAP (metadata[i]);
3017 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3018 - MAYBE_SWAP (metadata[i]))
3019 / 8);
3020 ++i;
3021
3022 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3023 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3024 map->address_table
3025 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3026 ++i;
3027
3028 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3029 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3030 map->symbol_table
3031 = gdb::array_view<mapped_index::symbol_table_slot>
3032 ((mapped_index::symbol_table_slot *) symbol_table,
3033 (mapped_index::symbol_table_slot *) symbol_table_end);
3034
3035 ++i;
3036 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3037
3038 return 1;
3039 }
3040
3041 /* Callback types for dwarf2_read_gdb_index. */
3042
3043 typedef gdb::function_view
3044 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3045 get_gdb_index_contents_ftype;
3046 typedef gdb::function_view
3047 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3048 get_gdb_index_contents_dwz_ftype;
3049
3050 /* Read .gdb_index. If everything went ok, initialize the "quick"
3051 elements of all the CUs and return 1. Otherwise, return 0. */
3052
3053 static int
3054 dwarf2_read_gdb_index
3055 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3056 get_gdb_index_contents_ftype get_gdb_index_contents,
3057 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3058 {
3059 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3060 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3061 struct dwz_file *dwz;
3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
3063
3064 gdb::array_view<const gdb_byte> main_index_contents
3065 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3066
3067 if (main_index_contents.empty ())
3068 return 0;
3069
3070 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3071 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3072 use_deprecated_index_sections,
3073 main_index_contents, map.get (), &cu_list,
3074 &cu_list_elements, &types_list,
3075 &types_list_elements))
3076 return 0;
3077
3078 /* Don't use the index if it's empty. */
3079 if (map->symbol_table.empty ())
3080 return 0;
3081
3082 /* If there is a .dwz file, read it so we can get its CU list as
3083 well. */
3084 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3085 if (dwz != NULL)
3086 {
3087 struct mapped_index dwz_map;
3088 const gdb_byte *dwz_types_ignore;
3089 offset_type dwz_types_elements_ignore;
3090
3091 gdb::array_view<const gdb_byte> dwz_index_content
3092 = get_gdb_index_contents_dwz (objfile, dwz);
3093
3094 if (dwz_index_content.empty ())
3095 return 0;
3096
3097 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3098 1, dwz_index_content, &dwz_map,
3099 &dwz_list, &dwz_list_elements,
3100 &dwz_types_ignore,
3101 &dwz_types_elements_ignore))
3102 {
3103 warning (_("could not read '.gdb_index' section from %s; skipping"),
3104 bfd_get_filename (dwz->dwz_bfd.get ()));
3105 return 0;
3106 }
3107 }
3108
3109 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3110 dwz_list, dwz_list_elements);
3111
3112 if (types_list_elements)
3113 {
3114 /* We can only handle a single .debug_types when we have an
3115 index. */
3116 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3117 return 0;
3118
3119 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3120
3121 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3122 types_list, types_list_elements);
3123 }
3124
3125 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3126
3127 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3128 dwarf2_per_objfile->per_bfd->using_index = 1;
3129 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3130 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3131
3132 return 1;
3133 }
3134
3135 /* die_reader_func for dw2_get_file_names. */
3136
3137 static void
3138 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3139 const gdb_byte *info_ptr,
3140 struct die_info *comp_unit_die)
3141 {
3142 struct dwarf2_cu *cu = reader->cu;
3143 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3144 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3145 struct dwarf2_per_cu_data *lh_cu;
3146 struct attribute *attr;
3147 void **slot;
3148 struct quick_file_names *qfn;
3149
3150 gdb_assert (! this_cu->is_debug_types);
3151
3152 /* Our callers never want to match partial units -- instead they
3153 will match the enclosing full CU. */
3154 if (comp_unit_die->tag == DW_TAG_partial_unit)
3155 {
3156 this_cu->v.quick->no_file_data = 1;
3157 return;
3158 }
3159
3160 lh_cu = this_cu;
3161 slot = NULL;
3162
3163 line_header_up lh;
3164 sect_offset line_offset {};
3165
3166 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3167 if (attr != nullptr)
3168 {
3169 struct quick_file_names find_entry;
3170
3171 line_offset = (sect_offset) DW_UNSND (attr);
3172
3173 /* We may have already read in this line header (TU line header sharing).
3174 If we have we're done. */
3175 find_entry.hash.dwo_unit = cu->dwo_unit;
3176 find_entry.hash.line_sect_off = line_offset;
3177 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3178 &find_entry, INSERT);
3179 if (*slot != NULL)
3180 {
3181 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3182 return;
3183 }
3184
3185 lh = dwarf_decode_line_header (line_offset, cu);
3186 }
3187 if (lh == NULL)
3188 {
3189 lh_cu->v.quick->no_file_data = 1;
3190 return;
3191 }
3192
3193 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3194 qfn->hash.dwo_unit = cu->dwo_unit;
3195 qfn->hash.line_sect_off = line_offset;
3196 gdb_assert (slot != NULL);
3197 *slot = qfn;
3198
3199 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3200
3201 int offset = 0;
3202 if (strcmp (fnd.name, "<unknown>") != 0)
3203 ++offset;
3204
3205 qfn->num_file_names = offset + lh->file_names_size ();
3206 qfn->file_names =
3207 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3208 qfn->num_file_names);
3209 if (offset != 0)
3210 qfn->file_names[0] = xstrdup (fnd.name);
3211 for (int i = 0; i < lh->file_names_size (); ++i)
3212 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3213 fnd.comp_dir).release ();
3214 qfn->real_names = NULL;
3215
3216 lh_cu->v.quick->file_names = qfn;
3217 }
3218
3219 /* A helper for the "quick" functions which attempts to read the line
3220 table for THIS_CU. */
3221
3222 static struct quick_file_names *
3223 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3224 dwarf2_per_objfile *per_objfile)
3225 {
3226 /* This should never be called for TUs. */
3227 gdb_assert (! this_cu->is_debug_types);
3228 /* Nor type unit groups. */
3229 gdb_assert (! this_cu->type_unit_group_p ());
3230
3231 if (this_cu->v.quick->file_names != NULL)
3232 return this_cu->v.quick->file_names;
3233 /* If we know there is no line data, no point in looking again. */
3234 if (this_cu->v.quick->no_file_data)
3235 return NULL;
3236
3237 cutu_reader reader (this_cu, per_objfile);
3238 if (!reader.dummy_p)
3239 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3240
3241 if (this_cu->v.quick->no_file_data)
3242 return NULL;
3243 return this_cu->v.quick->file_names;
3244 }
3245
3246 /* A helper for the "quick" functions which computes and caches the
3247 real path for a given file name from the line table. */
3248
3249 static const char *
3250 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3251 struct quick_file_names *qfn, int index)
3252 {
3253 if (qfn->real_names == NULL)
3254 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3255 qfn->num_file_names, const char *);
3256
3257 if (qfn->real_names[index] == NULL)
3258 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3259
3260 return qfn->real_names[index];
3261 }
3262
3263 static struct symtab *
3264 dw2_find_last_source_symtab (struct objfile *objfile)
3265 {
3266 struct dwarf2_per_objfile *dwarf2_per_objfile
3267 = get_dwarf2_per_objfile (objfile);
3268 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3269 compunit_symtab *cust
3270 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3271
3272 if (cust == NULL)
3273 return NULL;
3274
3275 return compunit_primary_filetab (cust);
3276 }
3277
3278 /* Traversal function for dw2_forget_cached_source_info. */
3279
3280 static int
3281 dw2_free_cached_file_names (void **slot, void *info)
3282 {
3283 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3284
3285 if (file_data->real_names)
3286 {
3287 int i;
3288
3289 for (i = 0; i < file_data->num_file_names; ++i)
3290 {
3291 xfree ((void*) file_data->real_names[i]);
3292 file_data->real_names[i] = NULL;
3293 }
3294 }
3295
3296 return 1;
3297 }
3298
3299 static void
3300 dw2_forget_cached_source_info (struct objfile *objfile)
3301 {
3302 struct dwarf2_per_objfile *dwarf2_per_objfile
3303 = get_dwarf2_per_objfile (objfile);
3304
3305 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3306 dw2_free_cached_file_names, NULL);
3307 }
3308
3309 /* Helper function for dw2_map_symtabs_matching_filename that expands
3310 the symtabs and calls the iterator. */
3311
3312 static int
3313 dw2_map_expand_apply (struct objfile *objfile,
3314 struct dwarf2_per_cu_data *per_cu,
3315 const char *name, const char *real_path,
3316 gdb::function_view<bool (symtab *)> callback)
3317 {
3318 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3319
3320 /* Don't visit already-expanded CUs. */
3321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3322 if (per_objfile->symtab_set_p (per_cu))
3323 return 0;
3324
3325 /* This may expand more than one symtab, and we want to iterate over
3326 all of them. */
3327 dw2_instantiate_symtab (per_cu, per_objfile, false);
3328
3329 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3330 last_made, callback);
3331 }
3332
3333 /* Implementation of the map_symtabs_matching_filename method. */
3334
3335 static bool
3336 dw2_map_symtabs_matching_filename
3337 (struct objfile *objfile, const char *name, const char *real_path,
3338 gdb::function_view<bool (symtab *)> callback)
3339 {
3340 const char *name_basename = lbasename (name);
3341 struct dwarf2_per_objfile *dwarf2_per_objfile
3342 = get_dwarf2_per_objfile (objfile);
3343
3344 /* The rule is CUs specify all the files, including those used by
3345 any TU, so there's no need to scan TUs here. */
3346
3347 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3348 {
3349 /* We only need to look at symtabs not already expanded. */
3350 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3351 continue;
3352
3353 quick_file_names *file_data
3354 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3355 if (file_data == NULL)
3356 continue;
3357
3358 for (int j = 0; j < file_data->num_file_names; ++j)
3359 {
3360 const char *this_name = file_data->file_names[j];
3361 const char *this_real_name;
3362
3363 if (compare_filenames_for_search (this_name, name))
3364 {
3365 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3366 callback))
3367 return true;
3368 continue;
3369 }
3370
3371 /* Before we invoke realpath, which can get expensive when many
3372 files are involved, do a quick comparison of the basenames. */
3373 if (! basenames_may_differ
3374 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3375 continue;
3376
3377 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3378 file_data, j);
3379 if (compare_filenames_for_search (this_real_name, name))
3380 {
3381 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3382 callback))
3383 return true;
3384 continue;
3385 }
3386
3387 if (real_path != NULL)
3388 {
3389 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3390 gdb_assert (IS_ABSOLUTE_PATH (name));
3391 if (this_real_name != NULL
3392 && FILENAME_CMP (real_path, this_real_name) == 0)
3393 {
3394 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3395 callback))
3396 return true;
3397 continue;
3398 }
3399 }
3400 }
3401 }
3402
3403 return false;
3404 }
3405
3406 /* Struct used to manage iterating over all CUs looking for a symbol. */
3407
3408 struct dw2_symtab_iterator
3409 {
3410 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3411 struct dwarf2_per_objfile *dwarf2_per_objfile;
3412 /* If set, only look for symbols that match that block. Valid values are
3413 GLOBAL_BLOCK and STATIC_BLOCK. */
3414 gdb::optional<block_enum> block_index;
3415 /* The kind of symbol we're looking for. */
3416 domain_enum domain;
3417 /* The list of CUs from the index entry of the symbol,
3418 or NULL if not found. */
3419 offset_type *vec;
3420 /* The next element in VEC to look at. */
3421 int next;
3422 /* The number of elements in VEC, or zero if there is no match. */
3423 int length;
3424 /* Have we seen a global version of the symbol?
3425 If so we can ignore all further global instances.
3426 This is to work around gold/15646, inefficient gold-generated
3427 indices. */
3428 int global_seen;
3429 };
3430
3431 /* Initialize the index symtab iterator ITER. */
3432
3433 static void
3434 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3435 struct dwarf2_per_objfile *dwarf2_per_objfile,
3436 gdb::optional<block_enum> block_index,
3437 domain_enum domain,
3438 const char *name)
3439 {
3440 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3441 iter->block_index = block_index;
3442 iter->domain = domain;
3443 iter->next = 0;
3444 iter->global_seen = 0;
3445
3446 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3447
3448 /* index is NULL if OBJF_READNOW. */
3449 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3450 iter->length = MAYBE_SWAP (*iter->vec);
3451 else
3452 {
3453 iter->vec = NULL;
3454 iter->length = 0;
3455 }
3456 }
3457
3458 /* Return the next matching CU or NULL if there are no more. */
3459
3460 static struct dwarf2_per_cu_data *
3461 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3462 {
3463 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3464
3465 for ( ; iter->next < iter->length; ++iter->next)
3466 {
3467 offset_type cu_index_and_attrs =
3468 MAYBE_SWAP (iter->vec[iter->next + 1]);
3469 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3470 gdb_index_symbol_kind symbol_kind =
3471 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3472 /* Only check the symbol attributes if they're present.
3473 Indices prior to version 7 don't record them,
3474 and indices >= 7 may elide them for certain symbols
3475 (gold does this). */
3476 int attrs_valid =
3477 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3478 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3479
3480 /* Don't crash on bad data. */
3481 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3482 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3483 {
3484 complaint (_(".gdb_index entry has bad CU index"
3485 " [in module %s]"),
3486 objfile_name (dwarf2_per_objfile->objfile));
3487 continue;
3488 }
3489
3490 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3491
3492 /* Skip if already read in. */
3493 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3494 continue;
3495
3496 /* Check static vs global. */
3497 if (attrs_valid)
3498 {
3499 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3500
3501 if (iter->block_index.has_value ())
3502 {
3503 bool want_static = *iter->block_index == STATIC_BLOCK;
3504
3505 if (is_static != want_static)
3506 continue;
3507 }
3508
3509 /* Work around gold/15646. */
3510 if (!is_static && iter->global_seen)
3511 continue;
3512 if (!is_static)
3513 iter->global_seen = 1;
3514 }
3515
3516 /* Only check the symbol's kind if it has one. */
3517 if (attrs_valid)
3518 {
3519 switch (iter->domain)
3520 {
3521 case VAR_DOMAIN:
3522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3523 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3524 /* Some types are also in VAR_DOMAIN. */
3525 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3526 continue;
3527 break;
3528 case STRUCT_DOMAIN:
3529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3530 continue;
3531 break;
3532 case LABEL_DOMAIN:
3533 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3534 continue;
3535 break;
3536 case MODULE_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3538 continue;
3539 break;
3540 default:
3541 break;
3542 }
3543 }
3544
3545 ++iter->next;
3546 return per_cu;
3547 }
3548
3549 return NULL;
3550 }
3551
3552 static struct compunit_symtab *
3553 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3554 const char *name, domain_enum domain)
3555 {
3556 struct compunit_symtab *stab_best = NULL;
3557 struct dwarf2_per_objfile *dwarf2_per_objfile
3558 = get_dwarf2_per_objfile (objfile);
3559
3560 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3561
3562 struct dw2_symtab_iterator iter;
3563 struct dwarf2_per_cu_data *per_cu;
3564
3565 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3566
3567 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3568 {
3569 struct symbol *sym, *with_opaque = NULL;
3570 struct compunit_symtab *stab
3571 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3572 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3573 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3574
3575 sym = block_find_symbol (block, name, domain,
3576 block_find_non_opaque_type_preferred,
3577 &with_opaque);
3578
3579 /* Some caution must be observed with overloaded functions
3580 and methods, since the index will not contain any overload
3581 information (but NAME might contain it). */
3582
3583 if (sym != NULL
3584 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3585 return stab;
3586 if (with_opaque != NULL
3587 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3588 stab_best = stab;
3589
3590 /* Keep looking through other CUs. */
3591 }
3592
3593 return stab_best;
3594 }
3595
3596 static void
3597 dw2_print_stats (struct objfile *objfile)
3598 {
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
3601 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3602 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3603 int count = 0;
3604
3605 for (int i = 0; i < total; ++i)
3606 {
3607 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3608
3609 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3610 ++count;
3611 }
3612 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3613 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3614 }
3615
3616 /* This dumps minimal information about the index.
3617 It is called via "mt print objfiles".
3618 One use is to verify .gdb_index has been loaded by the
3619 gdb.dwarf2/gdb-index.exp testcase. */
3620
3621 static void
3622 dw2_dump (struct objfile *objfile)
3623 {
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
3626
3627 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3628 printf_filtered (".gdb_index:");
3629 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3630 {
3631 printf_filtered (" version %d\n",
3632 dwarf2_per_objfile->per_bfd->index_table->version);
3633 }
3634 else
3635 printf_filtered (" faked for \"readnow\"\n");
3636 printf_filtered ("\n");
3637 }
3638
3639 static void
3640 dw2_expand_symtabs_for_function (struct objfile *objfile,
3641 const char *func_name)
3642 {
3643 struct dwarf2_per_objfile *dwarf2_per_objfile
3644 = get_dwarf2_per_objfile (objfile);
3645
3646 struct dw2_symtab_iterator iter;
3647 struct dwarf2_per_cu_data *per_cu;
3648
3649 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3650
3651 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3652 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3653
3654 }
3655
3656 static void
3657 dw2_expand_all_symtabs (struct objfile *objfile)
3658 {
3659 struct dwarf2_per_objfile *dwarf2_per_objfile
3660 = get_dwarf2_per_objfile (objfile);
3661 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3662 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3663
3664 for (int i = 0; i < total_units; ++i)
3665 {
3666 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3667
3668 /* We don't want to directly expand a partial CU, because if we
3669 read it with the wrong language, then assertion failures can
3670 be triggered later on. See PR symtab/23010. So, tell
3671 dw2_instantiate_symtab to skip partial CUs -- any important
3672 partial CU will be read via DW_TAG_imported_unit anyway. */
3673 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3674 }
3675 }
3676
3677 static void
3678 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3679 const char *fullname)
3680 {
3681 struct dwarf2_per_objfile *dwarf2_per_objfile
3682 = get_dwarf2_per_objfile (objfile);
3683
3684 /* We don't need to consider type units here.
3685 This is only called for examining code, e.g. expand_line_sal.
3686 There can be an order of magnitude (or more) more type units
3687 than comp units, and we avoid them if we can. */
3688
3689 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3690 {
3691 /* We only need to look at symtabs not already expanded. */
3692 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3693 continue;
3694
3695 quick_file_names *file_data
3696 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3697 if (file_data == NULL)
3698 continue;
3699
3700 for (int j = 0; j < file_data->num_file_names; ++j)
3701 {
3702 const char *this_fullname = file_data->file_names[j];
3703
3704 if (filename_cmp (this_fullname, fullname) == 0)
3705 {
3706 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3707 break;
3708 }
3709 }
3710 }
3711 }
3712
3713 static void
3714 dw2_expand_symtabs_matching_symbol
3715 (mapped_index_base &index,
3716 const lookup_name_info &lookup_name_in,
3717 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3718 enum search_domain kind,
3719 gdb::function_view<bool (offset_type)> match_callback);
3720
3721 static void
3722 dw2_expand_symtabs_matching_one
3723 (dwarf2_per_cu_data *per_cu,
3724 dwarf2_per_objfile *per_objfile,
3725 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3727
3728 static void
3729 dw2_map_matching_symbols
3730 (struct objfile *objfile,
3731 const lookup_name_info &name, domain_enum domain,
3732 int global,
3733 gdb::function_view<symbol_found_callback_ftype> callback,
3734 symbol_compare_ftype *ordered_compare)
3735 {
3736 /* Used for Ada. */
3737 struct dwarf2_per_objfile *dwarf2_per_objfile
3738 = get_dwarf2_per_objfile (objfile);
3739
3740 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3741
3742 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3743 {
3744 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3745 here though if the current language is Ada for a non-Ada objfile
3746 using GNU index. */
3747 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3748
3749 const char *match_name = name.ada ().lookup_name ().c_str ();
3750 auto matcher = [&] (const char *symname)
3751 {
3752 if (ordered_compare == nullptr)
3753 return true;
3754 return ordered_compare (symname, match_name) == 0;
3755 };
3756
3757 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3758 [&] (offset_type namei)
3759 {
3760 struct dw2_symtab_iterator iter;
3761 struct dwarf2_per_cu_data *per_cu;
3762
3763 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3764 match_name);
3765 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3766 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3767 nullptr);
3768 return true;
3769 });
3770 }
3771 else
3772 {
3773 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3774 proceed assuming all symtabs have been read in. */
3775 }
3776
3777 for (compunit_symtab *cust : objfile->compunits ())
3778 {
3779 const struct block *block;
3780
3781 if (cust == NULL)
3782 continue;
3783 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3784 if (!iterate_over_symbols_terminated (block, name,
3785 domain, callback))
3786 return;
3787 }
3788 }
3789
3790 /* Starting from a search name, return the string that finds the upper
3791 bound of all strings that start with SEARCH_NAME in a sorted name
3792 list. Returns the empty string to indicate that the upper bound is
3793 the end of the list. */
3794
3795 static std::string
3796 make_sort_after_prefix_name (const char *search_name)
3797 {
3798 /* When looking to complete "func", we find the upper bound of all
3799 symbols that start with "func" by looking for where we'd insert
3800 the closest string that would follow "func" in lexicographical
3801 order. Usually, that's "func"-with-last-character-incremented,
3802 i.e. "fund". Mind non-ASCII characters, though. Usually those
3803 will be UTF-8 multi-byte sequences, but we can't be certain.
3804 Especially mind the 0xff character, which is a valid character in
3805 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3806 rule out compilers allowing it in identifiers. Note that
3807 conveniently, strcmp/strcasecmp are specified to compare
3808 characters interpreted as unsigned char. So what we do is treat
3809 the whole string as a base 256 number composed of a sequence of
3810 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3811 to 0, and carries 1 to the following more-significant position.
3812 If the very first character in SEARCH_NAME ends up incremented
3813 and carries/overflows, then the upper bound is the end of the
3814 list. The string after the empty string is also the empty
3815 string.
3816
3817 Some examples of this operation:
3818
3819 SEARCH_NAME => "+1" RESULT
3820
3821 "abc" => "abd"
3822 "ab\xff" => "ac"
3823 "\xff" "a" "\xff" => "\xff" "b"
3824 "\xff" => ""
3825 "\xff\xff" => ""
3826 "" => ""
3827
3828 Then, with these symbols for example:
3829
3830 func
3831 func1
3832 fund
3833
3834 completing "func" looks for symbols between "func" and
3835 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3836 which finds "func" and "func1", but not "fund".
3837
3838 And with:
3839
3840 funcÿ (Latin1 'ÿ' [0xff])
3841 funcÿ1
3842 fund
3843
3844 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3845 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3846
3847 And with:
3848
3849 ÿÿ (Latin1 'ÿ' [0xff])
3850 ÿÿ1
3851
3852 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3853 the end of the list.
3854 */
3855 std::string after = search_name;
3856 while (!after.empty () && (unsigned char) after.back () == 0xff)
3857 after.pop_back ();
3858 if (!after.empty ())
3859 after.back () = (unsigned char) after.back () + 1;
3860 return after;
3861 }
3862
3863 /* See declaration. */
3864
3865 std::pair<std::vector<name_component>::const_iterator,
3866 std::vector<name_component>::const_iterator>
3867 mapped_index_base::find_name_components_bounds
3868 (const lookup_name_info &lookup_name_without_params, language lang) const
3869 {
3870 auto *name_cmp
3871 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3872
3873 const char *lang_name
3874 = lookup_name_without_params.language_lookup_name (lang);
3875
3876 /* Comparison function object for lower_bound that matches against a
3877 given symbol name. */
3878 auto lookup_compare_lower = [&] (const name_component &elem,
3879 const char *name)
3880 {
3881 const char *elem_qualified = this->symbol_name_at (elem.idx);
3882 const char *elem_name = elem_qualified + elem.name_offset;
3883 return name_cmp (elem_name, name) < 0;
3884 };
3885
3886 /* Comparison function object for upper_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_upper = [&] (const char *name,
3889 const name_component &elem)
3890 {
3891 const char *elem_qualified = this->symbol_name_at (elem.idx);
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (name, elem_name) < 0;
3894 };
3895
3896 auto begin = this->name_components.begin ();
3897 auto end = this->name_components.end ();
3898
3899 /* Find the lower bound. */
3900 auto lower = [&] ()
3901 {
3902 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3903 return begin;
3904 else
3905 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3906 } ();
3907
3908 /* Find the upper bound. */
3909 auto upper = [&] ()
3910 {
3911 if (lookup_name_without_params.completion_mode ())
3912 {
3913 /* In completion mode, we want UPPER to point past all
3914 symbols names that have the same prefix. I.e., with
3915 these symbols, and completing "func":
3916
3917 function << lower bound
3918 function1
3919 other_function << upper bound
3920
3921 We find the upper bound by looking for the insertion
3922 point of "func"-with-last-character-incremented,
3923 i.e. "fund". */
3924 std::string after = make_sort_after_prefix_name (lang_name);
3925 if (after.empty ())
3926 return end;
3927 return std::lower_bound (lower, end, after.c_str (),
3928 lookup_compare_lower);
3929 }
3930 else
3931 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3932 } ();
3933
3934 return {lower, upper};
3935 }
3936
3937 /* See declaration. */
3938
3939 void
3940 mapped_index_base::build_name_components ()
3941 {
3942 if (!this->name_components.empty ())
3943 return;
3944
3945 this->name_components_casing = case_sensitivity;
3946 auto *name_cmp
3947 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3948
3949 /* The code below only knows how to break apart components of C++
3950 symbol names (and other languages that use '::' as
3951 namespace/module separator) and Ada symbol names. */
3952 auto count = this->symbol_name_count ();
3953 for (offset_type idx = 0; idx < count; idx++)
3954 {
3955 if (this->symbol_name_slot_invalid (idx))
3956 continue;
3957
3958 const char *name = this->symbol_name_at (idx);
3959
3960 /* Add each name component to the name component table. */
3961 unsigned int previous_len = 0;
3962
3963 if (strstr (name, "::") != nullptr)
3964 {
3965 for (unsigned int current_len = cp_find_first_component (name);
3966 name[current_len] != '\0';
3967 current_len += cp_find_first_component (name + current_len))
3968 {
3969 gdb_assert (name[current_len] == ':');
3970 this->name_components.push_back ({previous_len, idx});
3971 /* Skip the '::'. */
3972 current_len += 2;
3973 previous_len = current_len;
3974 }
3975 }
3976 else
3977 {
3978 /* Handle the Ada encoded (aka mangled) form here. */
3979 for (const char *iter = strstr (name, "__");
3980 iter != nullptr;
3981 iter = strstr (iter, "__"))
3982 {
3983 this->name_components.push_back ({previous_len, idx});
3984 iter += 2;
3985 previous_len = iter - name;
3986 }
3987 }
3988
3989 this->name_components.push_back ({previous_len, idx});
3990 }
3991
3992 /* Sort name_components elements by name. */
3993 auto name_comp_compare = [&] (const name_component &left,
3994 const name_component &right)
3995 {
3996 const char *left_qualified = this->symbol_name_at (left.idx);
3997 const char *right_qualified = this->symbol_name_at (right.idx);
3998
3999 const char *left_name = left_qualified + left.name_offset;
4000 const char *right_name = right_qualified + right.name_offset;
4001
4002 return name_cmp (left_name, right_name) < 0;
4003 };
4004
4005 std::sort (this->name_components.begin (),
4006 this->name_components.end (),
4007 name_comp_compare);
4008 }
4009
4010 /* Helper for dw2_expand_symtabs_matching that works with a
4011 mapped_index_base instead of the containing objfile. This is split
4012 to a separate function in order to be able to unit test the
4013 name_components matching using a mock mapped_index_base. For each
4014 symbol name that matches, calls MATCH_CALLBACK, passing it the
4015 symbol's index in the mapped_index_base symbol table. */
4016
4017 static void
4018 dw2_expand_symtabs_matching_symbol
4019 (mapped_index_base &index,
4020 const lookup_name_info &lookup_name_in,
4021 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4022 enum search_domain kind,
4023 gdb::function_view<bool (offset_type)> match_callback)
4024 {
4025 lookup_name_info lookup_name_without_params
4026 = lookup_name_in.make_ignore_params ();
4027
4028 /* Build the symbol name component sorted vector, if we haven't
4029 yet. */
4030 index.build_name_components ();
4031
4032 /* The same symbol may appear more than once in the range though.
4033 E.g., if we're looking for symbols that complete "w", and we have
4034 a symbol named "w1::w2", we'll find the two name components for
4035 that same symbol in the range. To be sure we only call the
4036 callback once per symbol, we first collect the symbol name
4037 indexes that matched in a temporary vector and ignore
4038 duplicates. */
4039 std::vector<offset_type> matches;
4040
4041 struct name_and_matcher
4042 {
4043 symbol_name_matcher_ftype *matcher;
4044 const char *name;
4045
4046 bool operator== (const name_and_matcher &other) const
4047 {
4048 return matcher == other.matcher && strcmp (name, other.name) == 0;
4049 }
4050 };
4051
4052 /* A vector holding all the different symbol name matchers, for all
4053 languages. */
4054 std::vector<name_and_matcher> matchers;
4055
4056 for (int i = 0; i < nr_languages; i++)
4057 {
4058 enum language lang_e = (enum language) i;
4059
4060 const language_defn *lang = language_def (lang_e);
4061 symbol_name_matcher_ftype *name_matcher
4062 = get_symbol_name_matcher (lang, lookup_name_without_params);
4063
4064 name_and_matcher key {
4065 name_matcher,
4066 lookup_name_without_params.language_lookup_name (lang_e)
4067 };
4068
4069 /* Don't insert the same comparison routine more than once.
4070 Note that we do this linear walk. This is not a problem in
4071 practice because the number of supported languages is
4072 low. */
4073 if (std::find (matchers.begin (), matchers.end (), key)
4074 != matchers.end ())
4075 continue;
4076 matchers.push_back (std::move (key));
4077
4078 auto bounds
4079 = index.find_name_components_bounds (lookup_name_without_params,
4080 lang_e);
4081
4082 /* Now for each symbol name in range, check to see if we have a name
4083 match, and if so, call the MATCH_CALLBACK callback. */
4084
4085 for (; bounds.first != bounds.second; ++bounds.first)
4086 {
4087 const char *qualified = index.symbol_name_at (bounds.first->idx);
4088
4089 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4090 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4091 continue;
4092
4093 matches.push_back (bounds.first->idx);
4094 }
4095 }
4096
4097 std::sort (matches.begin (), matches.end ());
4098
4099 /* Finally call the callback, once per match. */
4100 ULONGEST prev = -1;
4101 for (offset_type idx : matches)
4102 {
4103 if (prev != idx)
4104 {
4105 if (!match_callback (idx))
4106 break;
4107 prev = idx;
4108 }
4109 }
4110
4111 /* Above we use a type wider than idx's for 'prev', since 0 and
4112 (offset_type)-1 are both possible values. */
4113 static_assert (sizeof (prev) > sizeof (offset_type), "");
4114 }
4115
4116 #if GDB_SELF_TEST
4117
4118 namespace selftests { namespace dw2_expand_symtabs_matching {
4119
4120 /* A mock .gdb_index/.debug_names-like name index table, enough to
4121 exercise dw2_expand_symtabs_matching_symbol, which works with the
4122 mapped_index_base interface. Builds an index from the symbol list
4123 passed as parameter to the constructor. */
4124 class mock_mapped_index : public mapped_index_base
4125 {
4126 public:
4127 mock_mapped_index (gdb::array_view<const char *> symbols)
4128 : m_symbol_table (symbols)
4129 {}
4130
4131 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4132
4133 /* Return the number of names in the symbol table. */
4134 size_t symbol_name_count () const override
4135 {
4136 return m_symbol_table.size ();
4137 }
4138
4139 /* Get the name of the symbol at IDX in the symbol table. */
4140 const char *symbol_name_at (offset_type idx) const override
4141 {
4142 return m_symbol_table[idx];
4143 }
4144
4145 private:
4146 gdb::array_view<const char *> m_symbol_table;
4147 };
4148
4149 /* Convenience function that converts a NULL pointer to a "<null>"
4150 string, to pass to print routines. */
4151
4152 static const char *
4153 string_or_null (const char *str)
4154 {
4155 return str != NULL ? str : "<null>";
4156 }
4157
4158 /* Check if a lookup_name_info built from
4159 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4160 index. EXPECTED_LIST is the list of expected matches, in expected
4161 matching order. If no match expected, then an empty list is
4162 specified. Returns true on success. On failure prints a warning
4163 indicating the file:line that failed, and returns false. */
4164
4165 static bool
4166 check_match (const char *file, int line,
4167 mock_mapped_index &mock_index,
4168 const char *name, symbol_name_match_type match_type,
4169 bool completion_mode,
4170 std::initializer_list<const char *> expected_list)
4171 {
4172 lookup_name_info lookup_name (name, match_type, completion_mode);
4173
4174 bool matched = true;
4175
4176 auto mismatch = [&] (const char *expected_str,
4177 const char *got)
4178 {
4179 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4180 "expected=\"%s\", got=\"%s\"\n"),
4181 file, line,
4182 (match_type == symbol_name_match_type::FULL
4183 ? "FULL" : "WILD"),
4184 name, string_or_null (expected_str), string_or_null (got));
4185 matched = false;
4186 };
4187
4188 auto expected_it = expected_list.begin ();
4189 auto expected_end = expected_list.end ();
4190
4191 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4192 NULL, ALL_DOMAIN,
4193 [&] (offset_type idx)
4194 {
4195 const char *matched_name = mock_index.symbol_name_at (idx);
4196 const char *expected_str
4197 = expected_it == expected_end ? NULL : *expected_it++;
4198
4199 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4200 mismatch (expected_str, matched_name);
4201 return true;
4202 });
4203
4204 const char *expected_str
4205 = expected_it == expected_end ? NULL : *expected_it++;
4206 if (expected_str != NULL)
4207 mismatch (expected_str, NULL);
4208
4209 return matched;
4210 }
4211
4212 /* The symbols added to the mock mapped_index for testing (in
4213 canonical form). */
4214 static const char *test_symbols[] = {
4215 "function",
4216 "std::bar",
4217 "std::zfunction",
4218 "std::zfunction2",
4219 "w1::w2",
4220 "ns::foo<char*>",
4221 "ns::foo<int>",
4222 "ns::foo<long>",
4223 "ns2::tmpl<int>::foo2",
4224 "(anonymous namespace)::A::B::C",
4225
4226 /* These are used to check that the increment-last-char in the
4227 matching algorithm for completion doesn't match "t1_fund" when
4228 completing "t1_func". */
4229 "t1_func",
4230 "t1_func1",
4231 "t1_fund",
4232 "t1_fund1",
4233
4234 /* A UTF-8 name with multi-byte sequences to make sure that
4235 cp-name-parser understands this as a single identifier ("função"
4236 is "function" in PT). */
4237 u8"u8função",
4238
4239 /* \377 (0xff) is Latin1 'ÿ'. */
4240 "yfunc\377",
4241
4242 /* \377 (0xff) is Latin1 'ÿ'. */
4243 "\377",
4244 "\377\377123",
4245
4246 /* A name with all sorts of complications. Starts with "z" to make
4247 it easier for the completion tests below. */
4248 #define Z_SYM_NAME \
4249 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4250 "::tuple<(anonymous namespace)::ui*, " \
4251 "std::default_delete<(anonymous namespace)::ui>, void>"
4252
4253 Z_SYM_NAME
4254 };
4255
4256 /* Returns true if the mapped_index_base::find_name_component_bounds
4257 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4258 in completion mode. */
4259
4260 static bool
4261 check_find_bounds_finds (mapped_index_base &index,
4262 const char *search_name,
4263 gdb::array_view<const char *> expected_syms)
4264 {
4265 lookup_name_info lookup_name (search_name,
4266 symbol_name_match_type::FULL, true);
4267
4268 auto bounds = index.find_name_components_bounds (lookup_name,
4269 language_cplus);
4270
4271 size_t distance = std::distance (bounds.first, bounds.second);
4272 if (distance != expected_syms.size ())
4273 return false;
4274
4275 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4276 {
4277 auto nc_elem = bounds.first + exp_elem;
4278 const char *qualified = index.symbol_name_at (nc_elem->idx);
4279 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4280 return false;
4281 }
4282
4283 return true;
4284 }
4285
4286 /* Test the lower-level mapped_index::find_name_component_bounds
4287 method. */
4288
4289 static void
4290 test_mapped_index_find_name_component_bounds ()
4291 {
4292 mock_mapped_index mock_index (test_symbols);
4293
4294 mock_index.build_name_components ();
4295
4296 /* Test the lower-level mapped_index::find_name_component_bounds
4297 method in completion mode. */
4298 {
4299 static const char *expected_syms[] = {
4300 "t1_func",
4301 "t1_func1",
4302 };
4303
4304 SELF_CHECK (check_find_bounds_finds (mock_index,
4305 "t1_func", expected_syms));
4306 }
4307
4308 /* Check that the increment-last-char in the name matching algorithm
4309 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4310 {
4311 static const char *expected_syms1[] = {
4312 "\377",
4313 "\377\377123",
4314 };
4315 SELF_CHECK (check_find_bounds_finds (mock_index,
4316 "\377", expected_syms1));
4317
4318 static const char *expected_syms2[] = {
4319 "\377\377123",
4320 };
4321 SELF_CHECK (check_find_bounds_finds (mock_index,
4322 "\377\377", expected_syms2));
4323 }
4324 }
4325
4326 /* Test dw2_expand_symtabs_matching_symbol. */
4327
4328 static void
4329 test_dw2_expand_symtabs_matching_symbol ()
4330 {
4331 mock_mapped_index mock_index (test_symbols);
4332
4333 /* We let all tests run until the end even if some fails, for debug
4334 convenience. */
4335 bool any_mismatch = false;
4336
4337 /* Create the expected symbols list (an initializer_list). Needed
4338 because lists have commas, and we need to pass them to CHECK,
4339 which is a macro. */
4340 #define EXPECT(...) { __VA_ARGS__ }
4341
4342 /* Wrapper for check_match that passes down the current
4343 __FILE__/__LINE__. */
4344 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4345 any_mismatch |= !check_match (__FILE__, __LINE__, \
4346 mock_index, \
4347 NAME, MATCH_TYPE, COMPLETION_MODE, \
4348 EXPECTED_LIST)
4349
4350 /* Identity checks. */
4351 for (const char *sym : test_symbols)
4352 {
4353 /* Should be able to match all existing symbols. */
4354 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4355 EXPECT (sym));
4356
4357 /* Should be able to match all existing symbols with
4358 parameters. */
4359 std::string with_params = std::string (sym) + "(int)";
4360 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4361 EXPECT (sym));
4362
4363 /* Should be able to match all existing symbols with
4364 parameters and qualifiers. */
4365 with_params = std::string (sym) + " ( int ) const";
4366 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4367 EXPECT (sym));
4368
4369 /* This should really find sym, but cp-name-parser.y doesn't
4370 know about lvalue/rvalue qualifiers yet. */
4371 with_params = std::string (sym) + " ( int ) &&";
4372 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4373 {});
4374 }
4375
4376 /* Check that the name matching algorithm for completion doesn't get
4377 confused with Latin1 'ÿ' / 0xff. */
4378 {
4379 static const char str[] = "\377";
4380 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4381 EXPECT ("\377", "\377\377123"));
4382 }
4383
4384 /* Check that the increment-last-char in the matching algorithm for
4385 completion doesn't match "t1_fund" when completing "t1_func". */
4386 {
4387 static const char str[] = "t1_func";
4388 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4389 EXPECT ("t1_func", "t1_func1"));
4390 }
4391
4392 /* Check that completion mode works at each prefix of the expected
4393 symbol name. */
4394 {
4395 static const char str[] = "function(int)";
4396 size_t len = strlen (str);
4397 std::string lookup;
4398
4399 for (size_t i = 1; i < len; i++)
4400 {
4401 lookup.assign (str, i);
4402 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4403 EXPECT ("function"));
4404 }
4405 }
4406
4407 /* While "w" is a prefix of both components, the match function
4408 should still only be called once. */
4409 {
4410 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4411 EXPECT ("w1::w2"));
4412 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4413 EXPECT ("w1::w2"));
4414 }
4415
4416 /* Same, with a "complicated" symbol. */
4417 {
4418 static const char str[] = Z_SYM_NAME;
4419 size_t len = strlen (str);
4420 std::string lookup;
4421
4422 for (size_t i = 1; i < len; i++)
4423 {
4424 lookup.assign (str, i);
4425 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4426 EXPECT (Z_SYM_NAME));
4427 }
4428 }
4429
4430 /* In FULL mode, an incomplete symbol doesn't match. */
4431 {
4432 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4433 {});
4434 }
4435
4436 /* A complete symbol with parameters matches any overload, since the
4437 index has no overload info. */
4438 {
4439 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4440 EXPECT ("std::zfunction", "std::zfunction2"));
4441 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4442 EXPECT ("std::zfunction", "std::zfunction2"));
4443 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4444 EXPECT ("std::zfunction", "std::zfunction2"));
4445 }
4446
4447 /* Check that whitespace is ignored appropriately. A symbol with a
4448 template argument list. */
4449 {
4450 static const char expected[] = "ns::foo<int>";
4451 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4452 EXPECT (expected));
4453 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4454 EXPECT (expected));
4455 }
4456
4457 /* Check that whitespace is ignored appropriately. A symbol with a
4458 template argument list that includes a pointer. */
4459 {
4460 static const char expected[] = "ns::foo<char*>";
4461 /* Try both completion and non-completion modes. */
4462 static const bool completion_mode[2] = {false, true};
4463 for (size_t i = 0; i < 2; i++)
4464 {
4465 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4466 completion_mode[i], EXPECT (expected));
4467 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4468 completion_mode[i], EXPECT (expected));
4469
4470 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4471 completion_mode[i], EXPECT (expected));
4472 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4473 completion_mode[i], EXPECT (expected));
4474 }
4475 }
4476
4477 {
4478 /* Check method qualifiers are ignored. */
4479 static const char expected[] = "ns::foo<char*>";
4480 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4481 symbol_name_match_type::FULL, true, EXPECT (expected));
4482 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4483 symbol_name_match_type::FULL, true, EXPECT (expected));
4484 CHECK_MATCH ("foo < char * > ( int ) const",
4485 symbol_name_match_type::WILD, true, EXPECT (expected));
4486 CHECK_MATCH ("foo < char * > ( int ) &&",
4487 symbol_name_match_type::WILD, true, EXPECT (expected));
4488 }
4489
4490 /* Test lookup names that don't match anything. */
4491 {
4492 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4493 {});
4494
4495 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4496 {});
4497 }
4498
4499 /* Some wild matching tests, exercising "(anonymous namespace)",
4500 which should not be confused with a parameter list. */
4501 {
4502 static const char *syms[] = {
4503 "A::B::C",
4504 "B::C",
4505 "C",
4506 "A :: B :: C ( int )",
4507 "B :: C ( int )",
4508 "C ( int )",
4509 };
4510
4511 for (const char *s : syms)
4512 {
4513 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4514 EXPECT ("(anonymous namespace)::A::B::C"));
4515 }
4516 }
4517
4518 {
4519 static const char expected[] = "ns2::tmpl<int>::foo2";
4520 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4521 EXPECT (expected));
4522 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4523 EXPECT (expected));
4524 }
4525
4526 SELF_CHECK (!any_mismatch);
4527
4528 #undef EXPECT
4529 #undef CHECK_MATCH
4530 }
4531
4532 static void
4533 run_test ()
4534 {
4535 test_mapped_index_find_name_component_bounds ();
4536 test_dw2_expand_symtabs_matching_symbol ();
4537 }
4538
4539 }} // namespace selftests::dw2_expand_symtabs_matching
4540
4541 #endif /* GDB_SELF_TEST */
4542
4543 /* If FILE_MATCHER is NULL or if PER_CU has
4544 dwarf2_per_cu_quick_data::MARK set (see
4545 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4546 EXPANSION_NOTIFY on it. */
4547
4548 static void
4549 dw2_expand_symtabs_matching_one
4550 (dwarf2_per_cu_data *per_cu,
4551 dwarf2_per_objfile *per_objfile,
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4554 {
4555 if (file_matcher == NULL || per_cu->v.quick->mark)
4556 {
4557 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4558
4559 compunit_symtab *symtab
4560 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4561 gdb_assert (symtab != nullptr);
4562
4563 if (expansion_notify != NULL && symtab_was_null)
4564 expansion_notify (symtab);
4565 }
4566 }
4567
4568 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4569 matched, to expand corresponding CUs that were marked. IDX is the
4570 index of the symbol name that matched. */
4571
4572 static void
4573 dw2_expand_marked_cus
4574 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4575 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4576 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4577 search_domain kind)
4578 {
4579 offset_type *vec, vec_len, vec_idx;
4580 bool global_seen = false;
4581 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4582
4583 vec = (offset_type *) (index.constant_pool
4584 + MAYBE_SWAP (index.symbol_table[idx].vec));
4585 vec_len = MAYBE_SWAP (vec[0]);
4586 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4587 {
4588 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4589 /* This value is only valid for index versions >= 7. */
4590 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4591 gdb_index_symbol_kind symbol_kind =
4592 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4593 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4594 /* Only check the symbol attributes if they're present.
4595 Indices prior to version 7 don't record them,
4596 and indices >= 7 may elide them for certain symbols
4597 (gold does this). */
4598 int attrs_valid =
4599 (index.version >= 7
4600 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4601
4602 /* Work around gold/15646. */
4603 if (attrs_valid)
4604 {
4605 if (!is_static && global_seen)
4606 continue;
4607 if (!is_static)
4608 global_seen = true;
4609 }
4610
4611 /* Only check the symbol's kind if it has one. */
4612 if (attrs_valid)
4613 {
4614 switch (kind)
4615 {
4616 case VARIABLES_DOMAIN:
4617 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4618 continue;
4619 break;
4620 case FUNCTIONS_DOMAIN:
4621 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4622 continue;
4623 break;
4624 case TYPES_DOMAIN:
4625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4626 continue;
4627 break;
4628 case MODULES_DOMAIN:
4629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4630 continue;
4631 break;
4632 default:
4633 break;
4634 }
4635 }
4636
4637 /* Don't crash on bad data. */
4638 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4639 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4640 {
4641 complaint (_(".gdb_index entry has bad CU index"
4642 " [in module %s]"),
4643 objfile_name (dwarf2_per_objfile->objfile));
4644 continue;
4645 }
4646
4647 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4648 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4649 expansion_notify);
4650 }
4651 }
4652
4653 /* If FILE_MATCHER is non-NULL, set all the
4654 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4655 that match FILE_MATCHER. */
4656
4657 static void
4658 dw_expand_symtabs_matching_file_matcher
4659 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4660 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4661 {
4662 if (file_matcher == NULL)
4663 return;
4664
4665 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4666 htab_eq_pointer,
4667 NULL, xcalloc, xfree));
4668 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4669 htab_eq_pointer,
4670 NULL, xcalloc, xfree));
4671
4672 /* The rule is CUs specify all the files, including those used by
4673 any TU, so there's no need to scan TUs here. */
4674
4675 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4676 {
4677 QUIT;
4678
4679 per_cu->v.quick->mark = 0;
4680
4681 /* We only need to look at symtabs not already expanded. */
4682 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4683 continue;
4684
4685 quick_file_names *file_data
4686 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4687 if (file_data == NULL)
4688 continue;
4689
4690 if (htab_find (visited_not_found.get (), file_data) != NULL)
4691 continue;
4692 else if (htab_find (visited_found.get (), file_data) != NULL)
4693 {
4694 per_cu->v.quick->mark = 1;
4695 continue;
4696 }
4697
4698 for (int j = 0; j < file_data->num_file_names; ++j)
4699 {
4700 const char *this_real_name;
4701
4702 if (file_matcher (file_data->file_names[j], false))
4703 {
4704 per_cu->v.quick->mark = 1;
4705 break;
4706 }
4707
4708 /* Before we invoke realpath, which can get expensive when many
4709 files are involved, do a quick comparison of the basenames. */
4710 if (!basenames_may_differ
4711 && !file_matcher (lbasename (file_data->file_names[j]),
4712 true))
4713 continue;
4714
4715 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4716 file_data, j);
4717 if (file_matcher (this_real_name, false))
4718 {
4719 per_cu->v.quick->mark = 1;
4720 break;
4721 }
4722 }
4723
4724 void **slot = htab_find_slot (per_cu->v.quick->mark
4725 ? visited_found.get ()
4726 : visited_not_found.get (),
4727 file_data, INSERT);
4728 *slot = file_data;
4729 }
4730 }
4731
4732 static void
4733 dw2_expand_symtabs_matching
4734 (struct objfile *objfile,
4735 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4736 const lookup_name_info *lookup_name,
4737 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4738 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4739 enum search_domain kind)
4740 {
4741 struct dwarf2_per_objfile *dwarf2_per_objfile
4742 = get_dwarf2_per_objfile (objfile);
4743
4744 /* index_table is NULL if OBJF_READNOW. */
4745 if (!dwarf2_per_objfile->per_bfd->index_table)
4746 return;
4747
4748 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4749
4750 if (symbol_matcher == NULL && lookup_name == NULL)
4751 {
4752 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4753 {
4754 QUIT;
4755
4756 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4757 file_matcher, expansion_notify);
4758 }
4759 return;
4760 }
4761
4762 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4763
4764 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4765 symbol_matcher,
4766 kind, [&] (offset_type idx)
4767 {
4768 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4769 expansion_notify, kind);
4770 return true;
4771 });
4772 }
4773
4774 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4775 symtab. */
4776
4777 static struct compunit_symtab *
4778 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4779 CORE_ADDR pc)
4780 {
4781 int i;
4782
4783 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4784 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4785 return cust;
4786
4787 if (cust->includes == NULL)
4788 return NULL;
4789
4790 for (i = 0; cust->includes[i]; ++i)
4791 {
4792 struct compunit_symtab *s = cust->includes[i];
4793
4794 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4795 if (s != NULL)
4796 return s;
4797 }
4798
4799 return NULL;
4800 }
4801
4802 static struct compunit_symtab *
4803 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4804 struct bound_minimal_symbol msymbol,
4805 CORE_ADDR pc,
4806 struct obj_section *section,
4807 int warn_if_readin)
4808 {
4809 struct dwarf2_per_cu_data *data;
4810 struct compunit_symtab *result;
4811
4812 if (!objfile->partial_symtabs->psymtabs_addrmap)
4813 return NULL;
4814
4815 CORE_ADDR baseaddr = objfile->text_section_offset ();
4816 data = (struct dwarf2_per_cu_data *) addrmap_find
4817 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4818 if (!data)
4819 return NULL;
4820
4821 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4822 if (warn_if_readin && per_objfile->symtab_set_p (data))
4823 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4824 paddress (objfile->arch (), pc));
4825
4826 result = recursively_find_pc_sect_compunit_symtab
4827 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4828
4829 gdb_assert (result != NULL);
4830 return result;
4831 }
4832
4833 static void
4834 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4835 void *data, int need_fullname)
4836 {
4837 struct dwarf2_per_objfile *dwarf2_per_objfile
4838 = get_dwarf2_per_objfile (objfile);
4839
4840 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4841 {
4842 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4843
4844 htab_up visited (htab_create_alloc (10,
4845 htab_hash_pointer, htab_eq_pointer,
4846 NULL, xcalloc, xfree));
4847
4848 /* The rule is CUs specify all the files, including those used
4849 by any TU, so there's no need to scan TUs here. We can
4850 ignore file names coming from already-expanded CUs. */
4851
4852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4853 {
4854 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4855 {
4856 void **slot = htab_find_slot (visited.get (),
4857 per_cu->v.quick->file_names,
4858 INSERT);
4859
4860 *slot = per_cu->v.quick->file_names;
4861 }
4862 }
4863
4864 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4865 {
4866 /* We only need to look at symtabs not already expanded. */
4867 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4868 continue;
4869
4870 quick_file_names *file_data
4871 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4872 if (file_data == NULL)
4873 continue;
4874
4875 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4876 if (*slot)
4877 {
4878 /* Already visited. */
4879 continue;
4880 }
4881 *slot = file_data;
4882
4883 for (int j = 0; j < file_data->num_file_names; ++j)
4884 {
4885 const char *filename = file_data->file_names[j];
4886 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4887 }
4888 }
4889 }
4890
4891 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4892 {
4893 gdb::unique_xmalloc_ptr<char> this_real_name;
4894
4895 if (need_fullname)
4896 this_real_name = gdb_realpath (filename);
4897 (*fun) (filename, this_real_name.get (), data);
4898 });
4899 }
4900
4901 static int
4902 dw2_has_symbols (struct objfile *objfile)
4903 {
4904 return 1;
4905 }
4906
4907 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4908 {
4909 dw2_has_symbols,
4910 dw2_find_last_source_symtab,
4911 dw2_forget_cached_source_info,
4912 dw2_map_symtabs_matching_filename,
4913 dw2_lookup_symbol,
4914 NULL,
4915 dw2_print_stats,
4916 dw2_dump,
4917 dw2_expand_symtabs_for_function,
4918 dw2_expand_all_symtabs,
4919 dw2_expand_symtabs_with_fullname,
4920 dw2_map_matching_symbols,
4921 dw2_expand_symtabs_matching,
4922 dw2_find_pc_sect_compunit_symtab,
4923 NULL,
4924 dw2_map_symbol_filenames
4925 };
4926
4927 /* DWARF-5 debug_names reader. */
4928
4929 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4930 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4931
4932 /* A helper function that reads the .debug_names section in SECTION
4933 and fills in MAP. FILENAME is the name of the file containing the
4934 section; it is used for error reporting.
4935
4936 Returns true if all went well, false otherwise. */
4937
4938 static bool
4939 read_debug_names_from_section (struct objfile *objfile,
4940 const char *filename,
4941 struct dwarf2_section_info *section,
4942 mapped_debug_names &map)
4943 {
4944 if (section->empty ())
4945 return false;
4946
4947 /* Older elfutils strip versions could keep the section in the main
4948 executable while splitting it for the separate debug info file. */
4949 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4950 return false;
4951
4952 section->read (objfile);
4953
4954 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4955
4956 const gdb_byte *addr = section->buffer;
4957
4958 bfd *const abfd = section->get_bfd_owner ();
4959
4960 unsigned int bytes_read;
4961 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4962 addr += bytes_read;
4963
4964 map.dwarf5_is_dwarf64 = bytes_read != 4;
4965 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4966 if (bytes_read + length != section->size)
4967 {
4968 /* There may be multiple per-CU indices. */
4969 warning (_("Section .debug_names in %s length %s does not match "
4970 "section length %s, ignoring .debug_names."),
4971 filename, plongest (bytes_read + length),
4972 pulongest (section->size));
4973 return false;
4974 }
4975
4976 /* The version number. */
4977 uint16_t version = read_2_bytes (abfd, addr);
4978 addr += 2;
4979 if (version != 5)
4980 {
4981 warning (_("Section .debug_names in %s has unsupported version %d, "
4982 "ignoring .debug_names."),
4983 filename, version);
4984 return false;
4985 }
4986
4987 /* Padding. */
4988 uint16_t padding = read_2_bytes (abfd, addr);
4989 addr += 2;
4990 if (padding != 0)
4991 {
4992 warning (_("Section .debug_names in %s has unsupported padding %d, "
4993 "ignoring .debug_names."),
4994 filename, padding);
4995 return false;
4996 }
4997
4998 /* comp_unit_count - The number of CUs in the CU list. */
4999 map.cu_count = read_4_bytes (abfd, addr);
5000 addr += 4;
5001
5002 /* local_type_unit_count - The number of TUs in the local TU
5003 list. */
5004 map.tu_count = read_4_bytes (abfd, addr);
5005 addr += 4;
5006
5007 /* foreign_type_unit_count - The number of TUs in the foreign TU
5008 list. */
5009 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5010 addr += 4;
5011 if (foreign_tu_count != 0)
5012 {
5013 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5014 "ignoring .debug_names."),
5015 filename, static_cast<unsigned long> (foreign_tu_count));
5016 return false;
5017 }
5018
5019 /* bucket_count - The number of hash buckets in the hash lookup
5020 table. */
5021 map.bucket_count = read_4_bytes (abfd, addr);
5022 addr += 4;
5023
5024 /* name_count - The number of unique names in the index. */
5025 map.name_count = read_4_bytes (abfd, addr);
5026 addr += 4;
5027
5028 /* abbrev_table_size - The size in bytes of the abbreviations
5029 table. */
5030 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5031 addr += 4;
5032
5033 /* augmentation_string_size - The size in bytes of the augmentation
5034 string. This value is rounded up to a multiple of 4. */
5035 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5036 addr += 4;
5037 map.augmentation_is_gdb = ((augmentation_string_size
5038 == sizeof (dwarf5_augmentation))
5039 && memcmp (addr, dwarf5_augmentation,
5040 sizeof (dwarf5_augmentation)) == 0);
5041 augmentation_string_size += (-augmentation_string_size) & 3;
5042 addr += augmentation_string_size;
5043
5044 /* List of CUs */
5045 map.cu_table_reordered = addr;
5046 addr += map.cu_count * map.offset_size;
5047
5048 /* List of Local TUs */
5049 map.tu_table_reordered = addr;
5050 addr += map.tu_count * map.offset_size;
5051
5052 /* Hash Lookup Table */
5053 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5054 addr += map.bucket_count * 4;
5055 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5056 addr += map.name_count * 4;
5057
5058 /* Name Table */
5059 map.name_table_string_offs_reordered = addr;
5060 addr += map.name_count * map.offset_size;
5061 map.name_table_entry_offs_reordered = addr;
5062 addr += map.name_count * map.offset_size;
5063
5064 const gdb_byte *abbrev_table_start = addr;
5065 for (;;)
5066 {
5067 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5068 addr += bytes_read;
5069 if (index_num == 0)
5070 break;
5071
5072 const auto insertpair
5073 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5074 if (!insertpair.second)
5075 {
5076 warning (_("Section .debug_names in %s has duplicate index %s, "
5077 "ignoring .debug_names."),
5078 filename, pulongest (index_num));
5079 return false;
5080 }
5081 mapped_debug_names::index_val &indexval = insertpair.first->second;
5082 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5083 addr += bytes_read;
5084
5085 for (;;)
5086 {
5087 mapped_debug_names::index_val::attr attr;
5088 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5089 addr += bytes_read;
5090 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5091 addr += bytes_read;
5092 if (attr.form == DW_FORM_implicit_const)
5093 {
5094 attr.implicit_const = read_signed_leb128 (abfd, addr,
5095 &bytes_read);
5096 addr += bytes_read;
5097 }
5098 if (attr.dw_idx == 0 && attr.form == 0)
5099 break;
5100 indexval.attr_vec.push_back (std::move (attr));
5101 }
5102 }
5103 if (addr != abbrev_table_start + abbrev_table_size)
5104 {
5105 warning (_("Section .debug_names in %s has abbreviation_table "
5106 "of size %s vs. written as %u, ignoring .debug_names."),
5107 filename, plongest (addr - abbrev_table_start),
5108 abbrev_table_size);
5109 return false;
5110 }
5111 map.entry_pool = addr;
5112
5113 return true;
5114 }
5115
5116 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5117 list. */
5118
5119 static void
5120 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5121 const mapped_debug_names &map,
5122 dwarf2_section_info &section,
5123 bool is_dwz)
5124 {
5125 if (!map.augmentation_is_gdb)
5126 {
5127 for (uint32_t i = 0; i < map.cu_count; ++i)
5128 {
5129 sect_offset sect_off
5130 = (sect_offset) (extract_unsigned_integer
5131 (map.cu_table_reordered + i * map.offset_size,
5132 map.offset_size,
5133 map.dwarf5_byte_order));
5134 /* We don't know the length of the CU, because the CU list in a
5135 .debug_names index can be incomplete, so we can't use the start of
5136 the next CU as end of this CU. We create the CUs here with length 0,
5137 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5138 dwarf2_per_cu_data *per_cu
5139 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5140 sect_off, 0);
5141 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5142 }
5143 }
5144
5145 sect_offset sect_off_prev;
5146 for (uint32_t i = 0; i <= map.cu_count; ++i)
5147 {
5148 sect_offset sect_off_next;
5149 if (i < map.cu_count)
5150 {
5151 sect_off_next
5152 = (sect_offset) (extract_unsigned_integer
5153 (map.cu_table_reordered + i * map.offset_size,
5154 map.offset_size,
5155 map.dwarf5_byte_order));
5156 }
5157 else
5158 sect_off_next = (sect_offset) section.size;
5159 if (i >= 1)
5160 {
5161 const ULONGEST length = sect_off_next - sect_off_prev;
5162 dwarf2_per_cu_data *per_cu
5163 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5164 sect_off_prev, length);
5165 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5166 }
5167 sect_off_prev = sect_off_next;
5168 }
5169 }
5170
5171 /* Read the CU list from the mapped index, and use it to create all
5172 the CU objects for this dwarf2_per_objfile. */
5173
5174 static void
5175 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5176 const mapped_debug_names &map,
5177 const mapped_debug_names &dwz_map)
5178 {
5179 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5180 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5181
5182 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5183 dwarf2_per_objfile->per_bfd->info,
5184 false /* is_dwz */);
5185
5186 if (dwz_map.cu_count == 0)
5187 return;
5188
5189 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5190 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5191 true /* is_dwz */);
5192 }
5193
5194 /* Read .debug_names. If everything went ok, initialize the "quick"
5195 elements of all the CUs and return true. Otherwise, return false. */
5196
5197 static bool
5198 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5199 {
5200 std::unique_ptr<mapped_debug_names> map
5201 (new mapped_debug_names (dwarf2_per_objfile));
5202 mapped_debug_names dwz_map (dwarf2_per_objfile);
5203 struct objfile *objfile = dwarf2_per_objfile->objfile;
5204
5205 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5206 &dwarf2_per_objfile->per_bfd->debug_names,
5207 *map))
5208 return false;
5209
5210 /* Don't use the index if it's empty. */
5211 if (map->name_count == 0)
5212 return false;
5213
5214 /* If there is a .dwz file, read it so we can get its CU list as
5215 well. */
5216 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5217 if (dwz != NULL)
5218 {
5219 if (!read_debug_names_from_section (objfile,
5220 bfd_get_filename (dwz->dwz_bfd.get ()),
5221 &dwz->debug_names, dwz_map))
5222 {
5223 warning (_("could not read '.debug_names' section from %s; skipping"),
5224 bfd_get_filename (dwz->dwz_bfd.get ()));
5225 return false;
5226 }
5227 }
5228
5229 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5230
5231 if (map->tu_count != 0)
5232 {
5233 /* We can only handle a single .debug_types when we have an
5234 index. */
5235 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5236 return false;
5237
5238 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5239
5240 create_signatured_type_table_from_debug_names
5241 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5242 }
5243
5244 create_addrmap_from_aranges (dwarf2_per_objfile,
5245 &dwarf2_per_objfile->per_bfd->debug_aranges);
5246
5247 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5248 dwarf2_per_objfile->per_bfd->using_index = 1;
5249 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5250 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5251
5252 return true;
5253 }
5254
5255 /* Type used to manage iterating over all CUs looking for a symbol for
5256 .debug_names. */
5257
5258 class dw2_debug_names_iterator
5259 {
5260 public:
5261 dw2_debug_names_iterator (const mapped_debug_names &map,
5262 gdb::optional<block_enum> block_index,
5263 domain_enum domain,
5264 const char *name)
5265 : m_map (map), m_block_index (block_index), m_domain (domain),
5266 m_addr (find_vec_in_debug_names (map, name))
5267 {}
5268
5269 dw2_debug_names_iterator (const mapped_debug_names &map,
5270 search_domain search, uint32_t namei)
5271 : m_map (map),
5272 m_search (search),
5273 m_addr (find_vec_in_debug_names (map, namei))
5274 {}
5275
5276 dw2_debug_names_iterator (const mapped_debug_names &map,
5277 block_enum block_index, domain_enum domain,
5278 uint32_t namei)
5279 : m_map (map), m_block_index (block_index), m_domain (domain),
5280 m_addr (find_vec_in_debug_names (map, namei))
5281 {}
5282
5283 /* Return the next matching CU or NULL if there are no more. */
5284 dwarf2_per_cu_data *next ();
5285
5286 private:
5287 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5288 const char *name);
5289 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5290 uint32_t namei);
5291
5292 /* The internalized form of .debug_names. */
5293 const mapped_debug_names &m_map;
5294
5295 /* If set, only look for symbols that match that block. Valid values are
5296 GLOBAL_BLOCK and STATIC_BLOCK. */
5297 const gdb::optional<block_enum> m_block_index;
5298
5299 /* The kind of symbol we're looking for. */
5300 const domain_enum m_domain = UNDEF_DOMAIN;
5301 const search_domain m_search = ALL_DOMAIN;
5302
5303 /* The list of CUs from the index entry of the symbol, or NULL if
5304 not found. */
5305 const gdb_byte *m_addr;
5306 };
5307
5308 const char *
5309 mapped_debug_names::namei_to_name (uint32_t namei) const
5310 {
5311 const ULONGEST namei_string_offs
5312 = extract_unsigned_integer ((name_table_string_offs_reordered
5313 + namei * offset_size),
5314 offset_size,
5315 dwarf5_byte_order);
5316 return read_indirect_string_at_offset (dwarf2_per_objfile,
5317 namei_string_offs);
5318 }
5319
5320 /* Find a slot in .debug_names for the object named NAME. If NAME is
5321 found, return pointer to its pool data. If NAME cannot be found,
5322 return NULL. */
5323
5324 const gdb_byte *
5325 dw2_debug_names_iterator::find_vec_in_debug_names
5326 (const mapped_debug_names &map, const char *name)
5327 {
5328 int (*cmp) (const char *, const char *);
5329
5330 gdb::unique_xmalloc_ptr<char> without_params;
5331 if (current_language->la_language == language_cplus
5332 || current_language->la_language == language_fortran
5333 || current_language->la_language == language_d)
5334 {
5335 /* NAME is already canonical. Drop any qualifiers as
5336 .debug_names does not contain any. */
5337
5338 if (strchr (name, '(') != NULL)
5339 {
5340 without_params = cp_remove_params (name);
5341 if (without_params != NULL)
5342 name = without_params.get ();
5343 }
5344 }
5345
5346 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5347
5348 const uint32_t full_hash = dwarf5_djb_hash (name);
5349 uint32_t namei
5350 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5351 (map.bucket_table_reordered
5352 + (full_hash % map.bucket_count)), 4,
5353 map.dwarf5_byte_order);
5354 if (namei == 0)
5355 return NULL;
5356 --namei;
5357 if (namei >= map.name_count)
5358 {
5359 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5360 "[in module %s]"),
5361 namei, map.name_count,
5362 objfile_name (map.dwarf2_per_objfile->objfile));
5363 return NULL;
5364 }
5365
5366 for (;;)
5367 {
5368 const uint32_t namei_full_hash
5369 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5370 (map.hash_table_reordered + namei), 4,
5371 map.dwarf5_byte_order);
5372 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5373 return NULL;
5374
5375 if (full_hash == namei_full_hash)
5376 {
5377 const char *const namei_string = map.namei_to_name (namei);
5378
5379 #if 0 /* An expensive sanity check. */
5380 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5381 {
5382 complaint (_("Wrong .debug_names hash for string at index %u "
5383 "[in module %s]"),
5384 namei, objfile_name (dwarf2_per_objfile->objfile));
5385 return NULL;
5386 }
5387 #endif
5388
5389 if (cmp (namei_string, name) == 0)
5390 {
5391 const ULONGEST namei_entry_offs
5392 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5393 + namei * map.offset_size),
5394 map.offset_size, map.dwarf5_byte_order);
5395 return map.entry_pool + namei_entry_offs;
5396 }
5397 }
5398
5399 ++namei;
5400 if (namei >= map.name_count)
5401 return NULL;
5402 }
5403 }
5404
5405 const gdb_byte *
5406 dw2_debug_names_iterator::find_vec_in_debug_names
5407 (const mapped_debug_names &map, uint32_t namei)
5408 {
5409 if (namei >= map.name_count)
5410 {
5411 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5412 "[in module %s]"),
5413 namei, map.name_count,
5414 objfile_name (map.dwarf2_per_objfile->objfile));
5415 return NULL;
5416 }
5417
5418 const ULONGEST namei_entry_offs
5419 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5420 + namei * map.offset_size),
5421 map.offset_size, map.dwarf5_byte_order);
5422 return map.entry_pool + namei_entry_offs;
5423 }
5424
5425 /* See dw2_debug_names_iterator. */
5426
5427 dwarf2_per_cu_data *
5428 dw2_debug_names_iterator::next ()
5429 {
5430 if (m_addr == NULL)
5431 return NULL;
5432
5433 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5434 struct objfile *objfile = dwarf2_per_objfile->objfile;
5435 bfd *const abfd = objfile->obfd;
5436
5437 again:
5438
5439 unsigned int bytes_read;
5440 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5441 m_addr += bytes_read;
5442 if (abbrev == 0)
5443 return NULL;
5444
5445 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5446 if (indexval_it == m_map.abbrev_map.cend ())
5447 {
5448 complaint (_("Wrong .debug_names undefined abbrev code %s "
5449 "[in module %s]"),
5450 pulongest (abbrev), objfile_name (objfile));
5451 return NULL;
5452 }
5453 const mapped_debug_names::index_val &indexval = indexval_it->second;
5454 enum class symbol_linkage {
5455 unknown,
5456 static_,
5457 extern_,
5458 } symbol_linkage_ = symbol_linkage::unknown;
5459 dwarf2_per_cu_data *per_cu = NULL;
5460 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5461 {
5462 ULONGEST ull;
5463 switch (attr.form)
5464 {
5465 case DW_FORM_implicit_const:
5466 ull = attr.implicit_const;
5467 break;
5468 case DW_FORM_flag_present:
5469 ull = 1;
5470 break;
5471 case DW_FORM_udata:
5472 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5473 m_addr += bytes_read;
5474 break;
5475 case DW_FORM_ref4:
5476 ull = read_4_bytes (abfd, m_addr);
5477 m_addr += 4;
5478 break;
5479 case DW_FORM_ref8:
5480 ull = read_8_bytes (abfd, m_addr);
5481 m_addr += 8;
5482 break;
5483 case DW_FORM_ref_sig8:
5484 ull = read_8_bytes (abfd, m_addr);
5485 m_addr += 8;
5486 break;
5487 default:
5488 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5489 dwarf_form_name (attr.form),
5490 objfile_name (objfile));
5491 return NULL;
5492 }
5493 switch (attr.dw_idx)
5494 {
5495 case DW_IDX_compile_unit:
5496 /* Don't crash on bad data. */
5497 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5498 {
5499 complaint (_(".debug_names entry has bad CU index %s"
5500 " [in module %s]"),
5501 pulongest (ull),
5502 objfile_name (dwarf2_per_objfile->objfile));
5503 continue;
5504 }
5505 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5506 break;
5507 case DW_IDX_type_unit:
5508 /* Don't crash on bad data. */
5509 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5510 {
5511 complaint (_(".debug_names entry has bad TU index %s"
5512 " [in module %s]"),
5513 pulongest (ull),
5514 objfile_name (dwarf2_per_objfile->objfile));
5515 continue;
5516 }
5517 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5518 break;
5519 case DW_IDX_die_offset:
5520 /* In a per-CU index (as opposed to a per-module index), index
5521 entries without CU attribute implicitly refer to the single CU. */
5522 if (per_cu == NULL)
5523 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5524 break;
5525 case DW_IDX_GNU_internal:
5526 if (!m_map.augmentation_is_gdb)
5527 break;
5528 symbol_linkage_ = symbol_linkage::static_;
5529 break;
5530 case DW_IDX_GNU_external:
5531 if (!m_map.augmentation_is_gdb)
5532 break;
5533 symbol_linkage_ = symbol_linkage::extern_;
5534 break;
5535 }
5536 }
5537
5538 /* Skip if already read in. */
5539 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5540 goto again;
5541
5542 /* Check static vs global. */
5543 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5544 {
5545 const bool want_static = *m_block_index == STATIC_BLOCK;
5546 const bool symbol_is_static =
5547 symbol_linkage_ == symbol_linkage::static_;
5548 if (want_static != symbol_is_static)
5549 goto again;
5550 }
5551
5552 /* Match dw2_symtab_iter_next, symbol_kind
5553 and debug_names::psymbol_tag. */
5554 switch (m_domain)
5555 {
5556 case VAR_DOMAIN:
5557 switch (indexval.dwarf_tag)
5558 {
5559 case DW_TAG_variable:
5560 case DW_TAG_subprogram:
5561 /* Some types are also in VAR_DOMAIN. */
5562 case DW_TAG_typedef:
5563 case DW_TAG_structure_type:
5564 break;
5565 default:
5566 goto again;
5567 }
5568 break;
5569 case STRUCT_DOMAIN:
5570 switch (indexval.dwarf_tag)
5571 {
5572 case DW_TAG_typedef:
5573 case DW_TAG_structure_type:
5574 break;
5575 default:
5576 goto again;
5577 }
5578 break;
5579 case LABEL_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case 0:
5583 case DW_TAG_variable:
5584 break;
5585 default:
5586 goto again;
5587 }
5588 break;
5589 case MODULE_DOMAIN:
5590 switch (indexval.dwarf_tag)
5591 {
5592 case DW_TAG_module:
5593 break;
5594 default:
5595 goto again;
5596 }
5597 break;
5598 default:
5599 break;
5600 }
5601
5602 /* Match dw2_expand_symtabs_matching, symbol_kind and
5603 debug_names::psymbol_tag. */
5604 switch (m_search)
5605 {
5606 case VARIABLES_DOMAIN:
5607 switch (indexval.dwarf_tag)
5608 {
5609 case DW_TAG_variable:
5610 break;
5611 default:
5612 goto again;
5613 }
5614 break;
5615 case FUNCTIONS_DOMAIN:
5616 switch (indexval.dwarf_tag)
5617 {
5618 case DW_TAG_subprogram:
5619 break;
5620 default:
5621 goto again;
5622 }
5623 break;
5624 case TYPES_DOMAIN:
5625 switch (indexval.dwarf_tag)
5626 {
5627 case DW_TAG_typedef:
5628 case DW_TAG_structure_type:
5629 break;
5630 default:
5631 goto again;
5632 }
5633 break;
5634 case MODULES_DOMAIN:
5635 switch (indexval.dwarf_tag)
5636 {
5637 case DW_TAG_module:
5638 break;
5639 default:
5640 goto again;
5641 }
5642 default:
5643 break;
5644 }
5645
5646 return per_cu;
5647 }
5648
5649 static struct compunit_symtab *
5650 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5651 const char *name, domain_enum domain)
5652 {
5653 struct dwarf2_per_objfile *dwarf2_per_objfile
5654 = get_dwarf2_per_objfile (objfile);
5655
5656 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5657 if (!mapp)
5658 {
5659 /* index is NULL if OBJF_READNOW. */
5660 return NULL;
5661 }
5662 const auto &map = *mapp;
5663
5664 dw2_debug_names_iterator iter (map, block_index, domain, name);
5665
5666 struct compunit_symtab *stab_best = NULL;
5667 struct dwarf2_per_cu_data *per_cu;
5668 while ((per_cu = iter.next ()) != NULL)
5669 {
5670 struct symbol *sym, *with_opaque = NULL;
5671 compunit_symtab *stab
5672 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5673 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5674 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5675
5676 sym = block_find_symbol (block, name, domain,
5677 block_find_non_opaque_type_preferred,
5678 &with_opaque);
5679
5680 /* Some caution must be observed with overloaded functions and
5681 methods, since the index will not contain any overload
5682 information (but NAME might contain it). */
5683
5684 if (sym != NULL
5685 && strcmp_iw (sym->search_name (), name) == 0)
5686 return stab;
5687 if (with_opaque != NULL
5688 && strcmp_iw (with_opaque->search_name (), name) == 0)
5689 stab_best = stab;
5690
5691 /* Keep looking through other CUs. */
5692 }
5693
5694 return stab_best;
5695 }
5696
5697 /* This dumps minimal information about .debug_names. It is called
5698 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5699 uses this to verify that .debug_names has been loaded. */
5700
5701 static void
5702 dw2_debug_names_dump (struct objfile *objfile)
5703 {
5704 struct dwarf2_per_objfile *dwarf2_per_objfile
5705 = get_dwarf2_per_objfile (objfile);
5706
5707 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5708 printf_filtered (".debug_names:");
5709 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5710 printf_filtered (" exists\n");
5711 else
5712 printf_filtered (" faked for \"readnow\"\n");
5713 printf_filtered ("\n");
5714 }
5715
5716 static void
5717 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5718 const char *func_name)
5719 {
5720 struct dwarf2_per_objfile *dwarf2_per_objfile
5721 = get_dwarf2_per_objfile (objfile);
5722
5723 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5724 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5725 {
5726 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5727
5728 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5729
5730 struct dwarf2_per_cu_data *per_cu;
5731 while ((per_cu = iter.next ()) != NULL)
5732 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5733 }
5734 }
5735
5736 static void
5737 dw2_debug_names_map_matching_symbols
5738 (struct objfile *objfile,
5739 const lookup_name_info &name, domain_enum domain,
5740 int global,
5741 gdb::function_view<symbol_found_callback_ftype> callback,
5742 symbol_compare_ftype *ordered_compare)
5743 {
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
5746
5747 /* debug_names_table is NULL if OBJF_READNOW. */
5748 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5749 return;
5750
5751 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5752 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5753
5754 const char *match_name = name.ada ().lookup_name ().c_str ();
5755 auto matcher = [&] (const char *symname)
5756 {
5757 if (ordered_compare == nullptr)
5758 return true;
5759 return ordered_compare (symname, match_name) == 0;
5760 };
5761
5762 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5763 [&] (offset_type namei)
5764 {
5765 /* The name was matched, now expand corresponding CUs that were
5766 marked. */
5767 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5768
5769 struct dwarf2_per_cu_data *per_cu;
5770 while ((per_cu = iter.next ()) != NULL)
5771 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5772 nullptr);
5773 return true;
5774 });
5775
5776 /* It's a shame we couldn't do this inside the
5777 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5778 that have already been expanded. Instead, this loop matches what
5779 the psymtab code does. */
5780 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5781 {
5782 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5783 if (symtab != nullptr)
5784 {
5785 const struct block *block
5786 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5787 if (!iterate_over_symbols_terminated (block, name,
5788 domain, callback))
5789 break;
5790 }
5791 }
5792 }
5793
5794 static void
5795 dw2_debug_names_expand_symtabs_matching
5796 (struct objfile *objfile,
5797 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5798 const lookup_name_info *lookup_name,
5799 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5800 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5801 enum search_domain kind)
5802 {
5803 struct dwarf2_per_objfile *dwarf2_per_objfile
5804 = get_dwarf2_per_objfile (objfile);
5805
5806 /* debug_names_table is NULL if OBJF_READNOW. */
5807 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5808 return;
5809
5810 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5811
5812 if (symbol_matcher == NULL && lookup_name == NULL)
5813 {
5814 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5815 {
5816 QUIT;
5817
5818 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5819 file_matcher, expansion_notify);
5820 }
5821 return;
5822 }
5823
5824 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5825
5826 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5827 symbol_matcher,
5828 kind, [&] (offset_type namei)
5829 {
5830 /* The name was matched, now expand corresponding CUs that were
5831 marked. */
5832 dw2_debug_names_iterator iter (map, kind, namei);
5833
5834 struct dwarf2_per_cu_data *per_cu;
5835 while ((per_cu = iter.next ()) != NULL)
5836 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5837 file_matcher, expansion_notify);
5838 return true;
5839 });
5840 }
5841
5842 const struct quick_symbol_functions dwarf2_debug_names_functions =
5843 {
5844 dw2_has_symbols,
5845 dw2_find_last_source_symtab,
5846 dw2_forget_cached_source_info,
5847 dw2_map_symtabs_matching_filename,
5848 dw2_debug_names_lookup_symbol,
5849 NULL,
5850 dw2_print_stats,
5851 dw2_debug_names_dump,
5852 dw2_debug_names_expand_symtabs_for_function,
5853 dw2_expand_all_symtabs,
5854 dw2_expand_symtabs_with_fullname,
5855 dw2_debug_names_map_matching_symbols,
5856 dw2_debug_names_expand_symtabs_matching,
5857 dw2_find_pc_sect_compunit_symtab,
5858 NULL,
5859 dw2_map_symbol_filenames
5860 };
5861
5862 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5863 to either a dwarf2_per_bfd or dwz_file object. */
5864
5865 template <typename T>
5866 static gdb::array_view<const gdb_byte>
5867 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5868 {
5869 dwarf2_section_info *section = &section_owner->gdb_index;
5870
5871 if (section->empty ())
5872 return {};
5873
5874 /* Older elfutils strip versions could keep the section in the main
5875 executable while splitting it for the separate debug info file. */
5876 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5877 return {};
5878
5879 section->read (obj);
5880
5881 /* dwarf2_section_info::size is a bfd_size_type, while
5882 gdb::array_view works with size_t. On 32-bit hosts, with
5883 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5884 is 32-bit. So we need an explicit narrowing conversion here.
5885 This is fine, because it's impossible to allocate or mmap an
5886 array/buffer larger than what size_t can represent. */
5887 return gdb::make_array_view (section->buffer, section->size);
5888 }
5889
5890 /* Lookup the index cache for the contents of the index associated to
5891 DWARF2_OBJ. */
5892
5893 static gdb::array_view<const gdb_byte>
5894 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5895 {
5896 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5897 if (build_id == nullptr)
5898 return {};
5899
5900 return global_index_cache.lookup_gdb_index (build_id,
5901 &dwarf2_per_bfd->index_cache_res);
5902 }
5903
5904 /* Same as the above, but for DWZ. */
5905
5906 static gdb::array_view<const gdb_byte>
5907 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5908 {
5909 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5910 if (build_id == nullptr)
5911 return {};
5912
5913 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5914 }
5915
5916 /* See symfile.h. */
5917
5918 bool
5919 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5920 {
5921 struct dwarf2_per_objfile *dwarf2_per_objfile
5922 = get_dwarf2_per_objfile (objfile);
5923
5924 /* If we're about to read full symbols, don't bother with the
5925 indices. In this case we also don't care if some other debug
5926 format is making psymtabs, because they are all about to be
5927 expanded anyway. */
5928 if ((objfile->flags & OBJF_READNOW))
5929 {
5930 dwarf2_per_objfile->per_bfd->using_index = 1;
5931 create_all_comp_units (dwarf2_per_objfile);
5932 create_all_type_units (dwarf2_per_objfile);
5933 dwarf2_per_objfile->per_bfd->quick_file_names_table
5934 = create_quick_file_names_table
5935 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5936 dwarf2_per_objfile->resize_symtabs ();
5937
5938 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5939 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5940 {
5941 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5942
5943 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5944 struct dwarf2_per_cu_quick_data);
5945 }
5946
5947 /* Return 1 so that gdb sees the "quick" functions. However,
5948 these functions will be no-ops because we will have expanded
5949 all symtabs. */
5950 *index_kind = dw_index_kind::GDB_INDEX;
5951 return true;
5952 }
5953
5954 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5955 {
5956 *index_kind = dw_index_kind::DEBUG_NAMES;
5957 dwarf2_per_objfile->resize_symtabs ();
5958 return true;
5959 }
5960
5961 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5962 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5963 get_gdb_index_contents_from_section<dwz_file>))
5964 {
5965 *index_kind = dw_index_kind::GDB_INDEX;
5966 dwarf2_per_objfile->resize_symtabs ();
5967 return true;
5968 }
5969
5970 /* ... otherwise, try to find the index in the index cache. */
5971 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5972 get_gdb_index_contents_from_cache,
5973 get_gdb_index_contents_from_cache_dwz))
5974 {
5975 global_index_cache.hit ();
5976 *index_kind = dw_index_kind::GDB_INDEX;
5977 dwarf2_per_objfile->resize_symtabs ();
5978 return true;
5979 }
5980
5981 global_index_cache.miss ();
5982 return false;
5983 }
5984
5985 \f
5986
5987 /* Build a partial symbol table. */
5988
5989 void
5990 dwarf2_build_psymtabs (struct objfile *objfile)
5991 {
5992 struct dwarf2_per_objfile *dwarf2_per_objfile
5993 = get_dwarf2_per_objfile (objfile);
5994
5995 init_psymbol_list (objfile, 1024);
5996
5997 try
5998 {
5999 /* This isn't really ideal: all the data we allocate on the
6000 objfile's obstack is still uselessly kept around. However,
6001 freeing it seems unsafe. */
6002 psymtab_discarder psymtabs (objfile);
6003 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6004 psymtabs.keep ();
6005
6006 dwarf2_per_objfile->resize_symtabs ();
6007
6008 /* (maybe) store an index in the cache. */
6009 global_index_cache.store (dwarf2_per_objfile);
6010 }
6011 catch (const gdb_exception_error &except)
6012 {
6013 exception_print (gdb_stderr, except);
6014 }
6015 }
6016
6017 /* Find the base address of the compilation unit for range lists and
6018 location lists. It will normally be specified by DW_AT_low_pc.
6019 In DWARF-3 draft 4, the base address could be overridden by
6020 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6021 compilation units with discontinuous ranges. */
6022
6023 static void
6024 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6025 {
6026 struct attribute *attr;
6027
6028 cu->base_address.reset ();
6029
6030 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6031 if (attr != nullptr)
6032 cu->base_address = attr->value_as_address ();
6033 else
6034 {
6035 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6036 if (attr != nullptr)
6037 cu->base_address = attr->value_as_address ();
6038 }
6039 }
6040
6041 /* Helper function that returns the proper abbrev section for
6042 THIS_CU. */
6043
6044 static struct dwarf2_section_info *
6045 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6046 {
6047 struct dwarf2_section_info *abbrev;
6048 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6049
6050 if (this_cu->is_dwz)
6051 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6052 else
6053 abbrev = &per_bfd->abbrev;
6054
6055 return abbrev;
6056 }
6057
6058 /* Fetch the abbreviation table offset from a comp or type unit header. */
6059
6060 static sect_offset
6061 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6062 struct dwarf2_section_info *section,
6063 sect_offset sect_off)
6064 {
6065 bfd *abfd = section->get_bfd_owner ();
6066 const gdb_byte *info_ptr;
6067 unsigned int initial_length_size, offset_size;
6068 uint16_t version;
6069
6070 section->read (dwarf2_per_objfile->objfile);
6071 info_ptr = section->buffer + to_underlying (sect_off);
6072 read_initial_length (abfd, info_ptr, &initial_length_size);
6073 offset_size = initial_length_size == 4 ? 4 : 8;
6074 info_ptr += initial_length_size;
6075
6076 version = read_2_bytes (abfd, info_ptr);
6077 info_ptr += 2;
6078 if (version >= 5)
6079 {
6080 /* Skip unit type and address size. */
6081 info_ptr += 2;
6082 }
6083
6084 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6085 }
6086
6087 /* A partial symtab that is used only for include files. */
6088 struct dwarf2_include_psymtab : public partial_symtab
6089 {
6090 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6091 : partial_symtab (filename, objfile)
6092 {
6093 }
6094
6095 void read_symtab (struct objfile *objfile) override
6096 {
6097 /* It's an include file, no symbols to read for it.
6098 Everything is in the includer symtab. */
6099
6100 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6101 expansion of the includer psymtab. We use the dependencies[0] field to
6102 model the includer. But if we go the regular route of calling
6103 expand_psymtab here, and having expand_psymtab call expand_dependencies
6104 to expand the includer, we'll only use expand_psymtab on the includer
6105 (making it a non-toplevel psymtab), while if we expand the includer via
6106 another path, we'll use read_symtab (making it a toplevel psymtab).
6107 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6108 psymtab, and trigger read_symtab on the includer here directly. */
6109 includer ()->read_symtab (objfile);
6110 }
6111
6112 void expand_psymtab (struct objfile *objfile) override
6113 {
6114 /* This is not called by read_symtab, and should not be called by any
6115 expand_dependencies. */
6116 gdb_assert (false);
6117 }
6118
6119 bool readin_p (struct objfile *objfile) const override
6120 {
6121 return includer ()->readin_p (objfile);
6122 }
6123
6124 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6125 {
6126 return nullptr;
6127 }
6128
6129 private:
6130 partial_symtab *includer () const
6131 {
6132 /* An include psymtab has exactly one dependency: the psymtab that
6133 includes it. */
6134 gdb_assert (this->number_of_dependencies == 1);
6135 return this->dependencies[0];
6136 }
6137 };
6138
6139 /* Allocate a new partial symtab for file named NAME and mark this new
6140 partial symtab as being an include of PST. */
6141
6142 static void
6143 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6144 struct objfile *objfile)
6145 {
6146 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6147
6148 if (!IS_ABSOLUTE_PATH (subpst->filename))
6149 subpst->dirname = pst->dirname;
6150
6151 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6152 subpst->dependencies[0] = pst;
6153 subpst->number_of_dependencies = 1;
6154 }
6155
6156 /* Read the Line Number Program data and extract the list of files
6157 included by the source file represented by PST. Build an include
6158 partial symtab for each of these included files. */
6159
6160 static void
6161 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6162 struct die_info *die,
6163 dwarf2_psymtab *pst)
6164 {
6165 line_header_up lh;
6166 struct attribute *attr;
6167
6168 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6169 if (attr != nullptr)
6170 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6171 if (lh == NULL)
6172 return; /* No linetable, so no includes. */
6173
6174 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6175 that we pass in the raw text_low here; that is ok because we're
6176 only decoding the line table to make include partial symtabs, and
6177 so the addresses aren't really used. */
6178 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6179 pst->raw_text_low (), 1);
6180 }
6181
6182 static hashval_t
6183 hash_signatured_type (const void *item)
6184 {
6185 const struct signatured_type *sig_type
6186 = (const struct signatured_type *) item;
6187
6188 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6189 return sig_type->signature;
6190 }
6191
6192 static int
6193 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6194 {
6195 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6196 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6197
6198 return lhs->signature == rhs->signature;
6199 }
6200
6201 /* Allocate a hash table for signatured types. */
6202
6203 static htab_up
6204 allocate_signatured_type_table ()
6205 {
6206 return htab_up (htab_create_alloc (41,
6207 hash_signatured_type,
6208 eq_signatured_type,
6209 NULL, xcalloc, xfree));
6210 }
6211
6212 /* A helper function to add a signatured type CU to a table. */
6213
6214 static int
6215 add_signatured_type_cu_to_table (void **slot, void *datum)
6216 {
6217 struct signatured_type *sigt = (struct signatured_type *) *slot;
6218 std::vector<signatured_type *> *all_type_units
6219 = (std::vector<signatured_type *> *) datum;
6220
6221 all_type_units->push_back (sigt);
6222
6223 return 1;
6224 }
6225
6226 /* A helper for create_debug_types_hash_table. Read types from SECTION
6227 and fill them into TYPES_HTAB. It will process only type units,
6228 therefore DW_UT_type. */
6229
6230 static void
6231 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6232 struct dwo_file *dwo_file,
6233 dwarf2_section_info *section, htab_up &types_htab,
6234 rcuh_kind section_kind)
6235 {
6236 struct objfile *objfile = dwarf2_per_objfile->objfile;
6237 struct dwarf2_section_info *abbrev_section;
6238 bfd *abfd;
6239 const gdb_byte *info_ptr, *end_ptr;
6240
6241 abbrev_section = (dwo_file != NULL
6242 ? &dwo_file->sections.abbrev
6243 : &dwarf2_per_objfile->per_bfd->abbrev);
6244
6245 if (dwarf_read_debug)
6246 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6247 section->get_name (),
6248 abbrev_section->get_file_name ());
6249
6250 section->read (objfile);
6251 info_ptr = section->buffer;
6252
6253 if (info_ptr == NULL)
6254 return;
6255
6256 /* We can't set abfd until now because the section may be empty or
6257 not present, in which case the bfd is unknown. */
6258 abfd = section->get_bfd_owner ();
6259
6260 /* We don't use cutu_reader here because we don't need to read
6261 any dies: the signature is in the header. */
6262
6263 end_ptr = info_ptr + section->size;
6264 while (info_ptr < end_ptr)
6265 {
6266 struct signatured_type *sig_type;
6267 struct dwo_unit *dwo_tu;
6268 void **slot;
6269 const gdb_byte *ptr = info_ptr;
6270 struct comp_unit_head header;
6271 unsigned int length;
6272
6273 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6274
6275 /* Initialize it due to a false compiler warning. */
6276 header.signature = -1;
6277 header.type_cu_offset_in_tu = (cu_offset) -1;
6278
6279 /* We need to read the type's signature in order to build the hash
6280 table, but we don't need anything else just yet. */
6281
6282 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6283 abbrev_section, ptr, section_kind);
6284
6285 length = header.get_length ();
6286
6287 /* Skip dummy type units. */
6288 if (ptr >= info_ptr + length
6289 || peek_abbrev_code (abfd, ptr) == 0
6290 || header.unit_type != DW_UT_type)
6291 {
6292 info_ptr += length;
6293 continue;
6294 }
6295
6296 if (types_htab == NULL)
6297 {
6298 if (dwo_file)
6299 types_htab = allocate_dwo_unit_table ();
6300 else
6301 types_htab = allocate_signatured_type_table ();
6302 }
6303
6304 if (dwo_file)
6305 {
6306 sig_type = NULL;
6307 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6308 struct dwo_unit);
6309 dwo_tu->dwo_file = dwo_file;
6310 dwo_tu->signature = header.signature;
6311 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6312 dwo_tu->section = section;
6313 dwo_tu->sect_off = sect_off;
6314 dwo_tu->length = length;
6315 }
6316 else
6317 {
6318 /* N.B.: type_offset is not usable if this type uses a DWO file.
6319 The real type_offset is in the DWO file. */
6320 dwo_tu = NULL;
6321 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6322 sig_type->signature = header.signature;
6323 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6324 sig_type->per_cu.is_debug_types = 1;
6325 sig_type->per_cu.section = section;
6326 sig_type->per_cu.sect_off = sect_off;
6327 sig_type->per_cu.length = length;
6328 }
6329
6330 slot = htab_find_slot (types_htab.get (),
6331 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6332 INSERT);
6333 gdb_assert (slot != NULL);
6334 if (*slot != NULL)
6335 {
6336 sect_offset dup_sect_off;
6337
6338 if (dwo_file)
6339 {
6340 const struct dwo_unit *dup_tu
6341 = (const struct dwo_unit *) *slot;
6342
6343 dup_sect_off = dup_tu->sect_off;
6344 }
6345 else
6346 {
6347 const struct signatured_type *dup_tu
6348 = (const struct signatured_type *) *slot;
6349
6350 dup_sect_off = dup_tu->per_cu.sect_off;
6351 }
6352
6353 complaint (_("debug type entry at offset %s is duplicate to"
6354 " the entry at offset %s, signature %s"),
6355 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6356 hex_string (header.signature));
6357 }
6358 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6359
6360 if (dwarf_read_debug > 1)
6361 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6362 sect_offset_str (sect_off),
6363 hex_string (header.signature));
6364
6365 info_ptr += length;
6366 }
6367 }
6368
6369 /* Create the hash table of all entries in the .debug_types
6370 (or .debug_types.dwo) section(s).
6371 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6372 otherwise it is NULL.
6373
6374 The result is a pointer to the hash table or NULL if there are no types.
6375
6376 Note: This function processes DWO files only, not DWP files. */
6377
6378 static void
6379 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6380 struct dwo_file *dwo_file,
6381 gdb::array_view<dwarf2_section_info> type_sections,
6382 htab_up &types_htab)
6383 {
6384 for (dwarf2_section_info &section : type_sections)
6385 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6386 types_htab, rcuh_kind::TYPE);
6387 }
6388
6389 /* Create the hash table of all entries in the .debug_types section,
6390 and initialize all_type_units.
6391 The result is zero if there is an error (e.g. missing .debug_types section),
6392 otherwise non-zero. */
6393
6394 static int
6395 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6396 {
6397 htab_up types_htab;
6398
6399 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6400 &dwarf2_per_objfile->per_bfd->info, types_htab,
6401 rcuh_kind::COMPILE);
6402 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6403 dwarf2_per_objfile->per_bfd->types, types_htab);
6404 if (types_htab == NULL)
6405 {
6406 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6407 return 0;
6408 }
6409
6410 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6411
6412 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6413 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6414 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6415
6416 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6417 add_signatured_type_cu_to_table,
6418 &dwarf2_per_objfile->per_bfd->all_type_units);
6419
6420 return 1;
6421 }
6422
6423 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6424 If SLOT is non-NULL, it is the entry to use in the hash table.
6425 Otherwise we find one. */
6426
6427 static struct signatured_type *
6428 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6429 void **slot)
6430 {
6431 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6432 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6433 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6434
6435 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6436
6437 dwarf2_per_objfile->resize_symtabs ();
6438
6439 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6440 sig_type->signature = sig;
6441 sig_type->per_cu.is_debug_types = 1;
6442 if (dwarf2_per_objfile->per_bfd->using_index)
6443 {
6444 sig_type->per_cu.v.quick =
6445 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6446 struct dwarf2_per_cu_quick_data);
6447 }
6448
6449 if (slot == NULL)
6450 {
6451 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6452 sig_type, INSERT);
6453 }
6454 gdb_assert (*slot == NULL);
6455 *slot = sig_type;
6456 /* The rest of sig_type must be filled in by the caller. */
6457 return sig_type;
6458 }
6459
6460 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6461 Fill in SIG_ENTRY with DWO_ENTRY. */
6462
6463 static void
6464 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6465 struct signatured_type *sig_entry,
6466 struct dwo_unit *dwo_entry)
6467 {
6468 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6469
6470 /* Make sure we're not clobbering something we don't expect to. */
6471 gdb_assert (! sig_entry->per_cu.queued);
6472 gdb_assert (sig_entry->per_cu.cu == NULL);
6473 if (per_bfd->using_index)
6474 {
6475 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6476 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6477 }
6478 else
6479 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6480 gdb_assert (sig_entry->signature == dwo_entry->signature);
6481 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6482 gdb_assert (sig_entry->type_unit_group == NULL);
6483 gdb_assert (sig_entry->dwo_unit == NULL);
6484
6485 sig_entry->per_cu.section = dwo_entry->section;
6486 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6487 sig_entry->per_cu.length = dwo_entry->length;
6488 sig_entry->per_cu.reading_dwo_directly = 1;
6489 sig_entry->per_cu.per_bfd = per_bfd;
6490 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6491 sig_entry->dwo_unit = dwo_entry;
6492 }
6493
6494 /* Subroutine of lookup_signatured_type.
6495 If we haven't read the TU yet, create the signatured_type data structure
6496 for a TU to be read in directly from a DWO file, bypassing the stub.
6497 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6498 using .gdb_index, then when reading a CU we want to stay in the DWO file
6499 containing that CU. Otherwise we could end up reading several other DWO
6500 files (due to comdat folding) to process the transitive closure of all the
6501 mentioned TUs, and that can be slow. The current DWO file will have every
6502 type signature that it needs.
6503 We only do this for .gdb_index because in the psymtab case we already have
6504 to read all the DWOs to build the type unit groups. */
6505
6506 static struct signatured_type *
6507 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6508 {
6509 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6510 struct dwo_file *dwo_file;
6511 struct dwo_unit find_dwo_entry, *dwo_entry;
6512 struct signatured_type find_sig_entry, *sig_entry;
6513 void **slot;
6514
6515 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6516
6517 /* If TU skeletons have been removed then we may not have read in any
6518 TUs yet. */
6519 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6520 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6521
6522 /* We only ever need to read in one copy of a signatured type.
6523 Use the global signatured_types array to do our own comdat-folding
6524 of types. If this is the first time we're reading this TU, and
6525 the TU has an entry in .gdb_index, replace the recorded data from
6526 .gdb_index with this TU. */
6527
6528 find_sig_entry.signature = sig;
6529 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6530 &find_sig_entry, INSERT);
6531 sig_entry = (struct signatured_type *) *slot;
6532
6533 /* We can get here with the TU already read, *or* in the process of being
6534 read. Don't reassign the global entry to point to this DWO if that's
6535 the case. Also note that if the TU is already being read, it may not
6536 have come from a DWO, the program may be a mix of Fission-compiled
6537 code and non-Fission-compiled code. */
6538
6539 /* Have we already tried to read this TU?
6540 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6541 needn't exist in the global table yet). */
6542 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6543 return sig_entry;
6544
6545 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6546 dwo_unit of the TU itself. */
6547 dwo_file = cu->dwo_unit->dwo_file;
6548
6549 /* Ok, this is the first time we're reading this TU. */
6550 if (dwo_file->tus == NULL)
6551 return NULL;
6552 find_dwo_entry.signature = sig;
6553 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6554 &find_dwo_entry);
6555 if (dwo_entry == NULL)
6556 return NULL;
6557
6558 /* If the global table doesn't have an entry for this TU, add one. */
6559 if (sig_entry == NULL)
6560 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6561
6562 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6563 sig_entry->per_cu.tu_read = 1;
6564 return sig_entry;
6565 }
6566
6567 /* Subroutine of lookup_signatured_type.
6568 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6569 then try the DWP file. If the TU stub (skeleton) has been removed then
6570 it won't be in .gdb_index. */
6571
6572 static struct signatured_type *
6573 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6574 {
6575 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6576 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6577 struct dwo_unit *dwo_entry;
6578 struct signatured_type find_sig_entry, *sig_entry;
6579 void **slot;
6580
6581 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6582 gdb_assert (dwp_file != NULL);
6583
6584 /* If TU skeletons have been removed then we may not have read in any
6585 TUs yet. */
6586 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6587 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6588
6589 find_sig_entry.signature = sig;
6590 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6591 &find_sig_entry, INSERT);
6592 sig_entry = (struct signatured_type *) *slot;
6593
6594 /* Have we already tried to read this TU?
6595 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6596 needn't exist in the global table yet). */
6597 if (sig_entry != NULL)
6598 return sig_entry;
6599
6600 if (dwp_file->tus == NULL)
6601 return NULL;
6602 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6603 sig, 1 /* is_debug_types */);
6604 if (dwo_entry == NULL)
6605 return NULL;
6606
6607 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6608 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6609
6610 return sig_entry;
6611 }
6612
6613 /* Lookup a signature based type for DW_FORM_ref_sig8.
6614 Returns NULL if signature SIG is not present in the table.
6615 It is up to the caller to complain about this. */
6616
6617 static struct signatured_type *
6618 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6619 {
6620 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6621
6622 if (cu->dwo_unit
6623 && dwarf2_per_objfile->per_bfd->using_index)
6624 {
6625 /* We're in a DWO/DWP file, and we're using .gdb_index.
6626 These cases require special processing. */
6627 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6628 return lookup_dwo_signatured_type (cu, sig);
6629 else
6630 return lookup_dwp_signatured_type (cu, sig);
6631 }
6632 else
6633 {
6634 struct signatured_type find_entry, *entry;
6635
6636 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6637 return NULL;
6638 find_entry.signature = sig;
6639 entry = ((struct signatured_type *)
6640 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6641 &find_entry));
6642 return entry;
6643 }
6644 }
6645
6646 /* Low level DIE reading support. */
6647
6648 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6649
6650 static void
6651 init_cu_die_reader (struct die_reader_specs *reader,
6652 struct dwarf2_cu *cu,
6653 struct dwarf2_section_info *section,
6654 struct dwo_file *dwo_file,
6655 struct abbrev_table *abbrev_table)
6656 {
6657 gdb_assert (section->readin && section->buffer != NULL);
6658 reader->abfd = section->get_bfd_owner ();
6659 reader->cu = cu;
6660 reader->dwo_file = dwo_file;
6661 reader->die_section = section;
6662 reader->buffer = section->buffer;
6663 reader->buffer_end = section->buffer + section->size;
6664 reader->abbrev_table = abbrev_table;
6665 }
6666
6667 /* Subroutine of cutu_reader to simplify it.
6668 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6669 There's just a lot of work to do, and cutu_reader is big enough
6670 already.
6671
6672 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6673 from it to the DIE in the DWO. If NULL we are skipping the stub.
6674 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6675 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6676 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6677 STUB_COMP_DIR may be non-NULL.
6678 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6679 are filled in with the info of the DIE from the DWO file.
6680 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6681 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6682 kept around for at least as long as *RESULT_READER.
6683
6684 The result is non-zero if a valid (non-dummy) DIE was found. */
6685
6686 static int
6687 read_cutu_die_from_dwo (dwarf2_cu *cu,
6688 struct dwo_unit *dwo_unit,
6689 struct die_info *stub_comp_unit_die,
6690 const char *stub_comp_dir,
6691 struct die_reader_specs *result_reader,
6692 const gdb_byte **result_info_ptr,
6693 struct die_info **result_comp_unit_die,
6694 abbrev_table_up *result_dwo_abbrev_table)
6695 {
6696 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6697 dwarf2_per_cu_data *per_cu = cu->per_cu;
6698 struct objfile *objfile = dwarf2_per_objfile->objfile;
6699 bfd *abfd;
6700 const gdb_byte *begin_info_ptr, *info_ptr;
6701 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6702 int i,num_extra_attrs;
6703 struct dwarf2_section_info *dwo_abbrev_section;
6704 struct die_info *comp_unit_die;
6705
6706 /* At most one of these may be provided. */
6707 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6708
6709 /* These attributes aren't processed until later:
6710 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6711 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6712 referenced later. However, these attributes are found in the stub
6713 which we won't have later. In order to not impose this complication
6714 on the rest of the code, we read them here and copy them to the
6715 DWO CU/TU die. */
6716
6717 stmt_list = NULL;
6718 low_pc = NULL;
6719 high_pc = NULL;
6720 ranges = NULL;
6721 comp_dir = NULL;
6722
6723 if (stub_comp_unit_die != NULL)
6724 {
6725 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6726 DWO file. */
6727 if (!per_cu->is_debug_types)
6728 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6729 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6730 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6731 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6732 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6733
6734 cu->addr_base = stub_comp_unit_die->addr_base ();
6735
6736 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6737 here (if needed). We need the value before we can process
6738 DW_AT_ranges. */
6739 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6740 }
6741 else if (stub_comp_dir != NULL)
6742 {
6743 /* Reconstruct the comp_dir attribute to simplify the code below. */
6744 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6745 comp_dir->name = DW_AT_comp_dir;
6746 comp_dir->form = DW_FORM_string;
6747 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6748 DW_STRING (comp_dir) = stub_comp_dir;
6749 }
6750
6751 /* Set up for reading the DWO CU/TU. */
6752 cu->dwo_unit = dwo_unit;
6753 dwarf2_section_info *section = dwo_unit->section;
6754 section->read (objfile);
6755 abfd = section->get_bfd_owner ();
6756 begin_info_ptr = info_ptr = (section->buffer
6757 + to_underlying (dwo_unit->sect_off));
6758 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6759
6760 if (per_cu->is_debug_types)
6761 {
6762 signatured_type *sig_type = (struct signatured_type *) per_cu;
6763
6764 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6765 &cu->header, section,
6766 dwo_abbrev_section,
6767 info_ptr, rcuh_kind::TYPE);
6768 /* This is not an assert because it can be caused by bad debug info. */
6769 if (sig_type->signature != cu->header.signature)
6770 {
6771 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6772 " TU at offset %s [in module %s]"),
6773 hex_string (sig_type->signature),
6774 hex_string (cu->header.signature),
6775 sect_offset_str (dwo_unit->sect_off),
6776 bfd_get_filename (abfd));
6777 }
6778 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6779 /* For DWOs coming from DWP files, we don't know the CU length
6780 nor the type's offset in the TU until now. */
6781 dwo_unit->length = cu->header.get_length ();
6782 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6783
6784 /* Establish the type offset that can be used to lookup the type.
6785 For DWO files, we don't know it until now. */
6786 sig_type->type_offset_in_section
6787 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6788 }
6789 else
6790 {
6791 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6792 &cu->header, section,
6793 dwo_abbrev_section,
6794 info_ptr, rcuh_kind::COMPILE);
6795 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6796 /* For DWOs coming from DWP files, we don't know the CU length
6797 until now. */
6798 dwo_unit->length = cu->header.get_length ();
6799 }
6800
6801 *result_dwo_abbrev_table
6802 = abbrev_table::read (objfile, dwo_abbrev_section,
6803 cu->header.abbrev_sect_off);
6804 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6805 result_dwo_abbrev_table->get ());
6806
6807 /* Read in the die, but leave space to copy over the attributes
6808 from the stub. This has the benefit of simplifying the rest of
6809 the code - all the work to maintain the illusion of a single
6810 DW_TAG_{compile,type}_unit DIE is done here. */
6811 num_extra_attrs = ((stmt_list != NULL)
6812 + (low_pc != NULL)
6813 + (high_pc != NULL)
6814 + (ranges != NULL)
6815 + (comp_dir != NULL));
6816 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6817 num_extra_attrs);
6818
6819 /* Copy over the attributes from the stub to the DIE we just read in. */
6820 comp_unit_die = *result_comp_unit_die;
6821 i = comp_unit_die->num_attrs;
6822 if (stmt_list != NULL)
6823 comp_unit_die->attrs[i++] = *stmt_list;
6824 if (low_pc != NULL)
6825 comp_unit_die->attrs[i++] = *low_pc;
6826 if (high_pc != NULL)
6827 comp_unit_die->attrs[i++] = *high_pc;
6828 if (ranges != NULL)
6829 comp_unit_die->attrs[i++] = *ranges;
6830 if (comp_dir != NULL)
6831 comp_unit_die->attrs[i++] = *comp_dir;
6832 comp_unit_die->num_attrs += num_extra_attrs;
6833
6834 if (dwarf_die_debug)
6835 {
6836 fprintf_unfiltered (gdb_stdlog,
6837 "Read die from %s@0x%x of %s:\n",
6838 section->get_name (),
6839 (unsigned) (begin_info_ptr - section->buffer),
6840 bfd_get_filename (abfd));
6841 dump_die (comp_unit_die, dwarf_die_debug);
6842 }
6843
6844 /* Skip dummy compilation units. */
6845 if (info_ptr >= begin_info_ptr + dwo_unit->length
6846 || peek_abbrev_code (abfd, info_ptr) == 0)
6847 return 0;
6848
6849 *result_info_ptr = info_ptr;
6850 return 1;
6851 }
6852
6853 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6854 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6855 signature is part of the header. */
6856 static gdb::optional<ULONGEST>
6857 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6858 {
6859 if (cu->header.version >= 5)
6860 return cu->header.signature;
6861 struct attribute *attr;
6862 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6863 if (attr == nullptr)
6864 return gdb::optional<ULONGEST> ();
6865 return DW_UNSND (attr);
6866 }
6867
6868 /* Subroutine of cutu_reader to simplify it.
6869 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6870 Returns NULL if the specified DWO unit cannot be found. */
6871
6872 static struct dwo_unit *
6873 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6874 {
6875 dwarf2_per_cu_data *per_cu = cu->per_cu;
6876 struct dwo_unit *dwo_unit;
6877 const char *comp_dir;
6878
6879 gdb_assert (cu != NULL);
6880
6881 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6882 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6883 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6884
6885 if (per_cu->is_debug_types)
6886 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6887 else
6888 {
6889 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6890
6891 if (!signature.has_value ())
6892 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6893 " [in module %s]"),
6894 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6895
6896 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6897 }
6898
6899 return dwo_unit;
6900 }
6901
6902 /* Subroutine of cutu_reader to simplify it.
6903 See it for a description of the parameters.
6904 Read a TU directly from a DWO file, bypassing the stub. */
6905
6906 void
6907 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6908 dwarf2_per_objfile *per_objfile,
6909 int use_existing_cu)
6910 {
6911 struct signatured_type *sig_type;
6912
6913 /* Verify we can do the following downcast, and that we have the
6914 data we need. */
6915 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6916 sig_type = (struct signatured_type *) this_cu;
6917 gdb_assert (sig_type->dwo_unit != NULL);
6918
6919 if (use_existing_cu && this_cu->cu != NULL)
6920 {
6921 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6922 /* There's no need to do the rereading_dwo_cu handling that
6923 cutu_reader does since we don't read the stub. */
6924 }
6925 else
6926 {
6927 /* If !use_existing_cu, this_cu->cu must be NULL. */
6928 gdb_assert (this_cu->cu == NULL);
6929 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6930 }
6931
6932 /* A future optimization, if needed, would be to use an existing
6933 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6934 could share abbrev tables. */
6935
6936 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
6937 NULL /* stub_comp_unit_die */,
6938 sig_type->dwo_unit->dwo_file->comp_dir,
6939 this, &info_ptr,
6940 &comp_unit_die,
6941 &m_dwo_abbrev_table) == 0)
6942 {
6943 /* Dummy die. */
6944 dummy_p = true;
6945 }
6946 }
6947
6948 /* Initialize a CU (or TU) and read its DIEs.
6949 If the CU defers to a DWO file, read the DWO file as well.
6950
6951 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6952 Otherwise the table specified in the comp unit header is read in and used.
6953 This is an optimization for when we already have the abbrev table.
6954
6955 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6956 Otherwise, a new CU is allocated with xmalloc. */
6957
6958 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6959 dwarf2_per_objfile *dwarf2_per_objfile,
6960 struct abbrev_table *abbrev_table,
6961 int use_existing_cu,
6962 bool skip_partial)
6963 : die_reader_specs {},
6964 m_this_cu (this_cu)
6965 {
6966 struct objfile *objfile = dwarf2_per_objfile->objfile;
6967 struct dwarf2_section_info *section = this_cu->section;
6968 bfd *abfd = section->get_bfd_owner ();
6969 struct dwarf2_cu *cu;
6970 const gdb_byte *begin_info_ptr;
6971 struct signatured_type *sig_type = NULL;
6972 struct dwarf2_section_info *abbrev_section;
6973 /* Non-zero if CU currently points to a DWO file and we need to
6974 reread it. When this happens we need to reread the skeleton die
6975 before we can reread the DWO file (this only applies to CUs, not TUs). */
6976 int rereading_dwo_cu = 0;
6977
6978 if (dwarf_die_debug)
6979 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6980 this_cu->is_debug_types ? "type" : "comp",
6981 sect_offset_str (this_cu->sect_off));
6982
6983 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6984 file (instead of going through the stub), short-circuit all of this. */
6985 if (this_cu->reading_dwo_directly)
6986 {
6987 /* Narrow down the scope of possibilities to have to understand. */
6988 gdb_assert (this_cu->is_debug_types);
6989 gdb_assert (abbrev_table == NULL);
6990 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
6991 return;
6992 }
6993
6994 /* This is cheap if the section is already read in. */
6995 section->read (objfile);
6996
6997 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6998
6999 abbrev_section = get_abbrev_section_for_cu (this_cu);
7000
7001 if (use_existing_cu && this_cu->cu != NULL)
7002 {
7003 cu = this_cu->cu;
7004 /* If this CU is from a DWO file we need to start over, we need to
7005 refetch the attributes from the skeleton CU.
7006 This could be optimized by retrieving those attributes from when we
7007 were here the first time: the previous comp_unit_die was stored in
7008 comp_unit_obstack. But there's no data yet that we need this
7009 optimization. */
7010 if (cu->dwo_unit != NULL)
7011 rereading_dwo_cu = 1;
7012 }
7013 else
7014 {
7015 /* If !use_existing_cu, this_cu->cu must be NULL. */
7016 gdb_assert (this_cu->cu == NULL);
7017 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7018 cu = m_new_cu.get ();
7019 }
7020
7021 /* Get the header. */
7022 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7023 {
7024 /* We already have the header, there's no need to read it in again. */
7025 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7026 }
7027 else
7028 {
7029 if (this_cu->is_debug_types)
7030 {
7031 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7032 &cu->header, section,
7033 abbrev_section, info_ptr,
7034 rcuh_kind::TYPE);
7035
7036 /* Since per_cu is the first member of struct signatured_type,
7037 we can go from a pointer to one to a pointer to the other. */
7038 sig_type = (struct signatured_type *) this_cu;
7039 gdb_assert (sig_type->signature == cu->header.signature);
7040 gdb_assert (sig_type->type_offset_in_tu
7041 == cu->header.type_cu_offset_in_tu);
7042 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7043
7044 /* LENGTH has not been set yet for type units if we're
7045 using .gdb_index. */
7046 this_cu->length = cu->header.get_length ();
7047
7048 /* Establish the type offset that can be used to lookup the type. */
7049 sig_type->type_offset_in_section =
7050 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7051
7052 this_cu->dwarf_version = cu->header.version;
7053 }
7054 else
7055 {
7056 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7057 &cu->header, section,
7058 abbrev_section,
7059 info_ptr,
7060 rcuh_kind::COMPILE);
7061
7062 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7063 if (this_cu->length == 0)
7064 this_cu->length = cu->header.get_length ();
7065 else
7066 gdb_assert (this_cu->length == cu->header.get_length ());
7067 this_cu->dwarf_version = cu->header.version;
7068 }
7069 }
7070
7071 /* Skip dummy compilation units. */
7072 if (info_ptr >= begin_info_ptr + this_cu->length
7073 || peek_abbrev_code (abfd, info_ptr) == 0)
7074 {
7075 dummy_p = true;
7076 return;
7077 }
7078
7079 /* If we don't have them yet, read the abbrevs for this compilation unit.
7080 And if we need to read them now, make sure they're freed when we're
7081 done. */
7082 if (abbrev_table != NULL)
7083 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7084 else
7085 {
7086 m_abbrev_table_holder
7087 = abbrev_table::read (objfile, abbrev_section,
7088 cu->header.abbrev_sect_off);
7089 abbrev_table = m_abbrev_table_holder.get ();
7090 }
7091
7092 /* Read the top level CU/TU die. */
7093 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7094 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7095
7096 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7097 {
7098 dummy_p = true;
7099 return;
7100 }
7101
7102 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7103 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7104 table from the DWO file and pass the ownership over to us. It will be
7105 referenced from READER, so we must make sure to free it after we're done
7106 with READER.
7107
7108 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7109 DWO CU, that this test will fail (the attribute will not be present). */
7110 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7111 if (dwo_name != nullptr)
7112 {
7113 struct dwo_unit *dwo_unit;
7114 struct die_info *dwo_comp_unit_die;
7115
7116 if (comp_unit_die->has_children)
7117 {
7118 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7119 " has children (offset %s) [in module %s]"),
7120 sect_offset_str (this_cu->sect_off),
7121 bfd_get_filename (abfd));
7122 }
7123 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7124 if (dwo_unit != NULL)
7125 {
7126 if (read_cutu_die_from_dwo (cu, dwo_unit,
7127 comp_unit_die, NULL,
7128 this, &info_ptr,
7129 &dwo_comp_unit_die,
7130 &m_dwo_abbrev_table) == 0)
7131 {
7132 /* Dummy die. */
7133 dummy_p = true;
7134 return;
7135 }
7136 comp_unit_die = dwo_comp_unit_die;
7137 }
7138 else
7139 {
7140 /* Yikes, we couldn't find the rest of the DIE, we only have
7141 the stub. A complaint has already been logged. There's
7142 not much more we can do except pass on the stub DIE to
7143 die_reader_func. We don't want to throw an error on bad
7144 debug info. */
7145 }
7146 }
7147 }
7148
7149 void
7150 cutu_reader::keep ()
7151 {
7152 /* Done, clean up. */
7153 gdb_assert (!dummy_p);
7154 if (m_new_cu != NULL)
7155 {
7156 /* We know that m_this_cu->cu is set, since we are in the process of
7157 parsing the CU. */
7158 gdb_assert (m_this_cu->cu != nullptr);
7159 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7160
7161 /* Link this CU into read_in_chain. */
7162 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7163 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7164 /* The chain owns it now. */
7165 m_new_cu.release ();
7166 }
7167 }
7168
7169 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7170 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7171 assumed to have already done the lookup to find the DWO file).
7172
7173 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7174 THIS_CU->is_debug_types, but nothing else.
7175
7176 We fill in THIS_CU->length.
7177
7178 THIS_CU->cu is always freed when done.
7179 This is done in order to not leave THIS_CU->cu in a state where we have
7180 to care whether it refers to the "main" CU or the DWO CU.
7181
7182 When parent_cu is passed, it is used to provide a default value for
7183 str_offsets_base and addr_base from the parent. */
7184
7185 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7186 dwarf2_per_objfile *dwarf2_per_objfile,
7187 struct dwarf2_cu *parent_cu,
7188 struct dwo_file *dwo_file)
7189 : die_reader_specs {},
7190 m_this_cu (this_cu)
7191 {
7192 struct objfile *objfile = dwarf2_per_objfile->objfile;
7193 struct dwarf2_section_info *section = this_cu->section;
7194 bfd *abfd = section->get_bfd_owner ();
7195 struct dwarf2_section_info *abbrev_section;
7196 const gdb_byte *begin_info_ptr, *info_ptr;
7197
7198 if (dwarf_die_debug)
7199 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7200 this_cu->is_debug_types ? "type" : "comp",
7201 sect_offset_str (this_cu->sect_off));
7202
7203 gdb_assert (this_cu->cu == NULL);
7204
7205 abbrev_section = (dwo_file != NULL
7206 ? &dwo_file->sections.abbrev
7207 : get_abbrev_section_for_cu (this_cu));
7208
7209 /* This is cheap if the section is already read in. */
7210 section->read (objfile);
7211
7212 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7213
7214 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7215 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7216 &m_new_cu->header, section,
7217 abbrev_section, info_ptr,
7218 (this_cu->is_debug_types
7219 ? rcuh_kind::TYPE
7220 : rcuh_kind::COMPILE));
7221
7222 if (parent_cu != nullptr)
7223 {
7224 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7225 m_new_cu->addr_base = parent_cu->addr_base;
7226 }
7227 this_cu->length = m_new_cu->header.get_length ();
7228
7229 /* Skip dummy compilation units. */
7230 if (info_ptr >= begin_info_ptr + this_cu->length
7231 || peek_abbrev_code (abfd, info_ptr) == 0)
7232 {
7233 dummy_p = true;
7234 return;
7235 }
7236
7237 m_abbrev_table_holder
7238 = abbrev_table::read (objfile, abbrev_section,
7239 m_new_cu->header.abbrev_sect_off);
7240
7241 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7242 m_abbrev_table_holder.get ());
7243 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7244 }
7245
7246 \f
7247 /* Type Unit Groups.
7248
7249 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7250 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7251 so that all types coming from the same compilation (.o file) are grouped
7252 together. A future step could be to put the types in the same symtab as
7253 the CU the types ultimately came from. */
7254
7255 static hashval_t
7256 hash_type_unit_group (const void *item)
7257 {
7258 const struct type_unit_group *tu_group
7259 = (const struct type_unit_group *) item;
7260
7261 return hash_stmt_list_entry (&tu_group->hash);
7262 }
7263
7264 static int
7265 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7266 {
7267 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7268 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7269
7270 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7271 }
7272
7273 /* Allocate a hash table for type unit groups. */
7274
7275 static htab_up
7276 allocate_type_unit_groups_table ()
7277 {
7278 return htab_up (htab_create_alloc (3,
7279 hash_type_unit_group,
7280 eq_type_unit_group,
7281 NULL, xcalloc, xfree));
7282 }
7283
7284 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7285 partial symtabs. We combine several TUs per psymtab to not let the size
7286 of any one psymtab grow too big. */
7287 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7288 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7289
7290 /* Helper routine for get_type_unit_group.
7291 Create the type_unit_group object used to hold one or more TUs. */
7292
7293 static struct type_unit_group *
7294 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7295 {
7296 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7297 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7298 struct dwarf2_per_cu_data *per_cu;
7299 struct type_unit_group *tu_group;
7300
7301 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7302 struct type_unit_group);
7303 per_cu = &tu_group->per_cu;
7304 per_cu->per_bfd = per_bfd;
7305
7306 if (per_bfd->using_index)
7307 {
7308 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7309 struct dwarf2_per_cu_quick_data);
7310 }
7311 else
7312 {
7313 unsigned int line_offset = to_underlying (line_offset_struct);
7314 dwarf2_psymtab *pst;
7315 std::string name;
7316
7317 /* Give the symtab a useful name for debug purposes. */
7318 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7319 name = string_printf ("<type_units_%d>",
7320 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7321 else
7322 name = string_printf ("<type_units_at_0x%x>", line_offset);
7323
7324 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7325 pst->anonymous = true;
7326 }
7327
7328 tu_group->hash.dwo_unit = cu->dwo_unit;
7329 tu_group->hash.line_sect_off = line_offset_struct;
7330
7331 return tu_group;
7332 }
7333
7334 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7335 STMT_LIST is a DW_AT_stmt_list attribute. */
7336
7337 static struct type_unit_group *
7338 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7339 {
7340 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7341 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7342 struct type_unit_group *tu_group;
7343 void **slot;
7344 unsigned int line_offset;
7345 struct type_unit_group type_unit_group_for_lookup;
7346
7347 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7348 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7349
7350 /* Do we need to create a new group, or can we use an existing one? */
7351
7352 if (stmt_list)
7353 {
7354 line_offset = DW_UNSND (stmt_list);
7355 ++tu_stats->nr_symtab_sharers;
7356 }
7357 else
7358 {
7359 /* Ugh, no stmt_list. Rare, but we have to handle it.
7360 We can do various things here like create one group per TU or
7361 spread them over multiple groups to split up the expansion work.
7362 To avoid worst case scenarios (too many groups or too large groups)
7363 we, umm, group them in bunches. */
7364 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7365 | (tu_stats->nr_stmt_less_type_units
7366 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7367 ++tu_stats->nr_stmt_less_type_units;
7368 }
7369
7370 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7371 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7372 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7373 &type_unit_group_for_lookup, INSERT);
7374 if (*slot != NULL)
7375 {
7376 tu_group = (struct type_unit_group *) *slot;
7377 gdb_assert (tu_group != NULL);
7378 }
7379 else
7380 {
7381 sect_offset line_offset_struct = (sect_offset) line_offset;
7382 tu_group = create_type_unit_group (cu, line_offset_struct);
7383 *slot = tu_group;
7384 ++tu_stats->nr_symtabs;
7385 }
7386
7387 return tu_group;
7388 }
7389 \f
7390 /* Partial symbol tables. */
7391
7392 /* Create a psymtab named NAME and assign it to PER_CU.
7393
7394 The caller must fill in the following details:
7395 dirname, textlow, texthigh. */
7396
7397 static dwarf2_psymtab *
7398 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7399 dwarf2_per_objfile *per_objfile,
7400 const char *name)
7401 {
7402 struct objfile *objfile = per_objfile->objfile;
7403 dwarf2_psymtab *pst;
7404
7405 pst = new dwarf2_psymtab (name, objfile, per_cu);
7406
7407 pst->psymtabs_addrmap_supported = true;
7408
7409 /* This is the glue that links PST into GDB's symbol API. */
7410 per_cu->v.psymtab = pst;
7411
7412 return pst;
7413 }
7414
7415 /* DIE reader function for process_psymtab_comp_unit. */
7416
7417 static void
7418 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7419 const gdb_byte *info_ptr,
7420 struct die_info *comp_unit_die,
7421 enum language pretend_language)
7422 {
7423 struct dwarf2_cu *cu = reader->cu;
7424 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7425 struct objfile *objfile = per_objfile->objfile;
7426 struct gdbarch *gdbarch = objfile->arch ();
7427 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7428 CORE_ADDR baseaddr;
7429 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7430 dwarf2_psymtab *pst;
7431 enum pc_bounds_kind cu_bounds_kind;
7432 const char *filename;
7433
7434 gdb_assert (! per_cu->is_debug_types);
7435
7436 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7437
7438 /* Allocate a new partial symbol table structure. */
7439 gdb::unique_xmalloc_ptr<char> debug_filename;
7440 static const char artificial[] = "<artificial>";
7441 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7442 if (filename == NULL)
7443 filename = "";
7444 else if (strcmp (filename, artificial) == 0)
7445 {
7446 debug_filename.reset (concat (artificial, "@",
7447 sect_offset_str (per_cu->sect_off),
7448 (char *) NULL));
7449 filename = debug_filename.get ();
7450 }
7451
7452 pst = create_partial_symtab (per_cu, per_objfile, filename);
7453
7454 /* This must be done before calling dwarf2_build_include_psymtabs. */
7455 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7456
7457 baseaddr = objfile->text_section_offset ();
7458
7459 dwarf2_find_base_address (comp_unit_die, cu);
7460
7461 /* Possibly set the default values of LOWPC and HIGHPC from
7462 `DW_AT_ranges'. */
7463 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7464 &best_highpc, cu, pst);
7465 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7466 {
7467 CORE_ADDR low
7468 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7469 - baseaddr);
7470 CORE_ADDR high
7471 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7472 - baseaddr - 1);
7473 /* Store the contiguous range if it is not empty; it can be
7474 empty for CUs with no code. */
7475 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7476 low, high, pst);
7477 }
7478
7479 /* Check if comp unit has_children.
7480 If so, read the rest of the partial symbols from this comp unit.
7481 If not, there's no more debug_info for this comp unit. */
7482 if (comp_unit_die->has_children)
7483 {
7484 struct partial_die_info *first_die;
7485 CORE_ADDR lowpc, highpc;
7486
7487 lowpc = ((CORE_ADDR) -1);
7488 highpc = ((CORE_ADDR) 0);
7489
7490 first_die = load_partial_dies (reader, info_ptr, 1);
7491
7492 scan_partial_symbols (first_die, &lowpc, &highpc,
7493 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7494
7495 /* If we didn't find a lowpc, set it to highpc to avoid
7496 complaints from `maint check'. */
7497 if (lowpc == ((CORE_ADDR) -1))
7498 lowpc = highpc;
7499
7500 /* If the compilation unit didn't have an explicit address range,
7501 then use the information extracted from its child dies. */
7502 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7503 {
7504 best_lowpc = lowpc;
7505 best_highpc = highpc;
7506 }
7507 }
7508 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7509 best_lowpc + baseaddr)
7510 - baseaddr);
7511 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7512 best_highpc + baseaddr)
7513 - baseaddr);
7514
7515 end_psymtab_common (objfile, pst);
7516
7517 if (!cu->per_cu->imported_symtabs_empty ())
7518 {
7519 int i;
7520 int len = cu->per_cu->imported_symtabs_size ();
7521
7522 /* Fill in 'dependencies' here; we fill in 'users' in a
7523 post-pass. */
7524 pst->number_of_dependencies = len;
7525 pst->dependencies
7526 = objfile->partial_symtabs->allocate_dependencies (len);
7527 for (i = 0; i < len; ++i)
7528 {
7529 pst->dependencies[i]
7530 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7531 }
7532
7533 cu->per_cu->imported_symtabs_free ();
7534 }
7535
7536 /* Get the list of files included in the current compilation unit,
7537 and build a psymtab for each of them. */
7538 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7539
7540 if (dwarf_read_debug)
7541 fprintf_unfiltered (gdb_stdlog,
7542 "Psymtab for %s unit @%s: %s - %s"
7543 ", %d global, %d static syms\n",
7544 per_cu->is_debug_types ? "type" : "comp",
7545 sect_offset_str (per_cu->sect_off),
7546 paddress (gdbarch, pst->text_low (objfile)),
7547 paddress (gdbarch, pst->text_high (objfile)),
7548 pst->n_global_syms, pst->n_static_syms);
7549 }
7550
7551 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7552 Process compilation unit THIS_CU for a psymtab. */
7553
7554 static void
7555 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7556 dwarf2_per_objfile *per_objfile,
7557 bool want_partial_unit,
7558 enum language pretend_language)
7559 {
7560 /* If this compilation unit was already read in, free the
7561 cached copy in order to read it in again. This is
7562 necessary because we skipped some symbols when we first
7563 read in the compilation unit (see load_partial_dies).
7564 This problem could be avoided, but the benefit is unclear. */
7565 if (this_cu->cu != NULL)
7566 free_one_cached_comp_unit (this_cu, per_objfile);
7567
7568 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7569
7570 switch (reader.comp_unit_die->tag)
7571 {
7572 case DW_TAG_compile_unit:
7573 this_cu->unit_type = DW_UT_compile;
7574 break;
7575 case DW_TAG_partial_unit:
7576 this_cu->unit_type = DW_UT_partial;
7577 break;
7578 default:
7579 abort ();
7580 }
7581
7582 if (reader.dummy_p)
7583 {
7584 /* Nothing. */
7585 }
7586 else if (this_cu->is_debug_types)
7587 build_type_psymtabs_reader (&reader, reader.info_ptr,
7588 reader.comp_unit_die);
7589 else if (want_partial_unit
7590 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7591 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7592 reader.comp_unit_die,
7593 pretend_language);
7594
7595 this_cu->lang = this_cu->cu->language;
7596
7597 /* Age out any secondary CUs. */
7598 age_cached_comp_units (per_objfile);
7599 }
7600
7601 /* Reader function for build_type_psymtabs. */
7602
7603 static void
7604 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7605 const gdb_byte *info_ptr,
7606 struct die_info *type_unit_die)
7607 {
7608 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7609 struct objfile *objfile = dwarf2_per_objfile->objfile;
7610 struct dwarf2_cu *cu = reader->cu;
7611 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7612 struct signatured_type *sig_type;
7613 struct type_unit_group *tu_group;
7614 struct attribute *attr;
7615 struct partial_die_info *first_die;
7616 CORE_ADDR lowpc, highpc;
7617 dwarf2_psymtab *pst;
7618
7619 gdb_assert (per_cu->is_debug_types);
7620 sig_type = (struct signatured_type *) per_cu;
7621
7622 if (! type_unit_die->has_children)
7623 return;
7624
7625 attr = type_unit_die->attr (DW_AT_stmt_list);
7626 tu_group = get_type_unit_group (cu, attr);
7627
7628 if (tu_group->tus == nullptr)
7629 tu_group->tus = new std::vector<signatured_type *>;
7630 tu_group->tus->push_back (sig_type);
7631
7632 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7633 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7634 pst->anonymous = true;
7635
7636 first_die = load_partial_dies (reader, info_ptr, 1);
7637
7638 lowpc = (CORE_ADDR) -1;
7639 highpc = (CORE_ADDR) 0;
7640 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7641
7642 end_psymtab_common (objfile, pst);
7643 }
7644
7645 /* Struct used to sort TUs by their abbreviation table offset. */
7646
7647 struct tu_abbrev_offset
7648 {
7649 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7650 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7651 {}
7652
7653 signatured_type *sig_type;
7654 sect_offset abbrev_offset;
7655 };
7656
7657 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7658
7659 static bool
7660 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7661 const struct tu_abbrev_offset &b)
7662 {
7663 return a.abbrev_offset < b.abbrev_offset;
7664 }
7665
7666 /* Efficiently read all the type units.
7667 This does the bulk of the work for build_type_psymtabs.
7668
7669 The efficiency is because we sort TUs by the abbrev table they use and
7670 only read each abbrev table once. In one program there are 200K TUs
7671 sharing 8K abbrev tables.
7672
7673 The main purpose of this function is to support building the
7674 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7675 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7676 can collapse the search space by grouping them by stmt_list.
7677 The savings can be significant, in the same program from above the 200K TUs
7678 share 8K stmt_list tables.
7679
7680 FUNC is expected to call get_type_unit_group, which will create the
7681 struct type_unit_group if necessary and add it to
7682 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7683
7684 static void
7685 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7686 {
7687 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7688 abbrev_table_up abbrev_table;
7689 sect_offset abbrev_offset;
7690
7691 /* It's up to the caller to not call us multiple times. */
7692 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7693
7694 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7695 return;
7696
7697 /* TUs typically share abbrev tables, and there can be way more TUs than
7698 abbrev tables. Sort by abbrev table to reduce the number of times we
7699 read each abbrev table in.
7700 Alternatives are to punt or to maintain a cache of abbrev tables.
7701 This is simpler and efficient enough for now.
7702
7703 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7704 symtab to use). Typically TUs with the same abbrev offset have the same
7705 stmt_list value too so in practice this should work well.
7706
7707 The basic algorithm here is:
7708
7709 sort TUs by abbrev table
7710 for each TU with same abbrev table:
7711 read abbrev table if first user
7712 read TU top level DIE
7713 [IWBN if DWO skeletons had DW_AT_stmt_list]
7714 call FUNC */
7715
7716 if (dwarf_read_debug)
7717 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7718
7719 /* Sort in a separate table to maintain the order of all_type_units
7720 for .gdb_index: TU indices directly index all_type_units. */
7721 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7722 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7723
7724 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7725 sorted_by_abbrev.emplace_back
7726 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7727 sig_type->per_cu.section,
7728 sig_type->per_cu.sect_off));
7729
7730 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7731 sort_tu_by_abbrev_offset);
7732
7733 abbrev_offset = (sect_offset) ~(unsigned) 0;
7734
7735 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7736 {
7737 /* Switch to the next abbrev table if necessary. */
7738 if (abbrev_table == NULL
7739 || tu.abbrev_offset != abbrev_offset)
7740 {
7741 abbrev_offset = tu.abbrev_offset;
7742 abbrev_table =
7743 abbrev_table::read (dwarf2_per_objfile->objfile,
7744 &dwarf2_per_objfile->per_bfd->abbrev,
7745 abbrev_offset);
7746 ++tu_stats->nr_uniq_abbrev_tables;
7747 }
7748
7749 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7750 abbrev_table.get (), 0, false);
7751 if (!reader.dummy_p)
7752 build_type_psymtabs_reader (&reader, reader.info_ptr,
7753 reader.comp_unit_die);
7754 }
7755 }
7756
7757 /* Print collected type unit statistics. */
7758
7759 static void
7760 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7761 {
7762 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7763
7764 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7765 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7766 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7767 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7768 tu_stats->nr_uniq_abbrev_tables);
7769 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7770 tu_stats->nr_symtabs);
7771 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7772 tu_stats->nr_symtab_sharers);
7773 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7774 tu_stats->nr_stmt_less_type_units);
7775 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7776 tu_stats->nr_all_type_units_reallocs);
7777 }
7778
7779 /* Traversal function for build_type_psymtabs. */
7780
7781 static int
7782 build_type_psymtab_dependencies (void **slot, void *info)
7783 {
7784 struct dwarf2_per_objfile *dwarf2_per_objfile
7785 = (struct dwarf2_per_objfile *) info;
7786 struct objfile *objfile = dwarf2_per_objfile->objfile;
7787 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7788 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7789 dwarf2_psymtab *pst = per_cu->v.psymtab;
7790 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7791 int i;
7792
7793 gdb_assert (len > 0);
7794 gdb_assert (per_cu->type_unit_group_p ());
7795
7796 pst->number_of_dependencies = len;
7797 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7798 for (i = 0; i < len; ++i)
7799 {
7800 struct signatured_type *iter = tu_group->tus->at (i);
7801 gdb_assert (iter->per_cu.is_debug_types);
7802 pst->dependencies[i] = iter->per_cu.v.psymtab;
7803 iter->type_unit_group = tu_group;
7804 }
7805
7806 delete tu_group->tus;
7807 tu_group->tus = nullptr;
7808
7809 return 1;
7810 }
7811
7812 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7813 Build partial symbol tables for the .debug_types comp-units. */
7814
7815 static void
7816 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7817 {
7818 if (! create_all_type_units (dwarf2_per_objfile))
7819 return;
7820
7821 build_type_psymtabs_1 (dwarf2_per_objfile);
7822 }
7823
7824 /* Traversal function for process_skeletonless_type_unit.
7825 Read a TU in a DWO file and build partial symbols for it. */
7826
7827 static int
7828 process_skeletonless_type_unit (void **slot, void *info)
7829 {
7830 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7831 struct dwarf2_per_objfile *dwarf2_per_objfile
7832 = (struct dwarf2_per_objfile *) info;
7833 struct signatured_type find_entry, *entry;
7834
7835 /* If this TU doesn't exist in the global table, add it and read it in. */
7836
7837 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7838 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7839
7840 find_entry.signature = dwo_unit->signature;
7841 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7842 &find_entry, INSERT);
7843 /* If we've already seen this type there's nothing to do. What's happening
7844 is we're doing our own version of comdat-folding here. */
7845 if (*slot != NULL)
7846 return 1;
7847
7848 /* This does the job that create_all_type_units would have done for
7849 this TU. */
7850 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7851 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7852 *slot = entry;
7853
7854 /* This does the job that build_type_psymtabs_1 would have done. */
7855 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7856 if (!reader.dummy_p)
7857 build_type_psymtabs_reader (&reader, reader.info_ptr,
7858 reader.comp_unit_die);
7859
7860 return 1;
7861 }
7862
7863 /* Traversal function for process_skeletonless_type_units. */
7864
7865 static int
7866 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7867 {
7868 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7869
7870 if (dwo_file->tus != NULL)
7871 htab_traverse_noresize (dwo_file->tus.get (),
7872 process_skeletonless_type_unit, info);
7873
7874 return 1;
7875 }
7876
7877 /* Scan all TUs of DWO files, verifying we've processed them.
7878 This is needed in case a TU was emitted without its skeleton.
7879 Note: This can't be done until we know what all the DWO files are. */
7880
7881 static void
7882 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7883 {
7884 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7885 if (get_dwp_file (dwarf2_per_objfile) == NULL
7886 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7887 {
7888 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7889 process_dwo_file_for_skeletonless_type_units,
7890 dwarf2_per_objfile);
7891 }
7892 }
7893
7894 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7895
7896 static void
7897 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7898 {
7899 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7900 {
7901 dwarf2_psymtab *pst = per_cu->v.psymtab;
7902
7903 if (pst == NULL)
7904 continue;
7905
7906 for (int j = 0; j < pst->number_of_dependencies; ++j)
7907 {
7908 /* Set the 'user' field only if it is not already set. */
7909 if (pst->dependencies[j]->user == NULL)
7910 pst->dependencies[j]->user = pst;
7911 }
7912 }
7913 }
7914
7915 /* Build the partial symbol table by doing a quick pass through the
7916 .debug_info and .debug_abbrev sections. */
7917
7918 static void
7919 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7920 {
7921 struct objfile *objfile = dwarf2_per_objfile->objfile;
7922
7923 if (dwarf_read_debug)
7924 {
7925 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7926 objfile_name (objfile));
7927 }
7928
7929 scoped_restore restore_reading_psyms
7930 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7931 true);
7932
7933 dwarf2_per_objfile->per_bfd->info.read (objfile);
7934
7935 /* Any cached compilation units will be linked by the per-objfile
7936 read_in_chain. Make sure to free them when we're done. */
7937 free_cached_comp_units freer (dwarf2_per_objfile);
7938
7939 build_type_psymtabs (dwarf2_per_objfile);
7940
7941 create_all_comp_units (dwarf2_per_objfile);
7942
7943 /* Create a temporary address map on a temporary obstack. We later
7944 copy this to the final obstack. */
7945 auto_obstack temp_obstack;
7946
7947 scoped_restore save_psymtabs_addrmap
7948 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7949 addrmap_create_mutable (&temp_obstack));
7950
7951 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7952 {
7953 if (per_cu->v.psymtab != NULL)
7954 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7955 continue;
7956 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7957 language_minimal);
7958 }
7959
7960 /* This has to wait until we read the CUs, we need the list of DWOs. */
7961 process_skeletonless_type_units (dwarf2_per_objfile);
7962
7963 /* Now that all TUs have been processed we can fill in the dependencies. */
7964 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7965 {
7966 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7967 build_type_psymtab_dependencies, dwarf2_per_objfile);
7968 }
7969
7970 if (dwarf_read_debug)
7971 print_tu_stats (dwarf2_per_objfile);
7972
7973 set_partial_user (dwarf2_per_objfile);
7974
7975 objfile->partial_symtabs->psymtabs_addrmap
7976 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7977 objfile->partial_symtabs->obstack ());
7978 /* At this point we want to keep the address map. */
7979 save_psymtabs_addrmap.release ();
7980
7981 if (dwarf_read_debug)
7982 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7983 objfile_name (objfile));
7984 }
7985
7986 /* Load the partial DIEs for a secondary CU into memory.
7987 This is also used when rereading a primary CU with load_all_dies. */
7988
7989 static void
7990 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7991 dwarf2_per_objfile *per_objfile)
7992 {
7993 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
7994
7995 if (!reader.dummy_p)
7996 {
7997 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7998 language_minimal);
7999
8000 /* Check if comp unit has_children.
8001 If so, read the rest of the partial symbols from this comp unit.
8002 If not, there's no more debug_info for this comp unit. */
8003 if (reader.comp_unit_die->has_children)
8004 load_partial_dies (&reader, reader.info_ptr, 0);
8005
8006 reader.keep ();
8007 }
8008 }
8009
8010 static void
8011 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8012 struct dwarf2_section_info *section,
8013 struct dwarf2_section_info *abbrev_section,
8014 unsigned int is_dwz)
8015 {
8016 const gdb_byte *info_ptr;
8017 struct objfile *objfile = dwarf2_per_objfile->objfile;
8018
8019 if (dwarf_read_debug)
8020 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8021 section->get_name (),
8022 section->get_file_name ());
8023
8024 section->read (objfile);
8025
8026 info_ptr = section->buffer;
8027
8028 while (info_ptr < section->buffer + section->size)
8029 {
8030 struct dwarf2_per_cu_data *this_cu;
8031
8032 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8033
8034 comp_unit_head cu_header;
8035 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8036 abbrev_section, info_ptr,
8037 rcuh_kind::COMPILE);
8038
8039 /* Save the compilation unit for later lookup. */
8040 if (cu_header.unit_type != DW_UT_type)
8041 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8042 else
8043 {
8044 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8045 sig_type->signature = cu_header.signature;
8046 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8047 this_cu = &sig_type->per_cu;
8048 }
8049 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8050 this_cu->sect_off = sect_off;
8051 this_cu->length = cu_header.length + cu_header.initial_length_size;
8052 this_cu->is_dwz = is_dwz;
8053 this_cu->section = section;
8054
8055 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8056
8057 info_ptr = info_ptr + this_cu->length;
8058 }
8059 }
8060
8061 /* Create a list of all compilation units in OBJFILE.
8062 This is only done for -readnow and building partial symtabs. */
8063
8064 static void
8065 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8066 {
8067 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8068 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8069 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8070
8071 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8072 if (dwz != NULL)
8073 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8074 1);
8075 }
8076
8077 /* Process all loaded DIEs for compilation unit CU, starting at
8078 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8079 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8080 DW_AT_ranges). See the comments of add_partial_subprogram on how
8081 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8082
8083 static void
8084 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8085 CORE_ADDR *highpc, int set_addrmap,
8086 struct dwarf2_cu *cu)
8087 {
8088 struct partial_die_info *pdi;
8089
8090 /* Now, march along the PDI's, descending into ones which have
8091 interesting children but skipping the children of the other ones,
8092 until we reach the end of the compilation unit. */
8093
8094 pdi = first_die;
8095
8096 while (pdi != NULL)
8097 {
8098 pdi->fixup (cu);
8099
8100 /* Anonymous namespaces or modules have no name but have interesting
8101 children, so we need to look at them. Ditto for anonymous
8102 enums. */
8103
8104 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8105 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8106 || pdi->tag == DW_TAG_imported_unit
8107 || pdi->tag == DW_TAG_inlined_subroutine)
8108 {
8109 switch (pdi->tag)
8110 {
8111 case DW_TAG_subprogram:
8112 case DW_TAG_inlined_subroutine:
8113 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8114 break;
8115 case DW_TAG_constant:
8116 case DW_TAG_variable:
8117 case DW_TAG_typedef:
8118 case DW_TAG_union_type:
8119 if (!pdi->is_declaration
8120 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8121 {
8122 add_partial_symbol (pdi, cu);
8123 }
8124 break;
8125 case DW_TAG_class_type:
8126 case DW_TAG_interface_type:
8127 case DW_TAG_structure_type:
8128 if (!pdi->is_declaration)
8129 {
8130 add_partial_symbol (pdi, cu);
8131 }
8132 if ((cu->language == language_rust
8133 || cu->language == language_cplus) && pdi->has_children)
8134 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8135 set_addrmap, cu);
8136 break;
8137 case DW_TAG_enumeration_type:
8138 if (!pdi->is_declaration)
8139 add_partial_enumeration (pdi, cu);
8140 break;
8141 case DW_TAG_base_type:
8142 case DW_TAG_subrange_type:
8143 /* File scope base type definitions are added to the partial
8144 symbol table. */
8145 add_partial_symbol (pdi, cu);
8146 break;
8147 case DW_TAG_namespace:
8148 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8149 break;
8150 case DW_TAG_module:
8151 if (!pdi->is_declaration)
8152 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8153 break;
8154 case DW_TAG_imported_unit:
8155 {
8156 struct dwarf2_per_cu_data *per_cu;
8157
8158 /* For now we don't handle imported units in type units. */
8159 if (cu->per_cu->is_debug_types)
8160 {
8161 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8162 " supported in type units [in module %s]"),
8163 objfile_name (cu->per_objfile->objfile));
8164 }
8165
8166 per_cu = dwarf2_find_containing_comp_unit
8167 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8168
8169 /* Go read the partial unit, if needed. */
8170 if (per_cu->v.psymtab == NULL)
8171 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8172 cu->language);
8173
8174 cu->per_cu->imported_symtabs_push (per_cu);
8175 }
8176 break;
8177 case DW_TAG_imported_declaration:
8178 add_partial_symbol (pdi, cu);
8179 break;
8180 default:
8181 break;
8182 }
8183 }
8184
8185 /* If the die has a sibling, skip to the sibling. */
8186
8187 pdi = pdi->die_sibling;
8188 }
8189 }
8190
8191 /* Functions used to compute the fully scoped name of a partial DIE.
8192
8193 Normally, this is simple. For C++, the parent DIE's fully scoped
8194 name is concatenated with "::" and the partial DIE's name.
8195 Enumerators are an exception; they use the scope of their parent
8196 enumeration type, i.e. the name of the enumeration type is not
8197 prepended to the enumerator.
8198
8199 There are two complexities. One is DW_AT_specification; in this
8200 case "parent" means the parent of the target of the specification,
8201 instead of the direct parent of the DIE. The other is compilers
8202 which do not emit DW_TAG_namespace; in this case we try to guess
8203 the fully qualified name of structure types from their members'
8204 linkage names. This must be done using the DIE's children rather
8205 than the children of any DW_AT_specification target. We only need
8206 to do this for structures at the top level, i.e. if the target of
8207 any DW_AT_specification (if any; otherwise the DIE itself) does not
8208 have a parent. */
8209
8210 /* Compute the scope prefix associated with PDI's parent, in
8211 compilation unit CU. The result will be allocated on CU's
8212 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8213 field. NULL is returned if no prefix is necessary. */
8214 static const char *
8215 partial_die_parent_scope (struct partial_die_info *pdi,
8216 struct dwarf2_cu *cu)
8217 {
8218 const char *grandparent_scope;
8219 struct partial_die_info *parent, *real_pdi;
8220
8221 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8222 then this means the parent of the specification DIE. */
8223
8224 real_pdi = pdi;
8225 while (real_pdi->has_specification)
8226 {
8227 auto res = find_partial_die (real_pdi->spec_offset,
8228 real_pdi->spec_is_dwz, cu);
8229 real_pdi = res.pdi;
8230 cu = res.cu;
8231 }
8232
8233 parent = real_pdi->die_parent;
8234 if (parent == NULL)
8235 return NULL;
8236
8237 if (parent->scope_set)
8238 return parent->scope;
8239
8240 parent->fixup (cu);
8241
8242 grandparent_scope = partial_die_parent_scope (parent, cu);
8243
8244 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8245 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8246 Work around this problem here. */
8247 if (cu->language == language_cplus
8248 && parent->tag == DW_TAG_namespace
8249 && strcmp (parent->name, "::") == 0
8250 && grandparent_scope == NULL)
8251 {
8252 parent->scope = NULL;
8253 parent->scope_set = 1;
8254 return NULL;
8255 }
8256
8257 /* Nested subroutines in Fortran get a prefix. */
8258 if (pdi->tag == DW_TAG_enumerator)
8259 /* Enumerators should not get the name of the enumeration as a prefix. */
8260 parent->scope = grandparent_scope;
8261 else if (parent->tag == DW_TAG_namespace
8262 || parent->tag == DW_TAG_module
8263 || parent->tag == DW_TAG_structure_type
8264 || parent->tag == DW_TAG_class_type
8265 || parent->tag == DW_TAG_interface_type
8266 || parent->tag == DW_TAG_union_type
8267 || parent->tag == DW_TAG_enumeration_type
8268 || (cu->language == language_fortran
8269 && parent->tag == DW_TAG_subprogram
8270 && pdi->tag == DW_TAG_subprogram))
8271 {
8272 if (grandparent_scope == NULL)
8273 parent->scope = parent->name;
8274 else
8275 parent->scope = typename_concat (&cu->comp_unit_obstack,
8276 grandparent_scope,
8277 parent->name, 0, cu);
8278 }
8279 else
8280 {
8281 /* FIXME drow/2004-04-01: What should we be doing with
8282 function-local names? For partial symbols, we should probably be
8283 ignoring them. */
8284 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8285 dwarf_tag_name (parent->tag),
8286 sect_offset_str (pdi->sect_off));
8287 parent->scope = grandparent_scope;
8288 }
8289
8290 parent->scope_set = 1;
8291 return parent->scope;
8292 }
8293
8294 /* Return the fully scoped name associated with PDI, from compilation unit
8295 CU. The result will be allocated with malloc. */
8296
8297 static gdb::unique_xmalloc_ptr<char>
8298 partial_die_full_name (struct partial_die_info *pdi,
8299 struct dwarf2_cu *cu)
8300 {
8301 const char *parent_scope;
8302
8303 /* If this is a template instantiation, we can not work out the
8304 template arguments from partial DIEs. So, unfortunately, we have
8305 to go through the full DIEs. At least any work we do building
8306 types here will be reused if full symbols are loaded later. */
8307 if (pdi->has_template_arguments)
8308 {
8309 pdi->fixup (cu);
8310
8311 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8312 {
8313 struct die_info *die;
8314 struct attribute attr;
8315 struct dwarf2_cu *ref_cu = cu;
8316
8317 /* DW_FORM_ref_addr is using section offset. */
8318 attr.name = (enum dwarf_attribute) 0;
8319 attr.form = DW_FORM_ref_addr;
8320 attr.u.unsnd = to_underlying (pdi->sect_off);
8321 die = follow_die_ref (NULL, &attr, &ref_cu);
8322
8323 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8324 }
8325 }
8326
8327 parent_scope = partial_die_parent_scope (pdi, cu);
8328 if (parent_scope == NULL)
8329 return NULL;
8330 else
8331 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8332 pdi->name, 0, cu));
8333 }
8334
8335 static void
8336 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8337 {
8338 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8339 struct objfile *objfile = dwarf2_per_objfile->objfile;
8340 struct gdbarch *gdbarch = objfile->arch ();
8341 CORE_ADDR addr = 0;
8342 const char *actual_name = NULL;
8343 CORE_ADDR baseaddr;
8344
8345 baseaddr = objfile->text_section_offset ();
8346
8347 gdb::unique_xmalloc_ptr<char> built_actual_name
8348 = partial_die_full_name (pdi, cu);
8349 if (built_actual_name != NULL)
8350 actual_name = built_actual_name.get ();
8351
8352 if (actual_name == NULL)
8353 actual_name = pdi->name;
8354
8355 partial_symbol psymbol;
8356 memset (&psymbol, 0, sizeof (psymbol));
8357 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8358 psymbol.ginfo.section = -1;
8359
8360 /* The code below indicates that the psymbol should be installed by
8361 setting this. */
8362 gdb::optional<psymbol_placement> where;
8363
8364 switch (pdi->tag)
8365 {
8366 case DW_TAG_inlined_subroutine:
8367 case DW_TAG_subprogram:
8368 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8369 - baseaddr);
8370 if (pdi->is_external
8371 || cu->language == language_ada
8372 || (cu->language == language_fortran
8373 && pdi->die_parent != NULL
8374 && pdi->die_parent->tag == DW_TAG_subprogram))
8375 {
8376 /* Normally, only "external" DIEs are part of the global scope.
8377 But in Ada and Fortran, we want to be able to access nested
8378 procedures globally. So all Ada and Fortran subprograms are
8379 stored in the global scope. */
8380 where = psymbol_placement::GLOBAL;
8381 }
8382 else
8383 where = psymbol_placement::STATIC;
8384
8385 psymbol.domain = VAR_DOMAIN;
8386 psymbol.aclass = LOC_BLOCK;
8387 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8388 psymbol.ginfo.value.address = addr;
8389
8390 if (pdi->main_subprogram && actual_name != NULL)
8391 set_objfile_main_name (objfile, actual_name, cu->language);
8392 break;
8393 case DW_TAG_constant:
8394 psymbol.domain = VAR_DOMAIN;
8395 psymbol.aclass = LOC_STATIC;
8396 where = (pdi->is_external
8397 ? psymbol_placement::GLOBAL
8398 : psymbol_placement::STATIC);
8399 break;
8400 case DW_TAG_variable:
8401 if (pdi->d.locdesc)
8402 addr = decode_locdesc (pdi->d.locdesc, cu);
8403
8404 if (pdi->d.locdesc
8405 && addr == 0
8406 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8407 {
8408 /* A global or static variable may also have been stripped
8409 out by the linker if unused, in which case its address
8410 will be nullified; do not add such variables into partial
8411 symbol table then. */
8412 }
8413 else if (pdi->is_external)
8414 {
8415 /* Global Variable.
8416 Don't enter into the minimal symbol tables as there is
8417 a minimal symbol table entry from the ELF symbols already.
8418 Enter into partial symbol table if it has a location
8419 descriptor or a type.
8420 If the location descriptor is missing, new_symbol will create
8421 a LOC_UNRESOLVED symbol, the address of the variable will then
8422 be determined from the minimal symbol table whenever the variable
8423 is referenced.
8424 The address for the partial symbol table entry is not
8425 used by GDB, but it comes in handy for debugging partial symbol
8426 table building. */
8427
8428 if (pdi->d.locdesc || pdi->has_type)
8429 {
8430 psymbol.domain = VAR_DOMAIN;
8431 psymbol.aclass = LOC_STATIC;
8432 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8433 psymbol.ginfo.value.address = addr;
8434 where = psymbol_placement::GLOBAL;
8435 }
8436 }
8437 else
8438 {
8439 int has_loc = pdi->d.locdesc != NULL;
8440
8441 /* Static Variable. Skip symbols whose value we cannot know (those
8442 without location descriptors or constant values). */
8443 if (!has_loc && !pdi->has_const_value)
8444 return;
8445
8446 psymbol.domain = VAR_DOMAIN;
8447 psymbol.aclass = LOC_STATIC;
8448 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8449 if (has_loc)
8450 psymbol.ginfo.value.address = addr;
8451 where = psymbol_placement::STATIC;
8452 }
8453 break;
8454 case DW_TAG_typedef:
8455 case DW_TAG_base_type:
8456 case DW_TAG_subrange_type:
8457 psymbol.domain = VAR_DOMAIN;
8458 psymbol.aclass = LOC_TYPEDEF;
8459 where = psymbol_placement::STATIC;
8460 break;
8461 case DW_TAG_imported_declaration:
8462 case DW_TAG_namespace:
8463 psymbol.domain = VAR_DOMAIN;
8464 psymbol.aclass = LOC_TYPEDEF;
8465 where = psymbol_placement::GLOBAL;
8466 break;
8467 case DW_TAG_module:
8468 /* With Fortran 77 there might be a "BLOCK DATA" module
8469 available without any name. If so, we skip the module as it
8470 doesn't bring any value. */
8471 if (actual_name != nullptr)
8472 {
8473 psymbol.domain = MODULE_DOMAIN;
8474 psymbol.aclass = LOC_TYPEDEF;
8475 where = psymbol_placement::GLOBAL;
8476 }
8477 break;
8478 case DW_TAG_class_type:
8479 case DW_TAG_interface_type:
8480 case DW_TAG_structure_type:
8481 case DW_TAG_union_type:
8482 case DW_TAG_enumeration_type:
8483 /* Skip external references. The DWARF standard says in the section
8484 about "Structure, Union, and Class Type Entries": "An incomplete
8485 structure, union or class type is represented by a structure,
8486 union or class entry that does not have a byte size attribute
8487 and that has a DW_AT_declaration attribute." */
8488 if (!pdi->has_byte_size && pdi->is_declaration)
8489 return;
8490
8491 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8492 static vs. global. */
8493 psymbol.domain = STRUCT_DOMAIN;
8494 psymbol.aclass = LOC_TYPEDEF;
8495 where = (cu->language == language_cplus
8496 ? psymbol_placement::GLOBAL
8497 : psymbol_placement::STATIC);
8498 break;
8499 case DW_TAG_enumerator:
8500 psymbol.domain = VAR_DOMAIN;
8501 psymbol.aclass = LOC_CONST;
8502 where = (cu->language == language_cplus
8503 ? psymbol_placement::GLOBAL
8504 : psymbol_placement::STATIC);
8505 break;
8506 default:
8507 break;
8508 }
8509
8510 if (where.has_value ())
8511 {
8512 if (built_actual_name != nullptr)
8513 actual_name = objfile->intern (actual_name);
8514 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8515 psymbol.ginfo.set_linkage_name (actual_name);
8516 else
8517 {
8518 psymbol.ginfo.set_demangled_name (actual_name,
8519 &objfile->objfile_obstack);
8520 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8521 }
8522 add_psymbol_to_list (psymbol, *where, objfile);
8523 }
8524 }
8525
8526 /* Read a partial die corresponding to a namespace; also, add a symbol
8527 corresponding to that namespace to the symbol table. NAMESPACE is
8528 the name of the enclosing namespace. */
8529
8530 static void
8531 add_partial_namespace (struct partial_die_info *pdi,
8532 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8533 int set_addrmap, struct dwarf2_cu *cu)
8534 {
8535 /* Add a symbol for the namespace. */
8536
8537 add_partial_symbol (pdi, cu);
8538
8539 /* Now scan partial symbols in that namespace. */
8540
8541 if (pdi->has_children)
8542 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8543 }
8544
8545 /* Read a partial die corresponding to a Fortran module. */
8546
8547 static void
8548 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8549 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8550 {
8551 /* Add a symbol for the namespace. */
8552
8553 add_partial_symbol (pdi, cu);
8554
8555 /* Now scan partial symbols in that module. */
8556
8557 if (pdi->has_children)
8558 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8559 }
8560
8561 /* Read a partial die corresponding to a subprogram or an inlined
8562 subprogram and create a partial symbol for that subprogram.
8563 When the CU language allows it, this routine also defines a partial
8564 symbol for each nested subprogram that this subprogram contains.
8565 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8566 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8567
8568 PDI may also be a lexical block, in which case we simply search
8569 recursively for subprograms defined inside that lexical block.
8570 Again, this is only performed when the CU language allows this
8571 type of definitions. */
8572
8573 static void
8574 add_partial_subprogram (struct partial_die_info *pdi,
8575 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8576 int set_addrmap, struct dwarf2_cu *cu)
8577 {
8578 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8579 {
8580 if (pdi->has_pc_info)
8581 {
8582 if (pdi->lowpc < *lowpc)
8583 *lowpc = pdi->lowpc;
8584 if (pdi->highpc > *highpc)
8585 *highpc = pdi->highpc;
8586 if (set_addrmap)
8587 {
8588 struct objfile *objfile = cu->per_objfile->objfile;
8589 struct gdbarch *gdbarch = objfile->arch ();
8590 CORE_ADDR baseaddr;
8591 CORE_ADDR this_highpc;
8592 CORE_ADDR this_lowpc;
8593
8594 baseaddr = objfile->text_section_offset ();
8595 this_lowpc
8596 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8597 pdi->lowpc + baseaddr)
8598 - baseaddr);
8599 this_highpc
8600 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8601 pdi->highpc + baseaddr)
8602 - baseaddr);
8603 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8604 this_lowpc, this_highpc - 1,
8605 cu->per_cu->v.psymtab);
8606 }
8607 }
8608
8609 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8610 {
8611 if (!pdi->is_declaration)
8612 /* Ignore subprogram DIEs that do not have a name, they are
8613 illegal. Do not emit a complaint at this point, we will
8614 do so when we convert this psymtab into a symtab. */
8615 if (pdi->name)
8616 add_partial_symbol (pdi, cu);
8617 }
8618 }
8619
8620 if (! pdi->has_children)
8621 return;
8622
8623 if (cu->language == language_ada || cu->language == language_fortran)
8624 {
8625 pdi = pdi->die_child;
8626 while (pdi != NULL)
8627 {
8628 pdi->fixup (cu);
8629 if (pdi->tag == DW_TAG_subprogram
8630 || pdi->tag == DW_TAG_inlined_subroutine
8631 || pdi->tag == DW_TAG_lexical_block)
8632 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8633 pdi = pdi->die_sibling;
8634 }
8635 }
8636 }
8637
8638 /* Read a partial die corresponding to an enumeration type. */
8639
8640 static void
8641 add_partial_enumeration (struct partial_die_info *enum_pdi,
8642 struct dwarf2_cu *cu)
8643 {
8644 struct partial_die_info *pdi;
8645
8646 if (enum_pdi->name != NULL)
8647 add_partial_symbol (enum_pdi, cu);
8648
8649 pdi = enum_pdi->die_child;
8650 while (pdi)
8651 {
8652 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8653 complaint (_("malformed enumerator DIE ignored"));
8654 else
8655 add_partial_symbol (pdi, cu);
8656 pdi = pdi->die_sibling;
8657 }
8658 }
8659
8660 /* Return the initial uleb128 in the die at INFO_PTR. */
8661
8662 static unsigned int
8663 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8664 {
8665 unsigned int bytes_read;
8666
8667 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8668 }
8669
8670 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8671 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8672
8673 Return the corresponding abbrev, or NULL if the number is zero (indicating
8674 an empty DIE). In either case *BYTES_READ will be set to the length of
8675 the initial number. */
8676
8677 static struct abbrev_info *
8678 peek_die_abbrev (const die_reader_specs &reader,
8679 const gdb_byte *info_ptr, unsigned int *bytes_read)
8680 {
8681 dwarf2_cu *cu = reader.cu;
8682 bfd *abfd = cu->per_objfile->objfile->obfd;
8683 unsigned int abbrev_number
8684 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8685
8686 if (abbrev_number == 0)
8687 return NULL;
8688
8689 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8690 if (!abbrev)
8691 {
8692 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8693 " at offset %s [in module %s]"),
8694 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8695 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8696 }
8697
8698 return abbrev;
8699 }
8700
8701 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8702 Returns a pointer to the end of a series of DIEs, terminated by an empty
8703 DIE. Any children of the skipped DIEs will also be skipped. */
8704
8705 static const gdb_byte *
8706 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8707 {
8708 while (1)
8709 {
8710 unsigned int bytes_read;
8711 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8712
8713 if (abbrev == NULL)
8714 return info_ptr + bytes_read;
8715 else
8716 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8717 }
8718 }
8719
8720 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8721 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8722 abbrev corresponding to that skipped uleb128 should be passed in
8723 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8724 children. */
8725
8726 static const gdb_byte *
8727 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8728 struct abbrev_info *abbrev)
8729 {
8730 unsigned int bytes_read;
8731 struct attribute attr;
8732 bfd *abfd = reader->abfd;
8733 struct dwarf2_cu *cu = reader->cu;
8734 const gdb_byte *buffer = reader->buffer;
8735 const gdb_byte *buffer_end = reader->buffer_end;
8736 unsigned int form, i;
8737
8738 for (i = 0; i < abbrev->num_attrs; i++)
8739 {
8740 /* The only abbrev we care about is DW_AT_sibling. */
8741 if (abbrev->attrs[i].name == DW_AT_sibling)
8742 {
8743 bool ignored;
8744 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8745 &ignored);
8746 if (attr.form == DW_FORM_ref_addr)
8747 complaint (_("ignoring absolute DW_AT_sibling"));
8748 else
8749 {
8750 sect_offset off = attr.get_ref_die_offset ();
8751 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8752
8753 if (sibling_ptr < info_ptr)
8754 complaint (_("DW_AT_sibling points backwards"));
8755 else if (sibling_ptr > reader->buffer_end)
8756 reader->die_section->overflow_complaint ();
8757 else
8758 return sibling_ptr;
8759 }
8760 }
8761
8762 /* If it isn't DW_AT_sibling, skip this attribute. */
8763 form = abbrev->attrs[i].form;
8764 skip_attribute:
8765 switch (form)
8766 {
8767 case DW_FORM_ref_addr:
8768 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8769 and later it is offset sized. */
8770 if (cu->header.version == 2)
8771 info_ptr += cu->header.addr_size;
8772 else
8773 info_ptr += cu->header.offset_size;
8774 break;
8775 case DW_FORM_GNU_ref_alt:
8776 info_ptr += cu->header.offset_size;
8777 break;
8778 case DW_FORM_addr:
8779 info_ptr += cu->header.addr_size;
8780 break;
8781 case DW_FORM_data1:
8782 case DW_FORM_ref1:
8783 case DW_FORM_flag:
8784 case DW_FORM_strx1:
8785 info_ptr += 1;
8786 break;
8787 case DW_FORM_flag_present:
8788 case DW_FORM_implicit_const:
8789 break;
8790 case DW_FORM_data2:
8791 case DW_FORM_ref2:
8792 case DW_FORM_strx2:
8793 info_ptr += 2;
8794 break;
8795 case DW_FORM_strx3:
8796 info_ptr += 3;
8797 break;
8798 case DW_FORM_data4:
8799 case DW_FORM_ref4:
8800 case DW_FORM_strx4:
8801 info_ptr += 4;
8802 break;
8803 case DW_FORM_data8:
8804 case DW_FORM_ref8:
8805 case DW_FORM_ref_sig8:
8806 info_ptr += 8;
8807 break;
8808 case DW_FORM_data16:
8809 info_ptr += 16;
8810 break;
8811 case DW_FORM_string:
8812 read_direct_string (abfd, info_ptr, &bytes_read);
8813 info_ptr += bytes_read;
8814 break;
8815 case DW_FORM_sec_offset:
8816 case DW_FORM_strp:
8817 case DW_FORM_GNU_strp_alt:
8818 info_ptr += cu->header.offset_size;
8819 break;
8820 case DW_FORM_exprloc:
8821 case DW_FORM_block:
8822 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8823 info_ptr += bytes_read;
8824 break;
8825 case DW_FORM_block1:
8826 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8827 break;
8828 case DW_FORM_block2:
8829 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8830 break;
8831 case DW_FORM_block4:
8832 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8833 break;
8834 case DW_FORM_addrx:
8835 case DW_FORM_strx:
8836 case DW_FORM_sdata:
8837 case DW_FORM_udata:
8838 case DW_FORM_ref_udata:
8839 case DW_FORM_GNU_addr_index:
8840 case DW_FORM_GNU_str_index:
8841 case DW_FORM_rnglistx:
8842 case DW_FORM_loclistx:
8843 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8844 break;
8845 case DW_FORM_indirect:
8846 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8847 info_ptr += bytes_read;
8848 /* We need to continue parsing from here, so just go back to
8849 the top. */
8850 goto skip_attribute;
8851
8852 default:
8853 error (_("Dwarf Error: Cannot handle %s "
8854 "in DWARF reader [in module %s]"),
8855 dwarf_form_name (form),
8856 bfd_get_filename (abfd));
8857 }
8858 }
8859
8860 if (abbrev->has_children)
8861 return skip_children (reader, info_ptr);
8862 else
8863 return info_ptr;
8864 }
8865
8866 /* Locate ORIG_PDI's sibling.
8867 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8868
8869 static const gdb_byte *
8870 locate_pdi_sibling (const struct die_reader_specs *reader,
8871 struct partial_die_info *orig_pdi,
8872 const gdb_byte *info_ptr)
8873 {
8874 /* Do we know the sibling already? */
8875
8876 if (orig_pdi->sibling)
8877 return orig_pdi->sibling;
8878
8879 /* Are there any children to deal with? */
8880
8881 if (!orig_pdi->has_children)
8882 return info_ptr;
8883
8884 /* Skip the children the long way. */
8885
8886 return skip_children (reader, info_ptr);
8887 }
8888
8889 /* Expand this partial symbol table into a full symbol table. SELF is
8890 not NULL. */
8891
8892 void
8893 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8894 {
8895 struct dwarf2_per_objfile *dwarf2_per_objfile
8896 = get_dwarf2_per_objfile (objfile);
8897
8898 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8899
8900 /* If this psymtab is constructed from a debug-only objfile, the
8901 has_section_at_zero flag will not necessarily be correct. We
8902 can get the correct value for this flag by looking at the data
8903 associated with the (presumably stripped) associated objfile. */
8904 if (objfile->separate_debug_objfile_backlink)
8905 {
8906 struct dwarf2_per_objfile *dpo_backlink
8907 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8908
8909 dwarf2_per_objfile->per_bfd->has_section_at_zero
8910 = dpo_backlink->per_bfd->has_section_at_zero;
8911 }
8912
8913 expand_psymtab (objfile);
8914
8915 process_cu_includes (dwarf2_per_objfile);
8916 }
8917 \f
8918 /* Reading in full CUs. */
8919
8920 /* Add PER_CU to the queue. */
8921
8922 static void
8923 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8924 dwarf2_per_objfile *per_objfile,
8925 enum language pretend_language)
8926 {
8927 per_cu->queued = 1;
8928 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8929 }
8930
8931 /* If PER_CU is not yet queued, add it to the queue.
8932 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8933 dependency.
8934 The result is non-zero if PER_CU was queued, otherwise the result is zero
8935 meaning either PER_CU is already queued or it is already loaded.
8936
8937 N.B. There is an invariant here that if a CU is queued then it is loaded.
8938 The caller is required to load PER_CU if we return non-zero. */
8939
8940 static int
8941 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8942 dwarf2_per_cu_data *per_cu,
8943 dwarf2_per_objfile *per_objfile,
8944 enum language pretend_language)
8945 {
8946 /* We may arrive here during partial symbol reading, if we need full
8947 DIEs to process an unusual case (e.g. template arguments). Do
8948 not queue PER_CU, just tell our caller to load its DIEs. */
8949 if (per_cu->per_bfd->reading_partial_symbols)
8950 {
8951 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8952 return 1;
8953 return 0;
8954 }
8955
8956 /* Mark the dependence relation so that we don't flush PER_CU
8957 too early. */
8958 if (dependent_cu != NULL)
8959 dwarf2_add_dependence (dependent_cu, per_cu);
8960
8961 /* If it's already on the queue, we have nothing to do. */
8962 if (per_cu->queued)
8963 return 0;
8964
8965 /* If the compilation unit is already loaded, just mark it as
8966 used. */
8967 if (per_cu->cu != NULL)
8968 {
8969 per_cu->cu->last_used = 0;
8970 return 0;
8971 }
8972
8973 /* Add it to the queue. */
8974 queue_comp_unit (per_cu, per_objfile, pretend_language);
8975
8976 return 1;
8977 }
8978
8979 /* Process the queue. */
8980
8981 static void
8982 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8983 {
8984 if (dwarf_read_debug)
8985 {
8986 fprintf_unfiltered (gdb_stdlog,
8987 "Expanding one or more symtabs of objfile %s ...\n",
8988 objfile_name (dwarf2_per_objfile->objfile));
8989 }
8990
8991 /* The queue starts out with one item, but following a DIE reference
8992 may load a new CU, adding it to the end of the queue. */
8993 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8994 {
8995 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8996
8997 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
8998 /* Skip dummy CUs. */
8999 && item.per_cu->cu != NULL)
9000 {
9001 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9002 unsigned int debug_print_threshold;
9003 char buf[100];
9004
9005 if (per_cu->is_debug_types)
9006 {
9007 struct signatured_type *sig_type =
9008 (struct signatured_type *) per_cu;
9009
9010 sprintf (buf, "TU %s at offset %s",
9011 hex_string (sig_type->signature),
9012 sect_offset_str (per_cu->sect_off));
9013 /* There can be 100s of TUs.
9014 Only print them in verbose mode. */
9015 debug_print_threshold = 2;
9016 }
9017 else
9018 {
9019 sprintf (buf, "CU at offset %s",
9020 sect_offset_str (per_cu->sect_off));
9021 debug_print_threshold = 1;
9022 }
9023
9024 if (dwarf_read_debug >= debug_print_threshold)
9025 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9026
9027 if (per_cu->is_debug_types)
9028 process_full_type_unit (per_cu, dwarf2_per_objfile,
9029 item.pretend_language);
9030 else
9031 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9032 item.pretend_language);
9033
9034 if (dwarf_read_debug >= debug_print_threshold)
9035 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9036 }
9037
9038 item.per_cu->queued = 0;
9039 dwarf2_per_objfile->per_bfd->queue.pop ();
9040 }
9041
9042 if (dwarf_read_debug)
9043 {
9044 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9045 objfile_name (dwarf2_per_objfile->objfile));
9046 }
9047 }
9048
9049 /* Read in full symbols for PST, and anything it depends on. */
9050
9051 void
9052 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9053 {
9054 gdb_assert (!readin_p (objfile));
9055
9056 expand_dependencies (objfile);
9057
9058 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9059 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9060 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9061 }
9062
9063 /* See psympriv.h. */
9064
9065 bool
9066 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9067 {
9068 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9069 return per_objfile->symtab_set_p (per_cu_data);
9070 }
9071
9072 /* See psympriv.h. */
9073
9074 compunit_symtab *
9075 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9076 {
9077 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9078 return per_objfile->get_symtab (per_cu_data);
9079 }
9080
9081 /* Trivial hash function for die_info: the hash value of a DIE
9082 is its offset in .debug_info for this objfile. */
9083
9084 static hashval_t
9085 die_hash (const void *item)
9086 {
9087 const struct die_info *die = (const struct die_info *) item;
9088
9089 return to_underlying (die->sect_off);
9090 }
9091
9092 /* Trivial comparison function for die_info structures: two DIEs
9093 are equal if they have the same offset. */
9094
9095 static int
9096 die_eq (const void *item_lhs, const void *item_rhs)
9097 {
9098 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9099 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9100
9101 return die_lhs->sect_off == die_rhs->sect_off;
9102 }
9103
9104 /* Load the DIEs associated with PER_CU into memory. */
9105
9106 static void
9107 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9108 dwarf2_per_objfile *per_objfile,
9109 bool skip_partial,
9110 enum language pretend_language)
9111 {
9112 gdb_assert (! this_cu->is_debug_types);
9113
9114 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9115 if (reader.dummy_p)
9116 return;
9117
9118 struct dwarf2_cu *cu = reader.cu;
9119 const gdb_byte *info_ptr = reader.info_ptr;
9120
9121 gdb_assert (cu->die_hash == NULL);
9122 cu->die_hash =
9123 htab_create_alloc_ex (cu->header.length / 12,
9124 die_hash,
9125 die_eq,
9126 NULL,
9127 &cu->comp_unit_obstack,
9128 hashtab_obstack_allocate,
9129 dummy_obstack_deallocate);
9130
9131 if (reader.comp_unit_die->has_children)
9132 reader.comp_unit_die->child
9133 = read_die_and_siblings (&reader, reader.info_ptr,
9134 &info_ptr, reader.comp_unit_die);
9135 cu->dies = reader.comp_unit_die;
9136 /* comp_unit_die is not stored in die_hash, no need. */
9137
9138 /* We try not to read any attributes in this function, because not
9139 all CUs needed for references have been loaded yet, and symbol
9140 table processing isn't initialized. But we have to set the CU language,
9141 or we won't be able to build types correctly.
9142 Similarly, if we do not read the producer, we can not apply
9143 producer-specific interpretation. */
9144 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9145
9146 reader.keep ();
9147 }
9148
9149 /* Add a DIE to the delayed physname list. */
9150
9151 static void
9152 add_to_method_list (struct type *type, int fnfield_index, int index,
9153 const char *name, struct die_info *die,
9154 struct dwarf2_cu *cu)
9155 {
9156 struct delayed_method_info mi;
9157 mi.type = type;
9158 mi.fnfield_index = fnfield_index;
9159 mi.index = index;
9160 mi.name = name;
9161 mi.die = die;
9162 cu->method_list.push_back (mi);
9163 }
9164
9165 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9166 "const" / "volatile". If so, decrements LEN by the length of the
9167 modifier and return true. Otherwise return false. */
9168
9169 template<size_t N>
9170 static bool
9171 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9172 {
9173 size_t mod_len = sizeof (mod) - 1;
9174 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9175 {
9176 len -= mod_len;
9177 return true;
9178 }
9179 return false;
9180 }
9181
9182 /* Compute the physnames of any methods on the CU's method list.
9183
9184 The computation of method physnames is delayed in order to avoid the
9185 (bad) condition that one of the method's formal parameters is of an as yet
9186 incomplete type. */
9187
9188 static void
9189 compute_delayed_physnames (struct dwarf2_cu *cu)
9190 {
9191 /* Only C++ delays computing physnames. */
9192 if (cu->method_list.empty ())
9193 return;
9194 gdb_assert (cu->language == language_cplus);
9195
9196 for (const delayed_method_info &mi : cu->method_list)
9197 {
9198 const char *physname;
9199 struct fn_fieldlist *fn_flp
9200 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9201 physname = dwarf2_physname (mi.name, mi.die, cu);
9202 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9203 = physname ? physname : "";
9204
9205 /* Since there's no tag to indicate whether a method is a
9206 const/volatile overload, extract that information out of the
9207 demangled name. */
9208 if (physname != NULL)
9209 {
9210 size_t len = strlen (physname);
9211
9212 while (1)
9213 {
9214 if (physname[len] == ')') /* shortcut */
9215 break;
9216 else if (check_modifier (physname, len, " const"))
9217 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9218 else if (check_modifier (physname, len, " volatile"))
9219 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9220 else
9221 break;
9222 }
9223 }
9224 }
9225
9226 /* The list is no longer needed. */
9227 cu->method_list.clear ();
9228 }
9229
9230 /* Go objects should be embedded in a DW_TAG_module DIE,
9231 and it's not clear if/how imported objects will appear.
9232 To keep Go support simple until that's worked out,
9233 go back through what we've read and create something usable.
9234 We could do this while processing each DIE, and feels kinda cleaner,
9235 but that way is more invasive.
9236 This is to, for example, allow the user to type "p var" or "b main"
9237 without having to specify the package name, and allow lookups
9238 of module.object to work in contexts that use the expression
9239 parser. */
9240
9241 static void
9242 fixup_go_packaging (struct dwarf2_cu *cu)
9243 {
9244 gdb::unique_xmalloc_ptr<char> package_name;
9245 struct pending *list;
9246 int i;
9247
9248 for (list = *cu->get_builder ()->get_global_symbols ();
9249 list != NULL;
9250 list = list->next)
9251 {
9252 for (i = 0; i < list->nsyms; ++i)
9253 {
9254 struct symbol *sym = list->symbol[i];
9255
9256 if (sym->language () == language_go
9257 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9258 {
9259 gdb::unique_xmalloc_ptr<char> this_package_name
9260 (go_symbol_package_name (sym));
9261
9262 if (this_package_name == NULL)
9263 continue;
9264 if (package_name == NULL)
9265 package_name = std::move (this_package_name);
9266 else
9267 {
9268 struct objfile *objfile = cu->per_objfile->objfile;
9269 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9270 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9271 (symbol_symtab (sym) != NULL
9272 ? symtab_to_filename_for_display
9273 (symbol_symtab (sym))
9274 : objfile_name (objfile)),
9275 this_package_name.get (), package_name.get ());
9276 }
9277 }
9278 }
9279 }
9280
9281 if (package_name != NULL)
9282 {
9283 struct objfile *objfile = cu->per_objfile->objfile;
9284 const char *saved_package_name = objfile->intern (package_name.get ());
9285 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9286 saved_package_name);
9287 struct symbol *sym;
9288
9289 sym = new (&objfile->objfile_obstack) symbol;
9290 sym->set_language (language_go, &objfile->objfile_obstack);
9291 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9292 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9293 e.g., "main" finds the "main" module and not C's main(). */
9294 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9295 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9296 SYMBOL_TYPE (sym) = type;
9297
9298 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9299 }
9300 }
9301
9302 /* Allocate a fully-qualified name consisting of the two parts on the
9303 obstack. */
9304
9305 static const char *
9306 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9307 {
9308 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9309 }
9310
9311 /* A helper that allocates a variant part to attach to a Rust enum
9312 type. OBSTACK is where the results should be allocated. TYPE is
9313 the type we're processing. DISCRIMINANT_INDEX is the index of the
9314 discriminant. It must be the index of one of the fields of TYPE.
9315 DEFAULT_INDEX is the index of the default field; or -1 if there is
9316 no default. RANGES is indexed by "effective" field number (the
9317 field index, but omitting the discriminant and default fields) and
9318 must hold the discriminant values used by the variants. Note that
9319 RANGES must have a lifetime at least as long as OBSTACK -- either
9320 already allocated on it, or static. */
9321
9322 static void
9323 alloc_rust_variant (struct obstack *obstack, struct type *type,
9324 int discriminant_index, int default_index,
9325 gdb::array_view<discriminant_range> ranges)
9326 {
9327 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9328 must be handled by the caller. */
9329 gdb_assert (discriminant_index >= 0
9330 && discriminant_index < type->num_fields ());
9331 gdb_assert (default_index == -1
9332 || (default_index >= 0 && default_index < type->num_fields ()));
9333
9334 /* We have one variant for each non-discriminant field. */
9335 int n_variants = type->num_fields () - 1;
9336
9337 variant *variants = new (obstack) variant[n_variants];
9338 int var_idx = 0;
9339 int range_idx = 0;
9340 for (int i = 0; i < type->num_fields (); ++i)
9341 {
9342 if (i == discriminant_index)
9343 continue;
9344
9345 variants[var_idx].first_field = i;
9346 variants[var_idx].last_field = i + 1;
9347
9348 /* The default field does not need a range, but other fields do.
9349 We skipped the discriminant above. */
9350 if (i != default_index)
9351 {
9352 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9353 ++range_idx;
9354 }
9355
9356 ++var_idx;
9357 }
9358
9359 gdb_assert (range_idx == ranges.size ());
9360 gdb_assert (var_idx == n_variants);
9361
9362 variant_part *part = new (obstack) variant_part;
9363 part->discriminant_index = discriminant_index;
9364 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9365 discriminant_index));
9366 part->variants = gdb::array_view<variant> (variants, n_variants);
9367
9368 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9369 gdb::array_view<variant_part> *prop_value
9370 = new (storage) gdb::array_view<variant_part> (part, 1);
9371
9372 struct dynamic_prop prop;
9373 prop.kind = PROP_VARIANT_PARTS;
9374 prop.data.variant_parts = prop_value;
9375
9376 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9377 }
9378
9379 /* Some versions of rustc emitted enums in an unusual way.
9380
9381 Ordinary enums were emitted as unions. The first element of each
9382 structure in the union was named "RUST$ENUM$DISR". This element
9383 held the discriminant.
9384
9385 These versions of Rust also implemented the "non-zero"
9386 optimization. When the enum had two values, and one is empty and
9387 the other holds a pointer that cannot be zero, the pointer is used
9388 as the discriminant, with a zero value meaning the empty variant.
9389 Here, the union's first member is of the form
9390 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9391 where the fieldnos are the indices of the fields that should be
9392 traversed in order to find the field (which may be several fields deep)
9393 and the variantname is the name of the variant of the case when the
9394 field is zero.
9395
9396 This function recognizes whether TYPE is of one of these forms,
9397 and, if so, smashes it to be a variant type. */
9398
9399 static void
9400 quirk_rust_enum (struct type *type, struct objfile *objfile)
9401 {
9402 gdb_assert (type->code () == TYPE_CODE_UNION);
9403
9404 /* We don't need to deal with empty enums. */
9405 if (type->num_fields () == 0)
9406 return;
9407
9408 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9409 if (type->num_fields () == 1
9410 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9411 {
9412 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9413
9414 /* Decode the field name to find the offset of the
9415 discriminant. */
9416 ULONGEST bit_offset = 0;
9417 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9418 while (name[0] >= '0' && name[0] <= '9')
9419 {
9420 char *tail;
9421 unsigned long index = strtoul (name, &tail, 10);
9422 name = tail;
9423 if (*name != '$'
9424 || index >= field_type->num_fields ()
9425 || (TYPE_FIELD_LOC_KIND (field_type, index)
9426 != FIELD_LOC_KIND_BITPOS))
9427 {
9428 complaint (_("Could not parse Rust enum encoding string \"%s\""
9429 "[in module %s]"),
9430 TYPE_FIELD_NAME (type, 0),
9431 objfile_name (objfile));
9432 return;
9433 }
9434 ++name;
9435
9436 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9437 field_type = TYPE_FIELD_TYPE (field_type, index);
9438 }
9439
9440 /* Smash this type to be a structure type. We have to do this
9441 because the type has already been recorded. */
9442 type->set_code (TYPE_CODE_STRUCT);
9443 type->set_num_fields (3);
9444 /* Save the field we care about. */
9445 struct field saved_field = type->field (0);
9446 type->set_fields
9447 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9448
9449 /* Put the discriminant at index 0. */
9450 TYPE_FIELD_TYPE (type, 0) = field_type;
9451 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9452 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9453 SET_FIELD_BITPOS (type->field (0), bit_offset);
9454
9455 /* The order of fields doesn't really matter, so put the real
9456 field at index 1 and the data-less field at index 2. */
9457 type->field (1) = saved_field;
9458 TYPE_FIELD_NAME (type, 1)
9459 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9460 TYPE_FIELD_TYPE (type, 1)->set_name
9461 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9462 TYPE_FIELD_NAME (type, 1)));
9463
9464 const char *dataless_name
9465 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9466 name);
9467 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9468 dataless_name);
9469 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9470 /* NAME points into the original discriminant name, which
9471 already has the correct lifetime. */
9472 TYPE_FIELD_NAME (type, 2) = name;
9473 SET_FIELD_BITPOS (type->field (2), 0);
9474
9475 /* Indicate that this is a variant type. */
9476 static discriminant_range ranges[1] = { { 0, 0 } };
9477 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9478 }
9479 /* A union with a single anonymous field is probably an old-style
9480 univariant enum. */
9481 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9482 {
9483 /* Smash this type to be a structure type. We have to do this
9484 because the type has already been recorded. */
9485 type->set_code (TYPE_CODE_STRUCT);
9486
9487 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9488 const char *variant_name
9489 = rust_last_path_segment (field_type->name ());
9490 TYPE_FIELD_NAME (type, 0) = variant_name;
9491 field_type->set_name
9492 (rust_fully_qualify (&objfile->objfile_obstack,
9493 type->name (), variant_name));
9494 }
9495 else
9496 {
9497 struct type *disr_type = nullptr;
9498 for (int i = 0; i < type->num_fields (); ++i)
9499 {
9500 disr_type = TYPE_FIELD_TYPE (type, i);
9501
9502 if (disr_type->code () != TYPE_CODE_STRUCT)
9503 {
9504 /* All fields of a true enum will be structs. */
9505 return;
9506 }
9507 else if (disr_type->num_fields () == 0)
9508 {
9509 /* Could be data-less variant, so keep going. */
9510 disr_type = nullptr;
9511 }
9512 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9513 "RUST$ENUM$DISR") != 0)
9514 {
9515 /* Not a Rust enum. */
9516 return;
9517 }
9518 else
9519 {
9520 /* Found one. */
9521 break;
9522 }
9523 }
9524
9525 /* If we got here without a discriminant, then it's probably
9526 just a union. */
9527 if (disr_type == nullptr)
9528 return;
9529
9530 /* Smash this type to be a structure type. We have to do this
9531 because the type has already been recorded. */
9532 type->set_code (TYPE_CODE_STRUCT);
9533
9534 /* Make space for the discriminant field. */
9535 struct field *disr_field = &disr_type->field (0);
9536 field *new_fields
9537 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9538 * sizeof (struct field)));
9539 memcpy (new_fields + 1, type->fields (),
9540 type->num_fields () * sizeof (struct field));
9541 type->set_fields (new_fields);
9542 type->set_num_fields (type->num_fields () + 1);
9543
9544 /* Install the discriminant at index 0 in the union. */
9545 type->field (0) = *disr_field;
9546 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9547 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9548
9549 /* We need a way to find the correct discriminant given a
9550 variant name. For convenience we build a map here. */
9551 struct type *enum_type = FIELD_TYPE (*disr_field);
9552 std::unordered_map<std::string, ULONGEST> discriminant_map;
9553 for (int i = 0; i < enum_type->num_fields (); ++i)
9554 {
9555 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9556 {
9557 const char *name
9558 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9559 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9560 }
9561 }
9562
9563 int n_fields = type->num_fields ();
9564 /* We don't need a range entry for the discriminant, but we do
9565 need one for every other field, as there is no default
9566 variant. */
9567 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9568 discriminant_range,
9569 n_fields - 1);
9570 /* Skip the discriminant here. */
9571 for (int i = 1; i < n_fields; ++i)
9572 {
9573 /* Find the final word in the name of this variant's type.
9574 That name can be used to look up the correct
9575 discriminant. */
9576 const char *variant_name
9577 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9578
9579 auto iter = discriminant_map.find (variant_name);
9580 if (iter != discriminant_map.end ())
9581 {
9582 ranges[i].low = iter->second;
9583 ranges[i].high = iter->second;
9584 }
9585
9586 /* Remove the discriminant field, if it exists. */
9587 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9588 if (sub_type->num_fields () > 0)
9589 {
9590 sub_type->set_num_fields (sub_type->num_fields () - 1);
9591 sub_type->set_fields (sub_type->fields () + 1);
9592 }
9593 TYPE_FIELD_NAME (type, i) = variant_name;
9594 sub_type->set_name
9595 (rust_fully_qualify (&objfile->objfile_obstack,
9596 type->name (), variant_name));
9597 }
9598
9599 /* Indicate that this is a variant type. */
9600 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9601 gdb::array_view<discriminant_range> (ranges,
9602 n_fields - 1));
9603 }
9604 }
9605
9606 /* Rewrite some Rust unions to be structures with variants parts. */
9607
9608 static void
9609 rust_union_quirks (struct dwarf2_cu *cu)
9610 {
9611 gdb_assert (cu->language == language_rust);
9612 for (type *type_ : cu->rust_unions)
9613 quirk_rust_enum (type_, cu->per_objfile->objfile);
9614 /* We don't need this any more. */
9615 cu->rust_unions.clear ();
9616 }
9617
9618 /* See read.h. */
9619
9620 type_unit_group_unshareable *
9621 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9622 {
9623 auto iter = this->m_type_units.find (tu_group);
9624 if (iter != this->m_type_units.end ())
9625 return iter->second.get ();
9626
9627 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9628 type_unit_group_unshareable *result = uniq.get ();
9629 this->m_type_units[tu_group] = std::move (uniq);
9630 return result;
9631 }
9632
9633 /* A helper function for computing the list of all symbol tables
9634 included by PER_CU. */
9635
9636 static void
9637 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9638 htab_t all_children, htab_t all_type_symtabs,
9639 dwarf2_per_cu_data *per_cu,
9640 dwarf2_per_objfile *per_objfile,
9641 struct compunit_symtab *immediate_parent)
9642 {
9643 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9644 if (*slot != NULL)
9645 {
9646 /* This inclusion and its children have been processed. */
9647 return;
9648 }
9649
9650 *slot = per_cu;
9651
9652 /* Only add a CU if it has a symbol table. */
9653 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9654 if (cust != NULL)
9655 {
9656 /* If this is a type unit only add its symbol table if we haven't
9657 seen it yet (type unit per_cu's can share symtabs). */
9658 if (per_cu->is_debug_types)
9659 {
9660 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9661 if (*slot == NULL)
9662 {
9663 *slot = cust;
9664 result->push_back (cust);
9665 if (cust->user == NULL)
9666 cust->user = immediate_parent;
9667 }
9668 }
9669 else
9670 {
9671 result->push_back (cust);
9672 if (cust->user == NULL)
9673 cust->user = immediate_parent;
9674 }
9675 }
9676
9677 if (!per_cu->imported_symtabs_empty ())
9678 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9679 {
9680 recursively_compute_inclusions (result, all_children,
9681 all_type_symtabs, ptr, per_objfile,
9682 cust);
9683 }
9684 }
9685
9686 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9687 PER_CU. */
9688
9689 static void
9690 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9691 dwarf2_per_objfile *per_objfile)
9692 {
9693 gdb_assert (! per_cu->is_debug_types);
9694
9695 if (!per_cu->imported_symtabs_empty ())
9696 {
9697 int len;
9698 std::vector<compunit_symtab *> result_symtabs;
9699 htab_t all_children, all_type_symtabs;
9700 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9701
9702 /* If we don't have a symtab, we can just skip this case. */
9703 if (cust == NULL)
9704 return;
9705
9706 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9707 NULL, xcalloc, xfree);
9708 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9709 NULL, xcalloc, xfree);
9710
9711 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9712 {
9713 recursively_compute_inclusions (&result_symtabs, all_children,
9714 all_type_symtabs, ptr, per_objfile,
9715 cust);
9716 }
9717
9718 /* Now we have a transitive closure of all the included symtabs. */
9719 len = result_symtabs.size ();
9720 cust->includes
9721 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9722 struct compunit_symtab *, len + 1);
9723 memcpy (cust->includes, result_symtabs.data (),
9724 len * sizeof (compunit_symtab *));
9725 cust->includes[len] = NULL;
9726
9727 htab_delete (all_children);
9728 htab_delete (all_type_symtabs);
9729 }
9730 }
9731
9732 /* Compute the 'includes' field for the symtabs of all the CUs we just
9733 read. */
9734
9735 static void
9736 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9737 {
9738 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9739 {
9740 if (! iter->is_debug_types)
9741 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9742 }
9743
9744 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9745 }
9746
9747 /* Generate full symbol information for PER_CU, whose DIEs have
9748 already been loaded into memory. */
9749
9750 static void
9751 process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9752 dwarf2_per_objfile *dwarf2_per_objfile,
9753 enum language pretend_language)
9754 {
9755 struct dwarf2_cu *cu = per_cu->cu;
9756 struct objfile *objfile = dwarf2_per_objfile->objfile;
9757 struct gdbarch *gdbarch = objfile->arch ();
9758 CORE_ADDR lowpc, highpc;
9759 struct compunit_symtab *cust;
9760 CORE_ADDR baseaddr;
9761 struct block *static_block;
9762 CORE_ADDR addr;
9763
9764 baseaddr = objfile->text_section_offset ();
9765
9766 /* Clear the list here in case something was left over. */
9767 cu->method_list.clear ();
9768
9769 cu->language = pretend_language;
9770 cu->language_defn = language_def (cu->language);
9771
9772 /* Do line number decoding in read_file_scope () */
9773 process_die (cu->dies, cu);
9774
9775 /* For now fudge the Go package. */
9776 if (cu->language == language_go)
9777 fixup_go_packaging (cu);
9778
9779 /* Now that we have processed all the DIEs in the CU, all the types
9780 should be complete, and it should now be safe to compute all of the
9781 physnames. */
9782 compute_delayed_physnames (cu);
9783
9784 if (cu->language == language_rust)
9785 rust_union_quirks (cu);
9786
9787 /* Some compilers don't define a DW_AT_high_pc attribute for the
9788 compilation unit. If the DW_AT_high_pc is missing, synthesize
9789 it, by scanning the DIE's below the compilation unit. */
9790 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9791
9792 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9793 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9794
9795 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9796 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9797 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9798 addrmap to help ensure it has an accurate map of pc values belonging to
9799 this comp unit. */
9800 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9801
9802 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9803 SECT_OFF_TEXT (objfile),
9804 0);
9805
9806 if (cust != NULL)
9807 {
9808 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9809
9810 /* Set symtab language to language from DW_AT_language. If the
9811 compilation is from a C file generated by language preprocessors, do
9812 not set the language if it was already deduced by start_subfile. */
9813 if (!(cu->language == language_c
9814 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9815 COMPUNIT_FILETABS (cust)->language = cu->language;
9816
9817 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9818 produce DW_AT_location with location lists but it can be possibly
9819 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9820 there were bugs in prologue debug info, fixed later in GCC-4.5
9821 by "unwind info for epilogues" patch (which is not directly related).
9822
9823 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9824 needed, it would be wrong due to missing DW_AT_producer there.
9825
9826 Still one can confuse GDB by using non-standard GCC compilation
9827 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9828 */
9829 if (cu->has_loclist && gcc_4_minor >= 5)
9830 cust->locations_valid = 1;
9831
9832 if (gcc_4_minor >= 5)
9833 cust->epilogue_unwind_valid = 1;
9834
9835 cust->call_site_htab = cu->call_site_htab;
9836 }
9837
9838 dwarf2_per_objfile->set_symtab (per_cu, cust);
9839
9840 /* Push it for inclusion processing later. */
9841 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9842
9843 /* Not needed any more. */
9844 cu->reset_builder ();
9845 }
9846
9847 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9848 already been loaded into memory. */
9849
9850 static void
9851 process_full_type_unit (dwarf2_per_cu_data *per_cu,
9852 dwarf2_per_objfile *dwarf2_per_objfile,
9853 enum language pretend_language)
9854 {
9855 struct dwarf2_cu *cu = per_cu->cu;
9856 struct objfile *objfile = dwarf2_per_objfile->objfile;
9857 struct compunit_symtab *cust;
9858 struct signatured_type *sig_type;
9859
9860 gdb_assert (per_cu->is_debug_types);
9861 sig_type = (struct signatured_type *) per_cu;
9862
9863 /* Clear the list here in case something was left over. */
9864 cu->method_list.clear ();
9865
9866 cu->language = pretend_language;
9867 cu->language_defn = language_def (cu->language);
9868
9869 /* The symbol tables are set up in read_type_unit_scope. */
9870 process_die (cu->dies, cu);
9871
9872 /* For now fudge the Go package. */
9873 if (cu->language == language_go)
9874 fixup_go_packaging (cu);
9875
9876 /* Now that we have processed all the DIEs in the CU, all the types
9877 should be complete, and it should now be safe to compute all of the
9878 physnames. */
9879 compute_delayed_physnames (cu);
9880
9881 if (cu->language == language_rust)
9882 rust_union_quirks (cu);
9883
9884 /* TUs share symbol tables.
9885 If this is the first TU to use this symtab, complete the construction
9886 of it with end_expandable_symtab. Otherwise, complete the addition of
9887 this TU's symbols to the existing symtab. */
9888 type_unit_group_unshareable *tug_unshare =
9889 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9890 if (tug_unshare->compunit_symtab == NULL)
9891 {
9892 buildsym_compunit *builder = cu->get_builder ();
9893 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9894 tug_unshare->compunit_symtab = cust;
9895
9896 if (cust != NULL)
9897 {
9898 /* Set symtab language to language from DW_AT_language. If the
9899 compilation is from a C file generated by language preprocessors,
9900 do not set the language if it was already deduced by
9901 start_subfile. */
9902 if (!(cu->language == language_c
9903 && COMPUNIT_FILETABS (cust)->language != language_c))
9904 COMPUNIT_FILETABS (cust)->language = cu->language;
9905 }
9906 }
9907 else
9908 {
9909 cu->get_builder ()->augment_type_symtab ();
9910 cust = tug_unshare->compunit_symtab;
9911 }
9912
9913 dwarf2_per_objfile->set_symtab (per_cu, cust);
9914
9915 /* Not needed any more. */
9916 cu->reset_builder ();
9917 }
9918
9919 /* Process an imported unit DIE. */
9920
9921 static void
9922 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9923 {
9924 struct attribute *attr;
9925
9926 /* For now we don't handle imported units in type units. */
9927 if (cu->per_cu->is_debug_types)
9928 {
9929 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9930 " supported in type units [in module %s]"),
9931 objfile_name (cu->per_objfile->objfile));
9932 }
9933
9934 attr = dwarf2_attr (die, DW_AT_import, cu);
9935 if (attr != NULL)
9936 {
9937 sect_offset sect_off = attr->get_ref_die_offset ();
9938 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9939 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9940 dwarf2_per_cu_data *per_cu
9941 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9942
9943 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9944 into another compilation unit, at root level. Regard this as a hint,
9945 and ignore it. */
9946 if (die->parent && die->parent->parent == NULL
9947 && per_cu->unit_type == DW_UT_compile
9948 && per_cu->lang == language_cplus)
9949 return;
9950
9951 /* If necessary, add it to the queue and load its DIEs. */
9952 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9953 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9954
9955 cu->per_cu->imported_symtabs_push (per_cu);
9956 }
9957 }
9958
9959 /* RAII object that represents a process_die scope: i.e.,
9960 starts/finishes processing a DIE. */
9961 class process_die_scope
9962 {
9963 public:
9964 process_die_scope (die_info *die, dwarf2_cu *cu)
9965 : m_die (die), m_cu (cu)
9966 {
9967 /* We should only be processing DIEs not already in process. */
9968 gdb_assert (!m_die->in_process);
9969 m_die->in_process = true;
9970 }
9971
9972 ~process_die_scope ()
9973 {
9974 m_die->in_process = false;
9975
9976 /* If we're done processing the DIE for the CU that owns the line
9977 header, we don't need the line header anymore. */
9978 if (m_cu->line_header_die_owner == m_die)
9979 {
9980 delete m_cu->line_header;
9981 m_cu->line_header = NULL;
9982 m_cu->line_header_die_owner = NULL;
9983 }
9984 }
9985
9986 private:
9987 die_info *m_die;
9988 dwarf2_cu *m_cu;
9989 };
9990
9991 /* Process a die and its children. */
9992
9993 static void
9994 process_die (struct die_info *die, struct dwarf2_cu *cu)
9995 {
9996 process_die_scope scope (die, cu);
9997
9998 switch (die->tag)
9999 {
10000 case DW_TAG_padding:
10001 break;
10002 case DW_TAG_compile_unit:
10003 case DW_TAG_partial_unit:
10004 read_file_scope (die, cu);
10005 break;
10006 case DW_TAG_type_unit:
10007 read_type_unit_scope (die, cu);
10008 break;
10009 case DW_TAG_subprogram:
10010 /* Nested subprograms in Fortran get a prefix. */
10011 if (cu->language == language_fortran
10012 && die->parent != NULL
10013 && die->parent->tag == DW_TAG_subprogram)
10014 cu->processing_has_namespace_info = true;
10015 /* Fall through. */
10016 case DW_TAG_inlined_subroutine:
10017 read_func_scope (die, cu);
10018 break;
10019 case DW_TAG_lexical_block:
10020 case DW_TAG_try_block:
10021 case DW_TAG_catch_block:
10022 read_lexical_block_scope (die, cu);
10023 break;
10024 case DW_TAG_call_site:
10025 case DW_TAG_GNU_call_site:
10026 read_call_site_scope (die, cu);
10027 break;
10028 case DW_TAG_class_type:
10029 case DW_TAG_interface_type:
10030 case DW_TAG_structure_type:
10031 case DW_TAG_union_type:
10032 process_structure_scope (die, cu);
10033 break;
10034 case DW_TAG_enumeration_type:
10035 process_enumeration_scope (die, cu);
10036 break;
10037
10038 /* These dies have a type, but processing them does not create
10039 a symbol or recurse to process the children. Therefore we can
10040 read them on-demand through read_type_die. */
10041 case DW_TAG_subroutine_type:
10042 case DW_TAG_set_type:
10043 case DW_TAG_array_type:
10044 case DW_TAG_pointer_type:
10045 case DW_TAG_ptr_to_member_type:
10046 case DW_TAG_reference_type:
10047 case DW_TAG_rvalue_reference_type:
10048 case DW_TAG_string_type:
10049 break;
10050
10051 case DW_TAG_base_type:
10052 case DW_TAG_subrange_type:
10053 case DW_TAG_typedef:
10054 /* Add a typedef symbol for the type definition, if it has a
10055 DW_AT_name. */
10056 new_symbol (die, read_type_die (die, cu), cu);
10057 break;
10058 case DW_TAG_common_block:
10059 read_common_block (die, cu);
10060 break;
10061 case DW_TAG_common_inclusion:
10062 break;
10063 case DW_TAG_namespace:
10064 cu->processing_has_namespace_info = true;
10065 read_namespace (die, cu);
10066 break;
10067 case DW_TAG_module:
10068 cu->processing_has_namespace_info = true;
10069 read_module (die, cu);
10070 break;
10071 case DW_TAG_imported_declaration:
10072 cu->processing_has_namespace_info = true;
10073 if (read_namespace_alias (die, cu))
10074 break;
10075 /* The declaration is not a global namespace alias. */
10076 /* Fall through. */
10077 case DW_TAG_imported_module:
10078 cu->processing_has_namespace_info = true;
10079 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10080 || cu->language != language_fortran))
10081 complaint (_("Tag '%s' has unexpected children"),
10082 dwarf_tag_name (die->tag));
10083 read_import_statement (die, cu);
10084 break;
10085
10086 case DW_TAG_imported_unit:
10087 process_imported_unit_die (die, cu);
10088 break;
10089
10090 case DW_TAG_variable:
10091 read_variable (die, cu);
10092 break;
10093
10094 default:
10095 new_symbol (die, NULL, cu);
10096 break;
10097 }
10098 }
10099 \f
10100 /* DWARF name computation. */
10101
10102 /* A helper function for dwarf2_compute_name which determines whether DIE
10103 needs to have the name of the scope prepended to the name listed in the
10104 die. */
10105
10106 static int
10107 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10108 {
10109 struct attribute *attr;
10110
10111 switch (die->tag)
10112 {
10113 case DW_TAG_namespace:
10114 case DW_TAG_typedef:
10115 case DW_TAG_class_type:
10116 case DW_TAG_interface_type:
10117 case DW_TAG_structure_type:
10118 case DW_TAG_union_type:
10119 case DW_TAG_enumeration_type:
10120 case DW_TAG_enumerator:
10121 case DW_TAG_subprogram:
10122 case DW_TAG_inlined_subroutine:
10123 case DW_TAG_member:
10124 case DW_TAG_imported_declaration:
10125 return 1;
10126
10127 case DW_TAG_variable:
10128 case DW_TAG_constant:
10129 /* We only need to prefix "globally" visible variables. These include
10130 any variable marked with DW_AT_external or any variable that
10131 lives in a namespace. [Variables in anonymous namespaces
10132 require prefixing, but they are not DW_AT_external.] */
10133
10134 if (dwarf2_attr (die, DW_AT_specification, cu))
10135 {
10136 struct dwarf2_cu *spec_cu = cu;
10137
10138 return die_needs_namespace (die_specification (die, &spec_cu),
10139 spec_cu);
10140 }
10141
10142 attr = dwarf2_attr (die, DW_AT_external, cu);
10143 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10144 && die->parent->tag != DW_TAG_module)
10145 return 0;
10146 /* A variable in a lexical block of some kind does not need a
10147 namespace, even though in C++ such variables may be external
10148 and have a mangled name. */
10149 if (die->parent->tag == DW_TAG_lexical_block
10150 || die->parent->tag == DW_TAG_try_block
10151 || die->parent->tag == DW_TAG_catch_block
10152 || die->parent->tag == DW_TAG_subprogram)
10153 return 0;
10154 return 1;
10155
10156 default:
10157 return 0;
10158 }
10159 }
10160
10161 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10162 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10163 defined for the given DIE. */
10164
10165 static struct attribute *
10166 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10167 {
10168 struct attribute *attr;
10169
10170 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10171 if (attr == NULL)
10172 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10173
10174 return attr;
10175 }
10176
10177 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10178 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10179 defined for the given DIE. */
10180
10181 static const char *
10182 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10183 {
10184 const char *linkage_name;
10185
10186 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10187 if (linkage_name == NULL)
10188 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10189
10190 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10191 See https://github.com/rust-lang/rust/issues/32925. */
10192 if (cu->language == language_rust && linkage_name != NULL
10193 && strchr (linkage_name, '{') != NULL)
10194 linkage_name = NULL;
10195
10196 return linkage_name;
10197 }
10198
10199 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10200 compute the physname for the object, which include a method's:
10201 - formal parameters (C++),
10202 - receiver type (Go),
10203
10204 The term "physname" is a bit confusing.
10205 For C++, for example, it is the demangled name.
10206 For Go, for example, it's the mangled name.
10207
10208 For Ada, return the DIE's linkage name rather than the fully qualified
10209 name. PHYSNAME is ignored..
10210
10211 The result is allocated on the objfile->per_bfd's obstack and
10212 canonicalized. */
10213
10214 static const char *
10215 dwarf2_compute_name (const char *name,
10216 struct die_info *die, struct dwarf2_cu *cu,
10217 int physname)
10218 {
10219 struct objfile *objfile = cu->per_objfile->objfile;
10220
10221 if (name == NULL)
10222 name = dwarf2_name (die, cu);
10223
10224 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10225 but otherwise compute it by typename_concat inside GDB.
10226 FIXME: Actually this is not really true, or at least not always true.
10227 It's all very confusing. compute_and_set_names doesn't try to demangle
10228 Fortran names because there is no mangling standard. So new_symbol
10229 will set the demangled name to the result of dwarf2_full_name, and it is
10230 the demangled name that GDB uses if it exists. */
10231 if (cu->language == language_ada
10232 || (cu->language == language_fortran && physname))
10233 {
10234 /* For Ada unit, we prefer the linkage name over the name, as
10235 the former contains the exported name, which the user expects
10236 to be able to reference. Ideally, we want the user to be able
10237 to reference this entity using either natural or linkage name,
10238 but we haven't started looking at this enhancement yet. */
10239 const char *linkage_name = dw2_linkage_name (die, cu);
10240
10241 if (linkage_name != NULL)
10242 return linkage_name;
10243 }
10244
10245 /* These are the only languages we know how to qualify names in. */
10246 if (name != NULL
10247 && (cu->language == language_cplus
10248 || cu->language == language_fortran || cu->language == language_d
10249 || cu->language == language_rust))
10250 {
10251 if (die_needs_namespace (die, cu))
10252 {
10253 const char *prefix;
10254 const char *canonical_name = NULL;
10255
10256 string_file buf;
10257
10258 prefix = determine_prefix (die, cu);
10259 if (*prefix != '\0')
10260 {
10261 gdb::unique_xmalloc_ptr<char> prefixed_name
10262 (typename_concat (NULL, prefix, name, physname, cu));
10263
10264 buf.puts (prefixed_name.get ());
10265 }
10266 else
10267 buf.puts (name);
10268
10269 /* Template parameters may be specified in the DIE's DW_AT_name, or
10270 as children with DW_TAG_template_type_param or
10271 DW_TAG_value_type_param. If the latter, add them to the name
10272 here. If the name already has template parameters, then
10273 skip this step; some versions of GCC emit both, and
10274 it is more efficient to use the pre-computed name.
10275
10276 Something to keep in mind about this process: it is very
10277 unlikely, or in some cases downright impossible, to produce
10278 something that will match the mangled name of a function.
10279 If the definition of the function has the same debug info,
10280 we should be able to match up with it anyway. But fallbacks
10281 using the minimal symbol, for instance to find a method
10282 implemented in a stripped copy of libstdc++, will not work.
10283 If we do not have debug info for the definition, we will have to
10284 match them up some other way.
10285
10286 When we do name matching there is a related problem with function
10287 templates; two instantiated function templates are allowed to
10288 differ only by their return types, which we do not add here. */
10289
10290 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10291 {
10292 struct attribute *attr;
10293 struct die_info *child;
10294 int first = 1;
10295
10296 die->building_fullname = 1;
10297
10298 for (child = die->child; child != NULL; child = child->sibling)
10299 {
10300 struct type *type;
10301 LONGEST value;
10302 const gdb_byte *bytes;
10303 struct dwarf2_locexpr_baton *baton;
10304 struct value *v;
10305
10306 if (child->tag != DW_TAG_template_type_param
10307 && child->tag != DW_TAG_template_value_param)
10308 continue;
10309
10310 if (first)
10311 {
10312 buf.puts ("<");
10313 first = 0;
10314 }
10315 else
10316 buf.puts (", ");
10317
10318 attr = dwarf2_attr (child, DW_AT_type, cu);
10319 if (attr == NULL)
10320 {
10321 complaint (_("template parameter missing DW_AT_type"));
10322 buf.puts ("UNKNOWN_TYPE");
10323 continue;
10324 }
10325 type = die_type (child, cu);
10326
10327 if (child->tag == DW_TAG_template_type_param)
10328 {
10329 c_print_type (type, "", &buf, -1, 0, cu->language,
10330 &type_print_raw_options);
10331 continue;
10332 }
10333
10334 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10335 if (attr == NULL)
10336 {
10337 complaint (_("template parameter missing "
10338 "DW_AT_const_value"));
10339 buf.puts ("UNKNOWN_VALUE");
10340 continue;
10341 }
10342
10343 dwarf2_const_value_attr (attr, type, name,
10344 &cu->comp_unit_obstack, cu,
10345 &value, &bytes, &baton);
10346
10347 if (TYPE_NOSIGN (type))
10348 /* GDB prints characters as NUMBER 'CHAR'. If that's
10349 changed, this can use value_print instead. */
10350 c_printchar (value, type, &buf);
10351 else
10352 {
10353 struct value_print_options opts;
10354
10355 if (baton != NULL)
10356 v = dwarf2_evaluate_loc_desc (type, NULL,
10357 baton->data,
10358 baton->size,
10359 baton->per_cu,
10360 baton->per_objfile);
10361 else if (bytes != NULL)
10362 {
10363 v = allocate_value (type);
10364 memcpy (value_contents_writeable (v), bytes,
10365 TYPE_LENGTH (type));
10366 }
10367 else
10368 v = value_from_longest (type, value);
10369
10370 /* Specify decimal so that we do not depend on
10371 the radix. */
10372 get_formatted_print_options (&opts, 'd');
10373 opts.raw = 1;
10374 value_print (v, &buf, &opts);
10375 release_value (v);
10376 }
10377 }
10378
10379 die->building_fullname = 0;
10380
10381 if (!first)
10382 {
10383 /* Close the argument list, with a space if necessary
10384 (nested templates). */
10385 if (!buf.empty () && buf.string ().back () == '>')
10386 buf.puts (" >");
10387 else
10388 buf.puts (">");
10389 }
10390 }
10391
10392 /* For C++ methods, append formal parameter type
10393 information, if PHYSNAME. */
10394
10395 if (physname && die->tag == DW_TAG_subprogram
10396 && cu->language == language_cplus)
10397 {
10398 struct type *type = read_type_die (die, cu);
10399
10400 c_type_print_args (type, &buf, 1, cu->language,
10401 &type_print_raw_options);
10402
10403 if (cu->language == language_cplus)
10404 {
10405 /* Assume that an artificial first parameter is
10406 "this", but do not crash if it is not. RealView
10407 marks unnamed (and thus unused) parameters as
10408 artificial; there is no way to differentiate
10409 the two cases. */
10410 if (type->num_fields () > 0
10411 && TYPE_FIELD_ARTIFICIAL (type, 0)
10412 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10413 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10414 0))))
10415 buf.puts (" const");
10416 }
10417 }
10418
10419 const std::string &intermediate_name = buf.string ();
10420
10421 if (cu->language == language_cplus)
10422 canonical_name
10423 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10424 objfile);
10425
10426 /* If we only computed INTERMEDIATE_NAME, or if
10427 INTERMEDIATE_NAME is already canonical, then we need to
10428 intern it. */
10429 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10430 name = objfile->intern (intermediate_name);
10431 else
10432 name = canonical_name;
10433 }
10434 }
10435
10436 return name;
10437 }
10438
10439 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10440 If scope qualifiers are appropriate they will be added. The result
10441 will be allocated on the storage_obstack, or NULL if the DIE does
10442 not have a name. NAME may either be from a previous call to
10443 dwarf2_name or NULL.
10444
10445 The output string will be canonicalized (if C++). */
10446
10447 static const char *
10448 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10449 {
10450 return dwarf2_compute_name (name, die, cu, 0);
10451 }
10452
10453 /* Construct a physname for the given DIE in CU. NAME may either be
10454 from a previous call to dwarf2_name or NULL. The result will be
10455 allocated on the objfile_objstack or NULL if the DIE does not have a
10456 name.
10457
10458 The output string will be canonicalized (if C++). */
10459
10460 static const char *
10461 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10462 {
10463 struct objfile *objfile = cu->per_objfile->objfile;
10464 const char *retval, *mangled = NULL, *canon = NULL;
10465 int need_copy = 1;
10466
10467 /* In this case dwarf2_compute_name is just a shortcut not building anything
10468 on its own. */
10469 if (!die_needs_namespace (die, cu))
10470 return dwarf2_compute_name (name, die, cu, 1);
10471
10472 if (cu->language != language_rust)
10473 mangled = dw2_linkage_name (die, cu);
10474
10475 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10476 has computed. */
10477 gdb::unique_xmalloc_ptr<char> demangled;
10478 if (mangled != NULL)
10479 {
10480
10481 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10482 {
10483 /* Do nothing (do not demangle the symbol name). */
10484 }
10485 else if (cu->language == language_go)
10486 {
10487 /* This is a lie, but we already lie to the caller new_symbol.
10488 new_symbol assumes we return the mangled name.
10489 This just undoes that lie until things are cleaned up. */
10490 }
10491 else
10492 {
10493 /* Use DMGL_RET_DROP for C++ template functions to suppress
10494 their return type. It is easier for GDB users to search
10495 for such functions as `name(params)' than `long name(params)'.
10496 In such case the minimal symbol names do not match the full
10497 symbol names but for template functions there is never a need
10498 to look up their definition from their declaration so
10499 the only disadvantage remains the minimal symbol variant
10500 `long name(params)' does not have the proper inferior type. */
10501 demangled.reset (gdb_demangle (mangled,
10502 (DMGL_PARAMS | DMGL_ANSI
10503 | DMGL_RET_DROP)));
10504 }
10505 if (demangled)
10506 canon = demangled.get ();
10507 else
10508 {
10509 canon = mangled;
10510 need_copy = 0;
10511 }
10512 }
10513
10514 if (canon == NULL || check_physname)
10515 {
10516 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10517
10518 if (canon != NULL && strcmp (physname, canon) != 0)
10519 {
10520 /* It may not mean a bug in GDB. The compiler could also
10521 compute DW_AT_linkage_name incorrectly. But in such case
10522 GDB would need to be bug-to-bug compatible. */
10523
10524 complaint (_("Computed physname <%s> does not match demangled <%s> "
10525 "(from linkage <%s>) - DIE at %s [in module %s]"),
10526 physname, canon, mangled, sect_offset_str (die->sect_off),
10527 objfile_name (objfile));
10528
10529 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10530 is available here - over computed PHYSNAME. It is safer
10531 against both buggy GDB and buggy compilers. */
10532
10533 retval = canon;
10534 }
10535 else
10536 {
10537 retval = physname;
10538 need_copy = 0;
10539 }
10540 }
10541 else
10542 retval = canon;
10543
10544 if (need_copy)
10545 retval = objfile->intern (retval);
10546
10547 return retval;
10548 }
10549
10550 /* Inspect DIE in CU for a namespace alias. If one exists, record
10551 a new symbol for it.
10552
10553 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10554
10555 static int
10556 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10557 {
10558 struct attribute *attr;
10559
10560 /* If the die does not have a name, this is not a namespace
10561 alias. */
10562 attr = dwarf2_attr (die, DW_AT_name, cu);
10563 if (attr != NULL)
10564 {
10565 int num;
10566 struct die_info *d = die;
10567 struct dwarf2_cu *imported_cu = cu;
10568
10569 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10570 keep inspecting DIEs until we hit the underlying import. */
10571 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10572 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10573 {
10574 attr = dwarf2_attr (d, DW_AT_import, cu);
10575 if (attr == NULL)
10576 break;
10577
10578 d = follow_die_ref (d, attr, &imported_cu);
10579 if (d->tag != DW_TAG_imported_declaration)
10580 break;
10581 }
10582
10583 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10584 {
10585 complaint (_("DIE at %s has too many recursively imported "
10586 "declarations"), sect_offset_str (d->sect_off));
10587 return 0;
10588 }
10589
10590 if (attr != NULL)
10591 {
10592 struct type *type;
10593 sect_offset sect_off = attr->get_ref_die_offset ();
10594
10595 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10596 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10597 {
10598 /* This declaration is a global namespace alias. Add
10599 a symbol for it whose type is the aliased namespace. */
10600 new_symbol (die, type, cu);
10601 return 1;
10602 }
10603 }
10604 }
10605
10606 return 0;
10607 }
10608
10609 /* Return the using directives repository (global or local?) to use in the
10610 current context for CU.
10611
10612 For Ada, imported declarations can materialize renamings, which *may* be
10613 global. However it is impossible (for now?) in DWARF to distinguish
10614 "external" imported declarations and "static" ones. As all imported
10615 declarations seem to be static in all other languages, make them all CU-wide
10616 global only in Ada. */
10617
10618 static struct using_direct **
10619 using_directives (struct dwarf2_cu *cu)
10620 {
10621 if (cu->language == language_ada
10622 && cu->get_builder ()->outermost_context_p ())
10623 return cu->get_builder ()->get_global_using_directives ();
10624 else
10625 return cu->get_builder ()->get_local_using_directives ();
10626 }
10627
10628 /* Read the import statement specified by the given die and record it. */
10629
10630 static void
10631 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10632 {
10633 struct objfile *objfile = cu->per_objfile->objfile;
10634 struct attribute *import_attr;
10635 struct die_info *imported_die, *child_die;
10636 struct dwarf2_cu *imported_cu;
10637 const char *imported_name;
10638 const char *imported_name_prefix;
10639 const char *canonical_name;
10640 const char *import_alias;
10641 const char *imported_declaration = NULL;
10642 const char *import_prefix;
10643 std::vector<const char *> excludes;
10644
10645 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10646 if (import_attr == NULL)
10647 {
10648 complaint (_("Tag '%s' has no DW_AT_import"),
10649 dwarf_tag_name (die->tag));
10650 return;
10651 }
10652
10653 imported_cu = cu;
10654 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10655 imported_name = dwarf2_name (imported_die, imported_cu);
10656 if (imported_name == NULL)
10657 {
10658 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10659
10660 The import in the following code:
10661 namespace A
10662 {
10663 typedef int B;
10664 }
10665
10666 int main ()
10667 {
10668 using A::B;
10669 B b;
10670 return b;
10671 }
10672
10673 ...
10674 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10675 <52> DW_AT_decl_file : 1
10676 <53> DW_AT_decl_line : 6
10677 <54> DW_AT_import : <0x75>
10678 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10679 <59> DW_AT_name : B
10680 <5b> DW_AT_decl_file : 1
10681 <5c> DW_AT_decl_line : 2
10682 <5d> DW_AT_type : <0x6e>
10683 ...
10684 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10685 <76> DW_AT_byte_size : 4
10686 <77> DW_AT_encoding : 5 (signed)
10687
10688 imports the wrong die ( 0x75 instead of 0x58 ).
10689 This case will be ignored until the gcc bug is fixed. */
10690 return;
10691 }
10692
10693 /* Figure out the local name after import. */
10694 import_alias = dwarf2_name (die, cu);
10695
10696 /* Figure out where the statement is being imported to. */
10697 import_prefix = determine_prefix (die, cu);
10698
10699 /* Figure out what the scope of the imported die is and prepend it
10700 to the name of the imported die. */
10701 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10702
10703 if (imported_die->tag != DW_TAG_namespace
10704 && imported_die->tag != DW_TAG_module)
10705 {
10706 imported_declaration = imported_name;
10707 canonical_name = imported_name_prefix;
10708 }
10709 else if (strlen (imported_name_prefix) > 0)
10710 canonical_name = obconcat (&objfile->objfile_obstack,
10711 imported_name_prefix,
10712 (cu->language == language_d ? "." : "::"),
10713 imported_name, (char *) NULL);
10714 else
10715 canonical_name = imported_name;
10716
10717 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10718 for (child_die = die->child; child_die && child_die->tag;
10719 child_die = child_die->sibling)
10720 {
10721 /* DWARF-4: A Fortran use statement with a “rename list” may be
10722 represented by an imported module entry with an import attribute
10723 referring to the module and owned entries corresponding to those
10724 entities that are renamed as part of being imported. */
10725
10726 if (child_die->tag != DW_TAG_imported_declaration)
10727 {
10728 complaint (_("child DW_TAG_imported_declaration expected "
10729 "- DIE at %s [in module %s]"),
10730 sect_offset_str (child_die->sect_off),
10731 objfile_name (objfile));
10732 continue;
10733 }
10734
10735 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10736 if (import_attr == NULL)
10737 {
10738 complaint (_("Tag '%s' has no DW_AT_import"),
10739 dwarf_tag_name (child_die->tag));
10740 continue;
10741 }
10742
10743 imported_cu = cu;
10744 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10745 &imported_cu);
10746 imported_name = dwarf2_name (imported_die, imported_cu);
10747 if (imported_name == NULL)
10748 {
10749 complaint (_("child DW_TAG_imported_declaration has unknown "
10750 "imported name - DIE at %s [in module %s]"),
10751 sect_offset_str (child_die->sect_off),
10752 objfile_name (objfile));
10753 continue;
10754 }
10755
10756 excludes.push_back (imported_name);
10757
10758 process_die (child_die, cu);
10759 }
10760
10761 add_using_directive (using_directives (cu),
10762 import_prefix,
10763 canonical_name,
10764 import_alias,
10765 imported_declaration,
10766 excludes,
10767 0,
10768 &objfile->objfile_obstack);
10769 }
10770
10771 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10772 types, but gives them a size of zero. Starting with version 14,
10773 ICC is compatible with GCC. */
10774
10775 static bool
10776 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10777 {
10778 if (!cu->checked_producer)
10779 check_producer (cu);
10780
10781 return cu->producer_is_icc_lt_14;
10782 }
10783
10784 /* ICC generates a DW_AT_type for C void functions. This was observed on
10785 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10786 which says that void functions should not have a DW_AT_type. */
10787
10788 static bool
10789 producer_is_icc (struct dwarf2_cu *cu)
10790 {
10791 if (!cu->checked_producer)
10792 check_producer (cu);
10793
10794 return cu->producer_is_icc;
10795 }
10796
10797 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10798 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10799 this, it was first present in GCC release 4.3.0. */
10800
10801 static bool
10802 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10803 {
10804 if (!cu->checked_producer)
10805 check_producer (cu);
10806
10807 return cu->producer_is_gcc_lt_4_3;
10808 }
10809
10810 static file_and_directory
10811 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10812 {
10813 file_and_directory res;
10814
10815 /* Find the filename. Do not use dwarf2_name here, since the filename
10816 is not a source language identifier. */
10817 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10818 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10819
10820 if (res.comp_dir == NULL
10821 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10822 && IS_ABSOLUTE_PATH (res.name))
10823 {
10824 res.comp_dir_storage = ldirname (res.name);
10825 if (!res.comp_dir_storage.empty ())
10826 res.comp_dir = res.comp_dir_storage.c_str ();
10827 }
10828 if (res.comp_dir != NULL)
10829 {
10830 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10831 directory, get rid of it. */
10832 const char *cp = strchr (res.comp_dir, ':');
10833
10834 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10835 res.comp_dir = cp + 1;
10836 }
10837
10838 if (res.name == NULL)
10839 res.name = "<unknown>";
10840
10841 return res;
10842 }
10843
10844 /* Handle DW_AT_stmt_list for a compilation unit.
10845 DIE is the DW_TAG_compile_unit die for CU.
10846 COMP_DIR is the compilation directory. LOWPC is passed to
10847 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10848
10849 static void
10850 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10851 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10852 {
10853 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10854 struct attribute *attr;
10855 struct line_header line_header_local;
10856 hashval_t line_header_local_hash;
10857 void **slot;
10858 int decode_mapping;
10859
10860 gdb_assert (! cu->per_cu->is_debug_types);
10861
10862 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10863 if (attr == NULL)
10864 return;
10865
10866 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10867
10868 /* The line header hash table is only created if needed (it exists to
10869 prevent redundant reading of the line table for partial_units).
10870 If we're given a partial_unit, we'll need it. If we're given a
10871 compile_unit, then use the line header hash table if it's already
10872 created, but don't create one just yet. */
10873
10874 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10875 && die->tag == DW_TAG_partial_unit)
10876 {
10877 dwarf2_per_objfile->per_bfd->line_header_hash
10878 .reset (htab_create_alloc (127, line_header_hash_voidp,
10879 line_header_eq_voidp,
10880 free_line_header_voidp,
10881 xcalloc, xfree));
10882 }
10883
10884 line_header_local.sect_off = line_offset;
10885 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10886 line_header_local_hash = line_header_hash (&line_header_local);
10887 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10888 {
10889 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10890 &line_header_local,
10891 line_header_local_hash, NO_INSERT);
10892
10893 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10894 is not present in *SLOT (since if there is something in *SLOT then
10895 it will be for a partial_unit). */
10896 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10897 {
10898 gdb_assert (*slot != NULL);
10899 cu->line_header = (struct line_header *) *slot;
10900 return;
10901 }
10902 }
10903
10904 /* dwarf_decode_line_header does not yet provide sufficient information.
10905 We always have to call also dwarf_decode_lines for it. */
10906 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10907 if (lh == NULL)
10908 return;
10909
10910 cu->line_header = lh.release ();
10911 cu->line_header_die_owner = die;
10912
10913 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10914 slot = NULL;
10915 else
10916 {
10917 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10918 &line_header_local,
10919 line_header_local_hash, INSERT);
10920 gdb_assert (slot != NULL);
10921 }
10922 if (slot != NULL && *slot == NULL)
10923 {
10924 /* This newly decoded line number information unit will be owned
10925 by line_header_hash hash table. */
10926 *slot = cu->line_header;
10927 cu->line_header_die_owner = NULL;
10928 }
10929 else
10930 {
10931 /* We cannot free any current entry in (*slot) as that struct line_header
10932 may be already used by multiple CUs. Create only temporary decoded
10933 line_header for this CU - it may happen at most once for each line
10934 number information unit. And if we're not using line_header_hash
10935 then this is what we want as well. */
10936 gdb_assert (die->tag != DW_TAG_partial_unit);
10937 }
10938 decode_mapping = (die->tag != DW_TAG_partial_unit);
10939 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10940 decode_mapping);
10941
10942 }
10943
10944 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10945
10946 static void
10947 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10948 {
10949 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10950 struct objfile *objfile = dwarf2_per_objfile->objfile;
10951 struct gdbarch *gdbarch = objfile->arch ();
10952 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10953 CORE_ADDR highpc = ((CORE_ADDR) 0);
10954 struct attribute *attr;
10955 struct die_info *child_die;
10956 CORE_ADDR baseaddr;
10957
10958 prepare_one_comp_unit (cu, die, cu->language);
10959 baseaddr = objfile->text_section_offset ();
10960
10961 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10962
10963 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10964 from finish_block. */
10965 if (lowpc == ((CORE_ADDR) -1))
10966 lowpc = highpc;
10967 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10968
10969 file_and_directory fnd = find_file_and_directory (die, cu);
10970
10971 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10972 standardised yet. As a workaround for the language detection we fall
10973 back to the DW_AT_producer string. */
10974 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10975 cu->language = language_opencl;
10976
10977 /* Similar hack for Go. */
10978 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10979 set_cu_language (DW_LANG_Go, cu);
10980
10981 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10982
10983 /* Decode line number information if present. We do this before
10984 processing child DIEs, so that the line header table is available
10985 for DW_AT_decl_file. */
10986 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10987
10988 /* Process all dies in compilation unit. */
10989 if (die->child != NULL)
10990 {
10991 child_die = die->child;
10992 while (child_die && child_die->tag)
10993 {
10994 process_die (child_die, cu);
10995 child_die = child_die->sibling;
10996 }
10997 }
10998
10999 /* Decode macro information, if present. Dwarf 2 macro information
11000 refers to information in the line number info statement program
11001 header, so we can only read it if we've read the header
11002 successfully. */
11003 attr = dwarf2_attr (die, DW_AT_macros, cu);
11004 if (attr == NULL)
11005 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11006 if (attr && cu->line_header)
11007 {
11008 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11009 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11010
11011 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11012 }
11013 else
11014 {
11015 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11016 if (attr && cu->line_header)
11017 {
11018 unsigned int macro_offset = DW_UNSND (attr);
11019
11020 dwarf_decode_macros (cu, macro_offset, 0);
11021 }
11022 }
11023 }
11024
11025 void
11026 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11027 {
11028 struct type_unit_group *tu_group;
11029 int first_time;
11030 struct attribute *attr;
11031 unsigned int i;
11032 struct signatured_type *sig_type;
11033
11034 gdb_assert (per_cu->is_debug_types);
11035 sig_type = (struct signatured_type *) per_cu;
11036
11037 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11038
11039 /* If we're using .gdb_index (includes -readnow) then
11040 per_cu->type_unit_group may not have been set up yet. */
11041 if (sig_type->type_unit_group == NULL)
11042 sig_type->type_unit_group = get_type_unit_group (this, attr);
11043 tu_group = sig_type->type_unit_group;
11044
11045 /* If we've already processed this stmt_list there's no real need to
11046 do it again, we could fake it and just recreate the part we need
11047 (file name,index -> symtab mapping). If data shows this optimization
11048 is useful we can do it then. */
11049 type_unit_group_unshareable *tug_unshare
11050 = per_objfile->get_type_unit_group_unshareable (tu_group);
11051 first_time = tug_unshare->compunit_symtab == NULL;
11052
11053 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11054 debug info. */
11055 line_header_up lh;
11056 if (attr != NULL)
11057 {
11058 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11059 lh = dwarf_decode_line_header (line_offset, this);
11060 }
11061 if (lh == NULL)
11062 {
11063 if (first_time)
11064 start_symtab ("", NULL, 0);
11065 else
11066 {
11067 gdb_assert (tug_unshare->symtabs == NULL);
11068 gdb_assert (m_builder == nullptr);
11069 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11070 m_builder.reset (new struct buildsym_compunit
11071 (COMPUNIT_OBJFILE (cust), "",
11072 COMPUNIT_DIRNAME (cust),
11073 compunit_language (cust),
11074 0, cust));
11075 list_in_scope = get_builder ()->get_file_symbols ();
11076 }
11077 return;
11078 }
11079
11080 line_header = lh.release ();
11081 line_header_die_owner = die;
11082
11083 if (first_time)
11084 {
11085 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11086
11087 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11088 still initializing it, and our caller (a few levels up)
11089 process_full_type_unit still needs to know if this is the first
11090 time. */
11091
11092 tug_unshare->symtabs
11093 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11094 struct symtab *, line_header->file_names_size ());
11095
11096 auto &file_names = line_header->file_names ();
11097 for (i = 0; i < file_names.size (); ++i)
11098 {
11099 file_entry &fe = file_names[i];
11100 dwarf2_start_subfile (this, fe.name,
11101 fe.include_dir (line_header));
11102 buildsym_compunit *b = get_builder ();
11103 if (b->get_current_subfile ()->symtab == NULL)
11104 {
11105 /* NOTE: start_subfile will recognize when it's been
11106 passed a file it has already seen. So we can't
11107 assume there's a simple mapping from
11108 cu->line_header->file_names to subfiles, plus
11109 cu->line_header->file_names may contain dups. */
11110 b->get_current_subfile ()->symtab
11111 = allocate_symtab (cust, b->get_current_subfile ()->name);
11112 }
11113
11114 fe.symtab = b->get_current_subfile ()->symtab;
11115 tug_unshare->symtabs[i] = fe.symtab;
11116 }
11117 }
11118 else
11119 {
11120 gdb_assert (m_builder == nullptr);
11121 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11122 m_builder.reset (new struct buildsym_compunit
11123 (COMPUNIT_OBJFILE (cust), "",
11124 COMPUNIT_DIRNAME (cust),
11125 compunit_language (cust),
11126 0, cust));
11127 list_in_scope = get_builder ()->get_file_symbols ();
11128
11129 auto &file_names = line_header->file_names ();
11130 for (i = 0; i < file_names.size (); ++i)
11131 {
11132 file_entry &fe = file_names[i];
11133 fe.symtab = tug_unshare->symtabs[i];
11134 }
11135 }
11136
11137 /* The main symtab is allocated last. Type units don't have DW_AT_name
11138 so they don't have a "real" (so to speak) symtab anyway.
11139 There is later code that will assign the main symtab to all symbols
11140 that don't have one. We need to handle the case of a symbol with a
11141 missing symtab (DW_AT_decl_file) anyway. */
11142 }
11143
11144 /* Process DW_TAG_type_unit.
11145 For TUs we want to skip the first top level sibling if it's not the
11146 actual type being defined by this TU. In this case the first top
11147 level sibling is there to provide context only. */
11148
11149 static void
11150 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11151 {
11152 struct die_info *child_die;
11153
11154 prepare_one_comp_unit (cu, die, language_minimal);
11155
11156 /* Initialize (or reinitialize) the machinery for building symtabs.
11157 We do this before processing child DIEs, so that the line header table
11158 is available for DW_AT_decl_file. */
11159 cu->setup_type_unit_groups (die);
11160
11161 if (die->child != NULL)
11162 {
11163 child_die = die->child;
11164 while (child_die && child_die->tag)
11165 {
11166 process_die (child_die, cu);
11167 child_die = child_die->sibling;
11168 }
11169 }
11170 }
11171 \f
11172 /* DWO/DWP files.
11173
11174 http://gcc.gnu.org/wiki/DebugFission
11175 http://gcc.gnu.org/wiki/DebugFissionDWP
11176
11177 To simplify handling of both DWO files ("object" files with the DWARF info)
11178 and DWP files (a file with the DWOs packaged up into one file), we treat
11179 DWP files as having a collection of virtual DWO files. */
11180
11181 static hashval_t
11182 hash_dwo_file (const void *item)
11183 {
11184 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11185 hashval_t hash;
11186
11187 hash = htab_hash_string (dwo_file->dwo_name);
11188 if (dwo_file->comp_dir != NULL)
11189 hash += htab_hash_string (dwo_file->comp_dir);
11190 return hash;
11191 }
11192
11193 static int
11194 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11195 {
11196 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11197 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11198
11199 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11200 return 0;
11201 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11202 return lhs->comp_dir == rhs->comp_dir;
11203 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11204 }
11205
11206 /* Allocate a hash table for DWO files. */
11207
11208 static htab_up
11209 allocate_dwo_file_hash_table ()
11210 {
11211 auto delete_dwo_file = [] (void *item)
11212 {
11213 struct dwo_file *dwo_file = (struct dwo_file *) item;
11214
11215 delete dwo_file;
11216 };
11217
11218 return htab_up (htab_create_alloc (41,
11219 hash_dwo_file,
11220 eq_dwo_file,
11221 delete_dwo_file,
11222 xcalloc, xfree));
11223 }
11224
11225 /* Lookup DWO file DWO_NAME. */
11226
11227 static void **
11228 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11229 const char *dwo_name,
11230 const char *comp_dir)
11231 {
11232 struct dwo_file find_entry;
11233 void **slot;
11234
11235 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11236 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11237
11238 find_entry.dwo_name = dwo_name;
11239 find_entry.comp_dir = comp_dir;
11240 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11241 INSERT);
11242
11243 return slot;
11244 }
11245
11246 static hashval_t
11247 hash_dwo_unit (const void *item)
11248 {
11249 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11250
11251 /* This drops the top 32 bits of the id, but is ok for a hash. */
11252 return dwo_unit->signature;
11253 }
11254
11255 static int
11256 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11257 {
11258 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11259 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11260
11261 /* The signature is assumed to be unique within the DWO file.
11262 So while object file CU dwo_id's always have the value zero,
11263 that's OK, assuming each object file DWO file has only one CU,
11264 and that's the rule for now. */
11265 return lhs->signature == rhs->signature;
11266 }
11267
11268 /* Allocate a hash table for DWO CUs,TUs.
11269 There is one of these tables for each of CUs,TUs for each DWO file. */
11270
11271 static htab_up
11272 allocate_dwo_unit_table ()
11273 {
11274 /* Start out with a pretty small number.
11275 Generally DWO files contain only one CU and maybe some TUs. */
11276 return htab_up (htab_create_alloc (3,
11277 hash_dwo_unit,
11278 eq_dwo_unit,
11279 NULL, xcalloc, xfree));
11280 }
11281
11282 /* die_reader_func for create_dwo_cu. */
11283
11284 static void
11285 create_dwo_cu_reader (const struct die_reader_specs *reader,
11286 const gdb_byte *info_ptr,
11287 struct die_info *comp_unit_die,
11288 struct dwo_file *dwo_file,
11289 struct dwo_unit *dwo_unit)
11290 {
11291 struct dwarf2_cu *cu = reader->cu;
11292 sect_offset sect_off = cu->per_cu->sect_off;
11293 struct dwarf2_section_info *section = cu->per_cu->section;
11294
11295 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11296 if (!signature.has_value ())
11297 {
11298 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11299 " its dwo_id [in module %s]"),
11300 sect_offset_str (sect_off), dwo_file->dwo_name);
11301 return;
11302 }
11303
11304 dwo_unit->dwo_file = dwo_file;
11305 dwo_unit->signature = *signature;
11306 dwo_unit->section = section;
11307 dwo_unit->sect_off = sect_off;
11308 dwo_unit->length = cu->per_cu->length;
11309
11310 if (dwarf_read_debug)
11311 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11312 sect_offset_str (sect_off),
11313 hex_string (dwo_unit->signature));
11314 }
11315
11316 /* Create the dwo_units for the CUs in a DWO_FILE.
11317 Note: This function processes DWO files only, not DWP files. */
11318
11319 static void
11320 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11321 dwarf2_cu *cu, struct dwo_file &dwo_file,
11322 dwarf2_section_info &section, htab_up &cus_htab)
11323 {
11324 struct objfile *objfile = dwarf2_per_objfile->objfile;
11325 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11326 const gdb_byte *info_ptr, *end_ptr;
11327
11328 section.read (objfile);
11329 info_ptr = section.buffer;
11330
11331 if (info_ptr == NULL)
11332 return;
11333
11334 if (dwarf_read_debug)
11335 {
11336 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11337 section.get_name (),
11338 section.get_file_name ());
11339 }
11340
11341 end_ptr = info_ptr + section.size;
11342 while (info_ptr < end_ptr)
11343 {
11344 struct dwarf2_per_cu_data per_cu;
11345 struct dwo_unit read_unit {};
11346 struct dwo_unit *dwo_unit;
11347 void **slot;
11348 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11349
11350 memset (&per_cu, 0, sizeof (per_cu));
11351 per_cu.per_bfd = per_bfd;
11352 per_cu.is_debug_types = 0;
11353 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11354 per_cu.section = &section;
11355
11356 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11357 if (!reader.dummy_p)
11358 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11359 &dwo_file, &read_unit);
11360 info_ptr += per_cu.length;
11361
11362 // If the unit could not be parsed, skip it.
11363 if (read_unit.dwo_file == NULL)
11364 continue;
11365
11366 if (cus_htab == NULL)
11367 cus_htab = allocate_dwo_unit_table ();
11368
11369 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11370 struct dwo_unit);
11371 *dwo_unit = read_unit;
11372 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11373 gdb_assert (slot != NULL);
11374 if (*slot != NULL)
11375 {
11376 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11377 sect_offset dup_sect_off = dup_cu->sect_off;
11378
11379 complaint (_("debug cu entry at offset %s is duplicate to"
11380 " the entry at offset %s, signature %s"),
11381 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11382 hex_string (dwo_unit->signature));
11383 }
11384 *slot = (void *)dwo_unit;
11385 }
11386 }
11387
11388 /* DWP file .debug_{cu,tu}_index section format:
11389 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11390
11391 DWP Version 1:
11392
11393 Both index sections have the same format, and serve to map a 64-bit
11394 signature to a set of section numbers. Each section begins with a header,
11395 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11396 indexes, and a pool of 32-bit section numbers. The index sections will be
11397 aligned at 8-byte boundaries in the file.
11398
11399 The index section header consists of:
11400
11401 V, 32 bit version number
11402 -, 32 bits unused
11403 N, 32 bit number of compilation units or type units in the index
11404 M, 32 bit number of slots in the hash table
11405
11406 Numbers are recorded using the byte order of the application binary.
11407
11408 The hash table begins at offset 16 in the section, and consists of an array
11409 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11410 order of the application binary). Unused slots in the hash table are 0.
11411 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11412
11413 The parallel table begins immediately after the hash table
11414 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11415 array of 32-bit indexes (using the byte order of the application binary),
11416 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11417 table contains a 32-bit index into the pool of section numbers. For unused
11418 hash table slots, the corresponding entry in the parallel table will be 0.
11419
11420 The pool of section numbers begins immediately following the hash table
11421 (at offset 16 + 12 * M from the beginning of the section). The pool of
11422 section numbers consists of an array of 32-bit words (using the byte order
11423 of the application binary). Each item in the array is indexed starting
11424 from 0. The hash table entry provides the index of the first section
11425 number in the set. Additional section numbers in the set follow, and the
11426 set is terminated by a 0 entry (section number 0 is not used in ELF).
11427
11428 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11429 section must be the first entry in the set, and the .debug_abbrev.dwo must
11430 be the second entry. Other members of the set may follow in any order.
11431
11432 ---
11433
11434 DWP Version 2:
11435
11436 DWP Version 2 combines all the .debug_info, etc. sections into one,
11437 and the entries in the index tables are now offsets into these sections.
11438 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11439 section.
11440
11441 Index Section Contents:
11442 Header
11443 Hash Table of Signatures dwp_hash_table.hash_table
11444 Parallel Table of Indices dwp_hash_table.unit_table
11445 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11446 Table of Section Sizes dwp_hash_table.v2.sizes
11447
11448 The index section header consists of:
11449
11450 V, 32 bit version number
11451 L, 32 bit number of columns in the table of section offsets
11452 N, 32 bit number of compilation units or type units in the index
11453 M, 32 bit number of slots in the hash table
11454
11455 Numbers are recorded using the byte order of the application binary.
11456
11457 The hash table has the same format as version 1.
11458 The parallel table of indices has the same format as version 1,
11459 except that the entries are origin-1 indices into the table of sections
11460 offsets and the table of section sizes.
11461
11462 The table of offsets begins immediately following the parallel table
11463 (at offset 16 + 12 * M from the beginning of the section). The table is
11464 a two-dimensional array of 32-bit words (using the byte order of the
11465 application binary), with L columns and N+1 rows, in row-major order.
11466 Each row in the array is indexed starting from 0. The first row provides
11467 a key to the remaining rows: each column in this row provides an identifier
11468 for a debug section, and the offsets in the same column of subsequent rows
11469 refer to that section. The section identifiers are:
11470
11471 DW_SECT_INFO 1 .debug_info.dwo
11472 DW_SECT_TYPES 2 .debug_types.dwo
11473 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11474 DW_SECT_LINE 4 .debug_line.dwo
11475 DW_SECT_LOC 5 .debug_loc.dwo
11476 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11477 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11478 DW_SECT_MACRO 8 .debug_macro.dwo
11479
11480 The offsets provided by the CU and TU index sections are the base offsets
11481 for the contributions made by each CU or TU to the corresponding section
11482 in the package file. Each CU and TU header contains an abbrev_offset
11483 field, used to find the abbreviations table for that CU or TU within the
11484 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11485 be interpreted as relative to the base offset given in the index section.
11486 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11487 should be interpreted as relative to the base offset for .debug_line.dwo,
11488 and offsets into other debug sections obtained from DWARF attributes should
11489 also be interpreted as relative to the corresponding base offset.
11490
11491 The table of sizes begins immediately following the table of offsets.
11492 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11493 with L columns and N rows, in row-major order. Each row in the array is
11494 indexed starting from 1 (row 0 is shared by the two tables).
11495
11496 ---
11497
11498 Hash table lookup is handled the same in version 1 and 2:
11499
11500 We assume that N and M will not exceed 2^32 - 1.
11501 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11502
11503 Given a 64-bit compilation unit signature or a type signature S, an entry
11504 in the hash table is located as follows:
11505
11506 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11507 the low-order k bits all set to 1.
11508
11509 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11510
11511 3) If the hash table entry at index H matches the signature, use that
11512 entry. If the hash table entry at index H is unused (all zeroes),
11513 terminate the search: the signature is not present in the table.
11514
11515 4) Let H = (H + H') modulo M. Repeat at Step 3.
11516
11517 Because M > N and H' and M are relatively prime, the search is guaranteed
11518 to stop at an unused slot or find the match. */
11519
11520 /* Create a hash table to map DWO IDs to their CU/TU entry in
11521 .debug_{info,types}.dwo in DWP_FILE.
11522 Returns NULL if there isn't one.
11523 Note: This function processes DWP files only, not DWO files. */
11524
11525 static struct dwp_hash_table *
11526 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11527 struct dwp_file *dwp_file, int is_debug_types)
11528 {
11529 struct objfile *objfile = dwarf2_per_objfile->objfile;
11530 bfd *dbfd = dwp_file->dbfd.get ();
11531 const gdb_byte *index_ptr, *index_end;
11532 struct dwarf2_section_info *index;
11533 uint32_t version, nr_columns, nr_units, nr_slots;
11534 struct dwp_hash_table *htab;
11535
11536 if (is_debug_types)
11537 index = &dwp_file->sections.tu_index;
11538 else
11539 index = &dwp_file->sections.cu_index;
11540
11541 if (index->empty ())
11542 return NULL;
11543 index->read (objfile);
11544
11545 index_ptr = index->buffer;
11546 index_end = index_ptr + index->size;
11547
11548 version = read_4_bytes (dbfd, index_ptr);
11549 index_ptr += 4;
11550 if (version == 2)
11551 nr_columns = read_4_bytes (dbfd, index_ptr);
11552 else
11553 nr_columns = 0;
11554 index_ptr += 4;
11555 nr_units = read_4_bytes (dbfd, index_ptr);
11556 index_ptr += 4;
11557 nr_slots = read_4_bytes (dbfd, index_ptr);
11558 index_ptr += 4;
11559
11560 if (version != 1 && version != 2)
11561 {
11562 error (_("Dwarf Error: unsupported DWP file version (%s)"
11563 " [in module %s]"),
11564 pulongest (version), dwp_file->name);
11565 }
11566 if (nr_slots != (nr_slots & -nr_slots))
11567 {
11568 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11569 " is not power of 2 [in module %s]"),
11570 pulongest (nr_slots), dwp_file->name);
11571 }
11572
11573 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11574 htab->version = version;
11575 htab->nr_columns = nr_columns;
11576 htab->nr_units = nr_units;
11577 htab->nr_slots = nr_slots;
11578 htab->hash_table = index_ptr;
11579 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11580
11581 /* Exit early if the table is empty. */
11582 if (nr_slots == 0 || nr_units == 0
11583 || (version == 2 && nr_columns == 0))
11584 {
11585 /* All must be zero. */
11586 if (nr_slots != 0 || nr_units != 0
11587 || (version == 2 && nr_columns != 0))
11588 {
11589 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11590 " all zero [in modules %s]"),
11591 dwp_file->name);
11592 }
11593 return htab;
11594 }
11595
11596 if (version == 1)
11597 {
11598 htab->section_pool.v1.indices =
11599 htab->unit_table + sizeof (uint32_t) * nr_slots;
11600 /* It's harder to decide whether the section is too small in v1.
11601 V1 is deprecated anyway so we punt. */
11602 }
11603 else
11604 {
11605 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11606 int *ids = htab->section_pool.v2.section_ids;
11607 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11608 /* Reverse map for error checking. */
11609 int ids_seen[DW_SECT_MAX + 1];
11610 int i;
11611
11612 if (nr_columns < 2)
11613 {
11614 error (_("Dwarf Error: bad DWP hash table, too few columns"
11615 " in section table [in module %s]"),
11616 dwp_file->name);
11617 }
11618 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11619 {
11620 error (_("Dwarf Error: bad DWP hash table, too many columns"
11621 " in section table [in module %s]"),
11622 dwp_file->name);
11623 }
11624 memset (ids, 255, sizeof_ids);
11625 memset (ids_seen, 255, sizeof (ids_seen));
11626 for (i = 0; i < nr_columns; ++i)
11627 {
11628 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11629
11630 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11631 {
11632 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11633 " in section table [in module %s]"),
11634 id, dwp_file->name);
11635 }
11636 if (ids_seen[id] != -1)
11637 {
11638 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11639 " id %d in section table [in module %s]"),
11640 id, dwp_file->name);
11641 }
11642 ids_seen[id] = i;
11643 ids[i] = id;
11644 }
11645 /* Must have exactly one info or types section. */
11646 if (((ids_seen[DW_SECT_INFO] != -1)
11647 + (ids_seen[DW_SECT_TYPES] != -1))
11648 != 1)
11649 {
11650 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11651 " DWO info/types section [in module %s]"),
11652 dwp_file->name);
11653 }
11654 /* Must have an abbrev section. */
11655 if (ids_seen[DW_SECT_ABBREV] == -1)
11656 {
11657 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11658 " section [in module %s]"),
11659 dwp_file->name);
11660 }
11661 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11662 htab->section_pool.v2.sizes =
11663 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11664 * nr_units * nr_columns);
11665 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11666 * nr_units * nr_columns))
11667 > index_end)
11668 {
11669 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11670 " [in module %s]"),
11671 dwp_file->name);
11672 }
11673 }
11674
11675 return htab;
11676 }
11677
11678 /* Update SECTIONS with the data from SECTP.
11679
11680 This function is like the other "locate" section routines that are
11681 passed to bfd_map_over_sections, but in this context the sections to
11682 read comes from the DWP V1 hash table, not the full ELF section table.
11683
11684 The result is non-zero for success, or zero if an error was found. */
11685
11686 static int
11687 locate_v1_virtual_dwo_sections (asection *sectp,
11688 struct virtual_v1_dwo_sections *sections)
11689 {
11690 const struct dwop_section_names *names = &dwop_section_names;
11691
11692 if (section_is_p (sectp->name, &names->abbrev_dwo))
11693 {
11694 /* There can be only one. */
11695 if (sections->abbrev.s.section != NULL)
11696 return 0;
11697 sections->abbrev.s.section = sectp;
11698 sections->abbrev.size = bfd_section_size (sectp);
11699 }
11700 else if (section_is_p (sectp->name, &names->info_dwo)
11701 || section_is_p (sectp->name, &names->types_dwo))
11702 {
11703 /* There can be only one. */
11704 if (sections->info_or_types.s.section != NULL)
11705 return 0;
11706 sections->info_or_types.s.section = sectp;
11707 sections->info_or_types.size = bfd_section_size (sectp);
11708 }
11709 else if (section_is_p (sectp->name, &names->line_dwo))
11710 {
11711 /* There can be only one. */
11712 if (sections->line.s.section != NULL)
11713 return 0;
11714 sections->line.s.section = sectp;
11715 sections->line.size = bfd_section_size (sectp);
11716 }
11717 else if (section_is_p (sectp->name, &names->loc_dwo))
11718 {
11719 /* There can be only one. */
11720 if (sections->loc.s.section != NULL)
11721 return 0;
11722 sections->loc.s.section = sectp;
11723 sections->loc.size = bfd_section_size (sectp);
11724 }
11725 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11726 {
11727 /* There can be only one. */
11728 if (sections->macinfo.s.section != NULL)
11729 return 0;
11730 sections->macinfo.s.section = sectp;
11731 sections->macinfo.size = bfd_section_size (sectp);
11732 }
11733 else if (section_is_p (sectp->name, &names->macro_dwo))
11734 {
11735 /* There can be only one. */
11736 if (sections->macro.s.section != NULL)
11737 return 0;
11738 sections->macro.s.section = sectp;
11739 sections->macro.size = bfd_section_size (sectp);
11740 }
11741 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11742 {
11743 /* There can be only one. */
11744 if (sections->str_offsets.s.section != NULL)
11745 return 0;
11746 sections->str_offsets.s.section = sectp;
11747 sections->str_offsets.size = bfd_section_size (sectp);
11748 }
11749 else
11750 {
11751 /* No other kind of section is valid. */
11752 return 0;
11753 }
11754
11755 return 1;
11756 }
11757
11758 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11759 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11760 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11761 This is for DWP version 1 files. */
11762
11763 static struct dwo_unit *
11764 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11765 struct dwp_file *dwp_file,
11766 uint32_t unit_index,
11767 const char *comp_dir,
11768 ULONGEST signature, int is_debug_types)
11769 {
11770 const struct dwp_hash_table *dwp_htab =
11771 is_debug_types ? dwp_file->tus : dwp_file->cus;
11772 bfd *dbfd = dwp_file->dbfd.get ();
11773 const char *kind = is_debug_types ? "TU" : "CU";
11774 struct dwo_file *dwo_file;
11775 struct dwo_unit *dwo_unit;
11776 struct virtual_v1_dwo_sections sections;
11777 void **dwo_file_slot;
11778 int i;
11779
11780 gdb_assert (dwp_file->version == 1);
11781
11782 if (dwarf_read_debug)
11783 {
11784 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11785 kind,
11786 pulongest (unit_index), hex_string (signature),
11787 dwp_file->name);
11788 }
11789
11790 /* Fetch the sections of this DWO unit.
11791 Put a limit on the number of sections we look for so that bad data
11792 doesn't cause us to loop forever. */
11793
11794 #define MAX_NR_V1_DWO_SECTIONS \
11795 (1 /* .debug_info or .debug_types */ \
11796 + 1 /* .debug_abbrev */ \
11797 + 1 /* .debug_line */ \
11798 + 1 /* .debug_loc */ \
11799 + 1 /* .debug_str_offsets */ \
11800 + 1 /* .debug_macro or .debug_macinfo */ \
11801 + 1 /* trailing zero */)
11802
11803 memset (&sections, 0, sizeof (sections));
11804
11805 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11806 {
11807 asection *sectp;
11808 uint32_t section_nr =
11809 read_4_bytes (dbfd,
11810 dwp_htab->section_pool.v1.indices
11811 + (unit_index + i) * sizeof (uint32_t));
11812
11813 if (section_nr == 0)
11814 break;
11815 if (section_nr >= dwp_file->num_sections)
11816 {
11817 error (_("Dwarf Error: bad DWP hash table, section number too large"
11818 " [in module %s]"),
11819 dwp_file->name);
11820 }
11821
11822 sectp = dwp_file->elf_sections[section_nr];
11823 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11824 {
11825 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11826 " [in module %s]"),
11827 dwp_file->name);
11828 }
11829 }
11830
11831 if (i < 2
11832 || sections.info_or_types.empty ()
11833 || sections.abbrev.empty ())
11834 {
11835 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11836 " [in module %s]"),
11837 dwp_file->name);
11838 }
11839 if (i == MAX_NR_V1_DWO_SECTIONS)
11840 {
11841 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11842 " [in module %s]"),
11843 dwp_file->name);
11844 }
11845
11846 /* It's easier for the rest of the code if we fake a struct dwo_file and
11847 have dwo_unit "live" in that. At least for now.
11848
11849 The DWP file can be made up of a random collection of CUs and TUs.
11850 However, for each CU + set of TUs that came from the same original DWO
11851 file, we can combine them back into a virtual DWO file to save space
11852 (fewer struct dwo_file objects to allocate). Remember that for really
11853 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11854
11855 std::string virtual_dwo_name =
11856 string_printf ("virtual-dwo/%d-%d-%d-%d",
11857 sections.abbrev.get_id (),
11858 sections.line.get_id (),
11859 sections.loc.get_id (),
11860 sections.str_offsets.get_id ());
11861 /* Can we use an existing virtual DWO file? */
11862 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11863 virtual_dwo_name.c_str (),
11864 comp_dir);
11865 /* Create one if necessary. */
11866 if (*dwo_file_slot == NULL)
11867 {
11868 if (dwarf_read_debug)
11869 {
11870 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11871 virtual_dwo_name.c_str ());
11872 }
11873 dwo_file = new struct dwo_file;
11874 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11875 dwo_file->comp_dir = comp_dir;
11876 dwo_file->sections.abbrev = sections.abbrev;
11877 dwo_file->sections.line = sections.line;
11878 dwo_file->sections.loc = sections.loc;
11879 dwo_file->sections.macinfo = sections.macinfo;
11880 dwo_file->sections.macro = sections.macro;
11881 dwo_file->sections.str_offsets = sections.str_offsets;
11882 /* The "str" section is global to the entire DWP file. */
11883 dwo_file->sections.str = dwp_file->sections.str;
11884 /* The info or types section is assigned below to dwo_unit,
11885 there's no need to record it in dwo_file.
11886 Also, we can't simply record type sections in dwo_file because
11887 we record a pointer into the vector in dwo_unit. As we collect more
11888 types we'll grow the vector and eventually have to reallocate space
11889 for it, invalidating all copies of pointers into the previous
11890 contents. */
11891 *dwo_file_slot = dwo_file;
11892 }
11893 else
11894 {
11895 if (dwarf_read_debug)
11896 {
11897 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11898 virtual_dwo_name.c_str ());
11899 }
11900 dwo_file = (struct dwo_file *) *dwo_file_slot;
11901 }
11902
11903 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11904 dwo_unit->dwo_file = dwo_file;
11905 dwo_unit->signature = signature;
11906 dwo_unit->section =
11907 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11908 *dwo_unit->section = sections.info_or_types;
11909 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11910
11911 return dwo_unit;
11912 }
11913
11914 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11915 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11916 piece within that section used by a TU/CU, return a virtual section
11917 of just that piece. */
11918
11919 static struct dwarf2_section_info
11920 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11921 struct dwarf2_section_info *section,
11922 bfd_size_type offset, bfd_size_type size)
11923 {
11924 struct dwarf2_section_info result;
11925 asection *sectp;
11926
11927 gdb_assert (section != NULL);
11928 gdb_assert (!section->is_virtual);
11929
11930 memset (&result, 0, sizeof (result));
11931 result.s.containing_section = section;
11932 result.is_virtual = true;
11933
11934 if (size == 0)
11935 return result;
11936
11937 sectp = section->get_bfd_section ();
11938
11939 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11940 bounds of the real section. This is a pretty-rare event, so just
11941 flag an error (easier) instead of a warning and trying to cope. */
11942 if (sectp == NULL
11943 || offset + size > bfd_section_size (sectp))
11944 {
11945 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11946 " in section %s [in module %s]"),
11947 sectp ? bfd_section_name (sectp) : "<unknown>",
11948 objfile_name (dwarf2_per_objfile->objfile));
11949 }
11950
11951 result.virtual_offset = offset;
11952 result.size = size;
11953 return result;
11954 }
11955
11956 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11957 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11958 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11959 This is for DWP version 2 files. */
11960
11961 static struct dwo_unit *
11962 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11963 struct dwp_file *dwp_file,
11964 uint32_t unit_index,
11965 const char *comp_dir,
11966 ULONGEST signature, int is_debug_types)
11967 {
11968 const struct dwp_hash_table *dwp_htab =
11969 is_debug_types ? dwp_file->tus : dwp_file->cus;
11970 bfd *dbfd = dwp_file->dbfd.get ();
11971 const char *kind = is_debug_types ? "TU" : "CU";
11972 struct dwo_file *dwo_file;
11973 struct dwo_unit *dwo_unit;
11974 struct virtual_v2_dwo_sections sections;
11975 void **dwo_file_slot;
11976 int i;
11977
11978 gdb_assert (dwp_file->version == 2);
11979
11980 if (dwarf_read_debug)
11981 {
11982 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11983 kind,
11984 pulongest (unit_index), hex_string (signature),
11985 dwp_file->name);
11986 }
11987
11988 /* Fetch the section offsets of this DWO unit. */
11989
11990 memset (&sections, 0, sizeof (sections));
11991
11992 for (i = 0; i < dwp_htab->nr_columns; ++i)
11993 {
11994 uint32_t offset = read_4_bytes (dbfd,
11995 dwp_htab->section_pool.v2.offsets
11996 + (((unit_index - 1) * dwp_htab->nr_columns
11997 + i)
11998 * sizeof (uint32_t)));
11999 uint32_t size = read_4_bytes (dbfd,
12000 dwp_htab->section_pool.v2.sizes
12001 + (((unit_index - 1) * dwp_htab->nr_columns
12002 + i)
12003 * sizeof (uint32_t)));
12004
12005 switch (dwp_htab->section_pool.v2.section_ids[i])
12006 {
12007 case DW_SECT_INFO:
12008 case DW_SECT_TYPES:
12009 sections.info_or_types_offset = offset;
12010 sections.info_or_types_size = size;
12011 break;
12012 case DW_SECT_ABBREV:
12013 sections.abbrev_offset = offset;
12014 sections.abbrev_size = size;
12015 break;
12016 case DW_SECT_LINE:
12017 sections.line_offset = offset;
12018 sections.line_size = size;
12019 break;
12020 case DW_SECT_LOC:
12021 sections.loc_offset = offset;
12022 sections.loc_size = size;
12023 break;
12024 case DW_SECT_STR_OFFSETS:
12025 sections.str_offsets_offset = offset;
12026 sections.str_offsets_size = size;
12027 break;
12028 case DW_SECT_MACINFO:
12029 sections.macinfo_offset = offset;
12030 sections.macinfo_size = size;
12031 break;
12032 case DW_SECT_MACRO:
12033 sections.macro_offset = offset;
12034 sections.macro_size = size;
12035 break;
12036 }
12037 }
12038
12039 /* It's easier for the rest of the code if we fake a struct dwo_file and
12040 have dwo_unit "live" in that. At least for now.
12041
12042 The DWP file can be made up of a random collection of CUs and TUs.
12043 However, for each CU + set of TUs that came from the same original DWO
12044 file, we can combine them back into a virtual DWO file to save space
12045 (fewer struct dwo_file objects to allocate). Remember that for really
12046 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12047
12048 std::string virtual_dwo_name =
12049 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12050 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12051 (long) (sections.line_size ? sections.line_offset : 0),
12052 (long) (sections.loc_size ? sections.loc_offset : 0),
12053 (long) (sections.str_offsets_size
12054 ? sections.str_offsets_offset : 0));
12055 /* Can we use an existing virtual DWO file? */
12056 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12057 virtual_dwo_name.c_str (),
12058 comp_dir);
12059 /* Create one if necessary. */
12060 if (*dwo_file_slot == NULL)
12061 {
12062 if (dwarf_read_debug)
12063 {
12064 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12065 virtual_dwo_name.c_str ());
12066 }
12067 dwo_file = new struct dwo_file;
12068 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12069 dwo_file->comp_dir = comp_dir;
12070 dwo_file->sections.abbrev =
12071 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12072 sections.abbrev_offset, sections.abbrev_size);
12073 dwo_file->sections.line =
12074 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12075 sections.line_offset, sections.line_size);
12076 dwo_file->sections.loc =
12077 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12078 sections.loc_offset, sections.loc_size);
12079 dwo_file->sections.macinfo =
12080 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12081 sections.macinfo_offset, sections.macinfo_size);
12082 dwo_file->sections.macro =
12083 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12084 sections.macro_offset, sections.macro_size);
12085 dwo_file->sections.str_offsets =
12086 create_dwp_v2_section (dwarf2_per_objfile,
12087 &dwp_file->sections.str_offsets,
12088 sections.str_offsets_offset,
12089 sections.str_offsets_size);
12090 /* The "str" section is global to the entire DWP file. */
12091 dwo_file->sections.str = dwp_file->sections.str;
12092 /* The info or types section is assigned below to dwo_unit,
12093 there's no need to record it in dwo_file.
12094 Also, we can't simply record type sections in dwo_file because
12095 we record a pointer into the vector in dwo_unit. As we collect more
12096 types we'll grow the vector and eventually have to reallocate space
12097 for it, invalidating all copies of pointers into the previous
12098 contents. */
12099 *dwo_file_slot = dwo_file;
12100 }
12101 else
12102 {
12103 if (dwarf_read_debug)
12104 {
12105 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12106 virtual_dwo_name.c_str ());
12107 }
12108 dwo_file = (struct dwo_file *) *dwo_file_slot;
12109 }
12110
12111 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12112 dwo_unit->dwo_file = dwo_file;
12113 dwo_unit->signature = signature;
12114 dwo_unit->section =
12115 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12116 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12117 is_debug_types
12118 ? &dwp_file->sections.types
12119 : &dwp_file->sections.info,
12120 sections.info_or_types_offset,
12121 sections.info_or_types_size);
12122 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12123
12124 return dwo_unit;
12125 }
12126
12127 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12128 Returns NULL if the signature isn't found. */
12129
12130 static struct dwo_unit *
12131 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12132 struct dwp_file *dwp_file, const char *comp_dir,
12133 ULONGEST signature, int is_debug_types)
12134 {
12135 const struct dwp_hash_table *dwp_htab =
12136 is_debug_types ? dwp_file->tus : dwp_file->cus;
12137 bfd *dbfd = dwp_file->dbfd.get ();
12138 uint32_t mask = dwp_htab->nr_slots - 1;
12139 uint32_t hash = signature & mask;
12140 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12141 unsigned int i;
12142 void **slot;
12143 struct dwo_unit find_dwo_cu;
12144
12145 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12146 find_dwo_cu.signature = signature;
12147 slot = htab_find_slot (is_debug_types
12148 ? dwp_file->loaded_tus.get ()
12149 : dwp_file->loaded_cus.get (),
12150 &find_dwo_cu, INSERT);
12151
12152 if (*slot != NULL)
12153 return (struct dwo_unit *) *slot;
12154
12155 /* Use a for loop so that we don't loop forever on bad debug info. */
12156 for (i = 0; i < dwp_htab->nr_slots; ++i)
12157 {
12158 ULONGEST signature_in_table;
12159
12160 signature_in_table =
12161 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12162 if (signature_in_table == signature)
12163 {
12164 uint32_t unit_index =
12165 read_4_bytes (dbfd,
12166 dwp_htab->unit_table + hash * sizeof (uint32_t));
12167
12168 if (dwp_file->version == 1)
12169 {
12170 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12171 dwp_file, unit_index,
12172 comp_dir, signature,
12173 is_debug_types);
12174 }
12175 else
12176 {
12177 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12178 dwp_file, unit_index,
12179 comp_dir, signature,
12180 is_debug_types);
12181 }
12182 return (struct dwo_unit *) *slot;
12183 }
12184 if (signature_in_table == 0)
12185 return NULL;
12186 hash = (hash + hash2) & mask;
12187 }
12188
12189 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12190 " [in module %s]"),
12191 dwp_file->name);
12192 }
12193
12194 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12195 Open the file specified by FILE_NAME and hand it off to BFD for
12196 preliminary analysis. Return a newly initialized bfd *, which
12197 includes a canonicalized copy of FILE_NAME.
12198 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12199 SEARCH_CWD is true if the current directory is to be searched.
12200 It will be searched before debug-file-directory.
12201 If successful, the file is added to the bfd include table of the
12202 objfile's bfd (see gdb_bfd_record_inclusion).
12203 If unable to find/open the file, return NULL.
12204 NOTE: This function is derived from symfile_bfd_open. */
12205
12206 static gdb_bfd_ref_ptr
12207 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12208 const char *file_name, int is_dwp, int search_cwd)
12209 {
12210 int desc;
12211 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12212 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12213 to debug_file_directory. */
12214 const char *search_path;
12215 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12216
12217 gdb::unique_xmalloc_ptr<char> search_path_holder;
12218 if (search_cwd)
12219 {
12220 if (*debug_file_directory != '\0')
12221 {
12222 search_path_holder.reset (concat (".", dirname_separator_string,
12223 debug_file_directory,
12224 (char *) NULL));
12225 search_path = search_path_holder.get ();
12226 }
12227 else
12228 search_path = ".";
12229 }
12230 else
12231 search_path = debug_file_directory;
12232
12233 openp_flags flags = OPF_RETURN_REALPATH;
12234 if (is_dwp)
12235 flags |= OPF_SEARCH_IN_PATH;
12236
12237 gdb::unique_xmalloc_ptr<char> absolute_name;
12238 desc = openp (search_path, flags, file_name,
12239 O_RDONLY | O_BINARY, &absolute_name);
12240 if (desc < 0)
12241 return NULL;
12242
12243 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12244 gnutarget, desc));
12245 if (sym_bfd == NULL)
12246 return NULL;
12247 bfd_set_cacheable (sym_bfd.get (), 1);
12248
12249 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12250 return NULL;
12251
12252 /* Success. Record the bfd as having been included by the objfile's bfd.
12253 This is important because things like demangled_names_hash lives in the
12254 objfile's per_bfd space and may have references to things like symbol
12255 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12256 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12257
12258 return sym_bfd;
12259 }
12260
12261 /* Try to open DWO file FILE_NAME.
12262 COMP_DIR is the DW_AT_comp_dir attribute.
12263 The result is the bfd handle of the file.
12264 If there is a problem finding or opening the file, return NULL.
12265 Upon success, the canonicalized path of the file is stored in the bfd,
12266 same as symfile_bfd_open. */
12267
12268 static gdb_bfd_ref_ptr
12269 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12270 const char *file_name, const char *comp_dir)
12271 {
12272 if (IS_ABSOLUTE_PATH (file_name))
12273 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12274 0 /*is_dwp*/, 0 /*search_cwd*/);
12275
12276 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12277
12278 if (comp_dir != NULL)
12279 {
12280 gdb::unique_xmalloc_ptr<char> path_to_try
12281 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12282
12283 /* NOTE: If comp_dir is a relative path, this will also try the
12284 search path, which seems useful. */
12285 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12286 path_to_try.get (),
12287 0 /*is_dwp*/,
12288 1 /*search_cwd*/));
12289 if (abfd != NULL)
12290 return abfd;
12291 }
12292
12293 /* That didn't work, try debug-file-directory, which, despite its name,
12294 is a list of paths. */
12295
12296 if (*debug_file_directory == '\0')
12297 return NULL;
12298
12299 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12300 0 /*is_dwp*/, 1 /*search_cwd*/);
12301 }
12302
12303 /* This function is mapped across the sections and remembers the offset and
12304 size of each of the DWO debugging sections we are interested in. */
12305
12306 static void
12307 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12308 {
12309 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12310 const struct dwop_section_names *names = &dwop_section_names;
12311
12312 if (section_is_p (sectp->name, &names->abbrev_dwo))
12313 {
12314 dwo_sections->abbrev.s.section = sectp;
12315 dwo_sections->abbrev.size = bfd_section_size (sectp);
12316 }
12317 else if (section_is_p (sectp->name, &names->info_dwo))
12318 {
12319 dwo_sections->info.s.section = sectp;
12320 dwo_sections->info.size = bfd_section_size (sectp);
12321 }
12322 else if (section_is_p (sectp->name, &names->line_dwo))
12323 {
12324 dwo_sections->line.s.section = sectp;
12325 dwo_sections->line.size = bfd_section_size (sectp);
12326 }
12327 else if (section_is_p (sectp->name, &names->loc_dwo))
12328 {
12329 dwo_sections->loc.s.section = sectp;
12330 dwo_sections->loc.size = bfd_section_size (sectp);
12331 }
12332 else if (section_is_p (sectp->name, &names->loclists_dwo))
12333 {
12334 dwo_sections->loclists.s.section = sectp;
12335 dwo_sections->loclists.size = bfd_section_size (sectp);
12336 }
12337 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12338 {
12339 dwo_sections->macinfo.s.section = sectp;
12340 dwo_sections->macinfo.size = bfd_section_size (sectp);
12341 }
12342 else if (section_is_p (sectp->name, &names->macro_dwo))
12343 {
12344 dwo_sections->macro.s.section = sectp;
12345 dwo_sections->macro.size = bfd_section_size (sectp);
12346 }
12347 else if (section_is_p (sectp->name, &names->str_dwo))
12348 {
12349 dwo_sections->str.s.section = sectp;
12350 dwo_sections->str.size = bfd_section_size (sectp);
12351 }
12352 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12353 {
12354 dwo_sections->str_offsets.s.section = sectp;
12355 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12356 }
12357 else if (section_is_p (sectp->name, &names->types_dwo))
12358 {
12359 struct dwarf2_section_info type_section;
12360
12361 memset (&type_section, 0, sizeof (type_section));
12362 type_section.s.section = sectp;
12363 type_section.size = bfd_section_size (sectp);
12364 dwo_sections->types.push_back (type_section);
12365 }
12366 }
12367
12368 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12369 by PER_CU. This is for the non-DWP case.
12370 The result is NULL if DWO_NAME can't be found. */
12371
12372 static struct dwo_file *
12373 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12374 const char *comp_dir)
12375 {
12376 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12377
12378 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12379 if (dbfd == NULL)
12380 {
12381 if (dwarf_read_debug)
12382 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12383 return NULL;
12384 }
12385
12386 dwo_file_up dwo_file (new struct dwo_file);
12387 dwo_file->dwo_name = dwo_name;
12388 dwo_file->comp_dir = comp_dir;
12389 dwo_file->dbfd = std::move (dbfd);
12390
12391 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12392 &dwo_file->sections);
12393
12394 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12395 dwo_file->sections.info, dwo_file->cus);
12396
12397 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12398 dwo_file->sections.types, dwo_file->tus);
12399
12400 if (dwarf_read_debug)
12401 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12402
12403 return dwo_file.release ();
12404 }
12405
12406 /* This function is mapped across the sections and remembers the offset and
12407 size of each of the DWP debugging sections common to version 1 and 2 that
12408 we are interested in. */
12409
12410 static void
12411 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12412 void *dwp_file_ptr)
12413 {
12414 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12415 const struct dwop_section_names *names = &dwop_section_names;
12416 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12417
12418 /* Record the ELF section number for later lookup: this is what the
12419 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12420 gdb_assert (elf_section_nr < dwp_file->num_sections);
12421 dwp_file->elf_sections[elf_section_nr] = sectp;
12422
12423 /* Look for specific sections that we need. */
12424 if (section_is_p (sectp->name, &names->str_dwo))
12425 {
12426 dwp_file->sections.str.s.section = sectp;
12427 dwp_file->sections.str.size = bfd_section_size (sectp);
12428 }
12429 else if (section_is_p (sectp->name, &names->cu_index))
12430 {
12431 dwp_file->sections.cu_index.s.section = sectp;
12432 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12433 }
12434 else if (section_is_p (sectp->name, &names->tu_index))
12435 {
12436 dwp_file->sections.tu_index.s.section = sectp;
12437 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12438 }
12439 }
12440
12441 /* This function is mapped across the sections and remembers the offset and
12442 size of each of the DWP version 2 debugging sections that we are interested
12443 in. This is split into a separate function because we don't know if we
12444 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12445
12446 static void
12447 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12448 {
12449 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12450 const struct dwop_section_names *names = &dwop_section_names;
12451 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12452
12453 /* Record the ELF section number for later lookup: this is what the
12454 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12455 gdb_assert (elf_section_nr < dwp_file->num_sections);
12456 dwp_file->elf_sections[elf_section_nr] = sectp;
12457
12458 /* Look for specific sections that we need. */
12459 if (section_is_p (sectp->name, &names->abbrev_dwo))
12460 {
12461 dwp_file->sections.abbrev.s.section = sectp;
12462 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12463 }
12464 else if (section_is_p (sectp->name, &names->info_dwo))
12465 {
12466 dwp_file->sections.info.s.section = sectp;
12467 dwp_file->sections.info.size = bfd_section_size (sectp);
12468 }
12469 else if (section_is_p (sectp->name, &names->line_dwo))
12470 {
12471 dwp_file->sections.line.s.section = sectp;
12472 dwp_file->sections.line.size = bfd_section_size (sectp);
12473 }
12474 else if (section_is_p (sectp->name, &names->loc_dwo))
12475 {
12476 dwp_file->sections.loc.s.section = sectp;
12477 dwp_file->sections.loc.size = bfd_section_size (sectp);
12478 }
12479 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12480 {
12481 dwp_file->sections.macinfo.s.section = sectp;
12482 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12483 }
12484 else if (section_is_p (sectp->name, &names->macro_dwo))
12485 {
12486 dwp_file->sections.macro.s.section = sectp;
12487 dwp_file->sections.macro.size = bfd_section_size (sectp);
12488 }
12489 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12490 {
12491 dwp_file->sections.str_offsets.s.section = sectp;
12492 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12493 }
12494 else if (section_is_p (sectp->name, &names->types_dwo))
12495 {
12496 dwp_file->sections.types.s.section = sectp;
12497 dwp_file->sections.types.size = bfd_section_size (sectp);
12498 }
12499 }
12500
12501 /* Hash function for dwp_file loaded CUs/TUs. */
12502
12503 static hashval_t
12504 hash_dwp_loaded_cutus (const void *item)
12505 {
12506 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12507
12508 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12509 return dwo_unit->signature;
12510 }
12511
12512 /* Equality function for dwp_file loaded CUs/TUs. */
12513
12514 static int
12515 eq_dwp_loaded_cutus (const void *a, const void *b)
12516 {
12517 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12518 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12519
12520 return dua->signature == dub->signature;
12521 }
12522
12523 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12524
12525 static htab_up
12526 allocate_dwp_loaded_cutus_table ()
12527 {
12528 return htab_up (htab_create_alloc (3,
12529 hash_dwp_loaded_cutus,
12530 eq_dwp_loaded_cutus,
12531 NULL, xcalloc, xfree));
12532 }
12533
12534 /* Try to open DWP file FILE_NAME.
12535 The result is the bfd handle of the file.
12536 If there is a problem finding or opening the file, return NULL.
12537 Upon success, the canonicalized path of the file is stored in the bfd,
12538 same as symfile_bfd_open. */
12539
12540 static gdb_bfd_ref_ptr
12541 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12542 const char *file_name)
12543 {
12544 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12545 1 /*is_dwp*/,
12546 1 /*search_cwd*/));
12547 if (abfd != NULL)
12548 return abfd;
12549
12550 /* Work around upstream bug 15652.
12551 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12552 [Whether that's a "bug" is debatable, but it is getting in our way.]
12553 We have no real idea where the dwp file is, because gdb's realpath-ing
12554 of the executable's path may have discarded the needed info.
12555 [IWBN if the dwp file name was recorded in the executable, akin to
12556 .gnu_debuglink, but that doesn't exist yet.]
12557 Strip the directory from FILE_NAME and search again. */
12558 if (*debug_file_directory != '\0')
12559 {
12560 /* Don't implicitly search the current directory here.
12561 If the user wants to search "." to handle this case,
12562 it must be added to debug-file-directory. */
12563 return try_open_dwop_file (dwarf2_per_objfile,
12564 lbasename (file_name), 1 /*is_dwp*/,
12565 0 /*search_cwd*/);
12566 }
12567
12568 return NULL;
12569 }
12570
12571 /* Initialize the use of the DWP file for the current objfile.
12572 By convention the name of the DWP file is ${objfile}.dwp.
12573 The result is NULL if it can't be found. */
12574
12575 static std::unique_ptr<struct dwp_file>
12576 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12577 {
12578 struct objfile *objfile = dwarf2_per_objfile->objfile;
12579
12580 /* Try to find first .dwp for the binary file before any symbolic links
12581 resolving. */
12582
12583 /* If the objfile is a debug file, find the name of the real binary
12584 file and get the name of dwp file from there. */
12585 std::string dwp_name;
12586 if (objfile->separate_debug_objfile_backlink != NULL)
12587 {
12588 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12589 const char *backlink_basename = lbasename (backlink->original_name);
12590
12591 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12592 }
12593 else
12594 dwp_name = objfile->original_name;
12595
12596 dwp_name += ".dwp";
12597
12598 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12599 if (dbfd == NULL
12600 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12601 {
12602 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12603 dwp_name = objfile_name (objfile);
12604 dwp_name += ".dwp";
12605 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12606 }
12607
12608 if (dbfd == NULL)
12609 {
12610 if (dwarf_read_debug)
12611 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12612 return std::unique_ptr<dwp_file> ();
12613 }
12614
12615 const char *name = bfd_get_filename (dbfd.get ());
12616 std::unique_ptr<struct dwp_file> dwp_file
12617 (new struct dwp_file (name, std::move (dbfd)));
12618
12619 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12620 dwp_file->elf_sections =
12621 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12622 dwp_file->num_sections, asection *);
12623
12624 bfd_map_over_sections (dwp_file->dbfd.get (),
12625 dwarf2_locate_common_dwp_sections,
12626 dwp_file.get ());
12627
12628 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12629 0);
12630
12631 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12632 1);
12633
12634 /* The DWP file version is stored in the hash table. Oh well. */
12635 if (dwp_file->cus && dwp_file->tus
12636 && dwp_file->cus->version != dwp_file->tus->version)
12637 {
12638 /* Technically speaking, we should try to limp along, but this is
12639 pretty bizarre. We use pulongest here because that's the established
12640 portability solution (e.g, we cannot use %u for uint32_t). */
12641 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12642 " TU version %s [in DWP file %s]"),
12643 pulongest (dwp_file->cus->version),
12644 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12645 }
12646
12647 if (dwp_file->cus)
12648 dwp_file->version = dwp_file->cus->version;
12649 else if (dwp_file->tus)
12650 dwp_file->version = dwp_file->tus->version;
12651 else
12652 dwp_file->version = 2;
12653
12654 if (dwp_file->version == 2)
12655 bfd_map_over_sections (dwp_file->dbfd.get (),
12656 dwarf2_locate_v2_dwp_sections,
12657 dwp_file.get ());
12658
12659 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12660 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12661
12662 if (dwarf_read_debug)
12663 {
12664 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12665 fprintf_unfiltered (gdb_stdlog,
12666 " %s CUs, %s TUs\n",
12667 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12668 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12669 }
12670
12671 return dwp_file;
12672 }
12673
12674 /* Wrapper around open_and_init_dwp_file, only open it once. */
12675
12676 static struct dwp_file *
12677 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12678 {
12679 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12680 {
12681 dwarf2_per_objfile->per_bfd->dwp_file
12682 = open_and_init_dwp_file (dwarf2_per_objfile);
12683 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12684 }
12685 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12686 }
12687
12688 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12689 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12690 or in the DWP file for the objfile, referenced by THIS_UNIT.
12691 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12692 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12693
12694 This is called, for example, when wanting to read a variable with a
12695 complex location. Therefore we don't want to do file i/o for every call.
12696 Therefore we don't want to look for a DWO file on every call.
12697 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12698 then we check if we've already seen DWO_NAME, and only THEN do we check
12699 for a DWO file.
12700
12701 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12702 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12703
12704 static struct dwo_unit *
12705 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12706 ULONGEST signature, int is_debug_types)
12707 {
12708 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12709 struct objfile *objfile = dwarf2_per_objfile->objfile;
12710 const char *kind = is_debug_types ? "TU" : "CU";
12711 void **dwo_file_slot;
12712 struct dwo_file *dwo_file;
12713 struct dwp_file *dwp_file;
12714
12715 /* First see if there's a DWP file.
12716 If we have a DWP file but didn't find the DWO inside it, don't
12717 look for the original DWO file. It makes gdb behave differently
12718 depending on whether one is debugging in the build tree. */
12719
12720 dwp_file = get_dwp_file (dwarf2_per_objfile);
12721 if (dwp_file != NULL)
12722 {
12723 const struct dwp_hash_table *dwp_htab =
12724 is_debug_types ? dwp_file->tus : dwp_file->cus;
12725
12726 if (dwp_htab != NULL)
12727 {
12728 struct dwo_unit *dwo_cutu =
12729 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12730 signature, is_debug_types);
12731
12732 if (dwo_cutu != NULL)
12733 {
12734 if (dwarf_read_debug)
12735 {
12736 fprintf_unfiltered (gdb_stdlog,
12737 "Virtual DWO %s %s found: @%s\n",
12738 kind, hex_string (signature),
12739 host_address_to_string (dwo_cutu));
12740 }
12741 return dwo_cutu;
12742 }
12743 }
12744 }
12745 else
12746 {
12747 /* No DWP file, look for the DWO file. */
12748
12749 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12750 dwo_name, comp_dir);
12751 if (*dwo_file_slot == NULL)
12752 {
12753 /* Read in the file and build a table of the CUs/TUs it contains. */
12754 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12755 }
12756 /* NOTE: This will be NULL if unable to open the file. */
12757 dwo_file = (struct dwo_file *) *dwo_file_slot;
12758
12759 if (dwo_file != NULL)
12760 {
12761 struct dwo_unit *dwo_cutu = NULL;
12762
12763 if (is_debug_types && dwo_file->tus)
12764 {
12765 struct dwo_unit find_dwo_cutu;
12766
12767 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12768 find_dwo_cutu.signature = signature;
12769 dwo_cutu
12770 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12771 &find_dwo_cutu);
12772 }
12773 else if (!is_debug_types && dwo_file->cus)
12774 {
12775 struct dwo_unit find_dwo_cutu;
12776
12777 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12778 find_dwo_cutu.signature = signature;
12779 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12780 &find_dwo_cutu);
12781 }
12782
12783 if (dwo_cutu != NULL)
12784 {
12785 if (dwarf_read_debug)
12786 {
12787 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12788 kind, dwo_name, hex_string (signature),
12789 host_address_to_string (dwo_cutu));
12790 }
12791 return dwo_cutu;
12792 }
12793 }
12794 }
12795
12796 /* We didn't find it. This could mean a dwo_id mismatch, or
12797 someone deleted the DWO/DWP file, or the search path isn't set up
12798 correctly to find the file. */
12799
12800 if (dwarf_read_debug)
12801 {
12802 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12803 kind, dwo_name, hex_string (signature));
12804 }
12805
12806 /* This is a warning and not a complaint because it can be caused by
12807 pilot error (e.g., user accidentally deleting the DWO). */
12808 {
12809 /* Print the name of the DWP file if we looked there, helps the user
12810 better diagnose the problem. */
12811 std::string dwp_text;
12812
12813 if (dwp_file != NULL)
12814 dwp_text = string_printf (" [in DWP file %s]",
12815 lbasename (dwp_file->name));
12816
12817 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12818 " [in module %s]"),
12819 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12820 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12821 }
12822 return NULL;
12823 }
12824
12825 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12826 See lookup_dwo_cutu_unit for details. */
12827
12828 static struct dwo_unit *
12829 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12830 ULONGEST signature)
12831 {
12832 gdb_assert (!cu->per_cu->is_debug_types);
12833
12834 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12835 }
12836
12837 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12838 See lookup_dwo_cutu_unit for details. */
12839
12840 static struct dwo_unit *
12841 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12842 {
12843 gdb_assert (cu->per_cu->is_debug_types);
12844
12845 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12846
12847 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12848 }
12849
12850 /* Traversal function for queue_and_load_all_dwo_tus. */
12851
12852 static int
12853 queue_and_load_dwo_tu (void **slot, void *info)
12854 {
12855 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12856 dwarf2_cu *cu = (dwarf2_cu *) info;
12857 ULONGEST signature = dwo_unit->signature;
12858 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12859
12860 if (sig_type != NULL)
12861 {
12862 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12863
12864 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12865 a real dependency of PER_CU on SIG_TYPE. That is detected later
12866 while processing PER_CU. */
12867 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12868 load_full_type_unit (sig_cu, cu->per_objfile);
12869 cu->per_cu->imported_symtabs_push (sig_cu);
12870 }
12871
12872 return 1;
12873 }
12874
12875 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12876 The DWO may have the only definition of the type, though it may not be
12877 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12878 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12879
12880 static void
12881 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12882 {
12883 struct dwo_unit *dwo_unit;
12884 struct dwo_file *dwo_file;
12885
12886 gdb_assert (!per_cu->is_debug_types);
12887 gdb_assert (per_cu->cu != NULL);
12888 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
12889
12890 dwo_unit = per_cu->cu->dwo_unit;
12891 gdb_assert (dwo_unit != NULL);
12892
12893 dwo_file = dwo_unit->dwo_file;
12894 if (dwo_file->tus != NULL)
12895 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12896 per_cu->cu);
12897 }
12898
12899 /* Read in various DIEs. */
12900
12901 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12902 Inherit only the children of the DW_AT_abstract_origin DIE not being
12903 already referenced by DW_AT_abstract_origin from the children of the
12904 current DIE. */
12905
12906 static void
12907 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12908 {
12909 struct die_info *child_die;
12910 sect_offset *offsetp;
12911 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12912 struct die_info *origin_die;
12913 /* Iterator of the ORIGIN_DIE children. */
12914 struct die_info *origin_child_die;
12915 struct attribute *attr;
12916 struct dwarf2_cu *origin_cu;
12917 struct pending **origin_previous_list_in_scope;
12918
12919 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12920 if (!attr)
12921 return;
12922
12923 /* Note that following die references may follow to a die in a
12924 different cu. */
12925
12926 origin_cu = cu;
12927 origin_die = follow_die_ref (die, attr, &origin_cu);
12928
12929 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12930 symbols in. */
12931 origin_previous_list_in_scope = origin_cu->list_in_scope;
12932 origin_cu->list_in_scope = cu->list_in_scope;
12933
12934 if (die->tag != origin_die->tag
12935 && !(die->tag == DW_TAG_inlined_subroutine
12936 && origin_die->tag == DW_TAG_subprogram))
12937 complaint (_("DIE %s and its abstract origin %s have different tags"),
12938 sect_offset_str (die->sect_off),
12939 sect_offset_str (origin_die->sect_off));
12940
12941 std::vector<sect_offset> offsets;
12942
12943 for (child_die = die->child;
12944 child_die && child_die->tag;
12945 child_die = child_die->sibling)
12946 {
12947 struct die_info *child_origin_die;
12948 struct dwarf2_cu *child_origin_cu;
12949
12950 /* We are trying to process concrete instance entries:
12951 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12952 it's not relevant to our analysis here. i.e. detecting DIEs that are
12953 present in the abstract instance but not referenced in the concrete
12954 one. */
12955 if (child_die->tag == DW_TAG_call_site
12956 || child_die->tag == DW_TAG_GNU_call_site)
12957 continue;
12958
12959 /* For each CHILD_DIE, find the corresponding child of
12960 ORIGIN_DIE. If there is more than one layer of
12961 DW_AT_abstract_origin, follow them all; there shouldn't be,
12962 but GCC versions at least through 4.4 generate this (GCC PR
12963 40573). */
12964 child_origin_die = child_die;
12965 child_origin_cu = cu;
12966 while (1)
12967 {
12968 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12969 child_origin_cu);
12970 if (attr == NULL)
12971 break;
12972 child_origin_die = follow_die_ref (child_origin_die, attr,
12973 &child_origin_cu);
12974 }
12975
12976 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12977 counterpart may exist. */
12978 if (child_origin_die != child_die)
12979 {
12980 if (child_die->tag != child_origin_die->tag
12981 && !(child_die->tag == DW_TAG_inlined_subroutine
12982 && child_origin_die->tag == DW_TAG_subprogram))
12983 complaint (_("Child DIE %s and its abstract origin %s have "
12984 "different tags"),
12985 sect_offset_str (child_die->sect_off),
12986 sect_offset_str (child_origin_die->sect_off));
12987 if (child_origin_die->parent != origin_die)
12988 complaint (_("Child DIE %s and its abstract origin %s have "
12989 "different parents"),
12990 sect_offset_str (child_die->sect_off),
12991 sect_offset_str (child_origin_die->sect_off));
12992 else
12993 offsets.push_back (child_origin_die->sect_off);
12994 }
12995 }
12996 std::sort (offsets.begin (), offsets.end ());
12997 sect_offset *offsets_end = offsets.data () + offsets.size ();
12998 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12999 if (offsetp[-1] == *offsetp)
13000 complaint (_("Multiple children of DIE %s refer "
13001 "to DIE %s as their abstract origin"),
13002 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13003
13004 offsetp = offsets.data ();
13005 origin_child_die = origin_die->child;
13006 while (origin_child_die && origin_child_die->tag)
13007 {
13008 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13009 while (offsetp < offsets_end
13010 && *offsetp < origin_child_die->sect_off)
13011 offsetp++;
13012 if (offsetp >= offsets_end
13013 || *offsetp > origin_child_die->sect_off)
13014 {
13015 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13016 Check whether we're already processing ORIGIN_CHILD_DIE.
13017 This can happen with mutually referenced abstract_origins.
13018 PR 16581. */
13019 if (!origin_child_die->in_process)
13020 process_die (origin_child_die, origin_cu);
13021 }
13022 origin_child_die = origin_child_die->sibling;
13023 }
13024 origin_cu->list_in_scope = origin_previous_list_in_scope;
13025
13026 if (cu != origin_cu)
13027 compute_delayed_physnames (origin_cu);
13028 }
13029
13030 static void
13031 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13032 {
13033 struct objfile *objfile = cu->per_objfile->objfile;
13034 struct gdbarch *gdbarch = objfile->arch ();
13035 struct context_stack *newobj;
13036 CORE_ADDR lowpc;
13037 CORE_ADDR highpc;
13038 struct die_info *child_die;
13039 struct attribute *attr, *call_line, *call_file;
13040 const char *name;
13041 CORE_ADDR baseaddr;
13042 struct block *block;
13043 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13044 std::vector<struct symbol *> template_args;
13045 struct template_symbol *templ_func = NULL;
13046
13047 if (inlined_func)
13048 {
13049 /* If we do not have call site information, we can't show the
13050 caller of this inlined function. That's too confusing, so
13051 only use the scope for local variables. */
13052 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13053 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13054 if (call_line == NULL || call_file == NULL)
13055 {
13056 read_lexical_block_scope (die, cu);
13057 return;
13058 }
13059 }
13060
13061 baseaddr = objfile->text_section_offset ();
13062
13063 name = dwarf2_name (die, cu);
13064
13065 /* Ignore functions with missing or empty names. These are actually
13066 illegal according to the DWARF standard. */
13067 if (name == NULL)
13068 {
13069 complaint (_("missing name for subprogram DIE at %s"),
13070 sect_offset_str (die->sect_off));
13071 return;
13072 }
13073
13074 /* Ignore functions with missing or invalid low and high pc attributes. */
13075 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13076 <= PC_BOUNDS_INVALID)
13077 {
13078 attr = dwarf2_attr (die, DW_AT_external, cu);
13079 if (!attr || !DW_UNSND (attr))
13080 complaint (_("cannot get low and high bounds "
13081 "for subprogram DIE at %s"),
13082 sect_offset_str (die->sect_off));
13083 return;
13084 }
13085
13086 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13087 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13088
13089 /* If we have any template arguments, then we must allocate a
13090 different sort of symbol. */
13091 for (child_die = die->child; child_die; child_die = child_die->sibling)
13092 {
13093 if (child_die->tag == DW_TAG_template_type_param
13094 || child_die->tag == DW_TAG_template_value_param)
13095 {
13096 templ_func = new (&objfile->objfile_obstack) template_symbol;
13097 templ_func->subclass = SYMBOL_TEMPLATE;
13098 break;
13099 }
13100 }
13101
13102 newobj = cu->get_builder ()->push_context (0, lowpc);
13103 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13104 (struct symbol *) templ_func);
13105
13106 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13107 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13108 cu->language);
13109
13110 /* If there is a location expression for DW_AT_frame_base, record
13111 it. */
13112 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13113 if (attr != nullptr)
13114 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13115
13116 /* If there is a location for the static link, record it. */
13117 newobj->static_link = NULL;
13118 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13119 if (attr != nullptr)
13120 {
13121 newobj->static_link
13122 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13123 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13124 cu->addr_type ());
13125 }
13126
13127 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13128
13129 if (die->child != NULL)
13130 {
13131 child_die = die->child;
13132 while (child_die && child_die->tag)
13133 {
13134 if (child_die->tag == DW_TAG_template_type_param
13135 || child_die->tag == DW_TAG_template_value_param)
13136 {
13137 struct symbol *arg = new_symbol (child_die, NULL, cu);
13138
13139 if (arg != NULL)
13140 template_args.push_back (arg);
13141 }
13142 else
13143 process_die (child_die, cu);
13144 child_die = child_die->sibling;
13145 }
13146 }
13147
13148 inherit_abstract_dies (die, cu);
13149
13150 /* If we have a DW_AT_specification, we might need to import using
13151 directives from the context of the specification DIE. See the
13152 comment in determine_prefix. */
13153 if (cu->language == language_cplus
13154 && dwarf2_attr (die, DW_AT_specification, cu))
13155 {
13156 struct dwarf2_cu *spec_cu = cu;
13157 struct die_info *spec_die = die_specification (die, &spec_cu);
13158
13159 while (spec_die)
13160 {
13161 child_die = spec_die->child;
13162 while (child_die && child_die->tag)
13163 {
13164 if (child_die->tag == DW_TAG_imported_module)
13165 process_die (child_die, spec_cu);
13166 child_die = child_die->sibling;
13167 }
13168
13169 /* In some cases, GCC generates specification DIEs that
13170 themselves contain DW_AT_specification attributes. */
13171 spec_die = die_specification (spec_die, &spec_cu);
13172 }
13173 }
13174
13175 struct context_stack cstk = cu->get_builder ()->pop_context ();
13176 /* Make a block for the local symbols within. */
13177 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13178 cstk.static_link, lowpc, highpc);
13179
13180 /* For C++, set the block's scope. */
13181 if ((cu->language == language_cplus
13182 || cu->language == language_fortran
13183 || cu->language == language_d
13184 || cu->language == language_rust)
13185 && cu->processing_has_namespace_info)
13186 block_set_scope (block, determine_prefix (die, cu),
13187 &objfile->objfile_obstack);
13188
13189 /* If we have address ranges, record them. */
13190 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13191
13192 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13193
13194 /* Attach template arguments to function. */
13195 if (!template_args.empty ())
13196 {
13197 gdb_assert (templ_func != NULL);
13198
13199 templ_func->n_template_arguments = template_args.size ();
13200 templ_func->template_arguments
13201 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13202 templ_func->n_template_arguments);
13203 memcpy (templ_func->template_arguments,
13204 template_args.data (),
13205 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13206
13207 /* Make sure that the symtab is set on the new symbols. Even
13208 though they don't appear in this symtab directly, other parts
13209 of gdb assume that symbols do, and this is reasonably
13210 true. */
13211 for (symbol *sym : template_args)
13212 symbol_set_symtab (sym, symbol_symtab (templ_func));
13213 }
13214
13215 /* In C++, we can have functions nested inside functions (e.g., when
13216 a function declares a class that has methods). This means that
13217 when we finish processing a function scope, we may need to go
13218 back to building a containing block's symbol lists. */
13219 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13220 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13221
13222 /* If we've finished processing a top-level function, subsequent
13223 symbols go in the file symbol list. */
13224 if (cu->get_builder ()->outermost_context_p ())
13225 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13226 }
13227
13228 /* Process all the DIES contained within a lexical block scope. Start
13229 a new scope, process the dies, and then close the scope. */
13230
13231 static void
13232 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13233 {
13234 struct objfile *objfile = cu->per_objfile->objfile;
13235 struct gdbarch *gdbarch = objfile->arch ();
13236 CORE_ADDR lowpc, highpc;
13237 struct die_info *child_die;
13238 CORE_ADDR baseaddr;
13239
13240 baseaddr = objfile->text_section_offset ();
13241
13242 /* Ignore blocks with missing or invalid low and high pc attributes. */
13243 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13244 as multiple lexical blocks? Handling children in a sane way would
13245 be nasty. Might be easier to properly extend generic blocks to
13246 describe ranges. */
13247 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13248 {
13249 case PC_BOUNDS_NOT_PRESENT:
13250 /* DW_TAG_lexical_block has no attributes, process its children as if
13251 there was no wrapping by that DW_TAG_lexical_block.
13252 GCC does no longer produces such DWARF since GCC r224161. */
13253 for (child_die = die->child;
13254 child_die != NULL && child_die->tag;
13255 child_die = child_die->sibling)
13256 {
13257 /* We might already be processing this DIE. This can happen
13258 in an unusual circumstance -- where a subroutine A
13259 appears lexically in another subroutine B, but A actually
13260 inlines B. The recursion is broken here, rather than in
13261 inherit_abstract_dies, because it seems better to simply
13262 drop concrete children here. */
13263 if (!child_die->in_process)
13264 process_die (child_die, cu);
13265 }
13266 return;
13267 case PC_BOUNDS_INVALID:
13268 return;
13269 }
13270 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13271 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13272
13273 cu->get_builder ()->push_context (0, lowpc);
13274 if (die->child != NULL)
13275 {
13276 child_die = die->child;
13277 while (child_die && child_die->tag)
13278 {
13279 process_die (child_die, cu);
13280 child_die = child_die->sibling;
13281 }
13282 }
13283 inherit_abstract_dies (die, cu);
13284 struct context_stack cstk = cu->get_builder ()->pop_context ();
13285
13286 if (*cu->get_builder ()->get_local_symbols () != NULL
13287 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13288 {
13289 struct block *block
13290 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13291 cstk.start_addr, highpc);
13292
13293 /* Note that recording ranges after traversing children, as we
13294 do here, means that recording a parent's ranges entails
13295 walking across all its children's ranges as they appear in
13296 the address map, which is quadratic behavior.
13297
13298 It would be nicer to record the parent's ranges before
13299 traversing its children, simply overriding whatever you find
13300 there. But since we don't even decide whether to create a
13301 block until after we've traversed its children, that's hard
13302 to do. */
13303 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13304 }
13305 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13306 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13307 }
13308
13309 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13310
13311 static void
13312 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13313 {
13314 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13315 struct objfile *objfile = per_objfile->objfile;
13316 struct gdbarch *gdbarch = objfile->arch ();
13317 CORE_ADDR pc, baseaddr;
13318 struct attribute *attr;
13319 struct call_site *call_site, call_site_local;
13320 void **slot;
13321 int nparams;
13322 struct die_info *child_die;
13323
13324 baseaddr = objfile->text_section_offset ();
13325
13326 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13327 if (attr == NULL)
13328 {
13329 /* This was a pre-DWARF-5 GNU extension alias
13330 for DW_AT_call_return_pc. */
13331 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13332 }
13333 if (!attr)
13334 {
13335 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13336 "DIE %s [in module %s]"),
13337 sect_offset_str (die->sect_off), objfile_name (objfile));
13338 return;
13339 }
13340 pc = attr->value_as_address () + baseaddr;
13341 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13342
13343 if (cu->call_site_htab == NULL)
13344 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13345 NULL, &objfile->objfile_obstack,
13346 hashtab_obstack_allocate, NULL);
13347 call_site_local.pc = pc;
13348 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13349 if (*slot != NULL)
13350 {
13351 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13352 "DIE %s [in module %s]"),
13353 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13354 objfile_name (objfile));
13355 return;
13356 }
13357
13358 /* Count parameters at the caller. */
13359
13360 nparams = 0;
13361 for (child_die = die->child; child_die && child_die->tag;
13362 child_die = child_die->sibling)
13363 {
13364 if (child_die->tag != DW_TAG_call_site_parameter
13365 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13366 {
13367 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13368 "DW_TAG_call_site child DIE %s [in module %s]"),
13369 child_die->tag, sect_offset_str (child_die->sect_off),
13370 objfile_name (objfile));
13371 continue;
13372 }
13373
13374 nparams++;
13375 }
13376
13377 call_site
13378 = ((struct call_site *)
13379 obstack_alloc (&objfile->objfile_obstack,
13380 sizeof (*call_site)
13381 + (sizeof (*call_site->parameter) * (nparams - 1))));
13382 *slot = call_site;
13383 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13384 call_site->pc = pc;
13385
13386 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13387 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13388 {
13389 struct die_info *func_die;
13390
13391 /* Skip also over DW_TAG_inlined_subroutine. */
13392 for (func_die = die->parent;
13393 func_die && func_die->tag != DW_TAG_subprogram
13394 && func_die->tag != DW_TAG_subroutine_type;
13395 func_die = func_die->parent);
13396
13397 /* DW_AT_call_all_calls is a superset
13398 of DW_AT_call_all_tail_calls. */
13399 if (func_die
13400 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13401 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13402 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13403 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13404 {
13405 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13406 not complete. But keep CALL_SITE for look ups via call_site_htab,
13407 both the initial caller containing the real return address PC and
13408 the final callee containing the current PC of a chain of tail
13409 calls do not need to have the tail call list complete. But any
13410 function candidate for a virtual tail call frame searched via
13411 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13412 determined unambiguously. */
13413 }
13414 else
13415 {
13416 struct type *func_type = NULL;
13417
13418 if (func_die)
13419 func_type = get_die_type (func_die, cu);
13420 if (func_type != NULL)
13421 {
13422 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13423
13424 /* Enlist this call site to the function. */
13425 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13426 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13427 }
13428 else
13429 complaint (_("Cannot find function owning DW_TAG_call_site "
13430 "DIE %s [in module %s]"),
13431 sect_offset_str (die->sect_off), objfile_name (objfile));
13432 }
13433 }
13434
13435 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13436 if (attr == NULL)
13437 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13438 if (attr == NULL)
13439 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13440 if (attr == NULL)
13441 {
13442 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13443 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13444 }
13445 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13446 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13447 /* Keep NULL DWARF_BLOCK. */;
13448 else if (attr->form_is_block ())
13449 {
13450 struct dwarf2_locexpr_baton *dlbaton;
13451
13452 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13453 dlbaton->data = DW_BLOCK (attr)->data;
13454 dlbaton->size = DW_BLOCK (attr)->size;
13455 dlbaton->per_objfile = per_objfile;
13456 dlbaton->per_cu = cu->per_cu;
13457
13458 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13459 }
13460 else if (attr->form_is_ref ())
13461 {
13462 struct dwarf2_cu *target_cu = cu;
13463 struct die_info *target_die;
13464
13465 target_die = follow_die_ref (die, attr, &target_cu);
13466 gdb_assert (target_cu->per_objfile->objfile == objfile);
13467 if (die_is_declaration (target_die, target_cu))
13468 {
13469 const char *target_physname;
13470
13471 /* Prefer the mangled name; otherwise compute the demangled one. */
13472 target_physname = dw2_linkage_name (target_die, target_cu);
13473 if (target_physname == NULL)
13474 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13475 if (target_physname == NULL)
13476 complaint (_("DW_AT_call_target target DIE has invalid "
13477 "physname, for referencing DIE %s [in module %s]"),
13478 sect_offset_str (die->sect_off), objfile_name (objfile));
13479 else
13480 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13481 }
13482 else
13483 {
13484 CORE_ADDR lowpc;
13485
13486 /* DW_AT_entry_pc should be preferred. */
13487 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13488 <= PC_BOUNDS_INVALID)
13489 complaint (_("DW_AT_call_target target DIE has invalid "
13490 "low pc, for referencing DIE %s [in module %s]"),
13491 sect_offset_str (die->sect_off), objfile_name (objfile));
13492 else
13493 {
13494 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13495 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13496 }
13497 }
13498 }
13499 else
13500 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13501 "block nor reference, for DIE %s [in module %s]"),
13502 sect_offset_str (die->sect_off), objfile_name (objfile));
13503
13504 call_site->per_cu = cu->per_cu;
13505 call_site->per_objfile = per_objfile;
13506
13507 for (child_die = die->child;
13508 child_die && child_die->tag;
13509 child_die = child_die->sibling)
13510 {
13511 struct call_site_parameter *parameter;
13512 struct attribute *loc, *origin;
13513
13514 if (child_die->tag != DW_TAG_call_site_parameter
13515 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13516 {
13517 /* Already printed the complaint above. */
13518 continue;
13519 }
13520
13521 gdb_assert (call_site->parameter_count < nparams);
13522 parameter = &call_site->parameter[call_site->parameter_count];
13523
13524 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13525 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13526 register is contained in DW_AT_call_value. */
13527
13528 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13529 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13530 if (origin == NULL)
13531 {
13532 /* This was a pre-DWARF-5 GNU extension alias
13533 for DW_AT_call_parameter. */
13534 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13535 }
13536 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13537 {
13538 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13539
13540 sect_offset sect_off = origin->get_ref_die_offset ();
13541 if (!cu->header.offset_in_cu_p (sect_off))
13542 {
13543 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13544 binding can be done only inside one CU. Such referenced DIE
13545 therefore cannot be even moved to DW_TAG_partial_unit. */
13546 complaint (_("DW_AT_call_parameter offset is not in CU for "
13547 "DW_TAG_call_site child DIE %s [in module %s]"),
13548 sect_offset_str (child_die->sect_off),
13549 objfile_name (objfile));
13550 continue;
13551 }
13552 parameter->u.param_cu_off
13553 = (cu_offset) (sect_off - cu->header.sect_off);
13554 }
13555 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13556 {
13557 complaint (_("No DW_FORM_block* DW_AT_location for "
13558 "DW_TAG_call_site child DIE %s [in module %s]"),
13559 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13560 continue;
13561 }
13562 else
13563 {
13564 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13565 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13566 if (parameter->u.dwarf_reg != -1)
13567 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13568 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13569 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13570 &parameter->u.fb_offset))
13571 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13572 else
13573 {
13574 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13575 "for DW_FORM_block* DW_AT_location is supported for "
13576 "DW_TAG_call_site child DIE %s "
13577 "[in module %s]"),
13578 sect_offset_str (child_die->sect_off),
13579 objfile_name (objfile));
13580 continue;
13581 }
13582 }
13583
13584 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13585 if (attr == NULL)
13586 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13587 if (attr == NULL || !attr->form_is_block ())
13588 {
13589 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13590 "DW_TAG_call_site child DIE %s [in module %s]"),
13591 sect_offset_str (child_die->sect_off),
13592 objfile_name (objfile));
13593 continue;
13594 }
13595 parameter->value = DW_BLOCK (attr)->data;
13596 parameter->value_size = DW_BLOCK (attr)->size;
13597
13598 /* Parameters are not pre-cleared by memset above. */
13599 parameter->data_value = NULL;
13600 parameter->data_value_size = 0;
13601 call_site->parameter_count++;
13602
13603 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13604 if (attr == NULL)
13605 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13606 if (attr != nullptr)
13607 {
13608 if (!attr->form_is_block ())
13609 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13610 "DW_TAG_call_site child DIE %s [in module %s]"),
13611 sect_offset_str (child_die->sect_off),
13612 objfile_name (objfile));
13613 else
13614 {
13615 parameter->data_value = DW_BLOCK (attr)->data;
13616 parameter->data_value_size = DW_BLOCK (attr)->size;
13617 }
13618 }
13619 }
13620 }
13621
13622 /* Helper function for read_variable. If DIE represents a virtual
13623 table, then return the type of the concrete object that is
13624 associated with the virtual table. Otherwise, return NULL. */
13625
13626 static struct type *
13627 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13628 {
13629 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13630 if (attr == NULL)
13631 return NULL;
13632
13633 /* Find the type DIE. */
13634 struct die_info *type_die = NULL;
13635 struct dwarf2_cu *type_cu = cu;
13636
13637 if (attr->form_is_ref ())
13638 type_die = follow_die_ref (die, attr, &type_cu);
13639 if (type_die == NULL)
13640 return NULL;
13641
13642 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13643 return NULL;
13644 return die_containing_type (type_die, type_cu);
13645 }
13646
13647 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13648
13649 static void
13650 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13651 {
13652 struct rust_vtable_symbol *storage = NULL;
13653
13654 if (cu->language == language_rust)
13655 {
13656 struct type *containing_type = rust_containing_type (die, cu);
13657
13658 if (containing_type != NULL)
13659 {
13660 struct objfile *objfile = cu->per_objfile->objfile;
13661
13662 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13663 storage->concrete_type = containing_type;
13664 storage->subclass = SYMBOL_RUST_VTABLE;
13665 }
13666 }
13667
13668 struct symbol *res = new_symbol (die, NULL, cu, storage);
13669 struct attribute *abstract_origin
13670 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13671 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13672 if (res == NULL && loc && abstract_origin)
13673 {
13674 /* We have a variable without a name, but with a location and an abstract
13675 origin. This may be a concrete instance of an abstract variable
13676 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13677 later. */
13678 struct dwarf2_cu *origin_cu = cu;
13679 struct die_info *origin_die
13680 = follow_die_ref (die, abstract_origin, &origin_cu);
13681 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13682 per_objfile->per_bfd->abstract_to_concrete
13683 [origin_die->sect_off].push_back (die->sect_off);
13684 }
13685 }
13686
13687 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13688 reading .debug_rnglists.
13689 Callback's type should be:
13690 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13691 Return true if the attributes are present and valid, otherwise,
13692 return false. */
13693
13694 template <typename Callback>
13695 static bool
13696 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13697 Callback &&callback)
13698 {
13699 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13700 struct objfile *objfile = dwarf2_per_objfile->objfile;
13701 bfd *obfd = objfile->obfd;
13702 /* Base address selection entry. */
13703 gdb::optional<CORE_ADDR> base;
13704 const gdb_byte *buffer;
13705 CORE_ADDR baseaddr;
13706 bool overflow = false;
13707
13708 base = cu->base_address;
13709
13710 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13711 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13712 {
13713 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13714 offset);
13715 return false;
13716 }
13717 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13718
13719 baseaddr = objfile->text_section_offset ();
13720
13721 while (1)
13722 {
13723 /* Initialize it due to a false compiler warning. */
13724 CORE_ADDR range_beginning = 0, range_end = 0;
13725 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13726 + dwarf2_per_objfile->per_bfd->rnglists.size);
13727 unsigned int bytes_read;
13728
13729 if (buffer == buf_end)
13730 {
13731 overflow = true;
13732 break;
13733 }
13734 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13735 switch (rlet)
13736 {
13737 case DW_RLE_end_of_list:
13738 break;
13739 case DW_RLE_base_address:
13740 if (buffer + cu->header.addr_size > buf_end)
13741 {
13742 overflow = true;
13743 break;
13744 }
13745 base = cu->header.read_address (obfd, buffer, &bytes_read);
13746 buffer += bytes_read;
13747 break;
13748 case DW_RLE_start_length:
13749 if (buffer + cu->header.addr_size > buf_end)
13750 {
13751 overflow = true;
13752 break;
13753 }
13754 range_beginning = cu->header.read_address (obfd, buffer,
13755 &bytes_read);
13756 buffer += bytes_read;
13757 range_end = (range_beginning
13758 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13759 buffer += bytes_read;
13760 if (buffer > buf_end)
13761 {
13762 overflow = true;
13763 break;
13764 }
13765 break;
13766 case DW_RLE_offset_pair:
13767 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13768 buffer += bytes_read;
13769 if (buffer > buf_end)
13770 {
13771 overflow = true;
13772 break;
13773 }
13774 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13775 buffer += bytes_read;
13776 if (buffer > buf_end)
13777 {
13778 overflow = true;
13779 break;
13780 }
13781 break;
13782 case DW_RLE_start_end:
13783 if (buffer + 2 * cu->header.addr_size > buf_end)
13784 {
13785 overflow = true;
13786 break;
13787 }
13788 range_beginning = cu->header.read_address (obfd, buffer,
13789 &bytes_read);
13790 buffer += bytes_read;
13791 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13792 buffer += bytes_read;
13793 break;
13794 default:
13795 complaint (_("Invalid .debug_rnglists data (no base address)"));
13796 return false;
13797 }
13798 if (rlet == DW_RLE_end_of_list || overflow)
13799 break;
13800 if (rlet == DW_RLE_base_address)
13801 continue;
13802
13803 if (!base.has_value ())
13804 {
13805 /* We have no valid base address for the ranges
13806 data. */
13807 complaint (_("Invalid .debug_rnglists data (no base address)"));
13808 return false;
13809 }
13810
13811 if (range_beginning > range_end)
13812 {
13813 /* Inverted range entries are invalid. */
13814 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13815 return false;
13816 }
13817
13818 /* Empty range entries have no effect. */
13819 if (range_beginning == range_end)
13820 continue;
13821
13822 range_beginning += *base;
13823 range_end += *base;
13824
13825 /* A not-uncommon case of bad debug info.
13826 Don't pollute the addrmap with bad data. */
13827 if (range_beginning + baseaddr == 0
13828 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13829 {
13830 complaint (_(".debug_rnglists entry has start address of zero"
13831 " [in module %s]"), objfile_name (objfile));
13832 continue;
13833 }
13834
13835 callback (range_beginning, range_end);
13836 }
13837
13838 if (overflow)
13839 {
13840 complaint (_("Offset %d is not terminated "
13841 "for DW_AT_ranges attribute"),
13842 offset);
13843 return false;
13844 }
13845
13846 return true;
13847 }
13848
13849 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13850 Callback's type should be:
13851 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13852 Return 1 if the attributes are present and valid, otherwise, return 0. */
13853
13854 template <typename Callback>
13855 static int
13856 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13857 Callback &&callback)
13858 {
13859 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13860 struct objfile *objfile = per_objfile->objfile;
13861 struct comp_unit_head *cu_header = &cu->header;
13862 bfd *obfd = objfile->obfd;
13863 unsigned int addr_size = cu_header->addr_size;
13864 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13865 /* Base address selection entry. */
13866 gdb::optional<CORE_ADDR> base;
13867 unsigned int dummy;
13868 const gdb_byte *buffer;
13869 CORE_ADDR baseaddr;
13870
13871 if (cu_header->version >= 5)
13872 return dwarf2_rnglists_process (offset, cu, callback);
13873
13874 base = cu->base_address;
13875
13876 per_objfile->per_bfd->ranges.read (objfile);
13877 if (offset >= per_objfile->per_bfd->ranges.size)
13878 {
13879 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13880 offset);
13881 return 0;
13882 }
13883 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13884
13885 baseaddr = objfile->text_section_offset ();
13886
13887 while (1)
13888 {
13889 CORE_ADDR range_beginning, range_end;
13890
13891 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13892 buffer += addr_size;
13893 range_end = cu->header.read_address (obfd, buffer, &dummy);
13894 buffer += addr_size;
13895 offset += 2 * addr_size;
13896
13897 /* An end of list marker is a pair of zero addresses. */
13898 if (range_beginning == 0 && range_end == 0)
13899 /* Found the end of list entry. */
13900 break;
13901
13902 /* Each base address selection entry is a pair of 2 values.
13903 The first is the largest possible address, the second is
13904 the base address. Check for a base address here. */
13905 if ((range_beginning & mask) == mask)
13906 {
13907 /* If we found the largest possible address, then we already
13908 have the base address in range_end. */
13909 base = range_end;
13910 continue;
13911 }
13912
13913 if (!base.has_value ())
13914 {
13915 /* We have no valid base address for the ranges
13916 data. */
13917 complaint (_("Invalid .debug_ranges data (no base address)"));
13918 return 0;
13919 }
13920
13921 if (range_beginning > range_end)
13922 {
13923 /* Inverted range entries are invalid. */
13924 complaint (_("Invalid .debug_ranges data (inverted range)"));
13925 return 0;
13926 }
13927
13928 /* Empty range entries have no effect. */
13929 if (range_beginning == range_end)
13930 continue;
13931
13932 range_beginning += *base;
13933 range_end += *base;
13934
13935 /* A not-uncommon case of bad debug info.
13936 Don't pollute the addrmap with bad data. */
13937 if (range_beginning + baseaddr == 0
13938 && !per_objfile->per_bfd->has_section_at_zero)
13939 {
13940 complaint (_(".debug_ranges entry has start address of zero"
13941 " [in module %s]"), objfile_name (objfile));
13942 continue;
13943 }
13944
13945 callback (range_beginning, range_end);
13946 }
13947
13948 return 1;
13949 }
13950
13951 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13952 Return 1 if the attributes are present and valid, otherwise, return 0.
13953 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13954
13955 static int
13956 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13957 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13958 dwarf2_psymtab *ranges_pst)
13959 {
13960 struct objfile *objfile = cu->per_objfile->objfile;
13961 struct gdbarch *gdbarch = objfile->arch ();
13962 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13963 int low_set = 0;
13964 CORE_ADDR low = 0;
13965 CORE_ADDR high = 0;
13966 int retval;
13967
13968 retval = dwarf2_ranges_process (offset, cu,
13969 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13970 {
13971 if (ranges_pst != NULL)
13972 {
13973 CORE_ADDR lowpc;
13974 CORE_ADDR highpc;
13975
13976 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13977 range_beginning + baseaddr)
13978 - baseaddr);
13979 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13980 range_end + baseaddr)
13981 - baseaddr);
13982 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13983 lowpc, highpc - 1, ranges_pst);
13984 }
13985
13986 /* FIXME: This is recording everything as a low-high
13987 segment of consecutive addresses. We should have a
13988 data structure for discontiguous block ranges
13989 instead. */
13990 if (! low_set)
13991 {
13992 low = range_beginning;
13993 high = range_end;
13994 low_set = 1;
13995 }
13996 else
13997 {
13998 if (range_beginning < low)
13999 low = range_beginning;
14000 if (range_end > high)
14001 high = range_end;
14002 }
14003 });
14004 if (!retval)
14005 return 0;
14006
14007 if (! low_set)
14008 /* If the first entry is an end-of-list marker, the range
14009 describes an empty scope, i.e. no instructions. */
14010 return 0;
14011
14012 if (low_return)
14013 *low_return = low;
14014 if (high_return)
14015 *high_return = high;
14016 return 1;
14017 }
14018
14019 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14020 definition for the return value. *LOWPC and *HIGHPC are set iff
14021 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14022
14023 static enum pc_bounds_kind
14024 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14025 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14026 dwarf2_psymtab *pst)
14027 {
14028 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14029 struct attribute *attr;
14030 struct attribute *attr_high;
14031 CORE_ADDR low = 0;
14032 CORE_ADDR high = 0;
14033 enum pc_bounds_kind ret;
14034
14035 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14036 if (attr_high)
14037 {
14038 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14039 if (attr != nullptr)
14040 {
14041 low = attr->value_as_address ();
14042 high = attr_high->value_as_address ();
14043 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14044 high += low;
14045 }
14046 else
14047 /* Found high w/o low attribute. */
14048 return PC_BOUNDS_INVALID;
14049
14050 /* Found consecutive range of addresses. */
14051 ret = PC_BOUNDS_HIGH_LOW;
14052 }
14053 else
14054 {
14055 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14056 if (attr != NULL)
14057 {
14058 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14059 We take advantage of the fact that DW_AT_ranges does not appear
14060 in DW_TAG_compile_unit of DWO files. */
14061 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14062 unsigned int ranges_offset = (DW_UNSND (attr)
14063 + (need_ranges_base
14064 ? cu->ranges_base
14065 : 0));
14066
14067 /* Value of the DW_AT_ranges attribute is the offset in the
14068 .debug_ranges section. */
14069 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14070 return PC_BOUNDS_INVALID;
14071 /* Found discontinuous range of addresses. */
14072 ret = PC_BOUNDS_RANGES;
14073 }
14074 else
14075 return PC_BOUNDS_NOT_PRESENT;
14076 }
14077
14078 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14079 if (high <= low)
14080 return PC_BOUNDS_INVALID;
14081
14082 /* When using the GNU linker, .gnu.linkonce. sections are used to
14083 eliminate duplicate copies of functions and vtables and such.
14084 The linker will arbitrarily choose one and discard the others.
14085 The AT_*_pc values for such functions refer to local labels in
14086 these sections. If the section from that file was discarded, the
14087 labels are not in the output, so the relocs get a value of 0.
14088 If this is a discarded function, mark the pc bounds as invalid,
14089 so that GDB will ignore it. */
14090 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14091 return PC_BOUNDS_INVALID;
14092
14093 *lowpc = low;
14094 if (highpc)
14095 *highpc = high;
14096 return ret;
14097 }
14098
14099 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14100 its low and high PC addresses. Do nothing if these addresses could not
14101 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14102 and HIGHPC to the high address if greater than HIGHPC. */
14103
14104 static void
14105 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14107 struct dwarf2_cu *cu)
14108 {
14109 CORE_ADDR low, high;
14110 struct die_info *child = die->child;
14111
14112 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14113 {
14114 *lowpc = std::min (*lowpc, low);
14115 *highpc = std::max (*highpc, high);
14116 }
14117
14118 /* If the language does not allow nested subprograms (either inside
14119 subprograms or lexical blocks), we're done. */
14120 if (cu->language != language_ada)
14121 return;
14122
14123 /* Check all the children of the given DIE. If it contains nested
14124 subprograms, then check their pc bounds. Likewise, we need to
14125 check lexical blocks as well, as they may also contain subprogram
14126 definitions. */
14127 while (child && child->tag)
14128 {
14129 if (child->tag == DW_TAG_subprogram
14130 || child->tag == DW_TAG_lexical_block)
14131 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14132 child = child->sibling;
14133 }
14134 }
14135
14136 /* Get the low and high pc's represented by the scope DIE, and store
14137 them in *LOWPC and *HIGHPC. If the correct values can't be
14138 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14139
14140 static void
14141 get_scope_pc_bounds (struct die_info *die,
14142 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14143 struct dwarf2_cu *cu)
14144 {
14145 CORE_ADDR best_low = (CORE_ADDR) -1;
14146 CORE_ADDR best_high = (CORE_ADDR) 0;
14147 CORE_ADDR current_low, current_high;
14148
14149 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14150 >= PC_BOUNDS_RANGES)
14151 {
14152 best_low = current_low;
14153 best_high = current_high;
14154 }
14155 else
14156 {
14157 struct die_info *child = die->child;
14158
14159 while (child && child->tag)
14160 {
14161 switch (child->tag) {
14162 case DW_TAG_subprogram:
14163 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14164 break;
14165 case DW_TAG_namespace:
14166 case DW_TAG_module:
14167 /* FIXME: carlton/2004-01-16: Should we do this for
14168 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14169 that current GCC's always emit the DIEs corresponding
14170 to definitions of methods of classes as children of a
14171 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14172 the DIEs giving the declarations, which could be
14173 anywhere). But I don't see any reason why the
14174 standards says that they have to be there. */
14175 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14176
14177 if (current_low != ((CORE_ADDR) -1))
14178 {
14179 best_low = std::min (best_low, current_low);
14180 best_high = std::max (best_high, current_high);
14181 }
14182 break;
14183 default:
14184 /* Ignore. */
14185 break;
14186 }
14187
14188 child = child->sibling;
14189 }
14190 }
14191
14192 *lowpc = best_low;
14193 *highpc = best_high;
14194 }
14195
14196 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14197 in DIE. */
14198
14199 static void
14200 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14201 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14202 {
14203 struct objfile *objfile = cu->per_objfile->objfile;
14204 struct gdbarch *gdbarch = objfile->arch ();
14205 struct attribute *attr;
14206 struct attribute *attr_high;
14207
14208 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14209 if (attr_high)
14210 {
14211 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14212 if (attr != nullptr)
14213 {
14214 CORE_ADDR low = attr->value_as_address ();
14215 CORE_ADDR high = attr_high->value_as_address ();
14216
14217 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14218 high += low;
14219
14220 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14221 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14222 cu->get_builder ()->record_block_range (block, low, high - 1);
14223 }
14224 }
14225
14226 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14227 if (attr != nullptr)
14228 {
14229 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14230 We take advantage of the fact that DW_AT_ranges does not appear
14231 in DW_TAG_compile_unit of DWO files. */
14232 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14233
14234 /* The value of the DW_AT_ranges attribute is the offset of the
14235 address range list in the .debug_ranges section. */
14236 unsigned long offset = (DW_UNSND (attr)
14237 + (need_ranges_base ? cu->ranges_base : 0));
14238
14239 std::vector<blockrange> blockvec;
14240 dwarf2_ranges_process (offset, cu,
14241 [&] (CORE_ADDR start, CORE_ADDR end)
14242 {
14243 start += baseaddr;
14244 end += baseaddr;
14245 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14246 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14247 cu->get_builder ()->record_block_range (block, start, end - 1);
14248 blockvec.emplace_back (start, end);
14249 });
14250
14251 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14252 }
14253 }
14254
14255 /* Check whether the producer field indicates either of GCC < 4.6, or the
14256 Intel C/C++ compiler, and cache the result in CU. */
14257
14258 static void
14259 check_producer (struct dwarf2_cu *cu)
14260 {
14261 int major, minor;
14262
14263 if (cu->producer == NULL)
14264 {
14265 /* For unknown compilers expect their behavior is DWARF version
14266 compliant.
14267
14268 GCC started to support .debug_types sections by -gdwarf-4 since
14269 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14270 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14271 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14272 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14273 }
14274 else if (producer_is_gcc (cu->producer, &major, &minor))
14275 {
14276 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14277 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14278 }
14279 else if (producer_is_icc (cu->producer, &major, &minor))
14280 {
14281 cu->producer_is_icc = true;
14282 cu->producer_is_icc_lt_14 = major < 14;
14283 }
14284 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14285 cu->producer_is_codewarrior = true;
14286 else
14287 {
14288 /* For other non-GCC compilers, expect their behavior is DWARF version
14289 compliant. */
14290 }
14291
14292 cu->checked_producer = true;
14293 }
14294
14295 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14296 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14297 during 4.6.0 experimental. */
14298
14299 static bool
14300 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14301 {
14302 if (!cu->checked_producer)
14303 check_producer (cu);
14304
14305 return cu->producer_is_gxx_lt_4_6;
14306 }
14307
14308
14309 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14310 with incorrect is_stmt attributes. */
14311
14312 static bool
14313 producer_is_codewarrior (struct dwarf2_cu *cu)
14314 {
14315 if (!cu->checked_producer)
14316 check_producer (cu);
14317
14318 return cu->producer_is_codewarrior;
14319 }
14320
14321 /* Return the default accessibility type if it is not overridden by
14322 DW_AT_accessibility. */
14323
14324 static enum dwarf_access_attribute
14325 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14326 {
14327 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14328 {
14329 /* The default DWARF 2 accessibility for members is public, the default
14330 accessibility for inheritance is private. */
14331
14332 if (die->tag != DW_TAG_inheritance)
14333 return DW_ACCESS_public;
14334 else
14335 return DW_ACCESS_private;
14336 }
14337 else
14338 {
14339 /* DWARF 3+ defines the default accessibility a different way. The same
14340 rules apply now for DW_TAG_inheritance as for the members and it only
14341 depends on the container kind. */
14342
14343 if (die->parent->tag == DW_TAG_class_type)
14344 return DW_ACCESS_private;
14345 else
14346 return DW_ACCESS_public;
14347 }
14348 }
14349
14350 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14351 offset. If the attribute was not found return 0, otherwise return
14352 1. If it was found but could not properly be handled, set *OFFSET
14353 to 0. */
14354
14355 static int
14356 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14357 LONGEST *offset)
14358 {
14359 struct attribute *attr;
14360
14361 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14362 if (attr != NULL)
14363 {
14364 *offset = 0;
14365
14366 /* Note that we do not check for a section offset first here.
14367 This is because DW_AT_data_member_location is new in DWARF 4,
14368 so if we see it, we can assume that a constant form is really
14369 a constant and not a section offset. */
14370 if (attr->form_is_constant ())
14371 *offset = attr->constant_value (0);
14372 else if (attr->form_is_section_offset ())
14373 dwarf2_complex_location_expr_complaint ();
14374 else if (attr->form_is_block ())
14375 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14376 else
14377 dwarf2_complex_location_expr_complaint ();
14378
14379 return 1;
14380 }
14381
14382 return 0;
14383 }
14384
14385 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14386
14387 static void
14388 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14389 struct field *field)
14390 {
14391 struct attribute *attr;
14392
14393 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14394 if (attr != NULL)
14395 {
14396 if (attr->form_is_constant ())
14397 {
14398 LONGEST offset = attr->constant_value (0);
14399 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14400 }
14401 else if (attr->form_is_section_offset ())
14402 dwarf2_complex_location_expr_complaint ();
14403 else if (attr->form_is_block ())
14404 {
14405 bool handled;
14406 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14407 if (handled)
14408 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14409 else
14410 {
14411 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14412 struct objfile *objfile = per_objfile->objfile;
14413 struct dwarf2_locexpr_baton *dlbaton
14414 = XOBNEW (&objfile->objfile_obstack,
14415 struct dwarf2_locexpr_baton);
14416 dlbaton->data = DW_BLOCK (attr)->data;
14417 dlbaton->size = DW_BLOCK (attr)->size;
14418 /* When using this baton, we want to compute the address
14419 of the field, not the value. This is why
14420 is_reference is set to false here. */
14421 dlbaton->is_reference = false;
14422 dlbaton->per_objfile = per_objfile;
14423 dlbaton->per_cu = cu->per_cu;
14424
14425 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14426 }
14427 }
14428 else
14429 dwarf2_complex_location_expr_complaint ();
14430 }
14431 }
14432
14433 /* Add an aggregate field to the field list. */
14434
14435 static void
14436 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14437 struct dwarf2_cu *cu)
14438 {
14439 struct objfile *objfile = cu->per_objfile->objfile;
14440 struct gdbarch *gdbarch = objfile->arch ();
14441 struct nextfield *new_field;
14442 struct attribute *attr;
14443 struct field *fp;
14444 const char *fieldname = "";
14445
14446 if (die->tag == DW_TAG_inheritance)
14447 {
14448 fip->baseclasses.emplace_back ();
14449 new_field = &fip->baseclasses.back ();
14450 }
14451 else
14452 {
14453 fip->fields.emplace_back ();
14454 new_field = &fip->fields.back ();
14455 }
14456
14457 new_field->offset = die->sect_off;
14458
14459 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14460 if (attr != nullptr)
14461 new_field->accessibility = DW_UNSND (attr);
14462 else
14463 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14464 if (new_field->accessibility != DW_ACCESS_public)
14465 fip->non_public_fields = 1;
14466
14467 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14468 if (attr != nullptr)
14469 new_field->virtuality = DW_UNSND (attr);
14470 else
14471 new_field->virtuality = DW_VIRTUALITY_none;
14472
14473 fp = &new_field->field;
14474
14475 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14476 {
14477 /* Data member other than a C++ static data member. */
14478
14479 /* Get type of field. */
14480 fp->type = die_type (die, cu);
14481
14482 SET_FIELD_BITPOS (*fp, 0);
14483
14484 /* Get bit size of field (zero if none). */
14485 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14486 if (attr != nullptr)
14487 {
14488 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14489 }
14490 else
14491 {
14492 FIELD_BITSIZE (*fp) = 0;
14493 }
14494
14495 /* Get bit offset of field. */
14496 handle_data_member_location (die, cu, fp);
14497 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14498 if (attr != nullptr)
14499 {
14500 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14501 {
14502 /* For big endian bits, the DW_AT_bit_offset gives the
14503 additional bit offset from the MSB of the containing
14504 anonymous object to the MSB of the field. We don't
14505 have to do anything special since we don't need to
14506 know the size of the anonymous object. */
14507 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14508 }
14509 else
14510 {
14511 /* For little endian bits, compute the bit offset to the
14512 MSB of the anonymous object, subtract off the number of
14513 bits from the MSB of the field to the MSB of the
14514 object, and then subtract off the number of bits of
14515 the field itself. The result is the bit offset of
14516 the LSB of the field. */
14517 int anonymous_size;
14518 int bit_offset = DW_UNSND (attr);
14519
14520 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14521 if (attr != nullptr)
14522 {
14523 /* The size of the anonymous object containing
14524 the bit field is explicit, so use the
14525 indicated size (in bytes). */
14526 anonymous_size = DW_UNSND (attr);
14527 }
14528 else
14529 {
14530 /* The size of the anonymous object containing
14531 the bit field must be inferred from the type
14532 attribute of the data member containing the
14533 bit field. */
14534 anonymous_size = TYPE_LENGTH (fp->type);
14535 }
14536 SET_FIELD_BITPOS (*fp,
14537 (FIELD_BITPOS (*fp)
14538 + anonymous_size * bits_per_byte
14539 - bit_offset - FIELD_BITSIZE (*fp)));
14540 }
14541 }
14542 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14543 if (attr != NULL)
14544 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14545 + attr->constant_value (0)));
14546
14547 /* Get name of field. */
14548 fieldname = dwarf2_name (die, cu);
14549 if (fieldname == NULL)
14550 fieldname = "";
14551
14552 /* The name is already allocated along with this objfile, so we don't
14553 need to duplicate it for the type. */
14554 fp->name = fieldname;
14555
14556 /* Change accessibility for artificial fields (e.g. virtual table
14557 pointer or virtual base class pointer) to private. */
14558 if (dwarf2_attr (die, DW_AT_artificial, cu))
14559 {
14560 FIELD_ARTIFICIAL (*fp) = 1;
14561 new_field->accessibility = DW_ACCESS_private;
14562 fip->non_public_fields = 1;
14563 }
14564 }
14565 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14566 {
14567 /* C++ static member. */
14568
14569 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14570 is a declaration, but all versions of G++ as of this writing
14571 (so through at least 3.2.1) incorrectly generate
14572 DW_TAG_variable tags. */
14573
14574 const char *physname;
14575
14576 /* Get name of field. */
14577 fieldname = dwarf2_name (die, cu);
14578 if (fieldname == NULL)
14579 return;
14580
14581 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14582 if (attr
14583 /* Only create a symbol if this is an external value.
14584 new_symbol checks this and puts the value in the global symbol
14585 table, which we want. If it is not external, new_symbol
14586 will try to put the value in cu->list_in_scope which is wrong. */
14587 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14588 {
14589 /* A static const member, not much different than an enum as far as
14590 we're concerned, except that we can support more types. */
14591 new_symbol (die, NULL, cu);
14592 }
14593
14594 /* Get physical name. */
14595 physname = dwarf2_physname (fieldname, die, cu);
14596
14597 /* The name is already allocated along with this objfile, so we don't
14598 need to duplicate it for the type. */
14599 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14600 FIELD_TYPE (*fp) = die_type (die, cu);
14601 FIELD_NAME (*fp) = fieldname;
14602 }
14603 else if (die->tag == DW_TAG_inheritance)
14604 {
14605 /* C++ base class field. */
14606 handle_data_member_location (die, cu, fp);
14607 FIELD_BITSIZE (*fp) = 0;
14608 FIELD_TYPE (*fp) = die_type (die, cu);
14609 FIELD_NAME (*fp) = fp->type->name ();
14610 }
14611 else
14612 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14613 }
14614
14615 /* Can the type given by DIE define another type? */
14616
14617 static bool
14618 type_can_define_types (const struct die_info *die)
14619 {
14620 switch (die->tag)
14621 {
14622 case DW_TAG_typedef:
14623 case DW_TAG_class_type:
14624 case DW_TAG_structure_type:
14625 case DW_TAG_union_type:
14626 case DW_TAG_enumeration_type:
14627 return true;
14628
14629 default:
14630 return false;
14631 }
14632 }
14633
14634 /* Add a type definition defined in the scope of the FIP's class. */
14635
14636 static void
14637 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14638 struct dwarf2_cu *cu)
14639 {
14640 struct decl_field fp;
14641 memset (&fp, 0, sizeof (fp));
14642
14643 gdb_assert (type_can_define_types (die));
14644
14645 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14646 fp.name = dwarf2_name (die, cu);
14647 fp.type = read_type_die (die, cu);
14648
14649 /* Save accessibility. */
14650 enum dwarf_access_attribute accessibility;
14651 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14652 if (attr != NULL)
14653 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14654 else
14655 accessibility = dwarf2_default_access_attribute (die, cu);
14656 switch (accessibility)
14657 {
14658 case DW_ACCESS_public:
14659 /* The assumed value if neither private nor protected. */
14660 break;
14661 case DW_ACCESS_private:
14662 fp.is_private = 1;
14663 break;
14664 case DW_ACCESS_protected:
14665 fp.is_protected = 1;
14666 break;
14667 default:
14668 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14669 }
14670
14671 if (die->tag == DW_TAG_typedef)
14672 fip->typedef_field_list.push_back (fp);
14673 else
14674 fip->nested_types_list.push_back (fp);
14675 }
14676
14677 /* A convenience typedef that's used when finding the discriminant
14678 field for a variant part. */
14679 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14680 offset_map_type;
14681
14682 /* Compute the discriminant range for a given variant. OBSTACK is
14683 where the results will be stored. VARIANT is the variant to
14684 process. IS_UNSIGNED indicates whether the discriminant is signed
14685 or unsigned. */
14686
14687 static const gdb::array_view<discriminant_range>
14688 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14689 bool is_unsigned)
14690 {
14691 std::vector<discriminant_range> ranges;
14692
14693 if (variant.default_branch)
14694 return {};
14695
14696 if (variant.discr_list_data == nullptr)
14697 {
14698 discriminant_range r
14699 = {variant.discriminant_value, variant.discriminant_value};
14700 ranges.push_back (r);
14701 }
14702 else
14703 {
14704 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14705 variant.discr_list_data->size);
14706 while (!data.empty ())
14707 {
14708 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14709 {
14710 complaint (_("invalid discriminant marker: %d"), data[0]);
14711 break;
14712 }
14713 bool is_range = data[0] == DW_DSC_range;
14714 data = data.slice (1);
14715
14716 ULONGEST low, high;
14717 unsigned int bytes_read;
14718
14719 if (data.empty ())
14720 {
14721 complaint (_("DW_AT_discr_list missing low value"));
14722 break;
14723 }
14724 if (is_unsigned)
14725 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14726 else
14727 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14728 &bytes_read);
14729 data = data.slice (bytes_read);
14730
14731 if (is_range)
14732 {
14733 if (data.empty ())
14734 {
14735 complaint (_("DW_AT_discr_list missing high value"));
14736 break;
14737 }
14738 if (is_unsigned)
14739 high = read_unsigned_leb128 (nullptr, data.data (),
14740 &bytes_read);
14741 else
14742 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14743 &bytes_read);
14744 data = data.slice (bytes_read);
14745 }
14746 else
14747 high = low;
14748
14749 ranges.push_back ({ low, high });
14750 }
14751 }
14752
14753 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14754 ranges.size ());
14755 std::copy (ranges.begin (), ranges.end (), result);
14756 return gdb::array_view<discriminant_range> (result, ranges.size ());
14757 }
14758
14759 static const gdb::array_view<variant_part> create_variant_parts
14760 (struct obstack *obstack,
14761 const offset_map_type &offset_map,
14762 struct field_info *fi,
14763 const std::vector<variant_part_builder> &variant_parts);
14764
14765 /* Fill in a "struct variant" for a given variant field. RESULT is
14766 the variant to fill in. OBSTACK is where any needed allocations
14767 will be done. OFFSET_MAP holds the mapping from section offsets to
14768 fields for the type. FI describes the fields of the type we're
14769 processing. FIELD is the variant field we're converting. */
14770
14771 static void
14772 create_one_variant (variant &result, struct obstack *obstack,
14773 const offset_map_type &offset_map,
14774 struct field_info *fi, const variant_field &field)
14775 {
14776 result.discriminants = convert_variant_range (obstack, field, false);
14777 result.first_field = field.first_field + fi->baseclasses.size ();
14778 result.last_field = field.last_field + fi->baseclasses.size ();
14779 result.parts = create_variant_parts (obstack, offset_map, fi,
14780 field.variant_parts);
14781 }
14782
14783 /* Fill in a "struct variant_part" for a given variant part. RESULT
14784 is the variant part to fill in. OBSTACK is where any needed
14785 allocations will be done. OFFSET_MAP holds the mapping from
14786 section offsets to fields for the type. FI describes the fields of
14787 the type we're processing. BUILDER is the variant part to be
14788 converted. */
14789
14790 static void
14791 create_one_variant_part (variant_part &result,
14792 struct obstack *obstack,
14793 const offset_map_type &offset_map,
14794 struct field_info *fi,
14795 const variant_part_builder &builder)
14796 {
14797 auto iter = offset_map.find (builder.discriminant_offset);
14798 if (iter == offset_map.end ())
14799 {
14800 result.discriminant_index = -1;
14801 /* Doesn't matter. */
14802 result.is_unsigned = false;
14803 }
14804 else
14805 {
14806 result.discriminant_index = iter->second;
14807 result.is_unsigned
14808 = TYPE_UNSIGNED (FIELD_TYPE
14809 (fi->fields[result.discriminant_index].field));
14810 }
14811
14812 size_t n = builder.variants.size ();
14813 variant *output = new (obstack) variant[n];
14814 for (size_t i = 0; i < n; ++i)
14815 create_one_variant (output[i], obstack, offset_map, fi,
14816 builder.variants[i]);
14817
14818 result.variants = gdb::array_view<variant> (output, n);
14819 }
14820
14821 /* Create a vector of variant parts that can be attached to a type.
14822 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14823 holds the mapping from section offsets to fields for the type. FI
14824 describes the fields of the type we're processing. VARIANT_PARTS
14825 is the vector to convert. */
14826
14827 static const gdb::array_view<variant_part>
14828 create_variant_parts (struct obstack *obstack,
14829 const offset_map_type &offset_map,
14830 struct field_info *fi,
14831 const std::vector<variant_part_builder> &variant_parts)
14832 {
14833 if (variant_parts.empty ())
14834 return {};
14835
14836 size_t n = variant_parts.size ();
14837 variant_part *result = new (obstack) variant_part[n];
14838 for (size_t i = 0; i < n; ++i)
14839 create_one_variant_part (result[i], obstack, offset_map, fi,
14840 variant_parts[i]);
14841
14842 return gdb::array_view<variant_part> (result, n);
14843 }
14844
14845 /* Compute the variant part vector for FIP, attaching it to TYPE when
14846 done. */
14847
14848 static void
14849 add_variant_property (struct field_info *fip, struct type *type,
14850 struct dwarf2_cu *cu)
14851 {
14852 /* Map section offsets of fields to their field index. Note the
14853 field index here does not take the number of baseclasses into
14854 account. */
14855 offset_map_type offset_map;
14856 for (int i = 0; i < fip->fields.size (); ++i)
14857 offset_map[fip->fields[i].offset] = i;
14858
14859 struct objfile *objfile = cu->per_objfile->objfile;
14860 gdb::array_view<variant_part> parts
14861 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14862 fip->variant_parts);
14863
14864 struct dynamic_prop prop;
14865 prop.kind = PROP_VARIANT_PARTS;
14866 prop.data.variant_parts
14867 = ((gdb::array_view<variant_part> *)
14868 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14869
14870 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14871 }
14872
14873 /* Create the vector of fields, and attach it to the type. */
14874
14875 static void
14876 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14877 struct dwarf2_cu *cu)
14878 {
14879 int nfields = fip->nfields ();
14880
14881 /* Record the field count, allocate space for the array of fields,
14882 and create blank accessibility bitfields if necessary. */
14883 type->set_num_fields (nfields);
14884 type->set_fields
14885 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14886
14887 if (fip->non_public_fields && cu->language != language_ada)
14888 {
14889 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14890
14891 TYPE_FIELD_PRIVATE_BITS (type) =
14892 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14893 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14894
14895 TYPE_FIELD_PROTECTED_BITS (type) =
14896 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14897 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14898
14899 TYPE_FIELD_IGNORE_BITS (type) =
14900 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14901 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14902 }
14903
14904 /* If the type has baseclasses, allocate and clear a bit vector for
14905 TYPE_FIELD_VIRTUAL_BITS. */
14906 if (!fip->baseclasses.empty () && cu->language != language_ada)
14907 {
14908 int num_bytes = B_BYTES (fip->baseclasses.size ());
14909 unsigned char *pointer;
14910
14911 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14912 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14913 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14914 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14915 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14916 }
14917
14918 if (!fip->variant_parts.empty ())
14919 add_variant_property (fip, type, cu);
14920
14921 /* Copy the saved-up fields into the field vector. */
14922 for (int i = 0; i < nfields; ++i)
14923 {
14924 struct nextfield &field
14925 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14926 : fip->fields[i - fip->baseclasses.size ()]);
14927
14928 type->field (i) = field.field;
14929 switch (field.accessibility)
14930 {
14931 case DW_ACCESS_private:
14932 if (cu->language != language_ada)
14933 SET_TYPE_FIELD_PRIVATE (type, i);
14934 break;
14935
14936 case DW_ACCESS_protected:
14937 if (cu->language != language_ada)
14938 SET_TYPE_FIELD_PROTECTED (type, i);
14939 break;
14940
14941 case DW_ACCESS_public:
14942 break;
14943
14944 default:
14945 /* Unknown accessibility. Complain and treat it as public. */
14946 {
14947 complaint (_("unsupported accessibility %d"),
14948 field.accessibility);
14949 }
14950 break;
14951 }
14952 if (i < fip->baseclasses.size ())
14953 {
14954 switch (field.virtuality)
14955 {
14956 case DW_VIRTUALITY_virtual:
14957 case DW_VIRTUALITY_pure_virtual:
14958 if (cu->language == language_ada)
14959 error (_("unexpected virtuality in component of Ada type"));
14960 SET_TYPE_FIELD_VIRTUAL (type, i);
14961 break;
14962 }
14963 }
14964 }
14965 }
14966
14967 /* Return true if this member function is a constructor, false
14968 otherwise. */
14969
14970 static int
14971 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14972 {
14973 const char *fieldname;
14974 const char *type_name;
14975 int len;
14976
14977 if (die->parent == NULL)
14978 return 0;
14979
14980 if (die->parent->tag != DW_TAG_structure_type
14981 && die->parent->tag != DW_TAG_union_type
14982 && die->parent->tag != DW_TAG_class_type)
14983 return 0;
14984
14985 fieldname = dwarf2_name (die, cu);
14986 type_name = dwarf2_name (die->parent, cu);
14987 if (fieldname == NULL || type_name == NULL)
14988 return 0;
14989
14990 len = strlen (fieldname);
14991 return (strncmp (fieldname, type_name, len) == 0
14992 && (type_name[len] == '\0' || type_name[len] == '<'));
14993 }
14994
14995 /* Check if the given VALUE is a recognized enum
14996 dwarf_defaulted_attribute constant according to DWARF5 spec,
14997 Table 7.24. */
14998
14999 static bool
15000 is_valid_DW_AT_defaulted (ULONGEST value)
15001 {
15002 switch (value)
15003 {
15004 case DW_DEFAULTED_no:
15005 case DW_DEFAULTED_in_class:
15006 case DW_DEFAULTED_out_of_class:
15007 return true;
15008 }
15009
15010 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15011 return false;
15012 }
15013
15014 /* Add a member function to the proper fieldlist. */
15015
15016 static void
15017 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15018 struct type *type, struct dwarf2_cu *cu)
15019 {
15020 struct objfile *objfile = cu->per_objfile->objfile;
15021 struct attribute *attr;
15022 int i;
15023 struct fnfieldlist *flp = nullptr;
15024 struct fn_field *fnp;
15025 const char *fieldname;
15026 struct type *this_type;
15027 enum dwarf_access_attribute accessibility;
15028
15029 if (cu->language == language_ada)
15030 error (_("unexpected member function in Ada type"));
15031
15032 /* Get name of member function. */
15033 fieldname = dwarf2_name (die, cu);
15034 if (fieldname == NULL)
15035 return;
15036
15037 /* Look up member function name in fieldlist. */
15038 for (i = 0; i < fip->fnfieldlists.size (); i++)
15039 {
15040 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15041 {
15042 flp = &fip->fnfieldlists[i];
15043 break;
15044 }
15045 }
15046
15047 /* Create a new fnfieldlist if necessary. */
15048 if (flp == nullptr)
15049 {
15050 fip->fnfieldlists.emplace_back ();
15051 flp = &fip->fnfieldlists.back ();
15052 flp->name = fieldname;
15053 i = fip->fnfieldlists.size () - 1;
15054 }
15055
15056 /* Create a new member function field and add it to the vector of
15057 fnfieldlists. */
15058 flp->fnfields.emplace_back ();
15059 fnp = &flp->fnfields.back ();
15060
15061 /* Delay processing of the physname until later. */
15062 if (cu->language == language_cplus)
15063 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15064 die, cu);
15065 else
15066 {
15067 const char *physname = dwarf2_physname (fieldname, die, cu);
15068 fnp->physname = physname ? physname : "";
15069 }
15070
15071 fnp->type = alloc_type (objfile);
15072 this_type = read_type_die (die, cu);
15073 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15074 {
15075 int nparams = this_type->num_fields ();
15076
15077 /* TYPE is the domain of this method, and THIS_TYPE is the type
15078 of the method itself (TYPE_CODE_METHOD). */
15079 smash_to_method_type (fnp->type, type,
15080 TYPE_TARGET_TYPE (this_type),
15081 this_type->fields (),
15082 this_type->num_fields (),
15083 TYPE_VARARGS (this_type));
15084
15085 /* Handle static member functions.
15086 Dwarf2 has no clean way to discern C++ static and non-static
15087 member functions. G++ helps GDB by marking the first
15088 parameter for non-static member functions (which is the this
15089 pointer) as artificial. We obtain this information from
15090 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15091 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15092 fnp->voffset = VOFFSET_STATIC;
15093 }
15094 else
15095 complaint (_("member function type missing for '%s'"),
15096 dwarf2_full_name (fieldname, die, cu));
15097
15098 /* Get fcontext from DW_AT_containing_type if present. */
15099 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15100 fnp->fcontext = die_containing_type (die, cu);
15101
15102 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15103 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15104
15105 /* Get accessibility. */
15106 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15107 if (attr != nullptr)
15108 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15109 else
15110 accessibility = dwarf2_default_access_attribute (die, cu);
15111 switch (accessibility)
15112 {
15113 case DW_ACCESS_private:
15114 fnp->is_private = 1;
15115 break;
15116 case DW_ACCESS_protected:
15117 fnp->is_protected = 1;
15118 break;
15119 }
15120
15121 /* Check for artificial methods. */
15122 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15123 if (attr && DW_UNSND (attr) != 0)
15124 fnp->is_artificial = 1;
15125
15126 /* Check for defaulted methods. */
15127 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15128 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15129 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15130
15131 /* Check for deleted methods. */
15132 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15133 if (attr != nullptr && DW_UNSND (attr) != 0)
15134 fnp->is_deleted = 1;
15135
15136 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15137
15138 /* Get index in virtual function table if it is a virtual member
15139 function. For older versions of GCC, this is an offset in the
15140 appropriate virtual table, as specified by DW_AT_containing_type.
15141 For everyone else, it is an expression to be evaluated relative
15142 to the object address. */
15143
15144 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15145 if (attr != nullptr)
15146 {
15147 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15148 {
15149 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15150 {
15151 /* Old-style GCC. */
15152 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15153 }
15154 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15155 || (DW_BLOCK (attr)->size > 1
15156 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15157 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15158 {
15159 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15160 if ((fnp->voffset % cu->header.addr_size) != 0)
15161 dwarf2_complex_location_expr_complaint ();
15162 else
15163 fnp->voffset /= cu->header.addr_size;
15164 fnp->voffset += 2;
15165 }
15166 else
15167 dwarf2_complex_location_expr_complaint ();
15168
15169 if (!fnp->fcontext)
15170 {
15171 /* If there is no `this' field and no DW_AT_containing_type,
15172 we cannot actually find a base class context for the
15173 vtable! */
15174 if (this_type->num_fields () == 0
15175 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15176 {
15177 complaint (_("cannot determine context for virtual member "
15178 "function \"%s\" (offset %s)"),
15179 fieldname, sect_offset_str (die->sect_off));
15180 }
15181 else
15182 {
15183 fnp->fcontext
15184 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15185 }
15186 }
15187 }
15188 else if (attr->form_is_section_offset ())
15189 {
15190 dwarf2_complex_location_expr_complaint ();
15191 }
15192 else
15193 {
15194 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15195 fieldname);
15196 }
15197 }
15198 else
15199 {
15200 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15201 if (attr && DW_UNSND (attr))
15202 {
15203 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15204 complaint (_("Member function \"%s\" (offset %s) is virtual "
15205 "but the vtable offset is not specified"),
15206 fieldname, sect_offset_str (die->sect_off));
15207 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15208 TYPE_CPLUS_DYNAMIC (type) = 1;
15209 }
15210 }
15211 }
15212
15213 /* Create the vector of member function fields, and attach it to the type. */
15214
15215 static void
15216 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15217 struct dwarf2_cu *cu)
15218 {
15219 if (cu->language == language_ada)
15220 error (_("unexpected member functions in Ada type"));
15221
15222 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15223 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15224 TYPE_ALLOC (type,
15225 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15226
15227 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15228 {
15229 struct fnfieldlist &nf = fip->fnfieldlists[i];
15230 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15231
15232 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15233 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15234 fn_flp->fn_fields = (struct fn_field *)
15235 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15236
15237 for (int k = 0; k < nf.fnfields.size (); ++k)
15238 fn_flp->fn_fields[k] = nf.fnfields[k];
15239 }
15240
15241 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15242 }
15243
15244 /* Returns non-zero if NAME is the name of a vtable member in CU's
15245 language, zero otherwise. */
15246 static int
15247 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15248 {
15249 static const char vptr[] = "_vptr";
15250
15251 /* Look for the C++ form of the vtable. */
15252 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15253 return 1;
15254
15255 return 0;
15256 }
15257
15258 /* GCC outputs unnamed structures that are really pointers to member
15259 functions, with the ABI-specified layout. If TYPE describes
15260 such a structure, smash it into a member function type.
15261
15262 GCC shouldn't do this; it should just output pointer to member DIEs.
15263 This is GCC PR debug/28767. */
15264
15265 static void
15266 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15267 {
15268 struct type *pfn_type, *self_type, *new_type;
15269
15270 /* Check for a structure with no name and two children. */
15271 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15272 return;
15273
15274 /* Check for __pfn and __delta members. */
15275 if (TYPE_FIELD_NAME (type, 0) == NULL
15276 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15277 || TYPE_FIELD_NAME (type, 1) == NULL
15278 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15279 return;
15280
15281 /* Find the type of the method. */
15282 pfn_type = TYPE_FIELD_TYPE (type, 0);
15283 if (pfn_type == NULL
15284 || pfn_type->code () != TYPE_CODE_PTR
15285 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15286 return;
15287
15288 /* Look for the "this" argument. */
15289 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15290 if (pfn_type->num_fields () == 0
15291 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15292 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15293 return;
15294
15295 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15296 new_type = alloc_type (objfile);
15297 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15298 pfn_type->fields (), pfn_type->num_fields (),
15299 TYPE_VARARGS (pfn_type));
15300 smash_to_methodptr_type (type, new_type);
15301 }
15302
15303 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15304 appropriate error checking and issuing complaints if there is a
15305 problem. */
15306
15307 static ULONGEST
15308 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15309 {
15310 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15311
15312 if (attr == nullptr)
15313 return 0;
15314
15315 if (!attr->form_is_constant ())
15316 {
15317 complaint (_("DW_AT_alignment must have constant form"
15318 " - DIE at %s [in module %s]"),
15319 sect_offset_str (die->sect_off),
15320 objfile_name (cu->per_objfile->objfile));
15321 return 0;
15322 }
15323
15324 ULONGEST align;
15325 if (attr->form == DW_FORM_sdata)
15326 {
15327 LONGEST val = DW_SND (attr);
15328 if (val < 0)
15329 {
15330 complaint (_("DW_AT_alignment value must not be negative"
15331 " - DIE at %s [in module %s]"),
15332 sect_offset_str (die->sect_off),
15333 objfile_name (cu->per_objfile->objfile));
15334 return 0;
15335 }
15336 align = val;
15337 }
15338 else
15339 align = DW_UNSND (attr);
15340
15341 if (align == 0)
15342 {
15343 complaint (_("DW_AT_alignment value must not be zero"
15344 " - DIE at %s [in module %s]"),
15345 sect_offset_str (die->sect_off),
15346 objfile_name (cu->per_objfile->objfile));
15347 return 0;
15348 }
15349 if ((align & (align - 1)) != 0)
15350 {
15351 complaint (_("DW_AT_alignment value must be a power of 2"
15352 " - DIE at %s [in module %s]"),
15353 sect_offset_str (die->sect_off),
15354 objfile_name (cu->per_objfile->objfile));
15355 return 0;
15356 }
15357
15358 return align;
15359 }
15360
15361 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15362 the alignment for TYPE. */
15363
15364 static void
15365 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15366 struct type *type)
15367 {
15368 if (!set_type_align (type, get_alignment (cu, die)))
15369 complaint (_("DW_AT_alignment value too large"
15370 " - DIE at %s [in module %s]"),
15371 sect_offset_str (die->sect_off),
15372 objfile_name (cu->per_objfile->objfile));
15373 }
15374
15375 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15376 constant for a type, according to DWARF5 spec, Table 5.5. */
15377
15378 static bool
15379 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15380 {
15381 switch (value)
15382 {
15383 case DW_CC_normal:
15384 case DW_CC_pass_by_reference:
15385 case DW_CC_pass_by_value:
15386 return true;
15387
15388 default:
15389 complaint (_("unrecognized DW_AT_calling_convention value "
15390 "(%s) for a type"), pulongest (value));
15391 return false;
15392 }
15393 }
15394
15395 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15396 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15397 also according to GNU-specific values (see include/dwarf2.h). */
15398
15399 static bool
15400 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15401 {
15402 switch (value)
15403 {
15404 case DW_CC_normal:
15405 case DW_CC_program:
15406 case DW_CC_nocall:
15407 return true;
15408
15409 case DW_CC_GNU_renesas_sh:
15410 case DW_CC_GNU_borland_fastcall_i386:
15411 case DW_CC_GDB_IBM_OpenCL:
15412 return true;
15413
15414 default:
15415 complaint (_("unrecognized DW_AT_calling_convention value "
15416 "(%s) for a subroutine"), pulongest (value));
15417 return false;
15418 }
15419 }
15420
15421 /* Called when we find the DIE that starts a structure or union scope
15422 (definition) to create a type for the structure or union. Fill in
15423 the type's name and general properties; the members will not be
15424 processed until process_structure_scope. A symbol table entry for
15425 the type will also not be done until process_structure_scope (assuming
15426 the type has a name).
15427
15428 NOTE: we need to call these functions regardless of whether or not the
15429 DIE has a DW_AT_name attribute, since it might be an anonymous
15430 structure or union. This gets the type entered into our set of
15431 user defined types. */
15432
15433 static struct type *
15434 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15435 {
15436 struct objfile *objfile = cu->per_objfile->objfile;
15437 struct type *type;
15438 struct attribute *attr;
15439 const char *name;
15440
15441 /* If the definition of this type lives in .debug_types, read that type.
15442 Don't follow DW_AT_specification though, that will take us back up
15443 the chain and we want to go down. */
15444 attr = die->attr (DW_AT_signature);
15445 if (attr != nullptr)
15446 {
15447 type = get_DW_AT_signature_type (die, attr, cu);
15448
15449 /* The type's CU may not be the same as CU.
15450 Ensure TYPE is recorded with CU in die_type_hash. */
15451 return set_die_type (die, type, cu);
15452 }
15453
15454 type = alloc_type (objfile);
15455 INIT_CPLUS_SPECIFIC (type);
15456
15457 name = dwarf2_name (die, cu);
15458 if (name != NULL)
15459 {
15460 if (cu->language == language_cplus
15461 || cu->language == language_d
15462 || cu->language == language_rust)
15463 {
15464 const char *full_name = dwarf2_full_name (name, die, cu);
15465
15466 /* dwarf2_full_name might have already finished building the DIE's
15467 type. If so, there is no need to continue. */
15468 if (get_die_type (die, cu) != NULL)
15469 return get_die_type (die, cu);
15470
15471 type->set_name (full_name);
15472 }
15473 else
15474 {
15475 /* The name is already allocated along with this objfile, so
15476 we don't need to duplicate it for the type. */
15477 type->set_name (name);
15478 }
15479 }
15480
15481 if (die->tag == DW_TAG_structure_type)
15482 {
15483 type->set_code (TYPE_CODE_STRUCT);
15484 }
15485 else if (die->tag == DW_TAG_union_type)
15486 {
15487 type->set_code (TYPE_CODE_UNION);
15488 }
15489 else
15490 {
15491 type->set_code (TYPE_CODE_STRUCT);
15492 }
15493
15494 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15495 TYPE_DECLARED_CLASS (type) = 1;
15496
15497 /* Store the calling convention in the type if it's available in
15498 the die. Otherwise the calling convention remains set to
15499 the default value DW_CC_normal. */
15500 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15501 if (attr != nullptr
15502 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15503 {
15504 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15505 TYPE_CPLUS_CALLING_CONVENTION (type)
15506 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15507 }
15508
15509 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15510 if (attr != nullptr)
15511 {
15512 if (attr->form_is_constant ())
15513 TYPE_LENGTH (type) = DW_UNSND (attr);
15514 else
15515 {
15516 struct dynamic_prop prop;
15517 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15518 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15519 TYPE_LENGTH (type) = 0;
15520 }
15521 }
15522 else
15523 {
15524 TYPE_LENGTH (type) = 0;
15525 }
15526
15527 maybe_set_alignment (cu, die, type);
15528
15529 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15530 {
15531 /* ICC<14 does not output the required DW_AT_declaration on
15532 incomplete types, but gives them a size of zero. */
15533 TYPE_STUB (type) = 1;
15534 }
15535 else
15536 TYPE_STUB_SUPPORTED (type) = 1;
15537
15538 if (die_is_declaration (die, cu))
15539 TYPE_STUB (type) = 1;
15540 else if (attr == NULL && die->child == NULL
15541 && producer_is_realview (cu->producer))
15542 /* RealView does not output the required DW_AT_declaration
15543 on incomplete types. */
15544 TYPE_STUB (type) = 1;
15545
15546 /* We need to add the type field to the die immediately so we don't
15547 infinitely recurse when dealing with pointers to the structure
15548 type within the structure itself. */
15549 set_die_type (die, type, cu);
15550
15551 /* set_die_type should be already done. */
15552 set_descriptive_type (type, die, cu);
15553
15554 return type;
15555 }
15556
15557 static void handle_struct_member_die
15558 (struct die_info *child_die,
15559 struct type *type,
15560 struct field_info *fi,
15561 std::vector<struct symbol *> *template_args,
15562 struct dwarf2_cu *cu);
15563
15564 /* A helper for handle_struct_member_die that handles
15565 DW_TAG_variant_part. */
15566
15567 static void
15568 handle_variant_part (struct die_info *die, struct type *type,
15569 struct field_info *fi,
15570 std::vector<struct symbol *> *template_args,
15571 struct dwarf2_cu *cu)
15572 {
15573 variant_part_builder *new_part;
15574 if (fi->current_variant_part == nullptr)
15575 {
15576 fi->variant_parts.emplace_back ();
15577 new_part = &fi->variant_parts.back ();
15578 }
15579 else if (!fi->current_variant_part->processing_variant)
15580 {
15581 complaint (_("nested DW_TAG_variant_part seen "
15582 "- DIE at %s [in module %s]"),
15583 sect_offset_str (die->sect_off),
15584 objfile_name (cu->per_objfile->objfile));
15585 return;
15586 }
15587 else
15588 {
15589 variant_field &current = fi->current_variant_part->variants.back ();
15590 current.variant_parts.emplace_back ();
15591 new_part = &current.variant_parts.back ();
15592 }
15593
15594 /* When we recurse, we want callees to add to this new variant
15595 part. */
15596 scoped_restore save_current_variant_part
15597 = make_scoped_restore (&fi->current_variant_part, new_part);
15598
15599 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15600 if (discr == NULL)
15601 {
15602 /* It's a univariant form, an extension we support. */
15603 }
15604 else if (discr->form_is_ref ())
15605 {
15606 struct dwarf2_cu *target_cu = cu;
15607 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15608
15609 new_part->discriminant_offset = target_die->sect_off;
15610 }
15611 else
15612 {
15613 complaint (_("DW_AT_discr does not have DIE reference form"
15614 " - DIE at %s [in module %s]"),
15615 sect_offset_str (die->sect_off),
15616 objfile_name (cu->per_objfile->objfile));
15617 }
15618
15619 for (die_info *child_die = die->child;
15620 child_die != NULL;
15621 child_die = child_die->sibling)
15622 handle_struct_member_die (child_die, type, fi, template_args, cu);
15623 }
15624
15625 /* A helper for handle_struct_member_die that handles
15626 DW_TAG_variant. */
15627
15628 static void
15629 handle_variant (struct die_info *die, struct type *type,
15630 struct field_info *fi,
15631 std::vector<struct symbol *> *template_args,
15632 struct dwarf2_cu *cu)
15633 {
15634 if (fi->current_variant_part == nullptr)
15635 {
15636 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15637 "- DIE at %s [in module %s]"),
15638 sect_offset_str (die->sect_off),
15639 objfile_name (cu->per_objfile->objfile));
15640 return;
15641 }
15642 if (fi->current_variant_part->processing_variant)
15643 {
15644 complaint (_("nested DW_TAG_variant seen "
15645 "- DIE at %s [in module %s]"),
15646 sect_offset_str (die->sect_off),
15647 objfile_name (cu->per_objfile->objfile));
15648 return;
15649 }
15650
15651 scoped_restore save_processing_variant
15652 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15653 true);
15654
15655 fi->current_variant_part->variants.emplace_back ();
15656 variant_field &variant = fi->current_variant_part->variants.back ();
15657 variant.first_field = fi->fields.size ();
15658
15659 /* In a variant we want to get the discriminant and also add a
15660 field for our sole member child. */
15661 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15662 if (discr == nullptr)
15663 {
15664 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15665 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15666 variant.default_branch = true;
15667 else
15668 variant.discr_list_data = DW_BLOCK (discr);
15669 }
15670 else
15671 variant.discriminant_value = DW_UNSND (discr);
15672
15673 for (die_info *variant_child = die->child;
15674 variant_child != NULL;
15675 variant_child = variant_child->sibling)
15676 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15677
15678 variant.last_field = fi->fields.size ();
15679 }
15680
15681 /* A helper for process_structure_scope that handles a single member
15682 DIE. */
15683
15684 static void
15685 handle_struct_member_die (struct die_info *child_die, struct type *type,
15686 struct field_info *fi,
15687 std::vector<struct symbol *> *template_args,
15688 struct dwarf2_cu *cu)
15689 {
15690 if (child_die->tag == DW_TAG_member
15691 || child_die->tag == DW_TAG_variable)
15692 {
15693 /* NOTE: carlton/2002-11-05: A C++ static data member
15694 should be a DW_TAG_member that is a declaration, but
15695 all versions of G++ as of this writing (so through at
15696 least 3.2.1) incorrectly generate DW_TAG_variable
15697 tags for them instead. */
15698 dwarf2_add_field (fi, child_die, cu);
15699 }
15700 else if (child_die->tag == DW_TAG_subprogram)
15701 {
15702 /* Rust doesn't have member functions in the C++ sense.
15703 However, it does emit ordinary functions as children
15704 of a struct DIE. */
15705 if (cu->language == language_rust)
15706 read_func_scope (child_die, cu);
15707 else
15708 {
15709 /* C++ member function. */
15710 dwarf2_add_member_fn (fi, child_die, type, cu);
15711 }
15712 }
15713 else if (child_die->tag == DW_TAG_inheritance)
15714 {
15715 /* C++ base class field. */
15716 dwarf2_add_field (fi, child_die, cu);
15717 }
15718 else if (type_can_define_types (child_die))
15719 dwarf2_add_type_defn (fi, child_die, cu);
15720 else if (child_die->tag == DW_TAG_template_type_param
15721 || child_die->tag == DW_TAG_template_value_param)
15722 {
15723 struct symbol *arg = new_symbol (child_die, NULL, cu);
15724
15725 if (arg != NULL)
15726 template_args->push_back (arg);
15727 }
15728 else if (child_die->tag == DW_TAG_variant_part)
15729 handle_variant_part (child_die, type, fi, template_args, cu);
15730 else if (child_die->tag == DW_TAG_variant)
15731 handle_variant (child_die, type, fi, template_args, cu);
15732 }
15733
15734 /* Finish creating a structure or union type, including filling in
15735 its members and creating a symbol for it. */
15736
15737 static void
15738 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15739 {
15740 struct objfile *objfile = cu->per_objfile->objfile;
15741 struct die_info *child_die;
15742 struct type *type;
15743
15744 type = get_die_type (die, cu);
15745 if (type == NULL)
15746 type = read_structure_type (die, cu);
15747
15748 bool has_template_parameters = false;
15749 if (die->child != NULL && ! die_is_declaration (die, cu))
15750 {
15751 struct field_info fi;
15752 std::vector<struct symbol *> template_args;
15753
15754 child_die = die->child;
15755
15756 while (child_die && child_die->tag)
15757 {
15758 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15759 child_die = child_die->sibling;
15760 }
15761
15762 /* Attach template arguments to type. */
15763 if (!template_args.empty ())
15764 {
15765 has_template_parameters = true;
15766 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15767 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15768 TYPE_TEMPLATE_ARGUMENTS (type)
15769 = XOBNEWVEC (&objfile->objfile_obstack,
15770 struct symbol *,
15771 TYPE_N_TEMPLATE_ARGUMENTS (type));
15772 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15773 template_args.data (),
15774 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15775 * sizeof (struct symbol *)));
15776 }
15777
15778 /* Attach fields and member functions to the type. */
15779 if (fi.nfields () > 0)
15780 dwarf2_attach_fields_to_type (&fi, type, cu);
15781 if (!fi.fnfieldlists.empty ())
15782 {
15783 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15784
15785 /* Get the type which refers to the base class (possibly this
15786 class itself) which contains the vtable pointer for the current
15787 class from the DW_AT_containing_type attribute. This use of
15788 DW_AT_containing_type is a GNU extension. */
15789
15790 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15791 {
15792 struct type *t = die_containing_type (die, cu);
15793
15794 set_type_vptr_basetype (type, t);
15795 if (type == t)
15796 {
15797 int i;
15798
15799 /* Our own class provides vtbl ptr. */
15800 for (i = t->num_fields () - 1;
15801 i >= TYPE_N_BASECLASSES (t);
15802 --i)
15803 {
15804 const char *fieldname = TYPE_FIELD_NAME (t, i);
15805
15806 if (is_vtable_name (fieldname, cu))
15807 {
15808 set_type_vptr_fieldno (type, i);
15809 break;
15810 }
15811 }
15812
15813 /* Complain if virtual function table field not found. */
15814 if (i < TYPE_N_BASECLASSES (t))
15815 complaint (_("virtual function table pointer "
15816 "not found when defining class '%s'"),
15817 type->name () ? type->name () : "");
15818 }
15819 else
15820 {
15821 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15822 }
15823 }
15824 else if (cu->producer
15825 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15826 {
15827 /* The IBM XLC compiler does not provide direct indication
15828 of the containing type, but the vtable pointer is
15829 always named __vfp. */
15830
15831 int i;
15832
15833 for (i = type->num_fields () - 1;
15834 i >= TYPE_N_BASECLASSES (type);
15835 --i)
15836 {
15837 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15838 {
15839 set_type_vptr_fieldno (type, i);
15840 set_type_vptr_basetype (type, type);
15841 break;
15842 }
15843 }
15844 }
15845 }
15846
15847 /* Copy fi.typedef_field_list linked list elements content into the
15848 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15849 if (!fi.typedef_field_list.empty ())
15850 {
15851 int count = fi.typedef_field_list.size ();
15852
15853 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15854 TYPE_TYPEDEF_FIELD_ARRAY (type)
15855 = ((struct decl_field *)
15856 TYPE_ALLOC (type,
15857 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15858 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15859
15860 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15861 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15862 }
15863
15864 /* Copy fi.nested_types_list linked list elements content into the
15865 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15866 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15867 {
15868 int count = fi.nested_types_list.size ();
15869
15870 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15871 TYPE_NESTED_TYPES_ARRAY (type)
15872 = ((struct decl_field *)
15873 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15874 TYPE_NESTED_TYPES_COUNT (type) = count;
15875
15876 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15877 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15878 }
15879 }
15880
15881 quirk_gcc_member_function_pointer (type, objfile);
15882 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15883 cu->rust_unions.push_back (type);
15884
15885 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15886 snapshots) has been known to create a die giving a declaration
15887 for a class that has, as a child, a die giving a definition for a
15888 nested class. So we have to process our children even if the
15889 current die is a declaration. Normally, of course, a declaration
15890 won't have any children at all. */
15891
15892 child_die = die->child;
15893
15894 while (child_die != NULL && child_die->tag)
15895 {
15896 if (child_die->tag == DW_TAG_member
15897 || child_die->tag == DW_TAG_variable
15898 || child_die->tag == DW_TAG_inheritance
15899 || child_die->tag == DW_TAG_template_value_param
15900 || child_die->tag == DW_TAG_template_type_param)
15901 {
15902 /* Do nothing. */
15903 }
15904 else
15905 process_die (child_die, cu);
15906
15907 child_die = child_die->sibling;
15908 }
15909
15910 /* Do not consider external references. According to the DWARF standard,
15911 these DIEs are identified by the fact that they have no byte_size
15912 attribute, and a declaration attribute. */
15913 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15914 || !die_is_declaration (die, cu)
15915 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15916 {
15917 struct symbol *sym = new_symbol (die, type, cu);
15918
15919 if (has_template_parameters)
15920 {
15921 struct symtab *symtab;
15922 if (sym != nullptr)
15923 symtab = symbol_symtab (sym);
15924 else if (cu->line_header != nullptr)
15925 {
15926 /* Any related symtab will do. */
15927 symtab
15928 = cu->line_header->file_names ()[0].symtab;
15929 }
15930 else
15931 {
15932 symtab = nullptr;
15933 complaint (_("could not find suitable "
15934 "symtab for template parameter"
15935 " - DIE at %s [in module %s]"),
15936 sect_offset_str (die->sect_off),
15937 objfile_name (objfile));
15938 }
15939
15940 if (symtab != nullptr)
15941 {
15942 /* Make sure that the symtab is set on the new symbols.
15943 Even though they don't appear in this symtab directly,
15944 other parts of gdb assume that symbols do, and this is
15945 reasonably true. */
15946 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15947 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15948 }
15949 }
15950 }
15951 }
15952
15953 /* Assuming DIE is an enumeration type, and TYPE is its associated
15954 type, update TYPE using some information only available in DIE's
15955 children. In particular, the fields are computed. */
15956
15957 static void
15958 update_enumeration_type_from_children (struct die_info *die,
15959 struct type *type,
15960 struct dwarf2_cu *cu)
15961 {
15962 struct die_info *child_die;
15963 int unsigned_enum = 1;
15964 int flag_enum = 1;
15965
15966 auto_obstack obstack;
15967 std::vector<struct field> fields;
15968
15969 for (child_die = die->child;
15970 child_die != NULL && child_die->tag;
15971 child_die = child_die->sibling)
15972 {
15973 struct attribute *attr;
15974 LONGEST value;
15975 const gdb_byte *bytes;
15976 struct dwarf2_locexpr_baton *baton;
15977 const char *name;
15978
15979 if (child_die->tag != DW_TAG_enumerator)
15980 continue;
15981
15982 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15983 if (attr == NULL)
15984 continue;
15985
15986 name = dwarf2_name (child_die, cu);
15987 if (name == NULL)
15988 name = "<anonymous enumerator>";
15989
15990 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15991 &value, &bytes, &baton);
15992 if (value < 0)
15993 {
15994 unsigned_enum = 0;
15995 flag_enum = 0;
15996 }
15997 else
15998 {
15999 if (count_one_bits_ll (value) >= 2)
16000 flag_enum = 0;
16001 }
16002
16003 fields.emplace_back ();
16004 struct field &field = fields.back ();
16005 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16006 SET_FIELD_ENUMVAL (field, value);
16007 }
16008
16009 if (!fields.empty ())
16010 {
16011 type->set_num_fields (fields.size ());
16012 type->set_fields
16013 ((struct field *)
16014 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16015 memcpy (type->fields (), fields.data (),
16016 sizeof (struct field) * fields.size ());
16017 }
16018
16019 if (unsigned_enum)
16020 TYPE_UNSIGNED (type) = 1;
16021 if (flag_enum)
16022 TYPE_FLAG_ENUM (type) = 1;
16023 }
16024
16025 /* Given a DW_AT_enumeration_type die, set its type. We do not
16026 complete the type's fields yet, or create any symbols. */
16027
16028 static struct type *
16029 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16030 {
16031 struct objfile *objfile = cu->per_objfile->objfile;
16032 struct type *type;
16033 struct attribute *attr;
16034 const char *name;
16035
16036 /* If the definition of this type lives in .debug_types, read that type.
16037 Don't follow DW_AT_specification though, that will take us back up
16038 the chain and we want to go down. */
16039 attr = die->attr (DW_AT_signature);
16040 if (attr != nullptr)
16041 {
16042 type = get_DW_AT_signature_type (die, attr, cu);
16043
16044 /* The type's CU may not be the same as CU.
16045 Ensure TYPE is recorded with CU in die_type_hash. */
16046 return set_die_type (die, type, cu);
16047 }
16048
16049 type = alloc_type (objfile);
16050
16051 type->set_code (TYPE_CODE_ENUM);
16052 name = dwarf2_full_name (NULL, die, cu);
16053 if (name != NULL)
16054 type->set_name (name);
16055
16056 attr = dwarf2_attr (die, DW_AT_type, cu);
16057 if (attr != NULL)
16058 {
16059 struct type *underlying_type = die_type (die, cu);
16060
16061 TYPE_TARGET_TYPE (type) = underlying_type;
16062 }
16063
16064 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16065 if (attr != nullptr)
16066 {
16067 TYPE_LENGTH (type) = DW_UNSND (attr);
16068 }
16069 else
16070 {
16071 TYPE_LENGTH (type) = 0;
16072 }
16073
16074 maybe_set_alignment (cu, die, type);
16075
16076 /* The enumeration DIE can be incomplete. In Ada, any type can be
16077 declared as private in the package spec, and then defined only
16078 inside the package body. Such types are known as Taft Amendment
16079 Types. When another package uses such a type, an incomplete DIE
16080 may be generated by the compiler. */
16081 if (die_is_declaration (die, cu))
16082 TYPE_STUB (type) = 1;
16083
16084 /* If this type has an underlying type that is not a stub, then we
16085 may use its attributes. We always use the "unsigned" attribute
16086 in this situation, because ordinarily we guess whether the type
16087 is unsigned -- but the guess can be wrong and the underlying type
16088 can tell us the reality. However, we defer to a local size
16089 attribute if one exists, because this lets the compiler override
16090 the underlying type if needed. */
16091 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16092 {
16093 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16094 underlying_type = check_typedef (underlying_type);
16095 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16096 if (TYPE_LENGTH (type) == 0)
16097 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16098 if (TYPE_RAW_ALIGN (type) == 0
16099 && TYPE_RAW_ALIGN (underlying_type) != 0)
16100 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16101 }
16102
16103 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16104
16105 set_die_type (die, type, cu);
16106
16107 /* Finish the creation of this type by using the enum's children.
16108 Note that, as usual, this must come after set_die_type to avoid
16109 infinite recursion when trying to compute the names of the
16110 enumerators. */
16111 update_enumeration_type_from_children (die, type, cu);
16112
16113 return type;
16114 }
16115
16116 /* Given a pointer to a die which begins an enumeration, process all
16117 the dies that define the members of the enumeration, and create the
16118 symbol for the enumeration type.
16119
16120 NOTE: We reverse the order of the element list. */
16121
16122 static void
16123 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16124 {
16125 struct type *this_type;
16126
16127 this_type = get_die_type (die, cu);
16128 if (this_type == NULL)
16129 this_type = read_enumeration_type (die, cu);
16130
16131 if (die->child != NULL)
16132 {
16133 struct die_info *child_die;
16134 const char *name;
16135
16136 child_die = die->child;
16137 while (child_die && child_die->tag)
16138 {
16139 if (child_die->tag != DW_TAG_enumerator)
16140 {
16141 process_die (child_die, cu);
16142 }
16143 else
16144 {
16145 name = dwarf2_name (child_die, cu);
16146 if (name)
16147 new_symbol (child_die, this_type, cu);
16148 }
16149
16150 child_die = child_die->sibling;
16151 }
16152 }
16153
16154 /* If we are reading an enum from a .debug_types unit, and the enum
16155 is a declaration, and the enum is not the signatured type in the
16156 unit, then we do not want to add a symbol for it. Adding a
16157 symbol would in some cases obscure the true definition of the
16158 enum, giving users an incomplete type when the definition is
16159 actually available. Note that we do not want to do this for all
16160 enums which are just declarations, because C++0x allows forward
16161 enum declarations. */
16162 if (cu->per_cu->is_debug_types
16163 && die_is_declaration (die, cu))
16164 {
16165 struct signatured_type *sig_type;
16166
16167 sig_type = (struct signatured_type *) cu->per_cu;
16168 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16169 if (sig_type->type_offset_in_section != die->sect_off)
16170 return;
16171 }
16172
16173 new_symbol (die, this_type, cu);
16174 }
16175
16176 /* Extract all information from a DW_TAG_array_type DIE and put it in
16177 the DIE's type field. For now, this only handles one dimensional
16178 arrays. */
16179
16180 static struct type *
16181 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16182 {
16183 struct objfile *objfile = cu->per_objfile->objfile;
16184 struct die_info *child_die;
16185 struct type *type;
16186 struct type *element_type, *range_type, *index_type;
16187 struct attribute *attr;
16188 const char *name;
16189 struct dynamic_prop *byte_stride_prop = NULL;
16190 unsigned int bit_stride = 0;
16191
16192 element_type = die_type (die, cu);
16193
16194 /* The die_type call above may have already set the type for this DIE. */
16195 type = get_die_type (die, cu);
16196 if (type)
16197 return type;
16198
16199 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16200 if (attr != NULL)
16201 {
16202 int stride_ok;
16203 struct type *prop_type = cu->addr_sized_int_type (false);
16204
16205 byte_stride_prop
16206 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16207 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16208 prop_type);
16209 if (!stride_ok)
16210 {
16211 complaint (_("unable to read array DW_AT_byte_stride "
16212 " - DIE at %s [in module %s]"),
16213 sect_offset_str (die->sect_off),
16214 objfile_name (cu->per_objfile->objfile));
16215 /* Ignore this attribute. We will likely not be able to print
16216 arrays of this type correctly, but there is little we can do
16217 to help if we cannot read the attribute's value. */
16218 byte_stride_prop = NULL;
16219 }
16220 }
16221
16222 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16223 if (attr != NULL)
16224 bit_stride = DW_UNSND (attr);
16225
16226 /* Irix 6.2 native cc creates array types without children for
16227 arrays with unspecified length. */
16228 if (die->child == NULL)
16229 {
16230 index_type = objfile_type (objfile)->builtin_int;
16231 range_type = create_static_range_type (NULL, index_type, 0, -1);
16232 type = create_array_type_with_stride (NULL, element_type, range_type,
16233 byte_stride_prop, bit_stride);
16234 return set_die_type (die, type, cu);
16235 }
16236
16237 std::vector<struct type *> range_types;
16238 child_die = die->child;
16239 while (child_die && child_die->tag)
16240 {
16241 if (child_die->tag == DW_TAG_subrange_type)
16242 {
16243 struct type *child_type = read_type_die (child_die, cu);
16244
16245 if (child_type != NULL)
16246 {
16247 /* The range type was succesfully read. Save it for the
16248 array type creation. */
16249 range_types.push_back (child_type);
16250 }
16251 }
16252 child_die = child_die->sibling;
16253 }
16254
16255 /* Dwarf2 dimensions are output from left to right, create the
16256 necessary array types in backwards order. */
16257
16258 type = element_type;
16259
16260 if (read_array_order (die, cu) == DW_ORD_col_major)
16261 {
16262 int i = 0;
16263
16264 while (i < range_types.size ())
16265 type = create_array_type_with_stride (NULL, type, range_types[i++],
16266 byte_stride_prop, bit_stride);
16267 }
16268 else
16269 {
16270 size_t ndim = range_types.size ();
16271 while (ndim-- > 0)
16272 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16273 byte_stride_prop, bit_stride);
16274 }
16275
16276 /* Understand Dwarf2 support for vector types (like they occur on
16277 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16278 array type. This is not part of the Dwarf2/3 standard yet, but a
16279 custom vendor extension. The main difference between a regular
16280 array and the vector variant is that vectors are passed by value
16281 to functions. */
16282 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16283 if (attr != nullptr)
16284 make_vector_type (type);
16285
16286 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16287 implementation may choose to implement triple vectors using this
16288 attribute. */
16289 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16290 if (attr != nullptr)
16291 {
16292 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16293 TYPE_LENGTH (type) = DW_UNSND (attr);
16294 else
16295 complaint (_("DW_AT_byte_size for array type smaller "
16296 "than the total size of elements"));
16297 }
16298
16299 name = dwarf2_name (die, cu);
16300 if (name)
16301 type->set_name (name);
16302
16303 maybe_set_alignment (cu, die, type);
16304
16305 /* Install the type in the die. */
16306 set_die_type (die, type, cu);
16307
16308 /* set_die_type should be already done. */
16309 set_descriptive_type (type, die, cu);
16310
16311 return type;
16312 }
16313
16314 static enum dwarf_array_dim_ordering
16315 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16316 {
16317 struct attribute *attr;
16318
16319 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16320
16321 if (attr != nullptr)
16322 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16323
16324 /* GNU F77 is a special case, as at 08/2004 array type info is the
16325 opposite order to the dwarf2 specification, but data is still
16326 laid out as per normal fortran.
16327
16328 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16329 version checking. */
16330
16331 if (cu->language == language_fortran
16332 && cu->producer && strstr (cu->producer, "GNU F77"))
16333 {
16334 return DW_ORD_row_major;
16335 }
16336
16337 switch (cu->language_defn->la_array_ordering)
16338 {
16339 case array_column_major:
16340 return DW_ORD_col_major;
16341 case array_row_major:
16342 default:
16343 return DW_ORD_row_major;
16344 };
16345 }
16346
16347 /* Extract all information from a DW_TAG_set_type DIE and put it in
16348 the DIE's type field. */
16349
16350 static struct type *
16351 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16352 {
16353 struct type *domain_type, *set_type;
16354 struct attribute *attr;
16355
16356 domain_type = die_type (die, cu);
16357
16358 /* The die_type call above may have already set the type for this DIE. */
16359 set_type = get_die_type (die, cu);
16360 if (set_type)
16361 return set_type;
16362
16363 set_type = create_set_type (NULL, domain_type);
16364
16365 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16366 if (attr != nullptr)
16367 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16368
16369 maybe_set_alignment (cu, die, set_type);
16370
16371 return set_die_type (die, set_type, cu);
16372 }
16373
16374 /* A helper for read_common_block that creates a locexpr baton.
16375 SYM is the symbol which we are marking as computed.
16376 COMMON_DIE is the DIE for the common block.
16377 COMMON_LOC is the location expression attribute for the common
16378 block itself.
16379 MEMBER_LOC is the location expression attribute for the particular
16380 member of the common block that we are processing.
16381 CU is the CU from which the above come. */
16382
16383 static void
16384 mark_common_block_symbol_computed (struct symbol *sym,
16385 struct die_info *common_die,
16386 struct attribute *common_loc,
16387 struct attribute *member_loc,
16388 struct dwarf2_cu *cu)
16389 {
16390 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16391 struct objfile *objfile = per_objfile->objfile;
16392 struct dwarf2_locexpr_baton *baton;
16393 gdb_byte *ptr;
16394 unsigned int cu_off;
16395 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16396 LONGEST offset = 0;
16397
16398 gdb_assert (common_loc && member_loc);
16399 gdb_assert (common_loc->form_is_block ());
16400 gdb_assert (member_loc->form_is_block ()
16401 || member_loc->form_is_constant ());
16402
16403 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16404 baton->per_objfile = per_objfile;
16405 baton->per_cu = cu->per_cu;
16406 gdb_assert (baton->per_cu);
16407
16408 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16409
16410 if (member_loc->form_is_constant ())
16411 {
16412 offset = member_loc->constant_value (0);
16413 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16414 }
16415 else
16416 baton->size += DW_BLOCK (member_loc)->size;
16417
16418 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16419 baton->data = ptr;
16420
16421 *ptr++ = DW_OP_call4;
16422 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16423 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16424 ptr += 4;
16425
16426 if (member_loc->form_is_constant ())
16427 {
16428 *ptr++ = DW_OP_addr;
16429 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16430 ptr += cu->header.addr_size;
16431 }
16432 else
16433 {
16434 /* We have to copy the data here, because DW_OP_call4 will only
16435 use a DW_AT_location attribute. */
16436 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16437 ptr += DW_BLOCK (member_loc)->size;
16438 }
16439
16440 *ptr++ = DW_OP_plus;
16441 gdb_assert (ptr - baton->data == baton->size);
16442
16443 SYMBOL_LOCATION_BATON (sym) = baton;
16444 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16445 }
16446
16447 /* Create appropriate locally-scoped variables for all the
16448 DW_TAG_common_block entries. Also create a struct common_block
16449 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16450 is used to separate the common blocks name namespace from regular
16451 variable names. */
16452
16453 static void
16454 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16455 {
16456 struct attribute *attr;
16457
16458 attr = dwarf2_attr (die, DW_AT_location, cu);
16459 if (attr != nullptr)
16460 {
16461 /* Support the .debug_loc offsets. */
16462 if (attr->form_is_block ())
16463 {
16464 /* Ok. */
16465 }
16466 else if (attr->form_is_section_offset ())
16467 {
16468 dwarf2_complex_location_expr_complaint ();
16469 attr = NULL;
16470 }
16471 else
16472 {
16473 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16474 "common block member");
16475 attr = NULL;
16476 }
16477 }
16478
16479 if (die->child != NULL)
16480 {
16481 struct objfile *objfile = cu->per_objfile->objfile;
16482 struct die_info *child_die;
16483 size_t n_entries = 0, size;
16484 struct common_block *common_block;
16485 struct symbol *sym;
16486
16487 for (child_die = die->child;
16488 child_die && child_die->tag;
16489 child_die = child_die->sibling)
16490 ++n_entries;
16491
16492 size = (sizeof (struct common_block)
16493 + (n_entries - 1) * sizeof (struct symbol *));
16494 common_block
16495 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16496 size);
16497 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16498 common_block->n_entries = 0;
16499
16500 for (child_die = die->child;
16501 child_die && child_die->tag;
16502 child_die = child_die->sibling)
16503 {
16504 /* Create the symbol in the DW_TAG_common_block block in the current
16505 symbol scope. */
16506 sym = new_symbol (child_die, NULL, cu);
16507 if (sym != NULL)
16508 {
16509 struct attribute *member_loc;
16510
16511 common_block->contents[common_block->n_entries++] = sym;
16512
16513 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16514 cu);
16515 if (member_loc)
16516 {
16517 /* GDB has handled this for a long time, but it is
16518 not specified by DWARF. It seems to have been
16519 emitted by gfortran at least as recently as:
16520 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16521 complaint (_("Variable in common block has "
16522 "DW_AT_data_member_location "
16523 "- DIE at %s [in module %s]"),
16524 sect_offset_str (child_die->sect_off),
16525 objfile_name (objfile));
16526
16527 if (member_loc->form_is_section_offset ())
16528 dwarf2_complex_location_expr_complaint ();
16529 else if (member_loc->form_is_constant ()
16530 || member_loc->form_is_block ())
16531 {
16532 if (attr != nullptr)
16533 mark_common_block_symbol_computed (sym, die, attr,
16534 member_loc, cu);
16535 }
16536 else
16537 dwarf2_complex_location_expr_complaint ();
16538 }
16539 }
16540 }
16541
16542 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16543 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16544 }
16545 }
16546
16547 /* Create a type for a C++ namespace. */
16548
16549 static struct type *
16550 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16551 {
16552 struct objfile *objfile = cu->per_objfile->objfile;
16553 const char *previous_prefix, *name;
16554 int is_anonymous;
16555 struct type *type;
16556
16557 /* For extensions, reuse the type of the original namespace. */
16558 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16559 {
16560 struct die_info *ext_die;
16561 struct dwarf2_cu *ext_cu = cu;
16562
16563 ext_die = dwarf2_extension (die, &ext_cu);
16564 type = read_type_die (ext_die, ext_cu);
16565
16566 /* EXT_CU may not be the same as CU.
16567 Ensure TYPE is recorded with CU in die_type_hash. */
16568 return set_die_type (die, type, cu);
16569 }
16570
16571 name = namespace_name (die, &is_anonymous, cu);
16572
16573 /* Now build the name of the current namespace. */
16574
16575 previous_prefix = determine_prefix (die, cu);
16576 if (previous_prefix[0] != '\0')
16577 name = typename_concat (&objfile->objfile_obstack,
16578 previous_prefix, name, 0, cu);
16579
16580 /* Create the type. */
16581 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16582
16583 return set_die_type (die, type, cu);
16584 }
16585
16586 /* Read a namespace scope. */
16587
16588 static void
16589 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16590 {
16591 struct objfile *objfile = cu->per_objfile->objfile;
16592 int is_anonymous;
16593
16594 /* Add a symbol associated to this if we haven't seen the namespace
16595 before. Also, add a using directive if it's an anonymous
16596 namespace. */
16597
16598 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16599 {
16600 struct type *type;
16601
16602 type = read_type_die (die, cu);
16603 new_symbol (die, type, cu);
16604
16605 namespace_name (die, &is_anonymous, cu);
16606 if (is_anonymous)
16607 {
16608 const char *previous_prefix = determine_prefix (die, cu);
16609
16610 std::vector<const char *> excludes;
16611 add_using_directive (using_directives (cu),
16612 previous_prefix, type->name (), NULL,
16613 NULL, excludes, 0, &objfile->objfile_obstack);
16614 }
16615 }
16616
16617 if (die->child != NULL)
16618 {
16619 struct die_info *child_die = die->child;
16620
16621 while (child_die && child_die->tag)
16622 {
16623 process_die (child_die, cu);
16624 child_die = child_die->sibling;
16625 }
16626 }
16627 }
16628
16629 /* Read a Fortran module as type. This DIE can be only a declaration used for
16630 imported module. Still we need that type as local Fortran "use ... only"
16631 declaration imports depend on the created type in determine_prefix. */
16632
16633 static struct type *
16634 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16635 {
16636 struct objfile *objfile = cu->per_objfile->objfile;
16637 const char *module_name;
16638 struct type *type;
16639
16640 module_name = dwarf2_name (die, cu);
16641 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16642
16643 return set_die_type (die, type, cu);
16644 }
16645
16646 /* Read a Fortran module. */
16647
16648 static void
16649 read_module (struct die_info *die, struct dwarf2_cu *cu)
16650 {
16651 struct die_info *child_die = die->child;
16652 struct type *type;
16653
16654 type = read_type_die (die, cu);
16655 new_symbol (die, type, cu);
16656
16657 while (child_die && child_die->tag)
16658 {
16659 process_die (child_die, cu);
16660 child_die = child_die->sibling;
16661 }
16662 }
16663
16664 /* Return the name of the namespace represented by DIE. Set
16665 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16666 namespace. */
16667
16668 static const char *
16669 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16670 {
16671 struct die_info *current_die;
16672 const char *name = NULL;
16673
16674 /* Loop through the extensions until we find a name. */
16675
16676 for (current_die = die;
16677 current_die != NULL;
16678 current_die = dwarf2_extension (die, &cu))
16679 {
16680 /* We don't use dwarf2_name here so that we can detect the absence
16681 of a name -> anonymous namespace. */
16682 name = dwarf2_string_attr (die, DW_AT_name, cu);
16683
16684 if (name != NULL)
16685 break;
16686 }
16687
16688 /* Is it an anonymous namespace? */
16689
16690 *is_anonymous = (name == NULL);
16691 if (*is_anonymous)
16692 name = CP_ANONYMOUS_NAMESPACE_STR;
16693
16694 return name;
16695 }
16696
16697 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16698 the user defined type vector. */
16699
16700 static struct type *
16701 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16702 {
16703 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16704 struct comp_unit_head *cu_header = &cu->header;
16705 struct type *type;
16706 struct attribute *attr_byte_size;
16707 struct attribute *attr_address_class;
16708 int byte_size, addr_class;
16709 struct type *target_type;
16710
16711 target_type = die_type (die, cu);
16712
16713 /* The die_type call above may have already set the type for this DIE. */
16714 type = get_die_type (die, cu);
16715 if (type)
16716 return type;
16717
16718 type = lookup_pointer_type (target_type);
16719
16720 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16721 if (attr_byte_size)
16722 byte_size = DW_UNSND (attr_byte_size);
16723 else
16724 byte_size = cu_header->addr_size;
16725
16726 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16727 if (attr_address_class)
16728 addr_class = DW_UNSND (attr_address_class);
16729 else
16730 addr_class = DW_ADDR_none;
16731
16732 ULONGEST alignment = get_alignment (cu, die);
16733
16734 /* If the pointer size, alignment, or address class is different
16735 than the default, create a type variant marked as such and set
16736 the length accordingly. */
16737 if (TYPE_LENGTH (type) != byte_size
16738 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16739 && alignment != TYPE_RAW_ALIGN (type))
16740 || addr_class != DW_ADDR_none)
16741 {
16742 if (gdbarch_address_class_type_flags_p (gdbarch))
16743 {
16744 int type_flags;
16745
16746 type_flags = gdbarch_address_class_type_flags
16747 (gdbarch, byte_size, addr_class);
16748 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16749 == 0);
16750 type = make_type_with_address_space (type, type_flags);
16751 }
16752 else if (TYPE_LENGTH (type) != byte_size)
16753 {
16754 complaint (_("invalid pointer size %d"), byte_size);
16755 }
16756 else if (TYPE_RAW_ALIGN (type) != alignment)
16757 {
16758 complaint (_("Invalid DW_AT_alignment"
16759 " - DIE at %s [in module %s]"),
16760 sect_offset_str (die->sect_off),
16761 objfile_name (cu->per_objfile->objfile));
16762 }
16763 else
16764 {
16765 /* Should we also complain about unhandled address classes? */
16766 }
16767 }
16768
16769 TYPE_LENGTH (type) = byte_size;
16770 set_type_align (type, alignment);
16771 return set_die_type (die, type, cu);
16772 }
16773
16774 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16775 the user defined type vector. */
16776
16777 static struct type *
16778 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16779 {
16780 struct type *type;
16781 struct type *to_type;
16782 struct type *domain;
16783
16784 to_type = die_type (die, cu);
16785 domain = die_containing_type (die, cu);
16786
16787 /* The calls above may have already set the type for this DIE. */
16788 type = get_die_type (die, cu);
16789 if (type)
16790 return type;
16791
16792 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16793 type = lookup_methodptr_type (to_type);
16794 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16795 {
16796 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16797
16798 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16799 to_type->fields (), to_type->num_fields (),
16800 TYPE_VARARGS (to_type));
16801 type = lookup_methodptr_type (new_type);
16802 }
16803 else
16804 type = lookup_memberptr_type (to_type, domain);
16805
16806 return set_die_type (die, type, cu);
16807 }
16808
16809 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16810 the user defined type vector. */
16811
16812 static struct type *
16813 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16814 enum type_code refcode)
16815 {
16816 struct comp_unit_head *cu_header = &cu->header;
16817 struct type *type, *target_type;
16818 struct attribute *attr;
16819
16820 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16821
16822 target_type = die_type (die, cu);
16823
16824 /* The die_type call above may have already set the type for this DIE. */
16825 type = get_die_type (die, cu);
16826 if (type)
16827 return type;
16828
16829 type = lookup_reference_type (target_type, refcode);
16830 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16831 if (attr != nullptr)
16832 {
16833 TYPE_LENGTH (type) = DW_UNSND (attr);
16834 }
16835 else
16836 {
16837 TYPE_LENGTH (type) = cu_header->addr_size;
16838 }
16839 maybe_set_alignment (cu, die, type);
16840 return set_die_type (die, type, cu);
16841 }
16842
16843 /* Add the given cv-qualifiers to the element type of the array. GCC
16844 outputs DWARF type qualifiers that apply to an array, not the
16845 element type. But GDB relies on the array element type to carry
16846 the cv-qualifiers. This mimics section 6.7.3 of the C99
16847 specification. */
16848
16849 static struct type *
16850 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16851 struct type *base_type, int cnst, int voltl)
16852 {
16853 struct type *el_type, *inner_array;
16854
16855 base_type = copy_type (base_type);
16856 inner_array = base_type;
16857
16858 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16859 {
16860 TYPE_TARGET_TYPE (inner_array) =
16861 copy_type (TYPE_TARGET_TYPE (inner_array));
16862 inner_array = TYPE_TARGET_TYPE (inner_array);
16863 }
16864
16865 el_type = TYPE_TARGET_TYPE (inner_array);
16866 cnst |= TYPE_CONST (el_type);
16867 voltl |= TYPE_VOLATILE (el_type);
16868 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16869
16870 return set_die_type (die, base_type, cu);
16871 }
16872
16873 static struct type *
16874 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16875 {
16876 struct type *base_type, *cv_type;
16877
16878 base_type = die_type (die, cu);
16879
16880 /* The die_type call above may have already set the type for this DIE. */
16881 cv_type = get_die_type (die, cu);
16882 if (cv_type)
16883 return cv_type;
16884
16885 /* In case the const qualifier is applied to an array type, the element type
16886 is so qualified, not the array type (section 6.7.3 of C99). */
16887 if (base_type->code () == TYPE_CODE_ARRAY)
16888 return add_array_cv_type (die, cu, base_type, 1, 0);
16889
16890 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16891 return set_die_type (die, cv_type, cu);
16892 }
16893
16894 static struct type *
16895 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16896 {
16897 struct type *base_type, *cv_type;
16898
16899 base_type = die_type (die, cu);
16900
16901 /* The die_type call above may have already set the type for this DIE. */
16902 cv_type = get_die_type (die, cu);
16903 if (cv_type)
16904 return cv_type;
16905
16906 /* In case the volatile qualifier is applied to an array type, the
16907 element type is so qualified, not the array type (section 6.7.3
16908 of C99). */
16909 if (base_type->code () == TYPE_CODE_ARRAY)
16910 return add_array_cv_type (die, cu, base_type, 0, 1);
16911
16912 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16913 return set_die_type (die, cv_type, cu);
16914 }
16915
16916 /* Handle DW_TAG_restrict_type. */
16917
16918 static struct type *
16919 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16920 {
16921 struct type *base_type, *cv_type;
16922
16923 base_type = die_type (die, cu);
16924
16925 /* The die_type call above may have already set the type for this DIE. */
16926 cv_type = get_die_type (die, cu);
16927 if (cv_type)
16928 return cv_type;
16929
16930 cv_type = make_restrict_type (base_type);
16931 return set_die_type (die, cv_type, cu);
16932 }
16933
16934 /* Handle DW_TAG_atomic_type. */
16935
16936 static struct type *
16937 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16938 {
16939 struct type *base_type, *cv_type;
16940
16941 base_type = die_type (die, cu);
16942
16943 /* The die_type call above may have already set the type for this DIE. */
16944 cv_type = get_die_type (die, cu);
16945 if (cv_type)
16946 return cv_type;
16947
16948 cv_type = make_atomic_type (base_type);
16949 return set_die_type (die, cv_type, cu);
16950 }
16951
16952 /* Extract all information from a DW_TAG_string_type DIE and add to
16953 the user defined type vector. It isn't really a user defined type,
16954 but it behaves like one, with other DIE's using an AT_user_def_type
16955 attribute to reference it. */
16956
16957 static struct type *
16958 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16959 {
16960 struct objfile *objfile = cu->per_objfile->objfile;
16961 struct gdbarch *gdbarch = objfile->arch ();
16962 struct type *type, *range_type, *index_type, *char_type;
16963 struct attribute *attr;
16964 struct dynamic_prop prop;
16965 bool length_is_constant = true;
16966 LONGEST length;
16967
16968 /* There are a couple of places where bit sizes might be made use of
16969 when parsing a DW_TAG_string_type, however, no producer that we know
16970 of make use of these. Handling bit sizes that are a multiple of the
16971 byte size is easy enough, but what about other bit sizes? Lets deal
16972 with that problem when we have to. Warn about these attributes being
16973 unsupported, then parse the type and ignore them like we always
16974 have. */
16975 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16976 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16977 {
16978 static bool warning_printed = false;
16979 if (!warning_printed)
16980 {
16981 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16982 "currently supported on DW_TAG_string_type."));
16983 warning_printed = true;
16984 }
16985 }
16986
16987 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16988 if (attr != nullptr && !attr->form_is_constant ())
16989 {
16990 /* The string length describes the location at which the length of
16991 the string can be found. The size of the length field can be
16992 specified with one of the attributes below. */
16993 struct type *prop_type;
16994 struct attribute *len
16995 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16996 if (len == nullptr)
16997 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16998 if (len != nullptr && len->form_is_constant ())
16999 {
17000 /* Pass 0 as the default as we know this attribute is constant
17001 and the default value will not be returned. */
17002 LONGEST sz = len->constant_value (0);
17003 prop_type = cu->per_objfile->int_type (sz, true);
17004 }
17005 else
17006 {
17007 /* If the size is not specified then we assume it is the size of
17008 an address on this target. */
17009 prop_type = cu->addr_sized_int_type (true);
17010 }
17011
17012 /* Convert the attribute into a dynamic property. */
17013 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17014 length = 1;
17015 else
17016 length_is_constant = false;
17017 }
17018 else if (attr != nullptr)
17019 {
17020 /* This DW_AT_string_length just contains the length with no
17021 indirection. There's no need to create a dynamic property in this
17022 case. Pass 0 for the default value as we know it will not be
17023 returned in this case. */
17024 length = attr->constant_value (0);
17025 }
17026 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17027 {
17028 /* We don't currently support non-constant byte sizes for strings. */
17029 length = attr->constant_value (1);
17030 }
17031 else
17032 {
17033 /* Use 1 as a fallback length if we have nothing else. */
17034 length = 1;
17035 }
17036
17037 index_type = objfile_type (objfile)->builtin_int;
17038 if (length_is_constant)
17039 range_type = create_static_range_type (NULL, index_type, 1, length);
17040 else
17041 {
17042 struct dynamic_prop low_bound;
17043
17044 low_bound.kind = PROP_CONST;
17045 low_bound.data.const_val = 1;
17046 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17047 }
17048 char_type = language_string_char_type (cu->language_defn, gdbarch);
17049 type = create_string_type (NULL, char_type, range_type);
17050
17051 return set_die_type (die, type, cu);
17052 }
17053
17054 /* Assuming that DIE corresponds to a function, returns nonzero
17055 if the function is prototyped. */
17056
17057 static int
17058 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17059 {
17060 struct attribute *attr;
17061
17062 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17063 if (attr && (DW_UNSND (attr) != 0))
17064 return 1;
17065
17066 /* The DWARF standard implies that the DW_AT_prototyped attribute
17067 is only meaningful for C, but the concept also extends to other
17068 languages that allow unprototyped functions (Eg: Objective C).
17069 For all other languages, assume that functions are always
17070 prototyped. */
17071 if (cu->language != language_c
17072 && cu->language != language_objc
17073 && cu->language != language_opencl)
17074 return 1;
17075
17076 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17077 prototyped and unprototyped functions; default to prototyped,
17078 since that is more common in modern code (and RealView warns
17079 about unprototyped functions). */
17080 if (producer_is_realview (cu->producer))
17081 return 1;
17082
17083 return 0;
17084 }
17085
17086 /* Handle DIES due to C code like:
17087
17088 struct foo
17089 {
17090 int (*funcp)(int a, long l);
17091 int b;
17092 };
17093
17094 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17095
17096 static struct type *
17097 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17098 {
17099 struct objfile *objfile = cu->per_objfile->objfile;
17100 struct type *type; /* Type that this function returns. */
17101 struct type *ftype; /* Function that returns above type. */
17102 struct attribute *attr;
17103
17104 type = die_type (die, cu);
17105
17106 /* The die_type call above may have already set the type for this DIE. */
17107 ftype = get_die_type (die, cu);
17108 if (ftype)
17109 return ftype;
17110
17111 ftype = lookup_function_type (type);
17112
17113 if (prototyped_function_p (die, cu))
17114 TYPE_PROTOTYPED (ftype) = 1;
17115
17116 /* Store the calling convention in the type if it's available in
17117 the subroutine die. Otherwise set the calling convention to
17118 the default value DW_CC_normal. */
17119 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17120 if (attr != nullptr
17121 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17122 TYPE_CALLING_CONVENTION (ftype)
17123 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17124 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17125 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17126 else
17127 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17128
17129 /* Record whether the function returns normally to its caller or not
17130 if the DWARF producer set that information. */
17131 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17132 if (attr && (DW_UNSND (attr) != 0))
17133 TYPE_NO_RETURN (ftype) = 1;
17134
17135 /* We need to add the subroutine type to the die immediately so
17136 we don't infinitely recurse when dealing with parameters
17137 declared as the same subroutine type. */
17138 set_die_type (die, ftype, cu);
17139
17140 if (die->child != NULL)
17141 {
17142 struct type *void_type = objfile_type (objfile)->builtin_void;
17143 struct die_info *child_die;
17144 int nparams, iparams;
17145
17146 /* Count the number of parameters.
17147 FIXME: GDB currently ignores vararg functions, but knows about
17148 vararg member functions. */
17149 nparams = 0;
17150 child_die = die->child;
17151 while (child_die && child_die->tag)
17152 {
17153 if (child_die->tag == DW_TAG_formal_parameter)
17154 nparams++;
17155 else if (child_die->tag == DW_TAG_unspecified_parameters)
17156 TYPE_VARARGS (ftype) = 1;
17157 child_die = child_die->sibling;
17158 }
17159
17160 /* Allocate storage for parameters and fill them in. */
17161 ftype->set_num_fields (nparams);
17162 ftype->set_fields
17163 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17164
17165 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17166 even if we error out during the parameters reading below. */
17167 for (iparams = 0; iparams < nparams; iparams++)
17168 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17169
17170 iparams = 0;
17171 child_die = die->child;
17172 while (child_die && child_die->tag)
17173 {
17174 if (child_die->tag == DW_TAG_formal_parameter)
17175 {
17176 struct type *arg_type;
17177
17178 /* DWARF version 2 has no clean way to discern C++
17179 static and non-static member functions. G++ helps
17180 GDB by marking the first parameter for non-static
17181 member functions (which is the this pointer) as
17182 artificial. We pass this information to
17183 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17184
17185 DWARF version 3 added DW_AT_object_pointer, which GCC
17186 4.5 does not yet generate. */
17187 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17188 if (attr != nullptr)
17189 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17190 else
17191 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17192 arg_type = die_type (child_die, cu);
17193
17194 /* RealView does not mark THIS as const, which the testsuite
17195 expects. GCC marks THIS as const in method definitions,
17196 but not in the class specifications (GCC PR 43053). */
17197 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17198 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17199 {
17200 int is_this = 0;
17201 struct dwarf2_cu *arg_cu = cu;
17202 const char *name = dwarf2_name (child_die, cu);
17203
17204 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17205 if (attr != nullptr)
17206 {
17207 /* If the compiler emits this, use it. */
17208 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17209 is_this = 1;
17210 }
17211 else if (name && strcmp (name, "this") == 0)
17212 /* Function definitions will have the argument names. */
17213 is_this = 1;
17214 else if (name == NULL && iparams == 0)
17215 /* Declarations may not have the names, so like
17216 elsewhere in GDB, assume an artificial first
17217 argument is "this". */
17218 is_this = 1;
17219
17220 if (is_this)
17221 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17222 arg_type, 0);
17223 }
17224
17225 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17226 iparams++;
17227 }
17228 child_die = child_die->sibling;
17229 }
17230 }
17231
17232 return ftype;
17233 }
17234
17235 static struct type *
17236 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17237 {
17238 struct objfile *objfile = cu->per_objfile->objfile;
17239 const char *name = NULL;
17240 struct type *this_type, *target_type;
17241
17242 name = dwarf2_full_name (NULL, die, cu);
17243 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17244 TYPE_TARGET_STUB (this_type) = 1;
17245 set_die_type (die, this_type, cu);
17246 target_type = die_type (die, cu);
17247 if (target_type != this_type)
17248 TYPE_TARGET_TYPE (this_type) = target_type;
17249 else
17250 {
17251 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17252 spec and cause infinite loops in GDB. */
17253 complaint (_("Self-referential DW_TAG_typedef "
17254 "- DIE at %s [in module %s]"),
17255 sect_offset_str (die->sect_off), objfile_name (objfile));
17256 TYPE_TARGET_TYPE (this_type) = NULL;
17257 }
17258 if (name == NULL)
17259 {
17260 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17261 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17262 Handle these by just returning the target type, rather than
17263 constructing an anonymous typedef type and trying to handle this
17264 elsewhere. */
17265 set_die_type (die, target_type, cu);
17266 return target_type;
17267 }
17268 return this_type;
17269 }
17270
17271 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17272 (which may be different from NAME) to the architecture back-end to allow
17273 it to guess the correct format if necessary. */
17274
17275 static struct type *
17276 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17277 const char *name_hint, enum bfd_endian byte_order)
17278 {
17279 struct gdbarch *gdbarch = objfile->arch ();
17280 const struct floatformat **format;
17281 struct type *type;
17282
17283 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17284 if (format)
17285 type = init_float_type (objfile, bits, name, format, byte_order);
17286 else
17287 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17288
17289 return type;
17290 }
17291
17292 /* Allocate an integer type of size BITS and name NAME. */
17293
17294 static struct type *
17295 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17296 int bits, int unsigned_p, const char *name)
17297 {
17298 struct type *type;
17299
17300 /* Versions of Intel's C Compiler generate an integer type called "void"
17301 instead of using DW_TAG_unspecified_type. This has been seen on
17302 at least versions 14, 17, and 18. */
17303 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17304 && strcmp (name, "void") == 0)
17305 type = objfile_type (objfile)->builtin_void;
17306 else
17307 type = init_integer_type (objfile, bits, unsigned_p, name);
17308
17309 return type;
17310 }
17311
17312 /* Initialise and return a floating point type of size BITS suitable for
17313 use as a component of a complex number. The NAME_HINT is passed through
17314 when initialising the floating point type and is the name of the complex
17315 type.
17316
17317 As DWARF doesn't currently provide an explicit name for the components
17318 of a complex number, but it can be helpful to have these components
17319 named, we try to select a suitable name based on the size of the
17320 component. */
17321 static struct type *
17322 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17323 struct objfile *objfile,
17324 int bits, const char *name_hint,
17325 enum bfd_endian byte_order)
17326 {
17327 gdbarch *gdbarch = objfile->arch ();
17328 struct type *tt = nullptr;
17329
17330 /* Try to find a suitable floating point builtin type of size BITS.
17331 We're going to use the name of this type as the name for the complex
17332 target type that we are about to create. */
17333 switch (cu->language)
17334 {
17335 case language_fortran:
17336 switch (bits)
17337 {
17338 case 32:
17339 tt = builtin_f_type (gdbarch)->builtin_real;
17340 break;
17341 case 64:
17342 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17343 break;
17344 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17345 case 128:
17346 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17347 break;
17348 }
17349 break;
17350 default:
17351 switch (bits)
17352 {
17353 case 32:
17354 tt = builtin_type (gdbarch)->builtin_float;
17355 break;
17356 case 64:
17357 tt = builtin_type (gdbarch)->builtin_double;
17358 break;
17359 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17360 case 128:
17361 tt = builtin_type (gdbarch)->builtin_long_double;
17362 break;
17363 }
17364 break;
17365 }
17366
17367 /* If the type we found doesn't match the size we were looking for, then
17368 pretend we didn't find a type at all, the complex target type we
17369 create will then be nameless. */
17370 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17371 tt = nullptr;
17372
17373 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17374 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17375 }
17376
17377 /* Find a representation of a given base type and install
17378 it in the TYPE field of the die. */
17379
17380 static struct type *
17381 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17382 {
17383 struct objfile *objfile = cu->per_objfile->objfile;
17384 struct type *type;
17385 struct attribute *attr;
17386 int encoding = 0, bits = 0;
17387 const char *name;
17388 gdbarch *arch;
17389
17390 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17391 if (attr != nullptr)
17392 encoding = DW_UNSND (attr);
17393 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17394 if (attr != nullptr)
17395 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17396 name = dwarf2_name (die, cu);
17397 if (!name)
17398 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17399
17400 arch = objfile->arch ();
17401 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17402
17403 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17404 if (attr)
17405 {
17406 int endianity = DW_UNSND (attr);
17407
17408 switch (endianity)
17409 {
17410 case DW_END_big:
17411 byte_order = BFD_ENDIAN_BIG;
17412 break;
17413 case DW_END_little:
17414 byte_order = BFD_ENDIAN_LITTLE;
17415 break;
17416 default:
17417 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17418 break;
17419 }
17420 }
17421
17422 switch (encoding)
17423 {
17424 case DW_ATE_address:
17425 /* Turn DW_ATE_address into a void * pointer. */
17426 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17427 type = init_pointer_type (objfile, bits, name, type);
17428 break;
17429 case DW_ATE_boolean:
17430 type = init_boolean_type (objfile, bits, 1, name);
17431 break;
17432 case DW_ATE_complex_float:
17433 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17434 byte_order);
17435 if (type->code () == TYPE_CODE_ERROR)
17436 {
17437 if (name == nullptr)
17438 {
17439 struct obstack *obstack
17440 = &cu->per_objfile->objfile->objfile_obstack;
17441 name = obconcat (obstack, "_Complex ", type->name (),
17442 nullptr);
17443 }
17444 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17445 }
17446 else
17447 type = init_complex_type (name, type);
17448 break;
17449 case DW_ATE_decimal_float:
17450 type = init_decfloat_type (objfile, bits, name);
17451 break;
17452 case DW_ATE_float:
17453 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17454 break;
17455 case DW_ATE_signed:
17456 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17457 break;
17458 case DW_ATE_unsigned:
17459 if (cu->language == language_fortran
17460 && name
17461 && startswith (name, "character("))
17462 type = init_character_type (objfile, bits, 1, name);
17463 else
17464 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17465 break;
17466 case DW_ATE_signed_char:
17467 if (cu->language == language_ada || cu->language == language_m2
17468 || cu->language == language_pascal
17469 || cu->language == language_fortran)
17470 type = init_character_type (objfile, bits, 0, name);
17471 else
17472 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17473 break;
17474 case DW_ATE_unsigned_char:
17475 if (cu->language == language_ada || cu->language == language_m2
17476 || cu->language == language_pascal
17477 || cu->language == language_fortran
17478 || cu->language == language_rust)
17479 type = init_character_type (objfile, bits, 1, name);
17480 else
17481 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17482 break;
17483 case DW_ATE_UTF:
17484 {
17485 if (bits == 16)
17486 type = builtin_type (arch)->builtin_char16;
17487 else if (bits == 32)
17488 type = builtin_type (arch)->builtin_char32;
17489 else
17490 {
17491 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17492 bits);
17493 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17494 }
17495 return set_die_type (die, type, cu);
17496 }
17497 break;
17498
17499 default:
17500 complaint (_("unsupported DW_AT_encoding: '%s'"),
17501 dwarf_type_encoding_name (encoding));
17502 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17503 break;
17504 }
17505
17506 if (name && strcmp (name, "char") == 0)
17507 TYPE_NOSIGN (type) = 1;
17508
17509 maybe_set_alignment (cu, die, type);
17510
17511 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17512
17513 return set_die_type (die, type, cu);
17514 }
17515
17516 /* Parse dwarf attribute if it's a block, reference or constant and put the
17517 resulting value of the attribute into struct bound_prop.
17518 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17519
17520 static int
17521 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17522 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17523 struct type *default_type)
17524 {
17525 struct dwarf2_property_baton *baton;
17526 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17527 struct objfile *objfile = per_objfile->objfile;
17528 struct obstack *obstack = &objfile->objfile_obstack;
17529
17530 gdb_assert (default_type != NULL);
17531
17532 if (attr == NULL || prop == NULL)
17533 return 0;
17534
17535 if (attr->form_is_block ())
17536 {
17537 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17538 baton->property_type = default_type;
17539 baton->locexpr.per_cu = cu->per_cu;
17540 baton->locexpr.per_objfile = per_objfile;
17541 baton->locexpr.size = DW_BLOCK (attr)->size;
17542 baton->locexpr.data = DW_BLOCK (attr)->data;
17543 switch (attr->name)
17544 {
17545 case DW_AT_string_length:
17546 baton->locexpr.is_reference = true;
17547 break;
17548 default:
17549 baton->locexpr.is_reference = false;
17550 break;
17551 }
17552 prop->data.baton = baton;
17553 prop->kind = PROP_LOCEXPR;
17554 gdb_assert (prop->data.baton != NULL);
17555 }
17556 else if (attr->form_is_ref ())
17557 {
17558 struct dwarf2_cu *target_cu = cu;
17559 struct die_info *target_die;
17560 struct attribute *target_attr;
17561
17562 target_die = follow_die_ref (die, attr, &target_cu);
17563 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17564 if (target_attr == NULL)
17565 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17566 target_cu);
17567 if (target_attr == NULL)
17568 return 0;
17569
17570 switch (target_attr->name)
17571 {
17572 case DW_AT_location:
17573 if (target_attr->form_is_section_offset ())
17574 {
17575 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17576 baton->property_type = die_type (target_die, target_cu);
17577 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17578 prop->data.baton = baton;
17579 prop->kind = PROP_LOCLIST;
17580 gdb_assert (prop->data.baton != NULL);
17581 }
17582 else if (target_attr->form_is_block ())
17583 {
17584 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17585 baton->property_type = die_type (target_die, target_cu);
17586 baton->locexpr.per_cu = cu->per_cu;
17587 baton->locexpr.per_objfile = per_objfile;
17588 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17589 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17590 baton->locexpr.is_reference = true;
17591 prop->data.baton = baton;
17592 prop->kind = PROP_LOCEXPR;
17593 gdb_assert (prop->data.baton != NULL);
17594 }
17595 else
17596 {
17597 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17598 "dynamic property");
17599 return 0;
17600 }
17601 break;
17602 case DW_AT_data_member_location:
17603 {
17604 LONGEST offset;
17605
17606 if (!handle_data_member_location (target_die, target_cu,
17607 &offset))
17608 return 0;
17609
17610 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17611 baton->property_type = read_type_die (target_die->parent,
17612 target_cu);
17613 baton->offset_info.offset = offset;
17614 baton->offset_info.type = die_type (target_die, target_cu);
17615 prop->data.baton = baton;
17616 prop->kind = PROP_ADDR_OFFSET;
17617 break;
17618 }
17619 }
17620 }
17621 else if (attr->form_is_constant ())
17622 {
17623 prop->data.const_val = attr->constant_value (0);
17624 prop->kind = PROP_CONST;
17625 }
17626 else
17627 {
17628 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17629 dwarf2_name (die, cu));
17630 return 0;
17631 }
17632
17633 return 1;
17634 }
17635
17636 /* See read.h. */
17637
17638 struct type *
17639 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17640 {
17641 struct type *int_type;
17642
17643 /* Helper macro to examine the various builtin types. */
17644 #define TRY_TYPE(F) \
17645 int_type = (unsigned_p \
17646 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17647 : objfile_type (objfile)->builtin_ ## F); \
17648 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17649 return int_type
17650
17651 TRY_TYPE (char);
17652 TRY_TYPE (short);
17653 TRY_TYPE (int);
17654 TRY_TYPE (long);
17655 TRY_TYPE (long_long);
17656
17657 #undef TRY_TYPE
17658
17659 gdb_assert_not_reached ("unable to find suitable integer type");
17660 }
17661
17662 /* See read.h. */
17663
17664 struct type *
17665 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17666 {
17667 int addr_size = this->per_cu->addr_size ();
17668 return this->per_objfile->int_type (addr_size, unsigned_p);
17669 }
17670
17671 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17672 present (which is valid) then compute the default type based on the
17673 compilation units address size. */
17674
17675 static struct type *
17676 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17677 {
17678 struct type *index_type = die_type (die, cu);
17679
17680 /* Dwarf-2 specifications explicitly allows to create subrange types
17681 without specifying a base type.
17682 In that case, the base type must be set to the type of
17683 the lower bound, upper bound or count, in that order, if any of these
17684 three attributes references an object that has a type.
17685 If no base type is found, the Dwarf-2 specifications say that
17686 a signed integer type of size equal to the size of an address should
17687 be used.
17688 For the following C code: `extern char gdb_int [];'
17689 GCC produces an empty range DIE.
17690 FIXME: muller/2010-05-28: Possible references to object for low bound,
17691 high bound or count are not yet handled by this code. */
17692 if (index_type->code () == TYPE_CODE_VOID)
17693 index_type = cu->addr_sized_int_type (false);
17694
17695 return index_type;
17696 }
17697
17698 /* Read the given DW_AT_subrange DIE. */
17699
17700 static struct type *
17701 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17702 {
17703 struct type *base_type, *orig_base_type;
17704 struct type *range_type;
17705 struct attribute *attr;
17706 struct dynamic_prop low, high;
17707 int low_default_is_valid;
17708 int high_bound_is_count = 0;
17709 const char *name;
17710 ULONGEST negative_mask;
17711
17712 orig_base_type = read_subrange_index_type (die, cu);
17713
17714 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17715 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17716 creating the range type, but we use the result of check_typedef
17717 when examining properties of the type. */
17718 base_type = check_typedef (orig_base_type);
17719
17720 /* The die_type call above may have already set the type for this DIE. */
17721 range_type = get_die_type (die, cu);
17722 if (range_type)
17723 return range_type;
17724
17725 low.kind = PROP_CONST;
17726 high.kind = PROP_CONST;
17727 high.data.const_val = 0;
17728
17729 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17730 omitting DW_AT_lower_bound. */
17731 switch (cu->language)
17732 {
17733 case language_c:
17734 case language_cplus:
17735 low.data.const_val = 0;
17736 low_default_is_valid = 1;
17737 break;
17738 case language_fortran:
17739 low.data.const_val = 1;
17740 low_default_is_valid = 1;
17741 break;
17742 case language_d:
17743 case language_objc:
17744 case language_rust:
17745 low.data.const_val = 0;
17746 low_default_is_valid = (cu->header.version >= 4);
17747 break;
17748 case language_ada:
17749 case language_m2:
17750 case language_pascal:
17751 low.data.const_val = 1;
17752 low_default_is_valid = (cu->header.version >= 4);
17753 break;
17754 default:
17755 low.data.const_val = 0;
17756 low_default_is_valid = 0;
17757 break;
17758 }
17759
17760 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17761 if (attr != nullptr)
17762 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17763 else if (!low_default_is_valid)
17764 complaint (_("Missing DW_AT_lower_bound "
17765 "- DIE at %s [in module %s]"),
17766 sect_offset_str (die->sect_off),
17767 objfile_name (cu->per_objfile->objfile));
17768
17769 struct attribute *attr_ub, *attr_count;
17770 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17771 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17772 {
17773 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17774 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17775 {
17776 /* If bounds are constant do the final calculation here. */
17777 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17778 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17779 else
17780 high_bound_is_count = 1;
17781 }
17782 else
17783 {
17784 if (attr_ub != NULL)
17785 complaint (_("Unresolved DW_AT_upper_bound "
17786 "- DIE at %s [in module %s]"),
17787 sect_offset_str (die->sect_off),
17788 objfile_name (cu->per_objfile->objfile));
17789 if (attr_count != NULL)
17790 complaint (_("Unresolved DW_AT_count "
17791 "- DIE at %s [in module %s]"),
17792 sect_offset_str (die->sect_off),
17793 objfile_name (cu->per_objfile->objfile));
17794 }
17795 }
17796
17797 LONGEST bias = 0;
17798 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17799 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17800 bias = bias_attr->constant_value (0);
17801
17802 /* Normally, the DWARF producers are expected to use a signed
17803 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17804 But this is unfortunately not always the case, as witnessed
17805 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17806 is used instead. To work around that ambiguity, we treat
17807 the bounds as signed, and thus sign-extend their values, when
17808 the base type is signed. */
17809 negative_mask =
17810 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17811 if (low.kind == PROP_CONST
17812 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17813 low.data.const_val |= negative_mask;
17814 if (high.kind == PROP_CONST
17815 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17816 high.data.const_val |= negative_mask;
17817
17818 /* Check for bit and byte strides. */
17819 struct dynamic_prop byte_stride_prop;
17820 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17821 if (attr_byte_stride != nullptr)
17822 {
17823 struct type *prop_type = cu->addr_sized_int_type (false);
17824 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17825 prop_type);
17826 }
17827
17828 struct dynamic_prop bit_stride_prop;
17829 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17830 if (attr_bit_stride != nullptr)
17831 {
17832 /* It only makes sense to have either a bit or byte stride. */
17833 if (attr_byte_stride != nullptr)
17834 {
17835 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17836 "- DIE at %s [in module %s]"),
17837 sect_offset_str (die->sect_off),
17838 objfile_name (cu->per_objfile->objfile));
17839 attr_bit_stride = nullptr;
17840 }
17841 else
17842 {
17843 struct type *prop_type = cu->addr_sized_int_type (false);
17844 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17845 prop_type);
17846 }
17847 }
17848
17849 if (attr_byte_stride != nullptr
17850 || attr_bit_stride != nullptr)
17851 {
17852 bool byte_stride_p = (attr_byte_stride != nullptr);
17853 struct dynamic_prop *stride
17854 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17855
17856 range_type
17857 = create_range_type_with_stride (NULL, orig_base_type, &low,
17858 &high, bias, stride, byte_stride_p);
17859 }
17860 else
17861 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17862
17863 if (high_bound_is_count)
17864 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17865
17866 /* Ada expects an empty array on no boundary attributes. */
17867 if (attr == NULL && cu->language != language_ada)
17868 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17869
17870 name = dwarf2_name (die, cu);
17871 if (name)
17872 range_type->set_name (name);
17873
17874 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17875 if (attr != nullptr)
17876 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17877
17878 maybe_set_alignment (cu, die, range_type);
17879
17880 set_die_type (die, range_type, cu);
17881
17882 /* set_die_type should be already done. */
17883 set_descriptive_type (range_type, die, cu);
17884
17885 return range_type;
17886 }
17887
17888 static struct type *
17889 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17890 {
17891 struct type *type;
17892
17893 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17894 type->set_name (dwarf2_name (die, cu));
17895
17896 /* In Ada, an unspecified type is typically used when the description
17897 of the type is deferred to a different unit. When encountering
17898 such a type, we treat it as a stub, and try to resolve it later on,
17899 when needed. */
17900 if (cu->language == language_ada)
17901 TYPE_STUB (type) = 1;
17902
17903 return set_die_type (die, type, cu);
17904 }
17905
17906 /* Read a single die and all its descendents. Set the die's sibling
17907 field to NULL; set other fields in the die correctly, and set all
17908 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17909 location of the info_ptr after reading all of those dies. PARENT
17910 is the parent of the die in question. */
17911
17912 static struct die_info *
17913 read_die_and_children (const struct die_reader_specs *reader,
17914 const gdb_byte *info_ptr,
17915 const gdb_byte **new_info_ptr,
17916 struct die_info *parent)
17917 {
17918 struct die_info *die;
17919 const gdb_byte *cur_ptr;
17920
17921 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17922 if (die == NULL)
17923 {
17924 *new_info_ptr = cur_ptr;
17925 return NULL;
17926 }
17927 store_in_ref_table (die, reader->cu);
17928
17929 if (die->has_children)
17930 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17931 else
17932 {
17933 die->child = NULL;
17934 *new_info_ptr = cur_ptr;
17935 }
17936
17937 die->sibling = NULL;
17938 die->parent = parent;
17939 return die;
17940 }
17941
17942 /* Read a die, all of its descendents, and all of its siblings; set
17943 all of the fields of all of the dies correctly. Arguments are as
17944 in read_die_and_children. */
17945
17946 static struct die_info *
17947 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17948 const gdb_byte *info_ptr,
17949 const gdb_byte **new_info_ptr,
17950 struct die_info *parent)
17951 {
17952 struct die_info *first_die, *last_sibling;
17953 const gdb_byte *cur_ptr;
17954
17955 cur_ptr = info_ptr;
17956 first_die = last_sibling = NULL;
17957
17958 while (1)
17959 {
17960 struct die_info *die
17961 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17962
17963 if (die == NULL)
17964 {
17965 *new_info_ptr = cur_ptr;
17966 return first_die;
17967 }
17968
17969 if (!first_die)
17970 first_die = die;
17971 else
17972 last_sibling->sibling = die;
17973
17974 last_sibling = die;
17975 }
17976 }
17977
17978 /* Read a die, all of its descendents, and all of its siblings; set
17979 all of the fields of all of the dies correctly. Arguments are as
17980 in read_die_and_children.
17981 This the main entry point for reading a DIE and all its children. */
17982
17983 static struct die_info *
17984 read_die_and_siblings (const struct die_reader_specs *reader,
17985 const gdb_byte *info_ptr,
17986 const gdb_byte **new_info_ptr,
17987 struct die_info *parent)
17988 {
17989 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17990 new_info_ptr, parent);
17991
17992 if (dwarf_die_debug)
17993 {
17994 fprintf_unfiltered (gdb_stdlog,
17995 "Read die from %s@0x%x of %s:\n",
17996 reader->die_section->get_name (),
17997 (unsigned) (info_ptr - reader->die_section->buffer),
17998 bfd_get_filename (reader->abfd));
17999 dump_die (die, dwarf_die_debug);
18000 }
18001
18002 return die;
18003 }
18004
18005 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18006 attributes.
18007 The caller is responsible for filling in the extra attributes
18008 and updating (*DIEP)->num_attrs.
18009 Set DIEP to point to a newly allocated die with its information,
18010 except for its child, sibling, and parent fields. */
18011
18012 static const gdb_byte *
18013 read_full_die_1 (const struct die_reader_specs *reader,
18014 struct die_info **diep, const gdb_byte *info_ptr,
18015 int num_extra_attrs)
18016 {
18017 unsigned int abbrev_number, bytes_read, i;
18018 struct abbrev_info *abbrev;
18019 struct die_info *die;
18020 struct dwarf2_cu *cu = reader->cu;
18021 bfd *abfd = reader->abfd;
18022
18023 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18024 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18025 info_ptr += bytes_read;
18026 if (!abbrev_number)
18027 {
18028 *diep = NULL;
18029 return info_ptr;
18030 }
18031
18032 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18033 if (!abbrev)
18034 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18035 abbrev_number,
18036 bfd_get_filename (abfd));
18037
18038 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18039 die->sect_off = sect_off;
18040 die->tag = abbrev->tag;
18041 die->abbrev = abbrev_number;
18042 die->has_children = abbrev->has_children;
18043
18044 /* Make the result usable.
18045 The caller needs to update num_attrs after adding the extra
18046 attributes. */
18047 die->num_attrs = abbrev->num_attrs;
18048
18049 std::vector<int> indexes_that_need_reprocess;
18050 for (i = 0; i < abbrev->num_attrs; ++i)
18051 {
18052 bool need_reprocess;
18053 info_ptr =
18054 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18055 info_ptr, &need_reprocess);
18056 if (need_reprocess)
18057 indexes_that_need_reprocess.push_back (i);
18058 }
18059
18060 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18061 if (attr != nullptr)
18062 cu->str_offsets_base = DW_UNSND (attr);
18063
18064 attr = die->attr (DW_AT_loclists_base);
18065 if (attr != nullptr)
18066 cu->loclist_base = DW_UNSND (attr);
18067
18068 auto maybe_addr_base = die->addr_base ();
18069 if (maybe_addr_base.has_value ())
18070 cu->addr_base = *maybe_addr_base;
18071 for (int index : indexes_that_need_reprocess)
18072 read_attribute_reprocess (reader, &die->attrs[index]);
18073 *diep = die;
18074 return info_ptr;
18075 }
18076
18077 /* Read a die and all its attributes.
18078 Set DIEP to point to a newly allocated die with its information,
18079 except for its child, sibling, and parent fields. */
18080
18081 static const gdb_byte *
18082 read_full_die (const struct die_reader_specs *reader,
18083 struct die_info **diep, const gdb_byte *info_ptr)
18084 {
18085 const gdb_byte *result;
18086
18087 result = read_full_die_1 (reader, diep, info_ptr, 0);
18088
18089 if (dwarf_die_debug)
18090 {
18091 fprintf_unfiltered (gdb_stdlog,
18092 "Read die from %s@0x%x of %s:\n",
18093 reader->die_section->get_name (),
18094 (unsigned) (info_ptr - reader->die_section->buffer),
18095 bfd_get_filename (reader->abfd));
18096 dump_die (*diep, dwarf_die_debug);
18097 }
18098
18099 return result;
18100 }
18101 \f
18102
18103 /* Returns nonzero if TAG represents a type that we might generate a partial
18104 symbol for. */
18105
18106 static int
18107 is_type_tag_for_partial (int tag)
18108 {
18109 switch (tag)
18110 {
18111 #if 0
18112 /* Some types that would be reasonable to generate partial symbols for,
18113 that we don't at present. */
18114 case DW_TAG_array_type:
18115 case DW_TAG_file_type:
18116 case DW_TAG_ptr_to_member_type:
18117 case DW_TAG_set_type:
18118 case DW_TAG_string_type:
18119 case DW_TAG_subroutine_type:
18120 #endif
18121 case DW_TAG_base_type:
18122 case DW_TAG_class_type:
18123 case DW_TAG_interface_type:
18124 case DW_TAG_enumeration_type:
18125 case DW_TAG_structure_type:
18126 case DW_TAG_subrange_type:
18127 case DW_TAG_typedef:
18128 case DW_TAG_union_type:
18129 return 1;
18130 default:
18131 return 0;
18132 }
18133 }
18134
18135 /* Load all DIEs that are interesting for partial symbols into memory. */
18136
18137 static struct partial_die_info *
18138 load_partial_dies (const struct die_reader_specs *reader,
18139 const gdb_byte *info_ptr, int building_psymtab)
18140 {
18141 struct dwarf2_cu *cu = reader->cu;
18142 struct objfile *objfile = cu->per_objfile->objfile;
18143 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18144 unsigned int bytes_read;
18145 unsigned int load_all = 0;
18146 int nesting_level = 1;
18147
18148 parent_die = NULL;
18149 last_die = NULL;
18150
18151 gdb_assert (cu->per_cu != NULL);
18152 if (cu->per_cu->load_all_dies)
18153 load_all = 1;
18154
18155 cu->partial_dies
18156 = htab_create_alloc_ex (cu->header.length / 12,
18157 partial_die_hash,
18158 partial_die_eq,
18159 NULL,
18160 &cu->comp_unit_obstack,
18161 hashtab_obstack_allocate,
18162 dummy_obstack_deallocate);
18163
18164 while (1)
18165 {
18166 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18167
18168 /* A NULL abbrev means the end of a series of children. */
18169 if (abbrev == NULL)
18170 {
18171 if (--nesting_level == 0)
18172 return first_die;
18173
18174 info_ptr += bytes_read;
18175 last_die = parent_die;
18176 parent_die = parent_die->die_parent;
18177 continue;
18178 }
18179
18180 /* Check for template arguments. We never save these; if
18181 they're seen, we just mark the parent, and go on our way. */
18182 if (parent_die != NULL
18183 && cu->language == language_cplus
18184 && (abbrev->tag == DW_TAG_template_type_param
18185 || abbrev->tag == DW_TAG_template_value_param))
18186 {
18187 parent_die->has_template_arguments = 1;
18188
18189 if (!load_all)
18190 {
18191 /* We don't need a partial DIE for the template argument. */
18192 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18193 continue;
18194 }
18195 }
18196
18197 /* We only recurse into c++ subprograms looking for template arguments.
18198 Skip their other children. */
18199 if (!load_all
18200 && cu->language == language_cplus
18201 && parent_die != NULL
18202 && parent_die->tag == DW_TAG_subprogram)
18203 {
18204 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18205 continue;
18206 }
18207
18208 /* Check whether this DIE is interesting enough to save. Normally
18209 we would not be interested in members here, but there may be
18210 later variables referencing them via DW_AT_specification (for
18211 static members). */
18212 if (!load_all
18213 && !is_type_tag_for_partial (abbrev->tag)
18214 && abbrev->tag != DW_TAG_constant
18215 && abbrev->tag != DW_TAG_enumerator
18216 && abbrev->tag != DW_TAG_subprogram
18217 && abbrev->tag != DW_TAG_inlined_subroutine
18218 && abbrev->tag != DW_TAG_lexical_block
18219 && abbrev->tag != DW_TAG_variable
18220 && abbrev->tag != DW_TAG_namespace
18221 && abbrev->tag != DW_TAG_module
18222 && abbrev->tag != DW_TAG_member
18223 && abbrev->tag != DW_TAG_imported_unit
18224 && abbrev->tag != DW_TAG_imported_declaration)
18225 {
18226 /* Otherwise we skip to the next sibling, if any. */
18227 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18228 continue;
18229 }
18230
18231 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18232 abbrev);
18233
18234 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18235
18236 /* This two-pass algorithm for processing partial symbols has a
18237 high cost in cache pressure. Thus, handle some simple cases
18238 here which cover the majority of C partial symbols. DIEs
18239 which neither have specification tags in them, nor could have
18240 specification tags elsewhere pointing at them, can simply be
18241 processed and discarded.
18242
18243 This segment is also optional; scan_partial_symbols and
18244 add_partial_symbol will handle these DIEs if we chain
18245 them in normally. When compilers which do not emit large
18246 quantities of duplicate debug information are more common,
18247 this code can probably be removed. */
18248
18249 /* Any complete simple types at the top level (pretty much all
18250 of them, for a language without namespaces), can be processed
18251 directly. */
18252 if (parent_die == NULL
18253 && pdi.has_specification == 0
18254 && pdi.is_declaration == 0
18255 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18256 || pdi.tag == DW_TAG_base_type
18257 || pdi.tag == DW_TAG_subrange_type))
18258 {
18259 if (building_psymtab && pdi.name != NULL)
18260 add_psymbol_to_list (pdi.name, false,
18261 VAR_DOMAIN, LOC_TYPEDEF, -1,
18262 psymbol_placement::STATIC,
18263 0, cu->language, objfile);
18264 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18265 continue;
18266 }
18267
18268 /* The exception for DW_TAG_typedef with has_children above is
18269 a workaround of GCC PR debug/47510. In the case of this complaint
18270 type_name_or_error will error on such types later.
18271
18272 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18273 it could not find the child DIEs referenced later, this is checked
18274 above. In correct DWARF DW_TAG_typedef should have no children. */
18275
18276 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18277 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18278 "- DIE at %s [in module %s]"),
18279 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18280
18281 /* If we're at the second level, and we're an enumerator, and
18282 our parent has no specification (meaning possibly lives in a
18283 namespace elsewhere), then we can add the partial symbol now
18284 instead of queueing it. */
18285 if (pdi.tag == DW_TAG_enumerator
18286 && parent_die != NULL
18287 && parent_die->die_parent == NULL
18288 && parent_die->tag == DW_TAG_enumeration_type
18289 && parent_die->has_specification == 0)
18290 {
18291 if (pdi.name == NULL)
18292 complaint (_("malformed enumerator DIE ignored"));
18293 else if (building_psymtab)
18294 add_psymbol_to_list (pdi.name, false,
18295 VAR_DOMAIN, LOC_CONST, -1,
18296 cu->language == language_cplus
18297 ? psymbol_placement::GLOBAL
18298 : psymbol_placement::STATIC,
18299 0, cu->language, objfile);
18300
18301 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18302 continue;
18303 }
18304
18305 struct partial_die_info *part_die
18306 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18307
18308 /* We'll save this DIE so link it in. */
18309 part_die->die_parent = parent_die;
18310 part_die->die_sibling = NULL;
18311 part_die->die_child = NULL;
18312
18313 if (last_die && last_die == parent_die)
18314 last_die->die_child = part_die;
18315 else if (last_die)
18316 last_die->die_sibling = part_die;
18317
18318 last_die = part_die;
18319
18320 if (first_die == NULL)
18321 first_die = part_die;
18322
18323 /* Maybe add the DIE to the hash table. Not all DIEs that we
18324 find interesting need to be in the hash table, because we
18325 also have the parent/sibling/child chains; only those that we
18326 might refer to by offset later during partial symbol reading.
18327
18328 For now this means things that might have be the target of a
18329 DW_AT_specification, DW_AT_abstract_origin, or
18330 DW_AT_extension. DW_AT_extension will refer only to
18331 namespaces; DW_AT_abstract_origin refers to functions (and
18332 many things under the function DIE, but we do not recurse
18333 into function DIEs during partial symbol reading) and
18334 possibly variables as well; DW_AT_specification refers to
18335 declarations. Declarations ought to have the DW_AT_declaration
18336 flag. It happens that GCC forgets to put it in sometimes, but
18337 only for functions, not for types.
18338
18339 Adding more things than necessary to the hash table is harmless
18340 except for the performance cost. Adding too few will result in
18341 wasted time in find_partial_die, when we reread the compilation
18342 unit with load_all_dies set. */
18343
18344 if (load_all
18345 || abbrev->tag == DW_TAG_constant
18346 || abbrev->tag == DW_TAG_subprogram
18347 || abbrev->tag == DW_TAG_variable
18348 || abbrev->tag == DW_TAG_namespace
18349 || part_die->is_declaration)
18350 {
18351 void **slot;
18352
18353 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18354 to_underlying (part_die->sect_off),
18355 INSERT);
18356 *slot = part_die;
18357 }
18358
18359 /* For some DIEs we want to follow their children (if any). For C
18360 we have no reason to follow the children of structures; for other
18361 languages we have to, so that we can get at method physnames
18362 to infer fully qualified class names, for DW_AT_specification,
18363 and for C++ template arguments. For C++, we also look one level
18364 inside functions to find template arguments (if the name of the
18365 function does not already contain the template arguments).
18366
18367 For Ada and Fortran, we need to scan the children of subprograms
18368 and lexical blocks as well because these languages allow the
18369 definition of nested entities that could be interesting for the
18370 debugger, such as nested subprograms for instance. */
18371 if (last_die->has_children
18372 && (load_all
18373 || last_die->tag == DW_TAG_namespace
18374 || last_die->tag == DW_TAG_module
18375 || last_die->tag == DW_TAG_enumeration_type
18376 || (cu->language == language_cplus
18377 && last_die->tag == DW_TAG_subprogram
18378 && (last_die->name == NULL
18379 || strchr (last_die->name, '<') == NULL))
18380 || (cu->language != language_c
18381 && (last_die->tag == DW_TAG_class_type
18382 || last_die->tag == DW_TAG_interface_type
18383 || last_die->tag == DW_TAG_structure_type
18384 || last_die->tag == DW_TAG_union_type))
18385 || ((cu->language == language_ada
18386 || cu->language == language_fortran)
18387 && (last_die->tag == DW_TAG_subprogram
18388 || last_die->tag == DW_TAG_lexical_block))))
18389 {
18390 nesting_level++;
18391 parent_die = last_die;
18392 continue;
18393 }
18394
18395 /* Otherwise we skip to the next sibling, if any. */
18396 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18397
18398 /* Back to the top, do it again. */
18399 }
18400 }
18401
18402 partial_die_info::partial_die_info (sect_offset sect_off_,
18403 struct abbrev_info *abbrev)
18404 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18405 {
18406 }
18407
18408 /* Read a minimal amount of information into the minimal die structure.
18409 INFO_PTR should point just after the initial uleb128 of a DIE. */
18410
18411 const gdb_byte *
18412 partial_die_info::read (const struct die_reader_specs *reader,
18413 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18414 {
18415 struct dwarf2_cu *cu = reader->cu;
18416 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18417 unsigned int i;
18418 int has_low_pc_attr = 0;
18419 int has_high_pc_attr = 0;
18420 int high_pc_relative = 0;
18421
18422 for (i = 0; i < abbrev.num_attrs; ++i)
18423 {
18424 attribute attr;
18425 bool need_reprocess;
18426 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18427 info_ptr, &need_reprocess);
18428 /* String and address offsets that need to do the reprocessing have
18429 already been read at this point, so there is no need to wait until
18430 the loop terminates to do the reprocessing. */
18431 if (need_reprocess)
18432 read_attribute_reprocess (reader, &attr);
18433 /* Store the data if it is of an attribute we want to keep in a
18434 partial symbol table. */
18435 switch (attr.name)
18436 {
18437 case DW_AT_name:
18438 switch (tag)
18439 {
18440 case DW_TAG_compile_unit:
18441 case DW_TAG_partial_unit:
18442 case DW_TAG_type_unit:
18443 /* Compilation units have a DW_AT_name that is a filename, not
18444 a source language identifier. */
18445 case DW_TAG_enumeration_type:
18446 case DW_TAG_enumerator:
18447 /* These tags always have simple identifiers already; no need
18448 to canonicalize them. */
18449 name = DW_STRING (&attr);
18450 break;
18451 default:
18452 {
18453 struct objfile *objfile = dwarf2_per_objfile->objfile;
18454
18455 name
18456 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18457 }
18458 break;
18459 }
18460 break;
18461 case DW_AT_linkage_name:
18462 case DW_AT_MIPS_linkage_name:
18463 /* Note that both forms of linkage name might appear. We
18464 assume they will be the same, and we only store the last
18465 one we see. */
18466 linkage_name = attr.value_as_string ();
18467 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18468 See https://github.com/rust-lang/rust/issues/32925. */
18469 if (cu->language == language_rust && linkage_name != NULL
18470 && strchr (linkage_name, '{') != NULL)
18471 linkage_name = NULL;
18472 break;
18473 case DW_AT_low_pc:
18474 has_low_pc_attr = 1;
18475 lowpc = attr.value_as_address ();
18476 break;
18477 case DW_AT_high_pc:
18478 has_high_pc_attr = 1;
18479 highpc = attr.value_as_address ();
18480 if (cu->header.version >= 4 && attr.form_is_constant ())
18481 high_pc_relative = 1;
18482 break;
18483 case DW_AT_location:
18484 /* Support the .debug_loc offsets. */
18485 if (attr.form_is_block ())
18486 {
18487 d.locdesc = DW_BLOCK (&attr);
18488 }
18489 else if (attr.form_is_section_offset ())
18490 {
18491 dwarf2_complex_location_expr_complaint ();
18492 }
18493 else
18494 {
18495 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18496 "partial symbol information");
18497 }
18498 break;
18499 case DW_AT_external:
18500 is_external = DW_UNSND (&attr);
18501 break;
18502 case DW_AT_declaration:
18503 is_declaration = DW_UNSND (&attr);
18504 break;
18505 case DW_AT_type:
18506 has_type = 1;
18507 break;
18508 case DW_AT_abstract_origin:
18509 case DW_AT_specification:
18510 case DW_AT_extension:
18511 has_specification = 1;
18512 spec_offset = attr.get_ref_die_offset ();
18513 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18514 || cu->per_cu->is_dwz);
18515 break;
18516 case DW_AT_sibling:
18517 /* Ignore absolute siblings, they might point outside of
18518 the current compile unit. */
18519 if (attr.form == DW_FORM_ref_addr)
18520 complaint (_("ignoring absolute DW_AT_sibling"));
18521 else
18522 {
18523 const gdb_byte *buffer = reader->buffer;
18524 sect_offset off = attr.get_ref_die_offset ();
18525 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18526
18527 if (sibling_ptr < info_ptr)
18528 complaint (_("DW_AT_sibling points backwards"));
18529 else if (sibling_ptr > reader->buffer_end)
18530 reader->die_section->overflow_complaint ();
18531 else
18532 sibling = sibling_ptr;
18533 }
18534 break;
18535 case DW_AT_byte_size:
18536 has_byte_size = 1;
18537 break;
18538 case DW_AT_const_value:
18539 has_const_value = 1;
18540 break;
18541 case DW_AT_calling_convention:
18542 /* DWARF doesn't provide a way to identify a program's source-level
18543 entry point. DW_AT_calling_convention attributes are only meant
18544 to describe functions' calling conventions.
18545
18546 However, because it's a necessary piece of information in
18547 Fortran, and before DWARF 4 DW_CC_program was the only
18548 piece of debugging information whose definition refers to
18549 a 'main program' at all, several compilers marked Fortran
18550 main programs with DW_CC_program --- even when those
18551 functions use the standard calling conventions.
18552
18553 Although DWARF now specifies a way to provide this
18554 information, we support this practice for backward
18555 compatibility. */
18556 if (DW_UNSND (&attr) == DW_CC_program
18557 && cu->language == language_fortran)
18558 main_subprogram = 1;
18559 break;
18560 case DW_AT_inline:
18561 if (DW_UNSND (&attr) == DW_INL_inlined
18562 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18563 may_be_inlined = 1;
18564 break;
18565
18566 case DW_AT_import:
18567 if (tag == DW_TAG_imported_unit)
18568 {
18569 d.sect_off = attr.get_ref_die_offset ();
18570 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18571 || cu->per_cu->is_dwz);
18572 }
18573 break;
18574
18575 case DW_AT_main_subprogram:
18576 main_subprogram = DW_UNSND (&attr);
18577 break;
18578
18579 case DW_AT_ranges:
18580 {
18581 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18582 but that requires a full DIE, so instead we just
18583 reimplement it. */
18584 int need_ranges_base = tag != DW_TAG_compile_unit;
18585 unsigned int ranges_offset = (DW_UNSND (&attr)
18586 + (need_ranges_base
18587 ? cu->ranges_base
18588 : 0));
18589
18590 /* Value of the DW_AT_ranges attribute is the offset in the
18591 .debug_ranges section. */
18592 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18593 nullptr))
18594 has_pc_info = 1;
18595 }
18596 break;
18597
18598 default:
18599 break;
18600 }
18601 }
18602
18603 /* For Ada, if both the name and the linkage name appear, we prefer
18604 the latter. This lets "catch exception" work better, regardless
18605 of the order in which the name and linkage name were emitted.
18606 Really, though, this is just a workaround for the fact that gdb
18607 doesn't store both the name and the linkage name. */
18608 if (cu->language == language_ada && linkage_name != nullptr)
18609 name = linkage_name;
18610
18611 if (high_pc_relative)
18612 highpc += lowpc;
18613
18614 if (has_low_pc_attr && has_high_pc_attr)
18615 {
18616 /* When using the GNU linker, .gnu.linkonce. sections are used to
18617 eliminate duplicate copies of functions and vtables and such.
18618 The linker will arbitrarily choose one and discard the others.
18619 The AT_*_pc values for such functions refer to local labels in
18620 these sections. If the section from that file was discarded, the
18621 labels are not in the output, so the relocs get a value of 0.
18622 If this is a discarded function, mark the pc bounds as invalid,
18623 so that GDB will ignore it. */
18624 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18625 {
18626 struct objfile *objfile = dwarf2_per_objfile->objfile;
18627 struct gdbarch *gdbarch = objfile->arch ();
18628
18629 complaint (_("DW_AT_low_pc %s is zero "
18630 "for DIE at %s [in module %s]"),
18631 paddress (gdbarch, lowpc),
18632 sect_offset_str (sect_off),
18633 objfile_name (objfile));
18634 }
18635 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18636 else if (lowpc >= highpc)
18637 {
18638 struct objfile *objfile = dwarf2_per_objfile->objfile;
18639 struct gdbarch *gdbarch = objfile->arch ();
18640
18641 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18642 "for DIE at %s [in module %s]"),
18643 paddress (gdbarch, lowpc),
18644 paddress (gdbarch, highpc),
18645 sect_offset_str (sect_off),
18646 objfile_name (objfile));
18647 }
18648 else
18649 has_pc_info = 1;
18650 }
18651
18652 return info_ptr;
18653 }
18654
18655 /* Find a cached partial DIE at OFFSET in CU. */
18656
18657 struct partial_die_info *
18658 dwarf2_cu::find_partial_die (sect_offset sect_off)
18659 {
18660 struct partial_die_info *lookup_die = NULL;
18661 struct partial_die_info part_die (sect_off);
18662
18663 lookup_die = ((struct partial_die_info *)
18664 htab_find_with_hash (partial_dies, &part_die,
18665 to_underlying (sect_off)));
18666
18667 return lookup_die;
18668 }
18669
18670 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18671 except in the case of .debug_types DIEs which do not reference
18672 outside their CU (they do however referencing other types via
18673 DW_FORM_ref_sig8). */
18674
18675 static const struct cu_partial_die_info
18676 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18677 {
18678 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18679 struct objfile *objfile = dwarf2_per_objfile->objfile;
18680 struct dwarf2_per_cu_data *per_cu = NULL;
18681 struct partial_die_info *pd = NULL;
18682
18683 if (offset_in_dwz == cu->per_cu->is_dwz
18684 && cu->header.offset_in_cu_p (sect_off))
18685 {
18686 pd = cu->find_partial_die (sect_off);
18687 if (pd != NULL)
18688 return { cu, pd };
18689 /* We missed recording what we needed.
18690 Load all dies and try again. */
18691 per_cu = cu->per_cu;
18692 }
18693 else
18694 {
18695 /* TUs don't reference other CUs/TUs (except via type signatures). */
18696 if (cu->per_cu->is_debug_types)
18697 {
18698 error (_("Dwarf Error: Type Unit at offset %s contains"
18699 " external reference to offset %s [in module %s].\n"),
18700 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18701 bfd_get_filename (objfile->obfd));
18702 }
18703 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18704 dwarf2_per_objfile);
18705
18706 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18707 load_partial_comp_unit (per_cu, cu->per_objfile);
18708
18709 per_cu->cu->last_used = 0;
18710 pd = per_cu->cu->find_partial_die (sect_off);
18711 }
18712
18713 /* If we didn't find it, and not all dies have been loaded,
18714 load them all and try again. */
18715
18716 if (pd == NULL && per_cu->load_all_dies == 0)
18717 {
18718 per_cu->load_all_dies = 1;
18719
18720 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18721 THIS_CU->cu may already be in use. So we can't just free it and
18722 replace its DIEs with the ones we read in. Instead, we leave those
18723 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18724 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18725 set. */
18726 load_partial_comp_unit (per_cu, cu->per_objfile);
18727
18728 pd = per_cu->cu->find_partial_die (sect_off);
18729 }
18730
18731 if (pd == NULL)
18732 internal_error (__FILE__, __LINE__,
18733 _("could not find partial DIE %s "
18734 "in cache [from module %s]\n"),
18735 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18736 return { per_cu->cu, pd };
18737 }
18738
18739 /* See if we can figure out if the class lives in a namespace. We do
18740 this by looking for a member function; its demangled name will
18741 contain namespace info, if there is any. */
18742
18743 static void
18744 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18745 struct dwarf2_cu *cu)
18746 {
18747 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18748 what template types look like, because the demangler
18749 frequently doesn't give the same name as the debug info. We
18750 could fix this by only using the demangled name to get the
18751 prefix (but see comment in read_structure_type). */
18752
18753 struct partial_die_info *real_pdi;
18754 struct partial_die_info *child_pdi;
18755
18756 /* If this DIE (this DIE's specification, if any) has a parent, then
18757 we should not do this. We'll prepend the parent's fully qualified
18758 name when we create the partial symbol. */
18759
18760 real_pdi = struct_pdi;
18761 while (real_pdi->has_specification)
18762 {
18763 auto res = find_partial_die (real_pdi->spec_offset,
18764 real_pdi->spec_is_dwz, cu);
18765 real_pdi = res.pdi;
18766 cu = res.cu;
18767 }
18768
18769 if (real_pdi->die_parent != NULL)
18770 return;
18771
18772 for (child_pdi = struct_pdi->die_child;
18773 child_pdi != NULL;
18774 child_pdi = child_pdi->die_sibling)
18775 {
18776 if (child_pdi->tag == DW_TAG_subprogram
18777 && child_pdi->linkage_name != NULL)
18778 {
18779 gdb::unique_xmalloc_ptr<char> actual_class_name
18780 (language_class_name_from_physname (cu->language_defn,
18781 child_pdi->linkage_name));
18782 if (actual_class_name != NULL)
18783 {
18784 struct objfile *objfile = cu->per_objfile->objfile;
18785 struct_pdi->name = objfile->intern (actual_class_name.get ());
18786 }
18787 break;
18788 }
18789 }
18790 }
18791
18792 /* Return true if a DIE with TAG may have the DW_AT_const_value
18793 attribute. */
18794
18795 static bool
18796 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18797 {
18798 switch (tag)
18799 {
18800 case DW_TAG_constant:
18801 case DW_TAG_enumerator:
18802 case DW_TAG_formal_parameter:
18803 case DW_TAG_template_value_param:
18804 case DW_TAG_variable:
18805 return true;
18806 }
18807
18808 return false;
18809 }
18810
18811 void
18812 partial_die_info::fixup (struct dwarf2_cu *cu)
18813 {
18814 /* Once we've fixed up a die, there's no point in doing so again.
18815 This also avoids a memory leak if we were to call
18816 guess_partial_die_structure_name multiple times. */
18817 if (fixup_called)
18818 return;
18819
18820 /* If we found a reference attribute and the DIE has no name, try
18821 to find a name in the referred to DIE. */
18822
18823 if (name == NULL && has_specification)
18824 {
18825 struct partial_die_info *spec_die;
18826
18827 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18828 spec_die = res.pdi;
18829 cu = res.cu;
18830
18831 spec_die->fixup (cu);
18832
18833 if (spec_die->name)
18834 {
18835 name = spec_die->name;
18836
18837 /* Copy DW_AT_external attribute if it is set. */
18838 if (spec_die->is_external)
18839 is_external = spec_die->is_external;
18840 }
18841 }
18842
18843 if (!has_const_value && has_specification
18844 && can_have_DW_AT_const_value_p (tag))
18845 {
18846 struct partial_die_info *spec_die;
18847
18848 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18849 spec_die = res.pdi;
18850 cu = res.cu;
18851
18852 spec_die->fixup (cu);
18853
18854 if (spec_die->has_const_value)
18855 {
18856 /* Copy DW_AT_const_value attribute if it is set. */
18857 has_const_value = spec_die->has_const_value;
18858 }
18859 }
18860
18861 /* Set default names for some unnamed DIEs. */
18862
18863 if (name == NULL && tag == DW_TAG_namespace)
18864 name = CP_ANONYMOUS_NAMESPACE_STR;
18865
18866 /* If there is no parent die to provide a namespace, and there are
18867 children, see if we can determine the namespace from their linkage
18868 name. */
18869 if (cu->language == language_cplus
18870 && !cu->per_objfile->per_bfd->types.empty ()
18871 && die_parent == NULL
18872 && has_children
18873 && (tag == DW_TAG_class_type
18874 || tag == DW_TAG_structure_type
18875 || tag == DW_TAG_union_type))
18876 guess_partial_die_structure_name (this, cu);
18877
18878 /* GCC might emit a nameless struct or union that has a linkage
18879 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18880 if (name == NULL
18881 && (tag == DW_TAG_class_type
18882 || tag == DW_TAG_interface_type
18883 || tag == DW_TAG_structure_type
18884 || tag == DW_TAG_union_type)
18885 && linkage_name != NULL)
18886 {
18887 gdb::unique_xmalloc_ptr<char> demangled
18888 (gdb_demangle (linkage_name, DMGL_TYPES));
18889 if (demangled != nullptr)
18890 {
18891 const char *base;
18892
18893 /* Strip any leading namespaces/classes, keep only the base name.
18894 DW_AT_name for named DIEs does not contain the prefixes. */
18895 base = strrchr (demangled.get (), ':');
18896 if (base && base > demangled.get () && base[-1] == ':')
18897 base++;
18898 else
18899 base = demangled.get ();
18900
18901 struct objfile *objfile = cu->per_objfile->objfile;
18902 name = objfile->intern (base);
18903 }
18904 }
18905
18906 fixup_called = 1;
18907 }
18908
18909 /* Read the .debug_loclists header contents from the given SECTION in the
18910 HEADER. */
18911 static void
18912 read_loclist_header (struct loclist_header *header,
18913 struct dwarf2_section_info *section)
18914 {
18915 unsigned int bytes_read;
18916 bfd *abfd = section->get_bfd_owner ();
18917 const gdb_byte *info_ptr = section->buffer;
18918 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18919 info_ptr += bytes_read;
18920 header->version = read_2_bytes (abfd, info_ptr);
18921 info_ptr += 2;
18922 header->addr_size = read_1_byte (abfd, info_ptr);
18923 info_ptr += 1;
18924 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18925 info_ptr += 1;
18926 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18927 }
18928
18929 /* Return the DW_AT_loclists_base value for the CU. */
18930 static ULONGEST
18931 lookup_loclist_base (struct dwarf2_cu *cu)
18932 {
18933 /* For the .dwo unit, the loclist_base points to the first offset following
18934 the header. The header consists of the following entities-
18935 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18936 bit format)
18937 2. version (2 bytes)
18938 3. address size (1 byte)
18939 4. segment selector size (1 byte)
18940 5. offset entry count (4 bytes)
18941 These sizes are derived as per the DWARFv5 standard. */
18942 if (cu->dwo_unit != nullptr)
18943 {
18944 if (cu->header.initial_length_size == 4)
18945 return LOCLIST_HEADER_SIZE32;
18946 return LOCLIST_HEADER_SIZE64;
18947 }
18948 return cu->loclist_base;
18949 }
18950
18951 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18952 array of offsets in the .debug_loclists section. */
18953 static CORE_ADDR
18954 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18955 {
18956 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18957 struct objfile *objfile = dwarf2_per_objfile->objfile;
18958 bfd *abfd = objfile->obfd;
18959 ULONGEST loclist_base = lookup_loclist_base (cu);
18960 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18961
18962 section->read (objfile);
18963 if (section->buffer == NULL)
18964 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18965 "section [in module %s]"), objfile_name (objfile));
18966 struct loclist_header header;
18967 read_loclist_header (&header, section);
18968 if (loclist_index >= header.offset_entry_count)
18969 complaint (_("DW_FORM_loclistx pointing outside of "
18970 ".debug_loclists offset array [in module %s]"),
18971 objfile_name (objfile));
18972 if (loclist_base + loclist_index * cu->header.offset_size
18973 >= section->size)
18974 complaint (_("DW_FORM_loclistx pointing outside of "
18975 ".debug_loclists section [in module %s]"),
18976 objfile_name (objfile));
18977 const gdb_byte *info_ptr
18978 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18979
18980 if (cu->header.offset_size == 4)
18981 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18982 else
18983 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18984 }
18985
18986 /* Process the attributes that had to be skipped in the first round. These
18987 attributes are the ones that need str_offsets_base or addr_base attributes.
18988 They could not have been processed in the first round, because at the time
18989 the values of str_offsets_base or addr_base may not have been known. */
18990 static void
18991 read_attribute_reprocess (const struct die_reader_specs *reader,
18992 struct attribute *attr)
18993 {
18994 struct dwarf2_cu *cu = reader->cu;
18995 switch (attr->form)
18996 {
18997 case DW_FORM_addrx:
18998 case DW_FORM_GNU_addr_index:
18999 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19000 break;
19001 case DW_FORM_loclistx:
19002 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19003 break;
19004 case DW_FORM_strx:
19005 case DW_FORM_strx1:
19006 case DW_FORM_strx2:
19007 case DW_FORM_strx3:
19008 case DW_FORM_strx4:
19009 case DW_FORM_GNU_str_index:
19010 {
19011 unsigned int str_index = DW_UNSND (attr);
19012 if (reader->dwo_file != NULL)
19013 {
19014 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19015 DW_STRING_IS_CANONICAL (attr) = 0;
19016 }
19017 else
19018 {
19019 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19020 DW_STRING_IS_CANONICAL (attr) = 0;
19021 }
19022 break;
19023 }
19024 default:
19025 gdb_assert_not_reached (_("Unexpected DWARF form."));
19026 }
19027 }
19028
19029 /* Read an attribute value described by an attribute form. */
19030
19031 static const gdb_byte *
19032 read_attribute_value (const struct die_reader_specs *reader,
19033 struct attribute *attr, unsigned form,
19034 LONGEST implicit_const, const gdb_byte *info_ptr,
19035 bool *need_reprocess)
19036 {
19037 struct dwarf2_cu *cu = reader->cu;
19038 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19039 struct objfile *objfile = dwarf2_per_objfile->objfile;
19040 bfd *abfd = reader->abfd;
19041 struct comp_unit_head *cu_header = &cu->header;
19042 unsigned int bytes_read;
19043 struct dwarf_block *blk;
19044 *need_reprocess = false;
19045
19046 attr->form = (enum dwarf_form) form;
19047 switch (form)
19048 {
19049 case DW_FORM_ref_addr:
19050 if (cu->header.version == 2)
19051 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19052 &bytes_read);
19053 else
19054 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19055 &bytes_read);
19056 info_ptr += bytes_read;
19057 break;
19058 case DW_FORM_GNU_ref_alt:
19059 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19060 info_ptr += bytes_read;
19061 break;
19062 case DW_FORM_addr:
19063 {
19064 struct gdbarch *gdbarch = objfile->arch ();
19065 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19066 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19067 info_ptr += bytes_read;
19068 }
19069 break;
19070 case DW_FORM_block2:
19071 blk = dwarf_alloc_block (cu);
19072 blk->size = read_2_bytes (abfd, info_ptr);
19073 info_ptr += 2;
19074 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19075 info_ptr += blk->size;
19076 DW_BLOCK (attr) = blk;
19077 break;
19078 case DW_FORM_block4:
19079 blk = dwarf_alloc_block (cu);
19080 blk->size = read_4_bytes (abfd, info_ptr);
19081 info_ptr += 4;
19082 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19083 info_ptr += blk->size;
19084 DW_BLOCK (attr) = blk;
19085 break;
19086 case DW_FORM_data2:
19087 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19088 info_ptr += 2;
19089 break;
19090 case DW_FORM_data4:
19091 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19092 info_ptr += 4;
19093 break;
19094 case DW_FORM_data8:
19095 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19096 info_ptr += 8;
19097 break;
19098 case DW_FORM_data16:
19099 blk = dwarf_alloc_block (cu);
19100 blk->size = 16;
19101 blk->data = read_n_bytes (abfd, info_ptr, 16);
19102 info_ptr += 16;
19103 DW_BLOCK (attr) = blk;
19104 break;
19105 case DW_FORM_sec_offset:
19106 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19107 info_ptr += bytes_read;
19108 break;
19109 case DW_FORM_loclistx:
19110 {
19111 *need_reprocess = true;
19112 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19113 info_ptr += bytes_read;
19114 }
19115 break;
19116 case DW_FORM_string:
19117 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19118 DW_STRING_IS_CANONICAL (attr) = 0;
19119 info_ptr += bytes_read;
19120 break;
19121 case DW_FORM_strp:
19122 if (!cu->per_cu->is_dwz)
19123 {
19124 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19125 abfd, info_ptr, cu_header,
19126 &bytes_read);
19127 DW_STRING_IS_CANONICAL (attr) = 0;
19128 info_ptr += bytes_read;
19129 break;
19130 }
19131 /* FALLTHROUGH */
19132 case DW_FORM_line_strp:
19133 if (!cu->per_cu->is_dwz)
19134 {
19135 DW_STRING (attr)
19136 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19137 &bytes_read);
19138 DW_STRING_IS_CANONICAL (attr) = 0;
19139 info_ptr += bytes_read;
19140 break;
19141 }
19142 /* FALLTHROUGH */
19143 case DW_FORM_GNU_strp_alt:
19144 {
19145 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19146 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19147 &bytes_read);
19148
19149 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19150 DW_STRING_IS_CANONICAL (attr) = 0;
19151 info_ptr += bytes_read;
19152 }
19153 break;
19154 case DW_FORM_exprloc:
19155 case DW_FORM_block:
19156 blk = dwarf_alloc_block (cu);
19157 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19158 info_ptr += bytes_read;
19159 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19160 info_ptr += blk->size;
19161 DW_BLOCK (attr) = blk;
19162 break;
19163 case DW_FORM_block1:
19164 blk = dwarf_alloc_block (cu);
19165 blk->size = read_1_byte (abfd, info_ptr);
19166 info_ptr += 1;
19167 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19168 info_ptr += blk->size;
19169 DW_BLOCK (attr) = blk;
19170 break;
19171 case DW_FORM_data1:
19172 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19173 info_ptr += 1;
19174 break;
19175 case DW_FORM_flag:
19176 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19177 info_ptr += 1;
19178 break;
19179 case DW_FORM_flag_present:
19180 DW_UNSND (attr) = 1;
19181 break;
19182 case DW_FORM_sdata:
19183 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19184 info_ptr += bytes_read;
19185 break;
19186 case DW_FORM_udata:
19187 case DW_FORM_rnglistx:
19188 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19189 info_ptr += bytes_read;
19190 break;
19191 case DW_FORM_ref1:
19192 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19193 + read_1_byte (abfd, info_ptr));
19194 info_ptr += 1;
19195 break;
19196 case DW_FORM_ref2:
19197 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19198 + read_2_bytes (abfd, info_ptr));
19199 info_ptr += 2;
19200 break;
19201 case DW_FORM_ref4:
19202 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19203 + read_4_bytes (abfd, info_ptr));
19204 info_ptr += 4;
19205 break;
19206 case DW_FORM_ref8:
19207 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19208 + read_8_bytes (abfd, info_ptr));
19209 info_ptr += 8;
19210 break;
19211 case DW_FORM_ref_sig8:
19212 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19213 info_ptr += 8;
19214 break;
19215 case DW_FORM_ref_udata:
19216 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19217 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19218 info_ptr += bytes_read;
19219 break;
19220 case DW_FORM_indirect:
19221 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19222 info_ptr += bytes_read;
19223 if (form == DW_FORM_implicit_const)
19224 {
19225 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19226 info_ptr += bytes_read;
19227 }
19228 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19229 info_ptr, need_reprocess);
19230 break;
19231 case DW_FORM_implicit_const:
19232 DW_SND (attr) = implicit_const;
19233 break;
19234 case DW_FORM_addrx:
19235 case DW_FORM_GNU_addr_index:
19236 *need_reprocess = true;
19237 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19238 info_ptr += bytes_read;
19239 break;
19240 case DW_FORM_strx:
19241 case DW_FORM_strx1:
19242 case DW_FORM_strx2:
19243 case DW_FORM_strx3:
19244 case DW_FORM_strx4:
19245 case DW_FORM_GNU_str_index:
19246 {
19247 ULONGEST str_index;
19248 if (form == DW_FORM_strx1)
19249 {
19250 str_index = read_1_byte (abfd, info_ptr);
19251 info_ptr += 1;
19252 }
19253 else if (form == DW_FORM_strx2)
19254 {
19255 str_index = read_2_bytes (abfd, info_ptr);
19256 info_ptr += 2;
19257 }
19258 else if (form == DW_FORM_strx3)
19259 {
19260 str_index = read_3_bytes (abfd, info_ptr);
19261 info_ptr += 3;
19262 }
19263 else if (form == DW_FORM_strx4)
19264 {
19265 str_index = read_4_bytes (abfd, info_ptr);
19266 info_ptr += 4;
19267 }
19268 else
19269 {
19270 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19271 info_ptr += bytes_read;
19272 }
19273 *need_reprocess = true;
19274 DW_UNSND (attr) = str_index;
19275 }
19276 break;
19277 default:
19278 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19279 dwarf_form_name (form),
19280 bfd_get_filename (abfd));
19281 }
19282
19283 /* Super hack. */
19284 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19285 attr->form = DW_FORM_GNU_ref_alt;
19286
19287 /* We have seen instances where the compiler tried to emit a byte
19288 size attribute of -1 which ended up being encoded as an unsigned
19289 0xffffffff. Although 0xffffffff is technically a valid size value,
19290 an object of this size seems pretty unlikely so we can relatively
19291 safely treat these cases as if the size attribute was invalid and
19292 treat them as zero by default. */
19293 if (attr->name == DW_AT_byte_size
19294 && form == DW_FORM_data4
19295 && DW_UNSND (attr) >= 0xffffffff)
19296 {
19297 complaint
19298 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19299 hex_string (DW_UNSND (attr)));
19300 DW_UNSND (attr) = 0;
19301 }
19302
19303 return info_ptr;
19304 }
19305
19306 /* Read an attribute described by an abbreviated attribute. */
19307
19308 static const gdb_byte *
19309 read_attribute (const struct die_reader_specs *reader,
19310 struct attribute *attr, struct attr_abbrev *abbrev,
19311 const gdb_byte *info_ptr, bool *need_reprocess)
19312 {
19313 attr->name = abbrev->name;
19314 return read_attribute_value (reader, attr, abbrev->form,
19315 abbrev->implicit_const, info_ptr,
19316 need_reprocess);
19317 }
19318
19319 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19320
19321 static const char *
19322 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19323 LONGEST str_offset)
19324 {
19325 return dwarf2_per_objfile->per_bfd->str.read_string
19326 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19327 }
19328
19329 /* Return pointer to string at .debug_str offset as read from BUF.
19330 BUF is assumed to be in a compilation unit described by CU_HEADER.
19331 Return *BYTES_READ_PTR count of bytes read from BUF. */
19332
19333 static const char *
19334 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19335 const gdb_byte *buf,
19336 const struct comp_unit_head *cu_header,
19337 unsigned int *bytes_read_ptr)
19338 {
19339 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19340
19341 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19342 }
19343
19344 /* See read.h. */
19345
19346 const char *
19347 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19348 const struct comp_unit_head *cu_header,
19349 unsigned int *bytes_read_ptr)
19350 {
19351 bfd *abfd = objfile->obfd;
19352 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19353
19354 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19355 }
19356
19357 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19358 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19359 ADDR_SIZE is the size of addresses from the CU header. */
19360
19361 static CORE_ADDR
19362 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19363 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19364 int addr_size)
19365 {
19366 struct objfile *objfile = dwarf2_per_objfile->objfile;
19367 bfd *abfd = objfile->obfd;
19368 const gdb_byte *info_ptr;
19369 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19370
19371 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19372 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19373 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19374 objfile_name (objfile));
19375 if (addr_base_or_zero + addr_index * addr_size
19376 >= dwarf2_per_objfile->per_bfd->addr.size)
19377 error (_("DW_FORM_addr_index pointing outside of "
19378 ".debug_addr section [in module %s]"),
19379 objfile_name (objfile));
19380 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19381 + addr_base_or_zero + addr_index * addr_size);
19382 if (addr_size == 4)
19383 return bfd_get_32 (abfd, info_ptr);
19384 else
19385 return bfd_get_64 (abfd, info_ptr);
19386 }
19387
19388 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19389
19390 static CORE_ADDR
19391 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19392 {
19393 return read_addr_index_1 (cu->per_objfile, addr_index,
19394 cu->addr_base, cu->header.addr_size);
19395 }
19396
19397 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19398
19399 static CORE_ADDR
19400 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19401 unsigned int *bytes_read)
19402 {
19403 bfd *abfd = cu->per_objfile->objfile->obfd;
19404 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19405
19406 return read_addr_index (cu, addr_index);
19407 }
19408
19409 /* See read.h. */
19410
19411 CORE_ADDR
19412 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19413 dwarf2_per_objfile *dwarf2_per_objfile,
19414 unsigned int addr_index)
19415 {
19416 struct dwarf2_cu *cu = per_cu->cu;
19417 gdb::optional<ULONGEST> addr_base;
19418 int addr_size;
19419
19420 /* We need addr_base and addr_size.
19421 If we don't have PER_CU->cu, we have to get it.
19422 Nasty, but the alternative is storing the needed info in PER_CU,
19423 which at this point doesn't seem justified: it's not clear how frequently
19424 it would get used and it would increase the size of every PER_CU.
19425 Entry points like dwarf2_per_cu_addr_size do a similar thing
19426 so we're not in uncharted territory here.
19427 Alas we need to be a bit more complicated as addr_base is contained
19428 in the DIE.
19429
19430 We don't need to read the entire CU(/TU).
19431 We just need the header and top level die.
19432
19433 IWBN to use the aging mechanism to let us lazily later discard the CU.
19434 For now we skip this optimization. */
19435
19436 if (cu != NULL)
19437 {
19438 addr_base = cu->addr_base;
19439 addr_size = cu->header.addr_size;
19440 }
19441 else
19442 {
19443 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19444 addr_base = reader.cu->addr_base;
19445 addr_size = reader.cu->header.addr_size;
19446 }
19447
19448 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19449 addr_size);
19450 }
19451
19452 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19453 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19454 DWO file. */
19455
19456 static const char *
19457 read_str_index (struct dwarf2_cu *cu,
19458 struct dwarf2_section_info *str_section,
19459 struct dwarf2_section_info *str_offsets_section,
19460 ULONGEST str_offsets_base, ULONGEST str_index)
19461 {
19462 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19463 struct objfile *objfile = dwarf2_per_objfile->objfile;
19464 const char *objf_name = objfile_name (objfile);
19465 bfd *abfd = objfile->obfd;
19466 const gdb_byte *info_ptr;
19467 ULONGEST str_offset;
19468 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19469
19470 str_section->read (objfile);
19471 str_offsets_section->read (objfile);
19472 if (str_section->buffer == NULL)
19473 error (_("%s used without %s section"
19474 " in CU at offset %s [in module %s]"),
19475 form_name, str_section->get_name (),
19476 sect_offset_str (cu->header.sect_off), objf_name);
19477 if (str_offsets_section->buffer == NULL)
19478 error (_("%s used without %s section"
19479 " in CU at offset %s [in module %s]"),
19480 form_name, str_section->get_name (),
19481 sect_offset_str (cu->header.sect_off), objf_name);
19482 info_ptr = (str_offsets_section->buffer
19483 + str_offsets_base
19484 + str_index * cu->header.offset_size);
19485 if (cu->header.offset_size == 4)
19486 str_offset = bfd_get_32 (abfd, info_ptr);
19487 else
19488 str_offset = bfd_get_64 (abfd, info_ptr);
19489 if (str_offset >= str_section->size)
19490 error (_("Offset from %s pointing outside of"
19491 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19492 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19493 return (const char *) (str_section->buffer + str_offset);
19494 }
19495
19496 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19497
19498 static const char *
19499 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19500 {
19501 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19502 ? reader->cu->header.addr_size : 0;
19503 return read_str_index (reader->cu,
19504 &reader->dwo_file->sections.str,
19505 &reader->dwo_file->sections.str_offsets,
19506 str_offsets_base, str_index);
19507 }
19508
19509 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19510
19511 static const char *
19512 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19513 {
19514 struct objfile *objfile = cu->per_objfile->objfile;
19515 const char *objf_name = objfile_name (objfile);
19516 static const char form_name[] = "DW_FORM_GNU_str_index";
19517 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19518
19519 if (!cu->str_offsets_base.has_value ())
19520 error (_("%s used in Fission stub without %s"
19521 " in CU at offset 0x%lx [in module %s]"),
19522 form_name, str_offsets_attr_name,
19523 (long) cu->header.offset_size, objf_name);
19524
19525 return read_str_index (cu,
19526 &cu->per_objfile->per_bfd->str,
19527 &cu->per_objfile->per_bfd->str_offsets,
19528 *cu->str_offsets_base, str_index);
19529 }
19530
19531 /* Return the length of an LEB128 number in BUF. */
19532
19533 static int
19534 leb128_size (const gdb_byte *buf)
19535 {
19536 const gdb_byte *begin = buf;
19537 gdb_byte byte;
19538
19539 while (1)
19540 {
19541 byte = *buf++;
19542 if ((byte & 128) == 0)
19543 return buf - begin;
19544 }
19545 }
19546
19547 static void
19548 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19549 {
19550 switch (lang)
19551 {
19552 case DW_LANG_C89:
19553 case DW_LANG_C99:
19554 case DW_LANG_C11:
19555 case DW_LANG_C:
19556 case DW_LANG_UPC:
19557 cu->language = language_c;
19558 break;
19559 case DW_LANG_Java:
19560 case DW_LANG_C_plus_plus:
19561 case DW_LANG_C_plus_plus_11:
19562 case DW_LANG_C_plus_plus_14:
19563 cu->language = language_cplus;
19564 break;
19565 case DW_LANG_D:
19566 cu->language = language_d;
19567 break;
19568 case DW_LANG_Fortran77:
19569 case DW_LANG_Fortran90:
19570 case DW_LANG_Fortran95:
19571 case DW_LANG_Fortran03:
19572 case DW_LANG_Fortran08:
19573 cu->language = language_fortran;
19574 break;
19575 case DW_LANG_Go:
19576 cu->language = language_go;
19577 break;
19578 case DW_LANG_Mips_Assembler:
19579 cu->language = language_asm;
19580 break;
19581 case DW_LANG_Ada83:
19582 case DW_LANG_Ada95:
19583 cu->language = language_ada;
19584 break;
19585 case DW_LANG_Modula2:
19586 cu->language = language_m2;
19587 break;
19588 case DW_LANG_Pascal83:
19589 cu->language = language_pascal;
19590 break;
19591 case DW_LANG_ObjC:
19592 cu->language = language_objc;
19593 break;
19594 case DW_LANG_Rust:
19595 case DW_LANG_Rust_old:
19596 cu->language = language_rust;
19597 break;
19598 case DW_LANG_Cobol74:
19599 case DW_LANG_Cobol85:
19600 default:
19601 cu->language = language_minimal;
19602 break;
19603 }
19604 cu->language_defn = language_def (cu->language);
19605 }
19606
19607 /* Return the named attribute or NULL if not there. */
19608
19609 static struct attribute *
19610 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19611 {
19612 for (;;)
19613 {
19614 unsigned int i;
19615 struct attribute *spec = NULL;
19616
19617 for (i = 0; i < die->num_attrs; ++i)
19618 {
19619 if (die->attrs[i].name == name)
19620 return &die->attrs[i];
19621 if (die->attrs[i].name == DW_AT_specification
19622 || die->attrs[i].name == DW_AT_abstract_origin)
19623 spec = &die->attrs[i];
19624 }
19625
19626 if (!spec)
19627 break;
19628
19629 die = follow_die_ref (die, spec, &cu);
19630 }
19631
19632 return NULL;
19633 }
19634
19635 /* Return the string associated with a string-typed attribute, or NULL if it
19636 is either not found or is of an incorrect type. */
19637
19638 static const char *
19639 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19640 {
19641 struct attribute *attr;
19642 const char *str = NULL;
19643
19644 attr = dwarf2_attr (die, name, cu);
19645
19646 if (attr != NULL)
19647 {
19648 str = attr->value_as_string ();
19649 if (str == nullptr)
19650 complaint (_("string type expected for attribute %s for "
19651 "DIE at %s in module %s"),
19652 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19653 objfile_name (cu->per_objfile->objfile));
19654 }
19655
19656 return str;
19657 }
19658
19659 /* Return the dwo name or NULL if not present. If present, it is in either
19660 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19661 static const char *
19662 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19663 {
19664 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19665 if (dwo_name == nullptr)
19666 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19667 return dwo_name;
19668 }
19669
19670 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19671 and holds a non-zero value. This function should only be used for
19672 DW_FORM_flag or DW_FORM_flag_present attributes. */
19673
19674 static int
19675 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19676 {
19677 struct attribute *attr = dwarf2_attr (die, name, cu);
19678
19679 return (attr && DW_UNSND (attr));
19680 }
19681
19682 static int
19683 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19684 {
19685 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19686 which value is non-zero. However, we have to be careful with
19687 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19688 (via dwarf2_flag_true_p) follows this attribute. So we may
19689 end up accidently finding a declaration attribute that belongs
19690 to a different DIE referenced by the specification attribute,
19691 even though the given DIE does not have a declaration attribute. */
19692 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19693 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19694 }
19695
19696 /* Return the die giving the specification for DIE, if there is
19697 one. *SPEC_CU is the CU containing DIE on input, and the CU
19698 containing the return value on output. If there is no
19699 specification, but there is an abstract origin, that is
19700 returned. */
19701
19702 static struct die_info *
19703 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19704 {
19705 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19706 *spec_cu);
19707
19708 if (spec_attr == NULL)
19709 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19710
19711 if (spec_attr == NULL)
19712 return NULL;
19713 else
19714 return follow_die_ref (die, spec_attr, spec_cu);
19715 }
19716
19717 /* Stub for free_line_header to match void * callback types. */
19718
19719 static void
19720 free_line_header_voidp (void *arg)
19721 {
19722 struct line_header *lh = (struct line_header *) arg;
19723
19724 delete lh;
19725 }
19726
19727 /* A convenience function to find the proper .debug_line section for a CU. */
19728
19729 static struct dwarf2_section_info *
19730 get_debug_line_section (struct dwarf2_cu *cu)
19731 {
19732 struct dwarf2_section_info *section;
19733 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19734
19735 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19736 DWO file. */
19737 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19738 section = &cu->dwo_unit->dwo_file->sections.line;
19739 else if (cu->per_cu->is_dwz)
19740 {
19741 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19742
19743 section = &dwz->line;
19744 }
19745 else
19746 section = &dwarf2_per_objfile->per_bfd->line;
19747
19748 return section;
19749 }
19750
19751 /* Read the statement program header starting at OFFSET in
19752 .debug_line, or .debug_line.dwo. Return a pointer
19753 to a struct line_header, allocated using xmalloc.
19754 Returns NULL if there is a problem reading the header, e.g., if it
19755 has a version we don't understand.
19756
19757 NOTE: the strings in the include directory and file name tables of
19758 the returned object point into the dwarf line section buffer,
19759 and must not be freed. */
19760
19761 static line_header_up
19762 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19763 {
19764 struct dwarf2_section_info *section;
19765 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19766
19767 section = get_debug_line_section (cu);
19768 section->read (dwarf2_per_objfile->objfile);
19769 if (section->buffer == NULL)
19770 {
19771 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19772 complaint (_("missing .debug_line.dwo section"));
19773 else
19774 complaint (_("missing .debug_line section"));
19775 return 0;
19776 }
19777
19778 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19779 dwarf2_per_objfile, section,
19780 &cu->header);
19781 }
19782
19783 /* Subroutine of dwarf_decode_lines to simplify it.
19784 Return the file name of the psymtab for the given file_entry.
19785 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19786 If space for the result is malloc'd, *NAME_HOLDER will be set.
19787 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19788
19789 static const char *
19790 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19791 const dwarf2_psymtab *pst,
19792 const char *comp_dir,
19793 gdb::unique_xmalloc_ptr<char> *name_holder)
19794 {
19795 const char *include_name = fe.name;
19796 const char *include_name_to_compare = include_name;
19797 const char *pst_filename;
19798 int file_is_pst;
19799
19800 const char *dir_name = fe.include_dir (lh);
19801
19802 gdb::unique_xmalloc_ptr<char> hold_compare;
19803 if (!IS_ABSOLUTE_PATH (include_name)
19804 && (dir_name != NULL || comp_dir != NULL))
19805 {
19806 /* Avoid creating a duplicate psymtab for PST.
19807 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19808 Before we do the comparison, however, we need to account
19809 for DIR_NAME and COMP_DIR.
19810 First prepend dir_name (if non-NULL). If we still don't
19811 have an absolute path prepend comp_dir (if non-NULL).
19812 However, the directory we record in the include-file's
19813 psymtab does not contain COMP_DIR (to match the
19814 corresponding symtab(s)).
19815
19816 Example:
19817
19818 bash$ cd /tmp
19819 bash$ gcc -g ./hello.c
19820 include_name = "hello.c"
19821 dir_name = "."
19822 DW_AT_comp_dir = comp_dir = "/tmp"
19823 DW_AT_name = "./hello.c"
19824
19825 */
19826
19827 if (dir_name != NULL)
19828 {
19829 name_holder->reset (concat (dir_name, SLASH_STRING,
19830 include_name, (char *) NULL));
19831 include_name = name_holder->get ();
19832 include_name_to_compare = include_name;
19833 }
19834 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19835 {
19836 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19837 include_name, (char *) NULL));
19838 include_name_to_compare = hold_compare.get ();
19839 }
19840 }
19841
19842 pst_filename = pst->filename;
19843 gdb::unique_xmalloc_ptr<char> copied_name;
19844 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19845 {
19846 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19847 pst_filename, (char *) NULL));
19848 pst_filename = copied_name.get ();
19849 }
19850
19851 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19852
19853 if (file_is_pst)
19854 return NULL;
19855 return include_name;
19856 }
19857
19858 /* State machine to track the state of the line number program. */
19859
19860 class lnp_state_machine
19861 {
19862 public:
19863 /* Initialize a machine state for the start of a line number
19864 program. */
19865 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19866 bool record_lines_p);
19867
19868 file_entry *current_file ()
19869 {
19870 /* lh->file_names is 0-based, but the file name numbers in the
19871 statement program are 1-based. */
19872 return m_line_header->file_name_at (m_file);
19873 }
19874
19875 /* Record the line in the state machine. END_SEQUENCE is true if
19876 we're processing the end of a sequence. */
19877 void record_line (bool end_sequence);
19878
19879 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19880 nop-out rest of the lines in this sequence. */
19881 void check_line_address (struct dwarf2_cu *cu,
19882 const gdb_byte *line_ptr,
19883 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19884
19885 void handle_set_discriminator (unsigned int discriminator)
19886 {
19887 m_discriminator = discriminator;
19888 m_line_has_non_zero_discriminator |= discriminator != 0;
19889 }
19890
19891 /* Handle DW_LNE_set_address. */
19892 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19893 {
19894 m_op_index = 0;
19895 address += baseaddr;
19896 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19897 }
19898
19899 /* Handle DW_LNS_advance_pc. */
19900 void handle_advance_pc (CORE_ADDR adjust);
19901
19902 /* Handle a special opcode. */
19903 void handle_special_opcode (unsigned char op_code);
19904
19905 /* Handle DW_LNS_advance_line. */
19906 void handle_advance_line (int line_delta)
19907 {
19908 advance_line (line_delta);
19909 }
19910
19911 /* Handle DW_LNS_set_file. */
19912 void handle_set_file (file_name_index file);
19913
19914 /* Handle DW_LNS_negate_stmt. */
19915 void handle_negate_stmt ()
19916 {
19917 m_is_stmt = !m_is_stmt;
19918 }
19919
19920 /* Handle DW_LNS_const_add_pc. */
19921 void handle_const_add_pc ();
19922
19923 /* Handle DW_LNS_fixed_advance_pc. */
19924 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19925 {
19926 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19927 m_op_index = 0;
19928 }
19929
19930 /* Handle DW_LNS_copy. */
19931 void handle_copy ()
19932 {
19933 record_line (false);
19934 m_discriminator = 0;
19935 }
19936
19937 /* Handle DW_LNE_end_sequence. */
19938 void handle_end_sequence ()
19939 {
19940 m_currently_recording_lines = true;
19941 }
19942
19943 private:
19944 /* Advance the line by LINE_DELTA. */
19945 void advance_line (int line_delta)
19946 {
19947 m_line += line_delta;
19948
19949 if (line_delta != 0)
19950 m_line_has_non_zero_discriminator = m_discriminator != 0;
19951 }
19952
19953 struct dwarf2_cu *m_cu;
19954
19955 gdbarch *m_gdbarch;
19956
19957 /* True if we're recording lines.
19958 Otherwise we're building partial symtabs and are just interested in
19959 finding include files mentioned by the line number program. */
19960 bool m_record_lines_p;
19961
19962 /* The line number header. */
19963 line_header *m_line_header;
19964
19965 /* These are part of the standard DWARF line number state machine,
19966 and initialized according to the DWARF spec. */
19967
19968 unsigned char m_op_index = 0;
19969 /* The line table index of the current file. */
19970 file_name_index m_file = 1;
19971 unsigned int m_line = 1;
19972
19973 /* These are initialized in the constructor. */
19974
19975 CORE_ADDR m_address;
19976 bool m_is_stmt;
19977 unsigned int m_discriminator;
19978
19979 /* Additional bits of state we need to track. */
19980
19981 /* The last file that we called dwarf2_start_subfile for.
19982 This is only used for TLLs. */
19983 unsigned int m_last_file = 0;
19984 /* The last file a line number was recorded for. */
19985 struct subfile *m_last_subfile = NULL;
19986
19987 /* When true, record the lines we decode. */
19988 bool m_currently_recording_lines = false;
19989
19990 /* The last line number that was recorded, used to coalesce
19991 consecutive entries for the same line. This can happen, for
19992 example, when discriminators are present. PR 17276. */
19993 unsigned int m_last_line = 0;
19994 bool m_line_has_non_zero_discriminator = false;
19995 };
19996
19997 void
19998 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19999 {
20000 CORE_ADDR addr_adj = (((m_op_index + adjust)
20001 / m_line_header->maximum_ops_per_instruction)
20002 * m_line_header->minimum_instruction_length);
20003 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20004 m_op_index = ((m_op_index + adjust)
20005 % m_line_header->maximum_ops_per_instruction);
20006 }
20007
20008 void
20009 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20010 {
20011 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20012 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20013 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20014 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20015 / m_line_header->maximum_ops_per_instruction)
20016 * m_line_header->minimum_instruction_length);
20017 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20018 m_op_index = ((m_op_index + adj_opcode_d)
20019 % m_line_header->maximum_ops_per_instruction);
20020
20021 int line_delta = m_line_header->line_base + adj_opcode_r;
20022 advance_line (line_delta);
20023 record_line (false);
20024 m_discriminator = 0;
20025 }
20026
20027 void
20028 lnp_state_machine::handle_set_file (file_name_index file)
20029 {
20030 m_file = file;
20031
20032 const file_entry *fe = current_file ();
20033 if (fe == NULL)
20034 dwarf2_debug_line_missing_file_complaint ();
20035 else if (m_record_lines_p)
20036 {
20037 const char *dir = fe->include_dir (m_line_header);
20038
20039 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20040 m_line_has_non_zero_discriminator = m_discriminator != 0;
20041 dwarf2_start_subfile (m_cu, fe->name, dir);
20042 }
20043 }
20044
20045 void
20046 lnp_state_machine::handle_const_add_pc ()
20047 {
20048 CORE_ADDR adjust
20049 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20050
20051 CORE_ADDR addr_adj
20052 = (((m_op_index + adjust)
20053 / m_line_header->maximum_ops_per_instruction)
20054 * m_line_header->minimum_instruction_length);
20055
20056 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20057 m_op_index = ((m_op_index + adjust)
20058 % m_line_header->maximum_ops_per_instruction);
20059 }
20060
20061 /* Return non-zero if we should add LINE to the line number table.
20062 LINE is the line to add, LAST_LINE is the last line that was added,
20063 LAST_SUBFILE is the subfile for LAST_LINE.
20064 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20065 had a non-zero discriminator.
20066
20067 We have to be careful in the presence of discriminators.
20068 E.g., for this line:
20069
20070 for (i = 0; i < 100000; i++);
20071
20072 clang can emit four line number entries for that one line,
20073 each with a different discriminator.
20074 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20075
20076 However, we want gdb to coalesce all four entries into one.
20077 Otherwise the user could stepi into the middle of the line and
20078 gdb would get confused about whether the pc really was in the
20079 middle of the line.
20080
20081 Things are further complicated by the fact that two consecutive
20082 line number entries for the same line is a heuristic used by gcc
20083 to denote the end of the prologue. So we can't just discard duplicate
20084 entries, we have to be selective about it. The heuristic we use is
20085 that we only collapse consecutive entries for the same line if at least
20086 one of those entries has a non-zero discriminator. PR 17276.
20087
20088 Note: Addresses in the line number state machine can never go backwards
20089 within one sequence, thus this coalescing is ok. */
20090
20091 static int
20092 dwarf_record_line_p (struct dwarf2_cu *cu,
20093 unsigned int line, unsigned int last_line,
20094 int line_has_non_zero_discriminator,
20095 struct subfile *last_subfile)
20096 {
20097 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20098 return 1;
20099 if (line != last_line)
20100 return 1;
20101 /* Same line for the same file that we've seen already.
20102 As a last check, for pr 17276, only record the line if the line
20103 has never had a non-zero discriminator. */
20104 if (!line_has_non_zero_discriminator)
20105 return 1;
20106 return 0;
20107 }
20108
20109 /* Use the CU's builder to record line number LINE beginning at
20110 address ADDRESS in the line table of subfile SUBFILE. */
20111
20112 static void
20113 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20114 unsigned int line, CORE_ADDR address, bool is_stmt,
20115 struct dwarf2_cu *cu)
20116 {
20117 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20118
20119 if (dwarf_line_debug)
20120 {
20121 fprintf_unfiltered (gdb_stdlog,
20122 "Recording line %u, file %s, address %s\n",
20123 line, lbasename (subfile->name),
20124 paddress (gdbarch, address));
20125 }
20126
20127 if (cu != nullptr)
20128 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20129 }
20130
20131 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20132 Mark the end of a set of line number records.
20133 The arguments are the same as for dwarf_record_line_1.
20134 If SUBFILE is NULL the request is ignored. */
20135
20136 static void
20137 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20138 CORE_ADDR address, struct dwarf2_cu *cu)
20139 {
20140 if (subfile == NULL)
20141 return;
20142
20143 if (dwarf_line_debug)
20144 {
20145 fprintf_unfiltered (gdb_stdlog,
20146 "Finishing current line, file %s, address %s\n",
20147 lbasename (subfile->name),
20148 paddress (gdbarch, address));
20149 }
20150
20151 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20152 }
20153
20154 void
20155 lnp_state_machine::record_line (bool end_sequence)
20156 {
20157 if (dwarf_line_debug)
20158 {
20159 fprintf_unfiltered (gdb_stdlog,
20160 "Processing actual line %u: file %u,"
20161 " address %s, is_stmt %u, discrim %u%s\n",
20162 m_line, m_file,
20163 paddress (m_gdbarch, m_address),
20164 m_is_stmt, m_discriminator,
20165 (end_sequence ? "\t(end sequence)" : ""));
20166 }
20167
20168 file_entry *fe = current_file ();
20169
20170 if (fe == NULL)
20171 dwarf2_debug_line_missing_file_complaint ();
20172 /* For now we ignore lines not starting on an instruction boundary.
20173 But not when processing end_sequence for compatibility with the
20174 previous version of the code. */
20175 else if (m_op_index == 0 || end_sequence)
20176 {
20177 fe->included_p = 1;
20178 if (m_record_lines_p)
20179 {
20180 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20181 || end_sequence)
20182 {
20183 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20184 m_currently_recording_lines ? m_cu : nullptr);
20185 }
20186
20187 if (!end_sequence)
20188 {
20189 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20190
20191 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20192 m_line_has_non_zero_discriminator,
20193 m_last_subfile))
20194 {
20195 buildsym_compunit *builder = m_cu->get_builder ();
20196 dwarf_record_line_1 (m_gdbarch,
20197 builder->get_current_subfile (),
20198 m_line, m_address, is_stmt,
20199 m_currently_recording_lines ? m_cu : nullptr);
20200 }
20201 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20202 m_last_line = m_line;
20203 }
20204 }
20205 }
20206 }
20207
20208 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20209 line_header *lh, bool record_lines_p)
20210 {
20211 m_cu = cu;
20212 m_gdbarch = arch;
20213 m_record_lines_p = record_lines_p;
20214 m_line_header = lh;
20215
20216 m_currently_recording_lines = true;
20217
20218 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20219 was a line entry for it so that the backend has a chance to adjust it
20220 and also record it in case it needs it. This is currently used by MIPS
20221 code, cf. `mips_adjust_dwarf2_line'. */
20222 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20223 m_is_stmt = lh->default_is_stmt;
20224 m_discriminator = 0;
20225 }
20226
20227 void
20228 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20229 const gdb_byte *line_ptr,
20230 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20231 {
20232 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20233 the pc range of the CU. However, we restrict the test to only ADDRESS
20234 values of zero to preserve GDB's previous behaviour which is to handle
20235 the specific case of a function being GC'd by the linker. */
20236
20237 if (address == 0 && address < unrelocated_lowpc)
20238 {
20239 /* This line table is for a function which has been
20240 GCd by the linker. Ignore it. PR gdb/12528 */
20241
20242 struct objfile *objfile = cu->per_objfile->objfile;
20243 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20244
20245 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20246 line_offset, objfile_name (objfile));
20247 m_currently_recording_lines = false;
20248 /* Note: m_currently_recording_lines is left as false until we see
20249 DW_LNE_end_sequence. */
20250 }
20251 }
20252
20253 /* Subroutine of dwarf_decode_lines to simplify it.
20254 Process the line number information in LH.
20255 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20256 program in order to set included_p for every referenced header. */
20257
20258 static void
20259 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20260 const int decode_for_pst_p, CORE_ADDR lowpc)
20261 {
20262 const gdb_byte *line_ptr, *extended_end;
20263 const gdb_byte *line_end;
20264 unsigned int bytes_read, extended_len;
20265 unsigned char op_code, extended_op;
20266 CORE_ADDR baseaddr;
20267 struct objfile *objfile = cu->per_objfile->objfile;
20268 bfd *abfd = objfile->obfd;
20269 struct gdbarch *gdbarch = objfile->arch ();
20270 /* True if we're recording line info (as opposed to building partial
20271 symtabs and just interested in finding include files mentioned by
20272 the line number program). */
20273 bool record_lines_p = !decode_for_pst_p;
20274
20275 baseaddr = objfile->text_section_offset ();
20276
20277 line_ptr = lh->statement_program_start;
20278 line_end = lh->statement_program_end;
20279
20280 /* Read the statement sequences until there's nothing left. */
20281 while (line_ptr < line_end)
20282 {
20283 /* The DWARF line number program state machine. Reset the state
20284 machine at the start of each sequence. */
20285 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20286 bool end_sequence = false;
20287
20288 if (record_lines_p)
20289 {
20290 /* Start a subfile for the current file of the state
20291 machine. */
20292 const file_entry *fe = state_machine.current_file ();
20293
20294 if (fe != NULL)
20295 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20296 }
20297
20298 /* Decode the table. */
20299 while (line_ptr < line_end && !end_sequence)
20300 {
20301 op_code = read_1_byte (abfd, line_ptr);
20302 line_ptr += 1;
20303
20304 if (op_code >= lh->opcode_base)
20305 {
20306 /* Special opcode. */
20307 state_machine.handle_special_opcode (op_code);
20308 }
20309 else switch (op_code)
20310 {
20311 case DW_LNS_extended_op:
20312 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20313 &bytes_read);
20314 line_ptr += bytes_read;
20315 extended_end = line_ptr + extended_len;
20316 extended_op = read_1_byte (abfd, line_ptr);
20317 line_ptr += 1;
20318 switch (extended_op)
20319 {
20320 case DW_LNE_end_sequence:
20321 state_machine.handle_end_sequence ();
20322 end_sequence = true;
20323 break;
20324 case DW_LNE_set_address:
20325 {
20326 CORE_ADDR address
20327 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20328 line_ptr += bytes_read;
20329
20330 state_machine.check_line_address (cu, line_ptr,
20331 lowpc - baseaddr, address);
20332 state_machine.handle_set_address (baseaddr, address);
20333 }
20334 break;
20335 case DW_LNE_define_file:
20336 {
20337 const char *cur_file;
20338 unsigned int mod_time, length;
20339 dir_index dindex;
20340
20341 cur_file = read_direct_string (abfd, line_ptr,
20342 &bytes_read);
20343 line_ptr += bytes_read;
20344 dindex = (dir_index)
20345 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20346 line_ptr += bytes_read;
20347 mod_time =
20348 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20349 line_ptr += bytes_read;
20350 length =
20351 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20352 line_ptr += bytes_read;
20353 lh->add_file_name (cur_file, dindex, mod_time, length);
20354 }
20355 break;
20356 case DW_LNE_set_discriminator:
20357 {
20358 /* The discriminator is not interesting to the
20359 debugger; just ignore it. We still need to
20360 check its value though:
20361 if there are consecutive entries for the same
20362 (non-prologue) line we want to coalesce them.
20363 PR 17276. */
20364 unsigned int discr
20365 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20366 line_ptr += bytes_read;
20367
20368 state_machine.handle_set_discriminator (discr);
20369 }
20370 break;
20371 default:
20372 complaint (_("mangled .debug_line section"));
20373 return;
20374 }
20375 /* Make sure that we parsed the extended op correctly. If e.g.
20376 we expected a different address size than the producer used,
20377 we may have read the wrong number of bytes. */
20378 if (line_ptr != extended_end)
20379 {
20380 complaint (_("mangled .debug_line section"));
20381 return;
20382 }
20383 break;
20384 case DW_LNS_copy:
20385 state_machine.handle_copy ();
20386 break;
20387 case DW_LNS_advance_pc:
20388 {
20389 CORE_ADDR adjust
20390 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20391 line_ptr += bytes_read;
20392
20393 state_machine.handle_advance_pc (adjust);
20394 }
20395 break;
20396 case DW_LNS_advance_line:
20397 {
20398 int line_delta
20399 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20400 line_ptr += bytes_read;
20401
20402 state_machine.handle_advance_line (line_delta);
20403 }
20404 break;
20405 case DW_LNS_set_file:
20406 {
20407 file_name_index file
20408 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20409 &bytes_read);
20410 line_ptr += bytes_read;
20411
20412 state_machine.handle_set_file (file);
20413 }
20414 break;
20415 case DW_LNS_set_column:
20416 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20417 line_ptr += bytes_read;
20418 break;
20419 case DW_LNS_negate_stmt:
20420 state_machine.handle_negate_stmt ();
20421 break;
20422 case DW_LNS_set_basic_block:
20423 break;
20424 /* Add to the address register of the state machine the
20425 address increment value corresponding to special opcode
20426 255. I.e., this value is scaled by the minimum
20427 instruction length since special opcode 255 would have
20428 scaled the increment. */
20429 case DW_LNS_const_add_pc:
20430 state_machine.handle_const_add_pc ();
20431 break;
20432 case DW_LNS_fixed_advance_pc:
20433 {
20434 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20435 line_ptr += 2;
20436
20437 state_machine.handle_fixed_advance_pc (addr_adj);
20438 }
20439 break;
20440 default:
20441 {
20442 /* Unknown standard opcode, ignore it. */
20443 int i;
20444
20445 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20446 {
20447 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20448 line_ptr += bytes_read;
20449 }
20450 }
20451 }
20452 }
20453
20454 if (!end_sequence)
20455 dwarf2_debug_line_missing_end_sequence_complaint ();
20456
20457 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20458 in which case we still finish recording the last line). */
20459 state_machine.record_line (true);
20460 }
20461 }
20462
20463 /* Decode the Line Number Program (LNP) for the given line_header
20464 structure and CU. The actual information extracted and the type
20465 of structures created from the LNP depends on the value of PST.
20466
20467 1. If PST is NULL, then this procedure uses the data from the program
20468 to create all necessary symbol tables, and their linetables.
20469
20470 2. If PST is not NULL, this procedure reads the program to determine
20471 the list of files included by the unit represented by PST, and
20472 builds all the associated partial symbol tables.
20473
20474 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20475 It is used for relative paths in the line table.
20476 NOTE: When processing partial symtabs (pst != NULL),
20477 comp_dir == pst->dirname.
20478
20479 NOTE: It is important that psymtabs have the same file name (via strcmp)
20480 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20481 symtab we don't use it in the name of the psymtabs we create.
20482 E.g. expand_line_sal requires this when finding psymtabs to expand.
20483 A good testcase for this is mb-inline.exp.
20484
20485 LOWPC is the lowest address in CU (or 0 if not known).
20486
20487 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20488 for its PC<->lines mapping information. Otherwise only the filename
20489 table is read in. */
20490
20491 static void
20492 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20493 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20494 CORE_ADDR lowpc, int decode_mapping)
20495 {
20496 struct objfile *objfile = cu->per_objfile->objfile;
20497 const int decode_for_pst_p = (pst != NULL);
20498
20499 if (decode_mapping)
20500 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20501
20502 if (decode_for_pst_p)
20503 {
20504 /* Now that we're done scanning the Line Header Program, we can
20505 create the psymtab of each included file. */
20506 for (auto &file_entry : lh->file_names ())
20507 if (file_entry.included_p == 1)
20508 {
20509 gdb::unique_xmalloc_ptr<char> name_holder;
20510 const char *include_name =
20511 psymtab_include_file_name (lh, file_entry, pst,
20512 comp_dir, &name_holder);
20513 if (include_name != NULL)
20514 dwarf2_create_include_psymtab (include_name, pst, objfile);
20515 }
20516 }
20517 else
20518 {
20519 /* Make sure a symtab is created for every file, even files
20520 which contain only variables (i.e. no code with associated
20521 line numbers). */
20522 buildsym_compunit *builder = cu->get_builder ();
20523 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20524
20525 for (auto &fe : lh->file_names ())
20526 {
20527 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20528 if (builder->get_current_subfile ()->symtab == NULL)
20529 {
20530 builder->get_current_subfile ()->symtab
20531 = allocate_symtab (cust,
20532 builder->get_current_subfile ()->name);
20533 }
20534 fe.symtab = builder->get_current_subfile ()->symtab;
20535 }
20536 }
20537 }
20538
20539 /* Start a subfile for DWARF. FILENAME is the name of the file and
20540 DIRNAME the name of the source directory which contains FILENAME
20541 or NULL if not known.
20542 This routine tries to keep line numbers from identical absolute and
20543 relative file names in a common subfile.
20544
20545 Using the `list' example from the GDB testsuite, which resides in
20546 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20547 of /srcdir/list0.c yields the following debugging information for list0.c:
20548
20549 DW_AT_name: /srcdir/list0.c
20550 DW_AT_comp_dir: /compdir
20551 files.files[0].name: list0.h
20552 files.files[0].dir: /srcdir
20553 files.files[1].name: list0.c
20554 files.files[1].dir: /srcdir
20555
20556 The line number information for list0.c has to end up in a single
20557 subfile, so that `break /srcdir/list0.c:1' works as expected.
20558 start_subfile will ensure that this happens provided that we pass the
20559 concatenation of files.files[1].dir and files.files[1].name as the
20560 subfile's name. */
20561
20562 static void
20563 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20564 const char *dirname)
20565 {
20566 gdb::unique_xmalloc_ptr<char> copy;
20567
20568 /* In order not to lose the line information directory,
20569 we concatenate it to the filename when it makes sense.
20570 Note that the Dwarf3 standard says (speaking of filenames in line
20571 information): ``The directory index is ignored for file names
20572 that represent full path names''. Thus ignoring dirname in the
20573 `else' branch below isn't an issue. */
20574
20575 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20576 {
20577 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20578 filename = copy.get ();
20579 }
20580
20581 cu->get_builder ()->start_subfile (filename);
20582 }
20583
20584 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20585 buildsym_compunit constructor. */
20586
20587 struct compunit_symtab *
20588 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20589 CORE_ADDR low_pc)
20590 {
20591 gdb_assert (m_builder == nullptr);
20592
20593 m_builder.reset (new struct buildsym_compunit
20594 (this->per_objfile->objfile,
20595 name, comp_dir, language, low_pc));
20596
20597 list_in_scope = get_builder ()->get_file_symbols ();
20598
20599 get_builder ()->record_debugformat ("DWARF 2");
20600 get_builder ()->record_producer (producer);
20601
20602 processing_has_namespace_info = false;
20603
20604 return get_builder ()->get_compunit_symtab ();
20605 }
20606
20607 static void
20608 var_decode_location (struct attribute *attr, struct symbol *sym,
20609 struct dwarf2_cu *cu)
20610 {
20611 struct objfile *objfile = cu->per_objfile->objfile;
20612 struct comp_unit_head *cu_header = &cu->header;
20613
20614 /* NOTE drow/2003-01-30: There used to be a comment and some special
20615 code here to turn a symbol with DW_AT_external and a
20616 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20617 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20618 with some versions of binutils) where shared libraries could have
20619 relocations against symbols in their debug information - the
20620 minimal symbol would have the right address, but the debug info
20621 would not. It's no longer necessary, because we will explicitly
20622 apply relocations when we read in the debug information now. */
20623
20624 /* A DW_AT_location attribute with no contents indicates that a
20625 variable has been optimized away. */
20626 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20627 {
20628 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20629 return;
20630 }
20631
20632 /* Handle one degenerate form of location expression specially, to
20633 preserve GDB's previous behavior when section offsets are
20634 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20635 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20636
20637 if (attr->form_is_block ()
20638 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20639 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20640 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20641 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20642 && (DW_BLOCK (attr)->size
20643 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20644 {
20645 unsigned int dummy;
20646
20647 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20648 SET_SYMBOL_VALUE_ADDRESS
20649 (sym, cu->header.read_address (objfile->obfd,
20650 DW_BLOCK (attr)->data + 1,
20651 &dummy));
20652 else
20653 SET_SYMBOL_VALUE_ADDRESS
20654 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20655 &dummy));
20656 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20657 fixup_symbol_section (sym, objfile);
20658 SET_SYMBOL_VALUE_ADDRESS
20659 (sym,
20660 SYMBOL_VALUE_ADDRESS (sym)
20661 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20662 return;
20663 }
20664
20665 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20666 expression evaluator, and use LOC_COMPUTED only when necessary
20667 (i.e. when the value of a register or memory location is
20668 referenced, or a thread-local block, etc.). Then again, it might
20669 not be worthwhile. I'm assuming that it isn't unless performance
20670 or memory numbers show me otherwise. */
20671
20672 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20673
20674 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20675 cu->has_loclist = true;
20676 }
20677
20678 /* Given a pointer to a DWARF information entry, figure out if we need
20679 to make a symbol table entry for it, and if so, create a new entry
20680 and return a pointer to it.
20681 If TYPE is NULL, determine symbol type from the die, otherwise
20682 used the passed type.
20683 If SPACE is not NULL, use it to hold the new symbol. If it is
20684 NULL, allocate a new symbol on the objfile's obstack. */
20685
20686 static struct symbol *
20687 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20688 struct symbol *space)
20689 {
20690 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20691 struct objfile *objfile = dwarf2_per_objfile->objfile;
20692 struct gdbarch *gdbarch = objfile->arch ();
20693 struct symbol *sym = NULL;
20694 const char *name;
20695 struct attribute *attr = NULL;
20696 struct attribute *attr2 = NULL;
20697 CORE_ADDR baseaddr;
20698 struct pending **list_to_add = NULL;
20699
20700 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20701
20702 baseaddr = objfile->text_section_offset ();
20703
20704 name = dwarf2_name (die, cu);
20705 if (name)
20706 {
20707 int suppress_add = 0;
20708
20709 if (space)
20710 sym = space;
20711 else
20712 sym = new (&objfile->objfile_obstack) symbol;
20713 OBJSTAT (objfile, n_syms++);
20714
20715 /* Cache this symbol's name and the name's demangled form (if any). */
20716 sym->set_language (cu->language, &objfile->objfile_obstack);
20717 /* Fortran does not have mangling standard and the mangling does differ
20718 between gfortran, iFort etc. */
20719 const char *physname
20720 = (cu->language == language_fortran
20721 ? dwarf2_full_name (name, die, cu)
20722 : dwarf2_physname (name, die, cu));
20723 const char *linkagename = dw2_linkage_name (die, cu);
20724
20725 if (linkagename == nullptr || cu->language == language_ada)
20726 sym->set_linkage_name (physname);
20727 else
20728 {
20729 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20730 sym->set_linkage_name (linkagename);
20731 }
20732
20733 /* Default assumptions.
20734 Use the passed type or decode it from the die. */
20735 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20736 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20737 if (type != NULL)
20738 SYMBOL_TYPE (sym) = type;
20739 else
20740 SYMBOL_TYPE (sym) = die_type (die, cu);
20741 attr = dwarf2_attr (die,
20742 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20743 cu);
20744 if (attr != nullptr)
20745 {
20746 SYMBOL_LINE (sym) = DW_UNSND (attr);
20747 }
20748
20749 attr = dwarf2_attr (die,
20750 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20751 cu);
20752 if (attr != nullptr)
20753 {
20754 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20755 struct file_entry *fe;
20756
20757 if (cu->line_header != NULL)
20758 fe = cu->line_header->file_name_at (file_index);
20759 else
20760 fe = NULL;
20761
20762 if (fe == NULL)
20763 complaint (_("file index out of range"));
20764 else
20765 symbol_set_symtab (sym, fe->symtab);
20766 }
20767
20768 switch (die->tag)
20769 {
20770 case DW_TAG_label:
20771 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20772 if (attr != nullptr)
20773 {
20774 CORE_ADDR addr;
20775
20776 addr = attr->value_as_address ();
20777 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20778 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20779 }
20780 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20781 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20782 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20783 add_symbol_to_list (sym, cu->list_in_scope);
20784 break;
20785 case DW_TAG_subprogram:
20786 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20787 finish_block. */
20788 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20789 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20790 if ((attr2 && (DW_UNSND (attr2) != 0))
20791 || cu->language == language_ada
20792 || cu->language == language_fortran)
20793 {
20794 /* Subprograms marked external are stored as a global symbol.
20795 Ada and Fortran subprograms, whether marked external or
20796 not, are always stored as a global symbol, because we want
20797 to be able to access them globally. For instance, we want
20798 to be able to break on a nested subprogram without having
20799 to specify the context. */
20800 list_to_add = cu->get_builder ()->get_global_symbols ();
20801 }
20802 else
20803 {
20804 list_to_add = cu->list_in_scope;
20805 }
20806 break;
20807 case DW_TAG_inlined_subroutine:
20808 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20809 finish_block. */
20810 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20811 SYMBOL_INLINED (sym) = 1;
20812 list_to_add = cu->list_in_scope;
20813 break;
20814 case DW_TAG_template_value_param:
20815 suppress_add = 1;
20816 /* Fall through. */
20817 case DW_TAG_constant:
20818 case DW_TAG_variable:
20819 case DW_TAG_member:
20820 /* Compilation with minimal debug info may result in
20821 variables with missing type entries. Change the
20822 misleading `void' type to something sensible. */
20823 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20824 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20825
20826 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20827 /* In the case of DW_TAG_member, we should only be called for
20828 static const members. */
20829 if (die->tag == DW_TAG_member)
20830 {
20831 /* dwarf2_add_field uses die_is_declaration,
20832 so we do the same. */
20833 gdb_assert (die_is_declaration (die, cu));
20834 gdb_assert (attr);
20835 }
20836 if (attr != nullptr)
20837 {
20838 dwarf2_const_value (attr, sym, cu);
20839 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20840 if (!suppress_add)
20841 {
20842 if (attr2 && (DW_UNSND (attr2) != 0))
20843 list_to_add = cu->get_builder ()->get_global_symbols ();
20844 else
20845 list_to_add = cu->list_in_scope;
20846 }
20847 break;
20848 }
20849 attr = dwarf2_attr (die, DW_AT_location, cu);
20850 if (attr != nullptr)
20851 {
20852 var_decode_location (attr, sym, cu);
20853 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20854
20855 /* Fortran explicitly imports any global symbols to the local
20856 scope by DW_TAG_common_block. */
20857 if (cu->language == language_fortran && die->parent
20858 && die->parent->tag == DW_TAG_common_block)
20859 attr2 = NULL;
20860
20861 if (SYMBOL_CLASS (sym) == LOC_STATIC
20862 && SYMBOL_VALUE_ADDRESS (sym) == 0
20863 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20864 {
20865 /* When a static variable is eliminated by the linker,
20866 the corresponding debug information is not stripped
20867 out, but the variable address is set to null;
20868 do not add such variables into symbol table. */
20869 }
20870 else if (attr2 && (DW_UNSND (attr2) != 0))
20871 {
20872 if (SYMBOL_CLASS (sym) == LOC_STATIC
20873 && (objfile->flags & OBJF_MAINLINE) == 0
20874 && dwarf2_per_objfile->per_bfd->can_copy)
20875 {
20876 /* A global static variable might be subject to
20877 copy relocation. We first check for a local
20878 minsym, though, because maybe the symbol was
20879 marked hidden, in which case this would not
20880 apply. */
20881 bound_minimal_symbol found
20882 = (lookup_minimal_symbol_linkage
20883 (sym->linkage_name (), objfile));
20884 if (found.minsym != nullptr)
20885 sym->maybe_copied = 1;
20886 }
20887
20888 /* A variable with DW_AT_external is never static,
20889 but it may be block-scoped. */
20890 list_to_add
20891 = ((cu->list_in_scope
20892 == cu->get_builder ()->get_file_symbols ())
20893 ? cu->get_builder ()->get_global_symbols ()
20894 : cu->list_in_scope);
20895 }
20896 else
20897 list_to_add = cu->list_in_scope;
20898 }
20899 else
20900 {
20901 /* We do not know the address of this symbol.
20902 If it is an external symbol and we have type information
20903 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20904 The address of the variable will then be determined from
20905 the minimal symbol table whenever the variable is
20906 referenced. */
20907 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20908
20909 /* Fortran explicitly imports any global symbols to the local
20910 scope by DW_TAG_common_block. */
20911 if (cu->language == language_fortran && die->parent
20912 && die->parent->tag == DW_TAG_common_block)
20913 {
20914 /* SYMBOL_CLASS doesn't matter here because
20915 read_common_block is going to reset it. */
20916 if (!suppress_add)
20917 list_to_add = cu->list_in_scope;
20918 }
20919 else if (attr2 && (DW_UNSND (attr2) != 0)
20920 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20921 {
20922 /* A variable with DW_AT_external is never static, but it
20923 may be block-scoped. */
20924 list_to_add
20925 = ((cu->list_in_scope
20926 == cu->get_builder ()->get_file_symbols ())
20927 ? cu->get_builder ()->get_global_symbols ()
20928 : cu->list_in_scope);
20929
20930 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20931 }
20932 else if (!die_is_declaration (die, cu))
20933 {
20934 /* Use the default LOC_OPTIMIZED_OUT class. */
20935 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20936 if (!suppress_add)
20937 list_to_add = cu->list_in_scope;
20938 }
20939 }
20940 break;
20941 case DW_TAG_formal_parameter:
20942 {
20943 /* If we are inside a function, mark this as an argument. If
20944 not, we might be looking at an argument to an inlined function
20945 when we do not have enough information to show inlined frames;
20946 pretend it's a local variable in that case so that the user can
20947 still see it. */
20948 struct context_stack *curr
20949 = cu->get_builder ()->get_current_context_stack ();
20950 if (curr != nullptr && curr->name != nullptr)
20951 SYMBOL_IS_ARGUMENT (sym) = 1;
20952 attr = dwarf2_attr (die, DW_AT_location, cu);
20953 if (attr != nullptr)
20954 {
20955 var_decode_location (attr, sym, cu);
20956 }
20957 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20958 if (attr != nullptr)
20959 {
20960 dwarf2_const_value (attr, sym, cu);
20961 }
20962
20963 list_to_add = cu->list_in_scope;
20964 }
20965 break;
20966 case DW_TAG_unspecified_parameters:
20967 /* From varargs functions; gdb doesn't seem to have any
20968 interest in this information, so just ignore it for now.
20969 (FIXME?) */
20970 break;
20971 case DW_TAG_template_type_param:
20972 suppress_add = 1;
20973 /* Fall through. */
20974 case DW_TAG_class_type:
20975 case DW_TAG_interface_type:
20976 case DW_TAG_structure_type:
20977 case DW_TAG_union_type:
20978 case DW_TAG_set_type:
20979 case DW_TAG_enumeration_type:
20980 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20981 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20982
20983 {
20984 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20985 really ever be static objects: otherwise, if you try
20986 to, say, break of a class's method and you're in a file
20987 which doesn't mention that class, it won't work unless
20988 the check for all static symbols in lookup_symbol_aux
20989 saves you. See the OtherFileClass tests in
20990 gdb.c++/namespace.exp. */
20991
20992 if (!suppress_add)
20993 {
20994 buildsym_compunit *builder = cu->get_builder ();
20995 list_to_add
20996 = (cu->list_in_scope == builder->get_file_symbols ()
20997 && cu->language == language_cplus
20998 ? builder->get_global_symbols ()
20999 : cu->list_in_scope);
21000
21001 /* The semantics of C++ state that "struct foo {
21002 ... }" also defines a typedef for "foo". */
21003 if (cu->language == language_cplus
21004 || cu->language == language_ada
21005 || cu->language == language_d
21006 || cu->language == language_rust)
21007 {
21008 /* The symbol's name is already allocated along
21009 with this objfile, so we don't need to
21010 duplicate it for the type. */
21011 if (SYMBOL_TYPE (sym)->name () == 0)
21012 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21013 }
21014 }
21015 }
21016 break;
21017 case DW_TAG_typedef:
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_base_type:
21023 case DW_TAG_subrange_type:
21024 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21025 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21026 list_to_add = cu->list_in_scope;
21027 break;
21028 case DW_TAG_enumerator:
21029 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21030 if (attr != nullptr)
21031 {
21032 dwarf2_const_value (attr, sym, cu);
21033 }
21034 {
21035 /* NOTE: carlton/2003-11-10: See comment above in the
21036 DW_TAG_class_type, etc. block. */
21037
21038 list_to_add
21039 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21040 && cu->language == language_cplus
21041 ? cu->get_builder ()->get_global_symbols ()
21042 : cu->list_in_scope);
21043 }
21044 break;
21045 case DW_TAG_imported_declaration:
21046 case DW_TAG_namespace:
21047 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21048 list_to_add = cu->get_builder ()->get_global_symbols ();
21049 break;
21050 case DW_TAG_module:
21051 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21052 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21053 list_to_add = cu->get_builder ()->get_global_symbols ();
21054 break;
21055 case DW_TAG_common_block:
21056 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21057 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21058 add_symbol_to_list (sym, cu->list_in_scope);
21059 break;
21060 default:
21061 /* Not a tag we recognize. Hopefully we aren't processing
21062 trash data, but since we must specifically ignore things
21063 we don't recognize, there is nothing else we should do at
21064 this point. */
21065 complaint (_("unsupported tag: '%s'"),
21066 dwarf_tag_name (die->tag));
21067 break;
21068 }
21069
21070 if (suppress_add)
21071 {
21072 sym->hash_next = objfile->template_symbols;
21073 objfile->template_symbols = sym;
21074 list_to_add = NULL;
21075 }
21076
21077 if (list_to_add != NULL)
21078 add_symbol_to_list (sym, list_to_add);
21079
21080 /* For the benefit of old versions of GCC, check for anonymous
21081 namespaces based on the demangled name. */
21082 if (!cu->processing_has_namespace_info
21083 && cu->language == language_cplus)
21084 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21085 }
21086 return (sym);
21087 }
21088
21089 /* Given an attr with a DW_FORM_dataN value in host byte order,
21090 zero-extend it as appropriate for the symbol's type. The DWARF
21091 standard (v4) is not entirely clear about the meaning of using
21092 DW_FORM_dataN for a constant with a signed type, where the type is
21093 wider than the data. The conclusion of a discussion on the DWARF
21094 list was that this is unspecified. We choose to always zero-extend
21095 because that is the interpretation long in use by GCC. */
21096
21097 static gdb_byte *
21098 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21099 struct dwarf2_cu *cu, LONGEST *value, int bits)
21100 {
21101 struct objfile *objfile = cu->per_objfile->objfile;
21102 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21103 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21104 LONGEST l = DW_UNSND (attr);
21105
21106 if (bits < sizeof (*value) * 8)
21107 {
21108 l &= ((LONGEST) 1 << bits) - 1;
21109 *value = l;
21110 }
21111 else if (bits == sizeof (*value) * 8)
21112 *value = l;
21113 else
21114 {
21115 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21116 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21117 return bytes;
21118 }
21119
21120 return NULL;
21121 }
21122
21123 /* Read a constant value from an attribute. Either set *VALUE, or if
21124 the value does not fit in *VALUE, set *BYTES - either already
21125 allocated on the objfile obstack, or newly allocated on OBSTACK,
21126 or, set *BATON, if we translated the constant to a location
21127 expression. */
21128
21129 static void
21130 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21131 const char *name, struct obstack *obstack,
21132 struct dwarf2_cu *cu,
21133 LONGEST *value, const gdb_byte **bytes,
21134 struct dwarf2_locexpr_baton **baton)
21135 {
21136 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21137 struct objfile *objfile = per_objfile->objfile;
21138 struct comp_unit_head *cu_header = &cu->header;
21139 struct dwarf_block *blk;
21140 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21141 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21142
21143 *value = 0;
21144 *bytes = NULL;
21145 *baton = NULL;
21146
21147 switch (attr->form)
21148 {
21149 case DW_FORM_addr:
21150 case DW_FORM_addrx:
21151 case DW_FORM_GNU_addr_index:
21152 {
21153 gdb_byte *data;
21154
21155 if (TYPE_LENGTH (type) != cu_header->addr_size)
21156 dwarf2_const_value_length_mismatch_complaint (name,
21157 cu_header->addr_size,
21158 TYPE_LENGTH (type));
21159 /* Symbols of this form are reasonably rare, so we just
21160 piggyback on the existing location code rather than writing
21161 a new implementation of symbol_computed_ops. */
21162 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21163 (*baton)->per_objfile = per_objfile;
21164 (*baton)->per_cu = cu->per_cu;
21165 gdb_assert ((*baton)->per_cu);
21166
21167 (*baton)->size = 2 + cu_header->addr_size;
21168 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21169 (*baton)->data = data;
21170
21171 data[0] = DW_OP_addr;
21172 store_unsigned_integer (&data[1], cu_header->addr_size,
21173 byte_order, DW_ADDR (attr));
21174 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21175 }
21176 break;
21177 case DW_FORM_string:
21178 case DW_FORM_strp:
21179 case DW_FORM_strx:
21180 case DW_FORM_GNU_str_index:
21181 case DW_FORM_GNU_strp_alt:
21182 /* DW_STRING is already allocated on the objfile obstack, point
21183 directly to it. */
21184 *bytes = (const gdb_byte *) DW_STRING (attr);
21185 break;
21186 case DW_FORM_block1:
21187 case DW_FORM_block2:
21188 case DW_FORM_block4:
21189 case DW_FORM_block:
21190 case DW_FORM_exprloc:
21191 case DW_FORM_data16:
21192 blk = DW_BLOCK (attr);
21193 if (TYPE_LENGTH (type) != blk->size)
21194 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21195 TYPE_LENGTH (type));
21196 *bytes = blk->data;
21197 break;
21198
21199 /* The DW_AT_const_value attributes are supposed to carry the
21200 symbol's value "represented as it would be on the target
21201 architecture." By the time we get here, it's already been
21202 converted to host endianness, so we just need to sign- or
21203 zero-extend it as appropriate. */
21204 case DW_FORM_data1:
21205 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21206 break;
21207 case DW_FORM_data2:
21208 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21209 break;
21210 case DW_FORM_data4:
21211 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21212 break;
21213 case DW_FORM_data8:
21214 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21215 break;
21216
21217 case DW_FORM_sdata:
21218 case DW_FORM_implicit_const:
21219 *value = DW_SND (attr);
21220 break;
21221
21222 case DW_FORM_udata:
21223 *value = DW_UNSND (attr);
21224 break;
21225
21226 default:
21227 complaint (_("unsupported const value attribute form: '%s'"),
21228 dwarf_form_name (attr->form));
21229 *value = 0;
21230 break;
21231 }
21232 }
21233
21234
21235 /* Copy constant value from an attribute to a symbol. */
21236
21237 static void
21238 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21239 struct dwarf2_cu *cu)
21240 {
21241 struct objfile *objfile = cu->per_objfile->objfile;
21242 LONGEST value;
21243 const gdb_byte *bytes;
21244 struct dwarf2_locexpr_baton *baton;
21245
21246 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21247 sym->print_name (),
21248 &objfile->objfile_obstack, cu,
21249 &value, &bytes, &baton);
21250
21251 if (baton != NULL)
21252 {
21253 SYMBOL_LOCATION_BATON (sym) = baton;
21254 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21255 }
21256 else if (bytes != NULL)
21257 {
21258 SYMBOL_VALUE_BYTES (sym) = bytes;
21259 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21260 }
21261 else
21262 {
21263 SYMBOL_VALUE (sym) = value;
21264 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21265 }
21266 }
21267
21268 /* Return the type of the die in question using its DW_AT_type attribute. */
21269
21270 static struct type *
21271 die_type (struct die_info *die, struct dwarf2_cu *cu)
21272 {
21273 struct attribute *type_attr;
21274
21275 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21276 if (!type_attr)
21277 {
21278 struct objfile *objfile = cu->per_objfile->objfile;
21279 /* A missing DW_AT_type represents a void type. */
21280 return objfile_type (objfile)->builtin_void;
21281 }
21282
21283 return lookup_die_type (die, type_attr, cu);
21284 }
21285
21286 /* True iff CU's producer generates GNAT Ada auxiliary information
21287 that allows to find parallel types through that information instead
21288 of having to do expensive parallel lookups by type name. */
21289
21290 static int
21291 need_gnat_info (struct dwarf2_cu *cu)
21292 {
21293 /* Assume that the Ada compiler was GNAT, which always produces
21294 the auxiliary information. */
21295 return (cu->language == language_ada);
21296 }
21297
21298 /* Return the auxiliary type of the die in question using its
21299 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21300 attribute is not present. */
21301
21302 static struct type *
21303 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21304 {
21305 struct attribute *type_attr;
21306
21307 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21308 if (!type_attr)
21309 return NULL;
21310
21311 return lookup_die_type (die, type_attr, cu);
21312 }
21313
21314 /* If DIE has a descriptive_type attribute, then set the TYPE's
21315 descriptive type accordingly. */
21316
21317 static void
21318 set_descriptive_type (struct type *type, struct die_info *die,
21319 struct dwarf2_cu *cu)
21320 {
21321 struct type *descriptive_type = die_descriptive_type (die, cu);
21322
21323 if (descriptive_type)
21324 {
21325 ALLOCATE_GNAT_AUX_TYPE (type);
21326 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21327 }
21328 }
21329
21330 /* Return the containing type of the die in question using its
21331 DW_AT_containing_type attribute. */
21332
21333 static struct type *
21334 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21335 {
21336 struct attribute *type_attr;
21337 struct objfile *objfile = cu->per_objfile->objfile;
21338
21339 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21340 if (!type_attr)
21341 error (_("Dwarf Error: Problem turning containing type into gdb type "
21342 "[in module %s]"), objfile_name (objfile));
21343
21344 return lookup_die_type (die, type_attr, cu);
21345 }
21346
21347 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21348
21349 static struct type *
21350 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21351 {
21352 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21353 struct objfile *objfile = dwarf2_per_objfile->objfile;
21354 char *saved;
21355
21356 std::string message
21357 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21358 objfile_name (objfile),
21359 sect_offset_str (cu->header.sect_off),
21360 sect_offset_str (die->sect_off));
21361 saved = obstack_strdup (&objfile->objfile_obstack, message);
21362
21363 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21364 }
21365
21366 /* Look up the type of DIE in CU using its type attribute ATTR.
21367 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21368 DW_AT_containing_type.
21369 If there is no type substitute an error marker. */
21370
21371 static struct type *
21372 lookup_die_type (struct die_info *die, const struct attribute *attr,
21373 struct dwarf2_cu *cu)
21374 {
21375 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21376 struct objfile *objfile = dwarf2_per_objfile->objfile;
21377 struct type *this_type;
21378
21379 gdb_assert (attr->name == DW_AT_type
21380 || attr->name == DW_AT_GNAT_descriptive_type
21381 || attr->name == DW_AT_containing_type);
21382
21383 /* First see if we have it cached. */
21384
21385 if (attr->form == DW_FORM_GNU_ref_alt)
21386 {
21387 struct dwarf2_per_cu_data *per_cu;
21388 sect_offset sect_off = attr->get_ref_die_offset ();
21389
21390 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21391 dwarf2_per_objfile);
21392 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
21393 }
21394 else if (attr->form_is_ref ())
21395 {
21396 sect_offset sect_off = attr->get_ref_die_offset ();
21397
21398 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21399 dwarf2_per_objfile);
21400 }
21401 else if (attr->form == DW_FORM_ref_sig8)
21402 {
21403 ULONGEST signature = DW_SIGNATURE (attr);
21404
21405 return get_signatured_type (die, signature, cu);
21406 }
21407 else
21408 {
21409 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21410 " at %s [in module %s]"),
21411 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21412 objfile_name (objfile));
21413 return build_error_marker_type (cu, die);
21414 }
21415
21416 /* If not cached we need to read it in. */
21417
21418 if (this_type == NULL)
21419 {
21420 struct die_info *type_die = NULL;
21421 struct dwarf2_cu *type_cu = cu;
21422
21423 if (attr->form_is_ref ())
21424 type_die = follow_die_ref (die, attr, &type_cu);
21425 if (type_die == NULL)
21426 return build_error_marker_type (cu, die);
21427 /* If we find the type now, it's probably because the type came
21428 from an inter-CU reference and the type's CU got expanded before
21429 ours. */
21430 this_type = read_type_die (type_die, type_cu);
21431 }
21432
21433 /* If we still don't have a type use an error marker. */
21434
21435 if (this_type == NULL)
21436 return build_error_marker_type (cu, die);
21437
21438 return this_type;
21439 }
21440
21441 /* Return the type in DIE, CU.
21442 Returns NULL for invalid types.
21443
21444 This first does a lookup in die_type_hash,
21445 and only reads the die in if necessary.
21446
21447 NOTE: This can be called when reading in partial or full symbols. */
21448
21449 static struct type *
21450 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21451 {
21452 struct type *this_type;
21453
21454 this_type = get_die_type (die, cu);
21455 if (this_type)
21456 return this_type;
21457
21458 return read_type_die_1 (die, cu);
21459 }
21460
21461 /* Read the type in DIE, CU.
21462 Returns NULL for invalid types. */
21463
21464 static struct type *
21465 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21466 {
21467 struct type *this_type = NULL;
21468
21469 switch (die->tag)
21470 {
21471 case DW_TAG_class_type:
21472 case DW_TAG_interface_type:
21473 case DW_TAG_structure_type:
21474 case DW_TAG_union_type:
21475 this_type = read_structure_type (die, cu);
21476 break;
21477 case DW_TAG_enumeration_type:
21478 this_type = read_enumeration_type (die, cu);
21479 break;
21480 case DW_TAG_subprogram:
21481 case DW_TAG_subroutine_type:
21482 case DW_TAG_inlined_subroutine:
21483 this_type = read_subroutine_type (die, cu);
21484 break;
21485 case DW_TAG_array_type:
21486 this_type = read_array_type (die, cu);
21487 break;
21488 case DW_TAG_set_type:
21489 this_type = read_set_type (die, cu);
21490 break;
21491 case DW_TAG_pointer_type:
21492 this_type = read_tag_pointer_type (die, cu);
21493 break;
21494 case DW_TAG_ptr_to_member_type:
21495 this_type = read_tag_ptr_to_member_type (die, cu);
21496 break;
21497 case DW_TAG_reference_type:
21498 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21499 break;
21500 case DW_TAG_rvalue_reference_type:
21501 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21502 break;
21503 case DW_TAG_const_type:
21504 this_type = read_tag_const_type (die, cu);
21505 break;
21506 case DW_TAG_volatile_type:
21507 this_type = read_tag_volatile_type (die, cu);
21508 break;
21509 case DW_TAG_restrict_type:
21510 this_type = read_tag_restrict_type (die, cu);
21511 break;
21512 case DW_TAG_string_type:
21513 this_type = read_tag_string_type (die, cu);
21514 break;
21515 case DW_TAG_typedef:
21516 this_type = read_typedef (die, cu);
21517 break;
21518 case DW_TAG_subrange_type:
21519 this_type = read_subrange_type (die, cu);
21520 break;
21521 case DW_TAG_base_type:
21522 this_type = read_base_type (die, cu);
21523 break;
21524 case DW_TAG_unspecified_type:
21525 this_type = read_unspecified_type (die, cu);
21526 break;
21527 case DW_TAG_namespace:
21528 this_type = read_namespace_type (die, cu);
21529 break;
21530 case DW_TAG_module:
21531 this_type = read_module_type (die, cu);
21532 break;
21533 case DW_TAG_atomic_type:
21534 this_type = read_tag_atomic_type (die, cu);
21535 break;
21536 default:
21537 complaint (_("unexpected tag in read_type_die: '%s'"),
21538 dwarf_tag_name (die->tag));
21539 break;
21540 }
21541
21542 return this_type;
21543 }
21544
21545 /* See if we can figure out if the class lives in a namespace. We do
21546 this by looking for a member function; its demangled name will
21547 contain namespace info, if there is any.
21548 Return the computed name or NULL.
21549 Space for the result is allocated on the objfile's obstack.
21550 This is the full-die version of guess_partial_die_structure_name.
21551 In this case we know DIE has no useful parent. */
21552
21553 static const char *
21554 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21555 {
21556 struct die_info *spec_die;
21557 struct dwarf2_cu *spec_cu;
21558 struct die_info *child;
21559 struct objfile *objfile = cu->per_objfile->objfile;
21560
21561 spec_cu = cu;
21562 spec_die = die_specification (die, &spec_cu);
21563 if (spec_die != NULL)
21564 {
21565 die = spec_die;
21566 cu = spec_cu;
21567 }
21568
21569 for (child = die->child;
21570 child != NULL;
21571 child = child->sibling)
21572 {
21573 if (child->tag == DW_TAG_subprogram)
21574 {
21575 const char *linkage_name = dw2_linkage_name (child, cu);
21576
21577 if (linkage_name != NULL)
21578 {
21579 gdb::unique_xmalloc_ptr<char> actual_name
21580 (language_class_name_from_physname (cu->language_defn,
21581 linkage_name));
21582 const char *name = NULL;
21583
21584 if (actual_name != NULL)
21585 {
21586 const char *die_name = dwarf2_name (die, cu);
21587
21588 if (die_name != NULL
21589 && strcmp (die_name, actual_name.get ()) != 0)
21590 {
21591 /* Strip off the class name from the full name.
21592 We want the prefix. */
21593 int die_name_len = strlen (die_name);
21594 int actual_name_len = strlen (actual_name.get ());
21595 const char *ptr = actual_name.get ();
21596
21597 /* Test for '::' as a sanity check. */
21598 if (actual_name_len > die_name_len + 2
21599 && ptr[actual_name_len - die_name_len - 1] == ':')
21600 name = obstack_strndup (
21601 &objfile->per_bfd->storage_obstack,
21602 ptr, actual_name_len - die_name_len - 2);
21603 }
21604 }
21605 return name;
21606 }
21607 }
21608 }
21609
21610 return NULL;
21611 }
21612
21613 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21614 prefix part in such case. See
21615 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21616
21617 static const char *
21618 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21619 {
21620 struct attribute *attr;
21621 const char *base;
21622
21623 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21624 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21625 return NULL;
21626
21627 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21628 return NULL;
21629
21630 attr = dw2_linkage_name_attr (die, cu);
21631 if (attr == NULL || DW_STRING (attr) == NULL)
21632 return NULL;
21633
21634 /* dwarf2_name had to be already called. */
21635 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21636
21637 /* Strip the base name, keep any leading namespaces/classes. */
21638 base = strrchr (DW_STRING (attr), ':');
21639 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21640 return "";
21641
21642 struct objfile *objfile = cu->per_objfile->objfile;
21643 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21644 DW_STRING (attr),
21645 &base[-1] - DW_STRING (attr));
21646 }
21647
21648 /* Return the name of the namespace/class that DIE is defined within,
21649 or "" if we can't tell. The caller should not xfree the result.
21650
21651 For example, if we're within the method foo() in the following
21652 code:
21653
21654 namespace N {
21655 class C {
21656 void foo () {
21657 }
21658 };
21659 }
21660
21661 then determine_prefix on foo's die will return "N::C". */
21662
21663 static const char *
21664 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21665 {
21666 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21667 struct die_info *parent, *spec_die;
21668 struct dwarf2_cu *spec_cu;
21669 struct type *parent_type;
21670 const char *retval;
21671
21672 if (cu->language != language_cplus
21673 && cu->language != language_fortran && cu->language != language_d
21674 && cu->language != language_rust)
21675 return "";
21676
21677 retval = anonymous_struct_prefix (die, cu);
21678 if (retval)
21679 return retval;
21680
21681 /* We have to be careful in the presence of DW_AT_specification.
21682 For example, with GCC 3.4, given the code
21683
21684 namespace N {
21685 void foo() {
21686 // Definition of N::foo.
21687 }
21688 }
21689
21690 then we'll have a tree of DIEs like this:
21691
21692 1: DW_TAG_compile_unit
21693 2: DW_TAG_namespace // N
21694 3: DW_TAG_subprogram // declaration of N::foo
21695 4: DW_TAG_subprogram // definition of N::foo
21696 DW_AT_specification // refers to die #3
21697
21698 Thus, when processing die #4, we have to pretend that we're in
21699 the context of its DW_AT_specification, namely the contex of die
21700 #3. */
21701 spec_cu = cu;
21702 spec_die = die_specification (die, &spec_cu);
21703 if (spec_die == NULL)
21704 parent = die->parent;
21705 else
21706 {
21707 parent = spec_die->parent;
21708 cu = spec_cu;
21709 }
21710
21711 if (parent == NULL)
21712 return "";
21713 else if (parent->building_fullname)
21714 {
21715 const char *name;
21716 const char *parent_name;
21717
21718 /* It has been seen on RealView 2.2 built binaries,
21719 DW_TAG_template_type_param types actually _defined_ as
21720 children of the parent class:
21721
21722 enum E {};
21723 template class <class Enum> Class{};
21724 Class<enum E> class_e;
21725
21726 1: DW_TAG_class_type (Class)
21727 2: DW_TAG_enumeration_type (E)
21728 3: DW_TAG_enumerator (enum1:0)
21729 3: DW_TAG_enumerator (enum2:1)
21730 ...
21731 2: DW_TAG_template_type_param
21732 DW_AT_type DW_FORM_ref_udata (E)
21733
21734 Besides being broken debug info, it can put GDB into an
21735 infinite loop. Consider:
21736
21737 When we're building the full name for Class<E>, we'll start
21738 at Class, and go look over its template type parameters,
21739 finding E. We'll then try to build the full name of E, and
21740 reach here. We're now trying to build the full name of E,
21741 and look over the parent DIE for containing scope. In the
21742 broken case, if we followed the parent DIE of E, we'd again
21743 find Class, and once again go look at its template type
21744 arguments, etc., etc. Simply don't consider such parent die
21745 as source-level parent of this die (it can't be, the language
21746 doesn't allow it), and break the loop here. */
21747 name = dwarf2_name (die, cu);
21748 parent_name = dwarf2_name (parent, cu);
21749 complaint (_("template param type '%s' defined within parent '%s'"),
21750 name ? name : "<unknown>",
21751 parent_name ? parent_name : "<unknown>");
21752 return "";
21753 }
21754 else
21755 switch (parent->tag)
21756 {
21757 case DW_TAG_namespace:
21758 parent_type = read_type_die (parent, cu);
21759 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21760 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21761 Work around this problem here. */
21762 if (cu->language == language_cplus
21763 && strcmp (parent_type->name (), "::") == 0)
21764 return "";
21765 /* We give a name to even anonymous namespaces. */
21766 return parent_type->name ();
21767 case DW_TAG_class_type:
21768 case DW_TAG_interface_type:
21769 case DW_TAG_structure_type:
21770 case DW_TAG_union_type:
21771 case DW_TAG_module:
21772 parent_type = read_type_die (parent, cu);
21773 if (parent_type->name () != NULL)
21774 return parent_type->name ();
21775 else
21776 /* An anonymous structure is only allowed non-static data
21777 members; no typedefs, no member functions, et cetera.
21778 So it does not need a prefix. */
21779 return "";
21780 case DW_TAG_compile_unit:
21781 case DW_TAG_partial_unit:
21782 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21783 if (cu->language == language_cplus
21784 && !dwarf2_per_objfile->per_bfd->types.empty ()
21785 && die->child != NULL
21786 && (die->tag == DW_TAG_class_type
21787 || die->tag == DW_TAG_structure_type
21788 || die->tag == DW_TAG_union_type))
21789 {
21790 const char *name = guess_full_die_structure_name (die, cu);
21791 if (name != NULL)
21792 return name;
21793 }
21794 return "";
21795 case DW_TAG_subprogram:
21796 /* Nested subroutines in Fortran get a prefix with the name
21797 of the parent's subroutine. */
21798 if (cu->language == language_fortran)
21799 {
21800 if ((die->tag == DW_TAG_subprogram)
21801 && (dwarf2_name (parent, cu) != NULL))
21802 return dwarf2_name (parent, cu);
21803 }
21804 return determine_prefix (parent, cu);
21805 case DW_TAG_enumeration_type:
21806 parent_type = read_type_die (parent, cu);
21807 if (TYPE_DECLARED_CLASS (parent_type))
21808 {
21809 if (parent_type->name () != NULL)
21810 return parent_type->name ();
21811 return "";
21812 }
21813 /* Fall through. */
21814 default:
21815 return determine_prefix (parent, cu);
21816 }
21817 }
21818
21819 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21820 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21821 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21822 an obconcat, otherwise allocate storage for the result. The CU argument is
21823 used to determine the language and hence, the appropriate separator. */
21824
21825 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21826
21827 static char *
21828 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21829 int physname, struct dwarf2_cu *cu)
21830 {
21831 const char *lead = "";
21832 const char *sep;
21833
21834 if (suffix == NULL || suffix[0] == '\0'
21835 || prefix == NULL || prefix[0] == '\0')
21836 sep = "";
21837 else if (cu->language == language_d)
21838 {
21839 /* For D, the 'main' function could be defined in any module, but it
21840 should never be prefixed. */
21841 if (strcmp (suffix, "D main") == 0)
21842 {
21843 prefix = "";
21844 sep = "";
21845 }
21846 else
21847 sep = ".";
21848 }
21849 else if (cu->language == language_fortran && physname)
21850 {
21851 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21852 DW_AT_MIPS_linkage_name is preferred and used instead. */
21853
21854 lead = "__";
21855 sep = "_MOD_";
21856 }
21857 else
21858 sep = "::";
21859
21860 if (prefix == NULL)
21861 prefix = "";
21862 if (suffix == NULL)
21863 suffix = "";
21864
21865 if (obs == NULL)
21866 {
21867 char *retval
21868 = ((char *)
21869 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21870
21871 strcpy (retval, lead);
21872 strcat (retval, prefix);
21873 strcat (retval, sep);
21874 strcat (retval, suffix);
21875 return retval;
21876 }
21877 else
21878 {
21879 /* We have an obstack. */
21880 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21881 }
21882 }
21883
21884 /* Get name of a die, return NULL if not found. */
21885
21886 static const char *
21887 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21888 struct objfile *objfile)
21889 {
21890 if (name && cu->language == language_cplus)
21891 {
21892 gdb::unique_xmalloc_ptr<char> canon_name
21893 = cp_canonicalize_string (name);
21894
21895 if (canon_name != nullptr)
21896 name = objfile->intern (canon_name.get ());
21897 }
21898
21899 return name;
21900 }
21901
21902 /* Get name of a die, return NULL if not found.
21903 Anonymous namespaces are converted to their magic string. */
21904
21905 static const char *
21906 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21907 {
21908 struct attribute *attr;
21909 struct objfile *objfile = cu->per_objfile->objfile;
21910
21911 attr = dwarf2_attr (die, DW_AT_name, cu);
21912 if ((!attr || !DW_STRING (attr))
21913 && die->tag != DW_TAG_namespace
21914 && die->tag != DW_TAG_class_type
21915 && die->tag != DW_TAG_interface_type
21916 && die->tag != DW_TAG_structure_type
21917 && die->tag != DW_TAG_union_type)
21918 return NULL;
21919
21920 switch (die->tag)
21921 {
21922 case DW_TAG_compile_unit:
21923 case DW_TAG_partial_unit:
21924 /* Compilation units have a DW_AT_name that is a filename, not
21925 a source language identifier. */
21926 case DW_TAG_enumeration_type:
21927 case DW_TAG_enumerator:
21928 /* These tags always have simple identifiers already; no need
21929 to canonicalize them. */
21930 return DW_STRING (attr);
21931
21932 case DW_TAG_namespace:
21933 if (attr != NULL && DW_STRING (attr) != NULL)
21934 return DW_STRING (attr);
21935 return CP_ANONYMOUS_NAMESPACE_STR;
21936
21937 case DW_TAG_class_type:
21938 case DW_TAG_interface_type:
21939 case DW_TAG_structure_type:
21940 case DW_TAG_union_type:
21941 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21942 structures or unions. These were of the form "._%d" in GCC 4.1,
21943 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21944 and GCC 4.4. We work around this problem by ignoring these. */
21945 if (attr && DW_STRING (attr)
21946 && (startswith (DW_STRING (attr), "._")
21947 || startswith (DW_STRING (attr), "<anonymous")))
21948 return NULL;
21949
21950 /* GCC might emit a nameless typedef that has a linkage name. See
21951 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21952 if (!attr || DW_STRING (attr) == NULL)
21953 {
21954 attr = dw2_linkage_name_attr (die, cu);
21955 if (attr == NULL || DW_STRING (attr) == NULL)
21956 return NULL;
21957
21958 /* Avoid demangling DW_STRING (attr) the second time on a second
21959 call for the same DIE. */
21960 if (!DW_STRING_IS_CANONICAL (attr))
21961 {
21962 gdb::unique_xmalloc_ptr<char> demangled
21963 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21964 if (demangled == nullptr)
21965 return nullptr;
21966
21967 DW_STRING (attr) = objfile->intern (demangled.get ());
21968 DW_STRING_IS_CANONICAL (attr) = 1;
21969 }
21970
21971 /* Strip any leading namespaces/classes, keep only the base name.
21972 DW_AT_name for named DIEs does not contain the prefixes. */
21973 const char *base = strrchr (DW_STRING (attr), ':');
21974 if (base && base > DW_STRING (attr) && base[-1] == ':')
21975 return &base[1];
21976 else
21977 return DW_STRING (attr);
21978 }
21979 break;
21980
21981 default:
21982 break;
21983 }
21984
21985 if (!DW_STRING_IS_CANONICAL (attr))
21986 {
21987 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21988 objfile);
21989 DW_STRING_IS_CANONICAL (attr) = 1;
21990 }
21991 return DW_STRING (attr);
21992 }
21993
21994 /* Return the die that this die in an extension of, or NULL if there
21995 is none. *EXT_CU is the CU containing DIE on input, and the CU
21996 containing the return value on output. */
21997
21998 static struct die_info *
21999 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22000 {
22001 struct attribute *attr;
22002
22003 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22004 if (attr == NULL)
22005 return NULL;
22006
22007 return follow_die_ref (die, attr, ext_cu);
22008 }
22009
22010 static void
22011 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22012 {
22013 unsigned int i;
22014
22015 print_spaces (indent, f);
22016 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22017 dwarf_tag_name (die->tag), die->abbrev,
22018 sect_offset_str (die->sect_off));
22019
22020 if (die->parent != NULL)
22021 {
22022 print_spaces (indent, f);
22023 fprintf_unfiltered (f, " parent at offset: %s\n",
22024 sect_offset_str (die->parent->sect_off));
22025 }
22026
22027 print_spaces (indent, f);
22028 fprintf_unfiltered (f, " has children: %s\n",
22029 dwarf_bool_name (die->child != NULL));
22030
22031 print_spaces (indent, f);
22032 fprintf_unfiltered (f, " attributes:\n");
22033
22034 for (i = 0; i < die->num_attrs; ++i)
22035 {
22036 print_spaces (indent, f);
22037 fprintf_unfiltered (f, " %s (%s) ",
22038 dwarf_attr_name (die->attrs[i].name),
22039 dwarf_form_name (die->attrs[i].form));
22040
22041 switch (die->attrs[i].form)
22042 {
22043 case DW_FORM_addr:
22044 case DW_FORM_addrx:
22045 case DW_FORM_GNU_addr_index:
22046 fprintf_unfiltered (f, "address: ");
22047 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22048 break;
22049 case DW_FORM_block2:
22050 case DW_FORM_block4:
22051 case DW_FORM_block:
22052 case DW_FORM_block1:
22053 fprintf_unfiltered (f, "block: size %s",
22054 pulongest (DW_BLOCK (&die->attrs[i])->size));
22055 break;
22056 case DW_FORM_exprloc:
22057 fprintf_unfiltered (f, "expression: size %s",
22058 pulongest (DW_BLOCK (&die->attrs[i])->size));
22059 break;
22060 case DW_FORM_data16:
22061 fprintf_unfiltered (f, "constant of 16 bytes");
22062 break;
22063 case DW_FORM_ref_addr:
22064 fprintf_unfiltered (f, "ref address: ");
22065 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22066 break;
22067 case DW_FORM_GNU_ref_alt:
22068 fprintf_unfiltered (f, "alt ref address: ");
22069 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22070 break;
22071 case DW_FORM_ref1:
22072 case DW_FORM_ref2:
22073 case DW_FORM_ref4:
22074 case DW_FORM_ref8:
22075 case DW_FORM_ref_udata:
22076 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22077 (long) (DW_UNSND (&die->attrs[i])));
22078 break;
22079 case DW_FORM_data1:
22080 case DW_FORM_data2:
22081 case DW_FORM_data4:
22082 case DW_FORM_data8:
22083 case DW_FORM_udata:
22084 case DW_FORM_sdata:
22085 fprintf_unfiltered (f, "constant: %s",
22086 pulongest (DW_UNSND (&die->attrs[i])));
22087 break;
22088 case DW_FORM_sec_offset:
22089 fprintf_unfiltered (f, "section offset: %s",
22090 pulongest (DW_UNSND (&die->attrs[i])));
22091 break;
22092 case DW_FORM_ref_sig8:
22093 fprintf_unfiltered (f, "signature: %s",
22094 hex_string (DW_SIGNATURE (&die->attrs[i])));
22095 break;
22096 case DW_FORM_string:
22097 case DW_FORM_strp:
22098 case DW_FORM_line_strp:
22099 case DW_FORM_strx:
22100 case DW_FORM_GNU_str_index:
22101 case DW_FORM_GNU_strp_alt:
22102 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22103 DW_STRING (&die->attrs[i])
22104 ? DW_STRING (&die->attrs[i]) : "",
22105 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22106 break;
22107 case DW_FORM_flag:
22108 if (DW_UNSND (&die->attrs[i]))
22109 fprintf_unfiltered (f, "flag: TRUE");
22110 else
22111 fprintf_unfiltered (f, "flag: FALSE");
22112 break;
22113 case DW_FORM_flag_present:
22114 fprintf_unfiltered (f, "flag: TRUE");
22115 break;
22116 case DW_FORM_indirect:
22117 /* The reader will have reduced the indirect form to
22118 the "base form" so this form should not occur. */
22119 fprintf_unfiltered (f,
22120 "unexpected attribute form: DW_FORM_indirect");
22121 break;
22122 case DW_FORM_implicit_const:
22123 fprintf_unfiltered (f, "constant: %s",
22124 plongest (DW_SND (&die->attrs[i])));
22125 break;
22126 default:
22127 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22128 die->attrs[i].form);
22129 break;
22130 }
22131 fprintf_unfiltered (f, "\n");
22132 }
22133 }
22134
22135 static void
22136 dump_die_for_error (struct die_info *die)
22137 {
22138 dump_die_shallow (gdb_stderr, 0, die);
22139 }
22140
22141 static void
22142 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22143 {
22144 int indent = level * 4;
22145
22146 gdb_assert (die != NULL);
22147
22148 if (level >= max_level)
22149 return;
22150
22151 dump_die_shallow (f, indent, die);
22152
22153 if (die->child != NULL)
22154 {
22155 print_spaces (indent, f);
22156 fprintf_unfiltered (f, " Children:");
22157 if (level + 1 < max_level)
22158 {
22159 fprintf_unfiltered (f, "\n");
22160 dump_die_1 (f, level + 1, max_level, die->child);
22161 }
22162 else
22163 {
22164 fprintf_unfiltered (f,
22165 " [not printed, max nesting level reached]\n");
22166 }
22167 }
22168
22169 if (die->sibling != NULL && level > 0)
22170 {
22171 dump_die_1 (f, level, max_level, die->sibling);
22172 }
22173 }
22174
22175 /* This is called from the pdie macro in gdbinit.in.
22176 It's not static so gcc will keep a copy callable from gdb. */
22177
22178 void
22179 dump_die (struct die_info *die, int max_level)
22180 {
22181 dump_die_1 (gdb_stdlog, 0, max_level, die);
22182 }
22183
22184 static void
22185 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22186 {
22187 void **slot;
22188
22189 slot = htab_find_slot_with_hash (cu->die_hash, die,
22190 to_underlying (die->sect_off),
22191 INSERT);
22192
22193 *slot = die;
22194 }
22195
22196 /* Follow reference or signature attribute ATTR of SRC_DIE.
22197 On entry *REF_CU is the CU of SRC_DIE.
22198 On exit *REF_CU is the CU of the result. */
22199
22200 static struct die_info *
22201 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22202 struct dwarf2_cu **ref_cu)
22203 {
22204 struct die_info *die;
22205
22206 if (attr->form_is_ref ())
22207 die = follow_die_ref (src_die, attr, ref_cu);
22208 else if (attr->form == DW_FORM_ref_sig8)
22209 die = follow_die_sig (src_die, attr, ref_cu);
22210 else
22211 {
22212 dump_die_for_error (src_die);
22213 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22214 objfile_name ((*ref_cu)->per_objfile->objfile));
22215 }
22216
22217 return die;
22218 }
22219
22220 /* Follow reference OFFSET.
22221 On entry *REF_CU is the CU of the source die referencing OFFSET.
22222 On exit *REF_CU is the CU of the result.
22223 Returns NULL if OFFSET is invalid. */
22224
22225 static struct die_info *
22226 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22227 struct dwarf2_cu **ref_cu)
22228 {
22229 struct die_info temp_die;
22230 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22231 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22232
22233 gdb_assert (cu->per_cu != NULL);
22234
22235 target_cu = cu;
22236
22237 if (cu->per_cu->is_debug_types)
22238 {
22239 /* .debug_types CUs cannot reference anything outside their CU.
22240 If they need to, they have to reference a signatured type via
22241 DW_FORM_ref_sig8. */
22242 if (!cu->header.offset_in_cu_p (sect_off))
22243 return NULL;
22244 }
22245 else if (offset_in_dwz != cu->per_cu->is_dwz
22246 || !cu->header.offset_in_cu_p (sect_off))
22247 {
22248 struct dwarf2_per_cu_data *per_cu;
22249
22250 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22251 dwarf2_per_objfile);
22252
22253 /* If necessary, add it to the queue and load its DIEs. */
22254 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22255 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22256
22257 target_cu = per_cu->cu;
22258 }
22259 else if (cu->dies == NULL)
22260 {
22261 /* We're loading full DIEs during partial symbol reading. */
22262 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22263 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22264 language_minimal);
22265 }
22266
22267 *ref_cu = target_cu;
22268 temp_die.sect_off = sect_off;
22269
22270 if (target_cu != cu)
22271 target_cu->ancestor = cu;
22272
22273 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22274 &temp_die,
22275 to_underlying (sect_off));
22276 }
22277
22278 /* Follow reference attribute ATTR of SRC_DIE.
22279 On entry *REF_CU is the CU of SRC_DIE.
22280 On exit *REF_CU is the CU of the result. */
22281
22282 static struct die_info *
22283 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22284 struct dwarf2_cu **ref_cu)
22285 {
22286 sect_offset sect_off = attr->get_ref_die_offset ();
22287 struct dwarf2_cu *cu = *ref_cu;
22288 struct die_info *die;
22289
22290 die = follow_die_offset (sect_off,
22291 (attr->form == DW_FORM_GNU_ref_alt
22292 || cu->per_cu->is_dwz),
22293 ref_cu);
22294 if (!die)
22295 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22296 "at %s [in module %s]"),
22297 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22298 objfile_name (cu->per_objfile->objfile));
22299
22300 return die;
22301 }
22302
22303 /* See read.h. */
22304
22305 struct dwarf2_locexpr_baton
22306 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22307 dwarf2_per_cu_data *per_cu,
22308 dwarf2_per_objfile *dwarf2_per_objfile,
22309 CORE_ADDR (*get_frame_pc) (void *baton),
22310 void *baton, bool resolve_abstract_p)
22311 {
22312 struct dwarf2_cu *cu;
22313 struct die_info *die;
22314 struct attribute *attr;
22315 struct dwarf2_locexpr_baton retval;
22316 struct objfile *objfile = dwarf2_per_objfile->objfile;
22317
22318 if (per_cu->cu == NULL)
22319 load_cu (per_cu, dwarf2_per_objfile, false);
22320 cu = per_cu->cu;
22321 if (cu == NULL)
22322 {
22323 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22324 Instead just throw an error, not much else we can do. */
22325 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22326 sect_offset_str (sect_off), objfile_name (objfile));
22327 }
22328
22329 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22330 if (!die)
22331 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22332 sect_offset_str (sect_off), objfile_name (objfile));
22333
22334 attr = dwarf2_attr (die, DW_AT_location, cu);
22335 if (!attr && resolve_abstract_p
22336 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22337 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22338 {
22339 CORE_ADDR pc = (*get_frame_pc) (baton);
22340 CORE_ADDR baseaddr = objfile->text_section_offset ();
22341 struct gdbarch *gdbarch = objfile->arch ();
22342
22343 for (const auto &cand_off
22344 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22345 {
22346 struct dwarf2_cu *cand_cu = cu;
22347 struct die_info *cand
22348 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22349 if (!cand
22350 || !cand->parent
22351 || cand->parent->tag != DW_TAG_subprogram)
22352 continue;
22353
22354 CORE_ADDR pc_low, pc_high;
22355 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22356 if (pc_low == ((CORE_ADDR) -1))
22357 continue;
22358 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22359 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22360 if (!(pc_low <= pc && pc < pc_high))
22361 continue;
22362
22363 die = cand;
22364 attr = dwarf2_attr (die, DW_AT_location, cu);
22365 break;
22366 }
22367 }
22368
22369 if (!attr)
22370 {
22371 /* DWARF: "If there is no such attribute, then there is no effect.".
22372 DATA is ignored if SIZE is 0. */
22373
22374 retval.data = NULL;
22375 retval.size = 0;
22376 }
22377 else if (attr->form_is_section_offset ())
22378 {
22379 struct dwarf2_loclist_baton loclist_baton;
22380 CORE_ADDR pc = (*get_frame_pc) (baton);
22381 size_t size;
22382
22383 fill_in_loclist_baton (cu, &loclist_baton, attr);
22384
22385 retval.data = dwarf2_find_location_expression (&loclist_baton,
22386 &size, pc);
22387 retval.size = size;
22388 }
22389 else
22390 {
22391 if (!attr->form_is_block ())
22392 error (_("Dwarf Error: DIE at %s referenced in module %s "
22393 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22394 sect_offset_str (sect_off), objfile_name (objfile));
22395
22396 retval.data = DW_BLOCK (attr)->data;
22397 retval.size = DW_BLOCK (attr)->size;
22398 }
22399 retval.per_objfile = dwarf2_per_objfile;
22400 retval.per_cu = cu->per_cu;
22401
22402 age_cached_comp_units (dwarf2_per_objfile);
22403
22404 return retval;
22405 }
22406
22407 /* See read.h. */
22408
22409 struct dwarf2_locexpr_baton
22410 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22411 dwarf2_per_cu_data *per_cu,
22412 dwarf2_per_objfile *per_objfile,
22413 CORE_ADDR (*get_frame_pc) (void *baton),
22414 void *baton)
22415 {
22416 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22417
22418 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22419 get_frame_pc, baton);
22420 }
22421
22422 /* Write a constant of a given type as target-ordered bytes into
22423 OBSTACK. */
22424
22425 static const gdb_byte *
22426 write_constant_as_bytes (struct obstack *obstack,
22427 enum bfd_endian byte_order,
22428 struct type *type,
22429 ULONGEST value,
22430 LONGEST *len)
22431 {
22432 gdb_byte *result;
22433
22434 *len = TYPE_LENGTH (type);
22435 result = (gdb_byte *) obstack_alloc (obstack, *len);
22436 store_unsigned_integer (result, *len, byte_order, value);
22437
22438 return result;
22439 }
22440
22441 /* See read.h. */
22442
22443 const gdb_byte *
22444 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22445 dwarf2_per_cu_data *per_cu,
22446 dwarf2_per_objfile *per_objfile,
22447 obstack *obstack,
22448 LONGEST *len)
22449 {
22450 struct dwarf2_cu *cu;
22451 struct die_info *die;
22452 struct attribute *attr;
22453 const gdb_byte *result = NULL;
22454 struct type *type;
22455 LONGEST value;
22456 enum bfd_endian byte_order;
22457 struct objfile *objfile = per_objfile->objfile;
22458
22459 if (per_cu->cu == NULL)
22460 load_cu (per_cu, per_objfile, false);
22461 cu = per_cu->cu;
22462 if (cu == NULL)
22463 {
22464 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22465 Instead just throw an error, not much else we can do. */
22466 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22467 sect_offset_str (sect_off), objfile_name (objfile));
22468 }
22469
22470 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22471 if (!die)
22472 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22473 sect_offset_str (sect_off), objfile_name (objfile));
22474
22475 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22476 if (attr == NULL)
22477 return NULL;
22478
22479 byte_order = (bfd_big_endian (objfile->obfd)
22480 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22481
22482 switch (attr->form)
22483 {
22484 case DW_FORM_addr:
22485 case DW_FORM_addrx:
22486 case DW_FORM_GNU_addr_index:
22487 {
22488 gdb_byte *tem;
22489
22490 *len = cu->header.addr_size;
22491 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22492 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22493 result = tem;
22494 }
22495 break;
22496 case DW_FORM_string:
22497 case DW_FORM_strp:
22498 case DW_FORM_strx:
22499 case DW_FORM_GNU_str_index:
22500 case DW_FORM_GNU_strp_alt:
22501 /* DW_STRING is already allocated on the objfile obstack, point
22502 directly to it. */
22503 result = (const gdb_byte *) DW_STRING (attr);
22504 *len = strlen (DW_STRING (attr));
22505 break;
22506 case DW_FORM_block1:
22507 case DW_FORM_block2:
22508 case DW_FORM_block4:
22509 case DW_FORM_block:
22510 case DW_FORM_exprloc:
22511 case DW_FORM_data16:
22512 result = DW_BLOCK (attr)->data;
22513 *len = DW_BLOCK (attr)->size;
22514 break;
22515
22516 /* The DW_AT_const_value attributes are supposed to carry the
22517 symbol's value "represented as it would be on the target
22518 architecture." By the time we get here, it's already been
22519 converted to host endianness, so we just need to sign- or
22520 zero-extend it as appropriate. */
22521 case DW_FORM_data1:
22522 type = die_type (die, cu);
22523 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22524 if (result == NULL)
22525 result = write_constant_as_bytes (obstack, byte_order,
22526 type, value, len);
22527 break;
22528 case DW_FORM_data2:
22529 type = die_type (die, cu);
22530 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22531 if (result == NULL)
22532 result = write_constant_as_bytes (obstack, byte_order,
22533 type, value, len);
22534 break;
22535 case DW_FORM_data4:
22536 type = die_type (die, cu);
22537 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22538 if (result == NULL)
22539 result = write_constant_as_bytes (obstack, byte_order,
22540 type, value, len);
22541 break;
22542 case DW_FORM_data8:
22543 type = die_type (die, cu);
22544 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22545 if (result == NULL)
22546 result = write_constant_as_bytes (obstack, byte_order,
22547 type, value, len);
22548 break;
22549
22550 case DW_FORM_sdata:
22551 case DW_FORM_implicit_const:
22552 type = die_type (die, cu);
22553 result = write_constant_as_bytes (obstack, byte_order,
22554 type, DW_SND (attr), len);
22555 break;
22556
22557 case DW_FORM_udata:
22558 type = die_type (die, cu);
22559 result = write_constant_as_bytes (obstack, byte_order,
22560 type, DW_UNSND (attr), len);
22561 break;
22562
22563 default:
22564 complaint (_("unsupported const value attribute form: '%s'"),
22565 dwarf_form_name (attr->form));
22566 break;
22567 }
22568
22569 return result;
22570 }
22571
22572 /* See read.h. */
22573
22574 struct type *
22575 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22576 dwarf2_per_cu_data *per_cu,
22577 dwarf2_per_objfile *per_objfile)
22578 {
22579 struct dwarf2_cu *cu;
22580 struct die_info *die;
22581
22582 if (per_cu->cu == NULL)
22583 load_cu (per_cu, per_objfile, false);
22584 cu = per_cu->cu;
22585 if (!cu)
22586 return NULL;
22587
22588 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22589 if (!die)
22590 return NULL;
22591
22592 return die_type (die, cu);
22593 }
22594
22595 /* See read.h. */
22596
22597 struct type *
22598 dwarf2_get_die_type (cu_offset die_offset,
22599 dwarf2_per_cu_data *per_cu,
22600 dwarf2_per_objfile *per_objfile)
22601 {
22602 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22603 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22604 }
22605
22606 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22607 On entry *REF_CU is the CU of SRC_DIE.
22608 On exit *REF_CU is the CU of the result.
22609 Returns NULL if the referenced DIE isn't found. */
22610
22611 static struct die_info *
22612 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22613 struct dwarf2_cu **ref_cu)
22614 {
22615 struct die_info temp_die;
22616 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22617 struct die_info *die;
22618 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22619
22620
22621 /* While it might be nice to assert sig_type->type == NULL here,
22622 we can get here for DW_AT_imported_declaration where we need
22623 the DIE not the type. */
22624
22625 /* If necessary, add it to the queue and load its DIEs. */
22626
22627 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22628 language_minimal))
22629 read_signatured_type (sig_type, dwarf2_per_objfile);
22630
22631 sig_cu = sig_type->per_cu.cu;
22632 gdb_assert (sig_cu != NULL);
22633 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22634 temp_die.sect_off = sig_type->type_offset_in_section;
22635 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22636 to_underlying (temp_die.sect_off));
22637 if (die)
22638 {
22639 /* For .gdb_index version 7 keep track of included TUs.
22640 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22641 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22642 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22643 {
22644 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22645 }
22646
22647 *ref_cu = sig_cu;
22648 if (sig_cu != cu)
22649 sig_cu->ancestor = cu;
22650
22651 return die;
22652 }
22653
22654 return NULL;
22655 }
22656
22657 /* Follow signatured type referenced by ATTR in SRC_DIE.
22658 On entry *REF_CU is the CU of SRC_DIE.
22659 On exit *REF_CU is the CU of the result.
22660 The result is the DIE of the type.
22661 If the referenced type cannot be found an error is thrown. */
22662
22663 static struct die_info *
22664 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22665 struct dwarf2_cu **ref_cu)
22666 {
22667 ULONGEST signature = DW_SIGNATURE (attr);
22668 struct signatured_type *sig_type;
22669 struct die_info *die;
22670
22671 gdb_assert (attr->form == DW_FORM_ref_sig8);
22672
22673 sig_type = lookup_signatured_type (*ref_cu, signature);
22674 /* sig_type will be NULL if the signatured type is missing from
22675 the debug info. */
22676 if (sig_type == NULL)
22677 {
22678 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22679 " from DIE at %s [in module %s]"),
22680 hex_string (signature), sect_offset_str (src_die->sect_off),
22681 objfile_name ((*ref_cu)->per_objfile->objfile));
22682 }
22683
22684 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22685 if (die == NULL)
22686 {
22687 dump_die_for_error (src_die);
22688 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22689 " from DIE at %s [in module %s]"),
22690 hex_string (signature), sect_offset_str (src_die->sect_off),
22691 objfile_name ((*ref_cu)->per_objfile->objfile));
22692 }
22693
22694 return die;
22695 }
22696
22697 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22698 reading in and processing the type unit if necessary. */
22699
22700 static struct type *
22701 get_signatured_type (struct die_info *die, ULONGEST signature,
22702 struct dwarf2_cu *cu)
22703 {
22704 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22705 struct signatured_type *sig_type;
22706 struct dwarf2_cu *type_cu;
22707 struct die_info *type_die;
22708 struct type *type;
22709
22710 sig_type = lookup_signatured_type (cu, signature);
22711 /* sig_type will be NULL if the signatured type is missing from
22712 the debug info. */
22713 if (sig_type == NULL)
22714 {
22715 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22716 " from DIE at %s [in module %s]"),
22717 hex_string (signature), sect_offset_str (die->sect_off),
22718 objfile_name (dwarf2_per_objfile->objfile));
22719 return build_error_marker_type (cu, die);
22720 }
22721
22722 /* If we already know the type we're done. */
22723 if (sig_type->type != NULL)
22724 return sig_type->type;
22725
22726 type_cu = cu;
22727 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22728 if (type_die != NULL)
22729 {
22730 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22731 is created. This is important, for example, because for c++ classes
22732 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22733 type = read_type_die (type_die, type_cu);
22734 if (type == NULL)
22735 {
22736 complaint (_("Dwarf Error: Cannot build signatured type %s"
22737 " referenced from DIE at %s [in module %s]"),
22738 hex_string (signature), sect_offset_str (die->sect_off),
22739 objfile_name (dwarf2_per_objfile->objfile));
22740 type = build_error_marker_type (cu, die);
22741 }
22742 }
22743 else
22744 {
22745 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22746 " from DIE at %s [in module %s]"),
22747 hex_string (signature), sect_offset_str (die->sect_off),
22748 objfile_name (dwarf2_per_objfile->objfile));
22749 type = build_error_marker_type (cu, die);
22750 }
22751 sig_type->type = type;
22752
22753 return type;
22754 }
22755
22756 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22757 reading in and processing the type unit if necessary. */
22758
22759 static struct type *
22760 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22761 struct dwarf2_cu *cu) /* ARI: editCase function */
22762 {
22763 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22764 if (attr->form_is_ref ())
22765 {
22766 struct dwarf2_cu *type_cu = cu;
22767 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22768
22769 return read_type_die (type_die, type_cu);
22770 }
22771 else if (attr->form == DW_FORM_ref_sig8)
22772 {
22773 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22774 }
22775 else
22776 {
22777 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22778
22779 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22780 " at %s [in module %s]"),
22781 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22782 objfile_name (dwarf2_per_objfile->objfile));
22783 return build_error_marker_type (cu, die);
22784 }
22785 }
22786
22787 /* Load the DIEs associated with type unit PER_CU into memory. */
22788
22789 static void
22790 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22791 dwarf2_per_objfile *per_objfile)
22792 {
22793 struct signatured_type *sig_type;
22794
22795 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22796 gdb_assert (! per_cu->type_unit_group_p ());
22797
22798 /* We have the per_cu, but we need the signatured_type.
22799 Fortunately this is an easy translation. */
22800 gdb_assert (per_cu->is_debug_types);
22801 sig_type = (struct signatured_type *) per_cu;
22802
22803 gdb_assert (per_cu->cu == NULL);
22804
22805 read_signatured_type (sig_type, per_objfile);
22806
22807 gdb_assert (per_cu->cu != NULL);
22808 }
22809
22810 /* Read in a signatured type and build its CU and DIEs.
22811 If the type is a stub for the real type in a DWO file,
22812 read in the real type from the DWO file as well. */
22813
22814 static void
22815 read_signatured_type (signatured_type *sig_type,
22816 dwarf2_per_objfile *per_objfile)
22817 {
22818 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22819
22820 gdb_assert (per_cu->is_debug_types);
22821 gdb_assert (per_cu->cu == NULL);
22822
22823 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22824
22825 if (!reader.dummy_p)
22826 {
22827 struct dwarf2_cu *cu = reader.cu;
22828 const gdb_byte *info_ptr = reader.info_ptr;
22829
22830 gdb_assert (cu->die_hash == NULL);
22831 cu->die_hash =
22832 htab_create_alloc_ex (cu->header.length / 12,
22833 die_hash,
22834 die_eq,
22835 NULL,
22836 &cu->comp_unit_obstack,
22837 hashtab_obstack_allocate,
22838 dummy_obstack_deallocate);
22839
22840 if (reader.comp_unit_die->has_children)
22841 reader.comp_unit_die->child
22842 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22843 reader.comp_unit_die);
22844 cu->dies = reader.comp_unit_die;
22845 /* comp_unit_die is not stored in die_hash, no need. */
22846
22847 /* We try not to read any attributes in this function, because
22848 not all CUs needed for references have been loaded yet, and
22849 symbol table processing isn't initialized. But we have to
22850 set the CU language, or we won't be able to build types
22851 correctly. Similarly, if we do not read the producer, we can
22852 not apply producer-specific interpretation. */
22853 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22854
22855 reader.keep ();
22856 }
22857
22858 sig_type->per_cu.tu_read = 1;
22859 }
22860
22861 /* Decode simple location descriptions.
22862 Given a pointer to a dwarf block that defines a location, compute
22863 the location and return the value. If COMPUTED is non-null, it is
22864 set to true to indicate that decoding was successful, and false
22865 otherwise. If COMPUTED is null, then this function may emit a
22866 complaint. */
22867
22868 static CORE_ADDR
22869 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22870 {
22871 struct objfile *objfile = cu->per_objfile->objfile;
22872 size_t i;
22873 size_t size = blk->size;
22874 const gdb_byte *data = blk->data;
22875 CORE_ADDR stack[64];
22876 int stacki;
22877 unsigned int bytes_read, unsnd;
22878 gdb_byte op;
22879
22880 if (computed != nullptr)
22881 *computed = false;
22882
22883 i = 0;
22884 stacki = 0;
22885 stack[stacki] = 0;
22886 stack[++stacki] = 0;
22887
22888 while (i < size)
22889 {
22890 op = data[i++];
22891 switch (op)
22892 {
22893 case DW_OP_lit0:
22894 case DW_OP_lit1:
22895 case DW_OP_lit2:
22896 case DW_OP_lit3:
22897 case DW_OP_lit4:
22898 case DW_OP_lit5:
22899 case DW_OP_lit6:
22900 case DW_OP_lit7:
22901 case DW_OP_lit8:
22902 case DW_OP_lit9:
22903 case DW_OP_lit10:
22904 case DW_OP_lit11:
22905 case DW_OP_lit12:
22906 case DW_OP_lit13:
22907 case DW_OP_lit14:
22908 case DW_OP_lit15:
22909 case DW_OP_lit16:
22910 case DW_OP_lit17:
22911 case DW_OP_lit18:
22912 case DW_OP_lit19:
22913 case DW_OP_lit20:
22914 case DW_OP_lit21:
22915 case DW_OP_lit22:
22916 case DW_OP_lit23:
22917 case DW_OP_lit24:
22918 case DW_OP_lit25:
22919 case DW_OP_lit26:
22920 case DW_OP_lit27:
22921 case DW_OP_lit28:
22922 case DW_OP_lit29:
22923 case DW_OP_lit30:
22924 case DW_OP_lit31:
22925 stack[++stacki] = op - DW_OP_lit0;
22926 break;
22927
22928 case DW_OP_reg0:
22929 case DW_OP_reg1:
22930 case DW_OP_reg2:
22931 case DW_OP_reg3:
22932 case DW_OP_reg4:
22933 case DW_OP_reg5:
22934 case DW_OP_reg6:
22935 case DW_OP_reg7:
22936 case DW_OP_reg8:
22937 case DW_OP_reg9:
22938 case DW_OP_reg10:
22939 case DW_OP_reg11:
22940 case DW_OP_reg12:
22941 case DW_OP_reg13:
22942 case DW_OP_reg14:
22943 case DW_OP_reg15:
22944 case DW_OP_reg16:
22945 case DW_OP_reg17:
22946 case DW_OP_reg18:
22947 case DW_OP_reg19:
22948 case DW_OP_reg20:
22949 case DW_OP_reg21:
22950 case DW_OP_reg22:
22951 case DW_OP_reg23:
22952 case DW_OP_reg24:
22953 case DW_OP_reg25:
22954 case DW_OP_reg26:
22955 case DW_OP_reg27:
22956 case DW_OP_reg28:
22957 case DW_OP_reg29:
22958 case DW_OP_reg30:
22959 case DW_OP_reg31:
22960 stack[++stacki] = op - DW_OP_reg0;
22961 if (i < size)
22962 {
22963 if (computed == nullptr)
22964 dwarf2_complex_location_expr_complaint ();
22965 else
22966 return 0;
22967 }
22968 break;
22969
22970 case DW_OP_regx:
22971 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22972 i += bytes_read;
22973 stack[++stacki] = unsnd;
22974 if (i < size)
22975 {
22976 if (computed == nullptr)
22977 dwarf2_complex_location_expr_complaint ();
22978 else
22979 return 0;
22980 }
22981 break;
22982
22983 case DW_OP_addr:
22984 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22985 &bytes_read);
22986 i += bytes_read;
22987 break;
22988
22989 case DW_OP_const1u:
22990 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22991 i += 1;
22992 break;
22993
22994 case DW_OP_const1s:
22995 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22996 i += 1;
22997 break;
22998
22999 case DW_OP_const2u:
23000 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23001 i += 2;
23002 break;
23003
23004 case DW_OP_const2s:
23005 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23006 i += 2;
23007 break;
23008
23009 case DW_OP_const4u:
23010 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23011 i += 4;
23012 break;
23013
23014 case DW_OP_const4s:
23015 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23016 i += 4;
23017 break;
23018
23019 case DW_OP_const8u:
23020 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23021 i += 8;
23022 break;
23023
23024 case DW_OP_constu:
23025 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23026 &bytes_read);
23027 i += bytes_read;
23028 break;
23029
23030 case DW_OP_consts:
23031 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23032 i += bytes_read;
23033 break;
23034
23035 case DW_OP_dup:
23036 stack[stacki + 1] = stack[stacki];
23037 stacki++;
23038 break;
23039
23040 case DW_OP_plus:
23041 stack[stacki - 1] += stack[stacki];
23042 stacki--;
23043 break;
23044
23045 case DW_OP_plus_uconst:
23046 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23047 &bytes_read);
23048 i += bytes_read;
23049 break;
23050
23051 case DW_OP_minus:
23052 stack[stacki - 1] -= stack[stacki];
23053 stacki--;
23054 break;
23055
23056 case DW_OP_deref:
23057 /* If we're not the last op, then we definitely can't encode
23058 this using GDB's address_class enum. This is valid for partial
23059 global symbols, although the variable's address will be bogus
23060 in the psymtab. */
23061 if (i < size)
23062 {
23063 if (computed == nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23065 else
23066 return 0;
23067 }
23068 break;
23069
23070 case DW_OP_GNU_push_tls_address:
23071 case DW_OP_form_tls_address:
23072 /* The top of the stack has the offset from the beginning
23073 of the thread control block at which the variable is located. */
23074 /* Nothing should follow this operator, so the top of stack would
23075 be returned. */
23076 /* This is valid for partial global symbols, but the variable's
23077 address will be bogus in the psymtab. Make it always at least
23078 non-zero to not look as a variable garbage collected by linker
23079 which have DW_OP_addr 0. */
23080 if (i < size)
23081 {
23082 if (computed == nullptr)
23083 dwarf2_complex_location_expr_complaint ();
23084 else
23085 return 0;
23086 }
23087 stack[stacki]++;
23088 break;
23089
23090 case DW_OP_GNU_uninit:
23091 if (computed != nullptr)
23092 return 0;
23093 break;
23094
23095 case DW_OP_addrx:
23096 case DW_OP_GNU_addr_index:
23097 case DW_OP_GNU_const_index:
23098 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23099 &bytes_read);
23100 i += bytes_read;
23101 break;
23102
23103 default:
23104 if (computed == nullptr)
23105 {
23106 const char *name = get_DW_OP_name (op);
23107
23108 if (name)
23109 complaint (_("unsupported stack op: '%s'"),
23110 name);
23111 else
23112 complaint (_("unsupported stack op: '%02x'"),
23113 op);
23114 }
23115
23116 return (stack[stacki]);
23117 }
23118
23119 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23120 outside of the allocated space. Also enforce minimum>0. */
23121 if (stacki >= ARRAY_SIZE (stack) - 1)
23122 {
23123 if (computed == nullptr)
23124 complaint (_("location description stack overflow"));
23125 return 0;
23126 }
23127
23128 if (stacki <= 0)
23129 {
23130 if (computed == nullptr)
23131 complaint (_("location description stack underflow"));
23132 return 0;
23133 }
23134 }
23135
23136 if (computed != nullptr)
23137 *computed = true;
23138 return (stack[stacki]);
23139 }
23140
23141 /* memory allocation interface */
23142
23143 static struct dwarf_block *
23144 dwarf_alloc_block (struct dwarf2_cu *cu)
23145 {
23146 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23147 }
23148
23149 static struct die_info *
23150 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23151 {
23152 struct die_info *die;
23153 size_t size = sizeof (struct die_info);
23154
23155 if (num_attrs > 1)
23156 size += (num_attrs - 1) * sizeof (struct attribute);
23157
23158 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23159 memset (die, 0, sizeof (struct die_info));
23160 return (die);
23161 }
23162
23163 \f
23164
23165 /* Macro support. */
23166
23167 /* An overload of dwarf_decode_macros that finds the correct section
23168 and ensures it is read in before calling the other overload. */
23169
23170 static void
23171 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23172 int section_is_gnu)
23173 {
23174 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23175 struct objfile *objfile = dwarf2_per_objfile->objfile;
23176 const struct line_header *lh = cu->line_header;
23177 unsigned int offset_size = cu->header.offset_size;
23178 struct dwarf2_section_info *section;
23179 const char *section_name;
23180
23181 if (cu->dwo_unit != nullptr)
23182 {
23183 if (section_is_gnu)
23184 {
23185 section = &cu->dwo_unit->dwo_file->sections.macro;
23186 section_name = ".debug_macro.dwo";
23187 }
23188 else
23189 {
23190 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23191 section_name = ".debug_macinfo.dwo";
23192 }
23193 }
23194 else
23195 {
23196 if (section_is_gnu)
23197 {
23198 section = &dwarf2_per_objfile->per_bfd->macro;
23199 section_name = ".debug_macro";
23200 }
23201 else
23202 {
23203 section = &dwarf2_per_objfile->per_bfd->macinfo;
23204 section_name = ".debug_macinfo";
23205 }
23206 }
23207
23208 section->read (objfile);
23209 if (section->buffer == nullptr)
23210 {
23211 complaint (_("missing %s section"), section_name);
23212 return;
23213 }
23214
23215 buildsym_compunit *builder = cu->get_builder ();
23216
23217 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23218 offset_size, offset, section_is_gnu);
23219 }
23220
23221 /* Return the .debug_loc section to use for CU.
23222 For DWO files use .debug_loc.dwo. */
23223
23224 static struct dwarf2_section_info *
23225 cu_debug_loc_section (struct dwarf2_cu *cu)
23226 {
23227 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23228
23229 if (cu->dwo_unit)
23230 {
23231 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23232
23233 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23234 }
23235 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23236 : &dwarf2_per_objfile->per_bfd->loc);
23237 }
23238
23239 /* A helper function that fills in a dwarf2_loclist_baton. */
23240
23241 static void
23242 fill_in_loclist_baton (struct dwarf2_cu *cu,
23243 struct dwarf2_loclist_baton *baton,
23244 const struct attribute *attr)
23245 {
23246 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23247 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23248
23249 section->read (dwarf2_per_objfile->objfile);
23250
23251 baton->per_objfile = dwarf2_per_objfile;
23252 baton->per_cu = cu->per_cu;
23253 gdb_assert (baton->per_cu);
23254 /* We don't know how long the location list is, but make sure we
23255 don't run off the edge of the section. */
23256 baton->size = section->size - DW_UNSND (attr);
23257 baton->data = section->buffer + DW_UNSND (attr);
23258 if (cu->base_address.has_value ())
23259 baton->base_address = *cu->base_address;
23260 else
23261 baton->base_address = 0;
23262 baton->from_dwo = cu->dwo_unit != NULL;
23263 }
23264
23265 static void
23266 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23267 struct dwarf2_cu *cu, int is_block)
23268 {
23269 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23270 struct objfile *objfile = dwarf2_per_objfile->objfile;
23271 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23272
23273 if (attr->form_is_section_offset ()
23274 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23275 the section. If so, fall through to the complaint in the
23276 other branch. */
23277 && DW_UNSND (attr) < section->get_size (objfile))
23278 {
23279 struct dwarf2_loclist_baton *baton;
23280
23281 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23282
23283 fill_in_loclist_baton (cu, baton, attr);
23284
23285 if (!cu->base_address.has_value ())
23286 complaint (_("Location list used without "
23287 "specifying the CU base address."));
23288
23289 SYMBOL_ACLASS_INDEX (sym) = (is_block
23290 ? dwarf2_loclist_block_index
23291 : dwarf2_loclist_index);
23292 SYMBOL_LOCATION_BATON (sym) = baton;
23293 }
23294 else
23295 {
23296 struct dwarf2_locexpr_baton *baton;
23297
23298 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23299 baton->per_objfile = dwarf2_per_objfile;
23300 baton->per_cu = cu->per_cu;
23301 gdb_assert (baton->per_cu);
23302
23303 if (attr->form_is_block ())
23304 {
23305 /* Note that we're just copying the block's data pointer
23306 here, not the actual data. We're still pointing into the
23307 info_buffer for SYM's objfile; right now we never release
23308 that buffer, but when we do clean up properly this may
23309 need to change. */
23310 baton->size = DW_BLOCK (attr)->size;
23311 baton->data = DW_BLOCK (attr)->data;
23312 }
23313 else
23314 {
23315 dwarf2_invalid_attrib_class_complaint ("location description",
23316 sym->natural_name ());
23317 baton->size = 0;
23318 }
23319
23320 SYMBOL_ACLASS_INDEX (sym) = (is_block
23321 ? dwarf2_locexpr_block_index
23322 : dwarf2_locexpr_index);
23323 SYMBOL_LOCATION_BATON (sym) = baton;
23324 }
23325 }
23326
23327 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23328 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23329 CU_HEADERP first. */
23330
23331 static const struct comp_unit_head *
23332 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23333 const struct dwarf2_per_cu_data *per_cu)
23334 {
23335 const gdb_byte *info_ptr;
23336
23337 if (per_cu->cu)
23338 return &per_cu->cu->header;
23339
23340 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23341
23342 memset (cu_headerp, 0, sizeof (*cu_headerp));
23343 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23344 rcuh_kind::COMPILE);
23345
23346 return cu_headerp;
23347 }
23348
23349 /* See read.h. */
23350
23351 int
23352 dwarf2_per_cu_data::addr_size () const
23353 {
23354 struct comp_unit_head cu_header_local;
23355 const struct comp_unit_head *cu_headerp;
23356
23357 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23358
23359 return cu_headerp->addr_size;
23360 }
23361
23362 /* See read.h. */
23363
23364 int
23365 dwarf2_per_cu_data::offset_size () const
23366 {
23367 struct comp_unit_head cu_header_local;
23368 const struct comp_unit_head *cu_headerp;
23369
23370 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23371
23372 return cu_headerp->offset_size;
23373 }
23374
23375 /* See read.h. */
23376
23377 int
23378 dwarf2_per_cu_data::ref_addr_size () const
23379 {
23380 struct comp_unit_head cu_header_local;
23381 const struct comp_unit_head *cu_headerp;
23382
23383 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23384
23385 if (cu_headerp->version == 2)
23386 return cu_headerp->addr_size;
23387 else
23388 return cu_headerp->offset_size;
23389 }
23390
23391 /* See read.h. */
23392
23393 struct type *
23394 dwarf2_cu::addr_type () const
23395 {
23396 struct objfile *objfile = this->per_objfile->objfile;
23397 struct type *void_type = objfile_type (objfile)->builtin_void;
23398 struct type *addr_type = lookup_pointer_type (void_type);
23399 int addr_size = this->per_cu->addr_size ();
23400
23401 if (TYPE_LENGTH (addr_type) == addr_size)
23402 return addr_type;
23403
23404 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23405 return addr_type;
23406 }
23407
23408 /* A helper function for dwarf2_find_containing_comp_unit that returns
23409 the index of the result, and that searches a vector. It will
23410 return a result even if the offset in question does not actually
23411 occur in any CU. This is separate so that it can be unit
23412 tested. */
23413
23414 static int
23415 dwarf2_find_containing_comp_unit
23416 (sect_offset sect_off,
23417 unsigned int offset_in_dwz,
23418 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23419 {
23420 int low, high;
23421
23422 low = 0;
23423 high = all_comp_units.size () - 1;
23424 while (high > low)
23425 {
23426 struct dwarf2_per_cu_data *mid_cu;
23427 int mid = low + (high - low) / 2;
23428
23429 mid_cu = all_comp_units[mid];
23430 if (mid_cu->is_dwz > offset_in_dwz
23431 || (mid_cu->is_dwz == offset_in_dwz
23432 && mid_cu->sect_off + mid_cu->length > sect_off))
23433 high = mid;
23434 else
23435 low = mid + 1;
23436 }
23437 gdb_assert (low == high);
23438 return low;
23439 }
23440
23441 /* Locate the .debug_info compilation unit from CU's objfile which contains
23442 the DIE at OFFSET. Raises an error on failure. */
23443
23444 static struct dwarf2_per_cu_data *
23445 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23446 unsigned int offset_in_dwz,
23447 struct dwarf2_per_objfile *dwarf2_per_objfile)
23448 {
23449 int low
23450 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23451 dwarf2_per_objfile->per_bfd->all_comp_units);
23452 struct dwarf2_per_cu_data *this_cu
23453 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23454
23455 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23456 {
23457 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23458 error (_("Dwarf Error: could not find partial DIE containing "
23459 "offset %s [in module %s]"),
23460 sect_offset_str (sect_off),
23461 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23462
23463 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23464 <= sect_off);
23465 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23466 }
23467 else
23468 {
23469 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23470 && sect_off >= this_cu->sect_off + this_cu->length)
23471 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23472 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23473 return this_cu;
23474 }
23475 }
23476
23477 #if GDB_SELF_TEST
23478
23479 namespace selftests {
23480 namespace find_containing_comp_unit {
23481
23482 static void
23483 run_test ()
23484 {
23485 struct dwarf2_per_cu_data one {};
23486 struct dwarf2_per_cu_data two {};
23487 struct dwarf2_per_cu_data three {};
23488 struct dwarf2_per_cu_data four {};
23489
23490 one.length = 5;
23491 two.sect_off = sect_offset (one.length);
23492 two.length = 7;
23493
23494 three.length = 5;
23495 three.is_dwz = 1;
23496 four.sect_off = sect_offset (three.length);
23497 four.length = 7;
23498 four.is_dwz = 1;
23499
23500 std::vector<dwarf2_per_cu_data *> units;
23501 units.push_back (&one);
23502 units.push_back (&two);
23503 units.push_back (&three);
23504 units.push_back (&four);
23505
23506 int result;
23507
23508 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23509 SELF_CHECK (units[result] == &one);
23510 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23511 SELF_CHECK (units[result] == &one);
23512 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23513 SELF_CHECK (units[result] == &two);
23514
23515 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23516 SELF_CHECK (units[result] == &three);
23517 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23518 SELF_CHECK (units[result] == &three);
23519 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23520 SELF_CHECK (units[result] == &four);
23521 }
23522
23523 }
23524 }
23525
23526 #endif /* GDB_SELF_TEST */
23527
23528 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23529
23530 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23531 dwarf2_per_objfile *per_objfile)
23532 : per_cu (per_cu),
23533 per_objfile (per_objfile),
23534 mark (false),
23535 has_loclist (false),
23536 checked_producer (false),
23537 producer_is_gxx_lt_4_6 (false),
23538 producer_is_gcc_lt_4_3 (false),
23539 producer_is_icc (false),
23540 producer_is_icc_lt_14 (false),
23541 producer_is_codewarrior (false),
23542 processing_has_namespace_info (false)
23543 {
23544 per_cu->cu = this;
23545 }
23546
23547 /* Destroy a dwarf2_cu. */
23548
23549 dwarf2_cu::~dwarf2_cu ()
23550 {
23551 per_cu->cu = NULL;
23552 }
23553
23554 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23555
23556 static void
23557 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23558 enum language pretend_language)
23559 {
23560 struct attribute *attr;
23561
23562 /* Set the language we're debugging. */
23563 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23564 if (attr != nullptr)
23565 set_cu_language (DW_UNSND (attr), cu);
23566 else
23567 {
23568 cu->language = pretend_language;
23569 cu->language_defn = language_def (cu->language);
23570 }
23571
23572 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23573 }
23574
23575 /* Increase the age counter on each cached compilation unit, and free
23576 any that are too old. */
23577
23578 static void
23579 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23580 {
23581 struct dwarf2_per_cu_data *per_cu, **last_chain;
23582
23583 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23584 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23585 while (per_cu != NULL)
23586 {
23587 per_cu->cu->last_used ++;
23588 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23589 dwarf2_mark (per_cu->cu);
23590 per_cu = per_cu->cu->read_in_chain;
23591 }
23592
23593 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23594 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23595 while (per_cu != NULL)
23596 {
23597 struct dwarf2_per_cu_data *next_cu;
23598
23599 next_cu = per_cu->cu->read_in_chain;
23600
23601 if (!per_cu->cu->mark)
23602 {
23603 delete per_cu->cu;
23604 *last_chain = next_cu;
23605 }
23606 else
23607 last_chain = &per_cu->cu->read_in_chain;
23608
23609 per_cu = next_cu;
23610 }
23611 }
23612
23613 /* Remove a single compilation unit from the cache. */
23614
23615 static void
23616 free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23617 dwarf2_per_objfile *dwarf2_per_objfile)
23618 {
23619 struct dwarf2_per_cu_data *per_cu, **last_chain;
23620
23621 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23622 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23623 while (per_cu != NULL)
23624 {
23625 struct dwarf2_per_cu_data *next_cu;
23626
23627 next_cu = per_cu->cu->read_in_chain;
23628
23629 if (per_cu == target_per_cu)
23630 {
23631 delete per_cu->cu;
23632 per_cu->cu = NULL;
23633 *last_chain = next_cu;
23634 break;
23635 }
23636 else
23637 last_chain = &per_cu->cu->read_in_chain;
23638
23639 per_cu = next_cu;
23640 }
23641 }
23642
23643 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23644 We store these in a hash table separate from the DIEs, and preserve them
23645 when the DIEs are flushed out of cache.
23646
23647 The CU "per_cu" pointer is needed because offset alone is not enough to
23648 uniquely identify the type. A file may have multiple .debug_types sections,
23649 or the type may come from a DWO file. Furthermore, while it's more logical
23650 to use per_cu->section+offset, with Fission the section with the data is in
23651 the DWO file but we don't know that section at the point we need it.
23652 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23653 because we can enter the lookup routine, get_die_type_at_offset, from
23654 outside this file, and thus won't necessarily have PER_CU->cu.
23655 Fortunately, PER_CU is stable for the life of the objfile. */
23656
23657 struct dwarf2_per_cu_offset_and_type
23658 {
23659 const struct dwarf2_per_cu_data *per_cu;
23660 sect_offset sect_off;
23661 struct type *type;
23662 };
23663
23664 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23665
23666 static hashval_t
23667 per_cu_offset_and_type_hash (const void *item)
23668 {
23669 const struct dwarf2_per_cu_offset_and_type *ofs
23670 = (const struct dwarf2_per_cu_offset_and_type *) item;
23671
23672 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23673 }
23674
23675 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23676
23677 static int
23678 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23679 {
23680 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23681 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23682 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23683 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23684
23685 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23686 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23687 }
23688
23689 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23690 table if necessary. For convenience, return TYPE.
23691
23692 The DIEs reading must have careful ordering to:
23693 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23694 reading current DIE.
23695 * Not trying to dereference contents of still incompletely read in types
23696 while reading in other DIEs.
23697 * Enable referencing still incompletely read in types just by a pointer to
23698 the type without accessing its fields.
23699
23700 Therefore caller should follow these rules:
23701 * Try to fetch any prerequisite types we may need to build this DIE type
23702 before building the type and calling set_die_type.
23703 * After building type call set_die_type for current DIE as soon as
23704 possible before fetching more types to complete the current type.
23705 * Make the type as complete as possible before fetching more types. */
23706
23707 static struct type *
23708 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23709 {
23710 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23711 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23712 struct objfile *objfile = dwarf2_per_objfile->objfile;
23713 struct attribute *attr;
23714 struct dynamic_prop prop;
23715
23716 /* For Ada types, make sure that the gnat-specific data is always
23717 initialized (if not already set). There are a few types where
23718 we should not be doing so, because the type-specific area is
23719 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23720 where the type-specific area is used to store the floatformat).
23721 But this is not a problem, because the gnat-specific information
23722 is actually not needed for these types. */
23723 if (need_gnat_info (cu)
23724 && type->code () != TYPE_CODE_FUNC
23725 && type->code () != TYPE_CODE_FLT
23726 && type->code () != TYPE_CODE_METHODPTR
23727 && type->code () != TYPE_CODE_MEMBERPTR
23728 && type->code () != TYPE_CODE_METHOD
23729 && !HAVE_GNAT_AUX_INFO (type))
23730 INIT_GNAT_SPECIFIC (type);
23731
23732 /* Read DW_AT_allocated and set in type. */
23733 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23734 if (attr != NULL && attr->form_is_block ())
23735 {
23736 struct type *prop_type = cu->addr_sized_int_type (false);
23737 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23738 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23739 }
23740 else if (attr != NULL)
23741 {
23742 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23743 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23744 sect_offset_str (die->sect_off));
23745 }
23746
23747 /* Read DW_AT_associated and set in type. */
23748 attr = dwarf2_attr (die, DW_AT_associated, cu);
23749 if (attr != NULL && attr->form_is_block ())
23750 {
23751 struct type *prop_type = cu->addr_sized_int_type (false);
23752 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23753 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23754 }
23755 else if (attr != NULL)
23756 {
23757 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23758 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23759 sect_offset_str (die->sect_off));
23760 }
23761
23762 /* Read DW_AT_data_location and set in type. */
23763 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23764 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23765 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23766
23767 if (dwarf2_per_objfile->die_type_hash == NULL)
23768 dwarf2_per_objfile->die_type_hash
23769 = htab_up (htab_create_alloc (127,
23770 per_cu_offset_and_type_hash,
23771 per_cu_offset_and_type_eq,
23772 NULL, xcalloc, xfree));
23773
23774 ofs.per_cu = cu->per_cu;
23775 ofs.sect_off = die->sect_off;
23776 ofs.type = type;
23777 slot = (struct dwarf2_per_cu_offset_and_type **)
23778 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23779 if (*slot)
23780 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23781 sect_offset_str (die->sect_off));
23782 *slot = XOBNEW (&objfile->objfile_obstack,
23783 struct dwarf2_per_cu_offset_and_type);
23784 **slot = ofs;
23785 return type;
23786 }
23787
23788 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23789 or return NULL if the die does not have a saved type. */
23790
23791 static struct type *
23792 get_die_type_at_offset (sect_offset sect_off,
23793 dwarf2_per_cu_data *per_cu,
23794 dwarf2_per_objfile *dwarf2_per_objfile)
23795 {
23796 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23797
23798 if (dwarf2_per_objfile->die_type_hash == NULL)
23799 return NULL;
23800
23801 ofs.per_cu = per_cu;
23802 ofs.sect_off = sect_off;
23803 slot = ((struct dwarf2_per_cu_offset_and_type *)
23804 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23805 if (slot)
23806 return slot->type;
23807 else
23808 return NULL;
23809 }
23810
23811 /* Look up the type for DIE in CU in die_type_hash,
23812 or return NULL if DIE does not have a saved type. */
23813
23814 static struct type *
23815 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23816 {
23817 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23818 }
23819
23820 /* Add a dependence relationship from CU to REF_PER_CU. */
23821
23822 static void
23823 dwarf2_add_dependence (struct dwarf2_cu *cu,
23824 struct dwarf2_per_cu_data *ref_per_cu)
23825 {
23826 void **slot;
23827
23828 if (cu->dependencies == NULL)
23829 cu->dependencies
23830 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23831 NULL, &cu->comp_unit_obstack,
23832 hashtab_obstack_allocate,
23833 dummy_obstack_deallocate);
23834
23835 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23836 if (*slot == NULL)
23837 *slot = ref_per_cu;
23838 }
23839
23840 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23841 Set the mark field in every compilation unit in the
23842 cache that we must keep because we are keeping CU. */
23843
23844 static int
23845 dwarf2_mark_helper (void **slot, void *data)
23846 {
23847 struct dwarf2_per_cu_data *per_cu;
23848
23849 per_cu = (struct dwarf2_per_cu_data *) *slot;
23850
23851 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23852 reading of the chain. As such dependencies remain valid it is not much
23853 useful to track and undo them during QUIT cleanups. */
23854 if (per_cu->cu == NULL)
23855 return 1;
23856
23857 if (per_cu->cu->mark)
23858 return 1;
23859 per_cu->cu->mark = true;
23860
23861 if (per_cu->cu->dependencies != NULL)
23862 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23863
23864 return 1;
23865 }
23866
23867 /* Set the mark field in CU and in every other compilation unit in the
23868 cache that we must keep because we are keeping CU. */
23869
23870 static void
23871 dwarf2_mark (struct dwarf2_cu *cu)
23872 {
23873 if (cu->mark)
23874 return;
23875 cu->mark = true;
23876 if (cu->dependencies != NULL)
23877 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23878 }
23879
23880 static void
23881 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23882 {
23883 while (per_cu)
23884 {
23885 per_cu->cu->mark = false;
23886 per_cu = per_cu->cu->read_in_chain;
23887 }
23888 }
23889
23890 /* Trivial hash function for partial_die_info: the hash value of a DIE
23891 is its offset in .debug_info for this objfile. */
23892
23893 static hashval_t
23894 partial_die_hash (const void *item)
23895 {
23896 const struct partial_die_info *part_die
23897 = (const struct partial_die_info *) item;
23898
23899 return to_underlying (part_die->sect_off);
23900 }
23901
23902 /* Trivial comparison function for partial_die_info structures: two DIEs
23903 are equal if they have the same offset. */
23904
23905 static int
23906 partial_die_eq (const void *item_lhs, const void *item_rhs)
23907 {
23908 const struct partial_die_info *part_die_lhs
23909 = (const struct partial_die_info *) item_lhs;
23910 const struct partial_die_info *part_die_rhs
23911 = (const struct partial_die_info *) item_rhs;
23912
23913 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23914 }
23915
23916 struct cmd_list_element *set_dwarf_cmdlist;
23917 struct cmd_list_element *show_dwarf_cmdlist;
23918
23919 static void
23920 show_check_physname (struct ui_file *file, int from_tty,
23921 struct cmd_list_element *c, const char *value)
23922 {
23923 fprintf_filtered (file,
23924 _("Whether to check \"physname\" is %s.\n"),
23925 value);
23926 }
23927
23928 void _initialize_dwarf2_read ();
23929 void
23930 _initialize_dwarf2_read ()
23931 {
23932 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23933 Set DWARF specific variables.\n\
23934 Configure DWARF variables such as the cache size."),
23935 &set_dwarf_cmdlist, "maintenance set dwarf ",
23936 0/*allow-unknown*/, &maintenance_set_cmdlist);
23937
23938 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23939 Show DWARF specific variables.\n\
23940 Show DWARF variables such as the cache size."),
23941 &show_dwarf_cmdlist, "maintenance show dwarf ",
23942 0/*allow-unknown*/, &maintenance_show_cmdlist);
23943
23944 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23945 &dwarf_max_cache_age, _("\
23946 Set the upper bound on the age of cached DWARF compilation units."), _("\
23947 Show the upper bound on the age of cached DWARF compilation units."), _("\
23948 A higher limit means that cached compilation units will be stored\n\
23949 in memory longer, and more total memory will be used. Zero disables\n\
23950 caching, which can slow down startup."),
23951 NULL,
23952 show_dwarf_max_cache_age,
23953 &set_dwarf_cmdlist,
23954 &show_dwarf_cmdlist);
23955
23956 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23957 Set debugging of the DWARF reader."), _("\
23958 Show debugging of the DWARF reader."), _("\
23959 When enabled (non-zero), debugging messages are printed during DWARF\n\
23960 reading and symtab expansion. A value of 1 (one) provides basic\n\
23961 information. A value greater than 1 provides more verbose information."),
23962 NULL,
23963 NULL,
23964 &setdebuglist, &showdebuglist);
23965
23966 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23967 Set debugging of the DWARF DIE reader."), _("\
23968 Show debugging of the DWARF DIE reader."), _("\
23969 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23970 The value is the maximum depth to print."),
23971 NULL,
23972 NULL,
23973 &setdebuglist, &showdebuglist);
23974
23975 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23976 Set debugging of the dwarf line reader."), _("\
23977 Show debugging of the dwarf line reader."), _("\
23978 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23979 A value of 1 (one) provides basic information.\n\
23980 A value greater than 1 provides more verbose information."),
23981 NULL,
23982 NULL,
23983 &setdebuglist, &showdebuglist);
23984
23985 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23986 Set cross-checking of \"physname\" code against demangler."), _("\
23987 Show cross-checking of \"physname\" code against demangler."), _("\
23988 When enabled, GDB's internal \"physname\" code is checked against\n\
23989 the demangler."),
23990 NULL, show_check_physname,
23991 &setdebuglist, &showdebuglist);
23992
23993 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23994 no_class, &use_deprecated_index_sections, _("\
23995 Set whether to use deprecated gdb_index sections."), _("\
23996 Show whether to use deprecated gdb_index sections."), _("\
23997 When enabled, deprecated .gdb_index sections are used anyway.\n\
23998 Normally they are ignored either because of a missing feature or\n\
23999 performance issue.\n\
24000 Warning: This option must be enabled before gdb reads the file."),
24001 NULL,
24002 NULL,
24003 &setlist, &showlist);
24004
24005 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24006 &dwarf2_locexpr_funcs);
24007 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24008 &dwarf2_loclist_funcs);
24009
24010 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24011 &dwarf2_block_frame_base_locexpr_funcs);
24012 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24013 &dwarf2_block_frame_base_loclist_funcs);
24014
24015 #if GDB_SELF_TEST
24016 selftests::register_test ("dw2_expand_symtabs_matching",
24017 selftests::dw2_expand_symtabs_matching::run_test);
24018 selftests::register_test ("dwarf2_find_containing_comp_unit",
24019 selftests::find_containing_comp_unit::run_test);
24020 #endif
24021 }
This page took 0.571743 seconds and 4 git commands to generate.