]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Add dwarf2_per_objfile parameter to free_one_cached_comp_unit
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
424 /* Return a type that is a generic pointer type, the size of which
425 matches the address size given in the compilation unit header for
426 this CU. */
427 struct type *addr_type () const;
428
429 /* Find an integer type the same size as the address size given in
430 the compilation unit header for this CU. UNSIGNED_P controls if
431 the integer is unsigned or not. */
432 struct type *addr_sized_int_type (bool unsigned_p) const;
433
434 /* The header of the compilation unit. */
435 struct comp_unit_head header {};
436
437 /* Base address of this compilation unit. */
438 gdb::optional<CORE_ADDR> base_address;
439
440 /* The language we are debugging. */
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
443
444 const char *producer = nullptr;
445
446 private:
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
449 std::unique_ptr<buildsym_compunit> m_builder;
450
451 public:
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
461 struct pending **list_in_scope = nullptr;
462
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
465 htab_t partial_dies = nullptr;
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
469 auto_obstack comp_unit_obstack;
470
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
476
477 /* Backlink to our per_cu entry. */
478 struct dwarf2_per_cu_data *per_cu;
479
480 /* The dwarf2_per_objfile that owns this. */
481 struct dwarf2_per_objfile *per_objfile;
482
483 /* How many compilation units ago was this CU last referenced? */
484 int last_used = 0;
485
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
488 htab_t die_hash = nullptr;
489
490 /* Full DIEs if read in. */
491 struct die_info *dies = nullptr;
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
496 htab_t dependencies = nullptr;
497
498 /* Header data from the line table, during full symbol processing. */
499 struct line_header *line_header = nullptr;
500 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
501 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
502 this is the DW_TAG_compile_unit die for this CU. We'll hold on
503 to the line header as long as this DIE is being processed. See
504 process_die_scope. */
505 die_info *line_header_die_owner = nullptr;
506
507 /* A list of methods which need to have physnames computed
508 after all type information has been read. */
509 std::vector<delayed_method_info> method_list;
510
511 /* To be copied to symtab->call_site_htab. */
512 htab_t call_site_htab = nullptr;
513
514 /* Non-NULL if this CU came from a DWO file.
515 There is an invariant here that is important to remember:
516 Except for attributes copied from the top level DIE in the "main"
517 (or "stub") file in preparation for reading the DWO file
518 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
519 Either there isn't a DWO file (in which case this is NULL and the point
520 is moot), or there is and either we're not going to read it (in which
521 case this is NULL) or there is and we are reading it (in which case this
522 is non-NULL). */
523 struct dwo_unit *dwo_unit = nullptr;
524
525 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
526 Note this value comes from the Fission stub CU/TU's DIE. */
527 gdb::optional<ULONGEST> addr_base;
528
529 /* The DW_AT_rnglists_base attribute if present.
530 Note this value comes from the Fission stub CU/TU's DIE.
531 Also note that the value is zero in the non-DWO case so this value can
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
536 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
538 ULONGEST ranges_base = 0;
539
540 /* The DW_AT_loclists_base attribute if present. */
541 ULONGEST loclist_base = 0;
542
543 /* When reading debug info generated by older versions of rustc, we
544 have to rewrite some union types to be struct types with a
545 variant part. This rewriting must be done after the CU is fully
546 read in, because otherwise at the point of rewriting some struct
547 type might not have been fully processed. So, we keep a list of
548 all such types here and process them after expansion. */
549 std::vector<struct type *> rust_unions;
550
551 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
552 files, the value is implicitly zero. For DWARF 5 version DWO files, the
553 value is often implicit and is the size of the header of
554 .debug_str_offsets section (8 or 4, depending on the address size). */
555 gdb::optional<ULONGEST> str_offsets_base;
556
557 /* Mark used when releasing cached dies. */
558 bool mark : 1;
559
560 /* This CU references .debug_loc. See the symtab->locations_valid field.
561 This test is imperfect as there may exist optimized debug code not using
562 any location list and still facing inlining issues if handled as
563 unoptimized code. For a future better test see GCC PR other/32998. */
564 bool has_loclist : 1;
565
566 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
567 if all the producer_is_* fields are valid. This information is cached
568 because profiling CU expansion showed excessive time spent in
569 producer_is_gxx_lt_4_6. */
570 bool checked_producer : 1;
571 bool producer_is_gxx_lt_4_6 : 1;
572 bool producer_is_gcc_lt_4_3 : 1;
573 bool producer_is_icc : 1;
574 bool producer_is_icc_lt_14 : 1;
575 bool producer_is_codewarrior : 1;
576
577 /* When true, the file that we're processing is known to have
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
581 bool processing_has_namespace_info : 1;
582
583 struct partial_die_info *find_partial_die (sect_offset sect_off);
584
585 /* If this CU was inherited by another CU (via specification,
586 abstract_origin, etc), this is the ancestor CU. */
587 dwarf2_cu *ancestor;
588
589 /* Get the buildsym_compunit for this CU. */
590 buildsym_compunit *get_builder ()
591 {
592 /* If this CU has a builder associated with it, use that. */
593 if (m_builder != nullptr)
594 return m_builder.get ();
595
596 /* Otherwise, search ancestors for a valid builder. */
597 if (ancestor != nullptr)
598 return ancestor->get_builder ();
599
600 return nullptr;
601 }
602 };
603
604 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
605 This includes type_unit_group and quick_file_names. */
606
607 struct stmt_list_hash
608 {
609 /* The DWO unit this table is from or NULL if there is none. */
610 struct dwo_unit *dwo_unit;
611
612 /* Offset in .debug_line or .debug_line.dwo. */
613 sect_offset line_sect_off;
614 };
615
616 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
617 an object of this type. */
618
619 struct type_unit_group
620 {
621 /* dwarf2read.c's main "handle" on a TU symtab.
622 To simplify things we create an artificial CU that "includes" all the
623 type units using this stmt_list so that the rest of the code still has
624 a "per_cu" handle on the symtab. */
625 struct dwarf2_per_cu_data per_cu;
626
627 /* The TUs that share this DW_AT_stmt_list entry.
628 This is added to while parsing type units to build partial symtabs,
629 and is deleted afterwards and not used again. */
630 std::vector<signatured_type *> *tus;
631
632 /* The compunit symtab.
633 Type units in a group needn't all be defined in the same source file,
634 so we create an essentially anonymous symtab as the compunit symtab. */
635 struct compunit_symtab *compunit_symtab;
636
637 /* The data used to construct the hash key. */
638 struct stmt_list_hash hash;
639
640 /* The symbol tables for this TU (obtained from the files listed in
641 DW_AT_stmt_list).
642 WARNING: The order of entries here must match the order of entries
643 in the line header. After the first TU using this type_unit_group, the
644 line header for the subsequent TUs is recreated from this. This is done
645 because we need to use the same symtabs for each TU using the same
646 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
647 there's no guarantee the line header doesn't have duplicate entries. */
648 struct symtab **symtabs;
649 };
650
651 /* These sections are what may appear in a (real or virtual) DWO file. */
652
653 struct dwo_sections
654 {
655 struct dwarf2_section_info abbrev;
656 struct dwarf2_section_info line;
657 struct dwarf2_section_info loc;
658 struct dwarf2_section_info loclists;
659 struct dwarf2_section_info macinfo;
660 struct dwarf2_section_info macro;
661 struct dwarf2_section_info str;
662 struct dwarf2_section_info str_offsets;
663 /* In the case of a virtual DWO file, these two are unused. */
664 struct dwarf2_section_info info;
665 std::vector<dwarf2_section_info> types;
666 };
667
668 /* CUs/TUs in DWP/DWO files. */
669
670 struct dwo_unit
671 {
672 /* Backlink to the containing struct dwo_file. */
673 struct dwo_file *dwo_file;
674
675 /* The "id" that distinguishes this CU/TU.
676 .debug_info calls this "dwo_id", .debug_types calls this "signature".
677 Since signatures came first, we stick with it for consistency. */
678 ULONGEST signature;
679
680 /* The section this CU/TU lives in, in the DWO file. */
681 struct dwarf2_section_info *section;
682
683 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
684 sect_offset sect_off;
685 unsigned int length;
686
687 /* For types, offset in the type's DIE of the type defined by this TU. */
688 cu_offset type_offset_in_tu;
689 };
690
691 /* include/dwarf2.h defines the DWP section codes.
692 It defines a max value but it doesn't define a min value, which we
693 use for error checking, so provide one. */
694
695 enum dwp_v2_section_ids
696 {
697 DW_SECT_MIN = 1
698 };
699
700 /* Data for one DWO file.
701
702 This includes virtual DWO files (a virtual DWO file is a DWO file as it
703 appears in a DWP file). DWP files don't really have DWO files per se -
704 comdat folding of types "loses" the DWO file they came from, and from
705 a high level view DWP files appear to contain a mass of random types.
706 However, to maintain consistency with the non-DWP case we pretend DWP
707 files contain virtual DWO files, and we assign each TU with one virtual
708 DWO file (generally based on the line and abbrev section offsets -
709 a heuristic that seems to work in practice). */
710
711 struct dwo_file
712 {
713 dwo_file () = default;
714 DISABLE_COPY_AND_ASSIGN (dwo_file);
715
716 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
717 For virtual DWO files the name is constructed from the section offsets
718 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
719 from related CU+TUs. */
720 const char *dwo_name = nullptr;
721
722 /* The DW_AT_comp_dir attribute. */
723 const char *comp_dir = nullptr;
724
725 /* The bfd, when the file is open. Otherwise this is NULL.
726 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
727 gdb_bfd_ref_ptr dbfd;
728
729 /* The sections that make up this DWO file.
730 Remember that for virtual DWO files in DWP V2, these are virtual
731 sections (for lack of a better name). */
732 struct dwo_sections sections {};
733
734 /* The CUs in the file.
735 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
736 an extension to handle LLVM's Link Time Optimization output (where
737 multiple source files may be compiled into a single object/dwo pair). */
738 htab_up cus;
739
740 /* Table of TUs in the file.
741 Each element is a struct dwo_unit. */
742 htab_up tus;
743 };
744
745 /* These sections are what may appear in a DWP file. */
746
747 struct dwp_sections
748 {
749 /* These are used by both DWP version 1 and 2. */
750 struct dwarf2_section_info str;
751 struct dwarf2_section_info cu_index;
752 struct dwarf2_section_info tu_index;
753
754 /* These are only used by DWP version 2 files.
755 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
756 sections are referenced by section number, and are not recorded here.
757 In DWP version 2 there is at most one copy of all these sections, each
758 section being (effectively) comprised of the concatenation of all of the
759 individual sections that exist in the version 1 format.
760 To keep the code simple we treat each of these concatenated pieces as a
761 section itself (a virtual section?). */
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info info;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 struct dwarf2_section_info types;
770 };
771
772 /* These sections are what may appear in a virtual DWO file in DWP version 1.
773 A virtual DWO file is a DWO file as it appears in a DWP file. */
774
775 struct virtual_v1_dwo_sections
776 {
777 struct dwarf2_section_info abbrev;
778 struct dwarf2_section_info line;
779 struct dwarf2_section_info loc;
780 struct dwarf2_section_info macinfo;
781 struct dwarf2_section_info macro;
782 struct dwarf2_section_info str_offsets;
783 /* Each DWP hash table entry records one CU or one TU.
784 That is recorded here, and copied to dwo_unit.section. */
785 struct dwarf2_section_info info_or_types;
786 };
787
788 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
789 In version 2, the sections of the DWO files are concatenated together
790 and stored in one section of that name. Thus each ELF section contains
791 several "virtual" sections. */
792
793 struct virtual_v2_dwo_sections
794 {
795 bfd_size_type abbrev_offset;
796 bfd_size_type abbrev_size;
797
798 bfd_size_type line_offset;
799 bfd_size_type line_size;
800
801 bfd_size_type loc_offset;
802 bfd_size_type loc_size;
803
804 bfd_size_type macinfo_offset;
805 bfd_size_type macinfo_size;
806
807 bfd_size_type macro_offset;
808 bfd_size_type macro_size;
809
810 bfd_size_type str_offsets_offset;
811 bfd_size_type str_offsets_size;
812
813 /* Each DWP hash table entry records one CU or one TU.
814 That is recorded here, and copied to dwo_unit.section. */
815 bfd_size_type info_or_types_offset;
816 bfd_size_type info_or_types_size;
817 };
818
819 /* Contents of DWP hash tables. */
820
821 struct dwp_hash_table
822 {
823 uint32_t version, nr_columns;
824 uint32_t nr_units, nr_slots;
825 const gdb_byte *hash_table, *unit_table;
826 union
827 {
828 struct
829 {
830 const gdb_byte *indices;
831 } v1;
832 struct
833 {
834 /* This is indexed by column number and gives the id of the section
835 in that column. */
836 #define MAX_NR_V2_DWO_SECTIONS \
837 (1 /* .debug_info or .debug_types */ \
838 + 1 /* .debug_abbrev */ \
839 + 1 /* .debug_line */ \
840 + 1 /* .debug_loc */ \
841 + 1 /* .debug_str_offsets */ \
842 + 1 /* .debug_macro or .debug_macinfo */)
843 int section_ids[MAX_NR_V2_DWO_SECTIONS];
844 const gdb_byte *offsets;
845 const gdb_byte *sizes;
846 } v2;
847 } section_pool;
848 };
849
850 /* Data for one DWP file. */
851
852 struct dwp_file
853 {
854 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
855 : name (name_),
856 dbfd (std::move (abfd))
857 {
858 }
859
860 /* Name of the file. */
861 const char *name;
862
863 /* File format version. */
864 int version = 0;
865
866 /* The bfd. */
867 gdb_bfd_ref_ptr dbfd;
868
869 /* Section info for this file. */
870 struct dwp_sections sections {};
871
872 /* Table of CUs in the file. */
873 const struct dwp_hash_table *cus = nullptr;
874
875 /* Table of TUs in the file. */
876 const struct dwp_hash_table *tus = nullptr;
877
878 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
879 htab_up loaded_cus;
880 htab_up loaded_tus;
881
882 /* Table to map ELF section numbers to their sections.
883 This is only needed for the DWP V1 file format. */
884 unsigned int num_sections = 0;
885 asection **elf_sections = nullptr;
886 };
887
888 /* Struct used to pass misc. parameters to read_die_and_children, et
889 al. which are used for both .debug_info and .debug_types dies.
890 All parameters here are unchanging for the life of the call. This
891 struct exists to abstract away the constant parameters of die reading. */
892
893 struct die_reader_specs
894 {
895 /* The bfd of die_section. */
896 bfd* abfd;
897
898 /* The CU of the DIE we are parsing. */
899 struct dwarf2_cu *cu;
900
901 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
902 struct dwo_file *dwo_file;
903
904 /* The section the die comes from.
905 This is either .debug_info or .debug_types, or the .dwo variants. */
906 struct dwarf2_section_info *die_section;
907
908 /* die_section->buffer. */
909 const gdb_byte *buffer;
910
911 /* The end of the buffer. */
912 const gdb_byte *buffer_end;
913
914 /* The abbreviation table to use when reading the DIEs. */
915 struct abbrev_table *abbrev_table;
916 };
917
918 /* A subclass of die_reader_specs that holds storage and has complex
919 constructor and destructor behavior. */
920
921 class cutu_reader : public die_reader_specs
922 {
923 public:
924
925 cutu_reader (dwarf2_per_cu_data *this_cu,
926 dwarf2_per_objfile *per_objfile,
927 struct abbrev_table *abbrev_table,
928 int use_existing_cu,
929 bool skip_partial);
930
931 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
932 dwarf2_per_objfile *per_objfile,
933 struct dwarf2_cu *parent_cu = nullptr,
934 struct dwo_file *dwo_file = nullptr);
935
936 DISABLE_COPY_AND_ASSIGN (cutu_reader);
937
938 const gdb_byte *info_ptr = nullptr;
939 struct die_info *comp_unit_die = nullptr;
940 bool dummy_p = false;
941
942 /* Release the new CU, putting it on the chain. This cannot be done
943 for dummy CUs. */
944 void keep ();
945
946 private:
947 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
948 dwarf2_per_objfile *per_objfile,
949 int use_existing_cu);
950
951 struct dwarf2_per_cu_data *m_this_cu;
952 std::unique_ptr<dwarf2_cu> m_new_cu;
953
954 /* The ordinary abbreviation table. */
955 abbrev_table_up m_abbrev_table_holder;
956
957 /* The DWO abbreviation table. */
958 abbrev_table_up m_dwo_abbrev_table;
959 };
960
961 /* When we construct a partial symbol table entry we only
962 need this much information. */
963 struct partial_die_info : public allocate_on_obstack
964 {
965 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
966
967 /* Disable assign but still keep copy ctor, which is needed
968 load_partial_dies. */
969 partial_die_info& operator=(const partial_die_info& rhs) = delete;
970
971 /* Adjust the partial die before generating a symbol for it. This
972 function may set the is_external flag or change the DIE's
973 name. */
974 void fixup (struct dwarf2_cu *cu);
975
976 /* Read a minimal amount of information into the minimal die
977 structure. */
978 const gdb_byte *read (const struct die_reader_specs *reader,
979 const struct abbrev_info &abbrev,
980 const gdb_byte *info_ptr);
981
982 /* Offset of this DIE. */
983 const sect_offset sect_off;
984
985 /* DWARF-2 tag for this DIE. */
986 const ENUM_BITFIELD(dwarf_tag) tag : 16;
987
988 /* Assorted flags describing the data found in this DIE. */
989 const unsigned int has_children : 1;
990
991 unsigned int is_external : 1;
992 unsigned int is_declaration : 1;
993 unsigned int has_type : 1;
994 unsigned int has_specification : 1;
995 unsigned int has_pc_info : 1;
996 unsigned int may_be_inlined : 1;
997
998 /* This DIE has been marked DW_AT_main_subprogram. */
999 unsigned int main_subprogram : 1;
1000
1001 /* Flag set if the SCOPE field of this structure has been
1002 computed. */
1003 unsigned int scope_set : 1;
1004
1005 /* Flag set if the DIE has a byte_size attribute. */
1006 unsigned int has_byte_size : 1;
1007
1008 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1009 unsigned int has_const_value : 1;
1010
1011 /* Flag set if any of the DIE's children are template arguments. */
1012 unsigned int has_template_arguments : 1;
1013
1014 /* Flag set if fixup has been called on this die. */
1015 unsigned int fixup_called : 1;
1016
1017 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1018 unsigned int is_dwz : 1;
1019
1020 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1021 unsigned int spec_is_dwz : 1;
1022
1023 /* The name of this DIE. Normally the value of DW_AT_name, but
1024 sometimes a default name for unnamed DIEs. */
1025 const char *name = nullptr;
1026
1027 /* The linkage name, if present. */
1028 const char *linkage_name = nullptr;
1029
1030 /* The scope to prepend to our children. This is generally
1031 allocated on the comp_unit_obstack, so will disappear
1032 when this compilation unit leaves the cache. */
1033 const char *scope = nullptr;
1034
1035 /* Some data associated with the partial DIE. The tag determines
1036 which field is live. */
1037 union
1038 {
1039 /* The location description associated with this DIE, if any. */
1040 struct dwarf_block *locdesc;
1041 /* The offset of an import, for DW_TAG_imported_unit. */
1042 sect_offset sect_off;
1043 } d {};
1044
1045 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1046 CORE_ADDR lowpc = 0;
1047 CORE_ADDR highpc = 0;
1048
1049 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1050 DW_AT_sibling, if any. */
1051 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1052 could return DW_AT_sibling values to its caller load_partial_dies. */
1053 const gdb_byte *sibling = nullptr;
1054
1055 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1056 DW_AT_specification (or DW_AT_abstract_origin or
1057 DW_AT_extension). */
1058 sect_offset spec_offset {};
1059
1060 /* Pointers to this DIE's parent, first child, and next sibling,
1061 if any. */
1062 struct partial_die_info *die_parent = nullptr;
1063 struct partial_die_info *die_child = nullptr;
1064 struct partial_die_info *die_sibling = nullptr;
1065
1066 friend struct partial_die_info *
1067 dwarf2_cu::find_partial_die (sect_offset sect_off);
1068
1069 private:
1070 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1071 partial_die_info (sect_offset sect_off)
1072 : partial_die_info (sect_off, DW_TAG_padding, 0)
1073 {
1074 }
1075
1076 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1077 int has_children_)
1078 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1079 {
1080 is_external = 0;
1081 is_declaration = 0;
1082 has_type = 0;
1083 has_specification = 0;
1084 has_pc_info = 0;
1085 may_be_inlined = 0;
1086 main_subprogram = 0;
1087 scope_set = 0;
1088 has_byte_size = 0;
1089 has_const_value = 0;
1090 has_template_arguments = 0;
1091 fixup_called = 0;
1092 is_dwz = 0;
1093 spec_is_dwz = 0;
1094 }
1095 };
1096
1097 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1098 but this would require a corresponding change in unpack_field_as_long
1099 and friends. */
1100 static int bits_per_byte = 8;
1101
1102 struct variant_part_builder;
1103
1104 /* When reading a variant, we track a bit more information about the
1105 field, and store it in an object of this type. */
1106
1107 struct variant_field
1108 {
1109 int first_field = -1;
1110 int last_field = -1;
1111
1112 /* A variant can contain other variant parts. */
1113 std::vector<variant_part_builder> variant_parts;
1114
1115 /* If we see a DW_TAG_variant, then this will be set if this is the
1116 default branch. */
1117 bool default_branch = false;
1118 /* If we see a DW_AT_discr_value, then this will be the discriminant
1119 value. */
1120 ULONGEST discriminant_value = 0;
1121 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1122 data. */
1123 struct dwarf_block *discr_list_data = nullptr;
1124 };
1125
1126 /* This represents a DW_TAG_variant_part. */
1127
1128 struct variant_part_builder
1129 {
1130 /* The offset of the discriminant field. */
1131 sect_offset discriminant_offset {};
1132
1133 /* Variants that are direct children of this variant part. */
1134 std::vector<variant_field> variants;
1135
1136 /* True if we're currently reading a variant. */
1137 bool processing_variant = false;
1138 };
1139
1140 struct nextfield
1141 {
1142 int accessibility = 0;
1143 int virtuality = 0;
1144 /* Variant parts need to find the discriminant, which is a DIE
1145 reference. We track the section offset of each field to make
1146 this link. */
1147 sect_offset offset;
1148 struct field field {};
1149 };
1150
1151 struct fnfieldlist
1152 {
1153 const char *name = nullptr;
1154 std::vector<struct fn_field> fnfields;
1155 };
1156
1157 /* The routines that read and process dies for a C struct or C++ class
1158 pass lists of data member fields and lists of member function fields
1159 in an instance of a field_info structure, as defined below. */
1160 struct field_info
1161 {
1162 /* List of data member and baseclasses fields. */
1163 std::vector<struct nextfield> fields;
1164 std::vector<struct nextfield> baseclasses;
1165
1166 /* Set if the accessibility of one of the fields is not public. */
1167 int non_public_fields = 0;
1168
1169 /* Member function fieldlist array, contains name of possibly overloaded
1170 member function, number of overloaded member functions and a pointer
1171 to the head of the member function field chain. */
1172 std::vector<struct fnfieldlist> fnfieldlists;
1173
1174 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1175 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1176 std::vector<struct decl_field> typedef_field_list;
1177
1178 /* Nested types defined by this class and the number of elements in this
1179 list. */
1180 std::vector<struct decl_field> nested_types_list;
1181
1182 /* If non-null, this is the variant part we are currently
1183 reading. */
1184 variant_part_builder *current_variant_part = nullptr;
1185 /* This holds all the top-level variant parts attached to the type
1186 we're reading. */
1187 std::vector<variant_part_builder> variant_parts;
1188
1189 /* Return the total number of fields (including baseclasses). */
1190 int nfields () const
1191 {
1192 return fields.size () + baseclasses.size ();
1193 }
1194 };
1195
1196 /* Loaded secondary compilation units are kept in memory until they
1197 have not been referenced for the processing of this many
1198 compilation units. Set this to zero to disable caching. Cache
1199 sizes of up to at least twenty will improve startup time for
1200 typical inter-CU-reference binaries, at an obvious memory cost. */
1201 static int dwarf_max_cache_age = 5;
1202 static void
1203 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1204 struct cmd_list_element *c, const char *value)
1205 {
1206 fprintf_filtered (file, _("The upper bound on the age of cached "
1207 "DWARF compilation units is %s.\n"),
1208 value);
1209 }
1210 \f
1211 /* local function prototypes */
1212
1213 static void dwarf2_find_base_address (struct die_info *die,
1214 struct dwarf2_cu *cu);
1215
1216 static dwarf2_psymtab *create_partial_symtab
1217 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1218 const char *name);
1219
1220 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1221 const gdb_byte *info_ptr,
1222 struct die_info *type_unit_die);
1223
1224 static void dwarf2_build_psymtabs_hard
1225 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1226
1227 static void scan_partial_symbols (struct partial_die_info *,
1228 CORE_ADDR *, CORE_ADDR *,
1229 int, struct dwarf2_cu *);
1230
1231 static void add_partial_symbol (struct partial_die_info *,
1232 struct dwarf2_cu *);
1233
1234 static void add_partial_namespace (struct partial_die_info *pdi,
1235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1236 int set_addrmap, struct dwarf2_cu *cu);
1237
1238 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1239 CORE_ADDR *highpc, int set_addrmap,
1240 struct dwarf2_cu *cu);
1241
1242 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1243 struct dwarf2_cu *cu);
1244
1245 static void add_partial_subprogram (struct partial_die_info *pdi,
1246 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1247 int need_pc, struct dwarf2_cu *cu);
1248
1249 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1250
1251 static struct partial_die_info *load_partial_dies
1252 (const struct die_reader_specs *, const gdb_byte *, int);
1253
1254 /* A pair of partial_die_info and compilation unit. */
1255 struct cu_partial_die_info
1256 {
1257 /* The compilation unit of the partial_die_info. */
1258 struct dwarf2_cu *cu;
1259 /* A partial_die_info. */
1260 struct partial_die_info *pdi;
1261
1262 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1263 : cu (cu),
1264 pdi (pdi)
1265 { /* Nothing. */ }
1266
1267 private:
1268 cu_partial_die_info () = delete;
1269 };
1270
1271 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1272 struct dwarf2_cu *);
1273
1274 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1275 struct attribute *, struct attr_abbrev *,
1276 const gdb_byte *, bool *need_reprocess);
1277
1278 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1279 struct attribute *attr);
1280
1281 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1282
1283 static sect_offset read_abbrev_offset
1284 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1285 struct dwarf2_section_info *, sect_offset);
1286
1287 static const char *read_indirect_string
1288 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1289 const struct comp_unit_head *, unsigned int *);
1290
1291 static const char *read_indirect_string_at_offset
1292 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1293
1294 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1295 const gdb_byte *,
1296 unsigned int *);
1297
1298 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1299 ULONGEST str_index);
1300
1301 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1302 ULONGEST str_index);
1303
1304 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1305
1306 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1307 struct dwarf2_cu *);
1308
1309 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1310 struct dwarf2_cu *cu);
1311
1312 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1313
1314 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1315 struct dwarf2_cu *cu);
1316
1317 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1318
1319 static struct die_info *die_specification (struct die_info *die,
1320 struct dwarf2_cu **);
1321
1322 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1323 struct dwarf2_cu *cu);
1324
1325 static void dwarf_decode_lines (struct line_header *, const char *,
1326 struct dwarf2_cu *, dwarf2_psymtab *,
1327 CORE_ADDR, int decode_mapping);
1328
1329 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1330 const char *);
1331
1332 static struct symbol *new_symbol (struct die_info *, struct type *,
1333 struct dwarf2_cu *, struct symbol * = NULL);
1334
1335 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1336 struct dwarf2_cu *);
1337
1338 static void dwarf2_const_value_attr (const struct attribute *attr,
1339 struct type *type,
1340 const char *name,
1341 struct obstack *obstack,
1342 struct dwarf2_cu *cu, LONGEST *value,
1343 const gdb_byte **bytes,
1344 struct dwarf2_locexpr_baton **baton);
1345
1346 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1347
1348 static int need_gnat_info (struct dwarf2_cu *);
1349
1350 static struct type *die_descriptive_type (struct die_info *,
1351 struct dwarf2_cu *);
1352
1353 static void set_descriptive_type (struct type *, struct die_info *,
1354 struct dwarf2_cu *);
1355
1356 static struct type *die_containing_type (struct die_info *,
1357 struct dwarf2_cu *);
1358
1359 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1360 struct dwarf2_cu *);
1361
1362 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1363
1364 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1365
1366 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1367
1368 static char *typename_concat (struct obstack *obs, const char *prefix,
1369 const char *suffix, int physname,
1370 struct dwarf2_cu *cu);
1371
1372 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1373
1374 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1375
1376 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1377
1378 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1379
1380 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1381
1382 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1383
1384 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1385 struct dwarf2_cu *, dwarf2_psymtab *);
1386
1387 /* Return the .debug_loclists section to use for cu. */
1388 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1389
1390 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1391 values. Keep the items ordered with increasing constraints compliance. */
1392 enum pc_bounds_kind
1393 {
1394 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1395 PC_BOUNDS_NOT_PRESENT,
1396
1397 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1398 were present but they do not form a valid range of PC addresses. */
1399 PC_BOUNDS_INVALID,
1400
1401 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1402 PC_BOUNDS_RANGES,
1403
1404 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1405 PC_BOUNDS_HIGH_LOW,
1406 };
1407
1408 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1409 CORE_ADDR *, CORE_ADDR *,
1410 struct dwarf2_cu *,
1411 dwarf2_psymtab *);
1412
1413 static void get_scope_pc_bounds (struct die_info *,
1414 CORE_ADDR *, CORE_ADDR *,
1415 struct dwarf2_cu *);
1416
1417 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1418 CORE_ADDR, struct dwarf2_cu *);
1419
1420 static void dwarf2_add_field (struct field_info *, struct die_info *,
1421 struct dwarf2_cu *);
1422
1423 static void dwarf2_attach_fields_to_type (struct field_info *,
1424 struct type *, struct dwarf2_cu *);
1425
1426 static void dwarf2_add_member_fn (struct field_info *,
1427 struct die_info *, struct type *,
1428 struct dwarf2_cu *);
1429
1430 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1431 struct type *,
1432 struct dwarf2_cu *);
1433
1434 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1435
1436 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1437
1438 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1439
1440 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1441
1442 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1443
1444 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1445
1446 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1447
1448 static struct type *read_module_type (struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
1451 static const char *namespace_name (struct die_info *die,
1452 int *is_anonymous, struct dwarf2_cu *);
1453
1454 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1455
1456 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1457 bool * = nullptr);
1458
1459 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1460 struct dwarf2_cu *);
1461
1462 static struct die_info *read_die_and_siblings_1
1463 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1464 struct die_info *);
1465
1466 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1467 const gdb_byte *info_ptr,
1468 const gdb_byte **new_info_ptr,
1469 struct die_info *parent);
1470
1471 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1472 struct die_info **, const gdb_byte *,
1473 int);
1474
1475 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1476 struct die_info **, const gdb_byte *);
1477
1478 static void process_die (struct die_info *, struct dwarf2_cu *);
1479
1480 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1481 struct objfile *);
1482
1483 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1484
1485 static const char *dwarf2_full_name (const char *name,
1486 struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
1489 static const char *dwarf2_physname (const char *name, struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
1492 static struct die_info *dwarf2_extension (struct die_info *die,
1493 struct dwarf2_cu **);
1494
1495 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1496
1497 static void dump_die_for_error (struct die_info *);
1498
1499 static void dump_die_1 (struct ui_file *, int level, int max_level,
1500 struct die_info *);
1501
1502 /*static*/ void dump_die (struct die_info *, int max_level);
1503
1504 static void store_in_ref_table (struct die_info *,
1505 struct dwarf2_cu *);
1506
1507 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1508 const struct attribute *,
1509 struct dwarf2_cu **);
1510
1511 static struct die_info *follow_die_ref (struct die_info *,
1512 const struct attribute *,
1513 struct dwarf2_cu **);
1514
1515 static struct die_info *follow_die_sig (struct die_info *,
1516 const struct attribute *,
1517 struct dwarf2_cu **);
1518
1519 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1520 struct dwarf2_cu *);
1521
1522 static struct type *get_DW_AT_signature_type (struct die_info *,
1523 const struct attribute *,
1524 struct dwarf2_cu *);
1525
1526 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1527 dwarf2_per_objfile *per_objfile);
1528
1529 static void read_signatured_type (signatured_type *sig_type,
1530 dwarf2_per_objfile *per_objfile);
1531
1532 static int attr_to_dynamic_prop (const struct attribute *attr,
1533 struct die_info *die, struct dwarf2_cu *cu,
1534 struct dynamic_prop *prop, struct type *type);
1535
1536 /* memory allocation interface */
1537
1538 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1539
1540 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1541
1542 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1543
1544 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1545 struct dwarf2_loclist_baton *baton,
1546 const struct attribute *attr);
1547
1548 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1549 struct symbol *sym,
1550 struct dwarf2_cu *cu,
1551 int is_block);
1552
1553 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1554 const gdb_byte *info_ptr,
1555 struct abbrev_info *abbrev);
1556
1557 static hashval_t partial_die_hash (const void *item);
1558
1559 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1560
1561 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1562 (sect_offset sect_off, unsigned int offset_in_dwz,
1563 struct dwarf2_per_objfile *dwarf2_per_objfile);
1564
1565 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1566 struct die_info *comp_unit_die,
1567 enum language pretend_language);
1568
1569 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1570
1571 static void free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
1572 dwarf2_per_objfile *per_objfile);
1573
1574 static struct type *set_die_type (struct die_info *, struct type *,
1575 struct dwarf2_cu *);
1576
1577 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1578
1579 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1580
1581 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1582 dwarf2_per_objfile *per_objfile,
1583 bool skip_partial,
1584 enum language pretend_language);
1585
1586 static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1587 dwarf2_per_objfile *per_objfile,
1588 enum language pretend_language);
1589
1590 static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile,
1592 enum language pretend_language);
1593
1594 static void dwarf2_add_dependence (struct dwarf2_cu *,
1595 struct dwarf2_per_cu_data *);
1596
1597 static void dwarf2_mark (struct dwarf2_cu *);
1598
1599 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1600
1601 static struct type *get_die_type_at_offset (sect_offset,
1602 struct dwarf2_per_cu_data *);
1603
1604 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1605
1606 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1607 dwarf2_per_objfile *per_objfile,
1608 enum language pretend_language);
1609
1610 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1611
1612 /* Class, the destructor of which frees all allocated queue entries. This
1613 will only have work to do if an error was thrown while processing the
1614 dwarf. If no error was thrown then the queue entries should have all
1615 been processed, and freed, as we went along. */
1616
1617 class dwarf2_queue_guard
1618 {
1619 public:
1620 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1621 : m_per_objfile (per_objfile)
1622 {
1623 }
1624
1625 /* Free any entries remaining on the queue. There should only be
1626 entries left if we hit an error while processing the dwarf. */
1627 ~dwarf2_queue_guard ()
1628 {
1629 /* Ensure that no memory is allocated by the queue. */
1630 std::queue<dwarf2_queue_item> empty;
1631 std::swap (m_per_objfile->per_bfd->queue, empty);
1632 }
1633
1634 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1635
1636 private:
1637 dwarf2_per_objfile *m_per_objfile;
1638 };
1639
1640 dwarf2_queue_item::~dwarf2_queue_item ()
1641 {
1642 /* Anything still marked queued is likely to be in an
1643 inconsistent state, so discard it. */
1644 if (per_cu->queued)
1645 {
1646 if (per_cu->cu != NULL)
1647 free_one_cached_comp_unit (per_cu, per_objfile);
1648 per_cu->queued = 0;
1649 }
1650 }
1651
1652 /* The return type of find_file_and_directory. Note, the enclosed
1653 string pointers are only valid while this object is valid. */
1654
1655 struct file_and_directory
1656 {
1657 /* The filename. This is never NULL. */
1658 const char *name;
1659
1660 /* The compilation directory. NULL if not known. If we needed to
1661 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1662 points directly to the DW_AT_comp_dir string attribute owned by
1663 the obstack that owns the DIE. */
1664 const char *comp_dir;
1665
1666 /* If we needed to build a new string for comp_dir, this is what
1667 owns the storage. */
1668 std::string comp_dir_storage;
1669 };
1670
1671 static file_and_directory find_file_and_directory (struct die_info *die,
1672 struct dwarf2_cu *cu);
1673
1674 static htab_up allocate_signatured_type_table ();
1675
1676 static htab_up allocate_dwo_unit_table ();
1677
1678 static struct dwo_unit *lookup_dwo_unit_in_dwp
1679 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1680 struct dwp_file *dwp_file, const char *comp_dir,
1681 ULONGEST signature, int is_debug_types);
1682
1683 static struct dwp_file *get_dwp_file
1684 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1685
1686 static struct dwo_unit *lookup_dwo_comp_unit
1687 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1688 ULONGEST signature);
1689
1690 static struct dwo_unit *lookup_dwo_type_unit
1691 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1692
1693 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1694
1695 /* A unique pointer to a dwo_file. */
1696
1697 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1698
1699 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1700
1701 static void check_producer (struct dwarf2_cu *cu);
1702
1703 static void free_line_header_voidp (void *arg);
1704 \f
1705 /* Various complaints about symbol reading that don't abort the process. */
1706
1707 static void
1708 dwarf2_debug_line_missing_file_complaint (void)
1709 {
1710 complaint (_(".debug_line section has line data without a file"));
1711 }
1712
1713 static void
1714 dwarf2_debug_line_missing_end_sequence_complaint (void)
1715 {
1716 complaint (_(".debug_line section has line "
1717 "program sequence without an end"));
1718 }
1719
1720 static void
1721 dwarf2_complex_location_expr_complaint (void)
1722 {
1723 complaint (_("location expression too complex"));
1724 }
1725
1726 static void
1727 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1728 int arg3)
1729 {
1730 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1731 arg1, arg2, arg3);
1732 }
1733
1734 static void
1735 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1736 {
1737 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1738 arg1, arg2);
1739 }
1740
1741 /* Hash function for line_header_hash. */
1742
1743 static hashval_t
1744 line_header_hash (const struct line_header *ofs)
1745 {
1746 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1747 }
1748
1749 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1750
1751 static hashval_t
1752 line_header_hash_voidp (const void *item)
1753 {
1754 const struct line_header *ofs = (const struct line_header *) item;
1755
1756 return line_header_hash (ofs);
1757 }
1758
1759 /* Equality function for line_header_hash. */
1760
1761 static int
1762 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1763 {
1764 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1765 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1766
1767 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1768 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1769 }
1770
1771 \f
1772
1773 /* See declaration. */
1774
1775 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1776 bool can_copy_)
1777 : obfd (obfd),
1778 can_copy (can_copy_)
1779 {
1780 if (names == NULL)
1781 names = &dwarf2_elf_names;
1782
1783 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1784 locate_sections (obfd, sec, *names);
1785 }
1786
1787 dwarf2_per_bfd::~dwarf2_per_bfd ()
1788 {
1789 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1790 free_cached_comp_units ();
1791
1792 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1793 per_cu->imported_symtabs_free ();
1794
1795 for (signatured_type *sig_type : all_type_units)
1796 sig_type->per_cu.imported_symtabs_free ();
1797
1798 /* Everything else should be on this->obstack. */
1799 }
1800
1801 /* See declaration. */
1802
1803 void
1804 dwarf2_per_bfd::free_cached_comp_units ()
1805 {
1806 dwarf2_per_cu_data *per_cu = read_in_chain;
1807 dwarf2_per_cu_data **last_chain = &read_in_chain;
1808 while (per_cu != NULL)
1809 {
1810 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1811
1812 delete per_cu->cu;
1813 *last_chain = next_cu;
1814 per_cu = next_cu;
1815 }
1816 }
1817
1818 /* A helper class that calls free_cached_comp_units on
1819 destruction. */
1820
1821 class free_cached_comp_units
1822 {
1823 public:
1824
1825 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1826 : m_per_objfile (per_objfile)
1827 {
1828 }
1829
1830 ~free_cached_comp_units ()
1831 {
1832 m_per_objfile->per_bfd->free_cached_comp_units ();
1833 }
1834
1835 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1836
1837 private:
1838
1839 dwarf2_per_objfile *m_per_objfile;
1840 };
1841
1842 /* See read.h. */
1843
1844 bool
1845 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1846 {
1847 gdb_assert (per_cu->index < this->m_symtabs.size ());
1848
1849 return this->m_symtabs[per_cu->index] != nullptr;
1850 }
1851
1852 /* See read.h. */
1853
1854 compunit_symtab *
1855 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1856 {
1857 gdb_assert (per_cu->index < this->m_symtabs.size ());
1858
1859 return this->m_symtabs[per_cu->index];
1860 }
1861
1862 /* See read.h. */
1863
1864 void
1865 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1866 compunit_symtab *symtab)
1867 {
1868 gdb_assert (per_cu->index < this->m_symtabs.size ());
1869 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1870
1871 this->m_symtabs[per_cu->index] = symtab;
1872 }
1873
1874 /* Try to locate the sections we need for DWARF 2 debugging
1875 information and return true if we have enough to do something.
1876 NAMES points to the dwarf2 section names, or is NULL if the standard
1877 ELF names are used. CAN_COPY is true for formats where symbol
1878 interposition is possible and so symbol values must follow copy
1879 relocation rules. */
1880
1881 int
1882 dwarf2_has_info (struct objfile *objfile,
1883 const struct dwarf2_debug_sections *names,
1884 bool can_copy)
1885 {
1886 if (objfile->flags & OBJF_READNEVER)
1887 return 0;
1888
1889 struct dwarf2_per_objfile *dwarf2_per_objfile
1890 = get_dwarf2_per_objfile (objfile);
1891
1892 if (dwarf2_per_objfile == NULL)
1893 {
1894 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1895 sharing yet). */
1896 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1897
1898 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1899 }
1900
1901 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1902 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1903 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1904 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1905 }
1906
1907 /* When loading sections, we look either for uncompressed section or for
1908 compressed section names. */
1909
1910 static int
1911 section_is_p (const char *section_name,
1912 const struct dwarf2_section_names *names)
1913 {
1914 if (names->normal != NULL
1915 && strcmp (section_name, names->normal) == 0)
1916 return 1;
1917 if (names->compressed != NULL
1918 && strcmp (section_name, names->compressed) == 0)
1919 return 1;
1920 return 0;
1921 }
1922
1923 /* See declaration. */
1924
1925 void
1926 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1927 const dwarf2_debug_sections &names)
1928 {
1929 flagword aflag = bfd_section_flags (sectp);
1930
1931 if ((aflag & SEC_HAS_CONTENTS) == 0)
1932 {
1933 }
1934 else if (elf_section_data (sectp)->this_hdr.sh_size
1935 > bfd_get_file_size (abfd))
1936 {
1937 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1938 warning (_("Discarding section %s which has a section size (%s"
1939 ") larger than the file size [in module %s]"),
1940 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1941 bfd_get_filename (abfd));
1942 }
1943 else if (section_is_p (sectp->name, &names.info))
1944 {
1945 this->info.s.section = sectp;
1946 this->info.size = bfd_section_size (sectp);
1947 }
1948 else if (section_is_p (sectp->name, &names.abbrev))
1949 {
1950 this->abbrev.s.section = sectp;
1951 this->abbrev.size = bfd_section_size (sectp);
1952 }
1953 else if (section_is_p (sectp->name, &names.line))
1954 {
1955 this->line.s.section = sectp;
1956 this->line.size = bfd_section_size (sectp);
1957 }
1958 else if (section_is_p (sectp->name, &names.loc))
1959 {
1960 this->loc.s.section = sectp;
1961 this->loc.size = bfd_section_size (sectp);
1962 }
1963 else if (section_is_p (sectp->name, &names.loclists))
1964 {
1965 this->loclists.s.section = sectp;
1966 this->loclists.size = bfd_section_size (sectp);
1967 }
1968 else if (section_is_p (sectp->name, &names.macinfo))
1969 {
1970 this->macinfo.s.section = sectp;
1971 this->macinfo.size = bfd_section_size (sectp);
1972 }
1973 else if (section_is_p (sectp->name, &names.macro))
1974 {
1975 this->macro.s.section = sectp;
1976 this->macro.size = bfd_section_size (sectp);
1977 }
1978 else if (section_is_p (sectp->name, &names.str))
1979 {
1980 this->str.s.section = sectp;
1981 this->str.size = bfd_section_size (sectp);
1982 }
1983 else if (section_is_p (sectp->name, &names.str_offsets))
1984 {
1985 this->str_offsets.s.section = sectp;
1986 this->str_offsets.size = bfd_section_size (sectp);
1987 }
1988 else if (section_is_p (sectp->name, &names.line_str))
1989 {
1990 this->line_str.s.section = sectp;
1991 this->line_str.size = bfd_section_size (sectp);
1992 }
1993 else if (section_is_p (sectp->name, &names.addr))
1994 {
1995 this->addr.s.section = sectp;
1996 this->addr.size = bfd_section_size (sectp);
1997 }
1998 else if (section_is_p (sectp->name, &names.frame))
1999 {
2000 this->frame.s.section = sectp;
2001 this->frame.size = bfd_section_size (sectp);
2002 }
2003 else if (section_is_p (sectp->name, &names.eh_frame))
2004 {
2005 this->eh_frame.s.section = sectp;
2006 this->eh_frame.size = bfd_section_size (sectp);
2007 }
2008 else if (section_is_p (sectp->name, &names.ranges))
2009 {
2010 this->ranges.s.section = sectp;
2011 this->ranges.size = bfd_section_size (sectp);
2012 }
2013 else if (section_is_p (sectp->name, &names.rnglists))
2014 {
2015 this->rnglists.s.section = sectp;
2016 this->rnglists.size = bfd_section_size (sectp);
2017 }
2018 else if (section_is_p (sectp->name, &names.types))
2019 {
2020 struct dwarf2_section_info type_section;
2021
2022 memset (&type_section, 0, sizeof (type_section));
2023 type_section.s.section = sectp;
2024 type_section.size = bfd_section_size (sectp);
2025
2026 this->types.push_back (type_section);
2027 }
2028 else if (section_is_p (sectp->name, &names.gdb_index))
2029 {
2030 this->gdb_index.s.section = sectp;
2031 this->gdb_index.size = bfd_section_size (sectp);
2032 }
2033 else if (section_is_p (sectp->name, &names.debug_names))
2034 {
2035 this->debug_names.s.section = sectp;
2036 this->debug_names.size = bfd_section_size (sectp);
2037 }
2038 else if (section_is_p (sectp->name, &names.debug_aranges))
2039 {
2040 this->debug_aranges.s.section = sectp;
2041 this->debug_aranges.size = bfd_section_size (sectp);
2042 }
2043
2044 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2045 && bfd_section_vma (sectp) == 0)
2046 this->has_section_at_zero = true;
2047 }
2048
2049 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2050 SECTION_NAME. */
2051
2052 void
2053 dwarf2_get_section_info (struct objfile *objfile,
2054 enum dwarf2_section_enum sect,
2055 asection **sectp, const gdb_byte **bufp,
2056 bfd_size_type *sizep)
2057 {
2058 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2059 struct dwarf2_section_info *info;
2060
2061 /* We may see an objfile without any DWARF, in which case we just
2062 return nothing. */
2063 if (data == NULL)
2064 {
2065 *sectp = NULL;
2066 *bufp = NULL;
2067 *sizep = 0;
2068 return;
2069 }
2070 switch (sect)
2071 {
2072 case DWARF2_DEBUG_FRAME:
2073 info = &data->per_bfd->frame;
2074 break;
2075 case DWARF2_EH_FRAME:
2076 info = &data->per_bfd->eh_frame;
2077 break;
2078 default:
2079 gdb_assert_not_reached ("unexpected section");
2080 }
2081
2082 info->read (objfile);
2083
2084 *sectp = info->get_bfd_section ();
2085 *bufp = info->buffer;
2086 *sizep = info->size;
2087 }
2088
2089 /* A helper function to find the sections for a .dwz file. */
2090
2091 static void
2092 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2093 {
2094 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2095
2096 /* Note that we only support the standard ELF names, because .dwz
2097 is ELF-only (at the time of writing). */
2098 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2099 {
2100 dwz_file->abbrev.s.section = sectp;
2101 dwz_file->abbrev.size = bfd_section_size (sectp);
2102 }
2103 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2104 {
2105 dwz_file->info.s.section = sectp;
2106 dwz_file->info.size = bfd_section_size (sectp);
2107 }
2108 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2109 {
2110 dwz_file->str.s.section = sectp;
2111 dwz_file->str.size = bfd_section_size (sectp);
2112 }
2113 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2114 {
2115 dwz_file->line.s.section = sectp;
2116 dwz_file->line.size = bfd_section_size (sectp);
2117 }
2118 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2119 {
2120 dwz_file->macro.s.section = sectp;
2121 dwz_file->macro.size = bfd_section_size (sectp);
2122 }
2123 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2124 {
2125 dwz_file->gdb_index.s.section = sectp;
2126 dwz_file->gdb_index.size = bfd_section_size (sectp);
2127 }
2128 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2129 {
2130 dwz_file->debug_names.s.section = sectp;
2131 dwz_file->debug_names.size = bfd_section_size (sectp);
2132 }
2133 }
2134
2135 /* See dwarf2read.h. */
2136
2137 struct dwz_file *
2138 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2139 {
2140 const char *filename;
2141 bfd_size_type buildid_len_arg;
2142 size_t buildid_len;
2143 bfd_byte *buildid;
2144
2145 if (per_bfd->dwz_file != NULL)
2146 return per_bfd->dwz_file.get ();
2147
2148 bfd_set_error (bfd_error_no_error);
2149 gdb::unique_xmalloc_ptr<char> data
2150 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2151 &buildid_len_arg, &buildid));
2152 if (data == NULL)
2153 {
2154 if (bfd_get_error () == bfd_error_no_error)
2155 return NULL;
2156 error (_("could not read '.gnu_debugaltlink' section: %s"),
2157 bfd_errmsg (bfd_get_error ()));
2158 }
2159
2160 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2161
2162 buildid_len = (size_t) buildid_len_arg;
2163
2164 filename = data.get ();
2165
2166 std::string abs_storage;
2167 if (!IS_ABSOLUTE_PATH (filename))
2168 {
2169 gdb::unique_xmalloc_ptr<char> abs
2170 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2171
2172 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2173 filename = abs_storage.c_str ();
2174 }
2175
2176 /* First try the file name given in the section. If that doesn't
2177 work, try to use the build-id instead. */
2178 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2179 if (dwz_bfd != NULL)
2180 {
2181 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2182 dwz_bfd.reset (nullptr);
2183 }
2184
2185 if (dwz_bfd == NULL)
2186 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2187
2188 if (dwz_bfd == nullptr)
2189 {
2190 gdb::unique_xmalloc_ptr<char> alt_filename;
2191 const char *origname = bfd_get_filename (per_bfd->obfd);
2192
2193 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2194 buildid_len,
2195 origname,
2196 &alt_filename));
2197
2198 if (fd.get () >= 0)
2199 {
2200 /* File successfully retrieved from server. */
2201 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2202
2203 if (dwz_bfd == nullptr)
2204 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2205 alt_filename.get ());
2206 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2207 dwz_bfd.reset (nullptr);
2208 }
2209 }
2210
2211 if (dwz_bfd == NULL)
2212 error (_("could not find '.gnu_debugaltlink' file for %s"),
2213 bfd_get_filename (per_bfd->obfd));
2214
2215 std::unique_ptr<struct dwz_file> result
2216 (new struct dwz_file (std::move (dwz_bfd)));
2217
2218 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2219 result.get ());
2220
2221 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2222 per_bfd->dwz_file = std::move (result);
2223 return per_bfd->dwz_file.get ();
2224 }
2225 \f
2226 /* DWARF quick_symbols_functions support. */
2227
2228 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2229 unique line tables, so we maintain a separate table of all .debug_line
2230 derived entries to support the sharing.
2231 All the quick functions need is the list of file names. We discard the
2232 line_header when we're done and don't need to record it here. */
2233 struct quick_file_names
2234 {
2235 /* The data used to construct the hash key. */
2236 struct stmt_list_hash hash;
2237
2238 /* The number of entries in file_names, real_names. */
2239 unsigned int num_file_names;
2240
2241 /* The file names from the line table, after being run through
2242 file_full_name. */
2243 const char **file_names;
2244
2245 /* The file names from the line table after being run through
2246 gdb_realpath. These are computed lazily. */
2247 const char **real_names;
2248 };
2249
2250 /* When using the index (and thus not using psymtabs), each CU has an
2251 object of this type. This is used to hold information needed by
2252 the various "quick" methods. */
2253 struct dwarf2_per_cu_quick_data
2254 {
2255 /* The file table. This can be NULL if there was no file table
2256 or it's currently not read in.
2257 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2258 struct quick_file_names *file_names;
2259
2260 /* A temporary mark bit used when iterating over all CUs in
2261 expand_symtabs_matching. */
2262 unsigned int mark : 1;
2263
2264 /* True if we've tried to read the file table and found there isn't one.
2265 There will be no point in trying to read it again next time. */
2266 unsigned int no_file_data : 1;
2267 };
2268
2269 /* Utility hash function for a stmt_list_hash. */
2270
2271 static hashval_t
2272 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2273 {
2274 hashval_t v = 0;
2275
2276 if (stmt_list_hash->dwo_unit != NULL)
2277 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2278 v += to_underlying (stmt_list_hash->line_sect_off);
2279 return v;
2280 }
2281
2282 /* Utility equality function for a stmt_list_hash. */
2283
2284 static int
2285 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2286 const struct stmt_list_hash *rhs)
2287 {
2288 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2289 return 0;
2290 if (lhs->dwo_unit != NULL
2291 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2292 return 0;
2293
2294 return lhs->line_sect_off == rhs->line_sect_off;
2295 }
2296
2297 /* Hash function for a quick_file_names. */
2298
2299 static hashval_t
2300 hash_file_name_entry (const void *e)
2301 {
2302 const struct quick_file_names *file_data
2303 = (const struct quick_file_names *) e;
2304
2305 return hash_stmt_list_entry (&file_data->hash);
2306 }
2307
2308 /* Equality function for a quick_file_names. */
2309
2310 static int
2311 eq_file_name_entry (const void *a, const void *b)
2312 {
2313 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2314 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2315
2316 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2317 }
2318
2319 /* Delete function for a quick_file_names. */
2320
2321 static void
2322 delete_file_name_entry (void *e)
2323 {
2324 struct quick_file_names *file_data = (struct quick_file_names *) e;
2325 int i;
2326
2327 for (i = 0; i < file_data->num_file_names; ++i)
2328 {
2329 xfree ((void*) file_data->file_names[i]);
2330 if (file_data->real_names)
2331 xfree ((void*) file_data->real_names[i]);
2332 }
2333
2334 /* The space for the struct itself lives on the obstack, so we don't
2335 free it here. */
2336 }
2337
2338 /* Create a quick_file_names hash table. */
2339
2340 static htab_up
2341 create_quick_file_names_table (unsigned int nr_initial_entries)
2342 {
2343 return htab_up (htab_create_alloc (nr_initial_entries,
2344 hash_file_name_entry, eq_file_name_entry,
2345 delete_file_name_entry, xcalloc, xfree));
2346 }
2347
2348 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2349 function is unrelated to symtabs, symtab would have to be created afterwards.
2350 You should call age_cached_comp_units after processing the CU. */
2351
2352 static void
2353 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2354 bool skip_partial)
2355 {
2356 if (per_cu->is_debug_types)
2357 load_full_type_unit (per_cu, per_objfile);
2358 else
2359 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2360
2361 if (per_cu->cu == NULL)
2362 return; /* Dummy CU. */
2363
2364 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2365 }
2366
2367 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2368
2369 static void
2370 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2371 dwarf2_per_objfile *dwarf2_per_objfile,
2372 bool skip_partial)
2373 {
2374 /* Skip type_unit_groups, reading the type units they contain
2375 is handled elsewhere. */
2376 if (per_cu->type_unit_group_p ())
2377 return;
2378
2379 /* The destructor of dwarf2_queue_guard frees any entries left on
2380 the queue. After this point we're guaranteed to leave this function
2381 with the dwarf queue empty. */
2382 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2383
2384 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2385 {
2386 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
2387 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2388
2389 /* If we just loaded a CU from a DWO, and we're working with an index
2390 that may badly handle TUs, load all the TUs in that DWO as well.
2391 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2392 if (!per_cu->is_debug_types
2393 && per_cu->cu != NULL
2394 && per_cu->cu->dwo_unit != NULL
2395 && dwarf2_per_objfile->per_bfd->index_table != NULL
2396 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2397 /* DWP files aren't supported yet. */
2398 && get_dwp_file (dwarf2_per_objfile) == NULL)
2399 queue_and_load_all_dwo_tus (per_cu);
2400 }
2401
2402 process_queue (dwarf2_per_objfile);
2403
2404 /* Age the cache, releasing compilation units that have not
2405 been used recently. */
2406 age_cached_comp_units (dwarf2_per_objfile);
2407 }
2408
2409 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2410 the per-objfile for which this symtab is instantiated.
2411
2412 Returns the resulting symbol table. */
2413
2414 static struct compunit_symtab *
2415 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2416 dwarf2_per_objfile *dwarf2_per_objfile,
2417 bool skip_partial)
2418 {
2419 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2420
2421 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2422 {
2423 free_cached_comp_units freer (dwarf2_per_objfile);
2424 scoped_restore decrementer = increment_reading_symtab ();
2425 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2426 process_cu_includes (dwarf2_per_objfile);
2427 }
2428
2429 return dwarf2_per_objfile->get_symtab (per_cu);
2430 }
2431
2432 /* See declaration. */
2433
2434 dwarf2_per_cu_data *
2435 dwarf2_per_bfd::get_cutu (int index)
2436 {
2437 if (index >= this->all_comp_units.size ())
2438 {
2439 index -= this->all_comp_units.size ();
2440 gdb_assert (index < this->all_type_units.size ());
2441 return &this->all_type_units[index]->per_cu;
2442 }
2443
2444 return this->all_comp_units[index];
2445 }
2446
2447 /* See declaration. */
2448
2449 dwarf2_per_cu_data *
2450 dwarf2_per_bfd::get_cu (int index)
2451 {
2452 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2453
2454 return this->all_comp_units[index];
2455 }
2456
2457 /* See declaration. */
2458
2459 signatured_type *
2460 dwarf2_per_bfd::get_tu (int index)
2461 {
2462 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2463
2464 return this->all_type_units[index];
2465 }
2466
2467 /* See read.h. */
2468
2469 dwarf2_per_cu_data *
2470 dwarf2_per_bfd::allocate_per_cu ()
2471 {
2472 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2473 result->per_bfd = this;
2474 result->index = m_num_psymtabs++;
2475 return result;
2476 }
2477
2478 /* See read.h. */
2479
2480 signatured_type *
2481 dwarf2_per_bfd::allocate_signatured_type ()
2482 {
2483 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2484 result->per_cu.per_bfd = this;
2485 result->per_cu.index = m_num_psymtabs++;
2486 return result;
2487 }
2488
2489 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2490 obstack, and constructed with the specified field values. */
2491
2492 static dwarf2_per_cu_data *
2493 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2494 struct dwarf2_section_info *section,
2495 int is_dwz,
2496 sect_offset sect_off, ULONGEST length)
2497 {
2498 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2499 the_cu->sect_off = sect_off;
2500 the_cu->length = length;
2501 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2502 the_cu->section = section;
2503 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2504 struct dwarf2_per_cu_quick_data);
2505 the_cu->is_dwz = is_dwz;
2506 return the_cu;
2507 }
2508
2509 /* A helper for create_cus_from_index that handles a given list of
2510 CUs. */
2511
2512 static void
2513 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2514 const gdb_byte *cu_list, offset_type n_elements,
2515 struct dwarf2_section_info *section,
2516 int is_dwz)
2517 {
2518 for (offset_type i = 0; i < n_elements; i += 2)
2519 {
2520 gdb_static_assert (sizeof (ULONGEST) >= 8);
2521
2522 sect_offset sect_off
2523 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2524 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2525 cu_list += 2 * 8;
2526
2527 dwarf2_per_cu_data *per_cu
2528 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2529 sect_off, length);
2530 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2531 }
2532 }
2533
2534 /* Read the CU list from the mapped index, and use it to create all
2535 the CU objects for this objfile. */
2536
2537 static void
2538 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2539 const gdb_byte *cu_list, offset_type cu_list_elements,
2540 const gdb_byte *dwz_list, offset_type dwz_elements)
2541 {
2542 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2543 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2544 ((cu_list_elements + dwz_elements) / 2);
2545
2546 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2547 &dwarf2_per_objfile->per_bfd->info, 0);
2548
2549 if (dwz_elements == 0)
2550 return;
2551
2552 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2553 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2554 &dwz->info, 1);
2555 }
2556
2557 /* Create the signatured type hash table from the index. */
2558
2559 static void
2560 create_signatured_type_table_from_index
2561 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2562 struct dwarf2_section_info *section,
2563 const gdb_byte *bytes,
2564 offset_type elements)
2565 {
2566 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2567 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2568
2569 htab_up sig_types_hash = allocate_signatured_type_table ();
2570
2571 for (offset_type i = 0; i < elements; i += 3)
2572 {
2573 struct signatured_type *sig_type;
2574 ULONGEST signature;
2575 void **slot;
2576 cu_offset type_offset_in_tu;
2577
2578 gdb_static_assert (sizeof (ULONGEST) >= 8);
2579 sect_offset sect_off
2580 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2581 type_offset_in_tu
2582 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2583 BFD_ENDIAN_LITTLE);
2584 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2585 bytes += 3 * 8;
2586
2587 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2588 sig_type->signature = signature;
2589 sig_type->type_offset_in_tu = type_offset_in_tu;
2590 sig_type->per_cu.is_debug_types = 1;
2591 sig_type->per_cu.section = section;
2592 sig_type->per_cu.sect_off = sect_off;
2593 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2594 sig_type->per_cu.v.quick
2595 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2596 struct dwarf2_per_cu_quick_data);
2597
2598 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2599 *slot = sig_type;
2600
2601 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2602 }
2603
2604 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2605 }
2606
2607 /* Create the signatured type hash table from .debug_names. */
2608
2609 static void
2610 create_signatured_type_table_from_debug_names
2611 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2612 const mapped_debug_names &map,
2613 struct dwarf2_section_info *section,
2614 struct dwarf2_section_info *abbrev_section)
2615 {
2616 struct objfile *objfile = dwarf2_per_objfile->objfile;
2617
2618 section->read (objfile);
2619 abbrev_section->read (objfile);
2620
2621 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2622 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2623
2624 htab_up sig_types_hash = allocate_signatured_type_table ();
2625
2626 for (uint32_t i = 0; i < map.tu_count; ++i)
2627 {
2628 struct signatured_type *sig_type;
2629 void **slot;
2630
2631 sect_offset sect_off
2632 = (sect_offset) (extract_unsigned_integer
2633 (map.tu_table_reordered + i * map.offset_size,
2634 map.offset_size,
2635 map.dwarf5_byte_order));
2636
2637 comp_unit_head cu_header;
2638 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2639 abbrev_section,
2640 section->buffer + to_underlying (sect_off),
2641 rcuh_kind::TYPE);
2642
2643 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2644 sig_type->signature = cu_header.signature;
2645 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2646 sig_type->per_cu.is_debug_types = 1;
2647 sig_type->per_cu.section = section;
2648 sig_type->per_cu.sect_off = sect_off;
2649 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2650 sig_type->per_cu.v.quick
2651 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2652 struct dwarf2_per_cu_quick_data);
2653
2654 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2655 *slot = sig_type;
2656
2657 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2658 }
2659
2660 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2661 }
2662
2663 /* Read the address map data from the mapped index, and use it to
2664 populate the objfile's psymtabs_addrmap. */
2665
2666 static void
2667 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2668 struct mapped_index *index)
2669 {
2670 struct objfile *objfile = dwarf2_per_objfile->objfile;
2671 struct gdbarch *gdbarch = objfile->arch ();
2672 const gdb_byte *iter, *end;
2673 struct addrmap *mutable_map;
2674 CORE_ADDR baseaddr;
2675
2676 auto_obstack temp_obstack;
2677
2678 mutable_map = addrmap_create_mutable (&temp_obstack);
2679
2680 iter = index->address_table.data ();
2681 end = iter + index->address_table.size ();
2682
2683 baseaddr = objfile->text_section_offset ();
2684
2685 while (iter < end)
2686 {
2687 ULONGEST hi, lo, cu_index;
2688 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2689 iter += 8;
2690 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2691 iter += 8;
2692 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2693 iter += 4;
2694
2695 if (lo > hi)
2696 {
2697 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2698 hex_string (lo), hex_string (hi));
2699 continue;
2700 }
2701
2702 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2703 {
2704 complaint (_(".gdb_index address table has invalid CU number %u"),
2705 (unsigned) cu_index);
2706 continue;
2707 }
2708
2709 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2710 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2711 addrmap_set_empty (mutable_map, lo, hi - 1,
2712 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2713 }
2714
2715 objfile->partial_symtabs->psymtabs_addrmap
2716 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2717 }
2718
2719 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2720 populate the objfile's psymtabs_addrmap. */
2721
2722 static void
2723 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2724 struct dwarf2_section_info *section)
2725 {
2726 struct objfile *objfile = dwarf2_per_objfile->objfile;
2727 bfd *abfd = objfile->obfd;
2728 struct gdbarch *gdbarch = objfile->arch ();
2729 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2730
2731 auto_obstack temp_obstack;
2732 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2733
2734 std::unordered_map<sect_offset,
2735 dwarf2_per_cu_data *,
2736 gdb::hash_enum<sect_offset>>
2737 debug_info_offset_to_per_cu;
2738 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2739 {
2740 const auto insertpair
2741 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2742 if (!insertpair.second)
2743 {
2744 warning (_("Section .debug_aranges in %s has duplicate "
2745 "debug_info_offset %s, ignoring .debug_aranges."),
2746 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2747 return;
2748 }
2749 }
2750
2751 section->read (objfile);
2752
2753 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2754
2755 const gdb_byte *addr = section->buffer;
2756
2757 while (addr < section->buffer + section->size)
2758 {
2759 const gdb_byte *const entry_addr = addr;
2760 unsigned int bytes_read;
2761
2762 const LONGEST entry_length = read_initial_length (abfd, addr,
2763 &bytes_read);
2764 addr += bytes_read;
2765
2766 const gdb_byte *const entry_end = addr + entry_length;
2767 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2768 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2769 if (addr + entry_length > section->buffer + section->size)
2770 {
2771 warning (_("Section .debug_aranges in %s entry at offset %s "
2772 "length %s exceeds section length %s, "
2773 "ignoring .debug_aranges."),
2774 objfile_name (objfile),
2775 plongest (entry_addr - section->buffer),
2776 plongest (bytes_read + entry_length),
2777 pulongest (section->size));
2778 return;
2779 }
2780
2781 /* The version number. */
2782 const uint16_t version = read_2_bytes (abfd, addr);
2783 addr += 2;
2784 if (version != 2)
2785 {
2786 warning (_("Section .debug_aranges in %s entry at offset %s "
2787 "has unsupported version %d, ignoring .debug_aranges."),
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer), version);
2790 return;
2791 }
2792
2793 const uint64_t debug_info_offset
2794 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2795 addr += offset_size;
2796 const auto per_cu_it
2797 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2798 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2799 {
2800 warning (_("Section .debug_aranges in %s entry at offset %s "
2801 "debug_info_offset %s does not exists, "
2802 "ignoring .debug_aranges."),
2803 objfile_name (objfile),
2804 plongest (entry_addr - section->buffer),
2805 pulongest (debug_info_offset));
2806 return;
2807 }
2808 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2809
2810 const uint8_t address_size = *addr++;
2811 if (address_size < 1 || address_size > 8)
2812 {
2813 warning (_("Section .debug_aranges in %s entry at offset %s "
2814 "address_size %u is invalid, ignoring .debug_aranges."),
2815 objfile_name (objfile),
2816 plongest (entry_addr - section->buffer), address_size);
2817 return;
2818 }
2819
2820 const uint8_t segment_selector_size = *addr++;
2821 if (segment_selector_size != 0)
2822 {
2823 warning (_("Section .debug_aranges in %s entry at offset %s "
2824 "segment_selector_size %u is not supported, "
2825 "ignoring .debug_aranges."),
2826 objfile_name (objfile),
2827 plongest (entry_addr - section->buffer),
2828 segment_selector_size);
2829 return;
2830 }
2831
2832 /* Must pad to an alignment boundary that is twice the address
2833 size. It is undocumented by the DWARF standard but GCC does
2834 use it. */
2835 for (size_t padding = ((-(addr - section->buffer))
2836 & (2 * address_size - 1));
2837 padding > 0; padding--)
2838 if (*addr++ != 0)
2839 {
2840 warning (_("Section .debug_aranges in %s entry at offset %s "
2841 "padding is not zero, ignoring .debug_aranges."),
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer));
2844 return;
2845 }
2846
2847 for (;;)
2848 {
2849 if (addr + 2 * address_size > entry_end)
2850 {
2851 warning (_("Section .debug_aranges in %s entry at offset %s "
2852 "address list is not properly terminated, "
2853 "ignoring .debug_aranges."),
2854 objfile_name (objfile),
2855 plongest (entry_addr - section->buffer));
2856 return;
2857 }
2858 ULONGEST start = extract_unsigned_integer (addr, address_size,
2859 dwarf5_byte_order);
2860 addr += address_size;
2861 ULONGEST length = extract_unsigned_integer (addr, address_size,
2862 dwarf5_byte_order);
2863 addr += address_size;
2864 if (start == 0 && length == 0)
2865 break;
2866 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2867 {
2868 /* Symbol was eliminated due to a COMDAT group. */
2869 continue;
2870 }
2871 ULONGEST end = start + length;
2872 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2873 - baseaddr);
2874 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2875 - baseaddr);
2876 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2877 }
2878 }
2879
2880 objfile->partial_symtabs->psymtabs_addrmap
2881 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2882 }
2883
2884 /* Find a slot in the mapped index INDEX for the object named NAME.
2885 If NAME is found, set *VEC_OUT to point to the CU vector in the
2886 constant pool and return true. If NAME cannot be found, return
2887 false. */
2888
2889 static bool
2890 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2891 offset_type **vec_out)
2892 {
2893 offset_type hash;
2894 offset_type slot, step;
2895 int (*cmp) (const char *, const char *);
2896
2897 gdb::unique_xmalloc_ptr<char> without_params;
2898 if (current_language->la_language == language_cplus
2899 || current_language->la_language == language_fortran
2900 || current_language->la_language == language_d)
2901 {
2902 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2903 not contain any. */
2904
2905 if (strchr (name, '(') != NULL)
2906 {
2907 without_params = cp_remove_params (name);
2908
2909 if (without_params != NULL)
2910 name = without_params.get ();
2911 }
2912 }
2913
2914 /* Index version 4 did not support case insensitive searches. But the
2915 indices for case insensitive languages are built in lowercase, therefore
2916 simulate our NAME being searched is also lowercased. */
2917 hash = mapped_index_string_hash ((index->version == 4
2918 && case_sensitivity == case_sensitive_off
2919 ? 5 : index->version),
2920 name);
2921
2922 slot = hash & (index->symbol_table.size () - 1);
2923 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2924 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2925
2926 for (;;)
2927 {
2928 const char *str;
2929
2930 const auto &bucket = index->symbol_table[slot];
2931 if (bucket.name == 0 && bucket.vec == 0)
2932 return false;
2933
2934 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2935 if (!cmp (name, str))
2936 {
2937 *vec_out = (offset_type *) (index->constant_pool
2938 + MAYBE_SWAP (bucket.vec));
2939 return true;
2940 }
2941
2942 slot = (slot + step) & (index->symbol_table.size () - 1);
2943 }
2944 }
2945
2946 /* A helper function that reads the .gdb_index from BUFFER and fills
2947 in MAP. FILENAME is the name of the file containing the data;
2948 it is used for error reporting. DEPRECATED_OK is true if it is
2949 ok to use deprecated sections.
2950
2951 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2952 out parameters that are filled in with information about the CU and
2953 TU lists in the section.
2954
2955 Returns true if all went well, false otherwise. */
2956
2957 static bool
2958 read_gdb_index_from_buffer (const char *filename,
2959 bool deprecated_ok,
2960 gdb::array_view<const gdb_byte> buffer,
2961 struct mapped_index *map,
2962 const gdb_byte **cu_list,
2963 offset_type *cu_list_elements,
2964 const gdb_byte **types_list,
2965 offset_type *types_list_elements)
2966 {
2967 const gdb_byte *addr = &buffer[0];
2968
2969 /* Version check. */
2970 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2971 /* Versions earlier than 3 emitted every copy of a psymbol. This
2972 causes the index to behave very poorly for certain requests. Version 3
2973 contained incomplete addrmap. So, it seems better to just ignore such
2974 indices. */
2975 if (version < 4)
2976 {
2977 static int warning_printed = 0;
2978 if (!warning_printed)
2979 {
2980 warning (_("Skipping obsolete .gdb_index section in %s."),
2981 filename);
2982 warning_printed = 1;
2983 }
2984 return 0;
2985 }
2986 /* Index version 4 uses a different hash function than index version
2987 5 and later.
2988
2989 Versions earlier than 6 did not emit psymbols for inlined
2990 functions. Using these files will cause GDB not to be able to
2991 set breakpoints on inlined functions by name, so we ignore these
2992 indices unless the user has done
2993 "set use-deprecated-index-sections on". */
2994 if (version < 6 && !deprecated_ok)
2995 {
2996 static int warning_printed = 0;
2997 if (!warning_printed)
2998 {
2999 warning (_("\
3000 Skipping deprecated .gdb_index section in %s.\n\
3001 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3002 to use the section anyway."),
3003 filename);
3004 warning_printed = 1;
3005 }
3006 return 0;
3007 }
3008 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3009 of the TU (for symbols coming from TUs),
3010 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3011 Plus gold-generated indices can have duplicate entries for global symbols,
3012 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3013 These are just performance bugs, and we can't distinguish gdb-generated
3014 indices from gold-generated ones, so issue no warning here. */
3015
3016 /* Indexes with higher version than the one supported by GDB may be no
3017 longer backward compatible. */
3018 if (version > 8)
3019 return 0;
3020
3021 map->version = version;
3022
3023 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3024
3025 int i = 0;
3026 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3027 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3028 / 8);
3029 ++i;
3030
3031 *types_list = addr + MAYBE_SWAP (metadata[i]);
3032 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3033 - MAYBE_SWAP (metadata[i]))
3034 / 8);
3035 ++i;
3036
3037 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3038 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3039 map->address_table
3040 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3041 ++i;
3042
3043 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3044 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3045 map->symbol_table
3046 = gdb::array_view<mapped_index::symbol_table_slot>
3047 ((mapped_index::symbol_table_slot *) symbol_table,
3048 (mapped_index::symbol_table_slot *) symbol_table_end);
3049
3050 ++i;
3051 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3052
3053 return 1;
3054 }
3055
3056 /* Callback types for dwarf2_read_gdb_index. */
3057
3058 typedef gdb::function_view
3059 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3060 get_gdb_index_contents_ftype;
3061 typedef gdb::function_view
3062 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3063 get_gdb_index_contents_dwz_ftype;
3064
3065 /* Read .gdb_index. If everything went ok, initialize the "quick"
3066 elements of all the CUs and return 1. Otherwise, return 0. */
3067
3068 static int
3069 dwarf2_read_gdb_index
3070 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3071 get_gdb_index_contents_ftype get_gdb_index_contents,
3072 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3073 {
3074 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3075 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3076 struct dwz_file *dwz;
3077 struct objfile *objfile = dwarf2_per_objfile->objfile;
3078
3079 gdb::array_view<const gdb_byte> main_index_contents
3080 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3081
3082 if (main_index_contents.empty ())
3083 return 0;
3084
3085 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3086 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3087 use_deprecated_index_sections,
3088 main_index_contents, map.get (), &cu_list,
3089 &cu_list_elements, &types_list,
3090 &types_list_elements))
3091 return 0;
3092
3093 /* Don't use the index if it's empty. */
3094 if (map->symbol_table.empty ())
3095 return 0;
3096
3097 /* If there is a .dwz file, read it so we can get its CU list as
3098 well. */
3099 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3100 if (dwz != NULL)
3101 {
3102 struct mapped_index dwz_map;
3103 const gdb_byte *dwz_types_ignore;
3104 offset_type dwz_types_elements_ignore;
3105
3106 gdb::array_view<const gdb_byte> dwz_index_content
3107 = get_gdb_index_contents_dwz (objfile, dwz);
3108
3109 if (dwz_index_content.empty ())
3110 return 0;
3111
3112 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3113 1, dwz_index_content, &dwz_map,
3114 &dwz_list, &dwz_list_elements,
3115 &dwz_types_ignore,
3116 &dwz_types_elements_ignore))
3117 {
3118 warning (_("could not read '.gdb_index' section from %s; skipping"),
3119 bfd_get_filename (dwz->dwz_bfd.get ()));
3120 return 0;
3121 }
3122 }
3123
3124 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3125 dwz_list, dwz_list_elements);
3126
3127 if (types_list_elements)
3128 {
3129 /* We can only handle a single .debug_types when we have an
3130 index. */
3131 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3132 return 0;
3133
3134 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3135
3136 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3137 types_list, types_list_elements);
3138 }
3139
3140 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3141
3142 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3143 dwarf2_per_objfile->per_bfd->using_index = 1;
3144 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3145 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3146
3147 return 1;
3148 }
3149
3150 /* die_reader_func for dw2_get_file_names. */
3151
3152 static void
3153 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3154 const gdb_byte *info_ptr,
3155 struct die_info *comp_unit_die)
3156 {
3157 struct dwarf2_cu *cu = reader->cu;
3158 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3159 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3160 struct dwarf2_per_cu_data *lh_cu;
3161 struct attribute *attr;
3162 void **slot;
3163 struct quick_file_names *qfn;
3164
3165 gdb_assert (! this_cu->is_debug_types);
3166
3167 /* Our callers never want to match partial units -- instead they
3168 will match the enclosing full CU. */
3169 if (comp_unit_die->tag == DW_TAG_partial_unit)
3170 {
3171 this_cu->v.quick->no_file_data = 1;
3172 return;
3173 }
3174
3175 lh_cu = this_cu;
3176 slot = NULL;
3177
3178 line_header_up lh;
3179 sect_offset line_offset {};
3180
3181 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3182 if (attr != nullptr)
3183 {
3184 struct quick_file_names find_entry;
3185
3186 line_offset = (sect_offset) DW_UNSND (attr);
3187
3188 /* We may have already read in this line header (TU line header sharing).
3189 If we have we're done. */
3190 find_entry.hash.dwo_unit = cu->dwo_unit;
3191 find_entry.hash.line_sect_off = line_offset;
3192 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3193 &find_entry, INSERT);
3194 if (*slot != NULL)
3195 {
3196 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3197 return;
3198 }
3199
3200 lh = dwarf_decode_line_header (line_offset, cu);
3201 }
3202 if (lh == NULL)
3203 {
3204 lh_cu->v.quick->no_file_data = 1;
3205 return;
3206 }
3207
3208 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3209 qfn->hash.dwo_unit = cu->dwo_unit;
3210 qfn->hash.line_sect_off = line_offset;
3211 gdb_assert (slot != NULL);
3212 *slot = qfn;
3213
3214 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3215
3216 int offset = 0;
3217 if (strcmp (fnd.name, "<unknown>") != 0)
3218 ++offset;
3219
3220 qfn->num_file_names = offset + lh->file_names_size ();
3221 qfn->file_names =
3222 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3223 qfn->num_file_names);
3224 if (offset != 0)
3225 qfn->file_names[0] = xstrdup (fnd.name);
3226 for (int i = 0; i < lh->file_names_size (); ++i)
3227 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3228 fnd.comp_dir).release ();
3229 qfn->real_names = NULL;
3230
3231 lh_cu->v.quick->file_names = qfn;
3232 }
3233
3234 /* A helper for the "quick" functions which attempts to read the line
3235 table for THIS_CU. */
3236
3237 static struct quick_file_names *
3238 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3239 dwarf2_per_objfile *per_objfile)
3240 {
3241 /* This should never be called for TUs. */
3242 gdb_assert (! this_cu->is_debug_types);
3243 /* Nor type unit groups. */
3244 gdb_assert (! this_cu->type_unit_group_p ());
3245
3246 if (this_cu->v.quick->file_names != NULL)
3247 return this_cu->v.quick->file_names;
3248 /* If we know there is no line data, no point in looking again. */
3249 if (this_cu->v.quick->no_file_data)
3250 return NULL;
3251
3252 cutu_reader reader (this_cu, per_objfile);
3253 if (!reader.dummy_p)
3254 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3255
3256 if (this_cu->v.quick->no_file_data)
3257 return NULL;
3258 return this_cu->v.quick->file_names;
3259 }
3260
3261 /* A helper for the "quick" functions which computes and caches the
3262 real path for a given file name from the line table. */
3263
3264 static const char *
3265 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3266 struct quick_file_names *qfn, int index)
3267 {
3268 if (qfn->real_names == NULL)
3269 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3270 qfn->num_file_names, const char *);
3271
3272 if (qfn->real_names[index] == NULL)
3273 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3274
3275 return qfn->real_names[index];
3276 }
3277
3278 static struct symtab *
3279 dw2_find_last_source_symtab (struct objfile *objfile)
3280 {
3281 struct dwarf2_per_objfile *dwarf2_per_objfile
3282 = get_dwarf2_per_objfile (objfile);
3283 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3284 compunit_symtab *cust
3285 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3286
3287 if (cust == NULL)
3288 return NULL;
3289
3290 return compunit_primary_filetab (cust);
3291 }
3292
3293 /* Traversal function for dw2_forget_cached_source_info. */
3294
3295 static int
3296 dw2_free_cached_file_names (void **slot, void *info)
3297 {
3298 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3299
3300 if (file_data->real_names)
3301 {
3302 int i;
3303
3304 for (i = 0; i < file_data->num_file_names; ++i)
3305 {
3306 xfree ((void*) file_data->real_names[i]);
3307 file_data->real_names[i] = NULL;
3308 }
3309 }
3310
3311 return 1;
3312 }
3313
3314 static void
3315 dw2_forget_cached_source_info (struct objfile *objfile)
3316 {
3317 struct dwarf2_per_objfile *dwarf2_per_objfile
3318 = get_dwarf2_per_objfile (objfile);
3319
3320 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3321 dw2_free_cached_file_names, NULL);
3322 }
3323
3324 /* Helper function for dw2_map_symtabs_matching_filename that expands
3325 the symtabs and calls the iterator. */
3326
3327 static int
3328 dw2_map_expand_apply (struct objfile *objfile,
3329 struct dwarf2_per_cu_data *per_cu,
3330 const char *name, const char *real_path,
3331 gdb::function_view<bool (symtab *)> callback)
3332 {
3333 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3334
3335 /* Don't visit already-expanded CUs. */
3336 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3337 if (per_objfile->symtab_set_p (per_cu))
3338 return 0;
3339
3340 /* This may expand more than one symtab, and we want to iterate over
3341 all of them. */
3342 dw2_instantiate_symtab (per_cu, per_objfile, false);
3343
3344 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3345 last_made, callback);
3346 }
3347
3348 /* Implementation of the map_symtabs_matching_filename method. */
3349
3350 static bool
3351 dw2_map_symtabs_matching_filename
3352 (struct objfile *objfile, const char *name, const char *real_path,
3353 gdb::function_view<bool (symtab *)> callback)
3354 {
3355 const char *name_basename = lbasename (name);
3356 struct dwarf2_per_objfile *dwarf2_per_objfile
3357 = get_dwarf2_per_objfile (objfile);
3358
3359 /* The rule is CUs specify all the files, including those used by
3360 any TU, so there's no need to scan TUs here. */
3361
3362 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3363 {
3364 /* We only need to look at symtabs not already expanded. */
3365 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3366 continue;
3367
3368 quick_file_names *file_data
3369 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3370 if (file_data == NULL)
3371 continue;
3372
3373 for (int j = 0; j < file_data->num_file_names; ++j)
3374 {
3375 const char *this_name = file_data->file_names[j];
3376 const char *this_real_name;
3377
3378 if (compare_filenames_for_search (this_name, name))
3379 {
3380 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3381 callback))
3382 return true;
3383 continue;
3384 }
3385
3386 /* Before we invoke realpath, which can get expensive when many
3387 files are involved, do a quick comparison of the basenames. */
3388 if (! basenames_may_differ
3389 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3390 continue;
3391
3392 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3393 file_data, j);
3394 if (compare_filenames_for_search (this_real_name, name))
3395 {
3396 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3397 callback))
3398 return true;
3399 continue;
3400 }
3401
3402 if (real_path != NULL)
3403 {
3404 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3405 gdb_assert (IS_ABSOLUTE_PATH (name));
3406 if (this_real_name != NULL
3407 && FILENAME_CMP (real_path, this_real_name) == 0)
3408 {
3409 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3410 callback))
3411 return true;
3412 continue;
3413 }
3414 }
3415 }
3416 }
3417
3418 return false;
3419 }
3420
3421 /* Struct used to manage iterating over all CUs looking for a symbol. */
3422
3423 struct dw2_symtab_iterator
3424 {
3425 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3426 struct dwarf2_per_objfile *dwarf2_per_objfile;
3427 /* If set, only look for symbols that match that block. Valid values are
3428 GLOBAL_BLOCK and STATIC_BLOCK. */
3429 gdb::optional<block_enum> block_index;
3430 /* The kind of symbol we're looking for. */
3431 domain_enum domain;
3432 /* The list of CUs from the index entry of the symbol,
3433 or NULL if not found. */
3434 offset_type *vec;
3435 /* The next element in VEC to look at. */
3436 int next;
3437 /* The number of elements in VEC, or zero if there is no match. */
3438 int length;
3439 /* Have we seen a global version of the symbol?
3440 If so we can ignore all further global instances.
3441 This is to work around gold/15646, inefficient gold-generated
3442 indices. */
3443 int global_seen;
3444 };
3445
3446 /* Initialize the index symtab iterator ITER. */
3447
3448 static void
3449 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3450 struct dwarf2_per_objfile *dwarf2_per_objfile,
3451 gdb::optional<block_enum> block_index,
3452 domain_enum domain,
3453 const char *name)
3454 {
3455 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3456 iter->block_index = block_index;
3457 iter->domain = domain;
3458 iter->next = 0;
3459 iter->global_seen = 0;
3460
3461 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3462
3463 /* index is NULL if OBJF_READNOW. */
3464 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3465 iter->length = MAYBE_SWAP (*iter->vec);
3466 else
3467 {
3468 iter->vec = NULL;
3469 iter->length = 0;
3470 }
3471 }
3472
3473 /* Return the next matching CU or NULL if there are no more. */
3474
3475 static struct dwarf2_per_cu_data *
3476 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3477 {
3478 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3479
3480 for ( ; iter->next < iter->length; ++iter->next)
3481 {
3482 offset_type cu_index_and_attrs =
3483 MAYBE_SWAP (iter->vec[iter->next + 1]);
3484 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3485 gdb_index_symbol_kind symbol_kind =
3486 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3487 /* Only check the symbol attributes if they're present.
3488 Indices prior to version 7 don't record them,
3489 and indices >= 7 may elide them for certain symbols
3490 (gold does this). */
3491 int attrs_valid =
3492 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3493 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3494
3495 /* Don't crash on bad data. */
3496 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3497 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3498 {
3499 complaint (_(".gdb_index entry has bad CU index"
3500 " [in module %s]"),
3501 objfile_name (dwarf2_per_objfile->objfile));
3502 continue;
3503 }
3504
3505 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3506
3507 /* Skip if already read in. */
3508 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3509 continue;
3510
3511 /* Check static vs global. */
3512 if (attrs_valid)
3513 {
3514 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3515
3516 if (iter->block_index.has_value ())
3517 {
3518 bool want_static = *iter->block_index == STATIC_BLOCK;
3519
3520 if (is_static != want_static)
3521 continue;
3522 }
3523
3524 /* Work around gold/15646. */
3525 if (!is_static && iter->global_seen)
3526 continue;
3527 if (!is_static)
3528 iter->global_seen = 1;
3529 }
3530
3531 /* Only check the symbol's kind if it has one. */
3532 if (attrs_valid)
3533 {
3534 switch (iter->domain)
3535 {
3536 case VAR_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3538 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3539 /* Some types are also in VAR_DOMAIN. */
3540 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3541 continue;
3542 break;
3543 case STRUCT_DOMAIN:
3544 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3545 continue;
3546 break;
3547 case LABEL_DOMAIN:
3548 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3549 continue;
3550 break;
3551 case MODULE_DOMAIN:
3552 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3553 continue;
3554 break;
3555 default:
3556 break;
3557 }
3558 }
3559
3560 ++iter->next;
3561 return per_cu;
3562 }
3563
3564 return NULL;
3565 }
3566
3567 static struct compunit_symtab *
3568 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3569 const char *name, domain_enum domain)
3570 {
3571 struct compunit_symtab *stab_best = NULL;
3572 struct dwarf2_per_objfile *dwarf2_per_objfile
3573 = get_dwarf2_per_objfile (objfile);
3574
3575 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3576
3577 struct dw2_symtab_iterator iter;
3578 struct dwarf2_per_cu_data *per_cu;
3579
3580 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3581
3582 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3583 {
3584 struct symbol *sym, *with_opaque = NULL;
3585 struct compunit_symtab *stab
3586 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3587 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3588 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3589
3590 sym = block_find_symbol (block, name, domain,
3591 block_find_non_opaque_type_preferred,
3592 &with_opaque);
3593
3594 /* Some caution must be observed with overloaded functions
3595 and methods, since the index will not contain any overload
3596 information (but NAME might contain it). */
3597
3598 if (sym != NULL
3599 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3600 return stab;
3601 if (with_opaque != NULL
3602 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3603 stab_best = stab;
3604
3605 /* Keep looking through other CUs. */
3606 }
3607
3608 return stab_best;
3609 }
3610
3611 static void
3612 dw2_print_stats (struct objfile *objfile)
3613 {
3614 struct dwarf2_per_objfile *dwarf2_per_objfile
3615 = get_dwarf2_per_objfile (objfile);
3616 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3617 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3618 int count = 0;
3619
3620 for (int i = 0; i < total; ++i)
3621 {
3622 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3623
3624 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3625 ++count;
3626 }
3627 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3628 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3629 }
3630
3631 /* This dumps minimal information about the index.
3632 It is called via "mt print objfiles".
3633 One use is to verify .gdb_index has been loaded by the
3634 gdb.dwarf2/gdb-index.exp testcase. */
3635
3636 static void
3637 dw2_dump (struct objfile *objfile)
3638 {
3639 struct dwarf2_per_objfile *dwarf2_per_objfile
3640 = get_dwarf2_per_objfile (objfile);
3641
3642 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3643 printf_filtered (".gdb_index:");
3644 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3645 {
3646 printf_filtered (" version %d\n",
3647 dwarf2_per_objfile->per_bfd->index_table->version);
3648 }
3649 else
3650 printf_filtered (" faked for \"readnow\"\n");
3651 printf_filtered ("\n");
3652 }
3653
3654 static void
3655 dw2_expand_symtabs_for_function (struct objfile *objfile,
3656 const char *func_name)
3657 {
3658 struct dwarf2_per_objfile *dwarf2_per_objfile
3659 = get_dwarf2_per_objfile (objfile);
3660
3661 struct dw2_symtab_iterator iter;
3662 struct dwarf2_per_cu_data *per_cu;
3663
3664 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3665
3666 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3667 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3668
3669 }
3670
3671 static void
3672 dw2_expand_all_symtabs (struct objfile *objfile)
3673 {
3674 struct dwarf2_per_objfile *dwarf2_per_objfile
3675 = get_dwarf2_per_objfile (objfile);
3676 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3677 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3678
3679 for (int i = 0; i < total_units; ++i)
3680 {
3681 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3682
3683 /* We don't want to directly expand a partial CU, because if we
3684 read it with the wrong language, then assertion failures can
3685 be triggered later on. See PR symtab/23010. So, tell
3686 dw2_instantiate_symtab to skip partial CUs -- any important
3687 partial CU will be read via DW_TAG_imported_unit anyway. */
3688 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3689 }
3690 }
3691
3692 static void
3693 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3694 const char *fullname)
3695 {
3696 struct dwarf2_per_objfile *dwarf2_per_objfile
3697 = get_dwarf2_per_objfile (objfile);
3698
3699 /* We don't need to consider type units here.
3700 This is only called for examining code, e.g. expand_line_sal.
3701 There can be an order of magnitude (or more) more type units
3702 than comp units, and we avoid them if we can. */
3703
3704 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3705 {
3706 /* We only need to look at symtabs not already expanded. */
3707 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3708 continue;
3709
3710 quick_file_names *file_data
3711 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3712 if (file_data == NULL)
3713 continue;
3714
3715 for (int j = 0; j < file_data->num_file_names; ++j)
3716 {
3717 const char *this_fullname = file_data->file_names[j];
3718
3719 if (filename_cmp (this_fullname, fullname) == 0)
3720 {
3721 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3722 break;
3723 }
3724 }
3725 }
3726 }
3727
3728 static void
3729 dw2_expand_symtabs_matching_symbol
3730 (mapped_index_base &index,
3731 const lookup_name_info &lookup_name_in,
3732 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3733 enum search_domain kind,
3734 gdb::function_view<bool (offset_type)> match_callback);
3735
3736 static void
3737 dw2_expand_symtabs_matching_one
3738 (dwarf2_per_cu_data *per_cu,
3739 dwarf2_per_objfile *per_objfile,
3740 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3741 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3742
3743 static void
3744 dw2_map_matching_symbols
3745 (struct objfile *objfile,
3746 const lookup_name_info &name, domain_enum domain,
3747 int global,
3748 gdb::function_view<symbol_found_callback_ftype> callback,
3749 symbol_compare_ftype *ordered_compare)
3750 {
3751 /* Used for Ada. */
3752 struct dwarf2_per_objfile *dwarf2_per_objfile
3753 = get_dwarf2_per_objfile (objfile);
3754
3755 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3756
3757 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3758 {
3759 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3760 here though if the current language is Ada for a non-Ada objfile
3761 using GNU index. */
3762 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3763
3764 const char *match_name = name.ada ().lookup_name ().c_str ();
3765 auto matcher = [&] (const char *symname)
3766 {
3767 if (ordered_compare == nullptr)
3768 return true;
3769 return ordered_compare (symname, match_name) == 0;
3770 };
3771
3772 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3773 [&] (offset_type namei)
3774 {
3775 struct dw2_symtab_iterator iter;
3776 struct dwarf2_per_cu_data *per_cu;
3777
3778 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3779 match_name);
3780 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3781 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3782 nullptr);
3783 return true;
3784 });
3785 }
3786 else
3787 {
3788 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3789 proceed assuming all symtabs have been read in. */
3790 }
3791
3792 for (compunit_symtab *cust : objfile->compunits ())
3793 {
3794 const struct block *block;
3795
3796 if (cust == NULL)
3797 continue;
3798 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3799 if (!iterate_over_symbols_terminated (block, name,
3800 domain, callback))
3801 return;
3802 }
3803 }
3804
3805 /* Starting from a search name, return the string that finds the upper
3806 bound of all strings that start with SEARCH_NAME in a sorted name
3807 list. Returns the empty string to indicate that the upper bound is
3808 the end of the list. */
3809
3810 static std::string
3811 make_sort_after_prefix_name (const char *search_name)
3812 {
3813 /* When looking to complete "func", we find the upper bound of all
3814 symbols that start with "func" by looking for where we'd insert
3815 the closest string that would follow "func" in lexicographical
3816 order. Usually, that's "func"-with-last-character-incremented,
3817 i.e. "fund". Mind non-ASCII characters, though. Usually those
3818 will be UTF-8 multi-byte sequences, but we can't be certain.
3819 Especially mind the 0xff character, which is a valid character in
3820 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3821 rule out compilers allowing it in identifiers. Note that
3822 conveniently, strcmp/strcasecmp are specified to compare
3823 characters interpreted as unsigned char. So what we do is treat
3824 the whole string as a base 256 number composed of a sequence of
3825 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3826 to 0, and carries 1 to the following more-significant position.
3827 If the very first character in SEARCH_NAME ends up incremented
3828 and carries/overflows, then the upper bound is the end of the
3829 list. The string after the empty string is also the empty
3830 string.
3831
3832 Some examples of this operation:
3833
3834 SEARCH_NAME => "+1" RESULT
3835
3836 "abc" => "abd"
3837 "ab\xff" => "ac"
3838 "\xff" "a" "\xff" => "\xff" "b"
3839 "\xff" => ""
3840 "\xff\xff" => ""
3841 "" => ""
3842
3843 Then, with these symbols for example:
3844
3845 func
3846 func1
3847 fund
3848
3849 completing "func" looks for symbols between "func" and
3850 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3851 which finds "func" and "func1", but not "fund".
3852
3853 And with:
3854
3855 funcÿ (Latin1 'ÿ' [0xff])
3856 funcÿ1
3857 fund
3858
3859 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3860 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3861
3862 And with:
3863
3864 ÿÿ (Latin1 'ÿ' [0xff])
3865 ÿÿ1
3866
3867 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3868 the end of the list.
3869 */
3870 std::string after = search_name;
3871 while (!after.empty () && (unsigned char) after.back () == 0xff)
3872 after.pop_back ();
3873 if (!after.empty ())
3874 after.back () = (unsigned char) after.back () + 1;
3875 return after;
3876 }
3877
3878 /* See declaration. */
3879
3880 std::pair<std::vector<name_component>::const_iterator,
3881 std::vector<name_component>::const_iterator>
3882 mapped_index_base::find_name_components_bounds
3883 (const lookup_name_info &lookup_name_without_params, language lang) const
3884 {
3885 auto *name_cmp
3886 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3887
3888 const char *lang_name
3889 = lookup_name_without_params.language_lookup_name (lang);
3890
3891 /* Comparison function object for lower_bound that matches against a
3892 given symbol name. */
3893 auto lookup_compare_lower = [&] (const name_component &elem,
3894 const char *name)
3895 {
3896 const char *elem_qualified = this->symbol_name_at (elem.idx);
3897 const char *elem_name = elem_qualified + elem.name_offset;
3898 return name_cmp (elem_name, name) < 0;
3899 };
3900
3901 /* Comparison function object for upper_bound that matches against a
3902 given symbol name. */
3903 auto lookup_compare_upper = [&] (const char *name,
3904 const name_component &elem)
3905 {
3906 const char *elem_qualified = this->symbol_name_at (elem.idx);
3907 const char *elem_name = elem_qualified + elem.name_offset;
3908 return name_cmp (name, elem_name) < 0;
3909 };
3910
3911 auto begin = this->name_components.begin ();
3912 auto end = this->name_components.end ();
3913
3914 /* Find the lower bound. */
3915 auto lower = [&] ()
3916 {
3917 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3918 return begin;
3919 else
3920 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3921 } ();
3922
3923 /* Find the upper bound. */
3924 auto upper = [&] ()
3925 {
3926 if (lookup_name_without_params.completion_mode ())
3927 {
3928 /* In completion mode, we want UPPER to point past all
3929 symbols names that have the same prefix. I.e., with
3930 these symbols, and completing "func":
3931
3932 function << lower bound
3933 function1
3934 other_function << upper bound
3935
3936 We find the upper bound by looking for the insertion
3937 point of "func"-with-last-character-incremented,
3938 i.e. "fund". */
3939 std::string after = make_sort_after_prefix_name (lang_name);
3940 if (after.empty ())
3941 return end;
3942 return std::lower_bound (lower, end, after.c_str (),
3943 lookup_compare_lower);
3944 }
3945 else
3946 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3947 } ();
3948
3949 return {lower, upper};
3950 }
3951
3952 /* See declaration. */
3953
3954 void
3955 mapped_index_base::build_name_components ()
3956 {
3957 if (!this->name_components.empty ())
3958 return;
3959
3960 this->name_components_casing = case_sensitivity;
3961 auto *name_cmp
3962 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3963
3964 /* The code below only knows how to break apart components of C++
3965 symbol names (and other languages that use '::' as
3966 namespace/module separator) and Ada symbol names. */
3967 auto count = this->symbol_name_count ();
3968 for (offset_type idx = 0; idx < count; idx++)
3969 {
3970 if (this->symbol_name_slot_invalid (idx))
3971 continue;
3972
3973 const char *name = this->symbol_name_at (idx);
3974
3975 /* Add each name component to the name component table. */
3976 unsigned int previous_len = 0;
3977
3978 if (strstr (name, "::") != nullptr)
3979 {
3980 for (unsigned int current_len = cp_find_first_component (name);
3981 name[current_len] != '\0';
3982 current_len += cp_find_first_component (name + current_len))
3983 {
3984 gdb_assert (name[current_len] == ':');
3985 this->name_components.push_back ({previous_len, idx});
3986 /* Skip the '::'. */
3987 current_len += 2;
3988 previous_len = current_len;
3989 }
3990 }
3991 else
3992 {
3993 /* Handle the Ada encoded (aka mangled) form here. */
3994 for (const char *iter = strstr (name, "__");
3995 iter != nullptr;
3996 iter = strstr (iter, "__"))
3997 {
3998 this->name_components.push_back ({previous_len, idx});
3999 iter += 2;
4000 previous_len = iter - name;
4001 }
4002 }
4003
4004 this->name_components.push_back ({previous_len, idx});
4005 }
4006
4007 /* Sort name_components elements by name. */
4008 auto name_comp_compare = [&] (const name_component &left,
4009 const name_component &right)
4010 {
4011 const char *left_qualified = this->symbol_name_at (left.idx);
4012 const char *right_qualified = this->symbol_name_at (right.idx);
4013
4014 const char *left_name = left_qualified + left.name_offset;
4015 const char *right_name = right_qualified + right.name_offset;
4016
4017 return name_cmp (left_name, right_name) < 0;
4018 };
4019
4020 std::sort (this->name_components.begin (),
4021 this->name_components.end (),
4022 name_comp_compare);
4023 }
4024
4025 /* Helper for dw2_expand_symtabs_matching that works with a
4026 mapped_index_base instead of the containing objfile. This is split
4027 to a separate function in order to be able to unit test the
4028 name_components matching using a mock mapped_index_base. For each
4029 symbol name that matches, calls MATCH_CALLBACK, passing it the
4030 symbol's index in the mapped_index_base symbol table. */
4031
4032 static void
4033 dw2_expand_symtabs_matching_symbol
4034 (mapped_index_base &index,
4035 const lookup_name_info &lookup_name_in,
4036 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4037 enum search_domain kind,
4038 gdb::function_view<bool (offset_type)> match_callback)
4039 {
4040 lookup_name_info lookup_name_without_params
4041 = lookup_name_in.make_ignore_params ();
4042
4043 /* Build the symbol name component sorted vector, if we haven't
4044 yet. */
4045 index.build_name_components ();
4046
4047 /* The same symbol may appear more than once in the range though.
4048 E.g., if we're looking for symbols that complete "w", and we have
4049 a symbol named "w1::w2", we'll find the two name components for
4050 that same symbol in the range. To be sure we only call the
4051 callback once per symbol, we first collect the symbol name
4052 indexes that matched in a temporary vector and ignore
4053 duplicates. */
4054 std::vector<offset_type> matches;
4055
4056 struct name_and_matcher
4057 {
4058 symbol_name_matcher_ftype *matcher;
4059 const char *name;
4060
4061 bool operator== (const name_and_matcher &other) const
4062 {
4063 return matcher == other.matcher && strcmp (name, other.name) == 0;
4064 }
4065 };
4066
4067 /* A vector holding all the different symbol name matchers, for all
4068 languages. */
4069 std::vector<name_and_matcher> matchers;
4070
4071 for (int i = 0; i < nr_languages; i++)
4072 {
4073 enum language lang_e = (enum language) i;
4074
4075 const language_defn *lang = language_def (lang_e);
4076 symbol_name_matcher_ftype *name_matcher
4077 = get_symbol_name_matcher (lang, lookup_name_without_params);
4078
4079 name_and_matcher key {
4080 name_matcher,
4081 lookup_name_without_params.language_lookup_name (lang_e)
4082 };
4083
4084 /* Don't insert the same comparison routine more than once.
4085 Note that we do this linear walk. This is not a problem in
4086 practice because the number of supported languages is
4087 low. */
4088 if (std::find (matchers.begin (), matchers.end (), key)
4089 != matchers.end ())
4090 continue;
4091 matchers.push_back (std::move (key));
4092
4093 auto bounds
4094 = index.find_name_components_bounds (lookup_name_without_params,
4095 lang_e);
4096
4097 /* Now for each symbol name in range, check to see if we have a name
4098 match, and if so, call the MATCH_CALLBACK callback. */
4099
4100 for (; bounds.first != bounds.second; ++bounds.first)
4101 {
4102 const char *qualified = index.symbol_name_at (bounds.first->idx);
4103
4104 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4105 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4106 continue;
4107
4108 matches.push_back (bounds.first->idx);
4109 }
4110 }
4111
4112 std::sort (matches.begin (), matches.end ());
4113
4114 /* Finally call the callback, once per match. */
4115 ULONGEST prev = -1;
4116 for (offset_type idx : matches)
4117 {
4118 if (prev != idx)
4119 {
4120 if (!match_callback (idx))
4121 break;
4122 prev = idx;
4123 }
4124 }
4125
4126 /* Above we use a type wider than idx's for 'prev', since 0 and
4127 (offset_type)-1 are both possible values. */
4128 static_assert (sizeof (prev) > sizeof (offset_type), "");
4129 }
4130
4131 #if GDB_SELF_TEST
4132
4133 namespace selftests { namespace dw2_expand_symtabs_matching {
4134
4135 /* A mock .gdb_index/.debug_names-like name index table, enough to
4136 exercise dw2_expand_symtabs_matching_symbol, which works with the
4137 mapped_index_base interface. Builds an index from the symbol list
4138 passed as parameter to the constructor. */
4139 class mock_mapped_index : public mapped_index_base
4140 {
4141 public:
4142 mock_mapped_index (gdb::array_view<const char *> symbols)
4143 : m_symbol_table (symbols)
4144 {}
4145
4146 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4147
4148 /* Return the number of names in the symbol table. */
4149 size_t symbol_name_count () const override
4150 {
4151 return m_symbol_table.size ();
4152 }
4153
4154 /* Get the name of the symbol at IDX in the symbol table. */
4155 const char *symbol_name_at (offset_type idx) const override
4156 {
4157 return m_symbol_table[idx];
4158 }
4159
4160 private:
4161 gdb::array_view<const char *> m_symbol_table;
4162 };
4163
4164 /* Convenience function that converts a NULL pointer to a "<null>"
4165 string, to pass to print routines. */
4166
4167 static const char *
4168 string_or_null (const char *str)
4169 {
4170 return str != NULL ? str : "<null>";
4171 }
4172
4173 /* Check if a lookup_name_info built from
4174 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4175 index. EXPECTED_LIST is the list of expected matches, in expected
4176 matching order. If no match expected, then an empty list is
4177 specified. Returns true on success. On failure prints a warning
4178 indicating the file:line that failed, and returns false. */
4179
4180 static bool
4181 check_match (const char *file, int line,
4182 mock_mapped_index &mock_index,
4183 const char *name, symbol_name_match_type match_type,
4184 bool completion_mode,
4185 std::initializer_list<const char *> expected_list)
4186 {
4187 lookup_name_info lookup_name (name, match_type, completion_mode);
4188
4189 bool matched = true;
4190
4191 auto mismatch = [&] (const char *expected_str,
4192 const char *got)
4193 {
4194 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4195 "expected=\"%s\", got=\"%s\"\n"),
4196 file, line,
4197 (match_type == symbol_name_match_type::FULL
4198 ? "FULL" : "WILD"),
4199 name, string_or_null (expected_str), string_or_null (got));
4200 matched = false;
4201 };
4202
4203 auto expected_it = expected_list.begin ();
4204 auto expected_end = expected_list.end ();
4205
4206 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4207 NULL, ALL_DOMAIN,
4208 [&] (offset_type idx)
4209 {
4210 const char *matched_name = mock_index.symbol_name_at (idx);
4211 const char *expected_str
4212 = expected_it == expected_end ? NULL : *expected_it++;
4213
4214 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4215 mismatch (expected_str, matched_name);
4216 return true;
4217 });
4218
4219 const char *expected_str
4220 = expected_it == expected_end ? NULL : *expected_it++;
4221 if (expected_str != NULL)
4222 mismatch (expected_str, NULL);
4223
4224 return matched;
4225 }
4226
4227 /* The symbols added to the mock mapped_index for testing (in
4228 canonical form). */
4229 static const char *test_symbols[] = {
4230 "function",
4231 "std::bar",
4232 "std::zfunction",
4233 "std::zfunction2",
4234 "w1::w2",
4235 "ns::foo<char*>",
4236 "ns::foo<int>",
4237 "ns::foo<long>",
4238 "ns2::tmpl<int>::foo2",
4239 "(anonymous namespace)::A::B::C",
4240
4241 /* These are used to check that the increment-last-char in the
4242 matching algorithm for completion doesn't match "t1_fund" when
4243 completing "t1_func". */
4244 "t1_func",
4245 "t1_func1",
4246 "t1_fund",
4247 "t1_fund1",
4248
4249 /* A UTF-8 name with multi-byte sequences to make sure that
4250 cp-name-parser understands this as a single identifier ("função"
4251 is "function" in PT). */
4252 u8"u8função",
4253
4254 /* \377 (0xff) is Latin1 'ÿ'. */
4255 "yfunc\377",
4256
4257 /* \377 (0xff) is Latin1 'ÿ'. */
4258 "\377",
4259 "\377\377123",
4260
4261 /* A name with all sorts of complications. Starts with "z" to make
4262 it easier for the completion tests below. */
4263 #define Z_SYM_NAME \
4264 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4265 "::tuple<(anonymous namespace)::ui*, " \
4266 "std::default_delete<(anonymous namespace)::ui>, void>"
4267
4268 Z_SYM_NAME
4269 };
4270
4271 /* Returns true if the mapped_index_base::find_name_component_bounds
4272 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4273 in completion mode. */
4274
4275 static bool
4276 check_find_bounds_finds (mapped_index_base &index,
4277 const char *search_name,
4278 gdb::array_view<const char *> expected_syms)
4279 {
4280 lookup_name_info lookup_name (search_name,
4281 symbol_name_match_type::FULL, true);
4282
4283 auto bounds = index.find_name_components_bounds (lookup_name,
4284 language_cplus);
4285
4286 size_t distance = std::distance (bounds.first, bounds.second);
4287 if (distance != expected_syms.size ())
4288 return false;
4289
4290 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4291 {
4292 auto nc_elem = bounds.first + exp_elem;
4293 const char *qualified = index.symbol_name_at (nc_elem->idx);
4294 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4295 return false;
4296 }
4297
4298 return true;
4299 }
4300
4301 /* Test the lower-level mapped_index::find_name_component_bounds
4302 method. */
4303
4304 static void
4305 test_mapped_index_find_name_component_bounds ()
4306 {
4307 mock_mapped_index mock_index (test_symbols);
4308
4309 mock_index.build_name_components ();
4310
4311 /* Test the lower-level mapped_index::find_name_component_bounds
4312 method in completion mode. */
4313 {
4314 static const char *expected_syms[] = {
4315 "t1_func",
4316 "t1_func1",
4317 };
4318
4319 SELF_CHECK (check_find_bounds_finds (mock_index,
4320 "t1_func", expected_syms));
4321 }
4322
4323 /* Check that the increment-last-char in the name matching algorithm
4324 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4325 {
4326 static const char *expected_syms1[] = {
4327 "\377",
4328 "\377\377123",
4329 };
4330 SELF_CHECK (check_find_bounds_finds (mock_index,
4331 "\377", expected_syms1));
4332
4333 static const char *expected_syms2[] = {
4334 "\377\377123",
4335 };
4336 SELF_CHECK (check_find_bounds_finds (mock_index,
4337 "\377\377", expected_syms2));
4338 }
4339 }
4340
4341 /* Test dw2_expand_symtabs_matching_symbol. */
4342
4343 static void
4344 test_dw2_expand_symtabs_matching_symbol ()
4345 {
4346 mock_mapped_index mock_index (test_symbols);
4347
4348 /* We let all tests run until the end even if some fails, for debug
4349 convenience. */
4350 bool any_mismatch = false;
4351
4352 /* Create the expected symbols list (an initializer_list). Needed
4353 because lists have commas, and we need to pass them to CHECK,
4354 which is a macro. */
4355 #define EXPECT(...) { __VA_ARGS__ }
4356
4357 /* Wrapper for check_match that passes down the current
4358 __FILE__/__LINE__. */
4359 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4360 any_mismatch |= !check_match (__FILE__, __LINE__, \
4361 mock_index, \
4362 NAME, MATCH_TYPE, COMPLETION_MODE, \
4363 EXPECTED_LIST)
4364
4365 /* Identity checks. */
4366 for (const char *sym : test_symbols)
4367 {
4368 /* Should be able to match all existing symbols. */
4369 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4370 EXPECT (sym));
4371
4372 /* Should be able to match all existing symbols with
4373 parameters. */
4374 std::string with_params = std::string (sym) + "(int)";
4375 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4376 EXPECT (sym));
4377
4378 /* Should be able to match all existing symbols with
4379 parameters and qualifiers. */
4380 with_params = std::string (sym) + " ( int ) const";
4381 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4382 EXPECT (sym));
4383
4384 /* This should really find sym, but cp-name-parser.y doesn't
4385 know about lvalue/rvalue qualifiers yet. */
4386 with_params = std::string (sym) + " ( int ) &&";
4387 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4388 {});
4389 }
4390
4391 /* Check that the name matching algorithm for completion doesn't get
4392 confused with Latin1 'ÿ' / 0xff. */
4393 {
4394 static const char str[] = "\377";
4395 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4396 EXPECT ("\377", "\377\377123"));
4397 }
4398
4399 /* Check that the increment-last-char in the matching algorithm for
4400 completion doesn't match "t1_fund" when completing "t1_func". */
4401 {
4402 static const char str[] = "t1_func";
4403 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4404 EXPECT ("t1_func", "t1_func1"));
4405 }
4406
4407 /* Check that completion mode works at each prefix of the expected
4408 symbol name. */
4409 {
4410 static const char str[] = "function(int)";
4411 size_t len = strlen (str);
4412 std::string lookup;
4413
4414 for (size_t i = 1; i < len; i++)
4415 {
4416 lookup.assign (str, i);
4417 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4418 EXPECT ("function"));
4419 }
4420 }
4421
4422 /* While "w" is a prefix of both components, the match function
4423 should still only be called once. */
4424 {
4425 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4426 EXPECT ("w1::w2"));
4427 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4428 EXPECT ("w1::w2"));
4429 }
4430
4431 /* Same, with a "complicated" symbol. */
4432 {
4433 static const char str[] = Z_SYM_NAME;
4434 size_t len = strlen (str);
4435 std::string lookup;
4436
4437 for (size_t i = 1; i < len; i++)
4438 {
4439 lookup.assign (str, i);
4440 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4441 EXPECT (Z_SYM_NAME));
4442 }
4443 }
4444
4445 /* In FULL mode, an incomplete symbol doesn't match. */
4446 {
4447 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4448 {});
4449 }
4450
4451 /* A complete symbol with parameters matches any overload, since the
4452 index has no overload info. */
4453 {
4454 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4455 EXPECT ("std::zfunction", "std::zfunction2"));
4456 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4457 EXPECT ("std::zfunction", "std::zfunction2"));
4458 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4459 EXPECT ("std::zfunction", "std::zfunction2"));
4460 }
4461
4462 /* Check that whitespace is ignored appropriately. A symbol with a
4463 template argument list. */
4464 {
4465 static const char expected[] = "ns::foo<int>";
4466 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4467 EXPECT (expected));
4468 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4469 EXPECT (expected));
4470 }
4471
4472 /* Check that whitespace is ignored appropriately. A symbol with a
4473 template argument list that includes a pointer. */
4474 {
4475 static const char expected[] = "ns::foo<char*>";
4476 /* Try both completion and non-completion modes. */
4477 static const bool completion_mode[2] = {false, true};
4478 for (size_t i = 0; i < 2; i++)
4479 {
4480 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4481 completion_mode[i], EXPECT (expected));
4482 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4483 completion_mode[i], EXPECT (expected));
4484
4485 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4486 completion_mode[i], EXPECT (expected));
4487 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4488 completion_mode[i], EXPECT (expected));
4489 }
4490 }
4491
4492 {
4493 /* Check method qualifiers are ignored. */
4494 static const char expected[] = "ns::foo<char*>";
4495 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4496 symbol_name_match_type::FULL, true, EXPECT (expected));
4497 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4498 symbol_name_match_type::FULL, true, EXPECT (expected));
4499 CHECK_MATCH ("foo < char * > ( int ) const",
4500 symbol_name_match_type::WILD, true, EXPECT (expected));
4501 CHECK_MATCH ("foo < char * > ( int ) &&",
4502 symbol_name_match_type::WILD, true, EXPECT (expected));
4503 }
4504
4505 /* Test lookup names that don't match anything. */
4506 {
4507 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4508 {});
4509
4510 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4511 {});
4512 }
4513
4514 /* Some wild matching tests, exercising "(anonymous namespace)",
4515 which should not be confused with a parameter list. */
4516 {
4517 static const char *syms[] = {
4518 "A::B::C",
4519 "B::C",
4520 "C",
4521 "A :: B :: C ( int )",
4522 "B :: C ( int )",
4523 "C ( int )",
4524 };
4525
4526 for (const char *s : syms)
4527 {
4528 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4529 EXPECT ("(anonymous namespace)::A::B::C"));
4530 }
4531 }
4532
4533 {
4534 static const char expected[] = "ns2::tmpl<int>::foo2";
4535 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4536 EXPECT (expected));
4537 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4538 EXPECT (expected));
4539 }
4540
4541 SELF_CHECK (!any_mismatch);
4542
4543 #undef EXPECT
4544 #undef CHECK_MATCH
4545 }
4546
4547 static void
4548 run_test ()
4549 {
4550 test_mapped_index_find_name_component_bounds ();
4551 test_dw2_expand_symtabs_matching_symbol ();
4552 }
4553
4554 }} // namespace selftests::dw2_expand_symtabs_matching
4555
4556 #endif /* GDB_SELF_TEST */
4557
4558 /* If FILE_MATCHER is NULL or if PER_CU has
4559 dwarf2_per_cu_quick_data::MARK set (see
4560 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4561 EXPANSION_NOTIFY on it. */
4562
4563 static void
4564 dw2_expand_symtabs_matching_one
4565 (dwarf2_per_cu_data *per_cu,
4566 dwarf2_per_objfile *per_objfile,
4567 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4568 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4569 {
4570 if (file_matcher == NULL || per_cu->v.quick->mark)
4571 {
4572 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4573
4574 compunit_symtab *symtab
4575 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4576 gdb_assert (symtab != nullptr);
4577
4578 if (expansion_notify != NULL && symtab_was_null)
4579 expansion_notify (symtab);
4580 }
4581 }
4582
4583 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4584 matched, to expand corresponding CUs that were marked. IDX is the
4585 index of the symbol name that matched. */
4586
4587 static void
4588 dw2_expand_marked_cus
4589 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4590 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4591 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4592 search_domain kind)
4593 {
4594 offset_type *vec, vec_len, vec_idx;
4595 bool global_seen = false;
4596 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4597
4598 vec = (offset_type *) (index.constant_pool
4599 + MAYBE_SWAP (index.symbol_table[idx].vec));
4600 vec_len = MAYBE_SWAP (vec[0]);
4601 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4602 {
4603 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4604 /* This value is only valid for index versions >= 7. */
4605 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4606 gdb_index_symbol_kind symbol_kind =
4607 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4608 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4609 /* Only check the symbol attributes if they're present.
4610 Indices prior to version 7 don't record them,
4611 and indices >= 7 may elide them for certain symbols
4612 (gold does this). */
4613 int attrs_valid =
4614 (index.version >= 7
4615 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4616
4617 /* Work around gold/15646. */
4618 if (attrs_valid)
4619 {
4620 if (!is_static && global_seen)
4621 continue;
4622 if (!is_static)
4623 global_seen = true;
4624 }
4625
4626 /* Only check the symbol's kind if it has one. */
4627 if (attrs_valid)
4628 {
4629 switch (kind)
4630 {
4631 case VARIABLES_DOMAIN:
4632 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4633 continue;
4634 break;
4635 case FUNCTIONS_DOMAIN:
4636 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4637 continue;
4638 break;
4639 case TYPES_DOMAIN:
4640 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4641 continue;
4642 break;
4643 case MODULES_DOMAIN:
4644 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4645 continue;
4646 break;
4647 default:
4648 break;
4649 }
4650 }
4651
4652 /* Don't crash on bad data. */
4653 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4654 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4655 {
4656 complaint (_(".gdb_index entry has bad CU index"
4657 " [in module %s]"),
4658 objfile_name (dwarf2_per_objfile->objfile));
4659 continue;
4660 }
4661
4662 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4663 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4664 expansion_notify);
4665 }
4666 }
4667
4668 /* If FILE_MATCHER is non-NULL, set all the
4669 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4670 that match FILE_MATCHER. */
4671
4672 static void
4673 dw_expand_symtabs_matching_file_matcher
4674 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4675 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4676 {
4677 if (file_matcher == NULL)
4678 return;
4679
4680 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4681 htab_eq_pointer,
4682 NULL, xcalloc, xfree));
4683 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4684 htab_eq_pointer,
4685 NULL, xcalloc, xfree));
4686
4687 /* The rule is CUs specify all the files, including those used by
4688 any TU, so there's no need to scan TUs here. */
4689
4690 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4691 {
4692 QUIT;
4693
4694 per_cu->v.quick->mark = 0;
4695
4696 /* We only need to look at symtabs not already expanded. */
4697 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4698 continue;
4699
4700 quick_file_names *file_data
4701 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4702 if (file_data == NULL)
4703 continue;
4704
4705 if (htab_find (visited_not_found.get (), file_data) != NULL)
4706 continue;
4707 else if (htab_find (visited_found.get (), file_data) != NULL)
4708 {
4709 per_cu->v.quick->mark = 1;
4710 continue;
4711 }
4712
4713 for (int j = 0; j < file_data->num_file_names; ++j)
4714 {
4715 const char *this_real_name;
4716
4717 if (file_matcher (file_data->file_names[j], false))
4718 {
4719 per_cu->v.quick->mark = 1;
4720 break;
4721 }
4722
4723 /* Before we invoke realpath, which can get expensive when many
4724 files are involved, do a quick comparison of the basenames. */
4725 if (!basenames_may_differ
4726 && !file_matcher (lbasename (file_data->file_names[j]),
4727 true))
4728 continue;
4729
4730 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4731 file_data, j);
4732 if (file_matcher (this_real_name, false))
4733 {
4734 per_cu->v.quick->mark = 1;
4735 break;
4736 }
4737 }
4738
4739 void **slot = htab_find_slot (per_cu->v.quick->mark
4740 ? visited_found.get ()
4741 : visited_not_found.get (),
4742 file_data, INSERT);
4743 *slot = file_data;
4744 }
4745 }
4746
4747 static void
4748 dw2_expand_symtabs_matching
4749 (struct objfile *objfile,
4750 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4751 const lookup_name_info *lookup_name,
4752 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4753 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4754 enum search_domain kind)
4755 {
4756 struct dwarf2_per_objfile *dwarf2_per_objfile
4757 = get_dwarf2_per_objfile (objfile);
4758
4759 /* index_table is NULL if OBJF_READNOW. */
4760 if (!dwarf2_per_objfile->per_bfd->index_table)
4761 return;
4762
4763 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4764
4765 if (symbol_matcher == NULL && lookup_name == NULL)
4766 {
4767 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4768 {
4769 QUIT;
4770
4771 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4772 file_matcher, expansion_notify);
4773 }
4774 return;
4775 }
4776
4777 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4778
4779 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4780 symbol_matcher,
4781 kind, [&] (offset_type idx)
4782 {
4783 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4784 expansion_notify, kind);
4785 return true;
4786 });
4787 }
4788
4789 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4790 symtab. */
4791
4792 static struct compunit_symtab *
4793 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4794 CORE_ADDR pc)
4795 {
4796 int i;
4797
4798 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4799 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4800 return cust;
4801
4802 if (cust->includes == NULL)
4803 return NULL;
4804
4805 for (i = 0; cust->includes[i]; ++i)
4806 {
4807 struct compunit_symtab *s = cust->includes[i];
4808
4809 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4810 if (s != NULL)
4811 return s;
4812 }
4813
4814 return NULL;
4815 }
4816
4817 static struct compunit_symtab *
4818 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4819 struct bound_minimal_symbol msymbol,
4820 CORE_ADDR pc,
4821 struct obj_section *section,
4822 int warn_if_readin)
4823 {
4824 struct dwarf2_per_cu_data *data;
4825 struct compunit_symtab *result;
4826
4827 if (!objfile->partial_symtabs->psymtabs_addrmap)
4828 return NULL;
4829
4830 CORE_ADDR baseaddr = objfile->text_section_offset ();
4831 data = (struct dwarf2_per_cu_data *) addrmap_find
4832 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4833 if (!data)
4834 return NULL;
4835
4836 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4837 if (warn_if_readin && per_objfile->symtab_set_p (data))
4838 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4839 paddress (objfile->arch (), pc));
4840
4841 result = recursively_find_pc_sect_compunit_symtab
4842 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4843
4844 gdb_assert (result != NULL);
4845 return result;
4846 }
4847
4848 static void
4849 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4850 void *data, int need_fullname)
4851 {
4852 struct dwarf2_per_objfile *dwarf2_per_objfile
4853 = get_dwarf2_per_objfile (objfile);
4854
4855 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4856 {
4857 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4858
4859 htab_up visited (htab_create_alloc (10,
4860 htab_hash_pointer, htab_eq_pointer,
4861 NULL, xcalloc, xfree));
4862
4863 /* The rule is CUs specify all the files, including those used
4864 by any TU, so there's no need to scan TUs here. We can
4865 ignore file names coming from already-expanded CUs. */
4866
4867 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4868 {
4869 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4870 {
4871 void **slot = htab_find_slot (visited.get (),
4872 per_cu->v.quick->file_names,
4873 INSERT);
4874
4875 *slot = per_cu->v.quick->file_names;
4876 }
4877 }
4878
4879 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4880 {
4881 /* We only need to look at symtabs not already expanded. */
4882 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4883 continue;
4884
4885 quick_file_names *file_data
4886 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4887 if (file_data == NULL)
4888 continue;
4889
4890 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4891 if (*slot)
4892 {
4893 /* Already visited. */
4894 continue;
4895 }
4896 *slot = file_data;
4897
4898 for (int j = 0; j < file_data->num_file_names; ++j)
4899 {
4900 const char *filename = file_data->file_names[j];
4901 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4902 }
4903 }
4904 }
4905
4906 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4907 {
4908 gdb::unique_xmalloc_ptr<char> this_real_name;
4909
4910 if (need_fullname)
4911 this_real_name = gdb_realpath (filename);
4912 (*fun) (filename, this_real_name.get (), data);
4913 });
4914 }
4915
4916 static int
4917 dw2_has_symbols (struct objfile *objfile)
4918 {
4919 return 1;
4920 }
4921
4922 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4923 {
4924 dw2_has_symbols,
4925 dw2_find_last_source_symtab,
4926 dw2_forget_cached_source_info,
4927 dw2_map_symtabs_matching_filename,
4928 dw2_lookup_symbol,
4929 NULL,
4930 dw2_print_stats,
4931 dw2_dump,
4932 dw2_expand_symtabs_for_function,
4933 dw2_expand_all_symtabs,
4934 dw2_expand_symtabs_with_fullname,
4935 dw2_map_matching_symbols,
4936 dw2_expand_symtabs_matching,
4937 dw2_find_pc_sect_compunit_symtab,
4938 NULL,
4939 dw2_map_symbol_filenames
4940 };
4941
4942 /* DWARF-5 debug_names reader. */
4943
4944 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4945 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4946
4947 /* A helper function that reads the .debug_names section in SECTION
4948 and fills in MAP. FILENAME is the name of the file containing the
4949 section; it is used for error reporting.
4950
4951 Returns true if all went well, false otherwise. */
4952
4953 static bool
4954 read_debug_names_from_section (struct objfile *objfile,
4955 const char *filename,
4956 struct dwarf2_section_info *section,
4957 mapped_debug_names &map)
4958 {
4959 if (section->empty ())
4960 return false;
4961
4962 /* Older elfutils strip versions could keep the section in the main
4963 executable while splitting it for the separate debug info file. */
4964 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4965 return false;
4966
4967 section->read (objfile);
4968
4969 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4970
4971 const gdb_byte *addr = section->buffer;
4972
4973 bfd *const abfd = section->get_bfd_owner ();
4974
4975 unsigned int bytes_read;
4976 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4977 addr += bytes_read;
4978
4979 map.dwarf5_is_dwarf64 = bytes_read != 4;
4980 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4981 if (bytes_read + length != section->size)
4982 {
4983 /* There may be multiple per-CU indices. */
4984 warning (_("Section .debug_names in %s length %s does not match "
4985 "section length %s, ignoring .debug_names."),
4986 filename, plongest (bytes_read + length),
4987 pulongest (section->size));
4988 return false;
4989 }
4990
4991 /* The version number. */
4992 uint16_t version = read_2_bytes (abfd, addr);
4993 addr += 2;
4994 if (version != 5)
4995 {
4996 warning (_("Section .debug_names in %s has unsupported version %d, "
4997 "ignoring .debug_names."),
4998 filename, version);
4999 return false;
5000 }
5001
5002 /* Padding. */
5003 uint16_t padding = read_2_bytes (abfd, addr);
5004 addr += 2;
5005 if (padding != 0)
5006 {
5007 warning (_("Section .debug_names in %s has unsupported padding %d, "
5008 "ignoring .debug_names."),
5009 filename, padding);
5010 return false;
5011 }
5012
5013 /* comp_unit_count - The number of CUs in the CU list. */
5014 map.cu_count = read_4_bytes (abfd, addr);
5015 addr += 4;
5016
5017 /* local_type_unit_count - The number of TUs in the local TU
5018 list. */
5019 map.tu_count = read_4_bytes (abfd, addr);
5020 addr += 4;
5021
5022 /* foreign_type_unit_count - The number of TUs in the foreign TU
5023 list. */
5024 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5025 addr += 4;
5026 if (foreign_tu_count != 0)
5027 {
5028 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5029 "ignoring .debug_names."),
5030 filename, static_cast<unsigned long> (foreign_tu_count));
5031 return false;
5032 }
5033
5034 /* bucket_count - The number of hash buckets in the hash lookup
5035 table. */
5036 map.bucket_count = read_4_bytes (abfd, addr);
5037 addr += 4;
5038
5039 /* name_count - The number of unique names in the index. */
5040 map.name_count = read_4_bytes (abfd, addr);
5041 addr += 4;
5042
5043 /* abbrev_table_size - The size in bytes of the abbreviations
5044 table. */
5045 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5046 addr += 4;
5047
5048 /* augmentation_string_size - The size in bytes of the augmentation
5049 string. This value is rounded up to a multiple of 4. */
5050 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5051 addr += 4;
5052 map.augmentation_is_gdb = ((augmentation_string_size
5053 == sizeof (dwarf5_augmentation))
5054 && memcmp (addr, dwarf5_augmentation,
5055 sizeof (dwarf5_augmentation)) == 0);
5056 augmentation_string_size += (-augmentation_string_size) & 3;
5057 addr += augmentation_string_size;
5058
5059 /* List of CUs */
5060 map.cu_table_reordered = addr;
5061 addr += map.cu_count * map.offset_size;
5062
5063 /* List of Local TUs */
5064 map.tu_table_reordered = addr;
5065 addr += map.tu_count * map.offset_size;
5066
5067 /* Hash Lookup Table */
5068 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5069 addr += map.bucket_count * 4;
5070 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5071 addr += map.name_count * 4;
5072
5073 /* Name Table */
5074 map.name_table_string_offs_reordered = addr;
5075 addr += map.name_count * map.offset_size;
5076 map.name_table_entry_offs_reordered = addr;
5077 addr += map.name_count * map.offset_size;
5078
5079 const gdb_byte *abbrev_table_start = addr;
5080 for (;;)
5081 {
5082 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5083 addr += bytes_read;
5084 if (index_num == 0)
5085 break;
5086
5087 const auto insertpair
5088 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5089 if (!insertpair.second)
5090 {
5091 warning (_("Section .debug_names in %s has duplicate index %s, "
5092 "ignoring .debug_names."),
5093 filename, pulongest (index_num));
5094 return false;
5095 }
5096 mapped_debug_names::index_val &indexval = insertpair.first->second;
5097 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5098 addr += bytes_read;
5099
5100 for (;;)
5101 {
5102 mapped_debug_names::index_val::attr attr;
5103 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5104 addr += bytes_read;
5105 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5106 addr += bytes_read;
5107 if (attr.form == DW_FORM_implicit_const)
5108 {
5109 attr.implicit_const = read_signed_leb128 (abfd, addr,
5110 &bytes_read);
5111 addr += bytes_read;
5112 }
5113 if (attr.dw_idx == 0 && attr.form == 0)
5114 break;
5115 indexval.attr_vec.push_back (std::move (attr));
5116 }
5117 }
5118 if (addr != abbrev_table_start + abbrev_table_size)
5119 {
5120 warning (_("Section .debug_names in %s has abbreviation_table "
5121 "of size %s vs. written as %u, ignoring .debug_names."),
5122 filename, plongest (addr - abbrev_table_start),
5123 abbrev_table_size);
5124 return false;
5125 }
5126 map.entry_pool = addr;
5127
5128 return true;
5129 }
5130
5131 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5132 list. */
5133
5134 static void
5135 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5136 const mapped_debug_names &map,
5137 dwarf2_section_info &section,
5138 bool is_dwz)
5139 {
5140 if (!map.augmentation_is_gdb)
5141 {
5142 for (uint32_t i = 0; i < map.cu_count; ++i)
5143 {
5144 sect_offset sect_off
5145 = (sect_offset) (extract_unsigned_integer
5146 (map.cu_table_reordered + i * map.offset_size,
5147 map.offset_size,
5148 map.dwarf5_byte_order));
5149 /* We don't know the length of the CU, because the CU list in a
5150 .debug_names index can be incomplete, so we can't use the start of
5151 the next CU as end of this CU. We create the CUs here with length 0,
5152 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5153 dwarf2_per_cu_data *per_cu
5154 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5155 sect_off, 0);
5156 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5157 }
5158 }
5159
5160 sect_offset sect_off_prev;
5161 for (uint32_t i = 0; i <= map.cu_count; ++i)
5162 {
5163 sect_offset sect_off_next;
5164 if (i < map.cu_count)
5165 {
5166 sect_off_next
5167 = (sect_offset) (extract_unsigned_integer
5168 (map.cu_table_reordered + i * map.offset_size,
5169 map.offset_size,
5170 map.dwarf5_byte_order));
5171 }
5172 else
5173 sect_off_next = (sect_offset) section.size;
5174 if (i >= 1)
5175 {
5176 const ULONGEST length = sect_off_next - sect_off_prev;
5177 dwarf2_per_cu_data *per_cu
5178 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5179 sect_off_prev, length);
5180 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5181 }
5182 sect_off_prev = sect_off_next;
5183 }
5184 }
5185
5186 /* Read the CU list from the mapped index, and use it to create all
5187 the CU objects for this dwarf2_per_objfile. */
5188
5189 static void
5190 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5191 const mapped_debug_names &map,
5192 const mapped_debug_names &dwz_map)
5193 {
5194 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5195 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5196
5197 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5198 dwarf2_per_objfile->per_bfd->info,
5199 false /* is_dwz */);
5200
5201 if (dwz_map.cu_count == 0)
5202 return;
5203
5204 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5205 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5206 true /* is_dwz */);
5207 }
5208
5209 /* Read .debug_names. If everything went ok, initialize the "quick"
5210 elements of all the CUs and return true. Otherwise, return false. */
5211
5212 static bool
5213 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5214 {
5215 std::unique_ptr<mapped_debug_names> map
5216 (new mapped_debug_names (dwarf2_per_objfile));
5217 mapped_debug_names dwz_map (dwarf2_per_objfile);
5218 struct objfile *objfile = dwarf2_per_objfile->objfile;
5219
5220 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5221 &dwarf2_per_objfile->per_bfd->debug_names,
5222 *map))
5223 return false;
5224
5225 /* Don't use the index if it's empty. */
5226 if (map->name_count == 0)
5227 return false;
5228
5229 /* If there is a .dwz file, read it so we can get its CU list as
5230 well. */
5231 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5232 if (dwz != NULL)
5233 {
5234 if (!read_debug_names_from_section (objfile,
5235 bfd_get_filename (dwz->dwz_bfd.get ()),
5236 &dwz->debug_names, dwz_map))
5237 {
5238 warning (_("could not read '.debug_names' section from %s; skipping"),
5239 bfd_get_filename (dwz->dwz_bfd.get ()));
5240 return false;
5241 }
5242 }
5243
5244 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5245
5246 if (map->tu_count != 0)
5247 {
5248 /* We can only handle a single .debug_types when we have an
5249 index. */
5250 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5251 return false;
5252
5253 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5254
5255 create_signatured_type_table_from_debug_names
5256 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5257 }
5258
5259 create_addrmap_from_aranges (dwarf2_per_objfile,
5260 &dwarf2_per_objfile->per_bfd->debug_aranges);
5261
5262 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5263 dwarf2_per_objfile->per_bfd->using_index = 1;
5264 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5265 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5266
5267 return true;
5268 }
5269
5270 /* Type used to manage iterating over all CUs looking for a symbol for
5271 .debug_names. */
5272
5273 class dw2_debug_names_iterator
5274 {
5275 public:
5276 dw2_debug_names_iterator (const mapped_debug_names &map,
5277 gdb::optional<block_enum> block_index,
5278 domain_enum domain,
5279 const char *name)
5280 : m_map (map), m_block_index (block_index), m_domain (domain),
5281 m_addr (find_vec_in_debug_names (map, name))
5282 {}
5283
5284 dw2_debug_names_iterator (const mapped_debug_names &map,
5285 search_domain search, uint32_t namei)
5286 : m_map (map),
5287 m_search (search),
5288 m_addr (find_vec_in_debug_names (map, namei))
5289 {}
5290
5291 dw2_debug_names_iterator (const mapped_debug_names &map,
5292 block_enum block_index, domain_enum domain,
5293 uint32_t namei)
5294 : m_map (map), m_block_index (block_index), m_domain (domain),
5295 m_addr (find_vec_in_debug_names (map, namei))
5296 {}
5297
5298 /* Return the next matching CU or NULL if there are no more. */
5299 dwarf2_per_cu_data *next ();
5300
5301 private:
5302 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5303 const char *name);
5304 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5305 uint32_t namei);
5306
5307 /* The internalized form of .debug_names. */
5308 const mapped_debug_names &m_map;
5309
5310 /* If set, only look for symbols that match that block. Valid values are
5311 GLOBAL_BLOCK and STATIC_BLOCK. */
5312 const gdb::optional<block_enum> m_block_index;
5313
5314 /* The kind of symbol we're looking for. */
5315 const domain_enum m_domain = UNDEF_DOMAIN;
5316 const search_domain m_search = ALL_DOMAIN;
5317
5318 /* The list of CUs from the index entry of the symbol, or NULL if
5319 not found. */
5320 const gdb_byte *m_addr;
5321 };
5322
5323 const char *
5324 mapped_debug_names::namei_to_name (uint32_t namei) const
5325 {
5326 const ULONGEST namei_string_offs
5327 = extract_unsigned_integer ((name_table_string_offs_reordered
5328 + namei * offset_size),
5329 offset_size,
5330 dwarf5_byte_order);
5331 return read_indirect_string_at_offset (dwarf2_per_objfile,
5332 namei_string_offs);
5333 }
5334
5335 /* Find a slot in .debug_names for the object named NAME. If NAME is
5336 found, return pointer to its pool data. If NAME cannot be found,
5337 return NULL. */
5338
5339 const gdb_byte *
5340 dw2_debug_names_iterator::find_vec_in_debug_names
5341 (const mapped_debug_names &map, const char *name)
5342 {
5343 int (*cmp) (const char *, const char *);
5344
5345 gdb::unique_xmalloc_ptr<char> without_params;
5346 if (current_language->la_language == language_cplus
5347 || current_language->la_language == language_fortran
5348 || current_language->la_language == language_d)
5349 {
5350 /* NAME is already canonical. Drop any qualifiers as
5351 .debug_names does not contain any. */
5352
5353 if (strchr (name, '(') != NULL)
5354 {
5355 without_params = cp_remove_params (name);
5356 if (without_params != NULL)
5357 name = without_params.get ();
5358 }
5359 }
5360
5361 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5362
5363 const uint32_t full_hash = dwarf5_djb_hash (name);
5364 uint32_t namei
5365 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5366 (map.bucket_table_reordered
5367 + (full_hash % map.bucket_count)), 4,
5368 map.dwarf5_byte_order);
5369 if (namei == 0)
5370 return NULL;
5371 --namei;
5372 if (namei >= map.name_count)
5373 {
5374 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5375 "[in module %s]"),
5376 namei, map.name_count,
5377 objfile_name (map.dwarf2_per_objfile->objfile));
5378 return NULL;
5379 }
5380
5381 for (;;)
5382 {
5383 const uint32_t namei_full_hash
5384 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5385 (map.hash_table_reordered + namei), 4,
5386 map.dwarf5_byte_order);
5387 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5388 return NULL;
5389
5390 if (full_hash == namei_full_hash)
5391 {
5392 const char *const namei_string = map.namei_to_name (namei);
5393
5394 #if 0 /* An expensive sanity check. */
5395 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5396 {
5397 complaint (_("Wrong .debug_names hash for string at index %u "
5398 "[in module %s]"),
5399 namei, objfile_name (dwarf2_per_objfile->objfile));
5400 return NULL;
5401 }
5402 #endif
5403
5404 if (cmp (namei_string, name) == 0)
5405 {
5406 const ULONGEST namei_entry_offs
5407 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5408 + namei * map.offset_size),
5409 map.offset_size, map.dwarf5_byte_order);
5410 return map.entry_pool + namei_entry_offs;
5411 }
5412 }
5413
5414 ++namei;
5415 if (namei >= map.name_count)
5416 return NULL;
5417 }
5418 }
5419
5420 const gdb_byte *
5421 dw2_debug_names_iterator::find_vec_in_debug_names
5422 (const mapped_debug_names &map, uint32_t namei)
5423 {
5424 if (namei >= map.name_count)
5425 {
5426 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5427 "[in module %s]"),
5428 namei, map.name_count,
5429 objfile_name (map.dwarf2_per_objfile->objfile));
5430 return NULL;
5431 }
5432
5433 const ULONGEST namei_entry_offs
5434 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5435 + namei * map.offset_size),
5436 map.offset_size, map.dwarf5_byte_order);
5437 return map.entry_pool + namei_entry_offs;
5438 }
5439
5440 /* See dw2_debug_names_iterator. */
5441
5442 dwarf2_per_cu_data *
5443 dw2_debug_names_iterator::next ()
5444 {
5445 if (m_addr == NULL)
5446 return NULL;
5447
5448 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5449 struct objfile *objfile = dwarf2_per_objfile->objfile;
5450 bfd *const abfd = objfile->obfd;
5451
5452 again:
5453
5454 unsigned int bytes_read;
5455 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5456 m_addr += bytes_read;
5457 if (abbrev == 0)
5458 return NULL;
5459
5460 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5461 if (indexval_it == m_map.abbrev_map.cend ())
5462 {
5463 complaint (_("Wrong .debug_names undefined abbrev code %s "
5464 "[in module %s]"),
5465 pulongest (abbrev), objfile_name (objfile));
5466 return NULL;
5467 }
5468 const mapped_debug_names::index_val &indexval = indexval_it->second;
5469 enum class symbol_linkage {
5470 unknown,
5471 static_,
5472 extern_,
5473 } symbol_linkage_ = symbol_linkage::unknown;
5474 dwarf2_per_cu_data *per_cu = NULL;
5475 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5476 {
5477 ULONGEST ull;
5478 switch (attr.form)
5479 {
5480 case DW_FORM_implicit_const:
5481 ull = attr.implicit_const;
5482 break;
5483 case DW_FORM_flag_present:
5484 ull = 1;
5485 break;
5486 case DW_FORM_udata:
5487 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5488 m_addr += bytes_read;
5489 break;
5490 case DW_FORM_ref4:
5491 ull = read_4_bytes (abfd, m_addr);
5492 m_addr += 4;
5493 break;
5494 case DW_FORM_ref8:
5495 ull = read_8_bytes (abfd, m_addr);
5496 m_addr += 8;
5497 break;
5498 case DW_FORM_ref_sig8:
5499 ull = read_8_bytes (abfd, m_addr);
5500 m_addr += 8;
5501 break;
5502 default:
5503 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5504 dwarf_form_name (attr.form),
5505 objfile_name (objfile));
5506 return NULL;
5507 }
5508 switch (attr.dw_idx)
5509 {
5510 case DW_IDX_compile_unit:
5511 /* Don't crash on bad data. */
5512 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5513 {
5514 complaint (_(".debug_names entry has bad CU index %s"
5515 " [in module %s]"),
5516 pulongest (ull),
5517 objfile_name (dwarf2_per_objfile->objfile));
5518 continue;
5519 }
5520 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5521 break;
5522 case DW_IDX_type_unit:
5523 /* Don't crash on bad data. */
5524 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5525 {
5526 complaint (_(".debug_names entry has bad TU index %s"
5527 " [in module %s]"),
5528 pulongest (ull),
5529 objfile_name (dwarf2_per_objfile->objfile));
5530 continue;
5531 }
5532 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5533 break;
5534 case DW_IDX_die_offset:
5535 /* In a per-CU index (as opposed to a per-module index), index
5536 entries without CU attribute implicitly refer to the single CU. */
5537 if (per_cu == NULL)
5538 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5539 break;
5540 case DW_IDX_GNU_internal:
5541 if (!m_map.augmentation_is_gdb)
5542 break;
5543 symbol_linkage_ = symbol_linkage::static_;
5544 break;
5545 case DW_IDX_GNU_external:
5546 if (!m_map.augmentation_is_gdb)
5547 break;
5548 symbol_linkage_ = symbol_linkage::extern_;
5549 break;
5550 }
5551 }
5552
5553 /* Skip if already read in. */
5554 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5555 goto again;
5556
5557 /* Check static vs global. */
5558 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5559 {
5560 const bool want_static = *m_block_index == STATIC_BLOCK;
5561 const bool symbol_is_static =
5562 symbol_linkage_ == symbol_linkage::static_;
5563 if (want_static != symbol_is_static)
5564 goto again;
5565 }
5566
5567 /* Match dw2_symtab_iter_next, symbol_kind
5568 and debug_names::psymbol_tag. */
5569 switch (m_domain)
5570 {
5571 case VAR_DOMAIN:
5572 switch (indexval.dwarf_tag)
5573 {
5574 case DW_TAG_variable:
5575 case DW_TAG_subprogram:
5576 /* Some types are also in VAR_DOMAIN. */
5577 case DW_TAG_typedef:
5578 case DW_TAG_structure_type:
5579 break;
5580 default:
5581 goto again;
5582 }
5583 break;
5584 case STRUCT_DOMAIN:
5585 switch (indexval.dwarf_tag)
5586 {
5587 case DW_TAG_typedef:
5588 case DW_TAG_structure_type:
5589 break;
5590 default:
5591 goto again;
5592 }
5593 break;
5594 case LABEL_DOMAIN:
5595 switch (indexval.dwarf_tag)
5596 {
5597 case 0:
5598 case DW_TAG_variable:
5599 break;
5600 default:
5601 goto again;
5602 }
5603 break;
5604 case MODULE_DOMAIN:
5605 switch (indexval.dwarf_tag)
5606 {
5607 case DW_TAG_module:
5608 break;
5609 default:
5610 goto again;
5611 }
5612 break;
5613 default:
5614 break;
5615 }
5616
5617 /* Match dw2_expand_symtabs_matching, symbol_kind and
5618 debug_names::psymbol_tag. */
5619 switch (m_search)
5620 {
5621 case VARIABLES_DOMAIN:
5622 switch (indexval.dwarf_tag)
5623 {
5624 case DW_TAG_variable:
5625 break;
5626 default:
5627 goto again;
5628 }
5629 break;
5630 case FUNCTIONS_DOMAIN:
5631 switch (indexval.dwarf_tag)
5632 {
5633 case DW_TAG_subprogram:
5634 break;
5635 default:
5636 goto again;
5637 }
5638 break;
5639 case TYPES_DOMAIN:
5640 switch (indexval.dwarf_tag)
5641 {
5642 case DW_TAG_typedef:
5643 case DW_TAG_structure_type:
5644 break;
5645 default:
5646 goto again;
5647 }
5648 break;
5649 case MODULES_DOMAIN:
5650 switch (indexval.dwarf_tag)
5651 {
5652 case DW_TAG_module:
5653 break;
5654 default:
5655 goto again;
5656 }
5657 default:
5658 break;
5659 }
5660
5661 return per_cu;
5662 }
5663
5664 static struct compunit_symtab *
5665 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5666 const char *name, domain_enum domain)
5667 {
5668 struct dwarf2_per_objfile *dwarf2_per_objfile
5669 = get_dwarf2_per_objfile (objfile);
5670
5671 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5672 if (!mapp)
5673 {
5674 /* index is NULL if OBJF_READNOW. */
5675 return NULL;
5676 }
5677 const auto &map = *mapp;
5678
5679 dw2_debug_names_iterator iter (map, block_index, domain, name);
5680
5681 struct compunit_symtab *stab_best = NULL;
5682 struct dwarf2_per_cu_data *per_cu;
5683 while ((per_cu = iter.next ()) != NULL)
5684 {
5685 struct symbol *sym, *with_opaque = NULL;
5686 compunit_symtab *stab
5687 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5688 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5689 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5690
5691 sym = block_find_symbol (block, name, domain,
5692 block_find_non_opaque_type_preferred,
5693 &with_opaque);
5694
5695 /* Some caution must be observed with overloaded functions and
5696 methods, since the index will not contain any overload
5697 information (but NAME might contain it). */
5698
5699 if (sym != NULL
5700 && strcmp_iw (sym->search_name (), name) == 0)
5701 return stab;
5702 if (with_opaque != NULL
5703 && strcmp_iw (with_opaque->search_name (), name) == 0)
5704 stab_best = stab;
5705
5706 /* Keep looking through other CUs. */
5707 }
5708
5709 return stab_best;
5710 }
5711
5712 /* This dumps minimal information about .debug_names. It is called
5713 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5714 uses this to verify that .debug_names has been loaded. */
5715
5716 static void
5717 dw2_debug_names_dump (struct objfile *objfile)
5718 {
5719 struct dwarf2_per_objfile *dwarf2_per_objfile
5720 = get_dwarf2_per_objfile (objfile);
5721
5722 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5723 printf_filtered (".debug_names:");
5724 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5725 printf_filtered (" exists\n");
5726 else
5727 printf_filtered (" faked for \"readnow\"\n");
5728 printf_filtered ("\n");
5729 }
5730
5731 static void
5732 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5733 const char *func_name)
5734 {
5735 struct dwarf2_per_objfile *dwarf2_per_objfile
5736 = get_dwarf2_per_objfile (objfile);
5737
5738 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5739 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5740 {
5741 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5742
5743 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5744
5745 struct dwarf2_per_cu_data *per_cu;
5746 while ((per_cu = iter.next ()) != NULL)
5747 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5748 }
5749 }
5750
5751 static void
5752 dw2_debug_names_map_matching_symbols
5753 (struct objfile *objfile,
5754 const lookup_name_info &name, domain_enum domain,
5755 int global,
5756 gdb::function_view<symbol_found_callback_ftype> callback,
5757 symbol_compare_ftype *ordered_compare)
5758 {
5759 struct dwarf2_per_objfile *dwarf2_per_objfile
5760 = get_dwarf2_per_objfile (objfile);
5761
5762 /* debug_names_table is NULL if OBJF_READNOW. */
5763 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5764 return;
5765
5766 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5767 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5768
5769 const char *match_name = name.ada ().lookup_name ().c_str ();
5770 auto matcher = [&] (const char *symname)
5771 {
5772 if (ordered_compare == nullptr)
5773 return true;
5774 return ordered_compare (symname, match_name) == 0;
5775 };
5776
5777 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5778 [&] (offset_type namei)
5779 {
5780 /* The name was matched, now expand corresponding CUs that were
5781 marked. */
5782 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5783
5784 struct dwarf2_per_cu_data *per_cu;
5785 while ((per_cu = iter.next ()) != NULL)
5786 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5787 nullptr);
5788 return true;
5789 });
5790
5791 /* It's a shame we couldn't do this inside the
5792 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5793 that have already been expanded. Instead, this loop matches what
5794 the psymtab code does. */
5795 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5796 {
5797 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5798 if (symtab != nullptr)
5799 {
5800 const struct block *block
5801 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5802 if (!iterate_over_symbols_terminated (block, name,
5803 domain, callback))
5804 break;
5805 }
5806 }
5807 }
5808
5809 static void
5810 dw2_debug_names_expand_symtabs_matching
5811 (struct objfile *objfile,
5812 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5813 const lookup_name_info *lookup_name,
5814 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5815 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5816 enum search_domain kind)
5817 {
5818 struct dwarf2_per_objfile *dwarf2_per_objfile
5819 = get_dwarf2_per_objfile (objfile);
5820
5821 /* debug_names_table is NULL if OBJF_READNOW. */
5822 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5823 return;
5824
5825 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5826
5827 if (symbol_matcher == NULL && lookup_name == NULL)
5828 {
5829 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5830 {
5831 QUIT;
5832
5833 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5834 file_matcher, expansion_notify);
5835 }
5836 return;
5837 }
5838
5839 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5840
5841 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5842 symbol_matcher,
5843 kind, [&] (offset_type namei)
5844 {
5845 /* The name was matched, now expand corresponding CUs that were
5846 marked. */
5847 dw2_debug_names_iterator iter (map, kind, namei);
5848
5849 struct dwarf2_per_cu_data *per_cu;
5850 while ((per_cu = iter.next ()) != NULL)
5851 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5852 file_matcher, expansion_notify);
5853 return true;
5854 });
5855 }
5856
5857 const struct quick_symbol_functions dwarf2_debug_names_functions =
5858 {
5859 dw2_has_symbols,
5860 dw2_find_last_source_symtab,
5861 dw2_forget_cached_source_info,
5862 dw2_map_symtabs_matching_filename,
5863 dw2_debug_names_lookup_symbol,
5864 NULL,
5865 dw2_print_stats,
5866 dw2_debug_names_dump,
5867 dw2_debug_names_expand_symtabs_for_function,
5868 dw2_expand_all_symtabs,
5869 dw2_expand_symtabs_with_fullname,
5870 dw2_debug_names_map_matching_symbols,
5871 dw2_debug_names_expand_symtabs_matching,
5872 dw2_find_pc_sect_compunit_symtab,
5873 NULL,
5874 dw2_map_symbol_filenames
5875 };
5876
5877 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5878 to either a dwarf2_per_bfd or dwz_file object. */
5879
5880 template <typename T>
5881 static gdb::array_view<const gdb_byte>
5882 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5883 {
5884 dwarf2_section_info *section = &section_owner->gdb_index;
5885
5886 if (section->empty ())
5887 return {};
5888
5889 /* Older elfutils strip versions could keep the section in the main
5890 executable while splitting it for the separate debug info file. */
5891 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5892 return {};
5893
5894 section->read (obj);
5895
5896 /* dwarf2_section_info::size is a bfd_size_type, while
5897 gdb::array_view works with size_t. On 32-bit hosts, with
5898 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5899 is 32-bit. So we need an explicit narrowing conversion here.
5900 This is fine, because it's impossible to allocate or mmap an
5901 array/buffer larger than what size_t can represent. */
5902 return gdb::make_array_view (section->buffer, section->size);
5903 }
5904
5905 /* Lookup the index cache for the contents of the index associated to
5906 DWARF2_OBJ. */
5907
5908 static gdb::array_view<const gdb_byte>
5909 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5910 {
5911 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5912 if (build_id == nullptr)
5913 return {};
5914
5915 return global_index_cache.lookup_gdb_index (build_id,
5916 &dwarf2_per_bfd->index_cache_res);
5917 }
5918
5919 /* Same as the above, but for DWZ. */
5920
5921 static gdb::array_view<const gdb_byte>
5922 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5923 {
5924 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5925 if (build_id == nullptr)
5926 return {};
5927
5928 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5929 }
5930
5931 /* See symfile.h. */
5932
5933 bool
5934 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5935 {
5936 struct dwarf2_per_objfile *dwarf2_per_objfile
5937 = get_dwarf2_per_objfile (objfile);
5938
5939 /* If we're about to read full symbols, don't bother with the
5940 indices. In this case we also don't care if some other debug
5941 format is making psymtabs, because they are all about to be
5942 expanded anyway. */
5943 if ((objfile->flags & OBJF_READNOW))
5944 {
5945 dwarf2_per_objfile->per_bfd->using_index = 1;
5946 create_all_comp_units (dwarf2_per_objfile);
5947 create_all_type_units (dwarf2_per_objfile);
5948 dwarf2_per_objfile->per_bfd->quick_file_names_table
5949 = create_quick_file_names_table
5950 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5951 dwarf2_per_objfile->resize_symtabs ();
5952
5953 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5954 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5955 {
5956 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5957
5958 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5959 struct dwarf2_per_cu_quick_data);
5960 }
5961
5962 /* Return 1 so that gdb sees the "quick" functions. However,
5963 these functions will be no-ops because we will have expanded
5964 all symtabs. */
5965 *index_kind = dw_index_kind::GDB_INDEX;
5966 return true;
5967 }
5968
5969 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5970 {
5971 *index_kind = dw_index_kind::DEBUG_NAMES;
5972 dwarf2_per_objfile->resize_symtabs ();
5973 return true;
5974 }
5975
5976 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5977 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5978 get_gdb_index_contents_from_section<dwz_file>))
5979 {
5980 *index_kind = dw_index_kind::GDB_INDEX;
5981 dwarf2_per_objfile->resize_symtabs ();
5982 return true;
5983 }
5984
5985 /* ... otherwise, try to find the index in the index cache. */
5986 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5987 get_gdb_index_contents_from_cache,
5988 get_gdb_index_contents_from_cache_dwz))
5989 {
5990 global_index_cache.hit ();
5991 *index_kind = dw_index_kind::GDB_INDEX;
5992 dwarf2_per_objfile->resize_symtabs ();
5993 return true;
5994 }
5995
5996 global_index_cache.miss ();
5997 return false;
5998 }
5999
6000 \f
6001
6002 /* Build a partial symbol table. */
6003
6004 void
6005 dwarf2_build_psymtabs (struct objfile *objfile)
6006 {
6007 struct dwarf2_per_objfile *dwarf2_per_objfile
6008 = get_dwarf2_per_objfile (objfile);
6009
6010 init_psymbol_list (objfile, 1024);
6011
6012 try
6013 {
6014 /* This isn't really ideal: all the data we allocate on the
6015 objfile's obstack is still uselessly kept around. However,
6016 freeing it seems unsafe. */
6017 psymtab_discarder psymtabs (objfile);
6018 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6019 psymtabs.keep ();
6020
6021 dwarf2_per_objfile->resize_symtabs ();
6022
6023 /* (maybe) store an index in the cache. */
6024 global_index_cache.store (dwarf2_per_objfile);
6025 }
6026 catch (const gdb_exception_error &except)
6027 {
6028 exception_print (gdb_stderr, except);
6029 }
6030 }
6031
6032 /* Find the base address of the compilation unit for range lists and
6033 location lists. It will normally be specified by DW_AT_low_pc.
6034 In DWARF-3 draft 4, the base address could be overridden by
6035 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6036 compilation units with discontinuous ranges. */
6037
6038 static void
6039 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6040 {
6041 struct attribute *attr;
6042
6043 cu->base_address.reset ();
6044
6045 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6046 if (attr != nullptr)
6047 cu->base_address = attr->value_as_address ();
6048 else
6049 {
6050 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6051 if (attr != nullptr)
6052 cu->base_address = attr->value_as_address ();
6053 }
6054 }
6055
6056 /* Helper function that returns the proper abbrev section for
6057 THIS_CU. */
6058
6059 static struct dwarf2_section_info *
6060 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6061 {
6062 struct dwarf2_section_info *abbrev;
6063 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6064
6065 if (this_cu->is_dwz)
6066 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6067 else
6068 abbrev = &per_bfd->abbrev;
6069
6070 return abbrev;
6071 }
6072
6073 /* Fetch the abbreviation table offset from a comp or type unit header. */
6074
6075 static sect_offset
6076 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6077 struct dwarf2_section_info *section,
6078 sect_offset sect_off)
6079 {
6080 bfd *abfd = section->get_bfd_owner ();
6081 const gdb_byte *info_ptr;
6082 unsigned int initial_length_size, offset_size;
6083 uint16_t version;
6084
6085 section->read (dwarf2_per_objfile->objfile);
6086 info_ptr = section->buffer + to_underlying (sect_off);
6087 read_initial_length (abfd, info_ptr, &initial_length_size);
6088 offset_size = initial_length_size == 4 ? 4 : 8;
6089 info_ptr += initial_length_size;
6090
6091 version = read_2_bytes (abfd, info_ptr);
6092 info_ptr += 2;
6093 if (version >= 5)
6094 {
6095 /* Skip unit type and address size. */
6096 info_ptr += 2;
6097 }
6098
6099 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6100 }
6101
6102 /* A partial symtab that is used only for include files. */
6103 struct dwarf2_include_psymtab : public partial_symtab
6104 {
6105 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6106 : partial_symtab (filename, objfile)
6107 {
6108 }
6109
6110 void read_symtab (struct objfile *objfile) override
6111 {
6112 /* It's an include file, no symbols to read for it.
6113 Everything is in the includer symtab. */
6114
6115 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6116 expansion of the includer psymtab. We use the dependencies[0] field to
6117 model the includer. But if we go the regular route of calling
6118 expand_psymtab here, and having expand_psymtab call expand_dependencies
6119 to expand the includer, we'll only use expand_psymtab on the includer
6120 (making it a non-toplevel psymtab), while if we expand the includer via
6121 another path, we'll use read_symtab (making it a toplevel psymtab).
6122 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6123 psymtab, and trigger read_symtab on the includer here directly. */
6124 includer ()->read_symtab (objfile);
6125 }
6126
6127 void expand_psymtab (struct objfile *objfile) override
6128 {
6129 /* This is not called by read_symtab, and should not be called by any
6130 expand_dependencies. */
6131 gdb_assert (false);
6132 }
6133
6134 bool readin_p (struct objfile *objfile) const override
6135 {
6136 return includer ()->readin_p (objfile);
6137 }
6138
6139 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6140 {
6141 return nullptr;
6142 }
6143
6144 private:
6145 partial_symtab *includer () const
6146 {
6147 /* An include psymtab has exactly one dependency: the psymtab that
6148 includes it. */
6149 gdb_assert (this->number_of_dependencies == 1);
6150 return this->dependencies[0];
6151 }
6152 };
6153
6154 /* Allocate a new partial symtab for file named NAME and mark this new
6155 partial symtab as being an include of PST. */
6156
6157 static void
6158 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6159 struct objfile *objfile)
6160 {
6161 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6162
6163 if (!IS_ABSOLUTE_PATH (subpst->filename))
6164 subpst->dirname = pst->dirname;
6165
6166 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6167 subpst->dependencies[0] = pst;
6168 subpst->number_of_dependencies = 1;
6169 }
6170
6171 /* Read the Line Number Program data and extract the list of files
6172 included by the source file represented by PST. Build an include
6173 partial symtab for each of these included files. */
6174
6175 static void
6176 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6177 struct die_info *die,
6178 dwarf2_psymtab *pst)
6179 {
6180 line_header_up lh;
6181 struct attribute *attr;
6182
6183 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6184 if (attr != nullptr)
6185 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6186 if (lh == NULL)
6187 return; /* No linetable, so no includes. */
6188
6189 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6190 that we pass in the raw text_low here; that is ok because we're
6191 only decoding the line table to make include partial symtabs, and
6192 so the addresses aren't really used. */
6193 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6194 pst->raw_text_low (), 1);
6195 }
6196
6197 static hashval_t
6198 hash_signatured_type (const void *item)
6199 {
6200 const struct signatured_type *sig_type
6201 = (const struct signatured_type *) item;
6202
6203 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6204 return sig_type->signature;
6205 }
6206
6207 static int
6208 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6209 {
6210 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6211 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6212
6213 return lhs->signature == rhs->signature;
6214 }
6215
6216 /* Allocate a hash table for signatured types. */
6217
6218 static htab_up
6219 allocate_signatured_type_table ()
6220 {
6221 return htab_up (htab_create_alloc (41,
6222 hash_signatured_type,
6223 eq_signatured_type,
6224 NULL, xcalloc, xfree));
6225 }
6226
6227 /* A helper function to add a signatured type CU to a table. */
6228
6229 static int
6230 add_signatured_type_cu_to_table (void **slot, void *datum)
6231 {
6232 struct signatured_type *sigt = (struct signatured_type *) *slot;
6233 std::vector<signatured_type *> *all_type_units
6234 = (std::vector<signatured_type *> *) datum;
6235
6236 all_type_units->push_back (sigt);
6237
6238 return 1;
6239 }
6240
6241 /* A helper for create_debug_types_hash_table. Read types from SECTION
6242 and fill them into TYPES_HTAB. It will process only type units,
6243 therefore DW_UT_type. */
6244
6245 static void
6246 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6247 struct dwo_file *dwo_file,
6248 dwarf2_section_info *section, htab_up &types_htab,
6249 rcuh_kind section_kind)
6250 {
6251 struct objfile *objfile = dwarf2_per_objfile->objfile;
6252 struct dwarf2_section_info *abbrev_section;
6253 bfd *abfd;
6254 const gdb_byte *info_ptr, *end_ptr;
6255
6256 abbrev_section = (dwo_file != NULL
6257 ? &dwo_file->sections.abbrev
6258 : &dwarf2_per_objfile->per_bfd->abbrev);
6259
6260 if (dwarf_read_debug)
6261 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6262 section->get_name (),
6263 abbrev_section->get_file_name ());
6264
6265 section->read (objfile);
6266 info_ptr = section->buffer;
6267
6268 if (info_ptr == NULL)
6269 return;
6270
6271 /* We can't set abfd until now because the section may be empty or
6272 not present, in which case the bfd is unknown. */
6273 abfd = section->get_bfd_owner ();
6274
6275 /* We don't use cutu_reader here because we don't need to read
6276 any dies: the signature is in the header. */
6277
6278 end_ptr = info_ptr + section->size;
6279 while (info_ptr < end_ptr)
6280 {
6281 struct signatured_type *sig_type;
6282 struct dwo_unit *dwo_tu;
6283 void **slot;
6284 const gdb_byte *ptr = info_ptr;
6285 struct comp_unit_head header;
6286 unsigned int length;
6287
6288 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6289
6290 /* Initialize it due to a false compiler warning. */
6291 header.signature = -1;
6292 header.type_cu_offset_in_tu = (cu_offset) -1;
6293
6294 /* We need to read the type's signature in order to build the hash
6295 table, but we don't need anything else just yet. */
6296
6297 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6298 abbrev_section, ptr, section_kind);
6299
6300 length = header.get_length ();
6301
6302 /* Skip dummy type units. */
6303 if (ptr >= info_ptr + length
6304 || peek_abbrev_code (abfd, ptr) == 0
6305 || header.unit_type != DW_UT_type)
6306 {
6307 info_ptr += length;
6308 continue;
6309 }
6310
6311 if (types_htab == NULL)
6312 {
6313 if (dwo_file)
6314 types_htab = allocate_dwo_unit_table ();
6315 else
6316 types_htab = allocate_signatured_type_table ();
6317 }
6318
6319 if (dwo_file)
6320 {
6321 sig_type = NULL;
6322 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6323 struct dwo_unit);
6324 dwo_tu->dwo_file = dwo_file;
6325 dwo_tu->signature = header.signature;
6326 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6327 dwo_tu->section = section;
6328 dwo_tu->sect_off = sect_off;
6329 dwo_tu->length = length;
6330 }
6331 else
6332 {
6333 /* N.B.: type_offset is not usable if this type uses a DWO file.
6334 The real type_offset is in the DWO file. */
6335 dwo_tu = NULL;
6336 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6337 sig_type->signature = header.signature;
6338 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6339 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6340 sig_type->per_cu.is_debug_types = 1;
6341 sig_type->per_cu.section = section;
6342 sig_type->per_cu.sect_off = sect_off;
6343 sig_type->per_cu.length = length;
6344 }
6345
6346 slot = htab_find_slot (types_htab.get (),
6347 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6348 INSERT);
6349 gdb_assert (slot != NULL);
6350 if (*slot != NULL)
6351 {
6352 sect_offset dup_sect_off;
6353
6354 if (dwo_file)
6355 {
6356 const struct dwo_unit *dup_tu
6357 = (const struct dwo_unit *) *slot;
6358
6359 dup_sect_off = dup_tu->sect_off;
6360 }
6361 else
6362 {
6363 const struct signatured_type *dup_tu
6364 = (const struct signatured_type *) *slot;
6365
6366 dup_sect_off = dup_tu->per_cu.sect_off;
6367 }
6368
6369 complaint (_("debug type entry at offset %s is duplicate to"
6370 " the entry at offset %s, signature %s"),
6371 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6372 hex_string (header.signature));
6373 }
6374 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6375
6376 if (dwarf_read_debug > 1)
6377 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6378 sect_offset_str (sect_off),
6379 hex_string (header.signature));
6380
6381 info_ptr += length;
6382 }
6383 }
6384
6385 /* Create the hash table of all entries in the .debug_types
6386 (or .debug_types.dwo) section(s).
6387 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6388 otherwise it is NULL.
6389
6390 The result is a pointer to the hash table or NULL if there are no types.
6391
6392 Note: This function processes DWO files only, not DWP files. */
6393
6394 static void
6395 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6396 struct dwo_file *dwo_file,
6397 gdb::array_view<dwarf2_section_info> type_sections,
6398 htab_up &types_htab)
6399 {
6400 for (dwarf2_section_info &section : type_sections)
6401 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6402 types_htab, rcuh_kind::TYPE);
6403 }
6404
6405 /* Create the hash table of all entries in the .debug_types section,
6406 and initialize all_type_units.
6407 The result is zero if there is an error (e.g. missing .debug_types section),
6408 otherwise non-zero. */
6409
6410 static int
6411 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6412 {
6413 htab_up types_htab;
6414
6415 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6416 &dwarf2_per_objfile->per_bfd->info, types_htab,
6417 rcuh_kind::COMPILE);
6418 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6419 dwarf2_per_objfile->per_bfd->types, types_htab);
6420 if (types_htab == NULL)
6421 {
6422 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6423 return 0;
6424 }
6425
6426 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6427
6428 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6429 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6430 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6431
6432 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6433 add_signatured_type_cu_to_table,
6434 &dwarf2_per_objfile->per_bfd->all_type_units);
6435
6436 return 1;
6437 }
6438
6439 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6440 If SLOT is non-NULL, it is the entry to use in the hash table.
6441 Otherwise we find one. */
6442
6443 static struct signatured_type *
6444 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6445 void **slot)
6446 {
6447 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6448 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6449 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6450
6451 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6452
6453 dwarf2_per_objfile->resize_symtabs ();
6454
6455 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6456 sig_type->signature = sig;
6457 sig_type->per_cu.is_debug_types = 1;
6458 if (dwarf2_per_objfile->per_bfd->using_index)
6459 {
6460 sig_type->per_cu.v.quick =
6461 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6462 struct dwarf2_per_cu_quick_data);
6463 }
6464
6465 if (slot == NULL)
6466 {
6467 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6468 sig_type, INSERT);
6469 }
6470 gdb_assert (*slot == NULL);
6471 *slot = sig_type;
6472 /* The rest of sig_type must be filled in by the caller. */
6473 return sig_type;
6474 }
6475
6476 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6477 Fill in SIG_ENTRY with DWO_ENTRY. */
6478
6479 static void
6480 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6481 struct signatured_type *sig_entry,
6482 struct dwo_unit *dwo_entry)
6483 {
6484 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6485
6486 /* Make sure we're not clobbering something we don't expect to. */
6487 gdb_assert (! sig_entry->per_cu.queued);
6488 gdb_assert (sig_entry->per_cu.cu == NULL);
6489 if (per_bfd->using_index)
6490 {
6491 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6492 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6493 }
6494 else
6495 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6496 gdb_assert (sig_entry->signature == dwo_entry->signature);
6497 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6498 gdb_assert (sig_entry->type_unit_group == NULL);
6499 gdb_assert (sig_entry->dwo_unit == NULL);
6500
6501 sig_entry->per_cu.section = dwo_entry->section;
6502 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6503 sig_entry->per_cu.length = dwo_entry->length;
6504 sig_entry->per_cu.reading_dwo_directly = 1;
6505 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6506 sig_entry->per_cu.per_bfd = per_bfd;
6507 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6508 sig_entry->dwo_unit = dwo_entry;
6509 }
6510
6511 /* Subroutine of lookup_signatured_type.
6512 If we haven't read the TU yet, create the signatured_type data structure
6513 for a TU to be read in directly from a DWO file, bypassing the stub.
6514 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6515 using .gdb_index, then when reading a CU we want to stay in the DWO file
6516 containing that CU. Otherwise we could end up reading several other DWO
6517 files (due to comdat folding) to process the transitive closure of all the
6518 mentioned TUs, and that can be slow. The current DWO file will have every
6519 type signature that it needs.
6520 We only do this for .gdb_index because in the psymtab case we already have
6521 to read all the DWOs to build the type unit groups. */
6522
6523 static struct signatured_type *
6524 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6525 {
6526 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6527 struct dwo_file *dwo_file;
6528 struct dwo_unit find_dwo_entry, *dwo_entry;
6529 struct signatured_type find_sig_entry, *sig_entry;
6530 void **slot;
6531
6532 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6533
6534 /* If TU skeletons have been removed then we may not have read in any
6535 TUs yet. */
6536 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6537 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6538
6539 /* We only ever need to read in one copy of a signatured type.
6540 Use the global signatured_types array to do our own comdat-folding
6541 of types. If this is the first time we're reading this TU, and
6542 the TU has an entry in .gdb_index, replace the recorded data from
6543 .gdb_index with this TU. */
6544
6545 find_sig_entry.signature = sig;
6546 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6547 &find_sig_entry, INSERT);
6548 sig_entry = (struct signatured_type *) *slot;
6549
6550 /* We can get here with the TU already read, *or* in the process of being
6551 read. Don't reassign the global entry to point to this DWO if that's
6552 the case. Also note that if the TU is already being read, it may not
6553 have come from a DWO, the program may be a mix of Fission-compiled
6554 code and non-Fission-compiled code. */
6555
6556 /* Have we already tried to read this TU?
6557 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6558 needn't exist in the global table yet). */
6559 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6560 return sig_entry;
6561
6562 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6563 dwo_unit of the TU itself. */
6564 dwo_file = cu->dwo_unit->dwo_file;
6565
6566 /* Ok, this is the first time we're reading this TU. */
6567 if (dwo_file->tus == NULL)
6568 return NULL;
6569 find_dwo_entry.signature = sig;
6570 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6571 &find_dwo_entry);
6572 if (dwo_entry == NULL)
6573 return NULL;
6574
6575 /* If the global table doesn't have an entry for this TU, add one. */
6576 if (sig_entry == NULL)
6577 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6578
6579 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6580 sig_entry->per_cu.tu_read = 1;
6581 return sig_entry;
6582 }
6583
6584 /* Subroutine of lookup_signatured_type.
6585 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6586 then try the DWP file. If the TU stub (skeleton) has been removed then
6587 it won't be in .gdb_index. */
6588
6589 static struct signatured_type *
6590 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6591 {
6592 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6593 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6594 struct dwo_unit *dwo_entry;
6595 struct signatured_type find_sig_entry, *sig_entry;
6596 void **slot;
6597
6598 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6599 gdb_assert (dwp_file != NULL);
6600
6601 /* If TU skeletons have been removed then we may not have read in any
6602 TUs yet. */
6603 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6604 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6605
6606 find_sig_entry.signature = sig;
6607 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6608 &find_sig_entry, INSERT);
6609 sig_entry = (struct signatured_type *) *slot;
6610
6611 /* Have we already tried to read this TU?
6612 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6613 needn't exist in the global table yet). */
6614 if (sig_entry != NULL)
6615 return sig_entry;
6616
6617 if (dwp_file->tus == NULL)
6618 return NULL;
6619 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6620 sig, 1 /* is_debug_types */);
6621 if (dwo_entry == NULL)
6622 return NULL;
6623
6624 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6625 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6626
6627 return sig_entry;
6628 }
6629
6630 /* Lookup a signature based type for DW_FORM_ref_sig8.
6631 Returns NULL if signature SIG is not present in the table.
6632 It is up to the caller to complain about this. */
6633
6634 static struct signatured_type *
6635 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6636 {
6637 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6638
6639 if (cu->dwo_unit
6640 && dwarf2_per_objfile->per_bfd->using_index)
6641 {
6642 /* We're in a DWO/DWP file, and we're using .gdb_index.
6643 These cases require special processing. */
6644 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6645 return lookup_dwo_signatured_type (cu, sig);
6646 else
6647 return lookup_dwp_signatured_type (cu, sig);
6648 }
6649 else
6650 {
6651 struct signatured_type find_entry, *entry;
6652
6653 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6654 return NULL;
6655 find_entry.signature = sig;
6656 entry = ((struct signatured_type *)
6657 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6658 &find_entry));
6659 return entry;
6660 }
6661 }
6662
6663 /* Low level DIE reading support. */
6664
6665 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6666
6667 static void
6668 init_cu_die_reader (struct die_reader_specs *reader,
6669 struct dwarf2_cu *cu,
6670 struct dwarf2_section_info *section,
6671 struct dwo_file *dwo_file,
6672 struct abbrev_table *abbrev_table)
6673 {
6674 gdb_assert (section->readin && section->buffer != NULL);
6675 reader->abfd = section->get_bfd_owner ();
6676 reader->cu = cu;
6677 reader->dwo_file = dwo_file;
6678 reader->die_section = section;
6679 reader->buffer = section->buffer;
6680 reader->buffer_end = section->buffer + section->size;
6681 reader->abbrev_table = abbrev_table;
6682 }
6683
6684 /* Subroutine of cutu_reader to simplify it.
6685 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6686 There's just a lot of work to do, and cutu_reader is big enough
6687 already.
6688
6689 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6690 from it to the DIE in the DWO. If NULL we are skipping the stub.
6691 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6692 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6693 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6694 STUB_COMP_DIR may be non-NULL.
6695 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6696 are filled in with the info of the DIE from the DWO file.
6697 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6698 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6699 kept around for at least as long as *RESULT_READER.
6700
6701 The result is non-zero if a valid (non-dummy) DIE was found. */
6702
6703 static int
6704 read_cutu_die_from_dwo (dwarf2_cu *cu,
6705 struct dwo_unit *dwo_unit,
6706 struct die_info *stub_comp_unit_die,
6707 const char *stub_comp_dir,
6708 struct die_reader_specs *result_reader,
6709 const gdb_byte **result_info_ptr,
6710 struct die_info **result_comp_unit_die,
6711 abbrev_table_up *result_dwo_abbrev_table)
6712 {
6713 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6714 dwarf2_per_cu_data *per_cu = cu->per_cu;
6715 struct objfile *objfile = dwarf2_per_objfile->objfile;
6716 bfd *abfd;
6717 const gdb_byte *begin_info_ptr, *info_ptr;
6718 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6719 int i,num_extra_attrs;
6720 struct dwarf2_section_info *dwo_abbrev_section;
6721 struct die_info *comp_unit_die;
6722
6723 /* At most one of these may be provided. */
6724 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6725
6726 /* These attributes aren't processed until later:
6727 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6728 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6729 referenced later. However, these attributes are found in the stub
6730 which we won't have later. In order to not impose this complication
6731 on the rest of the code, we read them here and copy them to the
6732 DWO CU/TU die. */
6733
6734 stmt_list = NULL;
6735 low_pc = NULL;
6736 high_pc = NULL;
6737 ranges = NULL;
6738 comp_dir = NULL;
6739
6740 if (stub_comp_unit_die != NULL)
6741 {
6742 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6743 DWO file. */
6744 if (!per_cu->is_debug_types)
6745 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6746 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6747 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6748 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6749 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6750
6751 cu->addr_base = stub_comp_unit_die->addr_base ();
6752
6753 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6754 here (if needed). We need the value before we can process
6755 DW_AT_ranges. */
6756 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6757 }
6758 else if (stub_comp_dir != NULL)
6759 {
6760 /* Reconstruct the comp_dir attribute to simplify the code below. */
6761 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6762 comp_dir->name = DW_AT_comp_dir;
6763 comp_dir->form = DW_FORM_string;
6764 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6765 DW_STRING (comp_dir) = stub_comp_dir;
6766 }
6767
6768 /* Set up for reading the DWO CU/TU. */
6769 cu->dwo_unit = dwo_unit;
6770 dwarf2_section_info *section = dwo_unit->section;
6771 section->read (objfile);
6772 abfd = section->get_bfd_owner ();
6773 begin_info_ptr = info_ptr = (section->buffer
6774 + to_underlying (dwo_unit->sect_off));
6775 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6776
6777 if (per_cu->is_debug_types)
6778 {
6779 signatured_type *sig_type = (struct signatured_type *) per_cu;
6780
6781 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6782 &cu->header, section,
6783 dwo_abbrev_section,
6784 info_ptr, rcuh_kind::TYPE);
6785 /* This is not an assert because it can be caused by bad debug info. */
6786 if (sig_type->signature != cu->header.signature)
6787 {
6788 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6789 " TU at offset %s [in module %s]"),
6790 hex_string (sig_type->signature),
6791 hex_string (cu->header.signature),
6792 sect_offset_str (dwo_unit->sect_off),
6793 bfd_get_filename (abfd));
6794 }
6795 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6796 /* For DWOs coming from DWP files, we don't know the CU length
6797 nor the type's offset in the TU until now. */
6798 dwo_unit->length = cu->header.get_length ();
6799 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6800
6801 /* Establish the type offset that can be used to lookup the type.
6802 For DWO files, we don't know it until now. */
6803 sig_type->type_offset_in_section
6804 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6805 }
6806 else
6807 {
6808 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6809 &cu->header, section,
6810 dwo_abbrev_section,
6811 info_ptr, rcuh_kind::COMPILE);
6812 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6813 /* For DWOs coming from DWP files, we don't know the CU length
6814 until now. */
6815 dwo_unit->length = cu->header.get_length ();
6816 }
6817
6818 *result_dwo_abbrev_table
6819 = abbrev_table::read (objfile, dwo_abbrev_section,
6820 cu->header.abbrev_sect_off);
6821 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6822 result_dwo_abbrev_table->get ());
6823
6824 /* Read in the die, but leave space to copy over the attributes
6825 from the stub. This has the benefit of simplifying the rest of
6826 the code - all the work to maintain the illusion of a single
6827 DW_TAG_{compile,type}_unit DIE is done here. */
6828 num_extra_attrs = ((stmt_list != NULL)
6829 + (low_pc != NULL)
6830 + (high_pc != NULL)
6831 + (ranges != NULL)
6832 + (comp_dir != NULL));
6833 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6834 num_extra_attrs);
6835
6836 /* Copy over the attributes from the stub to the DIE we just read in. */
6837 comp_unit_die = *result_comp_unit_die;
6838 i = comp_unit_die->num_attrs;
6839 if (stmt_list != NULL)
6840 comp_unit_die->attrs[i++] = *stmt_list;
6841 if (low_pc != NULL)
6842 comp_unit_die->attrs[i++] = *low_pc;
6843 if (high_pc != NULL)
6844 comp_unit_die->attrs[i++] = *high_pc;
6845 if (ranges != NULL)
6846 comp_unit_die->attrs[i++] = *ranges;
6847 if (comp_dir != NULL)
6848 comp_unit_die->attrs[i++] = *comp_dir;
6849 comp_unit_die->num_attrs += num_extra_attrs;
6850
6851 if (dwarf_die_debug)
6852 {
6853 fprintf_unfiltered (gdb_stdlog,
6854 "Read die from %s@0x%x of %s:\n",
6855 section->get_name (),
6856 (unsigned) (begin_info_ptr - section->buffer),
6857 bfd_get_filename (abfd));
6858 dump_die (comp_unit_die, dwarf_die_debug);
6859 }
6860
6861 /* Skip dummy compilation units. */
6862 if (info_ptr >= begin_info_ptr + dwo_unit->length
6863 || peek_abbrev_code (abfd, info_ptr) == 0)
6864 return 0;
6865
6866 *result_info_ptr = info_ptr;
6867 return 1;
6868 }
6869
6870 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6871 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6872 signature is part of the header. */
6873 static gdb::optional<ULONGEST>
6874 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6875 {
6876 if (cu->header.version >= 5)
6877 return cu->header.signature;
6878 struct attribute *attr;
6879 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6880 if (attr == nullptr)
6881 return gdb::optional<ULONGEST> ();
6882 return DW_UNSND (attr);
6883 }
6884
6885 /* Subroutine of cutu_reader to simplify it.
6886 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6887 Returns NULL if the specified DWO unit cannot be found. */
6888
6889 static struct dwo_unit *
6890 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6891 {
6892 dwarf2_per_cu_data *per_cu = cu->per_cu;
6893 struct dwo_unit *dwo_unit;
6894 const char *comp_dir;
6895
6896 gdb_assert (cu != NULL);
6897
6898 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6899 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6900 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6901
6902 if (per_cu->is_debug_types)
6903 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6904 else
6905 {
6906 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6907
6908 if (!signature.has_value ())
6909 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6910 " [in module %s]"),
6911 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6912
6913 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6914 }
6915
6916 return dwo_unit;
6917 }
6918
6919 /* Subroutine of cutu_reader to simplify it.
6920 See it for a description of the parameters.
6921 Read a TU directly from a DWO file, bypassing the stub. */
6922
6923 void
6924 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6925 dwarf2_per_objfile *per_objfile,
6926 int use_existing_cu)
6927 {
6928 struct signatured_type *sig_type;
6929
6930 /* Verify we can do the following downcast, and that we have the
6931 data we need. */
6932 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6933 sig_type = (struct signatured_type *) this_cu;
6934 gdb_assert (sig_type->dwo_unit != NULL);
6935
6936 if (use_existing_cu && this_cu->cu != NULL)
6937 {
6938 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6939 /* There's no need to do the rereading_dwo_cu handling that
6940 cutu_reader does since we don't read the stub. */
6941 }
6942 else
6943 {
6944 /* If !use_existing_cu, this_cu->cu must be NULL. */
6945 gdb_assert (this_cu->cu == NULL);
6946 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6947 }
6948
6949 /* A future optimization, if needed, would be to use an existing
6950 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6951 could share abbrev tables. */
6952
6953 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
6954 NULL /* stub_comp_unit_die */,
6955 sig_type->dwo_unit->dwo_file->comp_dir,
6956 this, &info_ptr,
6957 &comp_unit_die,
6958 &m_dwo_abbrev_table) == 0)
6959 {
6960 /* Dummy die. */
6961 dummy_p = true;
6962 }
6963 }
6964
6965 /* Initialize a CU (or TU) and read its DIEs.
6966 If the CU defers to a DWO file, read the DWO file as well.
6967
6968 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6969 Otherwise the table specified in the comp unit header is read in and used.
6970 This is an optimization for when we already have the abbrev table.
6971
6972 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6973 Otherwise, a new CU is allocated with xmalloc. */
6974
6975 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6976 dwarf2_per_objfile *dwarf2_per_objfile,
6977 struct abbrev_table *abbrev_table,
6978 int use_existing_cu,
6979 bool skip_partial)
6980 : die_reader_specs {},
6981 m_this_cu (this_cu)
6982 {
6983 struct objfile *objfile = dwarf2_per_objfile->objfile;
6984 struct dwarf2_section_info *section = this_cu->section;
6985 bfd *abfd = section->get_bfd_owner ();
6986 struct dwarf2_cu *cu;
6987 const gdb_byte *begin_info_ptr;
6988 struct signatured_type *sig_type = NULL;
6989 struct dwarf2_section_info *abbrev_section;
6990 /* Non-zero if CU currently points to a DWO file and we need to
6991 reread it. When this happens we need to reread the skeleton die
6992 before we can reread the DWO file (this only applies to CUs, not TUs). */
6993 int rereading_dwo_cu = 0;
6994
6995 if (dwarf_die_debug)
6996 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6997 this_cu->is_debug_types ? "type" : "comp",
6998 sect_offset_str (this_cu->sect_off));
6999
7000 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7001 file (instead of going through the stub), short-circuit all of this. */
7002 if (this_cu->reading_dwo_directly)
7003 {
7004 /* Narrow down the scope of possibilities to have to understand. */
7005 gdb_assert (this_cu->is_debug_types);
7006 gdb_assert (abbrev_table == NULL);
7007 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
7008 return;
7009 }
7010
7011 /* This is cheap if the section is already read in. */
7012 section->read (objfile);
7013
7014 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7015
7016 abbrev_section = get_abbrev_section_for_cu (this_cu);
7017
7018 if (use_existing_cu && this_cu->cu != NULL)
7019 {
7020 cu = this_cu->cu;
7021 /* If this CU is from a DWO file we need to start over, we need to
7022 refetch the attributes from the skeleton CU.
7023 This could be optimized by retrieving those attributes from when we
7024 were here the first time: the previous comp_unit_die was stored in
7025 comp_unit_obstack. But there's no data yet that we need this
7026 optimization. */
7027 if (cu->dwo_unit != NULL)
7028 rereading_dwo_cu = 1;
7029 }
7030 else
7031 {
7032 /* If !use_existing_cu, this_cu->cu must be NULL. */
7033 gdb_assert (this_cu->cu == NULL);
7034 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7035 cu = m_new_cu.get ();
7036 }
7037
7038 /* Get the header. */
7039 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7040 {
7041 /* We already have the header, there's no need to read it in again. */
7042 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7043 }
7044 else
7045 {
7046 if (this_cu->is_debug_types)
7047 {
7048 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7049 &cu->header, section,
7050 abbrev_section, info_ptr,
7051 rcuh_kind::TYPE);
7052
7053 /* Since per_cu is the first member of struct signatured_type,
7054 we can go from a pointer to one to a pointer to the other. */
7055 sig_type = (struct signatured_type *) this_cu;
7056 gdb_assert (sig_type->signature == cu->header.signature);
7057 gdb_assert (sig_type->type_offset_in_tu
7058 == cu->header.type_cu_offset_in_tu);
7059 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7060
7061 /* LENGTH has not been set yet for type units if we're
7062 using .gdb_index. */
7063 this_cu->length = cu->header.get_length ();
7064
7065 /* Establish the type offset that can be used to lookup the type. */
7066 sig_type->type_offset_in_section =
7067 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7068
7069 this_cu->dwarf_version = cu->header.version;
7070 }
7071 else
7072 {
7073 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7074 &cu->header, section,
7075 abbrev_section,
7076 info_ptr,
7077 rcuh_kind::COMPILE);
7078
7079 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7080 if (this_cu->length == 0)
7081 this_cu->length = cu->header.get_length ();
7082 else
7083 gdb_assert (this_cu->length == cu->header.get_length ());
7084 this_cu->dwarf_version = cu->header.version;
7085 }
7086 }
7087
7088 /* Skip dummy compilation units. */
7089 if (info_ptr >= begin_info_ptr + this_cu->length
7090 || peek_abbrev_code (abfd, info_ptr) == 0)
7091 {
7092 dummy_p = true;
7093 return;
7094 }
7095
7096 /* If we don't have them yet, read the abbrevs for this compilation unit.
7097 And if we need to read them now, make sure they're freed when we're
7098 done. */
7099 if (abbrev_table != NULL)
7100 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7101 else
7102 {
7103 m_abbrev_table_holder
7104 = abbrev_table::read (objfile, abbrev_section,
7105 cu->header.abbrev_sect_off);
7106 abbrev_table = m_abbrev_table_holder.get ();
7107 }
7108
7109 /* Read the top level CU/TU die. */
7110 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7111 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7112
7113 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7114 {
7115 dummy_p = true;
7116 return;
7117 }
7118
7119 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7120 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7121 table from the DWO file and pass the ownership over to us. It will be
7122 referenced from READER, so we must make sure to free it after we're done
7123 with READER.
7124
7125 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7126 DWO CU, that this test will fail (the attribute will not be present). */
7127 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7128 if (dwo_name != nullptr)
7129 {
7130 struct dwo_unit *dwo_unit;
7131 struct die_info *dwo_comp_unit_die;
7132
7133 if (comp_unit_die->has_children)
7134 {
7135 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7136 " has children (offset %s) [in module %s]"),
7137 sect_offset_str (this_cu->sect_off),
7138 bfd_get_filename (abfd));
7139 }
7140 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7141 if (dwo_unit != NULL)
7142 {
7143 if (read_cutu_die_from_dwo (cu, dwo_unit,
7144 comp_unit_die, NULL,
7145 this, &info_ptr,
7146 &dwo_comp_unit_die,
7147 &m_dwo_abbrev_table) == 0)
7148 {
7149 /* Dummy die. */
7150 dummy_p = true;
7151 return;
7152 }
7153 comp_unit_die = dwo_comp_unit_die;
7154 }
7155 else
7156 {
7157 /* Yikes, we couldn't find the rest of the DIE, we only have
7158 the stub. A complaint has already been logged. There's
7159 not much more we can do except pass on the stub DIE to
7160 die_reader_func. We don't want to throw an error on bad
7161 debug info. */
7162 }
7163 }
7164 }
7165
7166 void
7167 cutu_reader::keep ()
7168 {
7169 /* Done, clean up. */
7170 gdb_assert (!dummy_p);
7171 if (m_new_cu != NULL)
7172 {
7173 /* We know that m_this_cu->cu is set, since we are in the process of
7174 parsing the CU. */
7175 gdb_assert (m_this_cu->cu != nullptr);
7176 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7177
7178 /* Link this CU into read_in_chain. */
7179 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7180 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7181 /* The chain owns it now. */
7182 m_new_cu.release ();
7183 }
7184 }
7185
7186 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7187 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7188 assumed to have already done the lookup to find the DWO file).
7189
7190 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7191 THIS_CU->is_debug_types, but nothing else.
7192
7193 We fill in THIS_CU->length.
7194
7195 THIS_CU->cu is always freed when done.
7196 This is done in order to not leave THIS_CU->cu in a state where we have
7197 to care whether it refers to the "main" CU or the DWO CU.
7198
7199 When parent_cu is passed, it is used to provide a default value for
7200 str_offsets_base and addr_base from the parent. */
7201
7202 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7203 dwarf2_per_objfile *dwarf2_per_objfile,
7204 struct dwarf2_cu *parent_cu,
7205 struct dwo_file *dwo_file)
7206 : die_reader_specs {},
7207 m_this_cu (this_cu)
7208 {
7209 struct objfile *objfile = dwarf2_per_objfile->objfile;
7210 struct dwarf2_section_info *section = this_cu->section;
7211 bfd *abfd = section->get_bfd_owner ();
7212 struct dwarf2_section_info *abbrev_section;
7213 const gdb_byte *begin_info_ptr, *info_ptr;
7214
7215 if (dwarf_die_debug)
7216 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7217 this_cu->is_debug_types ? "type" : "comp",
7218 sect_offset_str (this_cu->sect_off));
7219
7220 gdb_assert (this_cu->cu == NULL);
7221
7222 abbrev_section = (dwo_file != NULL
7223 ? &dwo_file->sections.abbrev
7224 : get_abbrev_section_for_cu (this_cu));
7225
7226 /* This is cheap if the section is already read in. */
7227 section->read (objfile);
7228
7229 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7230
7231 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7232 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7233 &m_new_cu->header, section,
7234 abbrev_section, info_ptr,
7235 (this_cu->is_debug_types
7236 ? rcuh_kind::TYPE
7237 : rcuh_kind::COMPILE));
7238
7239 if (parent_cu != nullptr)
7240 {
7241 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7242 m_new_cu->addr_base = parent_cu->addr_base;
7243 }
7244 this_cu->length = m_new_cu->header.get_length ();
7245
7246 /* Skip dummy compilation units. */
7247 if (info_ptr >= begin_info_ptr + this_cu->length
7248 || peek_abbrev_code (abfd, info_ptr) == 0)
7249 {
7250 dummy_p = true;
7251 return;
7252 }
7253
7254 m_abbrev_table_holder
7255 = abbrev_table::read (objfile, abbrev_section,
7256 m_new_cu->header.abbrev_sect_off);
7257
7258 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7259 m_abbrev_table_holder.get ());
7260 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7261 }
7262
7263 \f
7264 /* Type Unit Groups.
7265
7266 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7267 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7268 so that all types coming from the same compilation (.o file) are grouped
7269 together. A future step could be to put the types in the same symtab as
7270 the CU the types ultimately came from. */
7271
7272 static hashval_t
7273 hash_type_unit_group (const void *item)
7274 {
7275 const struct type_unit_group *tu_group
7276 = (const struct type_unit_group *) item;
7277
7278 return hash_stmt_list_entry (&tu_group->hash);
7279 }
7280
7281 static int
7282 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7283 {
7284 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7285 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7286
7287 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7288 }
7289
7290 /* Allocate a hash table for type unit groups. */
7291
7292 static htab_up
7293 allocate_type_unit_groups_table ()
7294 {
7295 return htab_up (htab_create_alloc (3,
7296 hash_type_unit_group,
7297 eq_type_unit_group,
7298 NULL, xcalloc, xfree));
7299 }
7300
7301 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7302 partial symtabs. We combine several TUs per psymtab to not let the size
7303 of any one psymtab grow too big. */
7304 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7305 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7306
7307 /* Helper routine for get_type_unit_group.
7308 Create the type_unit_group object used to hold one or more TUs. */
7309
7310 static struct type_unit_group *
7311 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7312 {
7313 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7314 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7315 struct dwarf2_per_cu_data *per_cu;
7316 struct type_unit_group *tu_group;
7317
7318 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7319 struct type_unit_group);
7320 per_cu = &tu_group->per_cu;
7321 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7322 per_cu->per_bfd = per_bfd;
7323
7324 if (per_bfd->using_index)
7325 {
7326 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7327 struct dwarf2_per_cu_quick_data);
7328 }
7329 else
7330 {
7331 unsigned int line_offset = to_underlying (line_offset_struct);
7332 dwarf2_psymtab *pst;
7333 std::string name;
7334
7335 /* Give the symtab a useful name for debug purposes. */
7336 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7337 name = string_printf ("<type_units_%d>",
7338 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7339 else
7340 name = string_printf ("<type_units_at_0x%x>", line_offset);
7341
7342 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7343 pst->anonymous = true;
7344 }
7345
7346 tu_group->hash.dwo_unit = cu->dwo_unit;
7347 tu_group->hash.line_sect_off = line_offset_struct;
7348
7349 return tu_group;
7350 }
7351
7352 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7353 STMT_LIST is a DW_AT_stmt_list attribute. */
7354
7355 static struct type_unit_group *
7356 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7357 {
7358 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7359 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7360 struct type_unit_group *tu_group;
7361 void **slot;
7362 unsigned int line_offset;
7363 struct type_unit_group type_unit_group_for_lookup;
7364
7365 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7366 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7367
7368 /* Do we need to create a new group, or can we use an existing one? */
7369
7370 if (stmt_list)
7371 {
7372 line_offset = DW_UNSND (stmt_list);
7373 ++tu_stats->nr_symtab_sharers;
7374 }
7375 else
7376 {
7377 /* Ugh, no stmt_list. Rare, but we have to handle it.
7378 We can do various things here like create one group per TU or
7379 spread them over multiple groups to split up the expansion work.
7380 To avoid worst case scenarios (too many groups or too large groups)
7381 we, umm, group them in bunches. */
7382 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7383 | (tu_stats->nr_stmt_less_type_units
7384 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7385 ++tu_stats->nr_stmt_less_type_units;
7386 }
7387
7388 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7389 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7390 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7391 &type_unit_group_for_lookup, INSERT);
7392 if (*slot != NULL)
7393 {
7394 tu_group = (struct type_unit_group *) *slot;
7395 gdb_assert (tu_group != NULL);
7396 }
7397 else
7398 {
7399 sect_offset line_offset_struct = (sect_offset) line_offset;
7400 tu_group = create_type_unit_group (cu, line_offset_struct);
7401 *slot = tu_group;
7402 ++tu_stats->nr_symtabs;
7403 }
7404
7405 return tu_group;
7406 }
7407 \f
7408 /* Partial symbol tables. */
7409
7410 /* Create a psymtab named NAME and assign it to PER_CU.
7411
7412 The caller must fill in the following details:
7413 dirname, textlow, texthigh. */
7414
7415 static dwarf2_psymtab *
7416 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7417 dwarf2_per_objfile *per_objfile,
7418 const char *name)
7419 {
7420 struct objfile *objfile = per_objfile->objfile;
7421 dwarf2_psymtab *pst;
7422
7423 pst = new dwarf2_psymtab (name, objfile, per_cu);
7424
7425 pst->psymtabs_addrmap_supported = true;
7426
7427 /* This is the glue that links PST into GDB's symbol API. */
7428 per_cu->v.psymtab = pst;
7429
7430 return pst;
7431 }
7432
7433 /* DIE reader function for process_psymtab_comp_unit. */
7434
7435 static void
7436 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7437 const gdb_byte *info_ptr,
7438 struct die_info *comp_unit_die,
7439 enum language pretend_language)
7440 {
7441 struct dwarf2_cu *cu = reader->cu;
7442 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7443 struct objfile *objfile = per_objfile->objfile;
7444 struct gdbarch *gdbarch = objfile->arch ();
7445 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7446 CORE_ADDR baseaddr;
7447 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7448 dwarf2_psymtab *pst;
7449 enum pc_bounds_kind cu_bounds_kind;
7450 const char *filename;
7451
7452 gdb_assert (! per_cu->is_debug_types);
7453
7454 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7455
7456 /* Allocate a new partial symbol table structure. */
7457 gdb::unique_xmalloc_ptr<char> debug_filename;
7458 static const char artificial[] = "<artificial>";
7459 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7460 if (filename == NULL)
7461 filename = "";
7462 else if (strcmp (filename, artificial) == 0)
7463 {
7464 debug_filename.reset (concat (artificial, "@",
7465 sect_offset_str (per_cu->sect_off),
7466 (char *) NULL));
7467 filename = debug_filename.get ();
7468 }
7469
7470 pst = create_partial_symtab (per_cu, per_objfile, filename);
7471
7472 /* This must be done before calling dwarf2_build_include_psymtabs. */
7473 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7474
7475 baseaddr = objfile->text_section_offset ();
7476
7477 dwarf2_find_base_address (comp_unit_die, cu);
7478
7479 /* Possibly set the default values of LOWPC and HIGHPC from
7480 `DW_AT_ranges'. */
7481 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7482 &best_highpc, cu, pst);
7483 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7484 {
7485 CORE_ADDR low
7486 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7487 - baseaddr);
7488 CORE_ADDR high
7489 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7490 - baseaddr - 1);
7491 /* Store the contiguous range if it is not empty; it can be
7492 empty for CUs with no code. */
7493 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7494 low, high, pst);
7495 }
7496
7497 /* Check if comp unit has_children.
7498 If so, read the rest of the partial symbols from this comp unit.
7499 If not, there's no more debug_info for this comp unit. */
7500 if (comp_unit_die->has_children)
7501 {
7502 struct partial_die_info *first_die;
7503 CORE_ADDR lowpc, highpc;
7504
7505 lowpc = ((CORE_ADDR) -1);
7506 highpc = ((CORE_ADDR) 0);
7507
7508 first_die = load_partial_dies (reader, info_ptr, 1);
7509
7510 scan_partial_symbols (first_die, &lowpc, &highpc,
7511 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7512
7513 /* If we didn't find a lowpc, set it to highpc to avoid
7514 complaints from `maint check'. */
7515 if (lowpc == ((CORE_ADDR) -1))
7516 lowpc = highpc;
7517
7518 /* If the compilation unit didn't have an explicit address range,
7519 then use the information extracted from its child dies. */
7520 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7521 {
7522 best_lowpc = lowpc;
7523 best_highpc = highpc;
7524 }
7525 }
7526 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7527 best_lowpc + baseaddr)
7528 - baseaddr);
7529 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7530 best_highpc + baseaddr)
7531 - baseaddr);
7532
7533 end_psymtab_common (objfile, pst);
7534
7535 if (!cu->per_cu->imported_symtabs_empty ())
7536 {
7537 int i;
7538 int len = cu->per_cu->imported_symtabs_size ();
7539
7540 /* Fill in 'dependencies' here; we fill in 'users' in a
7541 post-pass. */
7542 pst->number_of_dependencies = len;
7543 pst->dependencies
7544 = objfile->partial_symtabs->allocate_dependencies (len);
7545 for (i = 0; i < len; ++i)
7546 {
7547 pst->dependencies[i]
7548 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7549 }
7550
7551 cu->per_cu->imported_symtabs_free ();
7552 }
7553
7554 /* Get the list of files included in the current compilation unit,
7555 and build a psymtab for each of them. */
7556 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7557
7558 if (dwarf_read_debug)
7559 fprintf_unfiltered (gdb_stdlog,
7560 "Psymtab for %s unit @%s: %s - %s"
7561 ", %d global, %d static syms\n",
7562 per_cu->is_debug_types ? "type" : "comp",
7563 sect_offset_str (per_cu->sect_off),
7564 paddress (gdbarch, pst->text_low (objfile)),
7565 paddress (gdbarch, pst->text_high (objfile)),
7566 pst->n_global_syms, pst->n_static_syms);
7567 }
7568
7569 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7570 Process compilation unit THIS_CU for a psymtab. */
7571
7572 static void
7573 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7574 dwarf2_per_objfile *per_objfile,
7575 bool want_partial_unit,
7576 enum language pretend_language)
7577 {
7578 /* If this compilation unit was already read in, free the
7579 cached copy in order to read it in again. This is
7580 necessary because we skipped some symbols when we first
7581 read in the compilation unit (see load_partial_dies).
7582 This problem could be avoided, but the benefit is unclear. */
7583 if (this_cu->cu != NULL)
7584 free_one_cached_comp_unit (this_cu, per_objfile);
7585
7586 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7587
7588 switch (reader.comp_unit_die->tag)
7589 {
7590 case DW_TAG_compile_unit:
7591 this_cu->unit_type = DW_UT_compile;
7592 break;
7593 case DW_TAG_partial_unit:
7594 this_cu->unit_type = DW_UT_partial;
7595 break;
7596 default:
7597 abort ();
7598 }
7599
7600 if (reader.dummy_p)
7601 {
7602 /* Nothing. */
7603 }
7604 else if (this_cu->is_debug_types)
7605 build_type_psymtabs_reader (&reader, reader.info_ptr,
7606 reader.comp_unit_die);
7607 else if (want_partial_unit
7608 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7609 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7610 reader.comp_unit_die,
7611 pretend_language);
7612
7613 this_cu->lang = this_cu->cu->language;
7614
7615 /* Age out any secondary CUs. */
7616 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7617 }
7618
7619 /* Reader function for build_type_psymtabs. */
7620
7621 static void
7622 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7623 const gdb_byte *info_ptr,
7624 struct die_info *type_unit_die)
7625 {
7626 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7627 struct objfile *objfile = dwarf2_per_objfile->objfile;
7628 struct dwarf2_cu *cu = reader->cu;
7629 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7630 struct signatured_type *sig_type;
7631 struct type_unit_group *tu_group;
7632 struct attribute *attr;
7633 struct partial_die_info *first_die;
7634 CORE_ADDR lowpc, highpc;
7635 dwarf2_psymtab *pst;
7636
7637 gdb_assert (per_cu->is_debug_types);
7638 sig_type = (struct signatured_type *) per_cu;
7639
7640 if (! type_unit_die->has_children)
7641 return;
7642
7643 attr = type_unit_die->attr (DW_AT_stmt_list);
7644 tu_group = get_type_unit_group (cu, attr);
7645
7646 if (tu_group->tus == nullptr)
7647 tu_group->tus = new std::vector<signatured_type *>;
7648 tu_group->tus->push_back (sig_type);
7649
7650 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7651 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7652 pst->anonymous = true;
7653
7654 first_die = load_partial_dies (reader, info_ptr, 1);
7655
7656 lowpc = (CORE_ADDR) -1;
7657 highpc = (CORE_ADDR) 0;
7658 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7659
7660 end_psymtab_common (objfile, pst);
7661 }
7662
7663 /* Struct used to sort TUs by their abbreviation table offset. */
7664
7665 struct tu_abbrev_offset
7666 {
7667 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7668 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7669 {}
7670
7671 signatured_type *sig_type;
7672 sect_offset abbrev_offset;
7673 };
7674
7675 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7676
7677 static bool
7678 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7679 const struct tu_abbrev_offset &b)
7680 {
7681 return a.abbrev_offset < b.abbrev_offset;
7682 }
7683
7684 /* Efficiently read all the type units.
7685 This does the bulk of the work for build_type_psymtabs.
7686
7687 The efficiency is because we sort TUs by the abbrev table they use and
7688 only read each abbrev table once. In one program there are 200K TUs
7689 sharing 8K abbrev tables.
7690
7691 The main purpose of this function is to support building the
7692 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7693 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7694 can collapse the search space by grouping them by stmt_list.
7695 The savings can be significant, in the same program from above the 200K TUs
7696 share 8K stmt_list tables.
7697
7698 FUNC is expected to call get_type_unit_group, which will create the
7699 struct type_unit_group if necessary and add it to
7700 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7701
7702 static void
7703 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7704 {
7705 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7706 abbrev_table_up abbrev_table;
7707 sect_offset abbrev_offset;
7708
7709 /* It's up to the caller to not call us multiple times. */
7710 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7711
7712 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7713 return;
7714
7715 /* TUs typically share abbrev tables, and there can be way more TUs than
7716 abbrev tables. Sort by abbrev table to reduce the number of times we
7717 read each abbrev table in.
7718 Alternatives are to punt or to maintain a cache of abbrev tables.
7719 This is simpler and efficient enough for now.
7720
7721 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7722 symtab to use). Typically TUs with the same abbrev offset have the same
7723 stmt_list value too so in practice this should work well.
7724
7725 The basic algorithm here is:
7726
7727 sort TUs by abbrev table
7728 for each TU with same abbrev table:
7729 read abbrev table if first user
7730 read TU top level DIE
7731 [IWBN if DWO skeletons had DW_AT_stmt_list]
7732 call FUNC */
7733
7734 if (dwarf_read_debug)
7735 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7736
7737 /* Sort in a separate table to maintain the order of all_type_units
7738 for .gdb_index: TU indices directly index all_type_units. */
7739 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7740 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7741
7742 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7743 sorted_by_abbrev.emplace_back
7744 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7745 sig_type->per_cu.section,
7746 sig_type->per_cu.sect_off));
7747
7748 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7749 sort_tu_by_abbrev_offset);
7750
7751 abbrev_offset = (sect_offset) ~(unsigned) 0;
7752
7753 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7754 {
7755 /* Switch to the next abbrev table if necessary. */
7756 if (abbrev_table == NULL
7757 || tu.abbrev_offset != abbrev_offset)
7758 {
7759 abbrev_offset = tu.abbrev_offset;
7760 abbrev_table =
7761 abbrev_table::read (dwarf2_per_objfile->objfile,
7762 &dwarf2_per_objfile->per_bfd->abbrev,
7763 abbrev_offset);
7764 ++tu_stats->nr_uniq_abbrev_tables;
7765 }
7766
7767 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7768 abbrev_table.get (), 0, false);
7769 if (!reader.dummy_p)
7770 build_type_psymtabs_reader (&reader, reader.info_ptr,
7771 reader.comp_unit_die);
7772 }
7773 }
7774
7775 /* Print collected type unit statistics. */
7776
7777 static void
7778 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7779 {
7780 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7781
7782 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7783 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7784 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7785 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7786 tu_stats->nr_uniq_abbrev_tables);
7787 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7788 tu_stats->nr_symtabs);
7789 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7790 tu_stats->nr_symtab_sharers);
7791 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7792 tu_stats->nr_stmt_less_type_units);
7793 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7794 tu_stats->nr_all_type_units_reallocs);
7795 }
7796
7797 /* Traversal function for build_type_psymtabs. */
7798
7799 static int
7800 build_type_psymtab_dependencies (void **slot, void *info)
7801 {
7802 struct dwarf2_per_objfile *dwarf2_per_objfile
7803 = (struct dwarf2_per_objfile *) info;
7804 struct objfile *objfile = dwarf2_per_objfile->objfile;
7805 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7806 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7807 dwarf2_psymtab *pst = per_cu->v.psymtab;
7808 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7809 int i;
7810
7811 gdb_assert (len > 0);
7812 gdb_assert (per_cu->type_unit_group_p ());
7813
7814 pst->number_of_dependencies = len;
7815 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7816 for (i = 0; i < len; ++i)
7817 {
7818 struct signatured_type *iter = tu_group->tus->at (i);
7819 gdb_assert (iter->per_cu.is_debug_types);
7820 pst->dependencies[i] = iter->per_cu.v.psymtab;
7821 iter->type_unit_group = tu_group;
7822 }
7823
7824 delete tu_group->tus;
7825 tu_group->tus = nullptr;
7826
7827 return 1;
7828 }
7829
7830 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7831 Build partial symbol tables for the .debug_types comp-units. */
7832
7833 static void
7834 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7835 {
7836 if (! create_all_type_units (dwarf2_per_objfile))
7837 return;
7838
7839 build_type_psymtabs_1 (dwarf2_per_objfile);
7840 }
7841
7842 /* Traversal function for process_skeletonless_type_unit.
7843 Read a TU in a DWO file and build partial symbols for it. */
7844
7845 static int
7846 process_skeletonless_type_unit (void **slot, void *info)
7847 {
7848 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7849 struct dwarf2_per_objfile *dwarf2_per_objfile
7850 = (struct dwarf2_per_objfile *) info;
7851 struct signatured_type find_entry, *entry;
7852
7853 /* If this TU doesn't exist in the global table, add it and read it in. */
7854
7855 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7856 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7857
7858 find_entry.signature = dwo_unit->signature;
7859 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7860 &find_entry, INSERT);
7861 /* If we've already seen this type there's nothing to do. What's happening
7862 is we're doing our own version of comdat-folding here. */
7863 if (*slot != NULL)
7864 return 1;
7865
7866 /* This does the job that create_all_type_units would have done for
7867 this TU. */
7868 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7869 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7870 *slot = entry;
7871
7872 /* This does the job that build_type_psymtabs_1 would have done. */
7873 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7874 if (!reader.dummy_p)
7875 build_type_psymtabs_reader (&reader, reader.info_ptr,
7876 reader.comp_unit_die);
7877
7878 return 1;
7879 }
7880
7881 /* Traversal function for process_skeletonless_type_units. */
7882
7883 static int
7884 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7885 {
7886 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7887
7888 if (dwo_file->tus != NULL)
7889 htab_traverse_noresize (dwo_file->tus.get (),
7890 process_skeletonless_type_unit, info);
7891
7892 return 1;
7893 }
7894
7895 /* Scan all TUs of DWO files, verifying we've processed them.
7896 This is needed in case a TU was emitted without its skeleton.
7897 Note: This can't be done until we know what all the DWO files are. */
7898
7899 static void
7900 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7901 {
7902 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7903 if (get_dwp_file (dwarf2_per_objfile) == NULL
7904 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7905 {
7906 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7907 process_dwo_file_for_skeletonless_type_units,
7908 dwarf2_per_objfile);
7909 }
7910 }
7911
7912 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7913
7914 static void
7915 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7916 {
7917 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7918 {
7919 dwarf2_psymtab *pst = per_cu->v.psymtab;
7920
7921 if (pst == NULL)
7922 continue;
7923
7924 for (int j = 0; j < pst->number_of_dependencies; ++j)
7925 {
7926 /* Set the 'user' field only if it is not already set. */
7927 if (pst->dependencies[j]->user == NULL)
7928 pst->dependencies[j]->user = pst;
7929 }
7930 }
7931 }
7932
7933 /* Build the partial symbol table by doing a quick pass through the
7934 .debug_info and .debug_abbrev sections. */
7935
7936 static void
7937 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7938 {
7939 struct objfile *objfile = dwarf2_per_objfile->objfile;
7940
7941 if (dwarf_read_debug)
7942 {
7943 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7944 objfile_name (objfile));
7945 }
7946
7947 scoped_restore restore_reading_psyms
7948 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7949 true);
7950
7951 dwarf2_per_objfile->per_bfd->info.read (objfile);
7952
7953 /* Any cached compilation units will be linked by the per-objfile
7954 read_in_chain. Make sure to free them when we're done. */
7955 free_cached_comp_units freer (dwarf2_per_objfile);
7956
7957 build_type_psymtabs (dwarf2_per_objfile);
7958
7959 create_all_comp_units (dwarf2_per_objfile);
7960
7961 /* Create a temporary address map on a temporary obstack. We later
7962 copy this to the final obstack. */
7963 auto_obstack temp_obstack;
7964
7965 scoped_restore save_psymtabs_addrmap
7966 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7967 addrmap_create_mutable (&temp_obstack));
7968
7969 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7970 {
7971 if (per_cu->v.psymtab != NULL)
7972 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7973 continue;
7974 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7975 language_minimal);
7976 }
7977
7978 /* This has to wait until we read the CUs, we need the list of DWOs. */
7979 process_skeletonless_type_units (dwarf2_per_objfile);
7980
7981 /* Now that all TUs have been processed we can fill in the dependencies. */
7982 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7983 {
7984 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7985 build_type_psymtab_dependencies, dwarf2_per_objfile);
7986 }
7987
7988 if (dwarf_read_debug)
7989 print_tu_stats (dwarf2_per_objfile);
7990
7991 set_partial_user (dwarf2_per_objfile);
7992
7993 objfile->partial_symtabs->psymtabs_addrmap
7994 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7995 objfile->partial_symtabs->obstack ());
7996 /* At this point we want to keep the address map. */
7997 save_psymtabs_addrmap.release ();
7998
7999 if (dwarf_read_debug)
8000 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8001 objfile_name (objfile));
8002 }
8003
8004 /* Load the partial DIEs for a secondary CU into memory.
8005 This is also used when rereading a primary CU with load_all_dies. */
8006
8007 static void
8008 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8009 dwarf2_per_objfile *per_objfile)
8010 {
8011 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
8012
8013 if (!reader.dummy_p)
8014 {
8015 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8016 language_minimal);
8017
8018 /* Check if comp unit has_children.
8019 If so, read the rest of the partial symbols from this comp unit.
8020 If not, there's no more debug_info for this comp unit. */
8021 if (reader.comp_unit_die->has_children)
8022 load_partial_dies (&reader, reader.info_ptr, 0);
8023
8024 reader.keep ();
8025 }
8026 }
8027
8028 static void
8029 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8030 struct dwarf2_section_info *section,
8031 struct dwarf2_section_info *abbrev_section,
8032 unsigned int is_dwz)
8033 {
8034 const gdb_byte *info_ptr;
8035 struct objfile *objfile = dwarf2_per_objfile->objfile;
8036
8037 if (dwarf_read_debug)
8038 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8039 section->get_name (),
8040 section->get_file_name ());
8041
8042 section->read (objfile);
8043
8044 info_ptr = section->buffer;
8045
8046 while (info_ptr < section->buffer + section->size)
8047 {
8048 struct dwarf2_per_cu_data *this_cu;
8049
8050 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8051
8052 comp_unit_head cu_header;
8053 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8054 abbrev_section, info_ptr,
8055 rcuh_kind::COMPILE);
8056
8057 /* Save the compilation unit for later lookup. */
8058 if (cu_header.unit_type != DW_UT_type)
8059 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8060 else
8061 {
8062 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8063 sig_type->signature = cu_header.signature;
8064 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8065 this_cu = &sig_type->per_cu;
8066 }
8067 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8068 this_cu->sect_off = sect_off;
8069 this_cu->length = cu_header.length + cu_header.initial_length_size;
8070 this_cu->is_dwz = is_dwz;
8071 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8072 this_cu->section = section;
8073
8074 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8075
8076 info_ptr = info_ptr + this_cu->length;
8077 }
8078 }
8079
8080 /* Create a list of all compilation units in OBJFILE.
8081 This is only done for -readnow and building partial symtabs. */
8082
8083 static void
8084 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8085 {
8086 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8087 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8088 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8089
8090 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8091 if (dwz != NULL)
8092 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8093 1);
8094 }
8095
8096 /* Process all loaded DIEs for compilation unit CU, starting at
8097 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8098 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8099 DW_AT_ranges). See the comments of add_partial_subprogram on how
8100 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8101
8102 static void
8103 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8104 CORE_ADDR *highpc, int set_addrmap,
8105 struct dwarf2_cu *cu)
8106 {
8107 struct partial_die_info *pdi;
8108
8109 /* Now, march along the PDI's, descending into ones which have
8110 interesting children but skipping the children of the other ones,
8111 until we reach the end of the compilation unit. */
8112
8113 pdi = first_die;
8114
8115 while (pdi != NULL)
8116 {
8117 pdi->fixup (cu);
8118
8119 /* Anonymous namespaces or modules have no name but have interesting
8120 children, so we need to look at them. Ditto for anonymous
8121 enums. */
8122
8123 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8124 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8125 || pdi->tag == DW_TAG_imported_unit
8126 || pdi->tag == DW_TAG_inlined_subroutine)
8127 {
8128 switch (pdi->tag)
8129 {
8130 case DW_TAG_subprogram:
8131 case DW_TAG_inlined_subroutine:
8132 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8133 break;
8134 case DW_TAG_constant:
8135 case DW_TAG_variable:
8136 case DW_TAG_typedef:
8137 case DW_TAG_union_type:
8138 if (!pdi->is_declaration
8139 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8140 {
8141 add_partial_symbol (pdi, cu);
8142 }
8143 break;
8144 case DW_TAG_class_type:
8145 case DW_TAG_interface_type:
8146 case DW_TAG_structure_type:
8147 if (!pdi->is_declaration)
8148 {
8149 add_partial_symbol (pdi, cu);
8150 }
8151 if ((cu->language == language_rust
8152 || cu->language == language_cplus) && pdi->has_children)
8153 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8154 set_addrmap, cu);
8155 break;
8156 case DW_TAG_enumeration_type:
8157 if (!pdi->is_declaration)
8158 add_partial_enumeration (pdi, cu);
8159 break;
8160 case DW_TAG_base_type:
8161 case DW_TAG_subrange_type:
8162 /* File scope base type definitions are added to the partial
8163 symbol table. */
8164 add_partial_symbol (pdi, cu);
8165 break;
8166 case DW_TAG_namespace:
8167 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8168 break;
8169 case DW_TAG_module:
8170 if (!pdi->is_declaration)
8171 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8172 break;
8173 case DW_TAG_imported_unit:
8174 {
8175 struct dwarf2_per_cu_data *per_cu;
8176
8177 /* For now we don't handle imported units in type units. */
8178 if (cu->per_cu->is_debug_types)
8179 {
8180 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8181 " supported in type units [in module %s]"),
8182 objfile_name (cu->per_objfile->objfile));
8183 }
8184
8185 per_cu = dwarf2_find_containing_comp_unit
8186 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8187
8188 /* Go read the partial unit, if needed. */
8189 if (per_cu->v.psymtab == NULL)
8190 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8191 cu->language);
8192
8193 cu->per_cu->imported_symtabs_push (per_cu);
8194 }
8195 break;
8196 case DW_TAG_imported_declaration:
8197 add_partial_symbol (pdi, cu);
8198 break;
8199 default:
8200 break;
8201 }
8202 }
8203
8204 /* If the die has a sibling, skip to the sibling. */
8205
8206 pdi = pdi->die_sibling;
8207 }
8208 }
8209
8210 /* Functions used to compute the fully scoped name of a partial DIE.
8211
8212 Normally, this is simple. For C++, the parent DIE's fully scoped
8213 name is concatenated with "::" and the partial DIE's name.
8214 Enumerators are an exception; they use the scope of their parent
8215 enumeration type, i.e. the name of the enumeration type is not
8216 prepended to the enumerator.
8217
8218 There are two complexities. One is DW_AT_specification; in this
8219 case "parent" means the parent of the target of the specification,
8220 instead of the direct parent of the DIE. The other is compilers
8221 which do not emit DW_TAG_namespace; in this case we try to guess
8222 the fully qualified name of structure types from their members'
8223 linkage names. This must be done using the DIE's children rather
8224 than the children of any DW_AT_specification target. We only need
8225 to do this for structures at the top level, i.e. if the target of
8226 any DW_AT_specification (if any; otherwise the DIE itself) does not
8227 have a parent. */
8228
8229 /* Compute the scope prefix associated with PDI's parent, in
8230 compilation unit CU. The result will be allocated on CU's
8231 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8232 field. NULL is returned if no prefix is necessary. */
8233 static const char *
8234 partial_die_parent_scope (struct partial_die_info *pdi,
8235 struct dwarf2_cu *cu)
8236 {
8237 const char *grandparent_scope;
8238 struct partial_die_info *parent, *real_pdi;
8239
8240 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8241 then this means the parent of the specification DIE. */
8242
8243 real_pdi = pdi;
8244 while (real_pdi->has_specification)
8245 {
8246 auto res = find_partial_die (real_pdi->spec_offset,
8247 real_pdi->spec_is_dwz, cu);
8248 real_pdi = res.pdi;
8249 cu = res.cu;
8250 }
8251
8252 parent = real_pdi->die_parent;
8253 if (parent == NULL)
8254 return NULL;
8255
8256 if (parent->scope_set)
8257 return parent->scope;
8258
8259 parent->fixup (cu);
8260
8261 grandparent_scope = partial_die_parent_scope (parent, cu);
8262
8263 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8264 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8265 Work around this problem here. */
8266 if (cu->language == language_cplus
8267 && parent->tag == DW_TAG_namespace
8268 && strcmp (parent->name, "::") == 0
8269 && grandparent_scope == NULL)
8270 {
8271 parent->scope = NULL;
8272 parent->scope_set = 1;
8273 return NULL;
8274 }
8275
8276 /* Nested subroutines in Fortran get a prefix. */
8277 if (pdi->tag == DW_TAG_enumerator)
8278 /* Enumerators should not get the name of the enumeration as a prefix. */
8279 parent->scope = grandparent_scope;
8280 else if (parent->tag == DW_TAG_namespace
8281 || parent->tag == DW_TAG_module
8282 || parent->tag == DW_TAG_structure_type
8283 || parent->tag == DW_TAG_class_type
8284 || parent->tag == DW_TAG_interface_type
8285 || parent->tag == DW_TAG_union_type
8286 || parent->tag == DW_TAG_enumeration_type
8287 || (cu->language == language_fortran
8288 && parent->tag == DW_TAG_subprogram
8289 && pdi->tag == DW_TAG_subprogram))
8290 {
8291 if (grandparent_scope == NULL)
8292 parent->scope = parent->name;
8293 else
8294 parent->scope = typename_concat (&cu->comp_unit_obstack,
8295 grandparent_scope,
8296 parent->name, 0, cu);
8297 }
8298 else
8299 {
8300 /* FIXME drow/2004-04-01: What should we be doing with
8301 function-local names? For partial symbols, we should probably be
8302 ignoring them. */
8303 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8304 dwarf_tag_name (parent->tag),
8305 sect_offset_str (pdi->sect_off));
8306 parent->scope = grandparent_scope;
8307 }
8308
8309 parent->scope_set = 1;
8310 return parent->scope;
8311 }
8312
8313 /* Return the fully scoped name associated with PDI, from compilation unit
8314 CU. The result will be allocated with malloc. */
8315
8316 static gdb::unique_xmalloc_ptr<char>
8317 partial_die_full_name (struct partial_die_info *pdi,
8318 struct dwarf2_cu *cu)
8319 {
8320 const char *parent_scope;
8321
8322 /* If this is a template instantiation, we can not work out the
8323 template arguments from partial DIEs. So, unfortunately, we have
8324 to go through the full DIEs. At least any work we do building
8325 types here will be reused if full symbols are loaded later. */
8326 if (pdi->has_template_arguments)
8327 {
8328 pdi->fixup (cu);
8329
8330 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8331 {
8332 struct die_info *die;
8333 struct attribute attr;
8334 struct dwarf2_cu *ref_cu = cu;
8335
8336 /* DW_FORM_ref_addr is using section offset. */
8337 attr.name = (enum dwarf_attribute) 0;
8338 attr.form = DW_FORM_ref_addr;
8339 attr.u.unsnd = to_underlying (pdi->sect_off);
8340 die = follow_die_ref (NULL, &attr, &ref_cu);
8341
8342 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8343 }
8344 }
8345
8346 parent_scope = partial_die_parent_scope (pdi, cu);
8347 if (parent_scope == NULL)
8348 return NULL;
8349 else
8350 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8351 pdi->name, 0, cu));
8352 }
8353
8354 static void
8355 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8356 {
8357 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8358 struct objfile *objfile = dwarf2_per_objfile->objfile;
8359 struct gdbarch *gdbarch = objfile->arch ();
8360 CORE_ADDR addr = 0;
8361 const char *actual_name = NULL;
8362 CORE_ADDR baseaddr;
8363
8364 baseaddr = objfile->text_section_offset ();
8365
8366 gdb::unique_xmalloc_ptr<char> built_actual_name
8367 = partial_die_full_name (pdi, cu);
8368 if (built_actual_name != NULL)
8369 actual_name = built_actual_name.get ();
8370
8371 if (actual_name == NULL)
8372 actual_name = pdi->name;
8373
8374 partial_symbol psymbol;
8375 memset (&psymbol, 0, sizeof (psymbol));
8376 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8377 psymbol.ginfo.section = -1;
8378
8379 /* The code below indicates that the psymbol should be installed by
8380 setting this. */
8381 gdb::optional<psymbol_placement> where;
8382
8383 switch (pdi->tag)
8384 {
8385 case DW_TAG_inlined_subroutine:
8386 case DW_TAG_subprogram:
8387 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8388 - baseaddr);
8389 if (pdi->is_external
8390 || cu->language == language_ada
8391 || (cu->language == language_fortran
8392 && pdi->die_parent != NULL
8393 && pdi->die_parent->tag == DW_TAG_subprogram))
8394 {
8395 /* Normally, only "external" DIEs are part of the global scope.
8396 But in Ada and Fortran, we want to be able to access nested
8397 procedures globally. So all Ada and Fortran subprograms are
8398 stored in the global scope. */
8399 where = psymbol_placement::GLOBAL;
8400 }
8401 else
8402 where = psymbol_placement::STATIC;
8403
8404 psymbol.domain = VAR_DOMAIN;
8405 psymbol.aclass = LOC_BLOCK;
8406 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8407 psymbol.ginfo.value.address = addr;
8408
8409 if (pdi->main_subprogram && actual_name != NULL)
8410 set_objfile_main_name (objfile, actual_name, cu->language);
8411 break;
8412 case DW_TAG_constant:
8413 psymbol.domain = VAR_DOMAIN;
8414 psymbol.aclass = LOC_STATIC;
8415 where = (pdi->is_external
8416 ? psymbol_placement::GLOBAL
8417 : psymbol_placement::STATIC);
8418 break;
8419 case DW_TAG_variable:
8420 if (pdi->d.locdesc)
8421 addr = decode_locdesc (pdi->d.locdesc, cu);
8422
8423 if (pdi->d.locdesc
8424 && addr == 0
8425 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8426 {
8427 /* A global or static variable may also have been stripped
8428 out by the linker if unused, in which case its address
8429 will be nullified; do not add such variables into partial
8430 symbol table then. */
8431 }
8432 else if (pdi->is_external)
8433 {
8434 /* Global Variable.
8435 Don't enter into the minimal symbol tables as there is
8436 a minimal symbol table entry from the ELF symbols already.
8437 Enter into partial symbol table if it has a location
8438 descriptor or a type.
8439 If the location descriptor is missing, new_symbol will create
8440 a LOC_UNRESOLVED symbol, the address of the variable will then
8441 be determined from the minimal symbol table whenever the variable
8442 is referenced.
8443 The address for the partial symbol table entry is not
8444 used by GDB, but it comes in handy for debugging partial symbol
8445 table building. */
8446
8447 if (pdi->d.locdesc || pdi->has_type)
8448 {
8449 psymbol.domain = VAR_DOMAIN;
8450 psymbol.aclass = LOC_STATIC;
8451 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8452 psymbol.ginfo.value.address = addr;
8453 where = psymbol_placement::GLOBAL;
8454 }
8455 }
8456 else
8457 {
8458 int has_loc = pdi->d.locdesc != NULL;
8459
8460 /* Static Variable. Skip symbols whose value we cannot know (those
8461 without location descriptors or constant values). */
8462 if (!has_loc && !pdi->has_const_value)
8463 return;
8464
8465 psymbol.domain = VAR_DOMAIN;
8466 psymbol.aclass = LOC_STATIC;
8467 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8468 if (has_loc)
8469 psymbol.ginfo.value.address = addr;
8470 where = psymbol_placement::STATIC;
8471 }
8472 break;
8473 case DW_TAG_typedef:
8474 case DW_TAG_base_type:
8475 case DW_TAG_subrange_type:
8476 psymbol.domain = VAR_DOMAIN;
8477 psymbol.aclass = LOC_TYPEDEF;
8478 where = psymbol_placement::STATIC;
8479 break;
8480 case DW_TAG_imported_declaration:
8481 case DW_TAG_namespace:
8482 psymbol.domain = VAR_DOMAIN;
8483 psymbol.aclass = LOC_TYPEDEF;
8484 where = psymbol_placement::GLOBAL;
8485 break;
8486 case DW_TAG_module:
8487 /* With Fortran 77 there might be a "BLOCK DATA" module
8488 available without any name. If so, we skip the module as it
8489 doesn't bring any value. */
8490 if (actual_name != nullptr)
8491 {
8492 psymbol.domain = MODULE_DOMAIN;
8493 psymbol.aclass = LOC_TYPEDEF;
8494 where = psymbol_placement::GLOBAL;
8495 }
8496 break;
8497 case DW_TAG_class_type:
8498 case DW_TAG_interface_type:
8499 case DW_TAG_structure_type:
8500 case DW_TAG_union_type:
8501 case DW_TAG_enumeration_type:
8502 /* Skip external references. The DWARF standard says in the section
8503 about "Structure, Union, and Class Type Entries": "An incomplete
8504 structure, union or class type is represented by a structure,
8505 union or class entry that does not have a byte size attribute
8506 and that has a DW_AT_declaration attribute." */
8507 if (!pdi->has_byte_size && pdi->is_declaration)
8508 return;
8509
8510 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8511 static vs. global. */
8512 psymbol.domain = STRUCT_DOMAIN;
8513 psymbol.aclass = LOC_TYPEDEF;
8514 where = (cu->language == language_cplus
8515 ? psymbol_placement::GLOBAL
8516 : psymbol_placement::STATIC);
8517 break;
8518 case DW_TAG_enumerator:
8519 psymbol.domain = VAR_DOMAIN;
8520 psymbol.aclass = LOC_CONST;
8521 where = (cu->language == language_cplus
8522 ? psymbol_placement::GLOBAL
8523 : psymbol_placement::STATIC);
8524 break;
8525 default:
8526 break;
8527 }
8528
8529 if (where.has_value ())
8530 {
8531 if (built_actual_name != nullptr)
8532 actual_name = objfile->intern (actual_name);
8533 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8534 psymbol.ginfo.set_linkage_name (actual_name);
8535 else
8536 {
8537 psymbol.ginfo.set_demangled_name (actual_name,
8538 &objfile->objfile_obstack);
8539 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8540 }
8541 add_psymbol_to_list (psymbol, *where, objfile);
8542 }
8543 }
8544
8545 /* Read a partial die corresponding to a namespace; also, add a symbol
8546 corresponding to that namespace to the symbol table. NAMESPACE is
8547 the name of the enclosing namespace. */
8548
8549 static void
8550 add_partial_namespace (struct partial_die_info *pdi,
8551 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8552 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 namespace. */
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 Fortran module. */
8565
8566 static void
8567 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8568 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8569 {
8570 /* Add a symbol for the namespace. */
8571
8572 add_partial_symbol (pdi, cu);
8573
8574 /* Now scan partial symbols in that module. */
8575
8576 if (pdi->has_children)
8577 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8578 }
8579
8580 /* Read a partial die corresponding to a subprogram or an inlined
8581 subprogram and create a partial symbol for that subprogram.
8582 When the CU language allows it, this routine also defines a partial
8583 symbol for each nested subprogram that this subprogram contains.
8584 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8585 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8586
8587 PDI may also be a lexical block, in which case we simply search
8588 recursively for subprograms defined inside that lexical block.
8589 Again, this is only performed when the CU language allows this
8590 type of definitions. */
8591
8592 static void
8593 add_partial_subprogram (struct partial_die_info *pdi,
8594 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8595 int set_addrmap, struct dwarf2_cu *cu)
8596 {
8597 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8598 {
8599 if (pdi->has_pc_info)
8600 {
8601 if (pdi->lowpc < *lowpc)
8602 *lowpc = pdi->lowpc;
8603 if (pdi->highpc > *highpc)
8604 *highpc = pdi->highpc;
8605 if (set_addrmap)
8606 {
8607 struct objfile *objfile = cu->per_objfile->objfile;
8608 struct gdbarch *gdbarch = objfile->arch ();
8609 CORE_ADDR baseaddr;
8610 CORE_ADDR this_highpc;
8611 CORE_ADDR this_lowpc;
8612
8613 baseaddr = objfile->text_section_offset ();
8614 this_lowpc
8615 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8616 pdi->lowpc + baseaddr)
8617 - baseaddr);
8618 this_highpc
8619 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8620 pdi->highpc + baseaddr)
8621 - baseaddr);
8622 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8623 this_lowpc, this_highpc - 1,
8624 cu->per_cu->v.psymtab);
8625 }
8626 }
8627
8628 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8629 {
8630 if (!pdi->is_declaration)
8631 /* Ignore subprogram DIEs that do not have a name, they are
8632 illegal. Do not emit a complaint at this point, we will
8633 do so when we convert this psymtab into a symtab. */
8634 if (pdi->name)
8635 add_partial_symbol (pdi, cu);
8636 }
8637 }
8638
8639 if (! pdi->has_children)
8640 return;
8641
8642 if (cu->language == language_ada || cu->language == language_fortran)
8643 {
8644 pdi = pdi->die_child;
8645 while (pdi != NULL)
8646 {
8647 pdi->fixup (cu);
8648 if (pdi->tag == DW_TAG_subprogram
8649 || pdi->tag == DW_TAG_inlined_subroutine
8650 || pdi->tag == DW_TAG_lexical_block)
8651 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8652 pdi = pdi->die_sibling;
8653 }
8654 }
8655 }
8656
8657 /* Read a partial die corresponding to an enumeration type. */
8658
8659 static void
8660 add_partial_enumeration (struct partial_die_info *enum_pdi,
8661 struct dwarf2_cu *cu)
8662 {
8663 struct partial_die_info *pdi;
8664
8665 if (enum_pdi->name != NULL)
8666 add_partial_symbol (enum_pdi, cu);
8667
8668 pdi = enum_pdi->die_child;
8669 while (pdi)
8670 {
8671 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8672 complaint (_("malformed enumerator DIE ignored"));
8673 else
8674 add_partial_symbol (pdi, cu);
8675 pdi = pdi->die_sibling;
8676 }
8677 }
8678
8679 /* Return the initial uleb128 in the die at INFO_PTR. */
8680
8681 static unsigned int
8682 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8683 {
8684 unsigned int bytes_read;
8685
8686 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8687 }
8688
8689 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8690 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8691
8692 Return the corresponding abbrev, or NULL if the number is zero (indicating
8693 an empty DIE). In either case *BYTES_READ will be set to the length of
8694 the initial number. */
8695
8696 static struct abbrev_info *
8697 peek_die_abbrev (const die_reader_specs &reader,
8698 const gdb_byte *info_ptr, unsigned int *bytes_read)
8699 {
8700 dwarf2_cu *cu = reader.cu;
8701 bfd *abfd = cu->per_objfile->objfile->obfd;
8702 unsigned int abbrev_number
8703 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8704
8705 if (abbrev_number == 0)
8706 return NULL;
8707
8708 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8709 if (!abbrev)
8710 {
8711 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8712 " at offset %s [in module %s]"),
8713 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8714 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8715 }
8716
8717 return abbrev;
8718 }
8719
8720 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8721 Returns a pointer to the end of a series of DIEs, terminated by an empty
8722 DIE. Any children of the skipped DIEs will also be skipped. */
8723
8724 static const gdb_byte *
8725 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8726 {
8727 while (1)
8728 {
8729 unsigned int bytes_read;
8730 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8731
8732 if (abbrev == NULL)
8733 return info_ptr + bytes_read;
8734 else
8735 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8736 }
8737 }
8738
8739 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8740 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8741 abbrev corresponding to that skipped uleb128 should be passed in
8742 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8743 children. */
8744
8745 static const gdb_byte *
8746 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8747 struct abbrev_info *abbrev)
8748 {
8749 unsigned int bytes_read;
8750 struct attribute attr;
8751 bfd *abfd = reader->abfd;
8752 struct dwarf2_cu *cu = reader->cu;
8753 const gdb_byte *buffer = reader->buffer;
8754 const gdb_byte *buffer_end = reader->buffer_end;
8755 unsigned int form, i;
8756
8757 for (i = 0; i < abbrev->num_attrs; i++)
8758 {
8759 /* The only abbrev we care about is DW_AT_sibling. */
8760 if (abbrev->attrs[i].name == DW_AT_sibling)
8761 {
8762 bool ignored;
8763 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8764 &ignored);
8765 if (attr.form == DW_FORM_ref_addr)
8766 complaint (_("ignoring absolute DW_AT_sibling"));
8767 else
8768 {
8769 sect_offset off = attr.get_ref_die_offset ();
8770 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8771
8772 if (sibling_ptr < info_ptr)
8773 complaint (_("DW_AT_sibling points backwards"));
8774 else if (sibling_ptr > reader->buffer_end)
8775 reader->die_section->overflow_complaint ();
8776 else
8777 return sibling_ptr;
8778 }
8779 }
8780
8781 /* If it isn't DW_AT_sibling, skip this attribute. */
8782 form = abbrev->attrs[i].form;
8783 skip_attribute:
8784 switch (form)
8785 {
8786 case DW_FORM_ref_addr:
8787 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8788 and later it is offset sized. */
8789 if (cu->header.version == 2)
8790 info_ptr += cu->header.addr_size;
8791 else
8792 info_ptr += cu->header.offset_size;
8793 break;
8794 case DW_FORM_GNU_ref_alt:
8795 info_ptr += cu->header.offset_size;
8796 break;
8797 case DW_FORM_addr:
8798 info_ptr += cu->header.addr_size;
8799 break;
8800 case DW_FORM_data1:
8801 case DW_FORM_ref1:
8802 case DW_FORM_flag:
8803 case DW_FORM_strx1:
8804 info_ptr += 1;
8805 break;
8806 case DW_FORM_flag_present:
8807 case DW_FORM_implicit_const:
8808 break;
8809 case DW_FORM_data2:
8810 case DW_FORM_ref2:
8811 case DW_FORM_strx2:
8812 info_ptr += 2;
8813 break;
8814 case DW_FORM_strx3:
8815 info_ptr += 3;
8816 break;
8817 case DW_FORM_data4:
8818 case DW_FORM_ref4:
8819 case DW_FORM_strx4:
8820 info_ptr += 4;
8821 break;
8822 case DW_FORM_data8:
8823 case DW_FORM_ref8:
8824 case DW_FORM_ref_sig8:
8825 info_ptr += 8;
8826 break;
8827 case DW_FORM_data16:
8828 info_ptr += 16;
8829 break;
8830 case DW_FORM_string:
8831 read_direct_string (abfd, info_ptr, &bytes_read);
8832 info_ptr += bytes_read;
8833 break;
8834 case DW_FORM_sec_offset:
8835 case DW_FORM_strp:
8836 case DW_FORM_GNU_strp_alt:
8837 info_ptr += cu->header.offset_size;
8838 break;
8839 case DW_FORM_exprloc:
8840 case DW_FORM_block:
8841 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8842 info_ptr += bytes_read;
8843 break;
8844 case DW_FORM_block1:
8845 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8846 break;
8847 case DW_FORM_block2:
8848 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8849 break;
8850 case DW_FORM_block4:
8851 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8852 break;
8853 case DW_FORM_addrx:
8854 case DW_FORM_strx:
8855 case DW_FORM_sdata:
8856 case DW_FORM_udata:
8857 case DW_FORM_ref_udata:
8858 case DW_FORM_GNU_addr_index:
8859 case DW_FORM_GNU_str_index:
8860 case DW_FORM_rnglistx:
8861 case DW_FORM_loclistx:
8862 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8863 break;
8864 case DW_FORM_indirect:
8865 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8866 info_ptr += bytes_read;
8867 /* We need to continue parsing from here, so just go back to
8868 the top. */
8869 goto skip_attribute;
8870
8871 default:
8872 error (_("Dwarf Error: Cannot handle %s "
8873 "in DWARF reader [in module %s]"),
8874 dwarf_form_name (form),
8875 bfd_get_filename (abfd));
8876 }
8877 }
8878
8879 if (abbrev->has_children)
8880 return skip_children (reader, info_ptr);
8881 else
8882 return info_ptr;
8883 }
8884
8885 /* Locate ORIG_PDI's sibling.
8886 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8887
8888 static const gdb_byte *
8889 locate_pdi_sibling (const struct die_reader_specs *reader,
8890 struct partial_die_info *orig_pdi,
8891 const gdb_byte *info_ptr)
8892 {
8893 /* Do we know the sibling already? */
8894
8895 if (orig_pdi->sibling)
8896 return orig_pdi->sibling;
8897
8898 /* Are there any children to deal with? */
8899
8900 if (!orig_pdi->has_children)
8901 return info_ptr;
8902
8903 /* Skip the children the long way. */
8904
8905 return skip_children (reader, info_ptr);
8906 }
8907
8908 /* Expand this partial symbol table into a full symbol table. SELF is
8909 not NULL. */
8910
8911 void
8912 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8913 {
8914 struct dwarf2_per_objfile *dwarf2_per_objfile
8915 = get_dwarf2_per_objfile (objfile);
8916
8917 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8918
8919 /* If this psymtab is constructed from a debug-only objfile, the
8920 has_section_at_zero flag will not necessarily be correct. We
8921 can get the correct value for this flag by looking at the data
8922 associated with the (presumably stripped) associated objfile. */
8923 if (objfile->separate_debug_objfile_backlink)
8924 {
8925 struct dwarf2_per_objfile *dpo_backlink
8926 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8927
8928 dwarf2_per_objfile->per_bfd->has_section_at_zero
8929 = dpo_backlink->per_bfd->has_section_at_zero;
8930 }
8931
8932 expand_psymtab (objfile);
8933
8934 process_cu_includes (dwarf2_per_objfile);
8935 }
8936 \f
8937 /* Reading in full CUs. */
8938
8939 /* Add PER_CU to the queue. */
8940
8941 static void
8942 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8943 dwarf2_per_objfile *per_objfile,
8944 enum language pretend_language)
8945 {
8946 per_cu->queued = 1;
8947 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8948 }
8949
8950 /* If PER_CU is not yet queued, add it to the queue.
8951 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8952 dependency.
8953 The result is non-zero if PER_CU was queued, otherwise the result is zero
8954 meaning either PER_CU is already queued or it is already loaded.
8955
8956 N.B. There is an invariant here that if a CU is queued then it is loaded.
8957 The caller is required to load PER_CU if we return non-zero. */
8958
8959 static int
8960 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8961 dwarf2_per_cu_data *per_cu,
8962 dwarf2_per_objfile *per_objfile,
8963 enum language pretend_language)
8964 {
8965 /* We may arrive here during partial symbol reading, if we need full
8966 DIEs to process an unusual case (e.g. template arguments). Do
8967 not queue PER_CU, just tell our caller to load its DIEs. */
8968 if (per_cu->per_bfd->reading_partial_symbols)
8969 {
8970 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8971 return 1;
8972 return 0;
8973 }
8974
8975 /* Mark the dependence relation so that we don't flush PER_CU
8976 too early. */
8977 if (dependent_cu != NULL)
8978 dwarf2_add_dependence (dependent_cu, per_cu);
8979
8980 /* If it's already on the queue, we have nothing to do. */
8981 if (per_cu->queued)
8982 return 0;
8983
8984 /* If the compilation unit is already loaded, just mark it as
8985 used. */
8986 if (per_cu->cu != NULL)
8987 {
8988 per_cu->cu->last_used = 0;
8989 return 0;
8990 }
8991
8992 /* Add it to the queue. */
8993 queue_comp_unit (per_cu, per_objfile, pretend_language);
8994
8995 return 1;
8996 }
8997
8998 /* Process the queue. */
8999
9000 static void
9001 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9002 {
9003 if (dwarf_read_debug)
9004 {
9005 fprintf_unfiltered (gdb_stdlog,
9006 "Expanding one or more symtabs of objfile %s ...\n",
9007 objfile_name (dwarf2_per_objfile->objfile));
9008 }
9009
9010 /* The queue starts out with one item, but following a DIE reference
9011 may load a new CU, adding it to the end of the queue. */
9012 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
9013 {
9014 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
9015
9016 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
9017 /* Skip dummy CUs. */
9018 && item.per_cu->cu != NULL)
9019 {
9020 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9021 unsigned int debug_print_threshold;
9022 char buf[100];
9023
9024 if (per_cu->is_debug_types)
9025 {
9026 struct signatured_type *sig_type =
9027 (struct signatured_type *) per_cu;
9028
9029 sprintf (buf, "TU %s at offset %s",
9030 hex_string (sig_type->signature),
9031 sect_offset_str (per_cu->sect_off));
9032 /* There can be 100s of TUs.
9033 Only print them in verbose mode. */
9034 debug_print_threshold = 2;
9035 }
9036 else
9037 {
9038 sprintf (buf, "CU at offset %s",
9039 sect_offset_str (per_cu->sect_off));
9040 debug_print_threshold = 1;
9041 }
9042
9043 if (dwarf_read_debug >= debug_print_threshold)
9044 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9045
9046 if (per_cu->is_debug_types)
9047 process_full_type_unit (per_cu, dwarf2_per_objfile,
9048 item.pretend_language);
9049 else
9050 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9051 item.pretend_language);
9052
9053 if (dwarf_read_debug >= debug_print_threshold)
9054 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9055 }
9056
9057 item.per_cu->queued = 0;
9058 dwarf2_per_objfile->per_bfd->queue.pop ();
9059 }
9060
9061 if (dwarf_read_debug)
9062 {
9063 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9064 objfile_name (dwarf2_per_objfile->objfile));
9065 }
9066 }
9067
9068 /* Read in full symbols for PST, and anything it depends on. */
9069
9070 void
9071 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9072 {
9073 gdb_assert (!readin_p (objfile));
9074
9075 expand_dependencies (objfile);
9076
9077 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9078 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9079 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9080 }
9081
9082 /* See psympriv.h. */
9083
9084 bool
9085 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9086 {
9087 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9088 return per_objfile->symtab_set_p (per_cu_data);
9089 }
9090
9091 /* See psympriv.h. */
9092
9093 compunit_symtab *
9094 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9095 {
9096 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9097 return per_objfile->get_symtab (per_cu_data);
9098 }
9099
9100 /* Trivial hash function for die_info: the hash value of a DIE
9101 is its offset in .debug_info for this objfile. */
9102
9103 static hashval_t
9104 die_hash (const void *item)
9105 {
9106 const struct die_info *die = (const struct die_info *) item;
9107
9108 return to_underlying (die->sect_off);
9109 }
9110
9111 /* Trivial comparison function for die_info structures: two DIEs
9112 are equal if they have the same offset. */
9113
9114 static int
9115 die_eq (const void *item_lhs, const void *item_rhs)
9116 {
9117 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9118 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9119
9120 return die_lhs->sect_off == die_rhs->sect_off;
9121 }
9122
9123 /* Load the DIEs associated with PER_CU into memory. */
9124
9125 static void
9126 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9127 dwarf2_per_objfile *per_objfile,
9128 bool skip_partial,
9129 enum language pretend_language)
9130 {
9131 gdb_assert (! this_cu->is_debug_types);
9132
9133 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9134 if (reader.dummy_p)
9135 return;
9136
9137 struct dwarf2_cu *cu = reader.cu;
9138 const gdb_byte *info_ptr = reader.info_ptr;
9139
9140 gdb_assert (cu->die_hash == NULL);
9141 cu->die_hash =
9142 htab_create_alloc_ex (cu->header.length / 12,
9143 die_hash,
9144 die_eq,
9145 NULL,
9146 &cu->comp_unit_obstack,
9147 hashtab_obstack_allocate,
9148 dummy_obstack_deallocate);
9149
9150 if (reader.comp_unit_die->has_children)
9151 reader.comp_unit_die->child
9152 = read_die_and_siblings (&reader, reader.info_ptr,
9153 &info_ptr, reader.comp_unit_die);
9154 cu->dies = reader.comp_unit_die;
9155 /* comp_unit_die is not stored in die_hash, no need. */
9156
9157 /* We try not to read any attributes in this function, because not
9158 all CUs needed for references have been loaded yet, and symbol
9159 table processing isn't initialized. But we have to set the CU language,
9160 or we won't be able to build types correctly.
9161 Similarly, if we do not read the producer, we can not apply
9162 producer-specific interpretation. */
9163 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9164
9165 reader.keep ();
9166 }
9167
9168 /* Add a DIE to the delayed physname list. */
9169
9170 static void
9171 add_to_method_list (struct type *type, int fnfield_index, int index,
9172 const char *name, struct die_info *die,
9173 struct dwarf2_cu *cu)
9174 {
9175 struct delayed_method_info mi;
9176 mi.type = type;
9177 mi.fnfield_index = fnfield_index;
9178 mi.index = index;
9179 mi.name = name;
9180 mi.die = die;
9181 cu->method_list.push_back (mi);
9182 }
9183
9184 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9185 "const" / "volatile". If so, decrements LEN by the length of the
9186 modifier and return true. Otherwise return false. */
9187
9188 template<size_t N>
9189 static bool
9190 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9191 {
9192 size_t mod_len = sizeof (mod) - 1;
9193 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9194 {
9195 len -= mod_len;
9196 return true;
9197 }
9198 return false;
9199 }
9200
9201 /* Compute the physnames of any methods on the CU's method list.
9202
9203 The computation of method physnames is delayed in order to avoid the
9204 (bad) condition that one of the method's formal parameters is of an as yet
9205 incomplete type. */
9206
9207 static void
9208 compute_delayed_physnames (struct dwarf2_cu *cu)
9209 {
9210 /* Only C++ delays computing physnames. */
9211 if (cu->method_list.empty ())
9212 return;
9213 gdb_assert (cu->language == language_cplus);
9214
9215 for (const delayed_method_info &mi : cu->method_list)
9216 {
9217 const char *physname;
9218 struct fn_fieldlist *fn_flp
9219 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9220 physname = dwarf2_physname (mi.name, mi.die, cu);
9221 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9222 = physname ? physname : "";
9223
9224 /* Since there's no tag to indicate whether a method is a
9225 const/volatile overload, extract that information out of the
9226 demangled name. */
9227 if (physname != NULL)
9228 {
9229 size_t len = strlen (physname);
9230
9231 while (1)
9232 {
9233 if (physname[len] == ')') /* shortcut */
9234 break;
9235 else if (check_modifier (physname, len, " const"))
9236 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9237 else if (check_modifier (physname, len, " volatile"))
9238 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9239 else
9240 break;
9241 }
9242 }
9243 }
9244
9245 /* The list is no longer needed. */
9246 cu->method_list.clear ();
9247 }
9248
9249 /* Go objects should be embedded in a DW_TAG_module DIE,
9250 and it's not clear if/how imported objects will appear.
9251 To keep Go support simple until that's worked out,
9252 go back through what we've read and create something usable.
9253 We could do this while processing each DIE, and feels kinda cleaner,
9254 but that way is more invasive.
9255 This is to, for example, allow the user to type "p var" or "b main"
9256 without having to specify the package name, and allow lookups
9257 of module.object to work in contexts that use the expression
9258 parser. */
9259
9260 static void
9261 fixup_go_packaging (struct dwarf2_cu *cu)
9262 {
9263 gdb::unique_xmalloc_ptr<char> package_name;
9264 struct pending *list;
9265 int i;
9266
9267 for (list = *cu->get_builder ()->get_global_symbols ();
9268 list != NULL;
9269 list = list->next)
9270 {
9271 for (i = 0; i < list->nsyms; ++i)
9272 {
9273 struct symbol *sym = list->symbol[i];
9274
9275 if (sym->language () == language_go
9276 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9277 {
9278 gdb::unique_xmalloc_ptr<char> this_package_name
9279 (go_symbol_package_name (sym));
9280
9281 if (this_package_name == NULL)
9282 continue;
9283 if (package_name == NULL)
9284 package_name = std::move (this_package_name);
9285 else
9286 {
9287 struct objfile *objfile = cu->per_objfile->objfile;
9288 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9289 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9290 (symbol_symtab (sym) != NULL
9291 ? symtab_to_filename_for_display
9292 (symbol_symtab (sym))
9293 : objfile_name (objfile)),
9294 this_package_name.get (), package_name.get ());
9295 }
9296 }
9297 }
9298 }
9299
9300 if (package_name != NULL)
9301 {
9302 struct objfile *objfile = cu->per_objfile->objfile;
9303 const char *saved_package_name = objfile->intern (package_name.get ());
9304 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9305 saved_package_name);
9306 struct symbol *sym;
9307
9308 sym = new (&objfile->objfile_obstack) symbol;
9309 sym->set_language (language_go, &objfile->objfile_obstack);
9310 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9311 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9312 e.g., "main" finds the "main" module and not C's main(). */
9313 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9314 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9315 SYMBOL_TYPE (sym) = type;
9316
9317 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9318 }
9319 }
9320
9321 /* Allocate a fully-qualified name consisting of the two parts on the
9322 obstack. */
9323
9324 static const char *
9325 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9326 {
9327 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9328 }
9329
9330 /* A helper that allocates a variant part to attach to a Rust enum
9331 type. OBSTACK is where the results should be allocated. TYPE is
9332 the type we're processing. DISCRIMINANT_INDEX is the index of the
9333 discriminant. It must be the index of one of the fields of TYPE.
9334 DEFAULT_INDEX is the index of the default field; or -1 if there is
9335 no default. RANGES is indexed by "effective" field number (the
9336 field index, but omitting the discriminant and default fields) and
9337 must hold the discriminant values used by the variants. Note that
9338 RANGES must have a lifetime at least as long as OBSTACK -- either
9339 already allocated on it, or static. */
9340
9341 static void
9342 alloc_rust_variant (struct obstack *obstack, struct type *type,
9343 int discriminant_index, int default_index,
9344 gdb::array_view<discriminant_range> ranges)
9345 {
9346 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9347 must be handled by the caller. */
9348 gdb_assert (discriminant_index >= 0
9349 && discriminant_index < type->num_fields ());
9350 gdb_assert (default_index == -1
9351 || (default_index >= 0 && default_index < type->num_fields ()));
9352
9353 /* We have one variant for each non-discriminant field. */
9354 int n_variants = type->num_fields () - 1;
9355
9356 variant *variants = new (obstack) variant[n_variants];
9357 int var_idx = 0;
9358 int range_idx = 0;
9359 for (int i = 0; i < type->num_fields (); ++i)
9360 {
9361 if (i == discriminant_index)
9362 continue;
9363
9364 variants[var_idx].first_field = i;
9365 variants[var_idx].last_field = i + 1;
9366
9367 /* The default field does not need a range, but other fields do.
9368 We skipped the discriminant above. */
9369 if (i != default_index)
9370 {
9371 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9372 ++range_idx;
9373 }
9374
9375 ++var_idx;
9376 }
9377
9378 gdb_assert (range_idx == ranges.size ());
9379 gdb_assert (var_idx == n_variants);
9380
9381 variant_part *part = new (obstack) variant_part;
9382 part->discriminant_index = discriminant_index;
9383 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9384 discriminant_index));
9385 part->variants = gdb::array_view<variant> (variants, n_variants);
9386
9387 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9388 gdb::array_view<variant_part> *prop_value
9389 = new (storage) gdb::array_view<variant_part> (part, 1);
9390
9391 struct dynamic_prop prop;
9392 prop.kind = PROP_VARIANT_PARTS;
9393 prop.data.variant_parts = prop_value;
9394
9395 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9396 }
9397
9398 /* Some versions of rustc emitted enums in an unusual way.
9399
9400 Ordinary enums were emitted as unions. The first element of each
9401 structure in the union was named "RUST$ENUM$DISR". This element
9402 held the discriminant.
9403
9404 These versions of Rust also implemented the "non-zero"
9405 optimization. When the enum had two values, and one is empty and
9406 the other holds a pointer that cannot be zero, the pointer is used
9407 as the discriminant, with a zero value meaning the empty variant.
9408 Here, the union's first member is of the form
9409 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9410 where the fieldnos are the indices of the fields that should be
9411 traversed in order to find the field (which may be several fields deep)
9412 and the variantname is the name of the variant of the case when the
9413 field is zero.
9414
9415 This function recognizes whether TYPE is of one of these forms,
9416 and, if so, smashes it to be a variant type. */
9417
9418 static void
9419 quirk_rust_enum (struct type *type, struct objfile *objfile)
9420 {
9421 gdb_assert (type->code () == TYPE_CODE_UNION);
9422
9423 /* We don't need to deal with empty enums. */
9424 if (type->num_fields () == 0)
9425 return;
9426
9427 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9428 if (type->num_fields () == 1
9429 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9430 {
9431 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9432
9433 /* Decode the field name to find the offset of the
9434 discriminant. */
9435 ULONGEST bit_offset = 0;
9436 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9437 while (name[0] >= '0' && name[0] <= '9')
9438 {
9439 char *tail;
9440 unsigned long index = strtoul (name, &tail, 10);
9441 name = tail;
9442 if (*name != '$'
9443 || index >= field_type->num_fields ()
9444 || (TYPE_FIELD_LOC_KIND (field_type, index)
9445 != FIELD_LOC_KIND_BITPOS))
9446 {
9447 complaint (_("Could not parse Rust enum encoding string \"%s\""
9448 "[in module %s]"),
9449 TYPE_FIELD_NAME (type, 0),
9450 objfile_name (objfile));
9451 return;
9452 }
9453 ++name;
9454
9455 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9456 field_type = TYPE_FIELD_TYPE (field_type, index);
9457 }
9458
9459 /* Smash this type to be a structure type. We have to do this
9460 because the type has already been recorded. */
9461 type->set_code (TYPE_CODE_STRUCT);
9462 type->set_num_fields (3);
9463 /* Save the field we care about. */
9464 struct field saved_field = type->field (0);
9465 type->set_fields
9466 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9467
9468 /* Put the discriminant at index 0. */
9469 TYPE_FIELD_TYPE (type, 0) = field_type;
9470 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9471 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9472 SET_FIELD_BITPOS (type->field (0), bit_offset);
9473
9474 /* The order of fields doesn't really matter, so put the real
9475 field at index 1 and the data-less field at index 2. */
9476 type->field (1) = saved_field;
9477 TYPE_FIELD_NAME (type, 1)
9478 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9479 TYPE_FIELD_TYPE (type, 1)->set_name
9480 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9481 TYPE_FIELD_NAME (type, 1)));
9482
9483 const char *dataless_name
9484 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9485 name);
9486 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9487 dataless_name);
9488 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9489 /* NAME points into the original discriminant name, which
9490 already has the correct lifetime. */
9491 TYPE_FIELD_NAME (type, 2) = name;
9492 SET_FIELD_BITPOS (type->field (2), 0);
9493
9494 /* Indicate that this is a variant type. */
9495 static discriminant_range ranges[1] = { { 0, 0 } };
9496 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9497 }
9498 /* A union with a single anonymous field is probably an old-style
9499 univariant enum. */
9500 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9501 {
9502 /* Smash this type to be a structure type. We have to do this
9503 because the type has already been recorded. */
9504 type->set_code (TYPE_CODE_STRUCT);
9505
9506 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9507 const char *variant_name
9508 = rust_last_path_segment (field_type->name ());
9509 TYPE_FIELD_NAME (type, 0) = variant_name;
9510 field_type->set_name
9511 (rust_fully_qualify (&objfile->objfile_obstack,
9512 type->name (), variant_name));
9513 }
9514 else
9515 {
9516 struct type *disr_type = nullptr;
9517 for (int i = 0; i < type->num_fields (); ++i)
9518 {
9519 disr_type = TYPE_FIELD_TYPE (type, i);
9520
9521 if (disr_type->code () != TYPE_CODE_STRUCT)
9522 {
9523 /* All fields of a true enum will be structs. */
9524 return;
9525 }
9526 else if (disr_type->num_fields () == 0)
9527 {
9528 /* Could be data-less variant, so keep going. */
9529 disr_type = nullptr;
9530 }
9531 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9532 "RUST$ENUM$DISR") != 0)
9533 {
9534 /* Not a Rust enum. */
9535 return;
9536 }
9537 else
9538 {
9539 /* Found one. */
9540 break;
9541 }
9542 }
9543
9544 /* If we got here without a discriminant, then it's probably
9545 just a union. */
9546 if (disr_type == nullptr)
9547 return;
9548
9549 /* Smash this type to be a structure type. We have to do this
9550 because the type has already been recorded. */
9551 type->set_code (TYPE_CODE_STRUCT);
9552
9553 /* Make space for the discriminant field. */
9554 struct field *disr_field = &disr_type->field (0);
9555 field *new_fields
9556 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9557 * sizeof (struct field)));
9558 memcpy (new_fields + 1, type->fields (),
9559 type->num_fields () * sizeof (struct field));
9560 type->set_fields (new_fields);
9561 type->set_num_fields (type->num_fields () + 1);
9562
9563 /* Install the discriminant at index 0 in the union. */
9564 type->field (0) = *disr_field;
9565 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9566 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9567
9568 /* We need a way to find the correct discriminant given a
9569 variant name. For convenience we build a map here. */
9570 struct type *enum_type = FIELD_TYPE (*disr_field);
9571 std::unordered_map<std::string, ULONGEST> discriminant_map;
9572 for (int i = 0; i < enum_type->num_fields (); ++i)
9573 {
9574 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9575 {
9576 const char *name
9577 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9578 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9579 }
9580 }
9581
9582 int n_fields = type->num_fields ();
9583 /* We don't need a range entry for the discriminant, but we do
9584 need one for every other field, as there is no default
9585 variant. */
9586 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9587 discriminant_range,
9588 n_fields - 1);
9589 /* Skip the discriminant here. */
9590 for (int i = 1; i < n_fields; ++i)
9591 {
9592 /* Find the final word in the name of this variant's type.
9593 That name can be used to look up the correct
9594 discriminant. */
9595 const char *variant_name
9596 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9597
9598 auto iter = discriminant_map.find (variant_name);
9599 if (iter != discriminant_map.end ())
9600 {
9601 ranges[i].low = iter->second;
9602 ranges[i].high = iter->second;
9603 }
9604
9605 /* Remove the discriminant field, if it exists. */
9606 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9607 if (sub_type->num_fields () > 0)
9608 {
9609 sub_type->set_num_fields (sub_type->num_fields () - 1);
9610 sub_type->set_fields (sub_type->fields () + 1);
9611 }
9612 TYPE_FIELD_NAME (type, i) = variant_name;
9613 sub_type->set_name
9614 (rust_fully_qualify (&objfile->objfile_obstack,
9615 type->name (), variant_name));
9616 }
9617
9618 /* Indicate that this is a variant type. */
9619 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9620 gdb::array_view<discriminant_range> (ranges,
9621 n_fields - 1));
9622 }
9623 }
9624
9625 /* Rewrite some Rust unions to be structures with variants parts. */
9626
9627 static void
9628 rust_union_quirks (struct dwarf2_cu *cu)
9629 {
9630 gdb_assert (cu->language == language_rust);
9631 for (type *type_ : cu->rust_unions)
9632 quirk_rust_enum (type_, cu->per_objfile->objfile);
9633 /* We don't need this any more. */
9634 cu->rust_unions.clear ();
9635 }
9636
9637 /* A helper function for computing the list of all symbol tables
9638 included by PER_CU. */
9639
9640 static void
9641 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9642 htab_t all_children, htab_t all_type_symtabs,
9643 dwarf2_per_cu_data *per_cu,
9644 dwarf2_per_objfile *per_objfile,
9645 struct compunit_symtab *immediate_parent)
9646 {
9647 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9648 if (*slot != NULL)
9649 {
9650 /* This inclusion and its children have been processed. */
9651 return;
9652 }
9653
9654 *slot = per_cu;
9655
9656 /* Only add a CU if it has a symbol table. */
9657 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9658 if (cust != NULL)
9659 {
9660 /* If this is a type unit only add its symbol table if we haven't
9661 seen it yet (type unit per_cu's can share symtabs). */
9662 if (per_cu->is_debug_types)
9663 {
9664 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9665 if (*slot == NULL)
9666 {
9667 *slot = cust;
9668 result->push_back (cust);
9669 if (cust->user == NULL)
9670 cust->user = immediate_parent;
9671 }
9672 }
9673 else
9674 {
9675 result->push_back (cust);
9676 if (cust->user == NULL)
9677 cust->user = immediate_parent;
9678 }
9679 }
9680
9681 if (!per_cu->imported_symtabs_empty ())
9682 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9683 {
9684 recursively_compute_inclusions (result, all_children,
9685 all_type_symtabs, ptr, per_objfile,
9686 cust);
9687 }
9688 }
9689
9690 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9691 PER_CU. */
9692
9693 static void
9694 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9695 dwarf2_per_objfile *per_objfile)
9696 {
9697 gdb_assert (! per_cu->is_debug_types);
9698
9699 if (!per_cu->imported_symtabs_empty ())
9700 {
9701 int len;
9702 std::vector<compunit_symtab *> result_symtabs;
9703 htab_t all_children, all_type_symtabs;
9704 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9705
9706 /* If we don't have a symtab, we can just skip this case. */
9707 if (cust == NULL)
9708 return;
9709
9710 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9711 NULL, xcalloc, xfree);
9712 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9713 NULL, xcalloc, xfree);
9714
9715 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9716 {
9717 recursively_compute_inclusions (&result_symtabs, all_children,
9718 all_type_symtabs, ptr, per_objfile,
9719 cust);
9720 }
9721
9722 /* Now we have a transitive closure of all the included symtabs. */
9723 len = result_symtabs.size ();
9724 cust->includes
9725 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9726 struct compunit_symtab *, len + 1);
9727 memcpy (cust->includes, result_symtabs.data (),
9728 len * sizeof (compunit_symtab *));
9729 cust->includes[len] = NULL;
9730
9731 htab_delete (all_children);
9732 htab_delete (all_type_symtabs);
9733 }
9734 }
9735
9736 /* Compute the 'includes' field for the symtabs of all the CUs we just
9737 read. */
9738
9739 static void
9740 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9741 {
9742 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9743 {
9744 if (! iter->is_debug_types)
9745 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9746 }
9747
9748 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9749 }
9750
9751 /* Generate full symbol information for PER_CU, whose DIEs have
9752 already been loaded into memory. */
9753
9754 static void
9755 process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9756 dwarf2_per_objfile *dwarf2_per_objfile,
9757 enum language pretend_language)
9758 {
9759 struct dwarf2_cu *cu = per_cu->cu;
9760 struct objfile *objfile = dwarf2_per_objfile->objfile;
9761 struct gdbarch *gdbarch = objfile->arch ();
9762 CORE_ADDR lowpc, highpc;
9763 struct compunit_symtab *cust;
9764 CORE_ADDR baseaddr;
9765 struct block *static_block;
9766 CORE_ADDR addr;
9767
9768 baseaddr = objfile->text_section_offset ();
9769
9770 /* Clear the list here in case something was left over. */
9771 cu->method_list.clear ();
9772
9773 cu->language = pretend_language;
9774 cu->language_defn = language_def (cu->language);
9775
9776 /* Do line number decoding in read_file_scope () */
9777 process_die (cu->dies, cu);
9778
9779 /* For now fudge the Go package. */
9780 if (cu->language == language_go)
9781 fixup_go_packaging (cu);
9782
9783 /* Now that we have processed all the DIEs in the CU, all the types
9784 should be complete, and it should now be safe to compute all of the
9785 physnames. */
9786 compute_delayed_physnames (cu);
9787
9788 if (cu->language == language_rust)
9789 rust_union_quirks (cu);
9790
9791 /* Some compilers don't define a DW_AT_high_pc attribute for the
9792 compilation unit. If the DW_AT_high_pc is missing, synthesize
9793 it, by scanning the DIE's below the compilation unit. */
9794 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9795
9796 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9797 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9798
9799 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9800 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9801 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9802 addrmap to help ensure it has an accurate map of pc values belonging to
9803 this comp unit. */
9804 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9805
9806 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9807 SECT_OFF_TEXT (objfile),
9808 0);
9809
9810 if (cust != NULL)
9811 {
9812 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9813
9814 /* Set symtab language to language from DW_AT_language. If the
9815 compilation is from a C file generated by language preprocessors, do
9816 not set the language if it was already deduced by start_subfile. */
9817 if (!(cu->language == language_c
9818 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9819 COMPUNIT_FILETABS (cust)->language = cu->language;
9820
9821 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9822 produce DW_AT_location with location lists but it can be possibly
9823 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9824 there were bugs in prologue debug info, fixed later in GCC-4.5
9825 by "unwind info for epilogues" patch (which is not directly related).
9826
9827 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9828 needed, it would be wrong due to missing DW_AT_producer there.
9829
9830 Still one can confuse GDB by using non-standard GCC compilation
9831 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9832 */
9833 if (cu->has_loclist && gcc_4_minor >= 5)
9834 cust->locations_valid = 1;
9835
9836 if (gcc_4_minor >= 5)
9837 cust->epilogue_unwind_valid = 1;
9838
9839 cust->call_site_htab = cu->call_site_htab;
9840 }
9841
9842 dwarf2_per_objfile->set_symtab (per_cu, cust);
9843
9844 /* Push it for inclusion processing later. */
9845 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9846
9847 /* Not needed any more. */
9848 cu->reset_builder ();
9849 }
9850
9851 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9852 already been loaded into memory. */
9853
9854 static void
9855 process_full_type_unit (dwarf2_per_cu_data *per_cu,
9856 dwarf2_per_objfile *dwarf2_per_objfile,
9857 enum language pretend_language)
9858 {
9859 struct dwarf2_cu *cu = per_cu->cu;
9860 struct objfile *objfile = dwarf2_per_objfile->objfile;
9861 struct compunit_symtab *cust;
9862 struct signatured_type *sig_type;
9863
9864 gdb_assert (per_cu->is_debug_types);
9865 sig_type = (struct signatured_type *) per_cu;
9866
9867 /* Clear the list here in case something was left over. */
9868 cu->method_list.clear ();
9869
9870 cu->language = pretend_language;
9871 cu->language_defn = language_def (cu->language);
9872
9873 /* The symbol tables are set up in read_type_unit_scope. */
9874 process_die (cu->dies, cu);
9875
9876 /* For now fudge the Go package. */
9877 if (cu->language == language_go)
9878 fixup_go_packaging (cu);
9879
9880 /* Now that we have processed all the DIEs in the CU, all the types
9881 should be complete, and it should now be safe to compute all of the
9882 physnames. */
9883 compute_delayed_physnames (cu);
9884
9885 if (cu->language == language_rust)
9886 rust_union_quirks (cu);
9887
9888 /* TUs share symbol tables.
9889 If this is the first TU to use this symtab, complete the construction
9890 of it with end_expandable_symtab. Otherwise, complete the addition of
9891 this TU's symbols to the existing symtab. */
9892 if (sig_type->type_unit_group->compunit_symtab == NULL)
9893 {
9894 buildsym_compunit *builder = cu->get_builder ();
9895 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9896 sig_type->type_unit_group->compunit_symtab = cust;
9897
9898 if (cust != NULL)
9899 {
9900 /* Set symtab language to language from DW_AT_language. If the
9901 compilation is from a C file generated by language preprocessors,
9902 do not set the language if it was already deduced by
9903 start_subfile. */
9904 if (!(cu->language == language_c
9905 && COMPUNIT_FILETABS (cust)->language != language_c))
9906 COMPUNIT_FILETABS (cust)->language = cu->language;
9907 }
9908 }
9909 else
9910 {
9911 cu->get_builder ()->augment_type_symtab ();
9912 cust = sig_type->type_unit_group->compunit_symtab;
9913 }
9914
9915 dwarf2_per_objfile->set_symtab (per_cu, cust);
9916
9917 /* Not needed any more. */
9918 cu->reset_builder ();
9919 }
9920
9921 /* Process an imported unit DIE. */
9922
9923 static void
9924 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9925 {
9926 struct attribute *attr;
9927
9928 /* For now we don't handle imported units in type units. */
9929 if (cu->per_cu->is_debug_types)
9930 {
9931 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9932 " supported in type units [in module %s]"),
9933 objfile_name (cu->per_objfile->objfile));
9934 }
9935
9936 attr = dwarf2_attr (die, DW_AT_import, cu);
9937 if (attr != NULL)
9938 {
9939 sect_offset sect_off = attr->get_ref_die_offset ();
9940 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9941 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9942 dwarf2_per_cu_data *per_cu
9943 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9944
9945 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9946 into another compilation unit, at root level. Regard this as a hint,
9947 and ignore it. */
9948 if (die->parent && die->parent->parent == NULL
9949 && per_cu->unit_type == DW_UT_compile
9950 && per_cu->lang == language_cplus)
9951 return;
9952
9953 /* If necessary, add it to the queue and load its DIEs. */
9954 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9955 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9956
9957 cu->per_cu->imported_symtabs_push (per_cu);
9958 }
9959 }
9960
9961 /* RAII object that represents a process_die scope: i.e.,
9962 starts/finishes processing a DIE. */
9963 class process_die_scope
9964 {
9965 public:
9966 process_die_scope (die_info *die, dwarf2_cu *cu)
9967 : m_die (die), m_cu (cu)
9968 {
9969 /* We should only be processing DIEs not already in process. */
9970 gdb_assert (!m_die->in_process);
9971 m_die->in_process = true;
9972 }
9973
9974 ~process_die_scope ()
9975 {
9976 m_die->in_process = false;
9977
9978 /* If we're done processing the DIE for the CU that owns the line
9979 header, we don't need the line header anymore. */
9980 if (m_cu->line_header_die_owner == m_die)
9981 {
9982 delete m_cu->line_header;
9983 m_cu->line_header = NULL;
9984 m_cu->line_header_die_owner = NULL;
9985 }
9986 }
9987
9988 private:
9989 die_info *m_die;
9990 dwarf2_cu *m_cu;
9991 };
9992
9993 /* Process a die and its children. */
9994
9995 static void
9996 process_die (struct die_info *die, struct dwarf2_cu *cu)
9997 {
9998 process_die_scope scope (die, cu);
9999
10000 switch (die->tag)
10001 {
10002 case DW_TAG_padding:
10003 break;
10004 case DW_TAG_compile_unit:
10005 case DW_TAG_partial_unit:
10006 read_file_scope (die, cu);
10007 break;
10008 case DW_TAG_type_unit:
10009 read_type_unit_scope (die, cu);
10010 break;
10011 case DW_TAG_subprogram:
10012 /* Nested subprograms in Fortran get a prefix. */
10013 if (cu->language == language_fortran
10014 && die->parent != NULL
10015 && die->parent->tag == DW_TAG_subprogram)
10016 cu->processing_has_namespace_info = true;
10017 /* Fall through. */
10018 case DW_TAG_inlined_subroutine:
10019 read_func_scope (die, cu);
10020 break;
10021 case DW_TAG_lexical_block:
10022 case DW_TAG_try_block:
10023 case DW_TAG_catch_block:
10024 read_lexical_block_scope (die, cu);
10025 break;
10026 case DW_TAG_call_site:
10027 case DW_TAG_GNU_call_site:
10028 read_call_site_scope (die, cu);
10029 break;
10030 case DW_TAG_class_type:
10031 case DW_TAG_interface_type:
10032 case DW_TAG_structure_type:
10033 case DW_TAG_union_type:
10034 process_structure_scope (die, cu);
10035 break;
10036 case DW_TAG_enumeration_type:
10037 process_enumeration_scope (die, cu);
10038 break;
10039
10040 /* These dies have a type, but processing them does not create
10041 a symbol or recurse to process the children. Therefore we can
10042 read them on-demand through read_type_die. */
10043 case DW_TAG_subroutine_type:
10044 case DW_TAG_set_type:
10045 case DW_TAG_array_type:
10046 case DW_TAG_pointer_type:
10047 case DW_TAG_ptr_to_member_type:
10048 case DW_TAG_reference_type:
10049 case DW_TAG_rvalue_reference_type:
10050 case DW_TAG_string_type:
10051 break;
10052
10053 case DW_TAG_base_type:
10054 case DW_TAG_subrange_type:
10055 case DW_TAG_typedef:
10056 /* Add a typedef symbol for the type definition, if it has a
10057 DW_AT_name. */
10058 new_symbol (die, read_type_die (die, cu), cu);
10059 break;
10060 case DW_TAG_common_block:
10061 read_common_block (die, cu);
10062 break;
10063 case DW_TAG_common_inclusion:
10064 break;
10065 case DW_TAG_namespace:
10066 cu->processing_has_namespace_info = true;
10067 read_namespace (die, cu);
10068 break;
10069 case DW_TAG_module:
10070 cu->processing_has_namespace_info = true;
10071 read_module (die, cu);
10072 break;
10073 case DW_TAG_imported_declaration:
10074 cu->processing_has_namespace_info = true;
10075 if (read_namespace_alias (die, cu))
10076 break;
10077 /* The declaration is not a global namespace alias. */
10078 /* Fall through. */
10079 case DW_TAG_imported_module:
10080 cu->processing_has_namespace_info = true;
10081 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10082 || cu->language != language_fortran))
10083 complaint (_("Tag '%s' has unexpected children"),
10084 dwarf_tag_name (die->tag));
10085 read_import_statement (die, cu);
10086 break;
10087
10088 case DW_TAG_imported_unit:
10089 process_imported_unit_die (die, cu);
10090 break;
10091
10092 case DW_TAG_variable:
10093 read_variable (die, cu);
10094 break;
10095
10096 default:
10097 new_symbol (die, NULL, cu);
10098 break;
10099 }
10100 }
10101 \f
10102 /* DWARF name computation. */
10103
10104 /* A helper function for dwarf2_compute_name which determines whether DIE
10105 needs to have the name of the scope prepended to the name listed in the
10106 die. */
10107
10108 static int
10109 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10110 {
10111 struct attribute *attr;
10112
10113 switch (die->tag)
10114 {
10115 case DW_TAG_namespace:
10116 case DW_TAG_typedef:
10117 case DW_TAG_class_type:
10118 case DW_TAG_interface_type:
10119 case DW_TAG_structure_type:
10120 case DW_TAG_union_type:
10121 case DW_TAG_enumeration_type:
10122 case DW_TAG_enumerator:
10123 case DW_TAG_subprogram:
10124 case DW_TAG_inlined_subroutine:
10125 case DW_TAG_member:
10126 case DW_TAG_imported_declaration:
10127 return 1;
10128
10129 case DW_TAG_variable:
10130 case DW_TAG_constant:
10131 /* We only need to prefix "globally" visible variables. These include
10132 any variable marked with DW_AT_external or any variable that
10133 lives in a namespace. [Variables in anonymous namespaces
10134 require prefixing, but they are not DW_AT_external.] */
10135
10136 if (dwarf2_attr (die, DW_AT_specification, cu))
10137 {
10138 struct dwarf2_cu *spec_cu = cu;
10139
10140 return die_needs_namespace (die_specification (die, &spec_cu),
10141 spec_cu);
10142 }
10143
10144 attr = dwarf2_attr (die, DW_AT_external, cu);
10145 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10146 && die->parent->tag != DW_TAG_module)
10147 return 0;
10148 /* A variable in a lexical block of some kind does not need a
10149 namespace, even though in C++ such variables may be external
10150 and have a mangled name. */
10151 if (die->parent->tag == DW_TAG_lexical_block
10152 || die->parent->tag == DW_TAG_try_block
10153 || die->parent->tag == DW_TAG_catch_block
10154 || die->parent->tag == DW_TAG_subprogram)
10155 return 0;
10156 return 1;
10157
10158 default:
10159 return 0;
10160 }
10161 }
10162
10163 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10164 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10165 defined for the given DIE. */
10166
10167 static struct attribute *
10168 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10169 {
10170 struct attribute *attr;
10171
10172 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10173 if (attr == NULL)
10174 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10175
10176 return attr;
10177 }
10178
10179 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10180 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10181 defined for the given DIE. */
10182
10183 static const char *
10184 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10185 {
10186 const char *linkage_name;
10187
10188 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10189 if (linkage_name == NULL)
10190 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10191
10192 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10193 See https://github.com/rust-lang/rust/issues/32925. */
10194 if (cu->language == language_rust && linkage_name != NULL
10195 && strchr (linkage_name, '{') != NULL)
10196 linkage_name = NULL;
10197
10198 return linkage_name;
10199 }
10200
10201 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10202 compute the physname for the object, which include a method's:
10203 - formal parameters (C++),
10204 - receiver type (Go),
10205
10206 The term "physname" is a bit confusing.
10207 For C++, for example, it is the demangled name.
10208 For Go, for example, it's the mangled name.
10209
10210 For Ada, return the DIE's linkage name rather than the fully qualified
10211 name. PHYSNAME is ignored..
10212
10213 The result is allocated on the objfile->per_bfd's obstack and
10214 canonicalized. */
10215
10216 static const char *
10217 dwarf2_compute_name (const char *name,
10218 struct die_info *die, struct dwarf2_cu *cu,
10219 int physname)
10220 {
10221 struct objfile *objfile = cu->per_objfile->objfile;
10222
10223 if (name == NULL)
10224 name = dwarf2_name (die, cu);
10225
10226 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10227 but otherwise compute it by typename_concat inside GDB.
10228 FIXME: Actually this is not really true, or at least not always true.
10229 It's all very confusing. compute_and_set_names doesn't try to demangle
10230 Fortran names because there is no mangling standard. So new_symbol
10231 will set the demangled name to the result of dwarf2_full_name, and it is
10232 the demangled name that GDB uses if it exists. */
10233 if (cu->language == language_ada
10234 || (cu->language == language_fortran && physname))
10235 {
10236 /* For Ada unit, we prefer the linkage name over the name, as
10237 the former contains the exported name, which the user expects
10238 to be able to reference. Ideally, we want the user to be able
10239 to reference this entity using either natural or linkage name,
10240 but we haven't started looking at this enhancement yet. */
10241 const char *linkage_name = dw2_linkage_name (die, cu);
10242
10243 if (linkage_name != NULL)
10244 return linkage_name;
10245 }
10246
10247 /* These are the only languages we know how to qualify names in. */
10248 if (name != NULL
10249 && (cu->language == language_cplus
10250 || cu->language == language_fortran || cu->language == language_d
10251 || cu->language == language_rust))
10252 {
10253 if (die_needs_namespace (die, cu))
10254 {
10255 const char *prefix;
10256 const char *canonical_name = NULL;
10257
10258 string_file buf;
10259
10260 prefix = determine_prefix (die, cu);
10261 if (*prefix != '\0')
10262 {
10263 gdb::unique_xmalloc_ptr<char> prefixed_name
10264 (typename_concat (NULL, prefix, name, physname, cu));
10265
10266 buf.puts (prefixed_name.get ());
10267 }
10268 else
10269 buf.puts (name);
10270
10271 /* Template parameters may be specified in the DIE's DW_AT_name, or
10272 as children with DW_TAG_template_type_param or
10273 DW_TAG_value_type_param. If the latter, add them to the name
10274 here. If the name already has template parameters, then
10275 skip this step; some versions of GCC emit both, and
10276 it is more efficient to use the pre-computed name.
10277
10278 Something to keep in mind about this process: it is very
10279 unlikely, or in some cases downright impossible, to produce
10280 something that will match the mangled name of a function.
10281 If the definition of the function has the same debug info,
10282 we should be able to match up with it anyway. But fallbacks
10283 using the minimal symbol, for instance to find a method
10284 implemented in a stripped copy of libstdc++, will not work.
10285 If we do not have debug info for the definition, we will have to
10286 match them up some other way.
10287
10288 When we do name matching there is a related problem with function
10289 templates; two instantiated function templates are allowed to
10290 differ only by their return types, which we do not add here. */
10291
10292 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10293 {
10294 struct attribute *attr;
10295 struct die_info *child;
10296 int first = 1;
10297
10298 die->building_fullname = 1;
10299
10300 for (child = die->child; child != NULL; child = child->sibling)
10301 {
10302 struct type *type;
10303 LONGEST value;
10304 const gdb_byte *bytes;
10305 struct dwarf2_locexpr_baton *baton;
10306 struct value *v;
10307
10308 if (child->tag != DW_TAG_template_type_param
10309 && child->tag != DW_TAG_template_value_param)
10310 continue;
10311
10312 if (first)
10313 {
10314 buf.puts ("<");
10315 first = 0;
10316 }
10317 else
10318 buf.puts (", ");
10319
10320 attr = dwarf2_attr (child, DW_AT_type, cu);
10321 if (attr == NULL)
10322 {
10323 complaint (_("template parameter missing DW_AT_type"));
10324 buf.puts ("UNKNOWN_TYPE");
10325 continue;
10326 }
10327 type = die_type (child, cu);
10328
10329 if (child->tag == DW_TAG_template_type_param)
10330 {
10331 c_print_type (type, "", &buf, -1, 0, cu->language,
10332 &type_print_raw_options);
10333 continue;
10334 }
10335
10336 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10337 if (attr == NULL)
10338 {
10339 complaint (_("template parameter missing "
10340 "DW_AT_const_value"));
10341 buf.puts ("UNKNOWN_VALUE");
10342 continue;
10343 }
10344
10345 dwarf2_const_value_attr (attr, type, name,
10346 &cu->comp_unit_obstack, cu,
10347 &value, &bytes, &baton);
10348
10349 if (TYPE_NOSIGN (type))
10350 /* GDB prints characters as NUMBER 'CHAR'. If that's
10351 changed, this can use value_print instead. */
10352 c_printchar (value, type, &buf);
10353 else
10354 {
10355 struct value_print_options opts;
10356
10357 if (baton != NULL)
10358 v = dwarf2_evaluate_loc_desc (type, NULL,
10359 baton->data,
10360 baton->size,
10361 baton->per_cu,
10362 baton->per_objfile);
10363 else if (bytes != NULL)
10364 {
10365 v = allocate_value (type);
10366 memcpy (value_contents_writeable (v), bytes,
10367 TYPE_LENGTH (type));
10368 }
10369 else
10370 v = value_from_longest (type, value);
10371
10372 /* Specify decimal so that we do not depend on
10373 the radix. */
10374 get_formatted_print_options (&opts, 'd');
10375 opts.raw = 1;
10376 value_print (v, &buf, &opts);
10377 release_value (v);
10378 }
10379 }
10380
10381 die->building_fullname = 0;
10382
10383 if (!first)
10384 {
10385 /* Close the argument list, with a space if necessary
10386 (nested templates). */
10387 if (!buf.empty () && buf.string ().back () == '>')
10388 buf.puts (" >");
10389 else
10390 buf.puts (">");
10391 }
10392 }
10393
10394 /* For C++ methods, append formal parameter type
10395 information, if PHYSNAME. */
10396
10397 if (physname && die->tag == DW_TAG_subprogram
10398 && cu->language == language_cplus)
10399 {
10400 struct type *type = read_type_die (die, cu);
10401
10402 c_type_print_args (type, &buf, 1, cu->language,
10403 &type_print_raw_options);
10404
10405 if (cu->language == language_cplus)
10406 {
10407 /* Assume that an artificial first parameter is
10408 "this", but do not crash if it is not. RealView
10409 marks unnamed (and thus unused) parameters as
10410 artificial; there is no way to differentiate
10411 the two cases. */
10412 if (type->num_fields () > 0
10413 && TYPE_FIELD_ARTIFICIAL (type, 0)
10414 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10415 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10416 0))))
10417 buf.puts (" const");
10418 }
10419 }
10420
10421 const std::string &intermediate_name = buf.string ();
10422
10423 if (cu->language == language_cplus)
10424 canonical_name
10425 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10426 objfile);
10427
10428 /* If we only computed INTERMEDIATE_NAME, or if
10429 INTERMEDIATE_NAME is already canonical, then we need to
10430 intern it. */
10431 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10432 name = objfile->intern (intermediate_name);
10433 else
10434 name = canonical_name;
10435 }
10436 }
10437
10438 return name;
10439 }
10440
10441 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10442 If scope qualifiers are appropriate they will be added. The result
10443 will be allocated on the storage_obstack, or NULL if the DIE does
10444 not have a name. NAME may either be from a previous call to
10445 dwarf2_name or NULL.
10446
10447 The output string will be canonicalized (if C++). */
10448
10449 static const char *
10450 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10451 {
10452 return dwarf2_compute_name (name, die, cu, 0);
10453 }
10454
10455 /* Construct a physname for the given DIE in CU. NAME may either be
10456 from a previous call to dwarf2_name or NULL. The result will be
10457 allocated on the objfile_objstack or NULL if the DIE does not have a
10458 name.
10459
10460 The output string will be canonicalized (if C++). */
10461
10462 static const char *
10463 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10464 {
10465 struct objfile *objfile = cu->per_objfile->objfile;
10466 const char *retval, *mangled = NULL, *canon = NULL;
10467 int need_copy = 1;
10468
10469 /* In this case dwarf2_compute_name is just a shortcut not building anything
10470 on its own. */
10471 if (!die_needs_namespace (die, cu))
10472 return dwarf2_compute_name (name, die, cu, 1);
10473
10474 if (cu->language != language_rust)
10475 mangled = dw2_linkage_name (die, cu);
10476
10477 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10478 has computed. */
10479 gdb::unique_xmalloc_ptr<char> demangled;
10480 if (mangled != NULL)
10481 {
10482
10483 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10484 {
10485 /* Do nothing (do not demangle the symbol name). */
10486 }
10487 else if (cu->language == language_go)
10488 {
10489 /* This is a lie, but we already lie to the caller new_symbol.
10490 new_symbol assumes we return the mangled name.
10491 This just undoes that lie until things are cleaned up. */
10492 }
10493 else
10494 {
10495 /* Use DMGL_RET_DROP for C++ template functions to suppress
10496 their return type. It is easier for GDB users to search
10497 for such functions as `name(params)' than `long name(params)'.
10498 In such case the minimal symbol names do not match the full
10499 symbol names but for template functions there is never a need
10500 to look up their definition from their declaration so
10501 the only disadvantage remains the minimal symbol variant
10502 `long name(params)' does not have the proper inferior type. */
10503 demangled.reset (gdb_demangle (mangled,
10504 (DMGL_PARAMS | DMGL_ANSI
10505 | DMGL_RET_DROP)));
10506 }
10507 if (demangled)
10508 canon = demangled.get ();
10509 else
10510 {
10511 canon = mangled;
10512 need_copy = 0;
10513 }
10514 }
10515
10516 if (canon == NULL || check_physname)
10517 {
10518 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10519
10520 if (canon != NULL && strcmp (physname, canon) != 0)
10521 {
10522 /* It may not mean a bug in GDB. The compiler could also
10523 compute DW_AT_linkage_name incorrectly. But in such case
10524 GDB would need to be bug-to-bug compatible. */
10525
10526 complaint (_("Computed physname <%s> does not match demangled <%s> "
10527 "(from linkage <%s>) - DIE at %s [in module %s]"),
10528 physname, canon, mangled, sect_offset_str (die->sect_off),
10529 objfile_name (objfile));
10530
10531 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10532 is available here - over computed PHYSNAME. It is safer
10533 against both buggy GDB and buggy compilers. */
10534
10535 retval = canon;
10536 }
10537 else
10538 {
10539 retval = physname;
10540 need_copy = 0;
10541 }
10542 }
10543 else
10544 retval = canon;
10545
10546 if (need_copy)
10547 retval = objfile->intern (retval);
10548
10549 return retval;
10550 }
10551
10552 /* Inspect DIE in CU for a namespace alias. If one exists, record
10553 a new symbol for it.
10554
10555 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10556
10557 static int
10558 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10559 {
10560 struct attribute *attr;
10561
10562 /* If the die does not have a name, this is not a namespace
10563 alias. */
10564 attr = dwarf2_attr (die, DW_AT_name, cu);
10565 if (attr != NULL)
10566 {
10567 int num;
10568 struct die_info *d = die;
10569 struct dwarf2_cu *imported_cu = cu;
10570
10571 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10572 keep inspecting DIEs until we hit the underlying import. */
10573 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10574 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10575 {
10576 attr = dwarf2_attr (d, DW_AT_import, cu);
10577 if (attr == NULL)
10578 break;
10579
10580 d = follow_die_ref (d, attr, &imported_cu);
10581 if (d->tag != DW_TAG_imported_declaration)
10582 break;
10583 }
10584
10585 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10586 {
10587 complaint (_("DIE at %s has too many recursively imported "
10588 "declarations"), sect_offset_str (d->sect_off));
10589 return 0;
10590 }
10591
10592 if (attr != NULL)
10593 {
10594 struct type *type;
10595 sect_offset sect_off = attr->get_ref_die_offset ();
10596
10597 type = get_die_type_at_offset (sect_off, cu->per_cu);
10598 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10599 {
10600 /* This declaration is a global namespace alias. Add
10601 a symbol for it whose type is the aliased namespace. */
10602 new_symbol (die, type, cu);
10603 return 1;
10604 }
10605 }
10606 }
10607
10608 return 0;
10609 }
10610
10611 /* Return the using directives repository (global or local?) to use in the
10612 current context for CU.
10613
10614 For Ada, imported declarations can materialize renamings, which *may* be
10615 global. However it is impossible (for now?) in DWARF to distinguish
10616 "external" imported declarations and "static" ones. As all imported
10617 declarations seem to be static in all other languages, make them all CU-wide
10618 global only in Ada. */
10619
10620 static struct using_direct **
10621 using_directives (struct dwarf2_cu *cu)
10622 {
10623 if (cu->language == language_ada
10624 && cu->get_builder ()->outermost_context_p ())
10625 return cu->get_builder ()->get_global_using_directives ();
10626 else
10627 return cu->get_builder ()->get_local_using_directives ();
10628 }
10629
10630 /* Read the import statement specified by the given die and record it. */
10631
10632 static void
10633 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10634 {
10635 struct objfile *objfile = cu->per_objfile->objfile;
10636 struct attribute *import_attr;
10637 struct die_info *imported_die, *child_die;
10638 struct dwarf2_cu *imported_cu;
10639 const char *imported_name;
10640 const char *imported_name_prefix;
10641 const char *canonical_name;
10642 const char *import_alias;
10643 const char *imported_declaration = NULL;
10644 const char *import_prefix;
10645 std::vector<const char *> excludes;
10646
10647 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10648 if (import_attr == NULL)
10649 {
10650 complaint (_("Tag '%s' has no DW_AT_import"),
10651 dwarf_tag_name (die->tag));
10652 return;
10653 }
10654
10655 imported_cu = cu;
10656 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10657 imported_name = dwarf2_name (imported_die, imported_cu);
10658 if (imported_name == NULL)
10659 {
10660 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10661
10662 The import in the following code:
10663 namespace A
10664 {
10665 typedef int B;
10666 }
10667
10668 int main ()
10669 {
10670 using A::B;
10671 B b;
10672 return b;
10673 }
10674
10675 ...
10676 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10677 <52> DW_AT_decl_file : 1
10678 <53> DW_AT_decl_line : 6
10679 <54> DW_AT_import : <0x75>
10680 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10681 <59> DW_AT_name : B
10682 <5b> DW_AT_decl_file : 1
10683 <5c> DW_AT_decl_line : 2
10684 <5d> DW_AT_type : <0x6e>
10685 ...
10686 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10687 <76> DW_AT_byte_size : 4
10688 <77> DW_AT_encoding : 5 (signed)
10689
10690 imports the wrong die ( 0x75 instead of 0x58 ).
10691 This case will be ignored until the gcc bug is fixed. */
10692 return;
10693 }
10694
10695 /* Figure out the local name after import. */
10696 import_alias = dwarf2_name (die, cu);
10697
10698 /* Figure out where the statement is being imported to. */
10699 import_prefix = determine_prefix (die, cu);
10700
10701 /* Figure out what the scope of the imported die is and prepend it
10702 to the name of the imported die. */
10703 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10704
10705 if (imported_die->tag != DW_TAG_namespace
10706 && imported_die->tag != DW_TAG_module)
10707 {
10708 imported_declaration = imported_name;
10709 canonical_name = imported_name_prefix;
10710 }
10711 else if (strlen (imported_name_prefix) > 0)
10712 canonical_name = obconcat (&objfile->objfile_obstack,
10713 imported_name_prefix,
10714 (cu->language == language_d ? "." : "::"),
10715 imported_name, (char *) NULL);
10716 else
10717 canonical_name = imported_name;
10718
10719 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10720 for (child_die = die->child; child_die && child_die->tag;
10721 child_die = child_die->sibling)
10722 {
10723 /* DWARF-4: A Fortran use statement with a “rename list” may be
10724 represented by an imported module entry with an import attribute
10725 referring to the module and owned entries corresponding to those
10726 entities that are renamed as part of being imported. */
10727
10728 if (child_die->tag != DW_TAG_imported_declaration)
10729 {
10730 complaint (_("child DW_TAG_imported_declaration expected "
10731 "- DIE at %s [in module %s]"),
10732 sect_offset_str (child_die->sect_off),
10733 objfile_name (objfile));
10734 continue;
10735 }
10736
10737 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10738 if (import_attr == NULL)
10739 {
10740 complaint (_("Tag '%s' has no DW_AT_import"),
10741 dwarf_tag_name (child_die->tag));
10742 continue;
10743 }
10744
10745 imported_cu = cu;
10746 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10747 &imported_cu);
10748 imported_name = dwarf2_name (imported_die, imported_cu);
10749 if (imported_name == NULL)
10750 {
10751 complaint (_("child DW_TAG_imported_declaration has unknown "
10752 "imported name - DIE at %s [in module %s]"),
10753 sect_offset_str (child_die->sect_off),
10754 objfile_name (objfile));
10755 continue;
10756 }
10757
10758 excludes.push_back (imported_name);
10759
10760 process_die (child_die, cu);
10761 }
10762
10763 add_using_directive (using_directives (cu),
10764 import_prefix,
10765 canonical_name,
10766 import_alias,
10767 imported_declaration,
10768 excludes,
10769 0,
10770 &objfile->objfile_obstack);
10771 }
10772
10773 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10774 types, but gives them a size of zero. Starting with version 14,
10775 ICC is compatible with GCC. */
10776
10777 static bool
10778 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10779 {
10780 if (!cu->checked_producer)
10781 check_producer (cu);
10782
10783 return cu->producer_is_icc_lt_14;
10784 }
10785
10786 /* ICC generates a DW_AT_type for C void functions. This was observed on
10787 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10788 which says that void functions should not have a DW_AT_type. */
10789
10790 static bool
10791 producer_is_icc (struct dwarf2_cu *cu)
10792 {
10793 if (!cu->checked_producer)
10794 check_producer (cu);
10795
10796 return cu->producer_is_icc;
10797 }
10798
10799 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10800 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10801 this, it was first present in GCC release 4.3.0. */
10802
10803 static bool
10804 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10805 {
10806 if (!cu->checked_producer)
10807 check_producer (cu);
10808
10809 return cu->producer_is_gcc_lt_4_3;
10810 }
10811
10812 static file_and_directory
10813 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10814 {
10815 file_and_directory res;
10816
10817 /* Find the filename. Do not use dwarf2_name here, since the filename
10818 is not a source language identifier. */
10819 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10820 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10821
10822 if (res.comp_dir == NULL
10823 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10824 && IS_ABSOLUTE_PATH (res.name))
10825 {
10826 res.comp_dir_storage = ldirname (res.name);
10827 if (!res.comp_dir_storage.empty ())
10828 res.comp_dir = res.comp_dir_storage.c_str ();
10829 }
10830 if (res.comp_dir != NULL)
10831 {
10832 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10833 directory, get rid of it. */
10834 const char *cp = strchr (res.comp_dir, ':');
10835
10836 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10837 res.comp_dir = cp + 1;
10838 }
10839
10840 if (res.name == NULL)
10841 res.name = "<unknown>";
10842
10843 return res;
10844 }
10845
10846 /* Handle DW_AT_stmt_list for a compilation unit.
10847 DIE is the DW_TAG_compile_unit die for CU.
10848 COMP_DIR is the compilation directory. LOWPC is passed to
10849 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10850
10851 static void
10852 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10853 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10854 {
10855 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10856 struct attribute *attr;
10857 struct line_header line_header_local;
10858 hashval_t line_header_local_hash;
10859 void **slot;
10860 int decode_mapping;
10861
10862 gdb_assert (! cu->per_cu->is_debug_types);
10863
10864 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10865 if (attr == NULL)
10866 return;
10867
10868 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10869
10870 /* The line header hash table is only created if needed (it exists to
10871 prevent redundant reading of the line table for partial_units).
10872 If we're given a partial_unit, we'll need it. If we're given a
10873 compile_unit, then use the line header hash table if it's already
10874 created, but don't create one just yet. */
10875
10876 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10877 && die->tag == DW_TAG_partial_unit)
10878 {
10879 dwarf2_per_objfile->per_bfd->line_header_hash
10880 .reset (htab_create_alloc (127, line_header_hash_voidp,
10881 line_header_eq_voidp,
10882 free_line_header_voidp,
10883 xcalloc, xfree));
10884 }
10885
10886 line_header_local.sect_off = line_offset;
10887 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10888 line_header_local_hash = line_header_hash (&line_header_local);
10889 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10890 {
10891 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10892 &line_header_local,
10893 line_header_local_hash, NO_INSERT);
10894
10895 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10896 is not present in *SLOT (since if there is something in *SLOT then
10897 it will be for a partial_unit). */
10898 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10899 {
10900 gdb_assert (*slot != NULL);
10901 cu->line_header = (struct line_header *) *slot;
10902 return;
10903 }
10904 }
10905
10906 /* dwarf_decode_line_header does not yet provide sufficient information.
10907 We always have to call also dwarf_decode_lines for it. */
10908 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10909 if (lh == NULL)
10910 return;
10911
10912 cu->line_header = lh.release ();
10913 cu->line_header_die_owner = die;
10914
10915 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10916 slot = NULL;
10917 else
10918 {
10919 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10920 &line_header_local,
10921 line_header_local_hash, INSERT);
10922 gdb_assert (slot != NULL);
10923 }
10924 if (slot != NULL && *slot == NULL)
10925 {
10926 /* This newly decoded line number information unit will be owned
10927 by line_header_hash hash table. */
10928 *slot = cu->line_header;
10929 cu->line_header_die_owner = NULL;
10930 }
10931 else
10932 {
10933 /* We cannot free any current entry in (*slot) as that struct line_header
10934 may be already used by multiple CUs. Create only temporary decoded
10935 line_header for this CU - it may happen at most once for each line
10936 number information unit. And if we're not using line_header_hash
10937 then this is what we want as well. */
10938 gdb_assert (die->tag != DW_TAG_partial_unit);
10939 }
10940 decode_mapping = (die->tag != DW_TAG_partial_unit);
10941 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10942 decode_mapping);
10943
10944 }
10945
10946 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10947
10948 static void
10949 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10950 {
10951 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10952 struct objfile *objfile = dwarf2_per_objfile->objfile;
10953 struct gdbarch *gdbarch = objfile->arch ();
10954 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10955 CORE_ADDR highpc = ((CORE_ADDR) 0);
10956 struct attribute *attr;
10957 struct die_info *child_die;
10958 CORE_ADDR baseaddr;
10959
10960 prepare_one_comp_unit (cu, die, cu->language);
10961 baseaddr = objfile->text_section_offset ();
10962
10963 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10964
10965 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10966 from finish_block. */
10967 if (lowpc == ((CORE_ADDR) -1))
10968 lowpc = highpc;
10969 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10970
10971 file_and_directory fnd = find_file_and_directory (die, cu);
10972
10973 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10974 standardised yet. As a workaround for the language detection we fall
10975 back to the DW_AT_producer string. */
10976 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10977 cu->language = language_opencl;
10978
10979 /* Similar hack for Go. */
10980 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10981 set_cu_language (DW_LANG_Go, cu);
10982
10983 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10984
10985 /* Decode line number information if present. We do this before
10986 processing child DIEs, so that the line header table is available
10987 for DW_AT_decl_file. */
10988 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10989
10990 /* Process all dies in compilation unit. */
10991 if (die->child != NULL)
10992 {
10993 child_die = die->child;
10994 while (child_die && child_die->tag)
10995 {
10996 process_die (child_die, cu);
10997 child_die = child_die->sibling;
10998 }
10999 }
11000
11001 /* Decode macro information, if present. Dwarf 2 macro information
11002 refers to information in the line number info statement program
11003 header, so we can only read it if we've read the header
11004 successfully. */
11005 attr = dwarf2_attr (die, DW_AT_macros, cu);
11006 if (attr == NULL)
11007 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11008 if (attr && cu->line_header)
11009 {
11010 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11011 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11012
11013 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11014 }
11015 else
11016 {
11017 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11018 if (attr && cu->line_header)
11019 {
11020 unsigned int macro_offset = DW_UNSND (attr);
11021
11022 dwarf_decode_macros (cu, macro_offset, 0);
11023 }
11024 }
11025 }
11026
11027 void
11028 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11029 {
11030 struct type_unit_group *tu_group;
11031 int first_time;
11032 struct attribute *attr;
11033 unsigned int i;
11034 struct signatured_type *sig_type;
11035
11036 gdb_assert (per_cu->is_debug_types);
11037 sig_type = (struct signatured_type *) per_cu;
11038
11039 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11040
11041 /* If we're using .gdb_index (includes -readnow) then
11042 per_cu->type_unit_group may not have been set up yet. */
11043 if (sig_type->type_unit_group == NULL)
11044 sig_type->type_unit_group = get_type_unit_group (this, attr);
11045 tu_group = sig_type->type_unit_group;
11046
11047 /* If we've already processed this stmt_list there's no real need to
11048 do it again, we could fake it and just recreate the part we need
11049 (file name,index -> symtab mapping). If data shows this optimization
11050 is useful we can do it then. */
11051 first_time = tu_group->compunit_symtab == NULL;
11052
11053 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11054 debug info. */
11055 line_header_up lh;
11056 if (attr != NULL)
11057 {
11058 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11059 lh = dwarf_decode_line_header (line_offset, this);
11060 }
11061 if (lh == NULL)
11062 {
11063 if (first_time)
11064 start_symtab ("", NULL, 0);
11065 else
11066 {
11067 gdb_assert (tu_group->symtabs == NULL);
11068 gdb_assert (m_builder == nullptr);
11069 struct compunit_symtab *cust = tu_group->compunit_symtab;
11070 m_builder.reset (new struct buildsym_compunit
11071 (COMPUNIT_OBJFILE (cust), "",
11072 COMPUNIT_DIRNAME (cust),
11073 compunit_language (cust),
11074 0, cust));
11075 list_in_scope = get_builder ()->get_file_symbols ();
11076 }
11077 return;
11078 }
11079
11080 line_header = lh.release ();
11081 line_header_die_owner = die;
11082
11083 if (first_time)
11084 {
11085 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11086
11087 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11088 still initializing it, and our caller (a few levels up)
11089 process_full_type_unit still needs to know if this is the first
11090 time. */
11091
11092 tu_group->symtabs
11093 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11094 struct symtab *, line_header->file_names_size ());
11095
11096 auto &file_names = line_header->file_names ();
11097 for (i = 0; i < file_names.size (); ++i)
11098 {
11099 file_entry &fe = file_names[i];
11100 dwarf2_start_subfile (this, fe.name,
11101 fe.include_dir (line_header));
11102 buildsym_compunit *b = get_builder ();
11103 if (b->get_current_subfile ()->symtab == NULL)
11104 {
11105 /* NOTE: start_subfile will recognize when it's been
11106 passed a file it has already seen. So we can't
11107 assume there's a simple mapping from
11108 cu->line_header->file_names to subfiles, plus
11109 cu->line_header->file_names may contain dups. */
11110 b->get_current_subfile ()->symtab
11111 = allocate_symtab (cust, b->get_current_subfile ()->name);
11112 }
11113
11114 fe.symtab = b->get_current_subfile ()->symtab;
11115 tu_group->symtabs[i] = fe.symtab;
11116 }
11117 }
11118 else
11119 {
11120 gdb_assert (m_builder == nullptr);
11121 struct compunit_symtab *cust = tu_group->compunit_symtab;
11122 m_builder.reset (new struct buildsym_compunit
11123 (COMPUNIT_OBJFILE (cust), "",
11124 COMPUNIT_DIRNAME (cust),
11125 compunit_language (cust),
11126 0, cust));
11127 list_in_scope = get_builder ()->get_file_symbols ();
11128
11129 auto &file_names = line_header->file_names ();
11130 for (i = 0; i < file_names.size (); ++i)
11131 {
11132 file_entry &fe = file_names[i];
11133 fe.symtab = tu_group->symtabs[i];
11134 }
11135 }
11136
11137 /* The main symtab is allocated last. Type units don't have DW_AT_name
11138 so they don't have a "real" (so to speak) symtab anyway.
11139 There is later code that will assign the main symtab to all symbols
11140 that don't have one. We need to handle the case of a symbol with a
11141 missing symtab (DW_AT_decl_file) anyway. */
11142 }
11143
11144 /* Process DW_TAG_type_unit.
11145 For TUs we want to skip the first top level sibling if it's not the
11146 actual type being defined by this TU. In this case the first top
11147 level sibling is there to provide context only. */
11148
11149 static void
11150 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11151 {
11152 struct die_info *child_die;
11153
11154 prepare_one_comp_unit (cu, die, language_minimal);
11155
11156 /* Initialize (or reinitialize) the machinery for building symtabs.
11157 We do this before processing child DIEs, so that the line header table
11158 is available for DW_AT_decl_file. */
11159 cu->setup_type_unit_groups (die);
11160
11161 if (die->child != NULL)
11162 {
11163 child_die = die->child;
11164 while (child_die && child_die->tag)
11165 {
11166 process_die (child_die, cu);
11167 child_die = child_die->sibling;
11168 }
11169 }
11170 }
11171 \f
11172 /* DWO/DWP files.
11173
11174 http://gcc.gnu.org/wiki/DebugFission
11175 http://gcc.gnu.org/wiki/DebugFissionDWP
11176
11177 To simplify handling of both DWO files ("object" files with the DWARF info)
11178 and DWP files (a file with the DWOs packaged up into one file), we treat
11179 DWP files as having a collection of virtual DWO files. */
11180
11181 static hashval_t
11182 hash_dwo_file (const void *item)
11183 {
11184 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11185 hashval_t hash;
11186
11187 hash = htab_hash_string (dwo_file->dwo_name);
11188 if (dwo_file->comp_dir != NULL)
11189 hash += htab_hash_string (dwo_file->comp_dir);
11190 return hash;
11191 }
11192
11193 static int
11194 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11195 {
11196 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11197 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11198
11199 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11200 return 0;
11201 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11202 return lhs->comp_dir == rhs->comp_dir;
11203 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11204 }
11205
11206 /* Allocate a hash table for DWO files. */
11207
11208 static htab_up
11209 allocate_dwo_file_hash_table ()
11210 {
11211 auto delete_dwo_file = [] (void *item)
11212 {
11213 struct dwo_file *dwo_file = (struct dwo_file *) item;
11214
11215 delete dwo_file;
11216 };
11217
11218 return htab_up (htab_create_alloc (41,
11219 hash_dwo_file,
11220 eq_dwo_file,
11221 delete_dwo_file,
11222 xcalloc, xfree));
11223 }
11224
11225 /* Lookup DWO file DWO_NAME. */
11226
11227 static void **
11228 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11229 const char *dwo_name,
11230 const char *comp_dir)
11231 {
11232 struct dwo_file find_entry;
11233 void **slot;
11234
11235 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11236 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11237
11238 find_entry.dwo_name = dwo_name;
11239 find_entry.comp_dir = comp_dir;
11240 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11241 INSERT);
11242
11243 return slot;
11244 }
11245
11246 static hashval_t
11247 hash_dwo_unit (const void *item)
11248 {
11249 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11250
11251 /* This drops the top 32 bits of the id, but is ok for a hash. */
11252 return dwo_unit->signature;
11253 }
11254
11255 static int
11256 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11257 {
11258 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11259 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11260
11261 /* The signature is assumed to be unique within the DWO file.
11262 So while object file CU dwo_id's always have the value zero,
11263 that's OK, assuming each object file DWO file has only one CU,
11264 and that's the rule for now. */
11265 return lhs->signature == rhs->signature;
11266 }
11267
11268 /* Allocate a hash table for DWO CUs,TUs.
11269 There is one of these tables for each of CUs,TUs for each DWO file. */
11270
11271 static htab_up
11272 allocate_dwo_unit_table ()
11273 {
11274 /* Start out with a pretty small number.
11275 Generally DWO files contain only one CU and maybe some TUs. */
11276 return htab_up (htab_create_alloc (3,
11277 hash_dwo_unit,
11278 eq_dwo_unit,
11279 NULL, xcalloc, xfree));
11280 }
11281
11282 /* die_reader_func for create_dwo_cu. */
11283
11284 static void
11285 create_dwo_cu_reader (const struct die_reader_specs *reader,
11286 const gdb_byte *info_ptr,
11287 struct die_info *comp_unit_die,
11288 struct dwo_file *dwo_file,
11289 struct dwo_unit *dwo_unit)
11290 {
11291 struct dwarf2_cu *cu = reader->cu;
11292 sect_offset sect_off = cu->per_cu->sect_off;
11293 struct dwarf2_section_info *section = cu->per_cu->section;
11294
11295 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11296 if (!signature.has_value ())
11297 {
11298 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11299 " its dwo_id [in module %s]"),
11300 sect_offset_str (sect_off), dwo_file->dwo_name);
11301 return;
11302 }
11303
11304 dwo_unit->dwo_file = dwo_file;
11305 dwo_unit->signature = *signature;
11306 dwo_unit->section = section;
11307 dwo_unit->sect_off = sect_off;
11308 dwo_unit->length = cu->per_cu->length;
11309
11310 if (dwarf_read_debug)
11311 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11312 sect_offset_str (sect_off),
11313 hex_string (dwo_unit->signature));
11314 }
11315
11316 /* Create the dwo_units for the CUs in a DWO_FILE.
11317 Note: This function processes DWO files only, not DWP files. */
11318
11319 static void
11320 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11321 dwarf2_cu *cu, struct dwo_file &dwo_file,
11322 dwarf2_section_info &section, htab_up &cus_htab)
11323 {
11324 struct objfile *objfile = dwarf2_per_objfile->objfile;
11325 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11326 const gdb_byte *info_ptr, *end_ptr;
11327
11328 section.read (objfile);
11329 info_ptr = section.buffer;
11330
11331 if (info_ptr == NULL)
11332 return;
11333
11334 if (dwarf_read_debug)
11335 {
11336 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11337 section.get_name (),
11338 section.get_file_name ());
11339 }
11340
11341 end_ptr = info_ptr + section.size;
11342 while (info_ptr < end_ptr)
11343 {
11344 struct dwarf2_per_cu_data per_cu;
11345 struct dwo_unit read_unit {};
11346 struct dwo_unit *dwo_unit;
11347 void **slot;
11348 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11349
11350 memset (&per_cu, 0, sizeof (per_cu));
11351 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11352 per_cu.per_bfd = per_bfd;
11353 per_cu.is_debug_types = 0;
11354 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11355 per_cu.section = &section;
11356
11357 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11358 if (!reader.dummy_p)
11359 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11360 &dwo_file, &read_unit);
11361 info_ptr += per_cu.length;
11362
11363 // If the unit could not be parsed, skip it.
11364 if (read_unit.dwo_file == NULL)
11365 continue;
11366
11367 if (cus_htab == NULL)
11368 cus_htab = allocate_dwo_unit_table ();
11369
11370 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11371 struct dwo_unit);
11372 *dwo_unit = read_unit;
11373 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11374 gdb_assert (slot != NULL);
11375 if (*slot != NULL)
11376 {
11377 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11378 sect_offset dup_sect_off = dup_cu->sect_off;
11379
11380 complaint (_("debug cu entry at offset %s is duplicate to"
11381 " the entry at offset %s, signature %s"),
11382 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11383 hex_string (dwo_unit->signature));
11384 }
11385 *slot = (void *)dwo_unit;
11386 }
11387 }
11388
11389 /* DWP file .debug_{cu,tu}_index section format:
11390 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11391
11392 DWP Version 1:
11393
11394 Both index sections have the same format, and serve to map a 64-bit
11395 signature to a set of section numbers. Each section begins with a header,
11396 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11397 indexes, and a pool of 32-bit section numbers. The index sections will be
11398 aligned at 8-byte boundaries in the file.
11399
11400 The index section header consists of:
11401
11402 V, 32 bit version number
11403 -, 32 bits unused
11404 N, 32 bit number of compilation units or type units in the index
11405 M, 32 bit number of slots in the hash table
11406
11407 Numbers are recorded using the byte order of the application binary.
11408
11409 The hash table begins at offset 16 in the section, and consists of an array
11410 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11411 order of the application binary). Unused slots in the hash table are 0.
11412 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11413
11414 The parallel table begins immediately after the hash table
11415 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11416 array of 32-bit indexes (using the byte order of the application binary),
11417 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11418 table contains a 32-bit index into the pool of section numbers. For unused
11419 hash table slots, the corresponding entry in the parallel table will be 0.
11420
11421 The pool of section numbers begins immediately following the hash table
11422 (at offset 16 + 12 * M from the beginning of the section). The pool of
11423 section numbers consists of an array of 32-bit words (using the byte order
11424 of the application binary). Each item in the array is indexed starting
11425 from 0. The hash table entry provides the index of the first section
11426 number in the set. Additional section numbers in the set follow, and the
11427 set is terminated by a 0 entry (section number 0 is not used in ELF).
11428
11429 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11430 section must be the first entry in the set, and the .debug_abbrev.dwo must
11431 be the second entry. Other members of the set may follow in any order.
11432
11433 ---
11434
11435 DWP Version 2:
11436
11437 DWP Version 2 combines all the .debug_info, etc. sections into one,
11438 and the entries in the index tables are now offsets into these sections.
11439 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11440 section.
11441
11442 Index Section Contents:
11443 Header
11444 Hash Table of Signatures dwp_hash_table.hash_table
11445 Parallel Table of Indices dwp_hash_table.unit_table
11446 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11447 Table of Section Sizes dwp_hash_table.v2.sizes
11448
11449 The index section header consists of:
11450
11451 V, 32 bit version number
11452 L, 32 bit number of columns in the table of section offsets
11453 N, 32 bit number of compilation units or type units in the index
11454 M, 32 bit number of slots in the hash table
11455
11456 Numbers are recorded using the byte order of the application binary.
11457
11458 The hash table has the same format as version 1.
11459 The parallel table of indices has the same format as version 1,
11460 except that the entries are origin-1 indices into the table of sections
11461 offsets and the table of section sizes.
11462
11463 The table of offsets begins immediately following the parallel table
11464 (at offset 16 + 12 * M from the beginning of the section). The table is
11465 a two-dimensional array of 32-bit words (using the byte order of the
11466 application binary), with L columns and N+1 rows, in row-major order.
11467 Each row in the array is indexed starting from 0. The first row provides
11468 a key to the remaining rows: each column in this row provides an identifier
11469 for a debug section, and the offsets in the same column of subsequent rows
11470 refer to that section. The section identifiers are:
11471
11472 DW_SECT_INFO 1 .debug_info.dwo
11473 DW_SECT_TYPES 2 .debug_types.dwo
11474 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11475 DW_SECT_LINE 4 .debug_line.dwo
11476 DW_SECT_LOC 5 .debug_loc.dwo
11477 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11478 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11479 DW_SECT_MACRO 8 .debug_macro.dwo
11480
11481 The offsets provided by the CU and TU index sections are the base offsets
11482 for the contributions made by each CU or TU to the corresponding section
11483 in the package file. Each CU and TU header contains an abbrev_offset
11484 field, used to find the abbreviations table for that CU or TU within the
11485 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11486 be interpreted as relative to the base offset given in the index section.
11487 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11488 should be interpreted as relative to the base offset for .debug_line.dwo,
11489 and offsets into other debug sections obtained from DWARF attributes should
11490 also be interpreted as relative to the corresponding base offset.
11491
11492 The table of sizes begins immediately following the table of offsets.
11493 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11494 with L columns and N rows, in row-major order. Each row in the array is
11495 indexed starting from 1 (row 0 is shared by the two tables).
11496
11497 ---
11498
11499 Hash table lookup is handled the same in version 1 and 2:
11500
11501 We assume that N and M will not exceed 2^32 - 1.
11502 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11503
11504 Given a 64-bit compilation unit signature or a type signature S, an entry
11505 in the hash table is located as follows:
11506
11507 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11508 the low-order k bits all set to 1.
11509
11510 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11511
11512 3) If the hash table entry at index H matches the signature, use that
11513 entry. If the hash table entry at index H is unused (all zeroes),
11514 terminate the search: the signature is not present in the table.
11515
11516 4) Let H = (H + H') modulo M. Repeat at Step 3.
11517
11518 Because M > N and H' and M are relatively prime, the search is guaranteed
11519 to stop at an unused slot or find the match. */
11520
11521 /* Create a hash table to map DWO IDs to their CU/TU entry in
11522 .debug_{info,types}.dwo in DWP_FILE.
11523 Returns NULL if there isn't one.
11524 Note: This function processes DWP files only, not DWO files. */
11525
11526 static struct dwp_hash_table *
11527 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11528 struct dwp_file *dwp_file, int is_debug_types)
11529 {
11530 struct objfile *objfile = dwarf2_per_objfile->objfile;
11531 bfd *dbfd = dwp_file->dbfd.get ();
11532 const gdb_byte *index_ptr, *index_end;
11533 struct dwarf2_section_info *index;
11534 uint32_t version, nr_columns, nr_units, nr_slots;
11535 struct dwp_hash_table *htab;
11536
11537 if (is_debug_types)
11538 index = &dwp_file->sections.tu_index;
11539 else
11540 index = &dwp_file->sections.cu_index;
11541
11542 if (index->empty ())
11543 return NULL;
11544 index->read (objfile);
11545
11546 index_ptr = index->buffer;
11547 index_end = index_ptr + index->size;
11548
11549 version = read_4_bytes (dbfd, index_ptr);
11550 index_ptr += 4;
11551 if (version == 2)
11552 nr_columns = read_4_bytes (dbfd, index_ptr);
11553 else
11554 nr_columns = 0;
11555 index_ptr += 4;
11556 nr_units = read_4_bytes (dbfd, index_ptr);
11557 index_ptr += 4;
11558 nr_slots = read_4_bytes (dbfd, index_ptr);
11559 index_ptr += 4;
11560
11561 if (version != 1 && version != 2)
11562 {
11563 error (_("Dwarf Error: unsupported DWP file version (%s)"
11564 " [in module %s]"),
11565 pulongest (version), dwp_file->name);
11566 }
11567 if (nr_slots != (nr_slots & -nr_slots))
11568 {
11569 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11570 " is not power of 2 [in module %s]"),
11571 pulongest (nr_slots), dwp_file->name);
11572 }
11573
11574 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11575 htab->version = version;
11576 htab->nr_columns = nr_columns;
11577 htab->nr_units = nr_units;
11578 htab->nr_slots = nr_slots;
11579 htab->hash_table = index_ptr;
11580 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11581
11582 /* Exit early if the table is empty. */
11583 if (nr_slots == 0 || nr_units == 0
11584 || (version == 2 && nr_columns == 0))
11585 {
11586 /* All must be zero. */
11587 if (nr_slots != 0 || nr_units != 0
11588 || (version == 2 && nr_columns != 0))
11589 {
11590 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11591 " all zero [in modules %s]"),
11592 dwp_file->name);
11593 }
11594 return htab;
11595 }
11596
11597 if (version == 1)
11598 {
11599 htab->section_pool.v1.indices =
11600 htab->unit_table + sizeof (uint32_t) * nr_slots;
11601 /* It's harder to decide whether the section is too small in v1.
11602 V1 is deprecated anyway so we punt. */
11603 }
11604 else
11605 {
11606 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11607 int *ids = htab->section_pool.v2.section_ids;
11608 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11609 /* Reverse map for error checking. */
11610 int ids_seen[DW_SECT_MAX + 1];
11611 int i;
11612
11613 if (nr_columns < 2)
11614 {
11615 error (_("Dwarf Error: bad DWP hash table, too few columns"
11616 " in section table [in module %s]"),
11617 dwp_file->name);
11618 }
11619 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11620 {
11621 error (_("Dwarf Error: bad DWP hash table, too many columns"
11622 " in section table [in module %s]"),
11623 dwp_file->name);
11624 }
11625 memset (ids, 255, sizeof_ids);
11626 memset (ids_seen, 255, sizeof (ids_seen));
11627 for (i = 0; i < nr_columns; ++i)
11628 {
11629 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11630
11631 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11632 {
11633 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11634 " in section table [in module %s]"),
11635 id, dwp_file->name);
11636 }
11637 if (ids_seen[id] != -1)
11638 {
11639 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11640 " id %d in section table [in module %s]"),
11641 id, dwp_file->name);
11642 }
11643 ids_seen[id] = i;
11644 ids[i] = id;
11645 }
11646 /* Must have exactly one info or types section. */
11647 if (((ids_seen[DW_SECT_INFO] != -1)
11648 + (ids_seen[DW_SECT_TYPES] != -1))
11649 != 1)
11650 {
11651 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11652 " DWO info/types section [in module %s]"),
11653 dwp_file->name);
11654 }
11655 /* Must have an abbrev section. */
11656 if (ids_seen[DW_SECT_ABBREV] == -1)
11657 {
11658 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11659 " section [in module %s]"),
11660 dwp_file->name);
11661 }
11662 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11663 htab->section_pool.v2.sizes =
11664 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11665 * nr_units * nr_columns);
11666 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11667 * nr_units * nr_columns))
11668 > index_end)
11669 {
11670 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11671 " [in module %s]"),
11672 dwp_file->name);
11673 }
11674 }
11675
11676 return htab;
11677 }
11678
11679 /* Update SECTIONS with the data from SECTP.
11680
11681 This function is like the other "locate" section routines that are
11682 passed to bfd_map_over_sections, but in this context the sections to
11683 read comes from the DWP V1 hash table, not the full ELF section table.
11684
11685 The result is non-zero for success, or zero if an error was found. */
11686
11687 static int
11688 locate_v1_virtual_dwo_sections (asection *sectp,
11689 struct virtual_v1_dwo_sections *sections)
11690 {
11691 const struct dwop_section_names *names = &dwop_section_names;
11692
11693 if (section_is_p (sectp->name, &names->abbrev_dwo))
11694 {
11695 /* There can be only one. */
11696 if (sections->abbrev.s.section != NULL)
11697 return 0;
11698 sections->abbrev.s.section = sectp;
11699 sections->abbrev.size = bfd_section_size (sectp);
11700 }
11701 else if (section_is_p (sectp->name, &names->info_dwo)
11702 || section_is_p (sectp->name, &names->types_dwo))
11703 {
11704 /* There can be only one. */
11705 if (sections->info_or_types.s.section != NULL)
11706 return 0;
11707 sections->info_or_types.s.section = sectp;
11708 sections->info_or_types.size = bfd_section_size (sectp);
11709 }
11710 else if (section_is_p (sectp->name, &names->line_dwo))
11711 {
11712 /* There can be only one. */
11713 if (sections->line.s.section != NULL)
11714 return 0;
11715 sections->line.s.section = sectp;
11716 sections->line.size = bfd_section_size (sectp);
11717 }
11718 else if (section_is_p (sectp->name, &names->loc_dwo))
11719 {
11720 /* There can be only one. */
11721 if (sections->loc.s.section != NULL)
11722 return 0;
11723 sections->loc.s.section = sectp;
11724 sections->loc.size = bfd_section_size (sectp);
11725 }
11726 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11727 {
11728 /* There can be only one. */
11729 if (sections->macinfo.s.section != NULL)
11730 return 0;
11731 sections->macinfo.s.section = sectp;
11732 sections->macinfo.size = bfd_section_size (sectp);
11733 }
11734 else if (section_is_p (sectp->name, &names->macro_dwo))
11735 {
11736 /* There can be only one. */
11737 if (sections->macro.s.section != NULL)
11738 return 0;
11739 sections->macro.s.section = sectp;
11740 sections->macro.size = bfd_section_size (sectp);
11741 }
11742 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11743 {
11744 /* There can be only one. */
11745 if (sections->str_offsets.s.section != NULL)
11746 return 0;
11747 sections->str_offsets.s.section = sectp;
11748 sections->str_offsets.size = bfd_section_size (sectp);
11749 }
11750 else
11751 {
11752 /* No other kind of section is valid. */
11753 return 0;
11754 }
11755
11756 return 1;
11757 }
11758
11759 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11760 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11761 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11762 This is for DWP version 1 files. */
11763
11764 static struct dwo_unit *
11765 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11766 struct dwp_file *dwp_file,
11767 uint32_t unit_index,
11768 const char *comp_dir,
11769 ULONGEST signature, int is_debug_types)
11770 {
11771 const struct dwp_hash_table *dwp_htab =
11772 is_debug_types ? dwp_file->tus : dwp_file->cus;
11773 bfd *dbfd = dwp_file->dbfd.get ();
11774 const char *kind = is_debug_types ? "TU" : "CU";
11775 struct dwo_file *dwo_file;
11776 struct dwo_unit *dwo_unit;
11777 struct virtual_v1_dwo_sections sections;
11778 void **dwo_file_slot;
11779 int i;
11780
11781 gdb_assert (dwp_file->version == 1);
11782
11783 if (dwarf_read_debug)
11784 {
11785 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11786 kind,
11787 pulongest (unit_index), hex_string (signature),
11788 dwp_file->name);
11789 }
11790
11791 /* Fetch the sections of this DWO unit.
11792 Put a limit on the number of sections we look for so that bad data
11793 doesn't cause us to loop forever. */
11794
11795 #define MAX_NR_V1_DWO_SECTIONS \
11796 (1 /* .debug_info or .debug_types */ \
11797 + 1 /* .debug_abbrev */ \
11798 + 1 /* .debug_line */ \
11799 + 1 /* .debug_loc */ \
11800 + 1 /* .debug_str_offsets */ \
11801 + 1 /* .debug_macro or .debug_macinfo */ \
11802 + 1 /* trailing zero */)
11803
11804 memset (&sections, 0, sizeof (sections));
11805
11806 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11807 {
11808 asection *sectp;
11809 uint32_t section_nr =
11810 read_4_bytes (dbfd,
11811 dwp_htab->section_pool.v1.indices
11812 + (unit_index + i) * sizeof (uint32_t));
11813
11814 if (section_nr == 0)
11815 break;
11816 if (section_nr >= dwp_file->num_sections)
11817 {
11818 error (_("Dwarf Error: bad DWP hash table, section number too large"
11819 " [in module %s]"),
11820 dwp_file->name);
11821 }
11822
11823 sectp = dwp_file->elf_sections[section_nr];
11824 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11825 {
11826 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11827 " [in module %s]"),
11828 dwp_file->name);
11829 }
11830 }
11831
11832 if (i < 2
11833 || sections.info_or_types.empty ()
11834 || sections.abbrev.empty ())
11835 {
11836 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11837 " [in module %s]"),
11838 dwp_file->name);
11839 }
11840 if (i == MAX_NR_V1_DWO_SECTIONS)
11841 {
11842 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11843 " [in module %s]"),
11844 dwp_file->name);
11845 }
11846
11847 /* It's easier for the rest of the code if we fake a struct dwo_file and
11848 have dwo_unit "live" in that. At least for now.
11849
11850 The DWP file can be made up of a random collection of CUs and TUs.
11851 However, for each CU + set of TUs that came from the same original DWO
11852 file, we can combine them back into a virtual DWO file to save space
11853 (fewer struct dwo_file objects to allocate). Remember that for really
11854 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11855
11856 std::string virtual_dwo_name =
11857 string_printf ("virtual-dwo/%d-%d-%d-%d",
11858 sections.abbrev.get_id (),
11859 sections.line.get_id (),
11860 sections.loc.get_id (),
11861 sections.str_offsets.get_id ());
11862 /* Can we use an existing virtual DWO file? */
11863 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11864 virtual_dwo_name.c_str (),
11865 comp_dir);
11866 /* Create one if necessary. */
11867 if (*dwo_file_slot == NULL)
11868 {
11869 if (dwarf_read_debug)
11870 {
11871 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11872 virtual_dwo_name.c_str ());
11873 }
11874 dwo_file = new struct dwo_file;
11875 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11876 dwo_file->comp_dir = comp_dir;
11877 dwo_file->sections.abbrev = sections.abbrev;
11878 dwo_file->sections.line = sections.line;
11879 dwo_file->sections.loc = sections.loc;
11880 dwo_file->sections.macinfo = sections.macinfo;
11881 dwo_file->sections.macro = sections.macro;
11882 dwo_file->sections.str_offsets = sections.str_offsets;
11883 /* The "str" section is global to the entire DWP file. */
11884 dwo_file->sections.str = dwp_file->sections.str;
11885 /* The info or types section is assigned below to dwo_unit,
11886 there's no need to record it in dwo_file.
11887 Also, we can't simply record type sections in dwo_file because
11888 we record a pointer into the vector in dwo_unit. As we collect more
11889 types we'll grow the vector and eventually have to reallocate space
11890 for it, invalidating all copies of pointers into the previous
11891 contents. */
11892 *dwo_file_slot = dwo_file;
11893 }
11894 else
11895 {
11896 if (dwarf_read_debug)
11897 {
11898 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11899 virtual_dwo_name.c_str ());
11900 }
11901 dwo_file = (struct dwo_file *) *dwo_file_slot;
11902 }
11903
11904 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11905 dwo_unit->dwo_file = dwo_file;
11906 dwo_unit->signature = signature;
11907 dwo_unit->section =
11908 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11909 *dwo_unit->section = sections.info_or_types;
11910 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11911
11912 return dwo_unit;
11913 }
11914
11915 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11916 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11917 piece within that section used by a TU/CU, return a virtual section
11918 of just that piece. */
11919
11920 static struct dwarf2_section_info
11921 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11922 struct dwarf2_section_info *section,
11923 bfd_size_type offset, bfd_size_type size)
11924 {
11925 struct dwarf2_section_info result;
11926 asection *sectp;
11927
11928 gdb_assert (section != NULL);
11929 gdb_assert (!section->is_virtual);
11930
11931 memset (&result, 0, sizeof (result));
11932 result.s.containing_section = section;
11933 result.is_virtual = true;
11934
11935 if (size == 0)
11936 return result;
11937
11938 sectp = section->get_bfd_section ();
11939
11940 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11941 bounds of the real section. This is a pretty-rare event, so just
11942 flag an error (easier) instead of a warning and trying to cope. */
11943 if (sectp == NULL
11944 || offset + size > bfd_section_size (sectp))
11945 {
11946 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11947 " in section %s [in module %s]"),
11948 sectp ? bfd_section_name (sectp) : "<unknown>",
11949 objfile_name (dwarf2_per_objfile->objfile));
11950 }
11951
11952 result.virtual_offset = offset;
11953 result.size = size;
11954 return result;
11955 }
11956
11957 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11958 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11959 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11960 This is for DWP version 2 files. */
11961
11962 static struct dwo_unit *
11963 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11964 struct dwp_file *dwp_file,
11965 uint32_t unit_index,
11966 const char *comp_dir,
11967 ULONGEST signature, int is_debug_types)
11968 {
11969 const struct dwp_hash_table *dwp_htab =
11970 is_debug_types ? dwp_file->tus : dwp_file->cus;
11971 bfd *dbfd = dwp_file->dbfd.get ();
11972 const char *kind = is_debug_types ? "TU" : "CU";
11973 struct dwo_file *dwo_file;
11974 struct dwo_unit *dwo_unit;
11975 struct virtual_v2_dwo_sections sections;
11976 void **dwo_file_slot;
11977 int i;
11978
11979 gdb_assert (dwp_file->version == 2);
11980
11981 if (dwarf_read_debug)
11982 {
11983 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11984 kind,
11985 pulongest (unit_index), hex_string (signature),
11986 dwp_file->name);
11987 }
11988
11989 /* Fetch the section offsets of this DWO unit. */
11990
11991 memset (&sections, 0, sizeof (sections));
11992
11993 for (i = 0; i < dwp_htab->nr_columns; ++i)
11994 {
11995 uint32_t offset = read_4_bytes (dbfd,
11996 dwp_htab->section_pool.v2.offsets
11997 + (((unit_index - 1) * dwp_htab->nr_columns
11998 + i)
11999 * sizeof (uint32_t)));
12000 uint32_t size = read_4_bytes (dbfd,
12001 dwp_htab->section_pool.v2.sizes
12002 + (((unit_index - 1) * dwp_htab->nr_columns
12003 + i)
12004 * sizeof (uint32_t)));
12005
12006 switch (dwp_htab->section_pool.v2.section_ids[i])
12007 {
12008 case DW_SECT_INFO:
12009 case DW_SECT_TYPES:
12010 sections.info_or_types_offset = offset;
12011 sections.info_or_types_size = size;
12012 break;
12013 case DW_SECT_ABBREV:
12014 sections.abbrev_offset = offset;
12015 sections.abbrev_size = size;
12016 break;
12017 case DW_SECT_LINE:
12018 sections.line_offset = offset;
12019 sections.line_size = size;
12020 break;
12021 case DW_SECT_LOC:
12022 sections.loc_offset = offset;
12023 sections.loc_size = size;
12024 break;
12025 case DW_SECT_STR_OFFSETS:
12026 sections.str_offsets_offset = offset;
12027 sections.str_offsets_size = size;
12028 break;
12029 case DW_SECT_MACINFO:
12030 sections.macinfo_offset = offset;
12031 sections.macinfo_size = size;
12032 break;
12033 case DW_SECT_MACRO:
12034 sections.macro_offset = offset;
12035 sections.macro_size = size;
12036 break;
12037 }
12038 }
12039
12040 /* It's easier for the rest of the code if we fake a struct dwo_file and
12041 have dwo_unit "live" in that. At least for now.
12042
12043 The DWP file can be made up of a random collection of CUs and TUs.
12044 However, for each CU + set of TUs that came from the same original DWO
12045 file, we can combine them back into a virtual DWO file to save space
12046 (fewer struct dwo_file objects to allocate). Remember that for really
12047 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12048
12049 std::string virtual_dwo_name =
12050 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12051 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12052 (long) (sections.line_size ? sections.line_offset : 0),
12053 (long) (sections.loc_size ? sections.loc_offset : 0),
12054 (long) (sections.str_offsets_size
12055 ? sections.str_offsets_offset : 0));
12056 /* Can we use an existing virtual DWO file? */
12057 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12058 virtual_dwo_name.c_str (),
12059 comp_dir);
12060 /* Create one if necessary. */
12061 if (*dwo_file_slot == NULL)
12062 {
12063 if (dwarf_read_debug)
12064 {
12065 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12066 virtual_dwo_name.c_str ());
12067 }
12068 dwo_file = new struct dwo_file;
12069 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12070 dwo_file->comp_dir = comp_dir;
12071 dwo_file->sections.abbrev =
12072 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12073 sections.abbrev_offset, sections.abbrev_size);
12074 dwo_file->sections.line =
12075 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12076 sections.line_offset, sections.line_size);
12077 dwo_file->sections.loc =
12078 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12079 sections.loc_offset, sections.loc_size);
12080 dwo_file->sections.macinfo =
12081 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12082 sections.macinfo_offset, sections.macinfo_size);
12083 dwo_file->sections.macro =
12084 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12085 sections.macro_offset, sections.macro_size);
12086 dwo_file->sections.str_offsets =
12087 create_dwp_v2_section (dwarf2_per_objfile,
12088 &dwp_file->sections.str_offsets,
12089 sections.str_offsets_offset,
12090 sections.str_offsets_size);
12091 /* The "str" section is global to the entire DWP file. */
12092 dwo_file->sections.str = dwp_file->sections.str;
12093 /* The info or types section is assigned below to dwo_unit,
12094 there's no need to record it in dwo_file.
12095 Also, we can't simply record type sections in dwo_file because
12096 we record a pointer into the vector in dwo_unit. As we collect more
12097 types we'll grow the vector and eventually have to reallocate space
12098 for it, invalidating all copies of pointers into the previous
12099 contents. */
12100 *dwo_file_slot = dwo_file;
12101 }
12102 else
12103 {
12104 if (dwarf_read_debug)
12105 {
12106 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12107 virtual_dwo_name.c_str ());
12108 }
12109 dwo_file = (struct dwo_file *) *dwo_file_slot;
12110 }
12111
12112 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12113 dwo_unit->dwo_file = dwo_file;
12114 dwo_unit->signature = signature;
12115 dwo_unit->section =
12116 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12117 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12118 is_debug_types
12119 ? &dwp_file->sections.types
12120 : &dwp_file->sections.info,
12121 sections.info_or_types_offset,
12122 sections.info_or_types_size);
12123 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12124
12125 return dwo_unit;
12126 }
12127
12128 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12129 Returns NULL if the signature isn't found. */
12130
12131 static struct dwo_unit *
12132 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12133 struct dwp_file *dwp_file, const char *comp_dir,
12134 ULONGEST signature, int is_debug_types)
12135 {
12136 const struct dwp_hash_table *dwp_htab =
12137 is_debug_types ? dwp_file->tus : dwp_file->cus;
12138 bfd *dbfd = dwp_file->dbfd.get ();
12139 uint32_t mask = dwp_htab->nr_slots - 1;
12140 uint32_t hash = signature & mask;
12141 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12142 unsigned int i;
12143 void **slot;
12144 struct dwo_unit find_dwo_cu;
12145
12146 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12147 find_dwo_cu.signature = signature;
12148 slot = htab_find_slot (is_debug_types
12149 ? dwp_file->loaded_tus.get ()
12150 : dwp_file->loaded_cus.get (),
12151 &find_dwo_cu, INSERT);
12152
12153 if (*slot != NULL)
12154 return (struct dwo_unit *) *slot;
12155
12156 /* Use a for loop so that we don't loop forever on bad debug info. */
12157 for (i = 0; i < dwp_htab->nr_slots; ++i)
12158 {
12159 ULONGEST signature_in_table;
12160
12161 signature_in_table =
12162 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12163 if (signature_in_table == signature)
12164 {
12165 uint32_t unit_index =
12166 read_4_bytes (dbfd,
12167 dwp_htab->unit_table + hash * sizeof (uint32_t));
12168
12169 if (dwp_file->version == 1)
12170 {
12171 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12172 dwp_file, unit_index,
12173 comp_dir, signature,
12174 is_debug_types);
12175 }
12176 else
12177 {
12178 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12179 dwp_file, unit_index,
12180 comp_dir, signature,
12181 is_debug_types);
12182 }
12183 return (struct dwo_unit *) *slot;
12184 }
12185 if (signature_in_table == 0)
12186 return NULL;
12187 hash = (hash + hash2) & mask;
12188 }
12189
12190 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12191 " [in module %s]"),
12192 dwp_file->name);
12193 }
12194
12195 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12196 Open the file specified by FILE_NAME and hand it off to BFD for
12197 preliminary analysis. Return a newly initialized bfd *, which
12198 includes a canonicalized copy of FILE_NAME.
12199 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12200 SEARCH_CWD is true if the current directory is to be searched.
12201 It will be searched before debug-file-directory.
12202 If successful, the file is added to the bfd include table of the
12203 objfile's bfd (see gdb_bfd_record_inclusion).
12204 If unable to find/open the file, return NULL.
12205 NOTE: This function is derived from symfile_bfd_open. */
12206
12207 static gdb_bfd_ref_ptr
12208 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12209 const char *file_name, int is_dwp, int search_cwd)
12210 {
12211 int desc;
12212 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12213 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12214 to debug_file_directory. */
12215 const char *search_path;
12216 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12217
12218 gdb::unique_xmalloc_ptr<char> search_path_holder;
12219 if (search_cwd)
12220 {
12221 if (*debug_file_directory != '\0')
12222 {
12223 search_path_holder.reset (concat (".", dirname_separator_string,
12224 debug_file_directory,
12225 (char *) NULL));
12226 search_path = search_path_holder.get ();
12227 }
12228 else
12229 search_path = ".";
12230 }
12231 else
12232 search_path = debug_file_directory;
12233
12234 openp_flags flags = OPF_RETURN_REALPATH;
12235 if (is_dwp)
12236 flags |= OPF_SEARCH_IN_PATH;
12237
12238 gdb::unique_xmalloc_ptr<char> absolute_name;
12239 desc = openp (search_path, flags, file_name,
12240 O_RDONLY | O_BINARY, &absolute_name);
12241 if (desc < 0)
12242 return NULL;
12243
12244 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12245 gnutarget, desc));
12246 if (sym_bfd == NULL)
12247 return NULL;
12248 bfd_set_cacheable (sym_bfd.get (), 1);
12249
12250 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12251 return NULL;
12252
12253 /* Success. Record the bfd as having been included by the objfile's bfd.
12254 This is important because things like demangled_names_hash lives in the
12255 objfile's per_bfd space and may have references to things like symbol
12256 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12257 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12258
12259 return sym_bfd;
12260 }
12261
12262 /* Try to open DWO file FILE_NAME.
12263 COMP_DIR is the DW_AT_comp_dir attribute.
12264 The result is the bfd handle of the file.
12265 If there is a problem finding or opening the file, return NULL.
12266 Upon success, the canonicalized path of the file is stored in the bfd,
12267 same as symfile_bfd_open. */
12268
12269 static gdb_bfd_ref_ptr
12270 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12271 const char *file_name, const char *comp_dir)
12272 {
12273 if (IS_ABSOLUTE_PATH (file_name))
12274 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12275 0 /*is_dwp*/, 0 /*search_cwd*/);
12276
12277 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12278
12279 if (comp_dir != NULL)
12280 {
12281 gdb::unique_xmalloc_ptr<char> path_to_try
12282 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12283
12284 /* NOTE: If comp_dir is a relative path, this will also try the
12285 search path, which seems useful. */
12286 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12287 path_to_try.get (),
12288 0 /*is_dwp*/,
12289 1 /*search_cwd*/));
12290 if (abfd != NULL)
12291 return abfd;
12292 }
12293
12294 /* That didn't work, try debug-file-directory, which, despite its name,
12295 is a list of paths. */
12296
12297 if (*debug_file_directory == '\0')
12298 return NULL;
12299
12300 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12301 0 /*is_dwp*/, 1 /*search_cwd*/);
12302 }
12303
12304 /* This function is mapped across the sections and remembers the offset and
12305 size of each of the DWO debugging sections we are interested in. */
12306
12307 static void
12308 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12309 {
12310 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12311 const struct dwop_section_names *names = &dwop_section_names;
12312
12313 if (section_is_p (sectp->name, &names->abbrev_dwo))
12314 {
12315 dwo_sections->abbrev.s.section = sectp;
12316 dwo_sections->abbrev.size = bfd_section_size (sectp);
12317 }
12318 else if (section_is_p (sectp->name, &names->info_dwo))
12319 {
12320 dwo_sections->info.s.section = sectp;
12321 dwo_sections->info.size = bfd_section_size (sectp);
12322 }
12323 else if (section_is_p (sectp->name, &names->line_dwo))
12324 {
12325 dwo_sections->line.s.section = sectp;
12326 dwo_sections->line.size = bfd_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->loc_dwo))
12329 {
12330 dwo_sections->loc.s.section = sectp;
12331 dwo_sections->loc.size = bfd_section_size (sectp);
12332 }
12333 else if (section_is_p (sectp->name, &names->loclists_dwo))
12334 {
12335 dwo_sections->loclists.s.section = sectp;
12336 dwo_sections->loclists.size = bfd_section_size (sectp);
12337 }
12338 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12339 {
12340 dwo_sections->macinfo.s.section = sectp;
12341 dwo_sections->macinfo.size = bfd_section_size (sectp);
12342 }
12343 else if (section_is_p (sectp->name, &names->macro_dwo))
12344 {
12345 dwo_sections->macro.s.section = sectp;
12346 dwo_sections->macro.size = bfd_section_size (sectp);
12347 }
12348 else if (section_is_p (sectp->name, &names->str_dwo))
12349 {
12350 dwo_sections->str.s.section = sectp;
12351 dwo_sections->str.size = bfd_section_size (sectp);
12352 }
12353 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12354 {
12355 dwo_sections->str_offsets.s.section = sectp;
12356 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12357 }
12358 else if (section_is_p (sectp->name, &names->types_dwo))
12359 {
12360 struct dwarf2_section_info type_section;
12361
12362 memset (&type_section, 0, sizeof (type_section));
12363 type_section.s.section = sectp;
12364 type_section.size = bfd_section_size (sectp);
12365 dwo_sections->types.push_back (type_section);
12366 }
12367 }
12368
12369 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12370 by PER_CU. This is for the non-DWP case.
12371 The result is NULL if DWO_NAME can't be found. */
12372
12373 static struct dwo_file *
12374 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12375 const char *comp_dir)
12376 {
12377 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12378
12379 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12380 if (dbfd == NULL)
12381 {
12382 if (dwarf_read_debug)
12383 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12384 return NULL;
12385 }
12386
12387 dwo_file_up dwo_file (new struct dwo_file);
12388 dwo_file->dwo_name = dwo_name;
12389 dwo_file->comp_dir = comp_dir;
12390 dwo_file->dbfd = std::move (dbfd);
12391
12392 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12393 &dwo_file->sections);
12394
12395 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12396 dwo_file->sections.info, dwo_file->cus);
12397
12398 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12399 dwo_file->sections.types, dwo_file->tus);
12400
12401 if (dwarf_read_debug)
12402 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12403
12404 return dwo_file.release ();
12405 }
12406
12407 /* This function is mapped across the sections and remembers the offset and
12408 size of each of the DWP debugging sections common to version 1 and 2 that
12409 we are interested in. */
12410
12411 static void
12412 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12413 void *dwp_file_ptr)
12414 {
12415 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12416 const struct dwop_section_names *names = &dwop_section_names;
12417 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12418
12419 /* Record the ELF section number for later lookup: this is what the
12420 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12421 gdb_assert (elf_section_nr < dwp_file->num_sections);
12422 dwp_file->elf_sections[elf_section_nr] = sectp;
12423
12424 /* Look for specific sections that we need. */
12425 if (section_is_p (sectp->name, &names->str_dwo))
12426 {
12427 dwp_file->sections.str.s.section = sectp;
12428 dwp_file->sections.str.size = bfd_section_size (sectp);
12429 }
12430 else if (section_is_p (sectp->name, &names->cu_index))
12431 {
12432 dwp_file->sections.cu_index.s.section = sectp;
12433 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12434 }
12435 else if (section_is_p (sectp->name, &names->tu_index))
12436 {
12437 dwp_file->sections.tu_index.s.section = sectp;
12438 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12439 }
12440 }
12441
12442 /* This function is mapped across the sections and remembers the offset and
12443 size of each of the DWP version 2 debugging sections that we are interested
12444 in. This is split into a separate function because we don't know if we
12445 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12446
12447 static void
12448 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12449 {
12450 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12451 const struct dwop_section_names *names = &dwop_section_names;
12452 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12453
12454 /* Record the ELF section number for later lookup: this is what the
12455 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12456 gdb_assert (elf_section_nr < dwp_file->num_sections);
12457 dwp_file->elf_sections[elf_section_nr] = sectp;
12458
12459 /* Look for specific sections that we need. */
12460 if (section_is_p (sectp->name, &names->abbrev_dwo))
12461 {
12462 dwp_file->sections.abbrev.s.section = sectp;
12463 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12464 }
12465 else if (section_is_p (sectp->name, &names->info_dwo))
12466 {
12467 dwp_file->sections.info.s.section = sectp;
12468 dwp_file->sections.info.size = bfd_section_size (sectp);
12469 }
12470 else if (section_is_p (sectp->name, &names->line_dwo))
12471 {
12472 dwp_file->sections.line.s.section = sectp;
12473 dwp_file->sections.line.size = bfd_section_size (sectp);
12474 }
12475 else if (section_is_p (sectp->name, &names->loc_dwo))
12476 {
12477 dwp_file->sections.loc.s.section = sectp;
12478 dwp_file->sections.loc.size = bfd_section_size (sectp);
12479 }
12480 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12481 {
12482 dwp_file->sections.macinfo.s.section = sectp;
12483 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12484 }
12485 else if (section_is_p (sectp->name, &names->macro_dwo))
12486 {
12487 dwp_file->sections.macro.s.section = sectp;
12488 dwp_file->sections.macro.size = bfd_section_size (sectp);
12489 }
12490 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12491 {
12492 dwp_file->sections.str_offsets.s.section = sectp;
12493 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12494 }
12495 else if (section_is_p (sectp->name, &names->types_dwo))
12496 {
12497 dwp_file->sections.types.s.section = sectp;
12498 dwp_file->sections.types.size = bfd_section_size (sectp);
12499 }
12500 }
12501
12502 /* Hash function for dwp_file loaded CUs/TUs. */
12503
12504 static hashval_t
12505 hash_dwp_loaded_cutus (const void *item)
12506 {
12507 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12508
12509 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12510 return dwo_unit->signature;
12511 }
12512
12513 /* Equality function for dwp_file loaded CUs/TUs. */
12514
12515 static int
12516 eq_dwp_loaded_cutus (const void *a, const void *b)
12517 {
12518 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12519 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12520
12521 return dua->signature == dub->signature;
12522 }
12523
12524 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12525
12526 static htab_up
12527 allocate_dwp_loaded_cutus_table ()
12528 {
12529 return htab_up (htab_create_alloc (3,
12530 hash_dwp_loaded_cutus,
12531 eq_dwp_loaded_cutus,
12532 NULL, xcalloc, xfree));
12533 }
12534
12535 /* Try to open DWP file FILE_NAME.
12536 The result is the bfd handle of the file.
12537 If there is a problem finding or opening the file, return NULL.
12538 Upon success, the canonicalized path of the file is stored in the bfd,
12539 same as symfile_bfd_open. */
12540
12541 static gdb_bfd_ref_ptr
12542 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12543 const char *file_name)
12544 {
12545 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12546 1 /*is_dwp*/,
12547 1 /*search_cwd*/));
12548 if (abfd != NULL)
12549 return abfd;
12550
12551 /* Work around upstream bug 15652.
12552 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12553 [Whether that's a "bug" is debatable, but it is getting in our way.]
12554 We have no real idea where the dwp file is, because gdb's realpath-ing
12555 of the executable's path may have discarded the needed info.
12556 [IWBN if the dwp file name was recorded in the executable, akin to
12557 .gnu_debuglink, but that doesn't exist yet.]
12558 Strip the directory from FILE_NAME and search again. */
12559 if (*debug_file_directory != '\0')
12560 {
12561 /* Don't implicitly search the current directory here.
12562 If the user wants to search "." to handle this case,
12563 it must be added to debug-file-directory. */
12564 return try_open_dwop_file (dwarf2_per_objfile,
12565 lbasename (file_name), 1 /*is_dwp*/,
12566 0 /*search_cwd*/);
12567 }
12568
12569 return NULL;
12570 }
12571
12572 /* Initialize the use of the DWP file for the current objfile.
12573 By convention the name of the DWP file is ${objfile}.dwp.
12574 The result is NULL if it can't be found. */
12575
12576 static std::unique_ptr<struct dwp_file>
12577 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12578 {
12579 struct objfile *objfile = dwarf2_per_objfile->objfile;
12580
12581 /* Try to find first .dwp for the binary file before any symbolic links
12582 resolving. */
12583
12584 /* If the objfile is a debug file, find the name of the real binary
12585 file and get the name of dwp file from there. */
12586 std::string dwp_name;
12587 if (objfile->separate_debug_objfile_backlink != NULL)
12588 {
12589 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12590 const char *backlink_basename = lbasename (backlink->original_name);
12591
12592 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12593 }
12594 else
12595 dwp_name = objfile->original_name;
12596
12597 dwp_name += ".dwp";
12598
12599 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12600 if (dbfd == NULL
12601 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12602 {
12603 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12604 dwp_name = objfile_name (objfile);
12605 dwp_name += ".dwp";
12606 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12607 }
12608
12609 if (dbfd == NULL)
12610 {
12611 if (dwarf_read_debug)
12612 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12613 return std::unique_ptr<dwp_file> ();
12614 }
12615
12616 const char *name = bfd_get_filename (dbfd.get ());
12617 std::unique_ptr<struct dwp_file> dwp_file
12618 (new struct dwp_file (name, std::move (dbfd)));
12619
12620 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12621 dwp_file->elf_sections =
12622 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12623 dwp_file->num_sections, asection *);
12624
12625 bfd_map_over_sections (dwp_file->dbfd.get (),
12626 dwarf2_locate_common_dwp_sections,
12627 dwp_file.get ());
12628
12629 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12630 0);
12631
12632 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12633 1);
12634
12635 /* The DWP file version is stored in the hash table. Oh well. */
12636 if (dwp_file->cus && dwp_file->tus
12637 && dwp_file->cus->version != dwp_file->tus->version)
12638 {
12639 /* Technically speaking, we should try to limp along, but this is
12640 pretty bizarre. We use pulongest here because that's the established
12641 portability solution (e.g, we cannot use %u for uint32_t). */
12642 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12643 " TU version %s [in DWP file %s]"),
12644 pulongest (dwp_file->cus->version),
12645 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12646 }
12647
12648 if (dwp_file->cus)
12649 dwp_file->version = dwp_file->cus->version;
12650 else if (dwp_file->tus)
12651 dwp_file->version = dwp_file->tus->version;
12652 else
12653 dwp_file->version = 2;
12654
12655 if (dwp_file->version == 2)
12656 bfd_map_over_sections (dwp_file->dbfd.get (),
12657 dwarf2_locate_v2_dwp_sections,
12658 dwp_file.get ());
12659
12660 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12661 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12662
12663 if (dwarf_read_debug)
12664 {
12665 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12666 fprintf_unfiltered (gdb_stdlog,
12667 " %s CUs, %s TUs\n",
12668 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12669 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12670 }
12671
12672 return dwp_file;
12673 }
12674
12675 /* Wrapper around open_and_init_dwp_file, only open it once. */
12676
12677 static struct dwp_file *
12678 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12679 {
12680 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12681 {
12682 dwarf2_per_objfile->per_bfd->dwp_file
12683 = open_and_init_dwp_file (dwarf2_per_objfile);
12684 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12685 }
12686 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12687 }
12688
12689 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12690 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12691 or in the DWP file for the objfile, referenced by THIS_UNIT.
12692 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12693 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12694
12695 This is called, for example, when wanting to read a variable with a
12696 complex location. Therefore we don't want to do file i/o for every call.
12697 Therefore we don't want to look for a DWO file on every call.
12698 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12699 then we check if we've already seen DWO_NAME, and only THEN do we check
12700 for a DWO file.
12701
12702 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12703 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12704
12705 static struct dwo_unit *
12706 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12707 ULONGEST signature, int is_debug_types)
12708 {
12709 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12710 struct objfile *objfile = dwarf2_per_objfile->objfile;
12711 const char *kind = is_debug_types ? "TU" : "CU";
12712 void **dwo_file_slot;
12713 struct dwo_file *dwo_file;
12714 struct dwp_file *dwp_file;
12715
12716 /* First see if there's a DWP file.
12717 If we have a DWP file but didn't find the DWO inside it, don't
12718 look for the original DWO file. It makes gdb behave differently
12719 depending on whether one is debugging in the build tree. */
12720
12721 dwp_file = get_dwp_file (dwarf2_per_objfile);
12722 if (dwp_file != NULL)
12723 {
12724 const struct dwp_hash_table *dwp_htab =
12725 is_debug_types ? dwp_file->tus : dwp_file->cus;
12726
12727 if (dwp_htab != NULL)
12728 {
12729 struct dwo_unit *dwo_cutu =
12730 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12731 signature, is_debug_types);
12732
12733 if (dwo_cutu != NULL)
12734 {
12735 if (dwarf_read_debug)
12736 {
12737 fprintf_unfiltered (gdb_stdlog,
12738 "Virtual DWO %s %s found: @%s\n",
12739 kind, hex_string (signature),
12740 host_address_to_string (dwo_cutu));
12741 }
12742 return dwo_cutu;
12743 }
12744 }
12745 }
12746 else
12747 {
12748 /* No DWP file, look for the DWO file. */
12749
12750 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12751 dwo_name, comp_dir);
12752 if (*dwo_file_slot == NULL)
12753 {
12754 /* Read in the file and build a table of the CUs/TUs it contains. */
12755 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12756 }
12757 /* NOTE: This will be NULL if unable to open the file. */
12758 dwo_file = (struct dwo_file *) *dwo_file_slot;
12759
12760 if (dwo_file != NULL)
12761 {
12762 struct dwo_unit *dwo_cutu = NULL;
12763
12764 if (is_debug_types && dwo_file->tus)
12765 {
12766 struct dwo_unit find_dwo_cutu;
12767
12768 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12769 find_dwo_cutu.signature = signature;
12770 dwo_cutu
12771 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12772 &find_dwo_cutu);
12773 }
12774 else if (!is_debug_types && dwo_file->cus)
12775 {
12776 struct dwo_unit find_dwo_cutu;
12777
12778 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12779 find_dwo_cutu.signature = signature;
12780 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12781 &find_dwo_cutu);
12782 }
12783
12784 if (dwo_cutu != NULL)
12785 {
12786 if (dwarf_read_debug)
12787 {
12788 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12789 kind, dwo_name, hex_string (signature),
12790 host_address_to_string (dwo_cutu));
12791 }
12792 return dwo_cutu;
12793 }
12794 }
12795 }
12796
12797 /* We didn't find it. This could mean a dwo_id mismatch, or
12798 someone deleted the DWO/DWP file, or the search path isn't set up
12799 correctly to find the file. */
12800
12801 if (dwarf_read_debug)
12802 {
12803 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12804 kind, dwo_name, hex_string (signature));
12805 }
12806
12807 /* This is a warning and not a complaint because it can be caused by
12808 pilot error (e.g., user accidentally deleting the DWO). */
12809 {
12810 /* Print the name of the DWP file if we looked there, helps the user
12811 better diagnose the problem. */
12812 std::string dwp_text;
12813
12814 if (dwp_file != NULL)
12815 dwp_text = string_printf (" [in DWP file %s]",
12816 lbasename (dwp_file->name));
12817
12818 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12819 " [in module %s]"),
12820 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12821 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12822 }
12823 return NULL;
12824 }
12825
12826 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12827 See lookup_dwo_cutu_unit for details. */
12828
12829 static struct dwo_unit *
12830 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12831 ULONGEST signature)
12832 {
12833 gdb_assert (!cu->per_cu->is_debug_types);
12834
12835 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12836 }
12837
12838 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12839 See lookup_dwo_cutu_unit for details. */
12840
12841 static struct dwo_unit *
12842 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12843 {
12844 gdb_assert (cu->per_cu->is_debug_types);
12845
12846 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12847
12848 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12849 }
12850
12851 /* Traversal function for queue_and_load_all_dwo_tus. */
12852
12853 static int
12854 queue_and_load_dwo_tu (void **slot, void *info)
12855 {
12856 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12857 dwarf2_cu *cu = (dwarf2_cu *) info;
12858 ULONGEST signature = dwo_unit->signature;
12859 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12860
12861 if (sig_type != NULL)
12862 {
12863 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12864
12865 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12866 a real dependency of PER_CU on SIG_TYPE. That is detected later
12867 while processing PER_CU. */
12868 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12869 load_full_type_unit (sig_cu, cu->per_objfile);
12870 cu->per_cu->imported_symtabs_push (sig_cu);
12871 }
12872
12873 return 1;
12874 }
12875
12876 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12877 The DWO may have the only definition of the type, though it may not be
12878 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12879 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12880
12881 static void
12882 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12883 {
12884 struct dwo_unit *dwo_unit;
12885 struct dwo_file *dwo_file;
12886
12887 gdb_assert (!per_cu->is_debug_types);
12888 gdb_assert (per_cu->cu != NULL);
12889 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
12890
12891 dwo_unit = per_cu->cu->dwo_unit;
12892 gdb_assert (dwo_unit != NULL);
12893
12894 dwo_file = dwo_unit->dwo_file;
12895 if (dwo_file->tus != NULL)
12896 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12897 per_cu->cu);
12898 }
12899
12900 /* Read in various DIEs. */
12901
12902 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12903 Inherit only the children of the DW_AT_abstract_origin DIE not being
12904 already referenced by DW_AT_abstract_origin from the children of the
12905 current DIE. */
12906
12907 static void
12908 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12909 {
12910 struct die_info *child_die;
12911 sect_offset *offsetp;
12912 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12913 struct die_info *origin_die;
12914 /* Iterator of the ORIGIN_DIE children. */
12915 struct die_info *origin_child_die;
12916 struct attribute *attr;
12917 struct dwarf2_cu *origin_cu;
12918 struct pending **origin_previous_list_in_scope;
12919
12920 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12921 if (!attr)
12922 return;
12923
12924 /* Note that following die references may follow to a die in a
12925 different cu. */
12926
12927 origin_cu = cu;
12928 origin_die = follow_die_ref (die, attr, &origin_cu);
12929
12930 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12931 symbols in. */
12932 origin_previous_list_in_scope = origin_cu->list_in_scope;
12933 origin_cu->list_in_scope = cu->list_in_scope;
12934
12935 if (die->tag != origin_die->tag
12936 && !(die->tag == DW_TAG_inlined_subroutine
12937 && origin_die->tag == DW_TAG_subprogram))
12938 complaint (_("DIE %s and its abstract origin %s have different tags"),
12939 sect_offset_str (die->sect_off),
12940 sect_offset_str (origin_die->sect_off));
12941
12942 std::vector<sect_offset> offsets;
12943
12944 for (child_die = die->child;
12945 child_die && child_die->tag;
12946 child_die = child_die->sibling)
12947 {
12948 struct die_info *child_origin_die;
12949 struct dwarf2_cu *child_origin_cu;
12950
12951 /* We are trying to process concrete instance entries:
12952 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12953 it's not relevant to our analysis here. i.e. detecting DIEs that are
12954 present in the abstract instance but not referenced in the concrete
12955 one. */
12956 if (child_die->tag == DW_TAG_call_site
12957 || child_die->tag == DW_TAG_GNU_call_site)
12958 continue;
12959
12960 /* For each CHILD_DIE, find the corresponding child of
12961 ORIGIN_DIE. If there is more than one layer of
12962 DW_AT_abstract_origin, follow them all; there shouldn't be,
12963 but GCC versions at least through 4.4 generate this (GCC PR
12964 40573). */
12965 child_origin_die = child_die;
12966 child_origin_cu = cu;
12967 while (1)
12968 {
12969 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12970 child_origin_cu);
12971 if (attr == NULL)
12972 break;
12973 child_origin_die = follow_die_ref (child_origin_die, attr,
12974 &child_origin_cu);
12975 }
12976
12977 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12978 counterpart may exist. */
12979 if (child_origin_die != child_die)
12980 {
12981 if (child_die->tag != child_origin_die->tag
12982 && !(child_die->tag == DW_TAG_inlined_subroutine
12983 && child_origin_die->tag == DW_TAG_subprogram))
12984 complaint (_("Child DIE %s and its abstract origin %s have "
12985 "different tags"),
12986 sect_offset_str (child_die->sect_off),
12987 sect_offset_str (child_origin_die->sect_off));
12988 if (child_origin_die->parent != origin_die)
12989 complaint (_("Child DIE %s and its abstract origin %s have "
12990 "different parents"),
12991 sect_offset_str (child_die->sect_off),
12992 sect_offset_str (child_origin_die->sect_off));
12993 else
12994 offsets.push_back (child_origin_die->sect_off);
12995 }
12996 }
12997 std::sort (offsets.begin (), offsets.end ());
12998 sect_offset *offsets_end = offsets.data () + offsets.size ();
12999 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13000 if (offsetp[-1] == *offsetp)
13001 complaint (_("Multiple children of DIE %s refer "
13002 "to DIE %s as their abstract origin"),
13003 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13004
13005 offsetp = offsets.data ();
13006 origin_child_die = origin_die->child;
13007 while (origin_child_die && origin_child_die->tag)
13008 {
13009 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13010 while (offsetp < offsets_end
13011 && *offsetp < origin_child_die->sect_off)
13012 offsetp++;
13013 if (offsetp >= offsets_end
13014 || *offsetp > origin_child_die->sect_off)
13015 {
13016 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13017 Check whether we're already processing ORIGIN_CHILD_DIE.
13018 This can happen with mutually referenced abstract_origins.
13019 PR 16581. */
13020 if (!origin_child_die->in_process)
13021 process_die (origin_child_die, origin_cu);
13022 }
13023 origin_child_die = origin_child_die->sibling;
13024 }
13025 origin_cu->list_in_scope = origin_previous_list_in_scope;
13026
13027 if (cu != origin_cu)
13028 compute_delayed_physnames (origin_cu);
13029 }
13030
13031 static void
13032 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13033 {
13034 struct objfile *objfile = cu->per_objfile->objfile;
13035 struct gdbarch *gdbarch = objfile->arch ();
13036 struct context_stack *newobj;
13037 CORE_ADDR lowpc;
13038 CORE_ADDR highpc;
13039 struct die_info *child_die;
13040 struct attribute *attr, *call_line, *call_file;
13041 const char *name;
13042 CORE_ADDR baseaddr;
13043 struct block *block;
13044 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13045 std::vector<struct symbol *> template_args;
13046 struct template_symbol *templ_func = NULL;
13047
13048 if (inlined_func)
13049 {
13050 /* If we do not have call site information, we can't show the
13051 caller of this inlined function. That's too confusing, so
13052 only use the scope for local variables. */
13053 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13054 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13055 if (call_line == NULL || call_file == NULL)
13056 {
13057 read_lexical_block_scope (die, cu);
13058 return;
13059 }
13060 }
13061
13062 baseaddr = objfile->text_section_offset ();
13063
13064 name = dwarf2_name (die, cu);
13065
13066 /* Ignore functions with missing or empty names. These are actually
13067 illegal according to the DWARF standard. */
13068 if (name == NULL)
13069 {
13070 complaint (_("missing name for subprogram DIE at %s"),
13071 sect_offset_str (die->sect_off));
13072 return;
13073 }
13074
13075 /* Ignore functions with missing or invalid low and high pc attributes. */
13076 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13077 <= PC_BOUNDS_INVALID)
13078 {
13079 attr = dwarf2_attr (die, DW_AT_external, cu);
13080 if (!attr || !DW_UNSND (attr))
13081 complaint (_("cannot get low and high bounds "
13082 "for subprogram DIE at %s"),
13083 sect_offset_str (die->sect_off));
13084 return;
13085 }
13086
13087 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13088 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13089
13090 /* If we have any template arguments, then we must allocate a
13091 different sort of symbol. */
13092 for (child_die = die->child; child_die; child_die = child_die->sibling)
13093 {
13094 if (child_die->tag == DW_TAG_template_type_param
13095 || child_die->tag == DW_TAG_template_value_param)
13096 {
13097 templ_func = new (&objfile->objfile_obstack) template_symbol;
13098 templ_func->subclass = SYMBOL_TEMPLATE;
13099 break;
13100 }
13101 }
13102
13103 newobj = cu->get_builder ()->push_context (0, lowpc);
13104 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13105 (struct symbol *) templ_func);
13106
13107 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13108 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13109 cu->language);
13110
13111 /* If there is a location expression for DW_AT_frame_base, record
13112 it. */
13113 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13114 if (attr != nullptr)
13115 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13116
13117 /* If there is a location for the static link, record it. */
13118 newobj->static_link = NULL;
13119 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13120 if (attr != nullptr)
13121 {
13122 newobj->static_link
13123 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13124 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13125 cu->addr_type ());
13126 }
13127
13128 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13129
13130 if (die->child != NULL)
13131 {
13132 child_die = die->child;
13133 while (child_die && child_die->tag)
13134 {
13135 if (child_die->tag == DW_TAG_template_type_param
13136 || child_die->tag == DW_TAG_template_value_param)
13137 {
13138 struct symbol *arg = new_symbol (child_die, NULL, cu);
13139
13140 if (arg != NULL)
13141 template_args.push_back (arg);
13142 }
13143 else
13144 process_die (child_die, cu);
13145 child_die = child_die->sibling;
13146 }
13147 }
13148
13149 inherit_abstract_dies (die, cu);
13150
13151 /* If we have a DW_AT_specification, we might need to import using
13152 directives from the context of the specification DIE. See the
13153 comment in determine_prefix. */
13154 if (cu->language == language_cplus
13155 && dwarf2_attr (die, DW_AT_specification, cu))
13156 {
13157 struct dwarf2_cu *spec_cu = cu;
13158 struct die_info *spec_die = die_specification (die, &spec_cu);
13159
13160 while (spec_die)
13161 {
13162 child_die = spec_die->child;
13163 while (child_die && child_die->tag)
13164 {
13165 if (child_die->tag == DW_TAG_imported_module)
13166 process_die (child_die, spec_cu);
13167 child_die = child_die->sibling;
13168 }
13169
13170 /* In some cases, GCC generates specification DIEs that
13171 themselves contain DW_AT_specification attributes. */
13172 spec_die = die_specification (spec_die, &spec_cu);
13173 }
13174 }
13175
13176 struct context_stack cstk = cu->get_builder ()->pop_context ();
13177 /* Make a block for the local symbols within. */
13178 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13179 cstk.static_link, lowpc, highpc);
13180
13181 /* For C++, set the block's scope. */
13182 if ((cu->language == language_cplus
13183 || cu->language == language_fortran
13184 || cu->language == language_d
13185 || cu->language == language_rust)
13186 && cu->processing_has_namespace_info)
13187 block_set_scope (block, determine_prefix (die, cu),
13188 &objfile->objfile_obstack);
13189
13190 /* If we have address ranges, record them. */
13191 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13192
13193 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13194
13195 /* Attach template arguments to function. */
13196 if (!template_args.empty ())
13197 {
13198 gdb_assert (templ_func != NULL);
13199
13200 templ_func->n_template_arguments = template_args.size ();
13201 templ_func->template_arguments
13202 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13203 templ_func->n_template_arguments);
13204 memcpy (templ_func->template_arguments,
13205 template_args.data (),
13206 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13207
13208 /* Make sure that the symtab is set on the new symbols. Even
13209 though they don't appear in this symtab directly, other parts
13210 of gdb assume that symbols do, and this is reasonably
13211 true. */
13212 for (symbol *sym : template_args)
13213 symbol_set_symtab (sym, symbol_symtab (templ_func));
13214 }
13215
13216 /* In C++, we can have functions nested inside functions (e.g., when
13217 a function declares a class that has methods). This means that
13218 when we finish processing a function scope, we may need to go
13219 back to building a containing block's symbol lists. */
13220 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13221 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13222
13223 /* If we've finished processing a top-level function, subsequent
13224 symbols go in the file symbol list. */
13225 if (cu->get_builder ()->outermost_context_p ())
13226 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13227 }
13228
13229 /* Process all the DIES contained within a lexical block scope. Start
13230 a new scope, process the dies, and then close the scope. */
13231
13232 static void
13233 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13234 {
13235 struct objfile *objfile = cu->per_objfile->objfile;
13236 struct gdbarch *gdbarch = objfile->arch ();
13237 CORE_ADDR lowpc, highpc;
13238 struct die_info *child_die;
13239 CORE_ADDR baseaddr;
13240
13241 baseaddr = objfile->text_section_offset ();
13242
13243 /* Ignore blocks with missing or invalid low and high pc attributes. */
13244 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13245 as multiple lexical blocks? Handling children in a sane way would
13246 be nasty. Might be easier to properly extend generic blocks to
13247 describe ranges. */
13248 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13249 {
13250 case PC_BOUNDS_NOT_PRESENT:
13251 /* DW_TAG_lexical_block has no attributes, process its children as if
13252 there was no wrapping by that DW_TAG_lexical_block.
13253 GCC does no longer produces such DWARF since GCC r224161. */
13254 for (child_die = die->child;
13255 child_die != NULL && child_die->tag;
13256 child_die = child_die->sibling)
13257 {
13258 /* We might already be processing this DIE. This can happen
13259 in an unusual circumstance -- where a subroutine A
13260 appears lexically in another subroutine B, but A actually
13261 inlines B. The recursion is broken here, rather than in
13262 inherit_abstract_dies, because it seems better to simply
13263 drop concrete children here. */
13264 if (!child_die->in_process)
13265 process_die (child_die, cu);
13266 }
13267 return;
13268 case PC_BOUNDS_INVALID:
13269 return;
13270 }
13271 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13272 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13273
13274 cu->get_builder ()->push_context (0, lowpc);
13275 if (die->child != NULL)
13276 {
13277 child_die = die->child;
13278 while (child_die && child_die->tag)
13279 {
13280 process_die (child_die, cu);
13281 child_die = child_die->sibling;
13282 }
13283 }
13284 inherit_abstract_dies (die, cu);
13285 struct context_stack cstk = cu->get_builder ()->pop_context ();
13286
13287 if (*cu->get_builder ()->get_local_symbols () != NULL
13288 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13289 {
13290 struct block *block
13291 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13292 cstk.start_addr, highpc);
13293
13294 /* Note that recording ranges after traversing children, as we
13295 do here, means that recording a parent's ranges entails
13296 walking across all its children's ranges as they appear in
13297 the address map, which is quadratic behavior.
13298
13299 It would be nicer to record the parent's ranges before
13300 traversing its children, simply overriding whatever you find
13301 there. But since we don't even decide whether to create a
13302 block until after we've traversed its children, that's hard
13303 to do. */
13304 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13305 }
13306 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13307 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13308 }
13309
13310 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13311
13312 static void
13313 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13314 {
13315 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13316 struct objfile *objfile = per_objfile->objfile;
13317 struct gdbarch *gdbarch = objfile->arch ();
13318 CORE_ADDR pc, baseaddr;
13319 struct attribute *attr;
13320 struct call_site *call_site, call_site_local;
13321 void **slot;
13322 int nparams;
13323 struct die_info *child_die;
13324
13325 baseaddr = objfile->text_section_offset ();
13326
13327 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13328 if (attr == NULL)
13329 {
13330 /* This was a pre-DWARF-5 GNU extension alias
13331 for DW_AT_call_return_pc. */
13332 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13333 }
13334 if (!attr)
13335 {
13336 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13337 "DIE %s [in module %s]"),
13338 sect_offset_str (die->sect_off), objfile_name (objfile));
13339 return;
13340 }
13341 pc = attr->value_as_address () + baseaddr;
13342 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13343
13344 if (cu->call_site_htab == NULL)
13345 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13346 NULL, &objfile->objfile_obstack,
13347 hashtab_obstack_allocate, NULL);
13348 call_site_local.pc = pc;
13349 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13350 if (*slot != NULL)
13351 {
13352 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13353 "DIE %s [in module %s]"),
13354 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13355 objfile_name (objfile));
13356 return;
13357 }
13358
13359 /* Count parameters at the caller. */
13360
13361 nparams = 0;
13362 for (child_die = die->child; child_die && child_die->tag;
13363 child_die = child_die->sibling)
13364 {
13365 if (child_die->tag != DW_TAG_call_site_parameter
13366 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13367 {
13368 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13369 "DW_TAG_call_site child DIE %s [in module %s]"),
13370 child_die->tag, sect_offset_str (child_die->sect_off),
13371 objfile_name (objfile));
13372 continue;
13373 }
13374
13375 nparams++;
13376 }
13377
13378 call_site
13379 = ((struct call_site *)
13380 obstack_alloc (&objfile->objfile_obstack,
13381 sizeof (*call_site)
13382 + (sizeof (*call_site->parameter) * (nparams - 1))));
13383 *slot = call_site;
13384 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13385 call_site->pc = pc;
13386
13387 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13388 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13389 {
13390 struct die_info *func_die;
13391
13392 /* Skip also over DW_TAG_inlined_subroutine. */
13393 for (func_die = die->parent;
13394 func_die && func_die->tag != DW_TAG_subprogram
13395 && func_die->tag != DW_TAG_subroutine_type;
13396 func_die = func_die->parent);
13397
13398 /* DW_AT_call_all_calls is a superset
13399 of DW_AT_call_all_tail_calls. */
13400 if (func_die
13401 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13402 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13403 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13404 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13405 {
13406 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13407 not complete. But keep CALL_SITE for look ups via call_site_htab,
13408 both the initial caller containing the real return address PC and
13409 the final callee containing the current PC of a chain of tail
13410 calls do not need to have the tail call list complete. But any
13411 function candidate for a virtual tail call frame searched via
13412 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13413 determined unambiguously. */
13414 }
13415 else
13416 {
13417 struct type *func_type = NULL;
13418
13419 if (func_die)
13420 func_type = get_die_type (func_die, cu);
13421 if (func_type != NULL)
13422 {
13423 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13424
13425 /* Enlist this call site to the function. */
13426 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13427 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13428 }
13429 else
13430 complaint (_("Cannot find function owning DW_TAG_call_site "
13431 "DIE %s [in module %s]"),
13432 sect_offset_str (die->sect_off), objfile_name (objfile));
13433 }
13434 }
13435
13436 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13437 if (attr == NULL)
13438 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13439 if (attr == NULL)
13440 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13441 if (attr == NULL)
13442 {
13443 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13444 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13445 }
13446 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13447 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13448 /* Keep NULL DWARF_BLOCK. */;
13449 else if (attr->form_is_block ())
13450 {
13451 struct dwarf2_locexpr_baton *dlbaton;
13452
13453 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13454 dlbaton->data = DW_BLOCK (attr)->data;
13455 dlbaton->size = DW_BLOCK (attr)->size;
13456 dlbaton->per_objfile = per_objfile;
13457 dlbaton->per_cu = cu->per_cu;
13458
13459 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13460 }
13461 else if (attr->form_is_ref ())
13462 {
13463 struct dwarf2_cu *target_cu = cu;
13464 struct die_info *target_die;
13465
13466 target_die = follow_die_ref (die, attr, &target_cu);
13467 gdb_assert (target_cu->per_objfile->objfile == objfile);
13468 if (die_is_declaration (target_die, target_cu))
13469 {
13470 const char *target_physname;
13471
13472 /* Prefer the mangled name; otherwise compute the demangled one. */
13473 target_physname = dw2_linkage_name (target_die, target_cu);
13474 if (target_physname == NULL)
13475 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13476 if (target_physname == NULL)
13477 complaint (_("DW_AT_call_target target DIE has invalid "
13478 "physname, for referencing DIE %s [in module %s]"),
13479 sect_offset_str (die->sect_off), objfile_name (objfile));
13480 else
13481 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13482 }
13483 else
13484 {
13485 CORE_ADDR lowpc;
13486
13487 /* DW_AT_entry_pc should be preferred. */
13488 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13489 <= PC_BOUNDS_INVALID)
13490 complaint (_("DW_AT_call_target target DIE has invalid "
13491 "low pc, for referencing DIE %s [in module %s]"),
13492 sect_offset_str (die->sect_off), objfile_name (objfile));
13493 else
13494 {
13495 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13496 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13497 }
13498 }
13499 }
13500 else
13501 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13502 "block nor reference, for DIE %s [in module %s]"),
13503 sect_offset_str (die->sect_off), objfile_name (objfile));
13504
13505 call_site->per_cu = cu->per_cu;
13506 call_site->per_objfile = per_objfile;
13507
13508 for (child_die = die->child;
13509 child_die && child_die->tag;
13510 child_die = child_die->sibling)
13511 {
13512 struct call_site_parameter *parameter;
13513 struct attribute *loc, *origin;
13514
13515 if (child_die->tag != DW_TAG_call_site_parameter
13516 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13517 {
13518 /* Already printed the complaint above. */
13519 continue;
13520 }
13521
13522 gdb_assert (call_site->parameter_count < nparams);
13523 parameter = &call_site->parameter[call_site->parameter_count];
13524
13525 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13526 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13527 register is contained in DW_AT_call_value. */
13528
13529 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13530 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13531 if (origin == NULL)
13532 {
13533 /* This was a pre-DWARF-5 GNU extension alias
13534 for DW_AT_call_parameter. */
13535 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13536 }
13537 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13538 {
13539 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13540
13541 sect_offset sect_off = origin->get_ref_die_offset ();
13542 if (!cu->header.offset_in_cu_p (sect_off))
13543 {
13544 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13545 binding can be done only inside one CU. Such referenced DIE
13546 therefore cannot be even moved to DW_TAG_partial_unit. */
13547 complaint (_("DW_AT_call_parameter offset is not in CU for "
13548 "DW_TAG_call_site child DIE %s [in module %s]"),
13549 sect_offset_str (child_die->sect_off),
13550 objfile_name (objfile));
13551 continue;
13552 }
13553 parameter->u.param_cu_off
13554 = (cu_offset) (sect_off - cu->header.sect_off);
13555 }
13556 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13557 {
13558 complaint (_("No DW_FORM_block* DW_AT_location for "
13559 "DW_TAG_call_site child DIE %s [in module %s]"),
13560 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13561 continue;
13562 }
13563 else
13564 {
13565 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13566 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13567 if (parameter->u.dwarf_reg != -1)
13568 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13569 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13570 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13571 &parameter->u.fb_offset))
13572 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13573 else
13574 {
13575 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13576 "for DW_FORM_block* DW_AT_location is supported for "
13577 "DW_TAG_call_site child DIE %s "
13578 "[in module %s]"),
13579 sect_offset_str (child_die->sect_off),
13580 objfile_name (objfile));
13581 continue;
13582 }
13583 }
13584
13585 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13586 if (attr == NULL)
13587 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13588 if (attr == NULL || !attr->form_is_block ())
13589 {
13590 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13591 "DW_TAG_call_site child DIE %s [in module %s]"),
13592 sect_offset_str (child_die->sect_off),
13593 objfile_name (objfile));
13594 continue;
13595 }
13596 parameter->value = DW_BLOCK (attr)->data;
13597 parameter->value_size = DW_BLOCK (attr)->size;
13598
13599 /* Parameters are not pre-cleared by memset above. */
13600 parameter->data_value = NULL;
13601 parameter->data_value_size = 0;
13602 call_site->parameter_count++;
13603
13604 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13605 if (attr == NULL)
13606 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13607 if (attr != nullptr)
13608 {
13609 if (!attr->form_is_block ())
13610 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13611 "DW_TAG_call_site child DIE %s [in module %s]"),
13612 sect_offset_str (child_die->sect_off),
13613 objfile_name (objfile));
13614 else
13615 {
13616 parameter->data_value = DW_BLOCK (attr)->data;
13617 parameter->data_value_size = DW_BLOCK (attr)->size;
13618 }
13619 }
13620 }
13621 }
13622
13623 /* Helper function for read_variable. If DIE represents a virtual
13624 table, then return the type of the concrete object that is
13625 associated with the virtual table. Otherwise, return NULL. */
13626
13627 static struct type *
13628 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13629 {
13630 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13631 if (attr == NULL)
13632 return NULL;
13633
13634 /* Find the type DIE. */
13635 struct die_info *type_die = NULL;
13636 struct dwarf2_cu *type_cu = cu;
13637
13638 if (attr->form_is_ref ())
13639 type_die = follow_die_ref (die, attr, &type_cu);
13640 if (type_die == NULL)
13641 return NULL;
13642
13643 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13644 return NULL;
13645 return die_containing_type (type_die, type_cu);
13646 }
13647
13648 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13649
13650 static void
13651 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13652 {
13653 struct rust_vtable_symbol *storage = NULL;
13654
13655 if (cu->language == language_rust)
13656 {
13657 struct type *containing_type = rust_containing_type (die, cu);
13658
13659 if (containing_type != NULL)
13660 {
13661 struct objfile *objfile = cu->per_objfile->objfile;
13662
13663 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13664 storage->concrete_type = containing_type;
13665 storage->subclass = SYMBOL_RUST_VTABLE;
13666 }
13667 }
13668
13669 struct symbol *res = new_symbol (die, NULL, cu, storage);
13670 struct attribute *abstract_origin
13671 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13672 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13673 if (res == NULL && loc && abstract_origin)
13674 {
13675 /* We have a variable without a name, but with a location and an abstract
13676 origin. This may be a concrete instance of an abstract variable
13677 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13678 later. */
13679 struct dwarf2_cu *origin_cu = cu;
13680 struct die_info *origin_die
13681 = follow_die_ref (die, abstract_origin, &origin_cu);
13682 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13683 per_objfile->per_bfd->abstract_to_concrete
13684 [origin_die->sect_off].push_back (die->sect_off);
13685 }
13686 }
13687
13688 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13689 reading .debug_rnglists.
13690 Callback's type should be:
13691 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13692 Return true if the attributes are present and valid, otherwise,
13693 return false. */
13694
13695 template <typename Callback>
13696 static bool
13697 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13698 Callback &&callback)
13699 {
13700 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13701 struct objfile *objfile = dwarf2_per_objfile->objfile;
13702 bfd *obfd = objfile->obfd;
13703 /* Base address selection entry. */
13704 gdb::optional<CORE_ADDR> base;
13705 const gdb_byte *buffer;
13706 CORE_ADDR baseaddr;
13707 bool overflow = false;
13708
13709 base = cu->base_address;
13710
13711 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13712 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13713 {
13714 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13715 offset);
13716 return false;
13717 }
13718 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13719
13720 baseaddr = objfile->text_section_offset ();
13721
13722 while (1)
13723 {
13724 /* Initialize it due to a false compiler warning. */
13725 CORE_ADDR range_beginning = 0, range_end = 0;
13726 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13727 + dwarf2_per_objfile->per_bfd->rnglists.size);
13728 unsigned int bytes_read;
13729
13730 if (buffer == buf_end)
13731 {
13732 overflow = true;
13733 break;
13734 }
13735 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13736 switch (rlet)
13737 {
13738 case DW_RLE_end_of_list:
13739 break;
13740 case DW_RLE_base_address:
13741 if (buffer + cu->header.addr_size > buf_end)
13742 {
13743 overflow = true;
13744 break;
13745 }
13746 base = cu->header.read_address (obfd, buffer, &bytes_read);
13747 buffer += bytes_read;
13748 break;
13749 case DW_RLE_start_length:
13750 if (buffer + cu->header.addr_size > buf_end)
13751 {
13752 overflow = true;
13753 break;
13754 }
13755 range_beginning = cu->header.read_address (obfd, buffer,
13756 &bytes_read);
13757 buffer += bytes_read;
13758 range_end = (range_beginning
13759 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13760 buffer += bytes_read;
13761 if (buffer > buf_end)
13762 {
13763 overflow = true;
13764 break;
13765 }
13766 break;
13767 case DW_RLE_offset_pair:
13768 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13769 buffer += bytes_read;
13770 if (buffer > buf_end)
13771 {
13772 overflow = true;
13773 break;
13774 }
13775 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13776 buffer += bytes_read;
13777 if (buffer > buf_end)
13778 {
13779 overflow = true;
13780 break;
13781 }
13782 break;
13783 case DW_RLE_start_end:
13784 if (buffer + 2 * cu->header.addr_size > buf_end)
13785 {
13786 overflow = true;
13787 break;
13788 }
13789 range_beginning = cu->header.read_address (obfd, buffer,
13790 &bytes_read);
13791 buffer += bytes_read;
13792 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13793 buffer += bytes_read;
13794 break;
13795 default:
13796 complaint (_("Invalid .debug_rnglists data (no base address)"));
13797 return false;
13798 }
13799 if (rlet == DW_RLE_end_of_list || overflow)
13800 break;
13801 if (rlet == DW_RLE_base_address)
13802 continue;
13803
13804 if (!base.has_value ())
13805 {
13806 /* We have no valid base address for the ranges
13807 data. */
13808 complaint (_("Invalid .debug_rnglists data (no base address)"));
13809 return false;
13810 }
13811
13812 if (range_beginning > range_end)
13813 {
13814 /* Inverted range entries are invalid. */
13815 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13816 return false;
13817 }
13818
13819 /* Empty range entries have no effect. */
13820 if (range_beginning == range_end)
13821 continue;
13822
13823 range_beginning += *base;
13824 range_end += *base;
13825
13826 /* A not-uncommon case of bad debug info.
13827 Don't pollute the addrmap with bad data. */
13828 if (range_beginning + baseaddr == 0
13829 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13830 {
13831 complaint (_(".debug_rnglists entry has start address of zero"
13832 " [in module %s]"), objfile_name (objfile));
13833 continue;
13834 }
13835
13836 callback (range_beginning, range_end);
13837 }
13838
13839 if (overflow)
13840 {
13841 complaint (_("Offset %d is not terminated "
13842 "for DW_AT_ranges attribute"),
13843 offset);
13844 return false;
13845 }
13846
13847 return true;
13848 }
13849
13850 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13851 Callback's type should be:
13852 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13853 Return 1 if the attributes are present and valid, otherwise, return 0. */
13854
13855 template <typename Callback>
13856 static int
13857 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13858 Callback &&callback)
13859 {
13860 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13861 struct objfile *objfile = per_objfile->objfile;
13862 struct comp_unit_head *cu_header = &cu->header;
13863 bfd *obfd = objfile->obfd;
13864 unsigned int addr_size = cu_header->addr_size;
13865 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13866 /* Base address selection entry. */
13867 gdb::optional<CORE_ADDR> base;
13868 unsigned int dummy;
13869 const gdb_byte *buffer;
13870 CORE_ADDR baseaddr;
13871
13872 if (cu_header->version >= 5)
13873 return dwarf2_rnglists_process (offset, cu, callback);
13874
13875 base = cu->base_address;
13876
13877 per_objfile->per_bfd->ranges.read (objfile);
13878 if (offset >= per_objfile->per_bfd->ranges.size)
13879 {
13880 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13881 offset);
13882 return 0;
13883 }
13884 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13885
13886 baseaddr = objfile->text_section_offset ();
13887
13888 while (1)
13889 {
13890 CORE_ADDR range_beginning, range_end;
13891
13892 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13893 buffer += addr_size;
13894 range_end = cu->header.read_address (obfd, buffer, &dummy);
13895 buffer += addr_size;
13896 offset += 2 * addr_size;
13897
13898 /* An end of list marker is a pair of zero addresses. */
13899 if (range_beginning == 0 && range_end == 0)
13900 /* Found the end of list entry. */
13901 break;
13902
13903 /* Each base address selection entry is a pair of 2 values.
13904 The first is the largest possible address, the second is
13905 the base address. Check for a base address here. */
13906 if ((range_beginning & mask) == mask)
13907 {
13908 /* If we found the largest possible address, then we already
13909 have the base address in range_end. */
13910 base = range_end;
13911 continue;
13912 }
13913
13914 if (!base.has_value ())
13915 {
13916 /* We have no valid base address for the ranges
13917 data. */
13918 complaint (_("Invalid .debug_ranges data (no base address)"));
13919 return 0;
13920 }
13921
13922 if (range_beginning > range_end)
13923 {
13924 /* Inverted range entries are invalid. */
13925 complaint (_("Invalid .debug_ranges data (inverted range)"));
13926 return 0;
13927 }
13928
13929 /* Empty range entries have no effect. */
13930 if (range_beginning == range_end)
13931 continue;
13932
13933 range_beginning += *base;
13934 range_end += *base;
13935
13936 /* A not-uncommon case of bad debug info.
13937 Don't pollute the addrmap with bad data. */
13938 if (range_beginning + baseaddr == 0
13939 && !per_objfile->per_bfd->has_section_at_zero)
13940 {
13941 complaint (_(".debug_ranges entry has start address of zero"
13942 " [in module %s]"), objfile_name (objfile));
13943 continue;
13944 }
13945
13946 callback (range_beginning, range_end);
13947 }
13948
13949 return 1;
13950 }
13951
13952 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13953 Return 1 if the attributes are present and valid, otherwise, return 0.
13954 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13955
13956 static int
13957 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13958 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13959 dwarf2_psymtab *ranges_pst)
13960 {
13961 struct objfile *objfile = cu->per_objfile->objfile;
13962 struct gdbarch *gdbarch = objfile->arch ();
13963 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13964 int low_set = 0;
13965 CORE_ADDR low = 0;
13966 CORE_ADDR high = 0;
13967 int retval;
13968
13969 retval = dwarf2_ranges_process (offset, cu,
13970 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13971 {
13972 if (ranges_pst != NULL)
13973 {
13974 CORE_ADDR lowpc;
13975 CORE_ADDR highpc;
13976
13977 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13978 range_beginning + baseaddr)
13979 - baseaddr);
13980 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13981 range_end + baseaddr)
13982 - baseaddr);
13983 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13984 lowpc, highpc - 1, ranges_pst);
13985 }
13986
13987 /* FIXME: This is recording everything as a low-high
13988 segment of consecutive addresses. We should have a
13989 data structure for discontiguous block ranges
13990 instead. */
13991 if (! low_set)
13992 {
13993 low = range_beginning;
13994 high = range_end;
13995 low_set = 1;
13996 }
13997 else
13998 {
13999 if (range_beginning < low)
14000 low = range_beginning;
14001 if (range_end > high)
14002 high = range_end;
14003 }
14004 });
14005 if (!retval)
14006 return 0;
14007
14008 if (! low_set)
14009 /* If the first entry is an end-of-list marker, the range
14010 describes an empty scope, i.e. no instructions. */
14011 return 0;
14012
14013 if (low_return)
14014 *low_return = low;
14015 if (high_return)
14016 *high_return = high;
14017 return 1;
14018 }
14019
14020 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14021 definition for the return value. *LOWPC and *HIGHPC are set iff
14022 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14023
14024 static enum pc_bounds_kind
14025 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14026 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14027 dwarf2_psymtab *pst)
14028 {
14029 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14030 struct attribute *attr;
14031 struct attribute *attr_high;
14032 CORE_ADDR low = 0;
14033 CORE_ADDR high = 0;
14034 enum pc_bounds_kind ret;
14035
14036 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14037 if (attr_high)
14038 {
14039 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14040 if (attr != nullptr)
14041 {
14042 low = attr->value_as_address ();
14043 high = attr_high->value_as_address ();
14044 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14045 high += low;
14046 }
14047 else
14048 /* Found high w/o low attribute. */
14049 return PC_BOUNDS_INVALID;
14050
14051 /* Found consecutive range of addresses. */
14052 ret = PC_BOUNDS_HIGH_LOW;
14053 }
14054 else
14055 {
14056 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14057 if (attr != NULL)
14058 {
14059 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14060 We take advantage of the fact that DW_AT_ranges does not appear
14061 in DW_TAG_compile_unit of DWO files. */
14062 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14063 unsigned int ranges_offset = (DW_UNSND (attr)
14064 + (need_ranges_base
14065 ? cu->ranges_base
14066 : 0));
14067
14068 /* Value of the DW_AT_ranges attribute is the offset in the
14069 .debug_ranges section. */
14070 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14071 return PC_BOUNDS_INVALID;
14072 /* Found discontinuous range of addresses. */
14073 ret = PC_BOUNDS_RANGES;
14074 }
14075 else
14076 return PC_BOUNDS_NOT_PRESENT;
14077 }
14078
14079 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14080 if (high <= low)
14081 return PC_BOUNDS_INVALID;
14082
14083 /* When using the GNU linker, .gnu.linkonce. sections are used to
14084 eliminate duplicate copies of functions and vtables and such.
14085 The linker will arbitrarily choose one and discard the others.
14086 The AT_*_pc values for such functions refer to local labels in
14087 these sections. If the section from that file was discarded, the
14088 labels are not in the output, so the relocs get a value of 0.
14089 If this is a discarded function, mark the pc bounds as invalid,
14090 so that GDB will ignore it. */
14091 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14092 return PC_BOUNDS_INVALID;
14093
14094 *lowpc = low;
14095 if (highpc)
14096 *highpc = high;
14097 return ret;
14098 }
14099
14100 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14101 its low and high PC addresses. Do nothing if these addresses could not
14102 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14103 and HIGHPC to the high address if greater than HIGHPC. */
14104
14105 static void
14106 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14107 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14108 struct dwarf2_cu *cu)
14109 {
14110 CORE_ADDR low, high;
14111 struct die_info *child = die->child;
14112
14113 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14114 {
14115 *lowpc = std::min (*lowpc, low);
14116 *highpc = std::max (*highpc, high);
14117 }
14118
14119 /* If the language does not allow nested subprograms (either inside
14120 subprograms or lexical blocks), we're done. */
14121 if (cu->language != language_ada)
14122 return;
14123
14124 /* Check all the children of the given DIE. If it contains nested
14125 subprograms, then check their pc bounds. Likewise, we need to
14126 check lexical blocks as well, as they may also contain subprogram
14127 definitions. */
14128 while (child && child->tag)
14129 {
14130 if (child->tag == DW_TAG_subprogram
14131 || child->tag == DW_TAG_lexical_block)
14132 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14133 child = child->sibling;
14134 }
14135 }
14136
14137 /* Get the low and high pc's represented by the scope DIE, and store
14138 them in *LOWPC and *HIGHPC. If the correct values can't be
14139 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14140
14141 static void
14142 get_scope_pc_bounds (struct die_info *die,
14143 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14144 struct dwarf2_cu *cu)
14145 {
14146 CORE_ADDR best_low = (CORE_ADDR) -1;
14147 CORE_ADDR best_high = (CORE_ADDR) 0;
14148 CORE_ADDR current_low, current_high;
14149
14150 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14151 >= PC_BOUNDS_RANGES)
14152 {
14153 best_low = current_low;
14154 best_high = current_high;
14155 }
14156 else
14157 {
14158 struct die_info *child = die->child;
14159
14160 while (child && child->tag)
14161 {
14162 switch (child->tag) {
14163 case DW_TAG_subprogram:
14164 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14165 break;
14166 case DW_TAG_namespace:
14167 case DW_TAG_module:
14168 /* FIXME: carlton/2004-01-16: Should we do this for
14169 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14170 that current GCC's always emit the DIEs corresponding
14171 to definitions of methods of classes as children of a
14172 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14173 the DIEs giving the declarations, which could be
14174 anywhere). But I don't see any reason why the
14175 standards says that they have to be there. */
14176 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14177
14178 if (current_low != ((CORE_ADDR) -1))
14179 {
14180 best_low = std::min (best_low, current_low);
14181 best_high = std::max (best_high, current_high);
14182 }
14183 break;
14184 default:
14185 /* Ignore. */
14186 break;
14187 }
14188
14189 child = child->sibling;
14190 }
14191 }
14192
14193 *lowpc = best_low;
14194 *highpc = best_high;
14195 }
14196
14197 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14198 in DIE. */
14199
14200 static void
14201 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14202 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14203 {
14204 struct objfile *objfile = cu->per_objfile->objfile;
14205 struct gdbarch *gdbarch = objfile->arch ();
14206 struct attribute *attr;
14207 struct attribute *attr_high;
14208
14209 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14210 if (attr_high)
14211 {
14212 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14213 if (attr != nullptr)
14214 {
14215 CORE_ADDR low = attr->value_as_address ();
14216 CORE_ADDR high = attr_high->value_as_address ();
14217
14218 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14219 high += low;
14220
14221 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14222 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14223 cu->get_builder ()->record_block_range (block, low, high - 1);
14224 }
14225 }
14226
14227 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14228 if (attr != nullptr)
14229 {
14230 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14231 We take advantage of the fact that DW_AT_ranges does not appear
14232 in DW_TAG_compile_unit of DWO files. */
14233 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14234
14235 /* The value of the DW_AT_ranges attribute is the offset of the
14236 address range list in the .debug_ranges section. */
14237 unsigned long offset = (DW_UNSND (attr)
14238 + (need_ranges_base ? cu->ranges_base : 0));
14239
14240 std::vector<blockrange> blockvec;
14241 dwarf2_ranges_process (offset, cu,
14242 [&] (CORE_ADDR start, CORE_ADDR end)
14243 {
14244 start += baseaddr;
14245 end += baseaddr;
14246 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14247 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14248 cu->get_builder ()->record_block_range (block, start, end - 1);
14249 blockvec.emplace_back (start, end);
14250 });
14251
14252 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14253 }
14254 }
14255
14256 /* Check whether the producer field indicates either of GCC < 4.6, or the
14257 Intel C/C++ compiler, and cache the result in CU. */
14258
14259 static void
14260 check_producer (struct dwarf2_cu *cu)
14261 {
14262 int major, minor;
14263
14264 if (cu->producer == NULL)
14265 {
14266 /* For unknown compilers expect their behavior is DWARF version
14267 compliant.
14268
14269 GCC started to support .debug_types sections by -gdwarf-4 since
14270 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14271 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14272 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14273 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14274 }
14275 else if (producer_is_gcc (cu->producer, &major, &minor))
14276 {
14277 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14278 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14279 }
14280 else if (producer_is_icc (cu->producer, &major, &minor))
14281 {
14282 cu->producer_is_icc = true;
14283 cu->producer_is_icc_lt_14 = major < 14;
14284 }
14285 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14286 cu->producer_is_codewarrior = true;
14287 else
14288 {
14289 /* For other non-GCC compilers, expect their behavior is DWARF version
14290 compliant. */
14291 }
14292
14293 cu->checked_producer = true;
14294 }
14295
14296 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14297 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14298 during 4.6.0 experimental. */
14299
14300 static bool
14301 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14302 {
14303 if (!cu->checked_producer)
14304 check_producer (cu);
14305
14306 return cu->producer_is_gxx_lt_4_6;
14307 }
14308
14309
14310 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14311 with incorrect is_stmt attributes. */
14312
14313 static bool
14314 producer_is_codewarrior (struct dwarf2_cu *cu)
14315 {
14316 if (!cu->checked_producer)
14317 check_producer (cu);
14318
14319 return cu->producer_is_codewarrior;
14320 }
14321
14322 /* Return the default accessibility type if it is not overridden by
14323 DW_AT_accessibility. */
14324
14325 static enum dwarf_access_attribute
14326 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14327 {
14328 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14329 {
14330 /* The default DWARF 2 accessibility for members is public, the default
14331 accessibility for inheritance is private. */
14332
14333 if (die->tag != DW_TAG_inheritance)
14334 return DW_ACCESS_public;
14335 else
14336 return DW_ACCESS_private;
14337 }
14338 else
14339 {
14340 /* DWARF 3+ defines the default accessibility a different way. The same
14341 rules apply now for DW_TAG_inheritance as for the members and it only
14342 depends on the container kind. */
14343
14344 if (die->parent->tag == DW_TAG_class_type)
14345 return DW_ACCESS_private;
14346 else
14347 return DW_ACCESS_public;
14348 }
14349 }
14350
14351 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14352 offset. If the attribute was not found return 0, otherwise return
14353 1. If it was found but could not properly be handled, set *OFFSET
14354 to 0. */
14355
14356 static int
14357 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14358 LONGEST *offset)
14359 {
14360 struct attribute *attr;
14361
14362 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14363 if (attr != NULL)
14364 {
14365 *offset = 0;
14366
14367 /* Note that we do not check for a section offset first here.
14368 This is because DW_AT_data_member_location is new in DWARF 4,
14369 so if we see it, we can assume that a constant form is really
14370 a constant and not a section offset. */
14371 if (attr->form_is_constant ())
14372 *offset = attr->constant_value (0);
14373 else if (attr->form_is_section_offset ())
14374 dwarf2_complex_location_expr_complaint ();
14375 else if (attr->form_is_block ())
14376 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14377 else
14378 dwarf2_complex_location_expr_complaint ();
14379
14380 return 1;
14381 }
14382
14383 return 0;
14384 }
14385
14386 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14387
14388 static void
14389 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14390 struct field *field)
14391 {
14392 struct attribute *attr;
14393
14394 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14395 if (attr != NULL)
14396 {
14397 if (attr->form_is_constant ())
14398 {
14399 LONGEST offset = attr->constant_value (0);
14400 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14401 }
14402 else if (attr->form_is_section_offset ())
14403 dwarf2_complex_location_expr_complaint ();
14404 else if (attr->form_is_block ())
14405 {
14406 bool handled;
14407 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14408 if (handled)
14409 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14410 else
14411 {
14412 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14413 struct objfile *objfile = per_objfile->objfile;
14414 struct dwarf2_locexpr_baton *dlbaton
14415 = XOBNEW (&objfile->objfile_obstack,
14416 struct dwarf2_locexpr_baton);
14417 dlbaton->data = DW_BLOCK (attr)->data;
14418 dlbaton->size = DW_BLOCK (attr)->size;
14419 /* When using this baton, we want to compute the address
14420 of the field, not the value. This is why
14421 is_reference is set to false here. */
14422 dlbaton->is_reference = false;
14423 dlbaton->per_objfile = per_objfile;
14424 dlbaton->per_cu = cu->per_cu;
14425
14426 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14427 }
14428 }
14429 else
14430 dwarf2_complex_location_expr_complaint ();
14431 }
14432 }
14433
14434 /* Add an aggregate field to the field list. */
14435
14436 static void
14437 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14438 struct dwarf2_cu *cu)
14439 {
14440 struct objfile *objfile = cu->per_objfile->objfile;
14441 struct gdbarch *gdbarch = objfile->arch ();
14442 struct nextfield *new_field;
14443 struct attribute *attr;
14444 struct field *fp;
14445 const char *fieldname = "";
14446
14447 if (die->tag == DW_TAG_inheritance)
14448 {
14449 fip->baseclasses.emplace_back ();
14450 new_field = &fip->baseclasses.back ();
14451 }
14452 else
14453 {
14454 fip->fields.emplace_back ();
14455 new_field = &fip->fields.back ();
14456 }
14457
14458 new_field->offset = die->sect_off;
14459
14460 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14461 if (attr != nullptr)
14462 new_field->accessibility = DW_UNSND (attr);
14463 else
14464 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14465 if (new_field->accessibility != DW_ACCESS_public)
14466 fip->non_public_fields = 1;
14467
14468 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14469 if (attr != nullptr)
14470 new_field->virtuality = DW_UNSND (attr);
14471 else
14472 new_field->virtuality = DW_VIRTUALITY_none;
14473
14474 fp = &new_field->field;
14475
14476 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14477 {
14478 /* Data member other than a C++ static data member. */
14479
14480 /* Get type of field. */
14481 fp->type = die_type (die, cu);
14482
14483 SET_FIELD_BITPOS (*fp, 0);
14484
14485 /* Get bit size of field (zero if none). */
14486 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14487 if (attr != nullptr)
14488 {
14489 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14490 }
14491 else
14492 {
14493 FIELD_BITSIZE (*fp) = 0;
14494 }
14495
14496 /* Get bit offset of field. */
14497 handle_data_member_location (die, cu, fp);
14498 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14499 if (attr != nullptr)
14500 {
14501 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14502 {
14503 /* For big endian bits, the DW_AT_bit_offset gives the
14504 additional bit offset from the MSB of the containing
14505 anonymous object to the MSB of the field. We don't
14506 have to do anything special since we don't need to
14507 know the size of the anonymous object. */
14508 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14509 }
14510 else
14511 {
14512 /* For little endian bits, compute the bit offset to the
14513 MSB of the anonymous object, subtract off the number of
14514 bits from the MSB of the field to the MSB of the
14515 object, and then subtract off the number of bits of
14516 the field itself. The result is the bit offset of
14517 the LSB of the field. */
14518 int anonymous_size;
14519 int bit_offset = DW_UNSND (attr);
14520
14521 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14522 if (attr != nullptr)
14523 {
14524 /* The size of the anonymous object containing
14525 the bit field is explicit, so use the
14526 indicated size (in bytes). */
14527 anonymous_size = DW_UNSND (attr);
14528 }
14529 else
14530 {
14531 /* The size of the anonymous object containing
14532 the bit field must be inferred from the type
14533 attribute of the data member containing the
14534 bit field. */
14535 anonymous_size = TYPE_LENGTH (fp->type);
14536 }
14537 SET_FIELD_BITPOS (*fp,
14538 (FIELD_BITPOS (*fp)
14539 + anonymous_size * bits_per_byte
14540 - bit_offset - FIELD_BITSIZE (*fp)));
14541 }
14542 }
14543 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14544 if (attr != NULL)
14545 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14546 + attr->constant_value (0)));
14547
14548 /* Get name of field. */
14549 fieldname = dwarf2_name (die, cu);
14550 if (fieldname == NULL)
14551 fieldname = "";
14552
14553 /* The name is already allocated along with this objfile, so we don't
14554 need to duplicate it for the type. */
14555 fp->name = fieldname;
14556
14557 /* Change accessibility for artificial fields (e.g. virtual table
14558 pointer or virtual base class pointer) to private. */
14559 if (dwarf2_attr (die, DW_AT_artificial, cu))
14560 {
14561 FIELD_ARTIFICIAL (*fp) = 1;
14562 new_field->accessibility = DW_ACCESS_private;
14563 fip->non_public_fields = 1;
14564 }
14565 }
14566 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14567 {
14568 /* C++ static member. */
14569
14570 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14571 is a declaration, but all versions of G++ as of this writing
14572 (so through at least 3.2.1) incorrectly generate
14573 DW_TAG_variable tags. */
14574
14575 const char *physname;
14576
14577 /* Get name of field. */
14578 fieldname = dwarf2_name (die, cu);
14579 if (fieldname == NULL)
14580 return;
14581
14582 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14583 if (attr
14584 /* Only create a symbol if this is an external value.
14585 new_symbol checks this and puts the value in the global symbol
14586 table, which we want. If it is not external, new_symbol
14587 will try to put the value in cu->list_in_scope which is wrong. */
14588 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14589 {
14590 /* A static const member, not much different than an enum as far as
14591 we're concerned, except that we can support more types. */
14592 new_symbol (die, NULL, cu);
14593 }
14594
14595 /* Get physical name. */
14596 physname = dwarf2_physname (fieldname, die, cu);
14597
14598 /* The name is already allocated along with this objfile, so we don't
14599 need to duplicate it for the type. */
14600 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14601 FIELD_TYPE (*fp) = die_type (die, cu);
14602 FIELD_NAME (*fp) = fieldname;
14603 }
14604 else if (die->tag == DW_TAG_inheritance)
14605 {
14606 /* C++ base class field. */
14607 handle_data_member_location (die, cu, fp);
14608 FIELD_BITSIZE (*fp) = 0;
14609 FIELD_TYPE (*fp) = die_type (die, cu);
14610 FIELD_NAME (*fp) = fp->type->name ();
14611 }
14612 else
14613 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14614 }
14615
14616 /* Can the type given by DIE define another type? */
14617
14618 static bool
14619 type_can_define_types (const struct die_info *die)
14620 {
14621 switch (die->tag)
14622 {
14623 case DW_TAG_typedef:
14624 case DW_TAG_class_type:
14625 case DW_TAG_structure_type:
14626 case DW_TAG_union_type:
14627 case DW_TAG_enumeration_type:
14628 return true;
14629
14630 default:
14631 return false;
14632 }
14633 }
14634
14635 /* Add a type definition defined in the scope of the FIP's class. */
14636
14637 static void
14638 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14639 struct dwarf2_cu *cu)
14640 {
14641 struct decl_field fp;
14642 memset (&fp, 0, sizeof (fp));
14643
14644 gdb_assert (type_can_define_types (die));
14645
14646 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14647 fp.name = dwarf2_name (die, cu);
14648 fp.type = read_type_die (die, cu);
14649
14650 /* Save accessibility. */
14651 enum dwarf_access_attribute accessibility;
14652 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14653 if (attr != NULL)
14654 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14655 else
14656 accessibility = dwarf2_default_access_attribute (die, cu);
14657 switch (accessibility)
14658 {
14659 case DW_ACCESS_public:
14660 /* The assumed value if neither private nor protected. */
14661 break;
14662 case DW_ACCESS_private:
14663 fp.is_private = 1;
14664 break;
14665 case DW_ACCESS_protected:
14666 fp.is_protected = 1;
14667 break;
14668 default:
14669 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14670 }
14671
14672 if (die->tag == DW_TAG_typedef)
14673 fip->typedef_field_list.push_back (fp);
14674 else
14675 fip->nested_types_list.push_back (fp);
14676 }
14677
14678 /* A convenience typedef that's used when finding the discriminant
14679 field for a variant part. */
14680 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14681 offset_map_type;
14682
14683 /* Compute the discriminant range for a given variant. OBSTACK is
14684 where the results will be stored. VARIANT is the variant to
14685 process. IS_UNSIGNED indicates whether the discriminant is signed
14686 or unsigned. */
14687
14688 static const gdb::array_view<discriminant_range>
14689 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14690 bool is_unsigned)
14691 {
14692 std::vector<discriminant_range> ranges;
14693
14694 if (variant.default_branch)
14695 return {};
14696
14697 if (variant.discr_list_data == nullptr)
14698 {
14699 discriminant_range r
14700 = {variant.discriminant_value, variant.discriminant_value};
14701 ranges.push_back (r);
14702 }
14703 else
14704 {
14705 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14706 variant.discr_list_data->size);
14707 while (!data.empty ())
14708 {
14709 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14710 {
14711 complaint (_("invalid discriminant marker: %d"), data[0]);
14712 break;
14713 }
14714 bool is_range = data[0] == DW_DSC_range;
14715 data = data.slice (1);
14716
14717 ULONGEST low, high;
14718 unsigned int bytes_read;
14719
14720 if (data.empty ())
14721 {
14722 complaint (_("DW_AT_discr_list missing low value"));
14723 break;
14724 }
14725 if (is_unsigned)
14726 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14727 else
14728 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14729 &bytes_read);
14730 data = data.slice (bytes_read);
14731
14732 if (is_range)
14733 {
14734 if (data.empty ())
14735 {
14736 complaint (_("DW_AT_discr_list missing high value"));
14737 break;
14738 }
14739 if (is_unsigned)
14740 high = read_unsigned_leb128 (nullptr, data.data (),
14741 &bytes_read);
14742 else
14743 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14744 &bytes_read);
14745 data = data.slice (bytes_read);
14746 }
14747 else
14748 high = low;
14749
14750 ranges.push_back ({ low, high });
14751 }
14752 }
14753
14754 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14755 ranges.size ());
14756 std::copy (ranges.begin (), ranges.end (), result);
14757 return gdb::array_view<discriminant_range> (result, ranges.size ());
14758 }
14759
14760 static const gdb::array_view<variant_part> create_variant_parts
14761 (struct obstack *obstack,
14762 const offset_map_type &offset_map,
14763 struct field_info *fi,
14764 const std::vector<variant_part_builder> &variant_parts);
14765
14766 /* Fill in a "struct variant" for a given variant field. RESULT is
14767 the variant to fill in. OBSTACK is where any needed allocations
14768 will be done. OFFSET_MAP holds the mapping from section offsets to
14769 fields for the type. FI describes the fields of the type we're
14770 processing. FIELD is the variant field we're converting. */
14771
14772 static void
14773 create_one_variant (variant &result, struct obstack *obstack,
14774 const offset_map_type &offset_map,
14775 struct field_info *fi, const variant_field &field)
14776 {
14777 result.discriminants = convert_variant_range (obstack, field, false);
14778 result.first_field = field.first_field + fi->baseclasses.size ();
14779 result.last_field = field.last_field + fi->baseclasses.size ();
14780 result.parts = create_variant_parts (obstack, offset_map, fi,
14781 field.variant_parts);
14782 }
14783
14784 /* Fill in a "struct variant_part" for a given variant part. RESULT
14785 is the variant part to fill in. OBSTACK is where any needed
14786 allocations will be done. OFFSET_MAP holds the mapping from
14787 section offsets to fields for the type. FI describes the fields of
14788 the type we're processing. BUILDER is the variant part to be
14789 converted. */
14790
14791 static void
14792 create_one_variant_part (variant_part &result,
14793 struct obstack *obstack,
14794 const offset_map_type &offset_map,
14795 struct field_info *fi,
14796 const variant_part_builder &builder)
14797 {
14798 auto iter = offset_map.find (builder.discriminant_offset);
14799 if (iter == offset_map.end ())
14800 {
14801 result.discriminant_index = -1;
14802 /* Doesn't matter. */
14803 result.is_unsigned = false;
14804 }
14805 else
14806 {
14807 result.discriminant_index = iter->second;
14808 result.is_unsigned
14809 = TYPE_UNSIGNED (FIELD_TYPE
14810 (fi->fields[result.discriminant_index].field));
14811 }
14812
14813 size_t n = builder.variants.size ();
14814 variant *output = new (obstack) variant[n];
14815 for (size_t i = 0; i < n; ++i)
14816 create_one_variant (output[i], obstack, offset_map, fi,
14817 builder.variants[i]);
14818
14819 result.variants = gdb::array_view<variant> (output, n);
14820 }
14821
14822 /* Create a vector of variant parts that can be attached to a type.
14823 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14824 holds the mapping from section offsets to fields for the type. FI
14825 describes the fields of the type we're processing. VARIANT_PARTS
14826 is the vector to convert. */
14827
14828 static const gdb::array_view<variant_part>
14829 create_variant_parts (struct obstack *obstack,
14830 const offset_map_type &offset_map,
14831 struct field_info *fi,
14832 const std::vector<variant_part_builder> &variant_parts)
14833 {
14834 if (variant_parts.empty ())
14835 return {};
14836
14837 size_t n = variant_parts.size ();
14838 variant_part *result = new (obstack) variant_part[n];
14839 for (size_t i = 0; i < n; ++i)
14840 create_one_variant_part (result[i], obstack, offset_map, fi,
14841 variant_parts[i]);
14842
14843 return gdb::array_view<variant_part> (result, n);
14844 }
14845
14846 /* Compute the variant part vector for FIP, attaching it to TYPE when
14847 done. */
14848
14849 static void
14850 add_variant_property (struct field_info *fip, struct type *type,
14851 struct dwarf2_cu *cu)
14852 {
14853 /* Map section offsets of fields to their field index. Note the
14854 field index here does not take the number of baseclasses into
14855 account. */
14856 offset_map_type offset_map;
14857 for (int i = 0; i < fip->fields.size (); ++i)
14858 offset_map[fip->fields[i].offset] = i;
14859
14860 struct objfile *objfile = cu->per_objfile->objfile;
14861 gdb::array_view<variant_part> parts
14862 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14863 fip->variant_parts);
14864
14865 struct dynamic_prop prop;
14866 prop.kind = PROP_VARIANT_PARTS;
14867 prop.data.variant_parts
14868 = ((gdb::array_view<variant_part> *)
14869 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14870
14871 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14872 }
14873
14874 /* Create the vector of fields, and attach it to the type. */
14875
14876 static void
14877 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14878 struct dwarf2_cu *cu)
14879 {
14880 int nfields = fip->nfields ();
14881
14882 /* Record the field count, allocate space for the array of fields,
14883 and create blank accessibility bitfields if necessary. */
14884 type->set_num_fields (nfields);
14885 type->set_fields
14886 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14887
14888 if (fip->non_public_fields && cu->language != language_ada)
14889 {
14890 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14891
14892 TYPE_FIELD_PRIVATE_BITS (type) =
14893 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14894 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14895
14896 TYPE_FIELD_PROTECTED_BITS (type) =
14897 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14898 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14899
14900 TYPE_FIELD_IGNORE_BITS (type) =
14901 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14902 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14903 }
14904
14905 /* If the type has baseclasses, allocate and clear a bit vector for
14906 TYPE_FIELD_VIRTUAL_BITS. */
14907 if (!fip->baseclasses.empty () && cu->language != language_ada)
14908 {
14909 int num_bytes = B_BYTES (fip->baseclasses.size ());
14910 unsigned char *pointer;
14911
14912 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14913 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14914 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14915 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14916 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14917 }
14918
14919 if (!fip->variant_parts.empty ())
14920 add_variant_property (fip, type, cu);
14921
14922 /* Copy the saved-up fields into the field vector. */
14923 for (int i = 0; i < nfields; ++i)
14924 {
14925 struct nextfield &field
14926 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14927 : fip->fields[i - fip->baseclasses.size ()]);
14928
14929 type->field (i) = field.field;
14930 switch (field.accessibility)
14931 {
14932 case DW_ACCESS_private:
14933 if (cu->language != language_ada)
14934 SET_TYPE_FIELD_PRIVATE (type, i);
14935 break;
14936
14937 case DW_ACCESS_protected:
14938 if (cu->language != language_ada)
14939 SET_TYPE_FIELD_PROTECTED (type, i);
14940 break;
14941
14942 case DW_ACCESS_public:
14943 break;
14944
14945 default:
14946 /* Unknown accessibility. Complain and treat it as public. */
14947 {
14948 complaint (_("unsupported accessibility %d"),
14949 field.accessibility);
14950 }
14951 break;
14952 }
14953 if (i < fip->baseclasses.size ())
14954 {
14955 switch (field.virtuality)
14956 {
14957 case DW_VIRTUALITY_virtual:
14958 case DW_VIRTUALITY_pure_virtual:
14959 if (cu->language == language_ada)
14960 error (_("unexpected virtuality in component of Ada type"));
14961 SET_TYPE_FIELD_VIRTUAL (type, i);
14962 break;
14963 }
14964 }
14965 }
14966 }
14967
14968 /* Return true if this member function is a constructor, false
14969 otherwise. */
14970
14971 static int
14972 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14973 {
14974 const char *fieldname;
14975 const char *type_name;
14976 int len;
14977
14978 if (die->parent == NULL)
14979 return 0;
14980
14981 if (die->parent->tag != DW_TAG_structure_type
14982 && die->parent->tag != DW_TAG_union_type
14983 && die->parent->tag != DW_TAG_class_type)
14984 return 0;
14985
14986 fieldname = dwarf2_name (die, cu);
14987 type_name = dwarf2_name (die->parent, cu);
14988 if (fieldname == NULL || type_name == NULL)
14989 return 0;
14990
14991 len = strlen (fieldname);
14992 return (strncmp (fieldname, type_name, len) == 0
14993 && (type_name[len] == '\0' || type_name[len] == '<'));
14994 }
14995
14996 /* Check if the given VALUE is a recognized enum
14997 dwarf_defaulted_attribute constant according to DWARF5 spec,
14998 Table 7.24. */
14999
15000 static bool
15001 is_valid_DW_AT_defaulted (ULONGEST value)
15002 {
15003 switch (value)
15004 {
15005 case DW_DEFAULTED_no:
15006 case DW_DEFAULTED_in_class:
15007 case DW_DEFAULTED_out_of_class:
15008 return true;
15009 }
15010
15011 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15012 return false;
15013 }
15014
15015 /* Add a member function to the proper fieldlist. */
15016
15017 static void
15018 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15019 struct type *type, struct dwarf2_cu *cu)
15020 {
15021 struct objfile *objfile = cu->per_objfile->objfile;
15022 struct attribute *attr;
15023 int i;
15024 struct fnfieldlist *flp = nullptr;
15025 struct fn_field *fnp;
15026 const char *fieldname;
15027 struct type *this_type;
15028 enum dwarf_access_attribute accessibility;
15029
15030 if (cu->language == language_ada)
15031 error (_("unexpected member function in Ada type"));
15032
15033 /* Get name of member function. */
15034 fieldname = dwarf2_name (die, cu);
15035 if (fieldname == NULL)
15036 return;
15037
15038 /* Look up member function name in fieldlist. */
15039 for (i = 0; i < fip->fnfieldlists.size (); i++)
15040 {
15041 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15042 {
15043 flp = &fip->fnfieldlists[i];
15044 break;
15045 }
15046 }
15047
15048 /* Create a new fnfieldlist if necessary. */
15049 if (flp == nullptr)
15050 {
15051 fip->fnfieldlists.emplace_back ();
15052 flp = &fip->fnfieldlists.back ();
15053 flp->name = fieldname;
15054 i = fip->fnfieldlists.size () - 1;
15055 }
15056
15057 /* Create a new member function field and add it to the vector of
15058 fnfieldlists. */
15059 flp->fnfields.emplace_back ();
15060 fnp = &flp->fnfields.back ();
15061
15062 /* Delay processing of the physname until later. */
15063 if (cu->language == language_cplus)
15064 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15065 die, cu);
15066 else
15067 {
15068 const char *physname = dwarf2_physname (fieldname, die, cu);
15069 fnp->physname = physname ? physname : "";
15070 }
15071
15072 fnp->type = alloc_type (objfile);
15073 this_type = read_type_die (die, cu);
15074 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15075 {
15076 int nparams = this_type->num_fields ();
15077
15078 /* TYPE is the domain of this method, and THIS_TYPE is the type
15079 of the method itself (TYPE_CODE_METHOD). */
15080 smash_to_method_type (fnp->type, type,
15081 TYPE_TARGET_TYPE (this_type),
15082 this_type->fields (),
15083 this_type->num_fields (),
15084 TYPE_VARARGS (this_type));
15085
15086 /* Handle static member functions.
15087 Dwarf2 has no clean way to discern C++ static and non-static
15088 member functions. G++ helps GDB by marking the first
15089 parameter for non-static member functions (which is the this
15090 pointer) as artificial. We obtain this information from
15091 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15092 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15093 fnp->voffset = VOFFSET_STATIC;
15094 }
15095 else
15096 complaint (_("member function type missing for '%s'"),
15097 dwarf2_full_name (fieldname, die, cu));
15098
15099 /* Get fcontext from DW_AT_containing_type if present. */
15100 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15101 fnp->fcontext = die_containing_type (die, cu);
15102
15103 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15104 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15105
15106 /* Get accessibility. */
15107 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15108 if (attr != nullptr)
15109 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15110 else
15111 accessibility = dwarf2_default_access_attribute (die, cu);
15112 switch (accessibility)
15113 {
15114 case DW_ACCESS_private:
15115 fnp->is_private = 1;
15116 break;
15117 case DW_ACCESS_protected:
15118 fnp->is_protected = 1;
15119 break;
15120 }
15121
15122 /* Check for artificial methods. */
15123 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15124 if (attr && DW_UNSND (attr) != 0)
15125 fnp->is_artificial = 1;
15126
15127 /* Check for defaulted methods. */
15128 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15129 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15130 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15131
15132 /* Check for deleted methods. */
15133 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15134 if (attr != nullptr && DW_UNSND (attr) != 0)
15135 fnp->is_deleted = 1;
15136
15137 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15138
15139 /* Get index in virtual function table if it is a virtual member
15140 function. For older versions of GCC, this is an offset in the
15141 appropriate virtual table, as specified by DW_AT_containing_type.
15142 For everyone else, it is an expression to be evaluated relative
15143 to the object address. */
15144
15145 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15146 if (attr != nullptr)
15147 {
15148 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15149 {
15150 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15151 {
15152 /* Old-style GCC. */
15153 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15154 }
15155 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15156 || (DW_BLOCK (attr)->size > 1
15157 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15158 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15159 {
15160 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15161 if ((fnp->voffset % cu->header.addr_size) != 0)
15162 dwarf2_complex_location_expr_complaint ();
15163 else
15164 fnp->voffset /= cu->header.addr_size;
15165 fnp->voffset += 2;
15166 }
15167 else
15168 dwarf2_complex_location_expr_complaint ();
15169
15170 if (!fnp->fcontext)
15171 {
15172 /* If there is no `this' field and no DW_AT_containing_type,
15173 we cannot actually find a base class context for the
15174 vtable! */
15175 if (this_type->num_fields () == 0
15176 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15177 {
15178 complaint (_("cannot determine context for virtual member "
15179 "function \"%s\" (offset %s)"),
15180 fieldname, sect_offset_str (die->sect_off));
15181 }
15182 else
15183 {
15184 fnp->fcontext
15185 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15186 }
15187 }
15188 }
15189 else if (attr->form_is_section_offset ())
15190 {
15191 dwarf2_complex_location_expr_complaint ();
15192 }
15193 else
15194 {
15195 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15196 fieldname);
15197 }
15198 }
15199 else
15200 {
15201 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15202 if (attr && DW_UNSND (attr))
15203 {
15204 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15205 complaint (_("Member function \"%s\" (offset %s) is virtual "
15206 "but the vtable offset is not specified"),
15207 fieldname, sect_offset_str (die->sect_off));
15208 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15209 TYPE_CPLUS_DYNAMIC (type) = 1;
15210 }
15211 }
15212 }
15213
15214 /* Create the vector of member function fields, and attach it to the type. */
15215
15216 static void
15217 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15218 struct dwarf2_cu *cu)
15219 {
15220 if (cu->language == language_ada)
15221 error (_("unexpected member functions in Ada type"));
15222
15223 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15224 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15225 TYPE_ALLOC (type,
15226 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15227
15228 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15229 {
15230 struct fnfieldlist &nf = fip->fnfieldlists[i];
15231 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15232
15233 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15234 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15235 fn_flp->fn_fields = (struct fn_field *)
15236 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15237
15238 for (int k = 0; k < nf.fnfields.size (); ++k)
15239 fn_flp->fn_fields[k] = nf.fnfields[k];
15240 }
15241
15242 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15243 }
15244
15245 /* Returns non-zero if NAME is the name of a vtable member in CU's
15246 language, zero otherwise. */
15247 static int
15248 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15249 {
15250 static const char vptr[] = "_vptr";
15251
15252 /* Look for the C++ form of the vtable. */
15253 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15254 return 1;
15255
15256 return 0;
15257 }
15258
15259 /* GCC outputs unnamed structures that are really pointers to member
15260 functions, with the ABI-specified layout. If TYPE describes
15261 such a structure, smash it into a member function type.
15262
15263 GCC shouldn't do this; it should just output pointer to member DIEs.
15264 This is GCC PR debug/28767. */
15265
15266 static void
15267 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15268 {
15269 struct type *pfn_type, *self_type, *new_type;
15270
15271 /* Check for a structure with no name and two children. */
15272 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15273 return;
15274
15275 /* Check for __pfn and __delta members. */
15276 if (TYPE_FIELD_NAME (type, 0) == NULL
15277 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15278 || TYPE_FIELD_NAME (type, 1) == NULL
15279 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15280 return;
15281
15282 /* Find the type of the method. */
15283 pfn_type = TYPE_FIELD_TYPE (type, 0);
15284 if (pfn_type == NULL
15285 || pfn_type->code () != TYPE_CODE_PTR
15286 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15287 return;
15288
15289 /* Look for the "this" argument. */
15290 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15291 if (pfn_type->num_fields () == 0
15292 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15293 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15294 return;
15295
15296 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15297 new_type = alloc_type (objfile);
15298 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15299 pfn_type->fields (), pfn_type->num_fields (),
15300 TYPE_VARARGS (pfn_type));
15301 smash_to_methodptr_type (type, new_type);
15302 }
15303
15304 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15305 appropriate error checking and issuing complaints if there is a
15306 problem. */
15307
15308 static ULONGEST
15309 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15310 {
15311 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15312
15313 if (attr == nullptr)
15314 return 0;
15315
15316 if (!attr->form_is_constant ())
15317 {
15318 complaint (_("DW_AT_alignment must have constant form"
15319 " - DIE at %s [in module %s]"),
15320 sect_offset_str (die->sect_off),
15321 objfile_name (cu->per_objfile->objfile));
15322 return 0;
15323 }
15324
15325 ULONGEST align;
15326 if (attr->form == DW_FORM_sdata)
15327 {
15328 LONGEST val = DW_SND (attr);
15329 if (val < 0)
15330 {
15331 complaint (_("DW_AT_alignment value must not be negative"
15332 " - DIE at %s [in module %s]"),
15333 sect_offset_str (die->sect_off),
15334 objfile_name (cu->per_objfile->objfile));
15335 return 0;
15336 }
15337 align = val;
15338 }
15339 else
15340 align = DW_UNSND (attr);
15341
15342 if (align == 0)
15343 {
15344 complaint (_("DW_AT_alignment value must not be zero"
15345 " - DIE at %s [in module %s]"),
15346 sect_offset_str (die->sect_off),
15347 objfile_name (cu->per_objfile->objfile));
15348 return 0;
15349 }
15350 if ((align & (align - 1)) != 0)
15351 {
15352 complaint (_("DW_AT_alignment value must be a power of 2"
15353 " - DIE at %s [in module %s]"),
15354 sect_offset_str (die->sect_off),
15355 objfile_name (cu->per_objfile->objfile));
15356 return 0;
15357 }
15358
15359 return align;
15360 }
15361
15362 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15363 the alignment for TYPE. */
15364
15365 static void
15366 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15367 struct type *type)
15368 {
15369 if (!set_type_align (type, get_alignment (cu, die)))
15370 complaint (_("DW_AT_alignment value too large"
15371 " - DIE at %s [in module %s]"),
15372 sect_offset_str (die->sect_off),
15373 objfile_name (cu->per_objfile->objfile));
15374 }
15375
15376 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15377 constant for a type, according to DWARF5 spec, Table 5.5. */
15378
15379 static bool
15380 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15381 {
15382 switch (value)
15383 {
15384 case DW_CC_normal:
15385 case DW_CC_pass_by_reference:
15386 case DW_CC_pass_by_value:
15387 return true;
15388
15389 default:
15390 complaint (_("unrecognized DW_AT_calling_convention value "
15391 "(%s) for a type"), pulongest (value));
15392 return false;
15393 }
15394 }
15395
15396 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15397 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15398 also according to GNU-specific values (see include/dwarf2.h). */
15399
15400 static bool
15401 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15402 {
15403 switch (value)
15404 {
15405 case DW_CC_normal:
15406 case DW_CC_program:
15407 case DW_CC_nocall:
15408 return true;
15409
15410 case DW_CC_GNU_renesas_sh:
15411 case DW_CC_GNU_borland_fastcall_i386:
15412 case DW_CC_GDB_IBM_OpenCL:
15413 return true;
15414
15415 default:
15416 complaint (_("unrecognized DW_AT_calling_convention value "
15417 "(%s) for a subroutine"), pulongest (value));
15418 return false;
15419 }
15420 }
15421
15422 /* Called when we find the DIE that starts a structure or union scope
15423 (definition) to create a type for the structure or union. Fill in
15424 the type's name and general properties; the members will not be
15425 processed until process_structure_scope. A symbol table entry for
15426 the type will also not be done until process_structure_scope (assuming
15427 the type has a name).
15428
15429 NOTE: we need to call these functions regardless of whether or not the
15430 DIE has a DW_AT_name attribute, since it might be an anonymous
15431 structure or union. This gets the type entered into our set of
15432 user defined types. */
15433
15434 static struct type *
15435 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15436 {
15437 struct objfile *objfile = cu->per_objfile->objfile;
15438 struct type *type;
15439 struct attribute *attr;
15440 const char *name;
15441
15442 /* If the definition of this type lives in .debug_types, read that type.
15443 Don't follow DW_AT_specification though, that will take us back up
15444 the chain and we want to go down. */
15445 attr = die->attr (DW_AT_signature);
15446 if (attr != nullptr)
15447 {
15448 type = get_DW_AT_signature_type (die, attr, cu);
15449
15450 /* The type's CU may not be the same as CU.
15451 Ensure TYPE is recorded with CU in die_type_hash. */
15452 return set_die_type (die, type, cu);
15453 }
15454
15455 type = alloc_type (objfile);
15456 INIT_CPLUS_SPECIFIC (type);
15457
15458 name = dwarf2_name (die, cu);
15459 if (name != NULL)
15460 {
15461 if (cu->language == language_cplus
15462 || cu->language == language_d
15463 || cu->language == language_rust)
15464 {
15465 const char *full_name = dwarf2_full_name (name, die, cu);
15466
15467 /* dwarf2_full_name might have already finished building the DIE's
15468 type. If so, there is no need to continue. */
15469 if (get_die_type (die, cu) != NULL)
15470 return get_die_type (die, cu);
15471
15472 type->set_name (full_name);
15473 }
15474 else
15475 {
15476 /* The name is already allocated along with this objfile, so
15477 we don't need to duplicate it for the type. */
15478 type->set_name (name);
15479 }
15480 }
15481
15482 if (die->tag == DW_TAG_structure_type)
15483 {
15484 type->set_code (TYPE_CODE_STRUCT);
15485 }
15486 else if (die->tag == DW_TAG_union_type)
15487 {
15488 type->set_code (TYPE_CODE_UNION);
15489 }
15490 else
15491 {
15492 type->set_code (TYPE_CODE_STRUCT);
15493 }
15494
15495 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15496 TYPE_DECLARED_CLASS (type) = 1;
15497
15498 /* Store the calling convention in the type if it's available in
15499 the die. Otherwise the calling convention remains set to
15500 the default value DW_CC_normal. */
15501 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15502 if (attr != nullptr
15503 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15504 {
15505 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15506 TYPE_CPLUS_CALLING_CONVENTION (type)
15507 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15508 }
15509
15510 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15511 if (attr != nullptr)
15512 {
15513 if (attr->form_is_constant ())
15514 TYPE_LENGTH (type) = DW_UNSND (attr);
15515 else
15516 {
15517 struct dynamic_prop prop;
15518 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15519 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15520 TYPE_LENGTH (type) = 0;
15521 }
15522 }
15523 else
15524 {
15525 TYPE_LENGTH (type) = 0;
15526 }
15527
15528 maybe_set_alignment (cu, die, type);
15529
15530 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15531 {
15532 /* ICC<14 does not output the required DW_AT_declaration on
15533 incomplete types, but gives them a size of zero. */
15534 TYPE_STUB (type) = 1;
15535 }
15536 else
15537 TYPE_STUB_SUPPORTED (type) = 1;
15538
15539 if (die_is_declaration (die, cu))
15540 TYPE_STUB (type) = 1;
15541 else if (attr == NULL && die->child == NULL
15542 && producer_is_realview (cu->producer))
15543 /* RealView does not output the required DW_AT_declaration
15544 on incomplete types. */
15545 TYPE_STUB (type) = 1;
15546
15547 /* We need to add the type field to the die immediately so we don't
15548 infinitely recurse when dealing with pointers to the structure
15549 type within the structure itself. */
15550 set_die_type (die, type, cu);
15551
15552 /* set_die_type should be already done. */
15553 set_descriptive_type (type, die, cu);
15554
15555 return type;
15556 }
15557
15558 static void handle_struct_member_die
15559 (struct die_info *child_die,
15560 struct type *type,
15561 struct field_info *fi,
15562 std::vector<struct symbol *> *template_args,
15563 struct dwarf2_cu *cu);
15564
15565 /* A helper for handle_struct_member_die that handles
15566 DW_TAG_variant_part. */
15567
15568 static void
15569 handle_variant_part (struct die_info *die, struct type *type,
15570 struct field_info *fi,
15571 std::vector<struct symbol *> *template_args,
15572 struct dwarf2_cu *cu)
15573 {
15574 variant_part_builder *new_part;
15575 if (fi->current_variant_part == nullptr)
15576 {
15577 fi->variant_parts.emplace_back ();
15578 new_part = &fi->variant_parts.back ();
15579 }
15580 else if (!fi->current_variant_part->processing_variant)
15581 {
15582 complaint (_("nested DW_TAG_variant_part seen "
15583 "- DIE at %s [in module %s]"),
15584 sect_offset_str (die->sect_off),
15585 objfile_name (cu->per_objfile->objfile));
15586 return;
15587 }
15588 else
15589 {
15590 variant_field &current = fi->current_variant_part->variants.back ();
15591 current.variant_parts.emplace_back ();
15592 new_part = &current.variant_parts.back ();
15593 }
15594
15595 /* When we recurse, we want callees to add to this new variant
15596 part. */
15597 scoped_restore save_current_variant_part
15598 = make_scoped_restore (&fi->current_variant_part, new_part);
15599
15600 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15601 if (discr == NULL)
15602 {
15603 /* It's a univariant form, an extension we support. */
15604 }
15605 else if (discr->form_is_ref ())
15606 {
15607 struct dwarf2_cu *target_cu = cu;
15608 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15609
15610 new_part->discriminant_offset = target_die->sect_off;
15611 }
15612 else
15613 {
15614 complaint (_("DW_AT_discr does not have DIE reference form"
15615 " - DIE at %s [in module %s]"),
15616 sect_offset_str (die->sect_off),
15617 objfile_name (cu->per_objfile->objfile));
15618 }
15619
15620 for (die_info *child_die = die->child;
15621 child_die != NULL;
15622 child_die = child_die->sibling)
15623 handle_struct_member_die (child_die, type, fi, template_args, cu);
15624 }
15625
15626 /* A helper for handle_struct_member_die that handles
15627 DW_TAG_variant. */
15628
15629 static void
15630 handle_variant (struct die_info *die, struct type *type,
15631 struct field_info *fi,
15632 std::vector<struct symbol *> *template_args,
15633 struct dwarf2_cu *cu)
15634 {
15635 if (fi->current_variant_part == nullptr)
15636 {
15637 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15638 "- DIE at %s [in module %s]"),
15639 sect_offset_str (die->sect_off),
15640 objfile_name (cu->per_objfile->objfile));
15641 return;
15642 }
15643 if (fi->current_variant_part->processing_variant)
15644 {
15645 complaint (_("nested DW_TAG_variant seen "
15646 "- DIE at %s [in module %s]"),
15647 sect_offset_str (die->sect_off),
15648 objfile_name (cu->per_objfile->objfile));
15649 return;
15650 }
15651
15652 scoped_restore save_processing_variant
15653 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15654 true);
15655
15656 fi->current_variant_part->variants.emplace_back ();
15657 variant_field &variant = fi->current_variant_part->variants.back ();
15658 variant.first_field = fi->fields.size ();
15659
15660 /* In a variant we want to get the discriminant and also add a
15661 field for our sole member child. */
15662 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15663 if (discr == nullptr)
15664 {
15665 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15666 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15667 variant.default_branch = true;
15668 else
15669 variant.discr_list_data = DW_BLOCK (discr);
15670 }
15671 else
15672 variant.discriminant_value = DW_UNSND (discr);
15673
15674 for (die_info *variant_child = die->child;
15675 variant_child != NULL;
15676 variant_child = variant_child->sibling)
15677 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15678
15679 variant.last_field = fi->fields.size ();
15680 }
15681
15682 /* A helper for process_structure_scope that handles a single member
15683 DIE. */
15684
15685 static void
15686 handle_struct_member_die (struct die_info *child_die, struct type *type,
15687 struct field_info *fi,
15688 std::vector<struct symbol *> *template_args,
15689 struct dwarf2_cu *cu)
15690 {
15691 if (child_die->tag == DW_TAG_member
15692 || child_die->tag == DW_TAG_variable)
15693 {
15694 /* NOTE: carlton/2002-11-05: A C++ static data member
15695 should be a DW_TAG_member that is a declaration, but
15696 all versions of G++ as of this writing (so through at
15697 least 3.2.1) incorrectly generate DW_TAG_variable
15698 tags for them instead. */
15699 dwarf2_add_field (fi, child_die, cu);
15700 }
15701 else if (child_die->tag == DW_TAG_subprogram)
15702 {
15703 /* Rust doesn't have member functions in the C++ sense.
15704 However, it does emit ordinary functions as children
15705 of a struct DIE. */
15706 if (cu->language == language_rust)
15707 read_func_scope (child_die, cu);
15708 else
15709 {
15710 /* C++ member function. */
15711 dwarf2_add_member_fn (fi, child_die, type, cu);
15712 }
15713 }
15714 else if (child_die->tag == DW_TAG_inheritance)
15715 {
15716 /* C++ base class field. */
15717 dwarf2_add_field (fi, child_die, cu);
15718 }
15719 else if (type_can_define_types (child_die))
15720 dwarf2_add_type_defn (fi, child_die, cu);
15721 else if (child_die->tag == DW_TAG_template_type_param
15722 || child_die->tag == DW_TAG_template_value_param)
15723 {
15724 struct symbol *arg = new_symbol (child_die, NULL, cu);
15725
15726 if (arg != NULL)
15727 template_args->push_back (arg);
15728 }
15729 else if (child_die->tag == DW_TAG_variant_part)
15730 handle_variant_part (child_die, type, fi, template_args, cu);
15731 else if (child_die->tag == DW_TAG_variant)
15732 handle_variant (child_die, type, fi, template_args, cu);
15733 }
15734
15735 /* Finish creating a structure or union type, including filling in
15736 its members and creating a symbol for it. */
15737
15738 static void
15739 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15740 {
15741 struct objfile *objfile = cu->per_objfile->objfile;
15742 struct die_info *child_die;
15743 struct type *type;
15744
15745 type = get_die_type (die, cu);
15746 if (type == NULL)
15747 type = read_structure_type (die, cu);
15748
15749 bool has_template_parameters = false;
15750 if (die->child != NULL && ! die_is_declaration (die, cu))
15751 {
15752 struct field_info fi;
15753 std::vector<struct symbol *> template_args;
15754
15755 child_die = die->child;
15756
15757 while (child_die && child_die->tag)
15758 {
15759 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15760 child_die = child_die->sibling;
15761 }
15762
15763 /* Attach template arguments to type. */
15764 if (!template_args.empty ())
15765 {
15766 has_template_parameters = true;
15767 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15768 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15769 TYPE_TEMPLATE_ARGUMENTS (type)
15770 = XOBNEWVEC (&objfile->objfile_obstack,
15771 struct symbol *,
15772 TYPE_N_TEMPLATE_ARGUMENTS (type));
15773 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15774 template_args.data (),
15775 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15776 * sizeof (struct symbol *)));
15777 }
15778
15779 /* Attach fields and member functions to the type. */
15780 if (fi.nfields () > 0)
15781 dwarf2_attach_fields_to_type (&fi, type, cu);
15782 if (!fi.fnfieldlists.empty ())
15783 {
15784 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15785
15786 /* Get the type which refers to the base class (possibly this
15787 class itself) which contains the vtable pointer for the current
15788 class from the DW_AT_containing_type attribute. This use of
15789 DW_AT_containing_type is a GNU extension. */
15790
15791 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15792 {
15793 struct type *t = die_containing_type (die, cu);
15794
15795 set_type_vptr_basetype (type, t);
15796 if (type == t)
15797 {
15798 int i;
15799
15800 /* Our own class provides vtbl ptr. */
15801 for (i = t->num_fields () - 1;
15802 i >= TYPE_N_BASECLASSES (t);
15803 --i)
15804 {
15805 const char *fieldname = TYPE_FIELD_NAME (t, i);
15806
15807 if (is_vtable_name (fieldname, cu))
15808 {
15809 set_type_vptr_fieldno (type, i);
15810 break;
15811 }
15812 }
15813
15814 /* Complain if virtual function table field not found. */
15815 if (i < TYPE_N_BASECLASSES (t))
15816 complaint (_("virtual function table pointer "
15817 "not found when defining class '%s'"),
15818 type->name () ? type->name () : "");
15819 }
15820 else
15821 {
15822 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15823 }
15824 }
15825 else if (cu->producer
15826 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15827 {
15828 /* The IBM XLC compiler does not provide direct indication
15829 of the containing type, but the vtable pointer is
15830 always named __vfp. */
15831
15832 int i;
15833
15834 for (i = type->num_fields () - 1;
15835 i >= TYPE_N_BASECLASSES (type);
15836 --i)
15837 {
15838 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15839 {
15840 set_type_vptr_fieldno (type, i);
15841 set_type_vptr_basetype (type, type);
15842 break;
15843 }
15844 }
15845 }
15846 }
15847
15848 /* Copy fi.typedef_field_list linked list elements content into the
15849 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15850 if (!fi.typedef_field_list.empty ())
15851 {
15852 int count = fi.typedef_field_list.size ();
15853
15854 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15855 TYPE_TYPEDEF_FIELD_ARRAY (type)
15856 = ((struct decl_field *)
15857 TYPE_ALLOC (type,
15858 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15859 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15860
15861 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15862 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15863 }
15864
15865 /* Copy fi.nested_types_list linked list elements content into the
15866 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15867 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15868 {
15869 int count = fi.nested_types_list.size ();
15870
15871 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15872 TYPE_NESTED_TYPES_ARRAY (type)
15873 = ((struct decl_field *)
15874 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15875 TYPE_NESTED_TYPES_COUNT (type) = count;
15876
15877 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15878 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15879 }
15880 }
15881
15882 quirk_gcc_member_function_pointer (type, objfile);
15883 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15884 cu->rust_unions.push_back (type);
15885
15886 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15887 snapshots) has been known to create a die giving a declaration
15888 for a class that has, as a child, a die giving a definition for a
15889 nested class. So we have to process our children even if the
15890 current die is a declaration. Normally, of course, a declaration
15891 won't have any children at all. */
15892
15893 child_die = die->child;
15894
15895 while (child_die != NULL && child_die->tag)
15896 {
15897 if (child_die->tag == DW_TAG_member
15898 || child_die->tag == DW_TAG_variable
15899 || child_die->tag == DW_TAG_inheritance
15900 || child_die->tag == DW_TAG_template_value_param
15901 || child_die->tag == DW_TAG_template_type_param)
15902 {
15903 /* Do nothing. */
15904 }
15905 else
15906 process_die (child_die, cu);
15907
15908 child_die = child_die->sibling;
15909 }
15910
15911 /* Do not consider external references. According to the DWARF standard,
15912 these DIEs are identified by the fact that they have no byte_size
15913 attribute, and a declaration attribute. */
15914 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15915 || !die_is_declaration (die, cu)
15916 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15917 {
15918 struct symbol *sym = new_symbol (die, type, cu);
15919
15920 if (has_template_parameters)
15921 {
15922 struct symtab *symtab;
15923 if (sym != nullptr)
15924 symtab = symbol_symtab (sym);
15925 else if (cu->line_header != nullptr)
15926 {
15927 /* Any related symtab will do. */
15928 symtab
15929 = cu->line_header->file_names ()[0].symtab;
15930 }
15931 else
15932 {
15933 symtab = nullptr;
15934 complaint (_("could not find suitable "
15935 "symtab for template parameter"
15936 " - DIE at %s [in module %s]"),
15937 sect_offset_str (die->sect_off),
15938 objfile_name (objfile));
15939 }
15940
15941 if (symtab != nullptr)
15942 {
15943 /* Make sure that the symtab is set on the new symbols.
15944 Even though they don't appear in this symtab directly,
15945 other parts of gdb assume that symbols do, and this is
15946 reasonably true. */
15947 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15948 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15949 }
15950 }
15951 }
15952 }
15953
15954 /* Assuming DIE is an enumeration type, and TYPE is its associated
15955 type, update TYPE using some information only available in DIE's
15956 children. In particular, the fields are computed. */
15957
15958 static void
15959 update_enumeration_type_from_children (struct die_info *die,
15960 struct type *type,
15961 struct dwarf2_cu *cu)
15962 {
15963 struct die_info *child_die;
15964 int unsigned_enum = 1;
15965 int flag_enum = 1;
15966
15967 auto_obstack obstack;
15968 std::vector<struct field> fields;
15969
15970 for (child_die = die->child;
15971 child_die != NULL && child_die->tag;
15972 child_die = child_die->sibling)
15973 {
15974 struct attribute *attr;
15975 LONGEST value;
15976 const gdb_byte *bytes;
15977 struct dwarf2_locexpr_baton *baton;
15978 const char *name;
15979
15980 if (child_die->tag != DW_TAG_enumerator)
15981 continue;
15982
15983 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15984 if (attr == NULL)
15985 continue;
15986
15987 name = dwarf2_name (child_die, cu);
15988 if (name == NULL)
15989 name = "<anonymous enumerator>";
15990
15991 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15992 &value, &bytes, &baton);
15993 if (value < 0)
15994 {
15995 unsigned_enum = 0;
15996 flag_enum = 0;
15997 }
15998 else
15999 {
16000 if (count_one_bits_ll (value) >= 2)
16001 flag_enum = 0;
16002 }
16003
16004 fields.emplace_back ();
16005 struct field &field = fields.back ();
16006 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16007 SET_FIELD_ENUMVAL (field, value);
16008 }
16009
16010 if (!fields.empty ())
16011 {
16012 type->set_num_fields (fields.size ());
16013 type->set_fields
16014 ((struct field *)
16015 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16016 memcpy (type->fields (), fields.data (),
16017 sizeof (struct field) * fields.size ());
16018 }
16019
16020 if (unsigned_enum)
16021 TYPE_UNSIGNED (type) = 1;
16022 if (flag_enum)
16023 TYPE_FLAG_ENUM (type) = 1;
16024 }
16025
16026 /* Given a DW_AT_enumeration_type die, set its type. We do not
16027 complete the type's fields yet, or create any symbols. */
16028
16029 static struct type *
16030 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16031 {
16032 struct objfile *objfile = cu->per_objfile->objfile;
16033 struct type *type;
16034 struct attribute *attr;
16035 const char *name;
16036
16037 /* If the definition of this type lives in .debug_types, read that type.
16038 Don't follow DW_AT_specification though, that will take us back up
16039 the chain and we want to go down. */
16040 attr = die->attr (DW_AT_signature);
16041 if (attr != nullptr)
16042 {
16043 type = get_DW_AT_signature_type (die, attr, cu);
16044
16045 /* The type's CU may not be the same as CU.
16046 Ensure TYPE is recorded with CU in die_type_hash. */
16047 return set_die_type (die, type, cu);
16048 }
16049
16050 type = alloc_type (objfile);
16051
16052 type->set_code (TYPE_CODE_ENUM);
16053 name = dwarf2_full_name (NULL, die, cu);
16054 if (name != NULL)
16055 type->set_name (name);
16056
16057 attr = dwarf2_attr (die, DW_AT_type, cu);
16058 if (attr != NULL)
16059 {
16060 struct type *underlying_type = die_type (die, cu);
16061
16062 TYPE_TARGET_TYPE (type) = underlying_type;
16063 }
16064
16065 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16066 if (attr != nullptr)
16067 {
16068 TYPE_LENGTH (type) = DW_UNSND (attr);
16069 }
16070 else
16071 {
16072 TYPE_LENGTH (type) = 0;
16073 }
16074
16075 maybe_set_alignment (cu, die, type);
16076
16077 /* The enumeration DIE can be incomplete. In Ada, any type can be
16078 declared as private in the package spec, and then defined only
16079 inside the package body. Such types are known as Taft Amendment
16080 Types. When another package uses such a type, an incomplete DIE
16081 may be generated by the compiler. */
16082 if (die_is_declaration (die, cu))
16083 TYPE_STUB (type) = 1;
16084
16085 /* If this type has an underlying type that is not a stub, then we
16086 may use its attributes. We always use the "unsigned" attribute
16087 in this situation, because ordinarily we guess whether the type
16088 is unsigned -- but the guess can be wrong and the underlying type
16089 can tell us the reality. However, we defer to a local size
16090 attribute if one exists, because this lets the compiler override
16091 the underlying type if needed. */
16092 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16093 {
16094 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16095 underlying_type = check_typedef (underlying_type);
16096 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16097 if (TYPE_LENGTH (type) == 0)
16098 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16099 if (TYPE_RAW_ALIGN (type) == 0
16100 && TYPE_RAW_ALIGN (underlying_type) != 0)
16101 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16102 }
16103
16104 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16105
16106 set_die_type (die, type, cu);
16107
16108 /* Finish the creation of this type by using the enum's children.
16109 Note that, as usual, this must come after set_die_type to avoid
16110 infinite recursion when trying to compute the names of the
16111 enumerators. */
16112 update_enumeration_type_from_children (die, type, cu);
16113
16114 return type;
16115 }
16116
16117 /* Given a pointer to a die which begins an enumeration, process all
16118 the dies that define the members of the enumeration, and create the
16119 symbol for the enumeration type.
16120
16121 NOTE: We reverse the order of the element list. */
16122
16123 static void
16124 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16125 {
16126 struct type *this_type;
16127
16128 this_type = get_die_type (die, cu);
16129 if (this_type == NULL)
16130 this_type = read_enumeration_type (die, cu);
16131
16132 if (die->child != NULL)
16133 {
16134 struct die_info *child_die;
16135 const char *name;
16136
16137 child_die = die->child;
16138 while (child_die && child_die->tag)
16139 {
16140 if (child_die->tag != DW_TAG_enumerator)
16141 {
16142 process_die (child_die, cu);
16143 }
16144 else
16145 {
16146 name = dwarf2_name (child_die, cu);
16147 if (name)
16148 new_symbol (child_die, this_type, cu);
16149 }
16150
16151 child_die = child_die->sibling;
16152 }
16153 }
16154
16155 /* If we are reading an enum from a .debug_types unit, and the enum
16156 is a declaration, and the enum is not the signatured type in the
16157 unit, then we do not want to add a symbol for it. Adding a
16158 symbol would in some cases obscure the true definition of the
16159 enum, giving users an incomplete type when the definition is
16160 actually available. Note that we do not want to do this for all
16161 enums which are just declarations, because C++0x allows forward
16162 enum declarations. */
16163 if (cu->per_cu->is_debug_types
16164 && die_is_declaration (die, cu))
16165 {
16166 struct signatured_type *sig_type;
16167
16168 sig_type = (struct signatured_type *) cu->per_cu;
16169 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16170 if (sig_type->type_offset_in_section != die->sect_off)
16171 return;
16172 }
16173
16174 new_symbol (die, this_type, cu);
16175 }
16176
16177 /* Extract all information from a DW_TAG_array_type DIE and put it in
16178 the DIE's type field. For now, this only handles one dimensional
16179 arrays. */
16180
16181 static struct type *
16182 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16183 {
16184 struct objfile *objfile = cu->per_objfile->objfile;
16185 struct die_info *child_die;
16186 struct type *type;
16187 struct type *element_type, *range_type, *index_type;
16188 struct attribute *attr;
16189 const char *name;
16190 struct dynamic_prop *byte_stride_prop = NULL;
16191 unsigned int bit_stride = 0;
16192
16193 element_type = die_type (die, cu);
16194
16195 /* The die_type call above may have already set the type for this DIE. */
16196 type = get_die_type (die, cu);
16197 if (type)
16198 return type;
16199
16200 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16201 if (attr != NULL)
16202 {
16203 int stride_ok;
16204 struct type *prop_type = cu->addr_sized_int_type (false);
16205
16206 byte_stride_prop
16207 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16208 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16209 prop_type);
16210 if (!stride_ok)
16211 {
16212 complaint (_("unable to read array DW_AT_byte_stride "
16213 " - DIE at %s [in module %s]"),
16214 sect_offset_str (die->sect_off),
16215 objfile_name (cu->per_objfile->objfile));
16216 /* Ignore this attribute. We will likely not be able to print
16217 arrays of this type correctly, but there is little we can do
16218 to help if we cannot read the attribute's value. */
16219 byte_stride_prop = NULL;
16220 }
16221 }
16222
16223 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16224 if (attr != NULL)
16225 bit_stride = DW_UNSND (attr);
16226
16227 /* Irix 6.2 native cc creates array types without children for
16228 arrays with unspecified length. */
16229 if (die->child == NULL)
16230 {
16231 index_type = objfile_type (objfile)->builtin_int;
16232 range_type = create_static_range_type (NULL, index_type, 0, -1);
16233 type = create_array_type_with_stride (NULL, element_type, range_type,
16234 byte_stride_prop, bit_stride);
16235 return set_die_type (die, type, cu);
16236 }
16237
16238 std::vector<struct type *> range_types;
16239 child_die = die->child;
16240 while (child_die && child_die->tag)
16241 {
16242 if (child_die->tag == DW_TAG_subrange_type)
16243 {
16244 struct type *child_type = read_type_die (child_die, cu);
16245
16246 if (child_type != NULL)
16247 {
16248 /* The range type was succesfully read. Save it for the
16249 array type creation. */
16250 range_types.push_back (child_type);
16251 }
16252 }
16253 child_die = child_die->sibling;
16254 }
16255
16256 /* Dwarf2 dimensions are output from left to right, create the
16257 necessary array types in backwards order. */
16258
16259 type = element_type;
16260
16261 if (read_array_order (die, cu) == DW_ORD_col_major)
16262 {
16263 int i = 0;
16264
16265 while (i < range_types.size ())
16266 type = create_array_type_with_stride (NULL, type, range_types[i++],
16267 byte_stride_prop, bit_stride);
16268 }
16269 else
16270 {
16271 size_t ndim = range_types.size ();
16272 while (ndim-- > 0)
16273 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16274 byte_stride_prop, bit_stride);
16275 }
16276
16277 /* Understand Dwarf2 support for vector types (like they occur on
16278 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16279 array type. This is not part of the Dwarf2/3 standard yet, but a
16280 custom vendor extension. The main difference between a regular
16281 array and the vector variant is that vectors are passed by value
16282 to functions. */
16283 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16284 if (attr != nullptr)
16285 make_vector_type (type);
16286
16287 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16288 implementation may choose to implement triple vectors using this
16289 attribute. */
16290 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16291 if (attr != nullptr)
16292 {
16293 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16294 TYPE_LENGTH (type) = DW_UNSND (attr);
16295 else
16296 complaint (_("DW_AT_byte_size for array type smaller "
16297 "than the total size of elements"));
16298 }
16299
16300 name = dwarf2_name (die, cu);
16301 if (name)
16302 type->set_name (name);
16303
16304 maybe_set_alignment (cu, die, type);
16305
16306 /* Install the type in the die. */
16307 set_die_type (die, type, cu);
16308
16309 /* set_die_type should be already done. */
16310 set_descriptive_type (type, die, cu);
16311
16312 return type;
16313 }
16314
16315 static enum dwarf_array_dim_ordering
16316 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16317 {
16318 struct attribute *attr;
16319
16320 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16321
16322 if (attr != nullptr)
16323 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16324
16325 /* GNU F77 is a special case, as at 08/2004 array type info is the
16326 opposite order to the dwarf2 specification, but data is still
16327 laid out as per normal fortran.
16328
16329 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16330 version checking. */
16331
16332 if (cu->language == language_fortran
16333 && cu->producer && strstr (cu->producer, "GNU F77"))
16334 {
16335 return DW_ORD_row_major;
16336 }
16337
16338 switch (cu->language_defn->la_array_ordering)
16339 {
16340 case array_column_major:
16341 return DW_ORD_col_major;
16342 case array_row_major:
16343 default:
16344 return DW_ORD_row_major;
16345 };
16346 }
16347
16348 /* Extract all information from a DW_TAG_set_type DIE and put it in
16349 the DIE's type field. */
16350
16351 static struct type *
16352 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16353 {
16354 struct type *domain_type, *set_type;
16355 struct attribute *attr;
16356
16357 domain_type = die_type (die, cu);
16358
16359 /* The die_type call above may have already set the type for this DIE. */
16360 set_type = get_die_type (die, cu);
16361 if (set_type)
16362 return set_type;
16363
16364 set_type = create_set_type (NULL, domain_type);
16365
16366 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16367 if (attr != nullptr)
16368 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16369
16370 maybe_set_alignment (cu, die, set_type);
16371
16372 return set_die_type (die, set_type, cu);
16373 }
16374
16375 /* A helper for read_common_block that creates a locexpr baton.
16376 SYM is the symbol which we are marking as computed.
16377 COMMON_DIE is the DIE for the common block.
16378 COMMON_LOC is the location expression attribute for the common
16379 block itself.
16380 MEMBER_LOC is the location expression attribute for the particular
16381 member of the common block that we are processing.
16382 CU is the CU from which the above come. */
16383
16384 static void
16385 mark_common_block_symbol_computed (struct symbol *sym,
16386 struct die_info *common_die,
16387 struct attribute *common_loc,
16388 struct attribute *member_loc,
16389 struct dwarf2_cu *cu)
16390 {
16391 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16392 struct objfile *objfile = per_objfile->objfile;
16393 struct dwarf2_locexpr_baton *baton;
16394 gdb_byte *ptr;
16395 unsigned int cu_off;
16396 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16397 LONGEST offset = 0;
16398
16399 gdb_assert (common_loc && member_loc);
16400 gdb_assert (common_loc->form_is_block ());
16401 gdb_assert (member_loc->form_is_block ()
16402 || member_loc->form_is_constant ());
16403
16404 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16405 baton->per_objfile = per_objfile;
16406 baton->per_cu = cu->per_cu;
16407 gdb_assert (baton->per_cu);
16408
16409 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16410
16411 if (member_loc->form_is_constant ())
16412 {
16413 offset = member_loc->constant_value (0);
16414 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16415 }
16416 else
16417 baton->size += DW_BLOCK (member_loc)->size;
16418
16419 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16420 baton->data = ptr;
16421
16422 *ptr++ = DW_OP_call4;
16423 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16424 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16425 ptr += 4;
16426
16427 if (member_loc->form_is_constant ())
16428 {
16429 *ptr++ = DW_OP_addr;
16430 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16431 ptr += cu->header.addr_size;
16432 }
16433 else
16434 {
16435 /* We have to copy the data here, because DW_OP_call4 will only
16436 use a DW_AT_location attribute. */
16437 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16438 ptr += DW_BLOCK (member_loc)->size;
16439 }
16440
16441 *ptr++ = DW_OP_plus;
16442 gdb_assert (ptr - baton->data == baton->size);
16443
16444 SYMBOL_LOCATION_BATON (sym) = baton;
16445 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16446 }
16447
16448 /* Create appropriate locally-scoped variables for all the
16449 DW_TAG_common_block entries. Also create a struct common_block
16450 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16451 is used to separate the common blocks name namespace from regular
16452 variable names. */
16453
16454 static void
16455 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16456 {
16457 struct attribute *attr;
16458
16459 attr = dwarf2_attr (die, DW_AT_location, cu);
16460 if (attr != nullptr)
16461 {
16462 /* Support the .debug_loc offsets. */
16463 if (attr->form_is_block ())
16464 {
16465 /* Ok. */
16466 }
16467 else if (attr->form_is_section_offset ())
16468 {
16469 dwarf2_complex_location_expr_complaint ();
16470 attr = NULL;
16471 }
16472 else
16473 {
16474 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16475 "common block member");
16476 attr = NULL;
16477 }
16478 }
16479
16480 if (die->child != NULL)
16481 {
16482 struct objfile *objfile = cu->per_objfile->objfile;
16483 struct die_info *child_die;
16484 size_t n_entries = 0, size;
16485 struct common_block *common_block;
16486 struct symbol *sym;
16487
16488 for (child_die = die->child;
16489 child_die && child_die->tag;
16490 child_die = child_die->sibling)
16491 ++n_entries;
16492
16493 size = (sizeof (struct common_block)
16494 + (n_entries - 1) * sizeof (struct symbol *));
16495 common_block
16496 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16497 size);
16498 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16499 common_block->n_entries = 0;
16500
16501 for (child_die = die->child;
16502 child_die && child_die->tag;
16503 child_die = child_die->sibling)
16504 {
16505 /* Create the symbol in the DW_TAG_common_block block in the current
16506 symbol scope. */
16507 sym = new_symbol (child_die, NULL, cu);
16508 if (sym != NULL)
16509 {
16510 struct attribute *member_loc;
16511
16512 common_block->contents[common_block->n_entries++] = sym;
16513
16514 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16515 cu);
16516 if (member_loc)
16517 {
16518 /* GDB has handled this for a long time, but it is
16519 not specified by DWARF. It seems to have been
16520 emitted by gfortran at least as recently as:
16521 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16522 complaint (_("Variable in common block has "
16523 "DW_AT_data_member_location "
16524 "- DIE at %s [in module %s]"),
16525 sect_offset_str (child_die->sect_off),
16526 objfile_name (objfile));
16527
16528 if (member_loc->form_is_section_offset ())
16529 dwarf2_complex_location_expr_complaint ();
16530 else if (member_loc->form_is_constant ()
16531 || member_loc->form_is_block ())
16532 {
16533 if (attr != nullptr)
16534 mark_common_block_symbol_computed (sym, die, attr,
16535 member_loc, cu);
16536 }
16537 else
16538 dwarf2_complex_location_expr_complaint ();
16539 }
16540 }
16541 }
16542
16543 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16544 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16545 }
16546 }
16547
16548 /* Create a type for a C++ namespace. */
16549
16550 static struct type *
16551 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16552 {
16553 struct objfile *objfile = cu->per_objfile->objfile;
16554 const char *previous_prefix, *name;
16555 int is_anonymous;
16556 struct type *type;
16557
16558 /* For extensions, reuse the type of the original namespace. */
16559 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16560 {
16561 struct die_info *ext_die;
16562 struct dwarf2_cu *ext_cu = cu;
16563
16564 ext_die = dwarf2_extension (die, &ext_cu);
16565 type = read_type_die (ext_die, ext_cu);
16566
16567 /* EXT_CU may not be the same as CU.
16568 Ensure TYPE is recorded with CU in die_type_hash. */
16569 return set_die_type (die, type, cu);
16570 }
16571
16572 name = namespace_name (die, &is_anonymous, cu);
16573
16574 /* Now build the name of the current namespace. */
16575
16576 previous_prefix = determine_prefix (die, cu);
16577 if (previous_prefix[0] != '\0')
16578 name = typename_concat (&objfile->objfile_obstack,
16579 previous_prefix, name, 0, cu);
16580
16581 /* Create the type. */
16582 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16583
16584 return set_die_type (die, type, cu);
16585 }
16586
16587 /* Read a namespace scope. */
16588
16589 static void
16590 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16591 {
16592 struct objfile *objfile = cu->per_objfile->objfile;
16593 int is_anonymous;
16594
16595 /* Add a symbol associated to this if we haven't seen the namespace
16596 before. Also, add a using directive if it's an anonymous
16597 namespace. */
16598
16599 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16600 {
16601 struct type *type;
16602
16603 type = read_type_die (die, cu);
16604 new_symbol (die, type, cu);
16605
16606 namespace_name (die, &is_anonymous, cu);
16607 if (is_anonymous)
16608 {
16609 const char *previous_prefix = determine_prefix (die, cu);
16610
16611 std::vector<const char *> excludes;
16612 add_using_directive (using_directives (cu),
16613 previous_prefix, type->name (), NULL,
16614 NULL, excludes, 0, &objfile->objfile_obstack);
16615 }
16616 }
16617
16618 if (die->child != NULL)
16619 {
16620 struct die_info *child_die = die->child;
16621
16622 while (child_die && child_die->tag)
16623 {
16624 process_die (child_die, cu);
16625 child_die = child_die->sibling;
16626 }
16627 }
16628 }
16629
16630 /* Read a Fortran module as type. This DIE can be only a declaration used for
16631 imported module. Still we need that type as local Fortran "use ... only"
16632 declaration imports depend on the created type in determine_prefix. */
16633
16634 static struct type *
16635 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16636 {
16637 struct objfile *objfile = cu->per_objfile->objfile;
16638 const char *module_name;
16639 struct type *type;
16640
16641 module_name = dwarf2_name (die, cu);
16642 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16643
16644 return set_die_type (die, type, cu);
16645 }
16646
16647 /* Read a Fortran module. */
16648
16649 static void
16650 read_module (struct die_info *die, struct dwarf2_cu *cu)
16651 {
16652 struct die_info *child_die = die->child;
16653 struct type *type;
16654
16655 type = read_type_die (die, cu);
16656 new_symbol (die, type, cu);
16657
16658 while (child_die && child_die->tag)
16659 {
16660 process_die (child_die, cu);
16661 child_die = child_die->sibling;
16662 }
16663 }
16664
16665 /* Return the name of the namespace represented by DIE. Set
16666 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16667 namespace. */
16668
16669 static const char *
16670 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16671 {
16672 struct die_info *current_die;
16673 const char *name = NULL;
16674
16675 /* Loop through the extensions until we find a name. */
16676
16677 for (current_die = die;
16678 current_die != NULL;
16679 current_die = dwarf2_extension (die, &cu))
16680 {
16681 /* We don't use dwarf2_name here so that we can detect the absence
16682 of a name -> anonymous namespace. */
16683 name = dwarf2_string_attr (die, DW_AT_name, cu);
16684
16685 if (name != NULL)
16686 break;
16687 }
16688
16689 /* Is it an anonymous namespace? */
16690
16691 *is_anonymous = (name == NULL);
16692 if (*is_anonymous)
16693 name = CP_ANONYMOUS_NAMESPACE_STR;
16694
16695 return name;
16696 }
16697
16698 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16699 the user defined type vector. */
16700
16701 static struct type *
16702 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16703 {
16704 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16705 struct comp_unit_head *cu_header = &cu->header;
16706 struct type *type;
16707 struct attribute *attr_byte_size;
16708 struct attribute *attr_address_class;
16709 int byte_size, addr_class;
16710 struct type *target_type;
16711
16712 target_type = die_type (die, cu);
16713
16714 /* The die_type call above may have already set the type for this DIE. */
16715 type = get_die_type (die, cu);
16716 if (type)
16717 return type;
16718
16719 type = lookup_pointer_type (target_type);
16720
16721 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16722 if (attr_byte_size)
16723 byte_size = DW_UNSND (attr_byte_size);
16724 else
16725 byte_size = cu_header->addr_size;
16726
16727 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16728 if (attr_address_class)
16729 addr_class = DW_UNSND (attr_address_class);
16730 else
16731 addr_class = DW_ADDR_none;
16732
16733 ULONGEST alignment = get_alignment (cu, die);
16734
16735 /* If the pointer size, alignment, or address class is different
16736 than the default, create a type variant marked as such and set
16737 the length accordingly. */
16738 if (TYPE_LENGTH (type) != byte_size
16739 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16740 && alignment != TYPE_RAW_ALIGN (type))
16741 || addr_class != DW_ADDR_none)
16742 {
16743 if (gdbarch_address_class_type_flags_p (gdbarch))
16744 {
16745 int type_flags;
16746
16747 type_flags = gdbarch_address_class_type_flags
16748 (gdbarch, byte_size, addr_class);
16749 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16750 == 0);
16751 type = make_type_with_address_space (type, type_flags);
16752 }
16753 else if (TYPE_LENGTH (type) != byte_size)
16754 {
16755 complaint (_("invalid pointer size %d"), byte_size);
16756 }
16757 else if (TYPE_RAW_ALIGN (type) != alignment)
16758 {
16759 complaint (_("Invalid DW_AT_alignment"
16760 " - DIE at %s [in module %s]"),
16761 sect_offset_str (die->sect_off),
16762 objfile_name (cu->per_objfile->objfile));
16763 }
16764 else
16765 {
16766 /* Should we also complain about unhandled address classes? */
16767 }
16768 }
16769
16770 TYPE_LENGTH (type) = byte_size;
16771 set_type_align (type, alignment);
16772 return set_die_type (die, type, cu);
16773 }
16774
16775 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16776 the user defined type vector. */
16777
16778 static struct type *
16779 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16780 {
16781 struct type *type;
16782 struct type *to_type;
16783 struct type *domain;
16784
16785 to_type = die_type (die, cu);
16786 domain = die_containing_type (die, cu);
16787
16788 /* The calls above may have already set the type for this DIE. */
16789 type = get_die_type (die, cu);
16790 if (type)
16791 return type;
16792
16793 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16794 type = lookup_methodptr_type (to_type);
16795 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16796 {
16797 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16798
16799 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16800 to_type->fields (), to_type->num_fields (),
16801 TYPE_VARARGS (to_type));
16802 type = lookup_methodptr_type (new_type);
16803 }
16804 else
16805 type = lookup_memberptr_type (to_type, domain);
16806
16807 return set_die_type (die, type, cu);
16808 }
16809
16810 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16811 the user defined type vector. */
16812
16813 static struct type *
16814 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16815 enum type_code refcode)
16816 {
16817 struct comp_unit_head *cu_header = &cu->header;
16818 struct type *type, *target_type;
16819 struct attribute *attr;
16820
16821 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16822
16823 target_type = die_type (die, cu);
16824
16825 /* The die_type call above may have already set the type for this DIE. */
16826 type = get_die_type (die, cu);
16827 if (type)
16828 return type;
16829
16830 type = lookup_reference_type (target_type, refcode);
16831 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16832 if (attr != nullptr)
16833 {
16834 TYPE_LENGTH (type) = DW_UNSND (attr);
16835 }
16836 else
16837 {
16838 TYPE_LENGTH (type) = cu_header->addr_size;
16839 }
16840 maybe_set_alignment (cu, die, type);
16841 return set_die_type (die, type, cu);
16842 }
16843
16844 /* Add the given cv-qualifiers to the element type of the array. GCC
16845 outputs DWARF type qualifiers that apply to an array, not the
16846 element type. But GDB relies on the array element type to carry
16847 the cv-qualifiers. This mimics section 6.7.3 of the C99
16848 specification. */
16849
16850 static struct type *
16851 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16852 struct type *base_type, int cnst, int voltl)
16853 {
16854 struct type *el_type, *inner_array;
16855
16856 base_type = copy_type (base_type);
16857 inner_array = base_type;
16858
16859 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16860 {
16861 TYPE_TARGET_TYPE (inner_array) =
16862 copy_type (TYPE_TARGET_TYPE (inner_array));
16863 inner_array = TYPE_TARGET_TYPE (inner_array);
16864 }
16865
16866 el_type = TYPE_TARGET_TYPE (inner_array);
16867 cnst |= TYPE_CONST (el_type);
16868 voltl |= TYPE_VOLATILE (el_type);
16869 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16870
16871 return set_die_type (die, base_type, cu);
16872 }
16873
16874 static struct type *
16875 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16876 {
16877 struct type *base_type, *cv_type;
16878
16879 base_type = die_type (die, cu);
16880
16881 /* The die_type call above may have already set the type for this DIE. */
16882 cv_type = get_die_type (die, cu);
16883 if (cv_type)
16884 return cv_type;
16885
16886 /* In case the const qualifier is applied to an array type, the element type
16887 is so qualified, not the array type (section 6.7.3 of C99). */
16888 if (base_type->code () == TYPE_CODE_ARRAY)
16889 return add_array_cv_type (die, cu, base_type, 1, 0);
16890
16891 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16892 return set_die_type (die, cv_type, cu);
16893 }
16894
16895 static struct type *
16896 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16897 {
16898 struct type *base_type, *cv_type;
16899
16900 base_type = die_type (die, cu);
16901
16902 /* The die_type call above may have already set the type for this DIE. */
16903 cv_type = get_die_type (die, cu);
16904 if (cv_type)
16905 return cv_type;
16906
16907 /* In case the volatile qualifier is applied to an array type, the
16908 element type is so qualified, not the array type (section 6.7.3
16909 of C99). */
16910 if (base_type->code () == TYPE_CODE_ARRAY)
16911 return add_array_cv_type (die, cu, base_type, 0, 1);
16912
16913 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16914 return set_die_type (die, cv_type, cu);
16915 }
16916
16917 /* Handle DW_TAG_restrict_type. */
16918
16919 static struct type *
16920 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16921 {
16922 struct type *base_type, *cv_type;
16923
16924 base_type = die_type (die, cu);
16925
16926 /* The die_type call above may have already set the type for this DIE. */
16927 cv_type = get_die_type (die, cu);
16928 if (cv_type)
16929 return cv_type;
16930
16931 cv_type = make_restrict_type (base_type);
16932 return set_die_type (die, cv_type, cu);
16933 }
16934
16935 /* Handle DW_TAG_atomic_type. */
16936
16937 static struct type *
16938 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16939 {
16940 struct type *base_type, *cv_type;
16941
16942 base_type = die_type (die, cu);
16943
16944 /* The die_type call above may have already set the type for this DIE. */
16945 cv_type = get_die_type (die, cu);
16946 if (cv_type)
16947 return cv_type;
16948
16949 cv_type = make_atomic_type (base_type);
16950 return set_die_type (die, cv_type, cu);
16951 }
16952
16953 /* Extract all information from a DW_TAG_string_type DIE and add to
16954 the user defined type vector. It isn't really a user defined type,
16955 but it behaves like one, with other DIE's using an AT_user_def_type
16956 attribute to reference it. */
16957
16958 static struct type *
16959 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16960 {
16961 struct objfile *objfile = cu->per_objfile->objfile;
16962 struct gdbarch *gdbarch = objfile->arch ();
16963 struct type *type, *range_type, *index_type, *char_type;
16964 struct attribute *attr;
16965 struct dynamic_prop prop;
16966 bool length_is_constant = true;
16967 LONGEST length;
16968
16969 /* There are a couple of places where bit sizes might be made use of
16970 when parsing a DW_TAG_string_type, however, no producer that we know
16971 of make use of these. Handling bit sizes that are a multiple of the
16972 byte size is easy enough, but what about other bit sizes? Lets deal
16973 with that problem when we have to. Warn about these attributes being
16974 unsupported, then parse the type and ignore them like we always
16975 have. */
16976 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16977 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16978 {
16979 static bool warning_printed = false;
16980 if (!warning_printed)
16981 {
16982 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16983 "currently supported on DW_TAG_string_type."));
16984 warning_printed = true;
16985 }
16986 }
16987
16988 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16989 if (attr != nullptr && !attr->form_is_constant ())
16990 {
16991 /* The string length describes the location at which the length of
16992 the string can be found. The size of the length field can be
16993 specified with one of the attributes below. */
16994 struct type *prop_type;
16995 struct attribute *len
16996 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16997 if (len == nullptr)
16998 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16999 if (len != nullptr && len->form_is_constant ())
17000 {
17001 /* Pass 0 as the default as we know this attribute is constant
17002 and the default value will not be returned. */
17003 LONGEST sz = len->constant_value (0);
17004 prop_type = cu->per_objfile->int_type (sz, true);
17005 }
17006 else
17007 {
17008 /* If the size is not specified then we assume it is the size of
17009 an address on this target. */
17010 prop_type = cu->addr_sized_int_type (true);
17011 }
17012
17013 /* Convert the attribute into a dynamic property. */
17014 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17015 length = 1;
17016 else
17017 length_is_constant = false;
17018 }
17019 else if (attr != nullptr)
17020 {
17021 /* This DW_AT_string_length just contains the length with no
17022 indirection. There's no need to create a dynamic property in this
17023 case. Pass 0 for the default value as we know it will not be
17024 returned in this case. */
17025 length = attr->constant_value (0);
17026 }
17027 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17028 {
17029 /* We don't currently support non-constant byte sizes for strings. */
17030 length = attr->constant_value (1);
17031 }
17032 else
17033 {
17034 /* Use 1 as a fallback length if we have nothing else. */
17035 length = 1;
17036 }
17037
17038 index_type = objfile_type (objfile)->builtin_int;
17039 if (length_is_constant)
17040 range_type = create_static_range_type (NULL, index_type, 1, length);
17041 else
17042 {
17043 struct dynamic_prop low_bound;
17044
17045 low_bound.kind = PROP_CONST;
17046 low_bound.data.const_val = 1;
17047 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17048 }
17049 char_type = language_string_char_type (cu->language_defn, gdbarch);
17050 type = create_string_type (NULL, char_type, range_type);
17051
17052 return set_die_type (die, type, cu);
17053 }
17054
17055 /* Assuming that DIE corresponds to a function, returns nonzero
17056 if the function is prototyped. */
17057
17058 static int
17059 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17060 {
17061 struct attribute *attr;
17062
17063 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17064 if (attr && (DW_UNSND (attr) != 0))
17065 return 1;
17066
17067 /* The DWARF standard implies that the DW_AT_prototyped attribute
17068 is only meaningful for C, but the concept also extends to other
17069 languages that allow unprototyped functions (Eg: Objective C).
17070 For all other languages, assume that functions are always
17071 prototyped. */
17072 if (cu->language != language_c
17073 && cu->language != language_objc
17074 && cu->language != language_opencl)
17075 return 1;
17076
17077 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17078 prototyped and unprototyped functions; default to prototyped,
17079 since that is more common in modern code (and RealView warns
17080 about unprototyped functions). */
17081 if (producer_is_realview (cu->producer))
17082 return 1;
17083
17084 return 0;
17085 }
17086
17087 /* Handle DIES due to C code like:
17088
17089 struct foo
17090 {
17091 int (*funcp)(int a, long l);
17092 int b;
17093 };
17094
17095 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17096
17097 static struct type *
17098 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17099 {
17100 struct objfile *objfile = cu->per_objfile->objfile;
17101 struct type *type; /* Type that this function returns. */
17102 struct type *ftype; /* Function that returns above type. */
17103 struct attribute *attr;
17104
17105 type = die_type (die, cu);
17106
17107 /* The die_type call above may have already set the type for this DIE. */
17108 ftype = get_die_type (die, cu);
17109 if (ftype)
17110 return ftype;
17111
17112 ftype = lookup_function_type (type);
17113
17114 if (prototyped_function_p (die, cu))
17115 TYPE_PROTOTYPED (ftype) = 1;
17116
17117 /* Store the calling convention in the type if it's available in
17118 the subroutine die. Otherwise set the calling convention to
17119 the default value DW_CC_normal. */
17120 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17121 if (attr != nullptr
17122 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17123 TYPE_CALLING_CONVENTION (ftype)
17124 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17125 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17126 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17127 else
17128 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17129
17130 /* Record whether the function returns normally to its caller or not
17131 if the DWARF producer set that information. */
17132 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17133 if (attr && (DW_UNSND (attr) != 0))
17134 TYPE_NO_RETURN (ftype) = 1;
17135
17136 /* We need to add the subroutine type to the die immediately so
17137 we don't infinitely recurse when dealing with parameters
17138 declared as the same subroutine type. */
17139 set_die_type (die, ftype, cu);
17140
17141 if (die->child != NULL)
17142 {
17143 struct type *void_type = objfile_type (objfile)->builtin_void;
17144 struct die_info *child_die;
17145 int nparams, iparams;
17146
17147 /* Count the number of parameters.
17148 FIXME: GDB currently ignores vararg functions, but knows about
17149 vararg member functions. */
17150 nparams = 0;
17151 child_die = die->child;
17152 while (child_die && child_die->tag)
17153 {
17154 if (child_die->tag == DW_TAG_formal_parameter)
17155 nparams++;
17156 else if (child_die->tag == DW_TAG_unspecified_parameters)
17157 TYPE_VARARGS (ftype) = 1;
17158 child_die = child_die->sibling;
17159 }
17160
17161 /* Allocate storage for parameters and fill them in. */
17162 ftype->set_num_fields (nparams);
17163 ftype->set_fields
17164 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17165
17166 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17167 even if we error out during the parameters reading below. */
17168 for (iparams = 0; iparams < nparams; iparams++)
17169 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17170
17171 iparams = 0;
17172 child_die = die->child;
17173 while (child_die && child_die->tag)
17174 {
17175 if (child_die->tag == DW_TAG_formal_parameter)
17176 {
17177 struct type *arg_type;
17178
17179 /* DWARF version 2 has no clean way to discern C++
17180 static and non-static member functions. G++ helps
17181 GDB by marking the first parameter for non-static
17182 member functions (which is the this pointer) as
17183 artificial. We pass this information to
17184 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17185
17186 DWARF version 3 added DW_AT_object_pointer, which GCC
17187 4.5 does not yet generate. */
17188 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17189 if (attr != nullptr)
17190 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17191 else
17192 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17193 arg_type = die_type (child_die, cu);
17194
17195 /* RealView does not mark THIS as const, which the testsuite
17196 expects. GCC marks THIS as const in method definitions,
17197 but not in the class specifications (GCC PR 43053). */
17198 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17199 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17200 {
17201 int is_this = 0;
17202 struct dwarf2_cu *arg_cu = cu;
17203 const char *name = dwarf2_name (child_die, cu);
17204
17205 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17206 if (attr != nullptr)
17207 {
17208 /* If the compiler emits this, use it. */
17209 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17210 is_this = 1;
17211 }
17212 else if (name && strcmp (name, "this") == 0)
17213 /* Function definitions will have the argument names. */
17214 is_this = 1;
17215 else if (name == NULL && iparams == 0)
17216 /* Declarations may not have the names, so like
17217 elsewhere in GDB, assume an artificial first
17218 argument is "this". */
17219 is_this = 1;
17220
17221 if (is_this)
17222 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17223 arg_type, 0);
17224 }
17225
17226 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17227 iparams++;
17228 }
17229 child_die = child_die->sibling;
17230 }
17231 }
17232
17233 return ftype;
17234 }
17235
17236 static struct type *
17237 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17238 {
17239 struct objfile *objfile = cu->per_objfile->objfile;
17240 const char *name = NULL;
17241 struct type *this_type, *target_type;
17242
17243 name = dwarf2_full_name (NULL, die, cu);
17244 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17245 TYPE_TARGET_STUB (this_type) = 1;
17246 set_die_type (die, this_type, cu);
17247 target_type = die_type (die, cu);
17248 if (target_type != this_type)
17249 TYPE_TARGET_TYPE (this_type) = target_type;
17250 else
17251 {
17252 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17253 spec and cause infinite loops in GDB. */
17254 complaint (_("Self-referential DW_TAG_typedef "
17255 "- DIE at %s [in module %s]"),
17256 sect_offset_str (die->sect_off), objfile_name (objfile));
17257 TYPE_TARGET_TYPE (this_type) = NULL;
17258 }
17259 if (name == NULL)
17260 {
17261 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17262 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17263 Handle these by just returning the target type, rather than
17264 constructing an anonymous typedef type and trying to handle this
17265 elsewhere. */
17266 set_die_type (die, target_type, cu);
17267 return target_type;
17268 }
17269 return this_type;
17270 }
17271
17272 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17273 (which may be different from NAME) to the architecture back-end to allow
17274 it to guess the correct format if necessary. */
17275
17276 static struct type *
17277 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17278 const char *name_hint, enum bfd_endian byte_order)
17279 {
17280 struct gdbarch *gdbarch = objfile->arch ();
17281 const struct floatformat **format;
17282 struct type *type;
17283
17284 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17285 if (format)
17286 type = init_float_type (objfile, bits, name, format, byte_order);
17287 else
17288 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17289
17290 return type;
17291 }
17292
17293 /* Allocate an integer type of size BITS and name NAME. */
17294
17295 static struct type *
17296 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17297 int bits, int unsigned_p, const char *name)
17298 {
17299 struct type *type;
17300
17301 /* Versions of Intel's C Compiler generate an integer type called "void"
17302 instead of using DW_TAG_unspecified_type. This has been seen on
17303 at least versions 14, 17, and 18. */
17304 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17305 && strcmp (name, "void") == 0)
17306 type = objfile_type (objfile)->builtin_void;
17307 else
17308 type = init_integer_type (objfile, bits, unsigned_p, name);
17309
17310 return type;
17311 }
17312
17313 /* Initialise and return a floating point type of size BITS suitable for
17314 use as a component of a complex number. The NAME_HINT is passed through
17315 when initialising the floating point type and is the name of the complex
17316 type.
17317
17318 As DWARF doesn't currently provide an explicit name for the components
17319 of a complex number, but it can be helpful to have these components
17320 named, we try to select a suitable name based on the size of the
17321 component. */
17322 static struct type *
17323 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17324 struct objfile *objfile,
17325 int bits, const char *name_hint,
17326 enum bfd_endian byte_order)
17327 {
17328 gdbarch *gdbarch = objfile->arch ();
17329 struct type *tt = nullptr;
17330
17331 /* Try to find a suitable floating point builtin type of size BITS.
17332 We're going to use the name of this type as the name for the complex
17333 target type that we are about to create. */
17334 switch (cu->language)
17335 {
17336 case language_fortran:
17337 switch (bits)
17338 {
17339 case 32:
17340 tt = builtin_f_type (gdbarch)->builtin_real;
17341 break;
17342 case 64:
17343 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17344 break;
17345 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17346 case 128:
17347 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17348 break;
17349 }
17350 break;
17351 default:
17352 switch (bits)
17353 {
17354 case 32:
17355 tt = builtin_type (gdbarch)->builtin_float;
17356 break;
17357 case 64:
17358 tt = builtin_type (gdbarch)->builtin_double;
17359 break;
17360 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17361 case 128:
17362 tt = builtin_type (gdbarch)->builtin_long_double;
17363 break;
17364 }
17365 break;
17366 }
17367
17368 /* If the type we found doesn't match the size we were looking for, then
17369 pretend we didn't find a type at all, the complex target type we
17370 create will then be nameless. */
17371 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17372 tt = nullptr;
17373
17374 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17375 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17376 }
17377
17378 /* Find a representation of a given base type and install
17379 it in the TYPE field of the die. */
17380
17381 static struct type *
17382 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17383 {
17384 struct objfile *objfile = cu->per_objfile->objfile;
17385 struct type *type;
17386 struct attribute *attr;
17387 int encoding = 0, bits = 0;
17388 const char *name;
17389 gdbarch *arch;
17390
17391 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17392 if (attr != nullptr)
17393 encoding = DW_UNSND (attr);
17394 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17395 if (attr != nullptr)
17396 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17397 name = dwarf2_name (die, cu);
17398 if (!name)
17399 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17400
17401 arch = objfile->arch ();
17402 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17403
17404 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17405 if (attr)
17406 {
17407 int endianity = DW_UNSND (attr);
17408
17409 switch (endianity)
17410 {
17411 case DW_END_big:
17412 byte_order = BFD_ENDIAN_BIG;
17413 break;
17414 case DW_END_little:
17415 byte_order = BFD_ENDIAN_LITTLE;
17416 break;
17417 default:
17418 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17419 break;
17420 }
17421 }
17422
17423 switch (encoding)
17424 {
17425 case DW_ATE_address:
17426 /* Turn DW_ATE_address into a void * pointer. */
17427 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17428 type = init_pointer_type (objfile, bits, name, type);
17429 break;
17430 case DW_ATE_boolean:
17431 type = init_boolean_type (objfile, bits, 1, name);
17432 break;
17433 case DW_ATE_complex_float:
17434 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17435 byte_order);
17436 if (type->code () == TYPE_CODE_ERROR)
17437 {
17438 if (name == nullptr)
17439 {
17440 struct obstack *obstack
17441 = &cu->per_objfile->objfile->objfile_obstack;
17442 name = obconcat (obstack, "_Complex ", type->name (),
17443 nullptr);
17444 }
17445 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17446 }
17447 else
17448 type = init_complex_type (name, type);
17449 break;
17450 case DW_ATE_decimal_float:
17451 type = init_decfloat_type (objfile, bits, name);
17452 break;
17453 case DW_ATE_float:
17454 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17455 break;
17456 case DW_ATE_signed:
17457 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17458 break;
17459 case DW_ATE_unsigned:
17460 if (cu->language == language_fortran
17461 && name
17462 && startswith (name, "character("))
17463 type = init_character_type (objfile, bits, 1, name);
17464 else
17465 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17466 break;
17467 case DW_ATE_signed_char:
17468 if (cu->language == language_ada || cu->language == language_m2
17469 || cu->language == language_pascal
17470 || cu->language == language_fortran)
17471 type = init_character_type (objfile, bits, 0, name);
17472 else
17473 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17474 break;
17475 case DW_ATE_unsigned_char:
17476 if (cu->language == language_ada || cu->language == language_m2
17477 || cu->language == language_pascal
17478 || cu->language == language_fortran
17479 || cu->language == language_rust)
17480 type = init_character_type (objfile, bits, 1, name);
17481 else
17482 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17483 break;
17484 case DW_ATE_UTF:
17485 {
17486 if (bits == 16)
17487 type = builtin_type (arch)->builtin_char16;
17488 else if (bits == 32)
17489 type = builtin_type (arch)->builtin_char32;
17490 else
17491 {
17492 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17493 bits);
17494 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17495 }
17496 return set_die_type (die, type, cu);
17497 }
17498 break;
17499
17500 default:
17501 complaint (_("unsupported DW_AT_encoding: '%s'"),
17502 dwarf_type_encoding_name (encoding));
17503 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17504 break;
17505 }
17506
17507 if (name && strcmp (name, "char") == 0)
17508 TYPE_NOSIGN (type) = 1;
17509
17510 maybe_set_alignment (cu, die, type);
17511
17512 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17513
17514 return set_die_type (die, type, cu);
17515 }
17516
17517 /* Parse dwarf attribute if it's a block, reference or constant and put the
17518 resulting value of the attribute into struct bound_prop.
17519 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17520
17521 static int
17522 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17523 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17524 struct type *default_type)
17525 {
17526 struct dwarf2_property_baton *baton;
17527 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17528 struct objfile *objfile = per_objfile->objfile;
17529 struct obstack *obstack = &objfile->objfile_obstack;
17530
17531 gdb_assert (default_type != NULL);
17532
17533 if (attr == NULL || prop == NULL)
17534 return 0;
17535
17536 if (attr->form_is_block ())
17537 {
17538 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17539 baton->property_type = default_type;
17540 baton->locexpr.per_cu = cu->per_cu;
17541 baton->locexpr.per_objfile = per_objfile;
17542 baton->locexpr.size = DW_BLOCK (attr)->size;
17543 baton->locexpr.data = DW_BLOCK (attr)->data;
17544 switch (attr->name)
17545 {
17546 case DW_AT_string_length:
17547 baton->locexpr.is_reference = true;
17548 break;
17549 default:
17550 baton->locexpr.is_reference = false;
17551 break;
17552 }
17553 prop->data.baton = baton;
17554 prop->kind = PROP_LOCEXPR;
17555 gdb_assert (prop->data.baton != NULL);
17556 }
17557 else if (attr->form_is_ref ())
17558 {
17559 struct dwarf2_cu *target_cu = cu;
17560 struct die_info *target_die;
17561 struct attribute *target_attr;
17562
17563 target_die = follow_die_ref (die, attr, &target_cu);
17564 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17565 if (target_attr == NULL)
17566 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17567 target_cu);
17568 if (target_attr == NULL)
17569 return 0;
17570
17571 switch (target_attr->name)
17572 {
17573 case DW_AT_location:
17574 if (target_attr->form_is_section_offset ())
17575 {
17576 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17577 baton->property_type = die_type (target_die, target_cu);
17578 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17579 prop->data.baton = baton;
17580 prop->kind = PROP_LOCLIST;
17581 gdb_assert (prop->data.baton != NULL);
17582 }
17583 else if (target_attr->form_is_block ())
17584 {
17585 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17586 baton->property_type = die_type (target_die, target_cu);
17587 baton->locexpr.per_cu = cu->per_cu;
17588 baton->locexpr.per_objfile = per_objfile;
17589 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17590 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17591 baton->locexpr.is_reference = true;
17592 prop->data.baton = baton;
17593 prop->kind = PROP_LOCEXPR;
17594 gdb_assert (prop->data.baton != NULL);
17595 }
17596 else
17597 {
17598 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17599 "dynamic property");
17600 return 0;
17601 }
17602 break;
17603 case DW_AT_data_member_location:
17604 {
17605 LONGEST offset;
17606
17607 if (!handle_data_member_location (target_die, target_cu,
17608 &offset))
17609 return 0;
17610
17611 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17612 baton->property_type = read_type_die (target_die->parent,
17613 target_cu);
17614 baton->offset_info.offset = offset;
17615 baton->offset_info.type = die_type (target_die, target_cu);
17616 prop->data.baton = baton;
17617 prop->kind = PROP_ADDR_OFFSET;
17618 break;
17619 }
17620 }
17621 }
17622 else if (attr->form_is_constant ())
17623 {
17624 prop->data.const_val = attr->constant_value (0);
17625 prop->kind = PROP_CONST;
17626 }
17627 else
17628 {
17629 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17630 dwarf2_name (die, cu));
17631 return 0;
17632 }
17633
17634 return 1;
17635 }
17636
17637 /* See read.h. */
17638
17639 struct type *
17640 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17641 {
17642 struct type *int_type;
17643
17644 /* Helper macro to examine the various builtin types. */
17645 #define TRY_TYPE(F) \
17646 int_type = (unsigned_p \
17647 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17648 : objfile_type (objfile)->builtin_ ## F); \
17649 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17650 return int_type
17651
17652 TRY_TYPE (char);
17653 TRY_TYPE (short);
17654 TRY_TYPE (int);
17655 TRY_TYPE (long);
17656 TRY_TYPE (long_long);
17657
17658 #undef TRY_TYPE
17659
17660 gdb_assert_not_reached ("unable to find suitable integer type");
17661 }
17662
17663 /* See read.h. */
17664
17665 struct type *
17666 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17667 {
17668 int addr_size = this->per_cu->addr_size ();
17669 return this->per_objfile->int_type (addr_size, unsigned_p);
17670 }
17671
17672 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17673 present (which is valid) then compute the default type based on the
17674 compilation units address size. */
17675
17676 static struct type *
17677 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17678 {
17679 struct type *index_type = die_type (die, cu);
17680
17681 /* Dwarf-2 specifications explicitly allows to create subrange types
17682 without specifying a base type.
17683 In that case, the base type must be set to the type of
17684 the lower bound, upper bound or count, in that order, if any of these
17685 three attributes references an object that has a type.
17686 If no base type is found, the Dwarf-2 specifications say that
17687 a signed integer type of size equal to the size of an address should
17688 be used.
17689 For the following C code: `extern char gdb_int [];'
17690 GCC produces an empty range DIE.
17691 FIXME: muller/2010-05-28: Possible references to object for low bound,
17692 high bound or count are not yet handled by this code. */
17693 if (index_type->code () == TYPE_CODE_VOID)
17694 index_type = cu->addr_sized_int_type (false);
17695
17696 return index_type;
17697 }
17698
17699 /* Read the given DW_AT_subrange DIE. */
17700
17701 static struct type *
17702 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17703 {
17704 struct type *base_type, *orig_base_type;
17705 struct type *range_type;
17706 struct attribute *attr;
17707 struct dynamic_prop low, high;
17708 int low_default_is_valid;
17709 int high_bound_is_count = 0;
17710 const char *name;
17711 ULONGEST negative_mask;
17712
17713 orig_base_type = read_subrange_index_type (die, cu);
17714
17715 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17716 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17717 creating the range type, but we use the result of check_typedef
17718 when examining properties of the type. */
17719 base_type = check_typedef (orig_base_type);
17720
17721 /* The die_type call above may have already set the type for this DIE. */
17722 range_type = get_die_type (die, cu);
17723 if (range_type)
17724 return range_type;
17725
17726 low.kind = PROP_CONST;
17727 high.kind = PROP_CONST;
17728 high.data.const_val = 0;
17729
17730 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17731 omitting DW_AT_lower_bound. */
17732 switch (cu->language)
17733 {
17734 case language_c:
17735 case language_cplus:
17736 low.data.const_val = 0;
17737 low_default_is_valid = 1;
17738 break;
17739 case language_fortran:
17740 low.data.const_val = 1;
17741 low_default_is_valid = 1;
17742 break;
17743 case language_d:
17744 case language_objc:
17745 case language_rust:
17746 low.data.const_val = 0;
17747 low_default_is_valid = (cu->header.version >= 4);
17748 break;
17749 case language_ada:
17750 case language_m2:
17751 case language_pascal:
17752 low.data.const_val = 1;
17753 low_default_is_valid = (cu->header.version >= 4);
17754 break;
17755 default:
17756 low.data.const_val = 0;
17757 low_default_is_valid = 0;
17758 break;
17759 }
17760
17761 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17762 if (attr != nullptr)
17763 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17764 else if (!low_default_is_valid)
17765 complaint (_("Missing DW_AT_lower_bound "
17766 "- DIE at %s [in module %s]"),
17767 sect_offset_str (die->sect_off),
17768 objfile_name (cu->per_objfile->objfile));
17769
17770 struct attribute *attr_ub, *attr_count;
17771 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17772 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17773 {
17774 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17775 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17776 {
17777 /* If bounds are constant do the final calculation here. */
17778 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17779 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17780 else
17781 high_bound_is_count = 1;
17782 }
17783 else
17784 {
17785 if (attr_ub != NULL)
17786 complaint (_("Unresolved DW_AT_upper_bound "
17787 "- DIE at %s [in module %s]"),
17788 sect_offset_str (die->sect_off),
17789 objfile_name (cu->per_objfile->objfile));
17790 if (attr_count != NULL)
17791 complaint (_("Unresolved DW_AT_count "
17792 "- DIE at %s [in module %s]"),
17793 sect_offset_str (die->sect_off),
17794 objfile_name (cu->per_objfile->objfile));
17795 }
17796 }
17797
17798 LONGEST bias = 0;
17799 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17800 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17801 bias = bias_attr->constant_value (0);
17802
17803 /* Normally, the DWARF producers are expected to use a signed
17804 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17805 But this is unfortunately not always the case, as witnessed
17806 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17807 is used instead. To work around that ambiguity, we treat
17808 the bounds as signed, and thus sign-extend their values, when
17809 the base type is signed. */
17810 negative_mask =
17811 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17812 if (low.kind == PROP_CONST
17813 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17814 low.data.const_val |= negative_mask;
17815 if (high.kind == PROP_CONST
17816 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17817 high.data.const_val |= negative_mask;
17818
17819 /* Check for bit and byte strides. */
17820 struct dynamic_prop byte_stride_prop;
17821 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17822 if (attr_byte_stride != nullptr)
17823 {
17824 struct type *prop_type = cu->addr_sized_int_type (false);
17825 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17826 prop_type);
17827 }
17828
17829 struct dynamic_prop bit_stride_prop;
17830 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17831 if (attr_bit_stride != nullptr)
17832 {
17833 /* It only makes sense to have either a bit or byte stride. */
17834 if (attr_byte_stride != nullptr)
17835 {
17836 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17837 "- DIE at %s [in module %s]"),
17838 sect_offset_str (die->sect_off),
17839 objfile_name (cu->per_objfile->objfile));
17840 attr_bit_stride = nullptr;
17841 }
17842 else
17843 {
17844 struct type *prop_type = cu->addr_sized_int_type (false);
17845 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17846 prop_type);
17847 }
17848 }
17849
17850 if (attr_byte_stride != nullptr
17851 || attr_bit_stride != nullptr)
17852 {
17853 bool byte_stride_p = (attr_byte_stride != nullptr);
17854 struct dynamic_prop *stride
17855 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17856
17857 range_type
17858 = create_range_type_with_stride (NULL, orig_base_type, &low,
17859 &high, bias, stride, byte_stride_p);
17860 }
17861 else
17862 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17863
17864 if (high_bound_is_count)
17865 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17866
17867 /* Ada expects an empty array on no boundary attributes. */
17868 if (attr == NULL && cu->language != language_ada)
17869 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17870
17871 name = dwarf2_name (die, cu);
17872 if (name)
17873 range_type->set_name (name);
17874
17875 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17876 if (attr != nullptr)
17877 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17878
17879 maybe_set_alignment (cu, die, range_type);
17880
17881 set_die_type (die, range_type, cu);
17882
17883 /* set_die_type should be already done. */
17884 set_descriptive_type (range_type, die, cu);
17885
17886 return range_type;
17887 }
17888
17889 static struct type *
17890 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17891 {
17892 struct type *type;
17893
17894 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17895 type->set_name (dwarf2_name (die, cu));
17896
17897 /* In Ada, an unspecified type is typically used when the description
17898 of the type is deferred to a different unit. When encountering
17899 such a type, we treat it as a stub, and try to resolve it later on,
17900 when needed. */
17901 if (cu->language == language_ada)
17902 TYPE_STUB (type) = 1;
17903
17904 return set_die_type (die, type, cu);
17905 }
17906
17907 /* Read a single die and all its descendents. Set the die's sibling
17908 field to NULL; set other fields in the die correctly, and set all
17909 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17910 location of the info_ptr after reading all of those dies. PARENT
17911 is the parent of the die in question. */
17912
17913 static struct die_info *
17914 read_die_and_children (const struct die_reader_specs *reader,
17915 const gdb_byte *info_ptr,
17916 const gdb_byte **new_info_ptr,
17917 struct die_info *parent)
17918 {
17919 struct die_info *die;
17920 const gdb_byte *cur_ptr;
17921
17922 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17923 if (die == NULL)
17924 {
17925 *new_info_ptr = cur_ptr;
17926 return NULL;
17927 }
17928 store_in_ref_table (die, reader->cu);
17929
17930 if (die->has_children)
17931 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17932 else
17933 {
17934 die->child = NULL;
17935 *new_info_ptr = cur_ptr;
17936 }
17937
17938 die->sibling = NULL;
17939 die->parent = parent;
17940 return die;
17941 }
17942
17943 /* Read a die, all of its descendents, and all of its siblings; set
17944 all of the fields of all of the dies correctly. Arguments are as
17945 in read_die_and_children. */
17946
17947 static struct die_info *
17948 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17949 const gdb_byte *info_ptr,
17950 const gdb_byte **new_info_ptr,
17951 struct die_info *parent)
17952 {
17953 struct die_info *first_die, *last_sibling;
17954 const gdb_byte *cur_ptr;
17955
17956 cur_ptr = info_ptr;
17957 first_die = last_sibling = NULL;
17958
17959 while (1)
17960 {
17961 struct die_info *die
17962 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17963
17964 if (die == NULL)
17965 {
17966 *new_info_ptr = cur_ptr;
17967 return first_die;
17968 }
17969
17970 if (!first_die)
17971 first_die = die;
17972 else
17973 last_sibling->sibling = die;
17974
17975 last_sibling = die;
17976 }
17977 }
17978
17979 /* Read a die, all of its descendents, and all of its siblings; set
17980 all of the fields of all of the dies correctly. Arguments are as
17981 in read_die_and_children.
17982 This the main entry point for reading a DIE and all its children. */
17983
17984 static struct die_info *
17985 read_die_and_siblings (const struct die_reader_specs *reader,
17986 const gdb_byte *info_ptr,
17987 const gdb_byte **new_info_ptr,
17988 struct die_info *parent)
17989 {
17990 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17991 new_info_ptr, parent);
17992
17993 if (dwarf_die_debug)
17994 {
17995 fprintf_unfiltered (gdb_stdlog,
17996 "Read die from %s@0x%x of %s:\n",
17997 reader->die_section->get_name (),
17998 (unsigned) (info_ptr - reader->die_section->buffer),
17999 bfd_get_filename (reader->abfd));
18000 dump_die (die, dwarf_die_debug);
18001 }
18002
18003 return die;
18004 }
18005
18006 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18007 attributes.
18008 The caller is responsible for filling in the extra attributes
18009 and updating (*DIEP)->num_attrs.
18010 Set DIEP to point to a newly allocated die with its information,
18011 except for its child, sibling, and parent fields. */
18012
18013 static const gdb_byte *
18014 read_full_die_1 (const struct die_reader_specs *reader,
18015 struct die_info **diep, const gdb_byte *info_ptr,
18016 int num_extra_attrs)
18017 {
18018 unsigned int abbrev_number, bytes_read, i;
18019 struct abbrev_info *abbrev;
18020 struct die_info *die;
18021 struct dwarf2_cu *cu = reader->cu;
18022 bfd *abfd = reader->abfd;
18023
18024 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18025 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18026 info_ptr += bytes_read;
18027 if (!abbrev_number)
18028 {
18029 *diep = NULL;
18030 return info_ptr;
18031 }
18032
18033 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18034 if (!abbrev)
18035 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18036 abbrev_number,
18037 bfd_get_filename (abfd));
18038
18039 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18040 die->sect_off = sect_off;
18041 die->tag = abbrev->tag;
18042 die->abbrev = abbrev_number;
18043 die->has_children = abbrev->has_children;
18044
18045 /* Make the result usable.
18046 The caller needs to update num_attrs after adding the extra
18047 attributes. */
18048 die->num_attrs = abbrev->num_attrs;
18049
18050 std::vector<int> indexes_that_need_reprocess;
18051 for (i = 0; i < abbrev->num_attrs; ++i)
18052 {
18053 bool need_reprocess;
18054 info_ptr =
18055 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18056 info_ptr, &need_reprocess);
18057 if (need_reprocess)
18058 indexes_that_need_reprocess.push_back (i);
18059 }
18060
18061 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18062 if (attr != nullptr)
18063 cu->str_offsets_base = DW_UNSND (attr);
18064
18065 attr = die->attr (DW_AT_loclists_base);
18066 if (attr != nullptr)
18067 cu->loclist_base = DW_UNSND (attr);
18068
18069 auto maybe_addr_base = die->addr_base ();
18070 if (maybe_addr_base.has_value ())
18071 cu->addr_base = *maybe_addr_base;
18072 for (int index : indexes_that_need_reprocess)
18073 read_attribute_reprocess (reader, &die->attrs[index]);
18074 *diep = die;
18075 return info_ptr;
18076 }
18077
18078 /* Read a die and all its attributes.
18079 Set DIEP to point to a newly allocated die with its information,
18080 except for its child, sibling, and parent fields. */
18081
18082 static const gdb_byte *
18083 read_full_die (const struct die_reader_specs *reader,
18084 struct die_info **diep, const gdb_byte *info_ptr)
18085 {
18086 const gdb_byte *result;
18087
18088 result = read_full_die_1 (reader, diep, info_ptr, 0);
18089
18090 if (dwarf_die_debug)
18091 {
18092 fprintf_unfiltered (gdb_stdlog,
18093 "Read die from %s@0x%x of %s:\n",
18094 reader->die_section->get_name (),
18095 (unsigned) (info_ptr - reader->die_section->buffer),
18096 bfd_get_filename (reader->abfd));
18097 dump_die (*diep, dwarf_die_debug);
18098 }
18099
18100 return result;
18101 }
18102 \f
18103
18104 /* Returns nonzero if TAG represents a type that we might generate a partial
18105 symbol for. */
18106
18107 static int
18108 is_type_tag_for_partial (int tag)
18109 {
18110 switch (tag)
18111 {
18112 #if 0
18113 /* Some types that would be reasonable to generate partial symbols for,
18114 that we don't at present. */
18115 case DW_TAG_array_type:
18116 case DW_TAG_file_type:
18117 case DW_TAG_ptr_to_member_type:
18118 case DW_TAG_set_type:
18119 case DW_TAG_string_type:
18120 case DW_TAG_subroutine_type:
18121 #endif
18122 case DW_TAG_base_type:
18123 case DW_TAG_class_type:
18124 case DW_TAG_interface_type:
18125 case DW_TAG_enumeration_type:
18126 case DW_TAG_structure_type:
18127 case DW_TAG_subrange_type:
18128 case DW_TAG_typedef:
18129 case DW_TAG_union_type:
18130 return 1;
18131 default:
18132 return 0;
18133 }
18134 }
18135
18136 /* Load all DIEs that are interesting for partial symbols into memory. */
18137
18138 static struct partial_die_info *
18139 load_partial_dies (const struct die_reader_specs *reader,
18140 const gdb_byte *info_ptr, int building_psymtab)
18141 {
18142 struct dwarf2_cu *cu = reader->cu;
18143 struct objfile *objfile = cu->per_objfile->objfile;
18144 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18145 unsigned int bytes_read;
18146 unsigned int load_all = 0;
18147 int nesting_level = 1;
18148
18149 parent_die = NULL;
18150 last_die = NULL;
18151
18152 gdb_assert (cu->per_cu != NULL);
18153 if (cu->per_cu->load_all_dies)
18154 load_all = 1;
18155
18156 cu->partial_dies
18157 = htab_create_alloc_ex (cu->header.length / 12,
18158 partial_die_hash,
18159 partial_die_eq,
18160 NULL,
18161 &cu->comp_unit_obstack,
18162 hashtab_obstack_allocate,
18163 dummy_obstack_deallocate);
18164
18165 while (1)
18166 {
18167 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18168
18169 /* A NULL abbrev means the end of a series of children. */
18170 if (abbrev == NULL)
18171 {
18172 if (--nesting_level == 0)
18173 return first_die;
18174
18175 info_ptr += bytes_read;
18176 last_die = parent_die;
18177 parent_die = parent_die->die_parent;
18178 continue;
18179 }
18180
18181 /* Check for template arguments. We never save these; if
18182 they're seen, we just mark the parent, and go on our way. */
18183 if (parent_die != NULL
18184 && cu->language == language_cplus
18185 && (abbrev->tag == DW_TAG_template_type_param
18186 || abbrev->tag == DW_TAG_template_value_param))
18187 {
18188 parent_die->has_template_arguments = 1;
18189
18190 if (!load_all)
18191 {
18192 /* We don't need a partial DIE for the template argument. */
18193 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18194 continue;
18195 }
18196 }
18197
18198 /* We only recurse into c++ subprograms looking for template arguments.
18199 Skip their other children. */
18200 if (!load_all
18201 && cu->language == language_cplus
18202 && parent_die != NULL
18203 && parent_die->tag == DW_TAG_subprogram)
18204 {
18205 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18206 continue;
18207 }
18208
18209 /* Check whether this DIE is interesting enough to save. Normally
18210 we would not be interested in members here, but there may be
18211 later variables referencing them via DW_AT_specification (for
18212 static members). */
18213 if (!load_all
18214 && !is_type_tag_for_partial (abbrev->tag)
18215 && abbrev->tag != DW_TAG_constant
18216 && abbrev->tag != DW_TAG_enumerator
18217 && abbrev->tag != DW_TAG_subprogram
18218 && abbrev->tag != DW_TAG_inlined_subroutine
18219 && abbrev->tag != DW_TAG_lexical_block
18220 && abbrev->tag != DW_TAG_variable
18221 && abbrev->tag != DW_TAG_namespace
18222 && abbrev->tag != DW_TAG_module
18223 && abbrev->tag != DW_TAG_member
18224 && abbrev->tag != DW_TAG_imported_unit
18225 && abbrev->tag != DW_TAG_imported_declaration)
18226 {
18227 /* Otherwise we skip to the next sibling, if any. */
18228 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18229 continue;
18230 }
18231
18232 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18233 abbrev);
18234
18235 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18236
18237 /* This two-pass algorithm for processing partial symbols has a
18238 high cost in cache pressure. Thus, handle some simple cases
18239 here which cover the majority of C partial symbols. DIEs
18240 which neither have specification tags in them, nor could have
18241 specification tags elsewhere pointing at them, can simply be
18242 processed and discarded.
18243
18244 This segment is also optional; scan_partial_symbols and
18245 add_partial_symbol will handle these DIEs if we chain
18246 them in normally. When compilers which do not emit large
18247 quantities of duplicate debug information are more common,
18248 this code can probably be removed. */
18249
18250 /* Any complete simple types at the top level (pretty much all
18251 of them, for a language without namespaces), can be processed
18252 directly. */
18253 if (parent_die == NULL
18254 && pdi.has_specification == 0
18255 && pdi.is_declaration == 0
18256 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18257 || pdi.tag == DW_TAG_base_type
18258 || pdi.tag == DW_TAG_subrange_type))
18259 {
18260 if (building_psymtab && pdi.name != NULL)
18261 add_psymbol_to_list (pdi.name, false,
18262 VAR_DOMAIN, LOC_TYPEDEF, -1,
18263 psymbol_placement::STATIC,
18264 0, cu->language, objfile);
18265 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18266 continue;
18267 }
18268
18269 /* The exception for DW_TAG_typedef with has_children above is
18270 a workaround of GCC PR debug/47510. In the case of this complaint
18271 type_name_or_error will error on such types later.
18272
18273 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18274 it could not find the child DIEs referenced later, this is checked
18275 above. In correct DWARF DW_TAG_typedef should have no children. */
18276
18277 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18278 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18279 "- DIE at %s [in module %s]"),
18280 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18281
18282 /* If we're at the second level, and we're an enumerator, and
18283 our parent has no specification (meaning possibly lives in a
18284 namespace elsewhere), then we can add the partial symbol now
18285 instead of queueing it. */
18286 if (pdi.tag == DW_TAG_enumerator
18287 && parent_die != NULL
18288 && parent_die->die_parent == NULL
18289 && parent_die->tag == DW_TAG_enumeration_type
18290 && parent_die->has_specification == 0)
18291 {
18292 if (pdi.name == NULL)
18293 complaint (_("malformed enumerator DIE ignored"));
18294 else if (building_psymtab)
18295 add_psymbol_to_list (pdi.name, false,
18296 VAR_DOMAIN, LOC_CONST, -1,
18297 cu->language == language_cplus
18298 ? psymbol_placement::GLOBAL
18299 : psymbol_placement::STATIC,
18300 0, cu->language, objfile);
18301
18302 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18303 continue;
18304 }
18305
18306 struct partial_die_info *part_die
18307 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18308
18309 /* We'll save this DIE so link it in. */
18310 part_die->die_parent = parent_die;
18311 part_die->die_sibling = NULL;
18312 part_die->die_child = NULL;
18313
18314 if (last_die && last_die == parent_die)
18315 last_die->die_child = part_die;
18316 else if (last_die)
18317 last_die->die_sibling = part_die;
18318
18319 last_die = part_die;
18320
18321 if (first_die == NULL)
18322 first_die = part_die;
18323
18324 /* Maybe add the DIE to the hash table. Not all DIEs that we
18325 find interesting need to be in the hash table, because we
18326 also have the parent/sibling/child chains; only those that we
18327 might refer to by offset later during partial symbol reading.
18328
18329 For now this means things that might have be the target of a
18330 DW_AT_specification, DW_AT_abstract_origin, or
18331 DW_AT_extension. DW_AT_extension will refer only to
18332 namespaces; DW_AT_abstract_origin refers to functions (and
18333 many things under the function DIE, but we do not recurse
18334 into function DIEs during partial symbol reading) and
18335 possibly variables as well; DW_AT_specification refers to
18336 declarations. Declarations ought to have the DW_AT_declaration
18337 flag. It happens that GCC forgets to put it in sometimes, but
18338 only for functions, not for types.
18339
18340 Adding more things than necessary to the hash table is harmless
18341 except for the performance cost. Adding too few will result in
18342 wasted time in find_partial_die, when we reread the compilation
18343 unit with load_all_dies set. */
18344
18345 if (load_all
18346 || abbrev->tag == DW_TAG_constant
18347 || abbrev->tag == DW_TAG_subprogram
18348 || abbrev->tag == DW_TAG_variable
18349 || abbrev->tag == DW_TAG_namespace
18350 || part_die->is_declaration)
18351 {
18352 void **slot;
18353
18354 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18355 to_underlying (part_die->sect_off),
18356 INSERT);
18357 *slot = part_die;
18358 }
18359
18360 /* For some DIEs we want to follow their children (if any). For C
18361 we have no reason to follow the children of structures; for other
18362 languages we have to, so that we can get at method physnames
18363 to infer fully qualified class names, for DW_AT_specification,
18364 and for C++ template arguments. For C++, we also look one level
18365 inside functions to find template arguments (if the name of the
18366 function does not already contain the template arguments).
18367
18368 For Ada and Fortran, we need to scan the children of subprograms
18369 and lexical blocks as well because these languages allow the
18370 definition of nested entities that could be interesting for the
18371 debugger, such as nested subprograms for instance. */
18372 if (last_die->has_children
18373 && (load_all
18374 || last_die->tag == DW_TAG_namespace
18375 || last_die->tag == DW_TAG_module
18376 || last_die->tag == DW_TAG_enumeration_type
18377 || (cu->language == language_cplus
18378 && last_die->tag == DW_TAG_subprogram
18379 && (last_die->name == NULL
18380 || strchr (last_die->name, '<') == NULL))
18381 || (cu->language != language_c
18382 && (last_die->tag == DW_TAG_class_type
18383 || last_die->tag == DW_TAG_interface_type
18384 || last_die->tag == DW_TAG_structure_type
18385 || last_die->tag == DW_TAG_union_type))
18386 || ((cu->language == language_ada
18387 || cu->language == language_fortran)
18388 && (last_die->tag == DW_TAG_subprogram
18389 || last_die->tag == DW_TAG_lexical_block))))
18390 {
18391 nesting_level++;
18392 parent_die = last_die;
18393 continue;
18394 }
18395
18396 /* Otherwise we skip to the next sibling, if any. */
18397 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18398
18399 /* Back to the top, do it again. */
18400 }
18401 }
18402
18403 partial_die_info::partial_die_info (sect_offset sect_off_,
18404 struct abbrev_info *abbrev)
18405 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18406 {
18407 }
18408
18409 /* Read a minimal amount of information into the minimal die structure.
18410 INFO_PTR should point just after the initial uleb128 of a DIE. */
18411
18412 const gdb_byte *
18413 partial_die_info::read (const struct die_reader_specs *reader,
18414 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18415 {
18416 struct dwarf2_cu *cu = reader->cu;
18417 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18418 unsigned int i;
18419 int has_low_pc_attr = 0;
18420 int has_high_pc_attr = 0;
18421 int high_pc_relative = 0;
18422
18423 for (i = 0; i < abbrev.num_attrs; ++i)
18424 {
18425 attribute attr;
18426 bool need_reprocess;
18427 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18428 info_ptr, &need_reprocess);
18429 /* String and address offsets that need to do the reprocessing have
18430 already been read at this point, so there is no need to wait until
18431 the loop terminates to do the reprocessing. */
18432 if (need_reprocess)
18433 read_attribute_reprocess (reader, &attr);
18434 /* Store the data if it is of an attribute we want to keep in a
18435 partial symbol table. */
18436 switch (attr.name)
18437 {
18438 case DW_AT_name:
18439 switch (tag)
18440 {
18441 case DW_TAG_compile_unit:
18442 case DW_TAG_partial_unit:
18443 case DW_TAG_type_unit:
18444 /* Compilation units have a DW_AT_name that is a filename, not
18445 a source language identifier. */
18446 case DW_TAG_enumeration_type:
18447 case DW_TAG_enumerator:
18448 /* These tags always have simple identifiers already; no need
18449 to canonicalize them. */
18450 name = DW_STRING (&attr);
18451 break;
18452 default:
18453 {
18454 struct objfile *objfile = dwarf2_per_objfile->objfile;
18455
18456 name
18457 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18458 }
18459 break;
18460 }
18461 break;
18462 case DW_AT_linkage_name:
18463 case DW_AT_MIPS_linkage_name:
18464 /* Note that both forms of linkage name might appear. We
18465 assume they will be the same, and we only store the last
18466 one we see. */
18467 linkage_name = attr.value_as_string ();
18468 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18469 See https://github.com/rust-lang/rust/issues/32925. */
18470 if (cu->language == language_rust && linkage_name != NULL
18471 && strchr (linkage_name, '{') != NULL)
18472 linkage_name = NULL;
18473 break;
18474 case DW_AT_low_pc:
18475 has_low_pc_attr = 1;
18476 lowpc = attr.value_as_address ();
18477 break;
18478 case DW_AT_high_pc:
18479 has_high_pc_attr = 1;
18480 highpc = attr.value_as_address ();
18481 if (cu->header.version >= 4 && attr.form_is_constant ())
18482 high_pc_relative = 1;
18483 break;
18484 case DW_AT_location:
18485 /* Support the .debug_loc offsets. */
18486 if (attr.form_is_block ())
18487 {
18488 d.locdesc = DW_BLOCK (&attr);
18489 }
18490 else if (attr.form_is_section_offset ())
18491 {
18492 dwarf2_complex_location_expr_complaint ();
18493 }
18494 else
18495 {
18496 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18497 "partial symbol information");
18498 }
18499 break;
18500 case DW_AT_external:
18501 is_external = DW_UNSND (&attr);
18502 break;
18503 case DW_AT_declaration:
18504 is_declaration = DW_UNSND (&attr);
18505 break;
18506 case DW_AT_type:
18507 has_type = 1;
18508 break;
18509 case DW_AT_abstract_origin:
18510 case DW_AT_specification:
18511 case DW_AT_extension:
18512 has_specification = 1;
18513 spec_offset = attr.get_ref_die_offset ();
18514 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18515 || cu->per_cu->is_dwz);
18516 break;
18517 case DW_AT_sibling:
18518 /* Ignore absolute siblings, they might point outside of
18519 the current compile unit. */
18520 if (attr.form == DW_FORM_ref_addr)
18521 complaint (_("ignoring absolute DW_AT_sibling"));
18522 else
18523 {
18524 const gdb_byte *buffer = reader->buffer;
18525 sect_offset off = attr.get_ref_die_offset ();
18526 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18527
18528 if (sibling_ptr < info_ptr)
18529 complaint (_("DW_AT_sibling points backwards"));
18530 else if (sibling_ptr > reader->buffer_end)
18531 reader->die_section->overflow_complaint ();
18532 else
18533 sibling = sibling_ptr;
18534 }
18535 break;
18536 case DW_AT_byte_size:
18537 has_byte_size = 1;
18538 break;
18539 case DW_AT_const_value:
18540 has_const_value = 1;
18541 break;
18542 case DW_AT_calling_convention:
18543 /* DWARF doesn't provide a way to identify a program's source-level
18544 entry point. DW_AT_calling_convention attributes are only meant
18545 to describe functions' calling conventions.
18546
18547 However, because it's a necessary piece of information in
18548 Fortran, and before DWARF 4 DW_CC_program was the only
18549 piece of debugging information whose definition refers to
18550 a 'main program' at all, several compilers marked Fortran
18551 main programs with DW_CC_program --- even when those
18552 functions use the standard calling conventions.
18553
18554 Although DWARF now specifies a way to provide this
18555 information, we support this practice for backward
18556 compatibility. */
18557 if (DW_UNSND (&attr) == DW_CC_program
18558 && cu->language == language_fortran)
18559 main_subprogram = 1;
18560 break;
18561 case DW_AT_inline:
18562 if (DW_UNSND (&attr) == DW_INL_inlined
18563 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18564 may_be_inlined = 1;
18565 break;
18566
18567 case DW_AT_import:
18568 if (tag == DW_TAG_imported_unit)
18569 {
18570 d.sect_off = attr.get_ref_die_offset ();
18571 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18572 || cu->per_cu->is_dwz);
18573 }
18574 break;
18575
18576 case DW_AT_main_subprogram:
18577 main_subprogram = DW_UNSND (&attr);
18578 break;
18579
18580 case DW_AT_ranges:
18581 {
18582 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18583 but that requires a full DIE, so instead we just
18584 reimplement it. */
18585 int need_ranges_base = tag != DW_TAG_compile_unit;
18586 unsigned int ranges_offset = (DW_UNSND (&attr)
18587 + (need_ranges_base
18588 ? cu->ranges_base
18589 : 0));
18590
18591 /* Value of the DW_AT_ranges attribute is the offset in the
18592 .debug_ranges section. */
18593 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18594 nullptr))
18595 has_pc_info = 1;
18596 }
18597 break;
18598
18599 default:
18600 break;
18601 }
18602 }
18603
18604 /* For Ada, if both the name and the linkage name appear, we prefer
18605 the latter. This lets "catch exception" work better, regardless
18606 of the order in which the name and linkage name were emitted.
18607 Really, though, this is just a workaround for the fact that gdb
18608 doesn't store both the name and the linkage name. */
18609 if (cu->language == language_ada && linkage_name != nullptr)
18610 name = linkage_name;
18611
18612 if (high_pc_relative)
18613 highpc += lowpc;
18614
18615 if (has_low_pc_attr && has_high_pc_attr)
18616 {
18617 /* When using the GNU linker, .gnu.linkonce. sections are used to
18618 eliminate duplicate copies of functions and vtables and such.
18619 The linker will arbitrarily choose one and discard the others.
18620 The AT_*_pc values for such functions refer to local labels in
18621 these sections. If the section from that file was discarded, the
18622 labels are not in the output, so the relocs get a value of 0.
18623 If this is a discarded function, mark the pc bounds as invalid,
18624 so that GDB will ignore it. */
18625 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18626 {
18627 struct objfile *objfile = dwarf2_per_objfile->objfile;
18628 struct gdbarch *gdbarch = objfile->arch ();
18629
18630 complaint (_("DW_AT_low_pc %s is zero "
18631 "for DIE at %s [in module %s]"),
18632 paddress (gdbarch, lowpc),
18633 sect_offset_str (sect_off),
18634 objfile_name (objfile));
18635 }
18636 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18637 else if (lowpc >= highpc)
18638 {
18639 struct objfile *objfile = dwarf2_per_objfile->objfile;
18640 struct gdbarch *gdbarch = objfile->arch ();
18641
18642 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18643 "for DIE at %s [in module %s]"),
18644 paddress (gdbarch, lowpc),
18645 paddress (gdbarch, highpc),
18646 sect_offset_str (sect_off),
18647 objfile_name (objfile));
18648 }
18649 else
18650 has_pc_info = 1;
18651 }
18652
18653 return info_ptr;
18654 }
18655
18656 /* Find a cached partial DIE at OFFSET in CU. */
18657
18658 struct partial_die_info *
18659 dwarf2_cu::find_partial_die (sect_offset sect_off)
18660 {
18661 struct partial_die_info *lookup_die = NULL;
18662 struct partial_die_info part_die (sect_off);
18663
18664 lookup_die = ((struct partial_die_info *)
18665 htab_find_with_hash (partial_dies, &part_die,
18666 to_underlying (sect_off)));
18667
18668 return lookup_die;
18669 }
18670
18671 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18672 except in the case of .debug_types DIEs which do not reference
18673 outside their CU (they do however referencing other types via
18674 DW_FORM_ref_sig8). */
18675
18676 static const struct cu_partial_die_info
18677 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18678 {
18679 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18680 struct objfile *objfile = dwarf2_per_objfile->objfile;
18681 struct dwarf2_per_cu_data *per_cu = NULL;
18682 struct partial_die_info *pd = NULL;
18683
18684 if (offset_in_dwz == cu->per_cu->is_dwz
18685 && cu->header.offset_in_cu_p (sect_off))
18686 {
18687 pd = cu->find_partial_die (sect_off);
18688 if (pd != NULL)
18689 return { cu, pd };
18690 /* We missed recording what we needed.
18691 Load all dies and try again. */
18692 per_cu = cu->per_cu;
18693 }
18694 else
18695 {
18696 /* TUs don't reference other CUs/TUs (except via type signatures). */
18697 if (cu->per_cu->is_debug_types)
18698 {
18699 error (_("Dwarf Error: Type Unit at offset %s contains"
18700 " external reference to offset %s [in module %s].\n"),
18701 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18702 bfd_get_filename (objfile->obfd));
18703 }
18704 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18705 dwarf2_per_objfile);
18706
18707 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18708 load_partial_comp_unit (per_cu, cu->per_objfile);
18709
18710 per_cu->cu->last_used = 0;
18711 pd = per_cu->cu->find_partial_die (sect_off);
18712 }
18713
18714 /* If we didn't find it, and not all dies have been loaded,
18715 load them all and try again. */
18716
18717 if (pd == NULL && per_cu->load_all_dies == 0)
18718 {
18719 per_cu->load_all_dies = 1;
18720
18721 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18722 THIS_CU->cu may already be in use. So we can't just free it and
18723 replace its DIEs with the ones we read in. Instead, we leave those
18724 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18725 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18726 set. */
18727 load_partial_comp_unit (per_cu, cu->per_objfile);
18728
18729 pd = per_cu->cu->find_partial_die (sect_off);
18730 }
18731
18732 if (pd == NULL)
18733 internal_error (__FILE__, __LINE__,
18734 _("could not find partial DIE %s "
18735 "in cache [from module %s]\n"),
18736 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18737 return { per_cu->cu, pd };
18738 }
18739
18740 /* See if we can figure out if the class lives in a namespace. We do
18741 this by looking for a member function; its demangled name will
18742 contain namespace info, if there is any. */
18743
18744 static void
18745 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18746 struct dwarf2_cu *cu)
18747 {
18748 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18749 what template types look like, because the demangler
18750 frequently doesn't give the same name as the debug info. We
18751 could fix this by only using the demangled name to get the
18752 prefix (but see comment in read_structure_type). */
18753
18754 struct partial_die_info *real_pdi;
18755 struct partial_die_info *child_pdi;
18756
18757 /* If this DIE (this DIE's specification, if any) has a parent, then
18758 we should not do this. We'll prepend the parent's fully qualified
18759 name when we create the partial symbol. */
18760
18761 real_pdi = struct_pdi;
18762 while (real_pdi->has_specification)
18763 {
18764 auto res = find_partial_die (real_pdi->spec_offset,
18765 real_pdi->spec_is_dwz, cu);
18766 real_pdi = res.pdi;
18767 cu = res.cu;
18768 }
18769
18770 if (real_pdi->die_parent != NULL)
18771 return;
18772
18773 for (child_pdi = struct_pdi->die_child;
18774 child_pdi != NULL;
18775 child_pdi = child_pdi->die_sibling)
18776 {
18777 if (child_pdi->tag == DW_TAG_subprogram
18778 && child_pdi->linkage_name != NULL)
18779 {
18780 gdb::unique_xmalloc_ptr<char> actual_class_name
18781 (language_class_name_from_physname (cu->language_defn,
18782 child_pdi->linkage_name));
18783 if (actual_class_name != NULL)
18784 {
18785 struct objfile *objfile = cu->per_objfile->objfile;
18786 struct_pdi->name = objfile->intern (actual_class_name.get ());
18787 }
18788 break;
18789 }
18790 }
18791 }
18792
18793 /* Return true if a DIE with TAG may have the DW_AT_const_value
18794 attribute. */
18795
18796 static bool
18797 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18798 {
18799 switch (tag)
18800 {
18801 case DW_TAG_constant:
18802 case DW_TAG_enumerator:
18803 case DW_TAG_formal_parameter:
18804 case DW_TAG_template_value_param:
18805 case DW_TAG_variable:
18806 return true;
18807 }
18808
18809 return false;
18810 }
18811
18812 void
18813 partial_die_info::fixup (struct dwarf2_cu *cu)
18814 {
18815 /* Once we've fixed up a die, there's no point in doing so again.
18816 This also avoids a memory leak if we were to call
18817 guess_partial_die_structure_name multiple times. */
18818 if (fixup_called)
18819 return;
18820
18821 /* If we found a reference attribute and the DIE has no name, try
18822 to find a name in the referred to DIE. */
18823
18824 if (name == NULL && has_specification)
18825 {
18826 struct partial_die_info *spec_die;
18827
18828 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18829 spec_die = res.pdi;
18830 cu = res.cu;
18831
18832 spec_die->fixup (cu);
18833
18834 if (spec_die->name)
18835 {
18836 name = spec_die->name;
18837
18838 /* Copy DW_AT_external attribute if it is set. */
18839 if (spec_die->is_external)
18840 is_external = spec_die->is_external;
18841 }
18842 }
18843
18844 if (!has_const_value && has_specification
18845 && can_have_DW_AT_const_value_p (tag))
18846 {
18847 struct partial_die_info *spec_die;
18848
18849 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18850 spec_die = res.pdi;
18851 cu = res.cu;
18852
18853 spec_die->fixup (cu);
18854
18855 if (spec_die->has_const_value)
18856 {
18857 /* Copy DW_AT_const_value attribute if it is set. */
18858 has_const_value = spec_die->has_const_value;
18859 }
18860 }
18861
18862 /* Set default names for some unnamed DIEs. */
18863
18864 if (name == NULL && tag == DW_TAG_namespace)
18865 name = CP_ANONYMOUS_NAMESPACE_STR;
18866
18867 /* If there is no parent die to provide a namespace, and there are
18868 children, see if we can determine the namespace from their linkage
18869 name. */
18870 if (cu->language == language_cplus
18871 && !cu->per_objfile->per_bfd->types.empty ()
18872 && die_parent == NULL
18873 && has_children
18874 && (tag == DW_TAG_class_type
18875 || tag == DW_TAG_structure_type
18876 || tag == DW_TAG_union_type))
18877 guess_partial_die_structure_name (this, cu);
18878
18879 /* GCC might emit a nameless struct or union that has a linkage
18880 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18881 if (name == NULL
18882 && (tag == DW_TAG_class_type
18883 || tag == DW_TAG_interface_type
18884 || tag == DW_TAG_structure_type
18885 || tag == DW_TAG_union_type)
18886 && linkage_name != NULL)
18887 {
18888 gdb::unique_xmalloc_ptr<char> demangled
18889 (gdb_demangle (linkage_name, DMGL_TYPES));
18890 if (demangled != nullptr)
18891 {
18892 const char *base;
18893
18894 /* Strip any leading namespaces/classes, keep only the base name.
18895 DW_AT_name for named DIEs does not contain the prefixes. */
18896 base = strrchr (demangled.get (), ':');
18897 if (base && base > demangled.get () && base[-1] == ':')
18898 base++;
18899 else
18900 base = demangled.get ();
18901
18902 struct objfile *objfile = cu->per_objfile->objfile;
18903 name = objfile->intern (base);
18904 }
18905 }
18906
18907 fixup_called = 1;
18908 }
18909
18910 /* Read the .debug_loclists header contents from the given SECTION in the
18911 HEADER. */
18912 static void
18913 read_loclist_header (struct loclist_header *header,
18914 struct dwarf2_section_info *section)
18915 {
18916 unsigned int bytes_read;
18917 bfd *abfd = section->get_bfd_owner ();
18918 const gdb_byte *info_ptr = section->buffer;
18919 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18920 info_ptr += bytes_read;
18921 header->version = read_2_bytes (abfd, info_ptr);
18922 info_ptr += 2;
18923 header->addr_size = read_1_byte (abfd, info_ptr);
18924 info_ptr += 1;
18925 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18926 info_ptr += 1;
18927 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18928 }
18929
18930 /* Return the DW_AT_loclists_base value for the CU. */
18931 static ULONGEST
18932 lookup_loclist_base (struct dwarf2_cu *cu)
18933 {
18934 /* For the .dwo unit, the loclist_base points to the first offset following
18935 the header. The header consists of the following entities-
18936 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18937 bit format)
18938 2. version (2 bytes)
18939 3. address size (1 byte)
18940 4. segment selector size (1 byte)
18941 5. offset entry count (4 bytes)
18942 These sizes are derived as per the DWARFv5 standard. */
18943 if (cu->dwo_unit != nullptr)
18944 {
18945 if (cu->header.initial_length_size == 4)
18946 return LOCLIST_HEADER_SIZE32;
18947 return LOCLIST_HEADER_SIZE64;
18948 }
18949 return cu->loclist_base;
18950 }
18951
18952 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18953 array of offsets in the .debug_loclists section. */
18954 static CORE_ADDR
18955 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18956 {
18957 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18958 struct objfile *objfile = dwarf2_per_objfile->objfile;
18959 bfd *abfd = objfile->obfd;
18960 ULONGEST loclist_base = lookup_loclist_base (cu);
18961 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18962
18963 section->read (objfile);
18964 if (section->buffer == NULL)
18965 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18966 "section [in module %s]"), objfile_name (objfile));
18967 struct loclist_header header;
18968 read_loclist_header (&header, section);
18969 if (loclist_index >= header.offset_entry_count)
18970 complaint (_("DW_FORM_loclistx pointing outside of "
18971 ".debug_loclists offset array [in module %s]"),
18972 objfile_name (objfile));
18973 if (loclist_base + loclist_index * cu->header.offset_size
18974 >= section->size)
18975 complaint (_("DW_FORM_loclistx pointing outside of "
18976 ".debug_loclists section [in module %s]"),
18977 objfile_name (objfile));
18978 const gdb_byte *info_ptr
18979 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18980
18981 if (cu->header.offset_size == 4)
18982 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18983 else
18984 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18985 }
18986
18987 /* Process the attributes that had to be skipped in the first round. These
18988 attributes are the ones that need str_offsets_base or addr_base attributes.
18989 They could not have been processed in the first round, because at the time
18990 the values of str_offsets_base or addr_base may not have been known. */
18991 static void
18992 read_attribute_reprocess (const struct die_reader_specs *reader,
18993 struct attribute *attr)
18994 {
18995 struct dwarf2_cu *cu = reader->cu;
18996 switch (attr->form)
18997 {
18998 case DW_FORM_addrx:
18999 case DW_FORM_GNU_addr_index:
19000 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19001 break;
19002 case DW_FORM_loclistx:
19003 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19004 break;
19005 case DW_FORM_strx:
19006 case DW_FORM_strx1:
19007 case DW_FORM_strx2:
19008 case DW_FORM_strx3:
19009 case DW_FORM_strx4:
19010 case DW_FORM_GNU_str_index:
19011 {
19012 unsigned int str_index = DW_UNSND (attr);
19013 if (reader->dwo_file != NULL)
19014 {
19015 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19016 DW_STRING_IS_CANONICAL (attr) = 0;
19017 }
19018 else
19019 {
19020 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19021 DW_STRING_IS_CANONICAL (attr) = 0;
19022 }
19023 break;
19024 }
19025 default:
19026 gdb_assert_not_reached (_("Unexpected DWARF form."));
19027 }
19028 }
19029
19030 /* Read an attribute value described by an attribute form. */
19031
19032 static const gdb_byte *
19033 read_attribute_value (const struct die_reader_specs *reader,
19034 struct attribute *attr, unsigned form,
19035 LONGEST implicit_const, const gdb_byte *info_ptr,
19036 bool *need_reprocess)
19037 {
19038 struct dwarf2_cu *cu = reader->cu;
19039 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19040 struct objfile *objfile = dwarf2_per_objfile->objfile;
19041 bfd *abfd = reader->abfd;
19042 struct comp_unit_head *cu_header = &cu->header;
19043 unsigned int bytes_read;
19044 struct dwarf_block *blk;
19045 *need_reprocess = false;
19046
19047 attr->form = (enum dwarf_form) form;
19048 switch (form)
19049 {
19050 case DW_FORM_ref_addr:
19051 if (cu->header.version == 2)
19052 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19053 &bytes_read);
19054 else
19055 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19056 &bytes_read);
19057 info_ptr += bytes_read;
19058 break;
19059 case DW_FORM_GNU_ref_alt:
19060 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19061 info_ptr += bytes_read;
19062 break;
19063 case DW_FORM_addr:
19064 {
19065 struct gdbarch *gdbarch = objfile->arch ();
19066 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19067 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19068 info_ptr += bytes_read;
19069 }
19070 break;
19071 case DW_FORM_block2:
19072 blk = dwarf_alloc_block (cu);
19073 blk->size = read_2_bytes (abfd, info_ptr);
19074 info_ptr += 2;
19075 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19076 info_ptr += blk->size;
19077 DW_BLOCK (attr) = blk;
19078 break;
19079 case DW_FORM_block4:
19080 blk = dwarf_alloc_block (cu);
19081 blk->size = read_4_bytes (abfd, info_ptr);
19082 info_ptr += 4;
19083 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19084 info_ptr += blk->size;
19085 DW_BLOCK (attr) = blk;
19086 break;
19087 case DW_FORM_data2:
19088 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19089 info_ptr += 2;
19090 break;
19091 case DW_FORM_data4:
19092 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19093 info_ptr += 4;
19094 break;
19095 case DW_FORM_data8:
19096 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19097 info_ptr += 8;
19098 break;
19099 case DW_FORM_data16:
19100 blk = dwarf_alloc_block (cu);
19101 blk->size = 16;
19102 blk->data = read_n_bytes (abfd, info_ptr, 16);
19103 info_ptr += 16;
19104 DW_BLOCK (attr) = blk;
19105 break;
19106 case DW_FORM_sec_offset:
19107 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19108 info_ptr += bytes_read;
19109 break;
19110 case DW_FORM_loclistx:
19111 {
19112 *need_reprocess = true;
19113 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19114 info_ptr += bytes_read;
19115 }
19116 break;
19117 case DW_FORM_string:
19118 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19119 DW_STRING_IS_CANONICAL (attr) = 0;
19120 info_ptr += bytes_read;
19121 break;
19122 case DW_FORM_strp:
19123 if (!cu->per_cu->is_dwz)
19124 {
19125 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19126 abfd, info_ptr, cu_header,
19127 &bytes_read);
19128 DW_STRING_IS_CANONICAL (attr) = 0;
19129 info_ptr += bytes_read;
19130 break;
19131 }
19132 /* FALLTHROUGH */
19133 case DW_FORM_line_strp:
19134 if (!cu->per_cu->is_dwz)
19135 {
19136 DW_STRING (attr)
19137 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19138 &bytes_read);
19139 DW_STRING_IS_CANONICAL (attr) = 0;
19140 info_ptr += bytes_read;
19141 break;
19142 }
19143 /* FALLTHROUGH */
19144 case DW_FORM_GNU_strp_alt:
19145 {
19146 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19147 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19148 &bytes_read);
19149
19150 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19151 DW_STRING_IS_CANONICAL (attr) = 0;
19152 info_ptr += bytes_read;
19153 }
19154 break;
19155 case DW_FORM_exprloc:
19156 case DW_FORM_block:
19157 blk = dwarf_alloc_block (cu);
19158 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19159 info_ptr += bytes_read;
19160 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19161 info_ptr += blk->size;
19162 DW_BLOCK (attr) = blk;
19163 break;
19164 case DW_FORM_block1:
19165 blk = dwarf_alloc_block (cu);
19166 blk->size = read_1_byte (abfd, info_ptr);
19167 info_ptr += 1;
19168 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19169 info_ptr += blk->size;
19170 DW_BLOCK (attr) = blk;
19171 break;
19172 case DW_FORM_data1:
19173 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19174 info_ptr += 1;
19175 break;
19176 case DW_FORM_flag:
19177 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19178 info_ptr += 1;
19179 break;
19180 case DW_FORM_flag_present:
19181 DW_UNSND (attr) = 1;
19182 break;
19183 case DW_FORM_sdata:
19184 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19185 info_ptr += bytes_read;
19186 break;
19187 case DW_FORM_udata:
19188 case DW_FORM_rnglistx:
19189 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19190 info_ptr += bytes_read;
19191 break;
19192 case DW_FORM_ref1:
19193 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19194 + read_1_byte (abfd, info_ptr));
19195 info_ptr += 1;
19196 break;
19197 case DW_FORM_ref2:
19198 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19199 + read_2_bytes (abfd, info_ptr));
19200 info_ptr += 2;
19201 break;
19202 case DW_FORM_ref4:
19203 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19204 + read_4_bytes (abfd, info_ptr));
19205 info_ptr += 4;
19206 break;
19207 case DW_FORM_ref8:
19208 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19209 + read_8_bytes (abfd, info_ptr));
19210 info_ptr += 8;
19211 break;
19212 case DW_FORM_ref_sig8:
19213 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19214 info_ptr += 8;
19215 break;
19216 case DW_FORM_ref_udata:
19217 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19218 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19219 info_ptr += bytes_read;
19220 break;
19221 case DW_FORM_indirect:
19222 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19223 info_ptr += bytes_read;
19224 if (form == DW_FORM_implicit_const)
19225 {
19226 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19227 info_ptr += bytes_read;
19228 }
19229 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19230 info_ptr, need_reprocess);
19231 break;
19232 case DW_FORM_implicit_const:
19233 DW_SND (attr) = implicit_const;
19234 break;
19235 case DW_FORM_addrx:
19236 case DW_FORM_GNU_addr_index:
19237 *need_reprocess = true;
19238 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19239 info_ptr += bytes_read;
19240 break;
19241 case DW_FORM_strx:
19242 case DW_FORM_strx1:
19243 case DW_FORM_strx2:
19244 case DW_FORM_strx3:
19245 case DW_FORM_strx4:
19246 case DW_FORM_GNU_str_index:
19247 {
19248 ULONGEST str_index;
19249 if (form == DW_FORM_strx1)
19250 {
19251 str_index = read_1_byte (abfd, info_ptr);
19252 info_ptr += 1;
19253 }
19254 else if (form == DW_FORM_strx2)
19255 {
19256 str_index = read_2_bytes (abfd, info_ptr);
19257 info_ptr += 2;
19258 }
19259 else if (form == DW_FORM_strx3)
19260 {
19261 str_index = read_3_bytes (abfd, info_ptr);
19262 info_ptr += 3;
19263 }
19264 else if (form == DW_FORM_strx4)
19265 {
19266 str_index = read_4_bytes (abfd, info_ptr);
19267 info_ptr += 4;
19268 }
19269 else
19270 {
19271 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19272 info_ptr += bytes_read;
19273 }
19274 *need_reprocess = true;
19275 DW_UNSND (attr) = str_index;
19276 }
19277 break;
19278 default:
19279 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19280 dwarf_form_name (form),
19281 bfd_get_filename (abfd));
19282 }
19283
19284 /* Super hack. */
19285 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19286 attr->form = DW_FORM_GNU_ref_alt;
19287
19288 /* We have seen instances where the compiler tried to emit a byte
19289 size attribute of -1 which ended up being encoded as an unsigned
19290 0xffffffff. Although 0xffffffff is technically a valid size value,
19291 an object of this size seems pretty unlikely so we can relatively
19292 safely treat these cases as if the size attribute was invalid and
19293 treat them as zero by default. */
19294 if (attr->name == DW_AT_byte_size
19295 && form == DW_FORM_data4
19296 && DW_UNSND (attr) >= 0xffffffff)
19297 {
19298 complaint
19299 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19300 hex_string (DW_UNSND (attr)));
19301 DW_UNSND (attr) = 0;
19302 }
19303
19304 return info_ptr;
19305 }
19306
19307 /* Read an attribute described by an abbreviated attribute. */
19308
19309 static const gdb_byte *
19310 read_attribute (const struct die_reader_specs *reader,
19311 struct attribute *attr, struct attr_abbrev *abbrev,
19312 const gdb_byte *info_ptr, bool *need_reprocess)
19313 {
19314 attr->name = abbrev->name;
19315 return read_attribute_value (reader, attr, abbrev->form,
19316 abbrev->implicit_const, info_ptr,
19317 need_reprocess);
19318 }
19319
19320 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19321
19322 static const char *
19323 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19324 LONGEST str_offset)
19325 {
19326 return dwarf2_per_objfile->per_bfd->str.read_string
19327 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19328 }
19329
19330 /* Return pointer to string at .debug_str offset as read from BUF.
19331 BUF is assumed to be in a compilation unit described by CU_HEADER.
19332 Return *BYTES_READ_PTR count of bytes read from BUF. */
19333
19334 static const char *
19335 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19336 const gdb_byte *buf,
19337 const struct comp_unit_head *cu_header,
19338 unsigned int *bytes_read_ptr)
19339 {
19340 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19341
19342 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19343 }
19344
19345 /* See read.h. */
19346
19347 const char *
19348 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19349 const struct comp_unit_head *cu_header,
19350 unsigned int *bytes_read_ptr)
19351 {
19352 bfd *abfd = objfile->obfd;
19353 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19354
19355 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19356 }
19357
19358 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19359 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19360 ADDR_SIZE is the size of addresses from the CU header. */
19361
19362 static CORE_ADDR
19363 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19364 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19365 int addr_size)
19366 {
19367 struct objfile *objfile = dwarf2_per_objfile->objfile;
19368 bfd *abfd = objfile->obfd;
19369 const gdb_byte *info_ptr;
19370 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19371
19372 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19373 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19374 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19375 objfile_name (objfile));
19376 if (addr_base_or_zero + addr_index * addr_size
19377 >= dwarf2_per_objfile->per_bfd->addr.size)
19378 error (_("DW_FORM_addr_index pointing outside of "
19379 ".debug_addr section [in module %s]"),
19380 objfile_name (objfile));
19381 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19382 + addr_base_or_zero + addr_index * addr_size);
19383 if (addr_size == 4)
19384 return bfd_get_32 (abfd, info_ptr);
19385 else
19386 return bfd_get_64 (abfd, info_ptr);
19387 }
19388
19389 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19390
19391 static CORE_ADDR
19392 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19393 {
19394 return read_addr_index_1 (cu->per_objfile, addr_index,
19395 cu->addr_base, cu->header.addr_size);
19396 }
19397
19398 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19399
19400 static CORE_ADDR
19401 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19402 unsigned int *bytes_read)
19403 {
19404 bfd *abfd = cu->per_objfile->objfile->obfd;
19405 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19406
19407 return read_addr_index (cu, addr_index);
19408 }
19409
19410 /* See read.h. */
19411
19412 CORE_ADDR
19413 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19414 dwarf2_per_objfile *dwarf2_per_objfile,
19415 unsigned int addr_index)
19416 {
19417 struct dwarf2_cu *cu = per_cu->cu;
19418 gdb::optional<ULONGEST> addr_base;
19419 int addr_size;
19420
19421 /* We need addr_base and addr_size.
19422 If we don't have PER_CU->cu, we have to get it.
19423 Nasty, but the alternative is storing the needed info in PER_CU,
19424 which at this point doesn't seem justified: it's not clear how frequently
19425 it would get used and it would increase the size of every PER_CU.
19426 Entry points like dwarf2_per_cu_addr_size do a similar thing
19427 so we're not in uncharted territory here.
19428 Alas we need to be a bit more complicated as addr_base is contained
19429 in the DIE.
19430
19431 We don't need to read the entire CU(/TU).
19432 We just need the header and top level die.
19433
19434 IWBN to use the aging mechanism to let us lazily later discard the CU.
19435 For now we skip this optimization. */
19436
19437 if (cu != NULL)
19438 {
19439 addr_base = cu->addr_base;
19440 addr_size = cu->header.addr_size;
19441 }
19442 else
19443 {
19444 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19445 addr_base = reader.cu->addr_base;
19446 addr_size = reader.cu->header.addr_size;
19447 }
19448
19449 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19450 addr_size);
19451 }
19452
19453 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19454 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19455 DWO file. */
19456
19457 static const char *
19458 read_str_index (struct dwarf2_cu *cu,
19459 struct dwarf2_section_info *str_section,
19460 struct dwarf2_section_info *str_offsets_section,
19461 ULONGEST str_offsets_base, ULONGEST str_index)
19462 {
19463 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19464 struct objfile *objfile = dwarf2_per_objfile->objfile;
19465 const char *objf_name = objfile_name (objfile);
19466 bfd *abfd = objfile->obfd;
19467 const gdb_byte *info_ptr;
19468 ULONGEST str_offset;
19469 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19470
19471 str_section->read (objfile);
19472 str_offsets_section->read (objfile);
19473 if (str_section->buffer == NULL)
19474 error (_("%s used without %s section"
19475 " in CU at offset %s [in module %s]"),
19476 form_name, str_section->get_name (),
19477 sect_offset_str (cu->header.sect_off), objf_name);
19478 if (str_offsets_section->buffer == NULL)
19479 error (_("%s used without %s section"
19480 " in CU at offset %s [in module %s]"),
19481 form_name, str_section->get_name (),
19482 sect_offset_str (cu->header.sect_off), objf_name);
19483 info_ptr = (str_offsets_section->buffer
19484 + str_offsets_base
19485 + str_index * cu->header.offset_size);
19486 if (cu->header.offset_size == 4)
19487 str_offset = bfd_get_32 (abfd, info_ptr);
19488 else
19489 str_offset = bfd_get_64 (abfd, info_ptr);
19490 if (str_offset >= str_section->size)
19491 error (_("Offset from %s pointing outside of"
19492 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19493 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19494 return (const char *) (str_section->buffer + str_offset);
19495 }
19496
19497 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19498
19499 static const char *
19500 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19501 {
19502 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19503 ? reader->cu->header.addr_size : 0;
19504 return read_str_index (reader->cu,
19505 &reader->dwo_file->sections.str,
19506 &reader->dwo_file->sections.str_offsets,
19507 str_offsets_base, str_index);
19508 }
19509
19510 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19511
19512 static const char *
19513 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19514 {
19515 struct objfile *objfile = cu->per_objfile->objfile;
19516 const char *objf_name = objfile_name (objfile);
19517 static const char form_name[] = "DW_FORM_GNU_str_index";
19518 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19519
19520 if (!cu->str_offsets_base.has_value ())
19521 error (_("%s used in Fission stub without %s"
19522 " in CU at offset 0x%lx [in module %s]"),
19523 form_name, str_offsets_attr_name,
19524 (long) cu->header.offset_size, objf_name);
19525
19526 return read_str_index (cu,
19527 &cu->per_objfile->per_bfd->str,
19528 &cu->per_objfile->per_bfd->str_offsets,
19529 *cu->str_offsets_base, str_index);
19530 }
19531
19532 /* Return the length of an LEB128 number in BUF. */
19533
19534 static int
19535 leb128_size (const gdb_byte *buf)
19536 {
19537 const gdb_byte *begin = buf;
19538 gdb_byte byte;
19539
19540 while (1)
19541 {
19542 byte = *buf++;
19543 if ((byte & 128) == 0)
19544 return buf - begin;
19545 }
19546 }
19547
19548 static void
19549 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19550 {
19551 switch (lang)
19552 {
19553 case DW_LANG_C89:
19554 case DW_LANG_C99:
19555 case DW_LANG_C11:
19556 case DW_LANG_C:
19557 case DW_LANG_UPC:
19558 cu->language = language_c;
19559 break;
19560 case DW_LANG_Java:
19561 case DW_LANG_C_plus_plus:
19562 case DW_LANG_C_plus_plus_11:
19563 case DW_LANG_C_plus_plus_14:
19564 cu->language = language_cplus;
19565 break;
19566 case DW_LANG_D:
19567 cu->language = language_d;
19568 break;
19569 case DW_LANG_Fortran77:
19570 case DW_LANG_Fortran90:
19571 case DW_LANG_Fortran95:
19572 case DW_LANG_Fortran03:
19573 case DW_LANG_Fortran08:
19574 cu->language = language_fortran;
19575 break;
19576 case DW_LANG_Go:
19577 cu->language = language_go;
19578 break;
19579 case DW_LANG_Mips_Assembler:
19580 cu->language = language_asm;
19581 break;
19582 case DW_LANG_Ada83:
19583 case DW_LANG_Ada95:
19584 cu->language = language_ada;
19585 break;
19586 case DW_LANG_Modula2:
19587 cu->language = language_m2;
19588 break;
19589 case DW_LANG_Pascal83:
19590 cu->language = language_pascal;
19591 break;
19592 case DW_LANG_ObjC:
19593 cu->language = language_objc;
19594 break;
19595 case DW_LANG_Rust:
19596 case DW_LANG_Rust_old:
19597 cu->language = language_rust;
19598 break;
19599 case DW_LANG_Cobol74:
19600 case DW_LANG_Cobol85:
19601 default:
19602 cu->language = language_minimal;
19603 break;
19604 }
19605 cu->language_defn = language_def (cu->language);
19606 }
19607
19608 /* Return the named attribute or NULL if not there. */
19609
19610 static struct attribute *
19611 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19612 {
19613 for (;;)
19614 {
19615 unsigned int i;
19616 struct attribute *spec = NULL;
19617
19618 for (i = 0; i < die->num_attrs; ++i)
19619 {
19620 if (die->attrs[i].name == name)
19621 return &die->attrs[i];
19622 if (die->attrs[i].name == DW_AT_specification
19623 || die->attrs[i].name == DW_AT_abstract_origin)
19624 spec = &die->attrs[i];
19625 }
19626
19627 if (!spec)
19628 break;
19629
19630 die = follow_die_ref (die, spec, &cu);
19631 }
19632
19633 return NULL;
19634 }
19635
19636 /* Return the string associated with a string-typed attribute, or NULL if it
19637 is either not found or is of an incorrect type. */
19638
19639 static const char *
19640 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19641 {
19642 struct attribute *attr;
19643 const char *str = NULL;
19644
19645 attr = dwarf2_attr (die, name, cu);
19646
19647 if (attr != NULL)
19648 {
19649 str = attr->value_as_string ();
19650 if (str == nullptr)
19651 complaint (_("string type expected for attribute %s for "
19652 "DIE at %s in module %s"),
19653 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19654 objfile_name (cu->per_objfile->objfile));
19655 }
19656
19657 return str;
19658 }
19659
19660 /* Return the dwo name or NULL if not present. If present, it is in either
19661 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19662 static const char *
19663 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19664 {
19665 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19666 if (dwo_name == nullptr)
19667 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19668 return dwo_name;
19669 }
19670
19671 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19672 and holds a non-zero value. This function should only be used for
19673 DW_FORM_flag or DW_FORM_flag_present attributes. */
19674
19675 static int
19676 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19677 {
19678 struct attribute *attr = dwarf2_attr (die, name, cu);
19679
19680 return (attr && DW_UNSND (attr));
19681 }
19682
19683 static int
19684 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19685 {
19686 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19687 which value is non-zero. However, we have to be careful with
19688 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19689 (via dwarf2_flag_true_p) follows this attribute. So we may
19690 end up accidently finding a declaration attribute that belongs
19691 to a different DIE referenced by the specification attribute,
19692 even though the given DIE does not have a declaration attribute. */
19693 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19694 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19695 }
19696
19697 /* Return the die giving the specification for DIE, if there is
19698 one. *SPEC_CU is the CU containing DIE on input, and the CU
19699 containing the return value on output. If there is no
19700 specification, but there is an abstract origin, that is
19701 returned. */
19702
19703 static struct die_info *
19704 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19705 {
19706 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19707 *spec_cu);
19708
19709 if (spec_attr == NULL)
19710 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19711
19712 if (spec_attr == NULL)
19713 return NULL;
19714 else
19715 return follow_die_ref (die, spec_attr, spec_cu);
19716 }
19717
19718 /* Stub for free_line_header to match void * callback types. */
19719
19720 static void
19721 free_line_header_voidp (void *arg)
19722 {
19723 struct line_header *lh = (struct line_header *) arg;
19724
19725 delete lh;
19726 }
19727
19728 /* A convenience function to find the proper .debug_line section for a CU. */
19729
19730 static struct dwarf2_section_info *
19731 get_debug_line_section (struct dwarf2_cu *cu)
19732 {
19733 struct dwarf2_section_info *section;
19734 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19735
19736 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19737 DWO file. */
19738 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19739 section = &cu->dwo_unit->dwo_file->sections.line;
19740 else if (cu->per_cu->is_dwz)
19741 {
19742 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19743
19744 section = &dwz->line;
19745 }
19746 else
19747 section = &dwarf2_per_objfile->per_bfd->line;
19748
19749 return section;
19750 }
19751
19752 /* Read the statement program header starting at OFFSET in
19753 .debug_line, or .debug_line.dwo. Return a pointer
19754 to a struct line_header, allocated using xmalloc.
19755 Returns NULL if there is a problem reading the header, e.g., if it
19756 has a version we don't understand.
19757
19758 NOTE: the strings in the include directory and file name tables of
19759 the returned object point into the dwarf line section buffer,
19760 and must not be freed. */
19761
19762 static line_header_up
19763 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19764 {
19765 struct dwarf2_section_info *section;
19766 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19767
19768 section = get_debug_line_section (cu);
19769 section->read (dwarf2_per_objfile->objfile);
19770 if (section->buffer == NULL)
19771 {
19772 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19773 complaint (_("missing .debug_line.dwo section"));
19774 else
19775 complaint (_("missing .debug_line section"));
19776 return 0;
19777 }
19778
19779 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19780 dwarf2_per_objfile, section,
19781 &cu->header);
19782 }
19783
19784 /* Subroutine of dwarf_decode_lines to simplify it.
19785 Return the file name of the psymtab for the given file_entry.
19786 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19787 If space for the result is malloc'd, *NAME_HOLDER will be set.
19788 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19789
19790 static const char *
19791 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19792 const dwarf2_psymtab *pst,
19793 const char *comp_dir,
19794 gdb::unique_xmalloc_ptr<char> *name_holder)
19795 {
19796 const char *include_name = fe.name;
19797 const char *include_name_to_compare = include_name;
19798 const char *pst_filename;
19799 int file_is_pst;
19800
19801 const char *dir_name = fe.include_dir (lh);
19802
19803 gdb::unique_xmalloc_ptr<char> hold_compare;
19804 if (!IS_ABSOLUTE_PATH (include_name)
19805 && (dir_name != NULL || comp_dir != NULL))
19806 {
19807 /* Avoid creating a duplicate psymtab for PST.
19808 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19809 Before we do the comparison, however, we need to account
19810 for DIR_NAME and COMP_DIR.
19811 First prepend dir_name (if non-NULL). If we still don't
19812 have an absolute path prepend comp_dir (if non-NULL).
19813 However, the directory we record in the include-file's
19814 psymtab does not contain COMP_DIR (to match the
19815 corresponding symtab(s)).
19816
19817 Example:
19818
19819 bash$ cd /tmp
19820 bash$ gcc -g ./hello.c
19821 include_name = "hello.c"
19822 dir_name = "."
19823 DW_AT_comp_dir = comp_dir = "/tmp"
19824 DW_AT_name = "./hello.c"
19825
19826 */
19827
19828 if (dir_name != NULL)
19829 {
19830 name_holder->reset (concat (dir_name, SLASH_STRING,
19831 include_name, (char *) NULL));
19832 include_name = name_holder->get ();
19833 include_name_to_compare = include_name;
19834 }
19835 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19836 {
19837 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19838 include_name, (char *) NULL));
19839 include_name_to_compare = hold_compare.get ();
19840 }
19841 }
19842
19843 pst_filename = pst->filename;
19844 gdb::unique_xmalloc_ptr<char> copied_name;
19845 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19846 {
19847 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19848 pst_filename, (char *) NULL));
19849 pst_filename = copied_name.get ();
19850 }
19851
19852 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19853
19854 if (file_is_pst)
19855 return NULL;
19856 return include_name;
19857 }
19858
19859 /* State machine to track the state of the line number program. */
19860
19861 class lnp_state_machine
19862 {
19863 public:
19864 /* Initialize a machine state for the start of a line number
19865 program. */
19866 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19867 bool record_lines_p);
19868
19869 file_entry *current_file ()
19870 {
19871 /* lh->file_names is 0-based, but the file name numbers in the
19872 statement program are 1-based. */
19873 return m_line_header->file_name_at (m_file);
19874 }
19875
19876 /* Record the line in the state machine. END_SEQUENCE is true if
19877 we're processing the end of a sequence. */
19878 void record_line (bool end_sequence);
19879
19880 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19881 nop-out rest of the lines in this sequence. */
19882 void check_line_address (struct dwarf2_cu *cu,
19883 const gdb_byte *line_ptr,
19884 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19885
19886 void handle_set_discriminator (unsigned int discriminator)
19887 {
19888 m_discriminator = discriminator;
19889 m_line_has_non_zero_discriminator |= discriminator != 0;
19890 }
19891
19892 /* Handle DW_LNE_set_address. */
19893 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19894 {
19895 m_op_index = 0;
19896 address += baseaddr;
19897 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19898 }
19899
19900 /* Handle DW_LNS_advance_pc. */
19901 void handle_advance_pc (CORE_ADDR adjust);
19902
19903 /* Handle a special opcode. */
19904 void handle_special_opcode (unsigned char op_code);
19905
19906 /* Handle DW_LNS_advance_line. */
19907 void handle_advance_line (int line_delta)
19908 {
19909 advance_line (line_delta);
19910 }
19911
19912 /* Handle DW_LNS_set_file. */
19913 void handle_set_file (file_name_index file);
19914
19915 /* Handle DW_LNS_negate_stmt. */
19916 void handle_negate_stmt ()
19917 {
19918 m_is_stmt = !m_is_stmt;
19919 }
19920
19921 /* Handle DW_LNS_const_add_pc. */
19922 void handle_const_add_pc ();
19923
19924 /* Handle DW_LNS_fixed_advance_pc. */
19925 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19926 {
19927 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19928 m_op_index = 0;
19929 }
19930
19931 /* Handle DW_LNS_copy. */
19932 void handle_copy ()
19933 {
19934 record_line (false);
19935 m_discriminator = 0;
19936 }
19937
19938 /* Handle DW_LNE_end_sequence. */
19939 void handle_end_sequence ()
19940 {
19941 m_currently_recording_lines = true;
19942 }
19943
19944 private:
19945 /* Advance the line by LINE_DELTA. */
19946 void advance_line (int line_delta)
19947 {
19948 m_line += line_delta;
19949
19950 if (line_delta != 0)
19951 m_line_has_non_zero_discriminator = m_discriminator != 0;
19952 }
19953
19954 struct dwarf2_cu *m_cu;
19955
19956 gdbarch *m_gdbarch;
19957
19958 /* True if we're recording lines.
19959 Otherwise we're building partial symtabs and are just interested in
19960 finding include files mentioned by the line number program. */
19961 bool m_record_lines_p;
19962
19963 /* The line number header. */
19964 line_header *m_line_header;
19965
19966 /* These are part of the standard DWARF line number state machine,
19967 and initialized according to the DWARF spec. */
19968
19969 unsigned char m_op_index = 0;
19970 /* The line table index of the current file. */
19971 file_name_index m_file = 1;
19972 unsigned int m_line = 1;
19973
19974 /* These are initialized in the constructor. */
19975
19976 CORE_ADDR m_address;
19977 bool m_is_stmt;
19978 unsigned int m_discriminator;
19979
19980 /* Additional bits of state we need to track. */
19981
19982 /* The last file that we called dwarf2_start_subfile for.
19983 This is only used for TLLs. */
19984 unsigned int m_last_file = 0;
19985 /* The last file a line number was recorded for. */
19986 struct subfile *m_last_subfile = NULL;
19987
19988 /* When true, record the lines we decode. */
19989 bool m_currently_recording_lines = false;
19990
19991 /* The last line number that was recorded, used to coalesce
19992 consecutive entries for the same line. This can happen, for
19993 example, when discriminators are present. PR 17276. */
19994 unsigned int m_last_line = 0;
19995 bool m_line_has_non_zero_discriminator = false;
19996 };
19997
19998 void
19999 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20000 {
20001 CORE_ADDR addr_adj = (((m_op_index + adjust)
20002 / m_line_header->maximum_ops_per_instruction)
20003 * m_line_header->minimum_instruction_length);
20004 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20005 m_op_index = ((m_op_index + adjust)
20006 % m_line_header->maximum_ops_per_instruction);
20007 }
20008
20009 void
20010 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20011 {
20012 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20013 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20014 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20015 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20016 / m_line_header->maximum_ops_per_instruction)
20017 * m_line_header->minimum_instruction_length);
20018 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20019 m_op_index = ((m_op_index + adj_opcode_d)
20020 % m_line_header->maximum_ops_per_instruction);
20021
20022 int line_delta = m_line_header->line_base + adj_opcode_r;
20023 advance_line (line_delta);
20024 record_line (false);
20025 m_discriminator = 0;
20026 }
20027
20028 void
20029 lnp_state_machine::handle_set_file (file_name_index file)
20030 {
20031 m_file = file;
20032
20033 const file_entry *fe = current_file ();
20034 if (fe == NULL)
20035 dwarf2_debug_line_missing_file_complaint ();
20036 else if (m_record_lines_p)
20037 {
20038 const char *dir = fe->include_dir (m_line_header);
20039
20040 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20041 m_line_has_non_zero_discriminator = m_discriminator != 0;
20042 dwarf2_start_subfile (m_cu, fe->name, dir);
20043 }
20044 }
20045
20046 void
20047 lnp_state_machine::handle_const_add_pc ()
20048 {
20049 CORE_ADDR adjust
20050 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20051
20052 CORE_ADDR addr_adj
20053 = (((m_op_index + adjust)
20054 / m_line_header->maximum_ops_per_instruction)
20055 * m_line_header->minimum_instruction_length);
20056
20057 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20058 m_op_index = ((m_op_index + adjust)
20059 % m_line_header->maximum_ops_per_instruction);
20060 }
20061
20062 /* Return non-zero if we should add LINE to the line number table.
20063 LINE is the line to add, LAST_LINE is the last line that was added,
20064 LAST_SUBFILE is the subfile for LAST_LINE.
20065 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20066 had a non-zero discriminator.
20067
20068 We have to be careful in the presence of discriminators.
20069 E.g., for this line:
20070
20071 for (i = 0; i < 100000; i++);
20072
20073 clang can emit four line number entries for that one line,
20074 each with a different discriminator.
20075 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20076
20077 However, we want gdb to coalesce all four entries into one.
20078 Otherwise the user could stepi into the middle of the line and
20079 gdb would get confused about whether the pc really was in the
20080 middle of the line.
20081
20082 Things are further complicated by the fact that two consecutive
20083 line number entries for the same line is a heuristic used by gcc
20084 to denote the end of the prologue. So we can't just discard duplicate
20085 entries, we have to be selective about it. The heuristic we use is
20086 that we only collapse consecutive entries for the same line if at least
20087 one of those entries has a non-zero discriminator. PR 17276.
20088
20089 Note: Addresses in the line number state machine can never go backwards
20090 within one sequence, thus this coalescing is ok. */
20091
20092 static int
20093 dwarf_record_line_p (struct dwarf2_cu *cu,
20094 unsigned int line, unsigned int last_line,
20095 int line_has_non_zero_discriminator,
20096 struct subfile *last_subfile)
20097 {
20098 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20099 return 1;
20100 if (line != last_line)
20101 return 1;
20102 /* Same line for the same file that we've seen already.
20103 As a last check, for pr 17276, only record the line if the line
20104 has never had a non-zero discriminator. */
20105 if (!line_has_non_zero_discriminator)
20106 return 1;
20107 return 0;
20108 }
20109
20110 /* Use the CU's builder to record line number LINE beginning at
20111 address ADDRESS in the line table of subfile SUBFILE. */
20112
20113 static void
20114 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20115 unsigned int line, CORE_ADDR address, bool is_stmt,
20116 struct dwarf2_cu *cu)
20117 {
20118 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20119
20120 if (dwarf_line_debug)
20121 {
20122 fprintf_unfiltered (gdb_stdlog,
20123 "Recording line %u, file %s, address %s\n",
20124 line, lbasename (subfile->name),
20125 paddress (gdbarch, address));
20126 }
20127
20128 if (cu != nullptr)
20129 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20130 }
20131
20132 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20133 Mark the end of a set of line number records.
20134 The arguments are the same as for dwarf_record_line_1.
20135 If SUBFILE is NULL the request is ignored. */
20136
20137 static void
20138 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20139 CORE_ADDR address, struct dwarf2_cu *cu)
20140 {
20141 if (subfile == NULL)
20142 return;
20143
20144 if (dwarf_line_debug)
20145 {
20146 fprintf_unfiltered (gdb_stdlog,
20147 "Finishing current line, file %s, address %s\n",
20148 lbasename (subfile->name),
20149 paddress (gdbarch, address));
20150 }
20151
20152 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20153 }
20154
20155 void
20156 lnp_state_machine::record_line (bool end_sequence)
20157 {
20158 if (dwarf_line_debug)
20159 {
20160 fprintf_unfiltered (gdb_stdlog,
20161 "Processing actual line %u: file %u,"
20162 " address %s, is_stmt %u, discrim %u%s\n",
20163 m_line, m_file,
20164 paddress (m_gdbarch, m_address),
20165 m_is_stmt, m_discriminator,
20166 (end_sequence ? "\t(end sequence)" : ""));
20167 }
20168
20169 file_entry *fe = current_file ();
20170
20171 if (fe == NULL)
20172 dwarf2_debug_line_missing_file_complaint ();
20173 /* For now we ignore lines not starting on an instruction boundary.
20174 But not when processing end_sequence for compatibility with the
20175 previous version of the code. */
20176 else if (m_op_index == 0 || end_sequence)
20177 {
20178 fe->included_p = 1;
20179 if (m_record_lines_p)
20180 {
20181 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20182 || end_sequence)
20183 {
20184 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20185 m_currently_recording_lines ? m_cu : nullptr);
20186 }
20187
20188 if (!end_sequence)
20189 {
20190 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20191
20192 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20193 m_line_has_non_zero_discriminator,
20194 m_last_subfile))
20195 {
20196 buildsym_compunit *builder = m_cu->get_builder ();
20197 dwarf_record_line_1 (m_gdbarch,
20198 builder->get_current_subfile (),
20199 m_line, m_address, is_stmt,
20200 m_currently_recording_lines ? m_cu : nullptr);
20201 }
20202 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20203 m_last_line = m_line;
20204 }
20205 }
20206 }
20207 }
20208
20209 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20210 line_header *lh, bool record_lines_p)
20211 {
20212 m_cu = cu;
20213 m_gdbarch = arch;
20214 m_record_lines_p = record_lines_p;
20215 m_line_header = lh;
20216
20217 m_currently_recording_lines = true;
20218
20219 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20220 was a line entry for it so that the backend has a chance to adjust it
20221 and also record it in case it needs it. This is currently used by MIPS
20222 code, cf. `mips_adjust_dwarf2_line'. */
20223 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20224 m_is_stmt = lh->default_is_stmt;
20225 m_discriminator = 0;
20226 }
20227
20228 void
20229 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20230 const gdb_byte *line_ptr,
20231 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20232 {
20233 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20234 the pc range of the CU. However, we restrict the test to only ADDRESS
20235 values of zero to preserve GDB's previous behaviour which is to handle
20236 the specific case of a function being GC'd by the linker. */
20237
20238 if (address == 0 && address < unrelocated_lowpc)
20239 {
20240 /* This line table is for a function which has been
20241 GCd by the linker. Ignore it. PR gdb/12528 */
20242
20243 struct objfile *objfile = cu->per_objfile->objfile;
20244 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20245
20246 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20247 line_offset, objfile_name (objfile));
20248 m_currently_recording_lines = false;
20249 /* Note: m_currently_recording_lines is left as false until we see
20250 DW_LNE_end_sequence. */
20251 }
20252 }
20253
20254 /* Subroutine of dwarf_decode_lines to simplify it.
20255 Process the line number information in LH.
20256 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20257 program in order to set included_p for every referenced header. */
20258
20259 static void
20260 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20261 const int decode_for_pst_p, CORE_ADDR lowpc)
20262 {
20263 const gdb_byte *line_ptr, *extended_end;
20264 const gdb_byte *line_end;
20265 unsigned int bytes_read, extended_len;
20266 unsigned char op_code, extended_op;
20267 CORE_ADDR baseaddr;
20268 struct objfile *objfile = cu->per_objfile->objfile;
20269 bfd *abfd = objfile->obfd;
20270 struct gdbarch *gdbarch = objfile->arch ();
20271 /* True if we're recording line info (as opposed to building partial
20272 symtabs and just interested in finding include files mentioned by
20273 the line number program). */
20274 bool record_lines_p = !decode_for_pst_p;
20275
20276 baseaddr = objfile->text_section_offset ();
20277
20278 line_ptr = lh->statement_program_start;
20279 line_end = lh->statement_program_end;
20280
20281 /* Read the statement sequences until there's nothing left. */
20282 while (line_ptr < line_end)
20283 {
20284 /* The DWARF line number program state machine. Reset the state
20285 machine at the start of each sequence. */
20286 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20287 bool end_sequence = false;
20288
20289 if (record_lines_p)
20290 {
20291 /* Start a subfile for the current file of the state
20292 machine. */
20293 const file_entry *fe = state_machine.current_file ();
20294
20295 if (fe != NULL)
20296 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20297 }
20298
20299 /* Decode the table. */
20300 while (line_ptr < line_end && !end_sequence)
20301 {
20302 op_code = read_1_byte (abfd, line_ptr);
20303 line_ptr += 1;
20304
20305 if (op_code >= lh->opcode_base)
20306 {
20307 /* Special opcode. */
20308 state_machine.handle_special_opcode (op_code);
20309 }
20310 else switch (op_code)
20311 {
20312 case DW_LNS_extended_op:
20313 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20314 &bytes_read);
20315 line_ptr += bytes_read;
20316 extended_end = line_ptr + extended_len;
20317 extended_op = read_1_byte (abfd, line_ptr);
20318 line_ptr += 1;
20319 switch (extended_op)
20320 {
20321 case DW_LNE_end_sequence:
20322 state_machine.handle_end_sequence ();
20323 end_sequence = true;
20324 break;
20325 case DW_LNE_set_address:
20326 {
20327 CORE_ADDR address
20328 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20329 line_ptr += bytes_read;
20330
20331 state_machine.check_line_address (cu, line_ptr,
20332 lowpc - baseaddr, address);
20333 state_machine.handle_set_address (baseaddr, address);
20334 }
20335 break;
20336 case DW_LNE_define_file:
20337 {
20338 const char *cur_file;
20339 unsigned int mod_time, length;
20340 dir_index dindex;
20341
20342 cur_file = read_direct_string (abfd, line_ptr,
20343 &bytes_read);
20344 line_ptr += bytes_read;
20345 dindex = (dir_index)
20346 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20347 line_ptr += bytes_read;
20348 mod_time =
20349 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20350 line_ptr += bytes_read;
20351 length =
20352 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20353 line_ptr += bytes_read;
20354 lh->add_file_name (cur_file, dindex, mod_time, length);
20355 }
20356 break;
20357 case DW_LNE_set_discriminator:
20358 {
20359 /* The discriminator is not interesting to the
20360 debugger; just ignore it. We still need to
20361 check its value though:
20362 if there are consecutive entries for the same
20363 (non-prologue) line we want to coalesce them.
20364 PR 17276. */
20365 unsigned int discr
20366 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20367 line_ptr += bytes_read;
20368
20369 state_machine.handle_set_discriminator (discr);
20370 }
20371 break;
20372 default:
20373 complaint (_("mangled .debug_line section"));
20374 return;
20375 }
20376 /* Make sure that we parsed the extended op correctly. If e.g.
20377 we expected a different address size than the producer used,
20378 we may have read the wrong number of bytes. */
20379 if (line_ptr != extended_end)
20380 {
20381 complaint (_("mangled .debug_line section"));
20382 return;
20383 }
20384 break;
20385 case DW_LNS_copy:
20386 state_machine.handle_copy ();
20387 break;
20388 case DW_LNS_advance_pc:
20389 {
20390 CORE_ADDR adjust
20391 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20392 line_ptr += bytes_read;
20393
20394 state_machine.handle_advance_pc (adjust);
20395 }
20396 break;
20397 case DW_LNS_advance_line:
20398 {
20399 int line_delta
20400 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20401 line_ptr += bytes_read;
20402
20403 state_machine.handle_advance_line (line_delta);
20404 }
20405 break;
20406 case DW_LNS_set_file:
20407 {
20408 file_name_index file
20409 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20410 &bytes_read);
20411 line_ptr += bytes_read;
20412
20413 state_machine.handle_set_file (file);
20414 }
20415 break;
20416 case DW_LNS_set_column:
20417 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20418 line_ptr += bytes_read;
20419 break;
20420 case DW_LNS_negate_stmt:
20421 state_machine.handle_negate_stmt ();
20422 break;
20423 case DW_LNS_set_basic_block:
20424 break;
20425 /* Add to the address register of the state machine the
20426 address increment value corresponding to special opcode
20427 255. I.e., this value is scaled by the minimum
20428 instruction length since special opcode 255 would have
20429 scaled the increment. */
20430 case DW_LNS_const_add_pc:
20431 state_machine.handle_const_add_pc ();
20432 break;
20433 case DW_LNS_fixed_advance_pc:
20434 {
20435 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20436 line_ptr += 2;
20437
20438 state_machine.handle_fixed_advance_pc (addr_adj);
20439 }
20440 break;
20441 default:
20442 {
20443 /* Unknown standard opcode, ignore it. */
20444 int i;
20445
20446 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20447 {
20448 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20449 line_ptr += bytes_read;
20450 }
20451 }
20452 }
20453 }
20454
20455 if (!end_sequence)
20456 dwarf2_debug_line_missing_end_sequence_complaint ();
20457
20458 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20459 in which case we still finish recording the last line). */
20460 state_machine.record_line (true);
20461 }
20462 }
20463
20464 /* Decode the Line Number Program (LNP) for the given line_header
20465 structure and CU. The actual information extracted and the type
20466 of structures created from the LNP depends on the value of PST.
20467
20468 1. If PST is NULL, then this procedure uses the data from the program
20469 to create all necessary symbol tables, and their linetables.
20470
20471 2. If PST is not NULL, this procedure reads the program to determine
20472 the list of files included by the unit represented by PST, and
20473 builds all the associated partial symbol tables.
20474
20475 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20476 It is used for relative paths in the line table.
20477 NOTE: When processing partial symtabs (pst != NULL),
20478 comp_dir == pst->dirname.
20479
20480 NOTE: It is important that psymtabs have the same file name (via strcmp)
20481 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20482 symtab we don't use it in the name of the psymtabs we create.
20483 E.g. expand_line_sal requires this when finding psymtabs to expand.
20484 A good testcase for this is mb-inline.exp.
20485
20486 LOWPC is the lowest address in CU (or 0 if not known).
20487
20488 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20489 for its PC<->lines mapping information. Otherwise only the filename
20490 table is read in. */
20491
20492 static void
20493 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20494 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20495 CORE_ADDR lowpc, int decode_mapping)
20496 {
20497 struct objfile *objfile = cu->per_objfile->objfile;
20498 const int decode_for_pst_p = (pst != NULL);
20499
20500 if (decode_mapping)
20501 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20502
20503 if (decode_for_pst_p)
20504 {
20505 /* Now that we're done scanning the Line Header Program, we can
20506 create the psymtab of each included file. */
20507 for (auto &file_entry : lh->file_names ())
20508 if (file_entry.included_p == 1)
20509 {
20510 gdb::unique_xmalloc_ptr<char> name_holder;
20511 const char *include_name =
20512 psymtab_include_file_name (lh, file_entry, pst,
20513 comp_dir, &name_holder);
20514 if (include_name != NULL)
20515 dwarf2_create_include_psymtab (include_name, pst, objfile);
20516 }
20517 }
20518 else
20519 {
20520 /* Make sure a symtab is created for every file, even files
20521 which contain only variables (i.e. no code with associated
20522 line numbers). */
20523 buildsym_compunit *builder = cu->get_builder ();
20524 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20525
20526 for (auto &fe : lh->file_names ())
20527 {
20528 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20529 if (builder->get_current_subfile ()->symtab == NULL)
20530 {
20531 builder->get_current_subfile ()->symtab
20532 = allocate_symtab (cust,
20533 builder->get_current_subfile ()->name);
20534 }
20535 fe.symtab = builder->get_current_subfile ()->symtab;
20536 }
20537 }
20538 }
20539
20540 /* Start a subfile for DWARF. FILENAME is the name of the file and
20541 DIRNAME the name of the source directory which contains FILENAME
20542 or NULL if not known.
20543 This routine tries to keep line numbers from identical absolute and
20544 relative file names in a common subfile.
20545
20546 Using the `list' example from the GDB testsuite, which resides in
20547 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20548 of /srcdir/list0.c yields the following debugging information for list0.c:
20549
20550 DW_AT_name: /srcdir/list0.c
20551 DW_AT_comp_dir: /compdir
20552 files.files[0].name: list0.h
20553 files.files[0].dir: /srcdir
20554 files.files[1].name: list0.c
20555 files.files[1].dir: /srcdir
20556
20557 The line number information for list0.c has to end up in a single
20558 subfile, so that `break /srcdir/list0.c:1' works as expected.
20559 start_subfile will ensure that this happens provided that we pass the
20560 concatenation of files.files[1].dir and files.files[1].name as the
20561 subfile's name. */
20562
20563 static void
20564 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20565 const char *dirname)
20566 {
20567 gdb::unique_xmalloc_ptr<char> copy;
20568
20569 /* In order not to lose the line information directory,
20570 we concatenate it to the filename when it makes sense.
20571 Note that the Dwarf3 standard says (speaking of filenames in line
20572 information): ``The directory index is ignored for file names
20573 that represent full path names''. Thus ignoring dirname in the
20574 `else' branch below isn't an issue. */
20575
20576 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20577 {
20578 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20579 filename = copy.get ();
20580 }
20581
20582 cu->get_builder ()->start_subfile (filename);
20583 }
20584
20585 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20586 buildsym_compunit constructor. */
20587
20588 struct compunit_symtab *
20589 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20590 CORE_ADDR low_pc)
20591 {
20592 gdb_assert (m_builder == nullptr);
20593
20594 m_builder.reset (new struct buildsym_compunit
20595 (per_cu->dwarf2_per_objfile->objfile,
20596 name, comp_dir, language, low_pc));
20597
20598 list_in_scope = get_builder ()->get_file_symbols ();
20599
20600 get_builder ()->record_debugformat ("DWARF 2");
20601 get_builder ()->record_producer (producer);
20602
20603 processing_has_namespace_info = false;
20604
20605 return get_builder ()->get_compunit_symtab ();
20606 }
20607
20608 static void
20609 var_decode_location (struct attribute *attr, struct symbol *sym,
20610 struct dwarf2_cu *cu)
20611 {
20612 struct objfile *objfile = cu->per_objfile->objfile;
20613 struct comp_unit_head *cu_header = &cu->header;
20614
20615 /* NOTE drow/2003-01-30: There used to be a comment and some special
20616 code here to turn a symbol with DW_AT_external and a
20617 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20618 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20619 with some versions of binutils) where shared libraries could have
20620 relocations against symbols in their debug information - the
20621 minimal symbol would have the right address, but the debug info
20622 would not. It's no longer necessary, because we will explicitly
20623 apply relocations when we read in the debug information now. */
20624
20625 /* A DW_AT_location attribute with no contents indicates that a
20626 variable has been optimized away. */
20627 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20628 {
20629 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20630 return;
20631 }
20632
20633 /* Handle one degenerate form of location expression specially, to
20634 preserve GDB's previous behavior when section offsets are
20635 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20636 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20637
20638 if (attr->form_is_block ()
20639 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20640 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20641 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20642 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20643 && (DW_BLOCK (attr)->size
20644 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20645 {
20646 unsigned int dummy;
20647
20648 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20649 SET_SYMBOL_VALUE_ADDRESS
20650 (sym, cu->header.read_address (objfile->obfd,
20651 DW_BLOCK (attr)->data + 1,
20652 &dummy));
20653 else
20654 SET_SYMBOL_VALUE_ADDRESS
20655 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20656 &dummy));
20657 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20658 fixup_symbol_section (sym, objfile);
20659 SET_SYMBOL_VALUE_ADDRESS
20660 (sym,
20661 SYMBOL_VALUE_ADDRESS (sym)
20662 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20663 return;
20664 }
20665
20666 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20667 expression evaluator, and use LOC_COMPUTED only when necessary
20668 (i.e. when the value of a register or memory location is
20669 referenced, or a thread-local block, etc.). Then again, it might
20670 not be worthwhile. I'm assuming that it isn't unless performance
20671 or memory numbers show me otherwise. */
20672
20673 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20674
20675 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20676 cu->has_loclist = true;
20677 }
20678
20679 /* Given a pointer to a DWARF information entry, figure out if we need
20680 to make a symbol table entry for it, and if so, create a new entry
20681 and return a pointer to it.
20682 If TYPE is NULL, determine symbol type from the die, otherwise
20683 used the passed type.
20684 If SPACE is not NULL, use it to hold the new symbol. If it is
20685 NULL, allocate a new symbol on the objfile's obstack. */
20686
20687 static struct symbol *
20688 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20689 struct symbol *space)
20690 {
20691 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20692 struct objfile *objfile = dwarf2_per_objfile->objfile;
20693 struct gdbarch *gdbarch = objfile->arch ();
20694 struct symbol *sym = NULL;
20695 const char *name;
20696 struct attribute *attr = NULL;
20697 struct attribute *attr2 = NULL;
20698 CORE_ADDR baseaddr;
20699 struct pending **list_to_add = NULL;
20700
20701 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20702
20703 baseaddr = objfile->text_section_offset ();
20704
20705 name = dwarf2_name (die, cu);
20706 if (name)
20707 {
20708 int suppress_add = 0;
20709
20710 if (space)
20711 sym = space;
20712 else
20713 sym = new (&objfile->objfile_obstack) symbol;
20714 OBJSTAT (objfile, n_syms++);
20715
20716 /* Cache this symbol's name and the name's demangled form (if any). */
20717 sym->set_language (cu->language, &objfile->objfile_obstack);
20718 /* Fortran does not have mangling standard and the mangling does differ
20719 between gfortran, iFort etc. */
20720 const char *physname
20721 = (cu->language == language_fortran
20722 ? dwarf2_full_name (name, die, cu)
20723 : dwarf2_physname (name, die, cu));
20724 const char *linkagename = dw2_linkage_name (die, cu);
20725
20726 if (linkagename == nullptr || cu->language == language_ada)
20727 sym->set_linkage_name (physname);
20728 else
20729 {
20730 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20731 sym->set_linkage_name (linkagename);
20732 }
20733
20734 /* Default assumptions.
20735 Use the passed type or decode it from the die. */
20736 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20737 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20738 if (type != NULL)
20739 SYMBOL_TYPE (sym) = type;
20740 else
20741 SYMBOL_TYPE (sym) = die_type (die, cu);
20742 attr = dwarf2_attr (die,
20743 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20744 cu);
20745 if (attr != nullptr)
20746 {
20747 SYMBOL_LINE (sym) = DW_UNSND (attr);
20748 }
20749
20750 attr = dwarf2_attr (die,
20751 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20752 cu);
20753 if (attr != nullptr)
20754 {
20755 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20756 struct file_entry *fe;
20757
20758 if (cu->line_header != NULL)
20759 fe = cu->line_header->file_name_at (file_index);
20760 else
20761 fe = NULL;
20762
20763 if (fe == NULL)
20764 complaint (_("file index out of range"));
20765 else
20766 symbol_set_symtab (sym, fe->symtab);
20767 }
20768
20769 switch (die->tag)
20770 {
20771 case DW_TAG_label:
20772 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20773 if (attr != nullptr)
20774 {
20775 CORE_ADDR addr;
20776
20777 addr = attr->value_as_address ();
20778 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20779 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20780 }
20781 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20782 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20783 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20784 add_symbol_to_list (sym, cu->list_in_scope);
20785 break;
20786 case DW_TAG_subprogram:
20787 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20788 finish_block. */
20789 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20790 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20791 if ((attr2 && (DW_UNSND (attr2) != 0))
20792 || cu->language == language_ada
20793 || cu->language == language_fortran)
20794 {
20795 /* Subprograms marked external are stored as a global symbol.
20796 Ada and Fortran subprograms, whether marked external or
20797 not, are always stored as a global symbol, because we want
20798 to be able to access them globally. For instance, we want
20799 to be able to break on a nested subprogram without having
20800 to specify the context. */
20801 list_to_add = cu->get_builder ()->get_global_symbols ();
20802 }
20803 else
20804 {
20805 list_to_add = cu->list_in_scope;
20806 }
20807 break;
20808 case DW_TAG_inlined_subroutine:
20809 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20810 finish_block. */
20811 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20812 SYMBOL_INLINED (sym) = 1;
20813 list_to_add = cu->list_in_scope;
20814 break;
20815 case DW_TAG_template_value_param:
20816 suppress_add = 1;
20817 /* Fall through. */
20818 case DW_TAG_constant:
20819 case DW_TAG_variable:
20820 case DW_TAG_member:
20821 /* Compilation with minimal debug info may result in
20822 variables with missing type entries. Change the
20823 misleading `void' type to something sensible. */
20824 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20825 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20826
20827 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20828 /* In the case of DW_TAG_member, we should only be called for
20829 static const members. */
20830 if (die->tag == DW_TAG_member)
20831 {
20832 /* dwarf2_add_field uses die_is_declaration,
20833 so we do the same. */
20834 gdb_assert (die_is_declaration (die, cu));
20835 gdb_assert (attr);
20836 }
20837 if (attr != nullptr)
20838 {
20839 dwarf2_const_value (attr, sym, cu);
20840 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20841 if (!suppress_add)
20842 {
20843 if (attr2 && (DW_UNSND (attr2) != 0))
20844 list_to_add = cu->get_builder ()->get_global_symbols ();
20845 else
20846 list_to_add = cu->list_in_scope;
20847 }
20848 break;
20849 }
20850 attr = dwarf2_attr (die, DW_AT_location, cu);
20851 if (attr != nullptr)
20852 {
20853 var_decode_location (attr, sym, cu);
20854 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20855
20856 /* Fortran explicitly imports any global symbols to the local
20857 scope by DW_TAG_common_block. */
20858 if (cu->language == language_fortran && die->parent
20859 && die->parent->tag == DW_TAG_common_block)
20860 attr2 = NULL;
20861
20862 if (SYMBOL_CLASS (sym) == LOC_STATIC
20863 && SYMBOL_VALUE_ADDRESS (sym) == 0
20864 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20865 {
20866 /* When a static variable is eliminated by the linker,
20867 the corresponding debug information is not stripped
20868 out, but the variable address is set to null;
20869 do not add such variables into symbol table. */
20870 }
20871 else if (attr2 && (DW_UNSND (attr2) != 0))
20872 {
20873 if (SYMBOL_CLASS (sym) == LOC_STATIC
20874 && (objfile->flags & OBJF_MAINLINE) == 0
20875 && dwarf2_per_objfile->per_bfd->can_copy)
20876 {
20877 /* A global static variable might be subject to
20878 copy relocation. We first check for a local
20879 minsym, though, because maybe the symbol was
20880 marked hidden, in which case this would not
20881 apply. */
20882 bound_minimal_symbol found
20883 = (lookup_minimal_symbol_linkage
20884 (sym->linkage_name (), objfile));
20885 if (found.minsym != nullptr)
20886 sym->maybe_copied = 1;
20887 }
20888
20889 /* A variable with DW_AT_external is never static,
20890 but it may be block-scoped. */
20891 list_to_add
20892 = ((cu->list_in_scope
20893 == cu->get_builder ()->get_file_symbols ())
20894 ? cu->get_builder ()->get_global_symbols ()
20895 : cu->list_in_scope);
20896 }
20897 else
20898 list_to_add = cu->list_in_scope;
20899 }
20900 else
20901 {
20902 /* We do not know the address of this symbol.
20903 If it is an external symbol and we have type information
20904 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20905 The address of the variable will then be determined from
20906 the minimal symbol table whenever the variable is
20907 referenced. */
20908 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20909
20910 /* Fortran explicitly imports any global symbols to the local
20911 scope by DW_TAG_common_block. */
20912 if (cu->language == language_fortran && die->parent
20913 && die->parent->tag == DW_TAG_common_block)
20914 {
20915 /* SYMBOL_CLASS doesn't matter here because
20916 read_common_block is going to reset it. */
20917 if (!suppress_add)
20918 list_to_add = cu->list_in_scope;
20919 }
20920 else if (attr2 && (DW_UNSND (attr2) != 0)
20921 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20922 {
20923 /* A variable with DW_AT_external is never static, but it
20924 may be block-scoped. */
20925 list_to_add
20926 = ((cu->list_in_scope
20927 == cu->get_builder ()->get_file_symbols ())
20928 ? cu->get_builder ()->get_global_symbols ()
20929 : cu->list_in_scope);
20930
20931 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20932 }
20933 else if (!die_is_declaration (die, cu))
20934 {
20935 /* Use the default LOC_OPTIMIZED_OUT class. */
20936 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20937 if (!suppress_add)
20938 list_to_add = cu->list_in_scope;
20939 }
20940 }
20941 break;
20942 case DW_TAG_formal_parameter:
20943 {
20944 /* If we are inside a function, mark this as an argument. If
20945 not, we might be looking at an argument to an inlined function
20946 when we do not have enough information to show inlined frames;
20947 pretend it's a local variable in that case so that the user can
20948 still see it. */
20949 struct context_stack *curr
20950 = cu->get_builder ()->get_current_context_stack ();
20951 if (curr != nullptr && curr->name != nullptr)
20952 SYMBOL_IS_ARGUMENT (sym) = 1;
20953 attr = dwarf2_attr (die, DW_AT_location, cu);
20954 if (attr != nullptr)
20955 {
20956 var_decode_location (attr, sym, cu);
20957 }
20958 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20959 if (attr != nullptr)
20960 {
20961 dwarf2_const_value (attr, sym, cu);
20962 }
20963
20964 list_to_add = cu->list_in_scope;
20965 }
20966 break;
20967 case DW_TAG_unspecified_parameters:
20968 /* From varargs functions; gdb doesn't seem to have any
20969 interest in this information, so just ignore it for now.
20970 (FIXME?) */
20971 break;
20972 case DW_TAG_template_type_param:
20973 suppress_add = 1;
20974 /* Fall through. */
20975 case DW_TAG_class_type:
20976 case DW_TAG_interface_type:
20977 case DW_TAG_structure_type:
20978 case DW_TAG_union_type:
20979 case DW_TAG_set_type:
20980 case DW_TAG_enumeration_type:
20981 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20982 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20983
20984 {
20985 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20986 really ever be static objects: otherwise, if you try
20987 to, say, break of a class's method and you're in a file
20988 which doesn't mention that class, it won't work unless
20989 the check for all static symbols in lookup_symbol_aux
20990 saves you. See the OtherFileClass tests in
20991 gdb.c++/namespace.exp. */
20992
20993 if (!suppress_add)
20994 {
20995 buildsym_compunit *builder = cu->get_builder ();
20996 list_to_add
20997 = (cu->list_in_scope == builder->get_file_symbols ()
20998 && cu->language == language_cplus
20999 ? builder->get_global_symbols ()
21000 : cu->list_in_scope);
21001
21002 /* The semantics of C++ state that "struct foo {
21003 ... }" also defines a typedef for "foo". */
21004 if (cu->language == language_cplus
21005 || cu->language == language_ada
21006 || cu->language == language_d
21007 || cu->language == language_rust)
21008 {
21009 /* The symbol's name is already allocated along
21010 with this objfile, so we don't need to
21011 duplicate it for the type. */
21012 if (SYMBOL_TYPE (sym)->name () == 0)
21013 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21014 }
21015 }
21016 }
21017 break;
21018 case DW_TAG_typedef:
21019 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21020 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21021 list_to_add = cu->list_in_scope;
21022 break;
21023 case DW_TAG_base_type:
21024 case DW_TAG_subrange_type:
21025 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21026 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21027 list_to_add = cu->list_in_scope;
21028 break;
21029 case DW_TAG_enumerator:
21030 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21031 if (attr != nullptr)
21032 {
21033 dwarf2_const_value (attr, sym, cu);
21034 }
21035 {
21036 /* NOTE: carlton/2003-11-10: See comment above in the
21037 DW_TAG_class_type, etc. block. */
21038
21039 list_to_add
21040 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21041 && cu->language == language_cplus
21042 ? cu->get_builder ()->get_global_symbols ()
21043 : cu->list_in_scope);
21044 }
21045 break;
21046 case DW_TAG_imported_declaration:
21047 case DW_TAG_namespace:
21048 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21049 list_to_add = cu->get_builder ()->get_global_symbols ();
21050 break;
21051 case DW_TAG_module:
21052 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21053 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21054 list_to_add = cu->get_builder ()->get_global_symbols ();
21055 break;
21056 case DW_TAG_common_block:
21057 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21058 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21059 add_symbol_to_list (sym, cu->list_in_scope);
21060 break;
21061 default:
21062 /* Not a tag we recognize. Hopefully we aren't processing
21063 trash data, but since we must specifically ignore things
21064 we don't recognize, there is nothing else we should do at
21065 this point. */
21066 complaint (_("unsupported tag: '%s'"),
21067 dwarf_tag_name (die->tag));
21068 break;
21069 }
21070
21071 if (suppress_add)
21072 {
21073 sym->hash_next = objfile->template_symbols;
21074 objfile->template_symbols = sym;
21075 list_to_add = NULL;
21076 }
21077
21078 if (list_to_add != NULL)
21079 add_symbol_to_list (sym, list_to_add);
21080
21081 /* For the benefit of old versions of GCC, check for anonymous
21082 namespaces based on the demangled name. */
21083 if (!cu->processing_has_namespace_info
21084 && cu->language == language_cplus)
21085 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21086 }
21087 return (sym);
21088 }
21089
21090 /* Given an attr with a DW_FORM_dataN value in host byte order,
21091 zero-extend it as appropriate for the symbol's type. The DWARF
21092 standard (v4) is not entirely clear about the meaning of using
21093 DW_FORM_dataN for a constant with a signed type, where the type is
21094 wider than the data. The conclusion of a discussion on the DWARF
21095 list was that this is unspecified. We choose to always zero-extend
21096 because that is the interpretation long in use by GCC. */
21097
21098 static gdb_byte *
21099 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21100 struct dwarf2_cu *cu, LONGEST *value, int bits)
21101 {
21102 struct objfile *objfile = cu->per_objfile->objfile;
21103 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21104 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21105 LONGEST l = DW_UNSND (attr);
21106
21107 if (bits < sizeof (*value) * 8)
21108 {
21109 l &= ((LONGEST) 1 << bits) - 1;
21110 *value = l;
21111 }
21112 else if (bits == sizeof (*value) * 8)
21113 *value = l;
21114 else
21115 {
21116 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21117 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21118 return bytes;
21119 }
21120
21121 return NULL;
21122 }
21123
21124 /* Read a constant value from an attribute. Either set *VALUE, or if
21125 the value does not fit in *VALUE, set *BYTES - either already
21126 allocated on the objfile obstack, or newly allocated on OBSTACK,
21127 or, set *BATON, if we translated the constant to a location
21128 expression. */
21129
21130 static void
21131 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21132 const char *name, struct obstack *obstack,
21133 struct dwarf2_cu *cu,
21134 LONGEST *value, const gdb_byte **bytes,
21135 struct dwarf2_locexpr_baton **baton)
21136 {
21137 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21138 struct objfile *objfile = per_objfile->objfile;
21139 struct comp_unit_head *cu_header = &cu->header;
21140 struct dwarf_block *blk;
21141 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21142 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21143
21144 *value = 0;
21145 *bytes = NULL;
21146 *baton = NULL;
21147
21148 switch (attr->form)
21149 {
21150 case DW_FORM_addr:
21151 case DW_FORM_addrx:
21152 case DW_FORM_GNU_addr_index:
21153 {
21154 gdb_byte *data;
21155
21156 if (TYPE_LENGTH (type) != cu_header->addr_size)
21157 dwarf2_const_value_length_mismatch_complaint (name,
21158 cu_header->addr_size,
21159 TYPE_LENGTH (type));
21160 /* Symbols of this form are reasonably rare, so we just
21161 piggyback on the existing location code rather than writing
21162 a new implementation of symbol_computed_ops. */
21163 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21164 (*baton)->per_objfile = per_objfile;
21165 (*baton)->per_cu = cu->per_cu;
21166 gdb_assert ((*baton)->per_cu);
21167
21168 (*baton)->size = 2 + cu_header->addr_size;
21169 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21170 (*baton)->data = data;
21171
21172 data[0] = DW_OP_addr;
21173 store_unsigned_integer (&data[1], cu_header->addr_size,
21174 byte_order, DW_ADDR (attr));
21175 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21176 }
21177 break;
21178 case DW_FORM_string:
21179 case DW_FORM_strp:
21180 case DW_FORM_strx:
21181 case DW_FORM_GNU_str_index:
21182 case DW_FORM_GNU_strp_alt:
21183 /* DW_STRING is already allocated on the objfile obstack, point
21184 directly to it. */
21185 *bytes = (const gdb_byte *) DW_STRING (attr);
21186 break;
21187 case DW_FORM_block1:
21188 case DW_FORM_block2:
21189 case DW_FORM_block4:
21190 case DW_FORM_block:
21191 case DW_FORM_exprloc:
21192 case DW_FORM_data16:
21193 blk = DW_BLOCK (attr);
21194 if (TYPE_LENGTH (type) != blk->size)
21195 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21196 TYPE_LENGTH (type));
21197 *bytes = blk->data;
21198 break;
21199
21200 /* The DW_AT_const_value attributes are supposed to carry the
21201 symbol's value "represented as it would be on the target
21202 architecture." By the time we get here, it's already been
21203 converted to host endianness, so we just need to sign- or
21204 zero-extend it as appropriate. */
21205 case DW_FORM_data1:
21206 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21207 break;
21208 case DW_FORM_data2:
21209 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21210 break;
21211 case DW_FORM_data4:
21212 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21213 break;
21214 case DW_FORM_data8:
21215 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21216 break;
21217
21218 case DW_FORM_sdata:
21219 case DW_FORM_implicit_const:
21220 *value = DW_SND (attr);
21221 break;
21222
21223 case DW_FORM_udata:
21224 *value = DW_UNSND (attr);
21225 break;
21226
21227 default:
21228 complaint (_("unsupported const value attribute form: '%s'"),
21229 dwarf_form_name (attr->form));
21230 *value = 0;
21231 break;
21232 }
21233 }
21234
21235
21236 /* Copy constant value from an attribute to a symbol. */
21237
21238 static void
21239 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21240 struct dwarf2_cu *cu)
21241 {
21242 struct objfile *objfile = cu->per_objfile->objfile;
21243 LONGEST value;
21244 const gdb_byte *bytes;
21245 struct dwarf2_locexpr_baton *baton;
21246
21247 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21248 sym->print_name (),
21249 &objfile->objfile_obstack, cu,
21250 &value, &bytes, &baton);
21251
21252 if (baton != NULL)
21253 {
21254 SYMBOL_LOCATION_BATON (sym) = baton;
21255 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21256 }
21257 else if (bytes != NULL)
21258 {
21259 SYMBOL_VALUE_BYTES (sym) = bytes;
21260 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21261 }
21262 else
21263 {
21264 SYMBOL_VALUE (sym) = value;
21265 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21266 }
21267 }
21268
21269 /* Return the type of the die in question using its DW_AT_type attribute. */
21270
21271 static struct type *
21272 die_type (struct die_info *die, struct dwarf2_cu *cu)
21273 {
21274 struct attribute *type_attr;
21275
21276 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21277 if (!type_attr)
21278 {
21279 struct objfile *objfile = cu->per_objfile->objfile;
21280 /* A missing DW_AT_type represents a void type. */
21281 return objfile_type (objfile)->builtin_void;
21282 }
21283
21284 return lookup_die_type (die, type_attr, cu);
21285 }
21286
21287 /* True iff CU's producer generates GNAT Ada auxiliary information
21288 that allows to find parallel types through that information instead
21289 of having to do expensive parallel lookups by type name. */
21290
21291 static int
21292 need_gnat_info (struct dwarf2_cu *cu)
21293 {
21294 /* Assume that the Ada compiler was GNAT, which always produces
21295 the auxiliary information. */
21296 return (cu->language == language_ada);
21297 }
21298
21299 /* Return the auxiliary type of the die in question using its
21300 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21301 attribute is not present. */
21302
21303 static struct type *
21304 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21305 {
21306 struct attribute *type_attr;
21307
21308 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21309 if (!type_attr)
21310 return NULL;
21311
21312 return lookup_die_type (die, type_attr, cu);
21313 }
21314
21315 /* If DIE has a descriptive_type attribute, then set the TYPE's
21316 descriptive type accordingly. */
21317
21318 static void
21319 set_descriptive_type (struct type *type, struct die_info *die,
21320 struct dwarf2_cu *cu)
21321 {
21322 struct type *descriptive_type = die_descriptive_type (die, cu);
21323
21324 if (descriptive_type)
21325 {
21326 ALLOCATE_GNAT_AUX_TYPE (type);
21327 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21328 }
21329 }
21330
21331 /* Return the containing type of the die in question using its
21332 DW_AT_containing_type attribute. */
21333
21334 static struct type *
21335 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21336 {
21337 struct attribute *type_attr;
21338 struct objfile *objfile = cu->per_objfile->objfile;
21339
21340 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21341 if (!type_attr)
21342 error (_("Dwarf Error: Problem turning containing type into gdb type "
21343 "[in module %s]"), objfile_name (objfile));
21344
21345 return lookup_die_type (die, type_attr, cu);
21346 }
21347
21348 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21349
21350 static struct type *
21351 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21352 {
21353 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21354 struct objfile *objfile = dwarf2_per_objfile->objfile;
21355 char *saved;
21356
21357 std::string message
21358 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21359 objfile_name (objfile),
21360 sect_offset_str (cu->header.sect_off),
21361 sect_offset_str (die->sect_off));
21362 saved = obstack_strdup (&objfile->objfile_obstack, message);
21363
21364 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21365 }
21366
21367 /* Look up the type of DIE in CU using its type attribute ATTR.
21368 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21369 DW_AT_containing_type.
21370 If there is no type substitute an error marker. */
21371
21372 static struct type *
21373 lookup_die_type (struct die_info *die, const struct attribute *attr,
21374 struct dwarf2_cu *cu)
21375 {
21376 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21377 struct objfile *objfile = dwarf2_per_objfile->objfile;
21378 struct type *this_type;
21379
21380 gdb_assert (attr->name == DW_AT_type
21381 || attr->name == DW_AT_GNAT_descriptive_type
21382 || attr->name == DW_AT_containing_type);
21383
21384 /* First see if we have it cached. */
21385
21386 if (attr->form == DW_FORM_GNU_ref_alt)
21387 {
21388 struct dwarf2_per_cu_data *per_cu;
21389 sect_offset sect_off = attr->get_ref_die_offset ();
21390
21391 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21392 dwarf2_per_objfile);
21393 this_type = get_die_type_at_offset (sect_off, per_cu);
21394 }
21395 else if (attr->form_is_ref ())
21396 {
21397 sect_offset sect_off = attr->get_ref_die_offset ();
21398
21399 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21400 }
21401 else if (attr->form == DW_FORM_ref_sig8)
21402 {
21403 ULONGEST signature = DW_SIGNATURE (attr);
21404
21405 return get_signatured_type (die, signature, cu);
21406 }
21407 else
21408 {
21409 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21410 " at %s [in module %s]"),
21411 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21412 objfile_name (objfile));
21413 return build_error_marker_type (cu, die);
21414 }
21415
21416 /* If not cached we need to read it in. */
21417
21418 if (this_type == NULL)
21419 {
21420 struct die_info *type_die = NULL;
21421 struct dwarf2_cu *type_cu = cu;
21422
21423 if (attr->form_is_ref ())
21424 type_die = follow_die_ref (die, attr, &type_cu);
21425 if (type_die == NULL)
21426 return build_error_marker_type (cu, die);
21427 /* If we find the type now, it's probably because the type came
21428 from an inter-CU reference and the type's CU got expanded before
21429 ours. */
21430 this_type = read_type_die (type_die, type_cu);
21431 }
21432
21433 /* If we still don't have a type use an error marker. */
21434
21435 if (this_type == NULL)
21436 return build_error_marker_type (cu, die);
21437
21438 return this_type;
21439 }
21440
21441 /* Return the type in DIE, CU.
21442 Returns NULL for invalid types.
21443
21444 This first does a lookup in die_type_hash,
21445 and only reads the die in if necessary.
21446
21447 NOTE: This can be called when reading in partial or full symbols. */
21448
21449 static struct type *
21450 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21451 {
21452 struct type *this_type;
21453
21454 this_type = get_die_type (die, cu);
21455 if (this_type)
21456 return this_type;
21457
21458 return read_type_die_1 (die, cu);
21459 }
21460
21461 /* Read the type in DIE, CU.
21462 Returns NULL for invalid types. */
21463
21464 static struct type *
21465 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21466 {
21467 struct type *this_type = NULL;
21468
21469 switch (die->tag)
21470 {
21471 case DW_TAG_class_type:
21472 case DW_TAG_interface_type:
21473 case DW_TAG_structure_type:
21474 case DW_TAG_union_type:
21475 this_type = read_structure_type (die, cu);
21476 break;
21477 case DW_TAG_enumeration_type:
21478 this_type = read_enumeration_type (die, cu);
21479 break;
21480 case DW_TAG_subprogram:
21481 case DW_TAG_subroutine_type:
21482 case DW_TAG_inlined_subroutine:
21483 this_type = read_subroutine_type (die, cu);
21484 break;
21485 case DW_TAG_array_type:
21486 this_type = read_array_type (die, cu);
21487 break;
21488 case DW_TAG_set_type:
21489 this_type = read_set_type (die, cu);
21490 break;
21491 case DW_TAG_pointer_type:
21492 this_type = read_tag_pointer_type (die, cu);
21493 break;
21494 case DW_TAG_ptr_to_member_type:
21495 this_type = read_tag_ptr_to_member_type (die, cu);
21496 break;
21497 case DW_TAG_reference_type:
21498 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21499 break;
21500 case DW_TAG_rvalue_reference_type:
21501 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21502 break;
21503 case DW_TAG_const_type:
21504 this_type = read_tag_const_type (die, cu);
21505 break;
21506 case DW_TAG_volatile_type:
21507 this_type = read_tag_volatile_type (die, cu);
21508 break;
21509 case DW_TAG_restrict_type:
21510 this_type = read_tag_restrict_type (die, cu);
21511 break;
21512 case DW_TAG_string_type:
21513 this_type = read_tag_string_type (die, cu);
21514 break;
21515 case DW_TAG_typedef:
21516 this_type = read_typedef (die, cu);
21517 break;
21518 case DW_TAG_subrange_type:
21519 this_type = read_subrange_type (die, cu);
21520 break;
21521 case DW_TAG_base_type:
21522 this_type = read_base_type (die, cu);
21523 break;
21524 case DW_TAG_unspecified_type:
21525 this_type = read_unspecified_type (die, cu);
21526 break;
21527 case DW_TAG_namespace:
21528 this_type = read_namespace_type (die, cu);
21529 break;
21530 case DW_TAG_module:
21531 this_type = read_module_type (die, cu);
21532 break;
21533 case DW_TAG_atomic_type:
21534 this_type = read_tag_atomic_type (die, cu);
21535 break;
21536 default:
21537 complaint (_("unexpected tag in read_type_die: '%s'"),
21538 dwarf_tag_name (die->tag));
21539 break;
21540 }
21541
21542 return this_type;
21543 }
21544
21545 /* See if we can figure out if the class lives in a namespace. We do
21546 this by looking for a member function; its demangled name will
21547 contain namespace info, if there is any.
21548 Return the computed name or NULL.
21549 Space for the result is allocated on the objfile's obstack.
21550 This is the full-die version of guess_partial_die_structure_name.
21551 In this case we know DIE has no useful parent. */
21552
21553 static const char *
21554 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21555 {
21556 struct die_info *spec_die;
21557 struct dwarf2_cu *spec_cu;
21558 struct die_info *child;
21559 struct objfile *objfile = cu->per_objfile->objfile;
21560
21561 spec_cu = cu;
21562 spec_die = die_specification (die, &spec_cu);
21563 if (spec_die != NULL)
21564 {
21565 die = spec_die;
21566 cu = spec_cu;
21567 }
21568
21569 for (child = die->child;
21570 child != NULL;
21571 child = child->sibling)
21572 {
21573 if (child->tag == DW_TAG_subprogram)
21574 {
21575 const char *linkage_name = dw2_linkage_name (child, cu);
21576
21577 if (linkage_name != NULL)
21578 {
21579 gdb::unique_xmalloc_ptr<char> actual_name
21580 (language_class_name_from_physname (cu->language_defn,
21581 linkage_name));
21582 const char *name = NULL;
21583
21584 if (actual_name != NULL)
21585 {
21586 const char *die_name = dwarf2_name (die, cu);
21587
21588 if (die_name != NULL
21589 && strcmp (die_name, actual_name.get ()) != 0)
21590 {
21591 /* Strip off the class name from the full name.
21592 We want the prefix. */
21593 int die_name_len = strlen (die_name);
21594 int actual_name_len = strlen (actual_name.get ());
21595 const char *ptr = actual_name.get ();
21596
21597 /* Test for '::' as a sanity check. */
21598 if (actual_name_len > die_name_len + 2
21599 && ptr[actual_name_len - die_name_len - 1] == ':')
21600 name = obstack_strndup (
21601 &objfile->per_bfd->storage_obstack,
21602 ptr, actual_name_len - die_name_len - 2);
21603 }
21604 }
21605 return name;
21606 }
21607 }
21608 }
21609
21610 return NULL;
21611 }
21612
21613 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21614 prefix part in such case. See
21615 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21616
21617 static const char *
21618 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21619 {
21620 struct attribute *attr;
21621 const char *base;
21622
21623 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21624 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21625 return NULL;
21626
21627 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21628 return NULL;
21629
21630 attr = dw2_linkage_name_attr (die, cu);
21631 if (attr == NULL || DW_STRING (attr) == NULL)
21632 return NULL;
21633
21634 /* dwarf2_name had to be already called. */
21635 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21636
21637 /* Strip the base name, keep any leading namespaces/classes. */
21638 base = strrchr (DW_STRING (attr), ':');
21639 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21640 return "";
21641
21642 struct objfile *objfile = cu->per_objfile->objfile;
21643 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21644 DW_STRING (attr),
21645 &base[-1] - DW_STRING (attr));
21646 }
21647
21648 /* Return the name of the namespace/class that DIE is defined within,
21649 or "" if we can't tell. The caller should not xfree the result.
21650
21651 For example, if we're within the method foo() in the following
21652 code:
21653
21654 namespace N {
21655 class C {
21656 void foo () {
21657 }
21658 };
21659 }
21660
21661 then determine_prefix on foo's die will return "N::C". */
21662
21663 static const char *
21664 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21665 {
21666 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21667 struct die_info *parent, *spec_die;
21668 struct dwarf2_cu *spec_cu;
21669 struct type *parent_type;
21670 const char *retval;
21671
21672 if (cu->language != language_cplus
21673 && cu->language != language_fortran && cu->language != language_d
21674 && cu->language != language_rust)
21675 return "";
21676
21677 retval = anonymous_struct_prefix (die, cu);
21678 if (retval)
21679 return retval;
21680
21681 /* We have to be careful in the presence of DW_AT_specification.
21682 For example, with GCC 3.4, given the code
21683
21684 namespace N {
21685 void foo() {
21686 // Definition of N::foo.
21687 }
21688 }
21689
21690 then we'll have a tree of DIEs like this:
21691
21692 1: DW_TAG_compile_unit
21693 2: DW_TAG_namespace // N
21694 3: DW_TAG_subprogram // declaration of N::foo
21695 4: DW_TAG_subprogram // definition of N::foo
21696 DW_AT_specification // refers to die #3
21697
21698 Thus, when processing die #4, we have to pretend that we're in
21699 the context of its DW_AT_specification, namely the contex of die
21700 #3. */
21701 spec_cu = cu;
21702 spec_die = die_specification (die, &spec_cu);
21703 if (spec_die == NULL)
21704 parent = die->parent;
21705 else
21706 {
21707 parent = spec_die->parent;
21708 cu = spec_cu;
21709 }
21710
21711 if (parent == NULL)
21712 return "";
21713 else if (parent->building_fullname)
21714 {
21715 const char *name;
21716 const char *parent_name;
21717
21718 /* It has been seen on RealView 2.2 built binaries,
21719 DW_TAG_template_type_param types actually _defined_ as
21720 children of the parent class:
21721
21722 enum E {};
21723 template class <class Enum> Class{};
21724 Class<enum E> class_e;
21725
21726 1: DW_TAG_class_type (Class)
21727 2: DW_TAG_enumeration_type (E)
21728 3: DW_TAG_enumerator (enum1:0)
21729 3: DW_TAG_enumerator (enum2:1)
21730 ...
21731 2: DW_TAG_template_type_param
21732 DW_AT_type DW_FORM_ref_udata (E)
21733
21734 Besides being broken debug info, it can put GDB into an
21735 infinite loop. Consider:
21736
21737 When we're building the full name for Class<E>, we'll start
21738 at Class, and go look over its template type parameters,
21739 finding E. We'll then try to build the full name of E, and
21740 reach here. We're now trying to build the full name of E,
21741 and look over the parent DIE for containing scope. In the
21742 broken case, if we followed the parent DIE of E, we'd again
21743 find Class, and once again go look at its template type
21744 arguments, etc., etc. Simply don't consider such parent die
21745 as source-level parent of this die (it can't be, the language
21746 doesn't allow it), and break the loop here. */
21747 name = dwarf2_name (die, cu);
21748 parent_name = dwarf2_name (parent, cu);
21749 complaint (_("template param type '%s' defined within parent '%s'"),
21750 name ? name : "<unknown>",
21751 parent_name ? parent_name : "<unknown>");
21752 return "";
21753 }
21754 else
21755 switch (parent->tag)
21756 {
21757 case DW_TAG_namespace:
21758 parent_type = read_type_die (parent, cu);
21759 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21760 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21761 Work around this problem here. */
21762 if (cu->language == language_cplus
21763 && strcmp (parent_type->name (), "::") == 0)
21764 return "";
21765 /* We give a name to even anonymous namespaces. */
21766 return parent_type->name ();
21767 case DW_TAG_class_type:
21768 case DW_TAG_interface_type:
21769 case DW_TAG_structure_type:
21770 case DW_TAG_union_type:
21771 case DW_TAG_module:
21772 parent_type = read_type_die (parent, cu);
21773 if (parent_type->name () != NULL)
21774 return parent_type->name ();
21775 else
21776 /* An anonymous structure is only allowed non-static data
21777 members; no typedefs, no member functions, et cetera.
21778 So it does not need a prefix. */
21779 return "";
21780 case DW_TAG_compile_unit:
21781 case DW_TAG_partial_unit:
21782 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21783 if (cu->language == language_cplus
21784 && !dwarf2_per_objfile->per_bfd->types.empty ()
21785 && die->child != NULL
21786 && (die->tag == DW_TAG_class_type
21787 || die->tag == DW_TAG_structure_type
21788 || die->tag == DW_TAG_union_type))
21789 {
21790 const char *name = guess_full_die_structure_name (die, cu);
21791 if (name != NULL)
21792 return name;
21793 }
21794 return "";
21795 case DW_TAG_subprogram:
21796 /* Nested subroutines in Fortran get a prefix with the name
21797 of the parent's subroutine. */
21798 if (cu->language == language_fortran)
21799 {
21800 if ((die->tag == DW_TAG_subprogram)
21801 && (dwarf2_name (parent, cu) != NULL))
21802 return dwarf2_name (parent, cu);
21803 }
21804 return determine_prefix (parent, cu);
21805 case DW_TAG_enumeration_type:
21806 parent_type = read_type_die (parent, cu);
21807 if (TYPE_DECLARED_CLASS (parent_type))
21808 {
21809 if (parent_type->name () != NULL)
21810 return parent_type->name ();
21811 return "";
21812 }
21813 /* Fall through. */
21814 default:
21815 return determine_prefix (parent, cu);
21816 }
21817 }
21818
21819 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21820 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21821 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21822 an obconcat, otherwise allocate storage for the result. The CU argument is
21823 used to determine the language and hence, the appropriate separator. */
21824
21825 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21826
21827 static char *
21828 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21829 int physname, struct dwarf2_cu *cu)
21830 {
21831 const char *lead = "";
21832 const char *sep;
21833
21834 if (suffix == NULL || suffix[0] == '\0'
21835 || prefix == NULL || prefix[0] == '\0')
21836 sep = "";
21837 else if (cu->language == language_d)
21838 {
21839 /* For D, the 'main' function could be defined in any module, but it
21840 should never be prefixed. */
21841 if (strcmp (suffix, "D main") == 0)
21842 {
21843 prefix = "";
21844 sep = "";
21845 }
21846 else
21847 sep = ".";
21848 }
21849 else if (cu->language == language_fortran && physname)
21850 {
21851 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21852 DW_AT_MIPS_linkage_name is preferred and used instead. */
21853
21854 lead = "__";
21855 sep = "_MOD_";
21856 }
21857 else
21858 sep = "::";
21859
21860 if (prefix == NULL)
21861 prefix = "";
21862 if (suffix == NULL)
21863 suffix = "";
21864
21865 if (obs == NULL)
21866 {
21867 char *retval
21868 = ((char *)
21869 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21870
21871 strcpy (retval, lead);
21872 strcat (retval, prefix);
21873 strcat (retval, sep);
21874 strcat (retval, suffix);
21875 return retval;
21876 }
21877 else
21878 {
21879 /* We have an obstack. */
21880 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21881 }
21882 }
21883
21884 /* Get name of a die, return NULL if not found. */
21885
21886 static const char *
21887 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21888 struct objfile *objfile)
21889 {
21890 if (name && cu->language == language_cplus)
21891 {
21892 gdb::unique_xmalloc_ptr<char> canon_name
21893 = cp_canonicalize_string (name);
21894
21895 if (canon_name != nullptr)
21896 name = objfile->intern (canon_name.get ());
21897 }
21898
21899 return name;
21900 }
21901
21902 /* Get name of a die, return NULL if not found.
21903 Anonymous namespaces are converted to their magic string. */
21904
21905 static const char *
21906 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21907 {
21908 struct attribute *attr;
21909 struct objfile *objfile = cu->per_objfile->objfile;
21910
21911 attr = dwarf2_attr (die, DW_AT_name, cu);
21912 if ((!attr || !DW_STRING (attr))
21913 && die->tag != DW_TAG_namespace
21914 && die->tag != DW_TAG_class_type
21915 && die->tag != DW_TAG_interface_type
21916 && die->tag != DW_TAG_structure_type
21917 && die->tag != DW_TAG_union_type)
21918 return NULL;
21919
21920 switch (die->tag)
21921 {
21922 case DW_TAG_compile_unit:
21923 case DW_TAG_partial_unit:
21924 /* Compilation units have a DW_AT_name that is a filename, not
21925 a source language identifier. */
21926 case DW_TAG_enumeration_type:
21927 case DW_TAG_enumerator:
21928 /* These tags always have simple identifiers already; no need
21929 to canonicalize them. */
21930 return DW_STRING (attr);
21931
21932 case DW_TAG_namespace:
21933 if (attr != NULL && DW_STRING (attr) != NULL)
21934 return DW_STRING (attr);
21935 return CP_ANONYMOUS_NAMESPACE_STR;
21936
21937 case DW_TAG_class_type:
21938 case DW_TAG_interface_type:
21939 case DW_TAG_structure_type:
21940 case DW_TAG_union_type:
21941 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21942 structures or unions. These were of the form "._%d" in GCC 4.1,
21943 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21944 and GCC 4.4. We work around this problem by ignoring these. */
21945 if (attr && DW_STRING (attr)
21946 && (startswith (DW_STRING (attr), "._")
21947 || startswith (DW_STRING (attr), "<anonymous")))
21948 return NULL;
21949
21950 /* GCC might emit a nameless typedef that has a linkage name. See
21951 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21952 if (!attr || DW_STRING (attr) == NULL)
21953 {
21954 attr = dw2_linkage_name_attr (die, cu);
21955 if (attr == NULL || DW_STRING (attr) == NULL)
21956 return NULL;
21957
21958 /* Avoid demangling DW_STRING (attr) the second time on a second
21959 call for the same DIE. */
21960 if (!DW_STRING_IS_CANONICAL (attr))
21961 {
21962 gdb::unique_xmalloc_ptr<char> demangled
21963 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21964 if (demangled == nullptr)
21965 return nullptr;
21966
21967 DW_STRING (attr) = objfile->intern (demangled.get ());
21968 DW_STRING_IS_CANONICAL (attr) = 1;
21969 }
21970
21971 /* Strip any leading namespaces/classes, keep only the base name.
21972 DW_AT_name for named DIEs does not contain the prefixes. */
21973 const char *base = strrchr (DW_STRING (attr), ':');
21974 if (base && base > DW_STRING (attr) && base[-1] == ':')
21975 return &base[1];
21976 else
21977 return DW_STRING (attr);
21978 }
21979 break;
21980
21981 default:
21982 break;
21983 }
21984
21985 if (!DW_STRING_IS_CANONICAL (attr))
21986 {
21987 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21988 objfile);
21989 DW_STRING_IS_CANONICAL (attr) = 1;
21990 }
21991 return DW_STRING (attr);
21992 }
21993
21994 /* Return the die that this die in an extension of, or NULL if there
21995 is none. *EXT_CU is the CU containing DIE on input, and the CU
21996 containing the return value on output. */
21997
21998 static struct die_info *
21999 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22000 {
22001 struct attribute *attr;
22002
22003 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22004 if (attr == NULL)
22005 return NULL;
22006
22007 return follow_die_ref (die, attr, ext_cu);
22008 }
22009
22010 static void
22011 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22012 {
22013 unsigned int i;
22014
22015 print_spaces (indent, f);
22016 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22017 dwarf_tag_name (die->tag), die->abbrev,
22018 sect_offset_str (die->sect_off));
22019
22020 if (die->parent != NULL)
22021 {
22022 print_spaces (indent, f);
22023 fprintf_unfiltered (f, " parent at offset: %s\n",
22024 sect_offset_str (die->parent->sect_off));
22025 }
22026
22027 print_spaces (indent, f);
22028 fprintf_unfiltered (f, " has children: %s\n",
22029 dwarf_bool_name (die->child != NULL));
22030
22031 print_spaces (indent, f);
22032 fprintf_unfiltered (f, " attributes:\n");
22033
22034 for (i = 0; i < die->num_attrs; ++i)
22035 {
22036 print_spaces (indent, f);
22037 fprintf_unfiltered (f, " %s (%s) ",
22038 dwarf_attr_name (die->attrs[i].name),
22039 dwarf_form_name (die->attrs[i].form));
22040
22041 switch (die->attrs[i].form)
22042 {
22043 case DW_FORM_addr:
22044 case DW_FORM_addrx:
22045 case DW_FORM_GNU_addr_index:
22046 fprintf_unfiltered (f, "address: ");
22047 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22048 break;
22049 case DW_FORM_block2:
22050 case DW_FORM_block4:
22051 case DW_FORM_block:
22052 case DW_FORM_block1:
22053 fprintf_unfiltered (f, "block: size %s",
22054 pulongest (DW_BLOCK (&die->attrs[i])->size));
22055 break;
22056 case DW_FORM_exprloc:
22057 fprintf_unfiltered (f, "expression: size %s",
22058 pulongest (DW_BLOCK (&die->attrs[i])->size));
22059 break;
22060 case DW_FORM_data16:
22061 fprintf_unfiltered (f, "constant of 16 bytes");
22062 break;
22063 case DW_FORM_ref_addr:
22064 fprintf_unfiltered (f, "ref address: ");
22065 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22066 break;
22067 case DW_FORM_GNU_ref_alt:
22068 fprintf_unfiltered (f, "alt ref address: ");
22069 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22070 break;
22071 case DW_FORM_ref1:
22072 case DW_FORM_ref2:
22073 case DW_FORM_ref4:
22074 case DW_FORM_ref8:
22075 case DW_FORM_ref_udata:
22076 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22077 (long) (DW_UNSND (&die->attrs[i])));
22078 break;
22079 case DW_FORM_data1:
22080 case DW_FORM_data2:
22081 case DW_FORM_data4:
22082 case DW_FORM_data8:
22083 case DW_FORM_udata:
22084 case DW_FORM_sdata:
22085 fprintf_unfiltered (f, "constant: %s",
22086 pulongest (DW_UNSND (&die->attrs[i])));
22087 break;
22088 case DW_FORM_sec_offset:
22089 fprintf_unfiltered (f, "section offset: %s",
22090 pulongest (DW_UNSND (&die->attrs[i])));
22091 break;
22092 case DW_FORM_ref_sig8:
22093 fprintf_unfiltered (f, "signature: %s",
22094 hex_string (DW_SIGNATURE (&die->attrs[i])));
22095 break;
22096 case DW_FORM_string:
22097 case DW_FORM_strp:
22098 case DW_FORM_line_strp:
22099 case DW_FORM_strx:
22100 case DW_FORM_GNU_str_index:
22101 case DW_FORM_GNU_strp_alt:
22102 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22103 DW_STRING (&die->attrs[i])
22104 ? DW_STRING (&die->attrs[i]) : "",
22105 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22106 break;
22107 case DW_FORM_flag:
22108 if (DW_UNSND (&die->attrs[i]))
22109 fprintf_unfiltered (f, "flag: TRUE");
22110 else
22111 fprintf_unfiltered (f, "flag: FALSE");
22112 break;
22113 case DW_FORM_flag_present:
22114 fprintf_unfiltered (f, "flag: TRUE");
22115 break;
22116 case DW_FORM_indirect:
22117 /* The reader will have reduced the indirect form to
22118 the "base form" so this form should not occur. */
22119 fprintf_unfiltered (f,
22120 "unexpected attribute form: DW_FORM_indirect");
22121 break;
22122 case DW_FORM_implicit_const:
22123 fprintf_unfiltered (f, "constant: %s",
22124 plongest (DW_SND (&die->attrs[i])));
22125 break;
22126 default:
22127 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22128 die->attrs[i].form);
22129 break;
22130 }
22131 fprintf_unfiltered (f, "\n");
22132 }
22133 }
22134
22135 static void
22136 dump_die_for_error (struct die_info *die)
22137 {
22138 dump_die_shallow (gdb_stderr, 0, die);
22139 }
22140
22141 static void
22142 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22143 {
22144 int indent = level * 4;
22145
22146 gdb_assert (die != NULL);
22147
22148 if (level >= max_level)
22149 return;
22150
22151 dump_die_shallow (f, indent, die);
22152
22153 if (die->child != NULL)
22154 {
22155 print_spaces (indent, f);
22156 fprintf_unfiltered (f, " Children:");
22157 if (level + 1 < max_level)
22158 {
22159 fprintf_unfiltered (f, "\n");
22160 dump_die_1 (f, level + 1, max_level, die->child);
22161 }
22162 else
22163 {
22164 fprintf_unfiltered (f,
22165 " [not printed, max nesting level reached]\n");
22166 }
22167 }
22168
22169 if (die->sibling != NULL && level > 0)
22170 {
22171 dump_die_1 (f, level, max_level, die->sibling);
22172 }
22173 }
22174
22175 /* This is called from the pdie macro in gdbinit.in.
22176 It's not static so gcc will keep a copy callable from gdb. */
22177
22178 void
22179 dump_die (struct die_info *die, int max_level)
22180 {
22181 dump_die_1 (gdb_stdlog, 0, max_level, die);
22182 }
22183
22184 static void
22185 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22186 {
22187 void **slot;
22188
22189 slot = htab_find_slot_with_hash (cu->die_hash, die,
22190 to_underlying (die->sect_off),
22191 INSERT);
22192
22193 *slot = die;
22194 }
22195
22196 /* Follow reference or signature attribute ATTR of SRC_DIE.
22197 On entry *REF_CU is the CU of SRC_DIE.
22198 On exit *REF_CU is the CU of the result. */
22199
22200 static struct die_info *
22201 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22202 struct dwarf2_cu **ref_cu)
22203 {
22204 struct die_info *die;
22205
22206 if (attr->form_is_ref ())
22207 die = follow_die_ref (src_die, attr, ref_cu);
22208 else if (attr->form == DW_FORM_ref_sig8)
22209 die = follow_die_sig (src_die, attr, ref_cu);
22210 else
22211 {
22212 dump_die_for_error (src_die);
22213 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22214 objfile_name ((*ref_cu)->per_objfile->objfile));
22215 }
22216
22217 return die;
22218 }
22219
22220 /* Follow reference OFFSET.
22221 On entry *REF_CU is the CU of the source die referencing OFFSET.
22222 On exit *REF_CU is the CU of the result.
22223 Returns NULL if OFFSET is invalid. */
22224
22225 static struct die_info *
22226 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22227 struct dwarf2_cu **ref_cu)
22228 {
22229 struct die_info temp_die;
22230 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22231 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22232
22233 gdb_assert (cu->per_cu != NULL);
22234
22235 target_cu = cu;
22236
22237 if (cu->per_cu->is_debug_types)
22238 {
22239 /* .debug_types CUs cannot reference anything outside their CU.
22240 If they need to, they have to reference a signatured type via
22241 DW_FORM_ref_sig8. */
22242 if (!cu->header.offset_in_cu_p (sect_off))
22243 return NULL;
22244 }
22245 else if (offset_in_dwz != cu->per_cu->is_dwz
22246 || !cu->header.offset_in_cu_p (sect_off))
22247 {
22248 struct dwarf2_per_cu_data *per_cu;
22249
22250 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22251 dwarf2_per_objfile);
22252
22253 /* If necessary, add it to the queue and load its DIEs. */
22254 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22255 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22256
22257 target_cu = per_cu->cu;
22258 }
22259 else if (cu->dies == NULL)
22260 {
22261 /* We're loading full DIEs during partial symbol reading. */
22262 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22263 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22264 language_minimal);
22265 }
22266
22267 *ref_cu = target_cu;
22268 temp_die.sect_off = sect_off;
22269
22270 if (target_cu != cu)
22271 target_cu->ancestor = cu;
22272
22273 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22274 &temp_die,
22275 to_underlying (sect_off));
22276 }
22277
22278 /* Follow reference attribute ATTR of SRC_DIE.
22279 On entry *REF_CU is the CU of SRC_DIE.
22280 On exit *REF_CU is the CU of the result. */
22281
22282 static struct die_info *
22283 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22284 struct dwarf2_cu **ref_cu)
22285 {
22286 sect_offset sect_off = attr->get_ref_die_offset ();
22287 struct dwarf2_cu *cu = *ref_cu;
22288 struct die_info *die;
22289
22290 die = follow_die_offset (sect_off,
22291 (attr->form == DW_FORM_GNU_ref_alt
22292 || cu->per_cu->is_dwz),
22293 ref_cu);
22294 if (!die)
22295 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22296 "at %s [in module %s]"),
22297 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22298 objfile_name (cu->per_objfile->objfile));
22299
22300 return die;
22301 }
22302
22303 /* See read.h. */
22304
22305 struct dwarf2_locexpr_baton
22306 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22307 dwarf2_per_cu_data *per_cu,
22308 dwarf2_per_objfile *dwarf2_per_objfile,
22309 CORE_ADDR (*get_frame_pc) (void *baton),
22310 void *baton, bool resolve_abstract_p)
22311 {
22312 struct dwarf2_cu *cu;
22313 struct die_info *die;
22314 struct attribute *attr;
22315 struct dwarf2_locexpr_baton retval;
22316 struct objfile *objfile = dwarf2_per_objfile->objfile;
22317
22318 if (per_cu->cu == NULL)
22319 load_cu (per_cu, dwarf2_per_objfile, false);
22320 cu = per_cu->cu;
22321 if (cu == NULL)
22322 {
22323 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22324 Instead just throw an error, not much else we can do. */
22325 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22326 sect_offset_str (sect_off), objfile_name (objfile));
22327 }
22328
22329 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22330 if (!die)
22331 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22332 sect_offset_str (sect_off), objfile_name (objfile));
22333
22334 attr = dwarf2_attr (die, DW_AT_location, cu);
22335 if (!attr && resolve_abstract_p
22336 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22337 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22338 {
22339 CORE_ADDR pc = (*get_frame_pc) (baton);
22340 CORE_ADDR baseaddr = objfile->text_section_offset ();
22341 struct gdbarch *gdbarch = objfile->arch ();
22342
22343 for (const auto &cand_off
22344 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22345 {
22346 struct dwarf2_cu *cand_cu = cu;
22347 struct die_info *cand
22348 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22349 if (!cand
22350 || !cand->parent
22351 || cand->parent->tag != DW_TAG_subprogram)
22352 continue;
22353
22354 CORE_ADDR pc_low, pc_high;
22355 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22356 if (pc_low == ((CORE_ADDR) -1))
22357 continue;
22358 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22359 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22360 if (!(pc_low <= pc && pc < pc_high))
22361 continue;
22362
22363 die = cand;
22364 attr = dwarf2_attr (die, DW_AT_location, cu);
22365 break;
22366 }
22367 }
22368
22369 if (!attr)
22370 {
22371 /* DWARF: "If there is no such attribute, then there is no effect.".
22372 DATA is ignored if SIZE is 0. */
22373
22374 retval.data = NULL;
22375 retval.size = 0;
22376 }
22377 else if (attr->form_is_section_offset ())
22378 {
22379 struct dwarf2_loclist_baton loclist_baton;
22380 CORE_ADDR pc = (*get_frame_pc) (baton);
22381 size_t size;
22382
22383 fill_in_loclist_baton (cu, &loclist_baton, attr);
22384
22385 retval.data = dwarf2_find_location_expression (&loclist_baton,
22386 &size, pc);
22387 retval.size = size;
22388 }
22389 else
22390 {
22391 if (!attr->form_is_block ())
22392 error (_("Dwarf Error: DIE at %s referenced in module %s "
22393 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22394 sect_offset_str (sect_off), objfile_name (objfile));
22395
22396 retval.data = DW_BLOCK (attr)->data;
22397 retval.size = DW_BLOCK (attr)->size;
22398 }
22399 retval.per_objfile = dwarf2_per_objfile;
22400 retval.per_cu = cu->per_cu;
22401
22402 age_cached_comp_units (dwarf2_per_objfile);
22403
22404 return retval;
22405 }
22406
22407 /* See read.h. */
22408
22409 struct dwarf2_locexpr_baton
22410 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22411 dwarf2_per_cu_data *per_cu,
22412 dwarf2_per_objfile *per_objfile,
22413 CORE_ADDR (*get_frame_pc) (void *baton),
22414 void *baton)
22415 {
22416 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22417
22418 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22419 get_frame_pc, baton);
22420 }
22421
22422 /* Write a constant of a given type as target-ordered bytes into
22423 OBSTACK. */
22424
22425 static const gdb_byte *
22426 write_constant_as_bytes (struct obstack *obstack,
22427 enum bfd_endian byte_order,
22428 struct type *type,
22429 ULONGEST value,
22430 LONGEST *len)
22431 {
22432 gdb_byte *result;
22433
22434 *len = TYPE_LENGTH (type);
22435 result = (gdb_byte *) obstack_alloc (obstack, *len);
22436 store_unsigned_integer (result, *len, byte_order, value);
22437
22438 return result;
22439 }
22440
22441 /* See read.h. */
22442
22443 const gdb_byte *
22444 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22445 dwarf2_per_cu_data *per_cu,
22446 dwarf2_per_objfile *per_objfile,
22447 obstack *obstack,
22448 LONGEST *len)
22449 {
22450 struct dwarf2_cu *cu;
22451 struct die_info *die;
22452 struct attribute *attr;
22453 const gdb_byte *result = NULL;
22454 struct type *type;
22455 LONGEST value;
22456 enum bfd_endian byte_order;
22457 struct objfile *objfile = per_objfile->objfile;
22458
22459 if (per_cu->cu == NULL)
22460 load_cu (per_cu, per_objfile, false);
22461 cu = per_cu->cu;
22462 if (cu == NULL)
22463 {
22464 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22465 Instead just throw an error, not much else we can do. */
22466 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22467 sect_offset_str (sect_off), objfile_name (objfile));
22468 }
22469
22470 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22471 if (!die)
22472 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22473 sect_offset_str (sect_off), objfile_name (objfile));
22474
22475 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22476 if (attr == NULL)
22477 return NULL;
22478
22479 byte_order = (bfd_big_endian (objfile->obfd)
22480 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22481
22482 switch (attr->form)
22483 {
22484 case DW_FORM_addr:
22485 case DW_FORM_addrx:
22486 case DW_FORM_GNU_addr_index:
22487 {
22488 gdb_byte *tem;
22489
22490 *len = cu->header.addr_size;
22491 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22492 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22493 result = tem;
22494 }
22495 break;
22496 case DW_FORM_string:
22497 case DW_FORM_strp:
22498 case DW_FORM_strx:
22499 case DW_FORM_GNU_str_index:
22500 case DW_FORM_GNU_strp_alt:
22501 /* DW_STRING is already allocated on the objfile obstack, point
22502 directly to it. */
22503 result = (const gdb_byte *) DW_STRING (attr);
22504 *len = strlen (DW_STRING (attr));
22505 break;
22506 case DW_FORM_block1:
22507 case DW_FORM_block2:
22508 case DW_FORM_block4:
22509 case DW_FORM_block:
22510 case DW_FORM_exprloc:
22511 case DW_FORM_data16:
22512 result = DW_BLOCK (attr)->data;
22513 *len = DW_BLOCK (attr)->size;
22514 break;
22515
22516 /* The DW_AT_const_value attributes are supposed to carry the
22517 symbol's value "represented as it would be on the target
22518 architecture." By the time we get here, it's already been
22519 converted to host endianness, so we just need to sign- or
22520 zero-extend it as appropriate. */
22521 case DW_FORM_data1:
22522 type = die_type (die, cu);
22523 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22524 if (result == NULL)
22525 result = write_constant_as_bytes (obstack, byte_order,
22526 type, value, len);
22527 break;
22528 case DW_FORM_data2:
22529 type = die_type (die, cu);
22530 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22531 if (result == NULL)
22532 result = write_constant_as_bytes (obstack, byte_order,
22533 type, value, len);
22534 break;
22535 case DW_FORM_data4:
22536 type = die_type (die, cu);
22537 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22538 if (result == NULL)
22539 result = write_constant_as_bytes (obstack, byte_order,
22540 type, value, len);
22541 break;
22542 case DW_FORM_data8:
22543 type = die_type (die, cu);
22544 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22545 if (result == NULL)
22546 result = write_constant_as_bytes (obstack, byte_order,
22547 type, value, len);
22548 break;
22549
22550 case DW_FORM_sdata:
22551 case DW_FORM_implicit_const:
22552 type = die_type (die, cu);
22553 result = write_constant_as_bytes (obstack, byte_order,
22554 type, DW_SND (attr), len);
22555 break;
22556
22557 case DW_FORM_udata:
22558 type = die_type (die, cu);
22559 result = write_constant_as_bytes (obstack, byte_order,
22560 type, DW_UNSND (attr), len);
22561 break;
22562
22563 default:
22564 complaint (_("unsupported const value attribute form: '%s'"),
22565 dwarf_form_name (attr->form));
22566 break;
22567 }
22568
22569 return result;
22570 }
22571
22572 /* See read.h. */
22573
22574 struct type *
22575 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22576 dwarf2_per_cu_data *per_cu,
22577 dwarf2_per_objfile *per_objfile)
22578 {
22579 struct dwarf2_cu *cu;
22580 struct die_info *die;
22581
22582 if (per_cu->cu == NULL)
22583 load_cu (per_cu, per_objfile, false);
22584 cu = per_cu->cu;
22585 if (!cu)
22586 return NULL;
22587
22588 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22589 if (!die)
22590 return NULL;
22591
22592 return die_type (die, cu);
22593 }
22594
22595 /* See read.h. */
22596
22597 struct type *
22598 dwarf2_get_die_type (cu_offset die_offset,
22599 struct dwarf2_per_cu_data *per_cu)
22600 {
22601 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22602 return get_die_type_at_offset (die_offset_sect, per_cu);
22603 }
22604
22605 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22606 On entry *REF_CU is the CU of SRC_DIE.
22607 On exit *REF_CU is the CU of the result.
22608 Returns NULL if the referenced DIE isn't found. */
22609
22610 static struct die_info *
22611 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22612 struct dwarf2_cu **ref_cu)
22613 {
22614 struct die_info temp_die;
22615 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22616 struct die_info *die;
22617 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22618
22619
22620 /* While it might be nice to assert sig_type->type == NULL here,
22621 we can get here for DW_AT_imported_declaration where we need
22622 the DIE not the type. */
22623
22624 /* If necessary, add it to the queue and load its DIEs. */
22625
22626 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22627 language_minimal))
22628 read_signatured_type (sig_type, dwarf2_per_objfile);
22629
22630 sig_cu = sig_type->per_cu.cu;
22631 gdb_assert (sig_cu != NULL);
22632 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22633 temp_die.sect_off = sig_type->type_offset_in_section;
22634 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22635 to_underlying (temp_die.sect_off));
22636 if (die)
22637 {
22638 /* For .gdb_index version 7 keep track of included TUs.
22639 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22640 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22641 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22642 {
22643 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22644 }
22645
22646 *ref_cu = sig_cu;
22647 if (sig_cu != cu)
22648 sig_cu->ancestor = cu;
22649
22650 return die;
22651 }
22652
22653 return NULL;
22654 }
22655
22656 /* Follow signatured type referenced by ATTR in SRC_DIE.
22657 On entry *REF_CU is the CU of SRC_DIE.
22658 On exit *REF_CU is the CU of the result.
22659 The result is the DIE of the type.
22660 If the referenced type cannot be found an error is thrown. */
22661
22662 static struct die_info *
22663 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22664 struct dwarf2_cu **ref_cu)
22665 {
22666 ULONGEST signature = DW_SIGNATURE (attr);
22667 struct signatured_type *sig_type;
22668 struct die_info *die;
22669
22670 gdb_assert (attr->form == DW_FORM_ref_sig8);
22671
22672 sig_type = lookup_signatured_type (*ref_cu, signature);
22673 /* sig_type will be NULL if the signatured type is missing from
22674 the debug info. */
22675 if (sig_type == NULL)
22676 {
22677 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22678 " from DIE at %s [in module %s]"),
22679 hex_string (signature), sect_offset_str (src_die->sect_off),
22680 objfile_name ((*ref_cu)->per_objfile->objfile));
22681 }
22682
22683 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22684 if (die == NULL)
22685 {
22686 dump_die_for_error (src_die);
22687 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22688 " from DIE at %s [in module %s]"),
22689 hex_string (signature), sect_offset_str (src_die->sect_off),
22690 objfile_name ((*ref_cu)->per_objfile->objfile));
22691 }
22692
22693 return die;
22694 }
22695
22696 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22697 reading in and processing the type unit if necessary. */
22698
22699 static struct type *
22700 get_signatured_type (struct die_info *die, ULONGEST signature,
22701 struct dwarf2_cu *cu)
22702 {
22703 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22704 struct signatured_type *sig_type;
22705 struct dwarf2_cu *type_cu;
22706 struct die_info *type_die;
22707 struct type *type;
22708
22709 sig_type = lookup_signatured_type (cu, signature);
22710 /* sig_type will be NULL if the signatured type is missing from
22711 the debug info. */
22712 if (sig_type == NULL)
22713 {
22714 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22715 " from DIE at %s [in module %s]"),
22716 hex_string (signature), sect_offset_str (die->sect_off),
22717 objfile_name (dwarf2_per_objfile->objfile));
22718 return build_error_marker_type (cu, die);
22719 }
22720
22721 /* If we already know the type we're done. */
22722 if (sig_type->type != NULL)
22723 return sig_type->type;
22724
22725 type_cu = cu;
22726 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22727 if (type_die != NULL)
22728 {
22729 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22730 is created. This is important, for example, because for c++ classes
22731 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22732 type = read_type_die (type_die, type_cu);
22733 if (type == NULL)
22734 {
22735 complaint (_("Dwarf Error: Cannot build signatured type %s"
22736 " referenced from DIE at %s [in module %s]"),
22737 hex_string (signature), sect_offset_str (die->sect_off),
22738 objfile_name (dwarf2_per_objfile->objfile));
22739 type = build_error_marker_type (cu, die);
22740 }
22741 }
22742 else
22743 {
22744 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22745 " from DIE at %s [in module %s]"),
22746 hex_string (signature), sect_offset_str (die->sect_off),
22747 objfile_name (dwarf2_per_objfile->objfile));
22748 type = build_error_marker_type (cu, die);
22749 }
22750 sig_type->type = type;
22751
22752 return type;
22753 }
22754
22755 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22756 reading in and processing the type unit if necessary. */
22757
22758 static struct type *
22759 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22760 struct dwarf2_cu *cu) /* ARI: editCase function */
22761 {
22762 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22763 if (attr->form_is_ref ())
22764 {
22765 struct dwarf2_cu *type_cu = cu;
22766 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22767
22768 return read_type_die (type_die, type_cu);
22769 }
22770 else if (attr->form == DW_FORM_ref_sig8)
22771 {
22772 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22773 }
22774 else
22775 {
22776 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22777
22778 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22779 " at %s [in module %s]"),
22780 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22781 objfile_name (dwarf2_per_objfile->objfile));
22782 return build_error_marker_type (cu, die);
22783 }
22784 }
22785
22786 /* Load the DIEs associated with type unit PER_CU into memory. */
22787
22788 static void
22789 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22790 dwarf2_per_objfile *per_objfile)
22791 {
22792 struct signatured_type *sig_type;
22793
22794 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22795 gdb_assert (! per_cu->type_unit_group_p ());
22796
22797 /* We have the per_cu, but we need the signatured_type.
22798 Fortunately this is an easy translation. */
22799 gdb_assert (per_cu->is_debug_types);
22800 sig_type = (struct signatured_type *) per_cu;
22801
22802 gdb_assert (per_cu->cu == NULL);
22803
22804 read_signatured_type (sig_type, per_objfile);
22805
22806 gdb_assert (per_cu->cu != NULL);
22807 }
22808
22809 /* Read in a signatured type and build its CU and DIEs.
22810 If the type is a stub for the real type in a DWO file,
22811 read in the real type from the DWO file as well. */
22812
22813 static void
22814 read_signatured_type (signatured_type *sig_type,
22815 dwarf2_per_objfile *per_objfile)
22816 {
22817 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22818
22819 gdb_assert (per_cu->is_debug_types);
22820 gdb_assert (per_cu->cu == NULL);
22821
22822 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22823
22824 if (!reader.dummy_p)
22825 {
22826 struct dwarf2_cu *cu = reader.cu;
22827 const gdb_byte *info_ptr = reader.info_ptr;
22828
22829 gdb_assert (cu->die_hash == NULL);
22830 cu->die_hash =
22831 htab_create_alloc_ex (cu->header.length / 12,
22832 die_hash,
22833 die_eq,
22834 NULL,
22835 &cu->comp_unit_obstack,
22836 hashtab_obstack_allocate,
22837 dummy_obstack_deallocate);
22838
22839 if (reader.comp_unit_die->has_children)
22840 reader.comp_unit_die->child
22841 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22842 reader.comp_unit_die);
22843 cu->dies = reader.comp_unit_die;
22844 /* comp_unit_die is not stored in die_hash, no need. */
22845
22846 /* We try not to read any attributes in this function, because
22847 not all CUs needed for references have been loaded yet, and
22848 symbol table processing isn't initialized. But we have to
22849 set the CU language, or we won't be able to build types
22850 correctly. Similarly, if we do not read the producer, we can
22851 not apply producer-specific interpretation. */
22852 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22853
22854 reader.keep ();
22855 }
22856
22857 sig_type->per_cu.tu_read = 1;
22858 }
22859
22860 /* Decode simple location descriptions.
22861 Given a pointer to a dwarf block that defines a location, compute
22862 the location and return the value. If COMPUTED is non-null, it is
22863 set to true to indicate that decoding was successful, and false
22864 otherwise. If COMPUTED is null, then this function may emit a
22865 complaint. */
22866
22867 static CORE_ADDR
22868 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22869 {
22870 struct objfile *objfile = cu->per_objfile->objfile;
22871 size_t i;
22872 size_t size = blk->size;
22873 const gdb_byte *data = blk->data;
22874 CORE_ADDR stack[64];
22875 int stacki;
22876 unsigned int bytes_read, unsnd;
22877 gdb_byte op;
22878
22879 if (computed != nullptr)
22880 *computed = false;
22881
22882 i = 0;
22883 stacki = 0;
22884 stack[stacki] = 0;
22885 stack[++stacki] = 0;
22886
22887 while (i < size)
22888 {
22889 op = data[i++];
22890 switch (op)
22891 {
22892 case DW_OP_lit0:
22893 case DW_OP_lit1:
22894 case DW_OP_lit2:
22895 case DW_OP_lit3:
22896 case DW_OP_lit4:
22897 case DW_OP_lit5:
22898 case DW_OP_lit6:
22899 case DW_OP_lit7:
22900 case DW_OP_lit8:
22901 case DW_OP_lit9:
22902 case DW_OP_lit10:
22903 case DW_OP_lit11:
22904 case DW_OP_lit12:
22905 case DW_OP_lit13:
22906 case DW_OP_lit14:
22907 case DW_OP_lit15:
22908 case DW_OP_lit16:
22909 case DW_OP_lit17:
22910 case DW_OP_lit18:
22911 case DW_OP_lit19:
22912 case DW_OP_lit20:
22913 case DW_OP_lit21:
22914 case DW_OP_lit22:
22915 case DW_OP_lit23:
22916 case DW_OP_lit24:
22917 case DW_OP_lit25:
22918 case DW_OP_lit26:
22919 case DW_OP_lit27:
22920 case DW_OP_lit28:
22921 case DW_OP_lit29:
22922 case DW_OP_lit30:
22923 case DW_OP_lit31:
22924 stack[++stacki] = op - DW_OP_lit0;
22925 break;
22926
22927 case DW_OP_reg0:
22928 case DW_OP_reg1:
22929 case DW_OP_reg2:
22930 case DW_OP_reg3:
22931 case DW_OP_reg4:
22932 case DW_OP_reg5:
22933 case DW_OP_reg6:
22934 case DW_OP_reg7:
22935 case DW_OP_reg8:
22936 case DW_OP_reg9:
22937 case DW_OP_reg10:
22938 case DW_OP_reg11:
22939 case DW_OP_reg12:
22940 case DW_OP_reg13:
22941 case DW_OP_reg14:
22942 case DW_OP_reg15:
22943 case DW_OP_reg16:
22944 case DW_OP_reg17:
22945 case DW_OP_reg18:
22946 case DW_OP_reg19:
22947 case DW_OP_reg20:
22948 case DW_OP_reg21:
22949 case DW_OP_reg22:
22950 case DW_OP_reg23:
22951 case DW_OP_reg24:
22952 case DW_OP_reg25:
22953 case DW_OP_reg26:
22954 case DW_OP_reg27:
22955 case DW_OP_reg28:
22956 case DW_OP_reg29:
22957 case DW_OP_reg30:
22958 case DW_OP_reg31:
22959 stack[++stacki] = op - DW_OP_reg0;
22960 if (i < size)
22961 {
22962 if (computed == nullptr)
22963 dwarf2_complex_location_expr_complaint ();
22964 else
22965 return 0;
22966 }
22967 break;
22968
22969 case DW_OP_regx:
22970 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22971 i += bytes_read;
22972 stack[++stacki] = unsnd;
22973 if (i < size)
22974 {
22975 if (computed == nullptr)
22976 dwarf2_complex_location_expr_complaint ();
22977 else
22978 return 0;
22979 }
22980 break;
22981
22982 case DW_OP_addr:
22983 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22984 &bytes_read);
22985 i += bytes_read;
22986 break;
22987
22988 case DW_OP_const1u:
22989 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22990 i += 1;
22991 break;
22992
22993 case DW_OP_const1s:
22994 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22995 i += 1;
22996 break;
22997
22998 case DW_OP_const2u:
22999 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23000 i += 2;
23001 break;
23002
23003 case DW_OP_const2s:
23004 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23005 i += 2;
23006 break;
23007
23008 case DW_OP_const4u:
23009 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23010 i += 4;
23011 break;
23012
23013 case DW_OP_const4s:
23014 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23015 i += 4;
23016 break;
23017
23018 case DW_OP_const8u:
23019 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23020 i += 8;
23021 break;
23022
23023 case DW_OP_constu:
23024 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23025 &bytes_read);
23026 i += bytes_read;
23027 break;
23028
23029 case DW_OP_consts:
23030 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23031 i += bytes_read;
23032 break;
23033
23034 case DW_OP_dup:
23035 stack[stacki + 1] = stack[stacki];
23036 stacki++;
23037 break;
23038
23039 case DW_OP_plus:
23040 stack[stacki - 1] += stack[stacki];
23041 stacki--;
23042 break;
23043
23044 case DW_OP_plus_uconst:
23045 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23046 &bytes_read);
23047 i += bytes_read;
23048 break;
23049
23050 case DW_OP_minus:
23051 stack[stacki - 1] -= stack[stacki];
23052 stacki--;
23053 break;
23054
23055 case DW_OP_deref:
23056 /* If we're not the last op, then we definitely can't encode
23057 this using GDB's address_class enum. This is valid for partial
23058 global symbols, although the variable's address will be bogus
23059 in the psymtab. */
23060 if (i < size)
23061 {
23062 if (computed == nullptr)
23063 dwarf2_complex_location_expr_complaint ();
23064 else
23065 return 0;
23066 }
23067 break;
23068
23069 case DW_OP_GNU_push_tls_address:
23070 case DW_OP_form_tls_address:
23071 /* The top of the stack has the offset from the beginning
23072 of the thread control block at which the variable is located. */
23073 /* Nothing should follow this operator, so the top of stack would
23074 be returned. */
23075 /* This is valid for partial global symbols, but the variable's
23076 address will be bogus in the psymtab. Make it always at least
23077 non-zero to not look as a variable garbage collected by linker
23078 which have DW_OP_addr 0. */
23079 if (i < size)
23080 {
23081 if (computed == nullptr)
23082 dwarf2_complex_location_expr_complaint ();
23083 else
23084 return 0;
23085 }
23086 stack[stacki]++;
23087 break;
23088
23089 case DW_OP_GNU_uninit:
23090 if (computed != nullptr)
23091 return 0;
23092 break;
23093
23094 case DW_OP_addrx:
23095 case DW_OP_GNU_addr_index:
23096 case DW_OP_GNU_const_index:
23097 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23098 &bytes_read);
23099 i += bytes_read;
23100 break;
23101
23102 default:
23103 if (computed == nullptr)
23104 {
23105 const char *name = get_DW_OP_name (op);
23106
23107 if (name)
23108 complaint (_("unsupported stack op: '%s'"),
23109 name);
23110 else
23111 complaint (_("unsupported stack op: '%02x'"),
23112 op);
23113 }
23114
23115 return (stack[stacki]);
23116 }
23117
23118 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23119 outside of the allocated space. Also enforce minimum>0. */
23120 if (stacki >= ARRAY_SIZE (stack) - 1)
23121 {
23122 if (computed == nullptr)
23123 complaint (_("location description stack overflow"));
23124 return 0;
23125 }
23126
23127 if (stacki <= 0)
23128 {
23129 if (computed == nullptr)
23130 complaint (_("location description stack underflow"));
23131 return 0;
23132 }
23133 }
23134
23135 if (computed != nullptr)
23136 *computed = true;
23137 return (stack[stacki]);
23138 }
23139
23140 /* memory allocation interface */
23141
23142 static struct dwarf_block *
23143 dwarf_alloc_block (struct dwarf2_cu *cu)
23144 {
23145 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23146 }
23147
23148 static struct die_info *
23149 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23150 {
23151 struct die_info *die;
23152 size_t size = sizeof (struct die_info);
23153
23154 if (num_attrs > 1)
23155 size += (num_attrs - 1) * sizeof (struct attribute);
23156
23157 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23158 memset (die, 0, sizeof (struct die_info));
23159 return (die);
23160 }
23161
23162 \f
23163
23164 /* Macro support. */
23165
23166 /* An overload of dwarf_decode_macros that finds the correct section
23167 and ensures it is read in before calling the other overload. */
23168
23169 static void
23170 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23171 int section_is_gnu)
23172 {
23173 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23174 struct objfile *objfile = dwarf2_per_objfile->objfile;
23175 const struct line_header *lh = cu->line_header;
23176 unsigned int offset_size = cu->header.offset_size;
23177 struct dwarf2_section_info *section;
23178 const char *section_name;
23179
23180 if (cu->dwo_unit != nullptr)
23181 {
23182 if (section_is_gnu)
23183 {
23184 section = &cu->dwo_unit->dwo_file->sections.macro;
23185 section_name = ".debug_macro.dwo";
23186 }
23187 else
23188 {
23189 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23190 section_name = ".debug_macinfo.dwo";
23191 }
23192 }
23193 else
23194 {
23195 if (section_is_gnu)
23196 {
23197 section = &dwarf2_per_objfile->per_bfd->macro;
23198 section_name = ".debug_macro";
23199 }
23200 else
23201 {
23202 section = &dwarf2_per_objfile->per_bfd->macinfo;
23203 section_name = ".debug_macinfo";
23204 }
23205 }
23206
23207 section->read (objfile);
23208 if (section->buffer == nullptr)
23209 {
23210 complaint (_("missing %s section"), section_name);
23211 return;
23212 }
23213
23214 buildsym_compunit *builder = cu->get_builder ();
23215
23216 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23217 offset_size, offset, section_is_gnu);
23218 }
23219
23220 /* Return the .debug_loc section to use for CU.
23221 For DWO files use .debug_loc.dwo. */
23222
23223 static struct dwarf2_section_info *
23224 cu_debug_loc_section (struct dwarf2_cu *cu)
23225 {
23226 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23227
23228 if (cu->dwo_unit)
23229 {
23230 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23231
23232 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23233 }
23234 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23235 : &dwarf2_per_objfile->per_bfd->loc);
23236 }
23237
23238 /* A helper function that fills in a dwarf2_loclist_baton. */
23239
23240 static void
23241 fill_in_loclist_baton (struct dwarf2_cu *cu,
23242 struct dwarf2_loclist_baton *baton,
23243 const struct attribute *attr)
23244 {
23245 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23246 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23247
23248 section->read (dwarf2_per_objfile->objfile);
23249
23250 baton->per_objfile = dwarf2_per_objfile;
23251 baton->per_cu = cu->per_cu;
23252 gdb_assert (baton->per_cu);
23253 /* We don't know how long the location list is, but make sure we
23254 don't run off the edge of the section. */
23255 baton->size = section->size - DW_UNSND (attr);
23256 baton->data = section->buffer + DW_UNSND (attr);
23257 if (cu->base_address.has_value ())
23258 baton->base_address = *cu->base_address;
23259 else
23260 baton->base_address = 0;
23261 baton->from_dwo = cu->dwo_unit != NULL;
23262 }
23263
23264 static void
23265 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23266 struct dwarf2_cu *cu, int is_block)
23267 {
23268 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23269 struct objfile *objfile = dwarf2_per_objfile->objfile;
23270 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23271
23272 if (attr->form_is_section_offset ()
23273 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23274 the section. If so, fall through to the complaint in the
23275 other branch. */
23276 && DW_UNSND (attr) < section->get_size (objfile))
23277 {
23278 struct dwarf2_loclist_baton *baton;
23279
23280 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23281
23282 fill_in_loclist_baton (cu, baton, attr);
23283
23284 if (!cu->base_address.has_value ())
23285 complaint (_("Location list used without "
23286 "specifying the CU base address."));
23287
23288 SYMBOL_ACLASS_INDEX (sym) = (is_block
23289 ? dwarf2_loclist_block_index
23290 : dwarf2_loclist_index);
23291 SYMBOL_LOCATION_BATON (sym) = baton;
23292 }
23293 else
23294 {
23295 struct dwarf2_locexpr_baton *baton;
23296
23297 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23298 baton->per_objfile = dwarf2_per_objfile;
23299 baton->per_cu = cu->per_cu;
23300 gdb_assert (baton->per_cu);
23301
23302 if (attr->form_is_block ())
23303 {
23304 /* Note that we're just copying the block's data pointer
23305 here, not the actual data. We're still pointing into the
23306 info_buffer for SYM's objfile; right now we never release
23307 that buffer, but when we do clean up properly this may
23308 need to change. */
23309 baton->size = DW_BLOCK (attr)->size;
23310 baton->data = DW_BLOCK (attr)->data;
23311 }
23312 else
23313 {
23314 dwarf2_invalid_attrib_class_complaint ("location description",
23315 sym->natural_name ());
23316 baton->size = 0;
23317 }
23318
23319 SYMBOL_ACLASS_INDEX (sym) = (is_block
23320 ? dwarf2_locexpr_block_index
23321 : dwarf2_locexpr_index);
23322 SYMBOL_LOCATION_BATON (sym) = baton;
23323 }
23324 }
23325
23326 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23327 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23328 CU_HEADERP first. */
23329
23330 static const struct comp_unit_head *
23331 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23332 const struct dwarf2_per_cu_data *per_cu)
23333 {
23334 const gdb_byte *info_ptr;
23335
23336 if (per_cu->cu)
23337 return &per_cu->cu->header;
23338
23339 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23340
23341 memset (cu_headerp, 0, sizeof (*cu_headerp));
23342 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23343 rcuh_kind::COMPILE);
23344
23345 return cu_headerp;
23346 }
23347
23348 /* See read.h. */
23349
23350 int
23351 dwarf2_per_cu_data::addr_size () const
23352 {
23353 struct comp_unit_head cu_header_local;
23354 const struct comp_unit_head *cu_headerp;
23355
23356 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23357
23358 return cu_headerp->addr_size;
23359 }
23360
23361 /* See read.h. */
23362
23363 int
23364 dwarf2_per_cu_data::offset_size () const
23365 {
23366 struct comp_unit_head cu_header_local;
23367 const struct comp_unit_head *cu_headerp;
23368
23369 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23370
23371 return cu_headerp->offset_size;
23372 }
23373
23374 /* See read.h. */
23375
23376 int
23377 dwarf2_per_cu_data::ref_addr_size () const
23378 {
23379 struct comp_unit_head cu_header_local;
23380 const struct comp_unit_head *cu_headerp;
23381
23382 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23383
23384 if (cu_headerp->version == 2)
23385 return cu_headerp->addr_size;
23386 else
23387 return cu_headerp->offset_size;
23388 }
23389
23390 /* See read.h. */
23391
23392 struct type *
23393 dwarf2_cu::addr_type () const
23394 {
23395 struct objfile *objfile = this->per_objfile->objfile;
23396 struct type *void_type = objfile_type (objfile)->builtin_void;
23397 struct type *addr_type = lookup_pointer_type (void_type);
23398 int addr_size = this->per_cu->addr_size ();
23399
23400 if (TYPE_LENGTH (addr_type) == addr_size)
23401 return addr_type;
23402
23403 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23404 return addr_type;
23405 }
23406
23407 /* A helper function for dwarf2_find_containing_comp_unit that returns
23408 the index of the result, and that searches a vector. It will
23409 return a result even if the offset in question does not actually
23410 occur in any CU. This is separate so that it can be unit
23411 tested. */
23412
23413 static int
23414 dwarf2_find_containing_comp_unit
23415 (sect_offset sect_off,
23416 unsigned int offset_in_dwz,
23417 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23418 {
23419 int low, high;
23420
23421 low = 0;
23422 high = all_comp_units.size () - 1;
23423 while (high > low)
23424 {
23425 struct dwarf2_per_cu_data *mid_cu;
23426 int mid = low + (high - low) / 2;
23427
23428 mid_cu = all_comp_units[mid];
23429 if (mid_cu->is_dwz > offset_in_dwz
23430 || (mid_cu->is_dwz == offset_in_dwz
23431 && mid_cu->sect_off + mid_cu->length > sect_off))
23432 high = mid;
23433 else
23434 low = mid + 1;
23435 }
23436 gdb_assert (low == high);
23437 return low;
23438 }
23439
23440 /* Locate the .debug_info compilation unit from CU's objfile which contains
23441 the DIE at OFFSET. Raises an error on failure. */
23442
23443 static struct dwarf2_per_cu_data *
23444 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23445 unsigned int offset_in_dwz,
23446 struct dwarf2_per_objfile *dwarf2_per_objfile)
23447 {
23448 int low
23449 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23450 dwarf2_per_objfile->per_bfd->all_comp_units);
23451 struct dwarf2_per_cu_data *this_cu
23452 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23453
23454 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23455 {
23456 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23457 error (_("Dwarf Error: could not find partial DIE containing "
23458 "offset %s [in module %s]"),
23459 sect_offset_str (sect_off),
23460 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23461
23462 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23463 <= sect_off);
23464 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23465 }
23466 else
23467 {
23468 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23469 && sect_off >= this_cu->sect_off + this_cu->length)
23470 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23471 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23472 return this_cu;
23473 }
23474 }
23475
23476 #if GDB_SELF_TEST
23477
23478 namespace selftests {
23479 namespace find_containing_comp_unit {
23480
23481 static void
23482 run_test ()
23483 {
23484 struct dwarf2_per_cu_data one {};
23485 struct dwarf2_per_cu_data two {};
23486 struct dwarf2_per_cu_data three {};
23487 struct dwarf2_per_cu_data four {};
23488
23489 one.length = 5;
23490 two.sect_off = sect_offset (one.length);
23491 two.length = 7;
23492
23493 three.length = 5;
23494 three.is_dwz = 1;
23495 four.sect_off = sect_offset (three.length);
23496 four.length = 7;
23497 four.is_dwz = 1;
23498
23499 std::vector<dwarf2_per_cu_data *> units;
23500 units.push_back (&one);
23501 units.push_back (&two);
23502 units.push_back (&three);
23503 units.push_back (&four);
23504
23505 int result;
23506
23507 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23508 SELF_CHECK (units[result] == &one);
23509 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23510 SELF_CHECK (units[result] == &one);
23511 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23512 SELF_CHECK (units[result] == &two);
23513
23514 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23515 SELF_CHECK (units[result] == &three);
23516 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23517 SELF_CHECK (units[result] == &three);
23518 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23519 SELF_CHECK (units[result] == &four);
23520 }
23521
23522 }
23523 }
23524
23525 #endif /* GDB_SELF_TEST */
23526
23527 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23528
23529 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23530 dwarf2_per_objfile *per_objfile)
23531 : per_cu (per_cu),
23532 per_objfile (per_objfile),
23533 mark (false),
23534 has_loclist (false),
23535 checked_producer (false),
23536 producer_is_gxx_lt_4_6 (false),
23537 producer_is_gcc_lt_4_3 (false),
23538 producer_is_icc (false),
23539 producer_is_icc_lt_14 (false),
23540 producer_is_codewarrior (false),
23541 processing_has_namespace_info (false)
23542 {
23543 per_cu->cu = this;
23544 }
23545
23546 /* Destroy a dwarf2_cu. */
23547
23548 dwarf2_cu::~dwarf2_cu ()
23549 {
23550 per_cu->cu = NULL;
23551 }
23552
23553 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23554
23555 static void
23556 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23557 enum language pretend_language)
23558 {
23559 struct attribute *attr;
23560
23561 /* Set the language we're debugging. */
23562 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23563 if (attr != nullptr)
23564 set_cu_language (DW_UNSND (attr), cu);
23565 else
23566 {
23567 cu->language = pretend_language;
23568 cu->language_defn = language_def (cu->language);
23569 }
23570
23571 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23572 }
23573
23574 /* Increase the age counter on each cached compilation unit, and free
23575 any that are too old. */
23576
23577 static void
23578 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23579 {
23580 struct dwarf2_per_cu_data *per_cu, **last_chain;
23581
23582 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23583 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23584 while (per_cu != NULL)
23585 {
23586 per_cu->cu->last_used ++;
23587 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23588 dwarf2_mark (per_cu->cu);
23589 per_cu = per_cu->cu->read_in_chain;
23590 }
23591
23592 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23593 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23594 while (per_cu != NULL)
23595 {
23596 struct dwarf2_per_cu_data *next_cu;
23597
23598 next_cu = per_cu->cu->read_in_chain;
23599
23600 if (!per_cu->cu->mark)
23601 {
23602 delete per_cu->cu;
23603 *last_chain = next_cu;
23604 }
23605 else
23606 last_chain = &per_cu->cu->read_in_chain;
23607
23608 per_cu = next_cu;
23609 }
23610 }
23611
23612 /* Remove a single compilation unit from the cache. */
23613
23614 static void
23615 free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23616 dwarf2_per_objfile *dwarf2_per_objfile)
23617 {
23618 struct dwarf2_per_cu_data *per_cu, **last_chain;
23619
23620 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23621 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23622 while (per_cu != NULL)
23623 {
23624 struct dwarf2_per_cu_data *next_cu;
23625
23626 next_cu = per_cu->cu->read_in_chain;
23627
23628 if (per_cu == target_per_cu)
23629 {
23630 delete per_cu->cu;
23631 per_cu->cu = NULL;
23632 *last_chain = next_cu;
23633 break;
23634 }
23635 else
23636 last_chain = &per_cu->cu->read_in_chain;
23637
23638 per_cu = next_cu;
23639 }
23640 }
23641
23642 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23643 We store these in a hash table separate from the DIEs, and preserve them
23644 when the DIEs are flushed out of cache.
23645
23646 The CU "per_cu" pointer is needed because offset alone is not enough to
23647 uniquely identify the type. A file may have multiple .debug_types sections,
23648 or the type may come from a DWO file. Furthermore, while it's more logical
23649 to use per_cu->section+offset, with Fission the section with the data is in
23650 the DWO file but we don't know that section at the point we need it.
23651 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23652 because we can enter the lookup routine, get_die_type_at_offset, from
23653 outside this file, and thus won't necessarily have PER_CU->cu.
23654 Fortunately, PER_CU is stable for the life of the objfile. */
23655
23656 struct dwarf2_per_cu_offset_and_type
23657 {
23658 const struct dwarf2_per_cu_data *per_cu;
23659 sect_offset sect_off;
23660 struct type *type;
23661 };
23662
23663 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23664
23665 static hashval_t
23666 per_cu_offset_and_type_hash (const void *item)
23667 {
23668 const struct dwarf2_per_cu_offset_and_type *ofs
23669 = (const struct dwarf2_per_cu_offset_and_type *) item;
23670
23671 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23672 }
23673
23674 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23675
23676 static int
23677 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23678 {
23679 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23680 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23681 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23682 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23683
23684 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23685 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23686 }
23687
23688 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23689 table if necessary. For convenience, return TYPE.
23690
23691 The DIEs reading must have careful ordering to:
23692 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23693 reading current DIE.
23694 * Not trying to dereference contents of still incompletely read in types
23695 while reading in other DIEs.
23696 * Enable referencing still incompletely read in types just by a pointer to
23697 the type without accessing its fields.
23698
23699 Therefore caller should follow these rules:
23700 * Try to fetch any prerequisite types we may need to build this DIE type
23701 before building the type and calling set_die_type.
23702 * After building type call set_die_type for current DIE as soon as
23703 possible before fetching more types to complete the current type.
23704 * Make the type as complete as possible before fetching more types. */
23705
23706 static struct type *
23707 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23708 {
23709 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23710 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23711 struct objfile *objfile = dwarf2_per_objfile->objfile;
23712 struct attribute *attr;
23713 struct dynamic_prop prop;
23714
23715 /* For Ada types, make sure that the gnat-specific data is always
23716 initialized (if not already set). There are a few types where
23717 we should not be doing so, because the type-specific area is
23718 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23719 where the type-specific area is used to store the floatformat).
23720 But this is not a problem, because the gnat-specific information
23721 is actually not needed for these types. */
23722 if (need_gnat_info (cu)
23723 && type->code () != TYPE_CODE_FUNC
23724 && type->code () != TYPE_CODE_FLT
23725 && type->code () != TYPE_CODE_METHODPTR
23726 && type->code () != TYPE_CODE_MEMBERPTR
23727 && type->code () != TYPE_CODE_METHOD
23728 && !HAVE_GNAT_AUX_INFO (type))
23729 INIT_GNAT_SPECIFIC (type);
23730
23731 /* Read DW_AT_allocated and set in type. */
23732 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23733 if (attr != NULL && attr->form_is_block ())
23734 {
23735 struct type *prop_type = cu->addr_sized_int_type (false);
23736 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23737 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23738 }
23739 else if (attr != NULL)
23740 {
23741 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23742 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23743 sect_offset_str (die->sect_off));
23744 }
23745
23746 /* Read DW_AT_associated and set in type. */
23747 attr = dwarf2_attr (die, DW_AT_associated, cu);
23748 if (attr != NULL && attr->form_is_block ())
23749 {
23750 struct type *prop_type = cu->addr_sized_int_type (false);
23751 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23752 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23753 }
23754 else if (attr != NULL)
23755 {
23756 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23757 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23758 sect_offset_str (die->sect_off));
23759 }
23760
23761 /* Read DW_AT_data_location and set in type. */
23762 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23763 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23764 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23765
23766 if (dwarf2_per_objfile->die_type_hash == NULL)
23767 dwarf2_per_objfile->die_type_hash
23768 = htab_up (htab_create_alloc (127,
23769 per_cu_offset_and_type_hash,
23770 per_cu_offset_and_type_eq,
23771 NULL, xcalloc, xfree));
23772
23773 ofs.per_cu = cu->per_cu;
23774 ofs.sect_off = die->sect_off;
23775 ofs.type = type;
23776 slot = (struct dwarf2_per_cu_offset_and_type **)
23777 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23778 if (*slot)
23779 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23780 sect_offset_str (die->sect_off));
23781 *slot = XOBNEW (&objfile->objfile_obstack,
23782 struct dwarf2_per_cu_offset_and_type);
23783 **slot = ofs;
23784 return type;
23785 }
23786
23787 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23788 or return NULL if the die does not have a saved type. */
23789
23790 static struct type *
23791 get_die_type_at_offset (sect_offset sect_off,
23792 struct dwarf2_per_cu_data *per_cu)
23793 {
23794 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23795 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23796
23797 if (dwarf2_per_objfile->die_type_hash == NULL)
23798 return NULL;
23799
23800 ofs.per_cu = per_cu;
23801 ofs.sect_off = sect_off;
23802 slot = ((struct dwarf2_per_cu_offset_and_type *)
23803 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23804 if (slot)
23805 return slot->type;
23806 else
23807 return NULL;
23808 }
23809
23810 /* Look up the type for DIE in CU in die_type_hash,
23811 or return NULL if DIE does not have a saved type. */
23812
23813 static struct type *
23814 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23815 {
23816 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23817 }
23818
23819 /* Add a dependence relationship from CU to REF_PER_CU. */
23820
23821 static void
23822 dwarf2_add_dependence (struct dwarf2_cu *cu,
23823 struct dwarf2_per_cu_data *ref_per_cu)
23824 {
23825 void **slot;
23826
23827 if (cu->dependencies == NULL)
23828 cu->dependencies
23829 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23830 NULL, &cu->comp_unit_obstack,
23831 hashtab_obstack_allocate,
23832 dummy_obstack_deallocate);
23833
23834 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23835 if (*slot == NULL)
23836 *slot = ref_per_cu;
23837 }
23838
23839 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23840 Set the mark field in every compilation unit in the
23841 cache that we must keep because we are keeping CU. */
23842
23843 static int
23844 dwarf2_mark_helper (void **slot, void *data)
23845 {
23846 struct dwarf2_per_cu_data *per_cu;
23847
23848 per_cu = (struct dwarf2_per_cu_data *) *slot;
23849
23850 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23851 reading of the chain. As such dependencies remain valid it is not much
23852 useful to track and undo them during QUIT cleanups. */
23853 if (per_cu->cu == NULL)
23854 return 1;
23855
23856 if (per_cu->cu->mark)
23857 return 1;
23858 per_cu->cu->mark = true;
23859
23860 if (per_cu->cu->dependencies != NULL)
23861 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23862
23863 return 1;
23864 }
23865
23866 /* Set the mark field in CU and in every other compilation unit in the
23867 cache that we must keep because we are keeping CU. */
23868
23869 static void
23870 dwarf2_mark (struct dwarf2_cu *cu)
23871 {
23872 if (cu->mark)
23873 return;
23874 cu->mark = true;
23875 if (cu->dependencies != NULL)
23876 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23877 }
23878
23879 static void
23880 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23881 {
23882 while (per_cu)
23883 {
23884 per_cu->cu->mark = false;
23885 per_cu = per_cu->cu->read_in_chain;
23886 }
23887 }
23888
23889 /* Trivial hash function for partial_die_info: the hash value of a DIE
23890 is its offset in .debug_info for this objfile. */
23891
23892 static hashval_t
23893 partial_die_hash (const void *item)
23894 {
23895 const struct partial_die_info *part_die
23896 = (const struct partial_die_info *) item;
23897
23898 return to_underlying (part_die->sect_off);
23899 }
23900
23901 /* Trivial comparison function for partial_die_info structures: two DIEs
23902 are equal if they have the same offset. */
23903
23904 static int
23905 partial_die_eq (const void *item_lhs, const void *item_rhs)
23906 {
23907 const struct partial_die_info *part_die_lhs
23908 = (const struct partial_die_info *) item_lhs;
23909 const struct partial_die_info *part_die_rhs
23910 = (const struct partial_die_info *) item_rhs;
23911
23912 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23913 }
23914
23915 struct cmd_list_element *set_dwarf_cmdlist;
23916 struct cmd_list_element *show_dwarf_cmdlist;
23917
23918 static void
23919 show_check_physname (struct ui_file *file, int from_tty,
23920 struct cmd_list_element *c, const char *value)
23921 {
23922 fprintf_filtered (file,
23923 _("Whether to check \"physname\" is %s.\n"),
23924 value);
23925 }
23926
23927 void _initialize_dwarf2_read ();
23928 void
23929 _initialize_dwarf2_read ()
23930 {
23931 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23932 Set DWARF specific variables.\n\
23933 Configure DWARF variables such as the cache size."),
23934 &set_dwarf_cmdlist, "maintenance set dwarf ",
23935 0/*allow-unknown*/, &maintenance_set_cmdlist);
23936
23937 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23938 Show DWARF specific variables.\n\
23939 Show DWARF variables such as the cache size."),
23940 &show_dwarf_cmdlist, "maintenance show dwarf ",
23941 0/*allow-unknown*/, &maintenance_show_cmdlist);
23942
23943 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23944 &dwarf_max_cache_age, _("\
23945 Set the upper bound on the age of cached DWARF compilation units."), _("\
23946 Show the upper bound on the age of cached DWARF compilation units."), _("\
23947 A higher limit means that cached compilation units will be stored\n\
23948 in memory longer, and more total memory will be used. Zero disables\n\
23949 caching, which can slow down startup."),
23950 NULL,
23951 show_dwarf_max_cache_age,
23952 &set_dwarf_cmdlist,
23953 &show_dwarf_cmdlist);
23954
23955 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23956 Set debugging of the DWARF reader."), _("\
23957 Show debugging of the DWARF reader."), _("\
23958 When enabled (non-zero), debugging messages are printed during DWARF\n\
23959 reading and symtab expansion. A value of 1 (one) provides basic\n\
23960 information. A value greater than 1 provides more verbose information."),
23961 NULL,
23962 NULL,
23963 &setdebuglist, &showdebuglist);
23964
23965 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23966 Set debugging of the DWARF DIE reader."), _("\
23967 Show debugging of the DWARF DIE reader."), _("\
23968 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23969 The value is the maximum depth to print."),
23970 NULL,
23971 NULL,
23972 &setdebuglist, &showdebuglist);
23973
23974 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23975 Set debugging of the dwarf line reader."), _("\
23976 Show debugging of the dwarf line reader."), _("\
23977 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23978 A value of 1 (one) provides basic information.\n\
23979 A value greater than 1 provides more verbose information."),
23980 NULL,
23981 NULL,
23982 &setdebuglist, &showdebuglist);
23983
23984 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23985 Set cross-checking of \"physname\" code against demangler."), _("\
23986 Show cross-checking of \"physname\" code against demangler."), _("\
23987 When enabled, GDB's internal \"physname\" code is checked against\n\
23988 the demangler."),
23989 NULL, show_check_physname,
23990 &setdebuglist, &showdebuglist);
23991
23992 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23993 no_class, &use_deprecated_index_sections, _("\
23994 Set whether to use deprecated gdb_index sections."), _("\
23995 Show whether to use deprecated gdb_index sections."), _("\
23996 When enabled, deprecated .gdb_index sections are used anyway.\n\
23997 Normally they are ignored either because of a missing feature or\n\
23998 performance issue.\n\
23999 Warning: This option must be enabled before gdb reads the file."),
24000 NULL,
24001 NULL,
24002 &setlist, &showlist);
24003
24004 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24005 &dwarf2_locexpr_funcs);
24006 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24007 &dwarf2_loclist_funcs);
24008
24009 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24010 &dwarf2_block_frame_base_locexpr_funcs);
24011 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24012 &dwarf2_block_frame_base_loclist_funcs);
24013
24014 #if GDB_SELF_TEST
24015 selftests::register_test ("dw2_expand_symtabs_matching",
24016 selftests::dw2_expand_symtabs_matching::run_test);
24017 selftests::register_test ("dwarf2_find_containing_comp_unit",
24018 selftests::find_containing_comp_unit::run_test);
24019 #endif
24020 }