Pass existing_cu object to cutu_reader
[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 dwarf2_cu *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 dwarf2_cu *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_cu *cu,
1574 enum language pretend_language);
1575
1576 static void process_full_type_unit (dwarf2_cu *cu,
1577 enum language pretend_language);
1578
1579 static void dwarf2_add_dependence (struct dwarf2_cu *,
1580 struct dwarf2_per_cu_data *);
1581
1582 static void dwarf2_mark (struct dwarf2_cu *);
1583
1584 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1585
1586 static struct type *get_die_type_at_offset (sect_offset,
1587 dwarf2_per_cu_data *per_cu,
1588 dwarf2_per_objfile *per_objfile);
1589
1590 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1591
1592 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1593 dwarf2_per_objfile *per_objfile,
1594 enum language pretend_language);
1595
1596 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1597
1598 /* Class, the destructor of which frees all allocated queue entries. This
1599 will only have work to do if an error was thrown while processing the
1600 dwarf. If no error was thrown then the queue entries should have all
1601 been processed, and freed, as we went along. */
1602
1603 class dwarf2_queue_guard
1604 {
1605 public:
1606 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1607 : m_per_objfile (per_objfile)
1608 {
1609 }
1610
1611 /* Free any entries remaining on the queue. There should only be
1612 entries left if we hit an error while processing the dwarf. */
1613 ~dwarf2_queue_guard ()
1614 {
1615 /* Ensure that no memory is allocated by the queue. */
1616 std::queue<dwarf2_queue_item> empty;
1617 std::swap (m_per_objfile->per_bfd->queue, empty);
1618 }
1619
1620 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1621
1622 private:
1623 dwarf2_per_objfile *m_per_objfile;
1624 };
1625
1626 dwarf2_queue_item::~dwarf2_queue_item ()
1627 {
1628 /* Anything still marked queued is likely to be in an
1629 inconsistent state, so discard it. */
1630 if (per_cu->queued)
1631 {
1632 if (per_cu->cu != NULL)
1633 free_one_cached_comp_unit (per_cu, per_objfile);
1634 per_cu->queued = 0;
1635 }
1636 }
1637
1638 /* The return type of find_file_and_directory. Note, the enclosed
1639 string pointers are only valid while this object is valid. */
1640
1641 struct file_and_directory
1642 {
1643 /* The filename. This is never NULL. */
1644 const char *name;
1645
1646 /* The compilation directory. NULL if not known. If we needed to
1647 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1648 points directly to the DW_AT_comp_dir string attribute owned by
1649 the obstack that owns the DIE. */
1650 const char *comp_dir;
1651
1652 /* If we needed to build a new string for comp_dir, this is what
1653 owns the storage. */
1654 std::string comp_dir_storage;
1655 };
1656
1657 static file_and_directory find_file_and_directory (struct die_info *die,
1658 struct dwarf2_cu *cu);
1659
1660 static htab_up allocate_signatured_type_table ();
1661
1662 static htab_up allocate_dwo_unit_table ();
1663
1664 static struct dwo_unit *lookup_dwo_unit_in_dwp
1665 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1666 struct dwp_file *dwp_file, const char *comp_dir,
1667 ULONGEST signature, int is_debug_types);
1668
1669 static struct dwp_file *get_dwp_file
1670 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1671
1672 static struct dwo_unit *lookup_dwo_comp_unit
1673 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1674 ULONGEST signature);
1675
1676 static struct dwo_unit *lookup_dwo_type_unit
1677 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1678
1679 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1680
1681 /* A unique pointer to a dwo_file. */
1682
1683 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1684
1685 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1686
1687 static void check_producer (struct dwarf2_cu *cu);
1688
1689 static void free_line_header_voidp (void *arg);
1690 \f
1691 /* Various complaints about symbol reading that don't abort the process. */
1692
1693 static void
1694 dwarf2_debug_line_missing_file_complaint (void)
1695 {
1696 complaint (_(".debug_line section has line data without a file"));
1697 }
1698
1699 static void
1700 dwarf2_debug_line_missing_end_sequence_complaint (void)
1701 {
1702 complaint (_(".debug_line section has line "
1703 "program sequence without an end"));
1704 }
1705
1706 static void
1707 dwarf2_complex_location_expr_complaint (void)
1708 {
1709 complaint (_("location expression too complex"));
1710 }
1711
1712 static void
1713 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1714 int arg3)
1715 {
1716 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1717 arg1, arg2, arg3);
1718 }
1719
1720 static void
1721 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1722 {
1723 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1724 arg1, arg2);
1725 }
1726
1727 /* Hash function for line_header_hash. */
1728
1729 static hashval_t
1730 line_header_hash (const struct line_header *ofs)
1731 {
1732 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1733 }
1734
1735 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1736
1737 static hashval_t
1738 line_header_hash_voidp (const void *item)
1739 {
1740 const struct line_header *ofs = (const struct line_header *) item;
1741
1742 return line_header_hash (ofs);
1743 }
1744
1745 /* Equality function for line_header_hash. */
1746
1747 static int
1748 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1749 {
1750 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1751 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1752
1753 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1754 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1755 }
1756
1757 \f
1758
1759 /* See declaration. */
1760
1761 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1762 bool can_copy_)
1763 : obfd (obfd),
1764 can_copy (can_copy_)
1765 {
1766 if (names == NULL)
1767 names = &dwarf2_elf_names;
1768
1769 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1770 locate_sections (obfd, sec, *names);
1771 }
1772
1773 dwarf2_per_bfd::~dwarf2_per_bfd ()
1774 {
1775 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1776 free_cached_comp_units ();
1777
1778 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1779 per_cu->imported_symtabs_free ();
1780
1781 for (signatured_type *sig_type : all_type_units)
1782 sig_type->per_cu.imported_symtabs_free ();
1783
1784 /* Everything else should be on this->obstack. */
1785 }
1786
1787 /* See declaration. */
1788
1789 void
1790 dwarf2_per_bfd::free_cached_comp_units ()
1791 {
1792 dwarf2_per_cu_data *per_cu = read_in_chain;
1793 dwarf2_per_cu_data **last_chain = &read_in_chain;
1794 while (per_cu != NULL)
1795 {
1796 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1797
1798 delete per_cu->cu;
1799 *last_chain = next_cu;
1800 per_cu = next_cu;
1801 }
1802 }
1803
1804 /* A helper class that calls free_cached_comp_units on
1805 destruction. */
1806
1807 class free_cached_comp_units
1808 {
1809 public:
1810
1811 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1812 : m_per_objfile (per_objfile)
1813 {
1814 }
1815
1816 ~free_cached_comp_units ()
1817 {
1818 m_per_objfile->per_bfd->free_cached_comp_units ();
1819 }
1820
1821 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1822
1823 private:
1824
1825 dwarf2_per_objfile *m_per_objfile;
1826 };
1827
1828 /* See read.h. */
1829
1830 bool
1831 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1832 {
1833 gdb_assert (per_cu->index < this->m_symtabs.size ());
1834
1835 return this->m_symtabs[per_cu->index] != nullptr;
1836 }
1837
1838 /* See read.h. */
1839
1840 compunit_symtab *
1841 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1842 {
1843 gdb_assert (per_cu->index < this->m_symtabs.size ());
1844
1845 return this->m_symtabs[per_cu->index];
1846 }
1847
1848 /* See read.h. */
1849
1850 void
1851 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1852 compunit_symtab *symtab)
1853 {
1854 gdb_assert (per_cu->index < this->m_symtabs.size ());
1855 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1856
1857 this->m_symtabs[per_cu->index] = symtab;
1858 }
1859
1860 /* Try to locate the sections we need for DWARF 2 debugging
1861 information and return true if we have enough to do something.
1862 NAMES points to the dwarf2 section names, or is NULL if the standard
1863 ELF names are used. CAN_COPY is true for formats where symbol
1864 interposition is possible and so symbol values must follow copy
1865 relocation rules. */
1866
1867 int
1868 dwarf2_has_info (struct objfile *objfile,
1869 const struct dwarf2_debug_sections *names,
1870 bool can_copy)
1871 {
1872 if (objfile->flags & OBJF_READNEVER)
1873 return 0;
1874
1875 struct dwarf2_per_objfile *dwarf2_per_objfile
1876 = get_dwarf2_per_objfile (objfile);
1877
1878 if (dwarf2_per_objfile == NULL)
1879 {
1880 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1881 sharing yet). */
1882 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1883
1884 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1885 }
1886
1887 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1888 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1889 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1890 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1891 }
1892
1893 /* When loading sections, we look either for uncompressed section or for
1894 compressed section names. */
1895
1896 static int
1897 section_is_p (const char *section_name,
1898 const struct dwarf2_section_names *names)
1899 {
1900 if (names->normal != NULL
1901 && strcmp (section_name, names->normal) == 0)
1902 return 1;
1903 if (names->compressed != NULL
1904 && strcmp (section_name, names->compressed) == 0)
1905 return 1;
1906 return 0;
1907 }
1908
1909 /* See declaration. */
1910
1911 void
1912 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1913 const dwarf2_debug_sections &names)
1914 {
1915 flagword aflag = bfd_section_flags (sectp);
1916
1917 if ((aflag & SEC_HAS_CONTENTS) == 0)
1918 {
1919 }
1920 else if (elf_section_data (sectp)->this_hdr.sh_size
1921 > bfd_get_file_size (abfd))
1922 {
1923 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1924 warning (_("Discarding section %s which has a section size (%s"
1925 ") larger than the file size [in module %s]"),
1926 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1927 bfd_get_filename (abfd));
1928 }
1929 else if (section_is_p (sectp->name, &names.info))
1930 {
1931 this->info.s.section = sectp;
1932 this->info.size = bfd_section_size (sectp);
1933 }
1934 else if (section_is_p (sectp->name, &names.abbrev))
1935 {
1936 this->abbrev.s.section = sectp;
1937 this->abbrev.size = bfd_section_size (sectp);
1938 }
1939 else if (section_is_p (sectp->name, &names.line))
1940 {
1941 this->line.s.section = sectp;
1942 this->line.size = bfd_section_size (sectp);
1943 }
1944 else if (section_is_p (sectp->name, &names.loc))
1945 {
1946 this->loc.s.section = sectp;
1947 this->loc.size = bfd_section_size (sectp);
1948 }
1949 else if (section_is_p (sectp->name, &names.loclists))
1950 {
1951 this->loclists.s.section = sectp;
1952 this->loclists.size = bfd_section_size (sectp);
1953 }
1954 else if (section_is_p (sectp->name, &names.macinfo))
1955 {
1956 this->macinfo.s.section = sectp;
1957 this->macinfo.size = bfd_section_size (sectp);
1958 }
1959 else if (section_is_p (sectp->name, &names.macro))
1960 {
1961 this->macro.s.section = sectp;
1962 this->macro.size = bfd_section_size (sectp);
1963 }
1964 else if (section_is_p (sectp->name, &names.str))
1965 {
1966 this->str.s.section = sectp;
1967 this->str.size = bfd_section_size (sectp);
1968 }
1969 else if (section_is_p (sectp->name, &names.str_offsets))
1970 {
1971 this->str_offsets.s.section = sectp;
1972 this->str_offsets.size = bfd_section_size (sectp);
1973 }
1974 else if (section_is_p (sectp->name, &names.line_str))
1975 {
1976 this->line_str.s.section = sectp;
1977 this->line_str.size = bfd_section_size (sectp);
1978 }
1979 else if (section_is_p (sectp->name, &names.addr))
1980 {
1981 this->addr.s.section = sectp;
1982 this->addr.size = bfd_section_size (sectp);
1983 }
1984 else if (section_is_p (sectp->name, &names.frame))
1985 {
1986 this->frame.s.section = sectp;
1987 this->frame.size = bfd_section_size (sectp);
1988 }
1989 else if (section_is_p (sectp->name, &names.eh_frame))
1990 {
1991 this->eh_frame.s.section = sectp;
1992 this->eh_frame.size = bfd_section_size (sectp);
1993 }
1994 else if (section_is_p (sectp->name, &names.ranges))
1995 {
1996 this->ranges.s.section = sectp;
1997 this->ranges.size = bfd_section_size (sectp);
1998 }
1999 else if (section_is_p (sectp->name, &names.rnglists))
2000 {
2001 this->rnglists.s.section = sectp;
2002 this->rnglists.size = bfd_section_size (sectp);
2003 }
2004 else if (section_is_p (sectp->name, &names.types))
2005 {
2006 struct dwarf2_section_info type_section;
2007
2008 memset (&type_section, 0, sizeof (type_section));
2009 type_section.s.section = sectp;
2010 type_section.size = bfd_section_size (sectp);
2011
2012 this->types.push_back (type_section);
2013 }
2014 else if (section_is_p (sectp->name, &names.gdb_index))
2015 {
2016 this->gdb_index.s.section = sectp;
2017 this->gdb_index.size = bfd_section_size (sectp);
2018 }
2019 else if (section_is_p (sectp->name, &names.debug_names))
2020 {
2021 this->debug_names.s.section = sectp;
2022 this->debug_names.size = bfd_section_size (sectp);
2023 }
2024 else if (section_is_p (sectp->name, &names.debug_aranges))
2025 {
2026 this->debug_aranges.s.section = sectp;
2027 this->debug_aranges.size = bfd_section_size (sectp);
2028 }
2029
2030 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2031 && bfd_section_vma (sectp) == 0)
2032 this->has_section_at_zero = true;
2033 }
2034
2035 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2036 SECTION_NAME. */
2037
2038 void
2039 dwarf2_get_section_info (struct objfile *objfile,
2040 enum dwarf2_section_enum sect,
2041 asection **sectp, const gdb_byte **bufp,
2042 bfd_size_type *sizep)
2043 {
2044 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2045 struct dwarf2_section_info *info;
2046
2047 /* We may see an objfile without any DWARF, in which case we just
2048 return nothing. */
2049 if (data == NULL)
2050 {
2051 *sectp = NULL;
2052 *bufp = NULL;
2053 *sizep = 0;
2054 return;
2055 }
2056 switch (sect)
2057 {
2058 case DWARF2_DEBUG_FRAME:
2059 info = &data->per_bfd->frame;
2060 break;
2061 case DWARF2_EH_FRAME:
2062 info = &data->per_bfd->eh_frame;
2063 break;
2064 default:
2065 gdb_assert_not_reached ("unexpected section");
2066 }
2067
2068 info->read (objfile);
2069
2070 *sectp = info->get_bfd_section ();
2071 *bufp = info->buffer;
2072 *sizep = info->size;
2073 }
2074
2075 /* A helper function to find the sections for a .dwz file. */
2076
2077 static void
2078 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2079 {
2080 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2081
2082 /* Note that we only support the standard ELF names, because .dwz
2083 is ELF-only (at the time of writing). */
2084 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2085 {
2086 dwz_file->abbrev.s.section = sectp;
2087 dwz_file->abbrev.size = bfd_section_size (sectp);
2088 }
2089 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2090 {
2091 dwz_file->info.s.section = sectp;
2092 dwz_file->info.size = bfd_section_size (sectp);
2093 }
2094 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2095 {
2096 dwz_file->str.s.section = sectp;
2097 dwz_file->str.size = bfd_section_size (sectp);
2098 }
2099 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2100 {
2101 dwz_file->line.s.section = sectp;
2102 dwz_file->line.size = bfd_section_size (sectp);
2103 }
2104 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2105 {
2106 dwz_file->macro.s.section = sectp;
2107 dwz_file->macro.size = bfd_section_size (sectp);
2108 }
2109 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2110 {
2111 dwz_file->gdb_index.s.section = sectp;
2112 dwz_file->gdb_index.size = bfd_section_size (sectp);
2113 }
2114 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2115 {
2116 dwz_file->debug_names.s.section = sectp;
2117 dwz_file->debug_names.size = bfd_section_size (sectp);
2118 }
2119 }
2120
2121 /* See dwarf2read.h. */
2122
2123 struct dwz_file *
2124 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2125 {
2126 const char *filename;
2127 bfd_size_type buildid_len_arg;
2128 size_t buildid_len;
2129 bfd_byte *buildid;
2130
2131 if (per_bfd->dwz_file != NULL)
2132 return per_bfd->dwz_file.get ();
2133
2134 bfd_set_error (bfd_error_no_error);
2135 gdb::unique_xmalloc_ptr<char> data
2136 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2137 &buildid_len_arg, &buildid));
2138 if (data == NULL)
2139 {
2140 if (bfd_get_error () == bfd_error_no_error)
2141 return NULL;
2142 error (_("could not read '.gnu_debugaltlink' section: %s"),
2143 bfd_errmsg (bfd_get_error ()));
2144 }
2145
2146 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2147
2148 buildid_len = (size_t) buildid_len_arg;
2149
2150 filename = data.get ();
2151
2152 std::string abs_storage;
2153 if (!IS_ABSOLUTE_PATH (filename))
2154 {
2155 gdb::unique_xmalloc_ptr<char> abs
2156 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2157
2158 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2159 filename = abs_storage.c_str ();
2160 }
2161
2162 /* First try the file name given in the section. If that doesn't
2163 work, try to use the build-id instead. */
2164 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2165 if (dwz_bfd != NULL)
2166 {
2167 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2168 dwz_bfd.reset (nullptr);
2169 }
2170
2171 if (dwz_bfd == NULL)
2172 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2173
2174 if (dwz_bfd == nullptr)
2175 {
2176 gdb::unique_xmalloc_ptr<char> alt_filename;
2177 const char *origname = bfd_get_filename (per_bfd->obfd);
2178
2179 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2180 buildid_len,
2181 origname,
2182 &alt_filename));
2183
2184 if (fd.get () >= 0)
2185 {
2186 /* File successfully retrieved from server. */
2187 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2188
2189 if (dwz_bfd == nullptr)
2190 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2191 alt_filename.get ());
2192 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2193 dwz_bfd.reset (nullptr);
2194 }
2195 }
2196
2197 if (dwz_bfd == NULL)
2198 error (_("could not find '.gnu_debugaltlink' file for %s"),
2199 bfd_get_filename (per_bfd->obfd));
2200
2201 std::unique_ptr<struct dwz_file> result
2202 (new struct dwz_file (std::move (dwz_bfd)));
2203
2204 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2205 result.get ());
2206
2207 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2208 per_bfd->dwz_file = std::move (result);
2209 return per_bfd->dwz_file.get ();
2210 }
2211 \f
2212 /* DWARF quick_symbols_functions support. */
2213
2214 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2215 unique line tables, so we maintain a separate table of all .debug_line
2216 derived entries to support the sharing.
2217 All the quick functions need is the list of file names. We discard the
2218 line_header when we're done and don't need to record it here. */
2219 struct quick_file_names
2220 {
2221 /* The data used to construct the hash key. */
2222 struct stmt_list_hash hash;
2223
2224 /* The number of entries in file_names, real_names. */
2225 unsigned int num_file_names;
2226
2227 /* The file names from the line table, after being run through
2228 file_full_name. */
2229 const char **file_names;
2230
2231 /* The file names from the line table after being run through
2232 gdb_realpath. These are computed lazily. */
2233 const char **real_names;
2234 };
2235
2236 /* When using the index (and thus not using psymtabs), each CU has an
2237 object of this type. This is used to hold information needed by
2238 the various "quick" methods. */
2239 struct dwarf2_per_cu_quick_data
2240 {
2241 /* The file table. This can be NULL if there was no file table
2242 or it's currently not read in.
2243 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2244 struct quick_file_names *file_names;
2245
2246 /* A temporary mark bit used when iterating over all CUs in
2247 expand_symtabs_matching. */
2248 unsigned int mark : 1;
2249
2250 /* True if we've tried to read the file table and found there isn't one.
2251 There will be no point in trying to read it again next time. */
2252 unsigned int no_file_data : 1;
2253 };
2254
2255 /* Utility hash function for a stmt_list_hash. */
2256
2257 static hashval_t
2258 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2259 {
2260 hashval_t v = 0;
2261
2262 if (stmt_list_hash->dwo_unit != NULL)
2263 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2264 v += to_underlying (stmt_list_hash->line_sect_off);
2265 return v;
2266 }
2267
2268 /* Utility equality function for a stmt_list_hash. */
2269
2270 static int
2271 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2272 const struct stmt_list_hash *rhs)
2273 {
2274 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2275 return 0;
2276 if (lhs->dwo_unit != NULL
2277 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2278 return 0;
2279
2280 return lhs->line_sect_off == rhs->line_sect_off;
2281 }
2282
2283 /* Hash function for a quick_file_names. */
2284
2285 static hashval_t
2286 hash_file_name_entry (const void *e)
2287 {
2288 const struct quick_file_names *file_data
2289 = (const struct quick_file_names *) e;
2290
2291 return hash_stmt_list_entry (&file_data->hash);
2292 }
2293
2294 /* Equality function for a quick_file_names. */
2295
2296 static int
2297 eq_file_name_entry (const void *a, const void *b)
2298 {
2299 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2300 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2301
2302 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2303 }
2304
2305 /* Delete function for a quick_file_names. */
2306
2307 static void
2308 delete_file_name_entry (void *e)
2309 {
2310 struct quick_file_names *file_data = (struct quick_file_names *) e;
2311 int i;
2312
2313 for (i = 0; i < file_data->num_file_names; ++i)
2314 {
2315 xfree ((void*) file_data->file_names[i]);
2316 if (file_data->real_names)
2317 xfree ((void*) file_data->real_names[i]);
2318 }
2319
2320 /* The space for the struct itself lives on the obstack, so we don't
2321 free it here. */
2322 }
2323
2324 /* Create a quick_file_names hash table. */
2325
2326 static htab_up
2327 create_quick_file_names_table (unsigned int nr_initial_entries)
2328 {
2329 return htab_up (htab_create_alloc (nr_initial_entries,
2330 hash_file_name_entry, eq_file_name_entry,
2331 delete_file_name_entry, xcalloc, xfree));
2332 }
2333
2334 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2335 function is unrelated to symtabs, symtab would have to be created afterwards.
2336 You should call age_cached_comp_units after processing the CU. */
2337
2338 static dwarf2_cu *
2339 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2340 bool skip_partial)
2341 {
2342 if (per_cu->is_debug_types)
2343 load_full_type_unit (per_cu, per_objfile);
2344 else
2345 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2346
2347 if (per_cu->cu == nullptr)
2348 return nullptr; /* Dummy CU. */
2349
2350 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2351
2352 return 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 dwarf2_cu *cu = 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 && cu != NULL
2382 && 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 (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 per-bfd
2478 obstack, and constructed with the specified field values. */
2479
2480 static dwarf2_per_cu_data *
2481 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
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 = 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 (&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 (dwarf2_per_bfd *per_bfd,
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 (per_bfd, section, is_dwz, sect_off,
2516 length);
2517 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 PER_BFD. */
2523
2524 static void
2525 create_cus_from_index (dwarf2_per_bfd *per_bfd,
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 (per_bfd->all_comp_units.empty ());
2530 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2531
2532 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2533 &per_bfd->info, 0);
2534
2535 if (dwz_elements == 0)
2536 return;
2537
2538 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2539 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2540 &dwz->info, 1);
2541 }
2542
2543 /* Create the signatured type hash table from the index. */
2544
2545 static void
2546 create_signatured_type_table_from_index
2547 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2548 const gdb_byte *bytes, offset_type elements)
2549 {
2550 gdb_assert (per_bfd->all_type_units.empty ());
2551 per_bfd->all_type_units.reserve (elements / 3);
2552
2553 htab_up sig_types_hash = allocate_signatured_type_table ();
2554
2555 for (offset_type i = 0; i < elements; i += 3)
2556 {
2557 struct signatured_type *sig_type;
2558 ULONGEST signature;
2559 void **slot;
2560 cu_offset type_offset_in_tu;
2561
2562 gdb_static_assert (sizeof (ULONGEST) >= 8);
2563 sect_offset sect_off
2564 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2565 type_offset_in_tu
2566 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2567 BFD_ENDIAN_LITTLE);
2568 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2569 bytes += 3 * 8;
2570
2571 sig_type = per_bfd->allocate_signatured_type ();
2572 sig_type->signature = signature;
2573 sig_type->type_offset_in_tu = type_offset_in_tu;
2574 sig_type->per_cu.is_debug_types = 1;
2575 sig_type->per_cu.section = section;
2576 sig_type->per_cu.sect_off = sect_off;
2577 sig_type->per_cu.v.quick
2578 = OBSTACK_ZALLOC (&per_bfd->obstack,
2579 struct dwarf2_per_cu_quick_data);
2580
2581 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2582 *slot = sig_type;
2583
2584 per_bfd->all_type_units.push_back (sig_type);
2585 }
2586
2587 per_bfd->signatured_types = std::move (sig_types_hash);
2588 }
2589
2590 /* Create the signatured type hash table from .debug_names. */
2591
2592 static void
2593 create_signatured_type_table_from_debug_names
2594 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2595 const mapped_debug_names &map,
2596 struct dwarf2_section_info *section,
2597 struct dwarf2_section_info *abbrev_section)
2598 {
2599 struct objfile *objfile = dwarf2_per_objfile->objfile;
2600
2601 section->read (objfile);
2602 abbrev_section->read (objfile);
2603
2604 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2605 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2606
2607 htab_up sig_types_hash = allocate_signatured_type_table ();
2608
2609 for (uint32_t i = 0; i < map.tu_count; ++i)
2610 {
2611 struct signatured_type *sig_type;
2612 void **slot;
2613
2614 sect_offset sect_off
2615 = (sect_offset) (extract_unsigned_integer
2616 (map.tu_table_reordered + i * map.offset_size,
2617 map.offset_size,
2618 map.dwarf5_byte_order));
2619
2620 comp_unit_head cu_header;
2621 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2622 abbrev_section,
2623 section->buffer + to_underlying (sect_off),
2624 rcuh_kind::TYPE);
2625
2626 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2627 sig_type->signature = cu_header.signature;
2628 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2629 sig_type->per_cu.is_debug_types = 1;
2630 sig_type->per_cu.section = section;
2631 sig_type->per_cu.sect_off = sect_off;
2632 sig_type->per_cu.v.quick
2633 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2634 struct dwarf2_per_cu_quick_data);
2635
2636 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2637 *slot = sig_type;
2638
2639 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2640 }
2641
2642 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2643 }
2644
2645 /* Read the address map data from the mapped index, and use it to
2646 populate the objfile's psymtabs_addrmap. */
2647
2648 static void
2649 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2650 struct mapped_index *index)
2651 {
2652 struct objfile *objfile = dwarf2_per_objfile->objfile;
2653 struct gdbarch *gdbarch = objfile->arch ();
2654 const gdb_byte *iter, *end;
2655 struct addrmap *mutable_map;
2656 CORE_ADDR baseaddr;
2657
2658 auto_obstack temp_obstack;
2659
2660 mutable_map = addrmap_create_mutable (&temp_obstack);
2661
2662 iter = index->address_table.data ();
2663 end = iter + index->address_table.size ();
2664
2665 baseaddr = objfile->text_section_offset ();
2666
2667 while (iter < end)
2668 {
2669 ULONGEST hi, lo, cu_index;
2670 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2671 iter += 8;
2672 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2673 iter += 8;
2674 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2675 iter += 4;
2676
2677 if (lo > hi)
2678 {
2679 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2680 hex_string (lo), hex_string (hi));
2681 continue;
2682 }
2683
2684 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2685 {
2686 complaint (_(".gdb_index address table has invalid CU number %u"),
2687 (unsigned) cu_index);
2688 continue;
2689 }
2690
2691 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2692 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2693 addrmap_set_empty (mutable_map, lo, hi - 1,
2694 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2695 }
2696
2697 objfile->partial_symtabs->psymtabs_addrmap
2698 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2699 }
2700
2701 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2702 populate the objfile's psymtabs_addrmap. */
2703
2704 static void
2705 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2706 struct dwarf2_section_info *section)
2707 {
2708 struct objfile *objfile = dwarf2_per_objfile->objfile;
2709 bfd *abfd = objfile->obfd;
2710 struct gdbarch *gdbarch = objfile->arch ();
2711 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2712
2713 auto_obstack temp_obstack;
2714 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2715
2716 std::unordered_map<sect_offset,
2717 dwarf2_per_cu_data *,
2718 gdb::hash_enum<sect_offset>>
2719 debug_info_offset_to_per_cu;
2720 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2721 {
2722 const auto insertpair
2723 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2724 if (!insertpair.second)
2725 {
2726 warning (_("Section .debug_aranges in %s has duplicate "
2727 "debug_info_offset %s, ignoring .debug_aranges."),
2728 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2729 return;
2730 }
2731 }
2732
2733 section->read (objfile);
2734
2735 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2736
2737 const gdb_byte *addr = section->buffer;
2738
2739 while (addr < section->buffer + section->size)
2740 {
2741 const gdb_byte *const entry_addr = addr;
2742 unsigned int bytes_read;
2743
2744 const LONGEST entry_length = read_initial_length (abfd, addr,
2745 &bytes_read);
2746 addr += bytes_read;
2747
2748 const gdb_byte *const entry_end = addr + entry_length;
2749 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2750 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2751 if (addr + entry_length > section->buffer + section->size)
2752 {
2753 warning (_("Section .debug_aranges in %s entry at offset %s "
2754 "length %s exceeds section length %s, "
2755 "ignoring .debug_aranges."),
2756 objfile_name (objfile),
2757 plongest (entry_addr - section->buffer),
2758 plongest (bytes_read + entry_length),
2759 pulongest (section->size));
2760 return;
2761 }
2762
2763 /* The version number. */
2764 const uint16_t version = read_2_bytes (abfd, addr);
2765 addr += 2;
2766 if (version != 2)
2767 {
2768 warning (_("Section .debug_aranges in %s entry at offset %s "
2769 "has unsupported version %d, ignoring .debug_aranges."),
2770 objfile_name (objfile),
2771 plongest (entry_addr - section->buffer), version);
2772 return;
2773 }
2774
2775 const uint64_t debug_info_offset
2776 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2777 addr += offset_size;
2778 const auto per_cu_it
2779 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2780 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2781 {
2782 warning (_("Section .debug_aranges in %s entry at offset %s "
2783 "debug_info_offset %s does not exists, "
2784 "ignoring .debug_aranges."),
2785 objfile_name (objfile),
2786 plongest (entry_addr - section->buffer),
2787 pulongest (debug_info_offset));
2788 return;
2789 }
2790 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2791
2792 const uint8_t address_size = *addr++;
2793 if (address_size < 1 || address_size > 8)
2794 {
2795 warning (_("Section .debug_aranges in %s entry at offset %s "
2796 "address_size %u is invalid, ignoring .debug_aranges."),
2797 objfile_name (objfile),
2798 plongest (entry_addr - section->buffer), address_size);
2799 return;
2800 }
2801
2802 const uint8_t segment_selector_size = *addr++;
2803 if (segment_selector_size != 0)
2804 {
2805 warning (_("Section .debug_aranges in %s entry at offset %s "
2806 "segment_selector_size %u is not supported, "
2807 "ignoring .debug_aranges."),
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer),
2810 segment_selector_size);
2811 return;
2812 }
2813
2814 /* Must pad to an alignment boundary that is twice the address
2815 size. It is undocumented by the DWARF standard but GCC does
2816 use it. */
2817 for (size_t padding = ((-(addr - section->buffer))
2818 & (2 * address_size - 1));
2819 padding > 0; padding--)
2820 if (*addr++ != 0)
2821 {
2822 warning (_("Section .debug_aranges in %s entry at offset %s "
2823 "padding is not zero, ignoring .debug_aranges."),
2824 objfile_name (objfile),
2825 plongest (entry_addr - section->buffer));
2826 return;
2827 }
2828
2829 for (;;)
2830 {
2831 if (addr + 2 * address_size > entry_end)
2832 {
2833 warning (_("Section .debug_aranges in %s entry at offset %s "
2834 "address list is not properly terminated, "
2835 "ignoring .debug_aranges."),
2836 objfile_name (objfile),
2837 plongest (entry_addr - section->buffer));
2838 return;
2839 }
2840 ULONGEST start = extract_unsigned_integer (addr, address_size,
2841 dwarf5_byte_order);
2842 addr += address_size;
2843 ULONGEST length = extract_unsigned_integer (addr, address_size,
2844 dwarf5_byte_order);
2845 addr += address_size;
2846 if (start == 0 && length == 0)
2847 break;
2848 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2849 {
2850 /* Symbol was eliminated due to a COMDAT group. */
2851 continue;
2852 }
2853 ULONGEST end = start + length;
2854 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2855 - baseaddr);
2856 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2857 - baseaddr);
2858 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2859 }
2860 }
2861
2862 objfile->partial_symtabs->psymtabs_addrmap
2863 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2864 }
2865
2866 /* Find a slot in the mapped index INDEX for the object named NAME.
2867 If NAME is found, set *VEC_OUT to point to the CU vector in the
2868 constant pool and return true. If NAME cannot be found, return
2869 false. */
2870
2871 static bool
2872 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2873 offset_type **vec_out)
2874 {
2875 offset_type hash;
2876 offset_type slot, step;
2877 int (*cmp) (const char *, const char *);
2878
2879 gdb::unique_xmalloc_ptr<char> without_params;
2880 if (current_language->la_language == language_cplus
2881 || current_language->la_language == language_fortran
2882 || current_language->la_language == language_d)
2883 {
2884 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2885 not contain any. */
2886
2887 if (strchr (name, '(') != NULL)
2888 {
2889 without_params = cp_remove_params (name);
2890
2891 if (without_params != NULL)
2892 name = without_params.get ();
2893 }
2894 }
2895
2896 /* Index version 4 did not support case insensitive searches. But the
2897 indices for case insensitive languages are built in lowercase, therefore
2898 simulate our NAME being searched is also lowercased. */
2899 hash = mapped_index_string_hash ((index->version == 4
2900 && case_sensitivity == case_sensitive_off
2901 ? 5 : index->version),
2902 name);
2903
2904 slot = hash & (index->symbol_table.size () - 1);
2905 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2906 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2907
2908 for (;;)
2909 {
2910 const char *str;
2911
2912 const auto &bucket = index->symbol_table[slot];
2913 if (bucket.name == 0 && bucket.vec == 0)
2914 return false;
2915
2916 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2917 if (!cmp (name, str))
2918 {
2919 *vec_out = (offset_type *) (index->constant_pool
2920 + MAYBE_SWAP (bucket.vec));
2921 return true;
2922 }
2923
2924 slot = (slot + step) & (index->symbol_table.size () - 1);
2925 }
2926 }
2927
2928 /* A helper function that reads the .gdb_index from BUFFER and fills
2929 in MAP. FILENAME is the name of the file containing the data;
2930 it is used for error reporting. DEPRECATED_OK is true if it is
2931 ok to use deprecated sections.
2932
2933 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2934 out parameters that are filled in with information about the CU and
2935 TU lists in the section.
2936
2937 Returns true if all went well, false otherwise. */
2938
2939 static bool
2940 read_gdb_index_from_buffer (const char *filename,
2941 bool deprecated_ok,
2942 gdb::array_view<const gdb_byte> buffer,
2943 struct mapped_index *map,
2944 const gdb_byte **cu_list,
2945 offset_type *cu_list_elements,
2946 const gdb_byte **types_list,
2947 offset_type *types_list_elements)
2948 {
2949 const gdb_byte *addr = &buffer[0];
2950
2951 /* Version check. */
2952 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2953 /* Versions earlier than 3 emitted every copy of a psymbol. This
2954 causes the index to behave very poorly for certain requests. Version 3
2955 contained incomplete addrmap. So, it seems better to just ignore such
2956 indices. */
2957 if (version < 4)
2958 {
2959 static int warning_printed = 0;
2960 if (!warning_printed)
2961 {
2962 warning (_("Skipping obsolete .gdb_index section in %s."),
2963 filename);
2964 warning_printed = 1;
2965 }
2966 return 0;
2967 }
2968 /* Index version 4 uses a different hash function than index version
2969 5 and later.
2970
2971 Versions earlier than 6 did not emit psymbols for inlined
2972 functions. Using these files will cause GDB not to be able to
2973 set breakpoints on inlined functions by name, so we ignore these
2974 indices unless the user has done
2975 "set use-deprecated-index-sections on". */
2976 if (version < 6 && !deprecated_ok)
2977 {
2978 static int warning_printed = 0;
2979 if (!warning_printed)
2980 {
2981 warning (_("\
2982 Skipping deprecated .gdb_index section in %s.\n\
2983 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2984 to use the section anyway."),
2985 filename);
2986 warning_printed = 1;
2987 }
2988 return 0;
2989 }
2990 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2991 of the TU (for symbols coming from TUs),
2992 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2993 Plus gold-generated indices can have duplicate entries for global symbols,
2994 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2995 These are just performance bugs, and we can't distinguish gdb-generated
2996 indices from gold-generated ones, so issue no warning here. */
2997
2998 /* Indexes with higher version than the one supported by GDB may be no
2999 longer backward compatible. */
3000 if (version > 8)
3001 return 0;
3002
3003 map->version = version;
3004
3005 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3006
3007 int i = 0;
3008 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3009 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3010 / 8);
3011 ++i;
3012
3013 *types_list = addr + MAYBE_SWAP (metadata[i]);
3014 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3015 - MAYBE_SWAP (metadata[i]))
3016 / 8);
3017 ++i;
3018
3019 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3020 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3021 map->address_table
3022 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3023 ++i;
3024
3025 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3026 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3027 map->symbol_table
3028 = gdb::array_view<mapped_index::symbol_table_slot>
3029 ((mapped_index::symbol_table_slot *) symbol_table,
3030 (mapped_index::symbol_table_slot *) symbol_table_end);
3031
3032 ++i;
3033 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3034
3035 return 1;
3036 }
3037
3038 /* Callback types for dwarf2_read_gdb_index. */
3039
3040 typedef gdb::function_view
3041 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3042 get_gdb_index_contents_ftype;
3043 typedef gdb::function_view
3044 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3045 get_gdb_index_contents_dwz_ftype;
3046
3047 /* Read .gdb_index. If everything went ok, initialize the "quick"
3048 elements of all the CUs and return 1. Otherwise, return 0. */
3049
3050 static int
3051 dwarf2_read_gdb_index
3052 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3053 get_gdb_index_contents_ftype get_gdb_index_contents,
3054 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3055 {
3056 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3057 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3058 struct dwz_file *dwz;
3059 struct objfile *objfile = dwarf2_per_objfile->objfile;
3060
3061 gdb::array_view<const gdb_byte> main_index_contents
3062 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3063
3064 if (main_index_contents.empty ())
3065 return 0;
3066
3067 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3068 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3069 use_deprecated_index_sections,
3070 main_index_contents, map.get (), &cu_list,
3071 &cu_list_elements, &types_list,
3072 &types_list_elements))
3073 return 0;
3074
3075 /* Don't use the index if it's empty. */
3076 if (map->symbol_table.empty ())
3077 return 0;
3078
3079 /* If there is a .dwz file, read it so we can get its CU list as
3080 well. */
3081 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3082 if (dwz != NULL)
3083 {
3084 struct mapped_index dwz_map;
3085 const gdb_byte *dwz_types_ignore;
3086 offset_type dwz_types_elements_ignore;
3087
3088 gdb::array_view<const gdb_byte> dwz_index_content
3089 = get_gdb_index_contents_dwz (objfile, dwz);
3090
3091 if (dwz_index_content.empty ())
3092 return 0;
3093
3094 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3095 1, dwz_index_content, &dwz_map,
3096 &dwz_list, &dwz_list_elements,
3097 &dwz_types_ignore,
3098 &dwz_types_elements_ignore))
3099 {
3100 warning (_("could not read '.gdb_index' section from %s; skipping"),
3101 bfd_get_filename (dwz->dwz_bfd.get ()));
3102 return 0;
3103 }
3104 }
3105
3106 create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
3107 dwz_list, dwz_list_elements);
3108
3109 if (types_list_elements)
3110 {
3111 /* We can only handle a single .debug_types when we have an
3112 index. */
3113 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3114 return 0;
3115
3116 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3117
3118 create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
3119 section, types_list,
3120 types_list_elements);
3121 }
3122
3123 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3124
3125 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3126 dwarf2_per_objfile->per_bfd->using_index = 1;
3127 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3128 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3129
3130 return 1;
3131 }
3132
3133 /* die_reader_func for dw2_get_file_names. */
3134
3135 static void
3136 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3137 const gdb_byte *info_ptr,
3138 struct die_info *comp_unit_die)
3139 {
3140 struct dwarf2_cu *cu = reader->cu;
3141 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3142 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3143 struct dwarf2_per_cu_data *lh_cu;
3144 struct attribute *attr;
3145 void **slot;
3146 struct quick_file_names *qfn;
3147
3148 gdb_assert (! this_cu->is_debug_types);
3149
3150 /* Our callers never want to match partial units -- instead they
3151 will match the enclosing full CU. */
3152 if (comp_unit_die->tag == DW_TAG_partial_unit)
3153 {
3154 this_cu->v.quick->no_file_data = 1;
3155 return;
3156 }
3157
3158 lh_cu = this_cu;
3159 slot = NULL;
3160
3161 line_header_up lh;
3162 sect_offset line_offset {};
3163
3164 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3165 if (attr != nullptr)
3166 {
3167 struct quick_file_names find_entry;
3168
3169 line_offset = (sect_offset) DW_UNSND (attr);
3170
3171 /* We may have already read in this line header (TU line header sharing).
3172 If we have we're done. */
3173 find_entry.hash.dwo_unit = cu->dwo_unit;
3174 find_entry.hash.line_sect_off = line_offset;
3175 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3176 &find_entry, INSERT);
3177 if (*slot != NULL)
3178 {
3179 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3180 return;
3181 }
3182
3183 lh = dwarf_decode_line_header (line_offset, cu);
3184 }
3185 if (lh == NULL)
3186 {
3187 lh_cu->v.quick->no_file_data = 1;
3188 return;
3189 }
3190
3191 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3192 qfn->hash.dwo_unit = cu->dwo_unit;
3193 qfn->hash.line_sect_off = line_offset;
3194 gdb_assert (slot != NULL);
3195 *slot = qfn;
3196
3197 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3198
3199 int offset = 0;
3200 if (strcmp (fnd.name, "<unknown>") != 0)
3201 ++offset;
3202
3203 qfn->num_file_names = offset + lh->file_names_size ();
3204 qfn->file_names =
3205 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3206 qfn->num_file_names);
3207 if (offset != 0)
3208 qfn->file_names[0] = xstrdup (fnd.name);
3209 for (int i = 0; i < lh->file_names_size (); ++i)
3210 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3211 fnd.comp_dir).release ();
3212 qfn->real_names = NULL;
3213
3214 lh_cu->v.quick->file_names = qfn;
3215 }
3216
3217 /* A helper for the "quick" functions which attempts to read the line
3218 table for THIS_CU. */
3219
3220 static struct quick_file_names *
3221 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3222 dwarf2_per_objfile *per_objfile)
3223 {
3224 /* This should never be called for TUs. */
3225 gdb_assert (! this_cu->is_debug_types);
3226 /* Nor type unit groups. */
3227 gdb_assert (! this_cu->type_unit_group_p ());
3228
3229 if (this_cu->v.quick->file_names != NULL)
3230 return this_cu->v.quick->file_names;
3231 /* If we know there is no line data, no point in looking again. */
3232 if (this_cu->v.quick->no_file_data)
3233 return NULL;
3234
3235 cutu_reader reader (this_cu, per_objfile);
3236 if (!reader.dummy_p)
3237 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3238
3239 if (this_cu->v.quick->no_file_data)
3240 return NULL;
3241 return this_cu->v.quick->file_names;
3242 }
3243
3244 /* A helper for the "quick" functions which computes and caches the
3245 real path for a given file name from the line table. */
3246
3247 static const char *
3248 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3249 struct quick_file_names *qfn, int index)
3250 {
3251 if (qfn->real_names == NULL)
3252 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3253 qfn->num_file_names, const char *);
3254
3255 if (qfn->real_names[index] == NULL)
3256 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3257
3258 return qfn->real_names[index];
3259 }
3260
3261 static struct symtab *
3262 dw2_find_last_source_symtab (struct objfile *objfile)
3263 {
3264 struct dwarf2_per_objfile *dwarf2_per_objfile
3265 = get_dwarf2_per_objfile (objfile);
3266 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3267 compunit_symtab *cust
3268 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3269
3270 if (cust == NULL)
3271 return NULL;
3272
3273 return compunit_primary_filetab (cust);
3274 }
3275
3276 /* Traversal function for dw2_forget_cached_source_info. */
3277
3278 static int
3279 dw2_free_cached_file_names (void **slot, void *info)
3280 {
3281 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3282
3283 if (file_data->real_names)
3284 {
3285 int i;
3286
3287 for (i = 0; i < file_data->num_file_names; ++i)
3288 {
3289 xfree ((void*) file_data->real_names[i]);
3290 file_data->real_names[i] = NULL;
3291 }
3292 }
3293
3294 return 1;
3295 }
3296
3297 static void
3298 dw2_forget_cached_source_info (struct objfile *objfile)
3299 {
3300 struct dwarf2_per_objfile *dwarf2_per_objfile
3301 = get_dwarf2_per_objfile (objfile);
3302
3303 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3304 dw2_free_cached_file_names, NULL);
3305 }
3306
3307 /* Helper function for dw2_map_symtabs_matching_filename that expands
3308 the symtabs and calls the iterator. */
3309
3310 static int
3311 dw2_map_expand_apply (struct objfile *objfile,
3312 struct dwarf2_per_cu_data *per_cu,
3313 const char *name, const char *real_path,
3314 gdb::function_view<bool (symtab *)> callback)
3315 {
3316 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3317
3318 /* Don't visit already-expanded CUs. */
3319 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3320 if (per_objfile->symtab_set_p (per_cu))
3321 return 0;
3322
3323 /* This may expand more than one symtab, and we want to iterate over
3324 all of them. */
3325 dw2_instantiate_symtab (per_cu, per_objfile, false);
3326
3327 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3328 last_made, callback);
3329 }
3330
3331 /* Implementation of the map_symtabs_matching_filename method. */
3332
3333 static bool
3334 dw2_map_symtabs_matching_filename
3335 (struct objfile *objfile, const char *name, const char *real_path,
3336 gdb::function_view<bool (symtab *)> callback)
3337 {
3338 const char *name_basename = lbasename (name);
3339 struct dwarf2_per_objfile *dwarf2_per_objfile
3340 = get_dwarf2_per_objfile (objfile);
3341
3342 /* The rule is CUs specify all the files, including those used by
3343 any TU, so there's no need to scan TUs here. */
3344
3345 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3346 {
3347 /* We only need to look at symtabs not already expanded. */
3348 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3349 continue;
3350
3351 quick_file_names *file_data
3352 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3353 if (file_data == NULL)
3354 continue;
3355
3356 for (int j = 0; j < file_data->num_file_names; ++j)
3357 {
3358 const char *this_name = file_data->file_names[j];
3359 const char *this_real_name;
3360
3361 if (compare_filenames_for_search (this_name, name))
3362 {
3363 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3364 callback))
3365 return true;
3366 continue;
3367 }
3368
3369 /* Before we invoke realpath, which can get expensive when many
3370 files are involved, do a quick comparison of the basenames. */
3371 if (! basenames_may_differ
3372 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3373 continue;
3374
3375 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3376 file_data, j);
3377 if (compare_filenames_for_search (this_real_name, name))
3378 {
3379 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3380 callback))
3381 return true;
3382 continue;
3383 }
3384
3385 if (real_path != NULL)
3386 {
3387 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3388 gdb_assert (IS_ABSOLUTE_PATH (name));
3389 if (this_real_name != NULL
3390 && FILENAME_CMP (real_path, this_real_name) == 0)
3391 {
3392 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3393 callback))
3394 return true;
3395 continue;
3396 }
3397 }
3398 }
3399 }
3400
3401 return false;
3402 }
3403
3404 /* Struct used to manage iterating over all CUs looking for a symbol. */
3405
3406 struct dw2_symtab_iterator
3407 {
3408 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3409 struct dwarf2_per_objfile *dwarf2_per_objfile;
3410 /* If set, only look for symbols that match that block. Valid values are
3411 GLOBAL_BLOCK and STATIC_BLOCK. */
3412 gdb::optional<block_enum> block_index;
3413 /* The kind of symbol we're looking for. */
3414 domain_enum domain;
3415 /* The list of CUs from the index entry of the symbol,
3416 or NULL if not found. */
3417 offset_type *vec;
3418 /* The next element in VEC to look at. */
3419 int next;
3420 /* The number of elements in VEC, or zero if there is no match. */
3421 int length;
3422 /* Have we seen a global version of the symbol?
3423 If so we can ignore all further global instances.
3424 This is to work around gold/15646, inefficient gold-generated
3425 indices. */
3426 int global_seen;
3427 };
3428
3429 /* Initialize the index symtab iterator ITER. */
3430
3431 static void
3432 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3433 struct dwarf2_per_objfile *dwarf2_per_objfile,
3434 gdb::optional<block_enum> block_index,
3435 domain_enum domain,
3436 const char *name)
3437 {
3438 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3439 iter->block_index = block_index;
3440 iter->domain = domain;
3441 iter->next = 0;
3442 iter->global_seen = 0;
3443
3444 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3445
3446 /* index is NULL if OBJF_READNOW. */
3447 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3448 iter->length = MAYBE_SWAP (*iter->vec);
3449 else
3450 {
3451 iter->vec = NULL;
3452 iter->length = 0;
3453 }
3454 }
3455
3456 /* Return the next matching CU or NULL if there are no more. */
3457
3458 static struct dwarf2_per_cu_data *
3459 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3460 {
3461 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3462
3463 for ( ; iter->next < iter->length; ++iter->next)
3464 {
3465 offset_type cu_index_and_attrs =
3466 MAYBE_SWAP (iter->vec[iter->next + 1]);
3467 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3468 gdb_index_symbol_kind symbol_kind =
3469 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3470 /* Only check the symbol attributes if they're present.
3471 Indices prior to version 7 don't record them,
3472 and indices >= 7 may elide them for certain symbols
3473 (gold does this). */
3474 int attrs_valid =
3475 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3476 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3477
3478 /* Don't crash on bad data. */
3479 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3480 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3481 {
3482 complaint (_(".gdb_index entry has bad CU index"
3483 " [in module %s]"),
3484 objfile_name (dwarf2_per_objfile->objfile));
3485 continue;
3486 }
3487
3488 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3489
3490 /* Skip if already read in. */
3491 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3492 continue;
3493
3494 /* Check static vs global. */
3495 if (attrs_valid)
3496 {
3497 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3498
3499 if (iter->block_index.has_value ())
3500 {
3501 bool want_static = *iter->block_index == STATIC_BLOCK;
3502
3503 if (is_static != want_static)
3504 continue;
3505 }
3506
3507 /* Work around gold/15646. */
3508 if (!is_static && iter->global_seen)
3509 continue;
3510 if (!is_static)
3511 iter->global_seen = 1;
3512 }
3513
3514 /* Only check the symbol's kind if it has one. */
3515 if (attrs_valid)
3516 {
3517 switch (iter->domain)
3518 {
3519 case VAR_DOMAIN:
3520 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3521 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3522 /* Some types are also in VAR_DOMAIN. */
3523 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3524 continue;
3525 break;
3526 case STRUCT_DOMAIN:
3527 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3528 continue;
3529 break;
3530 case LABEL_DOMAIN:
3531 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3532 continue;
3533 break;
3534 case MODULE_DOMAIN:
3535 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3536 continue;
3537 break;
3538 default:
3539 break;
3540 }
3541 }
3542
3543 ++iter->next;
3544 return per_cu;
3545 }
3546
3547 return NULL;
3548 }
3549
3550 static struct compunit_symtab *
3551 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3552 const char *name, domain_enum domain)
3553 {
3554 struct compunit_symtab *stab_best = NULL;
3555 struct dwarf2_per_objfile *dwarf2_per_objfile
3556 = get_dwarf2_per_objfile (objfile);
3557
3558 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3559
3560 struct dw2_symtab_iterator iter;
3561 struct dwarf2_per_cu_data *per_cu;
3562
3563 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3564
3565 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3566 {
3567 struct symbol *sym, *with_opaque = NULL;
3568 struct compunit_symtab *stab
3569 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3570 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3571 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3572
3573 sym = block_find_symbol (block, name, domain,
3574 block_find_non_opaque_type_preferred,
3575 &with_opaque);
3576
3577 /* Some caution must be observed with overloaded functions
3578 and methods, since the index will not contain any overload
3579 information (but NAME might contain it). */
3580
3581 if (sym != NULL
3582 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3583 return stab;
3584 if (with_opaque != NULL
3585 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3586 stab_best = stab;
3587
3588 /* Keep looking through other CUs. */
3589 }
3590
3591 return stab_best;
3592 }
3593
3594 static void
3595 dw2_print_stats (struct objfile *objfile)
3596 {
3597 struct dwarf2_per_objfile *dwarf2_per_objfile
3598 = get_dwarf2_per_objfile (objfile);
3599 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3600 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3601 int count = 0;
3602
3603 for (int i = 0; i < total; ++i)
3604 {
3605 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3606
3607 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3608 ++count;
3609 }
3610 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3611 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3612 }
3613
3614 /* This dumps minimal information about the index.
3615 It is called via "mt print objfiles".
3616 One use is to verify .gdb_index has been loaded by the
3617 gdb.dwarf2/gdb-index.exp testcase. */
3618
3619 static void
3620 dw2_dump (struct objfile *objfile)
3621 {
3622 struct dwarf2_per_objfile *dwarf2_per_objfile
3623 = get_dwarf2_per_objfile (objfile);
3624
3625 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3626 printf_filtered (".gdb_index:");
3627 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3628 {
3629 printf_filtered (" version %d\n",
3630 dwarf2_per_objfile->per_bfd->index_table->version);
3631 }
3632 else
3633 printf_filtered (" faked for \"readnow\"\n");
3634 printf_filtered ("\n");
3635 }
3636
3637 static void
3638 dw2_expand_symtabs_for_function (struct objfile *objfile,
3639 const char *func_name)
3640 {
3641 struct dwarf2_per_objfile *dwarf2_per_objfile
3642 = get_dwarf2_per_objfile (objfile);
3643
3644 struct dw2_symtab_iterator iter;
3645 struct dwarf2_per_cu_data *per_cu;
3646
3647 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3648
3649 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3650 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3651
3652 }
3653
3654 static void
3655 dw2_expand_all_symtabs (struct objfile *objfile)
3656 {
3657 struct dwarf2_per_objfile *dwarf2_per_objfile
3658 = get_dwarf2_per_objfile (objfile);
3659 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3660 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3661
3662 for (int i = 0; i < total_units; ++i)
3663 {
3664 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3665
3666 /* We don't want to directly expand a partial CU, because if we
3667 read it with the wrong language, then assertion failures can
3668 be triggered later on. See PR symtab/23010. So, tell
3669 dw2_instantiate_symtab to skip partial CUs -- any important
3670 partial CU will be read via DW_TAG_imported_unit anyway. */
3671 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3672 }
3673 }
3674
3675 static void
3676 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3677 const char *fullname)
3678 {
3679 struct dwarf2_per_objfile *dwarf2_per_objfile
3680 = get_dwarf2_per_objfile (objfile);
3681
3682 /* We don't need to consider type units here.
3683 This is only called for examining code, e.g. expand_line_sal.
3684 There can be an order of magnitude (or more) more type units
3685 than comp units, and we avoid them if we can. */
3686
3687 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3688 {
3689 /* We only need to look at symtabs not already expanded. */
3690 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3691 continue;
3692
3693 quick_file_names *file_data
3694 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3695 if (file_data == NULL)
3696 continue;
3697
3698 for (int j = 0; j < file_data->num_file_names; ++j)
3699 {
3700 const char *this_fullname = file_data->file_names[j];
3701
3702 if (filename_cmp (this_fullname, fullname) == 0)
3703 {
3704 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3705 break;
3706 }
3707 }
3708 }
3709 }
3710
3711 static void
3712 dw2_expand_symtabs_matching_symbol
3713 (mapped_index_base &index,
3714 const lookup_name_info &lookup_name_in,
3715 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3716 enum search_domain kind,
3717 gdb::function_view<bool (offset_type)> match_callback);
3718
3719 static void
3720 dw2_expand_symtabs_matching_one
3721 (dwarf2_per_cu_data *per_cu,
3722 dwarf2_per_objfile *per_objfile,
3723 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3724 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3725
3726 static void
3727 dw2_map_matching_symbols
3728 (struct objfile *objfile,
3729 const lookup_name_info &name, domain_enum domain,
3730 int global,
3731 gdb::function_view<symbol_found_callback_ftype> callback,
3732 symbol_compare_ftype *ordered_compare)
3733 {
3734 /* Used for Ada. */
3735 struct dwarf2_per_objfile *dwarf2_per_objfile
3736 = get_dwarf2_per_objfile (objfile);
3737
3738 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3739
3740 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3741 {
3742 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3743 here though if the current language is Ada for a non-Ada objfile
3744 using GNU index. */
3745 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3746
3747 const char *match_name = name.ada ().lookup_name ().c_str ();
3748 auto matcher = [&] (const char *symname)
3749 {
3750 if (ordered_compare == nullptr)
3751 return true;
3752 return ordered_compare (symname, match_name) == 0;
3753 };
3754
3755 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3756 [&] (offset_type namei)
3757 {
3758 struct dw2_symtab_iterator iter;
3759 struct dwarf2_per_cu_data *per_cu;
3760
3761 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3762 match_name);
3763 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3764 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3765 nullptr);
3766 return true;
3767 });
3768 }
3769 else
3770 {
3771 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3772 proceed assuming all symtabs have been read in. */
3773 }
3774
3775 for (compunit_symtab *cust : objfile->compunits ())
3776 {
3777 const struct block *block;
3778
3779 if (cust == NULL)
3780 continue;
3781 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3782 if (!iterate_over_symbols_terminated (block, name,
3783 domain, callback))
3784 return;
3785 }
3786 }
3787
3788 /* Starting from a search name, return the string that finds the upper
3789 bound of all strings that start with SEARCH_NAME in a sorted name
3790 list. Returns the empty string to indicate that the upper bound is
3791 the end of the list. */
3792
3793 static std::string
3794 make_sort_after_prefix_name (const char *search_name)
3795 {
3796 /* When looking to complete "func", we find the upper bound of all
3797 symbols that start with "func" by looking for where we'd insert
3798 the closest string that would follow "func" in lexicographical
3799 order. Usually, that's "func"-with-last-character-incremented,
3800 i.e. "fund". Mind non-ASCII characters, though. Usually those
3801 will be UTF-8 multi-byte sequences, but we can't be certain.
3802 Especially mind the 0xff character, which is a valid character in
3803 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3804 rule out compilers allowing it in identifiers. Note that
3805 conveniently, strcmp/strcasecmp are specified to compare
3806 characters interpreted as unsigned char. So what we do is treat
3807 the whole string as a base 256 number composed of a sequence of
3808 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3809 to 0, and carries 1 to the following more-significant position.
3810 If the very first character in SEARCH_NAME ends up incremented
3811 and carries/overflows, then the upper bound is the end of the
3812 list. The string after the empty string is also the empty
3813 string.
3814
3815 Some examples of this operation:
3816
3817 SEARCH_NAME => "+1" RESULT
3818
3819 "abc" => "abd"
3820 "ab\xff" => "ac"
3821 "\xff" "a" "\xff" => "\xff" "b"
3822 "\xff" => ""
3823 "\xff\xff" => ""
3824 "" => ""
3825
3826 Then, with these symbols for example:
3827
3828 func
3829 func1
3830 fund
3831
3832 completing "func" looks for symbols between "func" and
3833 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3834 which finds "func" and "func1", but not "fund".
3835
3836 And with:
3837
3838 funcÿ (Latin1 'ÿ' [0xff])
3839 funcÿ1
3840 fund
3841
3842 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3843 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3844
3845 And with:
3846
3847 ÿÿ (Latin1 'ÿ' [0xff])
3848 ÿÿ1
3849
3850 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3851 the end of the list.
3852 */
3853 std::string after = search_name;
3854 while (!after.empty () && (unsigned char) after.back () == 0xff)
3855 after.pop_back ();
3856 if (!after.empty ())
3857 after.back () = (unsigned char) after.back () + 1;
3858 return after;
3859 }
3860
3861 /* See declaration. */
3862
3863 std::pair<std::vector<name_component>::const_iterator,
3864 std::vector<name_component>::const_iterator>
3865 mapped_index_base::find_name_components_bounds
3866 (const lookup_name_info &lookup_name_without_params, language lang) const
3867 {
3868 auto *name_cmp
3869 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3870
3871 const char *lang_name
3872 = lookup_name_without_params.language_lookup_name (lang);
3873
3874 /* Comparison function object for lower_bound that matches against a
3875 given symbol name. */
3876 auto lookup_compare_lower = [&] (const name_component &elem,
3877 const char *name)
3878 {
3879 const char *elem_qualified = this->symbol_name_at (elem.idx);
3880 const char *elem_name = elem_qualified + elem.name_offset;
3881 return name_cmp (elem_name, name) < 0;
3882 };
3883
3884 /* Comparison function object for upper_bound that matches against a
3885 given symbol name. */
3886 auto lookup_compare_upper = [&] (const char *name,
3887 const name_component &elem)
3888 {
3889 const char *elem_qualified = this->symbol_name_at (elem.idx);
3890 const char *elem_name = elem_qualified + elem.name_offset;
3891 return name_cmp (name, elem_name) < 0;
3892 };
3893
3894 auto begin = this->name_components.begin ();
3895 auto end = this->name_components.end ();
3896
3897 /* Find the lower bound. */
3898 auto lower = [&] ()
3899 {
3900 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3901 return begin;
3902 else
3903 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3904 } ();
3905
3906 /* Find the upper bound. */
3907 auto upper = [&] ()
3908 {
3909 if (lookup_name_without_params.completion_mode ())
3910 {
3911 /* In completion mode, we want UPPER to point past all
3912 symbols names that have the same prefix. I.e., with
3913 these symbols, and completing "func":
3914
3915 function << lower bound
3916 function1
3917 other_function << upper bound
3918
3919 We find the upper bound by looking for the insertion
3920 point of "func"-with-last-character-incremented,
3921 i.e. "fund". */
3922 std::string after = make_sort_after_prefix_name (lang_name);
3923 if (after.empty ())
3924 return end;
3925 return std::lower_bound (lower, end, after.c_str (),
3926 lookup_compare_lower);
3927 }
3928 else
3929 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3930 } ();
3931
3932 return {lower, upper};
3933 }
3934
3935 /* See declaration. */
3936
3937 void
3938 mapped_index_base::build_name_components ()
3939 {
3940 if (!this->name_components.empty ())
3941 return;
3942
3943 this->name_components_casing = case_sensitivity;
3944 auto *name_cmp
3945 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3946
3947 /* The code below only knows how to break apart components of C++
3948 symbol names (and other languages that use '::' as
3949 namespace/module separator) and Ada symbol names. */
3950 auto count = this->symbol_name_count ();
3951 for (offset_type idx = 0; idx < count; idx++)
3952 {
3953 if (this->symbol_name_slot_invalid (idx))
3954 continue;
3955
3956 const char *name = this->symbol_name_at (idx);
3957
3958 /* Add each name component to the name component table. */
3959 unsigned int previous_len = 0;
3960
3961 if (strstr (name, "::") != nullptr)
3962 {
3963 for (unsigned int current_len = cp_find_first_component (name);
3964 name[current_len] != '\0';
3965 current_len += cp_find_first_component (name + current_len))
3966 {
3967 gdb_assert (name[current_len] == ':');
3968 this->name_components.push_back ({previous_len, idx});
3969 /* Skip the '::'. */
3970 current_len += 2;
3971 previous_len = current_len;
3972 }
3973 }
3974 else
3975 {
3976 /* Handle the Ada encoded (aka mangled) form here. */
3977 for (const char *iter = strstr (name, "__");
3978 iter != nullptr;
3979 iter = strstr (iter, "__"))
3980 {
3981 this->name_components.push_back ({previous_len, idx});
3982 iter += 2;
3983 previous_len = iter - name;
3984 }
3985 }
3986
3987 this->name_components.push_back ({previous_len, idx});
3988 }
3989
3990 /* Sort name_components elements by name. */
3991 auto name_comp_compare = [&] (const name_component &left,
3992 const name_component &right)
3993 {
3994 const char *left_qualified = this->symbol_name_at (left.idx);
3995 const char *right_qualified = this->symbol_name_at (right.idx);
3996
3997 const char *left_name = left_qualified + left.name_offset;
3998 const char *right_name = right_qualified + right.name_offset;
3999
4000 return name_cmp (left_name, right_name) < 0;
4001 };
4002
4003 std::sort (this->name_components.begin (),
4004 this->name_components.end (),
4005 name_comp_compare);
4006 }
4007
4008 /* Helper for dw2_expand_symtabs_matching that works with a
4009 mapped_index_base instead of the containing objfile. This is split
4010 to a separate function in order to be able to unit test the
4011 name_components matching using a mock mapped_index_base. For each
4012 symbol name that matches, calls MATCH_CALLBACK, passing it the
4013 symbol's index in the mapped_index_base symbol table. */
4014
4015 static void
4016 dw2_expand_symtabs_matching_symbol
4017 (mapped_index_base &index,
4018 const lookup_name_info &lookup_name_in,
4019 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4020 enum search_domain kind,
4021 gdb::function_view<bool (offset_type)> match_callback)
4022 {
4023 lookup_name_info lookup_name_without_params
4024 = lookup_name_in.make_ignore_params ();
4025
4026 /* Build the symbol name component sorted vector, if we haven't
4027 yet. */
4028 index.build_name_components ();
4029
4030 /* The same symbol may appear more than once in the range though.
4031 E.g., if we're looking for symbols that complete "w", and we have
4032 a symbol named "w1::w2", we'll find the two name components for
4033 that same symbol in the range. To be sure we only call the
4034 callback once per symbol, we first collect the symbol name
4035 indexes that matched in a temporary vector and ignore
4036 duplicates. */
4037 std::vector<offset_type> matches;
4038
4039 struct name_and_matcher
4040 {
4041 symbol_name_matcher_ftype *matcher;
4042 const char *name;
4043
4044 bool operator== (const name_and_matcher &other) const
4045 {
4046 return matcher == other.matcher && strcmp (name, other.name) == 0;
4047 }
4048 };
4049
4050 /* A vector holding all the different symbol name matchers, for all
4051 languages. */
4052 std::vector<name_and_matcher> matchers;
4053
4054 for (int i = 0; i < nr_languages; i++)
4055 {
4056 enum language lang_e = (enum language) i;
4057
4058 const language_defn *lang = language_def (lang_e);
4059 symbol_name_matcher_ftype *name_matcher
4060 = get_symbol_name_matcher (lang, lookup_name_without_params);
4061
4062 name_and_matcher key {
4063 name_matcher,
4064 lookup_name_without_params.language_lookup_name (lang_e)
4065 };
4066
4067 /* Don't insert the same comparison routine more than once.
4068 Note that we do this linear walk. This is not a problem in
4069 practice because the number of supported languages is
4070 low. */
4071 if (std::find (matchers.begin (), matchers.end (), key)
4072 != matchers.end ())
4073 continue;
4074 matchers.push_back (std::move (key));
4075
4076 auto bounds
4077 = index.find_name_components_bounds (lookup_name_without_params,
4078 lang_e);
4079
4080 /* Now for each symbol name in range, check to see if we have a name
4081 match, and if so, call the MATCH_CALLBACK callback. */
4082
4083 for (; bounds.first != bounds.second; ++bounds.first)
4084 {
4085 const char *qualified = index.symbol_name_at (bounds.first->idx);
4086
4087 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4088 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4089 continue;
4090
4091 matches.push_back (bounds.first->idx);
4092 }
4093 }
4094
4095 std::sort (matches.begin (), matches.end ());
4096
4097 /* Finally call the callback, once per match. */
4098 ULONGEST prev = -1;
4099 for (offset_type idx : matches)
4100 {
4101 if (prev != idx)
4102 {
4103 if (!match_callback (idx))
4104 break;
4105 prev = idx;
4106 }
4107 }
4108
4109 /* Above we use a type wider than idx's for 'prev', since 0 and
4110 (offset_type)-1 are both possible values. */
4111 static_assert (sizeof (prev) > sizeof (offset_type), "");
4112 }
4113
4114 #if GDB_SELF_TEST
4115
4116 namespace selftests { namespace dw2_expand_symtabs_matching {
4117
4118 /* A mock .gdb_index/.debug_names-like name index table, enough to
4119 exercise dw2_expand_symtabs_matching_symbol, which works with the
4120 mapped_index_base interface. Builds an index from the symbol list
4121 passed as parameter to the constructor. */
4122 class mock_mapped_index : public mapped_index_base
4123 {
4124 public:
4125 mock_mapped_index (gdb::array_view<const char *> symbols)
4126 : m_symbol_table (symbols)
4127 {}
4128
4129 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4130
4131 /* Return the number of names in the symbol table. */
4132 size_t symbol_name_count () const override
4133 {
4134 return m_symbol_table.size ();
4135 }
4136
4137 /* Get the name of the symbol at IDX in the symbol table. */
4138 const char *symbol_name_at (offset_type idx) const override
4139 {
4140 return m_symbol_table[idx];
4141 }
4142
4143 private:
4144 gdb::array_view<const char *> m_symbol_table;
4145 };
4146
4147 /* Convenience function that converts a NULL pointer to a "<null>"
4148 string, to pass to print routines. */
4149
4150 static const char *
4151 string_or_null (const char *str)
4152 {
4153 return str != NULL ? str : "<null>";
4154 }
4155
4156 /* Check if a lookup_name_info built from
4157 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4158 index. EXPECTED_LIST is the list of expected matches, in expected
4159 matching order. If no match expected, then an empty list is
4160 specified. Returns true on success. On failure prints a warning
4161 indicating the file:line that failed, and returns false. */
4162
4163 static bool
4164 check_match (const char *file, int line,
4165 mock_mapped_index &mock_index,
4166 const char *name, symbol_name_match_type match_type,
4167 bool completion_mode,
4168 std::initializer_list<const char *> expected_list)
4169 {
4170 lookup_name_info lookup_name (name, match_type, completion_mode);
4171
4172 bool matched = true;
4173
4174 auto mismatch = [&] (const char *expected_str,
4175 const char *got)
4176 {
4177 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4178 "expected=\"%s\", got=\"%s\"\n"),
4179 file, line,
4180 (match_type == symbol_name_match_type::FULL
4181 ? "FULL" : "WILD"),
4182 name, string_or_null (expected_str), string_or_null (got));
4183 matched = false;
4184 };
4185
4186 auto expected_it = expected_list.begin ();
4187 auto expected_end = expected_list.end ();
4188
4189 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4190 NULL, ALL_DOMAIN,
4191 [&] (offset_type idx)
4192 {
4193 const char *matched_name = mock_index.symbol_name_at (idx);
4194 const char *expected_str
4195 = expected_it == expected_end ? NULL : *expected_it++;
4196
4197 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4198 mismatch (expected_str, matched_name);
4199 return true;
4200 });
4201
4202 const char *expected_str
4203 = expected_it == expected_end ? NULL : *expected_it++;
4204 if (expected_str != NULL)
4205 mismatch (expected_str, NULL);
4206
4207 return matched;
4208 }
4209
4210 /* The symbols added to the mock mapped_index for testing (in
4211 canonical form). */
4212 static const char *test_symbols[] = {
4213 "function",
4214 "std::bar",
4215 "std::zfunction",
4216 "std::zfunction2",
4217 "w1::w2",
4218 "ns::foo<char*>",
4219 "ns::foo<int>",
4220 "ns::foo<long>",
4221 "ns2::tmpl<int>::foo2",
4222 "(anonymous namespace)::A::B::C",
4223
4224 /* These are used to check that the increment-last-char in the
4225 matching algorithm for completion doesn't match "t1_fund" when
4226 completing "t1_func". */
4227 "t1_func",
4228 "t1_func1",
4229 "t1_fund",
4230 "t1_fund1",
4231
4232 /* A UTF-8 name with multi-byte sequences to make sure that
4233 cp-name-parser understands this as a single identifier ("função"
4234 is "function" in PT). */
4235 u8"u8função",
4236
4237 /* \377 (0xff) is Latin1 'ÿ'. */
4238 "yfunc\377",
4239
4240 /* \377 (0xff) is Latin1 'ÿ'. */
4241 "\377",
4242 "\377\377123",
4243
4244 /* A name with all sorts of complications. Starts with "z" to make
4245 it easier for the completion tests below. */
4246 #define Z_SYM_NAME \
4247 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4248 "::tuple<(anonymous namespace)::ui*, " \
4249 "std::default_delete<(anonymous namespace)::ui>, void>"
4250
4251 Z_SYM_NAME
4252 };
4253
4254 /* Returns true if the mapped_index_base::find_name_component_bounds
4255 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4256 in completion mode. */
4257
4258 static bool
4259 check_find_bounds_finds (mapped_index_base &index,
4260 const char *search_name,
4261 gdb::array_view<const char *> expected_syms)
4262 {
4263 lookup_name_info lookup_name (search_name,
4264 symbol_name_match_type::FULL, true);
4265
4266 auto bounds = index.find_name_components_bounds (lookup_name,
4267 language_cplus);
4268
4269 size_t distance = std::distance (bounds.first, bounds.second);
4270 if (distance != expected_syms.size ())
4271 return false;
4272
4273 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4274 {
4275 auto nc_elem = bounds.first + exp_elem;
4276 const char *qualified = index.symbol_name_at (nc_elem->idx);
4277 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4278 return false;
4279 }
4280
4281 return true;
4282 }
4283
4284 /* Test the lower-level mapped_index::find_name_component_bounds
4285 method. */
4286
4287 static void
4288 test_mapped_index_find_name_component_bounds ()
4289 {
4290 mock_mapped_index mock_index (test_symbols);
4291
4292 mock_index.build_name_components ();
4293
4294 /* Test the lower-level mapped_index::find_name_component_bounds
4295 method in completion mode. */
4296 {
4297 static const char *expected_syms[] = {
4298 "t1_func",
4299 "t1_func1",
4300 };
4301
4302 SELF_CHECK (check_find_bounds_finds (mock_index,
4303 "t1_func", expected_syms));
4304 }
4305
4306 /* Check that the increment-last-char in the name matching algorithm
4307 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4308 {
4309 static const char *expected_syms1[] = {
4310 "\377",
4311 "\377\377123",
4312 };
4313 SELF_CHECK (check_find_bounds_finds (mock_index,
4314 "\377", expected_syms1));
4315
4316 static const char *expected_syms2[] = {
4317 "\377\377123",
4318 };
4319 SELF_CHECK (check_find_bounds_finds (mock_index,
4320 "\377\377", expected_syms2));
4321 }
4322 }
4323
4324 /* Test dw2_expand_symtabs_matching_symbol. */
4325
4326 static void
4327 test_dw2_expand_symtabs_matching_symbol ()
4328 {
4329 mock_mapped_index mock_index (test_symbols);
4330
4331 /* We let all tests run until the end even if some fails, for debug
4332 convenience. */
4333 bool any_mismatch = false;
4334
4335 /* Create the expected symbols list (an initializer_list). Needed
4336 because lists have commas, and we need to pass them to CHECK,
4337 which is a macro. */
4338 #define EXPECT(...) { __VA_ARGS__ }
4339
4340 /* Wrapper for check_match that passes down the current
4341 __FILE__/__LINE__. */
4342 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4343 any_mismatch |= !check_match (__FILE__, __LINE__, \
4344 mock_index, \
4345 NAME, MATCH_TYPE, COMPLETION_MODE, \
4346 EXPECTED_LIST)
4347
4348 /* Identity checks. */
4349 for (const char *sym : test_symbols)
4350 {
4351 /* Should be able to match all existing symbols. */
4352 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4353 EXPECT (sym));
4354
4355 /* Should be able to match all existing symbols with
4356 parameters. */
4357 std::string with_params = std::string (sym) + "(int)";
4358 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4359 EXPECT (sym));
4360
4361 /* Should be able to match all existing symbols with
4362 parameters and qualifiers. */
4363 with_params = std::string (sym) + " ( int ) const";
4364 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4365 EXPECT (sym));
4366
4367 /* This should really find sym, but cp-name-parser.y doesn't
4368 know about lvalue/rvalue qualifiers yet. */
4369 with_params = std::string (sym) + " ( int ) &&";
4370 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4371 {});
4372 }
4373
4374 /* Check that the name matching algorithm for completion doesn't get
4375 confused with Latin1 'ÿ' / 0xff. */
4376 {
4377 static const char str[] = "\377";
4378 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4379 EXPECT ("\377", "\377\377123"));
4380 }
4381
4382 /* Check that the increment-last-char in the matching algorithm for
4383 completion doesn't match "t1_fund" when completing "t1_func". */
4384 {
4385 static const char str[] = "t1_func";
4386 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4387 EXPECT ("t1_func", "t1_func1"));
4388 }
4389
4390 /* Check that completion mode works at each prefix of the expected
4391 symbol name. */
4392 {
4393 static const char str[] = "function(int)";
4394 size_t len = strlen (str);
4395 std::string lookup;
4396
4397 for (size_t i = 1; i < len; i++)
4398 {
4399 lookup.assign (str, i);
4400 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4401 EXPECT ("function"));
4402 }
4403 }
4404
4405 /* While "w" is a prefix of both components, the match function
4406 should still only be called once. */
4407 {
4408 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4409 EXPECT ("w1::w2"));
4410 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4411 EXPECT ("w1::w2"));
4412 }
4413
4414 /* Same, with a "complicated" symbol. */
4415 {
4416 static const char str[] = Z_SYM_NAME;
4417 size_t len = strlen (str);
4418 std::string lookup;
4419
4420 for (size_t i = 1; i < len; i++)
4421 {
4422 lookup.assign (str, i);
4423 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4424 EXPECT (Z_SYM_NAME));
4425 }
4426 }
4427
4428 /* In FULL mode, an incomplete symbol doesn't match. */
4429 {
4430 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4431 {});
4432 }
4433
4434 /* A complete symbol with parameters matches any overload, since the
4435 index has no overload info. */
4436 {
4437 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4438 EXPECT ("std::zfunction", "std::zfunction2"));
4439 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4440 EXPECT ("std::zfunction", "std::zfunction2"));
4441 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4442 EXPECT ("std::zfunction", "std::zfunction2"));
4443 }
4444
4445 /* Check that whitespace is ignored appropriately. A symbol with a
4446 template argument list. */
4447 {
4448 static const char expected[] = "ns::foo<int>";
4449 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4450 EXPECT (expected));
4451 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4452 EXPECT (expected));
4453 }
4454
4455 /* Check that whitespace is ignored appropriately. A symbol with a
4456 template argument list that includes a pointer. */
4457 {
4458 static const char expected[] = "ns::foo<char*>";
4459 /* Try both completion and non-completion modes. */
4460 static const bool completion_mode[2] = {false, true};
4461 for (size_t i = 0; i < 2; i++)
4462 {
4463 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4464 completion_mode[i], EXPECT (expected));
4465 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4466 completion_mode[i], EXPECT (expected));
4467
4468 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4469 completion_mode[i], EXPECT (expected));
4470 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4471 completion_mode[i], EXPECT (expected));
4472 }
4473 }
4474
4475 {
4476 /* Check method qualifiers are ignored. */
4477 static const char expected[] = "ns::foo<char*>";
4478 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4479 symbol_name_match_type::FULL, true, EXPECT (expected));
4480 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4481 symbol_name_match_type::FULL, true, EXPECT (expected));
4482 CHECK_MATCH ("foo < char * > ( int ) const",
4483 symbol_name_match_type::WILD, true, EXPECT (expected));
4484 CHECK_MATCH ("foo < char * > ( int ) &&",
4485 symbol_name_match_type::WILD, true, EXPECT (expected));
4486 }
4487
4488 /* Test lookup names that don't match anything. */
4489 {
4490 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4491 {});
4492
4493 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4494 {});
4495 }
4496
4497 /* Some wild matching tests, exercising "(anonymous namespace)",
4498 which should not be confused with a parameter list. */
4499 {
4500 static const char *syms[] = {
4501 "A::B::C",
4502 "B::C",
4503 "C",
4504 "A :: B :: C ( int )",
4505 "B :: C ( int )",
4506 "C ( int )",
4507 };
4508
4509 for (const char *s : syms)
4510 {
4511 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4512 EXPECT ("(anonymous namespace)::A::B::C"));
4513 }
4514 }
4515
4516 {
4517 static const char expected[] = "ns2::tmpl<int>::foo2";
4518 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4519 EXPECT (expected));
4520 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4521 EXPECT (expected));
4522 }
4523
4524 SELF_CHECK (!any_mismatch);
4525
4526 #undef EXPECT
4527 #undef CHECK_MATCH
4528 }
4529
4530 static void
4531 run_test ()
4532 {
4533 test_mapped_index_find_name_component_bounds ();
4534 test_dw2_expand_symtabs_matching_symbol ();
4535 }
4536
4537 }} // namespace selftests::dw2_expand_symtabs_matching
4538
4539 #endif /* GDB_SELF_TEST */
4540
4541 /* If FILE_MATCHER is NULL or if PER_CU has
4542 dwarf2_per_cu_quick_data::MARK set (see
4543 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4544 EXPANSION_NOTIFY on it. */
4545
4546 static void
4547 dw2_expand_symtabs_matching_one
4548 (dwarf2_per_cu_data *per_cu,
4549 dwarf2_per_objfile *per_objfile,
4550 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4551 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4552 {
4553 if (file_matcher == NULL || per_cu->v.quick->mark)
4554 {
4555 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4556
4557 compunit_symtab *symtab
4558 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4559 gdb_assert (symtab != nullptr);
4560
4561 if (expansion_notify != NULL && symtab_was_null)
4562 expansion_notify (symtab);
4563 }
4564 }
4565
4566 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4567 matched, to expand corresponding CUs that were marked. IDX is the
4568 index of the symbol name that matched. */
4569
4570 static void
4571 dw2_expand_marked_cus
4572 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4573 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4574 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4575 search_domain kind)
4576 {
4577 offset_type *vec, vec_len, vec_idx;
4578 bool global_seen = false;
4579 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4580
4581 vec = (offset_type *) (index.constant_pool
4582 + MAYBE_SWAP (index.symbol_table[idx].vec));
4583 vec_len = MAYBE_SWAP (vec[0]);
4584 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4585 {
4586 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4587 /* This value is only valid for index versions >= 7. */
4588 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4589 gdb_index_symbol_kind symbol_kind =
4590 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4591 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4592 /* Only check the symbol attributes if they're present.
4593 Indices prior to version 7 don't record them,
4594 and indices >= 7 may elide them for certain symbols
4595 (gold does this). */
4596 int attrs_valid =
4597 (index.version >= 7
4598 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4599
4600 /* Work around gold/15646. */
4601 if (attrs_valid)
4602 {
4603 if (!is_static && global_seen)
4604 continue;
4605 if (!is_static)
4606 global_seen = true;
4607 }
4608
4609 /* Only check the symbol's kind if it has one. */
4610 if (attrs_valid)
4611 {
4612 switch (kind)
4613 {
4614 case VARIABLES_DOMAIN:
4615 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4616 continue;
4617 break;
4618 case FUNCTIONS_DOMAIN:
4619 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4620 continue;
4621 break;
4622 case TYPES_DOMAIN:
4623 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4624 continue;
4625 break;
4626 case MODULES_DOMAIN:
4627 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4628 continue;
4629 break;
4630 default:
4631 break;
4632 }
4633 }
4634
4635 /* Don't crash on bad data. */
4636 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4637 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4638 {
4639 complaint (_(".gdb_index entry has bad CU index"
4640 " [in module %s]"),
4641 objfile_name (dwarf2_per_objfile->objfile));
4642 continue;
4643 }
4644
4645 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4646 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4647 expansion_notify);
4648 }
4649 }
4650
4651 /* If FILE_MATCHER is non-NULL, set all the
4652 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4653 that match FILE_MATCHER. */
4654
4655 static void
4656 dw_expand_symtabs_matching_file_matcher
4657 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4658 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4659 {
4660 if (file_matcher == NULL)
4661 return;
4662
4663 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4664 htab_eq_pointer,
4665 NULL, xcalloc, xfree));
4666 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4667 htab_eq_pointer,
4668 NULL, xcalloc, xfree));
4669
4670 /* The rule is CUs specify all the files, including those used by
4671 any TU, so there's no need to scan TUs here. */
4672
4673 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4674 {
4675 QUIT;
4676
4677 per_cu->v.quick->mark = 0;
4678
4679 /* We only need to look at symtabs not already expanded. */
4680 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4681 continue;
4682
4683 quick_file_names *file_data
4684 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4685 if (file_data == NULL)
4686 continue;
4687
4688 if (htab_find (visited_not_found.get (), file_data) != NULL)
4689 continue;
4690 else if (htab_find (visited_found.get (), file_data) != NULL)
4691 {
4692 per_cu->v.quick->mark = 1;
4693 continue;
4694 }
4695
4696 for (int j = 0; j < file_data->num_file_names; ++j)
4697 {
4698 const char *this_real_name;
4699
4700 if (file_matcher (file_data->file_names[j], false))
4701 {
4702 per_cu->v.quick->mark = 1;
4703 break;
4704 }
4705
4706 /* Before we invoke realpath, which can get expensive when many
4707 files are involved, do a quick comparison of the basenames. */
4708 if (!basenames_may_differ
4709 && !file_matcher (lbasename (file_data->file_names[j]),
4710 true))
4711 continue;
4712
4713 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4714 file_data, j);
4715 if (file_matcher (this_real_name, false))
4716 {
4717 per_cu->v.quick->mark = 1;
4718 break;
4719 }
4720 }
4721
4722 void **slot = htab_find_slot (per_cu->v.quick->mark
4723 ? visited_found.get ()
4724 : visited_not_found.get (),
4725 file_data, INSERT);
4726 *slot = file_data;
4727 }
4728 }
4729
4730 static void
4731 dw2_expand_symtabs_matching
4732 (struct objfile *objfile,
4733 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4734 const lookup_name_info *lookup_name,
4735 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4736 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4737 enum search_domain kind)
4738 {
4739 struct dwarf2_per_objfile *dwarf2_per_objfile
4740 = get_dwarf2_per_objfile (objfile);
4741
4742 /* index_table is NULL if OBJF_READNOW. */
4743 if (!dwarf2_per_objfile->per_bfd->index_table)
4744 return;
4745
4746 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4747
4748 if (symbol_matcher == NULL && lookup_name == NULL)
4749 {
4750 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4751 {
4752 QUIT;
4753
4754 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4755 file_matcher, expansion_notify);
4756 }
4757 return;
4758 }
4759
4760 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4761
4762 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4763 symbol_matcher,
4764 kind, [&] (offset_type idx)
4765 {
4766 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4767 expansion_notify, kind);
4768 return true;
4769 });
4770 }
4771
4772 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4773 symtab. */
4774
4775 static struct compunit_symtab *
4776 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4777 CORE_ADDR pc)
4778 {
4779 int i;
4780
4781 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4782 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4783 return cust;
4784
4785 if (cust->includes == NULL)
4786 return NULL;
4787
4788 for (i = 0; cust->includes[i]; ++i)
4789 {
4790 struct compunit_symtab *s = cust->includes[i];
4791
4792 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4793 if (s != NULL)
4794 return s;
4795 }
4796
4797 return NULL;
4798 }
4799
4800 static struct compunit_symtab *
4801 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4802 struct bound_minimal_symbol msymbol,
4803 CORE_ADDR pc,
4804 struct obj_section *section,
4805 int warn_if_readin)
4806 {
4807 struct dwarf2_per_cu_data *data;
4808 struct compunit_symtab *result;
4809
4810 if (!objfile->partial_symtabs->psymtabs_addrmap)
4811 return NULL;
4812
4813 CORE_ADDR baseaddr = objfile->text_section_offset ();
4814 data = (struct dwarf2_per_cu_data *) addrmap_find
4815 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4816 if (!data)
4817 return NULL;
4818
4819 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4820 if (warn_if_readin && per_objfile->symtab_set_p (data))
4821 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4822 paddress (objfile->arch (), pc));
4823
4824 result = recursively_find_pc_sect_compunit_symtab
4825 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4826
4827 gdb_assert (result != NULL);
4828 return result;
4829 }
4830
4831 static void
4832 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4833 void *data, int need_fullname)
4834 {
4835 struct dwarf2_per_objfile *dwarf2_per_objfile
4836 = get_dwarf2_per_objfile (objfile);
4837
4838 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4839 {
4840 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4841
4842 htab_up visited (htab_create_alloc (10,
4843 htab_hash_pointer, htab_eq_pointer,
4844 NULL, xcalloc, xfree));
4845
4846 /* The rule is CUs specify all the files, including those used
4847 by any TU, so there's no need to scan TUs here. We can
4848 ignore file names coming from already-expanded CUs. */
4849
4850 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4851 {
4852 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4853 {
4854 void **slot = htab_find_slot (visited.get (),
4855 per_cu->v.quick->file_names,
4856 INSERT);
4857
4858 *slot = per_cu->v.quick->file_names;
4859 }
4860 }
4861
4862 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4863 {
4864 /* We only need to look at symtabs not already expanded. */
4865 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4866 continue;
4867
4868 quick_file_names *file_data
4869 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4870 if (file_data == NULL)
4871 continue;
4872
4873 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4874 if (*slot)
4875 {
4876 /* Already visited. */
4877 continue;
4878 }
4879 *slot = file_data;
4880
4881 for (int j = 0; j < file_data->num_file_names; ++j)
4882 {
4883 const char *filename = file_data->file_names[j];
4884 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4885 }
4886 }
4887 }
4888
4889 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4890 {
4891 gdb::unique_xmalloc_ptr<char> this_real_name;
4892
4893 if (need_fullname)
4894 this_real_name = gdb_realpath (filename);
4895 (*fun) (filename, this_real_name.get (), data);
4896 });
4897 }
4898
4899 static int
4900 dw2_has_symbols (struct objfile *objfile)
4901 {
4902 return 1;
4903 }
4904
4905 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4906 {
4907 dw2_has_symbols,
4908 dw2_find_last_source_symtab,
4909 dw2_forget_cached_source_info,
4910 dw2_map_symtabs_matching_filename,
4911 dw2_lookup_symbol,
4912 NULL,
4913 dw2_print_stats,
4914 dw2_dump,
4915 dw2_expand_symtabs_for_function,
4916 dw2_expand_all_symtabs,
4917 dw2_expand_symtabs_with_fullname,
4918 dw2_map_matching_symbols,
4919 dw2_expand_symtabs_matching,
4920 dw2_find_pc_sect_compunit_symtab,
4921 NULL,
4922 dw2_map_symbol_filenames
4923 };
4924
4925 /* DWARF-5 debug_names reader. */
4926
4927 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4928 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4929
4930 /* A helper function that reads the .debug_names section in SECTION
4931 and fills in MAP. FILENAME is the name of the file containing the
4932 section; it is used for error reporting.
4933
4934 Returns true if all went well, false otherwise. */
4935
4936 static bool
4937 read_debug_names_from_section (struct objfile *objfile,
4938 const char *filename,
4939 struct dwarf2_section_info *section,
4940 mapped_debug_names &map)
4941 {
4942 if (section->empty ())
4943 return false;
4944
4945 /* Older elfutils strip versions could keep the section in the main
4946 executable while splitting it for the separate debug info file. */
4947 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4948 return false;
4949
4950 section->read (objfile);
4951
4952 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4953
4954 const gdb_byte *addr = section->buffer;
4955
4956 bfd *const abfd = section->get_bfd_owner ();
4957
4958 unsigned int bytes_read;
4959 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4960 addr += bytes_read;
4961
4962 map.dwarf5_is_dwarf64 = bytes_read != 4;
4963 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4964 if (bytes_read + length != section->size)
4965 {
4966 /* There may be multiple per-CU indices. */
4967 warning (_("Section .debug_names in %s length %s does not match "
4968 "section length %s, ignoring .debug_names."),
4969 filename, plongest (bytes_read + length),
4970 pulongest (section->size));
4971 return false;
4972 }
4973
4974 /* The version number. */
4975 uint16_t version = read_2_bytes (abfd, addr);
4976 addr += 2;
4977 if (version != 5)
4978 {
4979 warning (_("Section .debug_names in %s has unsupported version %d, "
4980 "ignoring .debug_names."),
4981 filename, version);
4982 return false;
4983 }
4984
4985 /* Padding. */
4986 uint16_t padding = read_2_bytes (abfd, addr);
4987 addr += 2;
4988 if (padding != 0)
4989 {
4990 warning (_("Section .debug_names in %s has unsupported padding %d, "
4991 "ignoring .debug_names."),
4992 filename, padding);
4993 return false;
4994 }
4995
4996 /* comp_unit_count - The number of CUs in the CU list. */
4997 map.cu_count = read_4_bytes (abfd, addr);
4998 addr += 4;
4999
5000 /* local_type_unit_count - The number of TUs in the local TU
5001 list. */
5002 map.tu_count = read_4_bytes (abfd, addr);
5003 addr += 4;
5004
5005 /* foreign_type_unit_count - The number of TUs in the foreign TU
5006 list. */
5007 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5008 addr += 4;
5009 if (foreign_tu_count != 0)
5010 {
5011 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5012 "ignoring .debug_names."),
5013 filename, static_cast<unsigned long> (foreign_tu_count));
5014 return false;
5015 }
5016
5017 /* bucket_count - The number of hash buckets in the hash lookup
5018 table. */
5019 map.bucket_count = read_4_bytes (abfd, addr);
5020 addr += 4;
5021
5022 /* name_count - The number of unique names in the index. */
5023 map.name_count = read_4_bytes (abfd, addr);
5024 addr += 4;
5025
5026 /* abbrev_table_size - The size in bytes of the abbreviations
5027 table. */
5028 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5029 addr += 4;
5030
5031 /* augmentation_string_size - The size in bytes of the augmentation
5032 string. This value is rounded up to a multiple of 4. */
5033 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5034 addr += 4;
5035 map.augmentation_is_gdb = ((augmentation_string_size
5036 == sizeof (dwarf5_augmentation))
5037 && memcmp (addr, dwarf5_augmentation,
5038 sizeof (dwarf5_augmentation)) == 0);
5039 augmentation_string_size += (-augmentation_string_size) & 3;
5040 addr += augmentation_string_size;
5041
5042 /* List of CUs */
5043 map.cu_table_reordered = addr;
5044 addr += map.cu_count * map.offset_size;
5045
5046 /* List of Local TUs */
5047 map.tu_table_reordered = addr;
5048 addr += map.tu_count * map.offset_size;
5049
5050 /* Hash Lookup Table */
5051 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5052 addr += map.bucket_count * 4;
5053 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5054 addr += map.name_count * 4;
5055
5056 /* Name Table */
5057 map.name_table_string_offs_reordered = addr;
5058 addr += map.name_count * map.offset_size;
5059 map.name_table_entry_offs_reordered = addr;
5060 addr += map.name_count * map.offset_size;
5061
5062 const gdb_byte *abbrev_table_start = addr;
5063 for (;;)
5064 {
5065 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5066 addr += bytes_read;
5067 if (index_num == 0)
5068 break;
5069
5070 const auto insertpair
5071 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5072 if (!insertpair.second)
5073 {
5074 warning (_("Section .debug_names in %s has duplicate index %s, "
5075 "ignoring .debug_names."),
5076 filename, pulongest (index_num));
5077 return false;
5078 }
5079 mapped_debug_names::index_val &indexval = insertpair.first->second;
5080 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5081 addr += bytes_read;
5082
5083 for (;;)
5084 {
5085 mapped_debug_names::index_val::attr attr;
5086 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5087 addr += bytes_read;
5088 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5089 addr += bytes_read;
5090 if (attr.form == DW_FORM_implicit_const)
5091 {
5092 attr.implicit_const = read_signed_leb128 (abfd, addr,
5093 &bytes_read);
5094 addr += bytes_read;
5095 }
5096 if (attr.dw_idx == 0 && attr.form == 0)
5097 break;
5098 indexval.attr_vec.push_back (std::move (attr));
5099 }
5100 }
5101 if (addr != abbrev_table_start + abbrev_table_size)
5102 {
5103 warning (_("Section .debug_names in %s has abbreviation_table "
5104 "of size %s vs. written as %u, ignoring .debug_names."),
5105 filename, plongest (addr - abbrev_table_start),
5106 abbrev_table_size);
5107 return false;
5108 }
5109 map.entry_pool = addr;
5110
5111 return true;
5112 }
5113
5114 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5115 list. */
5116
5117 static void
5118 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5119 const mapped_debug_names &map,
5120 dwarf2_section_info &section,
5121 bool is_dwz)
5122 {
5123 if (!map.augmentation_is_gdb)
5124 {
5125 for (uint32_t i = 0; i < map.cu_count; ++i)
5126 {
5127 sect_offset sect_off
5128 = (sect_offset) (extract_unsigned_integer
5129 (map.cu_table_reordered + i * map.offset_size,
5130 map.offset_size,
5131 map.dwarf5_byte_order));
5132 /* We don't know the length of the CU, because the CU list in a
5133 .debug_names index can be incomplete, so we can't use the start of
5134 the next CU as end of this CU. We create the CUs here with length 0,
5135 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5136 dwarf2_per_cu_data *per_cu
5137 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5138 per_bfd->all_comp_units.push_back (per_cu);
5139 }
5140 }
5141
5142 sect_offset sect_off_prev;
5143 for (uint32_t i = 0; i <= map.cu_count; ++i)
5144 {
5145 sect_offset sect_off_next;
5146 if (i < map.cu_count)
5147 {
5148 sect_off_next
5149 = (sect_offset) (extract_unsigned_integer
5150 (map.cu_table_reordered + i * map.offset_size,
5151 map.offset_size,
5152 map.dwarf5_byte_order));
5153 }
5154 else
5155 sect_off_next = (sect_offset) section.size;
5156 if (i >= 1)
5157 {
5158 const ULONGEST length = sect_off_next - sect_off_prev;
5159 dwarf2_per_cu_data *per_cu
5160 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5161 sect_off_prev, length);
5162 per_bfd->all_comp_units.push_back (per_cu);
5163 }
5164 sect_off_prev = sect_off_next;
5165 }
5166 }
5167
5168 /* Read the CU list from the mapped index, and use it to create all
5169 the CU objects for this dwarf2_per_objfile. */
5170
5171 static void
5172 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5173 const mapped_debug_names &map,
5174 const mapped_debug_names &dwz_map)
5175 {
5176 gdb_assert (per_bfd->all_comp_units.empty ());
5177 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5178
5179 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5180 false /* is_dwz */);
5181
5182 if (dwz_map.cu_count == 0)
5183 return;
5184
5185 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5186 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5187 true /* is_dwz */);
5188 }
5189
5190 /* Read .debug_names. If everything went ok, initialize the "quick"
5191 elements of all the CUs and return true. Otherwise, return false. */
5192
5193 static bool
5194 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5195 {
5196 std::unique_ptr<mapped_debug_names> map
5197 (new mapped_debug_names (dwarf2_per_objfile));
5198 mapped_debug_names dwz_map (dwarf2_per_objfile);
5199 struct objfile *objfile = dwarf2_per_objfile->objfile;
5200
5201 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5202 &dwarf2_per_objfile->per_bfd->debug_names,
5203 *map))
5204 return false;
5205
5206 /* Don't use the index if it's empty. */
5207 if (map->name_count == 0)
5208 return false;
5209
5210 /* If there is a .dwz file, read it so we can get its CU list as
5211 well. */
5212 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5213 if (dwz != NULL)
5214 {
5215 if (!read_debug_names_from_section (objfile,
5216 bfd_get_filename (dwz->dwz_bfd.get ()),
5217 &dwz->debug_names, dwz_map))
5218 {
5219 warning (_("could not read '.debug_names' section from %s; skipping"),
5220 bfd_get_filename (dwz->dwz_bfd.get ()));
5221 return false;
5222 }
5223 }
5224
5225 create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
5226
5227 if (map->tu_count != 0)
5228 {
5229 /* We can only handle a single .debug_types when we have an
5230 index. */
5231 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5232 return false;
5233
5234 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5235
5236 create_signatured_type_table_from_debug_names
5237 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5238 }
5239
5240 create_addrmap_from_aranges (dwarf2_per_objfile,
5241 &dwarf2_per_objfile->per_bfd->debug_aranges);
5242
5243 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5244 dwarf2_per_objfile->per_bfd->using_index = 1;
5245 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5246 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5247
5248 return true;
5249 }
5250
5251 /* Type used to manage iterating over all CUs looking for a symbol for
5252 .debug_names. */
5253
5254 class dw2_debug_names_iterator
5255 {
5256 public:
5257 dw2_debug_names_iterator (const mapped_debug_names &map,
5258 gdb::optional<block_enum> block_index,
5259 domain_enum domain,
5260 const char *name)
5261 : m_map (map), m_block_index (block_index), m_domain (domain),
5262 m_addr (find_vec_in_debug_names (map, name))
5263 {}
5264
5265 dw2_debug_names_iterator (const mapped_debug_names &map,
5266 search_domain search, uint32_t namei)
5267 : m_map (map),
5268 m_search (search),
5269 m_addr (find_vec_in_debug_names (map, namei))
5270 {}
5271
5272 dw2_debug_names_iterator (const mapped_debug_names &map,
5273 block_enum block_index, domain_enum domain,
5274 uint32_t namei)
5275 : m_map (map), m_block_index (block_index), m_domain (domain),
5276 m_addr (find_vec_in_debug_names (map, namei))
5277 {}
5278
5279 /* Return the next matching CU or NULL if there are no more. */
5280 dwarf2_per_cu_data *next ();
5281
5282 private:
5283 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5284 const char *name);
5285 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5286 uint32_t namei);
5287
5288 /* The internalized form of .debug_names. */
5289 const mapped_debug_names &m_map;
5290
5291 /* If set, only look for symbols that match that block. Valid values are
5292 GLOBAL_BLOCK and STATIC_BLOCK. */
5293 const gdb::optional<block_enum> m_block_index;
5294
5295 /* The kind of symbol we're looking for. */
5296 const domain_enum m_domain = UNDEF_DOMAIN;
5297 const search_domain m_search = ALL_DOMAIN;
5298
5299 /* The list of CUs from the index entry of the symbol, or NULL if
5300 not found. */
5301 const gdb_byte *m_addr;
5302 };
5303
5304 const char *
5305 mapped_debug_names::namei_to_name (uint32_t namei) const
5306 {
5307 const ULONGEST namei_string_offs
5308 = extract_unsigned_integer ((name_table_string_offs_reordered
5309 + namei * offset_size),
5310 offset_size,
5311 dwarf5_byte_order);
5312 return read_indirect_string_at_offset (dwarf2_per_objfile,
5313 namei_string_offs);
5314 }
5315
5316 /* Find a slot in .debug_names for the object named NAME. If NAME is
5317 found, return pointer to its pool data. If NAME cannot be found,
5318 return NULL. */
5319
5320 const gdb_byte *
5321 dw2_debug_names_iterator::find_vec_in_debug_names
5322 (const mapped_debug_names &map, const char *name)
5323 {
5324 int (*cmp) (const char *, const char *);
5325
5326 gdb::unique_xmalloc_ptr<char> without_params;
5327 if (current_language->la_language == language_cplus
5328 || current_language->la_language == language_fortran
5329 || current_language->la_language == language_d)
5330 {
5331 /* NAME is already canonical. Drop any qualifiers as
5332 .debug_names does not contain any. */
5333
5334 if (strchr (name, '(') != NULL)
5335 {
5336 without_params = cp_remove_params (name);
5337 if (without_params != NULL)
5338 name = without_params.get ();
5339 }
5340 }
5341
5342 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5343
5344 const uint32_t full_hash = dwarf5_djb_hash (name);
5345 uint32_t namei
5346 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5347 (map.bucket_table_reordered
5348 + (full_hash % map.bucket_count)), 4,
5349 map.dwarf5_byte_order);
5350 if (namei == 0)
5351 return NULL;
5352 --namei;
5353 if (namei >= map.name_count)
5354 {
5355 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5356 "[in module %s]"),
5357 namei, map.name_count,
5358 objfile_name (map.dwarf2_per_objfile->objfile));
5359 return NULL;
5360 }
5361
5362 for (;;)
5363 {
5364 const uint32_t namei_full_hash
5365 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5366 (map.hash_table_reordered + namei), 4,
5367 map.dwarf5_byte_order);
5368 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5369 return NULL;
5370
5371 if (full_hash == namei_full_hash)
5372 {
5373 const char *const namei_string = map.namei_to_name (namei);
5374
5375 #if 0 /* An expensive sanity check. */
5376 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5377 {
5378 complaint (_("Wrong .debug_names hash for string at index %u "
5379 "[in module %s]"),
5380 namei, objfile_name (dwarf2_per_objfile->objfile));
5381 return NULL;
5382 }
5383 #endif
5384
5385 if (cmp (namei_string, name) == 0)
5386 {
5387 const ULONGEST namei_entry_offs
5388 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5389 + namei * map.offset_size),
5390 map.offset_size, map.dwarf5_byte_order);
5391 return map.entry_pool + namei_entry_offs;
5392 }
5393 }
5394
5395 ++namei;
5396 if (namei >= map.name_count)
5397 return NULL;
5398 }
5399 }
5400
5401 const gdb_byte *
5402 dw2_debug_names_iterator::find_vec_in_debug_names
5403 (const mapped_debug_names &map, uint32_t namei)
5404 {
5405 if (namei >= map.name_count)
5406 {
5407 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5408 "[in module %s]"),
5409 namei, map.name_count,
5410 objfile_name (map.dwarf2_per_objfile->objfile));
5411 return NULL;
5412 }
5413
5414 const ULONGEST namei_entry_offs
5415 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5416 + namei * map.offset_size),
5417 map.offset_size, map.dwarf5_byte_order);
5418 return map.entry_pool + namei_entry_offs;
5419 }
5420
5421 /* See dw2_debug_names_iterator. */
5422
5423 dwarf2_per_cu_data *
5424 dw2_debug_names_iterator::next ()
5425 {
5426 if (m_addr == NULL)
5427 return NULL;
5428
5429 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5430 struct objfile *objfile = dwarf2_per_objfile->objfile;
5431 bfd *const abfd = objfile->obfd;
5432
5433 again:
5434
5435 unsigned int bytes_read;
5436 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5437 m_addr += bytes_read;
5438 if (abbrev == 0)
5439 return NULL;
5440
5441 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5442 if (indexval_it == m_map.abbrev_map.cend ())
5443 {
5444 complaint (_("Wrong .debug_names undefined abbrev code %s "
5445 "[in module %s]"),
5446 pulongest (abbrev), objfile_name (objfile));
5447 return NULL;
5448 }
5449 const mapped_debug_names::index_val &indexval = indexval_it->second;
5450 enum class symbol_linkage {
5451 unknown,
5452 static_,
5453 extern_,
5454 } symbol_linkage_ = symbol_linkage::unknown;
5455 dwarf2_per_cu_data *per_cu = NULL;
5456 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5457 {
5458 ULONGEST ull;
5459 switch (attr.form)
5460 {
5461 case DW_FORM_implicit_const:
5462 ull = attr.implicit_const;
5463 break;
5464 case DW_FORM_flag_present:
5465 ull = 1;
5466 break;
5467 case DW_FORM_udata:
5468 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5469 m_addr += bytes_read;
5470 break;
5471 case DW_FORM_ref4:
5472 ull = read_4_bytes (abfd, m_addr);
5473 m_addr += 4;
5474 break;
5475 case DW_FORM_ref8:
5476 ull = read_8_bytes (abfd, m_addr);
5477 m_addr += 8;
5478 break;
5479 case DW_FORM_ref_sig8:
5480 ull = read_8_bytes (abfd, m_addr);
5481 m_addr += 8;
5482 break;
5483 default:
5484 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5485 dwarf_form_name (attr.form),
5486 objfile_name (objfile));
5487 return NULL;
5488 }
5489 switch (attr.dw_idx)
5490 {
5491 case DW_IDX_compile_unit:
5492 /* Don't crash on bad data. */
5493 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5494 {
5495 complaint (_(".debug_names entry has bad CU index %s"
5496 " [in module %s]"),
5497 pulongest (ull),
5498 objfile_name (dwarf2_per_objfile->objfile));
5499 continue;
5500 }
5501 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5502 break;
5503 case DW_IDX_type_unit:
5504 /* Don't crash on bad data. */
5505 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5506 {
5507 complaint (_(".debug_names entry has bad TU index %s"
5508 " [in module %s]"),
5509 pulongest (ull),
5510 objfile_name (dwarf2_per_objfile->objfile));
5511 continue;
5512 }
5513 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5514 break;
5515 case DW_IDX_die_offset:
5516 /* In a per-CU index (as opposed to a per-module index), index
5517 entries without CU attribute implicitly refer to the single CU. */
5518 if (per_cu == NULL)
5519 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5520 break;
5521 case DW_IDX_GNU_internal:
5522 if (!m_map.augmentation_is_gdb)
5523 break;
5524 symbol_linkage_ = symbol_linkage::static_;
5525 break;
5526 case DW_IDX_GNU_external:
5527 if (!m_map.augmentation_is_gdb)
5528 break;
5529 symbol_linkage_ = symbol_linkage::extern_;
5530 break;
5531 }
5532 }
5533
5534 /* Skip if already read in. */
5535 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5536 goto again;
5537
5538 /* Check static vs global. */
5539 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5540 {
5541 const bool want_static = *m_block_index == STATIC_BLOCK;
5542 const bool symbol_is_static =
5543 symbol_linkage_ == symbol_linkage::static_;
5544 if (want_static != symbol_is_static)
5545 goto again;
5546 }
5547
5548 /* Match dw2_symtab_iter_next, symbol_kind
5549 and debug_names::psymbol_tag. */
5550 switch (m_domain)
5551 {
5552 case VAR_DOMAIN:
5553 switch (indexval.dwarf_tag)
5554 {
5555 case DW_TAG_variable:
5556 case DW_TAG_subprogram:
5557 /* Some types are also in VAR_DOMAIN. */
5558 case DW_TAG_typedef:
5559 case DW_TAG_structure_type:
5560 break;
5561 default:
5562 goto again;
5563 }
5564 break;
5565 case STRUCT_DOMAIN:
5566 switch (indexval.dwarf_tag)
5567 {
5568 case DW_TAG_typedef:
5569 case DW_TAG_structure_type:
5570 break;
5571 default:
5572 goto again;
5573 }
5574 break;
5575 case LABEL_DOMAIN:
5576 switch (indexval.dwarf_tag)
5577 {
5578 case 0:
5579 case DW_TAG_variable:
5580 break;
5581 default:
5582 goto again;
5583 }
5584 break;
5585 case MODULE_DOMAIN:
5586 switch (indexval.dwarf_tag)
5587 {
5588 case DW_TAG_module:
5589 break;
5590 default:
5591 goto again;
5592 }
5593 break;
5594 default:
5595 break;
5596 }
5597
5598 /* Match dw2_expand_symtabs_matching, symbol_kind and
5599 debug_names::psymbol_tag. */
5600 switch (m_search)
5601 {
5602 case VARIABLES_DOMAIN:
5603 switch (indexval.dwarf_tag)
5604 {
5605 case DW_TAG_variable:
5606 break;
5607 default:
5608 goto again;
5609 }
5610 break;
5611 case FUNCTIONS_DOMAIN:
5612 switch (indexval.dwarf_tag)
5613 {
5614 case DW_TAG_subprogram:
5615 break;
5616 default:
5617 goto again;
5618 }
5619 break;
5620 case TYPES_DOMAIN:
5621 switch (indexval.dwarf_tag)
5622 {
5623 case DW_TAG_typedef:
5624 case DW_TAG_structure_type:
5625 break;
5626 default:
5627 goto again;
5628 }
5629 break;
5630 case MODULES_DOMAIN:
5631 switch (indexval.dwarf_tag)
5632 {
5633 case DW_TAG_module:
5634 break;
5635 default:
5636 goto again;
5637 }
5638 default:
5639 break;
5640 }
5641
5642 return per_cu;
5643 }
5644
5645 static struct compunit_symtab *
5646 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5647 const char *name, domain_enum domain)
5648 {
5649 struct dwarf2_per_objfile *dwarf2_per_objfile
5650 = get_dwarf2_per_objfile (objfile);
5651
5652 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5653 if (!mapp)
5654 {
5655 /* index is NULL if OBJF_READNOW. */
5656 return NULL;
5657 }
5658 const auto &map = *mapp;
5659
5660 dw2_debug_names_iterator iter (map, block_index, domain, name);
5661
5662 struct compunit_symtab *stab_best = NULL;
5663 struct dwarf2_per_cu_data *per_cu;
5664 while ((per_cu = iter.next ()) != NULL)
5665 {
5666 struct symbol *sym, *with_opaque = NULL;
5667 compunit_symtab *stab
5668 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5669 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5670 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5671
5672 sym = block_find_symbol (block, name, domain,
5673 block_find_non_opaque_type_preferred,
5674 &with_opaque);
5675
5676 /* Some caution must be observed with overloaded functions and
5677 methods, since the index will not contain any overload
5678 information (but NAME might contain it). */
5679
5680 if (sym != NULL
5681 && strcmp_iw (sym->search_name (), name) == 0)
5682 return stab;
5683 if (with_opaque != NULL
5684 && strcmp_iw (with_opaque->search_name (), name) == 0)
5685 stab_best = stab;
5686
5687 /* Keep looking through other CUs. */
5688 }
5689
5690 return stab_best;
5691 }
5692
5693 /* This dumps minimal information about .debug_names. It is called
5694 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5695 uses this to verify that .debug_names has been loaded. */
5696
5697 static void
5698 dw2_debug_names_dump (struct objfile *objfile)
5699 {
5700 struct dwarf2_per_objfile *dwarf2_per_objfile
5701 = get_dwarf2_per_objfile (objfile);
5702
5703 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5704 printf_filtered (".debug_names:");
5705 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5706 printf_filtered (" exists\n");
5707 else
5708 printf_filtered (" faked for \"readnow\"\n");
5709 printf_filtered ("\n");
5710 }
5711
5712 static void
5713 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5714 const char *func_name)
5715 {
5716 struct dwarf2_per_objfile *dwarf2_per_objfile
5717 = get_dwarf2_per_objfile (objfile);
5718
5719 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5720 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5721 {
5722 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5723
5724 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5725
5726 struct dwarf2_per_cu_data *per_cu;
5727 while ((per_cu = iter.next ()) != NULL)
5728 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5729 }
5730 }
5731
5732 static void
5733 dw2_debug_names_map_matching_symbols
5734 (struct objfile *objfile,
5735 const lookup_name_info &name, domain_enum domain,
5736 int global,
5737 gdb::function_view<symbol_found_callback_ftype> callback,
5738 symbol_compare_ftype *ordered_compare)
5739 {
5740 struct dwarf2_per_objfile *dwarf2_per_objfile
5741 = get_dwarf2_per_objfile (objfile);
5742
5743 /* debug_names_table is NULL if OBJF_READNOW. */
5744 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5745 return;
5746
5747 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5748 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5749
5750 const char *match_name = name.ada ().lookup_name ().c_str ();
5751 auto matcher = [&] (const char *symname)
5752 {
5753 if (ordered_compare == nullptr)
5754 return true;
5755 return ordered_compare (symname, match_name) == 0;
5756 };
5757
5758 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5759 [&] (offset_type namei)
5760 {
5761 /* The name was matched, now expand corresponding CUs that were
5762 marked. */
5763 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5764
5765 struct dwarf2_per_cu_data *per_cu;
5766 while ((per_cu = iter.next ()) != NULL)
5767 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5768 nullptr);
5769 return true;
5770 });
5771
5772 /* It's a shame we couldn't do this inside the
5773 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5774 that have already been expanded. Instead, this loop matches what
5775 the psymtab code does. */
5776 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5777 {
5778 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5779 if (symtab != nullptr)
5780 {
5781 const struct block *block
5782 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5783 if (!iterate_over_symbols_terminated (block, name,
5784 domain, callback))
5785 break;
5786 }
5787 }
5788 }
5789
5790 static void
5791 dw2_debug_names_expand_symtabs_matching
5792 (struct objfile *objfile,
5793 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5794 const lookup_name_info *lookup_name,
5795 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5796 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5797 enum search_domain kind)
5798 {
5799 struct dwarf2_per_objfile *dwarf2_per_objfile
5800 = get_dwarf2_per_objfile (objfile);
5801
5802 /* debug_names_table is NULL if OBJF_READNOW. */
5803 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5804 return;
5805
5806 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5807
5808 if (symbol_matcher == NULL && lookup_name == NULL)
5809 {
5810 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5811 {
5812 QUIT;
5813
5814 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5815 file_matcher, expansion_notify);
5816 }
5817 return;
5818 }
5819
5820 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5821
5822 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5823 symbol_matcher,
5824 kind, [&] (offset_type namei)
5825 {
5826 /* The name was matched, now expand corresponding CUs that were
5827 marked. */
5828 dw2_debug_names_iterator iter (map, kind, namei);
5829
5830 struct dwarf2_per_cu_data *per_cu;
5831 while ((per_cu = iter.next ()) != NULL)
5832 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5833 file_matcher, expansion_notify);
5834 return true;
5835 });
5836 }
5837
5838 const struct quick_symbol_functions dwarf2_debug_names_functions =
5839 {
5840 dw2_has_symbols,
5841 dw2_find_last_source_symtab,
5842 dw2_forget_cached_source_info,
5843 dw2_map_symtabs_matching_filename,
5844 dw2_debug_names_lookup_symbol,
5845 NULL,
5846 dw2_print_stats,
5847 dw2_debug_names_dump,
5848 dw2_debug_names_expand_symtabs_for_function,
5849 dw2_expand_all_symtabs,
5850 dw2_expand_symtabs_with_fullname,
5851 dw2_debug_names_map_matching_symbols,
5852 dw2_debug_names_expand_symtabs_matching,
5853 dw2_find_pc_sect_compunit_symtab,
5854 NULL,
5855 dw2_map_symbol_filenames
5856 };
5857
5858 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5859 to either a dwarf2_per_bfd or dwz_file object. */
5860
5861 template <typename T>
5862 static gdb::array_view<const gdb_byte>
5863 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5864 {
5865 dwarf2_section_info *section = &section_owner->gdb_index;
5866
5867 if (section->empty ())
5868 return {};
5869
5870 /* Older elfutils strip versions could keep the section in the main
5871 executable while splitting it for the separate debug info file. */
5872 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5873 return {};
5874
5875 section->read (obj);
5876
5877 /* dwarf2_section_info::size is a bfd_size_type, while
5878 gdb::array_view works with size_t. On 32-bit hosts, with
5879 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5880 is 32-bit. So we need an explicit narrowing conversion here.
5881 This is fine, because it's impossible to allocate or mmap an
5882 array/buffer larger than what size_t can represent. */
5883 return gdb::make_array_view (section->buffer, section->size);
5884 }
5885
5886 /* Lookup the index cache for the contents of the index associated to
5887 DWARF2_OBJ. */
5888
5889 static gdb::array_view<const gdb_byte>
5890 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5891 {
5892 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5893 if (build_id == nullptr)
5894 return {};
5895
5896 return global_index_cache.lookup_gdb_index (build_id,
5897 &dwarf2_per_bfd->index_cache_res);
5898 }
5899
5900 /* Same as the above, but for DWZ. */
5901
5902 static gdb::array_view<const gdb_byte>
5903 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5904 {
5905 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5906 if (build_id == nullptr)
5907 return {};
5908
5909 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5910 }
5911
5912 /* See symfile.h. */
5913
5914 bool
5915 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5916 {
5917 struct dwarf2_per_objfile *dwarf2_per_objfile
5918 = get_dwarf2_per_objfile (objfile);
5919
5920 /* If we're about to read full symbols, don't bother with the
5921 indices. In this case we also don't care if some other debug
5922 format is making psymtabs, because they are all about to be
5923 expanded anyway. */
5924 if ((objfile->flags & OBJF_READNOW))
5925 {
5926 dwarf2_per_objfile->per_bfd->using_index = 1;
5927 create_all_comp_units (dwarf2_per_objfile);
5928 create_all_type_units (dwarf2_per_objfile);
5929 dwarf2_per_objfile->per_bfd->quick_file_names_table
5930 = create_quick_file_names_table
5931 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5932 dwarf2_per_objfile->resize_symtabs ();
5933
5934 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5935 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5936 {
5937 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5938
5939 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5940 struct dwarf2_per_cu_quick_data);
5941 }
5942
5943 /* Return 1 so that gdb sees the "quick" functions. However,
5944 these functions will be no-ops because we will have expanded
5945 all symtabs. */
5946 *index_kind = dw_index_kind::GDB_INDEX;
5947 return true;
5948 }
5949
5950 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5951 {
5952 *index_kind = dw_index_kind::DEBUG_NAMES;
5953 dwarf2_per_objfile->resize_symtabs ();
5954 return true;
5955 }
5956
5957 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5958 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5959 get_gdb_index_contents_from_section<dwz_file>))
5960 {
5961 *index_kind = dw_index_kind::GDB_INDEX;
5962 dwarf2_per_objfile->resize_symtabs ();
5963 return true;
5964 }
5965
5966 /* ... otherwise, try to find the index in the index cache. */
5967 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5968 get_gdb_index_contents_from_cache,
5969 get_gdb_index_contents_from_cache_dwz))
5970 {
5971 global_index_cache.hit ();
5972 *index_kind = dw_index_kind::GDB_INDEX;
5973 dwarf2_per_objfile->resize_symtabs ();
5974 return true;
5975 }
5976
5977 global_index_cache.miss ();
5978 return false;
5979 }
5980
5981 \f
5982
5983 /* Build a partial symbol table. */
5984
5985 void
5986 dwarf2_build_psymtabs (struct objfile *objfile)
5987 {
5988 struct dwarf2_per_objfile *dwarf2_per_objfile
5989 = get_dwarf2_per_objfile (objfile);
5990
5991 init_psymbol_list (objfile, 1024);
5992
5993 try
5994 {
5995 /* This isn't really ideal: all the data we allocate on the
5996 objfile's obstack is still uselessly kept around. However,
5997 freeing it seems unsafe. */
5998 psymtab_discarder psymtabs (objfile);
5999 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6000 psymtabs.keep ();
6001
6002 dwarf2_per_objfile->resize_symtabs ();
6003
6004 /* (maybe) store an index in the cache. */
6005 global_index_cache.store (dwarf2_per_objfile);
6006 }
6007 catch (const gdb_exception_error &except)
6008 {
6009 exception_print (gdb_stderr, except);
6010 }
6011 }
6012
6013 /* Find the base address of the compilation unit for range lists and
6014 location lists. It will normally be specified by DW_AT_low_pc.
6015 In DWARF-3 draft 4, the base address could be overridden by
6016 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6017 compilation units with discontinuous ranges. */
6018
6019 static void
6020 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6021 {
6022 struct attribute *attr;
6023
6024 cu->base_address.reset ();
6025
6026 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6027 if (attr != nullptr)
6028 cu->base_address = attr->value_as_address ();
6029 else
6030 {
6031 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6032 if (attr != nullptr)
6033 cu->base_address = attr->value_as_address ();
6034 }
6035 }
6036
6037 /* Helper function that returns the proper abbrev section for
6038 THIS_CU. */
6039
6040 static struct dwarf2_section_info *
6041 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6042 {
6043 struct dwarf2_section_info *abbrev;
6044 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6045
6046 if (this_cu->is_dwz)
6047 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6048 else
6049 abbrev = &per_bfd->abbrev;
6050
6051 return abbrev;
6052 }
6053
6054 /* Fetch the abbreviation table offset from a comp or type unit header. */
6055
6056 static sect_offset
6057 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6058 struct dwarf2_section_info *section,
6059 sect_offset sect_off)
6060 {
6061 bfd *abfd = section->get_bfd_owner ();
6062 const gdb_byte *info_ptr;
6063 unsigned int initial_length_size, offset_size;
6064 uint16_t version;
6065
6066 section->read (dwarf2_per_objfile->objfile);
6067 info_ptr = section->buffer + to_underlying (sect_off);
6068 read_initial_length (abfd, info_ptr, &initial_length_size);
6069 offset_size = initial_length_size == 4 ? 4 : 8;
6070 info_ptr += initial_length_size;
6071
6072 version = read_2_bytes (abfd, info_ptr);
6073 info_ptr += 2;
6074 if (version >= 5)
6075 {
6076 /* Skip unit type and address size. */
6077 info_ptr += 2;
6078 }
6079
6080 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6081 }
6082
6083 /* A partial symtab that is used only for include files. */
6084 struct dwarf2_include_psymtab : public partial_symtab
6085 {
6086 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6087 : partial_symtab (filename, objfile)
6088 {
6089 }
6090
6091 void read_symtab (struct objfile *objfile) override
6092 {
6093 /* It's an include file, no symbols to read for it.
6094 Everything is in the includer symtab. */
6095
6096 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6097 expansion of the includer psymtab. We use the dependencies[0] field to
6098 model the includer. But if we go the regular route of calling
6099 expand_psymtab here, and having expand_psymtab call expand_dependencies
6100 to expand the includer, we'll only use expand_psymtab on the includer
6101 (making it a non-toplevel psymtab), while if we expand the includer via
6102 another path, we'll use read_symtab (making it a toplevel psymtab).
6103 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6104 psymtab, and trigger read_symtab on the includer here directly. */
6105 includer ()->read_symtab (objfile);
6106 }
6107
6108 void expand_psymtab (struct objfile *objfile) override
6109 {
6110 /* This is not called by read_symtab, and should not be called by any
6111 expand_dependencies. */
6112 gdb_assert (false);
6113 }
6114
6115 bool readin_p (struct objfile *objfile) const override
6116 {
6117 return includer ()->readin_p (objfile);
6118 }
6119
6120 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6121 {
6122 return nullptr;
6123 }
6124
6125 private:
6126 partial_symtab *includer () const
6127 {
6128 /* An include psymtab has exactly one dependency: the psymtab that
6129 includes it. */
6130 gdb_assert (this->number_of_dependencies == 1);
6131 return this->dependencies[0];
6132 }
6133 };
6134
6135 /* Allocate a new partial symtab for file named NAME and mark this new
6136 partial symtab as being an include of PST. */
6137
6138 static void
6139 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6140 struct objfile *objfile)
6141 {
6142 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6143
6144 if (!IS_ABSOLUTE_PATH (subpst->filename))
6145 subpst->dirname = pst->dirname;
6146
6147 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6148 subpst->dependencies[0] = pst;
6149 subpst->number_of_dependencies = 1;
6150 }
6151
6152 /* Read the Line Number Program data and extract the list of files
6153 included by the source file represented by PST. Build an include
6154 partial symtab for each of these included files. */
6155
6156 static void
6157 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6158 struct die_info *die,
6159 dwarf2_psymtab *pst)
6160 {
6161 line_header_up lh;
6162 struct attribute *attr;
6163
6164 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6165 if (attr != nullptr)
6166 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6167 if (lh == NULL)
6168 return; /* No linetable, so no includes. */
6169
6170 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6171 that we pass in the raw text_low here; that is ok because we're
6172 only decoding the line table to make include partial symtabs, and
6173 so the addresses aren't really used. */
6174 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6175 pst->raw_text_low (), 1);
6176 }
6177
6178 static hashval_t
6179 hash_signatured_type (const void *item)
6180 {
6181 const struct signatured_type *sig_type
6182 = (const struct signatured_type *) item;
6183
6184 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6185 return sig_type->signature;
6186 }
6187
6188 static int
6189 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6190 {
6191 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6192 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6193
6194 return lhs->signature == rhs->signature;
6195 }
6196
6197 /* Allocate a hash table for signatured types. */
6198
6199 static htab_up
6200 allocate_signatured_type_table ()
6201 {
6202 return htab_up (htab_create_alloc (41,
6203 hash_signatured_type,
6204 eq_signatured_type,
6205 NULL, xcalloc, xfree));
6206 }
6207
6208 /* A helper function to add a signatured type CU to a table. */
6209
6210 static int
6211 add_signatured_type_cu_to_table (void **slot, void *datum)
6212 {
6213 struct signatured_type *sigt = (struct signatured_type *) *slot;
6214 std::vector<signatured_type *> *all_type_units
6215 = (std::vector<signatured_type *> *) datum;
6216
6217 all_type_units->push_back (sigt);
6218
6219 return 1;
6220 }
6221
6222 /* A helper for create_debug_types_hash_table. Read types from SECTION
6223 and fill them into TYPES_HTAB. It will process only type units,
6224 therefore DW_UT_type. */
6225
6226 static void
6227 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6228 struct dwo_file *dwo_file,
6229 dwarf2_section_info *section, htab_up &types_htab,
6230 rcuh_kind section_kind)
6231 {
6232 struct objfile *objfile = dwarf2_per_objfile->objfile;
6233 struct dwarf2_section_info *abbrev_section;
6234 bfd *abfd;
6235 const gdb_byte *info_ptr, *end_ptr;
6236
6237 abbrev_section = (dwo_file != NULL
6238 ? &dwo_file->sections.abbrev
6239 : &dwarf2_per_objfile->per_bfd->abbrev);
6240
6241 if (dwarf_read_debug)
6242 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6243 section->get_name (),
6244 abbrev_section->get_file_name ());
6245
6246 section->read (objfile);
6247 info_ptr = section->buffer;
6248
6249 if (info_ptr == NULL)
6250 return;
6251
6252 /* We can't set abfd until now because the section may be empty or
6253 not present, in which case the bfd is unknown. */
6254 abfd = section->get_bfd_owner ();
6255
6256 /* We don't use cutu_reader here because we don't need to read
6257 any dies: the signature is in the header. */
6258
6259 end_ptr = info_ptr + section->size;
6260 while (info_ptr < end_ptr)
6261 {
6262 struct signatured_type *sig_type;
6263 struct dwo_unit *dwo_tu;
6264 void **slot;
6265 const gdb_byte *ptr = info_ptr;
6266 struct comp_unit_head header;
6267 unsigned int length;
6268
6269 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6270
6271 /* Initialize it due to a false compiler warning. */
6272 header.signature = -1;
6273 header.type_cu_offset_in_tu = (cu_offset) -1;
6274
6275 /* We need to read the type's signature in order to build the hash
6276 table, but we don't need anything else just yet. */
6277
6278 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6279 abbrev_section, ptr, section_kind);
6280
6281 length = header.get_length ();
6282
6283 /* Skip dummy type units. */
6284 if (ptr >= info_ptr + length
6285 || peek_abbrev_code (abfd, ptr) == 0
6286 || header.unit_type != DW_UT_type)
6287 {
6288 info_ptr += length;
6289 continue;
6290 }
6291
6292 if (types_htab == NULL)
6293 {
6294 if (dwo_file)
6295 types_htab = allocate_dwo_unit_table ();
6296 else
6297 types_htab = allocate_signatured_type_table ();
6298 }
6299
6300 if (dwo_file)
6301 {
6302 sig_type = NULL;
6303 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6304 struct dwo_unit);
6305 dwo_tu->dwo_file = dwo_file;
6306 dwo_tu->signature = header.signature;
6307 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6308 dwo_tu->section = section;
6309 dwo_tu->sect_off = sect_off;
6310 dwo_tu->length = length;
6311 }
6312 else
6313 {
6314 /* N.B.: type_offset is not usable if this type uses a DWO file.
6315 The real type_offset is in the DWO file. */
6316 dwo_tu = NULL;
6317 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6318 sig_type->signature = header.signature;
6319 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6320 sig_type->per_cu.is_debug_types = 1;
6321 sig_type->per_cu.section = section;
6322 sig_type->per_cu.sect_off = sect_off;
6323 sig_type->per_cu.length = length;
6324 }
6325
6326 slot = htab_find_slot (types_htab.get (),
6327 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6328 INSERT);
6329 gdb_assert (slot != NULL);
6330 if (*slot != NULL)
6331 {
6332 sect_offset dup_sect_off;
6333
6334 if (dwo_file)
6335 {
6336 const struct dwo_unit *dup_tu
6337 = (const struct dwo_unit *) *slot;
6338
6339 dup_sect_off = dup_tu->sect_off;
6340 }
6341 else
6342 {
6343 const struct signatured_type *dup_tu
6344 = (const struct signatured_type *) *slot;
6345
6346 dup_sect_off = dup_tu->per_cu.sect_off;
6347 }
6348
6349 complaint (_("debug type entry at offset %s is duplicate to"
6350 " the entry at offset %s, signature %s"),
6351 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6352 hex_string (header.signature));
6353 }
6354 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6355
6356 if (dwarf_read_debug > 1)
6357 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6358 sect_offset_str (sect_off),
6359 hex_string (header.signature));
6360
6361 info_ptr += length;
6362 }
6363 }
6364
6365 /* Create the hash table of all entries in the .debug_types
6366 (or .debug_types.dwo) section(s).
6367 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6368 otherwise it is NULL.
6369
6370 The result is a pointer to the hash table or NULL if there are no types.
6371
6372 Note: This function processes DWO files only, not DWP files. */
6373
6374 static void
6375 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6376 struct dwo_file *dwo_file,
6377 gdb::array_view<dwarf2_section_info> type_sections,
6378 htab_up &types_htab)
6379 {
6380 for (dwarf2_section_info &section : type_sections)
6381 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6382 types_htab, rcuh_kind::TYPE);
6383 }
6384
6385 /* Create the hash table of all entries in the .debug_types section,
6386 and initialize all_type_units.
6387 The result is zero if there is an error (e.g. missing .debug_types section),
6388 otherwise non-zero. */
6389
6390 static int
6391 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6392 {
6393 htab_up types_htab;
6394
6395 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6396 &dwarf2_per_objfile->per_bfd->info, types_htab,
6397 rcuh_kind::COMPILE);
6398 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6399 dwarf2_per_objfile->per_bfd->types, types_htab);
6400 if (types_htab == NULL)
6401 {
6402 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6403 return 0;
6404 }
6405
6406 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6407
6408 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6409 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6410 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6411
6412 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6413 add_signatured_type_cu_to_table,
6414 &dwarf2_per_objfile->per_bfd->all_type_units);
6415
6416 return 1;
6417 }
6418
6419 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6420 If SLOT is non-NULL, it is the entry to use in the hash table.
6421 Otherwise we find one. */
6422
6423 static struct signatured_type *
6424 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6425 void **slot)
6426 {
6427 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6428 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6429 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6430
6431 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6432
6433 dwarf2_per_objfile->resize_symtabs ();
6434
6435 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6436 sig_type->signature = sig;
6437 sig_type->per_cu.is_debug_types = 1;
6438 if (dwarf2_per_objfile->per_bfd->using_index)
6439 {
6440 sig_type->per_cu.v.quick =
6441 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6442 struct dwarf2_per_cu_quick_data);
6443 }
6444
6445 if (slot == NULL)
6446 {
6447 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6448 sig_type, INSERT);
6449 }
6450 gdb_assert (*slot == NULL);
6451 *slot = sig_type;
6452 /* The rest of sig_type must be filled in by the caller. */
6453 return sig_type;
6454 }
6455
6456 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6457 Fill in SIG_ENTRY with DWO_ENTRY. */
6458
6459 static void
6460 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6461 struct signatured_type *sig_entry,
6462 struct dwo_unit *dwo_entry)
6463 {
6464 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6465
6466 /* Make sure we're not clobbering something we don't expect to. */
6467 gdb_assert (! sig_entry->per_cu.queued);
6468 gdb_assert (sig_entry->per_cu.cu == NULL);
6469 if (per_bfd->using_index)
6470 {
6471 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6472 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6473 }
6474 else
6475 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6476 gdb_assert (sig_entry->signature == dwo_entry->signature);
6477 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6478 gdb_assert (sig_entry->type_unit_group == NULL);
6479 gdb_assert (sig_entry->dwo_unit == NULL);
6480
6481 sig_entry->per_cu.section = dwo_entry->section;
6482 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6483 sig_entry->per_cu.length = dwo_entry->length;
6484 sig_entry->per_cu.reading_dwo_directly = 1;
6485 sig_entry->per_cu.per_bfd = per_bfd;
6486 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6487 sig_entry->dwo_unit = dwo_entry;
6488 }
6489
6490 /* Subroutine of lookup_signatured_type.
6491 If we haven't read the TU yet, create the signatured_type data structure
6492 for a TU to be read in directly from a DWO file, bypassing the stub.
6493 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6494 using .gdb_index, then when reading a CU we want to stay in the DWO file
6495 containing that CU. Otherwise we could end up reading several other DWO
6496 files (due to comdat folding) to process the transitive closure of all the
6497 mentioned TUs, and that can be slow. The current DWO file will have every
6498 type signature that it needs.
6499 We only do this for .gdb_index because in the psymtab case we already have
6500 to read all the DWOs to build the type unit groups. */
6501
6502 static struct signatured_type *
6503 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6504 {
6505 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6506 struct dwo_file *dwo_file;
6507 struct dwo_unit find_dwo_entry, *dwo_entry;
6508 struct signatured_type find_sig_entry, *sig_entry;
6509 void **slot;
6510
6511 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6512
6513 /* If TU skeletons have been removed then we may not have read in any
6514 TUs yet. */
6515 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6516 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6517
6518 /* We only ever need to read in one copy of a signatured type.
6519 Use the global signatured_types array to do our own comdat-folding
6520 of types. If this is the first time we're reading this TU, and
6521 the TU has an entry in .gdb_index, replace the recorded data from
6522 .gdb_index with this TU. */
6523
6524 find_sig_entry.signature = sig;
6525 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6526 &find_sig_entry, INSERT);
6527 sig_entry = (struct signatured_type *) *slot;
6528
6529 /* We can get here with the TU already read, *or* in the process of being
6530 read. Don't reassign the global entry to point to this DWO if that's
6531 the case. Also note that if the TU is already being read, it may not
6532 have come from a DWO, the program may be a mix of Fission-compiled
6533 code and non-Fission-compiled code. */
6534
6535 /* Have we already tried to read this TU?
6536 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6537 needn't exist in the global table yet). */
6538 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6539 return sig_entry;
6540
6541 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6542 dwo_unit of the TU itself. */
6543 dwo_file = cu->dwo_unit->dwo_file;
6544
6545 /* Ok, this is the first time we're reading this TU. */
6546 if (dwo_file->tus == NULL)
6547 return NULL;
6548 find_dwo_entry.signature = sig;
6549 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6550 &find_dwo_entry);
6551 if (dwo_entry == NULL)
6552 return NULL;
6553
6554 /* If the global table doesn't have an entry for this TU, add one. */
6555 if (sig_entry == NULL)
6556 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6557
6558 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6559 sig_entry->per_cu.tu_read = 1;
6560 return sig_entry;
6561 }
6562
6563 /* Subroutine of lookup_signatured_type.
6564 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6565 then try the DWP file. If the TU stub (skeleton) has been removed then
6566 it won't be in .gdb_index. */
6567
6568 static struct signatured_type *
6569 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6570 {
6571 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6572 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6573 struct dwo_unit *dwo_entry;
6574 struct signatured_type find_sig_entry, *sig_entry;
6575 void **slot;
6576
6577 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6578 gdb_assert (dwp_file != NULL);
6579
6580 /* If TU skeletons have been removed then we may not have read in any
6581 TUs yet. */
6582 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6583 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6584
6585 find_sig_entry.signature = sig;
6586 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6587 &find_sig_entry, INSERT);
6588 sig_entry = (struct signatured_type *) *slot;
6589
6590 /* Have we already tried to read this TU?
6591 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6592 needn't exist in the global table yet). */
6593 if (sig_entry != NULL)
6594 return sig_entry;
6595
6596 if (dwp_file->tus == NULL)
6597 return NULL;
6598 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6599 sig, 1 /* is_debug_types */);
6600 if (dwo_entry == NULL)
6601 return NULL;
6602
6603 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6604 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6605
6606 return sig_entry;
6607 }
6608
6609 /* Lookup a signature based type for DW_FORM_ref_sig8.
6610 Returns NULL if signature SIG is not present in the table.
6611 It is up to the caller to complain about this. */
6612
6613 static struct signatured_type *
6614 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6615 {
6616 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6617
6618 if (cu->dwo_unit
6619 && dwarf2_per_objfile->per_bfd->using_index)
6620 {
6621 /* We're in a DWO/DWP file, and we're using .gdb_index.
6622 These cases require special processing. */
6623 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6624 return lookup_dwo_signatured_type (cu, sig);
6625 else
6626 return lookup_dwp_signatured_type (cu, sig);
6627 }
6628 else
6629 {
6630 struct signatured_type find_entry, *entry;
6631
6632 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6633 return NULL;
6634 find_entry.signature = sig;
6635 entry = ((struct signatured_type *)
6636 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6637 &find_entry));
6638 return entry;
6639 }
6640 }
6641
6642 /* Low level DIE reading support. */
6643
6644 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6645
6646 static void
6647 init_cu_die_reader (struct die_reader_specs *reader,
6648 struct dwarf2_cu *cu,
6649 struct dwarf2_section_info *section,
6650 struct dwo_file *dwo_file,
6651 struct abbrev_table *abbrev_table)
6652 {
6653 gdb_assert (section->readin && section->buffer != NULL);
6654 reader->abfd = section->get_bfd_owner ();
6655 reader->cu = cu;
6656 reader->dwo_file = dwo_file;
6657 reader->die_section = section;
6658 reader->buffer = section->buffer;
6659 reader->buffer_end = section->buffer + section->size;
6660 reader->abbrev_table = abbrev_table;
6661 }
6662
6663 /* Subroutine of cutu_reader to simplify it.
6664 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6665 There's just a lot of work to do, and cutu_reader is big enough
6666 already.
6667
6668 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6669 from it to the DIE in the DWO. If NULL we are skipping the stub.
6670 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6671 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6672 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6673 STUB_COMP_DIR may be non-NULL.
6674 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6675 are filled in with the info of the DIE from the DWO file.
6676 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6677 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6678 kept around for at least as long as *RESULT_READER.
6679
6680 The result is non-zero if a valid (non-dummy) DIE was found. */
6681
6682 static int
6683 read_cutu_die_from_dwo (dwarf2_cu *cu,
6684 struct dwo_unit *dwo_unit,
6685 struct die_info *stub_comp_unit_die,
6686 const char *stub_comp_dir,
6687 struct die_reader_specs *result_reader,
6688 const gdb_byte **result_info_ptr,
6689 struct die_info **result_comp_unit_die,
6690 abbrev_table_up *result_dwo_abbrev_table)
6691 {
6692 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6693 dwarf2_per_cu_data *per_cu = cu->per_cu;
6694 struct objfile *objfile = dwarf2_per_objfile->objfile;
6695 bfd *abfd;
6696 const gdb_byte *begin_info_ptr, *info_ptr;
6697 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6698 int i,num_extra_attrs;
6699 struct dwarf2_section_info *dwo_abbrev_section;
6700 struct die_info *comp_unit_die;
6701
6702 /* At most one of these may be provided. */
6703 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6704
6705 /* These attributes aren't processed until later:
6706 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6707 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6708 referenced later. However, these attributes are found in the stub
6709 which we won't have later. In order to not impose this complication
6710 on the rest of the code, we read them here and copy them to the
6711 DWO CU/TU die. */
6712
6713 stmt_list = NULL;
6714 low_pc = NULL;
6715 high_pc = NULL;
6716 ranges = NULL;
6717 comp_dir = NULL;
6718
6719 if (stub_comp_unit_die != NULL)
6720 {
6721 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6722 DWO file. */
6723 if (!per_cu->is_debug_types)
6724 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6725 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6726 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6727 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6728 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6729
6730 cu->addr_base = stub_comp_unit_die->addr_base ();
6731
6732 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6733 here (if needed). We need the value before we can process
6734 DW_AT_ranges. */
6735 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6736 }
6737 else if (stub_comp_dir != NULL)
6738 {
6739 /* Reconstruct the comp_dir attribute to simplify the code below. */
6740 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6741 comp_dir->name = DW_AT_comp_dir;
6742 comp_dir->form = DW_FORM_string;
6743 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6744 DW_STRING (comp_dir) = stub_comp_dir;
6745 }
6746
6747 /* Set up for reading the DWO CU/TU. */
6748 cu->dwo_unit = dwo_unit;
6749 dwarf2_section_info *section = dwo_unit->section;
6750 section->read (objfile);
6751 abfd = section->get_bfd_owner ();
6752 begin_info_ptr = info_ptr = (section->buffer
6753 + to_underlying (dwo_unit->sect_off));
6754 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6755
6756 if (per_cu->is_debug_types)
6757 {
6758 signatured_type *sig_type = (struct signatured_type *) per_cu;
6759
6760 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6761 &cu->header, section,
6762 dwo_abbrev_section,
6763 info_ptr, rcuh_kind::TYPE);
6764 /* This is not an assert because it can be caused by bad debug info. */
6765 if (sig_type->signature != cu->header.signature)
6766 {
6767 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6768 " TU at offset %s [in module %s]"),
6769 hex_string (sig_type->signature),
6770 hex_string (cu->header.signature),
6771 sect_offset_str (dwo_unit->sect_off),
6772 bfd_get_filename (abfd));
6773 }
6774 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6775 /* For DWOs coming from DWP files, we don't know the CU length
6776 nor the type's offset in the TU until now. */
6777 dwo_unit->length = cu->header.get_length ();
6778 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6779
6780 /* Establish the type offset that can be used to lookup the type.
6781 For DWO files, we don't know it until now. */
6782 sig_type->type_offset_in_section
6783 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6784 }
6785 else
6786 {
6787 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6788 &cu->header, section,
6789 dwo_abbrev_section,
6790 info_ptr, rcuh_kind::COMPILE);
6791 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6792 /* For DWOs coming from DWP files, we don't know the CU length
6793 until now. */
6794 dwo_unit->length = cu->header.get_length ();
6795 }
6796
6797 *result_dwo_abbrev_table
6798 = abbrev_table::read (objfile, dwo_abbrev_section,
6799 cu->header.abbrev_sect_off);
6800 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6801 result_dwo_abbrev_table->get ());
6802
6803 /* Read in the die, but leave space to copy over the attributes
6804 from the stub. This has the benefit of simplifying the rest of
6805 the code - all the work to maintain the illusion of a single
6806 DW_TAG_{compile,type}_unit DIE is done here. */
6807 num_extra_attrs = ((stmt_list != NULL)
6808 + (low_pc != NULL)
6809 + (high_pc != NULL)
6810 + (ranges != NULL)
6811 + (comp_dir != NULL));
6812 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6813 num_extra_attrs);
6814
6815 /* Copy over the attributes from the stub to the DIE we just read in. */
6816 comp_unit_die = *result_comp_unit_die;
6817 i = comp_unit_die->num_attrs;
6818 if (stmt_list != NULL)
6819 comp_unit_die->attrs[i++] = *stmt_list;
6820 if (low_pc != NULL)
6821 comp_unit_die->attrs[i++] = *low_pc;
6822 if (high_pc != NULL)
6823 comp_unit_die->attrs[i++] = *high_pc;
6824 if (ranges != NULL)
6825 comp_unit_die->attrs[i++] = *ranges;
6826 if (comp_dir != NULL)
6827 comp_unit_die->attrs[i++] = *comp_dir;
6828 comp_unit_die->num_attrs += num_extra_attrs;
6829
6830 if (dwarf_die_debug)
6831 {
6832 fprintf_unfiltered (gdb_stdlog,
6833 "Read die from %s@0x%x of %s:\n",
6834 section->get_name (),
6835 (unsigned) (begin_info_ptr - section->buffer),
6836 bfd_get_filename (abfd));
6837 dump_die (comp_unit_die, dwarf_die_debug);
6838 }
6839
6840 /* Skip dummy compilation units. */
6841 if (info_ptr >= begin_info_ptr + dwo_unit->length
6842 || peek_abbrev_code (abfd, info_ptr) == 0)
6843 return 0;
6844
6845 *result_info_ptr = info_ptr;
6846 return 1;
6847 }
6848
6849 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6850 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6851 signature is part of the header. */
6852 static gdb::optional<ULONGEST>
6853 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6854 {
6855 if (cu->header.version >= 5)
6856 return cu->header.signature;
6857 struct attribute *attr;
6858 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6859 if (attr == nullptr)
6860 return gdb::optional<ULONGEST> ();
6861 return DW_UNSND (attr);
6862 }
6863
6864 /* Subroutine of cutu_reader to simplify it.
6865 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6866 Returns NULL if the specified DWO unit cannot be found. */
6867
6868 static struct dwo_unit *
6869 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6870 {
6871 dwarf2_per_cu_data *per_cu = cu->per_cu;
6872 struct dwo_unit *dwo_unit;
6873 const char *comp_dir;
6874
6875 gdb_assert (cu != NULL);
6876
6877 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6878 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6879 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6880
6881 if (per_cu->is_debug_types)
6882 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6883 else
6884 {
6885 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6886
6887 if (!signature.has_value ())
6888 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6889 " [in module %s]"),
6890 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6891
6892 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6893 }
6894
6895 return dwo_unit;
6896 }
6897
6898 /* Subroutine of cutu_reader to simplify it.
6899 See it for a description of the parameters.
6900 Read a TU directly from a DWO file, bypassing the stub. */
6901
6902 void
6903 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6904 dwarf2_per_objfile *per_objfile,
6905 dwarf2_cu *existing_cu)
6906 {
6907 struct signatured_type *sig_type;
6908
6909 /* Verify we can do the following downcast, and that we have the
6910 data we need. */
6911 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6912 sig_type = (struct signatured_type *) this_cu;
6913 gdb_assert (sig_type->dwo_unit != NULL);
6914
6915 dwarf2_cu *cu;
6916
6917 if (existing_cu != nullptr)
6918 {
6919 cu = existing_cu;
6920 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6921 /* There's no need to do the rereading_dwo_cu handling that
6922 cutu_reader does since we don't read the stub. */
6923 }
6924 else
6925 {
6926 /* If an existing_cu is provided, this_cu->cu must be NULL. */
6927 gdb_assert (this_cu->cu == NULL);
6928 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6929 cu = m_new_cu.get ();
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 (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 EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6956 allocated. */
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 dwarf2_cu *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 const gdb_byte *begin_info_ptr;
6970 struct signatured_type *sig_type = NULL;
6971 struct dwarf2_section_info *abbrev_section;
6972 /* Non-zero if CU currently points to a DWO file and we need to
6973 reread it. When this happens we need to reread the skeleton die
6974 before we can reread the DWO file (this only applies to CUs, not TUs). */
6975 int rereading_dwo_cu = 0;
6976
6977 if (dwarf_die_debug)
6978 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6979 this_cu->is_debug_types ? "type" : "comp",
6980 sect_offset_str (this_cu->sect_off));
6981
6982 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6983 file (instead of going through the stub), short-circuit all of this. */
6984 if (this_cu->reading_dwo_directly)
6985 {
6986 /* Narrow down the scope of possibilities to have to understand. */
6987 gdb_assert (this_cu->is_debug_types);
6988 gdb_assert (abbrev_table == NULL);
6989 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, existing_cu);
6990 return;
6991 }
6992
6993 /* This is cheap if the section is already read in. */
6994 section->read (objfile);
6995
6996 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6997
6998 abbrev_section = get_abbrev_section_for_cu (this_cu);
6999
7000 dwarf2_cu *cu;
7001
7002 if (existing_cu != nullptr)
7003 {
7004 cu = existing_cu;
7005 /* If this CU is from a DWO file we need to start over, we need to
7006 refetch the attributes from the skeleton CU.
7007 This could be optimized by retrieving those attributes from when we
7008 were here the first time: the previous comp_unit_die was stored in
7009 comp_unit_obstack. But there's no data yet that we need this
7010 optimization. */
7011 if (cu->dwo_unit != NULL)
7012 rereading_dwo_cu = 1;
7013 }
7014 else
7015 {
7016 /* If an existing_cu is provided, this_cu->cu must be NULL. */
7017 gdb_assert (this_cu->cu == NULL);
7018 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7019 cu = m_new_cu.get ();
7020 }
7021
7022 /* Get the header. */
7023 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7024 {
7025 /* We already have the header, there's no need to read it in again. */
7026 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7027 }
7028 else
7029 {
7030 if (this_cu->is_debug_types)
7031 {
7032 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7033 &cu->header, section,
7034 abbrev_section, info_ptr,
7035 rcuh_kind::TYPE);
7036
7037 /* Since per_cu is the first member of struct signatured_type,
7038 we can go from a pointer to one to a pointer to the other. */
7039 sig_type = (struct signatured_type *) this_cu;
7040 gdb_assert (sig_type->signature == cu->header.signature);
7041 gdb_assert (sig_type->type_offset_in_tu
7042 == cu->header.type_cu_offset_in_tu);
7043 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7044
7045 /* LENGTH has not been set yet for type units if we're
7046 using .gdb_index. */
7047 this_cu->length = cu->header.get_length ();
7048
7049 /* Establish the type offset that can be used to lookup the type. */
7050 sig_type->type_offset_in_section =
7051 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7052
7053 this_cu->dwarf_version = cu->header.version;
7054 }
7055 else
7056 {
7057 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7058 &cu->header, section,
7059 abbrev_section,
7060 info_ptr,
7061 rcuh_kind::COMPILE);
7062
7063 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7064 if (this_cu->length == 0)
7065 this_cu->length = cu->header.get_length ();
7066 else
7067 gdb_assert (this_cu->length == cu->header.get_length ());
7068 this_cu->dwarf_version = cu->header.version;
7069 }
7070 }
7071
7072 /* Skip dummy compilation units. */
7073 if (info_ptr >= begin_info_ptr + this_cu->length
7074 || peek_abbrev_code (abfd, info_ptr) == 0)
7075 {
7076 dummy_p = true;
7077 return;
7078 }
7079
7080 /* If we don't have them yet, read the abbrevs for this compilation unit.
7081 And if we need to read them now, make sure they're freed when we're
7082 done. */
7083 if (abbrev_table != NULL)
7084 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7085 else
7086 {
7087 m_abbrev_table_holder
7088 = abbrev_table::read (objfile, abbrev_section,
7089 cu->header.abbrev_sect_off);
7090 abbrev_table = m_abbrev_table_holder.get ();
7091 }
7092
7093 /* Read the top level CU/TU die. */
7094 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7095 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7096
7097 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7098 {
7099 dummy_p = true;
7100 return;
7101 }
7102
7103 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7104 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7105 table from the DWO file and pass the ownership over to us. It will be
7106 referenced from READER, so we must make sure to free it after we're done
7107 with READER.
7108
7109 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7110 DWO CU, that this test will fail (the attribute will not be present). */
7111 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7112 if (dwo_name != nullptr)
7113 {
7114 struct dwo_unit *dwo_unit;
7115 struct die_info *dwo_comp_unit_die;
7116
7117 if (comp_unit_die->has_children)
7118 {
7119 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7120 " has children (offset %s) [in module %s]"),
7121 sect_offset_str (this_cu->sect_off),
7122 bfd_get_filename (abfd));
7123 }
7124 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7125 if (dwo_unit != NULL)
7126 {
7127 if (read_cutu_die_from_dwo (cu, dwo_unit,
7128 comp_unit_die, NULL,
7129 this, &info_ptr,
7130 &dwo_comp_unit_die,
7131 &m_dwo_abbrev_table) == 0)
7132 {
7133 /* Dummy die. */
7134 dummy_p = true;
7135 return;
7136 }
7137 comp_unit_die = dwo_comp_unit_die;
7138 }
7139 else
7140 {
7141 /* Yikes, we couldn't find the rest of the DIE, we only have
7142 the stub. A complaint has already been logged. There's
7143 not much more we can do except pass on the stub DIE to
7144 die_reader_func. We don't want to throw an error on bad
7145 debug info. */
7146 }
7147 }
7148 }
7149
7150 void
7151 cutu_reader::keep ()
7152 {
7153 /* Done, clean up. */
7154 gdb_assert (!dummy_p);
7155 if (m_new_cu != NULL)
7156 {
7157 /* We know that m_this_cu->cu is set, since we are in the process of
7158 parsing the CU. */
7159 gdb_assert (m_this_cu->cu != nullptr);
7160 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7161
7162 /* Link this CU into read_in_chain. */
7163 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7164 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7165 /* The chain owns it now. */
7166 m_new_cu.release ();
7167 }
7168 }
7169
7170 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7171 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7172 assumed to have already done the lookup to find the DWO file).
7173
7174 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7175 THIS_CU->is_debug_types, but nothing else.
7176
7177 We fill in THIS_CU->length.
7178
7179 THIS_CU->cu is always freed when done.
7180 This is done in order to not leave THIS_CU->cu in a state where we have
7181 to care whether it refers to the "main" CU or the DWO CU.
7182
7183 When parent_cu is passed, it is used to provide a default value for
7184 str_offsets_base and addr_base from the parent. */
7185
7186 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7187 dwarf2_per_objfile *dwarf2_per_objfile,
7188 struct dwarf2_cu *parent_cu,
7189 struct dwo_file *dwo_file)
7190 : die_reader_specs {},
7191 m_this_cu (this_cu)
7192 {
7193 struct objfile *objfile = dwarf2_per_objfile->objfile;
7194 struct dwarf2_section_info *section = this_cu->section;
7195 bfd *abfd = section->get_bfd_owner ();
7196 struct dwarf2_section_info *abbrev_section;
7197 const gdb_byte *begin_info_ptr, *info_ptr;
7198
7199 if (dwarf_die_debug)
7200 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7201 this_cu->is_debug_types ? "type" : "comp",
7202 sect_offset_str (this_cu->sect_off));
7203
7204 gdb_assert (this_cu->cu == NULL);
7205
7206 abbrev_section = (dwo_file != NULL
7207 ? &dwo_file->sections.abbrev
7208 : get_abbrev_section_for_cu (this_cu));
7209
7210 /* This is cheap if the section is already read in. */
7211 section->read (objfile);
7212
7213 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7214
7215 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7216 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7217 &m_new_cu->header, section,
7218 abbrev_section, info_ptr,
7219 (this_cu->is_debug_types
7220 ? rcuh_kind::TYPE
7221 : rcuh_kind::COMPILE));
7222
7223 if (parent_cu != nullptr)
7224 {
7225 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7226 m_new_cu->addr_base = parent_cu->addr_base;
7227 }
7228 this_cu->length = m_new_cu->header.get_length ();
7229
7230 /* Skip dummy compilation units. */
7231 if (info_ptr >= begin_info_ptr + this_cu->length
7232 || peek_abbrev_code (abfd, info_ptr) == 0)
7233 {
7234 dummy_p = true;
7235 return;
7236 }
7237
7238 m_abbrev_table_holder
7239 = abbrev_table::read (objfile, abbrev_section,
7240 m_new_cu->header.abbrev_sect_off);
7241
7242 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7243 m_abbrev_table_holder.get ());
7244 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7245 }
7246
7247 \f
7248 /* Type Unit Groups.
7249
7250 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7251 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7252 so that all types coming from the same compilation (.o file) are grouped
7253 together. A future step could be to put the types in the same symtab as
7254 the CU the types ultimately came from. */
7255
7256 static hashval_t
7257 hash_type_unit_group (const void *item)
7258 {
7259 const struct type_unit_group *tu_group
7260 = (const struct type_unit_group *) item;
7261
7262 return hash_stmt_list_entry (&tu_group->hash);
7263 }
7264
7265 static int
7266 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7267 {
7268 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7269 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7270
7271 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7272 }
7273
7274 /* Allocate a hash table for type unit groups. */
7275
7276 static htab_up
7277 allocate_type_unit_groups_table ()
7278 {
7279 return htab_up (htab_create_alloc (3,
7280 hash_type_unit_group,
7281 eq_type_unit_group,
7282 NULL, xcalloc, xfree));
7283 }
7284
7285 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7286 partial symtabs. We combine several TUs per psymtab to not let the size
7287 of any one psymtab grow too big. */
7288 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7289 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7290
7291 /* Helper routine for get_type_unit_group.
7292 Create the type_unit_group object used to hold one or more TUs. */
7293
7294 static struct type_unit_group *
7295 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7296 {
7297 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7298 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7299 struct dwarf2_per_cu_data *per_cu;
7300 struct type_unit_group *tu_group;
7301
7302 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7303 struct type_unit_group);
7304 per_cu = &tu_group->per_cu;
7305 per_cu->per_bfd = per_bfd;
7306
7307 if (per_bfd->using_index)
7308 {
7309 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7310 struct dwarf2_per_cu_quick_data);
7311 }
7312 else
7313 {
7314 unsigned int line_offset = to_underlying (line_offset_struct);
7315 dwarf2_psymtab *pst;
7316 std::string name;
7317
7318 /* Give the symtab a useful name for debug purposes. */
7319 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7320 name = string_printf ("<type_units_%d>",
7321 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7322 else
7323 name = string_printf ("<type_units_at_0x%x>", line_offset);
7324
7325 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7326 pst->anonymous = true;
7327 }
7328
7329 tu_group->hash.dwo_unit = cu->dwo_unit;
7330 tu_group->hash.line_sect_off = line_offset_struct;
7331
7332 return tu_group;
7333 }
7334
7335 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7336 STMT_LIST is a DW_AT_stmt_list attribute. */
7337
7338 static struct type_unit_group *
7339 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7340 {
7341 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7342 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7343 struct type_unit_group *tu_group;
7344 void **slot;
7345 unsigned int line_offset;
7346 struct type_unit_group type_unit_group_for_lookup;
7347
7348 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7349 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7350
7351 /* Do we need to create a new group, or can we use an existing one? */
7352
7353 if (stmt_list)
7354 {
7355 line_offset = DW_UNSND (stmt_list);
7356 ++tu_stats->nr_symtab_sharers;
7357 }
7358 else
7359 {
7360 /* Ugh, no stmt_list. Rare, but we have to handle it.
7361 We can do various things here like create one group per TU or
7362 spread them over multiple groups to split up the expansion work.
7363 To avoid worst case scenarios (too many groups or too large groups)
7364 we, umm, group them in bunches. */
7365 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7366 | (tu_stats->nr_stmt_less_type_units
7367 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7368 ++tu_stats->nr_stmt_less_type_units;
7369 }
7370
7371 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7372 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7373 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7374 &type_unit_group_for_lookup, INSERT);
7375 if (*slot != NULL)
7376 {
7377 tu_group = (struct type_unit_group *) *slot;
7378 gdb_assert (tu_group != NULL);
7379 }
7380 else
7381 {
7382 sect_offset line_offset_struct = (sect_offset) line_offset;
7383 tu_group = create_type_unit_group (cu, line_offset_struct);
7384 *slot = tu_group;
7385 ++tu_stats->nr_symtabs;
7386 }
7387
7388 return tu_group;
7389 }
7390 \f
7391 /* Partial symbol tables. */
7392
7393 /* Create a psymtab named NAME and assign it to PER_CU.
7394
7395 The caller must fill in the following details:
7396 dirname, textlow, texthigh. */
7397
7398 static dwarf2_psymtab *
7399 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7400 dwarf2_per_objfile *per_objfile,
7401 const char *name)
7402 {
7403 struct objfile *objfile = per_objfile->objfile;
7404 dwarf2_psymtab *pst;
7405
7406 pst = new dwarf2_psymtab (name, objfile, per_cu);
7407
7408 pst->psymtabs_addrmap_supported = true;
7409
7410 /* This is the glue that links PST into GDB's symbol API. */
7411 per_cu->v.psymtab = pst;
7412
7413 return pst;
7414 }
7415
7416 /* DIE reader function for process_psymtab_comp_unit. */
7417
7418 static void
7419 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7420 const gdb_byte *info_ptr,
7421 struct die_info *comp_unit_die,
7422 enum language pretend_language)
7423 {
7424 struct dwarf2_cu *cu = reader->cu;
7425 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7426 struct objfile *objfile = per_objfile->objfile;
7427 struct gdbarch *gdbarch = objfile->arch ();
7428 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7429 CORE_ADDR baseaddr;
7430 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7431 dwarf2_psymtab *pst;
7432 enum pc_bounds_kind cu_bounds_kind;
7433 const char *filename;
7434
7435 gdb_assert (! per_cu->is_debug_types);
7436
7437 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7438
7439 /* Allocate a new partial symbol table structure. */
7440 gdb::unique_xmalloc_ptr<char> debug_filename;
7441 static const char artificial[] = "<artificial>";
7442 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7443 if (filename == NULL)
7444 filename = "";
7445 else if (strcmp (filename, artificial) == 0)
7446 {
7447 debug_filename.reset (concat (artificial, "@",
7448 sect_offset_str (per_cu->sect_off),
7449 (char *) NULL));
7450 filename = debug_filename.get ();
7451 }
7452
7453 pst = create_partial_symtab (per_cu, per_objfile, filename);
7454
7455 /* This must be done before calling dwarf2_build_include_psymtabs. */
7456 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7457
7458 baseaddr = objfile->text_section_offset ();
7459
7460 dwarf2_find_base_address (comp_unit_die, cu);
7461
7462 /* Possibly set the default values of LOWPC and HIGHPC from
7463 `DW_AT_ranges'. */
7464 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7465 &best_highpc, cu, pst);
7466 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7467 {
7468 CORE_ADDR low
7469 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7470 - baseaddr);
7471 CORE_ADDR high
7472 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7473 - baseaddr - 1);
7474 /* Store the contiguous range if it is not empty; it can be
7475 empty for CUs with no code. */
7476 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7477 low, high, pst);
7478 }
7479
7480 /* Check if comp unit has_children.
7481 If so, read the rest of the partial symbols from this comp unit.
7482 If not, there's no more debug_info for this comp unit. */
7483 if (comp_unit_die->has_children)
7484 {
7485 struct partial_die_info *first_die;
7486 CORE_ADDR lowpc, highpc;
7487
7488 lowpc = ((CORE_ADDR) -1);
7489 highpc = ((CORE_ADDR) 0);
7490
7491 first_die = load_partial_dies (reader, info_ptr, 1);
7492
7493 scan_partial_symbols (first_die, &lowpc, &highpc,
7494 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7495
7496 /* If we didn't find a lowpc, set it to highpc to avoid
7497 complaints from `maint check'. */
7498 if (lowpc == ((CORE_ADDR) -1))
7499 lowpc = highpc;
7500
7501 /* If the compilation unit didn't have an explicit address range,
7502 then use the information extracted from its child dies. */
7503 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7504 {
7505 best_lowpc = lowpc;
7506 best_highpc = highpc;
7507 }
7508 }
7509 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7510 best_lowpc + baseaddr)
7511 - baseaddr);
7512 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7513 best_highpc + baseaddr)
7514 - baseaddr);
7515
7516 end_psymtab_common (objfile, pst);
7517
7518 if (!cu->per_cu->imported_symtabs_empty ())
7519 {
7520 int i;
7521 int len = cu->per_cu->imported_symtabs_size ();
7522
7523 /* Fill in 'dependencies' here; we fill in 'users' in a
7524 post-pass. */
7525 pst->number_of_dependencies = len;
7526 pst->dependencies
7527 = objfile->partial_symtabs->allocate_dependencies (len);
7528 for (i = 0; i < len; ++i)
7529 {
7530 pst->dependencies[i]
7531 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7532 }
7533
7534 cu->per_cu->imported_symtabs_free ();
7535 }
7536
7537 /* Get the list of files included in the current compilation unit,
7538 and build a psymtab for each of them. */
7539 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7540
7541 if (dwarf_read_debug)
7542 fprintf_unfiltered (gdb_stdlog,
7543 "Psymtab for %s unit @%s: %s - %s"
7544 ", %d global, %d static syms\n",
7545 per_cu->is_debug_types ? "type" : "comp",
7546 sect_offset_str (per_cu->sect_off),
7547 paddress (gdbarch, pst->text_low (objfile)),
7548 paddress (gdbarch, pst->text_high (objfile)),
7549 pst->n_global_syms, pst->n_static_syms);
7550 }
7551
7552 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7553 Process compilation unit THIS_CU for a psymtab. */
7554
7555 static void
7556 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7557 dwarf2_per_objfile *per_objfile,
7558 bool want_partial_unit,
7559 enum language pretend_language)
7560 {
7561 /* If this compilation unit was already read in, free the
7562 cached copy in order to read it in again. This is
7563 necessary because we skipped some symbols when we first
7564 read in the compilation unit (see load_partial_dies).
7565 This problem could be avoided, but the benefit is unclear. */
7566 if (this_cu->cu != NULL)
7567 free_one_cached_comp_unit (this_cu, per_objfile);
7568
7569 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7570
7571 switch (reader.comp_unit_die->tag)
7572 {
7573 case DW_TAG_compile_unit:
7574 this_cu->unit_type = DW_UT_compile;
7575 break;
7576 case DW_TAG_partial_unit:
7577 this_cu->unit_type = DW_UT_partial;
7578 break;
7579 default:
7580 abort ();
7581 }
7582
7583 if (reader.dummy_p)
7584 {
7585 /* Nothing. */
7586 }
7587 else if (this_cu->is_debug_types)
7588 build_type_psymtabs_reader (&reader, reader.info_ptr,
7589 reader.comp_unit_die);
7590 else if (want_partial_unit
7591 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7592 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7593 reader.comp_unit_die,
7594 pretend_language);
7595
7596 this_cu->lang = this_cu->cu->language;
7597
7598 /* Age out any secondary CUs. */
7599 age_cached_comp_units (per_objfile);
7600 }
7601
7602 /* Reader function for build_type_psymtabs. */
7603
7604 static void
7605 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7606 const gdb_byte *info_ptr,
7607 struct die_info *type_unit_die)
7608 {
7609 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7610 struct objfile *objfile = dwarf2_per_objfile->objfile;
7611 struct dwarf2_cu *cu = reader->cu;
7612 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7613 struct signatured_type *sig_type;
7614 struct type_unit_group *tu_group;
7615 struct attribute *attr;
7616 struct partial_die_info *first_die;
7617 CORE_ADDR lowpc, highpc;
7618 dwarf2_psymtab *pst;
7619
7620 gdb_assert (per_cu->is_debug_types);
7621 sig_type = (struct signatured_type *) per_cu;
7622
7623 if (! type_unit_die->has_children)
7624 return;
7625
7626 attr = type_unit_die->attr (DW_AT_stmt_list);
7627 tu_group = get_type_unit_group (cu, attr);
7628
7629 if (tu_group->tus == nullptr)
7630 tu_group->tus = new std::vector<signatured_type *>;
7631 tu_group->tus->push_back (sig_type);
7632
7633 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7634 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7635 pst->anonymous = true;
7636
7637 first_die = load_partial_dies (reader, info_ptr, 1);
7638
7639 lowpc = (CORE_ADDR) -1;
7640 highpc = (CORE_ADDR) 0;
7641 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7642
7643 end_psymtab_common (objfile, pst);
7644 }
7645
7646 /* Struct used to sort TUs by their abbreviation table offset. */
7647
7648 struct tu_abbrev_offset
7649 {
7650 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7651 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7652 {}
7653
7654 signatured_type *sig_type;
7655 sect_offset abbrev_offset;
7656 };
7657
7658 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7659
7660 static bool
7661 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7662 const struct tu_abbrev_offset &b)
7663 {
7664 return a.abbrev_offset < b.abbrev_offset;
7665 }
7666
7667 /* Efficiently read all the type units.
7668 This does the bulk of the work for build_type_psymtabs.
7669
7670 The efficiency is because we sort TUs by the abbrev table they use and
7671 only read each abbrev table once. In one program there are 200K TUs
7672 sharing 8K abbrev tables.
7673
7674 The main purpose of this function is to support building the
7675 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7676 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7677 can collapse the search space by grouping them by stmt_list.
7678 The savings can be significant, in the same program from above the 200K TUs
7679 share 8K stmt_list tables.
7680
7681 FUNC is expected to call get_type_unit_group, which will create the
7682 struct type_unit_group if necessary and add it to
7683 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7684
7685 static void
7686 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7687 {
7688 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7689 abbrev_table_up abbrev_table;
7690 sect_offset abbrev_offset;
7691
7692 /* It's up to the caller to not call us multiple times. */
7693 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7694
7695 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7696 return;
7697
7698 /* TUs typically share abbrev tables, and there can be way more TUs than
7699 abbrev tables. Sort by abbrev table to reduce the number of times we
7700 read each abbrev table in.
7701 Alternatives are to punt or to maintain a cache of abbrev tables.
7702 This is simpler and efficient enough for now.
7703
7704 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7705 symtab to use). Typically TUs with the same abbrev offset have the same
7706 stmt_list value too so in practice this should work well.
7707
7708 The basic algorithm here is:
7709
7710 sort TUs by abbrev table
7711 for each TU with same abbrev table:
7712 read abbrev table if first user
7713 read TU top level DIE
7714 [IWBN if DWO skeletons had DW_AT_stmt_list]
7715 call FUNC */
7716
7717 if (dwarf_read_debug)
7718 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7719
7720 /* Sort in a separate table to maintain the order of all_type_units
7721 for .gdb_index: TU indices directly index all_type_units. */
7722 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7723 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7724
7725 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7726 sorted_by_abbrev.emplace_back
7727 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7728 sig_type->per_cu.section,
7729 sig_type->per_cu.sect_off));
7730
7731 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7732 sort_tu_by_abbrev_offset);
7733
7734 abbrev_offset = (sect_offset) ~(unsigned) 0;
7735
7736 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7737 {
7738 /* Switch to the next abbrev table if necessary. */
7739 if (abbrev_table == NULL
7740 || tu.abbrev_offset != abbrev_offset)
7741 {
7742 abbrev_offset = tu.abbrev_offset;
7743 abbrev_table =
7744 abbrev_table::read (dwarf2_per_objfile->objfile,
7745 &dwarf2_per_objfile->per_bfd->abbrev,
7746 abbrev_offset);
7747 ++tu_stats->nr_uniq_abbrev_tables;
7748 }
7749
7750 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7751 abbrev_table.get (), nullptr, false);
7752 if (!reader.dummy_p)
7753 build_type_psymtabs_reader (&reader, reader.info_ptr,
7754 reader.comp_unit_die);
7755 }
7756 }
7757
7758 /* Print collected type unit statistics. */
7759
7760 static void
7761 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7762 {
7763 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7764
7765 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7766 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7767 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7768 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7769 tu_stats->nr_uniq_abbrev_tables);
7770 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7771 tu_stats->nr_symtabs);
7772 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7773 tu_stats->nr_symtab_sharers);
7774 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7775 tu_stats->nr_stmt_less_type_units);
7776 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7777 tu_stats->nr_all_type_units_reallocs);
7778 }
7779
7780 /* Traversal function for build_type_psymtabs. */
7781
7782 static int
7783 build_type_psymtab_dependencies (void **slot, void *info)
7784 {
7785 struct dwarf2_per_objfile *dwarf2_per_objfile
7786 = (struct dwarf2_per_objfile *) info;
7787 struct objfile *objfile = dwarf2_per_objfile->objfile;
7788 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7789 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7790 dwarf2_psymtab *pst = per_cu->v.psymtab;
7791 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7792 int i;
7793
7794 gdb_assert (len > 0);
7795 gdb_assert (per_cu->type_unit_group_p ());
7796
7797 pst->number_of_dependencies = len;
7798 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7799 for (i = 0; i < len; ++i)
7800 {
7801 struct signatured_type *iter = tu_group->tus->at (i);
7802 gdb_assert (iter->per_cu.is_debug_types);
7803 pst->dependencies[i] = iter->per_cu.v.psymtab;
7804 iter->type_unit_group = tu_group;
7805 }
7806
7807 delete tu_group->tus;
7808 tu_group->tus = nullptr;
7809
7810 return 1;
7811 }
7812
7813 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7814 Build partial symbol tables for the .debug_types comp-units. */
7815
7816 static void
7817 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7818 {
7819 if (! create_all_type_units (dwarf2_per_objfile))
7820 return;
7821
7822 build_type_psymtabs_1 (dwarf2_per_objfile);
7823 }
7824
7825 /* Traversal function for process_skeletonless_type_unit.
7826 Read a TU in a DWO file and build partial symbols for it. */
7827
7828 static int
7829 process_skeletonless_type_unit (void **slot, void *info)
7830 {
7831 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7832 struct dwarf2_per_objfile *dwarf2_per_objfile
7833 = (struct dwarf2_per_objfile *) info;
7834 struct signatured_type find_entry, *entry;
7835
7836 /* If this TU doesn't exist in the global table, add it and read it in. */
7837
7838 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7839 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7840
7841 find_entry.signature = dwo_unit->signature;
7842 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7843 &find_entry, INSERT);
7844 /* If we've already seen this type there's nothing to do. What's happening
7845 is we're doing our own version of comdat-folding here. */
7846 if (*slot != NULL)
7847 return 1;
7848
7849 /* This does the job that create_all_type_units would have done for
7850 this TU. */
7851 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7852 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7853 *slot = entry;
7854
7855 /* This does the job that build_type_psymtabs_1 would have done. */
7856 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, nullptr, nullptr,
7857 false);
7858 if (!reader.dummy_p)
7859 build_type_psymtabs_reader (&reader, reader.info_ptr,
7860 reader.comp_unit_die);
7861
7862 return 1;
7863 }
7864
7865 /* Traversal function for process_skeletonless_type_units. */
7866
7867 static int
7868 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7869 {
7870 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7871
7872 if (dwo_file->tus != NULL)
7873 htab_traverse_noresize (dwo_file->tus.get (),
7874 process_skeletonless_type_unit, info);
7875
7876 return 1;
7877 }
7878
7879 /* Scan all TUs of DWO files, verifying we've processed them.
7880 This is needed in case a TU was emitted without its skeleton.
7881 Note: This can't be done until we know what all the DWO files are. */
7882
7883 static void
7884 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7885 {
7886 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7887 if (get_dwp_file (dwarf2_per_objfile) == NULL
7888 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7889 {
7890 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7891 process_dwo_file_for_skeletonless_type_units,
7892 dwarf2_per_objfile);
7893 }
7894 }
7895
7896 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7897
7898 static void
7899 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7900 {
7901 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7902 {
7903 dwarf2_psymtab *pst = per_cu->v.psymtab;
7904
7905 if (pst == NULL)
7906 continue;
7907
7908 for (int j = 0; j < pst->number_of_dependencies; ++j)
7909 {
7910 /* Set the 'user' field only if it is not already set. */
7911 if (pst->dependencies[j]->user == NULL)
7912 pst->dependencies[j]->user = pst;
7913 }
7914 }
7915 }
7916
7917 /* Build the partial symbol table by doing a quick pass through the
7918 .debug_info and .debug_abbrev sections. */
7919
7920 static void
7921 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7922 {
7923 struct objfile *objfile = dwarf2_per_objfile->objfile;
7924
7925 if (dwarf_read_debug)
7926 {
7927 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7928 objfile_name (objfile));
7929 }
7930
7931 scoped_restore restore_reading_psyms
7932 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7933 true);
7934
7935 dwarf2_per_objfile->per_bfd->info.read (objfile);
7936
7937 /* Any cached compilation units will be linked by the per-objfile
7938 read_in_chain. Make sure to free them when we're done. */
7939 free_cached_comp_units freer (dwarf2_per_objfile);
7940
7941 build_type_psymtabs (dwarf2_per_objfile);
7942
7943 create_all_comp_units (dwarf2_per_objfile);
7944
7945 /* Create a temporary address map on a temporary obstack. We later
7946 copy this to the final obstack. */
7947 auto_obstack temp_obstack;
7948
7949 scoped_restore save_psymtabs_addrmap
7950 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7951 addrmap_create_mutable (&temp_obstack));
7952
7953 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7954 {
7955 if (per_cu->v.psymtab != NULL)
7956 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7957 continue;
7958 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7959 language_minimal);
7960 }
7961
7962 /* This has to wait until we read the CUs, we need the list of DWOs. */
7963 process_skeletonless_type_units (dwarf2_per_objfile);
7964
7965 /* Now that all TUs have been processed we can fill in the dependencies. */
7966 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7967 {
7968 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7969 build_type_psymtab_dependencies, dwarf2_per_objfile);
7970 }
7971
7972 if (dwarf_read_debug)
7973 print_tu_stats (dwarf2_per_objfile);
7974
7975 set_partial_user (dwarf2_per_objfile);
7976
7977 objfile->partial_symtabs->psymtabs_addrmap
7978 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7979 objfile->partial_symtabs->obstack ());
7980 /* At this point we want to keep the address map. */
7981 save_psymtabs_addrmap.release ();
7982
7983 if (dwarf_read_debug)
7984 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7985 objfile_name (objfile));
7986 }
7987
7988 /* Load the partial DIEs for a secondary CU into memory.
7989 This is also used when rereading a primary CU with load_all_dies. */
7990
7991 static void
7992 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7993 dwarf2_per_objfile *per_objfile,
7994 dwarf2_cu *existing_cu)
7995 {
7996 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7997
7998 if (!reader.dummy_p)
7999 {
8000 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8001 language_minimal);
8002
8003 /* Check if comp unit has_children.
8004 If so, read the rest of the partial symbols from this comp unit.
8005 If not, there's no more debug_info for this comp unit. */
8006 if (reader.comp_unit_die->has_children)
8007 load_partial_dies (&reader, reader.info_ptr, 0);
8008
8009 reader.keep ();
8010 }
8011 }
8012
8013 static void
8014 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8015 struct dwarf2_section_info *section,
8016 struct dwarf2_section_info *abbrev_section,
8017 unsigned int is_dwz)
8018 {
8019 const gdb_byte *info_ptr;
8020 struct objfile *objfile = dwarf2_per_objfile->objfile;
8021
8022 if (dwarf_read_debug)
8023 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8024 section->get_name (),
8025 section->get_file_name ());
8026
8027 section->read (objfile);
8028
8029 info_ptr = section->buffer;
8030
8031 while (info_ptr < section->buffer + section->size)
8032 {
8033 struct dwarf2_per_cu_data *this_cu;
8034
8035 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8036
8037 comp_unit_head cu_header;
8038 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8039 abbrev_section, info_ptr,
8040 rcuh_kind::COMPILE);
8041
8042 /* Save the compilation unit for later lookup. */
8043 if (cu_header.unit_type != DW_UT_type)
8044 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8045 else
8046 {
8047 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8048 sig_type->signature = cu_header.signature;
8049 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8050 this_cu = &sig_type->per_cu;
8051 }
8052 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8053 this_cu->sect_off = sect_off;
8054 this_cu->length = cu_header.length + cu_header.initial_length_size;
8055 this_cu->is_dwz = is_dwz;
8056 this_cu->section = section;
8057
8058 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8059
8060 info_ptr = info_ptr + this_cu->length;
8061 }
8062 }
8063
8064 /* Create a list of all compilation units in OBJFILE.
8065 This is only done for -readnow and building partial symtabs. */
8066
8067 static void
8068 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8069 {
8070 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8071 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8072 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8073
8074 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8075 if (dwz != NULL)
8076 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8077 1);
8078 }
8079
8080 /* Process all loaded DIEs for compilation unit CU, starting at
8081 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8082 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8083 DW_AT_ranges). See the comments of add_partial_subprogram on how
8084 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8085
8086 static void
8087 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8088 CORE_ADDR *highpc, int set_addrmap,
8089 struct dwarf2_cu *cu)
8090 {
8091 struct partial_die_info *pdi;
8092
8093 /* Now, march along the PDI's, descending into ones which have
8094 interesting children but skipping the children of the other ones,
8095 until we reach the end of the compilation unit. */
8096
8097 pdi = first_die;
8098
8099 while (pdi != NULL)
8100 {
8101 pdi->fixup (cu);
8102
8103 /* Anonymous namespaces or modules have no name but have interesting
8104 children, so we need to look at them. Ditto for anonymous
8105 enums. */
8106
8107 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8108 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8109 || pdi->tag == DW_TAG_imported_unit
8110 || pdi->tag == DW_TAG_inlined_subroutine)
8111 {
8112 switch (pdi->tag)
8113 {
8114 case DW_TAG_subprogram:
8115 case DW_TAG_inlined_subroutine:
8116 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8117 break;
8118 case DW_TAG_constant:
8119 case DW_TAG_variable:
8120 case DW_TAG_typedef:
8121 case DW_TAG_union_type:
8122 if (!pdi->is_declaration
8123 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8124 {
8125 add_partial_symbol (pdi, cu);
8126 }
8127 break;
8128 case DW_TAG_class_type:
8129 case DW_TAG_interface_type:
8130 case DW_TAG_structure_type:
8131 if (!pdi->is_declaration)
8132 {
8133 add_partial_symbol (pdi, cu);
8134 }
8135 if ((cu->language == language_rust
8136 || cu->language == language_cplus) && pdi->has_children)
8137 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8138 set_addrmap, cu);
8139 break;
8140 case DW_TAG_enumeration_type:
8141 if (!pdi->is_declaration)
8142 add_partial_enumeration (pdi, cu);
8143 break;
8144 case DW_TAG_base_type:
8145 case DW_TAG_subrange_type:
8146 /* File scope base type definitions are added to the partial
8147 symbol table. */
8148 add_partial_symbol (pdi, cu);
8149 break;
8150 case DW_TAG_namespace:
8151 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8152 break;
8153 case DW_TAG_module:
8154 if (!pdi->is_declaration)
8155 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8156 break;
8157 case DW_TAG_imported_unit:
8158 {
8159 struct dwarf2_per_cu_data *per_cu;
8160
8161 /* For now we don't handle imported units in type units. */
8162 if (cu->per_cu->is_debug_types)
8163 {
8164 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8165 " supported in type units [in module %s]"),
8166 objfile_name (cu->per_objfile->objfile));
8167 }
8168
8169 per_cu = dwarf2_find_containing_comp_unit
8170 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8171
8172 /* Go read the partial unit, if needed. */
8173 if (per_cu->v.psymtab == NULL)
8174 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8175 cu->language);
8176
8177 cu->per_cu->imported_symtabs_push (per_cu);
8178 }
8179 break;
8180 case DW_TAG_imported_declaration:
8181 add_partial_symbol (pdi, cu);
8182 break;
8183 default:
8184 break;
8185 }
8186 }
8187
8188 /* If the die has a sibling, skip to the sibling. */
8189
8190 pdi = pdi->die_sibling;
8191 }
8192 }
8193
8194 /* Functions used to compute the fully scoped name of a partial DIE.
8195
8196 Normally, this is simple. For C++, the parent DIE's fully scoped
8197 name is concatenated with "::" and the partial DIE's name.
8198 Enumerators are an exception; they use the scope of their parent
8199 enumeration type, i.e. the name of the enumeration type is not
8200 prepended to the enumerator.
8201
8202 There are two complexities. One is DW_AT_specification; in this
8203 case "parent" means the parent of the target of the specification,
8204 instead of the direct parent of the DIE. The other is compilers
8205 which do not emit DW_TAG_namespace; in this case we try to guess
8206 the fully qualified name of structure types from their members'
8207 linkage names. This must be done using the DIE's children rather
8208 than the children of any DW_AT_specification target. We only need
8209 to do this for structures at the top level, i.e. if the target of
8210 any DW_AT_specification (if any; otherwise the DIE itself) does not
8211 have a parent. */
8212
8213 /* Compute the scope prefix associated with PDI's parent, in
8214 compilation unit CU. The result will be allocated on CU's
8215 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8216 field. NULL is returned if no prefix is necessary. */
8217 static const char *
8218 partial_die_parent_scope (struct partial_die_info *pdi,
8219 struct dwarf2_cu *cu)
8220 {
8221 const char *grandparent_scope;
8222 struct partial_die_info *parent, *real_pdi;
8223
8224 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8225 then this means the parent of the specification DIE. */
8226
8227 real_pdi = pdi;
8228 while (real_pdi->has_specification)
8229 {
8230 auto res = find_partial_die (real_pdi->spec_offset,
8231 real_pdi->spec_is_dwz, cu);
8232 real_pdi = res.pdi;
8233 cu = res.cu;
8234 }
8235
8236 parent = real_pdi->die_parent;
8237 if (parent == NULL)
8238 return NULL;
8239
8240 if (parent->scope_set)
8241 return parent->scope;
8242
8243 parent->fixup (cu);
8244
8245 grandparent_scope = partial_die_parent_scope (parent, cu);
8246
8247 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8248 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8249 Work around this problem here. */
8250 if (cu->language == language_cplus
8251 && parent->tag == DW_TAG_namespace
8252 && strcmp (parent->name, "::") == 0
8253 && grandparent_scope == NULL)
8254 {
8255 parent->scope = NULL;
8256 parent->scope_set = 1;
8257 return NULL;
8258 }
8259
8260 /* Nested subroutines in Fortran get a prefix. */
8261 if (pdi->tag == DW_TAG_enumerator)
8262 /* Enumerators should not get the name of the enumeration as a prefix. */
8263 parent->scope = grandparent_scope;
8264 else if (parent->tag == DW_TAG_namespace
8265 || parent->tag == DW_TAG_module
8266 || parent->tag == DW_TAG_structure_type
8267 || parent->tag == DW_TAG_class_type
8268 || parent->tag == DW_TAG_interface_type
8269 || parent->tag == DW_TAG_union_type
8270 || parent->tag == DW_TAG_enumeration_type
8271 || (cu->language == language_fortran
8272 && parent->tag == DW_TAG_subprogram
8273 && pdi->tag == DW_TAG_subprogram))
8274 {
8275 if (grandparent_scope == NULL)
8276 parent->scope = parent->name;
8277 else
8278 parent->scope = typename_concat (&cu->comp_unit_obstack,
8279 grandparent_scope,
8280 parent->name, 0, cu);
8281 }
8282 else
8283 {
8284 /* FIXME drow/2004-04-01: What should we be doing with
8285 function-local names? For partial symbols, we should probably be
8286 ignoring them. */
8287 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8288 dwarf_tag_name (parent->tag),
8289 sect_offset_str (pdi->sect_off));
8290 parent->scope = grandparent_scope;
8291 }
8292
8293 parent->scope_set = 1;
8294 return parent->scope;
8295 }
8296
8297 /* Return the fully scoped name associated with PDI, from compilation unit
8298 CU. The result will be allocated with malloc. */
8299
8300 static gdb::unique_xmalloc_ptr<char>
8301 partial_die_full_name (struct partial_die_info *pdi,
8302 struct dwarf2_cu *cu)
8303 {
8304 const char *parent_scope;
8305
8306 /* If this is a template instantiation, we can not work out the
8307 template arguments from partial DIEs. So, unfortunately, we have
8308 to go through the full DIEs. At least any work we do building
8309 types here will be reused if full symbols are loaded later. */
8310 if (pdi->has_template_arguments)
8311 {
8312 pdi->fixup (cu);
8313
8314 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8315 {
8316 struct die_info *die;
8317 struct attribute attr;
8318 struct dwarf2_cu *ref_cu = cu;
8319
8320 /* DW_FORM_ref_addr is using section offset. */
8321 attr.name = (enum dwarf_attribute) 0;
8322 attr.form = DW_FORM_ref_addr;
8323 attr.u.unsnd = to_underlying (pdi->sect_off);
8324 die = follow_die_ref (NULL, &attr, &ref_cu);
8325
8326 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8327 }
8328 }
8329
8330 parent_scope = partial_die_parent_scope (pdi, cu);
8331 if (parent_scope == NULL)
8332 return NULL;
8333 else
8334 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8335 pdi->name, 0, cu));
8336 }
8337
8338 static void
8339 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8340 {
8341 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8342 struct objfile *objfile = dwarf2_per_objfile->objfile;
8343 struct gdbarch *gdbarch = objfile->arch ();
8344 CORE_ADDR addr = 0;
8345 const char *actual_name = NULL;
8346 CORE_ADDR baseaddr;
8347
8348 baseaddr = objfile->text_section_offset ();
8349
8350 gdb::unique_xmalloc_ptr<char> built_actual_name
8351 = partial_die_full_name (pdi, cu);
8352 if (built_actual_name != NULL)
8353 actual_name = built_actual_name.get ();
8354
8355 if (actual_name == NULL)
8356 actual_name = pdi->name;
8357
8358 partial_symbol psymbol;
8359 memset (&psymbol, 0, sizeof (psymbol));
8360 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8361 psymbol.ginfo.section = -1;
8362
8363 /* The code below indicates that the psymbol should be installed by
8364 setting this. */
8365 gdb::optional<psymbol_placement> where;
8366
8367 switch (pdi->tag)
8368 {
8369 case DW_TAG_inlined_subroutine:
8370 case DW_TAG_subprogram:
8371 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8372 - baseaddr);
8373 if (pdi->is_external
8374 || cu->language == language_ada
8375 || (cu->language == language_fortran
8376 && pdi->die_parent != NULL
8377 && pdi->die_parent->tag == DW_TAG_subprogram))
8378 {
8379 /* Normally, only "external" DIEs are part of the global scope.
8380 But in Ada and Fortran, we want to be able to access nested
8381 procedures globally. So all Ada and Fortran subprograms are
8382 stored in the global scope. */
8383 where = psymbol_placement::GLOBAL;
8384 }
8385 else
8386 where = psymbol_placement::STATIC;
8387
8388 psymbol.domain = VAR_DOMAIN;
8389 psymbol.aclass = LOC_BLOCK;
8390 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8391 psymbol.ginfo.value.address = addr;
8392
8393 if (pdi->main_subprogram && actual_name != NULL)
8394 set_objfile_main_name (objfile, actual_name, cu->language);
8395 break;
8396 case DW_TAG_constant:
8397 psymbol.domain = VAR_DOMAIN;
8398 psymbol.aclass = LOC_STATIC;
8399 where = (pdi->is_external
8400 ? psymbol_placement::GLOBAL
8401 : psymbol_placement::STATIC);
8402 break;
8403 case DW_TAG_variable:
8404 if (pdi->d.locdesc)
8405 addr = decode_locdesc (pdi->d.locdesc, cu);
8406
8407 if (pdi->d.locdesc
8408 && addr == 0
8409 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8410 {
8411 /* A global or static variable may also have been stripped
8412 out by the linker if unused, in which case its address
8413 will be nullified; do not add such variables into partial
8414 symbol table then. */
8415 }
8416 else if (pdi->is_external)
8417 {
8418 /* Global Variable.
8419 Don't enter into the minimal symbol tables as there is
8420 a minimal symbol table entry from the ELF symbols already.
8421 Enter into partial symbol table if it has a location
8422 descriptor or a type.
8423 If the location descriptor is missing, new_symbol will create
8424 a LOC_UNRESOLVED symbol, the address of the variable will then
8425 be determined from the minimal symbol table whenever the variable
8426 is referenced.
8427 The address for the partial symbol table entry is not
8428 used by GDB, but it comes in handy for debugging partial symbol
8429 table building. */
8430
8431 if (pdi->d.locdesc || pdi->has_type)
8432 {
8433 psymbol.domain = VAR_DOMAIN;
8434 psymbol.aclass = LOC_STATIC;
8435 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8436 psymbol.ginfo.value.address = addr;
8437 where = psymbol_placement::GLOBAL;
8438 }
8439 }
8440 else
8441 {
8442 int has_loc = pdi->d.locdesc != NULL;
8443
8444 /* Static Variable. Skip symbols whose value we cannot know (those
8445 without location descriptors or constant values). */
8446 if (!has_loc && !pdi->has_const_value)
8447 return;
8448
8449 psymbol.domain = VAR_DOMAIN;
8450 psymbol.aclass = LOC_STATIC;
8451 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8452 if (has_loc)
8453 psymbol.ginfo.value.address = addr;
8454 where = psymbol_placement::STATIC;
8455 }
8456 break;
8457 case DW_TAG_typedef:
8458 case DW_TAG_base_type:
8459 case DW_TAG_subrange_type:
8460 psymbol.domain = VAR_DOMAIN;
8461 psymbol.aclass = LOC_TYPEDEF;
8462 where = psymbol_placement::STATIC;
8463 break;
8464 case DW_TAG_imported_declaration:
8465 case DW_TAG_namespace:
8466 psymbol.domain = VAR_DOMAIN;
8467 psymbol.aclass = LOC_TYPEDEF;
8468 where = psymbol_placement::GLOBAL;
8469 break;
8470 case DW_TAG_module:
8471 /* With Fortran 77 there might be a "BLOCK DATA" module
8472 available without any name. If so, we skip the module as it
8473 doesn't bring any value. */
8474 if (actual_name != nullptr)
8475 {
8476 psymbol.domain = MODULE_DOMAIN;
8477 psymbol.aclass = LOC_TYPEDEF;
8478 where = psymbol_placement::GLOBAL;
8479 }
8480 break;
8481 case DW_TAG_class_type:
8482 case DW_TAG_interface_type:
8483 case DW_TAG_structure_type:
8484 case DW_TAG_union_type:
8485 case DW_TAG_enumeration_type:
8486 /* Skip external references. The DWARF standard says in the section
8487 about "Structure, Union, and Class Type Entries": "An incomplete
8488 structure, union or class type is represented by a structure,
8489 union or class entry that does not have a byte size attribute
8490 and that has a DW_AT_declaration attribute." */
8491 if (!pdi->has_byte_size && pdi->is_declaration)
8492 return;
8493
8494 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8495 static vs. global. */
8496 psymbol.domain = STRUCT_DOMAIN;
8497 psymbol.aclass = LOC_TYPEDEF;
8498 where = (cu->language == language_cplus
8499 ? psymbol_placement::GLOBAL
8500 : psymbol_placement::STATIC);
8501 break;
8502 case DW_TAG_enumerator:
8503 psymbol.domain = VAR_DOMAIN;
8504 psymbol.aclass = LOC_CONST;
8505 where = (cu->language == language_cplus
8506 ? psymbol_placement::GLOBAL
8507 : psymbol_placement::STATIC);
8508 break;
8509 default:
8510 break;
8511 }
8512
8513 if (where.has_value ())
8514 {
8515 if (built_actual_name != nullptr)
8516 actual_name = objfile->intern (actual_name);
8517 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8518 psymbol.ginfo.set_linkage_name (actual_name);
8519 else
8520 {
8521 psymbol.ginfo.set_demangled_name (actual_name,
8522 &objfile->objfile_obstack);
8523 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8524 }
8525 add_psymbol_to_list (psymbol, *where, objfile);
8526 }
8527 }
8528
8529 /* Read a partial die corresponding to a namespace; also, add a symbol
8530 corresponding to that namespace to the symbol table. NAMESPACE is
8531 the name of the enclosing namespace. */
8532
8533 static void
8534 add_partial_namespace (struct partial_die_info *pdi,
8535 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8536 int set_addrmap, struct dwarf2_cu *cu)
8537 {
8538 /* Add a symbol for the namespace. */
8539
8540 add_partial_symbol (pdi, cu);
8541
8542 /* Now scan partial symbols in that namespace. */
8543
8544 if (pdi->has_children)
8545 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8546 }
8547
8548 /* Read a partial die corresponding to a Fortran module. */
8549
8550 static void
8551 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8552 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8553 {
8554 /* Add a symbol for the namespace. */
8555
8556 add_partial_symbol (pdi, cu);
8557
8558 /* Now scan partial symbols in that module. */
8559
8560 if (pdi->has_children)
8561 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8562 }
8563
8564 /* Read a partial die corresponding to a subprogram or an inlined
8565 subprogram and create a partial symbol for that subprogram.
8566 When the CU language allows it, this routine also defines a partial
8567 symbol for each nested subprogram that this subprogram contains.
8568 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8569 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8570
8571 PDI may also be a lexical block, in which case we simply search
8572 recursively for subprograms defined inside that lexical block.
8573 Again, this is only performed when the CU language allows this
8574 type of definitions. */
8575
8576 static void
8577 add_partial_subprogram (struct partial_die_info *pdi,
8578 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8579 int set_addrmap, struct dwarf2_cu *cu)
8580 {
8581 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8582 {
8583 if (pdi->has_pc_info)
8584 {
8585 if (pdi->lowpc < *lowpc)
8586 *lowpc = pdi->lowpc;
8587 if (pdi->highpc > *highpc)
8588 *highpc = pdi->highpc;
8589 if (set_addrmap)
8590 {
8591 struct objfile *objfile = cu->per_objfile->objfile;
8592 struct gdbarch *gdbarch = objfile->arch ();
8593 CORE_ADDR baseaddr;
8594 CORE_ADDR this_highpc;
8595 CORE_ADDR this_lowpc;
8596
8597 baseaddr = objfile->text_section_offset ();
8598 this_lowpc
8599 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8600 pdi->lowpc + baseaddr)
8601 - baseaddr);
8602 this_highpc
8603 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8604 pdi->highpc + baseaddr)
8605 - baseaddr);
8606 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8607 this_lowpc, this_highpc - 1,
8608 cu->per_cu->v.psymtab);
8609 }
8610 }
8611
8612 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8613 {
8614 if (!pdi->is_declaration)
8615 /* Ignore subprogram DIEs that do not have a name, they are
8616 illegal. Do not emit a complaint at this point, we will
8617 do so when we convert this psymtab into a symtab. */
8618 if (pdi->name)
8619 add_partial_symbol (pdi, cu);
8620 }
8621 }
8622
8623 if (! pdi->has_children)
8624 return;
8625
8626 if (cu->language == language_ada || cu->language == language_fortran)
8627 {
8628 pdi = pdi->die_child;
8629 while (pdi != NULL)
8630 {
8631 pdi->fixup (cu);
8632 if (pdi->tag == DW_TAG_subprogram
8633 || pdi->tag == DW_TAG_inlined_subroutine
8634 || pdi->tag == DW_TAG_lexical_block)
8635 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8636 pdi = pdi->die_sibling;
8637 }
8638 }
8639 }
8640
8641 /* Read a partial die corresponding to an enumeration type. */
8642
8643 static void
8644 add_partial_enumeration (struct partial_die_info *enum_pdi,
8645 struct dwarf2_cu *cu)
8646 {
8647 struct partial_die_info *pdi;
8648
8649 if (enum_pdi->name != NULL)
8650 add_partial_symbol (enum_pdi, cu);
8651
8652 pdi = enum_pdi->die_child;
8653 while (pdi)
8654 {
8655 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8656 complaint (_("malformed enumerator DIE ignored"));
8657 else
8658 add_partial_symbol (pdi, cu);
8659 pdi = pdi->die_sibling;
8660 }
8661 }
8662
8663 /* Return the initial uleb128 in the die at INFO_PTR. */
8664
8665 static unsigned int
8666 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8667 {
8668 unsigned int bytes_read;
8669
8670 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8671 }
8672
8673 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8674 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8675
8676 Return the corresponding abbrev, or NULL if the number is zero (indicating
8677 an empty DIE). In either case *BYTES_READ will be set to the length of
8678 the initial number. */
8679
8680 static struct abbrev_info *
8681 peek_die_abbrev (const die_reader_specs &reader,
8682 const gdb_byte *info_ptr, unsigned int *bytes_read)
8683 {
8684 dwarf2_cu *cu = reader.cu;
8685 bfd *abfd = cu->per_objfile->objfile->obfd;
8686 unsigned int abbrev_number
8687 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8688
8689 if (abbrev_number == 0)
8690 return NULL;
8691
8692 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8693 if (!abbrev)
8694 {
8695 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8696 " at offset %s [in module %s]"),
8697 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8698 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8699 }
8700
8701 return abbrev;
8702 }
8703
8704 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8705 Returns a pointer to the end of a series of DIEs, terminated by an empty
8706 DIE. Any children of the skipped DIEs will also be skipped. */
8707
8708 static const gdb_byte *
8709 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8710 {
8711 while (1)
8712 {
8713 unsigned int bytes_read;
8714 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8715
8716 if (abbrev == NULL)
8717 return info_ptr + bytes_read;
8718 else
8719 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8720 }
8721 }
8722
8723 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8724 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8725 abbrev corresponding to that skipped uleb128 should be passed in
8726 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8727 children. */
8728
8729 static const gdb_byte *
8730 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8731 struct abbrev_info *abbrev)
8732 {
8733 unsigned int bytes_read;
8734 struct attribute attr;
8735 bfd *abfd = reader->abfd;
8736 struct dwarf2_cu *cu = reader->cu;
8737 const gdb_byte *buffer = reader->buffer;
8738 const gdb_byte *buffer_end = reader->buffer_end;
8739 unsigned int form, i;
8740
8741 for (i = 0; i < abbrev->num_attrs; i++)
8742 {
8743 /* The only abbrev we care about is DW_AT_sibling. */
8744 if (abbrev->attrs[i].name == DW_AT_sibling)
8745 {
8746 bool ignored;
8747 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8748 &ignored);
8749 if (attr.form == DW_FORM_ref_addr)
8750 complaint (_("ignoring absolute DW_AT_sibling"));
8751 else
8752 {
8753 sect_offset off = attr.get_ref_die_offset ();
8754 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8755
8756 if (sibling_ptr < info_ptr)
8757 complaint (_("DW_AT_sibling points backwards"));
8758 else if (sibling_ptr > reader->buffer_end)
8759 reader->die_section->overflow_complaint ();
8760 else
8761 return sibling_ptr;
8762 }
8763 }
8764
8765 /* If it isn't DW_AT_sibling, skip this attribute. */
8766 form = abbrev->attrs[i].form;
8767 skip_attribute:
8768 switch (form)
8769 {
8770 case DW_FORM_ref_addr:
8771 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8772 and later it is offset sized. */
8773 if (cu->header.version == 2)
8774 info_ptr += cu->header.addr_size;
8775 else
8776 info_ptr += cu->header.offset_size;
8777 break;
8778 case DW_FORM_GNU_ref_alt:
8779 info_ptr += cu->header.offset_size;
8780 break;
8781 case DW_FORM_addr:
8782 info_ptr += cu->header.addr_size;
8783 break;
8784 case DW_FORM_data1:
8785 case DW_FORM_ref1:
8786 case DW_FORM_flag:
8787 case DW_FORM_strx1:
8788 info_ptr += 1;
8789 break;
8790 case DW_FORM_flag_present:
8791 case DW_FORM_implicit_const:
8792 break;
8793 case DW_FORM_data2:
8794 case DW_FORM_ref2:
8795 case DW_FORM_strx2:
8796 info_ptr += 2;
8797 break;
8798 case DW_FORM_strx3:
8799 info_ptr += 3;
8800 break;
8801 case DW_FORM_data4:
8802 case DW_FORM_ref4:
8803 case DW_FORM_strx4:
8804 info_ptr += 4;
8805 break;
8806 case DW_FORM_data8:
8807 case DW_FORM_ref8:
8808 case DW_FORM_ref_sig8:
8809 info_ptr += 8;
8810 break;
8811 case DW_FORM_data16:
8812 info_ptr += 16;
8813 break;
8814 case DW_FORM_string:
8815 read_direct_string (abfd, info_ptr, &bytes_read);
8816 info_ptr += bytes_read;
8817 break;
8818 case DW_FORM_sec_offset:
8819 case DW_FORM_strp:
8820 case DW_FORM_GNU_strp_alt:
8821 info_ptr += cu->header.offset_size;
8822 break;
8823 case DW_FORM_exprloc:
8824 case DW_FORM_block:
8825 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8826 info_ptr += bytes_read;
8827 break;
8828 case DW_FORM_block1:
8829 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8830 break;
8831 case DW_FORM_block2:
8832 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8833 break;
8834 case DW_FORM_block4:
8835 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8836 break;
8837 case DW_FORM_addrx:
8838 case DW_FORM_strx:
8839 case DW_FORM_sdata:
8840 case DW_FORM_udata:
8841 case DW_FORM_ref_udata:
8842 case DW_FORM_GNU_addr_index:
8843 case DW_FORM_GNU_str_index:
8844 case DW_FORM_rnglistx:
8845 case DW_FORM_loclistx:
8846 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8847 break;
8848 case DW_FORM_indirect:
8849 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8850 info_ptr += bytes_read;
8851 /* We need to continue parsing from here, so just go back to
8852 the top. */
8853 goto skip_attribute;
8854
8855 default:
8856 error (_("Dwarf Error: Cannot handle %s "
8857 "in DWARF reader [in module %s]"),
8858 dwarf_form_name (form),
8859 bfd_get_filename (abfd));
8860 }
8861 }
8862
8863 if (abbrev->has_children)
8864 return skip_children (reader, info_ptr);
8865 else
8866 return info_ptr;
8867 }
8868
8869 /* Locate ORIG_PDI's sibling.
8870 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8871
8872 static const gdb_byte *
8873 locate_pdi_sibling (const struct die_reader_specs *reader,
8874 struct partial_die_info *orig_pdi,
8875 const gdb_byte *info_ptr)
8876 {
8877 /* Do we know the sibling already? */
8878
8879 if (orig_pdi->sibling)
8880 return orig_pdi->sibling;
8881
8882 /* Are there any children to deal with? */
8883
8884 if (!orig_pdi->has_children)
8885 return info_ptr;
8886
8887 /* Skip the children the long way. */
8888
8889 return skip_children (reader, info_ptr);
8890 }
8891
8892 /* Expand this partial symbol table into a full symbol table. SELF is
8893 not NULL. */
8894
8895 void
8896 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8897 {
8898 struct dwarf2_per_objfile *dwarf2_per_objfile
8899 = get_dwarf2_per_objfile (objfile);
8900
8901 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8902
8903 /* If this psymtab is constructed from a debug-only objfile, the
8904 has_section_at_zero flag will not necessarily be correct. We
8905 can get the correct value for this flag by looking at the data
8906 associated with the (presumably stripped) associated objfile. */
8907 if (objfile->separate_debug_objfile_backlink)
8908 {
8909 struct dwarf2_per_objfile *dpo_backlink
8910 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8911
8912 dwarf2_per_objfile->per_bfd->has_section_at_zero
8913 = dpo_backlink->per_bfd->has_section_at_zero;
8914 }
8915
8916 expand_psymtab (objfile);
8917
8918 process_cu_includes (dwarf2_per_objfile);
8919 }
8920 \f
8921 /* Reading in full CUs. */
8922
8923 /* Add PER_CU to the queue. */
8924
8925 static void
8926 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8927 dwarf2_per_objfile *per_objfile,
8928 enum language pretend_language)
8929 {
8930 per_cu->queued = 1;
8931 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8932 }
8933
8934 /* If PER_CU is not yet queued, add it to the queue.
8935 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8936 dependency.
8937 The result is non-zero if PER_CU was queued, otherwise the result is zero
8938 meaning either PER_CU is already queued or it is already loaded.
8939
8940 N.B. There is an invariant here that if a CU is queued then it is loaded.
8941 The caller is required to load PER_CU if we return non-zero. */
8942
8943 static int
8944 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8945 dwarf2_per_cu_data *per_cu,
8946 dwarf2_per_objfile *per_objfile,
8947 enum language pretend_language)
8948 {
8949 /* We may arrive here during partial symbol reading, if we need full
8950 DIEs to process an unusual case (e.g. template arguments). Do
8951 not queue PER_CU, just tell our caller to load its DIEs. */
8952 if (per_cu->per_bfd->reading_partial_symbols)
8953 {
8954 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8955 return 1;
8956 return 0;
8957 }
8958
8959 /* Mark the dependence relation so that we don't flush PER_CU
8960 too early. */
8961 if (dependent_cu != NULL)
8962 dwarf2_add_dependence (dependent_cu, per_cu);
8963
8964 /* If it's already on the queue, we have nothing to do. */
8965 if (per_cu->queued)
8966 return 0;
8967
8968 /* If the compilation unit is already loaded, just mark it as
8969 used. */
8970 if (per_cu->cu != NULL)
8971 {
8972 per_cu->cu->last_used = 0;
8973 return 0;
8974 }
8975
8976 /* Add it to the queue. */
8977 queue_comp_unit (per_cu, per_objfile, pretend_language);
8978
8979 return 1;
8980 }
8981
8982 /* Process the queue. */
8983
8984 static void
8985 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8986 {
8987 if (dwarf_read_debug)
8988 {
8989 fprintf_unfiltered (gdb_stdlog,
8990 "Expanding one or more symtabs of objfile %s ...\n",
8991 objfile_name (dwarf2_per_objfile->objfile));
8992 }
8993
8994 /* The queue starts out with one item, but following a DIE reference
8995 may load a new CU, adding it to the end of the queue. */
8996 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8997 {
8998 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8999
9000 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
9001 /* Skip dummy CUs. */
9002 && item.per_cu->cu != NULL)
9003 {
9004 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9005 unsigned int debug_print_threshold;
9006 char buf[100];
9007
9008 if (per_cu->is_debug_types)
9009 {
9010 struct signatured_type *sig_type =
9011 (struct signatured_type *) per_cu;
9012
9013 sprintf (buf, "TU %s at offset %s",
9014 hex_string (sig_type->signature),
9015 sect_offset_str (per_cu->sect_off));
9016 /* There can be 100s of TUs.
9017 Only print them in verbose mode. */
9018 debug_print_threshold = 2;
9019 }
9020 else
9021 {
9022 sprintf (buf, "CU at offset %s",
9023 sect_offset_str (per_cu->sect_off));
9024 debug_print_threshold = 1;
9025 }
9026
9027 if (dwarf_read_debug >= debug_print_threshold)
9028 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9029
9030 if (per_cu->is_debug_types)
9031 process_full_type_unit (per_cu->cu, item.pretend_language);
9032 else
9033 process_full_comp_unit (per_cu->cu, item.pretend_language);
9034
9035 if (dwarf_read_debug >= debug_print_threshold)
9036 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9037 }
9038
9039 item.per_cu->queued = 0;
9040 dwarf2_per_objfile->per_bfd->queue.pop ();
9041 }
9042
9043 if (dwarf_read_debug)
9044 {
9045 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9046 objfile_name (dwarf2_per_objfile->objfile));
9047 }
9048 }
9049
9050 /* Read in full symbols for PST, and anything it depends on. */
9051
9052 void
9053 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9054 {
9055 gdb_assert (!readin_p (objfile));
9056
9057 expand_dependencies (objfile);
9058
9059 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9060 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9061 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9062 }
9063
9064 /* See psympriv.h. */
9065
9066 bool
9067 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9068 {
9069 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9070 return per_objfile->symtab_set_p (per_cu_data);
9071 }
9072
9073 /* See psympriv.h. */
9074
9075 compunit_symtab *
9076 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9077 {
9078 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9079 return per_objfile->get_symtab (per_cu_data);
9080 }
9081
9082 /* Trivial hash function for die_info: the hash value of a DIE
9083 is its offset in .debug_info for this objfile. */
9084
9085 static hashval_t
9086 die_hash (const void *item)
9087 {
9088 const struct die_info *die = (const struct die_info *) item;
9089
9090 return to_underlying (die->sect_off);
9091 }
9092
9093 /* Trivial comparison function for die_info structures: two DIEs
9094 are equal if they have the same offset. */
9095
9096 static int
9097 die_eq (const void *item_lhs, const void *item_rhs)
9098 {
9099 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9100 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9101
9102 return die_lhs->sect_off == die_rhs->sect_off;
9103 }
9104
9105 /* Load the DIEs associated with PER_CU into memory. */
9106
9107 static void
9108 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9109 dwarf2_per_objfile *per_objfile,
9110 bool skip_partial,
9111 enum language pretend_language)
9112 {
9113 gdb_assert (! this_cu->is_debug_types);
9114
9115 cutu_reader reader (this_cu, per_objfile, NULL, this_cu->cu, skip_partial);
9116 if (reader.dummy_p)
9117 return;
9118
9119 struct dwarf2_cu *cu = reader.cu;
9120 const gdb_byte *info_ptr = reader.info_ptr;
9121
9122 gdb_assert (cu->die_hash == NULL);
9123 cu->die_hash =
9124 htab_create_alloc_ex (cu->header.length / 12,
9125 die_hash,
9126 die_eq,
9127 NULL,
9128 &cu->comp_unit_obstack,
9129 hashtab_obstack_allocate,
9130 dummy_obstack_deallocate);
9131
9132 if (reader.comp_unit_die->has_children)
9133 reader.comp_unit_die->child
9134 = read_die_and_siblings (&reader, reader.info_ptr,
9135 &info_ptr, reader.comp_unit_die);
9136 cu->dies = reader.comp_unit_die;
9137 /* comp_unit_die is not stored in die_hash, no need. */
9138
9139 /* We try not to read any attributes in this function, because not
9140 all CUs needed for references have been loaded yet, and symbol
9141 table processing isn't initialized. But we have to set the CU language,
9142 or we won't be able to build types correctly.
9143 Similarly, if we do not read the producer, we can not apply
9144 producer-specific interpretation. */
9145 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9146
9147 reader.keep ();
9148 }
9149
9150 /* Add a DIE to the delayed physname list. */
9151
9152 static void
9153 add_to_method_list (struct type *type, int fnfield_index, int index,
9154 const char *name, struct die_info *die,
9155 struct dwarf2_cu *cu)
9156 {
9157 struct delayed_method_info mi;
9158 mi.type = type;
9159 mi.fnfield_index = fnfield_index;
9160 mi.index = index;
9161 mi.name = name;
9162 mi.die = die;
9163 cu->method_list.push_back (mi);
9164 }
9165
9166 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9167 "const" / "volatile". If so, decrements LEN by the length of the
9168 modifier and return true. Otherwise return false. */
9169
9170 template<size_t N>
9171 static bool
9172 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9173 {
9174 size_t mod_len = sizeof (mod) - 1;
9175 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9176 {
9177 len -= mod_len;
9178 return true;
9179 }
9180 return false;
9181 }
9182
9183 /* Compute the physnames of any methods on the CU's method list.
9184
9185 The computation of method physnames is delayed in order to avoid the
9186 (bad) condition that one of the method's formal parameters is of an as yet
9187 incomplete type. */
9188
9189 static void
9190 compute_delayed_physnames (struct dwarf2_cu *cu)
9191 {
9192 /* Only C++ delays computing physnames. */
9193 if (cu->method_list.empty ())
9194 return;
9195 gdb_assert (cu->language == language_cplus);
9196
9197 for (const delayed_method_info &mi : cu->method_list)
9198 {
9199 const char *physname;
9200 struct fn_fieldlist *fn_flp
9201 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9202 physname = dwarf2_physname (mi.name, mi.die, cu);
9203 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9204 = physname ? physname : "";
9205
9206 /* Since there's no tag to indicate whether a method is a
9207 const/volatile overload, extract that information out of the
9208 demangled name. */
9209 if (physname != NULL)
9210 {
9211 size_t len = strlen (physname);
9212
9213 while (1)
9214 {
9215 if (physname[len] == ')') /* shortcut */
9216 break;
9217 else if (check_modifier (physname, len, " const"))
9218 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9219 else if (check_modifier (physname, len, " volatile"))
9220 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9221 else
9222 break;
9223 }
9224 }
9225 }
9226
9227 /* The list is no longer needed. */
9228 cu->method_list.clear ();
9229 }
9230
9231 /* Go objects should be embedded in a DW_TAG_module DIE,
9232 and it's not clear if/how imported objects will appear.
9233 To keep Go support simple until that's worked out,
9234 go back through what we've read and create something usable.
9235 We could do this while processing each DIE, and feels kinda cleaner,
9236 but that way is more invasive.
9237 This is to, for example, allow the user to type "p var" or "b main"
9238 without having to specify the package name, and allow lookups
9239 of module.object to work in contexts that use the expression
9240 parser. */
9241
9242 static void
9243 fixup_go_packaging (struct dwarf2_cu *cu)
9244 {
9245 gdb::unique_xmalloc_ptr<char> package_name;
9246 struct pending *list;
9247 int i;
9248
9249 for (list = *cu->get_builder ()->get_global_symbols ();
9250 list != NULL;
9251 list = list->next)
9252 {
9253 for (i = 0; i < list->nsyms; ++i)
9254 {
9255 struct symbol *sym = list->symbol[i];
9256
9257 if (sym->language () == language_go
9258 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9259 {
9260 gdb::unique_xmalloc_ptr<char> this_package_name
9261 (go_symbol_package_name (sym));
9262
9263 if (this_package_name == NULL)
9264 continue;
9265 if (package_name == NULL)
9266 package_name = std::move (this_package_name);
9267 else
9268 {
9269 struct objfile *objfile = cu->per_objfile->objfile;
9270 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9271 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9272 (symbol_symtab (sym) != NULL
9273 ? symtab_to_filename_for_display
9274 (symbol_symtab (sym))
9275 : objfile_name (objfile)),
9276 this_package_name.get (), package_name.get ());
9277 }
9278 }
9279 }
9280 }
9281
9282 if (package_name != NULL)
9283 {
9284 struct objfile *objfile = cu->per_objfile->objfile;
9285 const char *saved_package_name = objfile->intern (package_name.get ());
9286 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9287 saved_package_name);
9288 struct symbol *sym;
9289
9290 sym = new (&objfile->objfile_obstack) symbol;
9291 sym->set_language (language_go, &objfile->objfile_obstack);
9292 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9293 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9294 e.g., "main" finds the "main" module and not C's main(). */
9295 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9296 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9297 SYMBOL_TYPE (sym) = type;
9298
9299 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9300 }
9301 }
9302
9303 /* Allocate a fully-qualified name consisting of the two parts on the
9304 obstack. */
9305
9306 static const char *
9307 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9308 {
9309 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9310 }
9311
9312 /* A helper that allocates a variant part to attach to a Rust enum
9313 type. OBSTACK is where the results should be allocated. TYPE is
9314 the type we're processing. DISCRIMINANT_INDEX is the index of the
9315 discriminant. It must be the index of one of the fields of TYPE.
9316 DEFAULT_INDEX is the index of the default field; or -1 if there is
9317 no default. RANGES is indexed by "effective" field number (the
9318 field index, but omitting the discriminant and default fields) and
9319 must hold the discriminant values used by the variants. Note that
9320 RANGES must have a lifetime at least as long as OBSTACK -- either
9321 already allocated on it, or static. */
9322
9323 static void
9324 alloc_rust_variant (struct obstack *obstack, struct type *type,
9325 int discriminant_index, int default_index,
9326 gdb::array_view<discriminant_range> ranges)
9327 {
9328 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9329 must be handled by the caller. */
9330 gdb_assert (discriminant_index >= 0
9331 && discriminant_index < type->num_fields ());
9332 gdb_assert (default_index == -1
9333 || (default_index >= 0 && default_index < type->num_fields ()));
9334
9335 /* We have one variant for each non-discriminant field. */
9336 int n_variants = type->num_fields () - 1;
9337
9338 variant *variants = new (obstack) variant[n_variants];
9339 int var_idx = 0;
9340 int range_idx = 0;
9341 for (int i = 0; i < type->num_fields (); ++i)
9342 {
9343 if (i == discriminant_index)
9344 continue;
9345
9346 variants[var_idx].first_field = i;
9347 variants[var_idx].last_field = i + 1;
9348
9349 /* The default field does not need a range, but other fields do.
9350 We skipped the discriminant above. */
9351 if (i != default_index)
9352 {
9353 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9354 ++range_idx;
9355 }
9356
9357 ++var_idx;
9358 }
9359
9360 gdb_assert (range_idx == ranges.size ());
9361 gdb_assert (var_idx == n_variants);
9362
9363 variant_part *part = new (obstack) variant_part;
9364 part->discriminant_index = discriminant_index;
9365 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9366 discriminant_index));
9367 part->variants = gdb::array_view<variant> (variants, n_variants);
9368
9369 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9370 gdb::array_view<variant_part> *prop_value
9371 = new (storage) gdb::array_view<variant_part> (part, 1);
9372
9373 struct dynamic_prop prop;
9374 prop.kind = PROP_VARIANT_PARTS;
9375 prop.data.variant_parts = prop_value;
9376
9377 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9378 }
9379
9380 /* Some versions of rustc emitted enums in an unusual way.
9381
9382 Ordinary enums were emitted as unions. The first element of each
9383 structure in the union was named "RUST$ENUM$DISR". This element
9384 held the discriminant.
9385
9386 These versions of Rust also implemented the "non-zero"
9387 optimization. When the enum had two values, and one is empty and
9388 the other holds a pointer that cannot be zero, the pointer is used
9389 as the discriminant, with a zero value meaning the empty variant.
9390 Here, the union's first member is of the form
9391 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9392 where the fieldnos are the indices of the fields that should be
9393 traversed in order to find the field (which may be several fields deep)
9394 and the variantname is the name of the variant of the case when the
9395 field is zero.
9396
9397 This function recognizes whether TYPE is of one of these forms,
9398 and, if so, smashes it to be a variant type. */
9399
9400 static void
9401 quirk_rust_enum (struct type *type, struct objfile *objfile)
9402 {
9403 gdb_assert (type->code () == TYPE_CODE_UNION);
9404
9405 /* We don't need to deal with empty enums. */
9406 if (type->num_fields () == 0)
9407 return;
9408
9409 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9410 if (type->num_fields () == 1
9411 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9412 {
9413 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9414
9415 /* Decode the field name to find the offset of the
9416 discriminant. */
9417 ULONGEST bit_offset = 0;
9418 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9419 while (name[0] >= '0' && name[0] <= '9')
9420 {
9421 char *tail;
9422 unsigned long index = strtoul (name, &tail, 10);
9423 name = tail;
9424 if (*name != '$'
9425 || index >= field_type->num_fields ()
9426 || (TYPE_FIELD_LOC_KIND (field_type, index)
9427 != FIELD_LOC_KIND_BITPOS))
9428 {
9429 complaint (_("Could not parse Rust enum encoding string \"%s\""
9430 "[in module %s]"),
9431 TYPE_FIELD_NAME (type, 0),
9432 objfile_name (objfile));
9433 return;
9434 }
9435 ++name;
9436
9437 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9438 field_type = TYPE_FIELD_TYPE (field_type, index);
9439 }
9440
9441 /* Smash this type to be a structure type. We have to do this
9442 because the type has already been recorded. */
9443 type->set_code (TYPE_CODE_STRUCT);
9444 type->set_num_fields (3);
9445 /* Save the field we care about. */
9446 struct field saved_field = type->field (0);
9447 type->set_fields
9448 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9449
9450 /* Put the discriminant at index 0. */
9451 TYPE_FIELD_TYPE (type, 0) = field_type;
9452 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9453 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9454 SET_FIELD_BITPOS (type->field (0), bit_offset);
9455
9456 /* The order of fields doesn't really matter, so put the real
9457 field at index 1 and the data-less field at index 2. */
9458 type->field (1) = saved_field;
9459 TYPE_FIELD_NAME (type, 1)
9460 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9461 TYPE_FIELD_TYPE (type, 1)->set_name
9462 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9463 TYPE_FIELD_NAME (type, 1)));
9464
9465 const char *dataless_name
9466 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9467 name);
9468 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9469 dataless_name);
9470 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9471 /* NAME points into the original discriminant name, which
9472 already has the correct lifetime. */
9473 TYPE_FIELD_NAME (type, 2) = name;
9474 SET_FIELD_BITPOS (type->field (2), 0);
9475
9476 /* Indicate that this is a variant type. */
9477 static discriminant_range ranges[1] = { { 0, 0 } };
9478 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9479 }
9480 /* A union with a single anonymous field is probably an old-style
9481 univariant enum. */
9482 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9483 {
9484 /* Smash this type to be a structure type. We have to do this
9485 because the type has already been recorded. */
9486 type->set_code (TYPE_CODE_STRUCT);
9487
9488 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9489 const char *variant_name
9490 = rust_last_path_segment (field_type->name ());
9491 TYPE_FIELD_NAME (type, 0) = variant_name;
9492 field_type->set_name
9493 (rust_fully_qualify (&objfile->objfile_obstack,
9494 type->name (), variant_name));
9495 }
9496 else
9497 {
9498 struct type *disr_type = nullptr;
9499 for (int i = 0; i < type->num_fields (); ++i)
9500 {
9501 disr_type = TYPE_FIELD_TYPE (type, i);
9502
9503 if (disr_type->code () != TYPE_CODE_STRUCT)
9504 {
9505 /* All fields of a true enum will be structs. */
9506 return;
9507 }
9508 else if (disr_type->num_fields () == 0)
9509 {
9510 /* Could be data-less variant, so keep going. */
9511 disr_type = nullptr;
9512 }
9513 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9514 "RUST$ENUM$DISR") != 0)
9515 {
9516 /* Not a Rust enum. */
9517 return;
9518 }
9519 else
9520 {
9521 /* Found one. */
9522 break;
9523 }
9524 }
9525
9526 /* If we got here without a discriminant, then it's probably
9527 just a union. */
9528 if (disr_type == nullptr)
9529 return;
9530
9531 /* Smash this type to be a structure type. We have to do this
9532 because the type has already been recorded. */
9533 type->set_code (TYPE_CODE_STRUCT);
9534
9535 /* Make space for the discriminant field. */
9536 struct field *disr_field = &disr_type->field (0);
9537 field *new_fields
9538 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9539 * sizeof (struct field)));
9540 memcpy (new_fields + 1, type->fields (),
9541 type->num_fields () * sizeof (struct field));
9542 type->set_fields (new_fields);
9543 type->set_num_fields (type->num_fields () + 1);
9544
9545 /* Install the discriminant at index 0 in the union. */
9546 type->field (0) = *disr_field;
9547 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9548 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9549
9550 /* We need a way to find the correct discriminant given a
9551 variant name. For convenience we build a map here. */
9552 struct type *enum_type = FIELD_TYPE (*disr_field);
9553 std::unordered_map<std::string, ULONGEST> discriminant_map;
9554 for (int i = 0; i < enum_type->num_fields (); ++i)
9555 {
9556 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9557 {
9558 const char *name
9559 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9560 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9561 }
9562 }
9563
9564 int n_fields = type->num_fields ();
9565 /* We don't need a range entry for the discriminant, but we do
9566 need one for every other field, as there is no default
9567 variant. */
9568 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9569 discriminant_range,
9570 n_fields - 1);
9571 /* Skip the discriminant here. */
9572 for (int i = 1; i < n_fields; ++i)
9573 {
9574 /* Find the final word in the name of this variant's type.
9575 That name can be used to look up the correct
9576 discriminant. */
9577 const char *variant_name
9578 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9579
9580 auto iter = discriminant_map.find (variant_name);
9581 if (iter != discriminant_map.end ())
9582 {
9583 ranges[i].low = iter->second;
9584 ranges[i].high = iter->second;
9585 }
9586
9587 /* Remove the discriminant field, if it exists. */
9588 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9589 if (sub_type->num_fields () > 0)
9590 {
9591 sub_type->set_num_fields (sub_type->num_fields () - 1);
9592 sub_type->set_fields (sub_type->fields () + 1);
9593 }
9594 TYPE_FIELD_NAME (type, i) = variant_name;
9595 sub_type->set_name
9596 (rust_fully_qualify (&objfile->objfile_obstack,
9597 type->name (), variant_name));
9598 }
9599
9600 /* Indicate that this is a variant type. */
9601 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9602 gdb::array_view<discriminant_range> (ranges,
9603 n_fields - 1));
9604 }
9605 }
9606
9607 /* Rewrite some Rust unions to be structures with variants parts. */
9608
9609 static void
9610 rust_union_quirks (struct dwarf2_cu *cu)
9611 {
9612 gdb_assert (cu->language == language_rust);
9613 for (type *type_ : cu->rust_unions)
9614 quirk_rust_enum (type_, cu->per_objfile->objfile);
9615 /* We don't need this any more. */
9616 cu->rust_unions.clear ();
9617 }
9618
9619 /* See read.h. */
9620
9621 type_unit_group_unshareable *
9622 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9623 {
9624 auto iter = this->m_type_units.find (tu_group);
9625 if (iter != this->m_type_units.end ())
9626 return iter->second.get ();
9627
9628 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9629 type_unit_group_unshareable *result = uniq.get ();
9630 this->m_type_units[tu_group] = std::move (uniq);
9631 return result;
9632 }
9633
9634 struct type *
9635 dwarf2_per_objfile::get_type_for_signatured_type
9636 (signatured_type *sig_type) const
9637 {
9638 auto iter = this->m_type_map.find (sig_type);
9639 if (iter == this->m_type_map.end ())
9640 return nullptr;
9641
9642 return iter->second;
9643 }
9644
9645 void dwarf2_per_objfile::set_type_for_signatured_type
9646 (signatured_type *sig_type, struct type *type)
9647 {
9648 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9649
9650 this->m_type_map[sig_type] = type;
9651 }
9652
9653 /* A helper function for computing the list of all symbol tables
9654 included by PER_CU. */
9655
9656 static void
9657 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9658 htab_t all_children, htab_t all_type_symtabs,
9659 dwarf2_per_cu_data *per_cu,
9660 dwarf2_per_objfile *per_objfile,
9661 struct compunit_symtab *immediate_parent)
9662 {
9663 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9664 if (*slot != NULL)
9665 {
9666 /* This inclusion and its children have been processed. */
9667 return;
9668 }
9669
9670 *slot = per_cu;
9671
9672 /* Only add a CU if it has a symbol table. */
9673 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9674 if (cust != NULL)
9675 {
9676 /* If this is a type unit only add its symbol table if we haven't
9677 seen it yet (type unit per_cu's can share symtabs). */
9678 if (per_cu->is_debug_types)
9679 {
9680 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9681 if (*slot == NULL)
9682 {
9683 *slot = cust;
9684 result->push_back (cust);
9685 if (cust->user == NULL)
9686 cust->user = immediate_parent;
9687 }
9688 }
9689 else
9690 {
9691 result->push_back (cust);
9692 if (cust->user == NULL)
9693 cust->user = immediate_parent;
9694 }
9695 }
9696
9697 if (!per_cu->imported_symtabs_empty ())
9698 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9699 {
9700 recursively_compute_inclusions (result, all_children,
9701 all_type_symtabs, ptr, per_objfile,
9702 cust);
9703 }
9704 }
9705
9706 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9707 PER_CU. */
9708
9709 static void
9710 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9711 dwarf2_per_objfile *per_objfile)
9712 {
9713 gdb_assert (! per_cu->is_debug_types);
9714
9715 if (!per_cu->imported_symtabs_empty ())
9716 {
9717 int len;
9718 std::vector<compunit_symtab *> result_symtabs;
9719 htab_t all_children, all_type_symtabs;
9720 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9721
9722 /* If we don't have a symtab, we can just skip this case. */
9723 if (cust == NULL)
9724 return;
9725
9726 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9727 NULL, xcalloc, xfree);
9728 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9729 NULL, xcalloc, xfree);
9730
9731 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9732 {
9733 recursively_compute_inclusions (&result_symtabs, all_children,
9734 all_type_symtabs, ptr, per_objfile,
9735 cust);
9736 }
9737
9738 /* Now we have a transitive closure of all the included symtabs. */
9739 len = result_symtabs.size ();
9740 cust->includes
9741 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9742 struct compunit_symtab *, len + 1);
9743 memcpy (cust->includes, result_symtabs.data (),
9744 len * sizeof (compunit_symtab *));
9745 cust->includes[len] = NULL;
9746
9747 htab_delete (all_children);
9748 htab_delete (all_type_symtabs);
9749 }
9750 }
9751
9752 /* Compute the 'includes' field for the symtabs of all the CUs we just
9753 read. */
9754
9755 static void
9756 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9757 {
9758 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9759 {
9760 if (! iter->is_debug_types)
9761 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9762 }
9763
9764 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9765 }
9766
9767 /* Generate full symbol information for CU, whose DIEs have
9768 already been loaded into memory. */
9769
9770 static void
9771 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9772 {
9773 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9774 struct objfile *objfile = dwarf2_per_objfile->objfile;
9775 struct gdbarch *gdbarch = objfile->arch ();
9776 CORE_ADDR lowpc, highpc;
9777 struct compunit_symtab *cust;
9778 CORE_ADDR baseaddr;
9779 struct block *static_block;
9780 CORE_ADDR addr;
9781
9782 baseaddr = objfile->text_section_offset ();
9783
9784 /* Clear the list here in case something was left over. */
9785 cu->method_list.clear ();
9786
9787 cu->language = pretend_language;
9788 cu->language_defn = language_def (cu->language);
9789
9790 /* Do line number decoding in read_file_scope () */
9791 process_die (cu->dies, cu);
9792
9793 /* For now fudge the Go package. */
9794 if (cu->language == language_go)
9795 fixup_go_packaging (cu);
9796
9797 /* Now that we have processed all the DIEs in the CU, all the types
9798 should be complete, and it should now be safe to compute all of the
9799 physnames. */
9800 compute_delayed_physnames (cu);
9801
9802 if (cu->language == language_rust)
9803 rust_union_quirks (cu);
9804
9805 /* Some compilers don't define a DW_AT_high_pc attribute for the
9806 compilation unit. If the DW_AT_high_pc is missing, synthesize
9807 it, by scanning the DIE's below the compilation unit. */
9808 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9809
9810 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9811 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9812
9813 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9814 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9815 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9816 addrmap to help ensure it has an accurate map of pc values belonging to
9817 this comp unit. */
9818 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9819
9820 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9821 SECT_OFF_TEXT (objfile),
9822 0);
9823
9824 if (cust != NULL)
9825 {
9826 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9827
9828 /* Set symtab language to language from DW_AT_language. If the
9829 compilation is from a C file generated by language preprocessors, do
9830 not set the language if it was already deduced by start_subfile. */
9831 if (!(cu->language == language_c
9832 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9833 COMPUNIT_FILETABS (cust)->language = cu->language;
9834
9835 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9836 produce DW_AT_location with location lists but it can be possibly
9837 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9838 there were bugs in prologue debug info, fixed later in GCC-4.5
9839 by "unwind info for epilogues" patch (which is not directly related).
9840
9841 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9842 needed, it would be wrong due to missing DW_AT_producer there.
9843
9844 Still one can confuse GDB by using non-standard GCC compilation
9845 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9846 */
9847 if (cu->has_loclist && gcc_4_minor >= 5)
9848 cust->locations_valid = 1;
9849
9850 if (gcc_4_minor >= 5)
9851 cust->epilogue_unwind_valid = 1;
9852
9853 cust->call_site_htab = cu->call_site_htab;
9854 }
9855
9856 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
9857
9858 /* Push it for inclusion processing later. */
9859 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9860
9861 /* Not needed any more. */
9862 cu->reset_builder ();
9863 }
9864
9865 /* Generate full symbol information for type unit CU, whose DIEs have
9866 already been loaded into memory. */
9867
9868 static void
9869 process_full_type_unit (dwarf2_cu *cu,
9870 enum language pretend_language)
9871 {
9872 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9873 struct objfile *objfile = dwarf2_per_objfile->objfile;
9874 struct compunit_symtab *cust;
9875 struct signatured_type *sig_type;
9876
9877 gdb_assert (cu->per_cu->is_debug_types);
9878 sig_type = (struct signatured_type *) cu->per_cu;
9879
9880 /* Clear the list here in case something was left over. */
9881 cu->method_list.clear ();
9882
9883 cu->language = pretend_language;
9884 cu->language_defn = language_def (cu->language);
9885
9886 /* The symbol tables are set up in read_type_unit_scope. */
9887 process_die (cu->dies, cu);
9888
9889 /* For now fudge the Go package. */
9890 if (cu->language == language_go)
9891 fixup_go_packaging (cu);
9892
9893 /* Now that we have processed all the DIEs in the CU, all the types
9894 should be complete, and it should now be safe to compute all of the
9895 physnames. */
9896 compute_delayed_physnames (cu);
9897
9898 if (cu->language == language_rust)
9899 rust_union_quirks (cu);
9900
9901 /* TUs share symbol tables.
9902 If this is the first TU to use this symtab, complete the construction
9903 of it with end_expandable_symtab. Otherwise, complete the addition of
9904 this TU's symbols to the existing symtab. */
9905 type_unit_group_unshareable *tug_unshare =
9906 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9907 if (tug_unshare->compunit_symtab == NULL)
9908 {
9909 buildsym_compunit *builder = cu->get_builder ();
9910 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9911 tug_unshare->compunit_symtab = cust;
9912
9913 if (cust != NULL)
9914 {
9915 /* Set symtab language to language from DW_AT_language. If the
9916 compilation is from a C file generated by language preprocessors,
9917 do not set the language if it was already deduced by
9918 start_subfile. */
9919 if (!(cu->language == language_c
9920 && COMPUNIT_FILETABS (cust)->language != language_c))
9921 COMPUNIT_FILETABS (cust)->language = cu->language;
9922 }
9923 }
9924 else
9925 {
9926 cu->get_builder ()->augment_type_symtab ();
9927 cust = tug_unshare->compunit_symtab;
9928 }
9929
9930 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
9931
9932 /* Not needed any more. */
9933 cu->reset_builder ();
9934 }
9935
9936 /* Process an imported unit DIE. */
9937
9938 static void
9939 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9940 {
9941 struct attribute *attr;
9942
9943 /* For now we don't handle imported units in type units. */
9944 if (cu->per_cu->is_debug_types)
9945 {
9946 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9947 " supported in type units [in module %s]"),
9948 objfile_name (cu->per_objfile->objfile));
9949 }
9950
9951 attr = dwarf2_attr (die, DW_AT_import, cu);
9952 if (attr != NULL)
9953 {
9954 sect_offset sect_off = attr->get_ref_die_offset ();
9955 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9956 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9957 dwarf2_per_cu_data *per_cu
9958 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9959
9960 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9961 into another compilation unit, at root level. Regard this as a hint,
9962 and ignore it. */
9963 if (die->parent && die->parent->parent == NULL
9964 && per_cu->unit_type == DW_UT_compile
9965 && per_cu->lang == language_cplus)
9966 return;
9967
9968 /* If necessary, add it to the queue and load its DIEs. */
9969 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9970 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9971
9972 cu->per_cu->imported_symtabs_push (per_cu);
9973 }
9974 }
9975
9976 /* RAII object that represents a process_die scope: i.e.,
9977 starts/finishes processing a DIE. */
9978 class process_die_scope
9979 {
9980 public:
9981 process_die_scope (die_info *die, dwarf2_cu *cu)
9982 : m_die (die), m_cu (cu)
9983 {
9984 /* We should only be processing DIEs not already in process. */
9985 gdb_assert (!m_die->in_process);
9986 m_die->in_process = true;
9987 }
9988
9989 ~process_die_scope ()
9990 {
9991 m_die->in_process = false;
9992
9993 /* If we're done processing the DIE for the CU that owns the line
9994 header, we don't need the line header anymore. */
9995 if (m_cu->line_header_die_owner == m_die)
9996 {
9997 delete m_cu->line_header;
9998 m_cu->line_header = NULL;
9999 m_cu->line_header_die_owner = NULL;
10000 }
10001 }
10002
10003 private:
10004 die_info *m_die;
10005 dwarf2_cu *m_cu;
10006 };
10007
10008 /* Process a die and its children. */
10009
10010 static void
10011 process_die (struct die_info *die, struct dwarf2_cu *cu)
10012 {
10013 process_die_scope scope (die, cu);
10014
10015 switch (die->tag)
10016 {
10017 case DW_TAG_padding:
10018 break;
10019 case DW_TAG_compile_unit:
10020 case DW_TAG_partial_unit:
10021 read_file_scope (die, cu);
10022 break;
10023 case DW_TAG_type_unit:
10024 read_type_unit_scope (die, cu);
10025 break;
10026 case DW_TAG_subprogram:
10027 /* Nested subprograms in Fortran get a prefix. */
10028 if (cu->language == language_fortran
10029 && die->parent != NULL
10030 && die->parent->tag == DW_TAG_subprogram)
10031 cu->processing_has_namespace_info = true;
10032 /* Fall through. */
10033 case DW_TAG_inlined_subroutine:
10034 read_func_scope (die, cu);
10035 break;
10036 case DW_TAG_lexical_block:
10037 case DW_TAG_try_block:
10038 case DW_TAG_catch_block:
10039 read_lexical_block_scope (die, cu);
10040 break;
10041 case DW_TAG_call_site:
10042 case DW_TAG_GNU_call_site:
10043 read_call_site_scope (die, cu);
10044 break;
10045 case DW_TAG_class_type:
10046 case DW_TAG_interface_type:
10047 case DW_TAG_structure_type:
10048 case DW_TAG_union_type:
10049 process_structure_scope (die, cu);
10050 break;
10051 case DW_TAG_enumeration_type:
10052 process_enumeration_scope (die, cu);
10053 break;
10054
10055 /* These dies have a type, but processing them does not create
10056 a symbol or recurse to process the children. Therefore we can
10057 read them on-demand through read_type_die. */
10058 case DW_TAG_subroutine_type:
10059 case DW_TAG_set_type:
10060 case DW_TAG_array_type:
10061 case DW_TAG_pointer_type:
10062 case DW_TAG_ptr_to_member_type:
10063 case DW_TAG_reference_type:
10064 case DW_TAG_rvalue_reference_type:
10065 case DW_TAG_string_type:
10066 break;
10067
10068 case DW_TAG_base_type:
10069 case DW_TAG_subrange_type:
10070 case DW_TAG_typedef:
10071 /* Add a typedef symbol for the type definition, if it has a
10072 DW_AT_name. */
10073 new_symbol (die, read_type_die (die, cu), cu);
10074 break;
10075 case DW_TAG_common_block:
10076 read_common_block (die, cu);
10077 break;
10078 case DW_TAG_common_inclusion:
10079 break;
10080 case DW_TAG_namespace:
10081 cu->processing_has_namespace_info = true;
10082 read_namespace (die, cu);
10083 break;
10084 case DW_TAG_module:
10085 cu->processing_has_namespace_info = true;
10086 read_module (die, cu);
10087 break;
10088 case DW_TAG_imported_declaration:
10089 cu->processing_has_namespace_info = true;
10090 if (read_namespace_alias (die, cu))
10091 break;
10092 /* The declaration is not a global namespace alias. */
10093 /* Fall through. */
10094 case DW_TAG_imported_module:
10095 cu->processing_has_namespace_info = true;
10096 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10097 || cu->language != language_fortran))
10098 complaint (_("Tag '%s' has unexpected children"),
10099 dwarf_tag_name (die->tag));
10100 read_import_statement (die, cu);
10101 break;
10102
10103 case DW_TAG_imported_unit:
10104 process_imported_unit_die (die, cu);
10105 break;
10106
10107 case DW_TAG_variable:
10108 read_variable (die, cu);
10109 break;
10110
10111 default:
10112 new_symbol (die, NULL, cu);
10113 break;
10114 }
10115 }
10116 \f
10117 /* DWARF name computation. */
10118
10119 /* A helper function for dwarf2_compute_name which determines whether DIE
10120 needs to have the name of the scope prepended to the name listed in the
10121 die. */
10122
10123 static int
10124 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10125 {
10126 struct attribute *attr;
10127
10128 switch (die->tag)
10129 {
10130 case DW_TAG_namespace:
10131 case DW_TAG_typedef:
10132 case DW_TAG_class_type:
10133 case DW_TAG_interface_type:
10134 case DW_TAG_structure_type:
10135 case DW_TAG_union_type:
10136 case DW_TAG_enumeration_type:
10137 case DW_TAG_enumerator:
10138 case DW_TAG_subprogram:
10139 case DW_TAG_inlined_subroutine:
10140 case DW_TAG_member:
10141 case DW_TAG_imported_declaration:
10142 return 1;
10143
10144 case DW_TAG_variable:
10145 case DW_TAG_constant:
10146 /* We only need to prefix "globally" visible variables. These include
10147 any variable marked with DW_AT_external or any variable that
10148 lives in a namespace. [Variables in anonymous namespaces
10149 require prefixing, but they are not DW_AT_external.] */
10150
10151 if (dwarf2_attr (die, DW_AT_specification, cu))
10152 {
10153 struct dwarf2_cu *spec_cu = cu;
10154
10155 return die_needs_namespace (die_specification (die, &spec_cu),
10156 spec_cu);
10157 }
10158
10159 attr = dwarf2_attr (die, DW_AT_external, cu);
10160 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10161 && die->parent->tag != DW_TAG_module)
10162 return 0;
10163 /* A variable in a lexical block of some kind does not need a
10164 namespace, even though in C++ such variables may be external
10165 and have a mangled name. */
10166 if (die->parent->tag == DW_TAG_lexical_block
10167 || die->parent->tag == DW_TAG_try_block
10168 || die->parent->tag == DW_TAG_catch_block
10169 || die->parent->tag == DW_TAG_subprogram)
10170 return 0;
10171 return 1;
10172
10173 default:
10174 return 0;
10175 }
10176 }
10177
10178 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10179 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10180 defined for the given DIE. */
10181
10182 static struct attribute *
10183 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10184 {
10185 struct attribute *attr;
10186
10187 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10188 if (attr == NULL)
10189 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10190
10191 return attr;
10192 }
10193
10194 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10195 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10196 defined for the given DIE. */
10197
10198 static const char *
10199 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10200 {
10201 const char *linkage_name;
10202
10203 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10204 if (linkage_name == NULL)
10205 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10206
10207 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10208 See https://github.com/rust-lang/rust/issues/32925. */
10209 if (cu->language == language_rust && linkage_name != NULL
10210 && strchr (linkage_name, '{') != NULL)
10211 linkage_name = NULL;
10212
10213 return linkage_name;
10214 }
10215
10216 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10217 compute the physname for the object, which include a method's:
10218 - formal parameters (C++),
10219 - receiver type (Go),
10220
10221 The term "physname" is a bit confusing.
10222 For C++, for example, it is the demangled name.
10223 For Go, for example, it's the mangled name.
10224
10225 For Ada, return the DIE's linkage name rather than the fully qualified
10226 name. PHYSNAME is ignored..
10227
10228 The result is allocated on the objfile->per_bfd's obstack and
10229 canonicalized. */
10230
10231 static const char *
10232 dwarf2_compute_name (const char *name,
10233 struct die_info *die, struct dwarf2_cu *cu,
10234 int physname)
10235 {
10236 struct objfile *objfile = cu->per_objfile->objfile;
10237
10238 if (name == NULL)
10239 name = dwarf2_name (die, cu);
10240
10241 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10242 but otherwise compute it by typename_concat inside GDB.
10243 FIXME: Actually this is not really true, or at least not always true.
10244 It's all very confusing. compute_and_set_names doesn't try to demangle
10245 Fortran names because there is no mangling standard. So new_symbol
10246 will set the demangled name to the result of dwarf2_full_name, and it is
10247 the demangled name that GDB uses if it exists. */
10248 if (cu->language == language_ada
10249 || (cu->language == language_fortran && physname))
10250 {
10251 /* For Ada unit, we prefer the linkage name over the name, as
10252 the former contains the exported name, which the user expects
10253 to be able to reference. Ideally, we want the user to be able
10254 to reference this entity using either natural or linkage name,
10255 but we haven't started looking at this enhancement yet. */
10256 const char *linkage_name = dw2_linkage_name (die, cu);
10257
10258 if (linkage_name != NULL)
10259 return linkage_name;
10260 }
10261
10262 /* These are the only languages we know how to qualify names in. */
10263 if (name != NULL
10264 && (cu->language == language_cplus
10265 || cu->language == language_fortran || cu->language == language_d
10266 || cu->language == language_rust))
10267 {
10268 if (die_needs_namespace (die, cu))
10269 {
10270 const char *prefix;
10271 const char *canonical_name = NULL;
10272
10273 string_file buf;
10274
10275 prefix = determine_prefix (die, cu);
10276 if (*prefix != '\0')
10277 {
10278 gdb::unique_xmalloc_ptr<char> prefixed_name
10279 (typename_concat (NULL, prefix, name, physname, cu));
10280
10281 buf.puts (prefixed_name.get ());
10282 }
10283 else
10284 buf.puts (name);
10285
10286 /* Template parameters may be specified in the DIE's DW_AT_name, or
10287 as children with DW_TAG_template_type_param or
10288 DW_TAG_value_type_param. If the latter, add them to the name
10289 here. If the name already has template parameters, then
10290 skip this step; some versions of GCC emit both, and
10291 it is more efficient to use the pre-computed name.
10292
10293 Something to keep in mind about this process: it is very
10294 unlikely, or in some cases downright impossible, to produce
10295 something that will match the mangled name of a function.
10296 If the definition of the function has the same debug info,
10297 we should be able to match up with it anyway. But fallbacks
10298 using the minimal symbol, for instance to find a method
10299 implemented in a stripped copy of libstdc++, will not work.
10300 If we do not have debug info for the definition, we will have to
10301 match them up some other way.
10302
10303 When we do name matching there is a related problem with function
10304 templates; two instantiated function templates are allowed to
10305 differ only by their return types, which we do not add here. */
10306
10307 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10308 {
10309 struct attribute *attr;
10310 struct die_info *child;
10311 int first = 1;
10312
10313 die->building_fullname = 1;
10314
10315 for (child = die->child; child != NULL; child = child->sibling)
10316 {
10317 struct type *type;
10318 LONGEST value;
10319 const gdb_byte *bytes;
10320 struct dwarf2_locexpr_baton *baton;
10321 struct value *v;
10322
10323 if (child->tag != DW_TAG_template_type_param
10324 && child->tag != DW_TAG_template_value_param)
10325 continue;
10326
10327 if (first)
10328 {
10329 buf.puts ("<");
10330 first = 0;
10331 }
10332 else
10333 buf.puts (", ");
10334
10335 attr = dwarf2_attr (child, DW_AT_type, cu);
10336 if (attr == NULL)
10337 {
10338 complaint (_("template parameter missing DW_AT_type"));
10339 buf.puts ("UNKNOWN_TYPE");
10340 continue;
10341 }
10342 type = die_type (child, cu);
10343
10344 if (child->tag == DW_TAG_template_type_param)
10345 {
10346 c_print_type (type, "", &buf, -1, 0, cu->language,
10347 &type_print_raw_options);
10348 continue;
10349 }
10350
10351 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10352 if (attr == NULL)
10353 {
10354 complaint (_("template parameter missing "
10355 "DW_AT_const_value"));
10356 buf.puts ("UNKNOWN_VALUE");
10357 continue;
10358 }
10359
10360 dwarf2_const_value_attr (attr, type, name,
10361 &cu->comp_unit_obstack, cu,
10362 &value, &bytes, &baton);
10363
10364 if (TYPE_NOSIGN (type))
10365 /* GDB prints characters as NUMBER 'CHAR'. If that's
10366 changed, this can use value_print instead. */
10367 c_printchar (value, type, &buf);
10368 else
10369 {
10370 struct value_print_options opts;
10371
10372 if (baton != NULL)
10373 v = dwarf2_evaluate_loc_desc (type, NULL,
10374 baton->data,
10375 baton->size,
10376 baton->per_cu,
10377 baton->per_objfile);
10378 else if (bytes != NULL)
10379 {
10380 v = allocate_value (type);
10381 memcpy (value_contents_writeable (v), bytes,
10382 TYPE_LENGTH (type));
10383 }
10384 else
10385 v = value_from_longest (type, value);
10386
10387 /* Specify decimal so that we do not depend on
10388 the radix. */
10389 get_formatted_print_options (&opts, 'd');
10390 opts.raw = 1;
10391 value_print (v, &buf, &opts);
10392 release_value (v);
10393 }
10394 }
10395
10396 die->building_fullname = 0;
10397
10398 if (!first)
10399 {
10400 /* Close the argument list, with a space if necessary
10401 (nested templates). */
10402 if (!buf.empty () && buf.string ().back () == '>')
10403 buf.puts (" >");
10404 else
10405 buf.puts (">");
10406 }
10407 }
10408
10409 /* For C++ methods, append formal parameter type
10410 information, if PHYSNAME. */
10411
10412 if (physname && die->tag == DW_TAG_subprogram
10413 && cu->language == language_cplus)
10414 {
10415 struct type *type = read_type_die (die, cu);
10416
10417 c_type_print_args (type, &buf, 1, cu->language,
10418 &type_print_raw_options);
10419
10420 if (cu->language == language_cplus)
10421 {
10422 /* Assume that an artificial first parameter is
10423 "this", but do not crash if it is not. RealView
10424 marks unnamed (and thus unused) parameters as
10425 artificial; there is no way to differentiate
10426 the two cases. */
10427 if (type->num_fields () > 0
10428 && TYPE_FIELD_ARTIFICIAL (type, 0)
10429 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10430 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10431 0))))
10432 buf.puts (" const");
10433 }
10434 }
10435
10436 const std::string &intermediate_name = buf.string ();
10437
10438 if (cu->language == language_cplus)
10439 canonical_name
10440 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10441 objfile);
10442
10443 /* If we only computed INTERMEDIATE_NAME, or if
10444 INTERMEDIATE_NAME is already canonical, then we need to
10445 intern it. */
10446 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10447 name = objfile->intern (intermediate_name);
10448 else
10449 name = canonical_name;
10450 }
10451 }
10452
10453 return name;
10454 }
10455
10456 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10457 If scope qualifiers are appropriate they will be added. The result
10458 will be allocated on the storage_obstack, or NULL if the DIE does
10459 not have a name. NAME may either be from a previous call to
10460 dwarf2_name or NULL.
10461
10462 The output string will be canonicalized (if C++). */
10463
10464 static const char *
10465 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10466 {
10467 return dwarf2_compute_name (name, die, cu, 0);
10468 }
10469
10470 /* Construct a physname for the given DIE in CU. NAME may either be
10471 from a previous call to dwarf2_name or NULL. The result will be
10472 allocated on the objfile_objstack or NULL if the DIE does not have a
10473 name.
10474
10475 The output string will be canonicalized (if C++). */
10476
10477 static const char *
10478 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10479 {
10480 struct objfile *objfile = cu->per_objfile->objfile;
10481 const char *retval, *mangled = NULL, *canon = NULL;
10482 int need_copy = 1;
10483
10484 /* In this case dwarf2_compute_name is just a shortcut not building anything
10485 on its own. */
10486 if (!die_needs_namespace (die, cu))
10487 return dwarf2_compute_name (name, die, cu, 1);
10488
10489 if (cu->language != language_rust)
10490 mangled = dw2_linkage_name (die, cu);
10491
10492 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10493 has computed. */
10494 gdb::unique_xmalloc_ptr<char> demangled;
10495 if (mangled != NULL)
10496 {
10497
10498 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10499 {
10500 /* Do nothing (do not demangle the symbol name). */
10501 }
10502 else if (cu->language == language_go)
10503 {
10504 /* This is a lie, but we already lie to the caller new_symbol.
10505 new_symbol assumes we return the mangled name.
10506 This just undoes that lie until things are cleaned up. */
10507 }
10508 else
10509 {
10510 /* Use DMGL_RET_DROP for C++ template functions to suppress
10511 their return type. It is easier for GDB users to search
10512 for such functions as `name(params)' than `long name(params)'.
10513 In such case the minimal symbol names do not match the full
10514 symbol names but for template functions there is never a need
10515 to look up their definition from their declaration so
10516 the only disadvantage remains the minimal symbol variant
10517 `long name(params)' does not have the proper inferior type. */
10518 demangled.reset (gdb_demangle (mangled,
10519 (DMGL_PARAMS | DMGL_ANSI
10520 | DMGL_RET_DROP)));
10521 }
10522 if (demangled)
10523 canon = demangled.get ();
10524 else
10525 {
10526 canon = mangled;
10527 need_copy = 0;
10528 }
10529 }
10530
10531 if (canon == NULL || check_physname)
10532 {
10533 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10534
10535 if (canon != NULL && strcmp (physname, canon) != 0)
10536 {
10537 /* It may not mean a bug in GDB. The compiler could also
10538 compute DW_AT_linkage_name incorrectly. But in such case
10539 GDB would need to be bug-to-bug compatible. */
10540
10541 complaint (_("Computed physname <%s> does not match demangled <%s> "
10542 "(from linkage <%s>) - DIE at %s [in module %s]"),
10543 physname, canon, mangled, sect_offset_str (die->sect_off),
10544 objfile_name (objfile));
10545
10546 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10547 is available here - over computed PHYSNAME. It is safer
10548 against both buggy GDB and buggy compilers. */
10549
10550 retval = canon;
10551 }
10552 else
10553 {
10554 retval = physname;
10555 need_copy = 0;
10556 }
10557 }
10558 else
10559 retval = canon;
10560
10561 if (need_copy)
10562 retval = objfile->intern (retval);
10563
10564 return retval;
10565 }
10566
10567 /* Inspect DIE in CU for a namespace alias. If one exists, record
10568 a new symbol for it.
10569
10570 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10571
10572 static int
10573 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10574 {
10575 struct attribute *attr;
10576
10577 /* If the die does not have a name, this is not a namespace
10578 alias. */
10579 attr = dwarf2_attr (die, DW_AT_name, cu);
10580 if (attr != NULL)
10581 {
10582 int num;
10583 struct die_info *d = die;
10584 struct dwarf2_cu *imported_cu = cu;
10585
10586 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10587 keep inspecting DIEs until we hit the underlying import. */
10588 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10589 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10590 {
10591 attr = dwarf2_attr (d, DW_AT_import, cu);
10592 if (attr == NULL)
10593 break;
10594
10595 d = follow_die_ref (d, attr, &imported_cu);
10596 if (d->tag != DW_TAG_imported_declaration)
10597 break;
10598 }
10599
10600 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10601 {
10602 complaint (_("DIE at %s has too many recursively imported "
10603 "declarations"), sect_offset_str (d->sect_off));
10604 return 0;
10605 }
10606
10607 if (attr != NULL)
10608 {
10609 struct type *type;
10610 sect_offset sect_off = attr->get_ref_die_offset ();
10611
10612 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10613 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10614 {
10615 /* This declaration is a global namespace alias. Add
10616 a symbol for it whose type is the aliased namespace. */
10617 new_symbol (die, type, cu);
10618 return 1;
10619 }
10620 }
10621 }
10622
10623 return 0;
10624 }
10625
10626 /* Return the using directives repository (global or local?) to use in the
10627 current context for CU.
10628
10629 For Ada, imported declarations can materialize renamings, which *may* be
10630 global. However it is impossible (for now?) in DWARF to distinguish
10631 "external" imported declarations and "static" ones. As all imported
10632 declarations seem to be static in all other languages, make them all CU-wide
10633 global only in Ada. */
10634
10635 static struct using_direct **
10636 using_directives (struct dwarf2_cu *cu)
10637 {
10638 if (cu->language == language_ada
10639 && cu->get_builder ()->outermost_context_p ())
10640 return cu->get_builder ()->get_global_using_directives ();
10641 else
10642 return cu->get_builder ()->get_local_using_directives ();
10643 }
10644
10645 /* Read the import statement specified by the given die and record it. */
10646
10647 static void
10648 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10649 {
10650 struct objfile *objfile = cu->per_objfile->objfile;
10651 struct attribute *import_attr;
10652 struct die_info *imported_die, *child_die;
10653 struct dwarf2_cu *imported_cu;
10654 const char *imported_name;
10655 const char *imported_name_prefix;
10656 const char *canonical_name;
10657 const char *import_alias;
10658 const char *imported_declaration = NULL;
10659 const char *import_prefix;
10660 std::vector<const char *> excludes;
10661
10662 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10663 if (import_attr == NULL)
10664 {
10665 complaint (_("Tag '%s' has no DW_AT_import"),
10666 dwarf_tag_name (die->tag));
10667 return;
10668 }
10669
10670 imported_cu = cu;
10671 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10672 imported_name = dwarf2_name (imported_die, imported_cu);
10673 if (imported_name == NULL)
10674 {
10675 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10676
10677 The import in the following code:
10678 namespace A
10679 {
10680 typedef int B;
10681 }
10682
10683 int main ()
10684 {
10685 using A::B;
10686 B b;
10687 return b;
10688 }
10689
10690 ...
10691 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10692 <52> DW_AT_decl_file : 1
10693 <53> DW_AT_decl_line : 6
10694 <54> DW_AT_import : <0x75>
10695 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10696 <59> DW_AT_name : B
10697 <5b> DW_AT_decl_file : 1
10698 <5c> DW_AT_decl_line : 2
10699 <5d> DW_AT_type : <0x6e>
10700 ...
10701 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10702 <76> DW_AT_byte_size : 4
10703 <77> DW_AT_encoding : 5 (signed)
10704
10705 imports the wrong die ( 0x75 instead of 0x58 ).
10706 This case will be ignored until the gcc bug is fixed. */
10707 return;
10708 }
10709
10710 /* Figure out the local name after import. */
10711 import_alias = dwarf2_name (die, cu);
10712
10713 /* Figure out where the statement is being imported to. */
10714 import_prefix = determine_prefix (die, cu);
10715
10716 /* Figure out what the scope of the imported die is and prepend it
10717 to the name of the imported die. */
10718 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10719
10720 if (imported_die->tag != DW_TAG_namespace
10721 && imported_die->tag != DW_TAG_module)
10722 {
10723 imported_declaration = imported_name;
10724 canonical_name = imported_name_prefix;
10725 }
10726 else if (strlen (imported_name_prefix) > 0)
10727 canonical_name = obconcat (&objfile->objfile_obstack,
10728 imported_name_prefix,
10729 (cu->language == language_d ? "." : "::"),
10730 imported_name, (char *) NULL);
10731 else
10732 canonical_name = imported_name;
10733
10734 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10735 for (child_die = die->child; child_die && child_die->tag;
10736 child_die = child_die->sibling)
10737 {
10738 /* DWARF-4: A Fortran use statement with a “rename list” may be
10739 represented by an imported module entry with an import attribute
10740 referring to the module and owned entries corresponding to those
10741 entities that are renamed as part of being imported. */
10742
10743 if (child_die->tag != DW_TAG_imported_declaration)
10744 {
10745 complaint (_("child DW_TAG_imported_declaration expected "
10746 "- DIE at %s [in module %s]"),
10747 sect_offset_str (child_die->sect_off),
10748 objfile_name (objfile));
10749 continue;
10750 }
10751
10752 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10753 if (import_attr == NULL)
10754 {
10755 complaint (_("Tag '%s' has no DW_AT_import"),
10756 dwarf_tag_name (child_die->tag));
10757 continue;
10758 }
10759
10760 imported_cu = cu;
10761 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10762 &imported_cu);
10763 imported_name = dwarf2_name (imported_die, imported_cu);
10764 if (imported_name == NULL)
10765 {
10766 complaint (_("child DW_TAG_imported_declaration has unknown "
10767 "imported name - DIE at %s [in module %s]"),
10768 sect_offset_str (child_die->sect_off),
10769 objfile_name (objfile));
10770 continue;
10771 }
10772
10773 excludes.push_back (imported_name);
10774
10775 process_die (child_die, cu);
10776 }
10777
10778 add_using_directive (using_directives (cu),
10779 import_prefix,
10780 canonical_name,
10781 import_alias,
10782 imported_declaration,
10783 excludes,
10784 0,
10785 &objfile->objfile_obstack);
10786 }
10787
10788 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10789 types, but gives them a size of zero. Starting with version 14,
10790 ICC is compatible with GCC. */
10791
10792 static bool
10793 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10794 {
10795 if (!cu->checked_producer)
10796 check_producer (cu);
10797
10798 return cu->producer_is_icc_lt_14;
10799 }
10800
10801 /* ICC generates a DW_AT_type for C void functions. This was observed on
10802 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10803 which says that void functions should not have a DW_AT_type. */
10804
10805 static bool
10806 producer_is_icc (struct dwarf2_cu *cu)
10807 {
10808 if (!cu->checked_producer)
10809 check_producer (cu);
10810
10811 return cu->producer_is_icc;
10812 }
10813
10814 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10815 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10816 this, it was first present in GCC release 4.3.0. */
10817
10818 static bool
10819 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10820 {
10821 if (!cu->checked_producer)
10822 check_producer (cu);
10823
10824 return cu->producer_is_gcc_lt_4_3;
10825 }
10826
10827 static file_and_directory
10828 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10829 {
10830 file_and_directory res;
10831
10832 /* Find the filename. Do not use dwarf2_name here, since the filename
10833 is not a source language identifier. */
10834 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10835 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10836
10837 if (res.comp_dir == NULL
10838 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10839 && IS_ABSOLUTE_PATH (res.name))
10840 {
10841 res.comp_dir_storage = ldirname (res.name);
10842 if (!res.comp_dir_storage.empty ())
10843 res.comp_dir = res.comp_dir_storage.c_str ();
10844 }
10845 if (res.comp_dir != NULL)
10846 {
10847 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10848 directory, get rid of it. */
10849 const char *cp = strchr (res.comp_dir, ':');
10850
10851 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10852 res.comp_dir = cp + 1;
10853 }
10854
10855 if (res.name == NULL)
10856 res.name = "<unknown>";
10857
10858 return res;
10859 }
10860
10861 /* Handle DW_AT_stmt_list for a compilation unit.
10862 DIE is the DW_TAG_compile_unit die for CU.
10863 COMP_DIR is the compilation directory. LOWPC is passed to
10864 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10865
10866 static void
10867 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10868 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10869 {
10870 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10871 struct attribute *attr;
10872 struct line_header line_header_local;
10873 hashval_t line_header_local_hash;
10874 void **slot;
10875 int decode_mapping;
10876
10877 gdb_assert (! cu->per_cu->is_debug_types);
10878
10879 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10880 if (attr == NULL)
10881 return;
10882
10883 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10884
10885 /* The line header hash table is only created if needed (it exists to
10886 prevent redundant reading of the line table for partial_units).
10887 If we're given a partial_unit, we'll need it. If we're given a
10888 compile_unit, then use the line header hash table if it's already
10889 created, but don't create one just yet. */
10890
10891 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10892 && die->tag == DW_TAG_partial_unit)
10893 {
10894 dwarf2_per_objfile->per_bfd->line_header_hash
10895 .reset (htab_create_alloc (127, line_header_hash_voidp,
10896 line_header_eq_voidp,
10897 free_line_header_voidp,
10898 xcalloc, xfree));
10899 }
10900
10901 line_header_local.sect_off = line_offset;
10902 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10903 line_header_local_hash = line_header_hash (&line_header_local);
10904 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10905 {
10906 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10907 &line_header_local,
10908 line_header_local_hash, NO_INSERT);
10909
10910 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10911 is not present in *SLOT (since if there is something in *SLOT then
10912 it will be for a partial_unit). */
10913 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10914 {
10915 gdb_assert (*slot != NULL);
10916 cu->line_header = (struct line_header *) *slot;
10917 return;
10918 }
10919 }
10920
10921 /* dwarf_decode_line_header does not yet provide sufficient information.
10922 We always have to call also dwarf_decode_lines for it. */
10923 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10924 if (lh == NULL)
10925 return;
10926
10927 cu->line_header = lh.release ();
10928 cu->line_header_die_owner = die;
10929
10930 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10931 slot = NULL;
10932 else
10933 {
10934 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10935 &line_header_local,
10936 line_header_local_hash, INSERT);
10937 gdb_assert (slot != NULL);
10938 }
10939 if (slot != NULL && *slot == NULL)
10940 {
10941 /* This newly decoded line number information unit will be owned
10942 by line_header_hash hash table. */
10943 *slot = cu->line_header;
10944 cu->line_header_die_owner = NULL;
10945 }
10946 else
10947 {
10948 /* We cannot free any current entry in (*slot) as that struct line_header
10949 may be already used by multiple CUs. Create only temporary decoded
10950 line_header for this CU - it may happen at most once for each line
10951 number information unit. And if we're not using line_header_hash
10952 then this is what we want as well. */
10953 gdb_assert (die->tag != DW_TAG_partial_unit);
10954 }
10955 decode_mapping = (die->tag != DW_TAG_partial_unit);
10956 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10957 decode_mapping);
10958
10959 }
10960
10961 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10962
10963 static void
10964 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10965 {
10966 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10967 struct objfile *objfile = dwarf2_per_objfile->objfile;
10968 struct gdbarch *gdbarch = objfile->arch ();
10969 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10970 CORE_ADDR highpc = ((CORE_ADDR) 0);
10971 struct attribute *attr;
10972 struct die_info *child_die;
10973 CORE_ADDR baseaddr;
10974
10975 prepare_one_comp_unit (cu, die, cu->language);
10976 baseaddr = objfile->text_section_offset ();
10977
10978 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10979
10980 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10981 from finish_block. */
10982 if (lowpc == ((CORE_ADDR) -1))
10983 lowpc = highpc;
10984 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10985
10986 file_and_directory fnd = find_file_and_directory (die, cu);
10987
10988 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10989 standardised yet. As a workaround for the language detection we fall
10990 back to the DW_AT_producer string. */
10991 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10992 cu->language = language_opencl;
10993
10994 /* Similar hack for Go. */
10995 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10996 set_cu_language (DW_LANG_Go, cu);
10997
10998 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10999
11000 /* Decode line number information if present. We do this before
11001 processing child DIEs, so that the line header table is available
11002 for DW_AT_decl_file. */
11003 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11004
11005 /* Process all dies in compilation unit. */
11006 if (die->child != NULL)
11007 {
11008 child_die = die->child;
11009 while (child_die && child_die->tag)
11010 {
11011 process_die (child_die, cu);
11012 child_die = child_die->sibling;
11013 }
11014 }
11015
11016 /* Decode macro information, if present. Dwarf 2 macro information
11017 refers to information in the line number info statement program
11018 header, so we can only read it if we've read the header
11019 successfully. */
11020 attr = dwarf2_attr (die, DW_AT_macros, cu);
11021 if (attr == NULL)
11022 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11023 if (attr && cu->line_header)
11024 {
11025 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11026 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11027
11028 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11029 }
11030 else
11031 {
11032 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11033 if (attr && cu->line_header)
11034 {
11035 unsigned int macro_offset = DW_UNSND (attr);
11036
11037 dwarf_decode_macros (cu, macro_offset, 0);
11038 }
11039 }
11040 }
11041
11042 void
11043 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11044 {
11045 struct type_unit_group *tu_group;
11046 int first_time;
11047 struct attribute *attr;
11048 unsigned int i;
11049 struct signatured_type *sig_type;
11050
11051 gdb_assert (per_cu->is_debug_types);
11052 sig_type = (struct signatured_type *) per_cu;
11053
11054 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11055
11056 /* If we're using .gdb_index (includes -readnow) then
11057 per_cu->type_unit_group may not have been set up yet. */
11058 if (sig_type->type_unit_group == NULL)
11059 sig_type->type_unit_group = get_type_unit_group (this, attr);
11060 tu_group = sig_type->type_unit_group;
11061
11062 /* If we've already processed this stmt_list there's no real need to
11063 do it again, we could fake it and just recreate the part we need
11064 (file name,index -> symtab mapping). If data shows this optimization
11065 is useful we can do it then. */
11066 type_unit_group_unshareable *tug_unshare
11067 = per_objfile->get_type_unit_group_unshareable (tu_group);
11068 first_time = tug_unshare->compunit_symtab == NULL;
11069
11070 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11071 debug info. */
11072 line_header_up lh;
11073 if (attr != NULL)
11074 {
11075 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11076 lh = dwarf_decode_line_header (line_offset, this);
11077 }
11078 if (lh == NULL)
11079 {
11080 if (first_time)
11081 start_symtab ("", NULL, 0);
11082 else
11083 {
11084 gdb_assert (tug_unshare->symtabs == NULL);
11085 gdb_assert (m_builder == nullptr);
11086 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11087 m_builder.reset (new struct buildsym_compunit
11088 (COMPUNIT_OBJFILE (cust), "",
11089 COMPUNIT_DIRNAME (cust),
11090 compunit_language (cust),
11091 0, cust));
11092 list_in_scope = get_builder ()->get_file_symbols ();
11093 }
11094 return;
11095 }
11096
11097 line_header = lh.release ();
11098 line_header_die_owner = die;
11099
11100 if (first_time)
11101 {
11102 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11103
11104 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11105 still initializing it, and our caller (a few levels up)
11106 process_full_type_unit still needs to know if this is the first
11107 time. */
11108
11109 tug_unshare->symtabs
11110 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11111 struct symtab *, line_header->file_names_size ());
11112
11113 auto &file_names = line_header->file_names ();
11114 for (i = 0; i < file_names.size (); ++i)
11115 {
11116 file_entry &fe = file_names[i];
11117 dwarf2_start_subfile (this, fe.name,
11118 fe.include_dir (line_header));
11119 buildsym_compunit *b = get_builder ();
11120 if (b->get_current_subfile ()->symtab == NULL)
11121 {
11122 /* NOTE: start_subfile will recognize when it's been
11123 passed a file it has already seen. So we can't
11124 assume there's a simple mapping from
11125 cu->line_header->file_names to subfiles, plus
11126 cu->line_header->file_names may contain dups. */
11127 b->get_current_subfile ()->symtab
11128 = allocate_symtab (cust, b->get_current_subfile ()->name);
11129 }
11130
11131 fe.symtab = b->get_current_subfile ()->symtab;
11132 tug_unshare->symtabs[i] = fe.symtab;
11133 }
11134 }
11135 else
11136 {
11137 gdb_assert (m_builder == nullptr);
11138 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11139 m_builder.reset (new struct buildsym_compunit
11140 (COMPUNIT_OBJFILE (cust), "",
11141 COMPUNIT_DIRNAME (cust),
11142 compunit_language (cust),
11143 0, cust));
11144 list_in_scope = get_builder ()->get_file_symbols ();
11145
11146 auto &file_names = line_header->file_names ();
11147 for (i = 0; i < file_names.size (); ++i)
11148 {
11149 file_entry &fe = file_names[i];
11150 fe.symtab = tug_unshare->symtabs[i];
11151 }
11152 }
11153
11154 /* The main symtab is allocated last. Type units don't have DW_AT_name
11155 so they don't have a "real" (so to speak) symtab anyway.
11156 There is later code that will assign the main symtab to all symbols
11157 that don't have one. We need to handle the case of a symbol with a
11158 missing symtab (DW_AT_decl_file) anyway. */
11159 }
11160
11161 /* Process DW_TAG_type_unit.
11162 For TUs we want to skip the first top level sibling if it's not the
11163 actual type being defined by this TU. In this case the first top
11164 level sibling is there to provide context only. */
11165
11166 static void
11167 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11168 {
11169 struct die_info *child_die;
11170
11171 prepare_one_comp_unit (cu, die, language_minimal);
11172
11173 /* Initialize (or reinitialize) the machinery for building symtabs.
11174 We do this before processing child DIEs, so that the line header table
11175 is available for DW_AT_decl_file. */
11176 cu->setup_type_unit_groups (die);
11177
11178 if (die->child != NULL)
11179 {
11180 child_die = die->child;
11181 while (child_die && child_die->tag)
11182 {
11183 process_die (child_die, cu);
11184 child_die = child_die->sibling;
11185 }
11186 }
11187 }
11188 \f
11189 /* DWO/DWP files.
11190
11191 http://gcc.gnu.org/wiki/DebugFission
11192 http://gcc.gnu.org/wiki/DebugFissionDWP
11193
11194 To simplify handling of both DWO files ("object" files with the DWARF info)
11195 and DWP files (a file with the DWOs packaged up into one file), we treat
11196 DWP files as having a collection of virtual DWO files. */
11197
11198 static hashval_t
11199 hash_dwo_file (const void *item)
11200 {
11201 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11202 hashval_t hash;
11203
11204 hash = htab_hash_string (dwo_file->dwo_name);
11205 if (dwo_file->comp_dir != NULL)
11206 hash += htab_hash_string (dwo_file->comp_dir);
11207 return hash;
11208 }
11209
11210 static int
11211 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11212 {
11213 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11214 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11215
11216 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11217 return 0;
11218 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11219 return lhs->comp_dir == rhs->comp_dir;
11220 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11221 }
11222
11223 /* Allocate a hash table for DWO files. */
11224
11225 static htab_up
11226 allocate_dwo_file_hash_table ()
11227 {
11228 auto delete_dwo_file = [] (void *item)
11229 {
11230 struct dwo_file *dwo_file = (struct dwo_file *) item;
11231
11232 delete dwo_file;
11233 };
11234
11235 return htab_up (htab_create_alloc (41,
11236 hash_dwo_file,
11237 eq_dwo_file,
11238 delete_dwo_file,
11239 xcalloc, xfree));
11240 }
11241
11242 /* Lookup DWO file DWO_NAME. */
11243
11244 static void **
11245 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11246 const char *dwo_name,
11247 const char *comp_dir)
11248 {
11249 struct dwo_file find_entry;
11250 void **slot;
11251
11252 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11253 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11254
11255 find_entry.dwo_name = dwo_name;
11256 find_entry.comp_dir = comp_dir;
11257 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11258 INSERT);
11259
11260 return slot;
11261 }
11262
11263 static hashval_t
11264 hash_dwo_unit (const void *item)
11265 {
11266 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11267
11268 /* This drops the top 32 bits of the id, but is ok for a hash. */
11269 return dwo_unit->signature;
11270 }
11271
11272 static int
11273 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11274 {
11275 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11276 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11277
11278 /* The signature is assumed to be unique within the DWO file.
11279 So while object file CU dwo_id's always have the value zero,
11280 that's OK, assuming each object file DWO file has only one CU,
11281 and that's the rule for now. */
11282 return lhs->signature == rhs->signature;
11283 }
11284
11285 /* Allocate a hash table for DWO CUs,TUs.
11286 There is one of these tables for each of CUs,TUs for each DWO file. */
11287
11288 static htab_up
11289 allocate_dwo_unit_table ()
11290 {
11291 /* Start out with a pretty small number.
11292 Generally DWO files contain only one CU and maybe some TUs. */
11293 return htab_up (htab_create_alloc (3,
11294 hash_dwo_unit,
11295 eq_dwo_unit,
11296 NULL, xcalloc, xfree));
11297 }
11298
11299 /* die_reader_func for create_dwo_cu. */
11300
11301 static void
11302 create_dwo_cu_reader (const struct die_reader_specs *reader,
11303 const gdb_byte *info_ptr,
11304 struct die_info *comp_unit_die,
11305 struct dwo_file *dwo_file,
11306 struct dwo_unit *dwo_unit)
11307 {
11308 struct dwarf2_cu *cu = reader->cu;
11309 sect_offset sect_off = cu->per_cu->sect_off;
11310 struct dwarf2_section_info *section = cu->per_cu->section;
11311
11312 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11313 if (!signature.has_value ())
11314 {
11315 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11316 " its dwo_id [in module %s]"),
11317 sect_offset_str (sect_off), dwo_file->dwo_name);
11318 return;
11319 }
11320
11321 dwo_unit->dwo_file = dwo_file;
11322 dwo_unit->signature = *signature;
11323 dwo_unit->section = section;
11324 dwo_unit->sect_off = sect_off;
11325 dwo_unit->length = cu->per_cu->length;
11326
11327 if (dwarf_read_debug)
11328 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11329 sect_offset_str (sect_off),
11330 hex_string (dwo_unit->signature));
11331 }
11332
11333 /* Create the dwo_units for the CUs in a DWO_FILE.
11334 Note: This function processes DWO files only, not DWP files. */
11335
11336 static void
11337 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11338 dwarf2_cu *cu, struct dwo_file &dwo_file,
11339 dwarf2_section_info &section, htab_up &cus_htab)
11340 {
11341 struct objfile *objfile = dwarf2_per_objfile->objfile;
11342 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11343 const gdb_byte *info_ptr, *end_ptr;
11344
11345 section.read (objfile);
11346 info_ptr = section.buffer;
11347
11348 if (info_ptr == NULL)
11349 return;
11350
11351 if (dwarf_read_debug)
11352 {
11353 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11354 section.get_name (),
11355 section.get_file_name ());
11356 }
11357
11358 end_ptr = info_ptr + section.size;
11359 while (info_ptr < end_ptr)
11360 {
11361 struct dwarf2_per_cu_data per_cu;
11362 struct dwo_unit read_unit {};
11363 struct dwo_unit *dwo_unit;
11364 void **slot;
11365 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11366
11367 memset (&per_cu, 0, sizeof (per_cu));
11368 per_cu.per_bfd = per_bfd;
11369 per_cu.is_debug_types = 0;
11370 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11371 per_cu.section = &section;
11372
11373 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11374 if (!reader.dummy_p)
11375 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11376 &dwo_file, &read_unit);
11377 info_ptr += per_cu.length;
11378
11379 // If the unit could not be parsed, skip it.
11380 if (read_unit.dwo_file == NULL)
11381 continue;
11382
11383 if (cus_htab == NULL)
11384 cus_htab = allocate_dwo_unit_table ();
11385
11386 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11387 struct dwo_unit);
11388 *dwo_unit = read_unit;
11389 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11390 gdb_assert (slot != NULL);
11391 if (*slot != NULL)
11392 {
11393 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11394 sect_offset dup_sect_off = dup_cu->sect_off;
11395
11396 complaint (_("debug cu entry at offset %s is duplicate to"
11397 " the entry at offset %s, signature %s"),
11398 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11399 hex_string (dwo_unit->signature));
11400 }
11401 *slot = (void *)dwo_unit;
11402 }
11403 }
11404
11405 /* DWP file .debug_{cu,tu}_index section format:
11406 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11407
11408 DWP Version 1:
11409
11410 Both index sections have the same format, and serve to map a 64-bit
11411 signature to a set of section numbers. Each section begins with a header,
11412 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11413 indexes, and a pool of 32-bit section numbers. The index sections will be
11414 aligned at 8-byte boundaries in the file.
11415
11416 The index section header consists of:
11417
11418 V, 32 bit version number
11419 -, 32 bits unused
11420 N, 32 bit number of compilation units or type units in the index
11421 M, 32 bit number of slots in the hash table
11422
11423 Numbers are recorded using the byte order of the application binary.
11424
11425 The hash table begins at offset 16 in the section, and consists of an array
11426 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11427 order of the application binary). Unused slots in the hash table are 0.
11428 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11429
11430 The parallel table begins immediately after the hash table
11431 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11432 array of 32-bit indexes (using the byte order of the application binary),
11433 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11434 table contains a 32-bit index into the pool of section numbers. For unused
11435 hash table slots, the corresponding entry in the parallel table will be 0.
11436
11437 The pool of section numbers begins immediately following the hash table
11438 (at offset 16 + 12 * M from the beginning of the section). The pool of
11439 section numbers consists of an array of 32-bit words (using the byte order
11440 of the application binary). Each item in the array is indexed starting
11441 from 0. The hash table entry provides the index of the first section
11442 number in the set. Additional section numbers in the set follow, and the
11443 set is terminated by a 0 entry (section number 0 is not used in ELF).
11444
11445 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11446 section must be the first entry in the set, and the .debug_abbrev.dwo must
11447 be the second entry. Other members of the set may follow in any order.
11448
11449 ---
11450
11451 DWP Version 2:
11452
11453 DWP Version 2 combines all the .debug_info, etc. sections into one,
11454 and the entries in the index tables are now offsets into these sections.
11455 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11456 section.
11457
11458 Index Section Contents:
11459 Header
11460 Hash Table of Signatures dwp_hash_table.hash_table
11461 Parallel Table of Indices dwp_hash_table.unit_table
11462 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11463 Table of Section Sizes dwp_hash_table.v2.sizes
11464
11465 The index section header consists of:
11466
11467 V, 32 bit version number
11468 L, 32 bit number of columns in the table of section offsets
11469 N, 32 bit number of compilation units or type units in the index
11470 M, 32 bit number of slots in the hash table
11471
11472 Numbers are recorded using the byte order of the application binary.
11473
11474 The hash table has the same format as version 1.
11475 The parallel table of indices has the same format as version 1,
11476 except that the entries are origin-1 indices into the table of sections
11477 offsets and the table of section sizes.
11478
11479 The table of offsets begins immediately following the parallel table
11480 (at offset 16 + 12 * M from the beginning of the section). The table is
11481 a two-dimensional array of 32-bit words (using the byte order of the
11482 application binary), with L columns and N+1 rows, in row-major order.
11483 Each row in the array is indexed starting from 0. The first row provides
11484 a key to the remaining rows: each column in this row provides an identifier
11485 for a debug section, and the offsets in the same column of subsequent rows
11486 refer to that section. The section identifiers are:
11487
11488 DW_SECT_INFO 1 .debug_info.dwo
11489 DW_SECT_TYPES 2 .debug_types.dwo
11490 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11491 DW_SECT_LINE 4 .debug_line.dwo
11492 DW_SECT_LOC 5 .debug_loc.dwo
11493 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11494 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11495 DW_SECT_MACRO 8 .debug_macro.dwo
11496
11497 The offsets provided by the CU and TU index sections are the base offsets
11498 for the contributions made by each CU or TU to the corresponding section
11499 in the package file. Each CU and TU header contains an abbrev_offset
11500 field, used to find the abbreviations table for that CU or TU within the
11501 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11502 be interpreted as relative to the base offset given in the index section.
11503 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11504 should be interpreted as relative to the base offset for .debug_line.dwo,
11505 and offsets into other debug sections obtained from DWARF attributes should
11506 also be interpreted as relative to the corresponding base offset.
11507
11508 The table of sizes begins immediately following the table of offsets.
11509 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11510 with L columns and N rows, in row-major order. Each row in the array is
11511 indexed starting from 1 (row 0 is shared by the two tables).
11512
11513 ---
11514
11515 Hash table lookup is handled the same in version 1 and 2:
11516
11517 We assume that N and M will not exceed 2^32 - 1.
11518 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11519
11520 Given a 64-bit compilation unit signature or a type signature S, an entry
11521 in the hash table is located as follows:
11522
11523 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11524 the low-order k bits all set to 1.
11525
11526 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11527
11528 3) If the hash table entry at index H matches the signature, use that
11529 entry. If the hash table entry at index H is unused (all zeroes),
11530 terminate the search: the signature is not present in the table.
11531
11532 4) Let H = (H + H') modulo M. Repeat at Step 3.
11533
11534 Because M > N and H' and M are relatively prime, the search is guaranteed
11535 to stop at an unused slot or find the match. */
11536
11537 /* Create a hash table to map DWO IDs to their CU/TU entry in
11538 .debug_{info,types}.dwo in DWP_FILE.
11539 Returns NULL if there isn't one.
11540 Note: This function processes DWP files only, not DWO files. */
11541
11542 static struct dwp_hash_table *
11543 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11544 struct dwp_file *dwp_file, int is_debug_types)
11545 {
11546 struct objfile *objfile = dwarf2_per_objfile->objfile;
11547 bfd *dbfd = dwp_file->dbfd.get ();
11548 const gdb_byte *index_ptr, *index_end;
11549 struct dwarf2_section_info *index;
11550 uint32_t version, nr_columns, nr_units, nr_slots;
11551 struct dwp_hash_table *htab;
11552
11553 if (is_debug_types)
11554 index = &dwp_file->sections.tu_index;
11555 else
11556 index = &dwp_file->sections.cu_index;
11557
11558 if (index->empty ())
11559 return NULL;
11560 index->read (objfile);
11561
11562 index_ptr = index->buffer;
11563 index_end = index_ptr + index->size;
11564
11565 version = read_4_bytes (dbfd, index_ptr);
11566 index_ptr += 4;
11567 if (version == 2)
11568 nr_columns = read_4_bytes (dbfd, index_ptr);
11569 else
11570 nr_columns = 0;
11571 index_ptr += 4;
11572 nr_units = read_4_bytes (dbfd, index_ptr);
11573 index_ptr += 4;
11574 nr_slots = read_4_bytes (dbfd, index_ptr);
11575 index_ptr += 4;
11576
11577 if (version != 1 && version != 2)
11578 {
11579 error (_("Dwarf Error: unsupported DWP file version (%s)"
11580 " [in module %s]"),
11581 pulongest (version), dwp_file->name);
11582 }
11583 if (nr_slots != (nr_slots & -nr_slots))
11584 {
11585 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11586 " is not power of 2 [in module %s]"),
11587 pulongest (nr_slots), dwp_file->name);
11588 }
11589
11590 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11591 htab->version = version;
11592 htab->nr_columns = nr_columns;
11593 htab->nr_units = nr_units;
11594 htab->nr_slots = nr_slots;
11595 htab->hash_table = index_ptr;
11596 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11597
11598 /* Exit early if the table is empty. */
11599 if (nr_slots == 0 || nr_units == 0
11600 || (version == 2 && nr_columns == 0))
11601 {
11602 /* All must be zero. */
11603 if (nr_slots != 0 || nr_units != 0
11604 || (version == 2 && nr_columns != 0))
11605 {
11606 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11607 " all zero [in modules %s]"),
11608 dwp_file->name);
11609 }
11610 return htab;
11611 }
11612
11613 if (version == 1)
11614 {
11615 htab->section_pool.v1.indices =
11616 htab->unit_table + sizeof (uint32_t) * nr_slots;
11617 /* It's harder to decide whether the section is too small in v1.
11618 V1 is deprecated anyway so we punt. */
11619 }
11620 else
11621 {
11622 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11623 int *ids = htab->section_pool.v2.section_ids;
11624 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11625 /* Reverse map for error checking. */
11626 int ids_seen[DW_SECT_MAX + 1];
11627 int i;
11628
11629 if (nr_columns < 2)
11630 {
11631 error (_("Dwarf Error: bad DWP hash table, too few columns"
11632 " in section table [in module %s]"),
11633 dwp_file->name);
11634 }
11635 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11636 {
11637 error (_("Dwarf Error: bad DWP hash table, too many columns"
11638 " in section table [in module %s]"),
11639 dwp_file->name);
11640 }
11641 memset (ids, 255, sizeof_ids);
11642 memset (ids_seen, 255, sizeof (ids_seen));
11643 for (i = 0; i < nr_columns; ++i)
11644 {
11645 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11646
11647 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11648 {
11649 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11650 " in section table [in module %s]"),
11651 id, dwp_file->name);
11652 }
11653 if (ids_seen[id] != -1)
11654 {
11655 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11656 " id %d in section table [in module %s]"),
11657 id, dwp_file->name);
11658 }
11659 ids_seen[id] = i;
11660 ids[i] = id;
11661 }
11662 /* Must have exactly one info or types section. */
11663 if (((ids_seen[DW_SECT_INFO] != -1)
11664 + (ids_seen[DW_SECT_TYPES] != -1))
11665 != 1)
11666 {
11667 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11668 " DWO info/types section [in module %s]"),
11669 dwp_file->name);
11670 }
11671 /* Must have an abbrev section. */
11672 if (ids_seen[DW_SECT_ABBREV] == -1)
11673 {
11674 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11675 " section [in module %s]"),
11676 dwp_file->name);
11677 }
11678 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11679 htab->section_pool.v2.sizes =
11680 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11681 * nr_units * nr_columns);
11682 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11683 * nr_units * nr_columns))
11684 > index_end)
11685 {
11686 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11687 " [in module %s]"),
11688 dwp_file->name);
11689 }
11690 }
11691
11692 return htab;
11693 }
11694
11695 /* Update SECTIONS with the data from SECTP.
11696
11697 This function is like the other "locate" section routines that are
11698 passed to bfd_map_over_sections, but in this context the sections to
11699 read comes from the DWP V1 hash table, not the full ELF section table.
11700
11701 The result is non-zero for success, or zero if an error was found. */
11702
11703 static int
11704 locate_v1_virtual_dwo_sections (asection *sectp,
11705 struct virtual_v1_dwo_sections *sections)
11706 {
11707 const struct dwop_section_names *names = &dwop_section_names;
11708
11709 if (section_is_p (sectp->name, &names->abbrev_dwo))
11710 {
11711 /* There can be only one. */
11712 if (sections->abbrev.s.section != NULL)
11713 return 0;
11714 sections->abbrev.s.section = sectp;
11715 sections->abbrev.size = bfd_section_size (sectp);
11716 }
11717 else if (section_is_p (sectp->name, &names->info_dwo)
11718 || section_is_p (sectp->name, &names->types_dwo))
11719 {
11720 /* There can be only one. */
11721 if (sections->info_or_types.s.section != NULL)
11722 return 0;
11723 sections->info_or_types.s.section = sectp;
11724 sections->info_or_types.size = bfd_section_size (sectp);
11725 }
11726 else if (section_is_p (sectp->name, &names->line_dwo))
11727 {
11728 /* There can be only one. */
11729 if (sections->line.s.section != NULL)
11730 return 0;
11731 sections->line.s.section = sectp;
11732 sections->line.size = bfd_section_size (sectp);
11733 }
11734 else if (section_is_p (sectp->name, &names->loc_dwo))
11735 {
11736 /* There can be only one. */
11737 if (sections->loc.s.section != NULL)
11738 return 0;
11739 sections->loc.s.section = sectp;
11740 sections->loc.size = bfd_section_size (sectp);
11741 }
11742 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11743 {
11744 /* There can be only one. */
11745 if (sections->macinfo.s.section != NULL)
11746 return 0;
11747 sections->macinfo.s.section = sectp;
11748 sections->macinfo.size = bfd_section_size (sectp);
11749 }
11750 else if (section_is_p (sectp->name, &names->macro_dwo))
11751 {
11752 /* There can be only one. */
11753 if (sections->macro.s.section != NULL)
11754 return 0;
11755 sections->macro.s.section = sectp;
11756 sections->macro.size = bfd_section_size (sectp);
11757 }
11758 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11759 {
11760 /* There can be only one. */
11761 if (sections->str_offsets.s.section != NULL)
11762 return 0;
11763 sections->str_offsets.s.section = sectp;
11764 sections->str_offsets.size = bfd_section_size (sectp);
11765 }
11766 else
11767 {
11768 /* No other kind of section is valid. */
11769 return 0;
11770 }
11771
11772 return 1;
11773 }
11774
11775 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11776 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11777 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11778 This is for DWP version 1 files. */
11779
11780 static struct dwo_unit *
11781 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11782 struct dwp_file *dwp_file,
11783 uint32_t unit_index,
11784 const char *comp_dir,
11785 ULONGEST signature, int is_debug_types)
11786 {
11787 const struct dwp_hash_table *dwp_htab =
11788 is_debug_types ? dwp_file->tus : dwp_file->cus;
11789 bfd *dbfd = dwp_file->dbfd.get ();
11790 const char *kind = is_debug_types ? "TU" : "CU";
11791 struct dwo_file *dwo_file;
11792 struct dwo_unit *dwo_unit;
11793 struct virtual_v1_dwo_sections sections;
11794 void **dwo_file_slot;
11795 int i;
11796
11797 gdb_assert (dwp_file->version == 1);
11798
11799 if (dwarf_read_debug)
11800 {
11801 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11802 kind,
11803 pulongest (unit_index), hex_string (signature),
11804 dwp_file->name);
11805 }
11806
11807 /* Fetch the sections of this DWO unit.
11808 Put a limit on the number of sections we look for so that bad data
11809 doesn't cause us to loop forever. */
11810
11811 #define MAX_NR_V1_DWO_SECTIONS \
11812 (1 /* .debug_info or .debug_types */ \
11813 + 1 /* .debug_abbrev */ \
11814 + 1 /* .debug_line */ \
11815 + 1 /* .debug_loc */ \
11816 + 1 /* .debug_str_offsets */ \
11817 + 1 /* .debug_macro or .debug_macinfo */ \
11818 + 1 /* trailing zero */)
11819
11820 memset (&sections, 0, sizeof (sections));
11821
11822 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11823 {
11824 asection *sectp;
11825 uint32_t section_nr =
11826 read_4_bytes (dbfd,
11827 dwp_htab->section_pool.v1.indices
11828 + (unit_index + i) * sizeof (uint32_t));
11829
11830 if (section_nr == 0)
11831 break;
11832 if (section_nr >= dwp_file->num_sections)
11833 {
11834 error (_("Dwarf Error: bad DWP hash table, section number too large"
11835 " [in module %s]"),
11836 dwp_file->name);
11837 }
11838
11839 sectp = dwp_file->elf_sections[section_nr];
11840 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11841 {
11842 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11843 " [in module %s]"),
11844 dwp_file->name);
11845 }
11846 }
11847
11848 if (i < 2
11849 || sections.info_or_types.empty ()
11850 || sections.abbrev.empty ())
11851 {
11852 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11853 " [in module %s]"),
11854 dwp_file->name);
11855 }
11856 if (i == MAX_NR_V1_DWO_SECTIONS)
11857 {
11858 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11859 " [in module %s]"),
11860 dwp_file->name);
11861 }
11862
11863 /* It's easier for the rest of the code if we fake a struct dwo_file and
11864 have dwo_unit "live" in that. At least for now.
11865
11866 The DWP file can be made up of a random collection of CUs and TUs.
11867 However, for each CU + set of TUs that came from the same original DWO
11868 file, we can combine them back into a virtual DWO file to save space
11869 (fewer struct dwo_file objects to allocate). Remember that for really
11870 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11871
11872 std::string virtual_dwo_name =
11873 string_printf ("virtual-dwo/%d-%d-%d-%d",
11874 sections.abbrev.get_id (),
11875 sections.line.get_id (),
11876 sections.loc.get_id (),
11877 sections.str_offsets.get_id ());
11878 /* Can we use an existing virtual DWO file? */
11879 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11880 virtual_dwo_name.c_str (),
11881 comp_dir);
11882 /* Create one if necessary. */
11883 if (*dwo_file_slot == NULL)
11884 {
11885 if (dwarf_read_debug)
11886 {
11887 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11888 virtual_dwo_name.c_str ());
11889 }
11890 dwo_file = new struct dwo_file;
11891 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11892 dwo_file->comp_dir = comp_dir;
11893 dwo_file->sections.abbrev = sections.abbrev;
11894 dwo_file->sections.line = sections.line;
11895 dwo_file->sections.loc = sections.loc;
11896 dwo_file->sections.macinfo = sections.macinfo;
11897 dwo_file->sections.macro = sections.macro;
11898 dwo_file->sections.str_offsets = sections.str_offsets;
11899 /* The "str" section is global to the entire DWP file. */
11900 dwo_file->sections.str = dwp_file->sections.str;
11901 /* The info or types section is assigned below to dwo_unit,
11902 there's no need to record it in dwo_file.
11903 Also, we can't simply record type sections in dwo_file because
11904 we record a pointer into the vector in dwo_unit. As we collect more
11905 types we'll grow the vector and eventually have to reallocate space
11906 for it, invalidating all copies of pointers into the previous
11907 contents. */
11908 *dwo_file_slot = dwo_file;
11909 }
11910 else
11911 {
11912 if (dwarf_read_debug)
11913 {
11914 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11915 virtual_dwo_name.c_str ());
11916 }
11917 dwo_file = (struct dwo_file *) *dwo_file_slot;
11918 }
11919
11920 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11921 dwo_unit->dwo_file = dwo_file;
11922 dwo_unit->signature = signature;
11923 dwo_unit->section =
11924 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11925 *dwo_unit->section = sections.info_or_types;
11926 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11927
11928 return dwo_unit;
11929 }
11930
11931 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11932 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11933 piece within that section used by a TU/CU, return a virtual section
11934 of just that piece. */
11935
11936 static struct dwarf2_section_info
11937 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11938 struct dwarf2_section_info *section,
11939 bfd_size_type offset, bfd_size_type size)
11940 {
11941 struct dwarf2_section_info result;
11942 asection *sectp;
11943
11944 gdb_assert (section != NULL);
11945 gdb_assert (!section->is_virtual);
11946
11947 memset (&result, 0, sizeof (result));
11948 result.s.containing_section = section;
11949 result.is_virtual = true;
11950
11951 if (size == 0)
11952 return result;
11953
11954 sectp = section->get_bfd_section ();
11955
11956 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11957 bounds of the real section. This is a pretty-rare event, so just
11958 flag an error (easier) instead of a warning and trying to cope. */
11959 if (sectp == NULL
11960 || offset + size > bfd_section_size (sectp))
11961 {
11962 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11963 " in section %s [in module %s]"),
11964 sectp ? bfd_section_name (sectp) : "<unknown>",
11965 objfile_name (dwarf2_per_objfile->objfile));
11966 }
11967
11968 result.virtual_offset = offset;
11969 result.size = size;
11970 return result;
11971 }
11972
11973 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11974 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11975 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11976 This is for DWP version 2 files. */
11977
11978 static struct dwo_unit *
11979 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11980 struct dwp_file *dwp_file,
11981 uint32_t unit_index,
11982 const char *comp_dir,
11983 ULONGEST signature, int is_debug_types)
11984 {
11985 const struct dwp_hash_table *dwp_htab =
11986 is_debug_types ? dwp_file->tus : dwp_file->cus;
11987 bfd *dbfd = dwp_file->dbfd.get ();
11988 const char *kind = is_debug_types ? "TU" : "CU";
11989 struct dwo_file *dwo_file;
11990 struct dwo_unit *dwo_unit;
11991 struct virtual_v2_dwo_sections sections;
11992 void **dwo_file_slot;
11993 int i;
11994
11995 gdb_assert (dwp_file->version == 2);
11996
11997 if (dwarf_read_debug)
11998 {
11999 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12000 kind,
12001 pulongest (unit_index), hex_string (signature),
12002 dwp_file->name);
12003 }
12004
12005 /* Fetch the section offsets of this DWO unit. */
12006
12007 memset (&sections, 0, sizeof (sections));
12008
12009 for (i = 0; i < dwp_htab->nr_columns; ++i)
12010 {
12011 uint32_t offset = read_4_bytes (dbfd,
12012 dwp_htab->section_pool.v2.offsets
12013 + (((unit_index - 1) * dwp_htab->nr_columns
12014 + i)
12015 * sizeof (uint32_t)));
12016 uint32_t size = read_4_bytes (dbfd,
12017 dwp_htab->section_pool.v2.sizes
12018 + (((unit_index - 1) * dwp_htab->nr_columns
12019 + i)
12020 * sizeof (uint32_t)));
12021
12022 switch (dwp_htab->section_pool.v2.section_ids[i])
12023 {
12024 case DW_SECT_INFO:
12025 case DW_SECT_TYPES:
12026 sections.info_or_types_offset = offset;
12027 sections.info_or_types_size = size;
12028 break;
12029 case DW_SECT_ABBREV:
12030 sections.abbrev_offset = offset;
12031 sections.abbrev_size = size;
12032 break;
12033 case DW_SECT_LINE:
12034 sections.line_offset = offset;
12035 sections.line_size = size;
12036 break;
12037 case DW_SECT_LOC:
12038 sections.loc_offset = offset;
12039 sections.loc_size = size;
12040 break;
12041 case DW_SECT_STR_OFFSETS:
12042 sections.str_offsets_offset = offset;
12043 sections.str_offsets_size = size;
12044 break;
12045 case DW_SECT_MACINFO:
12046 sections.macinfo_offset = offset;
12047 sections.macinfo_size = size;
12048 break;
12049 case DW_SECT_MACRO:
12050 sections.macro_offset = offset;
12051 sections.macro_size = size;
12052 break;
12053 }
12054 }
12055
12056 /* It's easier for the rest of the code if we fake a struct dwo_file and
12057 have dwo_unit "live" in that. At least for now.
12058
12059 The DWP file can be made up of a random collection of CUs and TUs.
12060 However, for each CU + set of TUs that came from the same original DWO
12061 file, we can combine them back into a virtual DWO file to save space
12062 (fewer struct dwo_file objects to allocate). Remember that for really
12063 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12064
12065 std::string virtual_dwo_name =
12066 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12067 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12068 (long) (sections.line_size ? sections.line_offset : 0),
12069 (long) (sections.loc_size ? sections.loc_offset : 0),
12070 (long) (sections.str_offsets_size
12071 ? sections.str_offsets_offset : 0));
12072 /* Can we use an existing virtual DWO file? */
12073 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12074 virtual_dwo_name.c_str (),
12075 comp_dir);
12076 /* Create one if necessary. */
12077 if (*dwo_file_slot == NULL)
12078 {
12079 if (dwarf_read_debug)
12080 {
12081 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12082 virtual_dwo_name.c_str ());
12083 }
12084 dwo_file = new struct dwo_file;
12085 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12086 dwo_file->comp_dir = comp_dir;
12087 dwo_file->sections.abbrev =
12088 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12089 sections.abbrev_offset, sections.abbrev_size);
12090 dwo_file->sections.line =
12091 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12092 sections.line_offset, sections.line_size);
12093 dwo_file->sections.loc =
12094 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12095 sections.loc_offset, sections.loc_size);
12096 dwo_file->sections.macinfo =
12097 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12098 sections.macinfo_offset, sections.macinfo_size);
12099 dwo_file->sections.macro =
12100 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12101 sections.macro_offset, sections.macro_size);
12102 dwo_file->sections.str_offsets =
12103 create_dwp_v2_section (dwarf2_per_objfile,
12104 &dwp_file->sections.str_offsets,
12105 sections.str_offsets_offset,
12106 sections.str_offsets_size);
12107 /* The "str" section is global to the entire DWP file. */
12108 dwo_file->sections.str = dwp_file->sections.str;
12109 /* The info or types section is assigned below to dwo_unit,
12110 there's no need to record it in dwo_file.
12111 Also, we can't simply record type sections in dwo_file because
12112 we record a pointer into the vector in dwo_unit. As we collect more
12113 types we'll grow the vector and eventually have to reallocate space
12114 for it, invalidating all copies of pointers into the previous
12115 contents. */
12116 *dwo_file_slot = dwo_file;
12117 }
12118 else
12119 {
12120 if (dwarf_read_debug)
12121 {
12122 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12123 virtual_dwo_name.c_str ());
12124 }
12125 dwo_file = (struct dwo_file *) *dwo_file_slot;
12126 }
12127
12128 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12129 dwo_unit->dwo_file = dwo_file;
12130 dwo_unit->signature = signature;
12131 dwo_unit->section =
12132 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12133 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12134 is_debug_types
12135 ? &dwp_file->sections.types
12136 : &dwp_file->sections.info,
12137 sections.info_or_types_offset,
12138 sections.info_or_types_size);
12139 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12140
12141 return dwo_unit;
12142 }
12143
12144 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12145 Returns NULL if the signature isn't found. */
12146
12147 static struct dwo_unit *
12148 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12149 struct dwp_file *dwp_file, const char *comp_dir,
12150 ULONGEST signature, int is_debug_types)
12151 {
12152 const struct dwp_hash_table *dwp_htab =
12153 is_debug_types ? dwp_file->tus : dwp_file->cus;
12154 bfd *dbfd = dwp_file->dbfd.get ();
12155 uint32_t mask = dwp_htab->nr_slots - 1;
12156 uint32_t hash = signature & mask;
12157 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12158 unsigned int i;
12159 void **slot;
12160 struct dwo_unit find_dwo_cu;
12161
12162 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12163 find_dwo_cu.signature = signature;
12164 slot = htab_find_slot (is_debug_types
12165 ? dwp_file->loaded_tus.get ()
12166 : dwp_file->loaded_cus.get (),
12167 &find_dwo_cu, INSERT);
12168
12169 if (*slot != NULL)
12170 return (struct dwo_unit *) *slot;
12171
12172 /* Use a for loop so that we don't loop forever on bad debug info. */
12173 for (i = 0; i < dwp_htab->nr_slots; ++i)
12174 {
12175 ULONGEST signature_in_table;
12176
12177 signature_in_table =
12178 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12179 if (signature_in_table == signature)
12180 {
12181 uint32_t unit_index =
12182 read_4_bytes (dbfd,
12183 dwp_htab->unit_table + hash * sizeof (uint32_t));
12184
12185 if (dwp_file->version == 1)
12186 {
12187 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12188 dwp_file, unit_index,
12189 comp_dir, signature,
12190 is_debug_types);
12191 }
12192 else
12193 {
12194 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12195 dwp_file, unit_index,
12196 comp_dir, signature,
12197 is_debug_types);
12198 }
12199 return (struct dwo_unit *) *slot;
12200 }
12201 if (signature_in_table == 0)
12202 return NULL;
12203 hash = (hash + hash2) & mask;
12204 }
12205
12206 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12207 " [in module %s]"),
12208 dwp_file->name);
12209 }
12210
12211 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12212 Open the file specified by FILE_NAME and hand it off to BFD for
12213 preliminary analysis. Return a newly initialized bfd *, which
12214 includes a canonicalized copy of FILE_NAME.
12215 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12216 SEARCH_CWD is true if the current directory is to be searched.
12217 It will be searched before debug-file-directory.
12218 If successful, the file is added to the bfd include table of the
12219 objfile's bfd (see gdb_bfd_record_inclusion).
12220 If unable to find/open the file, return NULL.
12221 NOTE: This function is derived from symfile_bfd_open. */
12222
12223 static gdb_bfd_ref_ptr
12224 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12225 const char *file_name, int is_dwp, int search_cwd)
12226 {
12227 int desc;
12228 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12229 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12230 to debug_file_directory. */
12231 const char *search_path;
12232 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12233
12234 gdb::unique_xmalloc_ptr<char> search_path_holder;
12235 if (search_cwd)
12236 {
12237 if (*debug_file_directory != '\0')
12238 {
12239 search_path_holder.reset (concat (".", dirname_separator_string,
12240 debug_file_directory,
12241 (char *) NULL));
12242 search_path = search_path_holder.get ();
12243 }
12244 else
12245 search_path = ".";
12246 }
12247 else
12248 search_path = debug_file_directory;
12249
12250 openp_flags flags = OPF_RETURN_REALPATH;
12251 if (is_dwp)
12252 flags |= OPF_SEARCH_IN_PATH;
12253
12254 gdb::unique_xmalloc_ptr<char> absolute_name;
12255 desc = openp (search_path, flags, file_name,
12256 O_RDONLY | O_BINARY, &absolute_name);
12257 if (desc < 0)
12258 return NULL;
12259
12260 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12261 gnutarget, desc));
12262 if (sym_bfd == NULL)
12263 return NULL;
12264 bfd_set_cacheable (sym_bfd.get (), 1);
12265
12266 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12267 return NULL;
12268
12269 /* Success. Record the bfd as having been included by the objfile's bfd.
12270 This is important because things like demangled_names_hash lives in the
12271 objfile's per_bfd space and may have references to things like symbol
12272 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12273 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12274
12275 return sym_bfd;
12276 }
12277
12278 /* Try to open DWO file FILE_NAME.
12279 COMP_DIR is the DW_AT_comp_dir attribute.
12280 The result is the bfd handle of the file.
12281 If there is a problem finding or opening the file, return NULL.
12282 Upon success, the canonicalized path of the file is stored in the bfd,
12283 same as symfile_bfd_open. */
12284
12285 static gdb_bfd_ref_ptr
12286 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12287 const char *file_name, const char *comp_dir)
12288 {
12289 if (IS_ABSOLUTE_PATH (file_name))
12290 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12291 0 /*is_dwp*/, 0 /*search_cwd*/);
12292
12293 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12294
12295 if (comp_dir != NULL)
12296 {
12297 gdb::unique_xmalloc_ptr<char> path_to_try
12298 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12299
12300 /* NOTE: If comp_dir is a relative path, this will also try the
12301 search path, which seems useful. */
12302 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12303 path_to_try.get (),
12304 0 /*is_dwp*/,
12305 1 /*search_cwd*/));
12306 if (abfd != NULL)
12307 return abfd;
12308 }
12309
12310 /* That didn't work, try debug-file-directory, which, despite its name,
12311 is a list of paths. */
12312
12313 if (*debug_file_directory == '\0')
12314 return NULL;
12315
12316 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12317 0 /*is_dwp*/, 1 /*search_cwd*/);
12318 }
12319
12320 /* This function is mapped across the sections and remembers the offset and
12321 size of each of the DWO debugging sections we are interested in. */
12322
12323 static void
12324 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12325 {
12326 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12327 const struct dwop_section_names *names = &dwop_section_names;
12328
12329 if (section_is_p (sectp->name, &names->abbrev_dwo))
12330 {
12331 dwo_sections->abbrev.s.section = sectp;
12332 dwo_sections->abbrev.size = bfd_section_size (sectp);
12333 }
12334 else if (section_is_p (sectp->name, &names->info_dwo))
12335 {
12336 dwo_sections->info.s.section = sectp;
12337 dwo_sections->info.size = bfd_section_size (sectp);
12338 }
12339 else if (section_is_p (sectp->name, &names->line_dwo))
12340 {
12341 dwo_sections->line.s.section = sectp;
12342 dwo_sections->line.size = bfd_section_size (sectp);
12343 }
12344 else if (section_is_p (sectp->name, &names->loc_dwo))
12345 {
12346 dwo_sections->loc.s.section = sectp;
12347 dwo_sections->loc.size = bfd_section_size (sectp);
12348 }
12349 else if (section_is_p (sectp->name, &names->loclists_dwo))
12350 {
12351 dwo_sections->loclists.s.section = sectp;
12352 dwo_sections->loclists.size = bfd_section_size (sectp);
12353 }
12354 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12355 {
12356 dwo_sections->macinfo.s.section = sectp;
12357 dwo_sections->macinfo.size = bfd_section_size (sectp);
12358 }
12359 else if (section_is_p (sectp->name, &names->macro_dwo))
12360 {
12361 dwo_sections->macro.s.section = sectp;
12362 dwo_sections->macro.size = bfd_section_size (sectp);
12363 }
12364 else if (section_is_p (sectp->name, &names->str_dwo))
12365 {
12366 dwo_sections->str.s.section = sectp;
12367 dwo_sections->str.size = bfd_section_size (sectp);
12368 }
12369 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12370 {
12371 dwo_sections->str_offsets.s.section = sectp;
12372 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12373 }
12374 else if (section_is_p (sectp->name, &names->types_dwo))
12375 {
12376 struct dwarf2_section_info type_section;
12377
12378 memset (&type_section, 0, sizeof (type_section));
12379 type_section.s.section = sectp;
12380 type_section.size = bfd_section_size (sectp);
12381 dwo_sections->types.push_back (type_section);
12382 }
12383 }
12384
12385 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12386 by PER_CU. This is for the non-DWP case.
12387 The result is NULL if DWO_NAME can't be found. */
12388
12389 static struct dwo_file *
12390 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12391 const char *comp_dir)
12392 {
12393 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12394
12395 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12396 if (dbfd == NULL)
12397 {
12398 if (dwarf_read_debug)
12399 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12400 return NULL;
12401 }
12402
12403 dwo_file_up dwo_file (new struct dwo_file);
12404 dwo_file->dwo_name = dwo_name;
12405 dwo_file->comp_dir = comp_dir;
12406 dwo_file->dbfd = std::move (dbfd);
12407
12408 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12409 &dwo_file->sections);
12410
12411 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12412 dwo_file->sections.info, dwo_file->cus);
12413
12414 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12415 dwo_file->sections.types, dwo_file->tus);
12416
12417 if (dwarf_read_debug)
12418 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12419
12420 return dwo_file.release ();
12421 }
12422
12423 /* This function is mapped across the sections and remembers the offset and
12424 size of each of the DWP debugging sections common to version 1 and 2 that
12425 we are interested in. */
12426
12427 static void
12428 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12429 void *dwp_file_ptr)
12430 {
12431 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12432 const struct dwop_section_names *names = &dwop_section_names;
12433 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12434
12435 /* Record the ELF section number for later lookup: this is what the
12436 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12437 gdb_assert (elf_section_nr < dwp_file->num_sections);
12438 dwp_file->elf_sections[elf_section_nr] = sectp;
12439
12440 /* Look for specific sections that we need. */
12441 if (section_is_p (sectp->name, &names->str_dwo))
12442 {
12443 dwp_file->sections.str.s.section = sectp;
12444 dwp_file->sections.str.size = bfd_section_size (sectp);
12445 }
12446 else if (section_is_p (sectp->name, &names->cu_index))
12447 {
12448 dwp_file->sections.cu_index.s.section = sectp;
12449 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12450 }
12451 else if (section_is_p (sectp->name, &names->tu_index))
12452 {
12453 dwp_file->sections.tu_index.s.section = sectp;
12454 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12455 }
12456 }
12457
12458 /* This function is mapped across the sections and remembers the offset and
12459 size of each of the DWP version 2 debugging sections that we are interested
12460 in. This is split into a separate function because we don't know if we
12461 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12462
12463 static void
12464 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12465 {
12466 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12467 const struct dwop_section_names *names = &dwop_section_names;
12468 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12469
12470 /* Record the ELF section number for later lookup: this is what the
12471 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12472 gdb_assert (elf_section_nr < dwp_file->num_sections);
12473 dwp_file->elf_sections[elf_section_nr] = sectp;
12474
12475 /* Look for specific sections that we need. */
12476 if (section_is_p (sectp->name, &names->abbrev_dwo))
12477 {
12478 dwp_file->sections.abbrev.s.section = sectp;
12479 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12480 }
12481 else if (section_is_p (sectp->name, &names->info_dwo))
12482 {
12483 dwp_file->sections.info.s.section = sectp;
12484 dwp_file->sections.info.size = bfd_section_size (sectp);
12485 }
12486 else if (section_is_p (sectp->name, &names->line_dwo))
12487 {
12488 dwp_file->sections.line.s.section = sectp;
12489 dwp_file->sections.line.size = bfd_section_size (sectp);
12490 }
12491 else if (section_is_p (sectp->name, &names->loc_dwo))
12492 {
12493 dwp_file->sections.loc.s.section = sectp;
12494 dwp_file->sections.loc.size = bfd_section_size (sectp);
12495 }
12496 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12497 {
12498 dwp_file->sections.macinfo.s.section = sectp;
12499 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12500 }
12501 else if (section_is_p (sectp->name, &names->macro_dwo))
12502 {
12503 dwp_file->sections.macro.s.section = sectp;
12504 dwp_file->sections.macro.size = bfd_section_size (sectp);
12505 }
12506 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12507 {
12508 dwp_file->sections.str_offsets.s.section = sectp;
12509 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12510 }
12511 else if (section_is_p (sectp->name, &names->types_dwo))
12512 {
12513 dwp_file->sections.types.s.section = sectp;
12514 dwp_file->sections.types.size = bfd_section_size (sectp);
12515 }
12516 }
12517
12518 /* Hash function for dwp_file loaded CUs/TUs. */
12519
12520 static hashval_t
12521 hash_dwp_loaded_cutus (const void *item)
12522 {
12523 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12524
12525 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12526 return dwo_unit->signature;
12527 }
12528
12529 /* Equality function for dwp_file loaded CUs/TUs. */
12530
12531 static int
12532 eq_dwp_loaded_cutus (const void *a, const void *b)
12533 {
12534 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12535 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12536
12537 return dua->signature == dub->signature;
12538 }
12539
12540 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12541
12542 static htab_up
12543 allocate_dwp_loaded_cutus_table ()
12544 {
12545 return htab_up (htab_create_alloc (3,
12546 hash_dwp_loaded_cutus,
12547 eq_dwp_loaded_cutus,
12548 NULL, xcalloc, xfree));
12549 }
12550
12551 /* Try to open DWP file FILE_NAME.
12552 The result is the bfd handle of the file.
12553 If there is a problem finding or opening the file, return NULL.
12554 Upon success, the canonicalized path of the file is stored in the bfd,
12555 same as symfile_bfd_open. */
12556
12557 static gdb_bfd_ref_ptr
12558 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12559 const char *file_name)
12560 {
12561 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12562 1 /*is_dwp*/,
12563 1 /*search_cwd*/));
12564 if (abfd != NULL)
12565 return abfd;
12566
12567 /* Work around upstream bug 15652.
12568 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12569 [Whether that's a "bug" is debatable, but it is getting in our way.]
12570 We have no real idea where the dwp file is, because gdb's realpath-ing
12571 of the executable's path may have discarded the needed info.
12572 [IWBN if the dwp file name was recorded in the executable, akin to
12573 .gnu_debuglink, but that doesn't exist yet.]
12574 Strip the directory from FILE_NAME and search again. */
12575 if (*debug_file_directory != '\0')
12576 {
12577 /* Don't implicitly search the current directory here.
12578 If the user wants to search "." to handle this case,
12579 it must be added to debug-file-directory. */
12580 return try_open_dwop_file (dwarf2_per_objfile,
12581 lbasename (file_name), 1 /*is_dwp*/,
12582 0 /*search_cwd*/);
12583 }
12584
12585 return NULL;
12586 }
12587
12588 /* Initialize the use of the DWP file for the current objfile.
12589 By convention the name of the DWP file is ${objfile}.dwp.
12590 The result is NULL if it can't be found. */
12591
12592 static std::unique_ptr<struct dwp_file>
12593 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12594 {
12595 struct objfile *objfile = dwarf2_per_objfile->objfile;
12596
12597 /* Try to find first .dwp for the binary file before any symbolic links
12598 resolving. */
12599
12600 /* If the objfile is a debug file, find the name of the real binary
12601 file and get the name of dwp file from there. */
12602 std::string dwp_name;
12603 if (objfile->separate_debug_objfile_backlink != NULL)
12604 {
12605 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12606 const char *backlink_basename = lbasename (backlink->original_name);
12607
12608 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12609 }
12610 else
12611 dwp_name = objfile->original_name;
12612
12613 dwp_name += ".dwp";
12614
12615 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12616 if (dbfd == NULL
12617 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12618 {
12619 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12620 dwp_name = objfile_name (objfile);
12621 dwp_name += ".dwp";
12622 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12623 }
12624
12625 if (dbfd == NULL)
12626 {
12627 if (dwarf_read_debug)
12628 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12629 return std::unique_ptr<dwp_file> ();
12630 }
12631
12632 const char *name = bfd_get_filename (dbfd.get ());
12633 std::unique_ptr<struct dwp_file> dwp_file
12634 (new struct dwp_file (name, std::move (dbfd)));
12635
12636 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12637 dwp_file->elf_sections =
12638 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12639 dwp_file->num_sections, asection *);
12640
12641 bfd_map_over_sections (dwp_file->dbfd.get (),
12642 dwarf2_locate_common_dwp_sections,
12643 dwp_file.get ());
12644
12645 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12646 0);
12647
12648 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12649 1);
12650
12651 /* The DWP file version is stored in the hash table. Oh well. */
12652 if (dwp_file->cus && dwp_file->tus
12653 && dwp_file->cus->version != dwp_file->tus->version)
12654 {
12655 /* Technically speaking, we should try to limp along, but this is
12656 pretty bizarre. We use pulongest here because that's the established
12657 portability solution (e.g, we cannot use %u for uint32_t). */
12658 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12659 " TU version %s [in DWP file %s]"),
12660 pulongest (dwp_file->cus->version),
12661 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12662 }
12663
12664 if (dwp_file->cus)
12665 dwp_file->version = dwp_file->cus->version;
12666 else if (dwp_file->tus)
12667 dwp_file->version = dwp_file->tus->version;
12668 else
12669 dwp_file->version = 2;
12670
12671 if (dwp_file->version == 2)
12672 bfd_map_over_sections (dwp_file->dbfd.get (),
12673 dwarf2_locate_v2_dwp_sections,
12674 dwp_file.get ());
12675
12676 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12677 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12678
12679 if (dwarf_read_debug)
12680 {
12681 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12682 fprintf_unfiltered (gdb_stdlog,
12683 " %s CUs, %s TUs\n",
12684 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12685 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12686 }
12687
12688 return dwp_file;
12689 }
12690
12691 /* Wrapper around open_and_init_dwp_file, only open it once. */
12692
12693 static struct dwp_file *
12694 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12695 {
12696 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12697 {
12698 dwarf2_per_objfile->per_bfd->dwp_file
12699 = open_and_init_dwp_file (dwarf2_per_objfile);
12700 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12701 }
12702 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12703 }
12704
12705 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12706 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12707 or in the DWP file for the objfile, referenced by THIS_UNIT.
12708 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12709 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12710
12711 This is called, for example, when wanting to read a variable with a
12712 complex location. Therefore we don't want to do file i/o for every call.
12713 Therefore we don't want to look for a DWO file on every call.
12714 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12715 then we check if we've already seen DWO_NAME, and only THEN do we check
12716 for a DWO file.
12717
12718 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12719 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12720
12721 static struct dwo_unit *
12722 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12723 ULONGEST signature, int is_debug_types)
12724 {
12725 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12726 struct objfile *objfile = dwarf2_per_objfile->objfile;
12727 const char *kind = is_debug_types ? "TU" : "CU";
12728 void **dwo_file_slot;
12729 struct dwo_file *dwo_file;
12730 struct dwp_file *dwp_file;
12731
12732 /* First see if there's a DWP file.
12733 If we have a DWP file but didn't find the DWO inside it, don't
12734 look for the original DWO file. It makes gdb behave differently
12735 depending on whether one is debugging in the build tree. */
12736
12737 dwp_file = get_dwp_file (dwarf2_per_objfile);
12738 if (dwp_file != NULL)
12739 {
12740 const struct dwp_hash_table *dwp_htab =
12741 is_debug_types ? dwp_file->tus : dwp_file->cus;
12742
12743 if (dwp_htab != NULL)
12744 {
12745 struct dwo_unit *dwo_cutu =
12746 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12747 signature, is_debug_types);
12748
12749 if (dwo_cutu != NULL)
12750 {
12751 if (dwarf_read_debug)
12752 {
12753 fprintf_unfiltered (gdb_stdlog,
12754 "Virtual DWO %s %s found: @%s\n",
12755 kind, hex_string (signature),
12756 host_address_to_string (dwo_cutu));
12757 }
12758 return dwo_cutu;
12759 }
12760 }
12761 }
12762 else
12763 {
12764 /* No DWP file, look for the DWO file. */
12765
12766 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12767 dwo_name, comp_dir);
12768 if (*dwo_file_slot == NULL)
12769 {
12770 /* Read in the file and build a table of the CUs/TUs it contains. */
12771 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12772 }
12773 /* NOTE: This will be NULL if unable to open the file. */
12774 dwo_file = (struct dwo_file *) *dwo_file_slot;
12775
12776 if (dwo_file != NULL)
12777 {
12778 struct dwo_unit *dwo_cutu = NULL;
12779
12780 if (is_debug_types && dwo_file->tus)
12781 {
12782 struct dwo_unit find_dwo_cutu;
12783
12784 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12785 find_dwo_cutu.signature = signature;
12786 dwo_cutu
12787 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12788 &find_dwo_cutu);
12789 }
12790 else if (!is_debug_types && dwo_file->cus)
12791 {
12792 struct dwo_unit find_dwo_cutu;
12793
12794 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12795 find_dwo_cutu.signature = signature;
12796 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12797 &find_dwo_cutu);
12798 }
12799
12800 if (dwo_cutu != NULL)
12801 {
12802 if (dwarf_read_debug)
12803 {
12804 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12805 kind, dwo_name, hex_string (signature),
12806 host_address_to_string (dwo_cutu));
12807 }
12808 return dwo_cutu;
12809 }
12810 }
12811 }
12812
12813 /* We didn't find it. This could mean a dwo_id mismatch, or
12814 someone deleted the DWO/DWP file, or the search path isn't set up
12815 correctly to find the file. */
12816
12817 if (dwarf_read_debug)
12818 {
12819 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12820 kind, dwo_name, hex_string (signature));
12821 }
12822
12823 /* This is a warning and not a complaint because it can be caused by
12824 pilot error (e.g., user accidentally deleting the DWO). */
12825 {
12826 /* Print the name of the DWP file if we looked there, helps the user
12827 better diagnose the problem. */
12828 std::string dwp_text;
12829
12830 if (dwp_file != NULL)
12831 dwp_text = string_printf (" [in DWP file %s]",
12832 lbasename (dwp_file->name));
12833
12834 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12835 " [in module %s]"),
12836 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12837 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12838 }
12839 return NULL;
12840 }
12841
12842 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12843 See lookup_dwo_cutu_unit for details. */
12844
12845 static struct dwo_unit *
12846 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12847 ULONGEST signature)
12848 {
12849 gdb_assert (!cu->per_cu->is_debug_types);
12850
12851 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12852 }
12853
12854 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12855 See lookup_dwo_cutu_unit for details. */
12856
12857 static struct dwo_unit *
12858 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12859 {
12860 gdb_assert (cu->per_cu->is_debug_types);
12861
12862 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12863
12864 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12865 }
12866
12867 /* Traversal function for queue_and_load_all_dwo_tus. */
12868
12869 static int
12870 queue_and_load_dwo_tu (void **slot, void *info)
12871 {
12872 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12873 dwarf2_cu *cu = (dwarf2_cu *) info;
12874 ULONGEST signature = dwo_unit->signature;
12875 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12876
12877 if (sig_type != NULL)
12878 {
12879 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12880
12881 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12882 a real dependency of PER_CU on SIG_TYPE. That is detected later
12883 while processing PER_CU. */
12884 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12885 load_full_type_unit (sig_cu, cu->per_objfile);
12886 cu->per_cu->imported_symtabs_push (sig_cu);
12887 }
12888
12889 return 1;
12890 }
12891
12892 /* Queue all TUs contained in the DWO of CU to be read in.
12893 The DWO may have the only definition of the type, though it may not be
12894 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12895 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12896
12897 static void
12898 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12899 {
12900 struct dwo_unit *dwo_unit;
12901 struct dwo_file *dwo_file;
12902
12903 gdb_assert (cu != nullptr);
12904 gdb_assert (!cu->per_cu->is_debug_types);
12905 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12906
12907 dwo_unit = cu->dwo_unit;
12908 gdb_assert (dwo_unit != NULL);
12909
12910 dwo_file = dwo_unit->dwo_file;
12911 if (dwo_file->tus != NULL)
12912 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12913 }
12914
12915 /* Read in various DIEs. */
12916
12917 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12918 Inherit only the children of the DW_AT_abstract_origin DIE not being
12919 already referenced by DW_AT_abstract_origin from the children of the
12920 current DIE. */
12921
12922 static void
12923 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12924 {
12925 struct die_info *child_die;
12926 sect_offset *offsetp;
12927 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12928 struct die_info *origin_die;
12929 /* Iterator of the ORIGIN_DIE children. */
12930 struct die_info *origin_child_die;
12931 struct attribute *attr;
12932 struct dwarf2_cu *origin_cu;
12933 struct pending **origin_previous_list_in_scope;
12934
12935 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12936 if (!attr)
12937 return;
12938
12939 /* Note that following die references may follow to a die in a
12940 different cu. */
12941
12942 origin_cu = cu;
12943 origin_die = follow_die_ref (die, attr, &origin_cu);
12944
12945 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12946 symbols in. */
12947 origin_previous_list_in_scope = origin_cu->list_in_scope;
12948 origin_cu->list_in_scope = cu->list_in_scope;
12949
12950 if (die->tag != origin_die->tag
12951 && !(die->tag == DW_TAG_inlined_subroutine
12952 && origin_die->tag == DW_TAG_subprogram))
12953 complaint (_("DIE %s and its abstract origin %s have different tags"),
12954 sect_offset_str (die->sect_off),
12955 sect_offset_str (origin_die->sect_off));
12956
12957 std::vector<sect_offset> offsets;
12958
12959 for (child_die = die->child;
12960 child_die && child_die->tag;
12961 child_die = child_die->sibling)
12962 {
12963 struct die_info *child_origin_die;
12964 struct dwarf2_cu *child_origin_cu;
12965
12966 /* We are trying to process concrete instance entries:
12967 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12968 it's not relevant to our analysis here. i.e. detecting DIEs that are
12969 present in the abstract instance but not referenced in the concrete
12970 one. */
12971 if (child_die->tag == DW_TAG_call_site
12972 || child_die->tag == DW_TAG_GNU_call_site)
12973 continue;
12974
12975 /* For each CHILD_DIE, find the corresponding child of
12976 ORIGIN_DIE. If there is more than one layer of
12977 DW_AT_abstract_origin, follow them all; there shouldn't be,
12978 but GCC versions at least through 4.4 generate this (GCC PR
12979 40573). */
12980 child_origin_die = child_die;
12981 child_origin_cu = cu;
12982 while (1)
12983 {
12984 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12985 child_origin_cu);
12986 if (attr == NULL)
12987 break;
12988 child_origin_die = follow_die_ref (child_origin_die, attr,
12989 &child_origin_cu);
12990 }
12991
12992 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12993 counterpart may exist. */
12994 if (child_origin_die != child_die)
12995 {
12996 if (child_die->tag != child_origin_die->tag
12997 && !(child_die->tag == DW_TAG_inlined_subroutine
12998 && child_origin_die->tag == DW_TAG_subprogram))
12999 complaint (_("Child DIE %s and its abstract origin %s have "
13000 "different tags"),
13001 sect_offset_str (child_die->sect_off),
13002 sect_offset_str (child_origin_die->sect_off));
13003 if (child_origin_die->parent != origin_die)
13004 complaint (_("Child DIE %s and its abstract origin %s have "
13005 "different parents"),
13006 sect_offset_str (child_die->sect_off),
13007 sect_offset_str (child_origin_die->sect_off));
13008 else
13009 offsets.push_back (child_origin_die->sect_off);
13010 }
13011 }
13012 std::sort (offsets.begin (), offsets.end ());
13013 sect_offset *offsets_end = offsets.data () + offsets.size ();
13014 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13015 if (offsetp[-1] == *offsetp)
13016 complaint (_("Multiple children of DIE %s refer "
13017 "to DIE %s as their abstract origin"),
13018 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13019
13020 offsetp = offsets.data ();
13021 origin_child_die = origin_die->child;
13022 while (origin_child_die && origin_child_die->tag)
13023 {
13024 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13025 while (offsetp < offsets_end
13026 && *offsetp < origin_child_die->sect_off)
13027 offsetp++;
13028 if (offsetp >= offsets_end
13029 || *offsetp > origin_child_die->sect_off)
13030 {
13031 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13032 Check whether we're already processing ORIGIN_CHILD_DIE.
13033 This can happen with mutually referenced abstract_origins.
13034 PR 16581. */
13035 if (!origin_child_die->in_process)
13036 process_die (origin_child_die, origin_cu);
13037 }
13038 origin_child_die = origin_child_die->sibling;
13039 }
13040 origin_cu->list_in_scope = origin_previous_list_in_scope;
13041
13042 if (cu != origin_cu)
13043 compute_delayed_physnames (origin_cu);
13044 }
13045
13046 static void
13047 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13048 {
13049 struct objfile *objfile = cu->per_objfile->objfile;
13050 struct gdbarch *gdbarch = objfile->arch ();
13051 struct context_stack *newobj;
13052 CORE_ADDR lowpc;
13053 CORE_ADDR highpc;
13054 struct die_info *child_die;
13055 struct attribute *attr, *call_line, *call_file;
13056 const char *name;
13057 CORE_ADDR baseaddr;
13058 struct block *block;
13059 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13060 std::vector<struct symbol *> template_args;
13061 struct template_symbol *templ_func = NULL;
13062
13063 if (inlined_func)
13064 {
13065 /* If we do not have call site information, we can't show the
13066 caller of this inlined function. That's too confusing, so
13067 only use the scope for local variables. */
13068 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13069 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13070 if (call_line == NULL || call_file == NULL)
13071 {
13072 read_lexical_block_scope (die, cu);
13073 return;
13074 }
13075 }
13076
13077 baseaddr = objfile->text_section_offset ();
13078
13079 name = dwarf2_name (die, cu);
13080
13081 /* Ignore functions with missing or empty names. These are actually
13082 illegal according to the DWARF standard. */
13083 if (name == NULL)
13084 {
13085 complaint (_("missing name for subprogram DIE at %s"),
13086 sect_offset_str (die->sect_off));
13087 return;
13088 }
13089
13090 /* Ignore functions with missing or invalid low and high pc attributes. */
13091 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13092 <= PC_BOUNDS_INVALID)
13093 {
13094 attr = dwarf2_attr (die, DW_AT_external, cu);
13095 if (!attr || !DW_UNSND (attr))
13096 complaint (_("cannot get low and high bounds "
13097 "for subprogram DIE at %s"),
13098 sect_offset_str (die->sect_off));
13099 return;
13100 }
13101
13102 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13103 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13104
13105 /* If we have any template arguments, then we must allocate a
13106 different sort of symbol. */
13107 for (child_die = die->child; child_die; child_die = child_die->sibling)
13108 {
13109 if (child_die->tag == DW_TAG_template_type_param
13110 || child_die->tag == DW_TAG_template_value_param)
13111 {
13112 templ_func = new (&objfile->objfile_obstack) template_symbol;
13113 templ_func->subclass = SYMBOL_TEMPLATE;
13114 break;
13115 }
13116 }
13117
13118 newobj = cu->get_builder ()->push_context (0, lowpc);
13119 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13120 (struct symbol *) templ_func);
13121
13122 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13123 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13124 cu->language);
13125
13126 /* If there is a location expression for DW_AT_frame_base, record
13127 it. */
13128 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13129 if (attr != nullptr)
13130 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13131
13132 /* If there is a location for the static link, record it. */
13133 newobj->static_link = NULL;
13134 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13135 if (attr != nullptr)
13136 {
13137 newobj->static_link
13138 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13139 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13140 cu->addr_type ());
13141 }
13142
13143 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13144
13145 if (die->child != NULL)
13146 {
13147 child_die = die->child;
13148 while (child_die && child_die->tag)
13149 {
13150 if (child_die->tag == DW_TAG_template_type_param
13151 || child_die->tag == DW_TAG_template_value_param)
13152 {
13153 struct symbol *arg = new_symbol (child_die, NULL, cu);
13154
13155 if (arg != NULL)
13156 template_args.push_back (arg);
13157 }
13158 else
13159 process_die (child_die, cu);
13160 child_die = child_die->sibling;
13161 }
13162 }
13163
13164 inherit_abstract_dies (die, cu);
13165
13166 /* If we have a DW_AT_specification, we might need to import using
13167 directives from the context of the specification DIE. See the
13168 comment in determine_prefix. */
13169 if (cu->language == language_cplus
13170 && dwarf2_attr (die, DW_AT_specification, cu))
13171 {
13172 struct dwarf2_cu *spec_cu = cu;
13173 struct die_info *spec_die = die_specification (die, &spec_cu);
13174
13175 while (spec_die)
13176 {
13177 child_die = spec_die->child;
13178 while (child_die && child_die->tag)
13179 {
13180 if (child_die->tag == DW_TAG_imported_module)
13181 process_die (child_die, spec_cu);
13182 child_die = child_die->sibling;
13183 }
13184
13185 /* In some cases, GCC generates specification DIEs that
13186 themselves contain DW_AT_specification attributes. */
13187 spec_die = die_specification (spec_die, &spec_cu);
13188 }
13189 }
13190
13191 struct context_stack cstk = cu->get_builder ()->pop_context ();
13192 /* Make a block for the local symbols within. */
13193 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13194 cstk.static_link, lowpc, highpc);
13195
13196 /* For C++, set the block's scope. */
13197 if ((cu->language == language_cplus
13198 || cu->language == language_fortran
13199 || cu->language == language_d
13200 || cu->language == language_rust)
13201 && cu->processing_has_namespace_info)
13202 block_set_scope (block, determine_prefix (die, cu),
13203 &objfile->objfile_obstack);
13204
13205 /* If we have address ranges, record them. */
13206 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13207
13208 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13209
13210 /* Attach template arguments to function. */
13211 if (!template_args.empty ())
13212 {
13213 gdb_assert (templ_func != NULL);
13214
13215 templ_func->n_template_arguments = template_args.size ();
13216 templ_func->template_arguments
13217 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13218 templ_func->n_template_arguments);
13219 memcpy (templ_func->template_arguments,
13220 template_args.data (),
13221 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13222
13223 /* Make sure that the symtab is set on the new symbols. Even
13224 though they don't appear in this symtab directly, other parts
13225 of gdb assume that symbols do, and this is reasonably
13226 true. */
13227 for (symbol *sym : template_args)
13228 symbol_set_symtab (sym, symbol_symtab (templ_func));
13229 }
13230
13231 /* In C++, we can have functions nested inside functions (e.g., when
13232 a function declares a class that has methods). This means that
13233 when we finish processing a function scope, we may need to go
13234 back to building a containing block's symbol lists. */
13235 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13236 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13237
13238 /* If we've finished processing a top-level function, subsequent
13239 symbols go in the file symbol list. */
13240 if (cu->get_builder ()->outermost_context_p ())
13241 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13242 }
13243
13244 /* Process all the DIES contained within a lexical block scope. Start
13245 a new scope, process the dies, and then close the scope. */
13246
13247 static void
13248 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13249 {
13250 struct objfile *objfile = cu->per_objfile->objfile;
13251 struct gdbarch *gdbarch = objfile->arch ();
13252 CORE_ADDR lowpc, highpc;
13253 struct die_info *child_die;
13254 CORE_ADDR baseaddr;
13255
13256 baseaddr = objfile->text_section_offset ();
13257
13258 /* Ignore blocks with missing or invalid low and high pc attributes. */
13259 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13260 as multiple lexical blocks? Handling children in a sane way would
13261 be nasty. Might be easier to properly extend generic blocks to
13262 describe ranges. */
13263 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13264 {
13265 case PC_BOUNDS_NOT_PRESENT:
13266 /* DW_TAG_lexical_block has no attributes, process its children as if
13267 there was no wrapping by that DW_TAG_lexical_block.
13268 GCC does no longer produces such DWARF since GCC r224161. */
13269 for (child_die = die->child;
13270 child_die != NULL && child_die->tag;
13271 child_die = child_die->sibling)
13272 {
13273 /* We might already be processing this DIE. This can happen
13274 in an unusual circumstance -- where a subroutine A
13275 appears lexically in another subroutine B, but A actually
13276 inlines B. The recursion is broken here, rather than in
13277 inherit_abstract_dies, because it seems better to simply
13278 drop concrete children here. */
13279 if (!child_die->in_process)
13280 process_die (child_die, cu);
13281 }
13282 return;
13283 case PC_BOUNDS_INVALID:
13284 return;
13285 }
13286 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13287 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13288
13289 cu->get_builder ()->push_context (0, lowpc);
13290 if (die->child != NULL)
13291 {
13292 child_die = die->child;
13293 while (child_die && child_die->tag)
13294 {
13295 process_die (child_die, cu);
13296 child_die = child_die->sibling;
13297 }
13298 }
13299 inherit_abstract_dies (die, cu);
13300 struct context_stack cstk = cu->get_builder ()->pop_context ();
13301
13302 if (*cu->get_builder ()->get_local_symbols () != NULL
13303 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13304 {
13305 struct block *block
13306 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13307 cstk.start_addr, highpc);
13308
13309 /* Note that recording ranges after traversing children, as we
13310 do here, means that recording a parent's ranges entails
13311 walking across all its children's ranges as they appear in
13312 the address map, which is quadratic behavior.
13313
13314 It would be nicer to record the parent's ranges before
13315 traversing its children, simply overriding whatever you find
13316 there. But since we don't even decide whether to create a
13317 block until after we've traversed its children, that's hard
13318 to do. */
13319 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13320 }
13321 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13322 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13323 }
13324
13325 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13326
13327 static void
13328 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13329 {
13330 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13331 struct objfile *objfile = per_objfile->objfile;
13332 struct gdbarch *gdbarch = objfile->arch ();
13333 CORE_ADDR pc, baseaddr;
13334 struct attribute *attr;
13335 struct call_site *call_site, call_site_local;
13336 void **slot;
13337 int nparams;
13338 struct die_info *child_die;
13339
13340 baseaddr = objfile->text_section_offset ();
13341
13342 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13343 if (attr == NULL)
13344 {
13345 /* This was a pre-DWARF-5 GNU extension alias
13346 for DW_AT_call_return_pc. */
13347 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13348 }
13349 if (!attr)
13350 {
13351 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13352 "DIE %s [in module %s]"),
13353 sect_offset_str (die->sect_off), objfile_name (objfile));
13354 return;
13355 }
13356 pc = attr->value_as_address () + baseaddr;
13357 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13358
13359 if (cu->call_site_htab == NULL)
13360 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13361 NULL, &objfile->objfile_obstack,
13362 hashtab_obstack_allocate, NULL);
13363 call_site_local.pc = pc;
13364 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13365 if (*slot != NULL)
13366 {
13367 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13368 "DIE %s [in module %s]"),
13369 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13370 objfile_name (objfile));
13371 return;
13372 }
13373
13374 /* Count parameters at the caller. */
13375
13376 nparams = 0;
13377 for (child_die = die->child; child_die && child_die->tag;
13378 child_die = child_die->sibling)
13379 {
13380 if (child_die->tag != DW_TAG_call_site_parameter
13381 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13382 {
13383 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13384 "DW_TAG_call_site child DIE %s [in module %s]"),
13385 child_die->tag, sect_offset_str (child_die->sect_off),
13386 objfile_name (objfile));
13387 continue;
13388 }
13389
13390 nparams++;
13391 }
13392
13393 call_site
13394 = ((struct call_site *)
13395 obstack_alloc (&objfile->objfile_obstack,
13396 sizeof (*call_site)
13397 + (sizeof (*call_site->parameter) * (nparams - 1))));
13398 *slot = call_site;
13399 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13400 call_site->pc = pc;
13401
13402 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13403 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13404 {
13405 struct die_info *func_die;
13406
13407 /* Skip also over DW_TAG_inlined_subroutine. */
13408 for (func_die = die->parent;
13409 func_die && func_die->tag != DW_TAG_subprogram
13410 && func_die->tag != DW_TAG_subroutine_type;
13411 func_die = func_die->parent);
13412
13413 /* DW_AT_call_all_calls is a superset
13414 of DW_AT_call_all_tail_calls. */
13415 if (func_die
13416 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13417 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13418 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13419 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13420 {
13421 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13422 not complete. But keep CALL_SITE for look ups via call_site_htab,
13423 both the initial caller containing the real return address PC and
13424 the final callee containing the current PC of a chain of tail
13425 calls do not need to have the tail call list complete. But any
13426 function candidate for a virtual tail call frame searched via
13427 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13428 determined unambiguously. */
13429 }
13430 else
13431 {
13432 struct type *func_type = NULL;
13433
13434 if (func_die)
13435 func_type = get_die_type (func_die, cu);
13436 if (func_type != NULL)
13437 {
13438 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13439
13440 /* Enlist this call site to the function. */
13441 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13442 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13443 }
13444 else
13445 complaint (_("Cannot find function owning DW_TAG_call_site "
13446 "DIE %s [in module %s]"),
13447 sect_offset_str (die->sect_off), objfile_name (objfile));
13448 }
13449 }
13450
13451 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13452 if (attr == NULL)
13453 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13454 if (attr == NULL)
13455 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13456 if (attr == NULL)
13457 {
13458 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13459 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13460 }
13461 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13462 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13463 /* Keep NULL DWARF_BLOCK. */;
13464 else if (attr->form_is_block ())
13465 {
13466 struct dwarf2_locexpr_baton *dlbaton;
13467
13468 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13469 dlbaton->data = DW_BLOCK (attr)->data;
13470 dlbaton->size = DW_BLOCK (attr)->size;
13471 dlbaton->per_objfile = per_objfile;
13472 dlbaton->per_cu = cu->per_cu;
13473
13474 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13475 }
13476 else if (attr->form_is_ref ())
13477 {
13478 struct dwarf2_cu *target_cu = cu;
13479 struct die_info *target_die;
13480
13481 target_die = follow_die_ref (die, attr, &target_cu);
13482 gdb_assert (target_cu->per_objfile->objfile == objfile);
13483 if (die_is_declaration (target_die, target_cu))
13484 {
13485 const char *target_physname;
13486
13487 /* Prefer the mangled name; otherwise compute the demangled one. */
13488 target_physname = dw2_linkage_name (target_die, target_cu);
13489 if (target_physname == NULL)
13490 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13491 if (target_physname == NULL)
13492 complaint (_("DW_AT_call_target target DIE has invalid "
13493 "physname, for referencing DIE %s [in module %s]"),
13494 sect_offset_str (die->sect_off), objfile_name (objfile));
13495 else
13496 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13497 }
13498 else
13499 {
13500 CORE_ADDR lowpc;
13501
13502 /* DW_AT_entry_pc should be preferred. */
13503 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13504 <= PC_BOUNDS_INVALID)
13505 complaint (_("DW_AT_call_target target DIE has invalid "
13506 "low pc, for referencing DIE %s [in module %s]"),
13507 sect_offset_str (die->sect_off), objfile_name (objfile));
13508 else
13509 {
13510 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13511 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13512 }
13513 }
13514 }
13515 else
13516 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13517 "block nor reference, for DIE %s [in module %s]"),
13518 sect_offset_str (die->sect_off), objfile_name (objfile));
13519
13520 call_site->per_cu = cu->per_cu;
13521 call_site->per_objfile = per_objfile;
13522
13523 for (child_die = die->child;
13524 child_die && child_die->tag;
13525 child_die = child_die->sibling)
13526 {
13527 struct call_site_parameter *parameter;
13528 struct attribute *loc, *origin;
13529
13530 if (child_die->tag != DW_TAG_call_site_parameter
13531 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13532 {
13533 /* Already printed the complaint above. */
13534 continue;
13535 }
13536
13537 gdb_assert (call_site->parameter_count < nparams);
13538 parameter = &call_site->parameter[call_site->parameter_count];
13539
13540 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13541 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13542 register is contained in DW_AT_call_value. */
13543
13544 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13545 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13546 if (origin == NULL)
13547 {
13548 /* This was a pre-DWARF-5 GNU extension alias
13549 for DW_AT_call_parameter. */
13550 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13551 }
13552 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13553 {
13554 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13555
13556 sect_offset sect_off = origin->get_ref_die_offset ();
13557 if (!cu->header.offset_in_cu_p (sect_off))
13558 {
13559 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13560 binding can be done only inside one CU. Such referenced DIE
13561 therefore cannot be even moved to DW_TAG_partial_unit. */
13562 complaint (_("DW_AT_call_parameter offset is not in CU for "
13563 "DW_TAG_call_site child DIE %s [in module %s]"),
13564 sect_offset_str (child_die->sect_off),
13565 objfile_name (objfile));
13566 continue;
13567 }
13568 parameter->u.param_cu_off
13569 = (cu_offset) (sect_off - cu->header.sect_off);
13570 }
13571 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13572 {
13573 complaint (_("No DW_FORM_block* DW_AT_location for "
13574 "DW_TAG_call_site child DIE %s [in module %s]"),
13575 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13576 continue;
13577 }
13578 else
13579 {
13580 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13581 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13582 if (parameter->u.dwarf_reg != -1)
13583 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13584 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13585 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13586 &parameter->u.fb_offset))
13587 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13588 else
13589 {
13590 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13591 "for DW_FORM_block* DW_AT_location is supported for "
13592 "DW_TAG_call_site child DIE %s "
13593 "[in module %s]"),
13594 sect_offset_str (child_die->sect_off),
13595 objfile_name (objfile));
13596 continue;
13597 }
13598 }
13599
13600 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13601 if (attr == NULL)
13602 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13603 if (attr == NULL || !attr->form_is_block ())
13604 {
13605 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13606 "DW_TAG_call_site child DIE %s [in module %s]"),
13607 sect_offset_str (child_die->sect_off),
13608 objfile_name (objfile));
13609 continue;
13610 }
13611 parameter->value = DW_BLOCK (attr)->data;
13612 parameter->value_size = DW_BLOCK (attr)->size;
13613
13614 /* Parameters are not pre-cleared by memset above. */
13615 parameter->data_value = NULL;
13616 parameter->data_value_size = 0;
13617 call_site->parameter_count++;
13618
13619 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13620 if (attr == NULL)
13621 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13622 if (attr != nullptr)
13623 {
13624 if (!attr->form_is_block ())
13625 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13626 "DW_TAG_call_site child DIE %s [in module %s]"),
13627 sect_offset_str (child_die->sect_off),
13628 objfile_name (objfile));
13629 else
13630 {
13631 parameter->data_value = DW_BLOCK (attr)->data;
13632 parameter->data_value_size = DW_BLOCK (attr)->size;
13633 }
13634 }
13635 }
13636 }
13637
13638 /* Helper function for read_variable. If DIE represents a virtual
13639 table, then return the type of the concrete object that is
13640 associated with the virtual table. Otherwise, return NULL. */
13641
13642 static struct type *
13643 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13644 {
13645 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13646 if (attr == NULL)
13647 return NULL;
13648
13649 /* Find the type DIE. */
13650 struct die_info *type_die = NULL;
13651 struct dwarf2_cu *type_cu = cu;
13652
13653 if (attr->form_is_ref ())
13654 type_die = follow_die_ref (die, attr, &type_cu);
13655 if (type_die == NULL)
13656 return NULL;
13657
13658 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13659 return NULL;
13660 return die_containing_type (type_die, type_cu);
13661 }
13662
13663 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13664
13665 static void
13666 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13667 {
13668 struct rust_vtable_symbol *storage = NULL;
13669
13670 if (cu->language == language_rust)
13671 {
13672 struct type *containing_type = rust_containing_type (die, cu);
13673
13674 if (containing_type != NULL)
13675 {
13676 struct objfile *objfile = cu->per_objfile->objfile;
13677
13678 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13679 storage->concrete_type = containing_type;
13680 storage->subclass = SYMBOL_RUST_VTABLE;
13681 }
13682 }
13683
13684 struct symbol *res = new_symbol (die, NULL, cu, storage);
13685 struct attribute *abstract_origin
13686 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13687 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13688 if (res == NULL && loc && abstract_origin)
13689 {
13690 /* We have a variable without a name, but with a location and an abstract
13691 origin. This may be a concrete instance of an abstract variable
13692 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13693 later. */
13694 struct dwarf2_cu *origin_cu = cu;
13695 struct die_info *origin_die
13696 = follow_die_ref (die, abstract_origin, &origin_cu);
13697 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13698 per_objfile->per_bfd->abstract_to_concrete
13699 [origin_die->sect_off].push_back (die->sect_off);
13700 }
13701 }
13702
13703 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13704 reading .debug_rnglists.
13705 Callback's type should be:
13706 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13707 Return true if the attributes are present and valid, otherwise,
13708 return false. */
13709
13710 template <typename Callback>
13711 static bool
13712 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13713 Callback &&callback)
13714 {
13715 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13716 struct objfile *objfile = dwarf2_per_objfile->objfile;
13717 bfd *obfd = objfile->obfd;
13718 /* Base address selection entry. */
13719 gdb::optional<CORE_ADDR> base;
13720 const gdb_byte *buffer;
13721 CORE_ADDR baseaddr;
13722 bool overflow = false;
13723
13724 base = cu->base_address;
13725
13726 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13727 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13728 {
13729 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13730 offset);
13731 return false;
13732 }
13733 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13734
13735 baseaddr = objfile->text_section_offset ();
13736
13737 while (1)
13738 {
13739 /* Initialize it due to a false compiler warning. */
13740 CORE_ADDR range_beginning = 0, range_end = 0;
13741 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13742 + dwarf2_per_objfile->per_bfd->rnglists.size);
13743 unsigned int bytes_read;
13744
13745 if (buffer == buf_end)
13746 {
13747 overflow = true;
13748 break;
13749 }
13750 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13751 switch (rlet)
13752 {
13753 case DW_RLE_end_of_list:
13754 break;
13755 case DW_RLE_base_address:
13756 if (buffer + cu->header.addr_size > buf_end)
13757 {
13758 overflow = true;
13759 break;
13760 }
13761 base = cu->header.read_address (obfd, buffer, &bytes_read);
13762 buffer += bytes_read;
13763 break;
13764 case DW_RLE_start_length:
13765 if (buffer + cu->header.addr_size > buf_end)
13766 {
13767 overflow = true;
13768 break;
13769 }
13770 range_beginning = cu->header.read_address (obfd, buffer,
13771 &bytes_read);
13772 buffer += bytes_read;
13773 range_end = (range_beginning
13774 + 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_offset_pair:
13783 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13784 buffer += bytes_read;
13785 if (buffer > buf_end)
13786 {
13787 overflow = true;
13788 break;
13789 }
13790 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13791 buffer += bytes_read;
13792 if (buffer > buf_end)
13793 {
13794 overflow = true;
13795 break;
13796 }
13797 break;
13798 case DW_RLE_start_end:
13799 if (buffer + 2 * cu->header.addr_size > buf_end)
13800 {
13801 overflow = true;
13802 break;
13803 }
13804 range_beginning = cu->header.read_address (obfd, buffer,
13805 &bytes_read);
13806 buffer += bytes_read;
13807 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13808 buffer += bytes_read;
13809 break;
13810 default:
13811 complaint (_("Invalid .debug_rnglists data (no base address)"));
13812 return false;
13813 }
13814 if (rlet == DW_RLE_end_of_list || overflow)
13815 break;
13816 if (rlet == DW_RLE_base_address)
13817 continue;
13818
13819 if (!base.has_value ())
13820 {
13821 /* We have no valid base address for the ranges
13822 data. */
13823 complaint (_("Invalid .debug_rnglists data (no base address)"));
13824 return false;
13825 }
13826
13827 if (range_beginning > range_end)
13828 {
13829 /* Inverted range entries are invalid. */
13830 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13831 return false;
13832 }
13833
13834 /* Empty range entries have no effect. */
13835 if (range_beginning == range_end)
13836 continue;
13837
13838 range_beginning += *base;
13839 range_end += *base;
13840
13841 /* A not-uncommon case of bad debug info.
13842 Don't pollute the addrmap with bad data. */
13843 if (range_beginning + baseaddr == 0
13844 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13845 {
13846 complaint (_(".debug_rnglists entry has start address of zero"
13847 " [in module %s]"), objfile_name (objfile));
13848 continue;
13849 }
13850
13851 callback (range_beginning, range_end);
13852 }
13853
13854 if (overflow)
13855 {
13856 complaint (_("Offset %d is not terminated "
13857 "for DW_AT_ranges attribute"),
13858 offset);
13859 return false;
13860 }
13861
13862 return true;
13863 }
13864
13865 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13866 Callback's type should be:
13867 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13868 Return 1 if the attributes are present and valid, otherwise, return 0. */
13869
13870 template <typename Callback>
13871 static int
13872 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13873 Callback &&callback)
13874 {
13875 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13876 struct objfile *objfile = per_objfile->objfile;
13877 struct comp_unit_head *cu_header = &cu->header;
13878 bfd *obfd = objfile->obfd;
13879 unsigned int addr_size = cu_header->addr_size;
13880 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13881 /* Base address selection entry. */
13882 gdb::optional<CORE_ADDR> base;
13883 unsigned int dummy;
13884 const gdb_byte *buffer;
13885 CORE_ADDR baseaddr;
13886
13887 if (cu_header->version >= 5)
13888 return dwarf2_rnglists_process (offset, cu, callback);
13889
13890 base = cu->base_address;
13891
13892 per_objfile->per_bfd->ranges.read (objfile);
13893 if (offset >= per_objfile->per_bfd->ranges.size)
13894 {
13895 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13896 offset);
13897 return 0;
13898 }
13899 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13900
13901 baseaddr = objfile->text_section_offset ();
13902
13903 while (1)
13904 {
13905 CORE_ADDR range_beginning, range_end;
13906
13907 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13908 buffer += addr_size;
13909 range_end = cu->header.read_address (obfd, buffer, &dummy);
13910 buffer += addr_size;
13911 offset += 2 * addr_size;
13912
13913 /* An end of list marker is a pair of zero addresses. */
13914 if (range_beginning == 0 && range_end == 0)
13915 /* Found the end of list entry. */
13916 break;
13917
13918 /* Each base address selection entry is a pair of 2 values.
13919 The first is the largest possible address, the second is
13920 the base address. Check for a base address here. */
13921 if ((range_beginning & mask) == mask)
13922 {
13923 /* If we found the largest possible address, then we already
13924 have the base address in range_end. */
13925 base = range_end;
13926 continue;
13927 }
13928
13929 if (!base.has_value ())
13930 {
13931 /* We have no valid base address for the ranges
13932 data. */
13933 complaint (_("Invalid .debug_ranges data (no base address)"));
13934 return 0;
13935 }
13936
13937 if (range_beginning > range_end)
13938 {
13939 /* Inverted range entries are invalid. */
13940 complaint (_("Invalid .debug_ranges data (inverted range)"));
13941 return 0;
13942 }
13943
13944 /* Empty range entries have no effect. */
13945 if (range_beginning == range_end)
13946 continue;
13947
13948 range_beginning += *base;
13949 range_end += *base;
13950
13951 /* A not-uncommon case of bad debug info.
13952 Don't pollute the addrmap with bad data. */
13953 if (range_beginning + baseaddr == 0
13954 && !per_objfile->per_bfd->has_section_at_zero)
13955 {
13956 complaint (_(".debug_ranges entry has start address of zero"
13957 " [in module %s]"), objfile_name (objfile));
13958 continue;
13959 }
13960
13961 callback (range_beginning, range_end);
13962 }
13963
13964 return 1;
13965 }
13966
13967 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13968 Return 1 if the attributes are present and valid, otherwise, return 0.
13969 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13970
13971 static int
13972 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13973 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13974 dwarf2_psymtab *ranges_pst)
13975 {
13976 struct objfile *objfile = cu->per_objfile->objfile;
13977 struct gdbarch *gdbarch = objfile->arch ();
13978 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13979 int low_set = 0;
13980 CORE_ADDR low = 0;
13981 CORE_ADDR high = 0;
13982 int retval;
13983
13984 retval = dwarf2_ranges_process (offset, cu,
13985 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13986 {
13987 if (ranges_pst != NULL)
13988 {
13989 CORE_ADDR lowpc;
13990 CORE_ADDR highpc;
13991
13992 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13993 range_beginning + baseaddr)
13994 - baseaddr);
13995 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13996 range_end + baseaddr)
13997 - baseaddr);
13998 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13999 lowpc, highpc - 1, ranges_pst);
14000 }
14001
14002 /* FIXME: This is recording everything as a low-high
14003 segment of consecutive addresses. We should have a
14004 data structure for discontiguous block ranges
14005 instead. */
14006 if (! low_set)
14007 {
14008 low = range_beginning;
14009 high = range_end;
14010 low_set = 1;
14011 }
14012 else
14013 {
14014 if (range_beginning < low)
14015 low = range_beginning;
14016 if (range_end > high)
14017 high = range_end;
14018 }
14019 });
14020 if (!retval)
14021 return 0;
14022
14023 if (! low_set)
14024 /* If the first entry is an end-of-list marker, the range
14025 describes an empty scope, i.e. no instructions. */
14026 return 0;
14027
14028 if (low_return)
14029 *low_return = low;
14030 if (high_return)
14031 *high_return = high;
14032 return 1;
14033 }
14034
14035 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14036 definition for the return value. *LOWPC and *HIGHPC are set iff
14037 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14038
14039 static enum pc_bounds_kind
14040 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14041 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14042 dwarf2_psymtab *pst)
14043 {
14044 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14045 struct attribute *attr;
14046 struct attribute *attr_high;
14047 CORE_ADDR low = 0;
14048 CORE_ADDR high = 0;
14049 enum pc_bounds_kind ret;
14050
14051 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14052 if (attr_high)
14053 {
14054 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14055 if (attr != nullptr)
14056 {
14057 low = attr->value_as_address ();
14058 high = attr_high->value_as_address ();
14059 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14060 high += low;
14061 }
14062 else
14063 /* Found high w/o low attribute. */
14064 return PC_BOUNDS_INVALID;
14065
14066 /* Found consecutive range of addresses. */
14067 ret = PC_BOUNDS_HIGH_LOW;
14068 }
14069 else
14070 {
14071 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14072 if (attr != NULL)
14073 {
14074 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14075 We take advantage of the fact that DW_AT_ranges does not appear
14076 in DW_TAG_compile_unit of DWO files. */
14077 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14078 unsigned int ranges_offset = (DW_UNSND (attr)
14079 + (need_ranges_base
14080 ? cu->ranges_base
14081 : 0));
14082
14083 /* Value of the DW_AT_ranges attribute is the offset in the
14084 .debug_ranges section. */
14085 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14086 return PC_BOUNDS_INVALID;
14087 /* Found discontinuous range of addresses. */
14088 ret = PC_BOUNDS_RANGES;
14089 }
14090 else
14091 return PC_BOUNDS_NOT_PRESENT;
14092 }
14093
14094 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14095 if (high <= low)
14096 return PC_BOUNDS_INVALID;
14097
14098 /* When using the GNU linker, .gnu.linkonce. sections are used to
14099 eliminate duplicate copies of functions and vtables and such.
14100 The linker will arbitrarily choose one and discard the others.
14101 The AT_*_pc values for such functions refer to local labels in
14102 these sections. If the section from that file was discarded, the
14103 labels are not in the output, so the relocs get a value of 0.
14104 If this is a discarded function, mark the pc bounds as invalid,
14105 so that GDB will ignore it. */
14106 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14107 return PC_BOUNDS_INVALID;
14108
14109 *lowpc = low;
14110 if (highpc)
14111 *highpc = high;
14112 return ret;
14113 }
14114
14115 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14116 its low and high PC addresses. Do nothing if these addresses could not
14117 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14118 and HIGHPC to the high address if greater than HIGHPC. */
14119
14120 static void
14121 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14122 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14123 struct dwarf2_cu *cu)
14124 {
14125 CORE_ADDR low, high;
14126 struct die_info *child = die->child;
14127
14128 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14129 {
14130 *lowpc = std::min (*lowpc, low);
14131 *highpc = std::max (*highpc, high);
14132 }
14133
14134 /* If the language does not allow nested subprograms (either inside
14135 subprograms or lexical blocks), we're done. */
14136 if (cu->language != language_ada)
14137 return;
14138
14139 /* Check all the children of the given DIE. If it contains nested
14140 subprograms, then check their pc bounds. Likewise, we need to
14141 check lexical blocks as well, as they may also contain subprogram
14142 definitions. */
14143 while (child && child->tag)
14144 {
14145 if (child->tag == DW_TAG_subprogram
14146 || child->tag == DW_TAG_lexical_block)
14147 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14148 child = child->sibling;
14149 }
14150 }
14151
14152 /* Get the low and high pc's represented by the scope DIE, and store
14153 them in *LOWPC and *HIGHPC. If the correct values can't be
14154 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14155
14156 static void
14157 get_scope_pc_bounds (struct die_info *die,
14158 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14159 struct dwarf2_cu *cu)
14160 {
14161 CORE_ADDR best_low = (CORE_ADDR) -1;
14162 CORE_ADDR best_high = (CORE_ADDR) 0;
14163 CORE_ADDR current_low, current_high;
14164
14165 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14166 >= PC_BOUNDS_RANGES)
14167 {
14168 best_low = current_low;
14169 best_high = current_high;
14170 }
14171 else
14172 {
14173 struct die_info *child = die->child;
14174
14175 while (child && child->tag)
14176 {
14177 switch (child->tag) {
14178 case DW_TAG_subprogram:
14179 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14180 break;
14181 case DW_TAG_namespace:
14182 case DW_TAG_module:
14183 /* FIXME: carlton/2004-01-16: Should we do this for
14184 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14185 that current GCC's always emit the DIEs corresponding
14186 to definitions of methods of classes as children of a
14187 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14188 the DIEs giving the declarations, which could be
14189 anywhere). But I don't see any reason why the
14190 standards says that they have to be there. */
14191 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14192
14193 if (current_low != ((CORE_ADDR) -1))
14194 {
14195 best_low = std::min (best_low, current_low);
14196 best_high = std::max (best_high, current_high);
14197 }
14198 break;
14199 default:
14200 /* Ignore. */
14201 break;
14202 }
14203
14204 child = child->sibling;
14205 }
14206 }
14207
14208 *lowpc = best_low;
14209 *highpc = best_high;
14210 }
14211
14212 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14213 in DIE. */
14214
14215 static void
14216 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14217 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14218 {
14219 struct objfile *objfile = cu->per_objfile->objfile;
14220 struct gdbarch *gdbarch = objfile->arch ();
14221 struct attribute *attr;
14222 struct attribute *attr_high;
14223
14224 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14225 if (attr_high)
14226 {
14227 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14228 if (attr != nullptr)
14229 {
14230 CORE_ADDR low = attr->value_as_address ();
14231 CORE_ADDR high = attr_high->value_as_address ();
14232
14233 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14234 high += low;
14235
14236 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14237 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14238 cu->get_builder ()->record_block_range (block, low, high - 1);
14239 }
14240 }
14241
14242 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14243 if (attr != nullptr)
14244 {
14245 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14246 We take advantage of the fact that DW_AT_ranges does not appear
14247 in DW_TAG_compile_unit of DWO files. */
14248 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14249
14250 /* The value of the DW_AT_ranges attribute is the offset of the
14251 address range list in the .debug_ranges section. */
14252 unsigned long offset = (DW_UNSND (attr)
14253 + (need_ranges_base ? cu->ranges_base : 0));
14254
14255 std::vector<blockrange> blockvec;
14256 dwarf2_ranges_process (offset, cu,
14257 [&] (CORE_ADDR start, CORE_ADDR end)
14258 {
14259 start += baseaddr;
14260 end += baseaddr;
14261 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14262 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14263 cu->get_builder ()->record_block_range (block, start, end - 1);
14264 blockvec.emplace_back (start, end);
14265 });
14266
14267 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14268 }
14269 }
14270
14271 /* Check whether the producer field indicates either of GCC < 4.6, or the
14272 Intel C/C++ compiler, and cache the result in CU. */
14273
14274 static void
14275 check_producer (struct dwarf2_cu *cu)
14276 {
14277 int major, minor;
14278
14279 if (cu->producer == NULL)
14280 {
14281 /* For unknown compilers expect their behavior is DWARF version
14282 compliant.
14283
14284 GCC started to support .debug_types sections by -gdwarf-4 since
14285 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14286 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14287 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14288 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14289 }
14290 else if (producer_is_gcc (cu->producer, &major, &minor))
14291 {
14292 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14293 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14294 }
14295 else if (producer_is_icc (cu->producer, &major, &minor))
14296 {
14297 cu->producer_is_icc = true;
14298 cu->producer_is_icc_lt_14 = major < 14;
14299 }
14300 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14301 cu->producer_is_codewarrior = true;
14302 else
14303 {
14304 /* For other non-GCC compilers, expect their behavior is DWARF version
14305 compliant. */
14306 }
14307
14308 cu->checked_producer = true;
14309 }
14310
14311 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14312 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14313 during 4.6.0 experimental. */
14314
14315 static bool
14316 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14317 {
14318 if (!cu->checked_producer)
14319 check_producer (cu);
14320
14321 return cu->producer_is_gxx_lt_4_6;
14322 }
14323
14324
14325 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14326 with incorrect is_stmt attributes. */
14327
14328 static bool
14329 producer_is_codewarrior (struct dwarf2_cu *cu)
14330 {
14331 if (!cu->checked_producer)
14332 check_producer (cu);
14333
14334 return cu->producer_is_codewarrior;
14335 }
14336
14337 /* Return the default accessibility type if it is not overridden by
14338 DW_AT_accessibility. */
14339
14340 static enum dwarf_access_attribute
14341 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14342 {
14343 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14344 {
14345 /* The default DWARF 2 accessibility for members is public, the default
14346 accessibility for inheritance is private. */
14347
14348 if (die->tag != DW_TAG_inheritance)
14349 return DW_ACCESS_public;
14350 else
14351 return DW_ACCESS_private;
14352 }
14353 else
14354 {
14355 /* DWARF 3+ defines the default accessibility a different way. The same
14356 rules apply now for DW_TAG_inheritance as for the members and it only
14357 depends on the container kind. */
14358
14359 if (die->parent->tag == DW_TAG_class_type)
14360 return DW_ACCESS_private;
14361 else
14362 return DW_ACCESS_public;
14363 }
14364 }
14365
14366 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14367 offset. If the attribute was not found return 0, otherwise return
14368 1. If it was found but could not properly be handled, set *OFFSET
14369 to 0. */
14370
14371 static int
14372 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14373 LONGEST *offset)
14374 {
14375 struct attribute *attr;
14376
14377 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14378 if (attr != NULL)
14379 {
14380 *offset = 0;
14381
14382 /* Note that we do not check for a section offset first here.
14383 This is because DW_AT_data_member_location is new in DWARF 4,
14384 so if we see it, we can assume that a constant form is really
14385 a constant and not a section offset. */
14386 if (attr->form_is_constant ())
14387 *offset = attr->constant_value (0);
14388 else if (attr->form_is_section_offset ())
14389 dwarf2_complex_location_expr_complaint ();
14390 else if (attr->form_is_block ())
14391 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14392 else
14393 dwarf2_complex_location_expr_complaint ();
14394
14395 return 1;
14396 }
14397
14398 return 0;
14399 }
14400
14401 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14402
14403 static void
14404 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14405 struct field *field)
14406 {
14407 struct attribute *attr;
14408
14409 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14410 if (attr != NULL)
14411 {
14412 if (attr->form_is_constant ())
14413 {
14414 LONGEST offset = attr->constant_value (0);
14415 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14416 }
14417 else if (attr->form_is_section_offset ())
14418 dwarf2_complex_location_expr_complaint ();
14419 else if (attr->form_is_block ())
14420 {
14421 bool handled;
14422 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14423 if (handled)
14424 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14425 else
14426 {
14427 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14428 struct objfile *objfile = per_objfile->objfile;
14429 struct dwarf2_locexpr_baton *dlbaton
14430 = XOBNEW (&objfile->objfile_obstack,
14431 struct dwarf2_locexpr_baton);
14432 dlbaton->data = DW_BLOCK (attr)->data;
14433 dlbaton->size = DW_BLOCK (attr)->size;
14434 /* When using this baton, we want to compute the address
14435 of the field, not the value. This is why
14436 is_reference is set to false here. */
14437 dlbaton->is_reference = false;
14438 dlbaton->per_objfile = per_objfile;
14439 dlbaton->per_cu = cu->per_cu;
14440
14441 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14442 }
14443 }
14444 else
14445 dwarf2_complex_location_expr_complaint ();
14446 }
14447 }
14448
14449 /* Add an aggregate field to the field list. */
14450
14451 static void
14452 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14453 struct dwarf2_cu *cu)
14454 {
14455 struct objfile *objfile = cu->per_objfile->objfile;
14456 struct gdbarch *gdbarch = objfile->arch ();
14457 struct nextfield *new_field;
14458 struct attribute *attr;
14459 struct field *fp;
14460 const char *fieldname = "";
14461
14462 if (die->tag == DW_TAG_inheritance)
14463 {
14464 fip->baseclasses.emplace_back ();
14465 new_field = &fip->baseclasses.back ();
14466 }
14467 else
14468 {
14469 fip->fields.emplace_back ();
14470 new_field = &fip->fields.back ();
14471 }
14472
14473 new_field->offset = die->sect_off;
14474
14475 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14476 if (attr != nullptr)
14477 new_field->accessibility = DW_UNSND (attr);
14478 else
14479 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14480 if (new_field->accessibility != DW_ACCESS_public)
14481 fip->non_public_fields = 1;
14482
14483 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14484 if (attr != nullptr)
14485 new_field->virtuality = DW_UNSND (attr);
14486 else
14487 new_field->virtuality = DW_VIRTUALITY_none;
14488
14489 fp = &new_field->field;
14490
14491 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14492 {
14493 /* Data member other than a C++ static data member. */
14494
14495 /* Get type of field. */
14496 fp->type = die_type (die, cu);
14497
14498 SET_FIELD_BITPOS (*fp, 0);
14499
14500 /* Get bit size of field (zero if none). */
14501 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14502 if (attr != nullptr)
14503 {
14504 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14505 }
14506 else
14507 {
14508 FIELD_BITSIZE (*fp) = 0;
14509 }
14510
14511 /* Get bit offset of field. */
14512 handle_data_member_location (die, cu, fp);
14513 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14514 if (attr != nullptr)
14515 {
14516 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14517 {
14518 /* For big endian bits, the DW_AT_bit_offset gives the
14519 additional bit offset from the MSB of the containing
14520 anonymous object to the MSB of the field. We don't
14521 have to do anything special since we don't need to
14522 know the size of the anonymous object. */
14523 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14524 }
14525 else
14526 {
14527 /* For little endian bits, compute the bit offset to the
14528 MSB of the anonymous object, subtract off the number of
14529 bits from the MSB of the field to the MSB of the
14530 object, and then subtract off the number of bits of
14531 the field itself. The result is the bit offset of
14532 the LSB of the field. */
14533 int anonymous_size;
14534 int bit_offset = DW_UNSND (attr);
14535
14536 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14537 if (attr != nullptr)
14538 {
14539 /* The size of the anonymous object containing
14540 the bit field is explicit, so use the
14541 indicated size (in bytes). */
14542 anonymous_size = DW_UNSND (attr);
14543 }
14544 else
14545 {
14546 /* The size of the anonymous object containing
14547 the bit field must be inferred from the type
14548 attribute of the data member containing the
14549 bit field. */
14550 anonymous_size = TYPE_LENGTH (fp->type);
14551 }
14552 SET_FIELD_BITPOS (*fp,
14553 (FIELD_BITPOS (*fp)
14554 + anonymous_size * bits_per_byte
14555 - bit_offset - FIELD_BITSIZE (*fp)));
14556 }
14557 }
14558 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14559 if (attr != NULL)
14560 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14561 + attr->constant_value (0)));
14562
14563 /* Get name of field. */
14564 fieldname = dwarf2_name (die, cu);
14565 if (fieldname == NULL)
14566 fieldname = "";
14567
14568 /* The name is already allocated along with this objfile, so we don't
14569 need to duplicate it for the type. */
14570 fp->name = fieldname;
14571
14572 /* Change accessibility for artificial fields (e.g. virtual table
14573 pointer or virtual base class pointer) to private. */
14574 if (dwarf2_attr (die, DW_AT_artificial, cu))
14575 {
14576 FIELD_ARTIFICIAL (*fp) = 1;
14577 new_field->accessibility = DW_ACCESS_private;
14578 fip->non_public_fields = 1;
14579 }
14580 }
14581 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14582 {
14583 /* C++ static member. */
14584
14585 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14586 is a declaration, but all versions of G++ as of this writing
14587 (so through at least 3.2.1) incorrectly generate
14588 DW_TAG_variable tags. */
14589
14590 const char *physname;
14591
14592 /* Get name of field. */
14593 fieldname = dwarf2_name (die, cu);
14594 if (fieldname == NULL)
14595 return;
14596
14597 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14598 if (attr
14599 /* Only create a symbol if this is an external value.
14600 new_symbol checks this and puts the value in the global symbol
14601 table, which we want. If it is not external, new_symbol
14602 will try to put the value in cu->list_in_scope which is wrong. */
14603 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14604 {
14605 /* A static const member, not much different than an enum as far as
14606 we're concerned, except that we can support more types. */
14607 new_symbol (die, NULL, cu);
14608 }
14609
14610 /* Get physical name. */
14611 physname = dwarf2_physname (fieldname, die, cu);
14612
14613 /* The name is already allocated along with this objfile, so we don't
14614 need to duplicate it for the type. */
14615 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14616 FIELD_TYPE (*fp) = die_type (die, cu);
14617 FIELD_NAME (*fp) = fieldname;
14618 }
14619 else if (die->tag == DW_TAG_inheritance)
14620 {
14621 /* C++ base class field. */
14622 handle_data_member_location (die, cu, fp);
14623 FIELD_BITSIZE (*fp) = 0;
14624 FIELD_TYPE (*fp) = die_type (die, cu);
14625 FIELD_NAME (*fp) = fp->type->name ();
14626 }
14627 else
14628 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14629 }
14630
14631 /* Can the type given by DIE define another type? */
14632
14633 static bool
14634 type_can_define_types (const struct die_info *die)
14635 {
14636 switch (die->tag)
14637 {
14638 case DW_TAG_typedef:
14639 case DW_TAG_class_type:
14640 case DW_TAG_structure_type:
14641 case DW_TAG_union_type:
14642 case DW_TAG_enumeration_type:
14643 return true;
14644
14645 default:
14646 return false;
14647 }
14648 }
14649
14650 /* Add a type definition defined in the scope of the FIP's class. */
14651
14652 static void
14653 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14654 struct dwarf2_cu *cu)
14655 {
14656 struct decl_field fp;
14657 memset (&fp, 0, sizeof (fp));
14658
14659 gdb_assert (type_can_define_types (die));
14660
14661 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14662 fp.name = dwarf2_name (die, cu);
14663 fp.type = read_type_die (die, cu);
14664
14665 /* Save accessibility. */
14666 enum dwarf_access_attribute accessibility;
14667 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14668 if (attr != NULL)
14669 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14670 else
14671 accessibility = dwarf2_default_access_attribute (die, cu);
14672 switch (accessibility)
14673 {
14674 case DW_ACCESS_public:
14675 /* The assumed value if neither private nor protected. */
14676 break;
14677 case DW_ACCESS_private:
14678 fp.is_private = 1;
14679 break;
14680 case DW_ACCESS_protected:
14681 fp.is_protected = 1;
14682 break;
14683 default:
14684 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14685 }
14686
14687 if (die->tag == DW_TAG_typedef)
14688 fip->typedef_field_list.push_back (fp);
14689 else
14690 fip->nested_types_list.push_back (fp);
14691 }
14692
14693 /* A convenience typedef that's used when finding the discriminant
14694 field for a variant part. */
14695 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14696 offset_map_type;
14697
14698 /* Compute the discriminant range for a given variant. OBSTACK is
14699 where the results will be stored. VARIANT is the variant to
14700 process. IS_UNSIGNED indicates whether the discriminant is signed
14701 or unsigned. */
14702
14703 static const gdb::array_view<discriminant_range>
14704 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14705 bool is_unsigned)
14706 {
14707 std::vector<discriminant_range> ranges;
14708
14709 if (variant.default_branch)
14710 return {};
14711
14712 if (variant.discr_list_data == nullptr)
14713 {
14714 discriminant_range r
14715 = {variant.discriminant_value, variant.discriminant_value};
14716 ranges.push_back (r);
14717 }
14718 else
14719 {
14720 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14721 variant.discr_list_data->size);
14722 while (!data.empty ())
14723 {
14724 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14725 {
14726 complaint (_("invalid discriminant marker: %d"), data[0]);
14727 break;
14728 }
14729 bool is_range = data[0] == DW_DSC_range;
14730 data = data.slice (1);
14731
14732 ULONGEST low, high;
14733 unsigned int bytes_read;
14734
14735 if (data.empty ())
14736 {
14737 complaint (_("DW_AT_discr_list missing low value"));
14738 break;
14739 }
14740 if (is_unsigned)
14741 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14742 else
14743 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14744 &bytes_read);
14745 data = data.slice (bytes_read);
14746
14747 if (is_range)
14748 {
14749 if (data.empty ())
14750 {
14751 complaint (_("DW_AT_discr_list missing high value"));
14752 break;
14753 }
14754 if (is_unsigned)
14755 high = read_unsigned_leb128 (nullptr, data.data (),
14756 &bytes_read);
14757 else
14758 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14759 &bytes_read);
14760 data = data.slice (bytes_read);
14761 }
14762 else
14763 high = low;
14764
14765 ranges.push_back ({ low, high });
14766 }
14767 }
14768
14769 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14770 ranges.size ());
14771 std::copy (ranges.begin (), ranges.end (), result);
14772 return gdb::array_view<discriminant_range> (result, ranges.size ());
14773 }
14774
14775 static const gdb::array_view<variant_part> create_variant_parts
14776 (struct obstack *obstack,
14777 const offset_map_type &offset_map,
14778 struct field_info *fi,
14779 const std::vector<variant_part_builder> &variant_parts);
14780
14781 /* Fill in a "struct variant" for a given variant field. RESULT is
14782 the variant to fill in. OBSTACK is where any needed allocations
14783 will be done. OFFSET_MAP holds the mapping from section offsets to
14784 fields for the type. FI describes the fields of the type we're
14785 processing. FIELD is the variant field we're converting. */
14786
14787 static void
14788 create_one_variant (variant &result, struct obstack *obstack,
14789 const offset_map_type &offset_map,
14790 struct field_info *fi, const variant_field &field)
14791 {
14792 result.discriminants = convert_variant_range (obstack, field, false);
14793 result.first_field = field.first_field + fi->baseclasses.size ();
14794 result.last_field = field.last_field + fi->baseclasses.size ();
14795 result.parts = create_variant_parts (obstack, offset_map, fi,
14796 field.variant_parts);
14797 }
14798
14799 /* Fill in a "struct variant_part" for a given variant part. RESULT
14800 is the variant part to fill in. OBSTACK is where any needed
14801 allocations will be done. OFFSET_MAP holds the mapping from
14802 section offsets to fields for the type. FI describes the fields of
14803 the type we're processing. BUILDER is the variant part to be
14804 converted. */
14805
14806 static void
14807 create_one_variant_part (variant_part &result,
14808 struct obstack *obstack,
14809 const offset_map_type &offset_map,
14810 struct field_info *fi,
14811 const variant_part_builder &builder)
14812 {
14813 auto iter = offset_map.find (builder.discriminant_offset);
14814 if (iter == offset_map.end ())
14815 {
14816 result.discriminant_index = -1;
14817 /* Doesn't matter. */
14818 result.is_unsigned = false;
14819 }
14820 else
14821 {
14822 result.discriminant_index = iter->second;
14823 result.is_unsigned
14824 = TYPE_UNSIGNED (FIELD_TYPE
14825 (fi->fields[result.discriminant_index].field));
14826 }
14827
14828 size_t n = builder.variants.size ();
14829 variant *output = new (obstack) variant[n];
14830 for (size_t i = 0; i < n; ++i)
14831 create_one_variant (output[i], obstack, offset_map, fi,
14832 builder.variants[i]);
14833
14834 result.variants = gdb::array_view<variant> (output, n);
14835 }
14836
14837 /* Create a vector of variant parts that can be attached to a type.
14838 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14839 holds the mapping from section offsets to fields for the type. FI
14840 describes the fields of the type we're processing. VARIANT_PARTS
14841 is the vector to convert. */
14842
14843 static const gdb::array_view<variant_part>
14844 create_variant_parts (struct obstack *obstack,
14845 const offset_map_type &offset_map,
14846 struct field_info *fi,
14847 const std::vector<variant_part_builder> &variant_parts)
14848 {
14849 if (variant_parts.empty ())
14850 return {};
14851
14852 size_t n = variant_parts.size ();
14853 variant_part *result = new (obstack) variant_part[n];
14854 for (size_t i = 0; i < n; ++i)
14855 create_one_variant_part (result[i], obstack, offset_map, fi,
14856 variant_parts[i]);
14857
14858 return gdb::array_view<variant_part> (result, n);
14859 }
14860
14861 /* Compute the variant part vector for FIP, attaching it to TYPE when
14862 done. */
14863
14864 static void
14865 add_variant_property (struct field_info *fip, struct type *type,
14866 struct dwarf2_cu *cu)
14867 {
14868 /* Map section offsets of fields to their field index. Note the
14869 field index here does not take the number of baseclasses into
14870 account. */
14871 offset_map_type offset_map;
14872 for (int i = 0; i < fip->fields.size (); ++i)
14873 offset_map[fip->fields[i].offset] = i;
14874
14875 struct objfile *objfile = cu->per_objfile->objfile;
14876 gdb::array_view<variant_part> parts
14877 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14878 fip->variant_parts);
14879
14880 struct dynamic_prop prop;
14881 prop.kind = PROP_VARIANT_PARTS;
14882 prop.data.variant_parts
14883 = ((gdb::array_view<variant_part> *)
14884 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14885
14886 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14887 }
14888
14889 /* Create the vector of fields, and attach it to the type. */
14890
14891 static void
14892 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14893 struct dwarf2_cu *cu)
14894 {
14895 int nfields = fip->nfields ();
14896
14897 /* Record the field count, allocate space for the array of fields,
14898 and create blank accessibility bitfields if necessary. */
14899 type->set_num_fields (nfields);
14900 type->set_fields
14901 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14902
14903 if (fip->non_public_fields && cu->language != language_ada)
14904 {
14905 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14906
14907 TYPE_FIELD_PRIVATE_BITS (type) =
14908 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14909 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14910
14911 TYPE_FIELD_PROTECTED_BITS (type) =
14912 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14913 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14914
14915 TYPE_FIELD_IGNORE_BITS (type) =
14916 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14917 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14918 }
14919
14920 /* If the type has baseclasses, allocate and clear a bit vector for
14921 TYPE_FIELD_VIRTUAL_BITS. */
14922 if (!fip->baseclasses.empty () && cu->language != language_ada)
14923 {
14924 int num_bytes = B_BYTES (fip->baseclasses.size ());
14925 unsigned char *pointer;
14926
14927 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14928 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14929 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14930 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14931 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14932 }
14933
14934 if (!fip->variant_parts.empty ())
14935 add_variant_property (fip, type, cu);
14936
14937 /* Copy the saved-up fields into the field vector. */
14938 for (int i = 0; i < nfields; ++i)
14939 {
14940 struct nextfield &field
14941 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14942 : fip->fields[i - fip->baseclasses.size ()]);
14943
14944 type->field (i) = field.field;
14945 switch (field.accessibility)
14946 {
14947 case DW_ACCESS_private:
14948 if (cu->language != language_ada)
14949 SET_TYPE_FIELD_PRIVATE (type, i);
14950 break;
14951
14952 case DW_ACCESS_protected:
14953 if (cu->language != language_ada)
14954 SET_TYPE_FIELD_PROTECTED (type, i);
14955 break;
14956
14957 case DW_ACCESS_public:
14958 break;
14959
14960 default:
14961 /* Unknown accessibility. Complain and treat it as public. */
14962 {
14963 complaint (_("unsupported accessibility %d"),
14964 field.accessibility);
14965 }
14966 break;
14967 }
14968 if (i < fip->baseclasses.size ())
14969 {
14970 switch (field.virtuality)
14971 {
14972 case DW_VIRTUALITY_virtual:
14973 case DW_VIRTUALITY_pure_virtual:
14974 if (cu->language == language_ada)
14975 error (_("unexpected virtuality in component of Ada type"));
14976 SET_TYPE_FIELD_VIRTUAL (type, i);
14977 break;
14978 }
14979 }
14980 }
14981 }
14982
14983 /* Return true if this member function is a constructor, false
14984 otherwise. */
14985
14986 static int
14987 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14988 {
14989 const char *fieldname;
14990 const char *type_name;
14991 int len;
14992
14993 if (die->parent == NULL)
14994 return 0;
14995
14996 if (die->parent->tag != DW_TAG_structure_type
14997 && die->parent->tag != DW_TAG_union_type
14998 && die->parent->tag != DW_TAG_class_type)
14999 return 0;
15000
15001 fieldname = dwarf2_name (die, cu);
15002 type_name = dwarf2_name (die->parent, cu);
15003 if (fieldname == NULL || type_name == NULL)
15004 return 0;
15005
15006 len = strlen (fieldname);
15007 return (strncmp (fieldname, type_name, len) == 0
15008 && (type_name[len] == '\0' || type_name[len] == '<'));
15009 }
15010
15011 /* Check if the given VALUE is a recognized enum
15012 dwarf_defaulted_attribute constant according to DWARF5 spec,
15013 Table 7.24. */
15014
15015 static bool
15016 is_valid_DW_AT_defaulted (ULONGEST value)
15017 {
15018 switch (value)
15019 {
15020 case DW_DEFAULTED_no:
15021 case DW_DEFAULTED_in_class:
15022 case DW_DEFAULTED_out_of_class:
15023 return true;
15024 }
15025
15026 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15027 return false;
15028 }
15029
15030 /* Add a member function to the proper fieldlist. */
15031
15032 static void
15033 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15034 struct type *type, struct dwarf2_cu *cu)
15035 {
15036 struct objfile *objfile = cu->per_objfile->objfile;
15037 struct attribute *attr;
15038 int i;
15039 struct fnfieldlist *flp = nullptr;
15040 struct fn_field *fnp;
15041 const char *fieldname;
15042 struct type *this_type;
15043 enum dwarf_access_attribute accessibility;
15044
15045 if (cu->language == language_ada)
15046 error (_("unexpected member function in Ada type"));
15047
15048 /* Get name of member function. */
15049 fieldname = dwarf2_name (die, cu);
15050 if (fieldname == NULL)
15051 return;
15052
15053 /* Look up member function name in fieldlist. */
15054 for (i = 0; i < fip->fnfieldlists.size (); i++)
15055 {
15056 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15057 {
15058 flp = &fip->fnfieldlists[i];
15059 break;
15060 }
15061 }
15062
15063 /* Create a new fnfieldlist if necessary. */
15064 if (flp == nullptr)
15065 {
15066 fip->fnfieldlists.emplace_back ();
15067 flp = &fip->fnfieldlists.back ();
15068 flp->name = fieldname;
15069 i = fip->fnfieldlists.size () - 1;
15070 }
15071
15072 /* Create a new member function field and add it to the vector of
15073 fnfieldlists. */
15074 flp->fnfields.emplace_back ();
15075 fnp = &flp->fnfields.back ();
15076
15077 /* Delay processing of the physname until later. */
15078 if (cu->language == language_cplus)
15079 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15080 die, cu);
15081 else
15082 {
15083 const char *physname = dwarf2_physname (fieldname, die, cu);
15084 fnp->physname = physname ? physname : "";
15085 }
15086
15087 fnp->type = alloc_type (objfile);
15088 this_type = read_type_die (die, cu);
15089 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15090 {
15091 int nparams = this_type->num_fields ();
15092
15093 /* TYPE is the domain of this method, and THIS_TYPE is the type
15094 of the method itself (TYPE_CODE_METHOD). */
15095 smash_to_method_type (fnp->type, type,
15096 TYPE_TARGET_TYPE (this_type),
15097 this_type->fields (),
15098 this_type->num_fields (),
15099 TYPE_VARARGS (this_type));
15100
15101 /* Handle static member functions.
15102 Dwarf2 has no clean way to discern C++ static and non-static
15103 member functions. G++ helps GDB by marking the first
15104 parameter for non-static member functions (which is the this
15105 pointer) as artificial. We obtain this information from
15106 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15107 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15108 fnp->voffset = VOFFSET_STATIC;
15109 }
15110 else
15111 complaint (_("member function type missing for '%s'"),
15112 dwarf2_full_name (fieldname, die, cu));
15113
15114 /* Get fcontext from DW_AT_containing_type if present. */
15115 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15116 fnp->fcontext = die_containing_type (die, cu);
15117
15118 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15119 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15120
15121 /* Get accessibility. */
15122 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15123 if (attr != nullptr)
15124 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15125 else
15126 accessibility = dwarf2_default_access_attribute (die, cu);
15127 switch (accessibility)
15128 {
15129 case DW_ACCESS_private:
15130 fnp->is_private = 1;
15131 break;
15132 case DW_ACCESS_protected:
15133 fnp->is_protected = 1;
15134 break;
15135 }
15136
15137 /* Check for artificial methods. */
15138 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15139 if (attr && DW_UNSND (attr) != 0)
15140 fnp->is_artificial = 1;
15141
15142 /* Check for defaulted methods. */
15143 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15144 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15145 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15146
15147 /* Check for deleted methods. */
15148 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15149 if (attr != nullptr && DW_UNSND (attr) != 0)
15150 fnp->is_deleted = 1;
15151
15152 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15153
15154 /* Get index in virtual function table if it is a virtual member
15155 function. For older versions of GCC, this is an offset in the
15156 appropriate virtual table, as specified by DW_AT_containing_type.
15157 For everyone else, it is an expression to be evaluated relative
15158 to the object address. */
15159
15160 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15161 if (attr != nullptr)
15162 {
15163 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15164 {
15165 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15166 {
15167 /* Old-style GCC. */
15168 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15169 }
15170 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15171 || (DW_BLOCK (attr)->size > 1
15172 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15173 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15174 {
15175 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15176 if ((fnp->voffset % cu->header.addr_size) != 0)
15177 dwarf2_complex_location_expr_complaint ();
15178 else
15179 fnp->voffset /= cu->header.addr_size;
15180 fnp->voffset += 2;
15181 }
15182 else
15183 dwarf2_complex_location_expr_complaint ();
15184
15185 if (!fnp->fcontext)
15186 {
15187 /* If there is no `this' field and no DW_AT_containing_type,
15188 we cannot actually find a base class context for the
15189 vtable! */
15190 if (this_type->num_fields () == 0
15191 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15192 {
15193 complaint (_("cannot determine context for virtual member "
15194 "function \"%s\" (offset %s)"),
15195 fieldname, sect_offset_str (die->sect_off));
15196 }
15197 else
15198 {
15199 fnp->fcontext
15200 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15201 }
15202 }
15203 }
15204 else if (attr->form_is_section_offset ())
15205 {
15206 dwarf2_complex_location_expr_complaint ();
15207 }
15208 else
15209 {
15210 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15211 fieldname);
15212 }
15213 }
15214 else
15215 {
15216 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15217 if (attr && DW_UNSND (attr))
15218 {
15219 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15220 complaint (_("Member function \"%s\" (offset %s) is virtual "
15221 "but the vtable offset is not specified"),
15222 fieldname, sect_offset_str (die->sect_off));
15223 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15224 TYPE_CPLUS_DYNAMIC (type) = 1;
15225 }
15226 }
15227 }
15228
15229 /* Create the vector of member function fields, and attach it to the type. */
15230
15231 static void
15232 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15233 struct dwarf2_cu *cu)
15234 {
15235 if (cu->language == language_ada)
15236 error (_("unexpected member functions in Ada type"));
15237
15238 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15239 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15240 TYPE_ALLOC (type,
15241 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15242
15243 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15244 {
15245 struct fnfieldlist &nf = fip->fnfieldlists[i];
15246 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15247
15248 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15249 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15250 fn_flp->fn_fields = (struct fn_field *)
15251 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15252
15253 for (int k = 0; k < nf.fnfields.size (); ++k)
15254 fn_flp->fn_fields[k] = nf.fnfields[k];
15255 }
15256
15257 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15258 }
15259
15260 /* Returns non-zero if NAME is the name of a vtable member in CU's
15261 language, zero otherwise. */
15262 static int
15263 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15264 {
15265 static const char vptr[] = "_vptr";
15266
15267 /* Look for the C++ form of the vtable. */
15268 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15269 return 1;
15270
15271 return 0;
15272 }
15273
15274 /* GCC outputs unnamed structures that are really pointers to member
15275 functions, with the ABI-specified layout. If TYPE describes
15276 such a structure, smash it into a member function type.
15277
15278 GCC shouldn't do this; it should just output pointer to member DIEs.
15279 This is GCC PR debug/28767. */
15280
15281 static void
15282 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15283 {
15284 struct type *pfn_type, *self_type, *new_type;
15285
15286 /* Check for a structure with no name and two children. */
15287 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15288 return;
15289
15290 /* Check for __pfn and __delta members. */
15291 if (TYPE_FIELD_NAME (type, 0) == NULL
15292 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15293 || TYPE_FIELD_NAME (type, 1) == NULL
15294 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15295 return;
15296
15297 /* Find the type of the method. */
15298 pfn_type = TYPE_FIELD_TYPE (type, 0);
15299 if (pfn_type == NULL
15300 || pfn_type->code () != TYPE_CODE_PTR
15301 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15302 return;
15303
15304 /* Look for the "this" argument. */
15305 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15306 if (pfn_type->num_fields () == 0
15307 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15308 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15309 return;
15310
15311 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15312 new_type = alloc_type (objfile);
15313 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15314 pfn_type->fields (), pfn_type->num_fields (),
15315 TYPE_VARARGS (pfn_type));
15316 smash_to_methodptr_type (type, new_type);
15317 }
15318
15319 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15320 appropriate error checking and issuing complaints if there is a
15321 problem. */
15322
15323 static ULONGEST
15324 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15325 {
15326 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15327
15328 if (attr == nullptr)
15329 return 0;
15330
15331 if (!attr->form_is_constant ())
15332 {
15333 complaint (_("DW_AT_alignment must have constant form"
15334 " - DIE at %s [in module %s]"),
15335 sect_offset_str (die->sect_off),
15336 objfile_name (cu->per_objfile->objfile));
15337 return 0;
15338 }
15339
15340 ULONGEST align;
15341 if (attr->form == DW_FORM_sdata)
15342 {
15343 LONGEST val = DW_SND (attr);
15344 if (val < 0)
15345 {
15346 complaint (_("DW_AT_alignment value must not be negative"
15347 " - DIE at %s [in module %s]"),
15348 sect_offset_str (die->sect_off),
15349 objfile_name (cu->per_objfile->objfile));
15350 return 0;
15351 }
15352 align = val;
15353 }
15354 else
15355 align = DW_UNSND (attr);
15356
15357 if (align == 0)
15358 {
15359 complaint (_("DW_AT_alignment value must not be zero"
15360 " - DIE at %s [in module %s]"),
15361 sect_offset_str (die->sect_off),
15362 objfile_name (cu->per_objfile->objfile));
15363 return 0;
15364 }
15365 if ((align & (align - 1)) != 0)
15366 {
15367 complaint (_("DW_AT_alignment value must be a power of 2"
15368 " - DIE at %s [in module %s]"),
15369 sect_offset_str (die->sect_off),
15370 objfile_name (cu->per_objfile->objfile));
15371 return 0;
15372 }
15373
15374 return align;
15375 }
15376
15377 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15378 the alignment for TYPE. */
15379
15380 static void
15381 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15382 struct type *type)
15383 {
15384 if (!set_type_align (type, get_alignment (cu, die)))
15385 complaint (_("DW_AT_alignment value too large"
15386 " - DIE at %s [in module %s]"),
15387 sect_offset_str (die->sect_off),
15388 objfile_name (cu->per_objfile->objfile));
15389 }
15390
15391 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15392 constant for a type, according to DWARF5 spec, Table 5.5. */
15393
15394 static bool
15395 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15396 {
15397 switch (value)
15398 {
15399 case DW_CC_normal:
15400 case DW_CC_pass_by_reference:
15401 case DW_CC_pass_by_value:
15402 return true;
15403
15404 default:
15405 complaint (_("unrecognized DW_AT_calling_convention value "
15406 "(%s) for a type"), pulongest (value));
15407 return false;
15408 }
15409 }
15410
15411 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15412 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15413 also according to GNU-specific values (see include/dwarf2.h). */
15414
15415 static bool
15416 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15417 {
15418 switch (value)
15419 {
15420 case DW_CC_normal:
15421 case DW_CC_program:
15422 case DW_CC_nocall:
15423 return true;
15424
15425 case DW_CC_GNU_renesas_sh:
15426 case DW_CC_GNU_borland_fastcall_i386:
15427 case DW_CC_GDB_IBM_OpenCL:
15428 return true;
15429
15430 default:
15431 complaint (_("unrecognized DW_AT_calling_convention value "
15432 "(%s) for a subroutine"), pulongest (value));
15433 return false;
15434 }
15435 }
15436
15437 /* Called when we find the DIE that starts a structure or union scope
15438 (definition) to create a type for the structure or union. Fill in
15439 the type's name and general properties; the members will not be
15440 processed until process_structure_scope. A symbol table entry for
15441 the type will also not be done until process_structure_scope (assuming
15442 the type has a name).
15443
15444 NOTE: we need to call these functions regardless of whether or not the
15445 DIE has a DW_AT_name attribute, since it might be an anonymous
15446 structure or union. This gets the type entered into our set of
15447 user defined types. */
15448
15449 static struct type *
15450 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15451 {
15452 struct objfile *objfile = cu->per_objfile->objfile;
15453 struct type *type;
15454 struct attribute *attr;
15455 const char *name;
15456
15457 /* If the definition of this type lives in .debug_types, read that type.
15458 Don't follow DW_AT_specification though, that will take us back up
15459 the chain and we want to go down. */
15460 attr = die->attr (DW_AT_signature);
15461 if (attr != nullptr)
15462 {
15463 type = get_DW_AT_signature_type (die, attr, cu);
15464
15465 /* The type's CU may not be the same as CU.
15466 Ensure TYPE is recorded with CU in die_type_hash. */
15467 return set_die_type (die, type, cu);
15468 }
15469
15470 type = alloc_type (objfile);
15471 INIT_CPLUS_SPECIFIC (type);
15472
15473 name = dwarf2_name (die, cu);
15474 if (name != NULL)
15475 {
15476 if (cu->language == language_cplus
15477 || cu->language == language_d
15478 || cu->language == language_rust)
15479 {
15480 const char *full_name = dwarf2_full_name (name, die, cu);
15481
15482 /* dwarf2_full_name might have already finished building the DIE's
15483 type. If so, there is no need to continue. */
15484 if (get_die_type (die, cu) != NULL)
15485 return get_die_type (die, cu);
15486
15487 type->set_name (full_name);
15488 }
15489 else
15490 {
15491 /* The name is already allocated along with this objfile, so
15492 we don't need to duplicate it for the type. */
15493 type->set_name (name);
15494 }
15495 }
15496
15497 if (die->tag == DW_TAG_structure_type)
15498 {
15499 type->set_code (TYPE_CODE_STRUCT);
15500 }
15501 else if (die->tag == DW_TAG_union_type)
15502 {
15503 type->set_code (TYPE_CODE_UNION);
15504 }
15505 else
15506 {
15507 type->set_code (TYPE_CODE_STRUCT);
15508 }
15509
15510 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15511 TYPE_DECLARED_CLASS (type) = 1;
15512
15513 /* Store the calling convention in the type if it's available in
15514 the die. Otherwise the calling convention remains set to
15515 the default value DW_CC_normal. */
15516 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15517 if (attr != nullptr
15518 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15519 {
15520 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15521 TYPE_CPLUS_CALLING_CONVENTION (type)
15522 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15523 }
15524
15525 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15526 if (attr != nullptr)
15527 {
15528 if (attr->form_is_constant ())
15529 TYPE_LENGTH (type) = DW_UNSND (attr);
15530 else
15531 {
15532 struct dynamic_prop prop;
15533 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15534 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15535 TYPE_LENGTH (type) = 0;
15536 }
15537 }
15538 else
15539 {
15540 TYPE_LENGTH (type) = 0;
15541 }
15542
15543 maybe_set_alignment (cu, die, type);
15544
15545 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15546 {
15547 /* ICC<14 does not output the required DW_AT_declaration on
15548 incomplete types, but gives them a size of zero. */
15549 TYPE_STUB (type) = 1;
15550 }
15551 else
15552 TYPE_STUB_SUPPORTED (type) = 1;
15553
15554 if (die_is_declaration (die, cu))
15555 TYPE_STUB (type) = 1;
15556 else if (attr == NULL && die->child == NULL
15557 && producer_is_realview (cu->producer))
15558 /* RealView does not output the required DW_AT_declaration
15559 on incomplete types. */
15560 TYPE_STUB (type) = 1;
15561
15562 /* We need to add the type field to the die immediately so we don't
15563 infinitely recurse when dealing with pointers to the structure
15564 type within the structure itself. */
15565 set_die_type (die, type, cu);
15566
15567 /* set_die_type should be already done. */
15568 set_descriptive_type (type, die, cu);
15569
15570 return type;
15571 }
15572
15573 static void handle_struct_member_die
15574 (struct die_info *child_die,
15575 struct type *type,
15576 struct field_info *fi,
15577 std::vector<struct symbol *> *template_args,
15578 struct dwarf2_cu *cu);
15579
15580 /* A helper for handle_struct_member_die that handles
15581 DW_TAG_variant_part. */
15582
15583 static void
15584 handle_variant_part (struct die_info *die, struct type *type,
15585 struct field_info *fi,
15586 std::vector<struct symbol *> *template_args,
15587 struct dwarf2_cu *cu)
15588 {
15589 variant_part_builder *new_part;
15590 if (fi->current_variant_part == nullptr)
15591 {
15592 fi->variant_parts.emplace_back ();
15593 new_part = &fi->variant_parts.back ();
15594 }
15595 else if (!fi->current_variant_part->processing_variant)
15596 {
15597 complaint (_("nested DW_TAG_variant_part seen "
15598 "- DIE at %s [in module %s]"),
15599 sect_offset_str (die->sect_off),
15600 objfile_name (cu->per_objfile->objfile));
15601 return;
15602 }
15603 else
15604 {
15605 variant_field &current = fi->current_variant_part->variants.back ();
15606 current.variant_parts.emplace_back ();
15607 new_part = &current.variant_parts.back ();
15608 }
15609
15610 /* When we recurse, we want callees to add to this new variant
15611 part. */
15612 scoped_restore save_current_variant_part
15613 = make_scoped_restore (&fi->current_variant_part, new_part);
15614
15615 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15616 if (discr == NULL)
15617 {
15618 /* It's a univariant form, an extension we support. */
15619 }
15620 else if (discr->form_is_ref ())
15621 {
15622 struct dwarf2_cu *target_cu = cu;
15623 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15624
15625 new_part->discriminant_offset = target_die->sect_off;
15626 }
15627 else
15628 {
15629 complaint (_("DW_AT_discr does not have DIE reference form"
15630 " - DIE at %s [in module %s]"),
15631 sect_offset_str (die->sect_off),
15632 objfile_name (cu->per_objfile->objfile));
15633 }
15634
15635 for (die_info *child_die = die->child;
15636 child_die != NULL;
15637 child_die = child_die->sibling)
15638 handle_struct_member_die (child_die, type, fi, template_args, cu);
15639 }
15640
15641 /* A helper for handle_struct_member_die that handles
15642 DW_TAG_variant. */
15643
15644 static void
15645 handle_variant (struct die_info *die, struct type *type,
15646 struct field_info *fi,
15647 std::vector<struct symbol *> *template_args,
15648 struct dwarf2_cu *cu)
15649 {
15650 if (fi->current_variant_part == nullptr)
15651 {
15652 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15653 "- DIE at %s [in module %s]"),
15654 sect_offset_str (die->sect_off),
15655 objfile_name (cu->per_objfile->objfile));
15656 return;
15657 }
15658 if (fi->current_variant_part->processing_variant)
15659 {
15660 complaint (_("nested DW_TAG_variant seen "
15661 "- DIE at %s [in module %s]"),
15662 sect_offset_str (die->sect_off),
15663 objfile_name (cu->per_objfile->objfile));
15664 return;
15665 }
15666
15667 scoped_restore save_processing_variant
15668 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15669 true);
15670
15671 fi->current_variant_part->variants.emplace_back ();
15672 variant_field &variant = fi->current_variant_part->variants.back ();
15673 variant.first_field = fi->fields.size ();
15674
15675 /* In a variant we want to get the discriminant and also add a
15676 field for our sole member child. */
15677 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15678 if (discr == nullptr)
15679 {
15680 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15681 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15682 variant.default_branch = true;
15683 else
15684 variant.discr_list_data = DW_BLOCK (discr);
15685 }
15686 else
15687 variant.discriminant_value = DW_UNSND (discr);
15688
15689 for (die_info *variant_child = die->child;
15690 variant_child != NULL;
15691 variant_child = variant_child->sibling)
15692 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15693
15694 variant.last_field = fi->fields.size ();
15695 }
15696
15697 /* A helper for process_structure_scope that handles a single member
15698 DIE. */
15699
15700 static void
15701 handle_struct_member_die (struct die_info *child_die, struct type *type,
15702 struct field_info *fi,
15703 std::vector<struct symbol *> *template_args,
15704 struct dwarf2_cu *cu)
15705 {
15706 if (child_die->tag == DW_TAG_member
15707 || child_die->tag == DW_TAG_variable)
15708 {
15709 /* NOTE: carlton/2002-11-05: A C++ static data member
15710 should be a DW_TAG_member that is a declaration, but
15711 all versions of G++ as of this writing (so through at
15712 least 3.2.1) incorrectly generate DW_TAG_variable
15713 tags for them instead. */
15714 dwarf2_add_field (fi, child_die, cu);
15715 }
15716 else if (child_die->tag == DW_TAG_subprogram)
15717 {
15718 /* Rust doesn't have member functions in the C++ sense.
15719 However, it does emit ordinary functions as children
15720 of a struct DIE. */
15721 if (cu->language == language_rust)
15722 read_func_scope (child_die, cu);
15723 else
15724 {
15725 /* C++ member function. */
15726 dwarf2_add_member_fn (fi, child_die, type, cu);
15727 }
15728 }
15729 else if (child_die->tag == DW_TAG_inheritance)
15730 {
15731 /* C++ base class field. */
15732 dwarf2_add_field (fi, child_die, cu);
15733 }
15734 else if (type_can_define_types (child_die))
15735 dwarf2_add_type_defn (fi, child_die, cu);
15736 else if (child_die->tag == DW_TAG_template_type_param
15737 || child_die->tag == DW_TAG_template_value_param)
15738 {
15739 struct symbol *arg = new_symbol (child_die, NULL, cu);
15740
15741 if (arg != NULL)
15742 template_args->push_back (arg);
15743 }
15744 else if (child_die->tag == DW_TAG_variant_part)
15745 handle_variant_part (child_die, type, fi, template_args, cu);
15746 else if (child_die->tag == DW_TAG_variant)
15747 handle_variant (child_die, type, fi, template_args, cu);
15748 }
15749
15750 /* Finish creating a structure or union type, including filling in
15751 its members and creating a symbol for it. */
15752
15753 static void
15754 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15755 {
15756 struct objfile *objfile = cu->per_objfile->objfile;
15757 struct die_info *child_die;
15758 struct type *type;
15759
15760 type = get_die_type (die, cu);
15761 if (type == NULL)
15762 type = read_structure_type (die, cu);
15763
15764 bool has_template_parameters = false;
15765 if (die->child != NULL && ! die_is_declaration (die, cu))
15766 {
15767 struct field_info fi;
15768 std::vector<struct symbol *> template_args;
15769
15770 child_die = die->child;
15771
15772 while (child_die && child_die->tag)
15773 {
15774 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15775 child_die = child_die->sibling;
15776 }
15777
15778 /* Attach template arguments to type. */
15779 if (!template_args.empty ())
15780 {
15781 has_template_parameters = true;
15782 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15783 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15784 TYPE_TEMPLATE_ARGUMENTS (type)
15785 = XOBNEWVEC (&objfile->objfile_obstack,
15786 struct symbol *,
15787 TYPE_N_TEMPLATE_ARGUMENTS (type));
15788 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15789 template_args.data (),
15790 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15791 * sizeof (struct symbol *)));
15792 }
15793
15794 /* Attach fields and member functions to the type. */
15795 if (fi.nfields () > 0)
15796 dwarf2_attach_fields_to_type (&fi, type, cu);
15797 if (!fi.fnfieldlists.empty ())
15798 {
15799 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15800
15801 /* Get the type which refers to the base class (possibly this
15802 class itself) which contains the vtable pointer for the current
15803 class from the DW_AT_containing_type attribute. This use of
15804 DW_AT_containing_type is a GNU extension. */
15805
15806 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15807 {
15808 struct type *t = die_containing_type (die, cu);
15809
15810 set_type_vptr_basetype (type, t);
15811 if (type == t)
15812 {
15813 int i;
15814
15815 /* Our own class provides vtbl ptr. */
15816 for (i = t->num_fields () - 1;
15817 i >= TYPE_N_BASECLASSES (t);
15818 --i)
15819 {
15820 const char *fieldname = TYPE_FIELD_NAME (t, i);
15821
15822 if (is_vtable_name (fieldname, cu))
15823 {
15824 set_type_vptr_fieldno (type, i);
15825 break;
15826 }
15827 }
15828
15829 /* Complain if virtual function table field not found. */
15830 if (i < TYPE_N_BASECLASSES (t))
15831 complaint (_("virtual function table pointer "
15832 "not found when defining class '%s'"),
15833 type->name () ? type->name () : "");
15834 }
15835 else
15836 {
15837 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15838 }
15839 }
15840 else if (cu->producer
15841 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15842 {
15843 /* The IBM XLC compiler does not provide direct indication
15844 of the containing type, but the vtable pointer is
15845 always named __vfp. */
15846
15847 int i;
15848
15849 for (i = type->num_fields () - 1;
15850 i >= TYPE_N_BASECLASSES (type);
15851 --i)
15852 {
15853 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15854 {
15855 set_type_vptr_fieldno (type, i);
15856 set_type_vptr_basetype (type, type);
15857 break;
15858 }
15859 }
15860 }
15861 }
15862
15863 /* Copy fi.typedef_field_list linked list elements content into the
15864 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15865 if (!fi.typedef_field_list.empty ())
15866 {
15867 int count = fi.typedef_field_list.size ();
15868
15869 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15870 TYPE_TYPEDEF_FIELD_ARRAY (type)
15871 = ((struct decl_field *)
15872 TYPE_ALLOC (type,
15873 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15874 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15875
15876 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15877 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15878 }
15879
15880 /* Copy fi.nested_types_list linked list elements content into the
15881 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15882 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15883 {
15884 int count = fi.nested_types_list.size ();
15885
15886 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15887 TYPE_NESTED_TYPES_ARRAY (type)
15888 = ((struct decl_field *)
15889 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15890 TYPE_NESTED_TYPES_COUNT (type) = count;
15891
15892 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15893 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15894 }
15895 }
15896
15897 quirk_gcc_member_function_pointer (type, objfile);
15898 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15899 cu->rust_unions.push_back (type);
15900
15901 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15902 snapshots) has been known to create a die giving a declaration
15903 for a class that has, as a child, a die giving a definition for a
15904 nested class. So we have to process our children even if the
15905 current die is a declaration. Normally, of course, a declaration
15906 won't have any children at all. */
15907
15908 child_die = die->child;
15909
15910 while (child_die != NULL && child_die->tag)
15911 {
15912 if (child_die->tag == DW_TAG_member
15913 || child_die->tag == DW_TAG_variable
15914 || child_die->tag == DW_TAG_inheritance
15915 || child_die->tag == DW_TAG_template_value_param
15916 || child_die->tag == DW_TAG_template_type_param)
15917 {
15918 /* Do nothing. */
15919 }
15920 else
15921 process_die (child_die, cu);
15922
15923 child_die = child_die->sibling;
15924 }
15925
15926 /* Do not consider external references. According to the DWARF standard,
15927 these DIEs are identified by the fact that they have no byte_size
15928 attribute, and a declaration attribute. */
15929 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15930 || !die_is_declaration (die, cu)
15931 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15932 {
15933 struct symbol *sym = new_symbol (die, type, cu);
15934
15935 if (has_template_parameters)
15936 {
15937 struct symtab *symtab;
15938 if (sym != nullptr)
15939 symtab = symbol_symtab (sym);
15940 else if (cu->line_header != nullptr)
15941 {
15942 /* Any related symtab will do. */
15943 symtab
15944 = cu->line_header->file_names ()[0].symtab;
15945 }
15946 else
15947 {
15948 symtab = nullptr;
15949 complaint (_("could not find suitable "
15950 "symtab for template parameter"
15951 " - DIE at %s [in module %s]"),
15952 sect_offset_str (die->sect_off),
15953 objfile_name (objfile));
15954 }
15955
15956 if (symtab != nullptr)
15957 {
15958 /* Make sure that the symtab is set on the new symbols.
15959 Even though they don't appear in this symtab directly,
15960 other parts of gdb assume that symbols do, and this is
15961 reasonably true. */
15962 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15963 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15964 }
15965 }
15966 }
15967 }
15968
15969 /* Assuming DIE is an enumeration type, and TYPE is its associated
15970 type, update TYPE using some information only available in DIE's
15971 children. In particular, the fields are computed. */
15972
15973 static void
15974 update_enumeration_type_from_children (struct die_info *die,
15975 struct type *type,
15976 struct dwarf2_cu *cu)
15977 {
15978 struct die_info *child_die;
15979 int unsigned_enum = 1;
15980 int flag_enum = 1;
15981
15982 auto_obstack obstack;
15983 std::vector<struct field> fields;
15984
15985 for (child_die = die->child;
15986 child_die != NULL && child_die->tag;
15987 child_die = child_die->sibling)
15988 {
15989 struct attribute *attr;
15990 LONGEST value;
15991 const gdb_byte *bytes;
15992 struct dwarf2_locexpr_baton *baton;
15993 const char *name;
15994
15995 if (child_die->tag != DW_TAG_enumerator)
15996 continue;
15997
15998 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15999 if (attr == NULL)
16000 continue;
16001
16002 name = dwarf2_name (child_die, cu);
16003 if (name == NULL)
16004 name = "<anonymous enumerator>";
16005
16006 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16007 &value, &bytes, &baton);
16008 if (value < 0)
16009 {
16010 unsigned_enum = 0;
16011 flag_enum = 0;
16012 }
16013 else
16014 {
16015 if (count_one_bits_ll (value) >= 2)
16016 flag_enum = 0;
16017 }
16018
16019 fields.emplace_back ();
16020 struct field &field = fields.back ();
16021 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16022 SET_FIELD_ENUMVAL (field, value);
16023 }
16024
16025 if (!fields.empty ())
16026 {
16027 type->set_num_fields (fields.size ());
16028 type->set_fields
16029 ((struct field *)
16030 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16031 memcpy (type->fields (), fields.data (),
16032 sizeof (struct field) * fields.size ());
16033 }
16034
16035 if (unsigned_enum)
16036 TYPE_UNSIGNED (type) = 1;
16037 if (flag_enum)
16038 TYPE_FLAG_ENUM (type) = 1;
16039 }
16040
16041 /* Given a DW_AT_enumeration_type die, set its type. We do not
16042 complete the type's fields yet, or create any symbols. */
16043
16044 static struct type *
16045 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16046 {
16047 struct objfile *objfile = cu->per_objfile->objfile;
16048 struct type *type;
16049 struct attribute *attr;
16050 const char *name;
16051
16052 /* If the definition of this type lives in .debug_types, read that type.
16053 Don't follow DW_AT_specification though, that will take us back up
16054 the chain and we want to go down. */
16055 attr = die->attr (DW_AT_signature);
16056 if (attr != nullptr)
16057 {
16058 type = get_DW_AT_signature_type (die, attr, cu);
16059
16060 /* The type's CU may not be the same as CU.
16061 Ensure TYPE is recorded with CU in die_type_hash. */
16062 return set_die_type (die, type, cu);
16063 }
16064
16065 type = alloc_type (objfile);
16066
16067 type->set_code (TYPE_CODE_ENUM);
16068 name = dwarf2_full_name (NULL, die, cu);
16069 if (name != NULL)
16070 type->set_name (name);
16071
16072 attr = dwarf2_attr (die, DW_AT_type, cu);
16073 if (attr != NULL)
16074 {
16075 struct type *underlying_type = die_type (die, cu);
16076
16077 TYPE_TARGET_TYPE (type) = underlying_type;
16078 }
16079
16080 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16081 if (attr != nullptr)
16082 {
16083 TYPE_LENGTH (type) = DW_UNSND (attr);
16084 }
16085 else
16086 {
16087 TYPE_LENGTH (type) = 0;
16088 }
16089
16090 maybe_set_alignment (cu, die, type);
16091
16092 /* The enumeration DIE can be incomplete. In Ada, any type can be
16093 declared as private in the package spec, and then defined only
16094 inside the package body. Such types are known as Taft Amendment
16095 Types. When another package uses such a type, an incomplete DIE
16096 may be generated by the compiler. */
16097 if (die_is_declaration (die, cu))
16098 TYPE_STUB (type) = 1;
16099
16100 /* If this type has an underlying type that is not a stub, then we
16101 may use its attributes. We always use the "unsigned" attribute
16102 in this situation, because ordinarily we guess whether the type
16103 is unsigned -- but the guess can be wrong and the underlying type
16104 can tell us the reality. However, we defer to a local size
16105 attribute if one exists, because this lets the compiler override
16106 the underlying type if needed. */
16107 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16108 {
16109 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16110 underlying_type = check_typedef (underlying_type);
16111 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16112 if (TYPE_LENGTH (type) == 0)
16113 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16114 if (TYPE_RAW_ALIGN (type) == 0
16115 && TYPE_RAW_ALIGN (underlying_type) != 0)
16116 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16117 }
16118
16119 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16120
16121 set_die_type (die, type, cu);
16122
16123 /* Finish the creation of this type by using the enum's children.
16124 Note that, as usual, this must come after set_die_type to avoid
16125 infinite recursion when trying to compute the names of the
16126 enumerators. */
16127 update_enumeration_type_from_children (die, type, cu);
16128
16129 return type;
16130 }
16131
16132 /* Given a pointer to a die which begins an enumeration, process all
16133 the dies that define the members of the enumeration, and create the
16134 symbol for the enumeration type.
16135
16136 NOTE: We reverse the order of the element list. */
16137
16138 static void
16139 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16140 {
16141 struct type *this_type;
16142
16143 this_type = get_die_type (die, cu);
16144 if (this_type == NULL)
16145 this_type = read_enumeration_type (die, cu);
16146
16147 if (die->child != NULL)
16148 {
16149 struct die_info *child_die;
16150 const char *name;
16151
16152 child_die = die->child;
16153 while (child_die && child_die->tag)
16154 {
16155 if (child_die->tag != DW_TAG_enumerator)
16156 {
16157 process_die (child_die, cu);
16158 }
16159 else
16160 {
16161 name = dwarf2_name (child_die, cu);
16162 if (name)
16163 new_symbol (child_die, this_type, cu);
16164 }
16165
16166 child_die = child_die->sibling;
16167 }
16168 }
16169
16170 /* If we are reading an enum from a .debug_types unit, and the enum
16171 is a declaration, and the enum is not the signatured type in the
16172 unit, then we do not want to add a symbol for it. Adding a
16173 symbol would in some cases obscure the true definition of the
16174 enum, giving users an incomplete type when the definition is
16175 actually available. Note that we do not want to do this for all
16176 enums which are just declarations, because C++0x allows forward
16177 enum declarations. */
16178 if (cu->per_cu->is_debug_types
16179 && die_is_declaration (die, cu))
16180 {
16181 struct signatured_type *sig_type;
16182
16183 sig_type = (struct signatured_type *) cu->per_cu;
16184 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16185 if (sig_type->type_offset_in_section != die->sect_off)
16186 return;
16187 }
16188
16189 new_symbol (die, this_type, cu);
16190 }
16191
16192 /* Extract all information from a DW_TAG_array_type DIE and put it in
16193 the DIE's type field. For now, this only handles one dimensional
16194 arrays. */
16195
16196 static struct type *
16197 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16198 {
16199 struct objfile *objfile = cu->per_objfile->objfile;
16200 struct die_info *child_die;
16201 struct type *type;
16202 struct type *element_type, *range_type, *index_type;
16203 struct attribute *attr;
16204 const char *name;
16205 struct dynamic_prop *byte_stride_prop = NULL;
16206 unsigned int bit_stride = 0;
16207
16208 element_type = die_type (die, cu);
16209
16210 /* The die_type call above may have already set the type for this DIE. */
16211 type = get_die_type (die, cu);
16212 if (type)
16213 return type;
16214
16215 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16216 if (attr != NULL)
16217 {
16218 int stride_ok;
16219 struct type *prop_type = cu->addr_sized_int_type (false);
16220
16221 byte_stride_prop
16222 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16223 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16224 prop_type);
16225 if (!stride_ok)
16226 {
16227 complaint (_("unable to read array DW_AT_byte_stride "
16228 " - DIE at %s [in module %s]"),
16229 sect_offset_str (die->sect_off),
16230 objfile_name (cu->per_objfile->objfile));
16231 /* Ignore this attribute. We will likely not be able to print
16232 arrays of this type correctly, but there is little we can do
16233 to help if we cannot read the attribute's value. */
16234 byte_stride_prop = NULL;
16235 }
16236 }
16237
16238 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16239 if (attr != NULL)
16240 bit_stride = DW_UNSND (attr);
16241
16242 /* Irix 6.2 native cc creates array types without children for
16243 arrays with unspecified length. */
16244 if (die->child == NULL)
16245 {
16246 index_type = objfile_type (objfile)->builtin_int;
16247 range_type = create_static_range_type (NULL, index_type, 0, -1);
16248 type = create_array_type_with_stride (NULL, element_type, range_type,
16249 byte_stride_prop, bit_stride);
16250 return set_die_type (die, type, cu);
16251 }
16252
16253 std::vector<struct type *> range_types;
16254 child_die = die->child;
16255 while (child_die && child_die->tag)
16256 {
16257 if (child_die->tag == DW_TAG_subrange_type)
16258 {
16259 struct type *child_type = read_type_die (child_die, cu);
16260
16261 if (child_type != NULL)
16262 {
16263 /* The range type was succesfully read. Save it for the
16264 array type creation. */
16265 range_types.push_back (child_type);
16266 }
16267 }
16268 child_die = child_die->sibling;
16269 }
16270
16271 /* Dwarf2 dimensions are output from left to right, create the
16272 necessary array types in backwards order. */
16273
16274 type = element_type;
16275
16276 if (read_array_order (die, cu) == DW_ORD_col_major)
16277 {
16278 int i = 0;
16279
16280 while (i < range_types.size ())
16281 type = create_array_type_with_stride (NULL, type, range_types[i++],
16282 byte_stride_prop, bit_stride);
16283 }
16284 else
16285 {
16286 size_t ndim = range_types.size ();
16287 while (ndim-- > 0)
16288 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16289 byte_stride_prop, bit_stride);
16290 }
16291
16292 /* Understand Dwarf2 support for vector types (like they occur on
16293 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16294 array type. This is not part of the Dwarf2/3 standard yet, but a
16295 custom vendor extension. The main difference between a regular
16296 array and the vector variant is that vectors are passed by value
16297 to functions. */
16298 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16299 if (attr != nullptr)
16300 make_vector_type (type);
16301
16302 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16303 implementation may choose to implement triple vectors using this
16304 attribute. */
16305 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16306 if (attr != nullptr)
16307 {
16308 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16309 TYPE_LENGTH (type) = DW_UNSND (attr);
16310 else
16311 complaint (_("DW_AT_byte_size for array type smaller "
16312 "than the total size of elements"));
16313 }
16314
16315 name = dwarf2_name (die, cu);
16316 if (name)
16317 type->set_name (name);
16318
16319 maybe_set_alignment (cu, die, type);
16320
16321 /* Install the type in the die. */
16322 set_die_type (die, type, cu);
16323
16324 /* set_die_type should be already done. */
16325 set_descriptive_type (type, die, cu);
16326
16327 return type;
16328 }
16329
16330 static enum dwarf_array_dim_ordering
16331 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16332 {
16333 struct attribute *attr;
16334
16335 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16336
16337 if (attr != nullptr)
16338 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16339
16340 /* GNU F77 is a special case, as at 08/2004 array type info is the
16341 opposite order to the dwarf2 specification, but data is still
16342 laid out as per normal fortran.
16343
16344 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16345 version checking. */
16346
16347 if (cu->language == language_fortran
16348 && cu->producer && strstr (cu->producer, "GNU F77"))
16349 {
16350 return DW_ORD_row_major;
16351 }
16352
16353 switch (cu->language_defn->la_array_ordering)
16354 {
16355 case array_column_major:
16356 return DW_ORD_col_major;
16357 case array_row_major:
16358 default:
16359 return DW_ORD_row_major;
16360 };
16361 }
16362
16363 /* Extract all information from a DW_TAG_set_type DIE and put it in
16364 the DIE's type field. */
16365
16366 static struct type *
16367 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16368 {
16369 struct type *domain_type, *set_type;
16370 struct attribute *attr;
16371
16372 domain_type = die_type (die, cu);
16373
16374 /* The die_type call above may have already set the type for this DIE. */
16375 set_type = get_die_type (die, cu);
16376 if (set_type)
16377 return set_type;
16378
16379 set_type = create_set_type (NULL, domain_type);
16380
16381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16382 if (attr != nullptr)
16383 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16384
16385 maybe_set_alignment (cu, die, set_type);
16386
16387 return set_die_type (die, set_type, cu);
16388 }
16389
16390 /* A helper for read_common_block that creates a locexpr baton.
16391 SYM is the symbol which we are marking as computed.
16392 COMMON_DIE is the DIE for the common block.
16393 COMMON_LOC is the location expression attribute for the common
16394 block itself.
16395 MEMBER_LOC is the location expression attribute for the particular
16396 member of the common block that we are processing.
16397 CU is the CU from which the above come. */
16398
16399 static void
16400 mark_common_block_symbol_computed (struct symbol *sym,
16401 struct die_info *common_die,
16402 struct attribute *common_loc,
16403 struct attribute *member_loc,
16404 struct dwarf2_cu *cu)
16405 {
16406 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16407 struct objfile *objfile = per_objfile->objfile;
16408 struct dwarf2_locexpr_baton *baton;
16409 gdb_byte *ptr;
16410 unsigned int cu_off;
16411 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16412 LONGEST offset = 0;
16413
16414 gdb_assert (common_loc && member_loc);
16415 gdb_assert (common_loc->form_is_block ());
16416 gdb_assert (member_loc->form_is_block ()
16417 || member_loc->form_is_constant ());
16418
16419 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16420 baton->per_objfile = per_objfile;
16421 baton->per_cu = cu->per_cu;
16422 gdb_assert (baton->per_cu);
16423
16424 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16425
16426 if (member_loc->form_is_constant ())
16427 {
16428 offset = member_loc->constant_value (0);
16429 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16430 }
16431 else
16432 baton->size += DW_BLOCK (member_loc)->size;
16433
16434 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16435 baton->data = ptr;
16436
16437 *ptr++ = DW_OP_call4;
16438 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16439 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16440 ptr += 4;
16441
16442 if (member_loc->form_is_constant ())
16443 {
16444 *ptr++ = DW_OP_addr;
16445 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16446 ptr += cu->header.addr_size;
16447 }
16448 else
16449 {
16450 /* We have to copy the data here, because DW_OP_call4 will only
16451 use a DW_AT_location attribute. */
16452 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16453 ptr += DW_BLOCK (member_loc)->size;
16454 }
16455
16456 *ptr++ = DW_OP_plus;
16457 gdb_assert (ptr - baton->data == baton->size);
16458
16459 SYMBOL_LOCATION_BATON (sym) = baton;
16460 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16461 }
16462
16463 /* Create appropriate locally-scoped variables for all the
16464 DW_TAG_common_block entries. Also create a struct common_block
16465 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16466 is used to separate the common blocks name namespace from regular
16467 variable names. */
16468
16469 static void
16470 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16471 {
16472 struct attribute *attr;
16473
16474 attr = dwarf2_attr (die, DW_AT_location, cu);
16475 if (attr != nullptr)
16476 {
16477 /* Support the .debug_loc offsets. */
16478 if (attr->form_is_block ())
16479 {
16480 /* Ok. */
16481 }
16482 else if (attr->form_is_section_offset ())
16483 {
16484 dwarf2_complex_location_expr_complaint ();
16485 attr = NULL;
16486 }
16487 else
16488 {
16489 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16490 "common block member");
16491 attr = NULL;
16492 }
16493 }
16494
16495 if (die->child != NULL)
16496 {
16497 struct objfile *objfile = cu->per_objfile->objfile;
16498 struct die_info *child_die;
16499 size_t n_entries = 0, size;
16500 struct common_block *common_block;
16501 struct symbol *sym;
16502
16503 for (child_die = die->child;
16504 child_die && child_die->tag;
16505 child_die = child_die->sibling)
16506 ++n_entries;
16507
16508 size = (sizeof (struct common_block)
16509 + (n_entries - 1) * sizeof (struct symbol *));
16510 common_block
16511 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16512 size);
16513 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16514 common_block->n_entries = 0;
16515
16516 for (child_die = die->child;
16517 child_die && child_die->tag;
16518 child_die = child_die->sibling)
16519 {
16520 /* Create the symbol in the DW_TAG_common_block block in the current
16521 symbol scope. */
16522 sym = new_symbol (child_die, NULL, cu);
16523 if (sym != NULL)
16524 {
16525 struct attribute *member_loc;
16526
16527 common_block->contents[common_block->n_entries++] = sym;
16528
16529 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16530 cu);
16531 if (member_loc)
16532 {
16533 /* GDB has handled this for a long time, but it is
16534 not specified by DWARF. It seems to have been
16535 emitted by gfortran at least as recently as:
16536 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16537 complaint (_("Variable in common block has "
16538 "DW_AT_data_member_location "
16539 "- DIE at %s [in module %s]"),
16540 sect_offset_str (child_die->sect_off),
16541 objfile_name (objfile));
16542
16543 if (member_loc->form_is_section_offset ())
16544 dwarf2_complex_location_expr_complaint ();
16545 else if (member_loc->form_is_constant ()
16546 || member_loc->form_is_block ())
16547 {
16548 if (attr != nullptr)
16549 mark_common_block_symbol_computed (sym, die, attr,
16550 member_loc, cu);
16551 }
16552 else
16553 dwarf2_complex_location_expr_complaint ();
16554 }
16555 }
16556 }
16557
16558 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16559 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16560 }
16561 }
16562
16563 /* Create a type for a C++ namespace. */
16564
16565 static struct type *
16566 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16567 {
16568 struct objfile *objfile = cu->per_objfile->objfile;
16569 const char *previous_prefix, *name;
16570 int is_anonymous;
16571 struct type *type;
16572
16573 /* For extensions, reuse the type of the original namespace. */
16574 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16575 {
16576 struct die_info *ext_die;
16577 struct dwarf2_cu *ext_cu = cu;
16578
16579 ext_die = dwarf2_extension (die, &ext_cu);
16580 type = read_type_die (ext_die, ext_cu);
16581
16582 /* EXT_CU may not be the same as CU.
16583 Ensure TYPE is recorded with CU in die_type_hash. */
16584 return set_die_type (die, type, cu);
16585 }
16586
16587 name = namespace_name (die, &is_anonymous, cu);
16588
16589 /* Now build the name of the current namespace. */
16590
16591 previous_prefix = determine_prefix (die, cu);
16592 if (previous_prefix[0] != '\0')
16593 name = typename_concat (&objfile->objfile_obstack,
16594 previous_prefix, name, 0, cu);
16595
16596 /* Create the type. */
16597 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16598
16599 return set_die_type (die, type, cu);
16600 }
16601
16602 /* Read a namespace scope. */
16603
16604 static void
16605 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16606 {
16607 struct objfile *objfile = cu->per_objfile->objfile;
16608 int is_anonymous;
16609
16610 /* Add a symbol associated to this if we haven't seen the namespace
16611 before. Also, add a using directive if it's an anonymous
16612 namespace. */
16613
16614 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16615 {
16616 struct type *type;
16617
16618 type = read_type_die (die, cu);
16619 new_symbol (die, type, cu);
16620
16621 namespace_name (die, &is_anonymous, cu);
16622 if (is_anonymous)
16623 {
16624 const char *previous_prefix = determine_prefix (die, cu);
16625
16626 std::vector<const char *> excludes;
16627 add_using_directive (using_directives (cu),
16628 previous_prefix, type->name (), NULL,
16629 NULL, excludes, 0, &objfile->objfile_obstack);
16630 }
16631 }
16632
16633 if (die->child != NULL)
16634 {
16635 struct die_info *child_die = die->child;
16636
16637 while (child_die && child_die->tag)
16638 {
16639 process_die (child_die, cu);
16640 child_die = child_die->sibling;
16641 }
16642 }
16643 }
16644
16645 /* Read a Fortran module as type. This DIE can be only a declaration used for
16646 imported module. Still we need that type as local Fortran "use ... only"
16647 declaration imports depend on the created type in determine_prefix. */
16648
16649 static struct type *
16650 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16651 {
16652 struct objfile *objfile = cu->per_objfile->objfile;
16653 const char *module_name;
16654 struct type *type;
16655
16656 module_name = dwarf2_name (die, cu);
16657 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16658
16659 return set_die_type (die, type, cu);
16660 }
16661
16662 /* Read a Fortran module. */
16663
16664 static void
16665 read_module (struct die_info *die, struct dwarf2_cu *cu)
16666 {
16667 struct die_info *child_die = die->child;
16668 struct type *type;
16669
16670 type = read_type_die (die, cu);
16671 new_symbol (die, type, cu);
16672
16673 while (child_die && child_die->tag)
16674 {
16675 process_die (child_die, cu);
16676 child_die = child_die->sibling;
16677 }
16678 }
16679
16680 /* Return the name of the namespace represented by DIE. Set
16681 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16682 namespace. */
16683
16684 static const char *
16685 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16686 {
16687 struct die_info *current_die;
16688 const char *name = NULL;
16689
16690 /* Loop through the extensions until we find a name. */
16691
16692 for (current_die = die;
16693 current_die != NULL;
16694 current_die = dwarf2_extension (die, &cu))
16695 {
16696 /* We don't use dwarf2_name here so that we can detect the absence
16697 of a name -> anonymous namespace. */
16698 name = dwarf2_string_attr (die, DW_AT_name, cu);
16699
16700 if (name != NULL)
16701 break;
16702 }
16703
16704 /* Is it an anonymous namespace? */
16705
16706 *is_anonymous = (name == NULL);
16707 if (*is_anonymous)
16708 name = CP_ANONYMOUS_NAMESPACE_STR;
16709
16710 return name;
16711 }
16712
16713 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16714 the user defined type vector. */
16715
16716 static struct type *
16717 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16718 {
16719 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16720 struct comp_unit_head *cu_header = &cu->header;
16721 struct type *type;
16722 struct attribute *attr_byte_size;
16723 struct attribute *attr_address_class;
16724 int byte_size, addr_class;
16725 struct type *target_type;
16726
16727 target_type = die_type (die, cu);
16728
16729 /* The die_type call above may have already set the type for this DIE. */
16730 type = get_die_type (die, cu);
16731 if (type)
16732 return type;
16733
16734 type = lookup_pointer_type (target_type);
16735
16736 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16737 if (attr_byte_size)
16738 byte_size = DW_UNSND (attr_byte_size);
16739 else
16740 byte_size = cu_header->addr_size;
16741
16742 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16743 if (attr_address_class)
16744 addr_class = DW_UNSND (attr_address_class);
16745 else
16746 addr_class = DW_ADDR_none;
16747
16748 ULONGEST alignment = get_alignment (cu, die);
16749
16750 /* If the pointer size, alignment, or address class is different
16751 than the default, create a type variant marked as such and set
16752 the length accordingly. */
16753 if (TYPE_LENGTH (type) != byte_size
16754 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16755 && alignment != TYPE_RAW_ALIGN (type))
16756 || addr_class != DW_ADDR_none)
16757 {
16758 if (gdbarch_address_class_type_flags_p (gdbarch))
16759 {
16760 int type_flags;
16761
16762 type_flags = gdbarch_address_class_type_flags
16763 (gdbarch, byte_size, addr_class);
16764 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16765 == 0);
16766 type = make_type_with_address_space (type, type_flags);
16767 }
16768 else if (TYPE_LENGTH (type) != byte_size)
16769 {
16770 complaint (_("invalid pointer size %d"), byte_size);
16771 }
16772 else if (TYPE_RAW_ALIGN (type) != alignment)
16773 {
16774 complaint (_("Invalid DW_AT_alignment"
16775 " - DIE at %s [in module %s]"),
16776 sect_offset_str (die->sect_off),
16777 objfile_name (cu->per_objfile->objfile));
16778 }
16779 else
16780 {
16781 /* Should we also complain about unhandled address classes? */
16782 }
16783 }
16784
16785 TYPE_LENGTH (type) = byte_size;
16786 set_type_align (type, alignment);
16787 return set_die_type (die, type, cu);
16788 }
16789
16790 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16791 the user defined type vector. */
16792
16793 static struct type *
16794 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16795 {
16796 struct type *type;
16797 struct type *to_type;
16798 struct type *domain;
16799
16800 to_type = die_type (die, cu);
16801 domain = die_containing_type (die, cu);
16802
16803 /* The calls above may have already set the type for this DIE. */
16804 type = get_die_type (die, cu);
16805 if (type)
16806 return type;
16807
16808 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16809 type = lookup_methodptr_type (to_type);
16810 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16811 {
16812 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16813
16814 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16815 to_type->fields (), to_type->num_fields (),
16816 TYPE_VARARGS (to_type));
16817 type = lookup_methodptr_type (new_type);
16818 }
16819 else
16820 type = lookup_memberptr_type (to_type, domain);
16821
16822 return set_die_type (die, type, cu);
16823 }
16824
16825 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16826 the user defined type vector. */
16827
16828 static struct type *
16829 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16830 enum type_code refcode)
16831 {
16832 struct comp_unit_head *cu_header = &cu->header;
16833 struct type *type, *target_type;
16834 struct attribute *attr;
16835
16836 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16837
16838 target_type = die_type (die, cu);
16839
16840 /* The die_type call above may have already set the type for this DIE. */
16841 type = get_die_type (die, cu);
16842 if (type)
16843 return type;
16844
16845 type = lookup_reference_type (target_type, refcode);
16846 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16847 if (attr != nullptr)
16848 {
16849 TYPE_LENGTH (type) = DW_UNSND (attr);
16850 }
16851 else
16852 {
16853 TYPE_LENGTH (type) = cu_header->addr_size;
16854 }
16855 maybe_set_alignment (cu, die, type);
16856 return set_die_type (die, type, cu);
16857 }
16858
16859 /* Add the given cv-qualifiers to the element type of the array. GCC
16860 outputs DWARF type qualifiers that apply to an array, not the
16861 element type. But GDB relies on the array element type to carry
16862 the cv-qualifiers. This mimics section 6.7.3 of the C99
16863 specification. */
16864
16865 static struct type *
16866 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16867 struct type *base_type, int cnst, int voltl)
16868 {
16869 struct type *el_type, *inner_array;
16870
16871 base_type = copy_type (base_type);
16872 inner_array = base_type;
16873
16874 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16875 {
16876 TYPE_TARGET_TYPE (inner_array) =
16877 copy_type (TYPE_TARGET_TYPE (inner_array));
16878 inner_array = TYPE_TARGET_TYPE (inner_array);
16879 }
16880
16881 el_type = TYPE_TARGET_TYPE (inner_array);
16882 cnst |= TYPE_CONST (el_type);
16883 voltl |= TYPE_VOLATILE (el_type);
16884 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16885
16886 return set_die_type (die, base_type, cu);
16887 }
16888
16889 static struct type *
16890 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16891 {
16892 struct type *base_type, *cv_type;
16893
16894 base_type = die_type (die, cu);
16895
16896 /* The die_type call above may have already set the type for this DIE. */
16897 cv_type = get_die_type (die, cu);
16898 if (cv_type)
16899 return cv_type;
16900
16901 /* In case the const qualifier is applied to an array type, the element type
16902 is so qualified, not the array type (section 6.7.3 of C99). */
16903 if (base_type->code () == TYPE_CODE_ARRAY)
16904 return add_array_cv_type (die, cu, base_type, 1, 0);
16905
16906 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16907 return set_die_type (die, cv_type, cu);
16908 }
16909
16910 static struct type *
16911 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16912 {
16913 struct type *base_type, *cv_type;
16914
16915 base_type = die_type (die, cu);
16916
16917 /* The die_type call above may have already set the type for this DIE. */
16918 cv_type = get_die_type (die, cu);
16919 if (cv_type)
16920 return cv_type;
16921
16922 /* In case the volatile qualifier is applied to an array type, the
16923 element type is so qualified, not the array type (section 6.7.3
16924 of C99). */
16925 if (base_type->code () == TYPE_CODE_ARRAY)
16926 return add_array_cv_type (die, cu, base_type, 0, 1);
16927
16928 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16929 return set_die_type (die, cv_type, cu);
16930 }
16931
16932 /* Handle DW_TAG_restrict_type. */
16933
16934 static struct type *
16935 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16936 {
16937 struct type *base_type, *cv_type;
16938
16939 base_type = die_type (die, cu);
16940
16941 /* The die_type call above may have already set the type for this DIE. */
16942 cv_type = get_die_type (die, cu);
16943 if (cv_type)
16944 return cv_type;
16945
16946 cv_type = make_restrict_type (base_type);
16947 return set_die_type (die, cv_type, cu);
16948 }
16949
16950 /* Handle DW_TAG_atomic_type. */
16951
16952 static struct type *
16953 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16954 {
16955 struct type *base_type, *cv_type;
16956
16957 base_type = die_type (die, cu);
16958
16959 /* The die_type call above may have already set the type for this DIE. */
16960 cv_type = get_die_type (die, cu);
16961 if (cv_type)
16962 return cv_type;
16963
16964 cv_type = make_atomic_type (base_type);
16965 return set_die_type (die, cv_type, cu);
16966 }
16967
16968 /* Extract all information from a DW_TAG_string_type DIE and add to
16969 the user defined type vector. It isn't really a user defined type,
16970 but it behaves like one, with other DIE's using an AT_user_def_type
16971 attribute to reference it. */
16972
16973 static struct type *
16974 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16975 {
16976 struct objfile *objfile = cu->per_objfile->objfile;
16977 struct gdbarch *gdbarch = objfile->arch ();
16978 struct type *type, *range_type, *index_type, *char_type;
16979 struct attribute *attr;
16980 struct dynamic_prop prop;
16981 bool length_is_constant = true;
16982 LONGEST length;
16983
16984 /* There are a couple of places where bit sizes might be made use of
16985 when parsing a DW_TAG_string_type, however, no producer that we know
16986 of make use of these. Handling bit sizes that are a multiple of the
16987 byte size is easy enough, but what about other bit sizes? Lets deal
16988 with that problem when we have to. Warn about these attributes being
16989 unsupported, then parse the type and ignore them like we always
16990 have. */
16991 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16992 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16993 {
16994 static bool warning_printed = false;
16995 if (!warning_printed)
16996 {
16997 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16998 "currently supported on DW_TAG_string_type."));
16999 warning_printed = true;
17000 }
17001 }
17002
17003 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17004 if (attr != nullptr && !attr->form_is_constant ())
17005 {
17006 /* The string length describes the location at which the length of
17007 the string can be found. The size of the length field can be
17008 specified with one of the attributes below. */
17009 struct type *prop_type;
17010 struct attribute *len
17011 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17012 if (len == nullptr)
17013 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17014 if (len != nullptr && len->form_is_constant ())
17015 {
17016 /* Pass 0 as the default as we know this attribute is constant
17017 and the default value will not be returned. */
17018 LONGEST sz = len->constant_value (0);
17019 prop_type = cu->per_objfile->int_type (sz, true);
17020 }
17021 else
17022 {
17023 /* If the size is not specified then we assume it is the size of
17024 an address on this target. */
17025 prop_type = cu->addr_sized_int_type (true);
17026 }
17027
17028 /* Convert the attribute into a dynamic property. */
17029 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17030 length = 1;
17031 else
17032 length_is_constant = false;
17033 }
17034 else if (attr != nullptr)
17035 {
17036 /* This DW_AT_string_length just contains the length with no
17037 indirection. There's no need to create a dynamic property in this
17038 case. Pass 0 for the default value as we know it will not be
17039 returned in this case. */
17040 length = attr->constant_value (0);
17041 }
17042 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17043 {
17044 /* We don't currently support non-constant byte sizes for strings. */
17045 length = attr->constant_value (1);
17046 }
17047 else
17048 {
17049 /* Use 1 as a fallback length if we have nothing else. */
17050 length = 1;
17051 }
17052
17053 index_type = objfile_type (objfile)->builtin_int;
17054 if (length_is_constant)
17055 range_type = create_static_range_type (NULL, index_type, 1, length);
17056 else
17057 {
17058 struct dynamic_prop low_bound;
17059
17060 low_bound.kind = PROP_CONST;
17061 low_bound.data.const_val = 1;
17062 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17063 }
17064 char_type = language_string_char_type (cu->language_defn, gdbarch);
17065 type = create_string_type (NULL, char_type, range_type);
17066
17067 return set_die_type (die, type, cu);
17068 }
17069
17070 /* Assuming that DIE corresponds to a function, returns nonzero
17071 if the function is prototyped. */
17072
17073 static int
17074 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17075 {
17076 struct attribute *attr;
17077
17078 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17079 if (attr && (DW_UNSND (attr) != 0))
17080 return 1;
17081
17082 /* The DWARF standard implies that the DW_AT_prototyped attribute
17083 is only meaningful for C, but the concept also extends to other
17084 languages that allow unprototyped functions (Eg: Objective C).
17085 For all other languages, assume that functions are always
17086 prototyped. */
17087 if (cu->language != language_c
17088 && cu->language != language_objc
17089 && cu->language != language_opencl)
17090 return 1;
17091
17092 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17093 prototyped and unprototyped functions; default to prototyped,
17094 since that is more common in modern code (and RealView warns
17095 about unprototyped functions). */
17096 if (producer_is_realview (cu->producer))
17097 return 1;
17098
17099 return 0;
17100 }
17101
17102 /* Handle DIES due to C code like:
17103
17104 struct foo
17105 {
17106 int (*funcp)(int a, long l);
17107 int b;
17108 };
17109
17110 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17111
17112 static struct type *
17113 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17114 {
17115 struct objfile *objfile = cu->per_objfile->objfile;
17116 struct type *type; /* Type that this function returns. */
17117 struct type *ftype; /* Function that returns above type. */
17118 struct attribute *attr;
17119
17120 type = die_type (die, cu);
17121
17122 /* The die_type call above may have already set the type for this DIE. */
17123 ftype = get_die_type (die, cu);
17124 if (ftype)
17125 return ftype;
17126
17127 ftype = lookup_function_type (type);
17128
17129 if (prototyped_function_p (die, cu))
17130 TYPE_PROTOTYPED (ftype) = 1;
17131
17132 /* Store the calling convention in the type if it's available in
17133 the subroutine die. Otherwise set the calling convention to
17134 the default value DW_CC_normal. */
17135 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17136 if (attr != nullptr
17137 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17138 TYPE_CALLING_CONVENTION (ftype)
17139 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17140 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17141 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17142 else
17143 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17144
17145 /* Record whether the function returns normally to its caller or not
17146 if the DWARF producer set that information. */
17147 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17148 if (attr && (DW_UNSND (attr) != 0))
17149 TYPE_NO_RETURN (ftype) = 1;
17150
17151 /* We need to add the subroutine type to the die immediately so
17152 we don't infinitely recurse when dealing with parameters
17153 declared as the same subroutine type. */
17154 set_die_type (die, ftype, cu);
17155
17156 if (die->child != NULL)
17157 {
17158 struct type *void_type = objfile_type (objfile)->builtin_void;
17159 struct die_info *child_die;
17160 int nparams, iparams;
17161
17162 /* Count the number of parameters.
17163 FIXME: GDB currently ignores vararg functions, but knows about
17164 vararg member functions. */
17165 nparams = 0;
17166 child_die = die->child;
17167 while (child_die && child_die->tag)
17168 {
17169 if (child_die->tag == DW_TAG_formal_parameter)
17170 nparams++;
17171 else if (child_die->tag == DW_TAG_unspecified_parameters)
17172 TYPE_VARARGS (ftype) = 1;
17173 child_die = child_die->sibling;
17174 }
17175
17176 /* Allocate storage for parameters and fill them in. */
17177 ftype->set_num_fields (nparams);
17178 ftype->set_fields
17179 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17180
17181 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17182 even if we error out during the parameters reading below. */
17183 for (iparams = 0; iparams < nparams; iparams++)
17184 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17185
17186 iparams = 0;
17187 child_die = die->child;
17188 while (child_die && child_die->tag)
17189 {
17190 if (child_die->tag == DW_TAG_formal_parameter)
17191 {
17192 struct type *arg_type;
17193
17194 /* DWARF version 2 has no clean way to discern C++
17195 static and non-static member functions. G++ helps
17196 GDB by marking the first parameter for non-static
17197 member functions (which is the this pointer) as
17198 artificial. We pass this information to
17199 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17200
17201 DWARF version 3 added DW_AT_object_pointer, which GCC
17202 4.5 does not yet generate. */
17203 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17204 if (attr != nullptr)
17205 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17206 else
17207 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17208 arg_type = die_type (child_die, cu);
17209
17210 /* RealView does not mark THIS as const, which the testsuite
17211 expects. GCC marks THIS as const in method definitions,
17212 but not in the class specifications (GCC PR 43053). */
17213 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17214 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17215 {
17216 int is_this = 0;
17217 struct dwarf2_cu *arg_cu = cu;
17218 const char *name = dwarf2_name (child_die, cu);
17219
17220 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17221 if (attr != nullptr)
17222 {
17223 /* If the compiler emits this, use it. */
17224 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17225 is_this = 1;
17226 }
17227 else if (name && strcmp (name, "this") == 0)
17228 /* Function definitions will have the argument names. */
17229 is_this = 1;
17230 else if (name == NULL && iparams == 0)
17231 /* Declarations may not have the names, so like
17232 elsewhere in GDB, assume an artificial first
17233 argument is "this". */
17234 is_this = 1;
17235
17236 if (is_this)
17237 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17238 arg_type, 0);
17239 }
17240
17241 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17242 iparams++;
17243 }
17244 child_die = child_die->sibling;
17245 }
17246 }
17247
17248 return ftype;
17249 }
17250
17251 static struct type *
17252 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17253 {
17254 struct objfile *objfile = cu->per_objfile->objfile;
17255 const char *name = NULL;
17256 struct type *this_type, *target_type;
17257
17258 name = dwarf2_full_name (NULL, die, cu);
17259 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17260 TYPE_TARGET_STUB (this_type) = 1;
17261 set_die_type (die, this_type, cu);
17262 target_type = die_type (die, cu);
17263 if (target_type != this_type)
17264 TYPE_TARGET_TYPE (this_type) = target_type;
17265 else
17266 {
17267 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17268 spec and cause infinite loops in GDB. */
17269 complaint (_("Self-referential DW_TAG_typedef "
17270 "- DIE at %s [in module %s]"),
17271 sect_offset_str (die->sect_off), objfile_name (objfile));
17272 TYPE_TARGET_TYPE (this_type) = NULL;
17273 }
17274 if (name == NULL)
17275 {
17276 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17277 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17278 Handle these by just returning the target type, rather than
17279 constructing an anonymous typedef type and trying to handle this
17280 elsewhere. */
17281 set_die_type (die, target_type, cu);
17282 return target_type;
17283 }
17284 return this_type;
17285 }
17286
17287 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17288 (which may be different from NAME) to the architecture back-end to allow
17289 it to guess the correct format if necessary. */
17290
17291 static struct type *
17292 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17293 const char *name_hint, enum bfd_endian byte_order)
17294 {
17295 struct gdbarch *gdbarch = objfile->arch ();
17296 const struct floatformat **format;
17297 struct type *type;
17298
17299 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17300 if (format)
17301 type = init_float_type (objfile, bits, name, format, byte_order);
17302 else
17303 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17304
17305 return type;
17306 }
17307
17308 /* Allocate an integer type of size BITS and name NAME. */
17309
17310 static struct type *
17311 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17312 int bits, int unsigned_p, const char *name)
17313 {
17314 struct type *type;
17315
17316 /* Versions of Intel's C Compiler generate an integer type called "void"
17317 instead of using DW_TAG_unspecified_type. This has been seen on
17318 at least versions 14, 17, and 18. */
17319 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17320 && strcmp (name, "void") == 0)
17321 type = objfile_type (objfile)->builtin_void;
17322 else
17323 type = init_integer_type (objfile, bits, unsigned_p, name);
17324
17325 return type;
17326 }
17327
17328 /* Initialise and return a floating point type of size BITS suitable for
17329 use as a component of a complex number. The NAME_HINT is passed through
17330 when initialising the floating point type and is the name of the complex
17331 type.
17332
17333 As DWARF doesn't currently provide an explicit name for the components
17334 of a complex number, but it can be helpful to have these components
17335 named, we try to select a suitable name based on the size of the
17336 component. */
17337 static struct type *
17338 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17339 struct objfile *objfile,
17340 int bits, const char *name_hint,
17341 enum bfd_endian byte_order)
17342 {
17343 gdbarch *gdbarch = objfile->arch ();
17344 struct type *tt = nullptr;
17345
17346 /* Try to find a suitable floating point builtin type of size BITS.
17347 We're going to use the name of this type as the name for the complex
17348 target type that we are about to create. */
17349 switch (cu->language)
17350 {
17351 case language_fortran:
17352 switch (bits)
17353 {
17354 case 32:
17355 tt = builtin_f_type (gdbarch)->builtin_real;
17356 break;
17357 case 64:
17358 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17359 break;
17360 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17361 case 128:
17362 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17363 break;
17364 }
17365 break;
17366 default:
17367 switch (bits)
17368 {
17369 case 32:
17370 tt = builtin_type (gdbarch)->builtin_float;
17371 break;
17372 case 64:
17373 tt = builtin_type (gdbarch)->builtin_double;
17374 break;
17375 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17376 case 128:
17377 tt = builtin_type (gdbarch)->builtin_long_double;
17378 break;
17379 }
17380 break;
17381 }
17382
17383 /* If the type we found doesn't match the size we were looking for, then
17384 pretend we didn't find a type at all, the complex target type we
17385 create will then be nameless. */
17386 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17387 tt = nullptr;
17388
17389 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17390 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17391 }
17392
17393 /* Find a representation of a given base type and install
17394 it in the TYPE field of the die. */
17395
17396 static struct type *
17397 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17398 {
17399 struct objfile *objfile = cu->per_objfile->objfile;
17400 struct type *type;
17401 struct attribute *attr;
17402 int encoding = 0, bits = 0;
17403 const char *name;
17404 gdbarch *arch;
17405
17406 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17407 if (attr != nullptr)
17408 encoding = DW_UNSND (attr);
17409 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17410 if (attr != nullptr)
17411 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17412 name = dwarf2_name (die, cu);
17413 if (!name)
17414 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17415
17416 arch = objfile->arch ();
17417 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17418
17419 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17420 if (attr)
17421 {
17422 int endianity = DW_UNSND (attr);
17423
17424 switch (endianity)
17425 {
17426 case DW_END_big:
17427 byte_order = BFD_ENDIAN_BIG;
17428 break;
17429 case DW_END_little:
17430 byte_order = BFD_ENDIAN_LITTLE;
17431 break;
17432 default:
17433 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17434 break;
17435 }
17436 }
17437
17438 switch (encoding)
17439 {
17440 case DW_ATE_address:
17441 /* Turn DW_ATE_address into a void * pointer. */
17442 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17443 type = init_pointer_type (objfile, bits, name, type);
17444 break;
17445 case DW_ATE_boolean:
17446 type = init_boolean_type (objfile, bits, 1, name);
17447 break;
17448 case DW_ATE_complex_float:
17449 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17450 byte_order);
17451 if (type->code () == TYPE_CODE_ERROR)
17452 {
17453 if (name == nullptr)
17454 {
17455 struct obstack *obstack
17456 = &cu->per_objfile->objfile->objfile_obstack;
17457 name = obconcat (obstack, "_Complex ", type->name (),
17458 nullptr);
17459 }
17460 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17461 }
17462 else
17463 type = init_complex_type (name, type);
17464 break;
17465 case DW_ATE_decimal_float:
17466 type = init_decfloat_type (objfile, bits, name);
17467 break;
17468 case DW_ATE_float:
17469 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17470 break;
17471 case DW_ATE_signed:
17472 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17473 break;
17474 case DW_ATE_unsigned:
17475 if (cu->language == language_fortran
17476 && name
17477 && startswith (name, "character("))
17478 type = init_character_type (objfile, bits, 1, name);
17479 else
17480 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17481 break;
17482 case DW_ATE_signed_char:
17483 if (cu->language == language_ada || cu->language == language_m2
17484 || cu->language == language_pascal
17485 || cu->language == language_fortran)
17486 type = init_character_type (objfile, bits, 0, name);
17487 else
17488 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17489 break;
17490 case DW_ATE_unsigned_char:
17491 if (cu->language == language_ada || cu->language == language_m2
17492 || cu->language == language_pascal
17493 || cu->language == language_fortran
17494 || cu->language == language_rust)
17495 type = init_character_type (objfile, bits, 1, name);
17496 else
17497 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17498 break;
17499 case DW_ATE_UTF:
17500 {
17501 if (bits == 16)
17502 type = builtin_type (arch)->builtin_char16;
17503 else if (bits == 32)
17504 type = builtin_type (arch)->builtin_char32;
17505 else
17506 {
17507 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17508 bits);
17509 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17510 }
17511 return set_die_type (die, type, cu);
17512 }
17513 break;
17514
17515 default:
17516 complaint (_("unsupported DW_AT_encoding: '%s'"),
17517 dwarf_type_encoding_name (encoding));
17518 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17519 break;
17520 }
17521
17522 if (name && strcmp (name, "char") == 0)
17523 TYPE_NOSIGN (type) = 1;
17524
17525 maybe_set_alignment (cu, die, type);
17526
17527 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17528
17529 return set_die_type (die, type, cu);
17530 }
17531
17532 /* Parse dwarf attribute if it's a block, reference or constant and put the
17533 resulting value of the attribute into struct bound_prop.
17534 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17535
17536 static int
17537 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17538 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17539 struct type *default_type)
17540 {
17541 struct dwarf2_property_baton *baton;
17542 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17543 struct objfile *objfile = per_objfile->objfile;
17544 struct obstack *obstack = &objfile->objfile_obstack;
17545
17546 gdb_assert (default_type != NULL);
17547
17548 if (attr == NULL || prop == NULL)
17549 return 0;
17550
17551 if (attr->form_is_block ())
17552 {
17553 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17554 baton->property_type = default_type;
17555 baton->locexpr.per_cu = cu->per_cu;
17556 baton->locexpr.per_objfile = per_objfile;
17557 baton->locexpr.size = DW_BLOCK (attr)->size;
17558 baton->locexpr.data = DW_BLOCK (attr)->data;
17559 switch (attr->name)
17560 {
17561 case DW_AT_string_length:
17562 baton->locexpr.is_reference = true;
17563 break;
17564 default:
17565 baton->locexpr.is_reference = false;
17566 break;
17567 }
17568 prop->data.baton = baton;
17569 prop->kind = PROP_LOCEXPR;
17570 gdb_assert (prop->data.baton != NULL);
17571 }
17572 else if (attr->form_is_ref ())
17573 {
17574 struct dwarf2_cu *target_cu = cu;
17575 struct die_info *target_die;
17576 struct attribute *target_attr;
17577
17578 target_die = follow_die_ref (die, attr, &target_cu);
17579 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17580 if (target_attr == NULL)
17581 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17582 target_cu);
17583 if (target_attr == NULL)
17584 return 0;
17585
17586 switch (target_attr->name)
17587 {
17588 case DW_AT_location:
17589 if (target_attr->form_is_section_offset ())
17590 {
17591 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17592 baton->property_type = die_type (target_die, target_cu);
17593 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17594 prop->data.baton = baton;
17595 prop->kind = PROP_LOCLIST;
17596 gdb_assert (prop->data.baton != NULL);
17597 }
17598 else if (target_attr->form_is_block ())
17599 {
17600 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17601 baton->property_type = die_type (target_die, target_cu);
17602 baton->locexpr.per_cu = cu->per_cu;
17603 baton->locexpr.per_objfile = per_objfile;
17604 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17605 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17606 baton->locexpr.is_reference = true;
17607 prop->data.baton = baton;
17608 prop->kind = PROP_LOCEXPR;
17609 gdb_assert (prop->data.baton != NULL);
17610 }
17611 else
17612 {
17613 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17614 "dynamic property");
17615 return 0;
17616 }
17617 break;
17618 case DW_AT_data_member_location:
17619 {
17620 LONGEST offset;
17621
17622 if (!handle_data_member_location (target_die, target_cu,
17623 &offset))
17624 return 0;
17625
17626 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17627 baton->property_type = read_type_die (target_die->parent,
17628 target_cu);
17629 baton->offset_info.offset = offset;
17630 baton->offset_info.type = die_type (target_die, target_cu);
17631 prop->data.baton = baton;
17632 prop->kind = PROP_ADDR_OFFSET;
17633 break;
17634 }
17635 }
17636 }
17637 else if (attr->form_is_constant ())
17638 {
17639 prop->data.const_val = attr->constant_value (0);
17640 prop->kind = PROP_CONST;
17641 }
17642 else
17643 {
17644 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17645 dwarf2_name (die, cu));
17646 return 0;
17647 }
17648
17649 return 1;
17650 }
17651
17652 /* See read.h. */
17653
17654 struct type *
17655 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17656 {
17657 struct type *int_type;
17658
17659 /* Helper macro to examine the various builtin types. */
17660 #define TRY_TYPE(F) \
17661 int_type = (unsigned_p \
17662 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17663 : objfile_type (objfile)->builtin_ ## F); \
17664 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17665 return int_type
17666
17667 TRY_TYPE (char);
17668 TRY_TYPE (short);
17669 TRY_TYPE (int);
17670 TRY_TYPE (long);
17671 TRY_TYPE (long_long);
17672
17673 #undef TRY_TYPE
17674
17675 gdb_assert_not_reached ("unable to find suitable integer type");
17676 }
17677
17678 /* See read.h. */
17679
17680 struct type *
17681 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17682 {
17683 int addr_size = this->per_cu->addr_size ();
17684 return this->per_objfile->int_type (addr_size, unsigned_p);
17685 }
17686
17687 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17688 present (which is valid) then compute the default type based on the
17689 compilation units address size. */
17690
17691 static struct type *
17692 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17693 {
17694 struct type *index_type = die_type (die, cu);
17695
17696 /* Dwarf-2 specifications explicitly allows to create subrange types
17697 without specifying a base type.
17698 In that case, the base type must be set to the type of
17699 the lower bound, upper bound or count, in that order, if any of these
17700 three attributes references an object that has a type.
17701 If no base type is found, the Dwarf-2 specifications say that
17702 a signed integer type of size equal to the size of an address should
17703 be used.
17704 For the following C code: `extern char gdb_int [];'
17705 GCC produces an empty range DIE.
17706 FIXME: muller/2010-05-28: Possible references to object for low bound,
17707 high bound or count are not yet handled by this code. */
17708 if (index_type->code () == TYPE_CODE_VOID)
17709 index_type = cu->addr_sized_int_type (false);
17710
17711 return index_type;
17712 }
17713
17714 /* Read the given DW_AT_subrange DIE. */
17715
17716 static struct type *
17717 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17718 {
17719 struct type *base_type, *orig_base_type;
17720 struct type *range_type;
17721 struct attribute *attr;
17722 struct dynamic_prop low, high;
17723 int low_default_is_valid;
17724 int high_bound_is_count = 0;
17725 const char *name;
17726 ULONGEST negative_mask;
17727
17728 orig_base_type = read_subrange_index_type (die, cu);
17729
17730 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17731 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17732 creating the range type, but we use the result of check_typedef
17733 when examining properties of the type. */
17734 base_type = check_typedef (orig_base_type);
17735
17736 /* The die_type call above may have already set the type for this DIE. */
17737 range_type = get_die_type (die, cu);
17738 if (range_type)
17739 return range_type;
17740
17741 low.kind = PROP_CONST;
17742 high.kind = PROP_CONST;
17743 high.data.const_val = 0;
17744
17745 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17746 omitting DW_AT_lower_bound. */
17747 switch (cu->language)
17748 {
17749 case language_c:
17750 case language_cplus:
17751 low.data.const_val = 0;
17752 low_default_is_valid = 1;
17753 break;
17754 case language_fortran:
17755 low.data.const_val = 1;
17756 low_default_is_valid = 1;
17757 break;
17758 case language_d:
17759 case language_objc:
17760 case language_rust:
17761 low.data.const_val = 0;
17762 low_default_is_valid = (cu->header.version >= 4);
17763 break;
17764 case language_ada:
17765 case language_m2:
17766 case language_pascal:
17767 low.data.const_val = 1;
17768 low_default_is_valid = (cu->header.version >= 4);
17769 break;
17770 default:
17771 low.data.const_val = 0;
17772 low_default_is_valid = 0;
17773 break;
17774 }
17775
17776 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17777 if (attr != nullptr)
17778 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17779 else if (!low_default_is_valid)
17780 complaint (_("Missing DW_AT_lower_bound "
17781 "- DIE at %s [in module %s]"),
17782 sect_offset_str (die->sect_off),
17783 objfile_name (cu->per_objfile->objfile));
17784
17785 struct attribute *attr_ub, *attr_count;
17786 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17787 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17788 {
17789 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17790 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17791 {
17792 /* If bounds are constant do the final calculation here. */
17793 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17794 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17795 else
17796 high_bound_is_count = 1;
17797 }
17798 else
17799 {
17800 if (attr_ub != NULL)
17801 complaint (_("Unresolved DW_AT_upper_bound "
17802 "- DIE at %s [in module %s]"),
17803 sect_offset_str (die->sect_off),
17804 objfile_name (cu->per_objfile->objfile));
17805 if (attr_count != NULL)
17806 complaint (_("Unresolved DW_AT_count "
17807 "- DIE at %s [in module %s]"),
17808 sect_offset_str (die->sect_off),
17809 objfile_name (cu->per_objfile->objfile));
17810 }
17811 }
17812
17813 LONGEST bias = 0;
17814 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17815 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17816 bias = bias_attr->constant_value (0);
17817
17818 /* Normally, the DWARF producers are expected to use a signed
17819 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17820 But this is unfortunately not always the case, as witnessed
17821 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17822 is used instead. To work around that ambiguity, we treat
17823 the bounds as signed, and thus sign-extend their values, when
17824 the base type is signed. */
17825 negative_mask =
17826 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17827 if (low.kind == PROP_CONST
17828 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17829 low.data.const_val |= negative_mask;
17830 if (high.kind == PROP_CONST
17831 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17832 high.data.const_val |= negative_mask;
17833
17834 /* Check for bit and byte strides. */
17835 struct dynamic_prop byte_stride_prop;
17836 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17837 if (attr_byte_stride != nullptr)
17838 {
17839 struct type *prop_type = cu->addr_sized_int_type (false);
17840 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17841 prop_type);
17842 }
17843
17844 struct dynamic_prop bit_stride_prop;
17845 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17846 if (attr_bit_stride != nullptr)
17847 {
17848 /* It only makes sense to have either a bit or byte stride. */
17849 if (attr_byte_stride != nullptr)
17850 {
17851 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17852 "- DIE at %s [in module %s]"),
17853 sect_offset_str (die->sect_off),
17854 objfile_name (cu->per_objfile->objfile));
17855 attr_bit_stride = nullptr;
17856 }
17857 else
17858 {
17859 struct type *prop_type = cu->addr_sized_int_type (false);
17860 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17861 prop_type);
17862 }
17863 }
17864
17865 if (attr_byte_stride != nullptr
17866 || attr_bit_stride != nullptr)
17867 {
17868 bool byte_stride_p = (attr_byte_stride != nullptr);
17869 struct dynamic_prop *stride
17870 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17871
17872 range_type
17873 = create_range_type_with_stride (NULL, orig_base_type, &low,
17874 &high, bias, stride, byte_stride_p);
17875 }
17876 else
17877 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17878
17879 if (high_bound_is_count)
17880 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17881
17882 /* Ada expects an empty array on no boundary attributes. */
17883 if (attr == NULL && cu->language != language_ada)
17884 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17885
17886 name = dwarf2_name (die, cu);
17887 if (name)
17888 range_type->set_name (name);
17889
17890 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17891 if (attr != nullptr)
17892 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17893
17894 maybe_set_alignment (cu, die, range_type);
17895
17896 set_die_type (die, range_type, cu);
17897
17898 /* set_die_type should be already done. */
17899 set_descriptive_type (range_type, die, cu);
17900
17901 return range_type;
17902 }
17903
17904 static struct type *
17905 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17906 {
17907 struct type *type;
17908
17909 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17910 type->set_name (dwarf2_name (die, cu));
17911
17912 /* In Ada, an unspecified type is typically used when the description
17913 of the type is deferred to a different unit. When encountering
17914 such a type, we treat it as a stub, and try to resolve it later on,
17915 when needed. */
17916 if (cu->language == language_ada)
17917 TYPE_STUB (type) = 1;
17918
17919 return set_die_type (die, type, cu);
17920 }
17921
17922 /* Read a single die and all its descendents. Set the die's sibling
17923 field to NULL; set other fields in the die correctly, and set all
17924 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17925 location of the info_ptr after reading all of those dies. PARENT
17926 is the parent of the die in question. */
17927
17928 static struct die_info *
17929 read_die_and_children (const struct die_reader_specs *reader,
17930 const gdb_byte *info_ptr,
17931 const gdb_byte **new_info_ptr,
17932 struct die_info *parent)
17933 {
17934 struct die_info *die;
17935 const gdb_byte *cur_ptr;
17936
17937 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17938 if (die == NULL)
17939 {
17940 *new_info_ptr = cur_ptr;
17941 return NULL;
17942 }
17943 store_in_ref_table (die, reader->cu);
17944
17945 if (die->has_children)
17946 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17947 else
17948 {
17949 die->child = NULL;
17950 *new_info_ptr = cur_ptr;
17951 }
17952
17953 die->sibling = NULL;
17954 die->parent = parent;
17955 return die;
17956 }
17957
17958 /* Read a die, all of its descendents, and all of its siblings; set
17959 all of the fields of all of the dies correctly. Arguments are as
17960 in read_die_and_children. */
17961
17962 static struct die_info *
17963 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17964 const gdb_byte *info_ptr,
17965 const gdb_byte **new_info_ptr,
17966 struct die_info *parent)
17967 {
17968 struct die_info *first_die, *last_sibling;
17969 const gdb_byte *cur_ptr;
17970
17971 cur_ptr = info_ptr;
17972 first_die = last_sibling = NULL;
17973
17974 while (1)
17975 {
17976 struct die_info *die
17977 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17978
17979 if (die == NULL)
17980 {
17981 *new_info_ptr = cur_ptr;
17982 return first_die;
17983 }
17984
17985 if (!first_die)
17986 first_die = die;
17987 else
17988 last_sibling->sibling = die;
17989
17990 last_sibling = die;
17991 }
17992 }
17993
17994 /* Read a die, all of its descendents, and all of its siblings; set
17995 all of the fields of all of the dies correctly. Arguments are as
17996 in read_die_and_children.
17997 This the main entry point for reading a DIE and all its children. */
17998
17999 static struct die_info *
18000 read_die_and_siblings (const struct die_reader_specs *reader,
18001 const gdb_byte *info_ptr,
18002 const gdb_byte **new_info_ptr,
18003 struct die_info *parent)
18004 {
18005 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18006 new_info_ptr, parent);
18007
18008 if (dwarf_die_debug)
18009 {
18010 fprintf_unfiltered (gdb_stdlog,
18011 "Read die from %s@0x%x of %s:\n",
18012 reader->die_section->get_name (),
18013 (unsigned) (info_ptr - reader->die_section->buffer),
18014 bfd_get_filename (reader->abfd));
18015 dump_die (die, dwarf_die_debug);
18016 }
18017
18018 return die;
18019 }
18020
18021 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18022 attributes.
18023 The caller is responsible for filling in the extra attributes
18024 and updating (*DIEP)->num_attrs.
18025 Set DIEP to point to a newly allocated die with its information,
18026 except for its child, sibling, and parent fields. */
18027
18028 static const gdb_byte *
18029 read_full_die_1 (const struct die_reader_specs *reader,
18030 struct die_info **diep, const gdb_byte *info_ptr,
18031 int num_extra_attrs)
18032 {
18033 unsigned int abbrev_number, bytes_read, i;
18034 struct abbrev_info *abbrev;
18035 struct die_info *die;
18036 struct dwarf2_cu *cu = reader->cu;
18037 bfd *abfd = reader->abfd;
18038
18039 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18040 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18041 info_ptr += bytes_read;
18042 if (!abbrev_number)
18043 {
18044 *diep = NULL;
18045 return info_ptr;
18046 }
18047
18048 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18049 if (!abbrev)
18050 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18051 abbrev_number,
18052 bfd_get_filename (abfd));
18053
18054 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18055 die->sect_off = sect_off;
18056 die->tag = abbrev->tag;
18057 die->abbrev = abbrev_number;
18058 die->has_children = abbrev->has_children;
18059
18060 /* Make the result usable.
18061 The caller needs to update num_attrs after adding the extra
18062 attributes. */
18063 die->num_attrs = abbrev->num_attrs;
18064
18065 std::vector<int> indexes_that_need_reprocess;
18066 for (i = 0; i < abbrev->num_attrs; ++i)
18067 {
18068 bool need_reprocess;
18069 info_ptr =
18070 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18071 info_ptr, &need_reprocess);
18072 if (need_reprocess)
18073 indexes_that_need_reprocess.push_back (i);
18074 }
18075
18076 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18077 if (attr != nullptr)
18078 cu->str_offsets_base = DW_UNSND (attr);
18079
18080 attr = die->attr (DW_AT_loclists_base);
18081 if (attr != nullptr)
18082 cu->loclist_base = DW_UNSND (attr);
18083
18084 auto maybe_addr_base = die->addr_base ();
18085 if (maybe_addr_base.has_value ())
18086 cu->addr_base = *maybe_addr_base;
18087 for (int index : indexes_that_need_reprocess)
18088 read_attribute_reprocess (reader, &die->attrs[index]);
18089 *diep = die;
18090 return info_ptr;
18091 }
18092
18093 /* Read a die and all its attributes.
18094 Set DIEP to point to a newly allocated die with its information,
18095 except for its child, sibling, and parent fields. */
18096
18097 static const gdb_byte *
18098 read_full_die (const struct die_reader_specs *reader,
18099 struct die_info **diep, const gdb_byte *info_ptr)
18100 {
18101 const gdb_byte *result;
18102
18103 result = read_full_die_1 (reader, diep, info_ptr, 0);
18104
18105 if (dwarf_die_debug)
18106 {
18107 fprintf_unfiltered (gdb_stdlog,
18108 "Read die from %s@0x%x of %s:\n",
18109 reader->die_section->get_name (),
18110 (unsigned) (info_ptr - reader->die_section->buffer),
18111 bfd_get_filename (reader->abfd));
18112 dump_die (*diep, dwarf_die_debug);
18113 }
18114
18115 return result;
18116 }
18117 \f
18118
18119 /* Returns nonzero if TAG represents a type that we might generate a partial
18120 symbol for. */
18121
18122 static int
18123 is_type_tag_for_partial (int tag)
18124 {
18125 switch (tag)
18126 {
18127 #if 0
18128 /* Some types that would be reasonable to generate partial symbols for,
18129 that we don't at present. */
18130 case DW_TAG_array_type:
18131 case DW_TAG_file_type:
18132 case DW_TAG_ptr_to_member_type:
18133 case DW_TAG_set_type:
18134 case DW_TAG_string_type:
18135 case DW_TAG_subroutine_type:
18136 #endif
18137 case DW_TAG_base_type:
18138 case DW_TAG_class_type:
18139 case DW_TAG_interface_type:
18140 case DW_TAG_enumeration_type:
18141 case DW_TAG_structure_type:
18142 case DW_TAG_subrange_type:
18143 case DW_TAG_typedef:
18144 case DW_TAG_union_type:
18145 return 1;
18146 default:
18147 return 0;
18148 }
18149 }
18150
18151 /* Load all DIEs that are interesting for partial symbols into memory. */
18152
18153 static struct partial_die_info *
18154 load_partial_dies (const struct die_reader_specs *reader,
18155 const gdb_byte *info_ptr, int building_psymtab)
18156 {
18157 struct dwarf2_cu *cu = reader->cu;
18158 struct objfile *objfile = cu->per_objfile->objfile;
18159 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18160 unsigned int bytes_read;
18161 unsigned int load_all = 0;
18162 int nesting_level = 1;
18163
18164 parent_die = NULL;
18165 last_die = NULL;
18166
18167 gdb_assert (cu->per_cu != NULL);
18168 if (cu->per_cu->load_all_dies)
18169 load_all = 1;
18170
18171 cu->partial_dies
18172 = htab_create_alloc_ex (cu->header.length / 12,
18173 partial_die_hash,
18174 partial_die_eq,
18175 NULL,
18176 &cu->comp_unit_obstack,
18177 hashtab_obstack_allocate,
18178 dummy_obstack_deallocate);
18179
18180 while (1)
18181 {
18182 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18183
18184 /* A NULL abbrev means the end of a series of children. */
18185 if (abbrev == NULL)
18186 {
18187 if (--nesting_level == 0)
18188 return first_die;
18189
18190 info_ptr += bytes_read;
18191 last_die = parent_die;
18192 parent_die = parent_die->die_parent;
18193 continue;
18194 }
18195
18196 /* Check for template arguments. We never save these; if
18197 they're seen, we just mark the parent, and go on our way. */
18198 if (parent_die != NULL
18199 && cu->language == language_cplus
18200 && (abbrev->tag == DW_TAG_template_type_param
18201 || abbrev->tag == DW_TAG_template_value_param))
18202 {
18203 parent_die->has_template_arguments = 1;
18204
18205 if (!load_all)
18206 {
18207 /* We don't need a partial DIE for the template argument. */
18208 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18209 continue;
18210 }
18211 }
18212
18213 /* We only recurse into c++ subprograms looking for template arguments.
18214 Skip their other children. */
18215 if (!load_all
18216 && cu->language == language_cplus
18217 && parent_die != NULL
18218 && parent_die->tag == DW_TAG_subprogram)
18219 {
18220 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18221 continue;
18222 }
18223
18224 /* Check whether this DIE is interesting enough to save. Normally
18225 we would not be interested in members here, but there may be
18226 later variables referencing them via DW_AT_specification (for
18227 static members). */
18228 if (!load_all
18229 && !is_type_tag_for_partial (abbrev->tag)
18230 && abbrev->tag != DW_TAG_constant
18231 && abbrev->tag != DW_TAG_enumerator
18232 && abbrev->tag != DW_TAG_subprogram
18233 && abbrev->tag != DW_TAG_inlined_subroutine
18234 && abbrev->tag != DW_TAG_lexical_block
18235 && abbrev->tag != DW_TAG_variable
18236 && abbrev->tag != DW_TAG_namespace
18237 && abbrev->tag != DW_TAG_module
18238 && abbrev->tag != DW_TAG_member
18239 && abbrev->tag != DW_TAG_imported_unit
18240 && abbrev->tag != DW_TAG_imported_declaration)
18241 {
18242 /* Otherwise we skip to the next sibling, if any. */
18243 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18244 continue;
18245 }
18246
18247 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18248 abbrev);
18249
18250 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18251
18252 /* This two-pass algorithm for processing partial symbols has a
18253 high cost in cache pressure. Thus, handle some simple cases
18254 here which cover the majority of C partial symbols. DIEs
18255 which neither have specification tags in them, nor could have
18256 specification tags elsewhere pointing at them, can simply be
18257 processed and discarded.
18258
18259 This segment is also optional; scan_partial_symbols and
18260 add_partial_symbol will handle these DIEs if we chain
18261 them in normally. When compilers which do not emit large
18262 quantities of duplicate debug information are more common,
18263 this code can probably be removed. */
18264
18265 /* Any complete simple types at the top level (pretty much all
18266 of them, for a language without namespaces), can be processed
18267 directly. */
18268 if (parent_die == NULL
18269 && pdi.has_specification == 0
18270 && pdi.is_declaration == 0
18271 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18272 || pdi.tag == DW_TAG_base_type
18273 || pdi.tag == DW_TAG_subrange_type))
18274 {
18275 if (building_psymtab && pdi.name != NULL)
18276 add_psymbol_to_list (pdi.name, false,
18277 VAR_DOMAIN, LOC_TYPEDEF, -1,
18278 psymbol_placement::STATIC,
18279 0, cu->language, objfile);
18280 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18281 continue;
18282 }
18283
18284 /* The exception for DW_TAG_typedef with has_children above is
18285 a workaround of GCC PR debug/47510. In the case of this complaint
18286 type_name_or_error will error on such types later.
18287
18288 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18289 it could not find the child DIEs referenced later, this is checked
18290 above. In correct DWARF DW_TAG_typedef should have no children. */
18291
18292 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18293 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18294 "- DIE at %s [in module %s]"),
18295 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18296
18297 /* If we're at the second level, and we're an enumerator, and
18298 our parent has no specification (meaning possibly lives in a
18299 namespace elsewhere), then we can add the partial symbol now
18300 instead of queueing it. */
18301 if (pdi.tag == DW_TAG_enumerator
18302 && parent_die != NULL
18303 && parent_die->die_parent == NULL
18304 && parent_die->tag == DW_TAG_enumeration_type
18305 && parent_die->has_specification == 0)
18306 {
18307 if (pdi.name == NULL)
18308 complaint (_("malformed enumerator DIE ignored"));
18309 else if (building_psymtab)
18310 add_psymbol_to_list (pdi.name, false,
18311 VAR_DOMAIN, LOC_CONST, -1,
18312 cu->language == language_cplus
18313 ? psymbol_placement::GLOBAL
18314 : psymbol_placement::STATIC,
18315 0, cu->language, objfile);
18316
18317 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18318 continue;
18319 }
18320
18321 struct partial_die_info *part_die
18322 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18323
18324 /* We'll save this DIE so link it in. */
18325 part_die->die_parent = parent_die;
18326 part_die->die_sibling = NULL;
18327 part_die->die_child = NULL;
18328
18329 if (last_die && last_die == parent_die)
18330 last_die->die_child = part_die;
18331 else if (last_die)
18332 last_die->die_sibling = part_die;
18333
18334 last_die = part_die;
18335
18336 if (first_die == NULL)
18337 first_die = part_die;
18338
18339 /* Maybe add the DIE to the hash table. Not all DIEs that we
18340 find interesting need to be in the hash table, because we
18341 also have the parent/sibling/child chains; only those that we
18342 might refer to by offset later during partial symbol reading.
18343
18344 For now this means things that might have be the target of a
18345 DW_AT_specification, DW_AT_abstract_origin, or
18346 DW_AT_extension. DW_AT_extension will refer only to
18347 namespaces; DW_AT_abstract_origin refers to functions (and
18348 many things under the function DIE, but we do not recurse
18349 into function DIEs during partial symbol reading) and
18350 possibly variables as well; DW_AT_specification refers to
18351 declarations. Declarations ought to have the DW_AT_declaration
18352 flag. It happens that GCC forgets to put it in sometimes, but
18353 only for functions, not for types.
18354
18355 Adding more things than necessary to the hash table is harmless
18356 except for the performance cost. Adding too few will result in
18357 wasted time in find_partial_die, when we reread the compilation
18358 unit with load_all_dies set. */
18359
18360 if (load_all
18361 || abbrev->tag == DW_TAG_constant
18362 || abbrev->tag == DW_TAG_subprogram
18363 || abbrev->tag == DW_TAG_variable
18364 || abbrev->tag == DW_TAG_namespace
18365 || part_die->is_declaration)
18366 {
18367 void **slot;
18368
18369 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18370 to_underlying (part_die->sect_off),
18371 INSERT);
18372 *slot = part_die;
18373 }
18374
18375 /* For some DIEs we want to follow their children (if any). For C
18376 we have no reason to follow the children of structures; for other
18377 languages we have to, so that we can get at method physnames
18378 to infer fully qualified class names, for DW_AT_specification,
18379 and for C++ template arguments. For C++, we also look one level
18380 inside functions to find template arguments (if the name of the
18381 function does not already contain the template arguments).
18382
18383 For Ada and Fortran, we need to scan the children of subprograms
18384 and lexical blocks as well because these languages allow the
18385 definition of nested entities that could be interesting for the
18386 debugger, such as nested subprograms for instance. */
18387 if (last_die->has_children
18388 && (load_all
18389 || last_die->tag == DW_TAG_namespace
18390 || last_die->tag == DW_TAG_module
18391 || last_die->tag == DW_TAG_enumeration_type
18392 || (cu->language == language_cplus
18393 && last_die->tag == DW_TAG_subprogram
18394 && (last_die->name == NULL
18395 || strchr (last_die->name, '<') == NULL))
18396 || (cu->language != language_c
18397 && (last_die->tag == DW_TAG_class_type
18398 || last_die->tag == DW_TAG_interface_type
18399 || last_die->tag == DW_TAG_structure_type
18400 || last_die->tag == DW_TAG_union_type))
18401 || ((cu->language == language_ada
18402 || cu->language == language_fortran)
18403 && (last_die->tag == DW_TAG_subprogram
18404 || last_die->tag == DW_TAG_lexical_block))))
18405 {
18406 nesting_level++;
18407 parent_die = last_die;
18408 continue;
18409 }
18410
18411 /* Otherwise we skip to the next sibling, if any. */
18412 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18413
18414 /* Back to the top, do it again. */
18415 }
18416 }
18417
18418 partial_die_info::partial_die_info (sect_offset sect_off_,
18419 struct abbrev_info *abbrev)
18420 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18421 {
18422 }
18423
18424 /* Read a minimal amount of information into the minimal die structure.
18425 INFO_PTR should point just after the initial uleb128 of a DIE. */
18426
18427 const gdb_byte *
18428 partial_die_info::read (const struct die_reader_specs *reader,
18429 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18430 {
18431 struct dwarf2_cu *cu = reader->cu;
18432 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18433 unsigned int i;
18434 int has_low_pc_attr = 0;
18435 int has_high_pc_attr = 0;
18436 int high_pc_relative = 0;
18437
18438 for (i = 0; i < abbrev.num_attrs; ++i)
18439 {
18440 attribute attr;
18441 bool need_reprocess;
18442 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18443 info_ptr, &need_reprocess);
18444 /* String and address offsets that need to do the reprocessing have
18445 already been read at this point, so there is no need to wait until
18446 the loop terminates to do the reprocessing. */
18447 if (need_reprocess)
18448 read_attribute_reprocess (reader, &attr);
18449 /* Store the data if it is of an attribute we want to keep in a
18450 partial symbol table. */
18451 switch (attr.name)
18452 {
18453 case DW_AT_name:
18454 switch (tag)
18455 {
18456 case DW_TAG_compile_unit:
18457 case DW_TAG_partial_unit:
18458 case DW_TAG_type_unit:
18459 /* Compilation units have a DW_AT_name that is a filename, not
18460 a source language identifier. */
18461 case DW_TAG_enumeration_type:
18462 case DW_TAG_enumerator:
18463 /* These tags always have simple identifiers already; no need
18464 to canonicalize them. */
18465 name = DW_STRING (&attr);
18466 break;
18467 default:
18468 {
18469 struct objfile *objfile = dwarf2_per_objfile->objfile;
18470
18471 name
18472 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18473 }
18474 break;
18475 }
18476 break;
18477 case DW_AT_linkage_name:
18478 case DW_AT_MIPS_linkage_name:
18479 /* Note that both forms of linkage name might appear. We
18480 assume they will be the same, and we only store the last
18481 one we see. */
18482 linkage_name = attr.value_as_string ();
18483 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18484 See https://github.com/rust-lang/rust/issues/32925. */
18485 if (cu->language == language_rust && linkage_name != NULL
18486 && strchr (linkage_name, '{') != NULL)
18487 linkage_name = NULL;
18488 break;
18489 case DW_AT_low_pc:
18490 has_low_pc_attr = 1;
18491 lowpc = attr.value_as_address ();
18492 break;
18493 case DW_AT_high_pc:
18494 has_high_pc_attr = 1;
18495 highpc = attr.value_as_address ();
18496 if (cu->header.version >= 4 && attr.form_is_constant ())
18497 high_pc_relative = 1;
18498 break;
18499 case DW_AT_location:
18500 /* Support the .debug_loc offsets. */
18501 if (attr.form_is_block ())
18502 {
18503 d.locdesc = DW_BLOCK (&attr);
18504 }
18505 else if (attr.form_is_section_offset ())
18506 {
18507 dwarf2_complex_location_expr_complaint ();
18508 }
18509 else
18510 {
18511 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18512 "partial symbol information");
18513 }
18514 break;
18515 case DW_AT_external:
18516 is_external = DW_UNSND (&attr);
18517 break;
18518 case DW_AT_declaration:
18519 is_declaration = DW_UNSND (&attr);
18520 break;
18521 case DW_AT_type:
18522 has_type = 1;
18523 break;
18524 case DW_AT_abstract_origin:
18525 case DW_AT_specification:
18526 case DW_AT_extension:
18527 has_specification = 1;
18528 spec_offset = attr.get_ref_die_offset ();
18529 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18530 || cu->per_cu->is_dwz);
18531 break;
18532 case DW_AT_sibling:
18533 /* Ignore absolute siblings, they might point outside of
18534 the current compile unit. */
18535 if (attr.form == DW_FORM_ref_addr)
18536 complaint (_("ignoring absolute DW_AT_sibling"));
18537 else
18538 {
18539 const gdb_byte *buffer = reader->buffer;
18540 sect_offset off = attr.get_ref_die_offset ();
18541 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18542
18543 if (sibling_ptr < info_ptr)
18544 complaint (_("DW_AT_sibling points backwards"));
18545 else if (sibling_ptr > reader->buffer_end)
18546 reader->die_section->overflow_complaint ();
18547 else
18548 sibling = sibling_ptr;
18549 }
18550 break;
18551 case DW_AT_byte_size:
18552 has_byte_size = 1;
18553 break;
18554 case DW_AT_const_value:
18555 has_const_value = 1;
18556 break;
18557 case DW_AT_calling_convention:
18558 /* DWARF doesn't provide a way to identify a program's source-level
18559 entry point. DW_AT_calling_convention attributes are only meant
18560 to describe functions' calling conventions.
18561
18562 However, because it's a necessary piece of information in
18563 Fortran, and before DWARF 4 DW_CC_program was the only
18564 piece of debugging information whose definition refers to
18565 a 'main program' at all, several compilers marked Fortran
18566 main programs with DW_CC_program --- even when those
18567 functions use the standard calling conventions.
18568
18569 Although DWARF now specifies a way to provide this
18570 information, we support this practice for backward
18571 compatibility. */
18572 if (DW_UNSND (&attr) == DW_CC_program
18573 && cu->language == language_fortran)
18574 main_subprogram = 1;
18575 break;
18576 case DW_AT_inline:
18577 if (DW_UNSND (&attr) == DW_INL_inlined
18578 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18579 may_be_inlined = 1;
18580 break;
18581
18582 case DW_AT_import:
18583 if (tag == DW_TAG_imported_unit)
18584 {
18585 d.sect_off = attr.get_ref_die_offset ();
18586 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18587 || cu->per_cu->is_dwz);
18588 }
18589 break;
18590
18591 case DW_AT_main_subprogram:
18592 main_subprogram = DW_UNSND (&attr);
18593 break;
18594
18595 case DW_AT_ranges:
18596 {
18597 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18598 but that requires a full DIE, so instead we just
18599 reimplement it. */
18600 int need_ranges_base = tag != DW_TAG_compile_unit;
18601 unsigned int ranges_offset = (DW_UNSND (&attr)
18602 + (need_ranges_base
18603 ? cu->ranges_base
18604 : 0));
18605
18606 /* Value of the DW_AT_ranges attribute is the offset in the
18607 .debug_ranges section. */
18608 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18609 nullptr))
18610 has_pc_info = 1;
18611 }
18612 break;
18613
18614 default:
18615 break;
18616 }
18617 }
18618
18619 /* For Ada, if both the name and the linkage name appear, we prefer
18620 the latter. This lets "catch exception" work better, regardless
18621 of the order in which the name and linkage name were emitted.
18622 Really, though, this is just a workaround for the fact that gdb
18623 doesn't store both the name and the linkage name. */
18624 if (cu->language == language_ada && linkage_name != nullptr)
18625 name = linkage_name;
18626
18627 if (high_pc_relative)
18628 highpc += lowpc;
18629
18630 if (has_low_pc_attr && has_high_pc_attr)
18631 {
18632 /* When using the GNU linker, .gnu.linkonce. sections are used to
18633 eliminate duplicate copies of functions and vtables and such.
18634 The linker will arbitrarily choose one and discard the others.
18635 The AT_*_pc values for such functions refer to local labels in
18636 these sections. If the section from that file was discarded, the
18637 labels are not in the output, so the relocs get a value of 0.
18638 If this is a discarded function, mark the pc bounds as invalid,
18639 so that GDB will ignore it. */
18640 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18641 {
18642 struct objfile *objfile = dwarf2_per_objfile->objfile;
18643 struct gdbarch *gdbarch = objfile->arch ();
18644
18645 complaint (_("DW_AT_low_pc %s is zero "
18646 "for DIE at %s [in module %s]"),
18647 paddress (gdbarch, lowpc),
18648 sect_offset_str (sect_off),
18649 objfile_name (objfile));
18650 }
18651 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18652 else if (lowpc >= highpc)
18653 {
18654 struct objfile *objfile = dwarf2_per_objfile->objfile;
18655 struct gdbarch *gdbarch = objfile->arch ();
18656
18657 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18658 "for DIE at %s [in module %s]"),
18659 paddress (gdbarch, lowpc),
18660 paddress (gdbarch, highpc),
18661 sect_offset_str (sect_off),
18662 objfile_name (objfile));
18663 }
18664 else
18665 has_pc_info = 1;
18666 }
18667
18668 return info_ptr;
18669 }
18670
18671 /* Find a cached partial DIE at OFFSET in CU. */
18672
18673 struct partial_die_info *
18674 dwarf2_cu::find_partial_die (sect_offset sect_off)
18675 {
18676 struct partial_die_info *lookup_die = NULL;
18677 struct partial_die_info part_die (sect_off);
18678
18679 lookup_die = ((struct partial_die_info *)
18680 htab_find_with_hash (partial_dies, &part_die,
18681 to_underlying (sect_off)));
18682
18683 return lookup_die;
18684 }
18685
18686 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18687 except in the case of .debug_types DIEs which do not reference
18688 outside their CU (they do however referencing other types via
18689 DW_FORM_ref_sig8). */
18690
18691 static const struct cu_partial_die_info
18692 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18693 {
18694 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18695 struct objfile *objfile = dwarf2_per_objfile->objfile;
18696 struct dwarf2_per_cu_data *per_cu = NULL;
18697 struct partial_die_info *pd = NULL;
18698
18699 if (offset_in_dwz == cu->per_cu->is_dwz
18700 && cu->header.offset_in_cu_p (sect_off))
18701 {
18702 pd = cu->find_partial_die (sect_off);
18703 if (pd != NULL)
18704 return { cu, pd };
18705 /* We missed recording what we needed.
18706 Load all dies and try again. */
18707 per_cu = cu->per_cu;
18708 }
18709 else
18710 {
18711 /* TUs don't reference other CUs/TUs (except via type signatures). */
18712 if (cu->per_cu->is_debug_types)
18713 {
18714 error (_("Dwarf Error: Type Unit at offset %s contains"
18715 " external reference to offset %s [in module %s].\n"),
18716 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18717 bfd_get_filename (objfile->obfd));
18718 }
18719 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18720 dwarf2_per_objfile);
18721
18722 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18723 load_partial_comp_unit (per_cu, cu->per_objfile, nullptr);
18724
18725 per_cu->cu->last_used = 0;
18726 pd = per_cu->cu->find_partial_die (sect_off);
18727 }
18728
18729 /* If we didn't find it, and not all dies have been loaded,
18730 load them all and try again. */
18731
18732 if (pd == NULL && per_cu->load_all_dies == 0)
18733 {
18734 per_cu->load_all_dies = 1;
18735
18736 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18737 THIS_CU->cu may already be in use. So we can't just free it and
18738 replace its DIEs with the ones we read in. Instead, we leave those
18739 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18740 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18741 set. */
18742 load_partial_comp_unit (per_cu, cu->per_objfile, cu);
18743
18744 pd = per_cu->cu->find_partial_die (sect_off);
18745 }
18746
18747 if (pd == NULL)
18748 internal_error (__FILE__, __LINE__,
18749 _("could not find partial DIE %s "
18750 "in cache [from module %s]\n"),
18751 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18752 return { per_cu->cu, pd };
18753 }
18754
18755 /* See if we can figure out if the class lives in a namespace. We do
18756 this by looking for a member function; its demangled name will
18757 contain namespace info, if there is any. */
18758
18759 static void
18760 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18761 struct dwarf2_cu *cu)
18762 {
18763 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18764 what template types look like, because the demangler
18765 frequently doesn't give the same name as the debug info. We
18766 could fix this by only using the demangled name to get the
18767 prefix (but see comment in read_structure_type). */
18768
18769 struct partial_die_info *real_pdi;
18770 struct partial_die_info *child_pdi;
18771
18772 /* If this DIE (this DIE's specification, if any) has a parent, then
18773 we should not do this. We'll prepend the parent's fully qualified
18774 name when we create the partial symbol. */
18775
18776 real_pdi = struct_pdi;
18777 while (real_pdi->has_specification)
18778 {
18779 auto res = find_partial_die (real_pdi->spec_offset,
18780 real_pdi->spec_is_dwz, cu);
18781 real_pdi = res.pdi;
18782 cu = res.cu;
18783 }
18784
18785 if (real_pdi->die_parent != NULL)
18786 return;
18787
18788 for (child_pdi = struct_pdi->die_child;
18789 child_pdi != NULL;
18790 child_pdi = child_pdi->die_sibling)
18791 {
18792 if (child_pdi->tag == DW_TAG_subprogram
18793 && child_pdi->linkage_name != NULL)
18794 {
18795 gdb::unique_xmalloc_ptr<char> actual_class_name
18796 (language_class_name_from_physname (cu->language_defn,
18797 child_pdi->linkage_name));
18798 if (actual_class_name != NULL)
18799 {
18800 struct objfile *objfile = cu->per_objfile->objfile;
18801 struct_pdi->name = objfile->intern (actual_class_name.get ());
18802 }
18803 break;
18804 }
18805 }
18806 }
18807
18808 /* Return true if a DIE with TAG may have the DW_AT_const_value
18809 attribute. */
18810
18811 static bool
18812 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18813 {
18814 switch (tag)
18815 {
18816 case DW_TAG_constant:
18817 case DW_TAG_enumerator:
18818 case DW_TAG_formal_parameter:
18819 case DW_TAG_template_value_param:
18820 case DW_TAG_variable:
18821 return true;
18822 }
18823
18824 return false;
18825 }
18826
18827 void
18828 partial_die_info::fixup (struct dwarf2_cu *cu)
18829 {
18830 /* Once we've fixed up a die, there's no point in doing so again.
18831 This also avoids a memory leak if we were to call
18832 guess_partial_die_structure_name multiple times. */
18833 if (fixup_called)
18834 return;
18835
18836 /* If we found a reference attribute and the DIE has no name, try
18837 to find a name in the referred to DIE. */
18838
18839 if (name == NULL && has_specification)
18840 {
18841 struct partial_die_info *spec_die;
18842
18843 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18844 spec_die = res.pdi;
18845 cu = res.cu;
18846
18847 spec_die->fixup (cu);
18848
18849 if (spec_die->name)
18850 {
18851 name = spec_die->name;
18852
18853 /* Copy DW_AT_external attribute if it is set. */
18854 if (spec_die->is_external)
18855 is_external = spec_die->is_external;
18856 }
18857 }
18858
18859 if (!has_const_value && has_specification
18860 && can_have_DW_AT_const_value_p (tag))
18861 {
18862 struct partial_die_info *spec_die;
18863
18864 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18865 spec_die = res.pdi;
18866 cu = res.cu;
18867
18868 spec_die->fixup (cu);
18869
18870 if (spec_die->has_const_value)
18871 {
18872 /* Copy DW_AT_const_value attribute if it is set. */
18873 has_const_value = spec_die->has_const_value;
18874 }
18875 }
18876
18877 /* Set default names for some unnamed DIEs. */
18878
18879 if (name == NULL && tag == DW_TAG_namespace)
18880 name = CP_ANONYMOUS_NAMESPACE_STR;
18881
18882 /* If there is no parent die to provide a namespace, and there are
18883 children, see if we can determine the namespace from their linkage
18884 name. */
18885 if (cu->language == language_cplus
18886 && !cu->per_objfile->per_bfd->types.empty ()
18887 && die_parent == NULL
18888 && has_children
18889 && (tag == DW_TAG_class_type
18890 || tag == DW_TAG_structure_type
18891 || tag == DW_TAG_union_type))
18892 guess_partial_die_structure_name (this, cu);
18893
18894 /* GCC might emit a nameless struct or union that has a linkage
18895 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18896 if (name == NULL
18897 && (tag == DW_TAG_class_type
18898 || tag == DW_TAG_interface_type
18899 || tag == DW_TAG_structure_type
18900 || tag == DW_TAG_union_type)
18901 && linkage_name != NULL)
18902 {
18903 gdb::unique_xmalloc_ptr<char> demangled
18904 (gdb_demangle (linkage_name, DMGL_TYPES));
18905 if (demangled != nullptr)
18906 {
18907 const char *base;
18908
18909 /* Strip any leading namespaces/classes, keep only the base name.
18910 DW_AT_name for named DIEs does not contain the prefixes. */
18911 base = strrchr (demangled.get (), ':');
18912 if (base && base > demangled.get () && base[-1] == ':')
18913 base++;
18914 else
18915 base = demangled.get ();
18916
18917 struct objfile *objfile = cu->per_objfile->objfile;
18918 name = objfile->intern (base);
18919 }
18920 }
18921
18922 fixup_called = 1;
18923 }
18924
18925 /* Read the .debug_loclists header contents from the given SECTION in the
18926 HEADER. */
18927 static void
18928 read_loclist_header (struct loclist_header *header,
18929 struct dwarf2_section_info *section)
18930 {
18931 unsigned int bytes_read;
18932 bfd *abfd = section->get_bfd_owner ();
18933 const gdb_byte *info_ptr = section->buffer;
18934 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18935 info_ptr += bytes_read;
18936 header->version = read_2_bytes (abfd, info_ptr);
18937 info_ptr += 2;
18938 header->addr_size = read_1_byte (abfd, info_ptr);
18939 info_ptr += 1;
18940 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18941 info_ptr += 1;
18942 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18943 }
18944
18945 /* Return the DW_AT_loclists_base value for the CU. */
18946 static ULONGEST
18947 lookup_loclist_base (struct dwarf2_cu *cu)
18948 {
18949 /* For the .dwo unit, the loclist_base points to the first offset following
18950 the header. The header consists of the following entities-
18951 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18952 bit format)
18953 2. version (2 bytes)
18954 3. address size (1 byte)
18955 4. segment selector size (1 byte)
18956 5. offset entry count (4 bytes)
18957 These sizes are derived as per the DWARFv5 standard. */
18958 if (cu->dwo_unit != nullptr)
18959 {
18960 if (cu->header.initial_length_size == 4)
18961 return LOCLIST_HEADER_SIZE32;
18962 return LOCLIST_HEADER_SIZE64;
18963 }
18964 return cu->loclist_base;
18965 }
18966
18967 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18968 array of offsets in the .debug_loclists section. */
18969 static CORE_ADDR
18970 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18971 {
18972 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18973 struct objfile *objfile = dwarf2_per_objfile->objfile;
18974 bfd *abfd = objfile->obfd;
18975 ULONGEST loclist_base = lookup_loclist_base (cu);
18976 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18977
18978 section->read (objfile);
18979 if (section->buffer == NULL)
18980 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18981 "section [in module %s]"), objfile_name (objfile));
18982 struct loclist_header header;
18983 read_loclist_header (&header, section);
18984 if (loclist_index >= header.offset_entry_count)
18985 complaint (_("DW_FORM_loclistx pointing outside of "
18986 ".debug_loclists offset array [in module %s]"),
18987 objfile_name (objfile));
18988 if (loclist_base + loclist_index * cu->header.offset_size
18989 >= section->size)
18990 complaint (_("DW_FORM_loclistx pointing outside of "
18991 ".debug_loclists section [in module %s]"),
18992 objfile_name (objfile));
18993 const gdb_byte *info_ptr
18994 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18995
18996 if (cu->header.offset_size == 4)
18997 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18998 else
18999 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19000 }
19001
19002 /* Process the attributes that had to be skipped in the first round. These
19003 attributes are the ones that need str_offsets_base or addr_base attributes.
19004 They could not have been processed in the first round, because at the time
19005 the values of str_offsets_base or addr_base may not have been known. */
19006 static void
19007 read_attribute_reprocess (const struct die_reader_specs *reader,
19008 struct attribute *attr)
19009 {
19010 struct dwarf2_cu *cu = reader->cu;
19011 switch (attr->form)
19012 {
19013 case DW_FORM_addrx:
19014 case DW_FORM_GNU_addr_index:
19015 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19016 break;
19017 case DW_FORM_loclistx:
19018 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19019 break;
19020 case DW_FORM_strx:
19021 case DW_FORM_strx1:
19022 case DW_FORM_strx2:
19023 case DW_FORM_strx3:
19024 case DW_FORM_strx4:
19025 case DW_FORM_GNU_str_index:
19026 {
19027 unsigned int str_index = DW_UNSND (attr);
19028 if (reader->dwo_file != NULL)
19029 {
19030 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19031 DW_STRING_IS_CANONICAL (attr) = 0;
19032 }
19033 else
19034 {
19035 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19036 DW_STRING_IS_CANONICAL (attr) = 0;
19037 }
19038 break;
19039 }
19040 default:
19041 gdb_assert_not_reached (_("Unexpected DWARF form."));
19042 }
19043 }
19044
19045 /* Read an attribute value described by an attribute form. */
19046
19047 static const gdb_byte *
19048 read_attribute_value (const struct die_reader_specs *reader,
19049 struct attribute *attr, unsigned form,
19050 LONGEST implicit_const, const gdb_byte *info_ptr,
19051 bool *need_reprocess)
19052 {
19053 struct dwarf2_cu *cu = reader->cu;
19054 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19055 struct objfile *objfile = dwarf2_per_objfile->objfile;
19056 bfd *abfd = reader->abfd;
19057 struct comp_unit_head *cu_header = &cu->header;
19058 unsigned int bytes_read;
19059 struct dwarf_block *blk;
19060 *need_reprocess = false;
19061
19062 attr->form = (enum dwarf_form) form;
19063 switch (form)
19064 {
19065 case DW_FORM_ref_addr:
19066 if (cu->header.version == 2)
19067 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19068 &bytes_read);
19069 else
19070 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19071 &bytes_read);
19072 info_ptr += bytes_read;
19073 break;
19074 case DW_FORM_GNU_ref_alt:
19075 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19076 info_ptr += bytes_read;
19077 break;
19078 case DW_FORM_addr:
19079 {
19080 struct gdbarch *gdbarch = objfile->arch ();
19081 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19082 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19083 info_ptr += bytes_read;
19084 }
19085 break;
19086 case DW_FORM_block2:
19087 blk = dwarf_alloc_block (cu);
19088 blk->size = read_2_bytes (abfd, info_ptr);
19089 info_ptr += 2;
19090 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19091 info_ptr += blk->size;
19092 DW_BLOCK (attr) = blk;
19093 break;
19094 case DW_FORM_block4:
19095 blk = dwarf_alloc_block (cu);
19096 blk->size = read_4_bytes (abfd, info_ptr);
19097 info_ptr += 4;
19098 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19099 info_ptr += blk->size;
19100 DW_BLOCK (attr) = blk;
19101 break;
19102 case DW_FORM_data2:
19103 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19104 info_ptr += 2;
19105 break;
19106 case DW_FORM_data4:
19107 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19108 info_ptr += 4;
19109 break;
19110 case DW_FORM_data8:
19111 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19112 info_ptr += 8;
19113 break;
19114 case DW_FORM_data16:
19115 blk = dwarf_alloc_block (cu);
19116 blk->size = 16;
19117 blk->data = read_n_bytes (abfd, info_ptr, 16);
19118 info_ptr += 16;
19119 DW_BLOCK (attr) = blk;
19120 break;
19121 case DW_FORM_sec_offset:
19122 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19123 info_ptr += bytes_read;
19124 break;
19125 case DW_FORM_loclistx:
19126 {
19127 *need_reprocess = true;
19128 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19129 info_ptr += bytes_read;
19130 }
19131 break;
19132 case DW_FORM_string:
19133 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19134 DW_STRING_IS_CANONICAL (attr) = 0;
19135 info_ptr += bytes_read;
19136 break;
19137 case DW_FORM_strp:
19138 if (!cu->per_cu->is_dwz)
19139 {
19140 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19141 abfd, info_ptr, cu_header,
19142 &bytes_read);
19143 DW_STRING_IS_CANONICAL (attr) = 0;
19144 info_ptr += bytes_read;
19145 break;
19146 }
19147 /* FALLTHROUGH */
19148 case DW_FORM_line_strp:
19149 if (!cu->per_cu->is_dwz)
19150 {
19151 DW_STRING (attr)
19152 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19153 &bytes_read);
19154 DW_STRING_IS_CANONICAL (attr) = 0;
19155 info_ptr += bytes_read;
19156 break;
19157 }
19158 /* FALLTHROUGH */
19159 case DW_FORM_GNU_strp_alt:
19160 {
19161 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19162 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19163 &bytes_read);
19164
19165 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19166 DW_STRING_IS_CANONICAL (attr) = 0;
19167 info_ptr += bytes_read;
19168 }
19169 break;
19170 case DW_FORM_exprloc:
19171 case DW_FORM_block:
19172 blk = dwarf_alloc_block (cu);
19173 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19174 info_ptr += bytes_read;
19175 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19176 info_ptr += blk->size;
19177 DW_BLOCK (attr) = blk;
19178 break;
19179 case DW_FORM_block1:
19180 blk = dwarf_alloc_block (cu);
19181 blk->size = read_1_byte (abfd, info_ptr);
19182 info_ptr += 1;
19183 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19184 info_ptr += blk->size;
19185 DW_BLOCK (attr) = blk;
19186 break;
19187 case DW_FORM_data1:
19188 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19189 info_ptr += 1;
19190 break;
19191 case DW_FORM_flag:
19192 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19193 info_ptr += 1;
19194 break;
19195 case DW_FORM_flag_present:
19196 DW_UNSND (attr) = 1;
19197 break;
19198 case DW_FORM_sdata:
19199 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19200 info_ptr += bytes_read;
19201 break;
19202 case DW_FORM_udata:
19203 case DW_FORM_rnglistx:
19204 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19205 info_ptr += bytes_read;
19206 break;
19207 case DW_FORM_ref1:
19208 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19209 + read_1_byte (abfd, info_ptr));
19210 info_ptr += 1;
19211 break;
19212 case DW_FORM_ref2:
19213 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19214 + read_2_bytes (abfd, info_ptr));
19215 info_ptr += 2;
19216 break;
19217 case DW_FORM_ref4:
19218 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19219 + read_4_bytes (abfd, info_ptr));
19220 info_ptr += 4;
19221 break;
19222 case DW_FORM_ref8:
19223 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19224 + read_8_bytes (abfd, info_ptr));
19225 info_ptr += 8;
19226 break;
19227 case DW_FORM_ref_sig8:
19228 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19229 info_ptr += 8;
19230 break;
19231 case DW_FORM_ref_udata:
19232 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19233 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19234 info_ptr += bytes_read;
19235 break;
19236 case DW_FORM_indirect:
19237 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19238 info_ptr += bytes_read;
19239 if (form == DW_FORM_implicit_const)
19240 {
19241 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19242 info_ptr += bytes_read;
19243 }
19244 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19245 info_ptr, need_reprocess);
19246 break;
19247 case DW_FORM_implicit_const:
19248 DW_SND (attr) = implicit_const;
19249 break;
19250 case DW_FORM_addrx:
19251 case DW_FORM_GNU_addr_index:
19252 *need_reprocess = true;
19253 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19254 info_ptr += bytes_read;
19255 break;
19256 case DW_FORM_strx:
19257 case DW_FORM_strx1:
19258 case DW_FORM_strx2:
19259 case DW_FORM_strx3:
19260 case DW_FORM_strx4:
19261 case DW_FORM_GNU_str_index:
19262 {
19263 ULONGEST str_index;
19264 if (form == DW_FORM_strx1)
19265 {
19266 str_index = read_1_byte (abfd, info_ptr);
19267 info_ptr += 1;
19268 }
19269 else if (form == DW_FORM_strx2)
19270 {
19271 str_index = read_2_bytes (abfd, info_ptr);
19272 info_ptr += 2;
19273 }
19274 else if (form == DW_FORM_strx3)
19275 {
19276 str_index = read_3_bytes (abfd, info_ptr);
19277 info_ptr += 3;
19278 }
19279 else if (form == DW_FORM_strx4)
19280 {
19281 str_index = read_4_bytes (abfd, info_ptr);
19282 info_ptr += 4;
19283 }
19284 else
19285 {
19286 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19287 info_ptr += bytes_read;
19288 }
19289 *need_reprocess = true;
19290 DW_UNSND (attr) = str_index;
19291 }
19292 break;
19293 default:
19294 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19295 dwarf_form_name (form),
19296 bfd_get_filename (abfd));
19297 }
19298
19299 /* Super hack. */
19300 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19301 attr->form = DW_FORM_GNU_ref_alt;
19302
19303 /* We have seen instances where the compiler tried to emit a byte
19304 size attribute of -1 which ended up being encoded as an unsigned
19305 0xffffffff. Although 0xffffffff is technically a valid size value,
19306 an object of this size seems pretty unlikely so we can relatively
19307 safely treat these cases as if the size attribute was invalid and
19308 treat them as zero by default. */
19309 if (attr->name == DW_AT_byte_size
19310 && form == DW_FORM_data4
19311 && DW_UNSND (attr) >= 0xffffffff)
19312 {
19313 complaint
19314 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19315 hex_string (DW_UNSND (attr)));
19316 DW_UNSND (attr) = 0;
19317 }
19318
19319 return info_ptr;
19320 }
19321
19322 /* Read an attribute described by an abbreviated attribute. */
19323
19324 static const gdb_byte *
19325 read_attribute (const struct die_reader_specs *reader,
19326 struct attribute *attr, struct attr_abbrev *abbrev,
19327 const gdb_byte *info_ptr, bool *need_reprocess)
19328 {
19329 attr->name = abbrev->name;
19330 return read_attribute_value (reader, attr, abbrev->form,
19331 abbrev->implicit_const, info_ptr,
19332 need_reprocess);
19333 }
19334
19335 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19336
19337 static const char *
19338 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19339 LONGEST str_offset)
19340 {
19341 return dwarf2_per_objfile->per_bfd->str.read_string
19342 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19343 }
19344
19345 /* Return pointer to string at .debug_str offset as read from BUF.
19346 BUF is assumed to be in a compilation unit described by CU_HEADER.
19347 Return *BYTES_READ_PTR count of bytes read from BUF. */
19348
19349 static const char *
19350 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19351 const gdb_byte *buf,
19352 const struct comp_unit_head *cu_header,
19353 unsigned int *bytes_read_ptr)
19354 {
19355 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19356
19357 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19358 }
19359
19360 /* See read.h. */
19361
19362 const char *
19363 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19364 const struct comp_unit_head *cu_header,
19365 unsigned int *bytes_read_ptr)
19366 {
19367 bfd *abfd = objfile->obfd;
19368 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19369
19370 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19371 }
19372
19373 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19374 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19375 ADDR_SIZE is the size of addresses from the CU header. */
19376
19377 static CORE_ADDR
19378 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19379 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19380 int addr_size)
19381 {
19382 struct objfile *objfile = dwarf2_per_objfile->objfile;
19383 bfd *abfd = objfile->obfd;
19384 const gdb_byte *info_ptr;
19385 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19386
19387 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19388 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19389 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19390 objfile_name (objfile));
19391 if (addr_base_or_zero + addr_index * addr_size
19392 >= dwarf2_per_objfile->per_bfd->addr.size)
19393 error (_("DW_FORM_addr_index pointing outside of "
19394 ".debug_addr section [in module %s]"),
19395 objfile_name (objfile));
19396 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19397 + addr_base_or_zero + addr_index * addr_size);
19398 if (addr_size == 4)
19399 return bfd_get_32 (abfd, info_ptr);
19400 else
19401 return bfd_get_64 (abfd, info_ptr);
19402 }
19403
19404 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19405
19406 static CORE_ADDR
19407 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19408 {
19409 return read_addr_index_1 (cu->per_objfile, addr_index,
19410 cu->addr_base, cu->header.addr_size);
19411 }
19412
19413 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19414
19415 static CORE_ADDR
19416 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19417 unsigned int *bytes_read)
19418 {
19419 bfd *abfd = cu->per_objfile->objfile->obfd;
19420 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19421
19422 return read_addr_index (cu, addr_index);
19423 }
19424
19425 /* See read.h. */
19426
19427 CORE_ADDR
19428 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19429 dwarf2_per_objfile *dwarf2_per_objfile,
19430 unsigned int addr_index)
19431 {
19432 struct dwarf2_cu *cu = per_cu->cu;
19433 gdb::optional<ULONGEST> addr_base;
19434 int addr_size;
19435
19436 /* We need addr_base and addr_size.
19437 If we don't have PER_CU->cu, we have to get it.
19438 Nasty, but the alternative is storing the needed info in PER_CU,
19439 which at this point doesn't seem justified: it's not clear how frequently
19440 it would get used and it would increase the size of every PER_CU.
19441 Entry points like dwarf2_per_cu_addr_size do a similar thing
19442 so we're not in uncharted territory here.
19443 Alas we need to be a bit more complicated as addr_base is contained
19444 in the DIE.
19445
19446 We don't need to read the entire CU(/TU).
19447 We just need the header and top level die.
19448
19449 IWBN to use the aging mechanism to let us lazily later discard the CU.
19450 For now we skip this optimization. */
19451
19452 if (cu != NULL)
19453 {
19454 addr_base = cu->addr_base;
19455 addr_size = cu->header.addr_size;
19456 }
19457 else
19458 {
19459 cutu_reader reader (per_cu, dwarf2_per_objfile, nullptr, nullptr, false);
19460 addr_base = reader.cu->addr_base;
19461 addr_size = reader.cu->header.addr_size;
19462 }
19463
19464 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19465 addr_size);
19466 }
19467
19468 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19469 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19470 DWO file. */
19471
19472 static const char *
19473 read_str_index (struct dwarf2_cu *cu,
19474 struct dwarf2_section_info *str_section,
19475 struct dwarf2_section_info *str_offsets_section,
19476 ULONGEST str_offsets_base, ULONGEST str_index)
19477 {
19478 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19479 struct objfile *objfile = dwarf2_per_objfile->objfile;
19480 const char *objf_name = objfile_name (objfile);
19481 bfd *abfd = objfile->obfd;
19482 const gdb_byte *info_ptr;
19483 ULONGEST str_offset;
19484 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19485
19486 str_section->read (objfile);
19487 str_offsets_section->read (objfile);
19488 if (str_section->buffer == NULL)
19489 error (_("%s used without %s section"
19490 " in CU at offset %s [in module %s]"),
19491 form_name, str_section->get_name (),
19492 sect_offset_str (cu->header.sect_off), objf_name);
19493 if (str_offsets_section->buffer == NULL)
19494 error (_("%s used without %s section"
19495 " in CU at offset %s [in module %s]"),
19496 form_name, str_section->get_name (),
19497 sect_offset_str (cu->header.sect_off), objf_name);
19498 info_ptr = (str_offsets_section->buffer
19499 + str_offsets_base
19500 + str_index * cu->header.offset_size);
19501 if (cu->header.offset_size == 4)
19502 str_offset = bfd_get_32 (abfd, info_ptr);
19503 else
19504 str_offset = bfd_get_64 (abfd, info_ptr);
19505 if (str_offset >= str_section->size)
19506 error (_("Offset from %s pointing outside of"
19507 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19508 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19509 return (const char *) (str_section->buffer + str_offset);
19510 }
19511
19512 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19513
19514 static const char *
19515 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19516 {
19517 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19518 ? reader->cu->header.addr_size : 0;
19519 return read_str_index (reader->cu,
19520 &reader->dwo_file->sections.str,
19521 &reader->dwo_file->sections.str_offsets,
19522 str_offsets_base, str_index);
19523 }
19524
19525 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19526
19527 static const char *
19528 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19529 {
19530 struct objfile *objfile = cu->per_objfile->objfile;
19531 const char *objf_name = objfile_name (objfile);
19532 static const char form_name[] = "DW_FORM_GNU_str_index";
19533 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19534
19535 if (!cu->str_offsets_base.has_value ())
19536 error (_("%s used in Fission stub without %s"
19537 " in CU at offset 0x%lx [in module %s]"),
19538 form_name, str_offsets_attr_name,
19539 (long) cu->header.offset_size, objf_name);
19540
19541 return read_str_index (cu,
19542 &cu->per_objfile->per_bfd->str,
19543 &cu->per_objfile->per_bfd->str_offsets,
19544 *cu->str_offsets_base, str_index);
19545 }
19546
19547 /* Return the length of an LEB128 number in BUF. */
19548
19549 static int
19550 leb128_size (const gdb_byte *buf)
19551 {
19552 const gdb_byte *begin = buf;
19553 gdb_byte byte;
19554
19555 while (1)
19556 {
19557 byte = *buf++;
19558 if ((byte & 128) == 0)
19559 return buf - begin;
19560 }
19561 }
19562
19563 static void
19564 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19565 {
19566 switch (lang)
19567 {
19568 case DW_LANG_C89:
19569 case DW_LANG_C99:
19570 case DW_LANG_C11:
19571 case DW_LANG_C:
19572 case DW_LANG_UPC:
19573 cu->language = language_c;
19574 break;
19575 case DW_LANG_Java:
19576 case DW_LANG_C_plus_plus:
19577 case DW_LANG_C_plus_plus_11:
19578 case DW_LANG_C_plus_plus_14:
19579 cu->language = language_cplus;
19580 break;
19581 case DW_LANG_D:
19582 cu->language = language_d;
19583 break;
19584 case DW_LANG_Fortran77:
19585 case DW_LANG_Fortran90:
19586 case DW_LANG_Fortran95:
19587 case DW_LANG_Fortran03:
19588 case DW_LANG_Fortran08:
19589 cu->language = language_fortran;
19590 break;
19591 case DW_LANG_Go:
19592 cu->language = language_go;
19593 break;
19594 case DW_LANG_Mips_Assembler:
19595 cu->language = language_asm;
19596 break;
19597 case DW_LANG_Ada83:
19598 case DW_LANG_Ada95:
19599 cu->language = language_ada;
19600 break;
19601 case DW_LANG_Modula2:
19602 cu->language = language_m2;
19603 break;
19604 case DW_LANG_Pascal83:
19605 cu->language = language_pascal;
19606 break;
19607 case DW_LANG_ObjC:
19608 cu->language = language_objc;
19609 break;
19610 case DW_LANG_Rust:
19611 case DW_LANG_Rust_old:
19612 cu->language = language_rust;
19613 break;
19614 case DW_LANG_Cobol74:
19615 case DW_LANG_Cobol85:
19616 default:
19617 cu->language = language_minimal;
19618 break;
19619 }
19620 cu->language_defn = language_def (cu->language);
19621 }
19622
19623 /* Return the named attribute or NULL if not there. */
19624
19625 static struct attribute *
19626 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19627 {
19628 for (;;)
19629 {
19630 unsigned int i;
19631 struct attribute *spec = NULL;
19632
19633 for (i = 0; i < die->num_attrs; ++i)
19634 {
19635 if (die->attrs[i].name == name)
19636 return &die->attrs[i];
19637 if (die->attrs[i].name == DW_AT_specification
19638 || die->attrs[i].name == DW_AT_abstract_origin)
19639 spec = &die->attrs[i];
19640 }
19641
19642 if (!spec)
19643 break;
19644
19645 die = follow_die_ref (die, spec, &cu);
19646 }
19647
19648 return NULL;
19649 }
19650
19651 /* Return the string associated with a string-typed attribute, or NULL if it
19652 is either not found or is of an incorrect type. */
19653
19654 static const char *
19655 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19656 {
19657 struct attribute *attr;
19658 const char *str = NULL;
19659
19660 attr = dwarf2_attr (die, name, cu);
19661
19662 if (attr != NULL)
19663 {
19664 str = attr->value_as_string ();
19665 if (str == nullptr)
19666 complaint (_("string type expected for attribute %s for "
19667 "DIE at %s in module %s"),
19668 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19669 objfile_name (cu->per_objfile->objfile));
19670 }
19671
19672 return str;
19673 }
19674
19675 /* Return the dwo name or NULL if not present. If present, it is in either
19676 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19677 static const char *
19678 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19679 {
19680 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19681 if (dwo_name == nullptr)
19682 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19683 return dwo_name;
19684 }
19685
19686 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19687 and holds a non-zero value. This function should only be used for
19688 DW_FORM_flag or DW_FORM_flag_present attributes. */
19689
19690 static int
19691 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19692 {
19693 struct attribute *attr = dwarf2_attr (die, name, cu);
19694
19695 return (attr && DW_UNSND (attr));
19696 }
19697
19698 static int
19699 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19700 {
19701 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19702 which value is non-zero. However, we have to be careful with
19703 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19704 (via dwarf2_flag_true_p) follows this attribute. So we may
19705 end up accidently finding a declaration attribute that belongs
19706 to a different DIE referenced by the specification attribute,
19707 even though the given DIE does not have a declaration attribute. */
19708 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19709 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19710 }
19711
19712 /* Return the die giving the specification for DIE, if there is
19713 one. *SPEC_CU is the CU containing DIE on input, and the CU
19714 containing the return value on output. If there is no
19715 specification, but there is an abstract origin, that is
19716 returned. */
19717
19718 static struct die_info *
19719 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19720 {
19721 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19722 *spec_cu);
19723
19724 if (spec_attr == NULL)
19725 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19726
19727 if (spec_attr == NULL)
19728 return NULL;
19729 else
19730 return follow_die_ref (die, spec_attr, spec_cu);
19731 }
19732
19733 /* Stub for free_line_header to match void * callback types. */
19734
19735 static void
19736 free_line_header_voidp (void *arg)
19737 {
19738 struct line_header *lh = (struct line_header *) arg;
19739
19740 delete lh;
19741 }
19742
19743 /* A convenience function to find the proper .debug_line section for a CU. */
19744
19745 static struct dwarf2_section_info *
19746 get_debug_line_section (struct dwarf2_cu *cu)
19747 {
19748 struct dwarf2_section_info *section;
19749 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19750
19751 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19752 DWO file. */
19753 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19754 section = &cu->dwo_unit->dwo_file->sections.line;
19755 else if (cu->per_cu->is_dwz)
19756 {
19757 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19758
19759 section = &dwz->line;
19760 }
19761 else
19762 section = &dwarf2_per_objfile->per_bfd->line;
19763
19764 return section;
19765 }
19766
19767 /* Read the statement program header starting at OFFSET in
19768 .debug_line, or .debug_line.dwo. Return a pointer
19769 to a struct line_header, allocated using xmalloc.
19770 Returns NULL if there is a problem reading the header, e.g., if it
19771 has a version we don't understand.
19772
19773 NOTE: the strings in the include directory and file name tables of
19774 the returned object point into the dwarf line section buffer,
19775 and must not be freed. */
19776
19777 static line_header_up
19778 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19779 {
19780 struct dwarf2_section_info *section;
19781 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19782
19783 section = get_debug_line_section (cu);
19784 section->read (dwarf2_per_objfile->objfile);
19785 if (section->buffer == NULL)
19786 {
19787 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19788 complaint (_("missing .debug_line.dwo section"));
19789 else
19790 complaint (_("missing .debug_line section"));
19791 return 0;
19792 }
19793
19794 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19795 dwarf2_per_objfile, section,
19796 &cu->header);
19797 }
19798
19799 /* Subroutine of dwarf_decode_lines to simplify it.
19800 Return the file name of the psymtab for the given file_entry.
19801 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19802 If space for the result is malloc'd, *NAME_HOLDER will be set.
19803 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19804
19805 static const char *
19806 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19807 const dwarf2_psymtab *pst,
19808 const char *comp_dir,
19809 gdb::unique_xmalloc_ptr<char> *name_holder)
19810 {
19811 const char *include_name = fe.name;
19812 const char *include_name_to_compare = include_name;
19813 const char *pst_filename;
19814 int file_is_pst;
19815
19816 const char *dir_name = fe.include_dir (lh);
19817
19818 gdb::unique_xmalloc_ptr<char> hold_compare;
19819 if (!IS_ABSOLUTE_PATH (include_name)
19820 && (dir_name != NULL || comp_dir != NULL))
19821 {
19822 /* Avoid creating a duplicate psymtab for PST.
19823 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19824 Before we do the comparison, however, we need to account
19825 for DIR_NAME and COMP_DIR.
19826 First prepend dir_name (if non-NULL). If we still don't
19827 have an absolute path prepend comp_dir (if non-NULL).
19828 However, the directory we record in the include-file's
19829 psymtab does not contain COMP_DIR (to match the
19830 corresponding symtab(s)).
19831
19832 Example:
19833
19834 bash$ cd /tmp
19835 bash$ gcc -g ./hello.c
19836 include_name = "hello.c"
19837 dir_name = "."
19838 DW_AT_comp_dir = comp_dir = "/tmp"
19839 DW_AT_name = "./hello.c"
19840
19841 */
19842
19843 if (dir_name != NULL)
19844 {
19845 name_holder->reset (concat (dir_name, SLASH_STRING,
19846 include_name, (char *) NULL));
19847 include_name = name_holder->get ();
19848 include_name_to_compare = include_name;
19849 }
19850 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19851 {
19852 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19853 include_name, (char *) NULL));
19854 include_name_to_compare = hold_compare.get ();
19855 }
19856 }
19857
19858 pst_filename = pst->filename;
19859 gdb::unique_xmalloc_ptr<char> copied_name;
19860 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19861 {
19862 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19863 pst_filename, (char *) NULL));
19864 pst_filename = copied_name.get ();
19865 }
19866
19867 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19868
19869 if (file_is_pst)
19870 return NULL;
19871 return include_name;
19872 }
19873
19874 /* State machine to track the state of the line number program. */
19875
19876 class lnp_state_machine
19877 {
19878 public:
19879 /* Initialize a machine state for the start of a line number
19880 program. */
19881 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19882 bool record_lines_p);
19883
19884 file_entry *current_file ()
19885 {
19886 /* lh->file_names is 0-based, but the file name numbers in the
19887 statement program are 1-based. */
19888 return m_line_header->file_name_at (m_file);
19889 }
19890
19891 /* Record the line in the state machine. END_SEQUENCE is true if
19892 we're processing the end of a sequence. */
19893 void record_line (bool end_sequence);
19894
19895 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19896 nop-out rest of the lines in this sequence. */
19897 void check_line_address (struct dwarf2_cu *cu,
19898 const gdb_byte *line_ptr,
19899 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19900
19901 void handle_set_discriminator (unsigned int discriminator)
19902 {
19903 m_discriminator = discriminator;
19904 m_line_has_non_zero_discriminator |= discriminator != 0;
19905 }
19906
19907 /* Handle DW_LNE_set_address. */
19908 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19909 {
19910 m_op_index = 0;
19911 address += baseaddr;
19912 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19913 }
19914
19915 /* Handle DW_LNS_advance_pc. */
19916 void handle_advance_pc (CORE_ADDR adjust);
19917
19918 /* Handle a special opcode. */
19919 void handle_special_opcode (unsigned char op_code);
19920
19921 /* Handle DW_LNS_advance_line. */
19922 void handle_advance_line (int line_delta)
19923 {
19924 advance_line (line_delta);
19925 }
19926
19927 /* Handle DW_LNS_set_file. */
19928 void handle_set_file (file_name_index file);
19929
19930 /* Handle DW_LNS_negate_stmt. */
19931 void handle_negate_stmt ()
19932 {
19933 m_is_stmt = !m_is_stmt;
19934 }
19935
19936 /* Handle DW_LNS_const_add_pc. */
19937 void handle_const_add_pc ();
19938
19939 /* Handle DW_LNS_fixed_advance_pc. */
19940 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19941 {
19942 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19943 m_op_index = 0;
19944 }
19945
19946 /* Handle DW_LNS_copy. */
19947 void handle_copy ()
19948 {
19949 record_line (false);
19950 m_discriminator = 0;
19951 }
19952
19953 /* Handle DW_LNE_end_sequence. */
19954 void handle_end_sequence ()
19955 {
19956 m_currently_recording_lines = true;
19957 }
19958
19959 private:
19960 /* Advance the line by LINE_DELTA. */
19961 void advance_line (int line_delta)
19962 {
19963 m_line += line_delta;
19964
19965 if (line_delta != 0)
19966 m_line_has_non_zero_discriminator = m_discriminator != 0;
19967 }
19968
19969 struct dwarf2_cu *m_cu;
19970
19971 gdbarch *m_gdbarch;
19972
19973 /* True if we're recording lines.
19974 Otherwise we're building partial symtabs and are just interested in
19975 finding include files mentioned by the line number program. */
19976 bool m_record_lines_p;
19977
19978 /* The line number header. */
19979 line_header *m_line_header;
19980
19981 /* These are part of the standard DWARF line number state machine,
19982 and initialized according to the DWARF spec. */
19983
19984 unsigned char m_op_index = 0;
19985 /* The line table index of the current file. */
19986 file_name_index m_file = 1;
19987 unsigned int m_line = 1;
19988
19989 /* These are initialized in the constructor. */
19990
19991 CORE_ADDR m_address;
19992 bool m_is_stmt;
19993 unsigned int m_discriminator;
19994
19995 /* Additional bits of state we need to track. */
19996
19997 /* The last file that we called dwarf2_start_subfile for.
19998 This is only used for TLLs. */
19999 unsigned int m_last_file = 0;
20000 /* The last file a line number was recorded for. */
20001 struct subfile *m_last_subfile = NULL;
20002
20003 /* When true, record the lines we decode. */
20004 bool m_currently_recording_lines = false;
20005
20006 /* The last line number that was recorded, used to coalesce
20007 consecutive entries for the same line. This can happen, for
20008 example, when discriminators are present. PR 17276. */
20009 unsigned int m_last_line = 0;
20010 bool m_line_has_non_zero_discriminator = false;
20011 };
20012
20013 void
20014 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20015 {
20016 CORE_ADDR addr_adj = (((m_op_index + adjust)
20017 / m_line_header->maximum_ops_per_instruction)
20018 * m_line_header->minimum_instruction_length);
20019 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20020 m_op_index = ((m_op_index + adjust)
20021 % m_line_header->maximum_ops_per_instruction);
20022 }
20023
20024 void
20025 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20026 {
20027 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20028 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20029 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20030 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20031 / m_line_header->maximum_ops_per_instruction)
20032 * m_line_header->minimum_instruction_length);
20033 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20034 m_op_index = ((m_op_index + adj_opcode_d)
20035 % m_line_header->maximum_ops_per_instruction);
20036
20037 int line_delta = m_line_header->line_base + adj_opcode_r;
20038 advance_line (line_delta);
20039 record_line (false);
20040 m_discriminator = 0;
20041 }
20042
20043 void
20044 lnp_state_machine::handle_set_file (file_name_index file)
20045 {
20046 m_file = file;
20047
20048 const file_entry *fe = current_file ();
20049 if (fe == NULL)
20050 dwarf2_debug_line_missing_file_complaint ();
20051 else if (m_record_lines_p)
20052 {
20053 const char *dir = fe->include_dir (m_line_header);
20054
20055 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20056 m_line_has_non_zero_discriminator = m_discriminator != 0;
20057 dwarf2_start_subfile (m_cu, fe->name, dir);
20058 }
20059 }
20060
20061 void
20062 lnp_state_machine::handle_const_add_pc ()
20063 {
20064 CORE_ADDR adjust
20065 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20066
20067 CORE_ADDR addr_adj
20068 = (((m_op_index + adjust)
20069 / m_line_header->maximum_ops_per_instruction)
20070 * m_line_header->minimum_instruction_length);
20071
20072 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20073 m_op_index = ((m_op_index + adjust)
20074 % m_line_header->maximum_ops_per_instruction);
20075 }
20076
20077 /* Return non-zero if we should add LINE to the line number table.
20078 LINE is the line to add, LAST_LINE is the last line that was added,
20079 LAST_SUBFILE is the subfile for LAST_LINE.
20080 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20081 had a non-zero discriminator.
20082
20083 We have to be careful in the presence of discriminators.
20084 E.g., for this line:
20085
20086 for (i = 0; i < 100000; i++);
20087
20088 clang can emit four line number entries for that one line,
20089 each with a different discriminator.
20090 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20091
20092 However, we want gdb to coalesce all four entries into one.
20093 Otherwise the user could stepi into the middle of the line and
20094 gdb would get confused about whether the pc really was in the
20095 middle of the line.
20096
20097 Things are further complicated by the fact that two consecutive
20098 line number entries for the same line is a heuristic used by gcc
20099 to denote the end of the prologue. So we can't just discard duplicate
20100 entries, we have to be selective about it. The heuristic we use is
20101 that we only collapse consecutive entries for the same line if at least
20102 one of those entries has a non-zero discriminator. PR 17276.
20103
20104 Note: Addresses in the line number state machine can never go backwards
20105 within one sequence, thus this coalescing is ok. */
20106
20107 static int
20108 dwarf_record_line_p (struct dwarf2_cu *cu,
20109 unsigned int line, unsigned int last_line,
20110 int line_has_non_zero_discriminator,
20111 struct subfile *last_subfile)
20112 {
20113 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20114 return 1;
20115 if (line != last_line)
20116 return 1;
20117 /* Same line for the same file that we've seen already.
20118 As a last check, for pr 17276, only record the line if the line
20119 has never had a non-zero discriminator. */
20120 if (!line_has_non_zero_discriminator)
20121 return 1;
20122 return 0;
20123 }
20124
20125 /* Use the CU's builder to record line number LINE beginning at
20126 address ADDRESS in the line table of subfile SUBFILE. */
20127
20128 static void
20129 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20130 unsigned int line, CORE_ADDR address, bool is_stmt,
20131 struct dwarf2_cu *cu)
20132 {
20133 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20134
20135 if (dwarf_line_debug)
20136 {
20137 fprintf_unfiltered (gdb_stdlog,
20138 "Recording line %u, file %s, address %s\n",
20139 line, lbasename (subfile->name),
20140 paddress (gdbarch, address));
20141 }
20142
20143 if (cu != nullptr)
20144 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20145 }
20146
20147 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20148 Mark the end of a set of line number records.
20149 The arguments are the same as for dwarf_record_line_1.
20150 If SUBFILE is NULL the request is ignored. */
20151
20152 static void
20153 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20154 CORE_ADDR address, struct dwarf2_cu *cu)
20155 {
20156 if (subfile == NULL)
20157 return;
20158
20159 if (dwarf_line_debug)
20160 {
20161 fprintf_unfiltered (gdb_stdlog,
20162 "Finishing current line, file %s, address %s\n",
20163 lbasename (subfile->name),
20164 paddress (gdbarch, address));
20165 }
20166
20167 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20168 }
20169
20170 void
20171 lnp_state_machine::record_line (bool end_sequence)
20172 {
20173 if (dwarf_line_debug)
20174 {
20175 fprintf_unfiltered (gdb_stdlog,
20176 "Processing actual line %u: file %u,"
20177 " address %s, is_stmt %u, discrim %u%s\n",
20178 m_line, m_file,
20179 paddress (m_gdbarch, m_address),
20180 m_is_stmt, m_discriminator,
20181 (end_sequence ? "\t(end sequence)" : ""));
20182 }
20183
20184 file_entry *fe = current_file ();
20185
20186 if (fe == NULL)
20187 dwarf2_debug_line_missing_file_complaint ();
20188 /* For now we ignore lines not starting on an instruction boundary.
20189 But not when processing end_sequence for compatibility with the
20190 previous version of the code. */
20191 else if (m_op_index == 0 || end_sequence)
20192 {
20193 fe->included_p = 1;
20194 if (m_record_lines_p)
20195 {
20196 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20197 || end_sequence)
20198 {
20199 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20200 m_currently_recording_lines ? m_cu : nullptr);
20201 }
20202
20203 if (!end_sequence)
20204 {
20205 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20206
20207 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20208 m_line_has_non_zero_discriminator,
20209 m_last_subfile))
20210 {
20211 buildsym_compunit *builder = m_cu->get_builder ();
20212 dwarf_record_line_1 (m_gdbarch,
20213 builder->get_current_subfile (),
20214 m_line, m_address, is_stmt,
20215 m_currently_recording_lines ? m_cu : nullptr);
20216 }
20217 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20218 m_last_line = m_line;
20219 }
20220 }
20221 }
20222 }
20223
20224 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20225 line_header *lh, bool record_lines_p)
20226 {
20227 m_cu = cu;
20228 m_gdbarch = arch;
20229 m_record_lines_p = record_lines_p;
20230 m_line_header = lh;
20231
20232 m_currently_recording_lines = true;
20233
20234 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20235 was a line entry for it so that the backend has a chance to adjust it
20236 and also record it in case it needs it. This is currently used by MIPS
20237 code, cf. `mips_adjust_dwarf2_line'. */
20238 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20239 m_is_stmt = lh->default_is_stmt;
20240 m_discriminator = 0;
20241 }
20242
20243 void
20244 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20245 const gdb_byte *line_ptr,
20246 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20247 {
20248 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20249 the pc range of the CU. However, we restrict the test to only ADDRESS
20250 values of zero to preserve GDB's previous behaviour which is to handle
20251 the specific case of a function being GC'd by the linker. */
20252
20253 if (address == 0 && address < unrelocated_lowpc)
20254 {
20255 /* This line table is for a function which has been
20256 GCd by the linker. Ignore it. PR gdb/12528 */
20257
20258 struct objfile *objfile = cu->per_objfile->objfile;
20259 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20260
20261 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20262 line_offset, objfile_name (objfile));
20263 m_currently_recording_lines = false;
20264 /* Note: m_currently_recording_lines is left as false until we see
20265 DW_LNE_end_sequence. */
20266 }
20267 }
20268
20269 /* Subroutine of dwarf_decode_lines to simplify it.
20270 Process the line number information in LH.
20271 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20272 program in order to set included_p for every referenced header. */
20273
20274 static void
20275 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20276 const int decode_for_pst_p, CORE_ADDR lowpc)
20277 {
20278 const gdb_byte *line_ptr, *extended_end;
20279 const gdb_byte *line_end;
20280 unsigned int bytes_read, extended_len;
20281 unsigned char op_code, extended_op;
20282 CORE_ADDR baseaddr;
20283 struct objfile *objfile = cu->per_objfile->objfile;
20284 bfd *abfd = objfile->obfd;
20285 struct gdbarch *gdbarch = objfile->arch ();
20286 /* True if we're recording line info (as opposed to building partial
20287 symtabs and just interested in finding include files mentioned by
20288 the line number program). */
20289 bool record_lines_p = !decode_for_pst_p;
20290
20291 baseaddr = objfile->text_section_offset ();
20292
20293 line_ptr = lh->statement_program_start;
20294 line_end = lh->statement_program_end;
20295
20296 /* Read the statement sequences until there's nothing left. */
20297 while (line_ptr < line_end)
20298 {
20299 /* The DWARF line number program state machine. Reset the state
20300 machine at the start of each sequence. */
20301 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20302 bool end_sequence = false;
20303
20304 if (record_lines_p)
20305 {
20306 /* Start a subfile for the current file of the state
20307 machine. */
20308 const file_entry *fe = state_machine.current_file ();
20309
20310 if (fe != NULL)
20311 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20312 }
20313
20314 /* Decode the table. */
20315 while (line_ptr < line_end && !end_sequence)
20316 {
20317 op_code = read_1_byte (abfd, line_ptr);
20318 line_ptr += 1;
20319
20320 if (op_code >= lh->opcode_base)
20321 {
20322 /* Special opcode. */
20323 state_machine.handle_special_opcode (op_code);
20324 }
20325 else switch (op_code)
20326 {
20327 case DW_LNS_extended_op:
20328 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20329 &bytes_read);
20330 line_ptr += bytes_read;
20331 extended_end = line_ptr + extended_len;
20332 extended_op = read_1_byte (abfd, line_ptr);
20333 line_ptr += 1;
20334 switch (extended_op)
20335 {
20336 case DW_LNE_end_sequence:
20337 state_machine.handle_end_sequence ();
20338 end_sequence = true;
20339 break;
20340 case DW_LNE_set_address:
20341 {
20342 CORE_ADDR address
20343 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20344 line_ptr += bytes_read;
20345
20346 state_machine.check_line_address (cu, line_ptr,
20347 lowpc - baseaddr, address);
20348 state_machine.handle_set_address (baseaddr, address);
20349 }
20350 break;
20351 case DW_LNE_define_file:
20352 {
20353 const char *cur_file;
20354 unsigned int mod_time, length;
20355 dir_index dindex;
20356
20357 cur_file = read_direct_string (abfd, line_ptr,
20358 &bytes_read);
20359 line_ptr += bytes_read;
20360 dindex = (dir_index)
20361 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20362 line_ptr += bytes_read;
20363 mod_time =
20364 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20365 line_ptr += bytes_read;
20366 length =
20367 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20368 line_ptr += bytes_read;
20369 lh->add_file_name (cur_file, dindex, mod_time, length);
20370 }
20371 break;
20372 case DW_LNE_set_discriminator:
20373 {
20374 /* The discriminator is not interesting to the
20375 debugger; just ignore it. We still need to
20376 check its value though:
20377 if there are consecutive entries for the same
20378 (non-prologue) line we want to coalesce them.
20379 PR 17276. */
20380 unsigned int discr
20381 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20382 line_ptr += bytes_read;
20383
20384 state_machine.handle_set_discriminator (discr);
20385 }
20386 break;
20387 default:
20388 complaint (_("mangled .debug_line section"));
20389 return;
20390 }
20391 /* Make sure that we parsed the extended op correctly. If e.g.
20392 we expected a different address size than the producer used,
20393 we may have read the wrong number of bytes. */
20394 if (line_ptr != extended_end)
20395 {
20396 complaint (_("mangled .debug_line section"));
20397 return;
20398 }
20399 break;
20400 case DW_LNS_copy:
20401 state_machine.handle_copy ();
20402 break;
20403 case DW_LNS_advance_pc:
20404 {
20405 CORE_ADDR adjust
20406 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20407 line_ptr += bytes_read;
20408
20409 state_machine.handle_advance_pc (adjust);
20410 }
20411 break;
20412 case DW_LNS_advance_line:
20413 {
20414 int line_delta
20415 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20416 line_ptr += bytes_read;
20417
20418 state_machine.handle_advance_line (line_delta);
20419 }
20420 break;
20421 case DW_LNS_set_file:
20422 {
20423 file_name_index file
20424 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20425 &bytes_read);
20426 line_ptr += bytes_read;
20427
20428 state_machine.handle_set_file (file);
20429 }
20430 break;
20431 case DW_LNS_set_column:
20432 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20433 line_ptr += bytes_read;
20434 break;
20435 case DW_LNS_negate_stmt:
20436 state_machine.handle_negate_stmt ();
20437 break;
20438 case DW_LNS_set_basic_block:
20439 break;
20440 /* Add to the address register of the state machine the
20441 address increment value corresponding to special opcode
20442 255. I.e., this value is scaled by the minimum
20443 instruction length since special opcode 255 would have
20444 scaled the increment. */
20445 case DW_LNS_const_add_pc:
20446 state_machine.handle_const_add_pc ();
20447 break;
20448 case DW_LNS_fixed_advance_pc:
20449 {
20450 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20451 line_ptr += 2;
20452
20453 state_machine.handle_fixed_advance_pc (addr_adj);
20454 }
20455 break;
20456 default:
20457 {
20458 /* Unknown standard opcode, ignore it. */
20459 int i;
20460
20461 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20462 {
20463 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20464 line_ptr += bytes_read;
20465 }
20466 }
20467 }
20468 }
20469
20470 if (!end_sequence)
20471 dwarf2_debug_line_missing_end_sequence_complaint ();
20472
20473 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20474 in which case we still finish recording the last line). */
20475 state_machine.record_line (true);
20476 }
20477 }
20478
20479 /* Decode the Line Number Program (LNP) for the given line_header
20480 structure and CU. The actual information extracted and the type
20481 of structures created from the LNP depends on the value of PST.
20482
20483 1. If PST is NULL, then this procedure uses the data from the program
20484 to create all necessary symbol tables, and their linetables.
20485
20486 2. If PST is not NULL, this procedure reads the program to determine
20487 the list of files included by the unit represented by PST, and
20488 builds all the associated partial symbol tables.
20489
20490 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20491 It is used for relative paths in the line table.
20492 NOTE: When processing partial symtabs (pst != NULL),
20493 comp_dir == pst->dirname.
20494
20495 NOTE: It is important that psymtabs have the same file name (via strcmp)
20496 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20497 symtab we don't use it in the name of the psymtabs we create.
20498 E.g. expand_line_sal requires this when finding psymtabs to expand.
20499 A good testcase for this is mb-inline.exp.
20500
20501 LOWPC is the lowest address in CU (or 0 if not known).
20502
20503 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20504 for its PC<->lines mapping information. Otherwise only the filename
20505 table is read in. */
20506
20507 static void
20508 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20509 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20510 CORE_ADDR lowpc, int decode_mapping)
20511 {
20512 struct objfile *objfile = cu->per_objfile->objfile;
20513 const int decode_for_pst_p = (pst != NULL);
20514
20515 if (decode_mapping)
20516 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20517
20518 if (decode_for_pst_p)
20519 {
20520 /* Now that we're done scanning the Line Header Program, we can
20521 create the psymtab of each included file. */
20522 for (auto &file_entry : lh->file_names ())
20523 if (file_entry.included_p == 1)
20524 {
20525 gdb::unique_xmalloc_ptr<char> name_holder;
20526 const char *include_name =
20527 psymtab_include_file_name (lh, file_entry, pst,
20528 comp_dir, &name_holder);
20529 if (include_name != NULL)
20530 dwarf2_create_include_psymtab (include_name, pst, objfile);
20531 }
20532 }
20533 else
20534 {
20535 /* Make sure a symtab is created for every file, even files
20536 which contain only variables (i.e. no code with associated
20537 line numbers). */
20538 buildsym_compunit *builder = cu->get_builder ();
20539 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20540
20541 for (auto &fe : lh->file_names ())
20542 {
20543 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20544 if (builder->get_current_subfile ()->symtab == NULL)
20545 {
20546 builder->get_current_subfile ()->symtab
20547 = allocate_symtab (cust,
20548 builder->get_current_subfile ()->name);
20549 }
20550 fe.symtab = builder->get_current_subfile ()->symtab;
20551 }
20552 }
20553 }
20554
20555 /* Start a subfile for DWARF. FILENAME is the name of the file and
20556 DIRNAME the name of the source directory which contains FILENAME
20557 or NULL if not known.
20558 This routine tries to keep line numbers from identical absolute and
20559 relative file names in a common subfile.
20560
20561 Using the `list' example from the GDB testsuite, which resides in
20562 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20563 of /srcdir/list0.c yields the following debugging information for list0.c:
20564
20565 DW_AT_name: /srcdir/list0.c
20566 DW_AT_comp_dir: /compdir
20567 files.files[0].name: list0.h
20568 files.files[0].dir: /srcdir
20569 files.files[1].name: list0.c
20570 files.files[1].dir: /srcdir
20571
20572 The line number information for list0.c has to end up in a single
20573 subfile, so that `break /srcdir/list0.c:1' works as expected.
20574 start_subfile will ensure that this happens provided that we pass the
20575 concatenation of files.files[1].dir and files.files[1].name as the
20576 subfile's name. */
20577
20578 static void
20579 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20580 const char *dirname)
20581 {
20582 gdb::unique_xmalloc_ptr<char> copy;
20583
20584 /* In order not to lose the line information directory,
20585 we concatenate it to the filename when it makes sense.
20586 Note that the Dwarf3 standard says (speaking of filenames in line
20587 information): ``The directory index is ignored for file names
20588 that represent full path names''. Thus ignoring dirname in the
20589 `else' branch below isn't an issue. */
20590
20591 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20592 {
20593 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20594 filename = copy.get ();
20595 }
20596
20597 cu->get_builder ()->start_subfile (filename);
20598 }
20599
20600 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20601 buildsym_compunit constructor. */
20602
20603 struct compunit_symtab *
20604 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20605 CORE_ADDR low_pc)
20606 {
20607 gdb_assert (m_builder == nullptr);
20608
20609 m_builder.reset (new struct buildsym_compunit
20610 (this->per_objfile->objfile,
20611 name, comp_dir, language, low_pc));
20612
20613 list_in_scope = get_builder ()->get_file_symbols ();
20614
20615 get_builder ()->record_debugformat ("DWARF 2");
20616 get_builder ()->record_producer (producer);
20617
20618 processing_has_namespace_info = false;
20619
20620 return get_builder ()->get_compunit_symtab ();
20621 }
20622
20623 static void
20624 var_decode_location (struct attribute *attr, struct symbol *sym,
20625 struct dwarf2_cu *cu)
20626 {
20627 struct objfile *objfile = cu->per_objfile->objfile;
20628 struct comp_unit_head *cu_header = &cu->header;
20629
20630 /* NOTE drow/2003-01-30: There used to be a comment and some special
20631 code here to turn a symbol with DW_AT_external and a
20632 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20633 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20634 with some versions of binutils) where shared libraries could have
20635 relocations against symbols in their debug information - the
20636 minimal symbol would have the right address, but the debug info
20637 would not. It's no longer necessary, because we will explicitly
20638 apply relocations when we read in the debug information now. */
20639
20640 /* A DW_AT_location attribute with no contents indicates that a
20641 variable has been optimized away. */
20642 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20643 {
20644 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20645 return;
20646 }
20647
20648 /* Handle one degenerate form of location expression specially, to
20649 preserve GDB's previous behavior when section offsets are
20650 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20651 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20652
20653 if (attr->form_is_block ()
20654 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20655 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20656 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20657 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20658 && (DW_BLOCK (attr)->size
20659 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20660 {
20661 unsigned int dummy;
20662
20663 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20664 SET_SYMBOL_VALUE_ADDRESS
20665 (sym, cu->header.read_address (objfile->obfd,
20666 DW_BLOCK (attr)->data + 1,
20667 &dummy));
20668 else
20669 SET_SYMBOL_VALUE_ADDRESS
20670 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20671 &dummy));
20672 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20673 fixup_symbol_section (sym, objfile);
20674 SET_SYMBOL_VALUE_ADDRESS
20675 (sym,
20676 SYMBOL_VALUE_ADDRESS (sym)
20677 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20678 return;
20679 }
20680
20681 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20682 expression evaluator, and use LOC_COMPUTED only when necessary
20683 (i.e. when the value of a register or memory location is
20684 referenced, or a thread-local block, etc.). Then again, it might
20685 not be worthwhile. I'm assuming that it isn't unless performance
20686 or memory numbers show me otherwise. */
20687
20688 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20689
20690 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20691 cu->has_loclist = true;
20692 }
20693
20694 /* Given a pointer to a DWARF information entry, figure out if we need
20695 to make a symbol table entry for it, and if so, create a new entry
20696 and return a pointer to it.
20697 If TYPE is NULL, determine symbol type from the die, otherwise
20698 used the passed type.
20699 If SPACE is not NULL, use it to hold the new symbol. If it is
20700 NULL, allocate a new symbol on the objfile's obstack. */
20701
20702 static struct symbol *
20703 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20704 struct symbol *space)
20705 {
20706 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20707 struct objfile *objfile = dwarf2_per_objfile->objfile;
20708 struct gdbarch *gdbarch = objfile->arch ();
20709 struct symbol *sym = NULL;
20710 const char *name;
20711 struct attribute *attr = NULL;
20712 struct attribute *attr2 = NULL;
20713 CORE_ADDR baseaddr;
20714 struct pending **list_to_add = NULL;
20715
20716 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20717
20718 baseaddr = objfile->text_section_offset ();
20719
20720 name = dwarf2_name (die, cu);
20721 if (name)
20722 {
20723 int suppress_add = 0;
20724
20725 if (space)
20726 sym = space;
20727 else
20728 sym = new (&objfile->objfile_obstack) symbol;
20729 OBJSTAT (objfile, n_syms++);
20730
20731 /* Cache this symbol's name and the name's demangled form (if any). */
20732 sym->set_language (cu->language, &objfile->objfile_obstack);
20733 /* Fortran does not have mangling standard and the mangling does differ
20734 between gfortran, iFort etc. */
20735 const char *physname
20736 = (cu->language == language_fortran
20737 ? dwarf2_full_name (name, die, cu)
20738 : dwarf2_physname (name, die, cu));
20739 const char *linkagename = dw2_linkage_name (die, cu);
20740
20741 if (linkagename == nullptr || cu->language == language_ada)
20742 sym->set_linkage_name (physname);
20743 else
20744 {
20745 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20746 sym->set_linkage_name (linkagename);
20747 }
20748
20749 /* Default assumptions.
20750 Use the passed type or decode it from the die. */
20751 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20752 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20753 if (type != NULL)
20754 SYMBOL_TYPE (sym) = type;
20755 else
20756 SYMBOL_TYPE (sym) = die_type (die, cu);
20757 attr = dwarf2_attr (die,
20758 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20759 cu);
20760 if (attr != nullptr)
20761 {
20762 SYMBOL_LINE (sym) = DW_UNSND (attr);
20763 }
20764
20765 attr = dwarf2_attr (die,
20766 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20767 cu);
20768 if (attr != nullptr)
20769 {
20770 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20771 struct file_entry *fe;
20772
20773 if (cu->line_header != NULL)
20774 fe = cu->line_header->file_name_at (file_index);
20775 else
20776 fe = NULL;
20777
20778 if (fe == NULL)
20779 complaint (_("file index out of range"));
20780 else
20781 symbol_set_symtab (sym, fe->symtab);
20782 }
20783
20784 switch (die->tag)
20785 {
20786 case DW_TAG_label:
20787 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20788 if (attr != nullptr)
20789 {
20790 CORE_ADDR addr;
20791
20792 addr = attr->value_as_address ();
20793 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20794 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20795 }
20796 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20797 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20798 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20799 add_symbol_to_list (sym, cu->list_in_scope);
20800 break;
20801 case DW_TAG_subprogram:
20802 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20803 finish_block. */
20804 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20805 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20806 if ((attr2 && (DW_UNSND (attr2) != 0))
20807 || cu->language == language_ada
20808 || cu->language == language_fortran)
20809 {
20810 /* Subprograms marked external are stored as a global symbol.
20811 Ada and Fortran subprograms, whether marked external or
20812 not, are always stored as a global symbol, because we want
20813 to be able to access them globally. For instance, we want
20814 to be able to break on a nested subprogram without having
20815 to specify the context. */
20816 list_to_add = cu->get_builder ()->get_global_symbols ();
20817 }
20818 else
20819 {
20820 list_to_add = cu->list_in_scope;
20821 }
20822 break;
20823 case DW_TAG_inlined_subroutine:
20824 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20825 finish_block. */
20826 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20827 SYMBOL_INLINED (sym) = 1;
20828 list_to_add = cu->list_in_scope;
20829 break;
20830 case DW_TAG_template_value_param:
20831 suppress_add = 1;
20832 /* Fall through. */
20833 case DW_TAG_constant:
20834 case DW_TAG_variable:
20835 case DW_TAG_member:
20836 /* Compilation with minimal debug info may result in
20837 variables with missing type entries. Change the
20838 misleading `void' type to something sensible. */
20839 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20840 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20841
20842 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20843 /* In the case of DW_TAG_member, we should only be called for
20844 static const members. */
20845 if (die->tag == DW_TAG_member)
20846 {
20847 /* dwarf2_add_field uses die_is_declaration,
20848 so we do the same. */
20849 gdb_assert (die_is_declaration (die, cu));
20850 gdb_assert (attr);
20851 }
20852 if (attr != nullptr)
20853 {
20854 dwarf2_const_value (attr, sym, cu);
20855 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20856 if (!suppress_add)
20857 {
20858 if (attr2 && (DW_UNSND (attr2) != 0))
20859 list_to_add = cu->get_builder ()->get_global_symbols ();
20860 else
20861 list_to_add = cu->list_in_scope;
20862 }
20863 break;
20864 }
20865 attr = dwarf2_attr (die, DW_AT_location, cu);
20866 if (attr != nullptr)
20867 {
20868 var_decode_location (attr, sym, cu);
20869 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20870
20871 /* Fortran explicitly imports any global symbols to the local
20872 scope by DW_TAG_common_block. */
20873 if (cu->language == language_fortran && die->parent
20874 && die->parent->tag == DW_TAG_common_block)
20875 attr2 = NULL;
20876
20877 if (SYMBOL_CLASS (sym) == LOC_STATIC
20878 && SYMBOL_VALUE_ADDRESS (sym) == 0
20879 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20880 {
20881 /* When a static variable is eliminated by the linker,
20882 the corresponding debug information is not stripped
20883 out, but the variable address is set to null;
20884 do not add such variables into symbol table. */
20885 }
20886 else if (attr2 && (DW_UNSND (attr2) != 0))
20887 {
20888 if (SYMBOL_CLASS (sym) == LOC_STATIC
20889 && (objfile->flags & OBJF_MAINLINE) == 0
20890 && dwarf2_per_objfile->per_bfd->can_copy)
20891 {
20892 /* A global static variable might be subject to
20893 copy relocation. We first check for a local
20894 minsym, though, because maybe the symbol was
20895 marked hidden, in which case this would not
20896 apply. */
20897 bound_minimal_symbol found
20898 = (lookup_minimal_symbol_linkage
20899 (sym->linkage_name (), objfile));
20900 if (found.minsym != nullptr)
20901 sym->maybe_copied = 1;
20902 }
20903
20904 /* A variable with DW_AT_external is never static,
20905 but it may be block-scoped. */
20906 list_to_add
20907 = ((cu->list_in_scope
20908 == cu->get_builder ()->get_file_symbols ())
20909 ? cu->get_builder ()->get_global_symbols ()
20910 : cu->list_in_scope);
20911 }
20912 else
20913 list_to_add = cu->list_in_scope;
20914 }
20915 else
20916 {
20917 /* We do not know the address of this symbol.
20918 If it is an external symbol and we have type information
20919 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20920 The address of the variable will then be determined from
20921 the minimal symbol table whenever the variable is
20922 referenced. */
20923 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20924
20925 /* Fortran explicitly imports any global symbols to the local
20926 scope by DW_TAG_common_block. */
20927 if (cu->language == language_fortran && die->parent
20928 && die->parent->tag == DW_TAG_common_block)
20929 {
20930 /* SYMBOL_CLASS doesn't matter here because
20931 read_common_block is going to reset it. */
20932 if (!suppress_add)
20933 list_to_add = cu->list_in_scope;
20934 }
20935 else if (attr2 && (DW_UNSND (attr2) != 0)
20936 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20937 {
20938 /* A variable with DW_AT_external is never static, but it
20939 may be block-scoped. */
20940 list_to_add
20941 = ((cu->list_in_scope
20942 == cu->get_builder ()->get_file_symbols ())
20943 ? cu->get_builder ()->get_global_symbols ()
20944 : cu->list_in_scope);
20945
20946 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20947 }
20948 else if (!die_is_declaration (die, cu))
20949 {
20950 /* Use the default LOC_OPTIMIZED_OUT class. */
20951 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20952 if (!suppress_add)
20953 list_to_add = cu->list_in_scope;
20954 }
20955 }
20956 break;
20957 case DW_TAG_formal_parameter:
20958 {
20959 /* If we are inside a function, mark this as an argument. If
20960 not, we might be looking at an argument to an inlined function
20961 when we do not have enough information to show inlined frames;
20962 pretend it's a local variable in that case so that the user can
20963 still see it. */
20964 struct context_stack *curr
20965 = cu->get_builder ()->get_current_context_stack ();
20966 if (curr != nullptr && curr->name != nullptr)
20967 SYMBOL_IS_ARGUMENT (sym) = 1;
20968 attr = dwarf2_attr (die, DW_AT_location, cu);
20969 if (attr != nullptr)
20970 {
20971 var_decode_location (attr, sym, cu);
20972 }
20973 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20974 if (attr != nullptr)
20975 {
20976 dwarf2_const_value (attr, sym, cu);
20977 }
20978
20979 list_to_add = cu->list_in_scope;
20980 }
20981 break;
20982 case DW_TAG_unspecified_parameters:
20983 /* From varargs functions; gdb doesn't seem to have any
20984 interest in this information, so just ignore it for now.
20985 (FIXME?) */
20986 break;
20987 case DW_TAG_template_type_param:
20988 suppress_add = 1;
20989 /* Fall through. */
20990 case DW_TAG_class_type:
20991 case DW_TAG_interface_type:
20992 case DW_TAG_structure_type:
20993 case DW_TAG_union_type:
20994 case DW_TAG_set_type:
20995 case DW_TAG_enumeration_type:
20996 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20997 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20998
20999 {
21000 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21001 really ever be static objects: otherwise, if you try
21002 to, say, break of a class's method and you're in a file
21003 which doesn't mention that class, it won't work unless
21004 the check for all static symbols in lookup_symbol_aux
21005 saves you. See the OtherFileClass tests in
21006 gdb.c++/namespace.exp. */
21007
21008 if (!suppress_add)
21009 {
21010 buildsym_compunit *builder = cu->get_builder ();
21011 list_to_add
21012 = (cu->list_in_scope == builder->get_file_symbols ()
21013 && cu->language == language_cplus
21014 ? builder->get_global_symbols ()
21015 : cu->list_in_scope);
21016
21017 /* The semantics of C++ state that "struct foo {
21018 ... }" also defines a typedef for "foo". */
21019 if (cu->language == language_cplus
21020 || cu->language == language_ada
21021 || cu->language == language_d
21022 || cu->language == language_rust)
21023 {
21024 /* The symbol's name is already allocated along
21025 with this objfile, so we don't need to
21026 duplicate it for the type. */
21027 if (SYMBOL_TYPE (sym)->name () == 0)
21028 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21029 }
21030 }
21031 }
21032 break;
21033 case DW_TAG_typedef:
21034 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21035 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21036 list_to_add = cu->list_in_scope;
21037 break;
21038 case DW_TAG_base_type:
21039 case DW_TAG_subrange_type:
21040 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21041 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21042 list_to_add = cu->list_in_scope;
21043 break;
21044 case DW_TAG_enumerator:
21045 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21046 if (attr != nullptr)
21047 {
21048 dwarf2_const_value (attr, sym, cu);
21049 }
21050 {
21051 /* NOTE: carlton/2003-11-10: See comment above in the
21052 DW_TAG_class_type, etc. block. */
21053
21054 list_to_add
21055 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21056 && cu->language == language_cplus
21057 ? cu->get_builder ()->get_global_symbols ()
21058 : cu->list_in_scope);
21059 }
21060 break;
21061 case DW_TAG_imported_declaration:
21062 case DW_TAG_namespace:
21063 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21064 list_to_add = cu->get_builder ()->get_global_symbols ();
21065 break;
21066 case DW_TAG_module:
21067 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21068 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21069 list_to_add = cu->get_builder ()->get_global_symbols ();
21070 break;
21071 case DW_TAG_common_block:
21072 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21073 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21074 add_symbol_to_list (sym, cu->list_in_scope);
21075 break;
21076 default:
21077 /* Not a tag we recognize. Hopefully we aren't processing
21078 trash data, but since we must specifically ignore things
21079 we don't recognize, there is nothing else we should do at
21080 this point. */
21081 complaint (_("unsupported tag: '%s'"),
21082 dwarf_tag_name (die->tag));
21083 break;
21084 }
21085
21086 if (suppress_add)
21087 {
21088 sym->hash_next = objfile->template_symbols;
21089 objfile->template_symbols = sym;
21090 list_to_add = NULL;
21091 }
21092
21093 if (list_to_add != NULL)
21094 add_symbol_to_list (sym, list_to_add);
21095
21096 /* For the benefit of old versions of GCC, check for anonymous
21097 namespaces based on the demangled name. */
21098 if (!cu->processing_has_namespace_info
21099 && cu->language == language_cplus)
21100 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21101 }
21102 return (sym);
21103 }
21104
21105 /* Given an attr with a DW_FORM_dataN value in host byte order,
21106 zero-extend it as appropriate for the symbol's type. The DWARF
21107 standard (v4) is not entirely clear about the meaning of using
21108 DW_FORM_dataN for a constant with a signed type, where the type is
21109 wider than the data. The conclusion of a discussion on the DWARF
21110 list was that this is unspecified. We choose to always zero-extend
21111 because that is the interpretation long in use by GCC. */
21112
21113 static gdb_byte *
21114 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21115 struct dwarf2_cu *cu, LONGEST *value, int bits)
21116 {
21117 struct objfile *objfile = cu->per_objfile->objfile;
21118 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21119 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21120 LONGEST l = DW_UNSND (attr);
21121
21122 if (bits < sizeof (*value) * 8)
21123 {
21124 l &= ((LONGEST) 1 << bits) - 1;
21125 *value = l;
21126 }
21127 else if (bits == sizeof (*value) * 8)
21128 *value = l;
21129 else
21130 {
21131 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21132 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21133 return bytes;
21134 }
21135
21136 return NULL;
21137 }
21138
21139 /* Read a constant value from an attribute. Either set *VALUE, or if
21140 the value does not fit in *VALUE, set *BYTES - either already
21141 allocated on the objfile obstack, or newly allocated on OBSTACK,
21142 or, set *BATON, if we translated the constant to a location
21143 expression. */
21144
21145 static void
21146 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21147 const char *name, struct obstack *obstack,
21148 struct dwarf2_cu *cu,
21149 LONGEST *value, const gdb_byte **bytes,
21150 struct dwarf2_locexpr_baton **baton)
21151 {
21152 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21153 struct objfile *objfile = per_objfile->objfile;
21154 struct comp_unit_head *cu_header = &cu->header;
21155 struct dwarf_block *blk;
21156 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21157 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21158
21159 *value = 0;
21160 *bytes = NULL;
21161 *baton = NULL;
21162
21163 switch (attr->form)
21164 {
21165 case DW_FORM_addr:
21166 case DW_FORM_addrx:
21167 case DW_FORM_GNU_addr_index:
21168 {
21169 gdb_byte *data;
21170
21171 if (TYPE_LENGTH (type) != cu_header->addr_size)
21172 dwarf2_const_value_length_mismatch_complaint (name,
21173 cu_header->addr_size,
21174 TYPE_LENGTH (type));
21175 /* Symbols of this form are reasonably rare, so we just
21176 piggyback on the existing location code rather than writing
21177 a new implementation of symbol_computed_ops. */
21178 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21179 (*baton)->per_objfile = per_objfile;
21180 (*baton)->per_cu = cu->per_cu;
21181 gdb_assert ((*baton)->per_cu);
21182
21183 (*baton)->size = 2 + cu_header->addr_size;
21184 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21185 (*baton)->data = data;
21186
21187 data[0] = DW_OP_addr;
21188 store_unsigned_integer (&data[1], cu_header->addr_size,
21189 byte_order, DW_ADDR (attr));
21190 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21191 }
21192 break;
21193 case DW_FORM_string:
21194 case DW_FORM_strp:
21195 case DW_FORM_strx:
21196 case DW_FORM_GNU_str_index:
21197 case DW_FORM_GNU_strp_alt:
21198 /* DW_STRING is already allocated on the objfile obstack, point
21199 directly to it. */
21200 *bytes = (const gdb_byte *) DW_STRING (attr);
21201 break;
21202 case DW_FORM_block1:
21203 case DW_FORM_block2:
21204 case DW_FORM_block4:
21205 case DW_FORM_block:
21206 case DW_FORM_exprloc:
21207 case DW_FORM_data16:
21208 blk = DW_BLOCK (attr);
21209 if (TYPE_LENGTH (type) != blk->size)
21210 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21211 TYPE_LENGTH (type));
21212 *bytes = blk->data;
21213 break;
21214
21215 /* The DW_AT_const_value attributes are supposed to carry the
21216 symbol's value "represented as it would be on the target
21217 architecture." By the time we get here, it's already been
21218 converted to host endianness, so we just need to sign- or
21219 zero-extend it as appropriate. */
21220 case DW_FORM_data1:
21221 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21222 break;
21223 case DW_FORM_data2:
21224 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21225 break;
21226 case DW_FORM_data4:
21227 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21228 break;
21229 case DW_FORM_data8:
21230 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21231 break;
21232
21233 case DW_FORM_sdata:
21234 case DW_FORM_implicit_const:
21235 *value = DW_SND (attr);
21236 break;
21237
21238 case DW_FORM_udata:
21239 *value = DW_UNSND (attr);
21240 break;
21241
21242 default:
21243 complaint (_("unsupported const value attribute form: '%s'"),
21244 dwarf_form_name (attr->form));
21245 *value = 0;
21246 break;
21247 }
21248 }
21249
21250
21251 /* Copy constant value from an attribute to a symbol. */
21252
21253 static void
21254 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21255 struct dwarf2_cu *cu)
21256 {
21257 struct objfile *objfile = cu->per_objfile->objfile;
21258 LONGEST value;
21259 const gdb_byte *bytes;
21260 struct dwarf2_locexpr_baton *baton;
21261
21262 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21263 sym->print_name (),
21264 &objfile->objfile_obstack, cu,
21265 &value, &bytes, &baton);
21266
21267 if (baton != NULL)
21268 {
21269 SYMBOL_LOCATION_BATON (sym) = baton;
21270 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21271 }
21272 else if (bytes != NULL)
21273 {
21274 SYMBOL_VALUE_BYTES (sym) = bytes;
21275 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21276 }
21277 else
21278 {
21279 SYMBOL_VALUE (sym) = value;
21280 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21281 }
21282 }
21283
21284 /* Return the type of the die in question using its DW_AT_type attribute. */
21285
21286 static struct type *
21287 die_type (struct die_info *die, struct dwarf2_cu *cu)
21288 {
21289 struct attribute *type_attr;
21290
21291 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21292 if (!type_attr)
21293 {
21294 struct objfile *objfile = cu->per_objfile->objfile;
21295 /* A missing DW_AT_type represents a void type. */
21296 return objfile_type (objfile)->builtin_void;
21297 }
21298
21299 return lookup_die_type (die, type_attr, cu);
21300 }
21301
21302 /* True iff CU's producer generates GNAT Ada auxiliary information
21303 that allows to find parallel types through that information instead
21304 of having to do expensive parallel lookups by type name. */
21305
21306 static int
21307 need_gnat_info (struct dwarf2_cu *cu)
21308 {
21309 /* Assume that the Ada compiler was GNAT, which always produces
21310 the auxiliary information. */
21311 return (cu->language == language_ada);
21312 }
21313
21314 /* Return the auxiliary type of the die in question using its
21315 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21316 attribute is not present. */
21317
21318 static struct type *
21319 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21320 {
21321 struct attribute *type_attr;
21322
21323 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21324 if (!type_attr)
21325 return NULL;
21326
21327 return lookup_die_type (die, type_attr, cu);
21328 }
21329
21330 /* If DIE has a descriptive_type attribute, then set the TYPE's
21331 descriptive type accordingly. */
21332
21333 static void
21334 set_descriptive_type (struct type *type, struct die_info *die,
21335 struct dwarf2_cu *cu)
21336 {
21337 struct type *descriptive_type = die_descriptive_type (die, cu);
21338
21339 if (descriptive_type)
21340 {
21341 ALLOCATE_GNAT_AUX_TYPE (type);
21342 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21343 }
21344 }
21345
21346 /* Return the containing type of the die in question using its
21347 DW_AT_containing_type attribute. */
21348
21349 static struct type *
21350 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21351 {
21352 struct attribute *type_attr;
21353 struct objfile *objfile = cu->per_objfile->objfile;
21354
21355 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21356 if (!type_attr)
21357 error (_("Dwarf Error: Problem turning containing type into gdb type "
21358 "[in module %s]"), objfile_name (objfile));
21359
21360 return lookup_die_type (die, type_attr, cu);
21361 }
21362
21363 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21364
21365 static struct type *
21366 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21367 {
21368 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21369 struct objfile *objfile = dwarf2_per_objfile->objfile;
21370 char *saved;
21371
21372 std::string message
21373 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21374 objfile_name (objfile),
21375 sect_offset_str (cu->header.sect_off),
21376 sect_offset_str (die->sect_off));
21377 saved = obstack_strdup (&objfile->objfile_obstack, message);
21378
21379 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21380 }
21381
21382 /* Look up the type of DIE in CU using its type attribute ATTR.
21383 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21384 DW_AT_containing_type.
21385 If there is no type substitute an error marker. */
21386
21387 static struct type *
21388 lookup_die_type (struct die_info *die, const struct attribute *attr,
21389 struct dwarf2_cu *cu)
21390 {
21391 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21392 struct objfile *objfile = dwarf2_per_objfile->objfile;
21393 struct type *this_type;
21394
21395 gdb_assert (attr->name == DW_AT_type
21396 || attr->name == DW_AT_GNAT_descriptive_type
21397 || attr->name == DW_AT_containing_type);
21398
21399 /* First see if we have it cached. */
21400
21401 if (attr->form == DW_FORM_GNU_ref_alt)
21402 {
21403 struct dwarf2_per_cu_data *per_cu;
21404 sect_offset sect_off = attr->get_ref_die_offset ();
21405
21406 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21407 dwarf2_per_objfile);
21408 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
21409 }
21410 else if (attr->form_is_ref ())
21411 {
21412 sect_offset sect_off = attr->get_ref_die_offset ();
21413
21414 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21415 dwarf2_per_objfile);
21416 }
21417 else if (attr->form == DW_FORM_ref_sig8)
21418 {
21419 ULONGEST signature = DW_SIGNATURE (attr);
21420
21421 return get_signatured_type (die, signature, cu);
21422 }
21423 else
21424 {
21425 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21426 " at %s [in module %s]"),
21427 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21428 objfile_name (objfile));
21429 return build_error_marker_type (cu, die);
21430 }
21431
21432 /* If not cached we need to read it in. */
21433
21434 if (this_type == NULL)
21435 {
21436 struct die_info *type_die = NULL;
21437 struct dwarf2_cu *type_cu = cu;
21438
21439 if (attr->form_is_ref ())
21440 type_die = follow_die_ref (die, attr, &type_cu);
21441 if (type_die == NULL)
21442 return build_error_marker_type (cu, die);
21443 /* If we find the type now, it's probably because the type came
21444 from an inter-CU reference and the type's CU got expanded before
21445 ours. */
21446 this_type = read_type_die (type_die, type_cu);
21447 }
21448
21449 /* If we still don't have a type use an error marker. */
21450
21451 if (this_type == NULL)
21452 return build_error_marker_type (cu, die);
21453
21454 return this_type;
21455 }
21456
21457 /* Return the type in DIE, CU.
21458 Returns NULL for invalid types.
21459
21460 This first does a lookup in die_type_hash,
21461 and only reads the die in if necessary.
21462
21463 NOTE: This can be called when reading in partial or full symbols. */
21464
21465 static struct type *
21466 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21467 {
21468 struct type *this_type;
21469
21470 this_type = get_die_type (die, cu);
21471 if (this_type)
21472 return this_type;
21473
21474 return read_type_die_1 (die, cu);
21475 }
21476
21477 /* Read the type in DIE, CU.
21478 Returns NULL for invalid types. */
21479
21480 static struct type *
21481 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21482 {
21483 struct type *this_type = NULL;
21484
21485 switch (die->tag)
21486 {
21487 case DW_TAG_class_type:
21488 case DW_TAG_interface_type:
21489 case DW_TAG_structure_type:
21490 case DW_TAG_union_type:
21491 this_type = read_structure_type (die, cu);
21492 break;
21493 case DW_TAG_enumeration_type:
21494 this_type = read_enumeration_type (die, cu);
21495 break;
21496 case DW_TAG_subprogram:
21497 case DW_TAG_subroutine_type:
21498 case DW_TAG_inlined_subroutine:
21499 this_type = read_subroutine_type (die, cu);
21500 break;
21501 case DW_TAG_array_type:
21502 this_type = read_array_type (die, cu);
21503 break;
21504 case DW_TAG_set_type:
21505 this_type = read_set_type (die, cu);
21506 break;
21507 case DW_TAG_pointer_type:
21508 this_type = read_tag_pointer_type (die, cu);
21509 break;
21510 case DW_TAG_ptr_to_member_type:
21511 this_type = read_tag_ptr_to_member_type (die, cu);
21512 break;
21513 case DW_TAG_reference_type:
21514 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21515 break;
21516 case DW_TAG_rvalue_reference_type:
21517 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21518 break;
21519 case DW_TAG_const_type:
21520 this_type = read_tag_const_type (die, cu);
21521 break;
21522 case DW_TAG_volatile_type:
21523 this_type = read_tag_volatile_type (die, cu);
21524 break;
21525 case DW_TAG_restrict_type:
21526 this_type = read_tag_restrict_type (die, cu);
21527 break;
21528 case DW_TAG_string_type:
21529 this_type = read_tag_string_type (die, cu);
21530 break;
21531 case DW_TAG_typedef:
21532 this_type = read_typedef (die, cu);
21533 break;
21534 case DW_TAG_subrange_type:
21535 this_type = read_subrange_type (die, cu);
21536 break;
21537 case DW_TAG_base_type:
21538 this_type = read_base_type (die, cu);
21539 break;
21540 case DW_TAG_unspecified_type:
21541 this_type = read_unspecified_type (die, cu);
21542 break;
21543 case DW_TAG_namespace:
21544 this_type = read_namespace_type (die, cu);
21545 break;
21546 case DW_TAG_module:
21547 this_type = read_module_type (die, cu);
21548 break;
21549 case DW_TAG_atomic_type:
21550 this_type = read_tag_atomic_type (die, cu);
21551 break;
21552 default:
21553 complaint (_("unexpected tag in read_type_die: '%s'"),
21554 dwarf_tag_name (die->tag));
21555 break;
21556 }
21557
21558 return this_type;
21559 }
21560
21561 /* See if we can figure out if the class lives in a namespace. We do
21562 this by looking for a member function; its demangled name will
21563 contain namespace info, if there is any.
21564 Return the computed name or NULL.
21565 Space for the result is allocated on the objfile's obstack.
21566 This is the full-die version of guess_partial_die_structure_name.
21567 In this case we know DIE has no useful parent. */
21568
21569 static const char *
21570 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21571 {
21572 struct die_info *spec_die;
21573 struct dwarf2_cu *spec_cu;
21574 struct die_info *child;
21575 struct objfile *objfile = cu->per_objfile->objfile;
21576
21577 spec_cu = cu;
21578 spec_die = die_specification (die, &spec_cu);
21579 if (spec_die != NULL)
21580 {
21581 die = spec_die;
21582 cu = spec_cu;
21583 }
21584
21585 for (child = die->child;
21586 child != NULL;
21587 child = child->sibling)
21588 {
21589 if (child->tag == DW_TAG_subprogram)
21590 {
21591 const char *linkage_name = dw2_linkage_name (child, cu);
21592
21593 if (linkage_name != NULL)
21594 {
21595 gdb::unique_xmalloc_ptr<char> actual_name
21596 (language_class_name_from_physname (cu->language_defn,
21597 linkage_name));
21598 const char *name = NULL;
21599
21600 if (actual_name != NULL)
21601 {
21602 const char *die_name = dwarf2_name (die, cu);
21603
21604 if (die_name != NULL
21605 && strcmp (die_name, actual_name.get ()) != 0)
21606 {
21607 /* Strip off the class name from the full name.
21608 We want the prefix. */
21609 int die_name_len = strlen (die_name);
21610 int actual_name_len = strlen (actual_name.get ());
21611 const char *ptr = actual_name.get ();
21612
21613 /* Test for '::' as a sanity check. */
21614 if (actual_name_len > die_name_len + 2
21615 && ptr[actual_name_len - die_name_len - 1] == ':')
21616 name = obstack_strndup (
21617 &objfile->per_bfd->storage_obstack,
21618 ptr, actual_name_len - die_name_len - 2);
21619 }
21620 }
21621 return name;
21622 }
21623 }
21624 }
21625
21626 return NULL;
21627 }
21628
21629 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21630 prefix part in such case. See
21631 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21632
21633 static const char *
21634 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21635 {
21636 struct attribute *attr;
21637 const char *base;
21638
21639 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21640 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21641 return NULL;
21642
21643 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21644 return NULL;
21645
21646 attr = dw2_linkage_name_attr (die, cu);
21647 if (attr == NULL || DW_STRING (attr) == NULL)
21648 return NULL;
21649
21650 /* dwarf2_name had to be already called. */
21651 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21652
21653 /* Strip the base name, keep any leading namespaces/classes. */
21654 base = strrchr (DW_STRING (attr), ':');
21655 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21656 return "";
21657
21658 struct objfile *objfile = cu->per_objfile->objfile;
21659 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21660 DW_STRING (attr),
21661 &base[-1] - DW_STRING (attr));
21662 }
21663
21664 /* Return the name of the namespace/class that DIE is defined within,
21665 or "" if we can't tell. The caller should not xfree the result.
21666
21667 For example, if we're within the method foo() in the following
21668 code:
21669
21670 namespace N {
21671 class C {
21672 void foo () {
21673 }
21674 };
21675 }
21676
21677 then determine_prefix on foo's die will return "N::C". */
21678
21679 static const char *
21680 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21681 {
21682 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21683 struct die_info *parent, *spec_die;
21684 struct dwarf2_cu *spec_cu;
21685 struct type *parent_type;
21686 const char *retval;
21687
21688 if (cu->language != language_cplus
21689 && cu->language != language_fortran && cu->language != language_d
21690 && cu->language != language_rust)
21691 return "";
21692
21693 retval = anonymous_struct_prefix (die, cu);
21694 if (retval)
21695 return retval;
21696
21697 /* We have to be careful in the presence of DW_AT_specification.
21698 For example, with GCC 3.4, given the code
21699
21700 namespace N {
21701 void foo() {
21702 // Definition of N::foo.
21703 }
21704 }
21705
21706 then we'll have a tree of DIEs like this:
21707
21708 1: DW_TAG_compile_unit
21709 2: DW_TAG_namespace // N
21710 3: DW_TAG_subprogram // declaration of N::foo
21711 4: DW_TAG_subprogram // definition of N::foo
21712 DW_AT_specification // refers to die #3
21713
21714 Thus, when processing die #4, we have to pretend that we're in
21715 the context of its DW_AT_specification, namely the contex of die
21716 #3. */
21717 spec_cu = cu;
21718 spec_die = die_specification (die, &spec_cu);
21719 if (spec_die == NULL)
21720 parent = die->parent;
21721 else
21722 {
21723 parent = spec_die->parent;
21724 cu = spec_cu;
21725 }
21726
21727 if (parent == NULL)
21728 return "";
21729 else if (parent->building_fullname)
21730 {
21731 const char *name;
21732 const char *parent_name;
21733
21734 /* It has been seen on RealView 2.2 built binaries,
21735 DW_TAG_template_type_param types actually _defined_ as
21736 children of the parent class:
21737
21738 enum E {};
21739 template class <class Enum> Class{};
21740 Class<enum E> class_e;
21741
21742 1: DW_TAG_class_type (Class)
21743 2: DW_TAG_enumeration_type (E)
21744 3: DW_TAG_enumerator (enum1:0)
21745 3: DW_TAG_enumerator (enum2:1)
21746 ...
21747 2: DW_TAG_template_type_param
21748 DW_AT_type DW_FORM_ref_udata (E)
21749
21750 Besides being broken debug info, it can put GDB into an
21751 infinite loop. Consider:
21752
21753 When we're building the full name for Class<E>, we'll start
21754 at Class, and go look over its template type parameters,
21755 finding E. We'll then try to build the full name of E, and
21756 reach here. We're now trying to build the full name of E,
21757 and look over the parent DIE for containing scope. In the
21758 broken case, if we followed the parent DIE of E, we'd again
21759 find Class, and once again go look at its template type
21760 arguments, etc., etc. Simply don't consider such parent die
21761 as source-level parent of this die (it can't be, the language
21762 doesn't allow it), and break the loop here. */
21763 name = dwarf2_name (die, cu);
21764 parent_name = dwarf2_name (parent, cu);
21765 complaint (_("template param type '%s' defined within parent '%s'"),
21766 name ? name : "<unknown>",
21767 parent_name ? parent_name : "<unknown>");
21768 return "";
21769 }
21770 else
21771 switch (parent->tag)
21772 {
21773 case DW_TAG_namespace:
21774 parent_type = read_type_die (parent, cu);
21775 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21776 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21777 Work around this problem here. */
21778 if (cu->language == language_cplus
21779 && strcmp (parent_type->name (), "::") == 0)
21780 return "";
21781 /* We give a name to even anonymous namespaces. */
21782 return parent_type->name ();
21783 case DW_TAG_class_type:
21784 case DW_TAG_interface_type:
21785 case DW_TAG_structure_type:
21786 case DW_TAG_union_type:
21787 case DW_TAG_module:
21788 parent_type = read_type_die (parent, cu);
21789 if (parent_type->name () != NULL)
21790 return parent_type->name ();
21791 else
21792 /* An anonymous structure is only allowed non-static data
21793 members; no typedefs, no member functions, et cetera.
21794 So it does not need a prefix. */
21795 return "";
21796 case DW_TAG_compile_unit:
21797 case DW_TAG_partial_unit:
21798 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21799 if (cu->language == language_cplus
21800 && !dwarf2_per_objfile->per_bfd->types.empty ()
21801 && die->child != NULL
21802 && (die->tag == DW_TAG_class_type
21803 || die->tag == DW_TAG_structure_type
21804 || die->tag == DW_TAG_union_type))
21805 {
21806 const char *name = guess_full_die_structure_name (die, cu);
21807 if (name != NULL)
21808 return name;
21809 }
21810 return "";
21811 case DW_TAG_subprogram:
21812 /* Nested subroutines in Fortran get a prefix with the name
21813 of the parent's subroutine. */
21814 if (cu->language == language_fortran)
21815 {
21816 if ((die->tag == DW_TAG_subprogram)
21817 && (dwarf2_name (parent, cu) != NULL))
21818 return dwarf2_name (parent, cu);
21819 }
21820 return determine_prefix (parent, cu);
21821 case DW_TAG_enumeration_type:
21822 parent_type = read_type_die (parent, cu);
21823 if (TYPE_DECLARED_CLASS (parent_type))
21824 {
21825 if (parent_type->name () != NULL)
21826 return parent_type->name ();
21827 return "";
21828 }
21829 /* Fall through. */
21830 default:
21831 return determine_prefix (parent, cu);
21832 }
21833 }
21834
21835 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21836 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21837 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21838 an obconcat, otherwise allocate storage for the result. The CU argument is
21839 used to determine the language and hence, the appropriate separator. */
21840
21841 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21842
21843 static char *
21844 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21845 int physname, struct dwarf2_cu *cu)
21846 {
21847 const char *lead = "";
21848 const char *sep;
21849
21850 if (suffix == NULL || suffix[0] == '\0'
21851 || prefix == NULL || prefix[0] == '\0')
21852 sep = "";
21853 else if (cu->language == language_d)
21854 {
21855 /* For D, the 'main' function could be defined in any module, but it
21856 should never be prefixed. */
21857 if (strcmp (suffix, "D main") == 0)
21858 {
21859 prefix = "";
21860 sep = "";
21861 }
21862 else
21863 sep = ".";
21864 }
21865 else if (cu->language == language_fortran && physname)
21866 {
21867 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21868 DW_AT_MIPS_linkage_name is preferred and used instead. */
21869
21870 lead = "__";
21871 sep = "_MOD_";
21872 }
21873 else
21874 sep = "::";
21875
21876 if (prefix == NULL)
21877 prefix = "";
21878 if (suffix == NULL)
21879 suffix = "";
21880
21881 if (obs == NULL)
21882 {
21883 char *retval
21884 = ((char *)
21885 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21886
21887 strcpy (retval, lead);
21888 strcat (retval, prefix);
21889 strcat (retval, sep);
21890 strcat (retval, suffix);
21891 return retval;
21892 }
21893 else
21894 {
21895 /* We have an obstack. */
21896 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21897 }
21898 }
21899
21900 /* Get name of a die, return NULL if not found. */
21901
21902 static const char *
21903 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21904 struct objfile *objfile)
21905 {
21906 if (name && cu->language == language_cplus)
21907 {
21908 gdb::unique_xmalloc_ptr<char> canon_name
21909 = cp_canonicalize_string (name);
21910
21911 if (canon_name != nullptr)
21912 name = objfile->intern (canon_name.get ());
21913 }
21914
21915 return name;
21916 }
21917
21918 /* Get name of a die, return NULL if not found.
21919 Anonymous namespaces are converted to their magic string. */
21920
21921 static const char *
21922 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21923 {
21924 struct attribute *attr;
21925 struct objfile *objfile = cu->per_objfile->objfile;
21926
21927 attr = dwarf2_attr (die, DW_AT_name, cu);
21928 if ((!attr || !DW_STRING (attr))
21929 && die->tag != DW_TAG_namespace
21930 && die->tag != DW_TAG_class_type
21931 && die->tag != DW_TAG_interface_type
21932 && die->tag != DW_TAG_structure_type
21933 && die->tag != DW_TAG_union_type)
21934 return NULL;
21935
21936 switch (die->tag)
21937 {
21938 case DW_TAG_compile_unit:
21939 case DW_TAG_partial_unit:
21940 /* Compilation units have a DW_AT_name that is a filename, not
21941 a source language identifier. */
21942 case DW_TAG_enumeration_type:
21943 case DW_TAG_enumerator:
21944 /* These tags always have simple identifiers already; no need
21945 to canonicalize them. */
21946 return DW_STRING (attr);
21947
21948 case DW_TAG_namespace:
21949 if (attr != NULL && DW_STRING (attr) != NULL)
21950 return DW_STRING (attr);
21951 return CP_ANONYMOUS_NAMESPACE_STR;
21952
21953 case DW_TAG_class_type:
21954 case DW_TAG_interface_type:
21955 case DW_TAG_structure_type:
21956 case DW_TAG_union_type:
21957 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21958 structures or unions. These were of the form "._%d" in GCC 4.1,
21959 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21960 and GCC 4.4. We work around this problem by ignoring these. */
21961 if (attr && DW_STRING (attr)
21962 && (startswith (DW_STRING (attr), "._")
21963 || startswith (DW_STRING (attr), "<anonymous")))
21964 return NULL;
21965
21966 /* GCC might emit a nameless typedef that has a linkage name. See
21967 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21968 if (!attr || DW_STRING (attr) == NULL)
21969 {
21970 attr = dw2_linkage_name_attr (die, cu);
21971 if (attr == NULL || DW_STRING (attr) == NULL)
21972 return NULL;
21973
21974 /* Avoid demangling DW_STRING (attr) the second time on a second
21975 call for the same DIE. */
21976 if (!DW_STRING_IS_CANONICAL (attr))
21977 {
21978 gdb::unique_xmalloc_ptr<char> demangled
21979 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21980 if (demangled == nullptr)
21981 return nullptr;
21982
21983 DW_STRING (attr) = objfile->intern (demangled.get ());
21984 DW_STRING_IS_CANONICAL (attr) = 1;
21985 }
21986
21987 /* Strip any leading namespaces/classes, keep only the base name.
21988 DW_AT_name for named DIEs does not contain the prefixes. */
21989 const char *base = strrchr (DW_STRING (attr), ':');
21990 if (base && base > DW_STRING (attr) && base[-1] == ':')
21991 return &base[1];
21992 else
21993 return DW_STRING (attr);
21994 }
21995 break;
21996
21997 default:
21998 break;
21999 }
22000
22001 if (!DW_STRING_IS_CANONICAL (attr))
22002 {
22003 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22004 objfile);
22005 DW_STRING_IS_CANONICAL (attr) = 1;
22006 }
22007 return DW_STRING (attr);
22008 }
22009
22010 /* Return the die that this die in an extension of, or NULL if there
22011 is none. *EXT_CU is the CU containing DIE on input, and the CU
22012 containing the return value on output. */
22013
22014 static struct die_info *
22015 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22016 {
22017 struct attribute *attr;
22018
22019 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22020 if (attr == NULL)
22021 return NULL;
22022
22023 return follow_die_ref (die, attr, ext_cu);
22024 }
22025
22026 static void
22027 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22028 {
22029 unsigned int i;
22030
22031 print_spaces (indent, f);
22032 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22033 dwarf_tag_name (die->tag), die->abbrev,
22034 sect_offset_str (die->sect_off));
22035
22036 if (die->parent != NULL)
22037 {
22038 print_spaces (indent, f);
22039 fprintf_unfiltered (f, " parent at offset: %s\n",
22040 sect_offset_str (die->parent->sect_off));
22041 }
22042
22043 print_spaces (indent, f);
22044 fprintf_unfiltered (f, " has children: %s\n",
22045 dwarf_bool_name (die->child != NULL));
22046
22047 print_spaces (indent, f);
22048 fprintf_unfiltered (f, " attributes:\n");
22049
22050 for (i = 0; i < die->num_attrs; ++i)
22051 {
22052 print_spaces (indent, f);
22053 fprintf_unfiltered (f, " %s (%s) ",
22054 dwarf_attr_name (die->attrs[i].name),
22055 dwarf_form_name (die->attrs[i].form));
22056
22057 switch (die->attrs[i].form)
22058 {
22059 case DW_FORM_addr:
22060 case DW_FORM_addrx:
22061 case DW_FORM_GNU_addr_index:
22062 fprintf_unfiltered (f, "address: ");
22063 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22064 break;
22065 case DW_FORM_block2:
22066 case DW_FORM_block4:
22067 case DW_FORM_block:
22068 case DW_FORM_block1:
22069 fprintf_unfiltered (f, "block: size %s",
22070 pulongest (DW_BLOCK (&die->attrs[i])->size));
22071 break;
22072 case DW_FORM_exprloc:
22073 fprintf_unfiltered (f, "expression: size %s",
22074 pulongest (DW_BLOCK (&die->attrs[i])->size));
22075 break;
22076 case DW_FORM_data16:
22077 fprintf_unfiltered (f, "constant of 16 bytes");
22078 break;
22079 case DW_FORM_ref_addr:
22080 fprintf_unfiltered (f, "ref address: ");
22081 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22082 break;
22083 case DW_FORM_GNU_ref_alt:
22084 fprintf_unfiltered (f, "alt ref address: ");
22085 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22086 break;
22087 case DW_FORM_ref1:
22088 case DW_FORM_ref2:
22089 case DW_FORM_ref4:
22090 case DW_FORM_ref8:
22091 case DW_FORM_ref_udata:
22092 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22093 (long) (DW_UNSND (&die->attrs[i])));
22094 break;
22095 case DW_FORM_data1:
22096 case DW_FORM_data2:
22097 case DW_FORM_data4:
22098 case DW_FORM_data8:
22099 case DW_FORM_udata:
22100 case DW_FORM_sdata:
22101 fprintf_unfiltered (f, "constant: %s",
22102 pulongest (DW_UNSND (&die->attrs[i])));
22103 break;
22104 case DW_FORM_sec_offset:
22105 fprintf_unfiltered (f, "section offset: %s",
22106 pulongest (DW_UNSND (&die->attrs[i])));
22107 break;
22108 case DW_FORM_ref_sig8:
22109 fprintf_unfiltered (f, "signature: %s",
22110 hex_string (DW_SIGNATURE (&die->attrs[i])));
22111 break;
22112 case DW_FORM_string:
22113 case DW_FORM_strp:
22114 case DW_FORM_line_strp:
22115 case DW_FORM_strx:
22116 case DW_FORM_GNU_str_index:
22117 case DW_FORM_GNU_strp_alt:
22118 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22119 DW_STRING (&die->attrs[i])
22120 ? DW_STRING (&die->attrs[i]) : "",
22121 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22122 break;
22123 case DW_FORM_flag:
22124 if (DW_UNSND (&die->attrs[i]))
22125 fprintf_unfiltered (f, "flag: TRUE");
22126 else
22127 fprintf_unfiltered (f, "flag: FALSE");
22128 break;
22129 case DW_FORM_flag_present:
22130 fprintf_unfiltered (f, "flag: TRUE");
22131 break;
22132 case DW_FORM_indirect:
22133 /* The reader will have reduced the indirect form to
22134 the "base form" so this form should not occur. */
22135 fprintf_unfiltered (f,
22136 "unexpected attribute form: DW_FORM_indirect");
22137 break;
22138 case DW_FORM_implicit_const:
22139 fprintf_unfiltered (f, "constant: %s",
22140 plongest (DW_SND (&die->attrs[i])));
22141 break;
22142 default:
22143 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22144 die->attrs[i].form);
22145 break;
22146 }
22147 fprintf_unfiltered (f, "\n");
22148 }
22149 }
22150
22151 static void
22152 dump_die_for_error (struct die_info *die)
22153 {
22154 dump_die_shallow (gdb_stderr, 0, die);
22155 }
22156
22157 static void
22158 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22159 {
22160 int indent = level * 4;
22161
22162 gdb_assert (die != NULL);
22163
22164 if (level >= max_level)
22165 return;
22166
22167 dump_die_shallow (f, indent, die);
22168
22169 if (die->child != NULL)
22170 {
22171 print_spaces (indent, f);
22172 fprintf_unfiltered (f, " Children:");
22173 if (level + 1 < max_level)
22174 {
22175 fprintf_unfiltered (f, "\n");
22176 dump_die_1 (f, level + 1, max_level, die->child);
22177 }
22178 else
22179 {
22180 fprintf_unfiltered (f,
22181 " [not printed, max nesting level reached]\n");
22182 }
22183 }
22184
22185 if (die->sibling != NULL && level > 0)
22186 {
22187 dump_die_1 (f, level, max_level, die->sibling);
22188 }
22189 }
22190
22191 /* This is called from the pdie macro in gdbinit.in.
22192 It's not static so gcc will keep a copy callable from gdb. */
22193
22194 void
22195 dump_die (struct die_info *die, int max_level)
22196 {
22197 dump_die_1 (gdb_stdlog, 0, max_level, die);
22198 }
22199
22200 static void
22201 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22202 {
22203 void **slot;
22204
22205 slot = htab_find_slot_with_hash (cu->die_hash, die,
22206 to_underlying (die->sect_off),
22207 INSERT);
22208
22209 *slot = die;
22210 }
22211
22212 /* Follow reference or signature attribute ATTR of SRC_DIE.
22213 On entry *REF_CU is the CU of SRC_DIE.
22214 On exit *REF_CU is the CU of the result. */
22215
22216 static struct die_info *
22217 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22218 struct dwarf2_cu **ref_cu)
22219 {
22220 struct die_info *die;
22221
22222 if (attr->form_is_ref ())
22223 die = follow_die_ref (src_die, attr, ref_cu);
22224 else if (attr->form == DW_FORM_ref_sig8)
22225 die = follow_die_sig (src_die, attr, ref_cu);
22226 else
22227 {
22228 dump_die_for_error (src_die);
22229 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22230 objfile_name ((*ref_cu)->per_objfile->objfile));
22231 }
22232
22233 return die;
22234 }
22235
22236 /* Follow reference OFFSET.
22237 On entry *REF_CU is the CU of the source die referencing OFFSET.
22238 On exit *REF_CU is the CU of the result.
22239 Returns NULL if OFFSET is invalid. */
22240
22241 static struct die_info *
22242 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22243 struct dwarf2_cu **ref_cu)
22244 {
22245 struct die_info temp_die;
22246 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22247 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22248
22249 gdb_assert (cu->per_cu != NULL);
22250
22251 target_cu = cu;
22252
22253 if (cu->per_cu->is_debug_types)
22254 {
22255 /* .debug_types CUs cannot reference anything outside their CU.
22256 If they need to, they have to reference a signatured type via
22257 DW_FORM_ref_sig8. */
22258 if (!cu->header.offset_in_cu_p (sect_off))
22259 return NULL;
22260 }
22261 else if (offset_in_dwz != cu->per_cu->is_dwz
22262 || !cu->header.offset_in_cu_p (sect_off))
22263 {
22264 struct dwarf2_per_cu_data *per_cu;
22265
22266 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22267 dwarf2_per_objfile);
22268
22269 /* If necessary, add it to the queue and load its DIEs. */
22270 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22271 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22272
22273 target_cu = per_cu->cu;
22274 }
22275 else if (cu->dies == NULL)
22276 {
22277 /* We're loading full DIEs during partial symbol reading. */
22278 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22279 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22280 language_minimal);
22281 }
22282
22283 *ref_cu = target_cu;
22284 temp_die.sect_off = sect_off;
22285
22286 if (target_cu != cu)
22287 target_cu->ancestor = cu;
22288
22289 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22290 &temp_die,
22291 to_underlying (sect_off));
22292 }
22293
22294 /* Follow reference attribute ATTR of SRC_DIE.
22295 On entry *REF_CU is the CU of SRC_DIE.
22296 On exit *REF_CU is the CU of the result. */
22297
22298 static struct die_info *
22299 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22300 struct dwarf2_cu **ref_cu)
22301 {
22302 sect_offset sect_off = attr->get_ref_die_offset ();
22303 struct dwarf2_cu *cu = *ref_cu;
22304 struct die_info *die;
22305
22306 die = follow_die_offset (sect_off,
22307 (attr->form == DW_FORM_GNU_ref_alt
22308 || cu->per_cu->is_dwz),
22309 ref_cu);
22310 if (!die)
22311 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22312 "at %s [in module %s]"),
22313 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22314 objfile_name (cu->per_objfile->objfile));
22315
22316 return die;
22317 }
22318
22319 /* See read.h. */
22320
22321 struct dwarf2_locexpr_baton
22322 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22323 dwarf2_per_cu_data *per_cu,
22324 dwarf2_per_objfile *dwarf2_per_objfile,
22325 CORE_ADDR (*get_frame_pc) (void *baton),
22326 void *baton, bool resolve_abstract_p)
22327 {
22328 struct die_info *die;
22329 struct attribute *attr;
22330 struct dwarf2_locexpr_baton retval;
22331 struct objfile *objfile = dwarf2_per_objfile->objfile;
22332
22333 dwarf2_cu *cu = per_cu->cu;
22334 if (cu == nullptr)
22335 cu = load_cu (per_cu, dwarf2_per_objfile, false);
22336
22337 if (cu == nullptr)
22338 {
22339 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22340 Instead just throw an error, not much else we can do. */
22341 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22342 sect_offset_str (sect_off), objfile_name (objfile));
22343 }
22344
22345 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22346 if (!die)
22347 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22348 sect_offset_str (sect_off), objfile_name (objfile));
22349
22350 attr = dwarf2_attr (die, DW_AT_location, cu);
22351 if (!attr && resolve_abstract_p
22352 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22353 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22354 {
22355 CORE_ADDR pc = (*get_frame_pc) (baton);
22356 CORE_ADDR baseaddr = objfile->text_section_offset ();
22357 struct gdbarch *gdbarch = objfile->arch ();
22358
22359 for (const auto &cand_off
22360 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22361 {
22362 struct dwarf2_cu *cand_cu = cu;
22363 struct die_info *cand
22364 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22365 if (!cand
22366 || !cand->parent
22367 || cand->parent->tag != DW_TAG_subprogram)
22368 continue;
22369
22370 CORE_ADDR pc_low, pc_high;
22371 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22372 if (pc_low == ((CORE_ADDR) -1))
22373 continue;
22374 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22375 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22376 if (!(pc_low <= pc && pc < pc_high))
22377 continue;
22378
22379 die = cand;
22380 attr = dwarf2_attr (die, DW_AT_location, cu);
22381 break;
22382 }
22383 }
22384
22385 if (!attr)
22386 {
22387 /* DWARF: "If there is no such attribute, then there is no effect.".
22388 DATA is ignored if SIZE is 0. */
22389
22390 retval.data = NULL;
22391 retval.size = 0;
22392 }
22393 else if (attr->form_is_section_offset ())
22394 {
22395 struct dwarf2_loclist_baton loclist_baton;
22396 CORE_ADDR pc = (*get_frame_pc) (baton);
22397 size_t size;
22398
22399 fill_in_loclist_baton (cu, &loclist_baton, attr);
22400
22401 retval.data = dwarf2_find_location_expression (&loclist_baton,
22402 &size, pc);
22403 retval.size = size;
22404 }
22405 else
22406 {
22407 if (!attr->form_is_block ())
22408 error (_("Dwarf Error: DIE at %s referenced in module %s "
22409 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22410 sect_offset_str (sect_off), objfile_name (objfile));
22411
22412 retval.data = DW_BLOCK (attr)->data;
22413 retval.size = DW_BLOCK (attr)->size;
22414 }
22415 retval.per_objfile = dwarf2_per_objfile;
22416 retval.per_cu = cu->per_cu;
22417
22418 age_cached_comp_units (dwarf2_per_objfile);
22419
22420 return retval;
22421 }
22422
22423 /* See read.h. */
22424
22425 struct dwarf2_locexpr_baton
22426 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22427 dwarf2_per_cu_data *per_cu,
22428 dwarf2_per_objfile *per_objfile,
22429 CORE_ADDR (*get_frame_pc) (void *baton),
22430 void *baton)
22431 {
22432 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22433
22434 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22435 get_frame_pc, baton);
22436 }
22437
22438 /* Write a constant of a given type as target-ordered bytes into
22439 OBSTACK. */
22440
22441 static const gdb_byte *
22442 write_constant_as_bytes (struct obstack *obstack,
22443 enum bfd_endian byte_order,
22444 struct type *type,
22445 ULONGEST value,
22446 LONGEST *len)
22447 {
22448 gdb_byte *result;
22449
22450 *len = TYPE_LENGTH (type);
22451 result = (gdb_byte *) obstack_alloc (obstack, *len);
22452 store_unsigned_integer (result, *len, byte_order, value);
22453
22454 return result;
22455 }
22456
22457 /* See read.h. */
22458
22459 const gdb_byte *
22460 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22461 dwarf2_per_cu_data *per_cu,
22462 dwarf2_per_objfile *per_objfile,
22463 obstack *obstack,
22464 LONGEST *len)
22465 {
22466 struct die_info *die;
22467 struct attribute *attr;
22468 const gdb_byte *result = NULL;
22469 struct type *type;
22470 LONGEST value;
22471 enum bfd_endian byte_order;
22472 struct objfile *objfile = per_objfile->objfile;
22473
22474 dwarf2_cu *cu = per_cu->cu;
22475 if (cu == nullptr)
22476 cu = load_cu (per_cu, per_objfile, false);
22477
22478 if (cu == nullptr)
22479 {
22480 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22481 Instead just throw an error, not much else we can do. */
22482 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22483 sect_offset_str (sect_off), objfile_name (objfile));
22484 }
22485
22486 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22487 if (!die)
22488 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22489 sect_offset_str (sect_off), objfile_name (objfile));
22490
22491 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22492 if (attr == NULL)
22493 return NULL;
22494
22495 byte_order = (bfd_big_endian (objfile->obfd)
22496 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22497
22498 switch (attr->form)
22499 {
22500 case DW_FORM_addr:
22501 case DW_FORM_addrx:
22502 case DW_FORM_GNU_addr_index:
22503 {
22504 gdb_byte *tem;
22505
22506 *len = cu->header.addr_size;
22507 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22508 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22509 result = tem;
22510 }
22511 break;
22512 case DW_FORM_string:
22513 case DW_FORM_strp:
22514 case DW_FORM_strx:
22515 case DW_FORM_GNU_str_index:
22516 case DW_FORM_GNU_strp_alt:
22517 /* DW_STRING is already allocated on the objfile obstack, point
22518 directly to it. */
22519 result = (const gdb_byte *) DW_STRING (attr);
22520 *len = strlen (DW_STRING (attr));
22521 break;
22522 case DW_FORM_block1:
22523 case DW_FORM_block2:
22524 case DW_FORM_block4:
22525 case DW_FORM_block:
22526 case DW_FORM_exprloc:
22527 case DW_FORM_data16:
22528 result = DW_BLOCK (attr)->data;
22529 *len = DW_BLOCK (attr)->size;
22530 break;
22531
22532 /* The DW_AT_const_value attributes are supposed to carry the
22533 symbol's value "represented as it would be on the target
22534 architecture." By the time we get here, it's already been
22535 converted to host endianness, so we just need to sign- or
22536 zero-extend it as appropriate. */
22537 case DW_FORM_data1:
22538 type = die_type (die, cu);
22539 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22540 if (result == NULL)
22541 result = write_constant_as_bytes (obstack, byte_order,
22542 type, value, len);
22543 break;
22544 case DW_FORM_data2:
22545 type = die_type (die, cu);
22546 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22547 if (result == NULL)
22548 result = write_constant_as_bytes (obstack, byte_order,
22549 type, value, len);
22550 break;
22551 case DW_FORM_data4:
22552 type = die_type (die, cu);
22553 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22554 if (result == NULL)
22555 result = write_constant_as_bytes (obstack, byte_order,
22556 type, value, len);
22557 break;
22558 case DW_FORM_data8:
22559 type = die_type (die, cu);
22560 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22561 if (result == NULL)
22562 result = write_constant_as_bytes (obstack, byte_order,
22563 type, value, len);
22564 break;
22565
22566 case DW_FORM_sdata:
22567 case DW_FORM_implicit_const:
22568 type = die_type (die, cu);
22569 result = write_constant_as_bytes (obstack, byte_order,
22570 type, DW_SND (attr), len);
22571 break;
22572
22573 case DW_FORM_udata:
22574 type = die_type (die, cu);
22575 result = write_constant_as_bytes (obstack, byte_order,
22576 type, DW_UNSND (attr), len);
22577 break;
22578
22579 default:
22580 complaint (_("unsupported const value attribute form: '%s'"),
22581 dwarf_form_name (attr->form));
22582 break;
22583 }
22584
22585 return result;
22586 }
22587
22588 /* See read.h. */
22589
22590 struct type *
22591 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22592 dwarf2_per_cu_data *per_cu,
22593 dwarf2_per_objfile *per_objfile)
22594 {
22595 struct die_info *die;
22596
22597 dwarf2_cu *cu = per_cu->cu;
22598 if (cu == nullptr)
22599 cu = load_cu (per_cu, per_objfile, false);
22600
22601 if (cu == nullptr)
22602 return nullptr;
22603
22604 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22605 if (!die)
22606 return NULL;
22607
22608 return die_type (die, cu);
22609 }
22610
22611 /* See read.h. */
22612
22613 struct type *
22614 dwarf2_get_die_type (cu_offset die_offset,
22615 dwarf2_per_cu_data *per_cu,
22616 dwarf2_per_objfile *per_objfile)
22617 {
22618 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22619 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22620 }
22621
22622 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22623 On entry *REF_CU is the CU of SRC_DIE.
22624 On exit *REF_CU is the CU of the result.
22625 Returns NULL if the referenced DIE isn't found. */
22626
22627 static struct die_info *
22628 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22629 struct dwarf2_cu **ref_cu)
22630 {
22631 struct die_info temp_die;
22632 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22633 struct die_info *die;
22634 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22635
22636
22637 /* While it might be nice to assert sig_type->type == NULL here,
22638 we can get here for DW_AT_imported_declaration where we need
22639 the DIE not the type. */
22640
22641 /* If necessary, add it to the queue and load its DIEs. */
22642
22643 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22644 language_minimal))
22645 read_signatured_type (sig_type, dwarf2_per_objfile);
22646
22647 sig_cu = sig_type->per_cu.cu;
22648 gdb_assert (sig_cu != NULL);
22649 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22650 temp_die.sect_off = sig_type->type_offset_in_section;
22651 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22652 to_underlying (temp_die.sect_off));
22653 if (die)
22654 {
22655 /* For .gdb_index version 7 keep track of included TUs.
22656 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22657 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22658 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22659 {
22660 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22661 }
22662
22663 *ref_cu = sig_cu;
22664 if (sig_cu != cu)
22665 sig_cu->ancestor = cu;
22666
22667 return die;
22668 }
22669
22670 return NULL;
22671 }
22672
22673 /* Follow signatured type referenced by ATTR in SRC_DIE.
22674 On entry *REF_CU is the CU of SRC_DIE.
22675 On exit *REF_CU is the CU of the result.
22676 The result is the DIE of the type.
22677 If the referenced type cannot be found an error is thrown. */
22678
22679 static struct die_info *
22680 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22681 struct dwarf2_cu **ref_cu)
22682 {
22683 ULONGEST signature = DW_SIGNATURE (attr);
22684 struct signatured_type *sig_type;
22685 struct die_info *die;
22686
22687 gdb_assert (attr->form == DW_FORM_ref_sig8);
22688
22689 sig_type = lookup_signatured_type (*ref_cu, signature);
22690 /* sig_type will be NULL if the signatured type is missing from
22691 the debug info. */
22692 if (sig_type == NULL)
22693 {
22694 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22695 " from DIE at %s [in module %s]"),
22696 hex_string (signature), sect_offset_str (src_die->sect_off),
22697 objfile_name ((*ref_cu)->per_objfile->objfile));
22698 }
22699
22700 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22701 if (die == NULL)
22702 {
22703 dump_die_for_error (src_die);
22704 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22705 " from DIE at %s [in module %s]"),
22706 hex_string (signature), sect_offset_str (src_die->sect_off),
22707 objfile_name ((*ref_cu)->per_objfile->objfile));
22708 }
22709
22710 return die;
22711 }
22712
22713 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22714 reading in and processing the type unit if necessary. */
22715
22716 static struct type *
22717 get_signatured_type (struct die_info *die, ULONGEST signature,
22718 struct dwarf2_cu *cu)
22719 {
22720 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22721 struct signatured_type *sig_type;
22722 struct dwarf2_cu *type_cu;
22723 struct die_info *type_die;
22724 struct type *type;
22725
22726 sig_type = lookup_signatured_type (cu, signature);
22727 /* sig_type will be NULL if the signatured type is missing from
22728 the debug info. */
22729 if (sig_type == NULL)
22730 {
22731 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22732 " from DIE at %s [in module %s]"),
22733 hex_string (signature), sect_offset_str (die->sect_off),
22734 objfile_name (dwarf2_per_objfile->objfile));
22735 return build_error_marker_type (cu, die);
22736 }
22737
22738 /* If we already know the type we're done. */
22739 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22740 if (type != nullptr)
22741 return type;
22742
22743 type_cu = cu;
22744 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22745 if (type_die != NULL)
22746 {
22747 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22748 is created. This is important, for example, because for c++ classes
22749 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22750 type = read_type_die (type_die, type_cu);
22751 if (type == NULL)
22752 {
22753 complaint (_("Dwarf Error: Cannot build signatured type %s"
22754 " referenced from DIE at %s [in module %s]"),
22755 hex_string (signature), sect_offset_str (die->sect_off),
22756 objfile_name (dwarf2_per_objfile->objfile));
22757 type = build_error_marker_type (cu, die);
22758 }
22759 }
22760 else
22761 {
22762 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22763 " from DIE at %s [in module %s]"),
22764 hex_string (signature), sect_offset_str (die->sect_off),
22765 objfile_name (dwarf2_per_objfile->objfile));
22766 type = build_error_marker_type (cu, die);
22767 }
22768
22769 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
22770
22771 return type;
22772 }
22773
22774 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22775 reading in and processing the type unit if necessary. */
22776
22777 static struct type *
22778 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22779 struct dwarf2_cu *cu) /* ARI: editCase function */
22780 {
22781 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22782 if (attr->form_is_ref ())
22783 {
22784 struct dwarf2_cu *type_cu = cu;
22785 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22786
22787 return read_type_die (type_die, type_cu);
22788 }
22789 else if (attr->form == DW_FORM_ref_sig8)
22790 {
22791 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22792 }
22793 else
22794 {
22795 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22796
22797 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22798 " at %s [in module %s]"),
22799 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22800 objfile_name (dwarf2_per_objfile->objfile));
22801 return build_error_marker_type (cu, die);
22802 }
22803 }
22804
22805 /* Load the DIEs associated with type unit PER_CU into memory. */
22806
22807 static void
22808 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22809 dwarf2_per_objfile *per_objfile)
22810 {
22811 struct signatured_type *sig_type;
22812
22813 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22814 gdb_assert (! per_cu->type_unit_group_p ());
22815
22816 /* We have the per_cu, but we need the signatured_type.
22817 Fortunately this is an easy translation. */
22818 gdb_assert (per_cu->is_debug_types);
22819 sig_type = (struct signatured_type *) per_cu;
22820
22821 gdb_assert (per_cu->cu == NULL);
22822
22823 read_signatured_type (sig_type, per_objfile);
22824
22825 gdb_assert (per_cu->cu != NULL);
22826 }
22827
22828 /* Read in a signatured type and build its CU and DIEs.
22829 If the type is a stub for the real type in a DWO file,
22830 read in the real type from the DWO file as well. */
22831
22832 static void
22833 read_signatured_type (signatured_type *sig_type,
22834 dwarf2_per_objfile *per_objfile)
22835 {
22836 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22837
22838 gdb_assert (per_cu->is_debug_types);
22839 gdb_assert (per_cu->cu == NULL);
22840
22841 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22842
22843 if (!reader.dummy_p)
22844 {
22845 struct dwarf2_cu *cu = reader.cu;
22846 const gdb_byte *info_ptr = reader.info_ptr;
22847
22848 gdb_assert (cu->die_hash == NULL);
22849 cu->die_hash =
22850 htab_create_alloc_ex (cu->header.length / 12,
22851 die_hash,
22852 die_eq,
22853 NULL,
22854 &cu->comp_unit_obstack,
22855 hashtab_obstack_allocate,
22856 dummy_obstack_deallocate);
22857
22858 if (reader.comp_unit_die->has_children)
22859 reader.comp_unit_die->child
22860 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22861 reader.comp_unit_die);
22862 cu->dies = reader.comp_unit_die;
22863 /* comp_unit_die is not stored in die_hash, no need. */
22864
22865 /* We try not to read any attributes in this function, because
22866 not all CUs needed for references have been loaded yet, and
22867 symbol table processing isn't initialized. But we have to
22868 set the CU language, or we won't be able to build types
22869 correctly. Similarly, if we do not read the producer, we can
22870 not apply producer-specific interpretation. */
22871 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22872
22873 reader.keep ();
22874 }
22875
22876 sig_type->per_cu.tu_read = 1;
22877 }
22878
22879 /* Decode simple location descriptions.
22880 Given a pointer to a dwarf block that defines a location, compute
22881 the location and return the value. If COMPUTED is non-null, it is
22882 set to true to indicate that decoding was successful, and false
22883 otherwise. If COMPUTED is null, then this function may emit a
22884 complaint. */
22885
22886 static CORE_ADDR
22887 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22888 {
22889 struct objfile *objfile = cu->per_objfile->objfile;
22890 size_t i;
22891 size_t size = blk->size;
22892 const gdb_byte *data = blk->data;
22893 CORE_ADDR stack[64];
22894 int stacki;
22895 unsigned int bytes_read, unsnd;
22896 gdb_byte op;
22897
22898 if (computed != nullptr)
22899 *computed = false;
22900
22901 i = 0;
22902 stacki = 0;
22903 stack[stacki] = 0;
22904 stack[++stacki] = 0;
22905
22906 while (i < size)
22907 {
22908 op = data[i++];
22909 switch (op)
22910 {
22911 case DW_OP_lit0:
22912 case DW_OP_lit1:
22913 case DW_OP_lit2:
22914 case DW_OP_lit3:
22915 case DW_OP_lit4:
22916 case DW_OP_lit5:
22917 case DW_OP_lit6:
22918 case DW_OP_lit7:
22919 case DW_OP_lit8:
22920 case DW_OP_lit9:
22921 case DW_OP_lit10:
22922 case DW_OP_lit11:
22923 case DW_OP_lit12:
22924 case DW_OP_lit13:
22925 case DW_OP_lit14:
22926 case DW_OP_lit15:
22927 case DW_OP_lit16:
22928 case DW_OP_lit17:
22929 case DW_OP_lit18:
22930 case DW_OP_lit19:
22931 case DW_OP_lit20:
22932 case DW_OP_lit21:
22933 case DW_OP_lit22:
22934 case DW_OP_lit23:
22935 case DW_OP_lit24:
22936 case DW_OP_lit25:
22937 case DW_OP_lit26:
22938 case DW_OP_lit27:
22939 case DW_OP_lit28:
22940 case DW_OP_lit29:
22941 case DW_OP_lit30:
22942 case DW_OP_lit31:
22943 stack[++stacki] = op - DW_OP_lit0;
22944 break;
22945
22946 case DW_OP_reg0:
22947 case DW_OP_reg1:
22948 case DW_OP_reg2:
22949 case DW_OP_reg3:
22950 case DW_OP_reg4:
22951 case DW_OP_reg5:
22952 case DW_OP_reg6:
22953 case DW_OP_reg7:
22954 case DW_OP_reg8:
22955 case DW_OP_reg9:
22956 case DW_OP_reg10:
22957 case DW_OP_reg11:
22958 case DW_OP_reg12:
22959 case DW_OP_reg13:
22960 case DW_OP_reg14:
22961 case DW_OP_reg15:
22962 case DW_OP_reg16:
22963 case DW_OP_reg17:
22964 case DW_OP_reg18:
22965 case DW_OP_reg19:
22966 case DW_OP_reg20:
22967 case DW_OP_reg21:
22968 case DW_OP_reg22:
22969 case DW_OP_reg23:
22970 case DW_OP_reg24:
22971 case DW_OP_reg25:
22972 case DW_OP_reg26:
22973 case DW_OP_reg27:
22974 case DW_OP_reg28:
22975 case DW_OP_reg29:
22976 case DW_OP_reg30:
22977 case DW_OP_reg31:
22978 stack[++stacki] = op - DW_OP_reg0;
22979 if (i < size)
22980 {
22981 if (computed == nullptr)
22982 dwarf2_complex_location_expr_complaint ();
22983 else
22984 return 0;
22985 }
22986 break;
22987
22988 case DW_OP_regx:
22989 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22990 i += bytes_read;
22991 stack[++stacki] = unsnd;
22992 if (i < size)
22993 {
22994 if (computed == nullptr)
22995 dwarf2_complex_location_expr_complaint ();
22996 else
22997 return 0;
22998 }
22999 break;
23000
23001 case DW_OP_addr:
23002 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23003 &bytes_read);
23004 i += bytes_read;
23005 break;
23006
23007 case DW_OP_const1u:
23008 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23009 i += 1;
23010 break;
23011
23012 case DW_OP_const1s:
23013 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23014 i += 1;
23015 break;
23016
23017 case DW_OP_const2u:
23018 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23019 i += 2;
23020 break;
23021
23022 case DW_OP_const2s:
23023 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23024 i += 2;
23025 break;
23026
23027 case DW_OP_const4u:
23028 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23029 i += 4;
23030 break;
23031
23032 case DW_OP_const4s:
23033 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23034 i += 4;
23035 break;
23036
23037 case DW_OP_const8u:
23038 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23039 i += 8;
23040 break;
23041
23042 case DW_OP_constu:
23043 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23044 &bytes_read);
23045 i += bytes_read;
23046 break;
23047
23048 case DW_OP_consts:
23049 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23050 i += bytes_read;
23051 break;
23052
23053 case DW_OP_dup:
23054 stack[stacki + 1] = stack[stacki];
23055 stacki++;
23056 break;
23057
23058 case DW_OP_plus:
23059 stack[stacki - 1] += stack[stacki];
23060 stacki--;
23061 break;
23062
23063 case DW_OP_plus_uconst:
23064 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23065 &bytes_read);
23066 i += bytes_read;
23067 break;
23068
23069 case DW_OP_minus:
23070 stack[stacki - 1] -= stack[stacki];
23071 stacki--;
23072 break;
23073
23074 case DW_OP_deref:
23075 /* If we're not the last op, then we definitely can't encode
23076 this using GDB's address_class enum. This is valid for partial
23077 global symbols, although the variable's address will be bogus
23078 in the psymtab. */
23079 if (i < size)
23080 {
23081 if (computed == nullptr)
23082 dwarf2_complex_location_expr_complaint ();
23083 else
23084 return 0;
23085 }
23086 break;
23087
23088 case DW_OP_GNU_push_tls_address:
23089 case DW_OP_form_tls_address:
23090 /* The top of the stack has the offset from the beginning
23091 of the thread control block at which the variable is located. */
23092 /* Nothing should follow this operator, so the top of stack would
23093 be returned. */
23094 /* This is valid for partial global symbols, but the variable's
23095 address will be bogus in the psymtab. Make it always at least
23096 non-zero to not look as a variable garbage collected by linker
23097 which have DW_OP_addr 0. */
23098 if (i < size)
23099 {
23100 if (computed == nullptr)
23101 dwarf2_complex_location_expr_complaint ();
23102 else
23103 return 0;
23104 }
23105 stack[stacki]++;
23106 break;
23107
23108 case DW_OP_GNU_uninit:
23109 if (computed != nullptr)
23110 return 0;
23111 break;
23112
23113 case DW_OP_addrx:
23114 case DW_OP_GNU_addr_index:
23115 case DW_OP_GNU_const_index:
23116 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23117 &bytes_read);
23118 i += bytes_read;
23119 break;
23120
23121 default:
23122 if (computed == nullptr)
23123 {
23124 const char *name = get_DW_OP_name (op);
23125
23126 if (name)
23127 complaint (_("unsupported stack op: '%s'"),
23128 name);
23129 else
23130 complaint (_("unsupported stack op: '%02x'"),
23131 op);
23132 }
23133
23134 return (stack[stacki]);
23135 }
23136
23137 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23138 outside of the allocated space. Also enforce minimum>0. */
23139 if (stacki >= ARRAY_SIZE (stack) - 1)
23140 {
23141 if (computed == nullptr)
23142 complaint (_("location description stack overflow"));
23143 return 0;
23144 }
23145
23146 if (stacki <= 0)
23147 {
23148 if (computed == nullptr)
23149 complaint (_("location description stack underflow"));
23150 return 0;
23151 }
23152 }
23153
23154 if (computed != nullptr)
23155 *computed = true;
23156 return (stack[stacki]);
23157 }
23158
23159 /* memory allocation interface */
23160
23161 static struct dwarf_block *
23162 dwarf_alloc_block (struct dwarf2_cu *cu)
23163 {
23164 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23165 }
23166
23167 static struct die_info *
23168 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23169 {
23170 struct die_info *die;
23171 size_t size = sizeof (struct die_info);
23172
23173 if (num_attrs > 1)
23174 size += (num_attrs - 1) * sizeof (struct attribute);
23175
23176 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23177 memset (die, 0, sizeof (struct die_info));
23178 return (die);
23179 }
23180
23181 \f
23182
23183 /* Macro support. */
23184
23185 /* An overload of dwarf_decode_macros that finds the correct section
23186 and ensures it is read in before calling the other overload. */
23187
23188 static void
23189 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23190 int section_is_gnu)
23191 {
23192 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23193 struct objfile *objfile = dwarf2_per_objfile->objfile;
23194 const struct line_header *lh = cu->line_header;
23195 unsigned int offset_size = cu->header.offset_size;
23196 struct dwarf2_section_info *section;
23197 const char *section_name;
23198
23199 if (cu->dwo_unit != nullptr)
23200 {
23201 if (section_is_gnu)
23202 {
23203 section = &cu->dwo_unit->dwo_file->sections.macro;
23204 section_name = ".debug_macro.dwo";
23205 }
23206 else
23207 {
23208 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23209 section_name = ".debug_macinfo.dwo";
23210 }
23211 }
23212 else
23213 {
23214 if (section_is_gnu)
23215 {
23216 section = &dwarf2_per_objfile->per_bfd->macro;
23217 section_name = ".debug_macro";
23218 }
23219 else
23220 {
23221 section = &dwarf2_per_objfile->per_bfd->macinfo;
23222 section_name = ".debug_macinfo";
23223 }
23224 }
23225
23226 section->read (objfile);
23227 if (section->buffer == nullptr)
23228 {
23229 complaint (_("missing %s section"), section_name);
23230 return;
23231 }
23232
23233 buildsym_compunit *builder = cu->get_builder ();
23234
23235 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23236 offset_size, offset, section_is_gnu);
23237 }
23238
23239 /* Return the .debug_loc section to use for CU.
23240 For DWO files use .debug_loc.dwo. */
23241
23242 static struct dwarf2_section_info *
23243 cu_debug_loc_section (struct dwarf2_cu *cu)
23244 {
23245 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23246
23247 if (cu->dwo_unit)
23248 {
23249 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23250
23251 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23252 }
23253 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23254 : &dwarf2_per_objfile->per_bfd->loc);
23255 }
23256
23257 /* A helper function that fills in a dwarf2_loclist_baton. */
23258
23259 static void
23260 fill_in_loclist_baton (struct dwarf2_cu *cu,
23261 struct dwarf2_loclist_baton *baton,
23262 const struct attribute *attr)
23263 {
23264 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23265 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23266
23267 section->read (dwarf2_per_objfile->objfile);
23268
23269 baton->per_objfile = dwarf2_per_objfile;
23270 baton->per_cu = cu->per_cu;
23271 gdb_assert (baton->per_cu);
23272 /* We don't know how long the location list is, but make sure we
23273 don't run off the edge of the section. */
23274 baton->size = section->size - DW_UNSND (attr);
23275 baton->data = section->buffer + DW_UNSND (attr);
23276 if (cu->base_address.has_value ())
23277 baton->base_address = *cu->base_address;
23278 else
23279 baton->base_address = 0;
23280 baton->from_dwo = cu->dwo_unit != NULL;
23281 }
23282
23283 static void
23284 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23285 struct dwarf2_cu *cu, int is_block)
23286 {
23287 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23288 struct objfile *objfile = dwarf2_per_objfile->objfile;
23289 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23290
23291 if (attr->form_is_section_offset ()
23292 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23293 the section. If so, fall through to the complaint in the
23294 other branch. */
23295 && DW_UNSND (attr) < section->get_size (objfile))
23296 {
23297 struct dwarf2_loclist_baton *baton;
23298
23299 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23300
23301 fill_in_loclist_baton (cu, baton, attr);
23302
23303 if (!cu->base_address.has_value ())
23304 complaint (_("Location list used without "
23305 "specifying the CU base address."));
23306
23307 SYMBOL_ACLASS_INDEX (sym) = (is_block
23308 ? dwarf2_loclist_block_index
23309 : dwarf2_loclist_index);
23310 SYMBOL_LOCATION_BATON (sym) = baton;
23311 }
23312 else
23313 {
23314 struct dwarf2_locexpr_baton *baton;
23315
23316 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23317 baton->per_objfile = dwarf2_per_objfile;
23318 baton->per_cu = cu->per_cu;
23319 gdb_assert (baton->per_cu);
23320
23321 if (attr->form_is_block ())
23322 {
23323 /* Note that we're just copying the block's data pointer
23324 here, not the actual data. We're still pointing into the
23325 info_buffer for SYM's objfile; right now we never release
23326 that buffer, but when we do clean up properly this may
23327 need to change. */
23328 baton->size = DW_BLOCK (attr)->size;
23329 baton->data = DW_BLOCK (attr)->data;
23330 }
23331 else
23332 {
23333 dwarf2_invalid_attrib_class_complaint ("location description",
23334 sym->natural_name ());
23335 baton->size = 0;
23336 }
23337
23338 SYMBOL_ACLASS_INDEX (sym) = (is_block
23339 ? dwarf2_locexpr_block_index
23340 : dwarf2_locexpr_index);
23341 SYMBOL_LOCATION_BATON (sym) = baton;
23342 }
23343 }
23344
23345 /* See read.h. */
23346
23347 const comp_unit_head *
23348 dwarf2_per_cu_data::get_header () const
23349 {
23350 if (!m_header_read_in)
23351 {
23352 const gdb_byte *info_ptr
23353 = this->section->buffer + to_underlying (this->sect_off);
23354
23355 memset (&m_header, 0, sizeof (m_header));
23356
23357 read_comp_unit_head (&m_header, info_ptr, this->section,
23358 rcuh_kind::COMPILE);
23359 }
23360
23361 return &m_header;
23362 }
23363
23364 /* See read.h. */
23365
23366 int
23367 dwarf2_per_cu_data::addr_size () const
23368 {
23369 return this->get_header ()->addr_size;
23370 }
23371
23372 /* See read.h. */
23373
23374 int
23375 dwarf2_per_cu_data::offset_size () const
23376 {
23377 return this->get_header ()->offset_size;
23378 }
23379
23380 /* See read.h. */
23381
23382 int
23383 dwarf2_per_cu_data::ref_addr_size () const
23384 {
23385 const comp_unit_head *header = this->get_header ();
23386
23387 if (header->version == 2)
23388 return header->addr_size;
23389 else
23390 return header->offset_size;
23391 }
23392
23393 /* See read.h. */
23394
23395 struct type *
23396 dwarf2_cu::addr_type () const
23397 {
23398 struct objfile *objfile = this->per_objfile->objfile;
23399 struct type *void_type = objfile_type (objfile)->builtin_void;
23400 struct type *addr_type = lookup_pointer_type (void_type);
23401 int addr_size = this->per_cu->addr_size ();
23402
23403 if (TYPE_LENGTH (addr_type) == addr_size)
23404 return addr_type;
23405
23406 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23407 return addr_type;
23408 }
23409
23410 /* A helper function for dwarf2_find_containing_comp_unit that returns
23411 the index of the result, and that searches a vector. It will
23412 return a result even if the offset in question does not actually
23413 occur in any CU. This is separate so that it can be unit
23414 tested. */
23415
23416 static int
23417 dwarf2_find_containing_comp_unit
23418 (sect_offset sect_off,
23419 unsigned int offset_in_dwz,
23420 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23421 {
23422 int low, high;
23423
23424 low = 0;
23425 high = all_comp_units.size () - 1;
23426 while (high > low)
23427 {
23428 struct dwarf2_per_cu_data *mid_cu;
23429 int mid = low + (high - low) / 2;
23430
23431 mid_cu = all_comp_units[mid];
23432 if (mid_cu->is_dwz > offset_in_dwz
23433 || (mid_cu->is_dwz == offset_in_dwz
23434 && mid_cu->sect_off + mid_cu->length > sect_off))
23435 high = mid;
23436 else
23437 low = mid + 1;
23438 }
23439 gdb_assert (low == high);
23440 return low;
23441 }
23442
23443 /* Locate the .debug_info compilation unit from CU's objfile which contains
23444 the DIE at OFFSET. Raises an error on failure. */
23445
23446 static struct dwarf2_per_cu_data *
23447 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23448 unsigned int offset_in_dwz,
23449 struct dwarf2_per_objfile *dwarf2_per_objfile)
23450 {
23451 int low
23452 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23453 dwarf2_per_objfile->per_bfd->all_comp_units);
23454 struct dwarf2_per_cu_data *this_cu
23455 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23456
23457 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23458 {
23459 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23460 error (_("Dwarf Error: could not find partial DIE containing "
23461 "offset %s [in module %s]"),
23462 sect_offset_str (sect_off),
23463 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23464
23465 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23466 <= sect_off);
23467 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23468 }
23469 else
23470 {
23471 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23472 && sect_off >= this_cu->sect_off + this_cu->length)
23473 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23474 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23475 return this_cu;
23476 }
23477 }
23478
23479 #if GDB_SELF_TEST
23480
23481 namespace selftests {
23482 namespace find_containing_comp_unit {
23483
23484 static void
23485 run_test ()
23486 {
23487 struct dwarf2_per_cu_data one {};
23488 struct dwarf2_per_cu_data two {};
23489 struct dwarf2_per_cu_data three {};
23490 struct dwarf2_per_cu_data four {};
23491
23492 one.length = 5;
23493 two.sect_off = sect_offset (one.length);
23494 two.length = 7;
23495
23496 three.length = 5;
23497 three.is_dwz = 1;
23498 four.sect_off = sect_offset (three.length);
23499 four.length = 7;
23500 four.is_dwz = 1;
23501
23502 std::vector<dwarf2_per_cu_data *> units;
23503 units.push_back (&one);
23504 units.push_back (&two);
23505 units.push_back (&three);
23506 units.push_back (&four);
23507
23508 int result;
23509
23510 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23511 SELF_CHECK (units[result] == &one);
23512 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23513 SELF_CHECK (units[result] == &one);
23514 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23515 SELF_CHECK (units[result] == &two);
23516
23517 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23518 SELF_CHECK (units[result] == &three);
23519 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23520 SELF_CHECK (units[result] == &three);
23521 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23522 SELF_CHECK (units[result] == &four);
23523 }
23524
23525 }
23526 }
23527
23528 #endif /* GDB_SELF_TEST */
23529
23530 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23531
23532 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23533 dwarf2_per_objfile *per_objfile)
23534 : per_cu (per_cu),
23535 per_objfile (per_objfile),
23536 mark (false),
23537 has_loclist (false),
23538 checked_producer (false),
23539 producer_is_gxx_lt_4_6 (false),
23540 producer_is_gcc_lt_4_3 (false),
23541 producer_is_icc (false),
23542 producer_is_icc_lt_14 (false),
23543 producer_is_codewarrior (false),
23544 processing_has_namespace_info (false)
23545 {
23546 per_cu->cu = this;
23547 }
23548
23549 /* Destroy a dwarf2_cu. */
23550
23551 dwarf2_cu::~dwarf2_cu ()
23552 {
23553 per_cu->cu = NULL;
23554 }
23555
23556 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23557
23558 static void
23559 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23560 enum language pretend_language)
23561 {
23562 struct attribute *attr;
23563
23564 /* Set the language we're debugging. */
23565 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23566 if (attr != nullptr)
23567 set_cu_language (DW_UNSND (attr), cu);
23568 else
23569 {
23570 cu->language = pretend_language;
23571 cu->language_defn = language_def (cu->language);
23572 }
23573
23574 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23575 }
23576
23577 /* Increase the age counter on each cached compilation unit, and free
23578 any that are too old. */
23579
23580 static void
23581 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23582 {
23583 struct dwarf2_per_cu_data *per_cu, **last_chain;
23584
23585 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23586 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23587 while (per_cu != NULL)
23588 {
23589 per_cu->cu->last_used ++;
23590 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23591 dwarf2_mark (per_cu->cu);
23592 per_cu = per_cu->cu->read_in_chain;
23593 }
23594
23595 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23596 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23597 while (per_cu != NULL)
23598 {
23599 struct dwarf2_per_cu_data *next_cu;
23600
23601 next_cu = per_cu->cu->read_in_chain;
23602
23603 if (!per_cu->cu->mark)
23604 {
23605 delete per_cu->cu;
23606 *last_chain = next_cu;
23607 }
23608 else
23609 last_chain = &per_cu->cu->read_in_chain;
23610
23611 per_cu = next_cu;
23612 }
23613 }
23614
23615 /* Remove a single compilation unit from the cache. */
23616
23617 static void
23618 free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23619 dwarf2_per_objfile *dwarf2_per_objfile)
23620 {
23621 struct dwarf2_per_cu_data *per_cu, **last_chain;
23622
23623 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23624 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23625 while (per_cu != NULL)
23626 {
23627 struct dwarf2_per_cu_data *next_cu;
23628
23629 next_cu = per_cu->cu->read_in_chain;
23630
23631 if (per_cu == target_per_cu)
23632 {
23633 delete per_cu->cu;
23634 per_cu->cu = NULL;
23635 *last_chain = next_cu;
23636 break;
23637 }
23638 else
23639 last_chain = &per_cu->cu->read_in_chain;
23640
23641 per_cu = next_cu;
23642 }
23643 }
23644
23645 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23646 We store these in a hash table separate from the DIEs, and preserve them
23647 when the DIEs are flushed out of cache.
23648
23649 The CU "per_cu" pointer is needed because offset alone is not enough to
23650 uniquely identify the type. A file may have multiple .debug_types sections,
23651 or the type may come from a DWO file. Furthermore, while it's more logical
23652 to use per_cu->section+offset, with Fission the section with the data is in
23653 the DWO file but we don't know that section at the point we need it.
23654 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23655 because we can enter the lookup routine, get_die_type_at_offset, from
23656 outside this file, and thus won't necessarily have PER_CU->cu.
23657 Fortunately, PER_CU is stable for the life of the objfile. */
23658
23659 struct dwarf2_per_cu_offset_and_type
23660 {
23661 const struct dwarf2_per_cu_data *per_cu;
23662 sect_offset sect_off;
23663 struct type *type;
23664 };
23665
23666 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23667
23668 static hashval_t
23669 per_cu_offset_and_type_hash (const void *item)
23670 {
23671 const struct dwarf2_per_cu_offset_and_type *ofs
23672 = (const struct dwarf2_per_cu_offset_and_type *) item;
23673
23674 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23675 }
23676
23677 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23678
23679 static int
23680 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23681 {
23682 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23683 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23684 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23685 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23686
23687 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23688 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23689 }
23690
23691 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23692 table if necessary. For convenience, return TYPE.
23693
23694 The DIEs reading must have careful ordering to:
23695 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23696 reading current DIE.
23697 * Not trying to dereference contents of still incompletely read in types
23698 while reading in other DIEs.
23699 * Enable referencing still incompletely read in types just by a pointer to
23700 the type without accessing its fields.
23701
23702 Therefore caller should follow these rules:
23703 * Try to fetch any prerequisite types we may need to build this DIE type
23704 before building the type and calling set_die_type.
23705 * After building type call set_die_type for current DIE as soon as
23706 possible before fetching more types to complete the current type.
23707 * Make the type as complete as possible before fetching more types. */
23708
23709 static struct type *
23710 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23711 {
23712 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23713 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23714 struct objfile *objfile = dwarf2_per_objfile->objfile;
23715 struct attribute *attr;
23716 struct dynamic_prop prop;
23717
23718 /* For Ada types, make sure that the gnat-specific data is always
23719 initialized (if not already set). There are a few types where
23720 we should not be doing so, because the type-specific area is
23721 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23722 where the type-specific area is used to store the floatformat).
23723 But this is not a problem, because the gnat-specific information
23724 is actually not needed for these types. */
23725 if (need_gnat_info (cu)
23726 && type->code () != TYPE_CODE_FUNC
23727 && type->code () != TYPE_CODE_FLT
23728 && type->code () != TYPE_CODE_METHODPTR
23729 && type->code () != TYPE_CODE_MEMBERPTR
23730 && type->code () != TYPE_CODE_METHOD
23731 && !HAVE_GNAT_AUX_INFO (type))
23732 INIT_GNAT_SPECIFIC (type);
23733
23734 /* Read DW_AT_allocated and set in type. */
23735 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23736 if (attr != NULL && attr->form_is_block ())
23737 {
23738 struct type *prop_type = cu->addr_sized_int_type (false);
23739 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23740 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23741 }
23742 else if (attr != NULL)
23743 {
23744 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23745 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23746 sect_offset_str (die->sect_off));
23747 }
23748
23749 /* Read DW_AT_associated and set in type. */
23750 attr = dwarf2_attr (die, DW_AT_associated, cu);
23751 if (attr != NULL && attr->form_is_block ())
23752 {
23753 struct type *prop_type = cu->addr_sized_int_type (false);
23754 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23755 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23756 }
23757 else if (attr != NULL)
23758 {
23759 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23760 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23761 sect_offset_str (die->sect_off));
23762 }
23763
23764 /* Read DW_AT_data_location and set in type. */
23765 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23766 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23767 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23768
23769 if (dwarf2_per_objfile->die_type_hash == NULL)
23770 dwarf2_per_objfile->die_type_hash
23771 = htab_up (htab_create_alloc (127,
23772 per_cu_offset_and_type_hash,
23773 per_cu_offset_and_type_eq,
23774 NULL, xcalloc, xfree));
23775
23776 ofs.per_cu = cu->per_cu;
23777 ofs.sect_off = die->sect_off;
23778 ofs.type = type;
23779 slot = (struct dwarf2_per_cu_offset_and_type **)
23780 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23781 if (*slot)
23782 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23783 sect_offset_str (die->sect_off));
23784 *slot = XOBNEW (&objfile->objfile_obstack,
23785 struct dwarf2_per_cu_offset_and_type);
23786 **slot = ofs;
23787 return type;
23788 }
23789
23790 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23791 or return NULL if the die does not have a saved type. */
23792
23793 static struct type *
23794 get_die_type_at_offset (sect_offset sect_off,
23795 dwarf2_per_cu_data *per_cu,
23796 dwarf2_per_objfile *dwarf2_per_objfile)
23797 {
23798 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23799
23800 if (dwarf2_per_objfile->die_type_hash == NULL)
23801 return NULL;
23802
23803 ofs.per_cu = per_cu;
23804 ofs.sect_off = sect_off;
23805 slot = ((struct dwarf2_per_cu_offset_and_type *)
23806 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23807 if (slot)
23808 return slot->type;
23809 else
23810 return NULL;
23811 }
23812
23813 /* Look up the type for DIE in CU in die_type_hash,
23814 or return NULL if DIE does not have a saved type. */
23815
23816 static struct type *
23817 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23818 {
23819 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23820 }
23821
23822 /* Add a dependence relationship from CU to REF_PER_CU. */
23823
23824 static void
23825 dwarf2_add_dependence (struct dwarf2_cu *cu,
23826 struct dwarf2_per_cu_data *ref_per_cu)
23827 {
23828 void **slot;
23829
23830 if (cu->dependencies == NULL)
23831 cu->dependencies
23832 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23833 NULL, &cu->comp_unit_obstack,
23834 hashtab_obstack_allocate,
23835 dummy_obstack_deallocate);
23836
23837 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23838 if (*slot == NULL)
23839 *slot = ref_per_cu;
23840 }
23841
23842 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23843 Set the mark field in every compilation unit in the
23844 cache that we must keep because we are keeping CU. */
23845
23846 static int
23847 dwarf2_mark_helper (void **slot, void *data)
23848 {
23849 struct dwarf2_per_cu_data *per_cu;
23850
23851 per_cu = (struct dwarf2_per_cu_data *) *slot;
23852
23853 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23854 reading of the chain. As such dependencies remain valid it is not much
23855 useful to track and undo them during QUIT cleanups. */
23856 if (per_cu->cu == NULL)
23857 return 1;
23858
23859 if (per_cu->cu->mark)
23860 return 1;
23861 per_cu->cu->mark = true;
23862
23863 if (per_cu->cu->dependencies != NULL)
23864 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23865
23866 return 1;
23867 }
23868
23869 /* Set the mark field in CU and in every other compilation unit in the
23870 cache that we must keep because we are keeping CU. */
23871
23872 static void
23873 dwarf2_mark (struct dwarf2_cu *cu)
23874 {
23875 if (cu->mark)
23876 return;
23877 cu->mark = true;
23878 if (cu->dependencies != NULL)
23879 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23880 }
23881
23882 static void
23883 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23884 {
23885 while (per_cu)
23886 {
23887 per_cu->cu->mark = false;
23888 per_cu = per_cu->cu->read_in_chain;
23889 }
23890 }
23891
23892 /* Trivial hash function for partial_die_info: the hash value of a DIE
23893 is its offset in .debug_info for this objfile. */
23894
23895 static hashval_t
23896 partial_die_hash (const void *item)
23897 {
23898 const struct partial_die_info *part_die
23899 = (const struct partial_die_info *) item;
23900
23901 return to_underlying (part_die->sect_off);
23902 }
23903
23904 /* Trivial comparison function for partial_die_info structures: two DIEs
23905 are equal if they have the same offset. */
23906
23907 static int
23908 partial_die_eq (const void *item_lhs, const void *item_rhs)
23909 {
23910 const struct partial_die_info *part_die_lhs
23911 = (const struct partial_die_info *) item_lhs;
23912 const struct partial_die_info *part_die_rhs
23913 = (const struct partial_die_info *) item_rhs;
23914
23915 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23916 }
23917
23918 struct cmd_list_element *set_dwarf_cmdlist;
23919 struct cmd_list_element *show_dwarf_cmdlist;
23920
23921 static void
23922 show_check_physname (struct ui_file *file, int from_tty,
23923 struct cmd_list_element *c, const char *value)
23924 {
23925 fprintf_filtered (file,
23926 _("Whether to check \"physname\" is %s.\n"),
23927 value);
23928 }
23929
23930 void _initialize_dwarf2_read ();
23931 void
23932 _initialize_dwarf2_read ()
23933 {
23934 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23935 Set DWARF specific variables.\n\
23936 Configure DWARF variables such as the cache size."),
23937 &set_dwarf_cmdlist, "maintenance set dwarf ",
23938 0/*allow-unknown*/, &maintenance_set_cmdlist);
23939
23940 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23941 Show DWARF specific variables.\n\
23942 Show DWARF variables such as the cache size."),
23943 &show_dwarf_cmdlist, "maintenance show dwarf ",
23944 0/*allow-unknown*/, &maintenance_show_cmdlist);
23945
23946 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23947 &dwarf_max_cache_age, _("\
23948 Set the upper bound on the age of cached DWARF compilation units."), _("\
23949 Show the upper bound on the age of cached DWARF compilation units."), _("\
23950 A higher limit means that cached compilation units will be stored\n\
23951 in memory longer, and more total memory will be used. Zero disables\n\
23952 caching, which can slow down startup."),
23953 NULL,
23954 show_dwarf_max_cache_age,
23955 &set_dwarf_cmdlist,
23956 &show_dwarf_cmdlist);
23957
23958 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23959 Set debugging of the DWARF reader."), _("\
23960 Show debugging of the DWARF reader."), _("\
23961 When enabled (non-zero), debugging messages are printed during DWARF\n\
23962 reading and symtab expansion. A value of 1 (one) provides basic\n\
23963 information. A value greater than 1 provides more verbose information."),
23964 NULL,
23965 NULL,
23966 &setdebuglist, &showdebuglist);
23967
23968 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23969 Set debugging of the DWARF DIE reader."), _("\
23970 Show debugging of the DWARF DIE reader."), _("\
23971 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23972 The value is the maximum depth to print."),
23973 NULL,
23974 NULL,
23975 &setdebuglist, &showdebuglist);
23976
23977 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23978 Set debugging of the dwarf line reader."), _("\
23979 Show debugging of the dwarf line reader."), _("\
23980 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23981 A value of 1 (one) provides basic information.\n\
23982 A value greater than 1 provides more verbose information."),
23983 NULL,
23984 NULL,
23985 &setdebuglist, &showdebuglist);
23986
23987 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23988 Set cross-checking of \"physname\" code against demangler."), _("\
23989 Show cross-checking of \"physname\" code against demangler."), _("\
23990 When enabled, GDB's internal \"physname\" code is checked against\n\
23991 the demangler."),
23992 NULL, show_check_physname,
23993 &setdebuglist, &showdebuglist);
23994
23995 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23996 no_class, &use_deprecated_index_sections, _("\
23997 Set whether to use deprecated gdb_index sections."), _("\
23998 Show whether to use deprecated gdb_index sections."), _("\
23999 When enabled, deprecated .gdb_index sections are used anyway.\n\
24000 Normally they are ignored either because of a missing feature or\n\
24001 performance issue.\n\
24002 Warning: This option must be enabled before gdb reads the file."),
24003 NULL,
24004 NULL,
24005 &setlist, &showlist);
24006
24007 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24008 &dwarf2_locexpr_funcs);
24009 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24010 &dwarf2_loclist_funcs);
24011
24012 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24013 &dwarf2_block_frame_base_locexpr_funcs);
24014 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24015 &dwarf2_block_frame_base_loclist_funcs);
24016
24017 #if GDB_SELF_TEST
24018 selftests::register_test ("dw2_expand_symtabs_matching",
24019 selftests::dw2_expand_symtabs_matching::run_test);
24020 selftests::register_test ("dwarf2_find_containing_comp_unit",
24021 selftests::find_containing_comp_unit::run_test);
24022 #endif
24023 }
This page took 0.621886 seconds and 4 git commands to generate.