4c8a0717c785b63126713703d944c4f5261af449
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 gdb::optional<CORE_ADDR> base_address;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 gdb::optional<ULONGEST> addr_base;
514
515 /* The DW_AT_rnglists_base attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE.
517 Also note that the value is zero in the non-DWO case so this value can
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
524 ULONGEST ranges_base = 0;
525
526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab. */
611 struct dwarf2_per_cu_data per_cu;
612
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
616 std::vector<signatured_type *> *tus;
617
618 /* The compunit symtab.
619 Type units in a group needn't all be defined in the same source file,
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
622
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
625
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635 };
636
637 /* These sections are what may appear in a (real or virtual) DWO file. */
638
639 struct dwo_sections
640 {
641 struct dwarf2_section_info abbrev;
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
644 struct dwarf2_section_info loclists;
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
651 std::vector<dwarf2_section_info> types;
652 };
653
654 /* CUs/TUs in DWP/DWO files. */
655
656 struct dwo_unit
657 {
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
667 struct dwarf2_section_info *section;
668
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675 };
676
677 /* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681 enum dwp_v2_section_ids
682 {
683 DW_SECT_MIN = 1
684 };
685
686 /* Data for one DWO file.
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
696
697 struct dwo_file
698 {
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
706 const char *dwo_name = nullptr;
707
708 /* The DW_AT_comp_dir attribute. */
709 const char *comp_dir = nullptr;
710
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 gdb_bfd_ref_ptr dbfd;
714
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
718 struct dwo_sections sections {};
719
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
724 htab_up cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_up tus;
729 };
730
731 /* These sections are what may appear in a DWP file. */
732
733 struct dwp_sections
734 {
735 /* These are used by both DWP version 1 and 2. */
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
756 };
757
758 /* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
760
761 struct virtual_v1_dwo_sections
762 {
763 struct dwarf2_section_info abbrev;
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 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 struct dwarf2_section_info info_or_types;
772 };
773
774 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779 struct virtual_v2_dwo_sections
780 {
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803 };
804
805 /* Contents of DWP hash tables. */
806
807 struct dwp_hash_table
808 {
809 uint32_t version, nr_columns;
810 uint32_t nr_units, nr_slots;
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822 #define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
834 };
835
836 /* Data for one DWP file. */
837
838 struct dwp_file
839 {
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
846 /* Name of the file. */
847 const char *name;
848
849 /* File format version. */
850 int version = 0;
851
852 /* The bfd. */
853 gdb_bfd_ref_ptr dbfd;
854
855 /* Section info for this file. */
856 struct dwp_sections sections {};
857
858 /* Table of CUs in the file. */
859 const struct dwp_hash_table *cus = nullptr;
860
861 /* Table of TUs in the file. */
862 const struct dwp_hash_table *tus = nullptr;
863
864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
865 htab_up loaded_cus;
866 htab_up loaded_tus;
867
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
872 };
873
874 /* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
877 struct exists to abstract away the constant parameters of die reading. */
878
879 struct die_reader_specs
880 {
881 /* The bfd of die_section. */
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
888 struct dwo_file *dwo_file;
889
890 /* The section the die comes from.
891 This is either .debug_info or .debug_types, or the .dwo variants. */
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
895 const gdb_byte *buffer;
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
899
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
902 };
903
904 /* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907 class cutu_reader : public die_reader_specs
908 {
909 public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
913 int use_existing_cu,
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
924 bool dummy_p = false;
925
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
930 private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
932 int use_existing_cu);
933
934 struct dwarf2_per_cu_data *m_this_cu;
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942 };
943
944 /* When we construct a partial symbol table entry we only
945 need this much information. */
946 struct partial_die_info : public allocate_on_obstack
947 {
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
965 /* Offset of this DIE. */
966 const sect_offset sect_off;
967
968 /* DWARF-2 tag for this DIE. */
969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
970
971 /* Assorted flags describing the data found in this DIE. */
972 const unsigned int has_children : 1;
973
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
979 unsigned int may_be_inlined : 1;
980
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
997 /* Flag set if fixup has been called on this die. */
998 unsigned int fixup_called : 1;
999
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
1006 /* The name of this DIE. Normally the value of DW_AT_name, but
1007 sometimes a default name for unnamed DIEs. */
1008 const char *name = nullptr;
1009
1010 /* The linkage name, if present. */
1011 const char *linkage_name = nullptr;
1012
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
1016 const char *scope = nullptr;
1017
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
1025 sect_offset sect_off;
1026 } d {};
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
1031
1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1033 DW_AT_sibling, if any. */
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
1036 const gdb_byte *sibling = nullptr;
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
1041 sect_offset spec_offset {};
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
1078 };
1079
1080 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083 static int bits_per_byte = 8;
1084
1085 struct variant_part_builder;
1086
1087 /* When reading a variant, we track a bit more information about the
1088 field, and store it in an object of this type. */
1089
1090 struct variant_field
1091 {
1092 int first_field = -1;
1093 int last_field = -1;
1094
1095 /* A variant can contain other variant parts. */
1096 std::vector<variant_part_builder> variant_parts;
1097
1098 /* If we see a DW_TAG_variant, then this will be set if this is the
1099 default branch. */
1100 bool default_branch = false;
1101 /* If we see a DW_AT_discr_value, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value = 0;
1104 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1105 data. */
1106 struct dwarf_block *discr_list_data = nullptr;
1107 };
1108
1109 /* This represents a DW_TAG_variant_part. */
1110
1111 struct variant_part_builder
1112 {
1113 /* The offset of the discriminant field. */
1114 sect_offset discriminant_offset {};
1115
1116 /* Variants that are direct children of this variant part. */
1117 std::vector<variant_field> variants;
1118
1119 /* True if we're currently reading a variant. */
1120 bool processing_variant = false;
1121 };
1122
1123 struct nextfield
1124 {
1125 int accessibility = 0;
1126 int virtuality = 0;
1127 /* Variant parts need to find the discriminant, which is a DIE
1128 reference. We track the section offset of each field to make
1129 this link. */
1130 sect_offset offset;
1131 struct field field {};
1132 };
1133
1134 struct fnfieldlist
1135 {
1136 const char *name = nullptr;
1137 std::vector<struct fn_field> fnfields;
1138 };
1139
1140 /* The routines that read and process dies for a C struct or C++ class
1141 pass lists of data member fields and lists of member function fields
1142 in an instance of a field_info structure, as defined below. */
1143 struct field_info
1144 {
1145 /* List of data member and baseclasses fields. */
1146 std::vector<struct nextfield> fields;
1147 std::vector<struct nextfield> baseclasses;
1148
1149 /* Set if the accessibility of one of the fields is not public. */
1150 int non_public_fields = 0;
1151
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1155 std::vector<struct fnfieldlist> fnfieldlists;
1156
1157 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1158 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1159 std::vector<struct decl_field> typedef_field_list;
1160
1161 /* Nested types defined by this class and the number of elements in this
1162 list. */
1163 std::vector<struct decl_field> nested_types_list;
1164
1165 /* If non-null, this is the variant part we are currently
1166 reading. */
1167 variant_part_builder *current_variant_part = nullptr;
1168 /* This holds all the top-level variant parts attached to the type
1169 we're reading. */
1170 std::vector<variant_part_builder> variant_parts;
1171
1172 /* Return the total number of fields (including baseclasses). */
1173 int nfields () const
1174 {
1175 return fields.size () + baseclasses.size ();
1176 }
1177 };
1178
1179 /* Loaded secondary compilation units are kept in memory until they
1180 have not been referenced for the processing of this many
1181 compilation units. Set this to zero to disable caching. Cache
1182 sizes of up to at least twenty will improve startup time for
1183 typical inter-CU-reference binaries, at an obvious memory cost. */
1184 static int dwarf_max_cache_age = 5;
1185 static void
1186 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1187 struct cmd_list_element *c, const char *value)
1188 {
1189 fprintf_filtered (file, _("The upper bound on the age of cached "
1190 "DWARF compilation units is %s.\n"),
1191 value);
1192 }
1193 \f
1194 /* local function prototypes */
1195
1196 static void dwarf2_find_base_address (struct die_info *die,
1197 struct dwarf2_cu *cu);
1198
1199 static dwarf2_psymtab *create_partial_symtab
1200 (struct dwarf2_per_cu_data *per_cu, const char *name);
1201
1202 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1203 const gdb_byte *info_ptr,
1204 struct die_info *type_unit_die);
1205
1206 static void dwarf2_build_psymtabs_hard
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1208
1209 static void scan_partial_symbols (struct partial_die_info *,
1210 CORE_ADDR *, CORE_ADDR *,
1211 int, struct dwarf2_cu *);
1212
1213 static void add_partial_symbol (struct partial_die_info *,
1214 struct dwarf2_cu *);
1215
1216 static void add_partial_namespace (struct partial_die_info *pdi,
1217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1218 int set_addrmap, struct dwarf2_cu *cu);
1219
1220 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1221 CORE_ADDR *highpc, int set_addrmap,
1222 struct dwarf2_cu *cu);
1223
1224 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1225 struct dwarf2_cu *cu);
1226
1227 static void add_partial_subprogram (struct partial_die_info *pdi,
1228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1229 int need_pc, struct dwarf2_cu *cu);
1230
1231 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1232
1233 static struct partial_die_info *load_partial_dies
1234 (const struct die_reader_specs *, const gdb_byte *, int);
1235
1236 /* A pair of partial_die_info and compilation unit. */
1237 struct cu_partial_die_info
1238 {
1239 /* The compilation unit of the partial_die_info. */
1240 struct dwarf2_cu *cu;
1241 /* A partial_die_info. */
1242 struct partial_die_info *pdi;
1243
1244 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1245 : cu (cu),
1246 pdi (pdi)
1247 { /* Nothing. */ }
1248
1249 private:
1250 cu_partial_die_info () = delete;
1251 };
1252
1253 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1254 struct dwarf2_cu *);
1255
1256 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1257 struct attribute *, struct attr_abbrev *,
1258 const gdb_byte *, bool *need_reprocess);
1259
1260 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1261 struct attribute *attr);
1262
1263 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1264
1265 static sect_offset read_abbrev_offset
1266 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1267 struct dwarf2_section_info *, sect_offset);
1268
1269 static const char *read_indirect_string
1270 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1271 const struct comp_unit_head *, unsigned int *);
1272
1273 static const char *read_indirect_string_at_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1275
1276 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1277 const gdb_byte *,
1278 unsigned int *);
1279
1280 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1281 ULONGEST str_index);
1282
1283 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1284 ULONGEST str_index);
1285
1286 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1287
1288 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
1290
1291 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1292 struct dwarf2_cu *cu);
1293
1294 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1295
1296 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1297 struct dwarf2_cu *cu);
1298
1299 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1300
1301 static struct die_info *die_specification (struct die_info *die,
1302 struct dwarf2_cu **);
1303
1304 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1305 struct dwarf2_cu *cu);
1306
1307 static void dwarf_decode_lines (struct line_header *, const char *,
1308 struct dwarf2_cu *, dwarf2_psymtab *,
1309 CORE_ADDR, int decode_mapping);
1310
1311 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1312 const char *);
1313
1314 static struct symbol *new_symbol (struct die_info *, struct type *,
1315 struct dwarf2_cu *, struct symbol * = NULL);
1316
1317 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1318 struct dwarf2_cu *);
1319
1320 static void dwarf2_const_value_attr (const struct attribute *attr,
1321 struct type *type,
1322 const char *name,
1323 struct obstack *obstack,
1324 struct dwarf2_cu *cu, LONGEST *value,
1325 const gdb_byte **bytes,
1326 struct dwarf2_locexpr_baton **baton);
1327
1328 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1329
1330 static int need_gnat_info (struct dwarf2_cu *);
1331
1332 static struct type *die_descriptive_type (struct die_info *,
1333 struct dwarf2_cu *);
1334
1335 static void set_descriptive_type (struct type *, struct die_info *,
1336 struct dwarf2_cu *);
1337
1338 static struct type *die_containing_type (struct die_info *,
1339 struct dwarf2_cu *);
1340
1341 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1342 struct dwarf2_cu *);
1343
1344 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1345
1346 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1347
1348 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1349
1350 static char *typename_concat (struct obstack *obs, const char *prefix,
1351 const char *suffix, int physname,
1352 struct dwarf2_cu *cu);
1353
1354 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1355
1356 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1357
1358 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1359
1360 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1361
1362 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1365
1366 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *, dwarf2_psymtab *);
1368
1369 /* Return the .debug_loclists section to use for cu. */
1370 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1371
1372 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1373 values. Keep the items ordered with increasing constraints compliance. */
1374 enum pc_bounds_kind
1375 {
1376 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1377 PC_BOUNDS_NOT_PRESENT,
1378
1379 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1380 were present but they do not form a valid range of PC addresses. */
1381 PC_BOUNDS_INVALID,
1382
1383 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1384 PC_BOUNDS_RANGES,
1385
1386 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1387 PC_BOUNDS_HIGH_LOW,
1388 };
1389
1390 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1391 CORE_ADDR *, CORE_ADDR *,
1392 struct dwarf2_cu *,
1393 dwarf2_psymtab *);
1394
1395 static void get_scope_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *);
1398
1399 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1400 CORE_ADDR, struct dwarf2_cu *);
1401
1402 static void dwarf2_add_field (struct field_info *, struct die_info *,
1403 struct dwarf2_cu *);
1404
1405 static void dwarf2_attach_fields_to_type (struct field_info *,
1406 struct type *, struct dwarf2_cu *);
1407
1408 static void dwarf2_add_member_fn (struct field_info *,
1409 struct die_info *, struct type *,
1410 struct dwarf2_cu *);
1411
1412 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1413 struct type *,
1414 struct dwarf2_cu *);
1415
1416 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1417
1418 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1419
1420 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1421
1422 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1423
1424 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1425
1426 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1427
1428 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1429
1430 static struct type *read_module_type (struct die_info *die,
1431 struct dwarf2_cu *cu);
1432
1433 static const char *namespace_name (struct die_info *die,
1434 int *is_anonymous, struct dwarf2_cu *);
1435
1436 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1437
1438 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1439 bool * = nullptr);
1440
1441 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1442 struct dwarf2_cu *);
1443
1444 static struct die_info *read_die_and_siblings_1
1445 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1446 struct die_info *);
1447
1448 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1449 const gdb_byte *info_ptr,
1450 const gdb_byte **new_info_ptr,
1451 struct die_info *parent);
1452
1453 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1454 struct die_info **, const gdb_byte *,
1455 int);
1456
1457 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1458 struct die_info **, const gdb_byte *);
1459
1460 static void process_die (struct die_info *, struct dwarf2_cu *);
1461
1462 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1463 struct objfile *);
1464
1465 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1466
1467 static const char *dwarf2_full_name (const char *name,
1468 struct die_info *die,
1469 struct dwarf2_cu *cu);
1470
1471 static const char *dwarf2_physname (const char *name, struct die_info *die,
1472 struct dwarf2_cu *cu);
1473
1474 static struct die_info *dwarf2_extension (struct die_info *die,
1475 struct dwarf2_cu **);
1476
1477 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479 static void dump_die_for_error (struct die_info *);
1480
1481 static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
1483
1484 /*static*/ void dump_die (struct die_info *, int max_level);
1485
1486 static void store_in_ref_table (struct die_info *,
1487 struct dwarf2_cu *);
1488
1489 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1490 const struct attribute *,
1491 struct dwarf2_cu **);
1492
1493 static struct die_info *follow_die_ref (struct die_info *,
1494 const struct attribute *,
1495 struct dwarf2_cu **);
1496
1497 static struct die_info *follow_die_sig (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504 static struct type *get_DW_AT_signature_type (struct die_info *,
1505 const struct attribute *,
1506 struct dwarf2_cu *);
1507
1508 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1509
1510 static void read_signatured_type (struct signatured_type *);
1511
1512 static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
1514 struct dynamic_prop *prop, struct type *type);
1515
1516 /* memory allocation interface */
1517
1518 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1519
1520 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1521
1522 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1523
1524 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
1526 const struct attribute *attr);
1527
1528 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1529 struct symbol *sym,
1530 struct dwarf2_cu *cu,
1531 int is_block);
1532
1533 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
1536
1537 static hashval_t partial_die_hash (const void *item);
1538
1539 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
1541 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
1544
1545 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
1548
1549 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1550
1551 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1552
1553 static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1555
1556 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1557
1558 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1559
1560 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1561 enum language);
1562
1563 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
1565
1566 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
1569 static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
1572 static void dwarf2_mark (struct dwarf2_cu *);
1573
1574 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
1576 static struct type *get_die_type_at_offset (sect_offset,
1577 struct dwarf2_per_cu_data *);
1578
1579 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1580
1581 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
1584 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1585
1586 /* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591 class dwarf2_queue_guard
1592 {
1593 public:
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->queue, empty);
1606 }
1607
1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1609
1610 private:
1611 dwarf2_per_objfile *m_per_objfile;
1612 };
1613
1614 dwarf2_queue_item::~dwarf2_queue_item ()
1615 {
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624 }
1625
1626 /* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629 struct file_and_directory
1630 {
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643 };
1644
1645 static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
1647
1648 static htab_up allocate_signatured_type_table ();
1649
1650 static htab_up allocate_dwo_unit_table ();
1651
1652 static struct dwo_unit *lookup_dwo_unit_in_dwp
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
1655 ULONGEST signature, int is_debug_types);
1656
1657 static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1659
1660 static struct dwo_unit *lookup_dwo_comp_unit
1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1662
1663 static struct dwo_unit *lookup_dwo_type_unit
1664 (struct signatured_type *, const char *, const char *);
1665
1666 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
1668 /* A unique pointer to a dwo_file. */
1669
1670 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1671
1672 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1673
1674 static void check_producer (struct dwarf2_cu *cu);
1675
1676 static void free_line_header_voidp (void *arg);
1677 \f
1678 /* Various complaints about symbol reading that don't abort the process. */
1679
1680 static void
1681 dwarf2_debug_line_missing_file_complaint (void)
1682 {
1683 complaint (_(".debug_line section has line data without a file"));
1684 }
1685
1686 static void
1687 dwarf2_debug_line_missing_end_sequence_complaint (void)
1688 {
1689 complaint (_(".debug_line section has line "
1690 "program sequence without an end"));
1691 }
1692
1693 static void
1694 dwarf2_complex_location_expr_complaint (void)
1695 {
1696 complaint (_("location expression too complex"));
1697 }
1698
1699 static void
1700 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1701 int arg3)
1702 {
1703 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1704 arg1, arg2, arg3);
1705 }
1706
1707 static void
1708 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1709 {
1710 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1711 arg1, arg2);
1712 }
1713
1714 /* Hash function for line_header_hash. */
1715
1716 static hashval_t
1717 line_header_hash (const struct line_header *ofs)
1718 {
1719 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1720 }
1721
1722 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1723
1724 static hashval_t
1725 line_header_hash_voidp (const void *item)
1726 {
1727 const struct line_header *ofs = (const struct line_header *) item;
1728
1729 return line_header_hash (ofs);
1730 }
1731
1732 /* Equality function for line_header_hash. */
1733
1734 static int
1735 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1736 {
1737 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1738 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1739
1740 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1741 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1742 }
1743
1744 \f
1745
1746 /* See declaration. */
1747
1748 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1749 const dwarf2_debug_sections *names,
1750 bool can_copy_)
1751 : objfile (objfile_),
1752 can_copy (can_copy_)
1753 {
1754 if (names == NULL)
1755 names = &dwarf2_elf_names;
1756
1757 bfd *obfd = objfile->obfd;
1758
1759 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1760 locate_sections (obfd, sec, *names);
1761 }
1762
1763 dwarf2_per_objfile::~dwarf2_per_objfile ()
1764 {
1765 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1766 free_cached_comp_units ();
1767
1768 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1769 per_cu->imported_symtabs_free ();
1770
1771 for (signatured_type *sig_type : all_type_units)
1772 sig_type->per_cu.imported_symtabs_free ();
1773
1774 /* Everything else should be on the objfile obstack. */
1775 }
1776
1777 /* See declaration. */
1778
1779 void
1780 dwarf2_per_objfile::free_cached_comp_units ()
1781 {
1782 dwarf2_per_cu_data *per_cu = read_in_chain;
1783 dwarf2_per_cu_data **last_chain = &read_in_chain;
1784 while (per_cu != NULL)
1785 {
1786 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1787
1788 delete per_cu->cu;
1789 *last_chain = next_cu;
1790 per_cu = next_cu;
1791 }
1792 }
1793
1794 /* A helper class that calls free_cached_comp_units on
1795 destruction. */
1796
1797 class free_cached_comp_units
1798 {
1799 public:
1800
1801 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1802 : m_per_objfile (per_objfile)
1803 {
1804 }
1805
1806 ~free_cached_comp_units ()
1807 {
1808 m_per_objfile->free_cached_comp_units ();
1809 }
1810
1811 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1812
1813 private:
1814
1815 dwarf2_per_objfile *m_per_objfile;
1816 };
1817
1818 /* Try to locate the sections we need for DWARF 2 debugging
1819 information and return true if we have enough to do something.
1820 NAMES points to the dwarf2 section names, or is NULL if the standard
1821 ELF names are used. CAN_COPY is true for formats where symbol
1822 interposition is possible and so symbol values must follow copy
1823 relocation rules. */
1824
1825 int
1826 dwarf2_has_info (struct objfile *objfile,
1827 const struct dwarf2_debug_sections *names,
1828 bool can_copy)
1829 {
1830 if (objfile->flags & OBJF_READNEVER)
1831 return 0;
1832
1833 struct dwarf2_per_objfile *dwarf2_per_objfile
1834 = get_dwarf2_per_objfile (objfile);
1835
1836 if (dwarf2_per_objfile == NULL)
1837 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1838 names,
1839 can_copy);
1840
1841 return (!dwarf2_per_objfile->info.is_virtual
1842 && dwarf2_per_objfile->info.s.section != NULL
1843 && !dwarf2_per_objfile->abbrev.is_virtual
1844 && dwarf2_per_objfile->abbrev.s.section != NULL);
1845 }
1846
1847 /* When loading sections, we look either for uncompressed section or for
1848 compressed section names. */
1849
1850 static int
1851 section_is_p (const char *section_name,
1852 const struct dwarf2_section_names *names)
1853 {
1854 if (names->normal != NULL
1855 && strcmp (section_name, names->normal) == 0)
1856 return 1;
1857 if (names->compressed != NULL
1858 && strcmp (section_name, names->compressed) == 0)
1859 return 1;
1860 return 0;
1861 }
1862
1863 /* See declaration. */
1864
1865 void
1866 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1867 const dwarf2_debug_sections &names)
1868 {
1869 flagword aflag = bfd_section_flags (sectp);
1870
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1872 {
1873 }
1874 else if (elf_section_data (sectp)->this_hdr.sh_size
1875 > bfd_get_file_size (abfd))
1876 {
1877 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1878 warning (_("Discarding section %s which has a section size (%s"
1879 ") larger than the file size [in module %s]"),
1880 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1881 bfd_get_filename (abfd));
1882 }
1883 else if (section_is_p (sectp->name, &names.info))
1884 {
1885 this->info.s.section = sectp;
1886 this->info.size = bfd_section_size (sectp);
1887 }
1888 else if (section_is_p (sectp->name, &names.abbrev))
1889 {
1890 this->abbrev.s.section = sectp;
1891 this->abbrev.size = bfd_section_size (sectp);
1892 }
1893 else if (section_is_p (sectp->name, &names.line))
1894 {
1895 this->line.s.section = sectp;
1896 this->line.size = bfd_section_size (sectp);
1897 }
1898 else if (section_is_p (sectp->name, &names.loc))
1899 {
1900 this->loc.s.section = sectp;
1901 this->loc.size = bfd_section_size (sectp);
1902 }
1903 else if (section_is_p (sectp->name, &names.loclists))
1904 {
1905 this->loclists.s.section = sectp;
1906 this->loclists.size = bfd_section_size (sectp);
1907 }
1908 else if (section_is_p (sectp->name, &names.macinfo))
1909 {
1910 this->macinfo.s.section = sectp;
1911 this->macinfo.size = bfd_section_size (sectp);
1912 }
1913 else if (section_is_p (sectp->name, &names.macro))
1914 {
1915 this->macro.s.section = sectp;
1916 this->macro.size = bfd_section_size (sectp);
1917 }
1918 else if (section_is_p (sectp->name, &names.str))
1919 {
1920 this->str.s.section = sectp;
1921 this->str.size = bfd_section_size (sectp);
1922 }
1923 else if (section_is_p (sectp->name, &names.str_offsets))
1924 {
1925 this->str_offsets.s.section = sectp;
1926 this->str_offsets.size = bfd_section_size (sectp);
1927 }
1928 else if (section_is_p (sectp->name, &names.line_str))
1929 {
1930 this->line_str.s.section = sectp;
1931 this->line_str.size = bfd_section_size (sectp);
1932 }
1933 else if (section_is_p (sectp->name, &names.addr))
1934 {
1935 this->addr.s.section = sectp;
1936 this->addr.size = bfd_section_size (sectp);
1937 }
1938 else if (section_is_p (sectp->name, &names.frame))
1939 {
1940 this->frame.s.section = sectp;
1941 this->frame.size = bfd_section_size (sectp);
1942 }
1943 else if (section_is_p (sectp->name, &names.eh_frame))
1944 {
1945 this->eh_frame.s.section = sectp;
1946 this->eh_frame.size = bfd_section_size (sectp);
1947 }
1948 else if (section_is_p (sectp->name, &names.ranges))
1949 {
1950 this->ranges.s.section = sectp;
1951 this->ranges.size = bfd_section_size (sectp);
1952 }
1953 else if (section_is_p (sectp->name, &names.rnglists))
1954 {
1955 this->rnglists.s.section = sectp;
1956 this->rnglists.size = bfd_section_size (sectp);
1957 }
1958 else if (section_is_p (sectp->name, &names.types))
1959 {
1960 struct dwarf2_section_info type_section;
1961
1962 memset (&type_section, 0, sizeof (type_section));
1963 type_section.s.section = sectp;
1964 type_section.size = bfd_section_size (sectp);
1965
1966 this->types.push_back (type_section);
1967 }
1968 else if (section_is_p (sectp->name, &names.gdb_index))
1969 {
1970 this->gdb_index.s.section = sectp;
1971 this->gdb_index.size = bfd_section_size (sectp);
1972 }
1973 else if (section_is_p (sectp->name, &names.debug_names))
1974 {
1975 this->debug_names.s.section = sectp;
1976 this->debug_names.size = bfd_section_size (sectp);
1977 }
1978 else if (section_is_p (sectp->name, &names.debug_aranges))
1979 {
1980 this->debug_aranges.s.section = sectp;
1981 this->debug_aranges.size = bfd_section_size (sectp);
1982 }
1983
1984 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1985 && bfd_section_vma (sectp) == 0)
1986 this->has_section_at_zero = true;
1987 }
1988
1989 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1990 SECTION_NAME. */
1991
1992 void
1993 dwarf2_get_section_info (struct objfile *objfile,
1994 enum dwarf2_section_enum sect,
1995 asection **sectp, const gdb_byte **bufp,
1996 bfd_size_type *sizep)
1997 {
1998 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1999 struct dwarf2_section_info *info;
2000
2001 /* We may see an objfile without any DWARF, in which case we just
2002 return nothing. */
2003 if (data == NULL)
2004 {
2005 *sectp = NULL;
2006 *bufp = NULL;
2007 *sizep = 0;
2008 return;
2009 }
2010 switch (sect)
2011 {
2012 case DWARF2_DEBUG_FRAME:
2013 info = &data->frame;
2014 break;
2015 case DWARF2_EH_FRAME:
2016 info = &data->eh_frame;
2017 break;
2018 default:
2019 gdb_assert_not_reached ("unexpected section");
2020 }
2021
2022 info->read (objfile);
2023
2024 *sectp = info->get_bfd_section ();
2025 *bufp = info->buffer;
2026 *sizep = info->size;
2027 }
2028
2029 /* A helper function to find the sections for a .dwz file. */
2030
2031 static void
2032 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2033 {
2034 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2035
2036 /* Note that we only support the standard ELF names, because .dwz
2037 is ELF-only (at the time of writing). */
2038 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2039 {
2040 dwz_file->abbrev.s.section = sectp;
2041 dwz_file->abbrev.size = bfd_section_size (sectp);
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2044 {
2045 dwz_file->info.s.section = sectp;
2046 dwz_file->info.size = bfd_section_size (sectp);
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2049 {
2050 dwz_file->str.s.section = sectp;
2051 dwz_file->str.size = bfd_section_size (sectp);
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2054 {
2055 dwz_file->line.s.section = sectp;
2056 dwz_file->line.size = bfd_section_size (sectp);
2057 }
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2059 {
2060 dwz_file->macro.s.section = sectp;
2061 dwz_file->macro.size = bfd_section_size (sectp);
2062 }
2063 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2064 {
2065 dwz_file->gdb_index.s.section = sectp;
2066 dwz_file->gdb_index.size = bfd_section_size (sectp);
2067 }
2068 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2069 {
2070 dwz_file->debug_names.s.section = sectp;
2071 dwz_file->debug_names.size = bfd_section_size (sectp);
2072 }
2073 }
2074
2075 /* See dwarf2read.h. */
2076
2077 struct dwz_file *
2078 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2079 {
2080 const char *filename;
2081 bfd_size_type buildid_len_arg;
2082 size_t buildid_len;
2083 bfd_byte *buildid;
2084
2085 if (dwarf2_per_objfile->dwz_file != NULL)
2086 return dwarf2_per_objfile->dwz_file.get ();
2087
2088 bfd_set_error (bfd_error_no_error);
2089 gdb::unique_xmalloc_ptr<char> data
2090 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2091 &buildid_len_arg, &buildid));
2092 if (data == NULL)
2093 {
2094 if (bfd_get_error () == bfd_error_no_error)
2095 return NULL;
2096 error (_("could not read '.gnu_debugaltlink' section: %s"),
2097 bfd_errmsg (bfd_get_error ()));
2098 }
2099
2100 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2101
2102 buildid_len = (size_t) buildid_len_arg;
2103
2104 filename = data.get ();
2105
2106 std::string abs_storage;
2107 if (!IS_ABSOLUTE_PATH (filename))
2108 {
2109 gdb::unique_xmalloc_ptr<char> abs
2110 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2111
2112 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2113 filename = abs_storage.c_str ();
2114 }
2115
2116 /* First try the file name given in the section. If that doesn't
2117 work, try to use the build-id instead. */
2118 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2119 if (dwz_bfd != NULL)
2120 {
2121 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2122 dwz_bfd.reset (nullptr);
2123 }
2124
2125 if (dwz_bfd == NULL)
2126 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2127
2128 if (dwz_bfd == nullptr)
2129 {
2130 gdb::unique_xmalloc_ptr<char> alt_filename;
2131 const char *origname = dwarf2_per_objfile->objfile->original_name;
2132
2133 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2134 buildid_len,
2135 origname,
2136 &alt_filename));
2137
2138 if (fd.get () >= 0)
2139 {
2140 /* File successfully retrieved from server. */
2141 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2142
2143 if (dwz_bfd == nullptr)
2144 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2145 alt_filename.get ());
2146 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2147 dwz_bfd.reset (nullptr);
2148 }
2149 }
2150
2151 if (dwz_bfd == NULL)
2152 error (_("could not find '.gnu_debugaltlink' file for %s"),
2153 objfile_name (dwarf2_per_objfile->objfile));
2154
2155 std::unique_ptr<struct dwz_file> result
2156 (new struct dwz_file (std::move (dwz_bfd)));
2157
2158 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2159 result.get ());
2160
2161 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2162 result->dwz_bfd.get ());
2163 dwarf2_per_objfile->dwz_file = std::move (result);
2164 return dwarf2_per_objfile->dwz_file.get ();
2165 }
2166 \f
2167 /* DWARF quick_symbols_functions support. */
2168
2169 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2170 unique line tables, so we maintain a separate table of all .debug_line
2171 derived entries to support the sharing.
2172 All the quick functions need is the list of file names. We discard the
2173 line_header when we're done and don't need to record it here. */
2174 struct quick_file_names
2175 {
2176 /* The data used to construct the hash key. */
2177 struct stmt_list_hash hash;
2178
2179 /* The number of entries in file_names, real_names. */
2180 unsigned int num_file_names;
2181
2182 /* The file names from the line table, after being run through
2183 file_full_name. */
2184 const char **file_names;
2185
2186 /* The file names from the line table after being run through
2187 gdb_realpath. These are computed lazily. */
2188 const char **real_names;
2189 };
2190
2191 /* When using the index (and thus not using psymtabs), each CU has an
2192 object of this type. This is used to hold information needed by
2193 the various "quick" methods. */
2194 struct dwarf2_per_cu_quick_data
2195 {
2196 /* The file table. This can be NULL if there was no file table
2197 or it's currently not read in.
2198 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2199 struct quick_file_names *file_names;
2200
2201 /* The corresponding symbol table. This is NULL if symbols for this
2202 CU have not yet been read. */
2203 struct compunit_symtab *compunit_symtab;
2204
2205 /* A temporary mark bit used when iterating over all CUs in
2206 expand_symtabs_matching. */
2207 unsigned int mark : 1;
2208
2209 /* True if we've tried to read the file table and found there isn't one.
2210 There will be no point in trying to read it again next time. */
2211 unsigned int no_file_data : 1;
2212 };
2213
2214 /* Utility hash function for a stmt_list_hash. */
2215
2216 static hashval_t
2217 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2218 {
2219 hashval_t v = 0;
2220
2221 if (stmt_list_hash->dwo_unit != NULL)
2222 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2223 v += to_underlying (stmt_list_hash->line_sect_off);
2224 return v;
2225 }
2226
2227 /* Utility equality function for a stmt_list_hash. */
2228
2229 static int
2230 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2231 const struct stmt_list_hash *rhs)
2232 {
2233 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2234 return 0;
2235 if (lhs->dwo_unit != NULL
2236 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2237 return 0;
2238
2239 return lhs->line_sect_off == rhs->line_sect_off;
2240 }
2241
2242 /* Hash function for a quick_file_names. */
2243
2244 static hashval_t
2245 hash_file_name_entry (const void *e)
2246 {
2247 const struct quick_file_names *file_data
2248 = (const struct quick_file_names *) e;
2249
2250 return hash_stmt_list_entry (&file_data->hash);
2251 }
2252
2253 /* Equality function for a quick_file_names. */
2254
2255 static int
2256 eq_file_name_entry (const void *a, const void *b)
2257 {
2258 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2259 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2260
2261 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2262 }
2263
2264 /* Delete function for a quick_file_names. */
2265
2266 static void
2267 delete_file_name_entry (void *e)
2268 {
2269 struct quick_file_names *file_data = (struct quick_file_names *) e;
2270 int i;
2271
2272 for (i = 0; i < file_data->num_file_names; ++i)
2273 {
2274 xfree ((void*) file_data->file_names[i]);
2275 if (file_data->real_names)
2276 xfree ((void*) file_data->real_names[i]);
2277 }
2278
2279 /* The space for the struct itself lives on objfile_obstack,
2280 so we don't free it here. */
2281 }
2282
2283 /* Create a quick_file_names hash table. */
2284
2285 static htab_up
2286 create_quick_file_names_table (unsigned int nr_initial_entries)
2287 {
2288 return htab_up (htab_create_alloc (nr_initial_entries,
2289 hash_file_name_entry, eq_file_name_entry,
2290 delete_file_name_entry, xcalloc, xfree));
2291 }
2292
2293 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2294 have to be created afterwards. You should call age_cached_comp_units after
2295 processing PER_CU->CU. dw2_setup must have been already called. */
2296
2297 static void
2298 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2299 {
2300 if (per_cu->is_debug_types)
2301 load_full_type_unit (per_cu);
2302 else
2303 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2304
2305 if (per_cu->cu == NULL)
2306 return; /* Dummy CU. */
2307
2308 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2309 }
2310
2311 /* Read in the symbols for PER_CU. */
2312
2313 static void
2314 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2315 {
2316 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2317
2318 /* Skip type_unit_groups, reading the type units they contain
2319 is handled elsewhere. */
2320 if (per_cu->type_unit_group_p ())
2321 return;
2322
2323 /* The destructor of dwarf2_queue_guard frees any entries left on
2324 the queue. After this point we're guaranteed to leave this function
2325 with the dwarf queue empty. */
2326 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2327
2328 if (dwarf2_per_objfile->using_index
2329 ? per_cu->v.quick->compunit_symtab == NULL
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2331 {
2332 queue_comp_unit (per_cu, language_minimal);
2333 load_cu (per_cu, skip_partial);
2334
2335 /* If we just loaded a CU from a DWO, and we're working with an index
2336 that may badly handle TUs, load all the TUs in that DWO as well.
2337 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2338 if (!per_cu->is_debug_types
2339 && per_cu->cu != NULL
2340 && per_cu->cu->dwo_unit != NULL
2341 && dwarf2_per_objfile->index_table != NULL
2342 && dwarf2_per_objfile->index_table->version <= 7
2343 /* DWP files aren't supported yet. */
2344 && get_dwp_file (dwarf2_per_objfile) == NULL)
2345 queue_and_load_all_dwo_tus (per_cu);
2346 }
2347
2348 process_queue (dwarf2_per_objfile);
2349
2350 /* Age the cache, releasing compilation units that have not
2351 been used recently. */
2352 age_cached_comp_units (dwarf2_per_objfile);
2353 }
2354
2355 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2358
2359 static struct compunit_symtab *
2360 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2361 {
2362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2363
2364 gdb_assert (dwarf2_per_objfile->using_index);
2365 if (!per_cu->v.quick->compunit_symtab)
2366 {
2367 free_cached_comp_units freer (dwarf2_per_objfile);
2368 scoped_restore decrementer = increment_reading_symtab ();
2369 dw2_do_instantiate_symtab (per_cu, skip_partial);
2370 process_cu_includes (dwarf2_per_objfile);
2371 }
2372
2373 return per_cu->v.quick->compunit_symtab;
2374 }
2375
2376 /* See declaration. */
2377
2378 dwarf2_per_cu_data *
2379 dwarf2_per_objfile::get_cutu (int index)
2380 {
2381 if (index >= this->all_comp_units.size ())
2382 {
2383 index -= this->all_comp_units.size ();
2384 gdb_assert (index < this->all_type_units.size ());
2385 return &this->all_type_units[index]->per_cu;
2386 }
2387
2388 return this->all_comp_units[index];
2389 }
2390
2391 /* See declaration. */
2392
2393 dwarf2_per_cu_data *
2394 dwarf2_per_objfile::get_cu (int index)
2395 {
2396 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2397
2398 return this->all_comp_units[index];
2399 }
2400
2401 /* See declaration. */
2402
2403 signatured_type *
2404 dwarf2_per_objfile::get_tu (int index)
2405 {
2406 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2407
2408 return this->all_type_units[index];
2409 }
2410
2411 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2412 objfile_obstack, and constructed with the specified field
2413 values. */
2414
2415 static dwarf2_per_cu_data *
2416 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2417 struct dwarf2_section_info *section,
2418 int is_dwz,
2419 sect_offset sect_off, ULONGEST length)
2420 {
2421 struct objfile *objfile = dwarf2_per_objfile->objfile;
2422 dwarf2_per_cu_data *the_cu
2423 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2424 struct dwarf2_per_cu_data);
2425 the_cu->sect_off = sect_off;
2426 the_cu->length = length;
2427 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2428 the_cu->section = section;
2429 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2430 struct dwarf2_per_cu_quick_data);
2431 the_cu->is_dwz = is_dwz;
2432 return the_cu;
2433 }
2434
2435 /* A helper for create_cus_from_index that handles a given list of
2436 CUs. */
2437
2438 static void
2439 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2440 const gdb_byte *cu_list, offset_type n_elements,
2441 struct dwarf2_section_info *section,
2442 int is_dwz)
2443 {
2444 for (offset_type i = 0; i < n_elements; i += 2)
2445 {
2446 gdb_static_assert (sizeof (ULONGEST) >= 8);
2447
2448 sect_offset sect_off
2449 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2450 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2451 cu_list += 2 * 8;
2452
2453 dwarf2_per_cu_data *per_cu
2454 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2455 sect_off, length);
2456 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2457 }
2458 }
2459
2460 /* Read the CU list from the mapped index, and use it to create all
2461 the CU objects for this objfile. */
2462
2463 static void
2464 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2465 const gdb_byte *cu_list, offset_type cu_list_elements,
2466 const gdb_byte *dwz_list, offset_type dwz_elements)
2467 {
2468 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2469 dwarf2_per_objfile->all_comp_units.reserve
2470 ((cu_list_elements + dwz_elements) / 2);
2471
2472 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2473 &dwarf2_per_objfile->info, 0);
2474
2475 if (dwz_elements == 0)
2476 return;
2477
2478 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2479 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2480 &dwz->info, 1);
2481 }
2482
2483 /* Create the signatured type hash table from the index. */
2484
2485 static void
2486 create_signatured_type_table_from_index
2487 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2488 struct dwarf2_section_info *section,
2489 const gdb_byte *bytes,
2490 offset_type elements)
2491 {
2492 struct objfile *objfile = dwarf2_per_objfile->objfile;
2493
2494 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2495 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2496
2497 htab_up sig_types_hash = allocate_signatured_type_table ();
2498
2499 for (offset_type i = 0; i < elements; i += 3)
2500 {
2501 struct signatured_type *sig_type;
2502 ULONGEST signature;
2503 void **slot;
2504 cu_offset type_offset_in_tu;
2505
2506 gdb_static_assert (sizeof (ULONGEST) >= 8);
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2509 type_offset_in_tu
2510 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2511 BFD_ENDIAN_LITTLE);
2512 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2513 bytes += 3 * 8;
2514
2515 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2516 struct signatured_type);
2517 sig_type->signature = signature;
2518 sig_type->type_offset_in_tu = type_offset_in_tu;
2519 sig_type->per_cu.is_debug_types = 1;
2520 sig_type->per_cu.section = section;
2521 sig_type->per_cu.sect_off = sect_off;
2522 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2523 sig_type->per_cu.v.quick
2524 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2525 struct dwarf2_per_cu_quick_data);
2526
2527 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2528 *slot = sig_type;
2529
2530 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2531 }
2532
2533 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2534 }
2535
2536 /* Create the signatured type hash table from .debug_names. */
2537
2538 static void
2539 create_signatured_type_table_from_debug_names
2540 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2541 const mapped_debug_names &map,
2542 struct dwarf2_section_info *section,
2543 struct dwarf2_section_info *abbrev_section)
2544 {
2545 struct objfile *objfile = dwarf2_per_objfile->objfile;
2546
2547 section->read (objfile);
2548 abbrev_section->read (objfile);
2549
2550 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2551 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2552
2553 htab_up sig_types_hash = allocate_signatured_type_table ();
2554
2555 for (uint32_t i = 0; i < map.tu_count; ++i)
2556 {
2557 struct signatured_type *sig_type;
2558 void **slot;
2559
2560 sect_offset sect_off
2561 = (sect_offset) (extract_unsigned_integer
2562 (map.tu_table_reordered + i * map.offset_size,
2563 map.offset_size,
2564 map.dwarf5_byte_order));
2565
2566 comp_unit_head cu_header;
2567 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2568 abbrev_section,
2569 section->buffer + to_underlying (sect_off),
2570 rcuh_kind::TYPE);
2571
2572 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2573 struct signatured_type);
2574 sig_type->signature = cu_header.signature;
2575 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2576 sig_type->per_cu.is_debug_types = 1;
2577 sig_type->per_cu.section = section;
2578 sig_type->per_cu.sect_off = sect_off;
2579 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2580 sig_type->per_cu.v.quick
2581 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2582 struct dwarf2_per_cu_quick_data);
2583
2584 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2585 *slot = sig_type;
2586
2587 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2588 }
2589
2590 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2591 }
2592
2593 /* Read the address map data from the mapped index, and use it to
2594 populate the objfile's psymtabs_addrmap. */
2595
2596 static void
2597 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2598 struct mapped_index *index)
2599 {
2600 struct objfile *objfile = dwarf2_per_objfile->objfile;
2601 struct gdbarch *gdbarch = objfile->arch ();
2602 const gdb_byte *iter, *end;
2603 struct addrmap *mutable_map;
2604 CORE_ADDR baseaddr;
2605
2606 auto_obstack temp_obstack;
2607
2608 mutable_map = addrmap_create_mutable (&temp_obstack);
2609
2610 iter = index->address_table.data ();
2611 end = iter + index->address_table.size ();
2612
2613 baseaddr = objfile->text_section_offset ();
2614
2615 while (iter < end)
2616 {
2617 ULONGEST hi, lo, cu_index;
2618 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2619 iter += 8;
2620 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2621 iter += 8;
2622 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2623 iter += 4;
2624
2625 if (lo > hi)
2626 {
2627 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2628 hex_string (lo), hex_string (hi));
2629 continue;
2630 }
2631
2632 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2633 {
2634 complaint (_(".gdb_index address table has invalid CU number %u"),
2635 (unsigned) cu_index);
2636 continue;
2637 }
2638
2639 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2640 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2641 addrmap_set_empty (mutable_map, lo, hi - 1,
2642 dwarf2_per_objfile->get_cu (cu_index));
2643 }
2644
2645 objfile->partial_symtabs->psymtabs_addrmap
2646 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2647 }
2648
2649 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2650 populate the objfile's psymtabs_addrmap. */
2651
2652 static void
2653 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2654 struct dwarf2_section_info *section)
2655 {
2656 struct objfile *objfile = dwarf2_per_objfile->objfile;
2657 bfd *abfd = objfile->obfd;
2658 struct gdbarch *gdbarch = objfile->arch ();
2659 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2660
2661 auto_obstack temp_obstack;
2662 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2663
2664 std::unordered_map<sect_offset,
2665 dwarf2_per_cu_data *,
2666 gdb::hash_enum<sect_offset>>
2667 debug_info_offset_to_per_cu;
2668 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2669 {
2670 const auto insertpair
2671 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2672 if (!insertpair.second)
2673 {
2674 warning (_("Section .debug_aranges in %s has duplicate "
2675 "debug_info_offset %s, ignoring .debug_aranges."),
2676 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2677 return;
2678 }
2679 }
2680
2681 section->read (objfile);
2682
2683 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2684
2685 const gdb_byte *addr = section->buffer;
2686
2687 while (addr < section->buffer + section->size)
2688 {
2689 const gdb_byte *const entry_addr = addr;
2690 unsigned int bytes_read;
2691
2692 const LONGEST entry_length = read_initial_length (abfd, addr,
2693 &bytes_read);
2694 addr += bytes_read;
2695
2696 const gdb_byte *const entry_end = addr + entry_length;
2697 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2698 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2699 if (addr + entry_length > section->buffer + section->size)
2700 {
2701 warning (_("Section .debug_aranges in %s entry at offset %s "
2702 "length %s exceeds section length %s, "
2703 "ignoring .debug_aranges."),
2704 objfile_name (objfile),
2705 plongest (entry_addr - section->buffer),
2706 plongest (bytes_read + entry_length),
2707 pulongest (section->size));
2708 return;
2709 }
2710
2711 /* The version number. */
2712 const uint16_t version = read_2_bytes (abfd, addr);
2713 addr += 2;
2714 if (version != 2)
2715 {
2716 warning (_("Section .debug_aranges in %s entry at offset %s "
2717 "has unsupported version %d, ignoring .debug_aranges."),
2718 objfile_name (objfile),
2719 plongest (entry_addr - section->buffer), version);
2720 return;
2721 }
2722
2723 const uint64_t debug_info_offset
2724 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2725 addr += offset_size;
2726 const auto per_cu_it
2727 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2728 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2729 {
2730 warning (_("Section .debug_aranges in %s entry at offset %s "
2731 "debug_info_offset %s does not exists, "
2732 "ignoring .debug_aranges."),
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer),
2735 pulongest (debug_info_offset));
2736 return;
2737 }
2738 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2739
2740 const uint8_t address_size = *addr++;
2741 if (address_size < 1 || address_size > 8)
2742 {
2743 warning (_("Section .debug_aranges in %s entry at offset %s "
2744 "address_size %u is invalid, ignoring .debug_aranges."),
2745 objfile_name (objfile),
2746 plongest (entry_addr - section->buffer), address_size);
2747 return;
2748 }
2749
2750 const uint8_t segment_selector_size = *addr++;
2751 if (segment_selector_size != 0)
2752 {
2753 warning (_("Section .debug_aranges in %s entry at offset %s "
2754 "segment_selector_size %u is not supported, "
2755 "ignoring .debug_aranges."),
2756 objfile_name (objfile),
2757 plongest (entry_addr - section->buffer),
2758 segment_selector_size);
2759 return;
2760 }
2761
2762 /* Must pad to an alignment boundary that is twice the address
2763 size. It is undocumented by the DWARF standard but GCC does
2764 use it. */
2765 for (size_t padding = ((-(addr - section->buffer))
2766 & (2 * address_size - 1));
2767 padding > 0; padding--)
2768 if (*addr++ != 0)
2769 {
2770 warning (_("Section .debug_aranges in %s entry at offset %s "
2771 "padding is not zero, ignoring .debug_aranges."),
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer));
2774 return;
2775 }
2776
2777 for (;;)
2778 {
2779 if (addr + 2 * address_size > entry_end)
2780 {
2781 warning (_("Section .debug_aranges in %s entry at offset %s "
2782 "address list is not properly terminated, "
2783 "ignoring .debug_aranges."),
2784 objfile_name (objfile),
2785 plongest (entry_addr - section->buffer));
2786 return;
2787 }
2788 ULONGEST start = extract_unsigned_integer (addr, address_size,
2789 dwarf5_byte_order);
2790 addr += address_size;
2791 ULONGEST length = extract_unsigned_integer (addr, address_size,
2792 dwarf5_byte_order);
2793 addr += address_size;
2794 if (start == 0 && length == 0)
2795 break;
2796 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2797 {
2798 /* Symbol was eliminated due to a COMDAT group. */
2799 continue;
2800 }
2801 ULONGEST end = start + length;
2802 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2803 - baseaddr);
2804 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2805 - baseaddr);
2806 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2807 }
2808 }
2809
2810 objfile->partial_symtabs->psymtabs_addrmap
2811 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2812 }
2813
2814 /* Find a slot in the mapped index INDEX for the object named NAME.
2815 If NAME is found, set *VEC_OUT to point to the CU vector in the
2816 constant pool and return true. If NAME cannot be found, return
2817 false. */
2818
2819 static bool
2820 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2821 offset_type **vec_out)
2822 {
2823 offset_type hash;
2824 offset_type slot, step;
2825 int (*cmp) (const char *, const char *);
2826
2827 gdb::unique_xmalloc_ptr<char> without_params;
2828 if (current_language->la_language == language_cplus
2829 || current_language->la_language == language_fortran
2830 || current_language->la_language == language_d)
2831 {
2832 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2833 not contain any. */
2834
2835 if (strchr (name, '(') != NULL)
2836 {
2837 without_params = cp_remove_params (name);
2838
2839 if (without_params != NULL)
2840 name = without_params.get ();
2841 }
2842 }
2843
2844 /* Index version 4 did not support case insensitive searches. But the
2845 indices for case insensitive languages are built in lowercase, therefore
2846 simulate our NAME being searched is also lowercased. */
2847 hash = mapped_index_string_hash ((index->version == 4
2848 && case_sensitivity == case_sensitive_off
2849 ? 5 : index->version),
2850 name);
2851
2852 slot = hash & (index->symbol_table.size () - 1);
2853 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2854 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2855
2856 for (;;)
2857 {
2858 const char *str;
2859
2860 const auto &bucket = index->symbol_table[slot];
2861 if (bucket.name == 0 && bucket.vec == 0)
2862 return false;
2863
2864 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2865 if (!cmp (name, str))
2866 {
2867 *vec_out = (offset_type *) (index->constant_pool
2868 + MAYBE_SWAP (bucket.vec));
2869 return true;
2870 }
2871
2872 slot = (slot + step) & (index->symbol_table.size () - 1);
2873 }
2874 }
2875
2876 /* A helper function that reads the .gdb_index from BUFFER and fills
2877 in MAP. FILENAME is the name of the file containing the data;
2878 it is used for error reporting. DEPRECATED_OK is true if it is
2879 ok to use deprecated sections.
2880
2881 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2882 out parameters that are filled in with information about the CU and
2883 TU lists in the section.
2884
2885 Returns true if all went well, false otherwise. */
2886
2887 static bool
2888 read_gdb_index_from_buffer (const char *filename,
2889 bool deprecated_ok,
2890 gdb::array_view<const gdb_byte> buffer,
2891 struct mapped_index *map,
2892 const gdb_byte **cu_list,
2893 offset_type *cu_list_elements,
2894 const gdb_byte **types_list,
2895 offset_type *types_list_elements)
2896 {
2897 const gdb_byte *addr = &buffer[0];
2898
2899 /* Version check. */
2900 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2901 /* Versions earlier than 3 emitted every copy of a psymbol. This
2902 causes the index to behave very poorly for certain requests. Version 3
2903 contained incomplete addrmap. So, it seems better to just ignore such
2904 indices. */
2905 if (version < 4)
2906 {
2907 static int warning_printed = 0;
2908 if (!warning_printed)
2909 {
2910 warning (_("Skipping obsolete .gdb_index section in %s."),
2911 filename);
2912 warning_printed = 1;
2913 }
2914 return 0;
2915 }
2916 /* Index version 4 uses a different hash function than index version
2917 5 and later.
2918
2919 Versions earlier than 6 did not emit psymbols for inlined
2920 functions. Using these files will cause GDB not to be able to
2921 set breakpoints on inlined functions by name, so we ignore these
2922 indices unless the user has done
2923 "set use-deprecated-index-sections on". */
2924 if (version < 6 && !deprecated_ok)
2925 {
2926 static int warning_printed = 0;
2927 if (!warning_printed)
2928 {
2929 warning (_("\
2930 Skipping deprecated .gdb_index section in %s.\n\
2931 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2932 to use the section anyway."),
2933 filename);
2934 warning_printed = 1;
2935 }
2936 return 0;
2937 }
2938 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2939 of the TU (for symbols coming from TUs),
2940 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2941 Plus gold-generated indices can have duplicate entries for global symbols,
2942 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2943 These are just performance bugs, and we can't distinguish gdb-generated
2944 indices from gold-generated ones, so issue no warning here. */
2945
2946 /* Indexes with higher version than the one supported by GDB may be no
2947 longer backward compatible. */
2948 if (version > 8)
2949 return 0;
2950
2951 map->version = version;
2952
2953 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2954
2955 int i = 0;
2956 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2957 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2958 / 8);
2959 ++i;
2960
2961 *types_list = addr + MAYBE_SWAP (metadata[i]);
2962 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2963 - MAYBE_SWAP (metadata[i]))
2964 / 8);
2965 ++i;
2966
2967 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2968 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2969 map->address_table
2970 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2971 ++i;
2972
2973 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2974 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2975 map->symbol_table
2976 = gdb::array_view<mapped_index::symbol_table_slot>
2977 ((mapped_index::symbol_table_slot *) symbol_table,
2978 (mapped_index::symbol_table_slot *) symbol_table_end);
2979
2980 ++i;
2981 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2982
2983 return 1;
2984 }
2985
2986 /* Callback types for dwarf2_read_gdb_index. */
2987
2988 typedef gdb::function_view
2989 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2990 get_gdb_index_contents_ftype;
2991 typedef gdb::function_view
2992 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2993 get_gdb_index_contents_dwz_ftype;
2994
2995 /* Read .gdb_index. If everything went ok, initialize the "quick"
2996 elements of all the CUs and return 1. Otherwise, return 0. */
2997
2998 static int
2999 dwarf2_read_gdb_index
3000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 get_gdb_index_contents_ftype get_gdb_index_contents,
3002 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3003 {
3004 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3005 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3006 struct dwz_file *dwz;
3007 struct objfile *objfile = dwarf2_per_objfile->objfile;
3008
3009 gdb::array_view<const gdb_byte> main_index_contents
3010 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3011
3012 if (main_index_contents.empty ())
3013 return 0;
3014
3015 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3016 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3017 use_deprecated_index_sections,
3018 main_index_contents, map.get (), &cu_list,
3019 &cu_list_elements, &types_list,
3020 &types_list_elements))
3021 return 0;
3022
3023 /* Don't use the index if it's empty. */
3024 if (map->symbol_table.empty ())
3025 return 0;
3026
3027 /* If there is a .dwz file, read it so we can get its CU list as
3028 well. */
3029 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3030 if (dwz != NULL)
3031 {
3032 struct mapped_index dwz_map;
3033 const gdb_byte *dwz_types_ignore;
3034 offset_type dwz_types_elements_ignore;
3035
3036 gdb::array_view<const gdb_byte> dwz_index_content
3037 = get_gdb_index_contents_dwz (objfile, dwz);
3038
3039 if (dwz_index_content.empty ())
3040 return 0;
3041
3042 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3043 1, dwz_index_content, &dwz_map,
3044 &dwz_list, &dwz_list_elements,
3045 &dwz_types_ignore,
3046 &dwz_types_elements_ignore))
3047 {
3048 warning (_("could not read '.gdb_index' section from %s; skipping"),
3049 bfd_get_filename (dwz->dwz_bfd.get ()));
3050 return 0;
3051 }
3052 }
3053
3054 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3055 dwz_list, dwz_list_elements);
3056
3057 if (types_list_elements)
3058 {
3059 /* We can only handle a single .debug_types when we have an
3060 index. */
3061 if (dwarf2_per_objfile->types.size () != 1)
3062 return 0;
3063
3064 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3065
3066 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3067 types_list, types_list_elements);
3068 }
3069
3070 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3071
3072 dwarf2_per_objfile->index_table = std::move (map);
3073 dwarf2_per_objfile->using_index = 1;
3074 dwarf2_per_objfile->quick_file_names_table =
3075 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3076
3077 return 1;
3078 }
3079
3080 /* die_reader_func for dw2_get_file_names. */
3081
3082 static void
3083 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3084 const gdb_byte *info_ptr,
3085 struct die_info *comp_unit_die)
3086 {
3087 struct dwarf2_cu *cu = reader->cu;
3088 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3089 struct dwarf2_per_objfile *dwarf2_per_objfile
3090 = cu->per_cu->dwarf2_per_objfile;
3091 struct objfile *objfile = dwarf2_per_objfile->objfile;
3092 struct dwarf2_per_cu_data *lh_cu;
3093 struct attribute *attr;
3094 void **slot;
3095 struct quick_file_names *qfn;
3096
3097 gdb_assert (! this_cu->is_debug_types);
3098
3099 /* Our callers never want to match partial units -- instead they
3100 will match the enclosing full CU. */
3101 if (comp_unit_die->tag == DW_TAG_partial_unit)
3102 {
3103 this_cu->v.quick->no_file_data = 1;
3104 return;
3105 }
3106
3107 lh_cu = this_cu;
3108 slot = NULL;
3109
3110 line_header_up lh;
3111 sect_offset line_offset {};
3112
3113 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3114 if (attr != nullptr)
3115 {
3116 struct quick_file_names find_entry;
3117
3118 line_offset = (sect_offset) DW_UNSND (attr);
3119
3120 /* We may have already read in this line header (TU line header sharing).
3121 If we have we're done. */
3122 find_entry.hash.dwo_unit = cu->dwo_unit;
3123 find_entry.hash.line_sect_off = line_offset;
3124 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3125 &find_entry, INSERT);
3126 if (*slot != NULL)
3127 {
3128 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3129 return;
3130 }
3131
3132 lh = dwarf_decode_line_header (line_offset, cu);
3133 }
3134 if (lh == NULL)
3135 {
3136 lh_cu->v.quick->no_file_data = 1;
3137 return;
3138 }
3139
3140 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3141 qfn->hash.dwo_unit = cu->dwo_unit;
3142 qfn->hash.line_sect_off = line_offset;
3143 gdb_assert (slot != NULL);
3144 *slot = qfn;
3145
3146 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3147
3148 int offset = 0;
3149 if (strcmp (fnd.name, "<unknown>") != 0)
3150 ++offset;
3151
3152 qfn->num_file_names = offset + lh->file_names_size ();
3153 qfn->file_names =
3154 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3155 if (offset != 0)
3156 qfn->file_names[0] = xstrdup (fnd.name);
3157 for (int i = 0; i < lh->file_names_size (); ++i)
3158 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3159 fnd.comp_dir).release ();
3160 qfn->real_names = NULL;
3161
3162 lh_cu->v.quick->file_names = qfn;
3163 }
3164
3165 /* A helper for the "quick" functions which attempts to read the line
3166 table for THIS_CU. */
3167
3168 static struct quick_file_names *
3169 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3170 {
3171 /* This should never be called for TUs. */
3172 gdb_assert (! this_cu->is_debug_types);
3173 /* Nor type unit groups. */
3174 gdb_assert (! this_cu->type_unit_group_p ());
3175
3176 if (this_cu->v.quick->file_names != NULL)
3177 return this_cu->v.quick->file_names;
3178 /* If we know there is no line data, no point in looking again. */
3179 if (this_cu->v.quick->no_file_data)
3180 return NULL;
3181
3182 cutu_reader reader (this_cu);
3183 if (!reader.dummy_p)
3184 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3185
3186 if (this_cu->v.quick->no_file_data)
3187 return NULL;
3188 return this_cu->v.quick->file_names;
3189 }
3190
3191 /* A helper for the "quick" functions which computes and caches the
3192 real path for a given file name from the line table. */
3193
3194 static const char *
3195 dw2_get_real_path (struct objfile *objfile,
3196 struct quick_file_names *qfn, int index)
3197 {
3198 if (qfn->real_names == NULL)
3199 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3200 qfn->num_file_names, const char *);
3201
3202 if (qfn->real_names[index] == NULL)
3203 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3204
3205 return qfn->real_names[index];
3206 }
3207
3208 static struct symtab *
3209 dw2_find_last_source_symtab (struct objfile *objfile)
3210 {
3211 struct dwarf2_per_objfile *dwarf2_per_objfile
3212 = get_dwarf2_per_objfile (objfile);
3213 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3214 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3215
3216 if (cust == NULL)
3217 return NULL;
3218
3219 return compunit_primary_filetab (cust);
3220 }
3221
3222 /* Traversal function for dw2_forget_cached_source_info. */
3223
3224 static int
3225 dw2_free_cached_file_names (void **slot, void *info)
3226 {
3227 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3228
3229 if (file_data->real_names)
3230 {
3231 int i;
3232
3233 for (i = 0; i < file_data->num_file_names; ++i)
3234 {
3235 xfree ((void*) file_data->real_names[i]);
3236 file_data->real_names[i] = NULL;
3237 }
3238 }
3239
3240 return 1;
3241 }
3242
3243 static void
3244 dw2_forget_cached_source_info (struct objfile *objfile)
3245 {
3246 struct dwarf2_per_objfile *dwarf2_per_objfile
3247 = get_dwarf2_per_objfile (objfile);
3248
3249 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3250 dw2_free_cached_file_names, NULL);
3251 }
3252
3253 /* Helper function for dw2_map_symtabs_matching_filename that expands
3254 the symtabs and calls the iterator. */
3255
3256 static int
3257 dw2_map_expand_apply (struct objfile *objfile,
3258 struct dwarf2_per_cu_data *per_cu,
3259 const char *name, const char *real_path,
3260 gdb::function_view<bool (symtab *)> callback)
3261 {
3262 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3263
3264 /* Don't visit already-expanded CUs. */
3265 if (per_cu->v.quick->compunit_symtab)
3266 return 0;
3267
3268 /* This may expand more than one symtab, and we want to iterate over
3269 all of them. */
3270 dw2_instantiate_symtab (per_cu, false);
3271
3272 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3273 last_made, callback);
3274 }
3275
3276 /* Implementation of the map_symtabs_matching_filename method. */
3277
3278 static bool
3279 dw2_map_symtabs_matching_filename
3280 (struct objfile *objfile, const char *name, const char *real_path,
3281 gdb::function_view<bool (symtab *)> callback)
3282 {
3283 const char *name_basename = lbasename (name);
3284 struct dwarf2_per_objfile *dwarf2_per_objfile
3285 = get_dwarf2_per_objfile (objfile);
3286
3287 /* The rule is CUs specify all the files, including those used by
3288 any TU, so there's no need to scan TUs here. */
3289
3290 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3291 {
3292 /* We only need to look at symtabs not already expanded. */
3293 if (per_cu->v.quick->compunit_symtab)
3294 continue;
3295
3296 quick_file_names *file_data = dw2_get_file_names (per_cu);
3297 if (file_data == NULL)
3298 continue;
3299
3300 for (int j = 0; j < file_data->num_file_names; ++j)
3301 {
3302 const char *this_name = file_data->file_names[j];
3303 const char *this_real_name;
3304
3305 if (compare_filenames_for_search (this_name, name))
3306 {
3307 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3308 callback))
3309 return true;
3310 continue;
3311 }
3312
3313 /* Before we invoke realpath, which can get expensive when many
3314 files are involved, do a quick comparison of the basenames. */
3315 if (! basenames_may_differ
3316 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3317 continue;
3318
3319 this_real_name = dw2_get_real_path (objfile, file_data, j);
3320 if (compare_filenames_for_search (this_real_name, name))
3321 {
3322 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3323 callback))
3324 return true;
3325 continue;
3326 }
3327
3328 if (real_path != NULL)
3329 {
3330 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3331 gdb_assert (IS_ABSOLUTE_PATH (name));
3332 if (this_real_name != NULL
3333 && FILENAME_CMP (real_path, this_real_name) == 0)
3334 {
3335 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3336 callback))
3337 return true;
3338 continue;
3339 }
3340 }
3341 }
3342 }
3343
3344 return false;
3345 }
3346
3347 /* Struct used to manage iterating over all CUs looking for a symbol. */
3348
3349 struct dw2_symtab_iterator
3350 {
3351 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3352 struct dwarf2_per_objfile *dwarf2_per_objfile;
3353 /* If set, only look for symbols that match that block. Valid values are
3354 GLOBAL_BLOCK and STATIC_BLOCK. */
3355 gdb::optional<block_enum> block_index;
3356 /* The kind of symbol we're looking for. */
3357 domain_enum domain;
3358 /* The list of CUs from the index entry of the symbol,
3359 or NULL if not found. */
3360 offset_type *vec;
3361 /* The next element in VEC to look at. */
3362 int next;
3363 /* The number of elements in VEC, or zero if there is no match. */
3364 int length;
3365 /* Have we seen a global version of the symbol?
3366 If so we can ignore all further global instances.
3367 This is to work around gold/15646, inefficient gold-generated
3368 indices. */
3369 int global_seen;
3370 };
3371
3372 /* Initialize the index symtab iterator ITER. */
3373
3374 static void
3375 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3376 struct dwarf2_per_objfile *dwarf2_per_objfile,
3377 gdb::optional<block_enum> block_index,
3378 domain_enum domain,
3379 const char *name)
3380 {
3381 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3382 iter->block_index = block_index;
3383 iter->domain = domain;
3384 iter->next = 0;
3385 iter->global_seen = 0;
3386
3387 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3388
3389 /* index is NULL if OBJF_READNOW. */
3390 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3391 iter->length = MAYBE_SWAP (*iter->vec);
3392 else
3393 {
3394 iter->vec = NULL;
3395 iter->length = 0;
3396 }
3397 }
3398
3399 /* Return the next matching CU or NULL if there are no more. */
3400
3401 static struct dwarf2_per_cu_data *
3402 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3403 {
3404 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3405
3406 for ( ; iter->next < iter->length; ++iter->next)
3407 {
3408 offset_type cu_index_and_attrs =
3409 MAYBE_SWAP (iter->vec[iter->next + 1]);
3410 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3411 gdb_index_symbol_kind symbol_kind =
3412 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3413 /* Only check the symbol attributes if they're present.
3414 Indices prior to version 7 don't record them,
3415 and indices >= 7 may elide them for certain symbols
3416 (gold does this). */
3417 int attrs_valid =
3418 (dwarf2_per_objfile->index_table->version >= 7
3419 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3420
3421 /* Don't crash on bad data. */
3422 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3423 + dwarf2_per_objfile->all_type_units.size ()))
3424 {
3425 complaint (_(".gdb_index entry has bad CU index"
3426 " [in module %s]"),
3427 objfile_name (dwarf2_per_objfile->objfile));
3428 continue;
3429 }
3430
3431 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3432
3433 /* Skip if already read in. */
3434 if (per_cu->v.quick->compunit_symtab)
3435 continue;
3436
3437 /* Check static vs global. */
3438 if (attrs_valid)
3439 {
3440 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3441
3442 if (iter->block_index.has_value ())
3443 {
3444 bool want_static = *iter->block_index == STATIC_BLOCK;
3445
3446 if (is_static != want_static)
3447 continue;
3448 }
3449
3450 /* Work around gold/15646. */
3451 if (!is_static && iter->global_seen)
3452 continue;
3453 if (!is_static)
3454 iter->global_seen = 1;
3455 }
3456
3457 /* Only check the symbol's kind if it has one. */
3458 if (attrs_valid)
3459 {
3460 switch (iter->domain)
3461 {
3462 case VAR_DOMAIN:
3463 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3464 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3465 /* Some types are also in VAR_DOMAIN. */
3466 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3467 continue;
3468 break;
3469 case STRUCT_DOMAIN:
3470 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3471 continue;
3472 break;
3473 case LABEL_DOMAIN:
3474 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3475 continue;
3476 break;
3477 case MODULE_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3479 continue;
3480 break;
3481 default:
3482 break;
3483 }
3484 }
3485
3486 ++iter->next;
3487 return per_cu;
3488 }
3489
3490 return NULL;
3491 }
3492
3493 static struct compunit_symtab *
3494 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3495 const char *name, domain_enum domain)
3496 {
3497 struct compunit_symtab *stab_best = NULL;
3498 struct dwarf2_per_objfile *dwarf2_per_objfile
3499 = get_dwarf2_per_objfile (objfile);
3500
3501 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3502
3503 struct dw2_symtab_iterator iter;
3504 struct dwarf2_per_cu_data *per_cu;
3505
3506 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3507
3508 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3509 {
3510 struct symbol *sym, *with_opaque = NULL;
3511 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3512 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3513 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3514
3515 sym = block_find_symbol (block, name, domain,
3516 block_find_non_opaque_type_preferred,
3517 &with_opaque);
3518
3519 /* Some caution must be observed with overloaded functions
3520 and methods, since the index will not contain any overload
3521 information (but NAME might contain it). */
3522
3523 if (sym != NULL
3524 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3525 return stab;
3526 if (with_opaque != NULL
3527 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3528 stab_best = stab;
3529
3530 /* Keep looking through other CUs. */
3531 }
3532
3533 return stab_best;
3534 }
3535
3536 static void
3537 dw2_print_stats (struct objfile *objfile)
3538 {
3539 struct dwarf2_per_objfile *dwarf2_per_objfile
3540 = get_dwarf2_per_objfile (objfile);
3541 int total = (dwarf2_per_objfile->all_comp_units.size ()
3542 + dwarf2_per_objfile->all_type_units.size ());
3543 int count = 0;
3544
3545 for (int i = 0; i < total; ++i)
3546 {
3547 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3548
3549 if (!per_cu->v.quick->compunit_symtab)
3550 ++count;
3551 }
3552 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3553 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3554 }
3555
3556 /* This dumps minimal information about the index.
3557 It is called via "mt print objfiles".
3558 One use is to verify .gdb_index has been loaded by the
3559 gdb.dwarf2/gdb-index.exp testcase. */
3560
3561 static void
3562 dw2_dump (struct objfile *objfile)
3563 {
3564 struct dwarf2_per_objfile *dwarf2_per_objfile
3565 = get_dwarf2_per_objfile (objfile);
3566
3567 gdb_assert (dwarf2_per_objfile->using_index);
3568 printf_filtered (".gdb_index:");
3569 if (dwarf2_per_objfile->index_table != NULL)
3570 {
3571 printf_filtered (" version %d\n",
3572 dwarf2_per_objfile->index_table->version);
3573 }
3574 else
3575 printf_filtered (" faked for \"readnow\"\n");
3576 printf_filtered ("\n");
3577 }
3578
3579 static void
3580 dw2_expand_symtabs_for_function (struct objfile *objfile,
3581 const char *func_name)
3582 {
3583 struct dwarf2_per_objfile *dwarf2_per_objfile
3584 = get_dwarf2_per_objfile (objfile);
3585
3586 struct dw2_symtab_iterator iter;
3587 struct dwarf2_per_cu_data *per_cu;
3588
3589 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3590
3591 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3592 dw2_instantiate_symtab (per_cu, false);
3593
3594 }
3595
3596 static void
3597 dw2_expand_all_symtabs (struct objfile *objfile)
3598 {
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
3601 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3602 + dwarf2_per_objfile->all_type_units.size ());
3603
3604 for (int i = 0; i < total_units; ++i)
3605 {
3606 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3607
3608 /* We don't want to directly expand a partial CU, because if we
3609 read it with the wrong language, then assertion failures can
3610 be triggered later on. See PR symtab/23010. So, tell
3611 dw2_instantiate_symtab to skip partial CUs -- any important
3612 partial CU will be read via DW_TAG_imported_unit anyway. */
3613 dw2_instantiate_symtab (per_cu, true);
3614 }
3615 }
3616
3617 static void
3618 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3619 const char *fullname)
3620 {
3621 struct dwarf2_per_objfile *dwarf2_per_objfile
3622 = get_dwarf2_per_objfile (objfile);
3623
3624 /* We don't need to consider type units here.
3625 This is only called for examining code, e.g. expand_line_sal.
3626 There can be an order of magnitude (or more) more type units
3627 than comp units, and we avoid them if we can. */
3628
3629 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3630 {
3631 /* We only need to look at symtabs not already expanded. */
3632 if (per_cu->v.quick->compunit_symtab)
3633 continue;
3634
3635 quick_file_names *file_data = dw2_get_file_names (per_cu);
3636 if (file_data == NULL)
3637 continue;
3638
3639 for (int j = 0; j < file_data->num_file_names; ++j)
3640 {
3641 const char *this_fullname = file_data->file_names[j];
3642
3643 if (filename_cmp (this_fullname, fullname) == 0)
3644 {
3645 dw2_instantiate_symtab (per_cu, false);
3646 break;
3647 }
3648 }
3649 }
3650 }
3651
3652 static void
3653 dw2_map_matching_symbols
3654 (struct objfile *objfile,
3655 const lookup_name_info &name, domain_enum domain,
3656 int global,
3657 gdb::function_view<symbol_found_callback_ftype> callback,
3658 symbol_compare_ftype *ordered_compare)
3659 {
3660 /* Used for Ada. */
3661 struct dwarf2_per_objfile *dwarf2_per_objfile
3662 = get_dwarf2_per_objfile (objfile);
3663
3664 if (dwarf2_per_objfile->index_table != nullptr)
3665 {
3666 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3667 here though if the current language is Ada for a non-Ada objfile
3668 using GNU index. As Ada does not look for non-Ada symbols this
3669 function should just return. */
3670 return;
3671 }
3672
3673 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3674 inline psym_map_matching_symbols here, assuming all partial symtabs have
3675 been read in. */
3676 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3677
3678 for (compunit_symtab *cust : objfile->compunits ())
3679 {
3680 const struct block *block;
3681
3682 if (cust == NULL)
3683 continue;
3684 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3685 if (!iterate_over_symbols_terminated (block, name,
3686 domain, callback))
3687 return;
3688 }
3689 }
3690
3691 /* Starting from a search name, return the string that finds the upper
3692 bound of all strings that start with SEARCH_NAME in a sorted name
3693 list. Returns the empty string to indicate that the upper bound is
3694 the end of the list. */
3695
3696 static std::string
3697 make_sort_after_prefix_name (const char *search_name)
3698 {
3699 /* When looking to complete "func", we find the upper bound of all
3700 symbols that start with "func" by looking for where we'd insert
3701 the closest string that would follow "func" in lexicographical
3702 order. Usually, that's "func"-with-last-character-incremented,
3703 i.e. "fund". Mind non-ASCII characters, though. Usually those
3704 will be UTF-8 multi-byte sequences, but we can't be certain.
3705 Especially mind the 0xff character, which is a valid character in
3706 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3707 rule out compilers allowing it in identifiers. Note that
3708 conveniently, strcmp/strcasecmp are specified to compare
3709 characters interpreted as unsigned char. So what we do is treat
3710 the whole string as a base 256 number composed of a sequence of
3711 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3712 to 0, and carries 1 to the following more-significant position.
3713 If the very first character in SEARCH_NAME ends up incremented
3714 and carries/overflows, then the upper bound is the end of the
3715 list. The string after the empty string is also the empty
3716 string.
3717
3718 Some examples of this operation:
3719
3720 SEARCH_NAME => "+1" RESULT
3721
3722 "abc" => "abd"
3723 "ab\xff" => "ac"
3724 "\xff" "a" "\xff" => "\xff" "b"
3725 "\xff" => ""
3726 "\xff\xff" => ""
3727 "" => ""
3728
3729 Then, with these symbols for example:
3730
3731 func
3732 func1
3733 fund
3734
3735 completing "func" looks for symbols between "func" and
3736 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3737 which finds "func" and "func1", but not "fund".
3738
3739 And with:
3740
3741 funcÿ (Latin1 'ÿ' [0xff])
3742 funcÿ1
3743 fund
3744
3745 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3746 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3747
3748 And with:
3749
3750 ÿÿ (Latin1 'ÿ' [0xff])
3751 ÿÿ1
3752
3753 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3754 the end of the list.
3755 */
3756 std::string after = search_name;
3757 while (!after.empty () && (unsigned char) after.back () == 0xff)
3758 after.pop_back ();
3759 if (!after.empty ())
3760 after.back () = (unsigned char) after.back () + 1;
3761 return after;
3762 }
3763
3764 /* See declaration. */
3765
3766 std::pair<std::vector<name_component>::const_iterator,
3767 std::vector<name_component>::const_iterator>
3768 mapped_index_base::find_name_components_bounds
3769 (const lookup_name_info &lookup_name_without_params, language lang) const
3770 {
3771 auto *name_cmp
3772 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3773
3774 const char *lang_name
3775 = lookup_name_without_params.language_lookup_name (lang);
3776
3777 /* Comparison function object for lower_bound that matches against a
3778 given symbol name. */
3779 auto lookup_compare_lower = [&] (const name_component &elem,
3780 const char *name)
3781 {
3782 const char *elem_qualified = this->symbol_name_at (elem.idx);
3783 const char *elem_name = elem_qualified + elem.name_offset;
3784 return name_cmp (elem_name, name) < 0;
3785 };
3786
3787 /* Comparison function object for upper_bound that matches against a
3788 given symbol name. */
3789 auto lookup_compare_upper = [&] (const char *name,
3790 const name_component &elem)
3791 {
3792 const char *elem_qualified = this->symbol_name_at (elem.idx);
3793 const char *elem_name = elem_qualified + elem.name_offset;
3794 return name_cmp (name, elem_name) < 0;
3795 };
3796
3797 auto begin = this->name_components.begin ();
3798 auto end = this->name_components.end ();
3799
3800 /* Find the lower bound. */
3801 auto lower = [&] ()
3802 {
3803 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3804 return begin;
3805 else
3806 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3807 } ();
3808
3809 /* Find the upper bound. */
3810 auto upper = [&] ()
3811 {
3812 if (lookup_name_without_params.completion_mode ())
3813 {
3814 /* In completion mode, we want UPPER to point past all
3815 symbols names that have the same prefix. I.e., with
3816 these symbols, and completing "func":
3817
3818 function << lower bound
3819 function1
3820 other_function << upper bound
3821
3822 We find the upper bound by looking for the insertion
3823 point of "func"-with-last-character-incremented,
3824 i.e. "fund". */
3825 std::string after = make_sort_after_prefix_name (lang_name);
3826 if (after.empty ())
3827 return end;
3828 return std::lower_bound (lower, end, after.c_str (),
3829 lookup_compare_lower);
3830 }
3831 else
3832 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3833 } ();
3834
3835 return {lower, upper};
3836 }
3837
3838 /* See declaration. */
3839
3840 void
3841 mapped_index_base::build_name_components ()
3842 {
3843 if (!this->name_components.empty ())
3844 return;
3845
3846 this->name_components_casing = case_sensitivity;
3847 auto *name_cmp
3848 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3849
3850 /* The code below only knows how to break apart components of C++
3851 symbol names (and other languages that use '::' as
3852 namespace/module separator) and Ada symbol names. */
3853 auto count = this->symbol_name_count ();
3854 for (offset_type idx = 0; idx < count; idx++)
3855 {
3856 if (this->symbol_name_slot_invalid (idx))
3857 continue;
3858
3859 const char *name = this->symbol_name_at (idx);
3860
3861 /* Add each name component to the name component table. */
3862 unsigned int previous_len = 0;
3863
3864 if (strstr (name, "::") != nullptr)
3865 {
3866 for (unsigned int current_len = cp_find_first_component (name);
3867 name[current_len] != '\0';
3868 current_len += cp_find_first_component (name + current_len))
3869 {
3870 gdb_assert (name[current_len] == ':');
3871 this->name_components.push_back ({previous_len, idx});
3872 /* Skip the '::'. */
3873 current_len += 2;
3874 previous_len = current_len;
3875 }
3876 }
3877 else
3878 {
3879 /* Handle the Ada encoded (aka mangled) form here. */
3880 for (const char *iter = strstr (name, "__");
3881 iter != nullptr;
3882 iter = strstr (iter, "__"))
3883 {
3884 this->name_components.push_back ({previous_len, idx});
3885 iter += 2;
3886 previous_len = iter - name;
3887 }
3888 }
3889
3890 this->name_components.push_back ({previous_len, idx});
3891 }
3892
3893 /* Sort name_components elements by name. */
3894 auto name_comp_compare = [&] (const name_component &left,
3895 const name_component &right)
3896 {
3897 const char *left_qualified = this->symbol_name_at (left.idx);
3898 const char *right_qualified = this->symbol_name_at (right.idx);
3899
3900 const char *left_name = left_qualified + left.name_offset;
3901 const char *right_name = right_qualified + right.name_offset;
3902
3903 return name_cmp (left_name, right_name) < 0;
3904 };
3905
3906 std::sort (this->name_components.begin (),
3907 this->name_components.end (),
3908 name_comp_compare);
3909 }
3910
3911 /* Helper for dw2_expand_symtabs_matching that works with a
3912 mapped_index_base instead of the containing objfile. This is split
3913 to a separate function in order to be able to unit test the
3914 name_components matching using a mock mapped_index_base. For each
3915 symbol name that matches, calls MATCH_CALLBACK, passing it the
3916 symbol's index in the mapped_index_base symbol table. */
3917
3918 static void
3919 dw2_expand_symtabs_matching_symbol
3920 (mapped_index_base &index,
3921 const lookup_name_info &lookup_name_in,
3922 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3923 enum search_domain kind,
3924 gdb::function_view<bool (offset_type)> match_callback)
3925 {
3926 lookup_name_info lookup_name_without_params
3927 = lookup_name_in.make_ignore_params ();
3928
3929 /* Build the symbol name component sorted vector, if we haven't
3930 yet. */
3931 index.build_name_components ();
3932
3933 /* The same symbol may appear more than once in the range though.
3934 E.g., if we're looking for symbols that complete "w", and we have
3935 a symbol named "w1::w2", we'll find the two name components for
3936 that same symbol in the range. To be sure we only call the
3937 callback once per symbol, we first collect the symbol name
3938 indexes that matched in a temporary vector and ignore
3939 duplicates. */
3940 std::vector<offset_type> matches;
3941
3942 struct name_and_matcher
3943 {
3944 symbol_name_matcher_ftype *matcher;
3945 const char *name;
3946
3947 bool operator== (const name_and_matcher &other) const
3948 {
3949 return matcher == other.matcher && strcmp (name, other.name) == 0;
3950 }
3951 };
3952
3953 /* A vector holding all the different symbol name matchers, for all
3954 languages. */
3955 std::vector<name_and_matcher> matchers;
3956
3957 for (int i = 0; i < nr_languages; i++)
3958 {
3959 enum language lang_e = (enum language) i;
3960
3961 const language_defn *lang = language_def (lang_e);
3962 symbol_name_matcher_ftype *name_matcher
3963 = get_symbol_name_matcher (lang, lookup_name_without_params);
3964
3965 name_and_matcher key {
3966 name_matcher,
3967 lookup_name_without_params.language_lookup_name (lang_e)
3968 };
3969
3970 /* Don't insert the same comparison routine more than once.
3971 Note that we do this linear walk. This is not a problem in
3972 practice because the number of supported languages is
3973 low. */
3974 if (std::find (matchers.begin (), matchers.end (), key)
3975 != matchers.end ())
3976 continue;
3977 matchers.push_back (std::move (key));
3978
3979 auto bounds
3980 = index.find_name_components_bounds (lookup_name_without_params,
3981 lang_e);
3982
3983 /* Now for each symbol name in range, check to see if we have a name
3984 match, and if so, call the MATCH_CALLBACK callback. */
3985
3986 for (; bounds.first != bounds.second; ++bounds.first)
3987 {
3988 const char *qualified = index.symbol_name_at (bounds.first->idx);
3989
3990 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3991 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3992 continue;
3993
3994 matches.push_back (bounds.first->idx);
3995 }
3996 }
3997
3998 std::sort (matches.begin (), matches.end ());
3999
4000 /* Finally call the callback, once per match. */
4001 ULONGEST prev = -1;
4002 for (offset_type idx : matches)
4003 {
4004 if (prev != idx)
4005 {
4006 if (!match_callback (idx))
4007 break;
4008 prev = idx;
4009 }
4010 }
4011
4012 /* Above we use a type wider than idx's for 'prev', since 0 and
4013 (offset_type)-1 are both possible values. */
4014 static_assert (sizeof (prev) > sizeof (offset_type), "");
4015 }
4016
4017 #if GDB_SELF_TEST
4018
4019 namespace selftests { namespace dw2_expand_symtabs_matching {
4020
4021 /* A mock .gdb_index/.debug_names-like name index table, enough to
4022 exercise dw2_expand_symtabs_matching_symbol, which works with the
4023 mapped_index_base interface. Builds an index from the symbol list
4024 passed as parameter to the constructor. */
4025 class mock_mapped_index : public mapped_index_base
4026 {
4027 public:
4028 mock_mapped_index (gdb::array_view<const char *> symbols)
4029 : m_symbol_table (symbols)
4030 {}
4031
4032 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4033
4034 /* Return the number of names in the symbol table. */
4035 size_t symbol_name_count () const override
4036 {
4037 return m_symbol_table.size ();
4038 }
4039
4040 /* Get the name of the symbol at IDX in the symbol table. */
4041 const char *symbol_name_at (offset_type idx) const override
4042 {
4043 return m_symbol_table[idx];
4044 }
4045
4046 private:
4047 gdb::array_view<const char *> m_symbol_table;
4048 };
4049
4050 /* Convenience function that converts a NULL pointer to a "<null>"
4051 string, to pass to print routines. */
4052
4053 static const char *
4054 string_or_null (const char *str)
4055 {
4056 return str != NULL ? str : "<null>";
4057 }
4058
4059 /* Check if a lookup_name_info built from
4060 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4061 index. EXPECTED_LIST is the list of expected matches, in expected
4062 matching order. If no match expected, then an empty list is
4063 specified. Returns true on success. On failure prints a warning
4064 indicating the file:line that failed, and returns false. */
4065
4066 static bool
4067 check_match (const char *file, int line,
4068 mock_mapped_index &mock_index,
4069 const char *name, symbol_name_match_type match_type,
4070 bool completion_mode,
4071 std::initializer_list<const char *> expected_list)
4072 {
4073 lookup_name_info lookup_name (name, match_type, completion_mode);
4074
4075 bool matched = true;
4076
4077 auto mismatch = [&] (const char *expected_str,
4078 const char *got)
4079 {
4080 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4081 "expected=\"%s\", got=\"%s\"\n"),
4082 file, line,
4083 (match_type == symbol_name_match_type::FULL
4084 ? "FULL" : "WILD"),
4085 name, string_or_null (expected_str), string_or_null (got));
4086 matched = false;
4087 };
4088
4089 auto expected_it = expected_list.begin ();
4090 auto expected_end = expected_list.end ();
4091
4092 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4093 NULL, ALL_DOMAIN,
4094 [&] (offset_type idx)
4095 {
4096 const char *matched_name = mock_index.symbol_name_at (idx);
4097 const char *expected_str
4098 = expected_it == expected_end ? NULL : *expected_it++;
4099
4100 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4101 mismatch (expected_str, matched_name);
4102 return true;
4103 });
4104
4105 const char *expected_str
4106 = expected_it == expected_end ? NULL : *expected_it++;
4107 if (expected_str != NULL)
4108 mismatch (expected_str, NULL);
4109
4110 return matched;
4111 }
4112
4113 /* The symbols added to the mock mapped_index for testing (in
4114 canonical form). */
4115 static const char *test_symbols[] = {
4116 "function",
4117 "std::bar",
4118 "std::zfunction",
4119 "std::zfunction2",
4120 "w1::w2",
4121 "ns::foo<char*>",
4122 "ns::foo<int>",
4123 "ns::foo<long>",
4124 "ns2::tmpl<int>::foo2",
4125 "(anonymous namespace)::A::B::C",
4126
4127 /* These are used to check that the increment-last-char in the
4128 matching algorithm for completion doesn't match "t1_fund" when
4129 completing "t1_func". */
4130 "t1_func",
4131 "t1_func1",
4132 "t1_fund",
4133 "t1_fund1",
4134
4135 /* A UTF-8 name with multi-byte sequences to make sure that
4136 cp-name-parser understands this as a single identifier ("função"
4137 is "function" in PT). */
4138 u8"u8função",
4139
4140 /* \377 (0xff) is Latin1 'ÿ'. */
4141 "yfunc\377",
4142
4143 /* \377 (0xff) is Latin1 'ÿ'. */
4144 "\377",
4145 "\377\377123",
4146
4147 /* A name with all sorts of complications. Starts with "z" to make
4148 it easier for the completion tests below. */
4149 #define Z_SYM_NAME \
4150 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4151 "::tuple<(anonymous namespace)::ui*, " \
4152 "std::default_delete<(anonymous namespace)::ui>, void>"
4153
4154 Z_SYM_NAME
4155 };
4156
4157 /* Returns true if the mapped_index_base::find_name_component_bounds
4158 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4159 in completion mode. */
4160
4161 static bool
4162 check_find_bounds_finds (mapped_index_base &index,
4163 const char *search_name,
4164 gdb::array_view<const char *> expected_syms)
4165 {
4166 lookup_name_info lookup_name (search_name,
4167 symbol_name_match_type::FULL, true);
4168
4169 auto bounds = index.find_name_components_bounds (lookup_name,
4170 language_cplus);
4171
4172 size_t distance = std::distance (bounds.first, bounds.second);
4173 if (distance != expected_syms.size ())
4174 return false;
4175
4176 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4177 {
4178 auto nc_elem = bounds.first + exp_elem;
4179 const char *qualified = index.symbol_name_at (nc_elem->idx);
4180 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4181 return false;
4182 }
4183
4184 return true;
4185 }
4186
4187 /* Test the lower-level mapped_index::find_name_component_bounds
4188 method. */
4189
4190 static void
4191 test_mapped_index_find_name_component_bounds ()
4192 {
4193 mock_mapped_index mock_index (test_symbols);
4194
4195 mock_index.build_name_components ();
4196
4197 /* Test the lower-level mapped_index::find_name_component_bounds
4198 method in completion mode. */
4199 {
4200 static const char *expected_syms[] = {
4201 "t1_func",
4202 "t1_func1",
4203 };
4204
4205 SELF_CHECK (check_find_bounds_finds (mock_index,
4206 "t1_func", expected_syms));
4207 }
4208
4209 /* Check that the increment-last-char in the name matching algorithm
4210 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4211 {
4212 static const char *expected_syms1[] = {
4213 "\377",
4214 "\377\377123",
4215 };
4216 SELF_CHECK (check_find_bounds_finds (mock_index,
4217 "\377", expected_syms1));
4218
4219 static const char *expected_syms2[] = {
4220 "\377\377123",
4221 };
4222 SELF_CHECK (check_find_bounds_finds (mock_index,
4223 "\377\377", expected_syms2));
4224 }
4225 }
4226
4227 /* Test dw2_expand_symtabs_matching_symbol. */
4228
4229 static void
4230 test_dw2_expand_symtabs_matching_symbol ()
4231 {
4232 mock_mapped_index mock_index (test_symbols);
4233
4234 /* We let all tests run until the end even if some fails, for debug
4235 convenience. */
4236 bool any_mismatch = false;
4237
4238 /* Create the expected symbols list (an initializer_list). Needed
4239 because lists have commas, and we need to pass them to CHECK,
4240 which is a macro. */
4241 #define EXPECT(...) { __VA_ARGS__ }
4242
4243 /* Wrapper for check_match that passes down the current
4244 __FILE__/__LINE__. */
4245 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4246 any_mismatch |= !check_match (__FILE__, __LINE__, \
4247 mock_index, \
4248 NAME, MATCH_TYPE, COMPLETION_MODE, \
4249 EXPECTED_LIST)
4250
4251 /* Identity checks. */
4252 for (const char *sym : test_symbols)
4253 {
4254 /* Should be able to match all existing symbols. */
4255 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4256 EXPECT (sym));
4257
4258 /* Should be able to match all existing symbols with
4259 parameters. */
4260 std::string with_params = std::string (sym) + "(int)";
4261 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4262 EXPECT (sym));
4263
4264 /* Should be able to match all existing symbols with
4265 parameters and qualifiers. */
4266 with_params = std::string (sym) + " ( int ) const";
4267 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4268 EXPECT (sym));
4269
4270 /* This should really find sym, but cp-name-parser.y doesn't
4271 know about lvalue/rvalue qualifiers yet. */
4272 with_params = std::string (sym) + " ( int ) &&";
4273 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4274 {});
4275 }
4276
4277 /* Check that the name matching algorithm for completion doesn't get
4278 confused with Latin1 'ÿ' / 0xff. */
4279 {
4280 static const char str[] = "\377";
4281 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4282 EXPECT ("\377", "\377\377123"));
4283 }
4284
4285 /* Check that the increment-last-char in the matching algorithm for
4286 completion doesn't match "t1_fund" when completing "t1_func". */
4287 {
4288 static const char str[] = "t1_func";
4289 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4290 EXPECT ("t1_func", "t1_func1"));
4291 }
4292
4293 /* Check that completion mode works at each prefix of the expected
4294 symbol name. */
4295 {
4296 static const char str[] = "function(int)";
4297 size_t len = strlen (str);
4298 std::string lookup;
4299
4300 for (size_t i = 1; i < len; i++)
4301 {
4302 lookup.assign (str, i);
4303 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4304 EXPECT ("function"));
4305 }
4306 }
4307
4308 /* While "w" is a prefix of both components, the match function
4309 should still only be called once. */
4310 {
4311 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4312 EXPECT ("w1::w2"));
4313 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4314 EXPECT ("w1::w2"));
4315 }
4316
4317 /* Same, with a "complicated" symbol. */
4318 {
4319 static const char str[] = Z_SYM_NAME;
4320 size_t len = strlen (str);
4321 std::string lookup;
4322
4323 for (size_t i = 1; i < len; i++)
4324 {
4325 lookup.assign (str, i);
4326 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4327 EXPECT (Z_SYM_NAME));
4328 }
4329 }
4330
4331 /* In FULL mode, an incomplete symbol doesn't match. */
4332 {
4333 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4334 {});
4335 }
4336
4337 /* A complete symbol with parameters matches any overload, since the
4338 index has no overload info. */
4339 {
4340 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4341 EXPECT ("std::zfunction", "std::zfunction2"));
4342 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4343 EXPECT ("std::zfunction", "std::zfunction2"));
4344 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4345 EXPECT ("std::zfunction", "std::zfunction2"));
4346 }
4347
4348 /* Check that whitespace is ignored appropriately. A symbol with a
4349 template argument list. */
4350 {
4351 static const char expected[] = "ns::foo<int>";
4352 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4353 EXPECT (expected));
4354 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4355 EXPECT (expected));
4356 }
4357
4358 /* Check that whitespace is ignored appropriately. A symbol with a
4359 template argument list that includes a pointer. */
4360 {
4361 static const char expected[] = "ns::foo<char*>";
4362 /* Try both completion and non-completion modes. */
4363 static const bool completion_mode[2] = {false, true};
4364 for (size_t i = 0; i < 2; i++)
4365 {
4366 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4367 completion_mode[i], EXPECT (expected));
4368 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4369 completion_mode[i], EXPECT (expected));
4370
4371 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4372 completion_mode[i], EXPECT (expected));
4373 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4374 completion_mode[i], EXPECT (expected));
4375 }
4376 }
4377
4378 {
4379 /* Check method qualifiers are ignored. */
4380 static const char expected[] = "ns::foo<char*>";
4381 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4382 symbol_name_match_type::FULL, true, EXPECT (expected));
4383 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4384 symbol_name_match_type::FULL, true, EXPECT (expected));
4385 CHECK_MATCH ("foo < char * > ( int ) const",
4386 symbol_name_match_type::WILD, true, EXPECT (expected));
4387 CHECK_MATCH ("foo < char * > ( int ) &&",
4388 symbol_name_match_type::WILD, true, EXPECT (expected));
4389 }
4390
4391 /* Test lookup names that don't match anything. */
4392 {
4393 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4394 {});
4395
4396 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4397 {});
4398 }
4399
4400 /* Some wild matching tests, exercising "(anonymous namespace)",
4401 which should not be confused with a parameter list. */
4402 {
4403 static const char *syms[] = {
4404 "A::B::C",
4405 "B::C",
4406 "C",
4407 "A :: B :: C ( int )",
4408 "B :: C ( int )",
4409 "C ( int )",
4410 };
4411
4412 for (const char *s : syms)
4413 {
4414 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4415 EXPECT ("(anonymous namespace)::A::B::C"));
4416 }
4417 }
4418
4419 {
4420 static const char expected[] = "ns2::tmpl<int>::foo2";
4421 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4422 EXPECT (expected));
4423 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4424 EXPECT (expected));
4425 }
4426
4427 SELF_CHECK (!any_mismatch);
4428
4429 #undef EXPECT
4430 #undef CHECK_MATCH
4431 }
4432
4433 static void
4434 run_test ()
4435 {
4436 test_mapped_index_find_name_component_bounds ();
4437 test_dw2_expand_symtabs_matching_symbol ();
4438 }
4439
4440 }} // namespace selftests::dw2_expand_symtabs_matching
4441
4442 #endif /* GDB_SELF_TEST */
4443
4444 /* If FILE_MATCHER is NULL or if PER_CU has
4445 dwarf2_per_cu_quick_data::MARK set (see
4446 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4447 EXPANSION_NOTIFY on it. */
4448
4449 static void
4450 dw2_expand_symtabs_matching_one
4451 (struct dwarf2_per_cu_data *per_cu,
4452 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4453 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4454 {
4455 if (file_matcher == NULL || per_cu->v.quick->mark)
4456 {
4457 bool symtab_was_null
4458 = (per_cu->v.quick->compunit_symtab == NULL);
4459
4460 dw2_instantiate_symtab (per_cu, false);
4461
4462 if (expansion_notify != NULL
4463 && symtab_was_null
4464 && per_cu->v.quick->compunit_symtab != NULL)
4465 expansion_notify (per_cu->v.quick->compunit_symtab);
4466 }
4467 }
4468
4469 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4470 matched, to expand corresponding CUs that were marked. IDX is the
4471 index of the symbol name that matched. */
4472
4473 static void
4474 dw2_expand_marked_cus
4475 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4477 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4478 search_domain kind)
4479 {
4480 offset_type *vec, vec_len, vec_idx;
4481 bool global_seen = false;
4482 mapped_index &index = *dwarf2_per_objfile->index_table;
4483
4484 vec = (offset_type *) (index.constant_pool
4485 + MAYBE_SWAP (index.symbol_table[idx].vec));
4486 vec_len = MAYBE_SWAP (vec[0]);
4487 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4488 {
4489 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4490 /* This value is only valid for index versions >= 7. */
4491 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4492 gdb_index_symbol_kind symbol_kind =
4493 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4494 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4495 /* Only check the symbol attributes if they're present.
4496 Indices prior to version 7 don't record them,
4497 and indices >= 7 may elide them for certain symbols
4498 (gold does this). */
4499 int attrs_valid =
4500 (index.version >= 7
4501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4502
4503 /* Work around gold/15646. */
4504 if (attrs_valid)
4505 {
4506 if (!is_static && global_seen)
4507 continue;
4508 if (!is_static)
4509 global_seen = true;
4510 }
4511
4512 /* Only check the symbol's kind if it has one. */
4513 if (attrs_valid)
4514 {
4515 switch (kind)
4516 {
4517 case VARIABLES_DOMAIN:
4518 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4519 continue;
4520 break;
4521 case FUNCTIONS_DOMAIN:
4522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4523 continue;
4524 break;
4525 case TYPES_DOMAIN:
4526 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4527 continue;
4528 break;
4529 case MODULES_DOMAIN:
4530 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4531 continue;
4532 break;
4533 default:
4534 break;
4535 }
4536 }
4537
4538 /* Don't crash on bad data. */
4539 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4540 + dwarf2_per_objfile->all_type_units.size ()))
4541 {
4542 complaint (_(".gdb_index entry has bad CU index"
4543 " [in module %s]"),
4544 objfile_name (dwarf2_per_objfile->objfile));
4545 continue;
4546 }
4547
4548 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4549 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4550 expansion_notify);
4551 }
4552 }
4553
4554 /* If FILE_MATCHER is non-NULL, set all the
4555 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4556 that match FILE_MATCHER. */
4557
4558 static void
4559 dw_expand_symtabs_matching_file_matcher
4560 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4561 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4562 {
4563 if (file_matcher == NULL)
4564 return;
4565
4566 objfile *const objfile = dwarf2_per_objfile->objfile;
4567
4568 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4569 htab_eq_pointer,
4570 NULL, xcalloc, xfree));
4571 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4572 htab_eq_pointer,
4573 NULL, xcalloc, xfree));
4574
4575 /* The rule is CUs specify all the files, including those used by
4576 any TU, so there's no need to scan TUs here. */
4577
4578 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4579 {
4580 QUIT;
4581
4582 per_cu->v.quick->mark = 0;
4583
4584 /* We only need to look at symtabs not already expanded. */
4585 if (per_cu->v.quick->compunit_symtab)
4586 continue;
4587
4588 quick_file_names *file_data = dw2_get_file_names (per_cu);
4589 if (file_data == NULL)
4590 continue;
4591
4592 if (htab_find (visited_not_found.get (), file_data) != NULL)
4593 continue;
4594 else if (htab_find (visited_found.get (), file_data) != NULL)
4595 {
4596 per_cu->v.quick->mark = 1;
4597 continue;
4598 }
4599
4600 for (int j = 0; j < file_data->num_file_names; ++j)
4601 {
4602 const char *this_real_name;
4603
4604 if (file_matcher (file_data->file_names[j], false))
4605 {
4606 per_cu->v.quick->mark = 1;
4607 break;
4608 }
4609
4610 /* Before we invoke realpath, which can get expensive when many
4611 files are involved, do a quick comparison of the basenames. */
4612 if (!basenames_may_differ
4613 && !file_matcher (lbasename (file_data->file_names[j]),
4614 true))
4615 continue;
4616
4617 this_real_name = dw2_get_real_path (objfile, file_data, j);
4618 if (file_matcher (this_real_name, false))
4619 {
4620 per_cu->v.quick->mark = 1;
4621 break;
4622 }
4623 }
4624
4625 void **slot = htab_find_slot (per_cu->v.quick->mark
4626 ? visited_found.get ()
4627 : visited_not_found.get (),
4628 file_data, INSERT);
4629 *slot = file_data;
4630 }
4631 }
4632
4633 static void
4634 dw2_expand_symtabs_matching
4635 (struct objfile *objfile,
4636 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4637 const lookup_name_info *lookup_name,
4638 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4639 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4640 enum search_domain kind)
4641 {
4642 struct dwarf2_per_objfile *dwarf2_per_objfile
4643 = get_dwarf2_per_objfile (objfile);
4644
4645 /* index_table is NULL if OBJF_READNOW. */
4646 if (!dwarf2_per_objfile->index_table)
4647 return;
4648
4649 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4650
4651 if (symbol_matcher == NULL && lookup_name == NULL)
4652 {
4653 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4654 {
4655 QUIT;
4656
4657 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4658 expansion_notify);
4659 }
4660 return;
4661 }
4662
4663 mapped_index &index = *dwarf2_per_objfile->index_table;
4664
4665 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4666 symbol_matcher,
4667 kind, [&] (offset_type idx)
4668 {
4669 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4670 expansion_notify, kind);
4671 return true;
4672 });
4673 }
4674
4675 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4676 symtab. */
4677
4678 static struct compunit_symtab *
4679 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4680 CORE_ADDR pc)
4681 {
4682 int i;
4683
4684 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4685 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4686 return cust;
4687
4688 if (cust->includes == NULL)
4689 return NULL;
4690
4691 for (i = 0; cust->includes[i]; ++i)
4692 {
4693 struct compunit_symtab *s = cust->includes[i];
4694
4695 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4696 if (s != NULL)
4697 return s;
4698 }
4699
4700 return NULL;
4701 }
4702
4703 static struct compunit_symtab *
4704 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4705 struct bound_minimal_symbol msymbol,
4706 CORE_ADDR pc,
4707 struct obj_section *section,
4708 int warn_if_readin)
4709 {
4710 struct dwarf2_per_cu_data *data;
4711 struct compunit_symtab *result;
4712
4713 if (!objfile->partial_symtabs->psymtabs_addrmap)
4714 return NULL;
4715
4716 CORE_ADDR baseaddr = objfile->text_section_offset ();
4717 data = (struct dwarf2_per_cu_data *) addrmap_find
4718 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4719 if (!data)
4720 return NULL;
4721
4722 if (warn_if_readin && data->v.quick->compunit_symtab)
4723 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4724 paddress (objfile->arch (), pc));
4725
4726 result
4727 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4728 false),
4729 pc);
4730 gdb_assert (result != NULL);
4731 return result;
4732 }
4733
4734 static void
4735 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4736 void *data, int need_fullname)
4737 {
4738 struct dwarf2_per_objfile *dwarf2_per_objfile
4739 = get_dwarf2_per_objfile (objfile);
4740
4741 if (!dwarf2_per_objfile->filenames_cache)
4742 {
4743 dwarf2_per_objfile->filenames_cache.emplace ();
4744
4745 htab_up visited (htab_create_alloc (10,
4746 htab_hash_pointer, htab_eq_pointer,
4747 NULL, xcalloc, xfree));
4748
4749 /* The rule is CUs specify all the files, including those used
4750 by any TU, so there's no need to scan TUs here. We can
4751 ignore file names coming from already-expanded CUs. */
4752
4753 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4754 {
4755 if (per_cu->v.quick->compunit_symtab)
4756 {
4757 void **slot = htab_find_slot (visited.get (),
4758 per_cu->v.quick->file_names,
4759 INSERT);
4760
4761 *slot = per_cu->v.quick->file_names;
4762 }
4763 }
4764
4765 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4766 {
4767 /* We only need to look at symtabs not already expanded. */
4768 if (per_cu->v.quick->compunit_symtab)
4769 continue;
4770
4771 quick_file_names *file_data = dw2_get_file_names (per_cu);
4772 if (file_data == NULL)
4773 continue;
4774
4775 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4776 if (*slot)
4777 {
4778 /* Already visited. */
4779 continue;
4780 }
4781 *slot = file_data;
4782
4783 for (int j = 0; j < file_data->num_file_names; ++j)
4784 {
4785 const char *filename = file_data->file_names[j];
4786 dwarf2_per_objfile->filenames_cache->seen (filename);
4787 }
4788 }
4789 }
4790
4791 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4792 {
4793 gdb::unique_xmalloc_ptr<char> this_real_name;
4794
4795 if (need_fullname)
4796 this_real_name = gdb_realpath (filename);
4797 (*fun) (filename, this_real_name.get (), data);
4798 });
4799 }
4800
4801 static int
4802 dw2_has_symbols (struct objfile *objfile)
4803 {
4804 return 1;
4805 }
4806
4807 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4808 {
4809 dw2_has_symbols,
4810 dw2_find_last_source_symtab,
4811 dw2_forget_cached_source_info,
4812 dw2_map_symtabs_matching_filename,
4813 dw2_lookup_symbol,
4814 NULL,
4815 dw2_print_stats,
4816 dw2_dump,
4817 dw2_expand_symtabs_for_function,
4818 dw2_expand_all_symtabs,
4819 dw2_expand_symtabs_with_fullname,
4820 dw2_map_matching_symbols,
4821 dw2_expand_symtabs_matching,
4822 dw2_find_pc_sect_compunit_symtab,
4823 NULL,
4824 dw2_map_symbol_filenames
4825 };
4826
4827 /* DWARF-5 debug_names reader. */
4828
4829 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4830 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4831
4832 /* A helper function that reads the .debug_names section in SECTION
4833 and fills in MAP. FILENAME is the name of the file containing the
4834 section; it is used for error reporting.
4835
4836 Returns true if all went well, false otherwise. */
4837
4838 static bool
4839 read_debug_names_from_section (struct objfile *objfile,
4840 const char *filename,
4841 struct dwarf2_section_info *section,
4842 mapped_debug_names &map)
4843 {
4844 if (section->empty ())
4845 return false;
4846
4847 /* Older elfutils strip versions could keep the section in the main
4848 executable while splitting it for the separate debug info file. */
4849 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4850 return false;
4851
4852 section->read (objfile);
4853
4854 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4855
4856 const gdb_byte *addr = section->buffer;
4857
4858 bfd *const abfd = section->get_bfd_owner ();
4859
4860 unsigned int bytes_read;
4861 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4862 addr += bytes_read;
4863
4864 map.dwarf5_is_dwarf64 = bytes_read != 4;
4865 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4866 if (bytes_read + length != section->size)
4867 {
4868 /* There may be multiple per-CU indices. */
4869 warning (_("Section .debug_names in %s length %s does not match "
4870 "section length %s, ignoring .debug_names."),
4871 filename, plongest (bytes_read + length),
4872 pulongest (section->size));
4873 return false;
4874 }
4875
4876 /* The version number. */
4877 uint16_t version = read_2_bytes (abfd, addr);
4878 addr += 2;
4879 if (version != 5)
4880 {
4881 warning (_("Section .debug_names in %s has unsupported version %d, "
4882 "ignoring .debug_names."),
4883 filename, version);
4884 return false;
4885 }
4886
4887 /* Padding. */
4888 uint16_t padding = read_2_bytes (abfd, addr);
4889 addr += 2;
4890 if (padding != 0)
4891 {
4892 warning (_("Section .debug_names in %s has unsupported padding %d, "
4893 "ignoring .debug_names."),
4894 filename, padding);
4895 return false;
4896 }
4897
4898 /* comp_unit_count - The number of CUs in the CU list. */
4899 map.cu_count = read_4_bytes (abfd, addr);
4900 addr += 4;
4901
4902 /* local_type_unit_count - The number of TUs in the local TU
4903 list. */
4904 map.tu_count = read_4_bytes (abfd, addr);
4905 addr += 4;
4906
4907 /* foreign_type_unit_count - The number of TUs in the foreign TU
4908 list. */
4909 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4910 addr += 4;
4911 if (foreign_tu_count != 0)
4912 {
4913 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4914 "ignoring .debug_names."),
4915 filename, static_cast<unsigned long> (foreign_tu_count));
4916 return false;
4917 }
4918
4919 /* bucket_count - The number of hash buckets in the hash lookup
4920 table. */
4921 map.bucket_count = read_4_bytes (abfd, addr);
4922 addr += 4;
4923
4924 /* name_count - The number of unique names in the index. */
4925 map.name_count = read_4_bytes (abfd, addr);
4926 addr += 4;
4927
4928 /* abbrev_table_size - The size in bytes of the abbreviations
4929 table. */
4930 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4931 addr += 4;
4932
4933 /* augmentation_string_size - The size in bytes of the augmentation
4934 string. This value is rounded up to a multiple of 4. */
4935 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4936 addr += 4;
4937 map.augmentation_is_gdb = ((augmentation_string_size
4938 == sizeof (dwarf5_augmentation))
4939 && memcmp (addr, dwarf5_augmentation,
4940 sizeof (dwarf5_augmentation)) == 0);
4941 augmentation_string_size += (-augmentation_string_size) & 3;
4942 addr += augmentation_string_size;
4943
4944 /* List of CUs */
4945 map.cu_table_reordered = addr;
4946 addr += map.cu_count * map.offset_size;
4947
4948 /* List of Local TUs */
4949 map.tu_table_reordered = addr;
4950 addr += map.tu_count * map.offset_size;
4951
4952 /* Hash Lookup Table */
4953 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4954 addr += map.bucket_count * 4;
4955 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4956 addr += map.name_count * 4;
4957
4958 /* Name Table */
4959 map.name_table_string_offs_reordered = addr;
4960 addr += map.name_count * map.offset_size;
4961 map.name_table_entry_offs_reordered = addr;
4962 addr += map.name_count * map.offset_size;
4963
4964 const gdb_byte *abbrev_table_start = addr;
4965 for (;;)
4966 {
4967 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4968 addr += bytes_read;
4969 if (index_num == 0)
4970 break;
4971
4972 const auto insertpair
4973 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4974 if (!insertpair.second)
4975 {
4976 warning (_("Section .debug_names in %s has duplicate index %s, "
4977 "ignoring .debug_names."),
4978 filename, pulongest (index_num));
4979 return false;
4980 }
4981 mapped_debug_names::index_val &indexval = insertpair.first->second;
4982 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4983 addr += bytes_read;
4984
4985 for (;;)
4986 {
4987 mapped_debug_names::index_val::attr attr;
4988 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4989 addr += bytes_read;
4990 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4991 addr += bytes_read;
4992 if (attr.form == DW_FORM_implicit_const)
4993 {
4994 attr.implicit_const = read_signed_leb128 (abfd, addr,
4995 &bytes_read);
4996 addr += bytes_read;
4997 }
4998 if (attr.dw_idx == 0 && attr.form == 0)
4999 break;
5000 indexval.attr_vec.push_back (std::move (attr));
5001 }
5002 }
5003 if (addr != abbrev_table_start + abbrev_table_size)
5004 {
5005 warning (_("Section .debug_names in %s has abbreviation_table "
5006 "of size %s vs. written as %u, ignoring .debug_names."),
5007 filename, plongest (addr - abbrev_table_start),
5008 abbrev_table_size);
5009 return false;
5010 }
5011 map.entry_pool = addr;
5012
5013 return true;
5014 }
5015
5016 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5017 list. */
5018
5019 static void
5020 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5021 const mapped_debug_names &map,
5022 dwarf2_section_info &section,
5023 bool is_dwz)
5024 {
5025 sect_offset sect_off_prev;
5026 for (uint32_t i = 0; i <= map.cu_count; ++i)
5027 {
5028 sect_offset sect_off_next;
5029 if (i < map.cu_count)
5030 {
5031 sect_off_next
5032 = (sect_offset) (extract_unsigned_integer
5033 (map.cu_table_reordered + i * map.offset_size,
5034 map.offset_size,
5035 map.dwarf5_byte_order));
5036 }
5037 else
5038 sect_off_next = (sect_offset) section.size;
5039 if (i >= 1)
5040 {
5041 const ULONGEST length = sect_off_next - sect_off_prev;
5042 dwarf2_per_cu_data *per_cu
5043 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5044 sect_off_prev, length);
5045 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5046 }
5047 sect_off_prev = sect_off_next;
5048 }
5049 }
5050
5051 /* Read the CU list from the mapped index, and use it to create all
5052 the CU objects for this dwarf2_per_objfile. */
5053
5054 static void
5055 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5056 const mapped_debug_names &map,
5057 const mapped_debug_names &dwz_map)
5058 {
5059 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5060 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5061
5062 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5063 dwarf2_per_objfile->info,
5064 false /* is_dwz */);
5065
5066 if (dwz_map.cu_count == 0)
5067 return;
5068
5069 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5070 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5071 true /* is_dwz */);
5072 }
5073
5074 /* Read .debug_names. If everything went ok, initialize the "quick"
5075 elements of all the CUs and return true. Otherwise, return false. */
5076
5077 static bool
5078 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5079 {
5080 std::unique_ptr<mapped_debug_names> map
5081 (new mapped_debug_names (dwarf2_per_objfile));
5082 mapped_debug_names dwz_map (dwarf2_per_objfile);
5083 struct objfile *objfile = dwarf2_per_objfile->objfile;
5084
5085 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5086 &dwarf2_per_objfile->debug_names,
5087 *map))
5088 return false;
5089
5090 /* Don't use the index if it's empty. */
5091 if (map->name_count == 0)
5092 return false;
5093
5094 /* If there is a .dwz file, read it so we can get its CU list as
5095 well. */
5096 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5097 if (dwz != NULL)
5098 {
5099 if (!read_debug_names_from_section (objfile,
5100 bfd_get_filename (dwz->dwz_bfd.get ()),
5101 &dwz->debug_names, dwz_map))
5102 {
5103 warning (_("could not read '.debug_names' section from %s; skipping"),
5104 bfd_get_filename (dwz->dwz_bfd.get ()));
5105 return false;
5106 }
5107 }
5108
5109 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5110
5111 if (map->tu_count != 0)
5112 {
5113 /* We can only handle a single .debug_types when we have an
5114 index. */
5115 if (dwarf2_per_objfile->types.size () != 1)
5116 return false;
5117
5118 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5119
5120 create_signatured_type_table_from_debug_names
5121 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5122 }
5123
5124 create_addrmap_from_aranges (dwarf2_per_objfile,
5125 &dwarf2_per_objfile->debug_aranges);
5126
5127 dwarf2_per_objfile->debug_names_table = std::move (map);
5128 dwarf2_per_objfile->using_index = 1;
5129 dwarf2_per_objfile->quick_file_names_table =
5130 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5131
5132 return true;
5133 }
5134
5135 /* Type used to manage iterating over all CUs looking for a symbol for
5136 .debug_names. */
5137
5138 class dw2_debug_names_iterator
5139 {
5140 public:
5141 dw2_debug_names_iterator (const mapped_debug_names &map,
5142 gdb::optional<block_enum> block_index,
5143 domain_enum domain,
5144 const char *name)
5145 : m_map (map), m_block_index (block_index), m_domain (domain),
5146 m_addr (find_vec_in_debug_names (map, name))
5147 {}
5148
5149 dw2_debug_names_iterator (const mapped_debug_names &map,
5150 search_domain search, uint32_t namei)
5151 : m_map (map),
5152 m_search (search),
5153 m_addr (find_vec_in_debug_names (map, namei))
5154 {}
5155
5156 dw2_debug_names_iterator (const mapped_debug_names &map,
5157 block_enum block_index, domain_enum domain,
5158 uint32_t namei)
5159 : m_map (map), m_block_index (block_index), m_domain (domain),
5160 m_addr (find_vec_in_debug_names (map, namei))
5161 {}
5162
5163 /* Return the next matching CU or NULL if there are no more. */
5164 dwarf2_per_cu_data *next ();
5165
5166 private:
5167 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5168 const char *name);
5169 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5170 uint32_t namei);
5171
5172 /* The internalized form of .debug_names. */
5173 const mapped_debug_names &m_map;
5174
5175 /* If set, only look for symbols that match that block. Valid values are
5176 GLOBAL_BLOCK and STATIC_BLOCK. */
5177 const gdb::optional<block_enum> m_block_index;
5178
5179 /* The kind of symbol we're looking for. */
5180 const domain_enum m_domain = UNDEF_DOMAIN;
5181 const search_domain m_search = ALL_DOMAIN;
5182
5183 /* The list of CUs from the index entry of the symbol, or NULL if
5184 not found. */
5185 const gdb_byte *m_addr;
5186 };
5187
5188 const char *
5189 mapped_debug_names::namei_to_name (uint32_t namei) const
5190 {
5191 const ULONGEST namei_string_offs
5192 = extract_unsigned_integer ((name_table_string_offs_reordered
5193 + namei * offset_size),
5194 offset_size,
5195 dwarf5_byte_order);
5196 return read_indirect_string_at_offset (dwarf2_per_objfile,
5197 namei_string_offs);
5198 }
5199
5200 /* Find a slot in .debug_names for the object named NAME. If NAME is
5201 found, return pointer to its pool data. If NAME cannot be found,
5202 return NULL. */
5203
5204 const gdb_byte *
5205 dw2_debug_names_iterator::find_vec_in_debug_names
5206 (const mapped_debug_names &map, const char *name)
5207 {
5208 int (*cmp) (const char *, const char *);
5209
5210 gdb::unique_xmalloc_ptr<char> without_params;
5211 if (current_language->la_language == language_cplus
5212 || current_language->la_language == language_fortran
5213 || current_language->la_language == language_d)
5214 {
5215 /* NAME is already canonical. Drop any qualifiers as
5216 .debug_names does not contain any. */
5217
5218 if (strchr (name, '(') != NULL)
5219 {
5220 without_params = cp_remove_params (name);
5221 if (without_params != NULL)
5222 name = without_params.get ();
5223 }
5224 }
5225
5226 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5227
5228 const uint32_t full_hash = dwarf5_djb_hash (name);
5229 uint32_t namei
5230 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5231 (map.bucket_table_reordered
5232 + (full_hash % map.bucket_count)), 4,
5233 map.dwarf5_byte_order);
5234 if (namei == 0)
5235 return NULL;
5236 --namei;
5237 if (namei >= map.name_count)
5238 {
5239 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5240 "[in module %s]"),
5241 namei, map.name_count,
5242 objfile_name (map.dwarf2_per_objfile->objfile));
5243 return NULL;
5244 }
5245
5246 for (;;)
5247 {
5248 const uint32_t namei_full_hash
5249 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5250 (map.hash_table_reordered + namei), 4,
5251 map.dwarf5_byte_order);
5252 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5253 return NULL;
5254
5255 if (full_hash == namei_full_hash)
5256 {
5257 const char *const namei_string = map.namei_to_name (namei);
5258
5259 #if 0 /* An expensive sanity check. */
5260 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5261 {
5262 complaint (_("Wrong .debug_names hash for string at index %u "
5263 "[in module %s]"),
5264 namei, objfile_name (dwarf2_per_objfile->objfile));
5265 return NULL;
5266 }
5267 #endif
5268
5269 if (cmp (namei_string, name) == 0)
5270 {
5271 const ULONGEST namei_entry_offs
5272 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5273 + namei * map.offset_size),
5274 map.offset_size, map.dwarf5_byte_order);
5275 return map.entry_pool + namei_entry_offs;
5276 }
5277 }
5278
5279 ++namei;
5280 if (namei >= map.name_count)
5281 return NULL;
5282 }
5283 }
5284
5285 const gdb_byte *
5286 dw2_debug_names_iterator::find_vec_in_debug_names
5287 (const mapped_debug_names &map, uint32_t namei)
5288 {
5289 if (namei >= map.name_count)
5290 {
5291 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5292 "[in module %s]"),
5293 namei, map.name_count,
5294 objfile_name (map.dwarf2_per_objfile->objfile));
5295 return NULL;
5296 }
5297
5298 const ULONGEST namei_entry_offs
5299 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5300 + namei * map.offset_size),
5301 map.offset_size, map.dwarf5_byte_order);
5302 return map.entry_pool + namei_entry_offs;
5303 }
5304
5305 /* See dw2_debug_names_iterator. */
5306
5307 dwarf2_per_cu_data *
5308 dw2_debug_names_iterator::next ()
5309 {
5310 if (m_addr == NULL)
5311 return NULL;
5312
5313 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5314 struct objfile *objfile = dwarf2_per_objfile->objfile;
5315 bfd *const abfd = objfile->obfd;
5316
5317 again:
5318
5319 unsigned int bytes_read;
5320 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5321 m_addr += bytes_read;
5322 if (abbrev == 0)
5323 return NULL;
5324
5325 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5326 if (indexval_it == m_map.abbrev_map.cend ())
5327 {
5328 complaint (_("Wrong .debug_names undefined abbrev code %s "
5329 "[in module %s]"),
5330 pulongest (abbrev), objfile_name (objfile));
5331 return NULL;
5332 }
5333 const mapped_debug_names::index_val &indexval = indexval_it->second;
5334 enum class symbol_linkage {
5335 unknown,
5336 static_,
5337 extern_,
5338 } symbol_linkage_ = symbol_linkage::unknown;
5339 dwarf2_per_cu_data *per_cu = NULL;
5340 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5341 {
5342 ULONGEST ull;
5343 switch (attr.form)
5344 {
5345 case DW_FORM_implicit_const:
5346 ull = attr.implicit_const;
5347 break;
5348 case DW_FORM_flag_present:
5349 ull = 1;
5350 break;
5351 case DW_FORM_udata:
5352 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5353 m_addr += bytes_read;
5354 break;
5355 case DW_FORM_ref4:
5356 ull = read_4_bytes (abfd, m_addr);
5357 m_addr += 4;
5358 break;
5359 case DW_FORM_ref8:
5360 ull = read_8_bytes (abfd, m_addr);
5361 m_addr += 8;
5362 break;
5363 case DW_FORM_ref_sig8:
5364 ull = read_8_bytes (abfd, m_addr);
5365 m_addr += 8;
5366 break;
5367 default:
5368 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5369 dwarf_form_name (attr.form),
5370 objfile_name (objfile));
5371 return NULL;
5372 }
5373 switch (attr.dw_idx)
5374 {
5375 case DW_IDX_compile_unit:
5376 /* Don't crash on bad data. */
5377 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5378 {
5379 complaint (_(".debug_names entry has bad CU index %s"
5380 " [in module %s]"),
5381 pulongest (ull),
5382 objfile_name (dwarf2_per_objfile->objfile));
5383 continue;
5384 }
5385 per_cu = dwarf2_per_objfile->get_cutu (ull);
5386 break;
5387 case DW_IDX_type_unit:
5388 /* Don't crash on bad data. */
5389 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5390 {
5391 complaint (_(".debug_names entry has bad TU index %s"
5392 " [in module %s]"),
5393 pulongest (ull),
5394 objfile_name (dwarf2_per_objfile->objfile));
5395 continue;
5396 }
5397 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5398 break;
5399 case DW_IDX_die_offset:
5400 /* In a per-CU index (as opposed to a per-module index), index
5401 entries without CU attribute implicitly refer to the single CU. */
5402 if (per_cu == NULL)
5403 per_cu = dwarf2_per_objfile->get_cu (0);
5404 break;
5405 case DW_IDX_GNU_internal:
5406 if (!m_map.augmentation_is_gdb)
5407 break;
5408 symbol_linkage_ = symbol_linkage::static_;
5409 break;
5410 case DW_IDX_GNU_external:
5411 if (!m_map.augmentation_is_gdb)
5412 break;
5413 symbol_linkage_ = symbol_linkage::extern_;
5414 break;
5415 }
5416 }
5417
5418 /* Skip if already read in. */
5419 if (per_cu->v.quick->compunit_symtab)
5420 goto again;
5421
5422 /* Check static vs global. */
5423 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5424 {
5425 const bool want_static = *m_block_index == STATIC_BLOCK;
5426 const bool symbol_is_static =
5427 symbol_linkage_ == symbol_linkage::static_;
5428 if (want_static != symbol_is_static)
5429 goto again;
5430 }
5431
5432 /* Match dw2_symtab_iter_next, symbol_kind
5433 and debug_names::psymbol_tag. */
5434 switch (m_domain)
5435 {
5436 case VAR_DOMAIN:
5437 switch (indexval.dwarf_tag)
5438 {
5439 case DW_TAG_variable:
5440 case DW_TAG_subprogram:
5441 /* Some types are also in VAR_DOMAIN. */
5442 case DW_TAG_typedef:
5443 case DW_TAG_structure_type:
5444 break;
5445 default:
5446 goto again;
5447 }
5448 break;
5449 case STRUCT_DOMAIN:
5450 switch (indexval.dwarf_tag)
5451 {
5452 case DW_TAG_typedef:
5453 case DW_TAG_structure_type:
5454 break;
5455 default:
5456 goto again;
5457 }
5458 break;
5459 case LABEL_DOMAIN:
5460 switch (indexval.dwarf_tag)
5461 {
5462 case 0:
5463 case DW_TAG_variable:
5464 break;
5465 default:
5466 goto again;
5467 }
5468 break;
5469 case MODULE_DOMAIN:
5470 switch (indexval.dwarf_tag)
5471 {
5472 case DW_TAG_module:
5473 break;
5474 default:
5475 goto again;
5476 }
5477 break;
5478 default:
5479 break;
5480 }
5481
5482 /* Match dw2_expand_symtabs_matching, symbol_kind and
5483 debug_names::psymbol_tag. */
5484 switch (m_search)
5485 {
5486 case VARIABLES_DOMAIN:
5487 switch (indexval.dwarf_tag)
5488 {
5489 case DW_TAG_variable:
5490 break;
5491 default:
5492 goto again;
5493 }
5494 break;
5495 case FUNCTIONS_DOMAIN:
5496 switch (indexval.dwarf_tag)
5497 {
5498 case DW_TAG_subprogram:
5499 break;
5500 default:
5501 goto again;
5502 }
5503 break;
5504 case TYPES_DOMAIN:
5505 switch (indexval.dwarf_tag)
5506 {
5507 case DW_TAG_typedef:
5508 case DW_TAG_structure_type:
5509 break;
5510 default:
5511 goto again;
5512 }
5513 break;
5514 case MODULES_DOMAIN:
5515 switch (indexval.dwarf_tag)
5516 {
5517 case DW_TAG_module:
5518 break;
5519 default:
5520 goto again;
5521 }
5522 default:
5523 break;
5524 }
5525
5526 return per_cu;
5527 }
5528
5529 static struct compunit_symtab *
5530 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5531 const char *name, domain_enum domain)
5532 {
5533 struct dwarf2_per_objfile *dwarf2_per_objfile
5534 = get_dwarf2_per_objfile (objfile);
5535
5536 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5537 if (!mapp)
5538 {
5539 /* index is NULL if OBJF_READNOW. */
5540 return NULL;
5541 }
5542 const auto &map = *mapp;
5543
5544 dw2_debug_names_iterator iter (map, block_index, domain, name);
5545
5546 struct compunit_symtab *stab_best = NULL;
5547 struct dwarf2_per_cu_data *per_cu;
5548 while ((per_cu = iter.next ()) != NULL)
5549 {
5550 struct symbol *sym, *with_opaque = NULL;
5551 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5552 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5553 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5554
5555 sym = block_find_symbol (block, name, domain,
5556 block_find_non_opaque_type_preferred,
5557 &with_opaque);
5558
5559 /* Some caution must be observed with overloaded functions and
5560 methods, since the index will not contain any overload
5561 information (but NAME might contain it). */
5562
5563 if (sym != NULL
5564 && strcmp_iw (sym->search_name (), name) == 0)
5565 return stab;
5566 if (with_opaque != NULL
5567 && strcmp_iw (with_opaque->search_name (), name) == 0)
5568 stab_best = stab;
5569
5570 /* Keep looking through other CUs. */
5571 }
5572
5573 return stab_best;
5574 }
5575
5576 /* This dumps minimal information about .debug_names. It is called
5577 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5578 uses this to verify that .debug_names has been loaded. */
5579
5580 static void
5581 dw2_debug_names_dump (struct objfile *objfile)
5582 {
5583 struct dwarf2_per_objfile *dwarf2_per_objfile
5584 = get_dwarf2_per_objfile (objfile);
5585
5586 gdb_assert (dwarf2_per_objfile->using_index);
5587 printf_filtered (".debug_names:");
5588 if (dwarf2_per_objfile->debug_names_table)
5589 printf_filtered (" exists\n");
5590 else
5591 printf_filtered (" faked for \"readnow\"\n");
5592 printf_filtered ("\n");
5593 }
5594
5595 static void
5596 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5597 const char *func_name)
5598 {
5599 struct dwarf2_per_objfile *dwarf2_per_objfile
5600 = get_dwarf2_per_objfile (objfile);
5601
5602 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5603 if (dwarf2_per_objfile->debug_names_table)
5604 {
5605 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5606
5607 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5608
5609 struct dwarf2_per_cu_data *per_cu;
5610 while ((per_cu = iter.next ()) != NULL)
5611 dw2_instantiate_symtab (per_cu, false);
5612 }
5613 }
5614
5615 static void
5616 dw2_debug_names_map_matching_symbols
5617 (struct objfile *objfile,
5618 const lookup_name_info &name, domain_enum domain,
5619 int global,
5620 gdb::function_view<symbol_found_callback_ftype> callback,
5621 symbol_compare_ftype *ordered_compare)
5622 {
5623 struct dwarf2_per_objfile *dwarf2_per_objfile
5624 = get_dwarf2_per_objfile (objfile);
5625
5626 /* debug_names_table is NULL if OBJF_READNOW. */
5627 if (!dwarf2_per_objfile->debug_names_table)
5628 return;
5629
5630 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5631 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5632
5633 const char *match_name = name.ada ().lookup_name ().c_str ();
5634 auto matcher = [&] (const char *symname)
5635 {
5636 if (ordered_compare == nullptr)
5637 return true;
5638 return ordered_compare (symname, match_name) == 0;
5639 };
5640
5641 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5642 [&] (offset_type namei)
5643 {
5644 /* The name was matched, now expand corresponding CUs that were
5645 marked. */
5646 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5647
5648 struct dwarf2_per_cu_data *per_cu;
5649 while ((per_cu = iter.next ()) != NULL)
5650 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5651 return true;
5652 });
5653
5654 /* It's a shame we couldn't do this inside the
5655 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5656 that have already been expanded. Instead, this loop matches what
5657 the psymtab code does. */
5658 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5659 {
5660 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5661 if (cust != nullptr)
5662 {
5663 const struct block *block
5664 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5665 if (!iterate_over_symbols_terminated (block, name,
5666 domain, callback))
5667 break;
5668 }
5669 }
5670 }
5671
5672 static void
5673 dw2_debug_names_expand_symtabs_matching
5674 (struct objfile *objfile,
5675 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5676 const lookup_name_info *lookup_name,
5677 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5678 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5679 enum search_domain kind)
5680 {
5681 struct dwarf2_per_objfile *dwarf2_per_objfile
5682 = get_dwarf2_per_objfile (objfile);
5683
5684 /* debug_names_table is NULL if OBJF_READNOW. */
5685 if (!dwarf2_per_objfile->debug_names_table)
5686 return;
5687
5688 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5689
5690 if (symbol_matcher == NULL && lookup_name == NULL)
5691 {
5692 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5693 {
5694 QUIT;
5695
5696 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5697 expansion_notify);
5698 }
5699 return;
5700 }
5701
5702 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5703
5704 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5705 symbol_matcher,
5706 kind, [&] (offset_type namei)
5707 {
5708 /* The name was matched, now expand corresponding CUs that were
5709 marked. */
5710 dw2_debug_names_iterator iter (map, kind, namei);
5711
5712 struct dwarf2_per_cu_data *per_cu;
5713 while ((per_cu = iter.next ()) != NULL)
5714 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5715 expansion_notify);
5716 return true;
5717 });
5718 }
5719
5720 const struct quick_symbol_functions dwarf2_debug_names_functions =
5721 {
5722 dw2_has_symbols,
5723 dw2_find_last_source_symtab,
5724 dw2_forget_cached_source_info,
5725 dw2_map_symtabs_matching_filename,
5726 dw2_debug_names_lookup_symbol,
5727 NULL,
5728 dw2_print_stats,
5729 dw2_debug_names_dump,
5730 dw2_debug_names_expand_symtabs_for_function,
5731 dw2_expand_all_symtabs,
5732 dw2_expand_symtabs_with_fullname,
5733 dw2_debug_names_map_matching_symbols,
5734 dw2_debug_names_expand_symtabs_matching,
5735 dw2_find_pc_sect_compunit_symtab,
5736 NULL,
5737 dw2_map_symbol_filenames
5738 };
5739
5740 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5741 to either a dwarf2_per_objfile or dwz_file object. */
5742
5743 template <typename T>
5744 static gdb::array_view<const gdb_byte>
5745 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5746 {
5747 dwarf2_section_info *section = &section_owner->gdb_index;
5748
5749 if (section->empty ())
5750 return {};
5751
5752 /* Older elfutils strip versions could keep the section in the main
5753 executable while splitting it for the separate debug info file. */
5754 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5755 return {};
5756
5757 section->read (obj);
5758
5759 /* dwarf2_section_info::size is a bfd_size_type, while
5760 gdb::array_view works with size_t. On 32-bit hosts, with
5761 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5762 is 32-bit. So we need an explicit narrowing conversion here.
5763 This is fine, because it's impossible to allocate or mmap an
5764 array/buffer larger than what size_t can represent. */
5765 return gdb::make_array_view (section->buffer, section->size);
5766 }
5767
5768 /* Lookup the index cache for the contents of the index associated to
5769 DWARF2_OBJ. */
5770
5771 static gdb::array_view<const gdb_byte>
5772 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5773 {
5774 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5775 if (build_id == nullptr)
5776 return {};
5777
5778 return global_index_cache.lookup_gdb_index (build_id,
5779 &dwarf2_obj->index_cache_res);
5780 }
5781
5782 /* Same as the above, but for DWZ. */
5783
5784 static gdb::array_view<const gdb_byte>
5785 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5786 {
5787 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5788 if (build_id == nullptr)
5789 return {};
5790
5791 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5792 }
5793
5794 /* See symfile.h. */
5795
5796 bool
5797 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5798 {
5799 struct dwarf2_per_objfile *dwarf2_per_objfile
5800 = get_dwarf2_per_objfile (objfile);
5801
5802 /* If we're about to read full symbols, don't bother with the
5803 indices. In this case we also don't care if some other debug
5804 format is making psymtabs, because they are all about to be
5805 expanded anyway. */
5806 if ((objfile->flags & OBJF_READNOW))
5807 {
5808 dwarf2_per_objfile->using_index = 1;
5809 create_all_comp_units (dwarf2_per_objfile);
5810 create_all_type_units (dwarf2_per_objfile);
5811 dwarf2_per_objfile->quick_file_names_table
5812 = create_quick_file_names_table
5813 (dwarf2_per_objfile->all_comp_units.size ());
5814
5815 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5816 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5817 {
5818 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5819
5820 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5821 struct dwarf2_per_cu_quick_data);
5822 }
5823
5824 /* Return 1 so that gdb sees the "quick" functions. However,
5825 these functions will be no-ops because we will have expanded
5826 all symtabs. */
5827 *index_kind = dw_index_kind::GDB_INDEX;
5828 return true;
5829 }
5830
5831 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5832 {
5833 *index_kind = dw_index_kind::DEBUG_NAMES;
5834 return true;
5835 }
5836
5837 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5838 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5839 get_gdb_index_contents_from_section<dwz_file>))
5840 {
5841 *index_kind = dw_index_kind::GDB_INDEX;
5842 return true;
5843 }
5844
5845 /* ... otherwise, try to find the index in the index cache. */
5846 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5847 get_gdb_index_contents_from_cache,
5848 get_gdb_index_contents_from_cache_dwz))
5849 {
5850 global_index_cache.hit ();
5851 *index_kind = dw_index_kind::GDB_INDEX;
5852 return true;
5853 }
5854
5855 global_index_cache.miss ();
5856 return false;
5857 }
5858
5859 \f
5860
5861 /* Build a partial symbol table. */
5862
5863 void
5864 dwarf2_build_psymtabs (struct objfile *objfile)
5865 {
5866 struct dwarf2_per_objfile *dwarf2_per_objfile
5867 = get_dwarf2_per_objfile (objfile);
5868
5869 init_psymbol_list (objfile, 1024);
5870
5871 try
5872 {
5873 /* This isn't really ideal: all the data we allocate on the
5874 objfile's obstack is still uselessly kept around. However,
5875 freeing it seems unsafe. */
5876 psymtab_discarder psymtabs (objfile);
5877 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5878 psymtabs.keep ();
5879
5880 /* (maybe) store an index in the cache. */
5881 global_index_cache.store (dwarf2_per_objfile);
5882 }
5883 catch (const gdb_exception_error &except)
5884 {
5885 exception_print (gdb_stderr, except);
5886 }
5887 }
5888
5889 /* Find the base address of the compilation unit for range lists and
5890 location lists. It will normally be specified by DW_AT_low_pc.
5891 In DWARF-3 draft 4, the base address could be overridden by
5892 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5893 compilation units with discontinuous ranges. */
5894
5895 static void
5896 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5897 {
5898 struct attribute *attr;
5899
5900 cu->base_address.reset ();
5901
5902 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5903 if (attr != nullptr)
5904 cu->base_address = attr->value_as_address ();
5905 else
5906 {
5907 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5908 if (attr != nullptr)
5909 cu->base_address = attr->value_as_address ();
5910 }
5911 }
5912
5913 /* Helper function that returns the proper abbrev section for
5914 THIS_CU. */
5915
5916 static struct dwarf2_section_info *
5917 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5918 {
5919 struct dwarf2_section_info *abbrev;
5920 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5921
5922 if (this_cu->is_dwz)
5923 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5924 else
5925 abbrev = &dwarf2_per_objfile->abbrev;
5926
5927 return abbrev;
5928 }
5929
5930 /* Fetch the abbreviation table offset from a comp or type unit header. */
5931
5932 static sect_offset
5933 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5934 struct dwarf2_section_info *section,
5935 sect_offset sect_off)
5936 {
5937 bfd *abfd = section->get_bfd_owner ();
5938 const gdb_byte *info_ptr;
5939 unsigned int initial_length_size, offset_size;
5940 uint16_t version;
5941
5942 section->read (dwarf2_per_objfile->objfile);
5943 info_ptr = section->buffer + to_underlying (sect_off);
5944 read_initial_length (abfd, info_ptr, &initial_length_size);
5945 offset_size = initial_length_size == 4 ? 4 : 8;
5946 info_ptr += initial_length_size;
5947
5948 version = read_2_bytes (abfd, info_ptr);
5949 info_ptr += 2;
5950 if (version >= 5)
5951 {
5952 /* Skip unit type and address size. */
5953 info_ptr += 2;
5954 }
5955
5956 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5957 }
5958
5959 /* A partial symtab that is used only for include files. */
5960 struct dwarf2_include_psymtab : public partial_symtab
5961 {
5962 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5963 : partial_symtab (filename, objfile)
5964 {
5965 }
5966
5967 void read_symtab (struct objfile *objfile) override
5968 {
5969 /* It's an include file, no symbols to read for it.
5970 Everything is in the includer symtab. */
5971
5972 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5973 expansion of the includer psymtab. We use the dependencies[0] field to
5974 model the includer. But if we go the regular route of calling
5975 expand_psymtab here, and having expand_psymtab call expand_dependencies
5976 to expand the includer, we'll only use expand_psymtab on the includer
5977 (making it a non-toplevel psymtab), while if we expand the includer via
5978 another path, we'll use read_symtab (making it a toplevel psymtab).
5979 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5980 psymtab, and trigger read_symtab on the includer here directly. */
5981 includer ()->read_symtab (objfile);
5982 }
5983
5984 void expand_psymtab (struct objfile *objfile) override
5985 {
5986 /* This is not called by read_symtab, and should not be called by any
5987 expand_dependencies. */
5988 gdb_assert (false);
5989 }
5990
5991 bool readin_p () const override
5992 {
5993 return includer ()->readin_p ();
5994 }
5995
5996 struct compunit_symtab *get_compunit_symtab () const override
5997 {
5998 return nullptr;
5999 }
6000
6001 private:
6002 partial_symtab *includer () const
6003 {
6004 /* An include psymtab has exactly one dependency: the psymtab that
6005 includes it. */
6006 gdb_assert (this->number_of_dependencies == 1);
6007 return this->dependencies[0];
6008 }
6009 };
6010
6011 /* Allocate a new partial symtab for file named NAME and mark this new
6012 partial symtab as being an include of PST. */
6013
6014 static void
6015 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6016 struct objfile *objfile)
6017 {
6018 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6019
6020 if (!IS_ABSOLUTE_PATH (subpst->filename))
6021 {
6022 /* It shares objfile->objfile_obstack. */
6023 subpst->dirname = pst->dirname;
6024 }
6025
6026 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6027 subpst->dependencies[0] = pst;
6028 subpst->number_of_dependencies = 1;
6029 }
6030
6031 /* Read the Line Number Program data and extract the list of files
6032 included by the source file represented by PST. Build an include
6033 partial symtab for each of these included files. */
6034
6035 static void
6036 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6037 struct die_info *die,
6038 dwarf2_psymtab *pst)
6039 {
6040 line_header_up lh;
6041 struct attribute *attr;
6042
6043 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6044 if (attr != nullptr)
6045 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6046 if (lh == NULL)
6047 return; /* No linetable, so no includes. */
6048
6049 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6050 that we pass in the raw text_low here; that is ok because we're
6051 only decoding the line table to make include partial symtabs, and
6052 so the addresses aren't really used. */
6053 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6054 pst->raw_text_low (), 1);
6055 }
6056
6057 static hashval_t
6058 hash_signatured_type (const void *item)
6059 {
6060 const struct signatured_type *sig_type
6061 = (const struct signatured_type *) item;
6062
6063 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6064 return sig_type->signature;
6065 }
6066
6067 static int
6068 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6069 {
6070 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6071 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6072
6073 return lhs->signature == rhs->signature;
6074 }
6075
6076 /* Allocate a hash table for signatured types. */
6077
6078 static htab_up
6079 allocate_signatured_type_table ()
6080 {
6081 return htab_up (htab_create_alloc (41,
6082 hash_signatured_type,
6083 eq_signatured_type,
6084 NULL, xcalloc, xfree));
6085 }
6086
6087 /* A helper function to add a signatured type CU to a table. */
6088
6089 static int
6090 add_signatured_type_cu_to_table (void **slot, void *datum)
6091 {
6092 struct signatured_type *sigt = (struct signatured_type *) *slot;
6093 std::vector<signatured_type *> *all_type_units
6094 = (std::vector<signatured_type *> *) datum;
6095
6096 all_type_units->push_back (sigt);
6097
6098 return 1;
6099 }
6100
6101 /* A helper for create_debug_types_hash_table. Read types from SECTION
6102 and fill them into TYPES_HTAB. It will process only type units,
6103 therefore DW_UT_type. */
6104
6105 static void
6106 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6107 struct dwo_file *dwo_file,
6108 dwarf2_section_info *section, htab_up &types_htab,
6109 rcuh_kind section_kind)
6110 {
6111 struct objfile *objfile = dwarf2_per_objfile->objfile;
6112 struct dwarf2_section_info *abbrev_section;
6113 bfd *abfd;
6114 const gdb_byte *info_ptr, *end_ptr;
6115
6116 abbrev_section = (dwo_file != NULL
6117 ? &dwo_file->sections.abbrev
6118 : &dwarf2_per_objfile->abbrev);
6119
6120 if (dwarf_read_debug)
6121 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6122 section->get_name (),
6123 abbrev_section->get_file_name ());
6124
6125 section->read (objfile);
6126 info_ptr = section->buffer;
6127
6128 if (info_ptr == NULL)
6129 return;
6130
6131 /* We can't set abfd until now because the section may be empty or
6132 not present, in which case the bfd is unknown. */
6133 abfd = section->get_bfd_owner ();
6134
6135 /* We don't use cutu_reader here because we don't need to read
6136 any dies: the signature is in the header. */
6137
6138 end_ptr = info_ptr + section->size;
6139 while (info_ptr < end_ptr)
6140 {
6141 struct signatured_type *sig_type;
6142 struct dwo_unit *dwo_tu;
6143 void **slot;
6144 const gdb_byte *ptr = info_ptr;
6145 struct comp_unit_head header;
6146 unsigned int length;
6147
6148 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6149
6150 /* Initialize it due to a false compiler warning. */
6151 header.signature = -1;
6152 header.type_cu_offset_in_tu = (cu_offset) -1;
6153
6154 /* We need to read the type's signature in order to build the hash
6155 table, but we don't need anything else just yet. */
6156
6157 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6158 abbrev_section, ptr, section_kind);
6159
6160 length = header.get_length ();
6161
6162 /* Skip dummy type units. */
6163 if (ptr >= info_ptr + length
6164 || peek_abbrev_code (abfd, ptr) == 0
6165 || header.unit_type != DW_UT_type)
6166 {
6167 info_ptr += length;
6168 continue;
6169 }
6170
6171 if (types_htab == NULL)
6172 {
6173 if (dwo_file)
6174 types_htab = allocate_dwo_unit_table ();
6175 else
6176 types_htab = allocate_signatured_type_table ();
6177 }
6178
6179 if (dwo_file)
6180 {
6181 sig_type = NULL;
6182 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6183 struct dwo_unit);
6184 dwo_tu->dwo_file = dwo_file;
6185 dwo_tu->signature = header.signature;
6186 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6187 dwo_tu->section = section;
6188 dwo_tu->sect_off = sect_off;
6189 dwo_tu->length = length;
6190 }
6191 else
6192 {
6193 /* N.B.: type_offset is not usable if this type uses a DWO file.
6194 The real type_offset is in the DWO file. */
6195 dwo_tu = NULL;
6196 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6197 struct signatured_type);
6198 sig_type->signature = header.signature;
6199 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6200 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6201 sig_type->per_cu.is_debug_types = 1;
6202 sig_type->per_cu.section = section;
6203 sig_type->per_cu.sect_off = sect_off;
6204 sig_type->per_cu.length = length;
6205 }
6206
6207 slot = htab_find_slot (types_htab.get (),
6208 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6209 INSERT);
6210 gdb_assert (slot != NULL);
6211 if (*slot != NULL)
6212 {
6213 sect_offset dup_sect_off;
6214
6215 if (dwo_file)
6216 {
6217 const struct dwo_unit *dup_tu
6218 = (const struct dwo_unit *) *slot;
6219
6220 dup_sect_off = dup_tu->sect_off;
6221 }
6222 else
6223 {
6224 const struct signatured_type *dup_tu
6225 = (const struct signatured_type *) *slot;
6226
6227 dup_sect_off = dup_tu->per_cu.sect_off;
6228 }
6229
6230 complaint (_("debug type entry at offset %s is duplicate to"
6231 " the entry at offset %s, signature %s"),
6232 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6233 hex_string (header.signature));
6234 }
6235 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6236
6237 if (dwarf_read_debug > 1)
6238 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6239 sect_offset_str (sect_off),
6240 hex_string (header.signature));
6241
6242 info_ptr += length;
6243 }
6244 }
6245
6246 /* Create the hash table of all entries in the .debug_types
6247 (or .debug_types.dwo) section(s).
6248 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6249 otherwise it is NULL.
6250
6251 The result is a pointer to the hash table or NULL if there are no types.
6252
6253 Note: This function processes DWO files only, not DWP files. */
6254
6255 static void
6256 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6257 struct dwo_file *dwo_file,
6258 gdb::array_view<dwarf2_section_info> type_sections,
6259 htab_up &types_htab)
6260 {
6261 for (dwarf2_section_info &section : type_sections)
6262 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6263 types_htab, rcuh_kind::TYPE);
6264 }
6265
6266 /* Create the hash table of all entries in the .debug_types section,
6267 and initialize all_type_units.
6268 The result is zero if there is an error (e.g. missing .debug_types section),
6269 otherwise non-zero. */
6270
6271 static int
6272 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6273 {
6274 htab_up types_htab;
6275
6276 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6277 &dwarf2_per_objfile->info, types_htab,
6278 rcuh_kind::COMPILE);
6279 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6280 dwarf2_per_objfile->types, types_htab);
6281 if (types_htab == NULL)
6282 {
6283 dwarf2_per_objfile->signatured_types = NULL;
6284 return 0;
6285 }
6286
6287 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6288
6289 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6290 dwarf2_per_objfile->all_type_units.reserve
6291 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6292
6293 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6294 add_signatured_type_cu_to_table,
6295 &dwarf2_per_objfile->all_type_units);
6296
6297 return 1;
6298 }
6299
6300 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6301 If SLOT is non-NULL, it is the entry to use in the hash table.
6302 Otherwise we find one. */
6303
6304 static struct signatured_type *
6305 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6306 void **slot)
6307 {
6308 struct objfile *objfile = dwarf2_per_objfile->objfile;
6309
6310 if (dwarf2_per_objfile->all_type_units.size ()
6311 == dwarf2_per_objfile->all_type_units.capacity ())
6312 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6313
6314 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6315 struct signatured_type);
6316
6317 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6318 sig_type->signature = sig;
6319 sig_type->per_cu.is_debug_types = 1;
6320 if (dwarf2_per_objfile->using_index)
6321 {
6322 sig_type->per_cu.v.quick =
6323 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6324 struct dwarf2_per_cu_quick_data);
6325 }
6326
6327 if (slot == NULL)
6328 {
6329 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6330 sig_type, INSERT);
6331 }
6332 gdb_assert (*slot == NULL);
6333 *slot = sig_type;
6334 /* The rest of sig_type must be filled in by the caller. */
6335 return sig_type;
6336 }
6337
6338 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6339 Fill in SIG_ENTRY with DWO_ENTRY. */
6340
6341 static void
6342 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6343 struct signatured_type *sig_entry,
6344 struct dwo_unit *dwo_entry)
6345 {
6346 /* Make sure we're not clobbering something we don't expect to. */
6347 gdb_assert (! sig_entry->per_cu.queued);
6348 gdb_assert (sig_entry->per_cu.cu == NULL);
6349 if (dwarf2_per_objfile->using_index)
6350 {
6351 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6352 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6353 }
6354 else
6355 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6356 gdb_assert (sig_entry->signature == dwo_entry->signature);
6357 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6358 gdb_assert (sig_entry->type_unit_group == NULL);
6359 gdb_assert (sig_entry->dwo_unit == NULL);
6360
6361 sig_entry->per_cu.section = dwo_entry->section;
6362 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6363 sig_entry->per_cu.length = dwo_entry->length;
6364 sig_entry->per_cu.reading_dwo_directly = 1;
6365 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6366 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6367 sig_entry->dwo_unit = dwo_entry;
6368 }
6369
6370 /* Subroutine of lookup_signatured_type.
6371 If we haven't read the TU yet, create the signatured_type data structure
6372 for a TU to be read in directly from a DWO file, bypassing the stub.
6373 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6374 using .gdb_index, then when reading a CU we want to stay in the DWO file
6375 containing that CU. Otherwise we could end up reading several other DWO
6376 files (due to comdat folding) to process the transitive closure of all the
6377 mentioned TUs, and that can be slow. The current DWO file will have every
6378 type signature that it needs.
6379 We only do this for .gdb_index because in the psymtab case we already have
6380 to read all the DWOs to build the type unit groups. */
6381
6382 static struct signatured_type *
6383 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6384 {
6385 struct dwarf2_per_objfile *dwarf2_per_objfile
6386 = cu->per_cu->dwarf2_per_objfile;
6387 struct dwo_file *dwo_file;
6388 struct dwo_unit find_dwo_entry, *dwo_entry;
6389 struct signatured_type find_sig_entry, *sig_entry;
6390 void **slot;
6391
6392 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6393
6394 /* If TU skeletons have been removed then we may not have read in any
6395 TUs yet. */
6396 if (dwarf2_per_objfile->signatured_types == NULL)
6397 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6398
6399 /* We only ever need to read in one copy of a signatured type.
6400 Use the global signatured_types array to do our own comdat-folding
6401 of types. If this is the first time we're reading this TU, and
6402 the TU has an entry in .gdb_index, replace the recorded data from
6403 .gdb_index with this TU. */
6404
6405 find_sig_entry.signature = sig;
6406 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6407 &find_sig_entry, INSERT);
6408 sig_entry = (struct signatured_type *) *slot;
6409
6410 /* We can get here with the TU already read, *or* in the process of being
6411 read. Don't reassign the global entry to point to this DWO if that's
6412 the case. Also note that if the TU is already being read, it may not
6413 have come from a DWO, the program may be a mix of Fission-compiled
6414 code and non-Fission-compiled code. */
6415
6416 /* Have we already tried to read this TU?
6417 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6418 needn't exist in the global table yet). */
6419 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6420 return sig_entry;
6421
6422 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6423 dwo_unit of the TU itself. */
6424 dwo_file = cu->dwo_unit->dwo_file;
6425
6426 /* Ok, this is the first time we're reading this TU. */
6427 if (dwo_file->tus == NULL)
6428 return NULL;
6429 find_dwo_entry.signature = sig;
6430 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6431 &find_dwo_entry);
6432 if (dwo_entry == NULL)
6433 return NULL;
6434
6435 /* If the global table doesn't have an entry for this TU, add one. */
6436 if (sig_entry == NULL)
6437 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6438
6439 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6440 sig_entry->per_cu.tu_read = 1;
6441 return sig_entry;
6442 }
6443
6444 /* Subroutine of lookup_signatured_type.
6445 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6446 then try the DWP file. If the TU stub (skeleton) has been removed then
6447 it won't be in .gdb_index. */
6448
6449 static struct signatured_type *
6450 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6451 {
6452 struct dwarf2_per_objfile *dwarf2_per_objfile
6453 = cu->per_cu->dwarf2_per_objfile;
6454 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6455 struct dwo_unit *dwo_entry;
6456 struct signatured_type find_sig_entry, *sig_entry;
6457 void **slot;
6458
6459 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6460 gdb_assert (dwp_file != NULL);
6461
6462 /* If TU skeletons have been removed then we may not have read in any
6463 TUs yet. */
6464 if (dwarf2_per_objfile->signatured_types == NULL)
6465 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6466
6467 find_sig_entry.signature = sig;
6468 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6469 &find_sig_entry, INSERT);
6470 sig_entry = (struct signatured_type *) *slot;
6471
6472 /* Have we already tried to read this TU?
6473 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6474 needn't exist in the global table yet). */
6475 if (sig_entry != NULL)
6476 return sig_entry;
6477
6478 if (dwp_file->tus == NULL)
6479 return NULL;
6480 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6481 sig, 1 /* is_debug_types */);
6482 if (dwo_entry == NULL)
6483 return NULL;
6484
6485 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6486 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6487
6488 return sig_entry;
6489 }
6490
6491 /* Lookup a signature based type for DW_FORM_ref_sig8.
6492 Returns NULL if signature SIG is not present in the table.
6493 It is up to the caller to complain about this. */
6494
6495 static struct signatured_type *
6496 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6497 {
6498 struct dwarf2_per_objfile *dwarf2_per_objfile
6499 = cu->per_cu->dwarf2_per_objfile;
6500
6501 if (cu->dwo_unit
6502 && dwarf2_per_objfile->using_index)
6503 {
6504 /* We're in a DWO/DWP file, and we're using .gdb_index.
6505 These cases require special processing. */
6506 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6507 return lookup_dwo_signatured_type (cu, sig);
6508 else
6509 return lookup_dwp_signatured_type (cu, sig);
6510 }
6511 else
6512 {
6513 struct signatured_type find_entry, *entry;
6514
6515 if (dwarf2_per_objfile->signatured_types == NULL)
6516 return NULL;
6517 find_entry.signature = sig;
6518 entry = ((struct signatured_type *)
6519 htab_find (dwarf2_per_objfile->signatured_types.get (),
6520 &find_entry));
6521 return entry;
6522 }
6523 }
6524
6525 /* Low level DIE reading support. */
6526
6527 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6528
6529 static void
6530 init_cu_die_reader (struct die_reader_specs *reader,
6531 struct dwarf2_cu *cu,
6532 struct dwarf2_section_info *section,
6533 struct dwo_file *dwo_file,
6534 struct abbrev_table *abbrev_table)
6535 {
6536 gdb_assert (section->readin && section->buffer != NULL);
6537 reader->abfd = section->get_bfd_owner ();
6538 reader->cu = cu;
6539 reader->dwo_file = dwo_file;
6540 reader->die_section = section;
6541 reader->buffer = section->buffer;
6542 reader->buffer_end = section->buffer + section->size;
6543 reader->abbrev_table = abbrev_table;
6544 }
6545
6546 /* Subroutine of cutu_reader to simplify it.
6547 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6548 There's just a lot of work to do, and cutu_reader is big enough
6549 already.
6550
6551 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6552 from it to the DIE in the DWO. If NULL we are skipping the stub.
6553 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6554 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6555 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6556 STUB_COMP_DIR may be non-NULL.
6557 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6558 are filled in with the info of the DIE from the DWO file.
6559 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6560 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6561 kept around for at least as long as *RESULT_READER.
6562
6563 The result is non-zero if a valid (non-dummy) DIE was found. */
6564
6565 static int
6566 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6567 struct dwo_unit *dwo_unit,
6568 struct die_info *stub_comp_unit_die,
6569 const char *stub_comp_dir,
6570 struct die_reader_specs *result_reader,
6571 const gdb_byte **result_info_ptr,
6572 struct die_info **result_comp_unit_die,
6573 abbrev_table_up *result_dwo_abbrev_table)
6574 {
6575 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6576 struct objfile *objfile = dwarf2_per_objfile->objfile;
6577 struct dwarf2_cu *cu = this_cu->cu;
6578 bfd *abfd;
6579 const gdb_byte *begin_info_ptr, *info_ptr;
6580 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6581 int i,num_extra_attrs;
6582 struct dwarf2_section_info *dwo_abbrev_section;
6583 struct die_info *comp_unit_die;
6584
6585 /* At most one of these may be provided. */
6586 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6587
6588 /* These attributes aren't processed until later:
6589 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6590 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6591 referenced later. However, these attributes are found in the stub
6592 which we won't have later. In order to not impose this complication
6593 on the rest of the code, we read them here and copy them to the
6594 DWO CU/TU die. */
6595
6596 stmt_list = NULL;
6597 low_pc = NULL;
6598 high_pc = NULL;
6599 ranges = NULL;
6600 comp_dir = NULL;
6601
6602 if (stub_comp_unit_die != NULL)
6603 {
6604 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6605 DWO file. */
6606 if (! this_cu->is_debug_types)
6607 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6608 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6609 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6610 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6611 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6612
6613 cu->addr_base = stub_comp_unit_die->addr_base ();
6614
6615 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6616 here (if needed). We need the value before we can process
6617 DW_AT_ranges. */
6618 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6619 }
6620 else if (stub_comp_dir != NULL)
6621 {
6622 /* Reconstruct the comp_dir attribute to simplify the code below. */
6623 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6624 comp_dir->name = DW_AT_comp_dir;
6625 comp_dir->form = DW_FORM_string;
6626 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6627 DW_STRING (comp_dir) = stub_comp_dir;
6628 }
6629
6630 /* Set up for reading the DWO CU/TU. */
6631 cu->dwo_unit = dwo_unit;
6632 dwarf2_section_info *section = dwo_unit->section;
6633 section->read (objfile);
6634 abfd = section->get_bfd_owner ();
6635 begin_info_ptr = info_ptr = (section->buffer
6636 + to_underlying (dwo_unit->sect_off));
6637 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6638
6639 if (this_cu->is_debug_types)
6640 {
6641 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6642
6643 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6644 &cu->header, section,
6645 dwo_abbrev_section,
6646 info_ptr, rcuh_kind::TYPE);
6647 /* This is not an assert because it can be caused by bad debug info. */
6648 if (sig_type->signature != cu->header.signature)
6649 {
6650 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6651 " TU at offset %s [in module %s]"),
6652 hex_string (sig_type->signature),
6653 hex_string (cu->header.signature),
6654 sect_offset_str (dwo_unit->sect_off),
6655 bfd_get_filename (abfd));
6656 }
6657 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6658 /* For DWOs coming from DWP files, we don't know the CU length
6659 nor the type's offset in the TU until now. */
6660 dwo_unit->length = cu->header.get_length ();
6661 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6662
6663 /* Establish the type offset that can be used to lookup the type.
6664 For DWO files, we don't know it until now. */
6665 sig_type->type_offset_in_section
6666 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6667 }
6668 else
6669 {
6670 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6671 &cu->header, section,
6672 dwo_abbrev_section,
6673 info_ptr, rcuh_kind::COMPILE);
6674 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6675 /* For DWOs coming from DWP files, we don't know the CU length
6676 until now. */
6677 dwo_unit->length = cu->header.get_length ();
6678 }
6679
6680 *result_dwo_abbrev_table
6681 = abbrev_table::read (objfile, dwo_abbrev_section,
6682 cu->header.abbrev_sect_off);
6683 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6684 result_dwo_abbrev_table->get ());
6685
6686 /* Read in the die, but leave space to copy over the attributes
6687 from the stub. This has the benefit of simplifying the rest of
6688 the code - all the work to maintain the illusion of a single
6689 DW_TAG_{compile,type}_unit DIE is done here. */
6690 num_extra_attrs = ((stmt_list != NULL)
6691 + (low_pc != NULL)
6692 + (high_pc != NULL)
6693 + (ranges != NULL)
6694 + (comp_dir != NULL));
6695 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6696 num_extra_attrs);
6697
6698 /* Copy over the attributes from the stub to the DIE we just read in. */
6699 comp_unit_die = *result_comp_unit_die;
6700 i = comp_unit_die->num_attrs;
6701 if (stmt_list != NULL)
6702 comp_unit_die->attrs[i++] = *stmt_list;
6703 if (low_pc != NULL)
6704 comp_unit_die->attrs[i++] = *low_pc;
6705 if (high_pc != NULL)
6706 comp_unit_die->attrs[i++] = *high_pc;
6707 if (ranges != NULL)
6708 comp_unit_die->attrs[i++] = *ranges;
6709 if (comp_dir != NULL)
6710 comp_unit_die->attrs[i++] = *comp_dir;
6711 comp_unit_die->num_attrs += num_extra_attrs;
6712
6713 if (dwarf_die_debug)
6714 {
6715 fprintf_unfiltered (gdb_stdlog,
6716 "Read die from %s@0x%x of %s:\n",
6717 section->get_name (),
6718 (unsigned) (begin_info_ptr - section->buffer),
6719 bfd_get_filename (abfd));
6720 dump_die (comp_unit_die, dwarf_die_debug);
6721 }
6722
6723 /* Skip dummy compilation units. */
6724 if (info_ptr >= begin_info_ptr + dwo_unit->length
6725 || peek_abbrev_code (abfd, info_ptr) == 0)
6726 return 0;
6727
6728 *result_info_ptr = info_ptr;
6729 return 1;
6730 }
6731
6732 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6733 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6734 signature is part of the header. */
6735 static gdb::optional<ULONGEST>
6736 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6737 {
6738 if (cu->header.version >= 5)
6739 return cu->header.signature;
6740 struct attribute *attr;
6741 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6742 if (attr == nullptr)
6743 return gdb::optional<ULONGEST> ();
6744 return DW_UNSND (attr);
6745 }
6746
6747 /* Subroutine of cutu_reader to simplify it.
6748 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6749 Returns NULL if the specified DWO unit cannot be found. */
6750
6751 static struct dwo_unit *
6752 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6753 struct die_info *comp_unit_die,
6754 const char *dwo_name)
6755 {
6756 struct dwarf2_cu *cu = this_cu->cu;
6757 struct dwo_unit *dwo_unit;
6758 const char *comp_dir;
6759
6760 gdb_assert (cu != NULL);
6761
6762 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6763 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6764 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6765
6766 if (this_cu->is_debug_types)
6767 {
6768 struct signatured_type *sig_type;
6769
6770 /* Since this_cu is the first member of struct signatured_type,
6771 we can go from a pointer to one to a pointer to the other. */
6772 sig_type = (struct signatured_type *) this_cu;
6773 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6774 }
6775 else
6776 {
6777 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6778 if (!signature.has_value ())
6779 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6780 " [in module %s]"),
6781 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6782 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6783 *signature);
6784 }
6785
6786 return dwo_unit;
6787 }
6788
6789 /* Subroutine of cutu_reader to simplify it.
6790 See it for a description of the parameters.
6791 Read a TU directly from a DWO file, bypassing the stub. */
6792
6793 void
6794 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6795 int use_existing_cu)
6796 {
6797 struct signatured_type *sig_type;
6798
6799 /* Verify we can do the following downcast, and that we have the
6800 data we need. */
6801 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6802 sig_type = (struct signatured_type *) this_cu;
6803 gdb_assert (sig_type->dwo_unit != NULL);
6804
6805 if (use_existing_cu && this_cu->cu != NULL)
6806 {
6807 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6808 /* There's no need to do the rereading_dwo_cu handling that
6809 cutu_reader does since we don't read the stub. */
6810 }
6811 else
6812 {
6813 /* If !use_existing_cu, this_cu->cu must be NULL. */
6814 gdb_assert (this_cu->cu == NULL);
6815 m_new_cu.reset (new dwarf2_cu (this_cu));
6816 }
6817
6818 /* A future optimization, if needed, would be to use an existing
6819 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6820 could share abbrev tables. */
6821
6822 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6823 NULL /* stub_comp_unit_die */,
6824 sig_type->dwo_unit->dwo_file->comp_dir,
6825 this, &info_ptr,
6826 &comp_unit_die,
6827 &m_dwo_abbrev_table) == 0)
6828 {
6829 /* Dummy die. */
6830 dummy_p = true;
6831 }
6832 }
6833
6834 /* Initialize a CU (or TU) and read its DIEs.
6835 If the CU defers to a DWO file, read the DWO file as well.
6836
6837 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6838 Otherwise the table specified in the comp unit header is read in and used.
6839 This is an optimization for when we already have the abbrev table.
6840
6841 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6842 Otherwise, a new CU is allocated with xmalloc. */
6843
6844 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6845 struct abbrev_table *abbrev_table,
6846 int use_existing_cu,
6847 bool skip_partial)
6848 : die_reader_specs {},
6849 m_this_cu (this_cu)
6850 {
6851 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6852 struct objfile *objfile = dwarf2_per_objfile->objfile;
6853 struct dwarf2_section_info *section = this_cu->section;
6854 bfd *abfd = section->get_bfd_owner ();
6855 struct dwarf2_cu *cu;
6856 const gdb_byte *begin_info_ptr;
6857 struct signatured_type *sig_type = NULL;
6858 struct dwarf2_section_info *abbrev_section;
6859 /* Non-zero if CU currently points to a DWO file and we need to
6860 reread it. When this happens we need to reread the skeleton die
6861 before we can reread the DWO file (this only applies to CUs, not TUs). */
6862 int rereading_dwo_cu = 0;
6863
6864 if (dwarf_die_debug)
6865 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6866 this_cu->is_debug_types ? "type" : "comp",
6867 sect_offset_str (this_cu->sect_off));
6868
6869 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6870 file (instead of going through the stub), short-circuit all of this. */
6871 if (this_cu->reading_dwo_directly)
6872 {
6873 /* Narrow down the scope of possibilities to have to understand. */
6874 gdb_assert (this_cu->is_debug_types);
6875 gdb_assert (abbrev_table == NULL);
6876 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6877 return;
6878 }
6879
6880 /* This is cheap if the section is already read in. */
6881 section->read (objfile);
6882
6883 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6884
6885 abbrev_section = get_abbrev_section_for_cu (this_cu);
6886
6887 if (use_existing_cu && this_cu->cu != NULL)
6888 {
6889 cu = this_cu->cu;
6890 /* If this CU is from a DWO file we need to start over, we need to
6891 refetch the attributes from the skeleton CU.
6892 This could be optimized by retrieving those attributes from when we
6893 were here the first time: the previous comp_unit_die was stored in
6894 comp_unit_obstack. But there's no data yet that we need this
6895 optimization. */
6896 if (cu->dwo_unit != NULL)
6897 rereading_dwo_cu = 1;
6898 }
6899 else
6900 {
6901 /* If !use_existing_cu, this_cu->cu must be NULL. */
6902 gdb_assert (this_cu->cu == NULL);
6903 m_new_cu.reset (new dwarf2_cu (this_cu));
6904 cu = m_new_cu.get ();
6905 }
6906
6907 /* Get the header. */
6908 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6909 {
6910 /* We already have the header, there's no need to read it in again. */
6911 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6912 }
6913 else
6914 {
6915 if (this_cu->is_debug_types)
6916 {
6917 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6918 &cu->header, section,
6919 abbrev_section, info_ptr,
6920 rcuh_kind::TYPE);
6921
6922 /* Since per_cu is the first member of struct signatured_type,
6923 we can go from a pointer to one to a pointer to the other. */
6924 sig_type = (struct signatured_type *) this_cu;
6925 gdb_assert (sig_type->signature == cu->header.signature);
6926 gdb_assert (sig_type->type_offset_in_tu
6927 == cu->header.type_cu_offset_in_tu);
6928 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6929
6930 /* LENGTH has not been set yet for type units if we're
6931 using .gdb_index. */
6932 this_cu->length = cu->header.get_length ();
6933
6934 /* Establish the type offset that can be used to lookup the type. */
6935 sig_type->type_offset_in_section =
6936 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6937
6938 this_cu->dwarf_version = cu->header.version;
6939 }
6940 else
6941 {
6942 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6943 &cu->header, section,
6944 abbrev_section,
6945 info_ptr,
6946 rcuh_kind::COMPILE);
6947
6948 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6949 gdb_assert (this_cu->length == cu->header.get_length ());
6950 this_cu->dwarf_version = cu->header.version;
6951 }
6952 }
6953
6954 /* Skip dummy compilation units. */
6955 if (info_ptr >= begin_info_ptr + this_cu->length
6956 || peek_abbrev_code (abfd, info_ptr) == 0)
6957 {
6958 dummy_p = true;
6959 return;
6960 }
6961
6962 /* If we don't have them yet, read the abbrevs for this compilation unit.
6963 And if we need to read them now, make sure they're freed when we're
6964 done. */
6965 if (abbrev_table != NULL)
6966 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6967 else
6968 {
6969 m_abbrev_table_holder
6970 = abbrev_table::read (objfile, abbrev_section,
6971 cu->header.abbrev_sect_off);
6972 abbrev_table = m_abbrev_table_holder.get ();
6973 }
6974
6975 /* Read the top level CU/TU die. */
6976 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6977 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6978
6979 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6980 {
6981 dummy_p = true;
6982 return;
6983 }
6984
6985 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6986 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6987 table from the DWO file and pass the ownership over to us. It will be
6988 referenced from READER, so we must make sure to free it after we're done
6989 with READER.
6990
6991 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6992 DWO CU, that this test will fail (the attribute will not be present). */
6993 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6994 if (dwo_name != nullptr)
6995 {
6996 struct dwo_unit *dwo_unit;
6997 struct die_info *dwo_comp_unit_die;
6998
6999 if (comp_unit_die->has_children)
7000 {
7001 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7002 " has children (offset %s) [in module %s]"),
7003 sect_offset_str (this_cu->sect_off),
7004 bfd_get_filename (abfd));
7005 }
7006 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7007 if (dwo_unit != NULL)
7008 {
7009 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7010 comp_unit_die, NULL,
7011 this, &info_ptr,
7012 &dwo_comp_unit_die,
7013 &m_dwo_abbrev_table) == 0)
7014 {
7015 /* Dummy die. */
7016 dummy_p = true;
7017 return;
7018 }
7019 comp_unit_die = dwo_comp_unit_die;
7020 }
7021 else
7022 {
7023 /* Yikes, we couldn't find the rest of the DIE, we only have
7024 the stub. A complaint has already been logged. There's
7025 not much more we can do except pass on the stub DIE to
7026 die_reader_func. We don't want to throw an error on bad
7027 debug info. */
7028 }
7029 }
7030 }
7031
7032 void
7033 cutu_reader::keep ()
7034 {
7035 /* Done, clean up. */
7036 gdb_assert (!dummy_p);
7037 if (m_new_cu != NULL)
7038 {
7039 struct dwarf2_per_objfile *dwarf2_per_objfile
7040 = m_this_cu->dwarf2_per_objfile;
7041 /* Link this CU into read_in_chain. */
7042 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7043 dwarf2_per_objfile->read_in_chain = m_this_cu;
7044 /* The chain owns it now. */
7045 m_new_cu.release ();
7046 }
7047 }
7048
7049 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7050 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7051 assumed to have already done the lookup to find the DWO file).
7052
7053 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7054 THIS_CU->is_debug_types, but nothing else.
7055
7056 We fill in THIS_CU->length.
7057
7058 THIS_CU->cu is always freed when done.
7059 This is done in order to not leave THIS_CU->cu in a state where we have
7060 to care whether it refers to the "main" CU or the DWO CU.
7061
7062 When parent_cu is passed, it is used to provide a default value for
7063 str_offsets_base and addr_base from the parent. */
7064
7065 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7066 struct dwarf2_cu *parent_cu,
7067 struct dwo_file *dwo_file)
7068 : die_reader_specs {},
7069 m_this_cu (this_cu)
7070 {
7071 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7072 struct objfile *objfile = dwarf2_per_objfile->objfile;
7073 struct dwarf2_section_info *section = this_cu->section;
7074 bfd *abfd = section->get_bfd_owner ();
7075 struct dwarf2_section_info *abbrev_section;
7076 const gdb_byte *begin_info_ptr, *info_ptr;
7077
7078 if (dwarf_die_debug)
7079 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7080 this_cu->is_debug_types ? "type" : "comp",
7081 sect_offset_str (this_cu->sect_off));
7082
7083 gdb_assert (this_cu->cu == NULL);
7084
7085 abbrev_section = (dwo_file != NULL
7086 ? &dwo_file->sections.abbrev
7087 : get_abbrev_section_for_cu (this_cu));
7088
7089 /* This is cheap if the section is already read in. */
7090 section->read (objfile);
7091
7092 m_new_cu.reset (new dwarf2_cu (this_cu));
7093
7094 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7095 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7096 &m_new_cu->header, section,
7097 abbrev_section, info_ptr,
7098 (this_cu->is_debug_types
7099 ? rcuh_kind::TYPE
7100 : rcuh_kind::COMPILE));
7101
7102 if (parent_cu != nullptr)
7103 {
7104 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7105 m_new_cu->addr_base = parent_cu->addr_base;
7106 }
7107 this_cu->length = m_new_cu->header.get_length ();
7108
7109 /* Skip dummy compilation units. */
7110 if (info_ptr >= begin_info_ptr + this_cu->length
7111 || peek_abbrev_code (abfd, info_ptr) == 0)
7112 {
7113 dummy_p = true;
7114 return;
7115 }
7116
7117 m_abbrev_table_holder
7118 = abbrev_table::read (objfile, abbrev_section,
7119 m_new_cu->header.abbrev_sect_off);
7120
7121 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7122 m_abbrev_table_holder.get ());
7123 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7124 }
7125
7126 \f
7127 /* Type Unit Groups.
7128
7129 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7130 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7131 so that all types coming from the same compilation (.o file) are grouped
7132 together. A future step could be to put the types in the same symtab as
7133 the CU the types ultimately came from. */
7134
7135 static hashval_t
7136 hash_type_unit_group (const void *item)
7137 {
7138 const struct type_unit_group *tu_group
7139 = (const struct type_unit_group *) item;
7140
7141 return hash_stmt_list_entry (&tu_group->hash);
7142 }
7143
7144 static int
7145 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7146 {
7147 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7148 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7149
7150 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7151 }
7152
7153 /* Allocate a hash table for type unit groups. */
7154
7155 static htab_up
7156 allocate_type_unit_groups_table ()
7157 {
7158 return htab_up (htab_create_alloc (3,
7159 hash_type_unit_group,
7160 eq_type_unit_group,
7161 NULL, xcalloc, xfree));
7162 }
7163
7164 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7165 partial symtabs. We combine several TUs per psymtab to not let the size
7166 of any one psymtab grow too big. */
7167 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7168 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7169
7170 /* Helper routine for get_type_unit_group.
7171 Create the type_unit_group object used to hold one or more TUs. */
7172
7173 static struct type_unit_group *
7174 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7175 {
7176 struct dwarf2_per_objfile *dwarf2_per_objfile
7177 = cu->per_cu->dwarf2_per_objfile;
7178 struct objfile *objfile = dwarf2_per_objfile->objfile;
7179 struct dwarf2_per_cu_data *per_cu;
7180 struct type_unit_group *tu_group;
7181
7182 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7183 struct type_unit_group);
7184 per_cu = &tu_group->per_cu;
7185 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7186
7187 if (dwarf2_per_objfile->using_index)
7188 {
7189 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7190 struct dwarf2_per_cu_quick_data);
7191 }
7192 else
7193 {
7194 unsigned int line_offset = to_underlying (line_offset_struct);
7195 dwarf2_psymtab *pst;
7196 std::string name;
7197
7198 /* Give the symtab a useful name for debug purposes. */
7199 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7200 name = string_printf ("<type_units_%d>",
7201 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7202 else
7203 name = string_printf ("<type_units_at_0x%x>", line_offset);
7204
7205 pst = create_partial_symtab (per_cu, name.c_str ());
7206 pst->anonymous = true;
7207 }
7208
7209 tu_group->hash.dwo_unit = cu->dwo_unit;
7210 tu_group->hash.line_sect_off = line_offset_struct;
7211
7212 return tu_group;
7213 }
7214
7215 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7216 STMT_LIST is a DW_AT_stmt_list attribute. */
7217
7218 static struct type_unit_group *
7219 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7220 {
7221 struct dwarf2_per_objfile *dwarf2_per_objfile
7222 = cu->per_cu->dwarf2_per_objfile;
7223 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7224 struct type_unit_group *tu_group;
7225 void **slot;
7226 unsigned int line_offset;
7227 struct type_unit_group type_unit_group_for_lookup;
7228
7229 if (dwarf2_per_objfile->type_unit_groups == NULL)
7230 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7231
7232 /* Do we need to create a new group, or can we use an existing one? */
7233
7234 if (stmt_list)
7235 {
7236 line_offset = DW_UNSND (stmt_list);
7237 ++tu_stats->nr_symtab_sharers;
7238 }
7239 else
7240 {
7241 /* Ugh, no stmt_list. Rare, but we have to handle it.
7242 We can do various things here like create one group per TU or
7243 spread them over multiple groups to split up the expansion work.
7244 To avoid worst case scenarios (too many groups or too large groups)
7245 we, umm, group them in bunches. */
7246 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7247 | (tu_stats->nr_stmt_less_type_units
7248 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7249 ++tu_stats->nr_stmt_less_type_units;
7250 }
7251
7252 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7253 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7254 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7255 &type_unit_group_for_lookup, INSERT);
7256 if (*slot != NULL)
7257 {
7258 tu_group = (struct type_unit_group *) *slot;
7259 gdb_assert (tu_group != NULL);
7260 }
7261 else
7262 {
7263 sect_offset line_offset_struct = (sect_offset) line_offset;
7264 tu_group = create_type_unit_group (cu, line_offset_struct);
7265 *slot = tu_group;
7266 ++tu_stats->nr_symtabs;
7267 }
7268
7269 return tu_group;
7270 }
7271 \f
7272 /* Partial symbol tables. */
7273
7274 /* Create a psymtab named NAME and assign it to PER_CU.
7275
7276 The caller must fill in the following details:
7277 dirname, textlow, texthigh. */
7278
7279 static dwarf2_psymtab *
7280 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7281 {
7282 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7283 dwarf2_psymtab *pst;
7284
7285 pst = new dwarf2_psymtab (name, objfile, per_cu);
7286
7287 pst->psymtabs_addrmap_supported = true;
7288
7289 /* This is the glue that links PST into GDB's symbol API. */
7290 per_cu->v.psymtab = pst;
7291
7292 return pst;
7293 }
7294
7295 /* DIE reader function for process_psymtab_comp_unit. */
7296
7297 static void
7298 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7299 const gdb_byte *info_ptr,
7300 struct die_info *comp_unit_die,
7301 enum language pretend_language)
7302 {
7303 struct dwarf2_cu *cu = reader->cu;
7304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7305 struct gdbarch *gdbarch = objfile->arch ();
7306 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7307 CORE_ADDR baseaddr;
7308 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7309 dwarf2_psymtab *pst;
7310 enum pc_bounds_kind cu_bounds_kind;
7311 const char *filename;
7312
7313 gdb_assert (! per_cu->is_debug_types);
7314
7315 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7316
7317 /* Allocate a new partial symbol table structure. */
7318 gdb::unique_xmalloc_ptr<char> debug_filename;
7319 static const char artificial[] = "<artificial>";
7320 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7321 if (filename == NULL)
7322 filename = "";
7323 else if (strcmp (filename, artificial) == 0)
7324 {
7325 debug_filename.reset (concat (artificial, "@",
7326 sect_offset_str (per_cu->sect_off),
7327 (char *) NULL));
7328 filename = debug_filename.get ();
7329 }
7330
7331 pst = create_partial_symtab (per_cu, filename);
7332
7333 /* This must be done before calling dwarf2_build_include_psymtabs. */
7334 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7335
7336 baseaddr = objfile->text_section_offset ();
7337
7338 dwarf2_find_base_address (comp_unit_die, cu);
7339
7340 /* Possibly set the default values of LOWPC and HIGHPC from
7341 `DW_AT_ranges'. */
7342 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7343 &best_highpc, cu, pst);
7344 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7345 {
7346 CORE_ADDR low
7347 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7348 - baseaddr);
7349 CORE_ADDR high
7350 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7351 - baseaddr - 1);
7352 /* Store the contiguous range if it is not empty; it can be
7353 empty for CUs with no code. */
7354 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7355 low, high, pst);
7356 }
7357
7358 /* Check if comp unit has_children.
7359 If so, read the rest of the partial symbols from this comp unit.
7360 If not, there's no more debug_info for this comp unit. */
7361 if (comp_unit_die->has_children)
7362 {
7363 struct partial_die_info *first_die;
7364 CORE_ADDR lowpc, highpc;
7365
7366 lowpc = ((CORE_ADDR) -1);
7367 highpc = ((CORE_ADDR) 0);
7368
7369 first_die = load_partial_dies (reader, info_ptr, 1);
7370
7371 scan_partial_symbols (first_die, &lowpc, &highpc,
7372 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7373
7374 /* If we didn't find a lowpc, set it to highpc to avoid
7375 complaints from `maint check'. */
7376 if (lowpc == ((CORE_ADDR) -1))
7377 lowpc = highpc;
7378
7379 /* If the compilation unit didn't have an explicit address range,
7380 then use the information extracted from its child dies. */
7381 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7382 {
7383 best_lowpc = lowpc;
7384 best_highpc = highpc;
7385 }
7386 }
7387 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7388 best_lowpc + baseaddr)
7389 - baseaddr);
7390 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7391 best_highpc + baseaddr)
7392 - baseaddr);
7393
7394 end_psymtab_common (objfile, pst);
7395
7396 if (!cu->per_cu->imported_symtabs_empty ())
7397 {
7398 int i;
7399 int len = cu->per_cu->imported_symtabs_size ();
7400
7401 /* Fill in 'dependencies' here; we fill in 'users' in a
7402 post-pass. */
7403 pst->number_of_dependencies = len;
7404 pst->dependencies
7405 = objfile->partial_symtabs->allocate_dependencies (len);
7406 for (i = 0; i < len; ++i)
7407 {
7408 pst->dependencies[i]
7409 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7410 }
7411
7412 cu->per_cu->imported_symtabs_free ();
7413 }
7414
7415 /* Get the list of files included in the current compilation unit,
7416 and build a psymtab for each of them. */
7417 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7418
7419 if (dwarf_read_debug)
7420 fprintf_unfiltered (gdb_stdlog,
7421 "Psymtab for %s unit @%s: %s - %s"
7422 ", %d global, %d static syms\n",
7423 per_cu->is_debug_types ? "type" : "comp",
7424 sect_offset_str (per_cu->sect_off),
7425 paddress (gdbarch, pst->text_low (objfile)),
7426 paddress (gdbarch, pst->text_high (objfile)),
7427 pst->n_global_syms, pst->n_static_syms);
7428 }
7429
7430 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7431 Process compilation unit THIS_CU for a psymtab. */
7432
7433 static void
7434 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7435 bool want_partial_unit,
7436 enum language pretend_language)
7437 {
7438 /* If this compilation unit was already read in, free the
7439 cached copy in order to read it in again. This is
7440 necessary because we skipped some symbols when we first
7441 read in the compilation unit (see load_partial_dies).
7442 This problem could be avoided, but the benefit is unclear. */
7443 if (this_cu->cu != NULL)
7444 free_one_cached_comp_unit (this_cu);
7445
7446 cutu_reader reader (this_cu, NULL, 0, false);
7447
7448 switch (reader.comp_unit_die->tag)
7449 {
7450 case DW_TAG_compile_unit:
7451 this_cu->unit_type = DW_UT_compile;
7452 break;
7453 case DW_TAG_partial_unit:
7454 this_cu->unit_type = DW_UT_partial;
7455 break;
7456 default:
7457 abort ();
7458 }
7459
7460 if (reader.dummy_p)
7461 {
7462 /* Nothing. */
7463 }
7464 else if (this_cu->is_debug_types)
7465 build_type_psymtabs_reader (&reader, reader.info_ptr,
7466 reader.comp_unit_die);
7467 else if (want_partial_unit
7468 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7469 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7470 reader.comp_unit_die,
7471 pretend_language);
7472
7473 this_cu->lang = this_cu->cu->language;
7474
7475 /* Age out any secondary CUs. */
7476 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7477 }
7478
7479 /* Reader function for build_type_psymtabs. */
7480
7481 static void
7482 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7483 const gdb_byte *info_ptr,
7484 struct die_info *type_unit_die)
7485 {
7486 struct dwarf2_per_objfile *dwarf2_per_objfile
7487 = reader->cu->per_cu->dwarf2_per_objfile;
7488 struct objfile *objfile = dwarf2_per_objfile->objfile;
7489 struct dwarf2_cu *cu = reader->cu;
7490 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7491 struct signatured_type *sig_type;
7492 struct type_unit_group *tu_group;
7493 struct attribute *attr;
7494 struct partial_die_info *first_die;
7495 CORE_ADDR lowpc, highpc;
7496 dwarf2_psymtab *pst;
7497
7498 gdb_assert (per_cu->is_debug_types);
7499 sig_type = (struct signatured_type *) per_cu;
7500
7501 if (! type_unit_die->has_children)
7502 return;
7503
7504 attr = type_unit_die->attr (DW_AT_stmt_list);
7505 tu_group = get_type_unit_group (cu, attr);
7506
7507 if (tu_group->tus == nullptr)
7508 tu_group->tus = new std::vector<signatured_type *>;
7509 tu_group->tus->push_back (sig_type);
7510
7511 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7512 pst = create_partial_symtab (per_cu, "");
7513 pst->anonymous = true;
7514
7515 first_die = load_partial_dies (reader, info_ptr, 1);
7516
7517 lowpc = (CORE_ADDR) -1;
7518 highpc = (CORE_ADDR) 0;
7519 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7520
7521 end_psymtab_common (objfile, pst);
7522 }
7523
7524 /* Struct used to sort TUs by their abbreviation table offset. */
7525
7526 struct tu_abbrev_offset
7527 {
7528 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7529 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7530 {}
7531
7532 signatured_type *sig_type;
7533 sect_offset abbrev_offset;
7534 };
7535
7536 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7537
7538 static bool
7539 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7540 const struct tu_abbrev_offset &b)
7541 {
7542 return a.abbrev_offset < b.abbrev_offset;
7543 }
7544
7545 /* Efficiently read all the type units.
7546 This does the bulk of the work for build_type_psymtabs.
7547
7548 The efficiency is because we sort TUs by the abbrev table they use and
7549 only read each abbrev table once. In one program there are 200K TUs
7550 sharing 8K abbrev tables.
7551
7552 The main purpose of this function is to support building the
7553 dwarf2_per_objfile->type_unit_groups table.
7554 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7555 can collapse the search space by grouping them by stmt_list.
7556 The savings can be significant, in the same program from above the 200K TUs
7557 share 8K stmt_list tables.
7558
7559 FUNC is expected to call get_type_unit_group, which will create the
7560 struct type_unit_group if necessary and add it to
7561 dwarf2_per_objfile->type_unit_groups. */
7562
7563 static void
7564 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7565 {
7566 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7567 abbrev_table_up abbrev_table;
7568 sect_offset abbrev_offset;
7569
7570 /* It's up to the caller to not call us multiple times. */
7571 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7572
7573 if (dwarf2_per_objfile->all_type_units.empty ())
7574 return;
7575
7576 /* TUs typically share abbrev tables, and there can be way more TUs than
7577 abbrev tables. Sort by abbrev table to reduce the number of times we
7578 read each abbrev table in.
7579 Alternatives are to punt or to maintain a cache of abbrev tables.
7580 This is simpler and efficient enough for now.
7581
7582 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7583 symtab to use). Typically TUs with the same abbrev offset have the same
7584 stmt_list value too so in practice this should work well.
7585
7586 The basic algorithm here is:
7587
7588 sort TUs by abbrev table
7589 for each TU with same abbrev table:
7590 read abbrev table if first user
7591 read TU top level DIE
7592 [IWBN if DWO skeletons had DW_AT_stmt_list]
7593 call FUNC */
7594
7595 if (dwarf_read_debug)
7596 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7597
7598 /* Sort in a separate table to maintain the order of all_type_units
7599 for .gdb_index: TU indices directly index all_type_units. */
7600 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7601 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7602
7603 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7604 sorted_by_abbrev.emplace_back
7605 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7606 sig_type->per_cu.section,
7607 sig_type->per_cu.sect_off));
7608
7609 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7610 sort_tu_by_abbrev_offset);
7611
7612 abbrev_offset = (sect_offset) ~(unsigned) 0;
7613
7614 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7615 {
7616 /* Switch to the next abbrev table if necessary. */
7617 if (abbrev_table == NULL
7618 || tu.abbrev_offset != abbrev_offset)
7619 {
7620 abbrev_offset = tu.abbrev_offset;
7621 abbrev_table =
7622 abbrev_table::read (dwarf2_per_objfile->objfile,
7623 &dwarf2_per_objfile->abbrev,
7624 abbrev_offset);
7625 ++tu_stats->nr_uniq_abbrev_tables;
7626 }
7627
7628 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7629 0, false);
7630 if (!reader.dummy_p)
7631 build_type_psymtabs_reader (&reader, reader.info_ptr,
7632 reader.comp_unit_die);
7633 }
7634 }
7635
7636 /* Print collected type unit statistics. */
7637
7638 static void
7639 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7640 {
7641 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7642
7643 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7644 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7645 dwarf2_per_objfile->all_type_units.size ());
7646 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7647 tu_stats->nr_uniq_abbrev_tables);
7648 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7649 tu_stats->nr_symtabs);
7650 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7651 tu_stats->nr_symtab_sharers);
7652 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7653 tu_stats->nr_stmt_less_type_units);
7654 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7655 tu_stats->nr_all_type_units_reallocs);
7656 }
7657
7658 /* Traversal function for build_type_psymtabs. */
7659
7660 static int
7661 build_type_psymtab_dependencies (void **slot, void *info)
7662 {
7663 struct dwarf2_per_objfile *dwarf2_per_objfile
7664 = (struct dwarf2_per_objfile *) info;
7665 struct objfile *objfile = dwarf2_per_objfile->objfile;
7666 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7667 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7668 dwarf2_psymtab *pst = per_cu->v.psymtab;
7669 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7670 int i;
7671
7672 gdb_assert (len > 0);
7673 gdb_assert (per_cu->type_unit_group_p ());
7674
7675 pst->number_of_dependencies = len;
7676 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7677 for (i = 0; i < len; ++i)
7678 {
7679 struct signatured_type *iter = tu_group->tus->at (i);
7680 gdb_assert (iter->per_cu.is_debug_types);
7681 pst->dependencies[i] = iter->per_cu.v.psymtab;
7682 iter->type_unit_group = tu_group;
7683 }
7684
7685 delete tu_group->tus;
7686 tu_group->tus = nullptr;
7687
7688 return 1;
7689 }
7690
7691 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7692 Build partial symbol tables for the .debug_types comp-units. */
7693
7694 static void
7695 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7696 {
7697 if (! create_all_type_units (dwarf2_per_objfile))
7698 return;
7699
7700 build_type_psymtabs_1 (dwarf2_per_objfile);
7701 }
7702
7703 /* Traversal function for process_skeletonless_type_unit.
7704 Read a TU in a DWO file and build partial symbols for it. */
7705
7706 static int
7707 process_skeletonless_type_unit (void **slot, void *info)
7708 {
7709 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7710 struct dwarf2_per_objfile *dwarf2_per_objfile
7711 = (struct dwarf2_per_objfile *) info;
7712 struct signatured_type find_entry, *entry;
7713
7714 /* If this TU doesn't exist in the global table, add it and read it in. */
7715
7716 if (dwarf2_per_objfile->signatured_types == NULL)
7717 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7718
7719 find_entry.signature = dwo_unit->signature;
7720 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7721 &find_entry, INSERT);
7722 /* If we've already seen this type there's nothing to do. What's happening
7723 is we're doing our own version of comdat-folding here. */
7724 if (*slot != NULL)
7725 return 1;
7726
7727 /* This does the job that create_all_type_units would have done for
7728 this TU. */
7729 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7730 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7731 *slot = entry;
7732
7733 /* This does the job that build_type_psymtabs_1 would have done. */
7734 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7735 if (!reader.dummy_p)
7736 build_type_psymtabs_reader (&reader, reader.info_ptr,
7737 reader.comp_unit_die);
7738
7739 return 1;
7740 }
7741
7742 /* Traversal function for process_skeletonless_type_units. */
7743
7744 static int
7745 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7746 {
7747 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7748
7749 if (dwo_file->tus != NULL)
7750 htab_traverse_noresize (dwo_file->tus.get (),
7751 process_skeletonless_type_unit, info);
7752
7753 return 1;
7754 }
7755
7756 /* Scan all TUs of DWO files, verifying we've processed them.
7757 This is needed in case a TU was emitted without its skeleton.
7758 Note: This can't be done until we know what all the DWO files are. */
7759
7760 static void
7761 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7762 {
7763 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7764 if (get_dwp_file (dwarf2_per_objfile) == NULL
7765 && dwarf2_per_objfile->dwo_files != NULL)
7766 {
7767 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7768 process_dwo_file_for_skeletonless_type_units,
7769 dwarf2_per_objfile);
7770 }
7771 }
7772
7773 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7774
7775 static void
7776 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7777 {
7778 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7779 {
7780 dwarf2_psymtab *pst = per_cu->v.psymtab;
7781
7782 if (pst == NULL)
7783 continue;
7784
7785 for (int j = 0; j < pst->number_of_dependencies; ++j)
7786 {
7787 /* Set the 'user' field only if it is not already set. */
7788 if (pst->dependencies[j]->user == NULL)
7789 pst->dependencies[j]->user = pst;
7790 }
7791 }
7792 }
7793
7794 /* Build the partial symbol table by doing a quick pass through the
7795 .debug_info and .debug_abbrev sections. */
7796
7797 static void
7798 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7799 {
7800 struct objfile *objfile = dwarf2_per_objfile->objfile;
7801
7802 if (dwarf_read_debug)
7803 {
7804 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7805 objfile_name (objfile));
7806 }
7807
7808 scoped_restore restore_reading_psyms
7809 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7810 true);
7811
7812 dwarf2_per_objfile->info.read (objfile);
7813
7814 /* Any cached compilation units will be linked by the per-objfile
7815 read_in_chain. Make sure to free them when we're done. */
7816 free_cached_comp_units freer (dwarf2_per_objfile);
7817
7818 build_type_psymtabs (dwarf2_per_objfile);
7819
7820 create_all_comp_units (dwarf2_per_objfile);
7821
7822 /* Create a temporary address map on a temporary obstack. We later
7823 copy this to the final obstack. */
7824 auto_obstack temp_obstack;
7825
7826 scoped_restore save_psymtabs_addrmap
7827 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7828 addrmap_create_mutable (&temp_obstack));
7829
7830 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7831 {
7832 if (per_cu->v.psymtab != NULL)
7833 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7834 continue;
7835 process_psymtab_comp_unit (per_cu, false, language_minimal);
7836 }
7837
7838 /* This has to wait until we read the CUs, we need the list of DWOs. */
7839 process_skeletonless_type_units (dwarf2_per_objfile);
7840
7841 /* Now that all TUs have been processed we can fill in the dependencies. */
7842 if (dwarf2_per_objfile->type_unit_groups != NULL)
7843 {
7844 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7845 build_type_psymtab_dependencies, dwarf2_per_objfile);
7846 }
7847
7848 if (dwarf_read_debug)
7849 print_tu_stats (dwarf2_per_objfile);
7850
7851 set_partial_user (dwarf2_per_objfile);
7852
7853 objfile->partial_symtabs->psymtabs_addrmap
7854 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7855 objfile->partial_symtabs->obstack ());
7856 /* At this point we want to keep the address map. */
7857 save_psymtabs_addrmap.release ();
7858
7859 if (dwarf_read_debug)
7860 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7861 objfile_name (objfile));
7862 }
7863
7864 /* Load the partial DIEs for a secondary CU into memory.
7865 This is also used when rereading a primary CU with load_all_dies. */
7866
7867 static void
7868 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7869 {
7870 cutu_reader reader (this_cu, NULL, 1, false);
7871
7872 if (!reader.dummy_p)
7873 {
7874 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7875 language_minimal);
7876
7877 /* Check if comp unit has_children.
7878 If so, read the rest of the partial symbols from this comp unit.
7879 If not, there's no more debug_info for this comp unit. */
7880 if (reader.comp_unit_die->has_children)
7881 load_partial_dies (&reader, reader.info_ptr, 0);
7882
7883 reader.keep ();
7884 }
7885 }
7886
7887 static void
7888 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7889 struct dwarf2_section_info *section,
7890 struct dwarf2_section_info *abbrev_section,
7891 unsigned int is_dwz)
7892 {
7893 const gdb_byte *info_ptr;
7894 struct objfile *objfile = dwarf2_per_objfile->objfile;
7895
7896 if (dwarf_read_debug)
7897 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7898 section->get_name (),
7899 section->get_file_name ());
7900
7901 section->read (objfile);
7902
7903 info_ptr = section->buffer;
7904
7905 while (info_ptr < section->buffer + section->size)
7906 {
7907 struct dwarf2_per_cu_data *this_cu;
7908
7909 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7910
7911 comp_unit_head cu_header;
7912 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7913 abbrev_section, info_ptr,
7914 rcuh_kind::COMPILE);
7915
7916 /* Save the compilation unit for later lookup. */
7917 if (cu_header.unit_type != DW_UT_type)
7918 {
7919 this_cu = XOBNEW (&objfile->objfile_obstack,
7920 struct dwarf2_per_cu_data);
7921 memset (this_cu, 0, sizeof (*this_cu));
7922 }
7923 else
7924 {
7925 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7926 struct signatured_type);
7927 memset (sig_type, 0, sizeof (*sig_type));
7928 sig_type->signature = cu_header.signature;
7929 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7930 this_cu = &sig_type->per_cu;
7931 }
7932 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7933 this_cu->sect_off = sect_off;
7934 this_cu->length = cu_header.length + cu_header.initial_length_size;
7935 this_cu->is_dwz = is_dwz;
7936 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7937 this_cu->section = section;
7938
7939 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7940
7941 info_ptr = info_ptr + this_cu->length;
7942 }
7943 }
7944
7945 /* Create a list of all compilation units in OBJFILE.
7946 This is only done for -readnow and building partial symtabs. */
7947
7948 static void
7949 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7950 {
7951 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7952 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7953 &dwarf2_per_objfile->abbrev, 0);
7954
7955 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7956 if (dwz != NULL)
7957 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7958 1);
7959 }
7960
7961 /* Process all loaded DIEs for compilation unit CU, starting at
7962 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7963 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7964 DW_AT_ranges). See the comments of add_partial_subprogram on how
7965 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7966
7967 static void
7968 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7969 CORE_ADDR *highpc, int set_addrmap,
7970 struct dwarf2_cu *cu)
7971 {
7972 struct partial_die_info *pdi;
7973
7974 /* Now, march along the PDI's, descending into ones which have
7975 interesting children but skipping the children of the other ones,
7976 until we reach the end of the compilation unit. */
7977
7978 pdi = first_die;
7979
7980 while (pdi != NULL)
7981 {
7982 pdi->fixup (cu);
7983
7984 /* Anonymous namespaces or modules have no name but have interesting
7985 children, so we need to look at them. Ditto for anonymous
7986 enums. */
7987
7988 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7989 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7990 || pdi->tag == DW_TAG_imported_unit
7991 || pdi->tag == DW_TAG_inlined_subroutine)
7992 {
7993 switch (pdi->tag)
7994 {
7995 case DW_TAG_subprogram:
7996 case DW_TAG_inlined_subroutine:
7997 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7998 break;
7999 case DW_TAG_constant:
8000 case DW_TAG_variable:
8001 case DW_TAG_typedef:
8002 case DW_TAG_union_type:
8003 if (!pdi->is_declaration
8004 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8005 {
8006 add_partial_symbol (pdi, cu);
8007 }
8008 break;
8009 case DW_TAG_class_type:
8010 case DW_TAG_interface_type:
8011 case DW_TAG_structure_type:
8012 if (!pdi->is_declaration)
8013 {
8014 add_partial_symbol (pdi, cu);
8015 }
8016 if ((cu->language == language_rust
8017 || cu->language == language_cplus) && pdi->has_children)
8018 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8019 set_addrmap, cu);
8020 break;
8021 case DW_TAG_enumeration_type:
8022 if (!pdi->is_declaration)
8023 add_partial_enumeration (pdi, cu);
8024 break;
8025 case DW_TAG_base_type:
8026 case DW_TAG_subrange_type:
8027 /* File scope base type definitions are added to the partial
8028 symbol table. */
8029 add_partial_symbol (pdi, cu);
8030 break;
8031 case DW_TAG_namespace:
8032 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8033 break;
8034 case DW_TAG_module:
8035 if (!pdi->is_declaration)
8036 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8037 break;
8038 case DW_TAG_imported_unit:
8039 {
8040 struct dwarf2_per_cu_data *per_cu;
8041
8042 /* For now we don't handle imported units in type units. */
8043 if (cu->per_cu->is_debug_types)
8044 {
8045 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8046 " supported in type units [in module %s]"),
8047 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8048 }
8049
8050 per_cu = dwarf2_find_containing_comp_unit
8051 (pdi->d.sect_off, pdi->is_dwz,
8052 cu->per_cu->dwarf2_per_objfile);
8053
8054 /* Go read the partial unit, if needed. */
8055 if (per_cu->v.psymtab == NULL)
8056 process_psymtab_comp_unit (per_cu, true, cu->language);
8057
8058 cu->per_cu->imported_symtabs_push (per_cu);
8059 }
8060 break;
8061 case DW_TAG_imported_declaration:
8062 add_partial_symbol (pdi, cu);
8063 break;
8064 default:
8065 break;
8066 }
8067 }
8068
8069 /* If the die has a sibling, skip to the sibling. */
8070
8071 pdi = pdi->die_sibling;
8072 }
8073 }
8074
8075 /* Functions used to compute the fully scoped name of a partial DIE.
8076
8077 Normally, this is simple. For C++, the parent DIE's fully scoped
8078 name is concatenated with "::" and the partial DIE's name.
8079 Enumerators are an exception; they use the scope of their parent
8080 enumeration type, i.e. the name of the enumeration type is not
8081 prepended to the enumerator.
8082
8083 There are two complexities. One is DW_AT_specification; in this
8084 case "parent" means the parent of the target of the specification,
8085 instead of the direct parent of the DIE. The other is compilers
8086 which do not emit DW_TAG_namespace; in this case we try to guess
8087 the fully qualified name of structure types from their members'
8088 linkage names. This must be done using the DIE's children rather
8089 than the children of any DW_AT_specification target. We only need
8090 to do this for structures at the top level, i.e. if the target of
8091 any DW_AT_specification (if any; otherwise the DIE itself) does not
8092 have a parent. */
8093
8094 /* Compute the scope prefix associated with PDI's parent, in
8095 compilation unit CU. The result will be allocated on CU's
8096 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8097 field. NULL is returned if no prefix is necessary. */
8098 static const char *
8099 partial_die_parent_scope (struct partial_die_info *pdi,
8100 struct dwarf2_cu *cu)
8101 {
8102 const char *grandparent_scope;
8103 struct partial_die_info *parent, *real_pdi;
8104
8105 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8106 then this means the parent of the specification DIE. */
8107
8108 real_pdi = pdi;
8109 while (real_pdi->has_specification)
8110 {
8111 auto res = find_partial_die (real_pdi->spec_offset,
8112 real_pdi->spec_is_dwz, cu);
8113 real_pdi = res.pdi;
8114 cu = res.cu;
8115 }
8116
8117 parent = real_pdi->die_parent;
8118 if (parent == NULL)
8119 return NULL;
8120
8121 if (parent->scope_set)
8122 return parent->scope;
8123
8124 parent->fixup (cu);
8125
8126 grandparent_scope = partial_die_parent_scope (parent, cu);
8127
8128 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8129 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8130 Work around this problem here. */
8131 if (cu->language == language_cplus
8132 && parent->tag == DW_TAG_namespace
8133 && strcmp (parent->name, "::") == 0
8134 && grandparent_scope == NULL)
8135 {
8136 parent->scope = NULL;
8137 parent->scope_set = 1;
8138 return NULL;
8139 }
8140
8141 /* Nested subroutines in Fortran get a prefix. */
8142 if (pdi->tag == DW_TAG_enumerator)
8143 /* Enumerators should not get the name of the enumeration as a prefix. */
8144 parent->scope = grandparent_scope;
8145 else if (parent->tag == DW_TAG_namespace
8146 || parent->tag == DW_TAG_module
8147 || parent->tag == DW_TAG_structure_type
8148 || parent->tag == DW_TAG_class_type
8149 || parent->tag == DW_TAG_interface_type
8150 || parent->tag == DW_TAG_union_type
8151 || parent->tag == DW_TAG_enumeration_type
8152 || (cu->language == language_fortran
8153 && parent->tag == DW_TAG_subprogram
8154 && pdi->tag == DW_TAG_subprogram))
8155 {
8156 if (grandparent_scope == NULL)
8157 parent->scope = parent->name;
8158 else
8159 parent->scope = typename_concat (&cu->comp_unit_obstack,
8160 grandparent_scope,
8161 parent->name, 0, cu);
8162 }
8163 else
8164 {
8165 /* FIXME drow/2004-04-01: What should we be doing with
8166 function-local names? For partial symbols, we should probably be
8167 ignoring them. */
8168 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8169 dwarf_tag_name (parent->tag),
8170 sect_offset_str (pdi->sect_off));
8171 parent->scope = grandparent_scope;
8172 }
8173
8174 parent->scope_set = 1;
8175 return parent->scope;
8176 }
8177
8178 /* Return the fully scoped name associated with PDI, from compilation unit
8179 CU. The result will be allocated with malloc. */
8180
8181 static gdb::unique_xmalloc_ptr<char>
8182 partial_die_full_name (struct partial_die_info *pdi,
8183 struct dwarf2_cu *cu)
8184 {
8185 const char *parent_scope;
8186
8187 /* If this is a template instantiation, we can not work out the
8188 template arguments from partial DIEs. So, unfortunately, we have
8189 to go through the full DIEs. At least any work we do building
8190 types here will be reused if full symbols are loaded later. */
8191 if (pdi->has_template_arguments)
8192 {
8193 pdi->fixup (cu);
8194
8195 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8196 {
8197 struct die_info *die;
8198 struct attribute attr;
8199 struct dwarf2_cu *ref_cu = cu;
8200
8201 /* DW_FORM_ref_addr is using section offset. */
8202 attr.name = (enum dwarf_attribute) 0;
8203 attr.form = DW_FORM_ref_addr;
8204 attr.u.unsnd = to_underlying (pdi->sect_off);
8205 die = follow_die_ref (NULL, &attr, &ref_cu);
8206
8207 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8208 }
8209 }
8210
8211 parent_scope = partial_die_parent_scope (pdi, cu);
8212 if (parent_scope == NULL)
8213 return NULL;
8214 else
8215 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8216 pdi->name, 0, cu));
8217 }
8218
8219 static void
8220 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8221 {
8222 struct dwarf2_per_objfile *dwarf2_per_objfile
8223 = cu->per_cu->dwarf2_per_objfile;
8224 struct objfile *objfile = dwarf2_per_objfile->objfile;
8225 struct gdbarch *gdbarch = objfile->arch ();
8226 CORE_ADDR addr = 0;
8227 const char *actual_name = NULL;
8228 CORE_ADDR baseaddr;
8229
8230 baseaddr = objfile->text_section_offset ();
8231
8232 gdb::unique_xmalloc_ptr<char> built_actual_name
8233 = partial_die_full_name (pdi, cu);
8234 if (built_actual_name != NULL)
8235 actual_name = built_actual_name.get ();
8236
8237 if (actual_name == NULL)
8238 actual_name = pdi->name;
8239
8240 partial_symbol psymbol;
8241 memset (&psymbol, 0, sizeof (psymbol));
8242 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8243 psymbol.ginfo.section = -1;
8244
8245 /* The code below indicates that the psymbol should be installed by
8246 setting this. */
8247 gdb::optional<psymbol_placement> where;
8248
8249 switch (pdi->tag)
8250 {
8251 case DW_TAG_inlined_subroutine:
8252 case DW_TAG_subprogram:
8253 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8254 - baseaddr);
8255 if (pdi->is_external
8256 || cu->language == language_ada
8257 || (cu->language == language_fortran
8258 && pdi->die_parent != NULL
8259 && pdi->die_parent->tag == DW_TAG_subprogram))
8260 {
8261 /* Normally, only "external" DIEs are part of the global scope.
8262 But in Ada and Fortran, we want to be able to access nested
8263 procedures globally. So all Ada and Fortran subprograms are
8264 stored in the global scope. */
8265 where = psymbol_placement::GLOBAL;
8266 }
8267 else
8268 where = psymbol_placement::STATIC;
8269
8270 psymbol.domain = VAR_DOMAIN;
8271 psymbol.aclass = LOC_BLOCK;
8272 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8273 psymbol.ginfo.value.address = addr;
8274
8275 if (pdi->main_subprogram && actual_name != NULL)
8276 set_objfile_main_name (objfile, actual_name, cu->language);
8277 break;
8278 case DW_TAG_constant:
8279 psymbol.domain = VAR_DOMAIN;
8280 psymbol.aclass = LOC_STATIC;
8281 where = (pdi->is_external
8282 ? psymbol_placement::GLOBAL
8283 : psymbol_placement::STATIC);
8284 break;
8285 case DW_TAG_variable:
8286 if (pdi->d.locdesc)
8287 addr = decode_locdesc (pdi->d.locdesc, cu);
8288
8289 if (pdi->d.locdesc
8290 && addr == 0
8291 && !dwarf2_per_objfile->has_section_at_zero)
8292 {
8293 /* A global or static variable may also have been stripped
8294 out by the linker if unused, in which case its address
8295 will be nullified; do not add such variables into partial
8296 symbol table then. */
8297 }
8298 else if (pdi->is_external)
8299 {
8300 /* Global Variable.
8301 Don't enter into the minimal symbol tables as there is
8302 a minimal symbol table entry from the ELF symbols already.
8303 Enter into partial symbol table if it has a location
8304 descriptor or a type.
8305 If the location descriptor is missing, new_symbol will create
8306 a LOC_UNRESOLVED symbol, the address of the variable will then
8307 be determined from the minimal symbol table whenever the variable
8308 is referenced.
8309 The address for the partial symbol table entry is not
8310 used by GDB, but it comes in handy for debugging partial symbol
8311 table building. */
8312
8313 if (pdi->d.locdesc || pdi->has_type)
8314 {
8315 psymbol.domain = VAR_DOMAIN;
8316 psymbol.aclass = LOC_STATIC;
8317 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8318 psymbol.ginfo.value.address = addr;
8319 where = psymbol_placement::GLOBAL;
8320 }
8321 }
8322 else
8323 {
8324 int has_loc = pdi->d.locdesc != NULL;
8325
8326 /* Static Variable. Skip symbols whose value we cannot know (those
8327 without location descriptors or constant values). */
8328 if (!has_loc && !pdi->has_const_value)
8329 return;
8330
8331 psymbol.domain = VAR_DOMAIN;
8332 psymbol.aclass = LOC_STATIC;
8333 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8334 if (has_loc)
8335 psymbol.ginfo.value.address = addr;
8336 where = psymbol_placement::STATIC;
8337 }
8338 break;
8339 case DW_TAG_typedef:
8340 case DW_TAG_base_type:
8341 case DW_TAG_subrange_type:
8342 psymbol.domain = VAR_DOMAIN;
8343 psymbol.aclass = LOC_TYPEDEF;
8344 where = psymbol_placement::STATIC;
8345 break;
8346 case DW_TAG_imported_declaration:
8347 case DW_TAG_namespace:
8348 psymbol.domain = VAR_DOMAIN;
8349 psymbol.aclass = LOC_TYPEDEF;
8350 where = psymbol_placement::GLOBAL;
8351 break;
8352 case DW_TAG_module:
8353 /* With Fortran 77 there might be a "BLOCK DATA" module
8354 available without any name. If so, we skip the module as it
8355 doesn't bring any value. */
8356 if (actual_name != nullptr)
8357 {
8358 psymbol.domain = MODULE_DOMAIN;
8359 psymbol.aclass = LOC_TYPEDEF;
8360 where = psymbol_placement::GLOBAL;
8361 }
8362 break;
8363 case DW_TAG_class_type:
8364 case DW_TAG_interface_type:
8365 case DW_TAG_structure_type:
8366 case DW_TAG_union_type:
8367 case DW_TAG_enumeration_type:
8368 /* Skip external references. The DWARF standard says in the section
8369 about "Structure, Union, and Class Type Entries": "An incomplete
8370 structure, union or class type is represented by a structure,
8371 union or class entry that does not have a byte size attribute
8372 and that has a DW_AT_declaration attribute." */
8373 if (!pdi->has_byte_size && pdi->is_declaration)
8374 return;
8375
8376 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8377 static vs. global. */
8378 psymbol.domain = STRUCT_DOMAIN;
8379 psymbol.aclass = LOC_TYPEDEF;
8380 where = (cu->language == language_cplus
8381 ? psymbol_placement::GLOBAL
8382 : psymbol_placement::STATIC);
8383 break;
8384 case DW_TAG_enumerator:
8385 psymbol.domain = VAR_DOMAIN;
8386 psymbol.aclass = LOC_CONST;
8387 where = (cu->language == language_cplus
8388 ? psymbol_placement::GLOBAL
8389 : psymbol_placement::STATIC);
8390 break;
8391 default:
8392 break;
8393 }
8394
8395 if (where.has_value ())
8396 {
8397 if (built_actual_name != nullptr)
8398 actual_name = objfile->intern (actual_name);
8399 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8400 psymbol.ginfo.set_linkage_name (actual_name);
8401 else
8402 {
8403 psymbol.ginfo.set_demangled_name (actual_name,
8404 &objfile->objfile_obstack);
8405 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8406 }
8407 add_psymbol_to_list (psymbol, *where, objfile);
8408 }
8409 }
8410
8411 /* Read a partial die corresponding to a namespace; also, add a symbol
8412 corresponding to that namespace to the symbol table. NAMESPACE is
8413 the name of the enclosing namespace. */
8414
8415 static void
8416 add_partial_namespace (struct partial_die_info *pdi,
8417 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8418 int set_addrmap, struct dwarf2_cu *cu)
8419 {
8420 /* Add a symbol for the namespace. */
8421
8422 add_partial_symbol (pdi, cu);
8423
8424 /* Now scan partial symbols in that namespace. */
8425
8426 if (pdi->has_children)
8427 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8428 }
8429
8430 /* Read a partial die corresponding to a Fortran module. */
8431
8432 static void
8433 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8434 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8435 {
8436 /* Add a symbol for the namespace. */
8437
8438 add_partial_symbol (pdi, cu);
8439
8440 /* Now scan partial symbols in that module. */
8441
8442 if (pdi->has_children)
8443 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8444 }
8445
8446 /* Read a partial die corresponding to a subprogram or an inlined
8447 subprogram and create a partial symbol for that subprogram.
8448 When the CU language allows it, this routine also defines a partial
8449 symbol for each nested subprogram that this subprogram contains.
8450 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8451 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8452
8453 PDI may also be a lexical block, in which case we simply search
8454 recursively for subprograms defined inside that lexical block.
8455 Again, this is only performed when the CU language allows this
8456 type of definitions. */
8457
8458 static void
8459 add_partial_subprogram (struct partial_die_info *pdi,
8460 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8461 int set_addrmap, struct dwarf2_cu *cu)
8462 {
8463 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8464 {
8465 if (pdi->has_pc_info)
8466 {
8467 if (pdi->lowpc < *lowpc)
8468 *lowpc = pdi->lowpc;
8469 if (pdi->highpc > *highpc)
8470 *highpc = pdi->highpc;
8471 if (set_addrmap)
8472 {
8473 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8474 struct gdbarch *gdbarch = objfile->arch ();
8475 CORE_ADDR baseaddr;
8476 CORE_ADDR this_highpc;
8477 CORE_ADDR this_lowpc;
8478
8479 baseaddr = objfile->text_section_offset ();
8480 this_lowpc
8481 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8482 pdi->lowpc + baseaddr)
8483 - baseaddr);
8484 this_highpc
8485 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8486 pdi->highpc + baseaddr)
8487 - baseaddr);
8488 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8489 this_lowpc, this_highpc - 1,
8490 cu->per_cu->v.psymtab);
8491 }
8492 }
8493
8494 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8495 {
8496 if (!pdi->is_declaration)
8497 /* Ignore subprogram DIEs that do not have a name, they are
8498 illegal. Do not emit a complaint at this point, we will
8499 do so when we convert this psymtab into a symtab. */
8500 if (pdi->name)
8501 add_partial_symbol (pdi, cu);
8502 }
8503 }
8504
8505 if (! pdi->has_children)
8506 return;
8507
8508 if (cu->language == language_ada || cu->language == language_fortran)
8509 {
8510 pdi = pdi->die_child;
8511 while (pdi != NULL)
8512 {
8513 pdi->fixup (cu);
8514 if (pdi->tag == DW_TAG_subprogram
8515 || pdi->tag == DW_TAG_inlined_subroutine
8516 || pdi->tag == DW_TAG_lexical_block)
8517 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8518 pdi = pdi->die_sibling;
8519 }
8520 }
8521 }
8522
8523 /* Read a partial die corresponding to an enumeration type. */
8524
8525 static void
8526 add_partial_enumeration (struct partial_die_info *enum_pdi,
8527 struct dwarf2_cu *cu)
8528 {
8529 struct partial_die_info *pdi;
8530
8531 if (enum_pdi->name != NULL)
8532 add_partial_symbol (enum_pdi, cu);
8533
8534 pdi = enum_pdi->die_child;
8535 while (pdi)
8536 {
8537 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8538 complaint (_("malformed enumerator DIE ignored"));
8539 else
8540 add_partial_symbol (pdi, cu);
8541 pdi = pdi->die_sibling;
8542 }
8543 }
8544
8545 /* Return the initial uleb128 in the die at INFO_PTR. */
8546
8547 static unsigned int
8548 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8549 {
8550 unsigned int bytes_read;
8551
8552 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8553 }
8554
8555 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8556 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8557
8558 Return the corresponding abbrev, or NULL if the number is zero (indicating
8559 an empty DIE). In either case *BYTES_READ will be set to the length of
8560 the initial number. */
8561
8562 static struct abbrev_info *
8563 peek_die_abbrev (const die_reader_specs &reader,
8564 const gdb_byte *info_ptr, unsigned int *bytes_read)
8565 {
8566 dwarf2_cu *cu = reader.cu;
8567 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8568 unsigned int abbrev_number
8569 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8570
8571 if (abbrev_number == 0)
8572 return NULL;
8573
8574 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8575 if (!abbrev)
8576 {
8577 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8578 " at offset %s [in module %s]"),
8579 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8580 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8581 }
8582
8583 return abbrev;
8584 }
8585
8586 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8587 Returns a pointer to the end of a series of DIEs, terminated by an empty
8588 DIE. Any children of the skipped DIEs will also be skipped. */
8589
8590 static const gdb_byte *
8591 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8592 {
8593 while (1)
8594 {
8595 unsigned int bytes_read;
8596 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8597
8598 if (abbrev == NULL)
8599 return info_ptr + bytes_read;
8600 else
8601 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8602 }
8603 }
8604
8605 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8606 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8607 abbrev corresponding to that skipped uleb128 should be passed in
8608 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8609 children. */
8610
8611 static const gdb_byte *
8612 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8613 struct abbrev_info *abbrev)
8614 {
8615 unsigned int bytes_read;
8616 struct attribute attr;
8617 bfd *abfd = reader->abfd;
8618 struct dwarf2_cu *cu = reader->cu;
8619 const gdb_byte *buffer = reader->buffer;
8620 const gdb_byte *buffer_end = reader->buffer_end;
8621 unsigned int form, i;
8622
8623 for (i = 0; i < abbrev->num_attrs; i++)
8624 {
8625 /* The only abbrev we care about is DW_AT_sibling. */
8626 if (abbrev->attrs[i].name == DW_AT_sibling)
8627 {
8628 bool ignored;
8629 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8630 &ignored);
8631 if (attr.form == DW_FORM_ref_addr)
8632 complaint (_("ignoring absolute DW_AT_sibling"));
8633 else
8634 {
8635 sect_offset off = attr.get_ref_die_offset ();
8636 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8637
8638 if (sibling_ptr < info_ptr)
8639 complaint (_("DW_AT_sibling points backwards"));
8640 else if (sibling_ptr > reader->buffer_end)
8641 reader->die_section->overflow_complaint ();
8642 else
8643 return sibling_ptr;
8644 }
8645 }
8646
8647 /* If it isn't DW_AT_sibling, skip this attribute. */
8648 form = abbrev->attrs[i].form;
8649 skip_attribute:
8650 switch (form)
8651 {
8652 case DW_FORM_ref_addr:
8653 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8654 and later it is offset sized. */
8655 if (cu->header.version == 2)
8656 info_ptr += cu->header.addr_size;
8657 else
8658 info_ptr += cu->header.offset_size;
8659 break;
8660 case DW_FORM_GNU_ref_alt:
8661 info_ptr += cu->header.offset_size;
8662 break;
8663 case DW_FORM_addr:
8664 info_ptr += cu->header.addr_size;
8665 break;
8666 case DW_FORM_data1:
8667 case DW_FORM_ref1:
8668 case DW_FORM_flag:
8669 case DW_FORM_strx1:
8670 info_ptr += 1;
8671 break;
8672 case DW_FORM_flag_present:
8673 case DW_FORM_implicit_const:
8674 break;
8675 case DW_FORM_data2:
8676 case DW_FORM_ref2:
8677 case DW_FORM_strx2:
8678 info_ptr += 2;
8679 break;
8680 case DW_FORM_strx3:
8681 info_ptr += 3;
8682 break;
8683 case DW_FORM_data4:
8684 case DW_FORM_ref4:
8685 case DW_FORM_strx4:
8686 info_ptr += 4;
8687 break;
8688 case DW_FORM_data8:
8689 case DW_FORM_ref8:
8690 case DW_FORM_ref_sig8:
8691 info_ptr += 8;
8692 break;
8693 case DW_FORM_data16:
8694 info_ptr += 16;
8695 break;
8696 case DW_FORM_string:
8697 read_direct_string (abfd, info_ptr, &bytes_read);
8698 info_ptr += bytes_read;
8699 break;
8700 case DW_FORM_sec_offset:
8701 case DW_FORM_strp:
8702 case DW_FORM_GNU_strp_alt:
8703 info_ptr += cu->header.offset_size;
8704 break;
8705 case DW_FORM_exprloc:
8706 case DW_FORM_block:
8707 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8708 info_ptr += bytes_read;
8709 break;
8710 case DW_FORM_block1:
8711 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8712 break;
8713 case DW_FORM_block2:
8714 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8715 break;
8716 case DW_FORM_block4:
8717 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8718 break;
8719 case DW_FORM_addrx:
8720 case DW_FORM_strx:
8721 case DW_FORM_sdata:
8722 case DW_FORM_udata:
8723 case DW_FORM_ref_udata:
8724 case DW_FORM_GNU_addr_index:
8725 case DW_FORM_GNU_str_index:
8726 case DW_FORM_rnglistx:
8727 case DW_FORM_loclistx:
8728 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8729 break;
8730 case DW_FORM_indirect:
8731 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8732 info_ptr += bytes_read;
8733 /* We need to continue parsing from here, so just go back to
8734 the top. */
8735 goto skip_attribute;
8736
8737 default:
8738 error (_("Dwarf Error: Cannot handle %s "
8739 "in DWARF reader [in module %s]"),
8740 dwarf_form_name (form),
8741 bfd_get_filename (abfd));
8742 }
8743 }
8744
8745 if (abbrev->has_children)
8746 return skip_children (reader, info_ptr);
8747 else
8748 return info_ptr;
8749 }
8750
8751 /* Locate ORIG_PDI's sibling.
8752 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8753
8754 static const gdb_byte *
8755 locate_pdi_sibling (const struct die_reader_specs *reader,
8756 struct partial_die_info *orig_pdi,
8757 const gdb_byte *info_ptr)
8758 {
8759 /* Do we know the sibling already? */
8760
8761 if (orig_pdi->sibling)
8762 return orig_pdi->sibling;
8763
8764 /* Are there any children to deal with? */
8765
8766 if (!orig_pdi->has_children)
8767 return info_ptr;
8768
8769 /* Skip the children the long way. */
8770
8771 return skip_children (reader, info_ptr);
8772 }
8773
8774 /* Expand this partial symbol table into a full symbol table. SELF is
8775 not NULL. */
8776
8777 void
8778 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8779 {
8780 struct dwarf2_per_objfile *dwarf2_per_objfile
8781 = get_dwarf2_per_objfile (objfile);
8782
8783 gdb_assert (!readin);
8784 /* If this psymtab is constructed from a debug-only objfile, the
8785 has_section_at_zero flag will not necessarily be correct. We
8786 can get the correct value for this flag by looking at the data
8787 associated with the (presumably stripped) associated objfile. */
8788 if (objfile->separate_debug_objfile_backlink)
8789 {
8790 struct dwarf2_per_objfile *dpo_backlink
8791 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8792
8793 dwarf2_per_objfile->has_section_at_zero
8794 = dpo_backlink->has_section_at_zero;
8795 }
8796
8797 expand_psymtab (objfile);
8798
8799 process_cu_includes (dwarf2_per_objfile);
8800 }
8801 \f
8802 /* Reading in full CUs. */
8803
8804 /* Add PER_CU to the queue. */
8805
8806 static void
8807 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8808 enum language pretend_language)
8809 {
8810 per_cu->queued = 1;
8811 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8812 }
8813
8814 /* If PER_CU is not yet queued, add it to the queue.
8815 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8816 dependency.
8817 The result is non-zero if PER_CU was queued, otherwise the result is zero
8818 meaning either PER_CU is already queued or it is already loaded.
8819
8820 N.B. There is an invariant here that if a CU is queued then it is loaded.
8821 The caller is required to load PER_CU if we return non-zero. */
8822
8823 static int
8824 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8825 struct dwarf2_per_cu_data *per_cu,
8826 enum language pretend_language)
8827 {
8828 /* We may arrive here during partial symbol reading, if we need full
8829 DIEs to process an unusual case (e.g. template arguments). Do
8830 not queue PER_CU, just tell our caller to load its DIEs. */
8831 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8832 {
8833 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8834 return 1;
8835 return 0;
8836 }
8837
8838 /* Mark the dependence relation so that we don't flush PER_CU
8839 too early. */
8840 if (dependent_cu != NULL)
8841 dwarf2_add_dependence (dependent_cu, per_cu);
8842
8843 /* If it's already on the queue, we have nothing to do. */
8844 if (per_cu->queued)
8845 return 0;
8846
8847 /* If the compilation unit is already loaded, just mark it as
8848 used. */
8849 if (per_cu->cu != NULL)
8850 {
8851 per_cu->cu->last_used = 0;
8852 return 0;
8853 }
8854
8855 /* Add it to the queue. */
8856 queue_comp_unit (per_cu, pretend_language);
8857
8858 return 1;
8859 }
8860
8861 /* Process the queue. */
8862
8863 static void
8864 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8865 {
8866 if (dwarf_read_debug)
8867 {
8868 fprintf_unfiltered (gdb_stdlog,
8869 "Expanding one or more symtabs of objfile %s ...\n",
8870 objfile_name (dwarf2_per_objfile->objfile));
8871 }
8872
8873 /* The queue starts out with one item, but following a DIE reference
8874 may load a new CU, adding it to the end of the queue. */
8875 while (!dwarf2_per_objfile->queue.empty ())
8876 {
8877 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8878
8879 if ((dwarf2_per_objfile->using_index
8880 ? !item.per_cu->v.quick->compunit_symtab
8881 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8882 /* Skip dummy CUs. */
8883 && item.per_cu->cu != NULL)
8884 {
8885 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8886 unsigned int debug_print_threshold;
8887 char buf[100];
8888
8889 if (per_cu->is_debug_types)
8890 {
8891 struct signatured_type *sig_type =
8892 (struct signatured_type *) per_cu;
8893
8894 sprintf (buf, "TU %s at offset %s",
8895 hex_string (sig_type->signature),
8896 sect_offset_str (per_cu->sect_off));
8897 /* There can be 100s of TUs.
8898 Only print them in verbose mode. */
8899 debug_print_threshold = 2;
8900 }
8901 else
8902 {
8903 sprintf (buf, "CU at offset %s",
8904 sect_offset_str (per_cu->sect_off));
8905 debug_print_threshold = 1;
8906 }
8907
8908 if (dwarf_read_debug >= debug_print_threshold)
8909 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8910
8911 if (per_cu->is_debug_types)
8912 process_full_type_unit (per_cu, item.pretend_language);
8913 else
8914 process_full_comp_unit (per_cu, item.pretend_language);
8915
8916 if (dwarf_read_debug >= debug_print_threshold)
8917 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8918 }
8919
8920 item.per_cu->queued = 0;
8921 dwarf2_per_objfile->queue.pop ();
8922 }
8923
8924 if (dwarf_read_debug)
8925 {
8926 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8927 objfile_name (dwarf2_per_objfile->objfile));
8928 }
8929 }
8930
8931 /* Read in full symbols for PST, and anything it depends on. */
8932
8933 void
8934 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8935 {
8936 gdb_assert (!readin);
8937
8938 expand_dependencies (objfile);
8939
8940 dw2_do_instantiate_symtab (per_cu_data, false);
8941 gdb_assert (get_compunit_symtab () != nullptr);
8942 }
8943
8944 /* Trivial hash function for die_info: the hash value of a DIE
8945 is its offset in .debug_info for this objfile. */
8946
8947 static hashval_t
8948 die_hash (const void *item)
8949 {
8950 const struct die_info *die = (const struct die_info *) item;
8951
8952 return to_underlying (die->sect_off);
8953 }
8954
8955 /* Trivial comparison function for die_info structures: two DIEs
8956 are equal if they have the same offset. */
8957
8958 static int
8959 die_eq (const void *item_lhs, const void *item_rhs)
8960 {
8961 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8962 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8963
8964 return die_lhs->sect_off == die_rhs->sect_off;
8965 }
8966
8967 /* Load the DIEs associated with PER_CU into memory. */
8968
8969 static void
8970 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8971 bool skip_partial,
8972 enum language pretend_language)
8973 {
8974 gdb_assert (! this_cu->is_debug_types);
8975
8976 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8977 if (reader.dummy_p)
8978 return;
8979
8980 struct dwarf2_cu *cu = reader.cu;
8981 const gdb_byte *info_ptr = reader.info_ptr;
8982
8983 gdb_assert (cu->die_hash == NULL);
8984 cu->die_hash =
8985 htab_create_alloc_ex (cu->header.length / 12,
8986 die_hash,
8987 die_eq,
8988 NULL,
8989 &cu->comp_unit_obstack,
8990 hashtab_obstack_allocate,
8991 dummy_obstack_deallocate);
8992
8993 if (reader.comp_unit_die->has_children)
8994 reader.comp_unit_die->child
8995 = read_die_and_siblings (&reader, reader.info_ptr,
8996 &info_ptr, reader.comp_unit_die);
8997 cu->dies = reader.comp_unit_die;
8998 /* comp_unit_die is not stored in die_hash, no need. */
8999
9000 /* We try not to read any attributes in this function, because not
9001 all CUs needed for references have been loaded yet, and symbol
9002 table processing isn't initialized. But we have to set the CU language,
9003 or we won't be able to build types correctly.
9004 Similarly, if we do not read the producer, we can not apply
9005 producer-specific interpretation. */
9006 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9007
9008 reader.keep ();
9009 }
9010
9011 /* Add a DIE to the delayed physname list. */
9012
9013 static void
9014 add_to_method_list (struct type *type, int fnfield_index, int index,
9015 const char *name, struct die_info *die,
9016 struct dwarf2_cu *cu)
9017 {
9018 struct delayed_method_info mi;
9019 mi.type = type;
9020 mi.fnfield_index = fnfield_index;
9021 mi.index = index;
9022 mi.name = name;
9023 mi.die = die;
9024 cu->method_list.push_back (mi);
9025 }
9026
9027 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9028 "const" / "volatile". If so, decrements LEN by the length of the
9029 modifier and return true. Otherwise return false. */
9030
9031 template<size_t N>
9032 static bool
9033 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9034 {
9035 size_t mod_len = sizeof (mod) - 1;
9036 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9037 {
9038 len -= mod_len;
9039 return true;
9040 }
9041 return false;
9042 }
9043
9044 /* Compute the physnames of any methods on the CU's method list.
9045
9046 The computation of method physnames is delayed in order to avoid the
9047 (bad) condition that one of the method's formal parameters is of an as yet
9048 incomplete type. */
9049
9050 static void
9051 compute_delayed_physnames (struct dwarf2_cu *cu)
9052 {
9053 /* Only C++ delays computing physnames. */
9054 if (cu->method_list.empty ())
9055 return;
9056 gdb_assert (cu->language == language_cplus);
9057
9058 for (const delayed_method_info &mi : cu->method_list)
9059 {
9060 const char *physname;
9061 struct fn_fieldlist *fn_flp
9062 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9063 physname = dwarf2_physname (mi.name, mi.die, cu);
9064 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9065 = physname ? physname : "";
9066
9067 /* Since there's no tag to indicate whether a method is a
9068 const/volatile overload, extract that information out of the
9069 demangled name. */
9070 if (physname != NULL)
9071 {
9072 size_t len = strlen (physname);
9073
9074 while (1)
9075 {
9076 if (physname[len] == ')') /* shortcut */
9077 break;
9078 else if (check_modifier (physname, len, " const"))
9079 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9080 else if (check_modifier (physname, len, " volatile"))
9081 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9082 else
9083 break;
9084 }
9085 }
9086 }
9087
9088 /* The list is no longer needed. */
9089 cu->method_list.clear ();
9090 }
9091
9092 /* Go objects should be embedded in a DW_TAG_module DIE,
9093 and it's not clear if/how imported objects will appear.
9094 To keep Go support simple until that's worked out,
9095 go back through what we've read and create something usable.
9096 We could do this while processing each DIE, and feels kinda cleaner,
9097 but that way is more invasive.
9098 This is to, for example, allow the user to type "p var" or "b main"
9099 without having to specify the package name, and allow lookups
9100 of module.object to work in contexts that use the expression
9101 parser. */
9102
9103 static void
9104 fixup_go_packaging (struct dwarf2_cu *cu)
9105 {
9106 gdb::unique_xmalloc_ptr<char> package_name;
9107 struct pending *list;
9108 int i;
9109
9110 for (list = *cu->get_builder ()->get_global_symbols ();
9111 list != NULL;
9112 list = list->next)
9113 {
9114 for (i = 0; i < list->nsyms; ++i)
9115 {
9116 struct symbol *sym = list->symbol[i];
9117
9118 if (sym->language () == language_go
9119 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9120 {
9121 gdb::unique_xmalloc_ptr<char> this_package_name
9122 (go_symbol_package_name (sym));
9123
9124 if (this_package_name == NULL)
9125 continue;
9126 if (package_name == NULL)
9127 package_name = std::move (this_package_name);
9128 else
9129 {
9130 struct objfile *objfile
9131 = cu->per_cu->dwarf2_per_objfile->objfile;
9132 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9133 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9134 (symbol_symtab (sym) != NULL
9135 ? symtab_to_filename_for_display
9136 (symbol_symtab (sym))
9137 : objfile_name (objfile)),
9138 this_package_name.get (), package_name.get ());
9139 }
9140 }
9141 }
9142 }
9143
9144 if (package_name != NULL)
9145 {
9146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9147 const char *saved_package_name = objfile->intern (package_name.get ());
9148 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9149 saved_package_name);
9150 struct symbol *sym;
9151
9152 sym = allocate_symbol (objfile);
9153 sym->set_language (language_go, &objfile->objfile_obstack);
9154 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9155 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9156 e.g., "main" finds the "main" module and not C's main(). */
9157 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9158 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9159 SYMBOL_TYPE (sym) = type;
9160
9161 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9162 }
9163 }
9164
9165 /* Allocate a fully-qualified name consisting of the two parts on the
9166 obstack. */
9167
9168 static const char *
9169 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9170 {
9171 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9172 }
9173
9174 /* A helper that allocates a variant part to attach to a Rust enum
9175 type. OBSTACK is where the results should be allocated. TYPE is
9176 the type we're processing. DISCRIMINANT_INDEX is the index of the
9177 discriminant. It must be the index of one of the fields of TYPE.
9178 DEFAULT_INDEX is the index of the default field; or -1 if there is
9179 no default. RANGES is indexed by "effective" field number (the
9180 field index, but omitting the discriminant and default fields) and
9181 must hold the discriminant values used by the variants. Note that
9182 RANGES must have a lifetime at least as long as OBSTACK -- either
9183 already allocated on it, or static. */
9184
9185 static void
9186 alloc_rust_variant (struct obstack *obstack, struct type *type,
9187 int discriminant_index, int default_index,
9188 gdb::array_view<discriminant_range> ranges)
9189 {
9190 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9191 must be handled by the caller. */
9192 gdb_assert (discriminant_index >= 0
9193 && discriminant_index < TYPE_NFIELDS (type));
9194 gdb_assert (default_index == -1
9195 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9196
9197 /* We have one variant for each non-discriminant field. */
9198 int n_variants = TYPE_NFIELDS (type) - 1;
9199
9200 variant *variants = new (obstack) variant[n_variants];
9201 int var_idx = 0;
9202 int range_idx = 0;
9203 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9204 {
9205 if (i == discriminant_index)
9206 continue;
9207
9208 variants[var_idx].first_field = i;
9209 variants[var_idx].last_field = i + 1;
9210
9211 /* The default field does not need a range, but other fields do.
9212 We skipped the discriminant above. */
9213 if (i != default_index)
9214 {
9215 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9216 ++range_idx;
9217 }
9218
9219 ++var_idx;
9220 }
9221
9222 gdb_assert (range_idx == ranges.size ());
9223 gdb_assert (var_idx == n_variants);
9224
9225 variant_part *part = new (obstack) variant_part;
9226 part->discriminant_index = discriminant_index;
9227 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9228 discriminant_index));
9229 part->variants = gdb::array_view<variant> (variants, n_variants);
9230
9231 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9232 gdb::array_view<variant_part> *prop_value
9233 = new (storage) gdb::array_view<variant_part> (part, 1);
9234
9235 struct dynamic_prop prop;
9236 prop.kind = PROP_VARIANT_PARTS;
9237 prop.data.variant_parts = prop_value;
9238
9239 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9240 }
9241
9242 /* Some versions of rustc emitted enums in an unusual way.
9243
9244 Ordinary enums were emitted as unions. The first element of each
9245 structure in the union was named "RUST$ENUM$DISR". This element
9246 held the discriminant.
9247
9248 These versions of Rust also implemented the "non-zero"
9249 optimization. When the enum had two values, and one is empty and
9250 the other holds a pointer that cannot be zero, the pointer is used
9251 as the discriminant, with a zero value meaning the empty variant.
9252 Here, the union's first member is of the form
9253 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9254 where the fieldnos are the indices of the fields that should be
9255 traversed in order to find the field (which may be several fields deep)
9256 and the variantname is the name of the variant of the case when the
9257 field is zero.
9258
9259 This function recognizes whether TYPE is of one of these forms,
9260 and, if so, smashes it to be a variant type. */
9261
9262 static void
9263 quirk_rust_enum (struct type *type, struct objfile *objfile)
9264 {
9265 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9266
9267 /* We don't need to deal with empty enums. */
9268 if (TYPE_NFIELDS (type) == 0)
9269 return;
9270
9271 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9272 if (TYPE_NFIELDS (type) == 1
9273 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9274 {
9275 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9276
9277 /* Decode the field name to find the offset of the
9278 discriminant. */
9279 ULONGEST bit_offset = 0;
9280 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9281 while (name[0] >= '0' && name[0] <= '9')
9282 {
9283 char *tail;
9284 unsigned long index = strtoul (name, &tail, 10);
9285 name = tail;
9286 if (*name != '$'
9287 || index >= TYPE_NFIELDS (field_type)
9288 || (TYPE_FIELD_LOC_KIND (field_type, index)
9289 != FIELD_LOC_KIND_BITPOS))
9290 {
9291 complaint (_("Could not parse Rust enum encoding string \"%s\""
9292 "[in module %s]"),
9293 TYPE_FIELD_NAME (type, 0),
9294 objfile_name (objfile));
9295 return;
9296 }
9297 ++name;
9298
9299 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9300 field_type = TYPE_FIELD_TYPE (field_type, index);
9301 }
9302
9303 /* Smash this type to be a structure type. We have to do this
9304 because the type has already been recorded. */
9305 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9306 TYPE_NFIELDS (type) = 3;
9307 /* Save the field we care about. */
9308 struct field saved_field = TYPE_FIELD (type, 0);
9309 TYPE_FIELDS (type)
9310 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9311
9312 /* Put the discriminant at index 0. */
9313 TYPE_FIELD_TYPE (type, 0) = field_type;
9314 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9315 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9316 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), bit_offset);
9317
9318 /* The order of fields doesn't really matter, so put the real
9319 field at index 1 and the data-less field at index 2. */
9320 TYPE_FIELD (type, 1) = saved_field;
9321 TYPE_FIELD_NAME (type, 1)
9322 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 1)));
9323 TYPE_NAME (TYPE_FIELD_TYPE (type, 1))
9324 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9325 TYPE_FIELD_NAME (type, 1));
9326
9327 const char *dataless_name
9328 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9329 name);
9330 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9331 dataless_name);
9332 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9333 /* NAME points into the original discriminant name, which
9334 already has the correct lifetime. */
9335 TYPE_FIELD_NAME (type, 2) = name;
9336 SET_FIELD_BITPOS (TYPE_FIELD (type, 2), 0);
9337
9338 /* Indicate that this is a variant type. */
9339 static discriminant_range ranges[1] = { { 0, 0 } };
9340 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9341 }
9342 /* A union with a single anonymous field is probably an old-style
9343 univariant enum. */
9344 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9345 {
9346 /* Smash this type to be a structure type. We have to do this
9347 because the type has already been recorded. */
9348 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9349
9350 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9351 const char *variant_name
9352 = rust_last_path_segment (TYPE_NAME (field_type));
9353 TYPE_FIELD_NAME (type, 0) = variant_name;
9354 TYPE_NAME (field_type)
9355 = rust_fully_qualify (&objfile->objfile_obstack,
9356 TYPE_NAME (type), variant_name);
9357 }
9358 else
9359 {
9360 struct type *disr_type = nullptr;
9361 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9362 {
9363 disr_type = TYPE_FIELD_TYPE (type, i);
9364
9365 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9366 {
9367 /* All fields of a true enum will be structs. */
9368 return;
9369 }
9370 else if (TYPE_NFIELDS (disr_type) == 0)
9371 {
9372 /* Could be data-less variant, so keep going. */
9373 disr_type = nullptr;
9374 }
9375 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9376 "RUST$ENUM$DISR") != 0)
9377 {
9378 /* Not a Rust enum. */
9379 return;
9380 }
9381 else
9382 {
9383 /* Found one. */
9384 break;
9385 }
9386 }
9387
9388 /* If we got here without a discriminant, then it's probably
9389 just a union. */
9390 if (disr_type == nullptr)
9391 return;
9392
9393 /* Smash this type to be a structure type. We have to do this
9394 because the type has already been recorded. */
9395 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9396
9397 /* Make space for the discriminant field. */
9398 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9399 field *new_fields
9400 = (struct field *) TYPE_ZALLOC (type, (TYPE_NFIELDS (type)
9401 * sizeof (struct field)));
9402 memcpy (new_fields + 1, TYPE_FIELDS (type),
9403 TYPE_NFIELDS (type) * sizeof (struct field));
9404 TYPE_FIELDS (type) = new_fields;
9405 TYPE_NFIELDS (type) = TYPE_NFIELDS (type) + 1;
9406
9407 /* Install the discriminant at index 0 in the union. */
9408 TYPE_FIELD (type, 0) = *disr_field;
9409 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9410 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9411
9412 /* We need a way to find the correct discriminant given a
9413 variant name. For convenience we build a map here. */
9414 struct type *enum_type = FIELD_TYPE (*disr_field);
9415 std::unordered_map<std::string, ULONGEST> discriminant_map;
9416 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9417 {
9418 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9419 {
9420 const char *name
9421 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9422 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9423 }
9424 }
9425
9426 int n_fields = TYPE_NFIELDS (type);
9427 /* We don't need a range entry for the discriminant, but we do
9428 need one for every other field, as there is no default
9429 variant. */
9430 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9431 discriminant_range,
9432 n_fields - 1);
9433 /* Skip the discriminant here. */
9434 for (int i = 1; i < n_fields; ++i)
9435 {
9436 /* Find the final word in the name of this variant's type.
9437 That name can be used to look up the correct
9438 discriminant. */
9439 const char *variant_name
9440 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, i)));
9441
9442 auto iter = discriminant_map.find (variant_name);
9443 if (iter != discriminant_map.end ())
9444 {
9445 ranges[i].low = iter->second;
9446 ranges[i].high = iter->second;
9447 }
9448
9449 /* Remove the discriminant field, if it exists. */
9450 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9451 if (TYPE_NFIELDS (sub_type) > 0)
9452 {
9453 --TYPE_NFIELDS (sub_type);
9454 ++TYPE_FIELDS (sub_type);
9455 }
9456 TYPE_FIELD_NAME (type, i) = variant_name;
9457 TYPE_NAME (sub_type)
9458 = rust_fully_qualify (&objfile->objfile_obstack,
9459 TYPE_NAME (type), variant_name);
9460 }
9461
9462 /* Indicate that this is a variant type. */
9463 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9464 gdb::array_view<discriminant_range> (ranges,
9465 n_fields - 1));
9466 }
9467 }
9468
9469 /* Rewrite some Rust unions to be structures with variants parts. */
9470
9471 static void
9472 rust_union_quirks (struct dwarf2_cu *cu)
9473 {
9474 gdb_assert (cu->language == language_rust);
9475 for (type *type_ : cu->rust_unions)
9476 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9477 /* We don't need this any more. */
9478 cu->rust_unions.clear ();
9479 }
9480
9481 /* Return the symtab for PER_CU. This works properly regardless of
9482 whether we're using the index or psymtabs. */
9483
9484 static struct compunit_symtab *
9485 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9486 {
9487 return (per_cu->dwarf2_per_objfile->using_index
9488 ? per_cu->v.quick->compunit_symtab
9489 : per_cu->v.psymtab->compunit_symtab);
9490 }
9491
9492 /* A helper function for computing the list of all symbol tables
9493 included by PER_CU. */
9494
9495 static void
9496 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9497 htab_t all_children, htab_t all_type_symtabs,
9498 struct dwarf2_per_cu_data *per_cu,
9499 struct compunit_symtab *immediate_parent)
9500 {
9501 void **slot;
9502 struct compunit_symtab *cust;
9503
9504 slot = htab_find_slot (all_children, per_cu, INSERT);
9505 if (*slot != NULL)
9506 {
9507 /* This inclusion and its children have been processed. */
9508 return;
9509 }
9510
9511 *slot = per_cu;
9512 /* Only add a CU if it has a symbol table. */
9513 cust = get_compunit_symtab (per_cu);
9514 if (cust != NULL)
9515 {
9516 /* If this is a type unit only add its symbol table if we haven't
9517 seen it yet (type unit per_cu's can share symtabs). */
9518 if (per_cu->is_debug_types)
9519 {
9520 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9521 if (*slot == NULL)
9522 {
9523 *slot = cust;
9524 result->push_back (cust);
9525 if (cust->user == NULL)
9526 cust->user = immediate_parent;
9527 }
9528 }
9529 else
9530 {
9531 result->push_back (cust);
9532 if (cust->user == NULL)
9533 cust->user = immediate_parent;
9534 }
9535 }
9536
9537 if (!per_cu->imported_symtabs_empty ())
9538 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9539 {
9540 recursively_compute_inclusions (result, all_children,
9541 all_type_symtabs, ptr, cust);
9542 }
9543 }
9544
9545 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9546 PER_CU. */
9547
9548 static void
9549 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9550 {
9551 gdb_assert (! per_cu->is_debug_types);
9552
9553 if (!per_cu->imported_symtabs_empty ())
9554 {
9555 int len;
9556 std::vector<compunit_symtab *> result_symtabs;
9557 htab_t all_children, all_type_symtabs;
9558 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9559
9560 /* If we don't have a symtab, we can just skip this case. */
9561 if (cust == NULL)
9562 return;
9563
9564 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9565 NULL, xcalloc, xfree);
9566 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9567 NULL, xcalloc, xfree);
9568
9569 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9570 {
9571 recursively_compute_inclusions (&result_symtabs, all_children,
9572 all_type_symtabs, ptr, cust);
9573 }
9574
9575 /* Now we have a transitive closure of all the included symtabs. */
9576 len = result_symtabs.size ();
9577 cust->includes
9578 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9579 struct compunit_symtab *, len + 1);
9580 memcpy (cust->includes, result_symtabs.data (),
9581 len * sizeof (compunit_symtab *));
9582 cust->includes[len] = NULL;
9583
9584 htab_delete (all_children);
9585 htab_delete (all_type_symtabs);
9586 }
9587 }
9588
9589 /* Compute the 'includes' field for the symtabs of all the CUs we just
9590 read. */
9591
9592 static void
9593 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9594 {
9595 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9596 {
9597 if (! iter->is_debug_types)
9598 compute_compunit_symtab_includes (iter);
9599 }
9600
9601 dwarf2_per_objfile->just_read_cus.clear ();
9602 }
9603
9604 /* Generate full symbol information for PER_CU, whose DIEs have
9605 already been loaded into memory. */
9606
9607 static void
9608 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9609 enum language pretend_language)
9610 {
9611 struct dwarf2_cu *cu = per_cu->cu;
9612 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9613 struct objfile *objfile = dwarf2_per_objfile->objfile;
9614 struct gdbarch *gdbarch = objfile->arch ();
9615 CORE_ADDR lowpc, highpc;
9616 struct compunit_symtab *cust;
9617 CORE_ADDR baseaddr;
9618 struct block *static_block;
9619 CORE_ADDR addr;
9620
9621 baseaddr = objfile->text_section_offset ();
9622
9623 /* Clear the list here in case something was left over. */
9624 cu->method_list.clear ();
9625
9626 cu->language = pretend_language;
9627 cu->language_defn = language_def (cu->language);
9628
9629 /* Do line number decoding in read_file_scope () */
9630 process_die (cu->dies, cu);
9631
9632 /* For now fudge the Go package. */
9633 if (cu->language == language_go)
9634 fixup_go_packaging (cu);
9635
9636 /* Now that we have processed all the DIEs in the CU, all the types
9637 should be complete, and it should now be safe to compute all of the
9638 physnames. */
9639 compute_delayed_physnames (cu);
9640
9641 if (cu->language == language_rust)
9642 rust_union_quirks (cu);
9643
9644 /* Some compilers don't define a DW_AT_high_pc attribute for the
9645 compilation unit. If the DW_AT_high_pc is missing, synthesize
9646 it, by scanning the DIE's below the compilation unit. */
9647 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9648
9649 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9650 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9651
9652 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9653 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9654 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9655 addrmap to help ensure it has an accurate map of pc values belonging to
9656 this comp unit. */
9657 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9658
9659 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9660 SECT_OFF_TEXT (objfile),
9661 0);
9662
9663 if (cust != NULL)
9664 {
9665 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9666
9667 /* Set symtab language to language from DW_AT_language. If the
9668 compilation is from a C file generated by language preprocessors, do
9669 not set the language if it was already deduced by start_subfile. */
9670 if (!(cu->language == language_c
9671 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9672 COMPUNIT_FILETABS (cust)->language = cu->language;
9673
9674 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9675 produce DW_AT_location with location lists but it can be possibly
9676 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9677 there were bugs in prologue debug info, fixed later in GCC-4.5
9678 by "unwind info for epilogues" patch (which is not directly related).
9679
9680 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9681 needed, it would be wrong due to missing DW_AT_producer there.
9682
9683 Still one can confuse GDB by using non-standard GCC compilation
9684 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9685 */
9686 if (cu->has_loclist && gcc_4_minor >= 5)
9687 cust->locations_valid = 1;
9688
9689 if (gcc_4_minor >= 5)
9690 cust->epilogue_unwind_valid = 1;
9691
9692 cust->call_site_htab = cu->call_site_htab;
9693 }
9694
9695 if (dwarf2_per_objfile->using_index)
9696 per_cu->v.quick->compunit_symtab = cust;
9697 else
9698 {
9699 dwarf2_psymtab *pst = per_cu->v.psymtab;
9700 pst->compunit_symtab = cust;
9701 pst->readin = true;
9702 }
9703
9704 /* Push it for inclusion processing later. */
9705 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9706
9707 /* Not needed any more. */
9708 cu->reset_builder ();
9709 }
9710
9711 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9712 already been loaded into memory. */
9713
9714 static void
9715 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9716 enum language pretend_language)
9717 {
9718 struct dwarf2_cu *cu = per_cu->cu;
9719 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9720 struct objfile *objfile = dwarf2_per_objfile->objfile;
9721 struct compunit_symtab *cust;
9722 struct signatured_type *sig_type;
9723
9724 gdb_assert (per_cu->is_debug_types);
9725 sig_type = (struct signatured_type *) per_cu;
9726
9727 /* Clear the list here in case something was left over. */
9728 cu->method_list.clear ();
9729
9730 cu->language = pretend_language;
9731 cu->language_defn = language_def (cu->language);
9732
9733 /* The symbol tables are set up in read_type_unit_scope. */
9734 process_die (cu->dies, cu);
9735
9736 /* For now fudge the Go package. */
9737 if (cu->language == language_go)
9738 fixup_go_packaging (cu);
9739
9740 /* Now that we have processed all the DIEs in the CU, all the types
9741 should be complete, and it should now be safe to compute all of the
9742 physnames. */
9743 compute_delayed_physnames (cu);
9744
9745 if (cu->language == language_rust)
9746 rust_union_quirks (cu);
9747
9748 /* TUs share symbol tables.
9749 If this is the first TU to use this symtab, complete the construction
9750 of it with end_expandable_symtab. Otherwise, complete the addition of
9751 this TU's symbols to the existing symtab. */
9752 if (sig_type->type_unit_group->compunit_symtab == NULL)
9753 {
9754 buildsym_compunit *builder = cu->get_builder ();
9755 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9756 sig_type->type_unit_group->compunit_symtab = cust;
9757
9758 if (cust != NULL)
9759 {
9760 /* Set symtab language to language from DW_AT_language. If the
9761 compilation is from a C file generated by language preprocessors,
9762 do not set the language if it was already deduced by
9763 start_subfile. */
9764 if (!(cu->language == language_c
9765 && COMPUNIT_FILETABS (cust)->language != language_c))
9766 COMPUNIT_FILETABS (cust)->language = cu->language;
9767 }
9768 }
9769 else
9770 {
9771 cu->get_builder ()->augment_type_symtab ();
9772 cust = sig_type->type_unit_group->compunit_symtab;
9773 }
9774
9775 if (dwarf2_per_objfile->using_index)
9776 per_cu->v.quick->compunit_symtab = cust;
9777 else
9778 {
9779 dwarf2_psymtab *pst = per_cu->v.psymtab;
9780 pst->compunit_symtab = cust;
9781 pst->readin = true;
9782 }
9783
9784 /* Not needed any more. */
9785 cu->reset_builder ();
9786 }
9787
9788 /* Process an imported unit DIE. */
9789
9790 static void
9791 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9792 {
9793 struct attribute *attr;
9794
9795 /* For now we don't handle imported units in type units. */
9796 if (cu->per_cu->is_debug_types)
9797 {
9798 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9799 " supported in type units [in module %s]"),
9800 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9801 }
9802
9803 attr = dwarf2_attr (die, DW_AT_import, cu);
9804 if (attr != NULL)
9805 {
9806 sect_offset sect_off = attr->get_ref_die_offset ();
9807 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9808 dwarf2_per_cu_data *per_cu
9809 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9810 cu->per_cu->dwarf2_per_objfile);
9811
9812 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9813 into another compilation unit, at root level. Regard this as a hint,
9814 and ignore it. */
9815 if (die->parent && die->parent->parent == NULL
9816 && per_cu->unit_type == DW_UT_compile
9817 && per_cu->lang == language_cplus)
9818 return;
9819
9820 /* If necessary, add it to the queue and load its DIEs. */
9821 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9822 load_full_comp_unit (per_cu, false, cu->language);
9823
9824 cu->per_cu->imported_symtabs_push (per_cu);
9825 }
9826 }
9827
9828 /* RAII object that represents a process_die scope: i.e.,
9829 starts/finishes processing a DIE. */
9830 class process_die_scope
9831 {
9832 public:
9833 process_die_scope (die_info *die, dwarf2_cu *cu)
9834 : m_die (die), m_cu (cu)
9835 {
9836 /* We should only be processing DIEs not already in process. */
9837 gdb_assert (!m_die->in_process);
9838 m_die->in_process = true;
9839 }
9840
9841 ~process_die_scope ()
9842 {
9843 m_die->in_process = false;
9844
9845 /* If we're done processing the DIE for the CU that owns the line
9846 header, we don't need the line header anymore. */
9847 if (m_cu->line_header_die_owner == m_die)
9848 {
9849 delete m_cu->line_header;
9850 m_cu->line_header = NULL;
9851 m_cu->line_header_die_owner = NULL;
9852 }
9853 }
9854
9855 private:
9856 die_info *m_die;
9857 dwarf2_cu *m_cu;
9858 };
9859
9860 /* Process a die and its children. */
9861
9862 static void
9863 process_die (struct die_info *die, struct dwarf2_cu *cu)
9864 {
9865 process_die_scope scope (die, cu);
9866
9867 switch (die->tag)
9868 {
9869 case DW_TAG_padding:
9870 break;
9871 case DW_TAG_compile_unit:
9872 case DW_TAG_partial_unit:
9873 read_file_scope (die, cu);
9874 break;
9875 case DW_TAG_type_unit:
9876 read_type_unit_scope (die, cu);
9877 break;
9878 case DW_TAG_subprogram:
9879 /* Nested subprograms in Fortran get a prefix. */
9880 if (cu->language == language_fortran
9881 && die->parent != NULL
9882 && die->parent->tag == DW_TAG_subprogram)
9883 cu->processing_has_namespace_info = true;
9884 /* Fall through. */
9885 case DW_TAG_inlined_subroutine:
9886 read_func_scope (die, cu);
9887 break;
9888 case DW_TAG_lexical_block:
9889 case DW_TAG_try_block:
9890 case DW_TAG_catch_block:
9891 read_lexical_block_scope (die, cu);
9892 break;
9893 case DW_TAG_call_site:
9894 case DW_TAG_GNU_call_site:
9895 read_call_site_scope (die, cu);
9896 break;
9897 case DW_TAG_class_type:
9898 case DW_TAG_interface_type:
9899 case DW_TAG_structure_type:
9900 case DW_TAG_union_type:
9901 process_structure_scope (die, cu);
9902 break;
9903 case DW_TAG_enumeration_type:
9904 process_enumeration_scope (die, cu);
9905 break;
9906
9907 /* These dies have a type, but processing them does not create
9908 a symbol or recurse to process the children. Therefore we can
9909 read them on-demand through read_type_die. */
9910 case DW_TAG_subroutine_type:
9911 case DW_TAG_set_type:
9912 case DW_TAG_array_type:
9913 case DW_TAG_pointer_type:
9914 case DW_TAG_ptr_to_member_type:
9915 case DW_TAG_reference_type:
9916 case DW_TAG_rvalue_reference_type:
9917 case DW_TAG_string_type:
9918 break;
9919
9920 case DW_TAG_base_type:
9921 case DW_TAG_subrange_type:
9922 case DW_TAG_typedef:
9923 /* Add a typedef symbol for the type definition, if it has a
9924 DW_AT_name. */
9925 new_symbol (die, read_type_die (die, cu), cu);
9926 break;
9927 case DW_TAG_common_block:
9928 read_common_block (die, cu);
9929 break;
9930 case DW_TAG_common_inclusion:
9931 break;
9932 case DW_TAG_namespace:
9933 cu->processing_has_namespace_info = true;
9934 read_namespace (die, cu);
9935 break;
9936 case DW_TAG_module:
9937 cu->processing_has_namespace_info = true;
9938 read_module (die, cu);
9939 break;
9940 case DW_TAG_imported_declaration:
9941 cu->processing_has_namespace_info = true;
9942 if (read_namespace_alias (die, cu))
9943 break;
9944 /* The declaration is not a global namespace alias. */
9945 /* Fall through. */
9946 case DW_TAG_imported_module:
9947 cu->processing_has_namespace_info = true;
9948 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9949 || cu->language != language_fortran))
9950 complaint (_("Tag '%s' has unexpected children"),
9951 dwarf_tag_name (die->tag));
9952 read_import_statement (die, cu);
9953 break;
9954
9955 case DW_TAG_imported_unit:
9956 process_imported_unit_die (die, cu);
9957 break;
9958
9959 case DW_TAG_variable:
9960 read_variable (die, cu);
9961 break;
9962
9963 default:
9964 new_symbol (die, NULL, cu);
9965 break;
9966 }
9967 }
9968 \f
9969 /* DWARF name computation. */
9970
9971 /* A helper function for dwarf2_compute_name which determines whether DIE
9972 needs to have the name of the scope prepended to the name listed in the
9973 die. */
9974
9975 static int
9976 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9977 {
9978 struct attribute *attr;
9979
9980 switch (die->tag)
9981 {
9982 case DW_TAG_namespace:
9983 case DW_TAG_typedef:
9984 case DW_TAG_class_type:
9985 case DW_TAG_interface_type:
9986 case DW_TAG_structure_type:
9987 case DW_TAG_union_type:
9988 case DW_TAG_enumeration_type:
9989 case DW_TAG_enumerator:
9990 case DW_TAG_subprogram:
9991 case DW_TAG_inlined_subroutine:
9992 case DW_TAG_member:
9993 case DW_TAG_imported_declaration:
9994 return 1;
9995
9996 case DW_TAG_variable:
9997 case DW_TAG_constant:
9998 /* We only need to prefix "globally" visible variables. These include
9999 any variable marked with DW_AT_external or any variable that
10000 lives in a namespace. [Variables in anonymous namespaces
10001 require prefixing, but they are not DW_AT_external.] */
10002
10003 if (dwarf2_attr (die, DW_AT_specification, cu))
10004 {
10005 struct dwarf2_cu *spec_cu = cu;
10006
10007 return die_needs_namespace (die_specification (die, &spec_cu),
10008 spec_cu);
10009 }
10010
10011 attr = dwarf2_attr (die, DW_AT_external, cu);
10012 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10013 && die->parent->tag != DW_TAG_module)
10014 return 0;
10015 /* A variable in a lexical block of some kind does not need a
10016 namespace, even though in C++ such variables may be external
10017 and have a mangled name. */
10018 if (die->parent->tag == DW_TAG_lexical_block
10019 || die->parent->tag == DW_TAG_try_block
10020 || die->parent->tag == DW_TAG_catch_block
10021 || die->parent->tag == DW_TAG_subprogram)
10022 return 0;
10023 return 1;
10024
10025 default:
10026 return 0;
10027 }
10028 }
10029
10030 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10031 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10032 defined for the given DIE. */
10033
10034 static struct attribute *
10035 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10036 {
10037 struct attribute *attr;
10038
10039 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10040 if (attr == NULL)
10041 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10042
10043 return attr;
10044 }
10045
10046 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10047 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10048 defined for the given DIE. */
10049
10050 static const char *
10051 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10052 {
10053 const char *linkage_name;
10054
10055 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10056 if (linkage_name == NULL)
10057 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10058
10059 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10060 See https://github.com/rust-lang/rust/issues/32925. */
10061 if (cu->language == language_rust && linkage_name != NULL
10062 && strchr (linkage_name, '{') != NULL)
10063 linkage_name = NULL;
10064
10065 return linkage_name;
10066 }
10067
10068 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10069 compute the physname for the object, which include a method's:
10070 - formal parameters (C++),
10071 - receiver type (Go),
10072
10073 The term "physname" is a bit confusing.
10074 For C++, for example, it is the demangled name.
10075 For Go, for example, it's the mangled name.
10076
10077 For Ada, return the DIE's linkage name rather than the fully qualified
10078 name. PHYSNAME is ignored..
10079
10080 The result is allocated on the objfile_obstack and canonicalized. */
10081
10082 static const char *
10083 dwarf2_compute_name (const char *name,
10084 struct die_info *die, struct dwarf2_cu *cu,
10085 int physname)
10086 {
10087 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10088
10089 if (name == NULL)
10090 name = dwarf2_name (die, cu);
10091
10092 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10093 but otherwise compute it by typename_concat inside GDB.
10094 FIXME: Actually this is not really true, or at least not always true.
10095 It's all very confusing. compute_and_set_names doesn't try to demangle
10096 Fortran names because there is no mangling standard. So new_symbol
10097 will set the demangled name to the result of dwarf2_full_name, and it is
10098 the demangled name that GDB uses if it exists. */
10099 if (cu->language == language_ada
10100 || (cu->language == language_fortran && physname))
10101 {
10102 /* For Ada unit, we prefer the linkage name over the name, as
10103 the former contains the exported name, which the user expects
10104 to be able to reference. Ideally, we want the user to be able
10105 to reference this entity using either natural or linkage name,
10106 but we haven't started looking at this enhancement yet. */
10107 const char *linkage_name = dw2_linkage_name (die, cu);
10108
10109 if (linkage_name != NULL)
10110 return linkage_name;
10111 }
10112
10113 /* These are the only languages we know how to qualify names in. */
10114 if (name != NULL
10115 && (cu->language == language_cplus
10116 || cu->language == language_fortran || cu->language == language_d
10117 || cu->language == language_rust))
10118 {
10119 if (die_needs_namespace (die, cu))
10120 {
10121 const char *prefix;
10122 const char *canonical_name = NULL;
10123
10124 string_file buf;
10125
10126 prefix = determine_prefix (die, cu);
10127 if (*prefix != '\0')
10128 {
10129 gdb::unique_xmalloc_ptr<char> prefixed_name
10130 (typename_concat (NULL, prefix, name, physname, cu));
10131
10132 buf.puts (prefixed_name.get ());
10133 }
10134 else
10135 buf.puts (name);
10136
10137 /* Template parameters may be specified in the DIE's DW_AT_name, or
10138 as children with DW_TAG_template_type_param or
10139 DW_TAG_value_type_param. If the latter, add them to the name
10140 here. If the name already has template parameters, then
10141 skip this step; some versions of GCC emit both, and
10142 it is more efficient to use the pre-computed name.
10143
10144 Something to keep in mind about this process: it is very
10145 unlikely, or in some cases downright impossible, to produce
10146 something that will match the mangled name of a function.
10147 If the definition of the function has the same debug info,
10148 we should be able to match up with it anyway. But fallbacks
10149 using the minimal symbol, for instance to find a method
10150 implemented in a stripped copy of libstdc++, will not work.
10151 If we do not have debug info for the definition, we will have to
10152 match them up some other way.
10153
10154 When we do name matching there is a related problem with function
10155 templates; two instantiated function templates are allowed to
10156 differ only by their return types, which we do not add here. */
10157
10158 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10159 {
10160 struct attribute *attr;
10161 struct die_info *child;
10162 int first = 1;
10163
10164 die->building_fullname = 1;
10165
10166 for (child = die->child; child != NULL; child = child->sibling)
10167 {
10168 struct type *type;
10169 LONGEST value;
10170 const gdb_byte *bytes;
10171 struct dwarf2_locexpr_baton *baton;
10172 struct value *v;
10173
10174 if (child->tag != DW_TAG_template_type_param
10175 && child->tag != DW_TAG_template_value_param)
10176 continue;
10177
10178 if (first)
10179 {
10180 buf.puts ("<");
10181 first = 0;
10182 }
10183 else
10184 buf.puts (", ");
10185
10186 attr = dwarf2_attr (child, DW_AT_type, cu);
10187 if (attr == NULL)
10188 {
10189 complaint (_("template parameter missing DW_AT_type"));
10190 buf.puts ("UNKNOWN_TYPE");
10191 continue;
10192 }
10193 type = die_type (child, cu);
10194
10195 if (child->tag == DW_TAG_template_type_param)
10196 {
10197 c_print_type (type, "", &buf, -1, 0, cu->language,
10198 &type_print_raw_options);
10199 continue;
10200 }
10201
10202 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10203 if (attr == NULL)
10204 {
10205 complaint (_("template parameter missing "
10206 "DW_AT_const_value"));
10207 buf.puts ("UNKNOWN_VALUE");
10208 continue;
10209 }
10210
10211 dwarf2_const_value_attr (attr, type, name,
10212 &cu->comp_unit_obstack, cu,
10213 &value, &bytes, &baton);
10214
10215 if (TYPE_NOSIGN (type))
10216 /* GDB prints characters as NUMBER 'CHAR'. If that's
10217 changed, this can use value_print instead. */
10218 c_printchar (value, type, &buf);
10219 else
10220 {
10221 struct value_print_options opts;
10222
10223 if (baton != NULL)
10224 v = dwarf2_evaluate_loc_desc (type, NULL,
10225 baton->data,
10226 baton->size,
10227 baton->per_cu);
10228 else if (bytes != NULL)
10229 {
10230 v = allocate_value (type);
10231 memcpy (value_contents_writeable (v), bytes,
10232 TYPE_LENGTH (type));
10233 }
10234 else
10235 v = value_from_longest (type, value);
10236
10237 /* Specify decimal so that we do not depend on
10238 the radix. */
10239 get_formatted_print_options (&opts, 'd');
10240 opts.raw = 1;
10241 value_print (v, &buf, &opts);
10242 release_value (v);
10243 }
10244 }
10245
10246 die->building_fullname = 0;
10247
10248 if (!first)
10249 {
10250 /* Close the argument list, with a space if necessary
10251 (nested templates). */
10252 if (!buf.empty () && buf.string ().back () == '>')
10253 buf.puts (" >");
10254 else
10255 buf.puts (">");
10256 }
10257 }
10258
10259 /* For C++ methods, append formal parameter type
10260 information, if PHYSNAME. */
10261
10262 if (physname && die->tag == DW_TAG_subprogram
10263 && cu->language == language_cplus)
10264 {
10265 struct type *type = read_type_die (die, cu);
10266
10267 c_type_print_args (type, &buf, 1, cu->language,
10268 &type_print_raw_options);
10269
10270 if (cu->language == language_cplus)
10271 {
10272 /* Assume that an artificial first parameter is
10273 "this", but do not crash if it is not. RealView
10274 marks unnamed (and thus unused) parameters as
10275 artificial; there is no way to differentiate
10276 the two cases. */
10277 if (TYPE_NFIELDS (type) > 0
10278 && TYPE_FIELD_ARTIFICIAL (type, 0)
10279 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10280 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10281 0))))
10282 buf.puts (" const");
10283 }
10284 }
10285
10286 const std::string &intermediate_name = buf.string ();
10287
10288 if (cu->language == language_cplus)
10289 canonical_name
10290 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10291 objfile);
10292
10293 /* If we only computed INTERMEDIATE_NAME, or if
10294 INTERMEDIATE_NAME is already canonical, then we need to
10295 intern it. */
10296 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10297 name = objfile->intern (intermediate_name);
10298 else
10299 name = canonical_name;
10300 }
10301 }
10302
10303 return name;
10304 }
10305
10306 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10307 If scope qualifiers are appropriate they will be added. The result
10308 will be allocated on the storage_obstack, or NULL if the DIE does
10309 not have a name. NAME may either be from a previous call to
10310 dwarf2_name or NULL.
10311
10312 The output string will be canonicalized (if C++). */
10313
10314 static const char *
10315 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10316 {
10317 return dwarf2_compute_name (name, die, cu, 0);
10318 }
10319
10320 /* Construct a physname for the given DIE in CU. NAME may either be
10321 from a previous call to dwarf2_name or NULL. The result will be
10322 allocated on the objfile_objstack or NULL if the DIE does not have a
10323 name.
10324
10325 The output string will be canonicalized (if C++). */
10326
10327 static const char *
10328 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10329 {
10330 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10331 const char *retval, *mangled = NULL, *canon = NULL;
10332 int need_copy = 1;
10333
10334 /* In this case dwarf2_compute_name is just a shortcut not building anything
10335 on its own. */
10336 if (!die_needs_namespace (die, cu))
10337 return dwarf2_compute_name (name, die, cu, 1);
10338
10339 if (cu->language != language_rust)
10340 mangled = dw2_linkage_name (die, cu);
10341
10342 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10343 has computed. */
10344 gdb::unique_xmalloc_ptr<char> demangled;
10345 if (mangled != NULL)
10346 {
10347
10348 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10349 {
10350 /* Do nothing (do not demangle the symbol name). */
10351 }
10352 else if (cu->language == language_go)
10353 {
10354 /* This is a lie, but we already lie to the caller new_symbol.
10355 new_symbol assumes we return the mangled name.
10356 This just undoes that lie until things are cleaned up. */
10357 }
10358 else
10359 {
10360 /* Use DMGL_RET_DROP for C++ template functions to suppress
10361 their return type. It is easier for GDB users to search
10362 for such functions as `name(params)' than `long name(params)'.
10363 In such case the minimal symbol names do not match the full
10364 symbol names but for template functions there is never a need
10365 to look up their definition from their declaration so
10366 the only disadvantage remains the minimal symbol variant
10367 `long name(params)' does not have the proper inferior type. */
10368 demangled.reset (gdb_demangle (mangled,
10369 (DMGL_PARAMS | DMGL_ANSI
10370 | DMGL_RET_DROP)));
10371 }
10372 if (demangled)
10373 canon = demangled.get ();
10374 else
10375 {
10376 canon = mangled;
10377 need_copy = 0;
10378 }
10379 }
10380
10381 if (canon == NULL || check_physname)
10382 {
10383 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10384
10385 if (canon != NULL && strcmp (physname, canon) != 0)
10386 {
10387 /* It may not mean a bug in GDB. The compiler could also
10388 compute DW_AT_linkage_name incorrectly. But in such case
10389 GDB would need to be bug-to-bug compatible. */
10390
10391 complaint (_("Computed physname <%s> does not match demangled <%s> "
10392 "(from linkage <%s>) - DIE at %s [in module %s]"),
10393 physname, canon, mangled, sect_offset_str (die->sect_off),
10394 objfile_name (objfile));
10395
10396 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10397 is available here - over computed PHYSNAME. It is safer
10398 against both buggy GDB and buggy compilers. */
10399
10400 retval = canon;
10401 }
10402 else
10403 {
10404 retval = physname;
10405 need_copy = 0;
10406 }
10407 }
10408 else
10409 retval = canon;
10410
10411 if (need_copy)
10412 retval = objfile->intern (retval);
10413
10414 return retval;
10415 }
10416
10417 /* Inspect DIE in CU for a namespace alias. If one exists, record
10418 a new symbol for it.
10419
10420 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10421
10422 static int
10423 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10424 {
10425 struct attribute *attr;
10426
10427 /* If the die does not have a name, this is not a namespace
10428 alias. */
10429 attr = dwarf2_attr (die, DW_AT_name, cu);
10430 if (attr != NULL)
10431 {
10432 int num;
10433 struct die_info *d = die;
10434 struct dwarf2_cu *imported_cu = cu;
10435
10436 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10437 keep inspecting DIEs until we hit the underlying import. */
10438 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10439 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10440 {
10441 attr = dwarf2_attr (d, DW_AT_import, cu);
10442 if (attr == NULL)
10443 break;
10444
10445 d = follow_die_ref (d, attr, &imported_cu);
10446 if (d->tag != DW_TAG_imported_declaration)
10447 break;
10448 }
10449
10450 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10451 {
10452 complaint (_("DIE at %s has too many recursively imported "
10453 "declarations"), sect_offset_str (d->sect_off));
10454 return 0;
10455 }
10456
10457 if (attr != NULL)
10458 {
10459 struct type *type;
10460 sect_offset sect_off = attr->get_ref_die_offset ();
10461
10462 type = get_die_type_at_offset (sect_off, cu->per_cu);
10463 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10464 {
10465 /* This declaration is a global namespace alias. Add
10466 a symbol for it whose type is the aliased namespace. */
10467 new_symbol (die, type, cu);
10468 return 1;
10469 }
10470 }
10471 }
10472
10473 return 0;
10474 }
10475
10476 /* Return the using directives repository (global or local?) to use in the
10477 current context for CU.
10478
10479 For Ada, imported declarations can materialize renamings, which *may* be
10480 global. However it is impossible (for now?) in DWARF to distinguish
10481 "external" imported declarations and "static" ones. As all imported
10482 declarations seem to be static in all other languages, make them all CU-wide
10483 global only in Ada. */
10484
10485 static struct using_direct **
10486 using_directives (struct dwarf2_cu *cu)
10487 {
10488 if (cu->language == language_ada
10489 && cu->get_builder ()->outermost_context_p ())
10490 return cu->get_builder ()->get_global_using_directives ();
10491 else
10492 return cu->get_builder ()->get_local_using_directives ();
10493 }
10494
10495 /* Read the import statement specified by the given die and record it. */
10496
10497 static void
10498 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10499 {
10500 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10501 struct attribute *import_attr;
10502 struct die_info *imported_die, *child_die;
10503 struct dwarf2_cu *imported_cu;
10504 const char *imported_name;
10505 const char *imported_name_prefix;
10506 const char *canonical_name;
10507 const char *import_alias;
10508 const char *imported_declaration = NULL;
10509 const char *import_prefix;
10510 std::vector<const char *> excludes;
10511
10512 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10513 if (import_attr == NULL)
10514 {
10515 complaint (_("Tag '%s' has no DW_AT_import"),
10516 dwarf_tag_name (die->tag));
10517 return;
10518 }
10519
10520 imported_cu = cu;
10521 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10522 imported_name = dwarf2_name (imported_die, imported_cu);
10523 if (imported_name == NULL)
10524 {
10525 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10526
10527 The import in the following code:
10528 namespace A
10529 {
10530 typedef int B;
10531 }
10532
10533 int main ()
10534 {
10535 using A::B;
10536 B b;
10537 return b;
10538 }
10539
10540 ...
10541 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10542 <52> DW_AT_decl_file : 1
10543 <53> DW_AT_decl_line : 6
10544 <54> DW_AT_import : <0x75>
10545 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10546 <59> DW_AT_name : B
10547 <5b> DW_AT_decl_file : 1
10548 <5c> DW_AT_decl_line : 2
10549 <5d> DW_AT_type : <0x6e>
10550 ...
10551 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10552 <76> DW_AT_byte_size : 4
10553 <77> DW_AT_encoding : 5 (signed)
10554
10555 imports the wrong die ( 0x75 instead of 0x58 ).
10556 This case will be ignored until the gcc bug is fixed. */
10557 return;
10558 }
10559
10560 /* Figure out the local name after import. */
10561 import_alias = dwarf2_name (die, cu);
10562
10563 /* Figure out where the statement is being imported to. */
10564 import_prefix = determine_prefix (die, cu);
10565
10566 /* Figure out what the scope of the imported die is and prepend it
10567 to the name of the imported die. */
10568 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10569
10570 if (imported_die->tag != DW_TAG_namespace
10571 && imported_die->tag != DW_TAG_module)
10572 {
10573 imported_declaration = imported_name;
10574 canonical_name = imported_name_prefix;
10575 }
10576 else if (strlen (imported_name_prefix) > 0)
10577 canonical_name = obconcat (&objfile->objfile_obstack,
10578 imported_name_prefix,
10579 (cu->language == language_d ? "." : "::"),
10580 imported_name, (char *) NULL);
10581 else
10582 canonical_name = imported_name;
10583
10584 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10585 for (child_die = die->child; child_die && child_die->tag;
10586 child_die = child_die->sibling)
10587 {
10588 /* DWARF-4: A Fortran use statement with a “rename list” may be
10589 represented by an imported module entry with an import attribute
10590 referring to the module and owned entries corresponding to those
10591 entities that are renamed as part of being imported. */
10592
10593 if (child_die->tag != DW_TAG_imported_declaration)
10594 {
10595 complaint (_("child DW_TAG_imported_declaration expected "
10596 "- DIE at %s [in module %s]"),
10597 sect_offset_str (child_die->sect_off),
10598 objfile_name (objfile));
10599 continue;
10600 }
10601
10602 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10603 if (import_attr == NULL)
10604 {
10605 complaint (_("Tag '%s' has no DW_AT_import"),
10606 dwarf_tag_name (child_die->tag));
10607 continue;
10608 }
10609
10610 imported_cu = cu;
10611 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10612 &imported_cu);
10613 imported_name = dwarf2_name (imported_die, imported_cu);
10614 if (imported_name == NULL)
10615 {
10616 complaint (_("child DW_TAG_imported_declaration has unknown "
10617 "imported name - DIE at %s [in module %s]"),
10618 sect_offset_str (child_die->sect_off),
10619 objfile_name (objfile));
10620 continue;
10621 }
10622
10623 excludes.push_back (imported_name);
10624
10625 process_die (child_die, cu);
10626 }
10627
10628 add_using_directive (using_directives (cu),
10629 import_prefix,
10630 canonical_name,
10631 import_alias,
10632 imported_declaration,
10633 excludes,
10634 0,
10635 &objfile->objfile_obstack);
10636 }
10637
10638 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10639 types, but gives them a size of zero. Starting with version 14,
10640 ICC is compatible with GCC. */
10641
10642 static bool
10643 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10644 {
10645 if (!cu->checked_producer)
10646 check_producer (cu);
10647
10648 return cu->producer_is_icc_lt_14;
10649 }
10650
10651 /* ICC generates a DW_AT_type for C void functions. This was observed on
10652 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10653 which says that void functions should not have a DW_AT_type. */
10654
10655 static bool
10656 producer_is_icc (struct dwarf2_cu *cu)
10657 {
10658 if (!cu->checked_producer)
10659 check_producer (cu);
10660
10661 return cu->producer_is_icc;
10662 }
10663
10664 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10665 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10666 this, it was first present in GCC release 4.3.0. */
10667
10668 static bool
10669 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10670 {
10671 if (!cu->checked_producer)
10672 check_producer (cu);
10673
10674 return cu->producer_is_gcc_lt_4_3;
10675 }
10676
10677 static file_and_directory
10678 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10679 {
10680 file_and_directory res;
10681
10682 /* Find the filename. Do not use dwarf2_name here, since the filename
10683 is not a source language identifier. */
10684 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10685 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10686
10687 if (res.comp_dir == NULL
10688 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10689 && IS_ABSOLUTE_PATH (res.name))
10690 {
10691 res.comp_dir_storage = ldirname (res.name);
10692 if (!res.comp_dir_storage.empty ())
10693 res.comp_dir = res.comp_dir_storage.c_str ();
10694 }
10695 if (res.comp_dir != NULL)
10696 {
10697 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10698 directory, get rid of it. */
10699 const char *cp = strchr (res.comp_dir, ':');
10700
10701 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10702 res.comp_dir = cp + 1;
10703 }
10704
10705 if (res.name == NULL)
10706 res.name = "<unknown>";
10707
10708 return res;
10709 }
10710
10711 /* Handle DW_AT_stmt_list for a compilation unit.
10712 DIE is the DW_TAG_compile_unit die for CU.
10713 COMP_DIR is the compilation directory. LOWPC is passed to
10714 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10715
10716 static void
10717 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10718 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10719 {
10720 struct dwarf2_per_objfile *dwarf2_per_objfile
10721 = cu->per_cu->dwarf2_per_objfile;
10722 struct attribute *attr;
10723 struct line_header line_header_local;
10724 hashval_t line_header_local_hash;
10725 void **slot;
10726 int decode_mapping;
10727
10728 gdb_assert (! cu->per_cu->is_debug_types);
10729
10730 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10731 if (attr == NULL)
10732 return;
10733
10734 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10735
10736 /* The line header hash table is only created if needed (it exists to
10737 prevent redundant reading of the line table for partial_units).
10738 If we're given a partial_unit, we'll need it. If we're given a
10739 compile_unit, then use the line header hash table if it's already
10740 created, but don't create one just yet. */
10741
10742 if (dwarf2_per_objfile->line_header_hash == NULL
10743 && die->tag == DW_TAG_partial_unit)
10744 {
10745 dwarf2_per_objfile->line_header_hash
10746 .reset (htab_create_alloc (127, line_header_hash_voidp,
10747 line_header_eq_voidp,
10748 free_line_header_voidp,
10749 xcalloc, xfree));
10750 }
10751
10752 line_header_local.sect_off = line_offset;
10753 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10754 line_header_local_hash = line_header_hash (&line_header_local);
10755 if (dwarf2_per_objfile->line_header_hash != NULL)
10756 {
10757 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10758 &line_header_local,
10759 line_header_local_hash, NO_INSERT);
10760
10761 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10762 is not present in *SLOT (since if there is something in *SLOT then
10763 it will be for a partial_unit). */
10764 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10765 {
10766 gdb_assert (*slot != NULL);
10767 cu->line_header = (struct line_header *) *slot;
10768 return;
10769 }
10770 }
10771
10772 /* dwarf_decode_line_header does not yet provide sufficient information.
10773 We always have to call also dwarf_decode_lines for it. */
10774 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10775 if (lh == NULL)
10776 return;
10777
10778 cu->line_header = lh.release ();
10779 cu->line_header_die_owner = die;
10780
10781 if (dwarf2_per_objfile->line_header_hash == NULL)
10782 slot = NULL;
10783 else
10784 {
10785 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10786 &line_header_local,
10787 line_header_local_hash, INSERT);
10788 gdb_assert (slot != NULL);
10789 }
10790 if (slot != NULL && *slot == NULL)
10791 {
10792 /* This newly decoded line number information unit will be owned
10793 by line_header_hash hash table. */
10794 *slot = cu->line_header;
10795 cu->line_header_die_owner = NULL;
10796 }
10797 else
10798 {
10799 /* We cannot free any current entry in (*slot) as that struct line_header
10800 may be already used by multiple CUs. Create only temporary decoded
10801 line_header for this CU - it may happen at most once for each line
10802 number information unit. And if we're not using line_header_hash
10803 then this is what we want as well. */
10804 gdb_assert (die->tag != DW_TAG_partial_unit);
10805 }
10806 decode_mapping = (die->tag != DW_TAG_partial_unit);
10807 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10808 decode_mapping);
10809
10810 }
10811
10812 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10813
10814 static void
10815 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10816 {
10817 struct dwarf2_per_objfile *dwarf2_per_objfile
10818 = cu->per_cu->dwarf2_per_objfile;
10819 struct objfile *objfile = dwarf2_per_objfile->objfile;
10820 struct gdbarch *gdbarch = objfile->arch ();
10821 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10822 CORE_ADDR highpc = ((CORE_ADDR) 0);
10823 struct attribute *attr;
10824 struct die_info *child_die;
10825 CORE_ADDR baseaddr;
10826
10827 prepare_one_comp_unit (cu, die, cu->language);
10828 baseaddr = objfile->text_section_offset ();
10829
10830 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10831
10832 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10833 from finish_block. */
10834 if (lowpc == ((CORE_ADDR) -1))
10835 lowpc = highpc;
10836 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10837
10838 file_and_directory fnd = find_file_and_directory (die, cu);
10839
10840 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10841 standardised yet. As a workaround for the language detection we fall
10842 back to the DW_AT_producer string. */
10843 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10844 cu->language = language_opencl;
10845
10846 /* Similar hack for Go. */
10847 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10848 set_cu_language (DW_LANG_Go, cu);
10849
10850 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10851
10852 /* Decode line number information if present. We do this before
10853 processing child DIEs, so that the line header table is available
10854 for DW_AT_decl_file. */
10855 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10856
10857 /* Process all dies in compilation unit. */
10858 if (die->child != NULL)
10859 {
10860 child_die = die->child;
10861 while (child_die && child_die->tag)
10862 {
10863 process_die (child_die, cu);
10864 child_die = child_die->sibling;
10865 }
10866 }
10867
10868 /* Decode macro information, if present. Dwarf 2 macro information
10869 refers to information in the line number info statement program
10870 header, so we can only read it if we've read the header
10871 successfully. */
10872 attr = dwarf2_attr (die, DW_AT_macros, cu);
10873 if (attr == NULL)
10874 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10875 if (attr && cu->line_header)
10876 {
10877 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10878 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10879
10880 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10881 }
10882 else
10883 {
10884 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10885 if (attr && cu->line_header)
10886 {
10887 unsigned int macro_offset = DW_UNSND (attr);
10888
10889 dwarf_decode_macros (cu, macro_offset, 0);
10890 }
10891 }
10892 }
10893
10894 void
10895 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10896 {
10897 struct type_unit_group *tu_group;
10898 int first_time;
10899 struct attribute *attr;
10900 unsigned int i;
10901 struct signatured_type *sig_type;
10902
10903 gdb_assert (per_cu->is_debug_types);
10904 sig_type = (struct signatured_type *) per_cu;
10905
10906 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10907
10908 /* If we're using .gdb_index (includes -readnow) then
10909 per_cu->type_unit_group may not have been set up yet. */
10910 if (sig_type->type_unit_group == NULL)
10911 sig_type->type_unit_group = get_type_unit_group (this, attr);
10912 tu_group = sig_type->type_unit_group;
10913
10914 /* If we've already processed this stmt_list there's no real need to
10915 do it again, we could fake it and just recreate the part we need
10916 (file name,index -> symtab mapping). If data shows this optimization
10917 is useful we can do it then. */
10918 first_time = tu_group->compunit_symtab == NULL;
10919
10920 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10921 debug info. */
10922 line_header_up lh;
10923 if (attr != NULL)
10924 {
10925 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10926 lh = dwarf_decode_line_header (line_offset, this);
10927 }
10928 if (lh == NULL)
10929 {
10930 if (first_time)
10931 start_symtab ("", NULL, 0);
10932 else
10933 {
10934 gdb_assert (tu_group->symtabs == NULL);
10935 gdb_assert (m_builder == nullptr);
10936 struct compunit_symtab *cust = tu_group->compunit_symtab;
10937 m_builder.reset (new struct buildsym_compunit
10938 (COMPUNIT_OBJFILE (cust), "",
10939 COMPUNIT_DIRNAME (cust),
10940 compunit_language (cust),
10941 0, cust));
10942 list_in_scope = get_builder ()->get_file_symbols ();
10943 }
10944 return;
10945 }
10946
10947 line_header = lh.release ();
10948 line_header_die_owner = die;
10949
10950 if (first_time)
10951 {
10952 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10953
10954 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10955 still initializing it, and our caller (a few levels up)
10956 process_full_type_unit still needs to know if this is the first
10957 time. */
10958
10959 tu_group->symtabs
10960 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10961 struct symtab *, line_header->file_names_size ());
10962
10963 auto &file_names = line_header->file_names ();
10964 for (i = 0; i < file_names.size (); ++i)
10965 {
10966 file_entry &fe = file_names[i];
10967 dwarf2_start_subfile (this, fe.name,
10968 fe.include_dir (line_header));
10969 buildsym_compunit *b = get_builder ();
10970 if (b->get_current_subfile ()->symtab == NULL)
10971 {
10972 /* NOTE: start_subfile will recognize when it's been
10973 passed a file it has already seen. So we can't
10974 assume there's a simple mapping from
10975 cu->line_header->file_names to subfiles, plus
10976 cu->line_header->file_names may contain dups. */
10977 b->get_current_subfile ()->symtab
10978 = allocate_symtab (cust, b->get_current_subfile ()->name);
10979 }
10980
10981 fe.symtab = b->get_current_subfile ()->symtab;
10982 tu_group->symtabs[i] = fe.symtab;
10983 }
10984 }
10985 else
10986 {
10987 gdb_assert (m_builder == nullptr);
10988 struct compunit_symtab *cust = tu_group->compunit_symtab;
10989 m_builder.reset (new struct buildsym_compunit
10990 (COMPUNIT_OBJFILE (cust), "",
10991 COMPUNIT_DIRNAME (cust),
10992 compunit_language (cust),
10993 0, cust));
10994 list_in_scope = get_builder ()->get_file_symbols ();
10995
10996 auto &file_names = line_header->file_names ();
10997 for (i = 0; i < file_names.size (); ++i)
10998 {
10999 file_entry &fe = file_names[i];
11000 fe.symtab = tu_group->symtabs[i];
11001 }
11002 }
11003
11004 /* The main symtab is allocated last. Type units don't have DW_AT_name
11005 so they don't have a "real" (so to speak) symtab anyway.
11006 There is later code that will assign the main symtab to all symbols
11007 that don't have one. We need to handle the case of a symbol with a
11008 missing symtab (DW_AT_decl_file) anyway. */
11009 }
11010
11011 /* Process DW_TAG_type_unit.
11012 For TUs we want to skip the first top level sibling if it's not the
11013 actual type being defined by this TU. In this case the first top
11014 level sibling is there to provide context only. */
11015
11016 static void
11017 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11018 {
11019 struct die_info *child_die;
11020
11021 prepare_one_comp_unit (cu, die, language_minimal);
11022
11023 /* Initialize (or reinitialize) the machinery for building symtabs.
11024 We do this before processing child DIEs, so that the line header table
11025 is available for DW_AT_decl_file. */
11026 cu->setup_type_unit_groups (die);
11027
11028 if (die->child != NULL)
11029 {
11030 child_die = die->child;
11031 while (child_die && child_die->tag)
11032 {
11033 process_die (child_die, cu);
11034 child_die = child_die->sibling;
11035 }
11036 }
11037 }
11038 \f
11039 /* DWO/DWP files.
11040
11041 http://gcc.gnu.org/wiki/DebugFission
11042 http://gcc.gnu.org/wiki/DebugFissionDWP
11043
11044 To simplify handling of both DWO files ("object" files with the DWARF info)
11045 and DWP files (a file with the DWOs packaged up into one file), we treat
11046 DWP files as having a collection of virtual DWO files. */
11047
11048 static hashval_t
11049 hash_dwo_file (const void *item)
11050 {
11051 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11052 hashval_t hash;
11053
11054 hash = htab_hash_string (dwo_file->dwo_name);
11055 if (dwo_file->comp_dir != NULL)
11056 hash += htab_hash_string (dwo_file->comp_dir);
11057 return hash;
11058 }
11059
11060 static int
11061 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11062 {
11063 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11064 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11065
11066 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11067 return 0;
11068 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11069 return lhs->comp_dir == rhs->comp_dir;
11070 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11071 }
11072
11073 /* Allocate a hash table for DWO files. */
11074
11075 static htab_up
11076 allocate_dwo_file_hash_table ()
11077 {
11078 auto delete_dwo_file = [] (void *item)
11079 {
11080 struct dwo_file *dwo_file = (struct dwo_file *) item;
11081
11082 delete dwo_file;
11083 };
11084
11085 return htab_up (htab_create_alloc (41,
11086 hash_dwo_file,
11087 eq_dwo_file,
11088 delete_dwo_file,
11089 xcalloc, xfree));
11090 }
11091
11092 /* Lookup DWO file DWO_NAME. */
11093
11094 static void **
11095 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11096 const char *dwo_name,
11097 const char *comp_dir)
11098 {
11099 struct dwo_file find_entry;
11100 void **slot;
11101
11102 if (dwarf2_per_objfile->dwo_files == NULL)
11103 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11104
11105 find_entry.dwo_name = dwo_name;
11106 find_entry.comp_dir = comp_dir;
11107 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11108 INSERT);
11109
11110 return slot;
11111 }
11112
11113 static hashval_t
11114 hash_dwo_unit (const void *item)
11115 {
11116 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11117
11118 /* This drops the top 32 bits of the id, but is ok for a hash. */
11119 return dwo_unit->signature;
11120 }
11121
11122 static int
11123 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11124 {
11125 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11126 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11127
11128 /* The signature is assumed to be unique within the DWO file.
11129 So while object file CU dwo_id's always have the value zero,
11130 that's OK, assuming each object file DWO file has only one CU,
11131 and that's the rule for now. */
11132 return lhs->signature == rhs->signature;
11133 }
11134
11135 /* Allocate a hash table for DWO CUs,TUs.
11136 There is one of these tables for each of CUs,TUs for each DWO file. */
11137
11138 static htab_up
11139 allocate_dwo_unit_table ()
11140 {
11141 /* Start out with a pretty small number.
11142 Generally DWO files contain only one CU and maybe some TUs. */
11143 return htab_up (htab_create_alloc (3,
11144 hash_dwo_unit,
11145 eq_dwo_unit,
11146 NULL, xcalloc, xfree));
11147 }
11148
11149 /* die_reader_func for create_dwo_cu. */
11150
11151 static void
11152 create_dwo_cu_reader (const struct die_reader_specs *reader,
11153 const gdb_byte *info_ptr,
11154 struct die_info *comp_unit_die,
11155 struct dwo_file *dwo_file,
11156 struct dwo_unit *dwo_unit)
11157 {
11158 struct dwarf2_cu *cu = reader->cu;
11159 sect_offset sect_off = cu->per_cu->sect_off;
11160 struct dwarf2_section_info *section = cu->per_cu->section;
11161
11162 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11163 if (!signature.has_value ())
11164 {
11165 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11166 " its dwo_id [in module %s]"),
11167 sect_offset_str (sect_off), dwo_file->dwo_name);
11168 return;
11169 }
11170
11171 dwo_unit->dwo_file = dwo_file;
11172 dwo_unit->signature = *signature;
11173 dwo_unit->section = section;
11174 dwo_unit->sect_off = sect_off;
11175 dwo_unit->length = cu->per_cu->length;
11176
11177 if (dwarf_read_debug)
11178 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11179 sect_offset_str (sect_off),
11180 hex_string (dwo_unit->signature));
11181 }
11182
11183 /* Create the dwo_units for the CUs in a DWO_FILE.
11184 Note: This function processes DWO files only, not DWP files. */
11185
11186 static void
11187 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11188 dwarf2_cu *cu, struct dwo_file &dwo_file,
11189 dwarf2_section_info &section, htab_up &cus_htab)
11190 {
11191 struct objfile *objfile = dwarf2_per_objfile->objfile;
11192 const gdb_byte *info_ptr, *end_ptr;
11193
11194 section.read (objfile);
11195 info_ptr = section.buffer;
11196
11197 if (info_ptr == NULL)
11198 return;
11199
11200 if (dwarf_read_debug)
11201 {
11202 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11203 section.get_name (),
11204 section.get_file_name ());
11205 }
11206
11207 end_ptr = info_ptr + section.size;
11208 while (info_ptr < end_ptr)
11209 {
11210 struct dwarf2_per_cu_data per_cu;
11211 struct dwo_unit read_unit {};
11212 struct dwo_unit *dwo_unit;
11213 void **slot;
11214 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11215
11216 memset (&per_cu, 0, sizeof (per_cu));
11217 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11218 per_cu.is_debug_types = 0;
11219 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11220 per_cu.section = &section;
11221
11222 cutu_reader reader (&per_cu, cu, &dwo_file);
11223 if (!reader.dummy_p)
11224 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11225 &dwo_file, &read_unit);
11226 info_ptr += per_cu.length;
11227
11228 // If the unit could not be parsed, skip it.
11229 if (read_unit.dwo_file == NULL)
11230 continue;
11231
11232 if (cus_htab == NULL)
11233 cus_htab = allocate_dwo_unit_table ();
11234
11235 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11236 *dwo_unit = read_unit;
11237 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11238 gdb_assert (slot != NULL);
11239 if (*slot != NULL)
11240 {
11241 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11242 sect_offset dup_sect_off = dup_cu->sect_off;
11243
11244 complaint (_("debug cu entry at offset %s is duplicate to"
11245 " the entry at offset %s, signature %s"),
11246 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11247 hex_string (dwo_unit->signature));
11248 }
11249 *slot = (void *)dwo_unit;
11250 }
11251 }
11252
11253 /* DWP file .debug_{cu,tu}_index section format:
11254 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11255
11256 DWP Version 1:
11257
11258 Both index sections have the same format, and serve to map a 64-bit
11259 signature to a set of section numbers. Each section begins with a header,
11260 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11261 indexes, and a pool of 32-bit section numbers. The index sections will be
11262 aligned at 8-byte boundaries in the file.
11263
11264 The index section header consists of:
11265
11266 V, 32 bit version number
11267 -, 32 bits unused
11268 N, 32 bit number of compilation units or type units in the index
11269 M, 32 bit number of slots in the hash table
11270
11271 Numbers are recorded using the byte order of the application binary.
11272
11273 The hash table begins at offset 16 in the section, and consists of an array
11274 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11275 order of the application binary). Unused slots in the hash table are 0.
11276 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11277
11278 The parallel table begins immediately after the hash table
11279 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11280 array of 32-bit indexes (using the byte order of the application binary),
11281 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11282 table contains a 32-bit index into the pool of section numbers. For unused
11283 hash table slots, the corresponding entry in the parallel table will be 0.
11284
11285 The pool of section numbers begins immediately following the hash table
11286 (at offset 16 + 12 * M from the beginning of the section). The pool of
11287 section numbers consists of an array of 32-bit words (using the byte order
11288 of the application binary). Each item in the array is indexed starting
11289 from 0. The hash table entry provides the index of the first section
11290 number in the set. Additional section numbers in the set follow, and the
11291 set is terminated by a 0 entry (section number 0 is not used in ELF).
11292
11293 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11294 section must be the first entry in the set, and the .debug_abbrev.dwo must
11295 be the second entry. Other members of the set may follow in any order.
11296
11297 ---
11298
11299 DWP Version 2:
11300
11301 DWP Version 2 combines all the .debug_info, etc. sections into one,
11302 and the entries in the index tables are now offsets into these sections.
11303 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11304 section.
11305
11306 Index Section Contents:
11307 Header
11308 Hash Table of Signatures dwp_hash_table.hash_table
11309 Parallel Table of Indices dwp_hash_table.unit_table
11310 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11311 Table of Section Sizes dwp_hash_table.v2.sizes
11312
11313 The index section header consists of:
11314
11315 V, 32 bit version number
11316 L, 32 bit number of columns in the table of section offsets
11317 N, 32 bit number of compilation units or type units in the index
11318 M, 32 bit number of slots in the hash table
11319
11320 Numbers are recorded using the byte order of the application binary.
11321
11322 The hash table has the same format as version 1.
11323 The parallel table of indices has the same format as version 1,
11324 except that the entries are origin-1 indices into the table of sections
11325 offsets and the table of section sizes.
11326
11327 The table of offsets begins immediately following the parallel table
11328 (at offset 16 + 12 * M from the beginning of the section). The table is
11329 a two-dimensional array of 32-bit words (using the byte order of the
11330 application binary), with L columns and N+1 rows, in row-major order.
11331 Each row in the array is indexed starting from 0. The first row provides
11332 a key to the remaining rows: each column in this row provides an identifier
11333 for a debug section, and the offsets in the same column of subsequent rows
11334 refer to that section. The section identifiers are:
11335
11336 DW_SECT_INFO 1 .debug_info.dwo
11337 DW_SECT_TYPES 2 .debug_types.dwo
11338 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11339 DW_SECT_LINE 4 .debug_line.dwo
11340 DW_SECT_LOC 5 .debug_loc.dwo
11341 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11342 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11343 DW_SECT_MACRO 8 .debug_macro.dwo
11344
11345 The offsets provided by the CU and TU index sections are the base offsets
11346 for the contributions made by each CU or TU to the corresponding section
11347 in the package file. Each CU and TU header contains an abbrev_offset
11348 field, used to find the abbreviations table for that CU or TU within the
11349 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11350 be interpreted as relative to the base offset given in the index section.
11351 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11352 should be interpreted as relative to the base offset for .debug_line.dwo,
11353 and offsets into other debug sections obtained from DWARF attributes should
11354 also be interpreted as relative to the corresponding base offset.
11355
11356 The table of sizes begins immediately following the table of offsets.
11357 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11358 with L columns and N rows, in row-major order. Each row in the array is
11359 indexed starting from 1 (row 0 is shared by the two tables).
11360
11361 ---
11362
11363 Hash table lookup is handled the same in version 1 and 2:
11364
11365 We assume that N and M will not exceed 2^32 - 1.
11366 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11367
11368 Given a 64-bit compilation unit signature or a type signature S, an entry
11369 in the hash table is located as follows:
11370
11371 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11372 the low-order k bits all set to 1.
11373
11374 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11375
11376 3) If the hash table entry at index H matches the signature, use that
11377 entry. If the hash table entry at index H is unused (all zeroes),
11378 terminate the search: the signature is not present in the table.
11379
11380 4) Let H = (H + H') modulo M. Repeat at Step 3.
11381
11382 Because M > N and H' and M are relatively prime, the search is guaranteed
11383 to stop at an unused slot or find the match. */
11384
11385 /* Create a hash table to map DWO IDs to their CU/TU entry in
11386 .debug_{info,types}.dwo in DWP_FILE.
11387 Returns NULL if there isn't one.
11388 Note: This function processes DWP files only, not DWO files. */
11389
11390 static struct dwp_hash_table *
11391 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11392 struct dwp_file *dwp_file, int is_debug_types)
11393 {
11394 struct objfile *objfile = dwarf2_per_objfile->objfile;
11395 bfd *dbfd = dwp_file->dbfd.get ();
11396 const gdb_byte *index_ptr, *index_end;
11397 struct dwarf2_section_info *index;
11398 uint32_t version, nr_columns, nr_units, nr_slots;
11399 struct dwp_hash_table *htab;
11400
11401 if (is_debug_types)
11402 index = &dwp_file->sections.tu_index;
11403 else
11404 index = &dwp_file->sections.cu_index;
11405
11406 if (index->empty ())
11407 return NULL;
11408 index->read (objfile);
11409
11410 index_ptr = index->buffer;
11411 index_end = index_ptr + index->size;
11412
11413 version = read_4_bytes (dbfd, index_ptr);
11414 index_ptr += 4;
11415 if (version == 2)
11416 nr_columns = read_4_bytes (dbfd, index_ptr);
11417 else
11418 nr_columns = 0;
11419 index_ptr += 4;
11420 nr_units = read_4_bytes (dbfd, index_ptr);
11421 index_ptr += 4;
11422 nr_slots = read_4_bytes (dbfd, index_ptr);
11423 index_ptr += 4;
11424
11425 if (version != 1 && version != 2)
11426 {
11427 error (_("Dwarf Error: unsupported DWP file version (%s)"
11428 " [in module %s]"),
11429 pulongest (version), dwp_file->name);
11430 }
11431 if (nr_slots != (nr_slots & -nr_slots))
11432 {
11433 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11434 " is not power of 2 [in module %s]"),
11435 pulongest (nr_slots), dwp_file->name);
11436 }
11437
11438 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11439 htab->version = version;
11440 htab->nr_columns = nr_columns;
11441 htab->nr_units = nr_units;
11442 htab->nr_slots = nr_slots;
11443 htab->hash_table = index_ptr;
11444 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11445
11446 /* Exit early if the table is empty. */
11447 if (nr_slots == 0 || nr_units == 0
11448 || (version == 2 && nr_columns == 0))
11449 {
11450 /* All must be zero. */
11451 if (nr_slots != 0 || nr_units != 0
11452 || (version == 2 && nr_columns != 0))
11453 {
11454 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11455 " all zero [in modules %s]"),
11456 dwp_file->name);
11457 }
11458 return htab;
11459 }
11460
11461 if (version == 1)
11462 {
11463 htab->section_pool.v1.indices =
11464 htab->unit_table + sizeof (uint32_t) * nr_slots;
11465 /* It's harder to decide whether the section is too small in v1.
11466 V1 is deprecated anyway so we punt. */
11467 }
11468 else
11469 {
11470 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11471 int *ids = htab->section_pool.v2.section_ids;
11472 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11473 /* Reverse map for error checking. */
11474 int ids_seen[DW_SECT_MAX + 1];
11475 int i;
11476
11477 if (nr_columns < 2)
11478 {
11479 error (_("Dwarf Error: bad DWP hash table, too few columns"
11480 " in section table [in module %s]"),
11481 dwp_file->name);
11482 }
11483 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11484 {
11485 error (_("Dwarf Error: bad DWP hash table, too many columns"
11486 " in section table [in module %s]"),
11487 dwp_file->name);
11488 }
11489 memset (ids, 255, sizeof_ids);
11490 memset (ids_seen, 255, sizeof (ids_seen));
11491 for (i = 0; i < nr_columns; ++i)
11492 {
11493 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11494
11495 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11496 {
11497 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11498 " in section table [in module %s]"),
11499 id, dwp_file->name);
11500 }
11501 if (ids_seen[id] != -1)
11502 {
11503 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11504 " id %d in section table [in module %s]"),
11505 id, dwp_file->name);
11506 }
11507 ids_seen[id] = i;
11508 ids[i] = id;
11509 }
11510 /* Must have exactly one info or types section. */
11511 if (((ids_seen[DW_SECT_INFO] != -1)
11512 + (ids_seen[DW_SECT_TYPES] != -1))
11513 != 1)
11514 {
11515 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11516 " DWO info/types section [in module %s]"),
11517 dwp_file->name);
11518 }
11519 /* Must have an abbrev section. */
11520 if (ids_seen[DW_SECT_ABBREV] == -1)
11521 {
11522 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11523 " section [in module %s]"),
11524 dwp_file->name);
11525 }
11526 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11527 htab->section_pool.v2.sizes =
11528 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11529 * nr_units * nr_columns);
11530 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11531 * nr_units * nr_columns))
11532 > index_end)
11533 {
11534 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11535 " [in module %s]"),
11536 dwp_file->name);
11537 }
11538 }
11539
11540 return htab;
11541 }
11542
11543 /* Update SECTIONS with the data from SECTP.
11544
11545 This function is like the other "locate" section routines that are
11546 passed to bfd_map_over_sections, but in this context the sections to
11547 read comes from the DWP V1 hash table, not the full ELF section table.
11548
11549 The result is non-zero for success, or zero if an error was found. */
11550
11551 static int
11552 locate_v1_virtual_dwo_sections (asection *sectp,
11553 struct virtual_v1_dwo_sections *sections)
11554 {
11555 const struct dwop_section_names *names = &dwop_section_names;
11556
11557 if (section_is_p (sectp->name, &names->abbrev_dwo))
11558 {
11559 /* There can be only one. */
11560 if (sections->abbrev.s.section != NULL)
11561 return 0;
11562 sections->abbrev.s.section = sectp;
11563 sections->abbrev.size = bfd_section_size (sectp);
11564 }
11565 else if (section_is_p (sectp->name, &names->info_dwo)
11566 || section_is_p (sectp->name, &names->types_dwo))
11567 {
11568 /* There can be only one. */
11569 if (sections->info_or_types.s.section != NULL)
11570 return 0;
11571 sections->info_or_types.s.section = sectp;
11572 sections->info_or_types.size = bfd_section_size (sectp);
11573 }
11574 else if (section_is_p (sectp->name, &names->line_dwo))
11575 {
11576 /* There can be only one. */
11577 if (sections->line.s.section != NULL)
11578 return 0;
11579 sections->line.s.section = sectp;
11580 sections->line.size = bfd_section_size (sectp);
11581 }
11582 else if (section_is_p (sectp->name, &names->loc_dwo))
11583 {
11584 /* There can be only one. */
11585 if (sections->loc.s.section != NULL)
11586 return 0;
11587 sections->loc.s.section = sectp;
11588 sections->loc.size = bfd_section_size (sectp);
11589 }
11590 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11591 {
11592 /* There can be only one. */
11593 if (sections->macinfo.s.section != NULL)
11594 return 0;
11595 sections->macinfo.s.section = sectp;
11596 sections->macinfo.size = bfd_section_size (sectp);
11597 }
11598 else if (section_is_p (sectp->name, &names->macro_dwo))
11599 {
11600 /* There can be only one. */
11601 if (sections->macro.s.section != NULL)
11602 return 0;
11603 sections->macro.s.section = sectp;
11604 sections->macro.size = bfd_section_size (sectp);
11605 }
11606 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11607 {
11608 /* There can be only one. */
11609 if (sections->str_offsets.s.section != NULL)
11610 return 0;
11611 sections->str_offsets.s.section = sectp;
11612 sections->str_offsets.size = bfd_section_size (sectp);
11613 }
11614 else
11615 {
11616 /* No other kind of section is valid. */
11617 return 0;
11618 }
11619
11620 return 1;
11621 }
11622
11623 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11624 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11625 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11626 This is for DWP version 1 files. */
11627
11628 static struct dwo_unit *
11629 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11630 struct dwp_file *dwp_file,
11631 uint32_t unit_index,
11632 const char *comp_dir,
11633 ULONGEST signature, int is_debug_types)
11634 {
11635 struct objfile *objfile = dwarf2_per_objfile->objfile;
11636 const struct dwp_hash_table *dwp_htab =
11637 is_debug_types ? dwp_file->tus : dwp_file->cus;
11638 bfd *dbfd = dwp_file->dbfd.get ();
11639 const char *kind = is_debug_types ? "TU" : "CU";
11640 struct dwo_file *dwo_file;
11641 struct dwo_unit *dwo_unit;
11642 struct virtual_v1_dwo_sections sections;
11643 void **dwo_file_slot;
11644 int i;
11645
11646 gdb_assert (dwp_file->version == 1);
11647
11648 if (dwarf_read_debug)
11649 {
11650 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11651 kind,
11652 pulongest (unit_index), hex_string (signature),
11653 dwp_file->name);
11654 }
11655
11656 /* Fetch the sections of this DWO unit.
11657 Put a limit on the number of sections we look for so that bad data
11658 doesn't cause us to loop forever. */
11659
11660 #define MAX_NR_V1_DWO_SECTIONS \
11661 (1 /* .debug_info or .debug_types */ \
11662 + 1 /* .debug_abbrev */ \
11663 + 1 /* .debug_line */ \
11664 + 1 /* .debug_loc */ \
11665 + 1 /* .debug_str_offsets */ \
11666 + 1 /* .debug_macro or .debug_macinfo */ \
11667 + 1 /* trailing zero */)
11668
11669 memset (&sections, 0, sizeof (sections));
11670
11671 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11672 {
11673 asection *sectp;
11674 uint32_t section_nr =
11675 read_4_bytes (dbfd,
11676 dwp_htab->section_pool.v1.indices
11677 + (unit_index + i) * sizeof (uint32_t));
11678
11679 if (section_nr == 0)
11680 break;
11681 if (section_nr >= dwp_file->num_sections)
11682 {
11683 error (_("Dwarf Error: bad DWP hash table, section number too large"
11684 " [in module %s]"),
11685 dwp_file->name);
11686 }
11687
11688 sectp = dwp_file->elf_sections[section_nr];
11689 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11690 {
11691 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11692 " [in module %s]"),
11693 dwp_file->name);
11694 }
11695 }
11696
11697 if (i < 2
11698 || sections.info_or_types.empty ()
11699 || sections.abbrev.empty ())
11700 {
11701 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11702 " [in module %s]"),
11703 dwp_file->name);
11704 }
11705 if (i == MAX_NR_V1_DWO_SECTIONS)
11706 {
11707 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11708 " [in module %s]"),
11709 dwp_file->name);
11710 }
11711
11712 /* It's easier for the rest of the code if we fake a struct dwo_file and
11713 have dwo_unit "live" in that. At least for now.
11714
11715 The DWP file can be made up of a random collection of CUs and TUs.
11716 However, for each CU + set of TUs that came from the same original DWO
11717 file, we can combine them back into a virtual DWO file to save space
11718 (fewer struct dwo_file objects to allocate). Remember that for really
11719 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11720
11721 std::string virtual_dwo_name =
11722 string_printf ("virtual-dwo/%d-%d-%d-%d",
11723 sections.abbrev.get_id (),
11724 sections.line.get_id (),
11725 sections.loc.get_id (),
11726 sections.str_offsets.get_id ());
11727 /* Can we use an existing virtual DWO file? */
11728 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11729 virtual_dwo_name.c_str (),
11730 comp_dir);
11731 /* Create one if necessary. */
11732 if (*dwo_file_slot == NULL)
11733 {
11734 if (dwarf_read_debug)
11735 {
11736 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11737 virtual_dwo_name.c_str ());
11738 }
11739 dwo_file = new struct dwo_file;
11740 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11741 dwo_file->comp_dir = comp_dir;
11742 dwo_file->sections.abbrev = sections.abbrev;
11743 dwo_file->sections.line = sections.line;
11744 dwo_file->sections.loc = sections.loc;
11745 dwo_file->sections.macinfo = sections.macinfo;
11746 dwo_file->sections.macro = sections.macro;
11747 dwo_file->sections.str_offsets = sections.str_offsets;
11748 /* The "str" section is global to the entire DWP file. */
11749 dwo_file->sections.str = dwp_file->sections.str;
11750 /* The info or types section is assigned below to dwo_unit,
11751 there's no need to record it in dwo_file.
11752 Also, we can't simply record type sections in dwo_file because
11753 we record a pointer into the vector in dwo_unit. As we collect more
11754 types we'll grow the vector and eventually have to reallocate space
11755 for it, invalidating all copies of pointers into the previous
11756 contents. */
11757 *dwo_file_slot = dwo_file;
11758 }
11759 else
11760 {
11761 if (dwarf_read_debug)
11762 {
11763 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11764 virtual_dwo_name.c_str ());
11765 }
11766 dwo_file = (struct dwo_file *) *dwo_file_slot;
11767 }
11768
11769 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11770 dwo_unit->dwo_file = dwo_file;
11771 dwo_unit->signature = signature;
11772 dwo_unit->section =
11773 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11774 *dwo_unit->section = sections.info_or_types;
11775 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11776
11777 return dwo_unit;
11778 }
11779
11780 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11781 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11782 piece within that section used by a TU/CU, return a virtual section
11783 of just that piece. */
11784
11785 static struct dwarf2_section_info
11786 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11787 struct dwarf2_section_info *section,
11788 bfd_size_type offset, bfd_size_type size)
11789 {
11790 struct dwarf2_section_info result;
11791 asection *sectp;
11792
11793 gdb_assert (section != NULL);
11794 gdb_assert (!section->is_virtual);
11795
11796 memset (&result, 0, sizeof (result));
11797 result.s.containing_section = section;
11798 result.is_virtual = true;
11799
11800 if (size == 0)
11801 return result;
11802
11803 sectp = section->get_bfd_section ();
11804
11805 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11806 bounds of the real section. This is a pretty-rare event, so just
11807 flag an error (easier) instead of a warning and trying to cope. */
11808 if (sectp == NULL
11809 || offset + size > bfd_section_size (sectp))
11810 {
11811 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11812 " in section %s [in module %s]"),
11813 sectp ? bfd_section_name (sectp) : "<unknown>",
11814 objfile_name (dwarf2_per_objfile->objfile));
11815 }
11816
11817 result.virtual_offset = offset;
11818 result.size = size;
11819 return result;
11820 }
11821
11822 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11823 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11824 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11825 This is for DWP version 2 files. */
11826
11827 static struct dwo_unit *
11828 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11829 struct dwp_file *dwp_file,
11830 uint32_t unit_index,
11831 const char *comp_dir,
11832 ULONGEST signature, int is_debug_types)
11833 {
11834 struct objfile *objfile = dwarf2_per_objfile->objfile;
11835 const struct dwp_hash_table *dwp_htab =
11836 is_debug_types ? dwp_file->tus : dwp_file->cus;
11837 bfd *dbfd = dwp_file->dbfd.get ();
11838 const char *kind = is_debug_types ? "TU" : "CU";
11839 struct dwo_file *dwo_file;
11840 struct dwo_unit *dwo_unit;
11841 struct virtual_v2_dwo_sections sections;
11842 void **dwo_file_slot;
11843 int i;
11844
11845 gdb_assert (dwp_file->version == 2);
11846
11847 if (dwarf_read_debug)
11848 {
11849 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11850 kind,
11851 pulongest (unit_index), hex_string (signature),
11852 dwp_file->name);
11853 }
11854
11855 /* Fetch the section offsets of this DWO unit. */
11856
11857 memset (&sections, 0, sizeof (sections));
11858
11859 for (i = 0; i < dwp_htab->nr_columns; ++i)
11860 {
11861 uint32_t offset = read_4_bytes (dbfd,
11862 dwp_htab->section_pool.v2.offsets
11863 + (((unit_index - 1) * dwp_htab->nr_columns
11864 + i)
11865 * sizeof (uint32_t)));
11866 uint32_t size = read_4_bytes (dbfd,
11867 dwp_htab->section_pool.v2.sizes
11868 + (((unit_index - 1) * dwp_htab->nr_columns
11869 + i)
11870 * sizeof (uint32_t)));
11871
11872 switch (dwp_htab->section_pool.v2.section_ids[i])
11873 {
11874 case DW_SECT_INFO:
11875 case DW_SECT_TYPES:
11876 sections.info_or_types_offset = offset;
11877 sections.info_or_types_size = size;
11878 break;
11879 case DW_SECT_ABBREV:
11880 sections.abbrev_offset = offset;
11881 sections.abbrev_size = size;
11882 break;
11883 case DW_SECT_LINE:
11884 sections.line_offset = offset;
11885 sections.line_size = size;
11886 break;
11887 case DW_SECT_LOC:
11888 sections.loc_offset = offset;
11889 sections.loc_size = size;
11890 break;
11891 case DW_SECT_STR_OFFSETS:
11892 sections.str_offsets_offset = offset;
11893 sections.str_offsets_size = size;
11894 break;
11895 case DW_SECT_MACINFO:
11896 sections.macinfo_offset = offset;
11897 sections.macinfo_size = size;
11898 break;
11899 case DW_SECT_MACRO:
11900 sections.macro_offset = offset;
11901 sections.macro_size = size;
11902 break;
11903 }
11904 }
11905
11906 /* It's easier for the rest of the code if we fake a struct dwo_file and
11907 have dwo_unit "live" in that. At least for now.
11908
11909 The DWP file can be made up of a random collection of CUs and TUs.
11910 However, for each CU + set of TUs that came from the same original DWO
11911 file, we can combine them back into a virtual DWO file to save space
11912 (fewer struct dwo_file objects to allocate). Remember that for really
11913 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11914
11915 std::string virtual_dwo_name =
11916 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11917 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11918 (long) (sections.line_size ? sections.line_offset : 0),
11919 (long) (sections.loc_size ? sections.loc_offset : 0),
11920 (long) (sections.str_offsets_size
11921 ? sections.str_offsets_offset : 0));
11922 /* Can we use an existing virtual DWO file? */
11923 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11924 virtual_dwo_name.c_str (),
11925 comp_dir);
11926 /* Create one if necessary. */
11927 if (*dwo_file_slot == NULL)
11928 {
11929 if (dwarf_read_debug)
11930 {
11931 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11932 virtual_dwo_name.c_str ());
11933 }
11934 dwo_file = new struct dwo_file;
11935 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11936 dwo_file->comp_dir = comp_dir;
11937 dwo_file->sections.abbrev =
11938 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11939 sections.abbrev_offset, sections.abbrev_size);
11940 dwo_file->sections.line =
11941 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11942 sections.line_offset, sections.line_size);
11943 dwo_file->sections.loc =
11944 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11945 sections.loc_offset, sections.loc_size);
11946 dwo_file->sections.macinfo =
11947 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11948 sections.macinfo_offset, sections.macinfo_size);
11949 dwo_file->sections.macro =
11950 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11951 sections.macro_offset, sections.macro_size);
11952 dwo_file->sections.str_offsets =
11953 create_dwp_v2_section (dwarf2_per_objfile,
11954 &dwp_file->sections.str_offsets,
11955 sections.str_offsets_offset,
11956 sections.str_offsets_size);
11957 /* The "str" section is global to the entire DWP file. */
11958 dwo_file->sections.str = dwp_file->sections.str;
11959 /* The info or types section is assigned below to dwo_unit,
11960 there's no need to record it in dwo_file.
11961 Also, we can't simply record type sections in dwo_file because
11962 we record a pointer into the vector in dwo_unit. As we collect more
11963 types we'll grow the vector and eventually have to reallocate space
11964 for it, invalidating all copies of pointers into the previous
11965 contents. */
11966 *dwo_file_slot = dwo_file;
11967 }
11968 else
11969 {
11970 if (dwarf_read_debug)
11971 {
11972 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11973 virtual_dwo_name.c_str ());
11974 }
11975 dwo_file = (struct dwo_file *) *dwo_file_slot;
11976 }
11977
11978 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11979 dwo_unit->dwo_file = dwo_file;
11980 dwo_unit->signature = signature;
11981 dwo_unit->section =
11982 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11983 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11984 is_debug_types
11985 ? &dwp_file->sections.types
11986 : &dwp_file->sections.info,
11987 sections.info_or_types_offset,
11988 sections.info_or_types_size);
11989 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11990
11991 return dwo_unit;
11992 }
11993
11994 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11995 Returns NULL if the signature isn't found. */
11996
11997 static struct dwo_unit *
11998 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11999 struct dwp_file *dwp_file, const char *comp_dir,
12000 ULONGEST signature, int is_debug_types)
12001 {
12002 const struct dwp_hash_table *dwp_htab =
12003 is_debug_types ? dwp_file->tus : dwp_file->cus;
12004 bfd *dbfd = dwp_file->dbfd.get ();
12005 uint32_t mask = dwp_htab->nr_slots - 1;
12006 uint32_t hash = signature & mask;
12007 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12008 unsigned int i;
12009 void **slot;
12010 struct dwo_unit find_dwo_cu;
12011
12012 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12013 find_dwo_cu.signature = signature;
12014 slot = htab_find_slot (is_debug_types
12015 ? dwp_file->loaded_tus.get ()
12016 : dwp_file->loaded_cus.get (),
12017 &find_dwo_cu, INSERT);
12018
12019 if (*slot != NULL)
12020 return (struct dwo_unit *) *slot;
12021
12022 /* Use a for loop so that we don't loop forever on bad debug info. */
12023 for (i = 0; i < dwp_htab->nr_slots; ++i)
12024 {
12025 ULONGEST signature_in_table;
12026
12027 signature_in_table =
12028 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12029 if (signature_in_table == signature)
12030 {
12031 uint32_t unit_index =
12032 read_4_bytes (dbfd,
12033 dwp_htab->unit_table + hash * sizeof (uint32_t));
12034
12035 if (dwp_file->version == 1)
12036 {
12037 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12038 dwp_file, unit_index,
12039 comp_dir, signature,
12040 is_debug_types);
12041 }
12042 else
12043 {
12044 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12045 dwp_file, unit_index,
12046 comp_dir, signature,
12047 is_debug_types);
12048 }
12049 return (struct dwo_unit *) *slot;
12050 }
12051 if (signature_in_table == 0)
12052 return NULL;
12053 hash = (hash + hash2) & mask;
12054 }
12055
12056 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12057 " [in module %s]"),
12058 dwp_file->name);
12059 }
12060
12061 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12062 Open the file specified by FILE_NAME and hand it off to BFD for
12063 preliminary analysis. Return a newly initialized bfd *, which
12064 includes a canonicalized copy of FILE_NAME.
12065 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12066 SEARCH_CWD is true if the current directory is to be searched.
12067 It will be searched before debug-file-directory.
12068 If successful, the file is added to the bfd include table of the
12069 objfile's bfd (see gdb_bfd_record_inclusion).
12070 If unable to find/open the file, return NULL.
12071 NOTE: This function is derived from symfile_bfd_open. */
12072
12073 static gdb_bfd_ref_ptr
12074 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12075 const char *file_name, int is_dwp, int search_cwd)
12076 {
12077 int desc;
12078 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12079 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12080 to debug_file_directory. */
12081 const char *search_path;
12082 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12083
12084 gdb::unique_xmalloc_ptr<char> search_path_holder;
12085 if (search_cwd)
12086 {
12087 if (*debug_file_directory != '\0')
12088 {
12089 search_path_holder.reset (concat (".", dirname_separator_string,
12090 debug_file_directory,
12091 (char *) NULL));
12092 search_path = search_path_holder.get ();
12093 }
12094 else
12095 search_path = ".";
12096 }
12097 else
12098 search_path = debug_file_directory;
12099
12100 openp_flags flags = OPF_RETURN_REALPATH;
12101 if (is_dwp)
12102 flags |= OPF_SEARCH_IN_PATH;
12103
12104 gdb::unique_xmalloc_ptr<char> absolute_name;
12105 desc = openp (search_path, flags, file_name,
12106 O_RDONLY | O_BINARY, &absolute_name);
12107 if (desc < 0)
12108 return NULL;
12109
12110 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12111 gnutarget, desc));
12112 if (sym_bfd == NULL)
12113 return NULL;
12114 bfd_set_cacheable (sym_bfd.get (), 1);
12115
12116 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12117 return NULL;
12118
12119 /* Success. Record the bfd as having been included by the objfile's bfd.
12120 This is important because things like demangled_names_hash lives in the
12121 objfile's per_bfd space and may have references to things like symbol
12122 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12123 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12124
12125 return sym_bfd;
12126 }
12127
12128 /* Try to open DWO file FILE_NAME.
12129 COMP_DIR is the DW_AT_comp_dir attribute.
12130 The result is the bfd handle of the file.
12131 If there is a problem finding or opening the file, return NULL.
12132 Upon success, the canonicalized path of the file is stored in the bfd,
12133 same as symfile_bfd_open. */
12134
12135 static gdb_bfd_ref_ptr
12136 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12137 const char *file_name, const char *comp_dir)
12138 {
12139 if (IS_ABSOLUTE_PATH (file_name))
12140 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12141 0 /*is_dwp*/, 0 /*search_cwd*/);
12142
12143 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12144
12145 if (comp_dir != NULL)
12146 {
12147 gdb::unique_xmalloc_ptr<char> path_to_try
12148 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12149
12150 /* NOTE: If comp_dir is a relative path, this will also try the
12151 search path, which seems useful. */
12152 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12153 path_to_try.get (),
12154 0 /*is_dwp*/,
12155 1 /*search_cwd*/));
12156 if (abfd != NULL)
12157 return abfd;
12158 }
12159
12160 /* That didn't work, try debug-file-directory, which, despite its name,
12161 is a list of paths. */
12162
12163 if (*debug_file_directory == '\0')
12164 return NULL;
12165
12166 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12167 0 /*is_dwp*/, 1 /*search_cwd*/);
12168 }
12169
12170 /* This function is mapped across the sections and remembers the offset and
12171 size of each of the DWO debugging sections we are interested in. */
12172
12173 static void
12174 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12175 {
12176 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12177 const struct dwop_section_names *names = &dwop_section_names;
12178
12179 if (section_is_p (sectp->name, &names->abbrev_dwo))
12180 {
12181 dwo_sections->abbrev.s.section = sectp;
12182 dwo_sections->abbrev.size = bfd_section_size (sectp);
12183 }
12184 else if (section_is_p (sectp->name, &names->info_dwo))
12185 {
12186 dwo_sections->info.s.section = sectp;
12187 dwo_sections->info.size = bfd_section_size (sectp);
12188 }
12189 else if (section_is_p (sectp->name, &names->line_dwo))
12190 {
12191 dwo_sections->line.s.section = sectp;
12192 dwo_sections->line.size = bfd_section_size (sectp);
12193 }
12194 else if (section_is_p (sectp->name, &names->loc_dwo))
12195 {
12196 dwo_sections->loc.s.section = sectp;
12197 dwo_sections->loc.size = bfd_section_size (sectp);
12198 }
12199 else if (section_is_p (sectp->name, &names->loclists_dwo))
12200 {
12201 dwo_sections->loclists.s.section = sectp;
12202 dwo_sections->loclists.size = bfd_section_size (sectp);
12203 }
12204 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12205 {
12206 dwo_sections->macinfo.s.section = sectp;
12207 dwo_sections->macinfo.size = bfd_section_size (sectp);
12208 }
12209 else if (section_is_p (sectp->name, &names->macro_dwo))
12210 {
12211 dwo_sections->macro.s.section = sectp;
12212 dwo_sections->macro.size = bfd_section_size (sectp);
12213 }
12214 else if (section_is_p (sectp->name, &names->str_dwo))
12215 {
12216 dwo_sections->str.s.section = sectp;
12217 dwo_sections->str.size = bfd_section_size (sectp);
12218 }
12219 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12220 {
12221 dwo_sections->str_offsets.s.section = sectp;
12222 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12223 }
12224 else if (section_is_p (sectp->name, &names->types_dwo))
12225 {
12226 struct dwarf2_section_info type_section;
12227
12228 memset (&type_section, 0, sizeof (type_section));
12229 type_section.s.section = sectp;
12230 type_section.size = bfd_section_size (sectp);
12231 dwo_sections->types.push_back (type_section);
12232 }
12233 }
12234
12235 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12236 by PER_CU. This is for the non-DWP case.
12237 The result is NULL if DWO_NAME can't be found. */
12238
12239 static struct dwo_file *
12240 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12241 const char *dwo_name, const char *comp_dir)
12242 {
12243 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12244
12245 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12246 if (dbfd == NULL)
12247 {
12248 if (dwarf_read_debug)
12249 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12250 return NULL;
12251 }
12252
12253 dwo_file_up dwo_file (new struct dwo_file);
12254 dwo_file->dwo_name = dwo_name;
12255 dwo_file->comp_dir = comp_dir;
12256 dwo_file->dbfd = std::move (dbfd);
12257
12258 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12259 &dwo_file->sections);
12260
12261 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12262 dwo_file->sections.info, dwo_file->cus);
12263
12264 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12265 dwo_file->sections.types, dwo_file->tus);
12266
12267 if (dwarf_read_debug)
12268 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12269
12270 return dwo_file.release ();
12271 }
12272
12273 /* This function is mapped across the sections and remembers the offset and
12274 size of each of the DWP debugging sections common to version 1 and 2 that
12275 we are interested in. */
12276
12277 static void
12278 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12279 void *dwp_file_ptr)
12280 {
12281 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12282 const struct dwop_section_names *names = &dwop_section_names;
12283 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12284
12285 /* Record the ELF section number for later lookup: this is what the
12286 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12287 gdb_assert (elf_section_nr < dwp_file->num_sections);
12288 dwp_file->elf_sections[elf_section_nr] = sectp;
12289
12290 /* Look for specific sections that we need. */
12291 if (section_is_p (sectp->name, &names->str_dwo))
12292 {
12293 dwp_file->sections.str.s.section = sectp;
12294 dwp_file->sections.str.size = bfd_section_size (sectp);
12295 }
12296 else if (section_is_p (sectp->name, &names->cu_index))
12297 {
12298 dwp_file->sections.cu_index.s.section = sectp;
12299 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12300 }
12301 else if (section_is_p (sectp->name, &names->tu_index))
12302 {
12303 dwp_file->sections.tu_index.s.section = sectp;
12304 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12305 }
12306 }
12307
12308 /* This function is mapped across the sections and remembers the offset and
12309 size of each of the DWP version 2 debugging sections that we are interested
12310 in. This is split into a separate function because we don't know if we
12311 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12312
12313 static void
12314 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12315 {
12316 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12317 const struct dwop_section_names *names = &dwop_section_names;
12318 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12319
12320 /* Record the ELF section number for later lookup: this is what the
12321 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12322 gdb_assert (elf_section_nr < dwp_file->num_sections);
12323 dwp_file->elf_sections[elf_section_nr] = sectp;
12324
12325 /* Look for specific sections that we need. */
12326 if (section_is_p (sectp->name, &names->abbrev_dwo))
12327 {
12328 dwp_file->sections.abbrev.s.section = sectp;
12329 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12330 }
12331 else if (section_is_p (sectp->name, &names->info_dwo))
12332 {
12333 dwp_file->sections.info.s.section = sectp;
12334 dwp_file->sections.info.size = bfd_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->line_dwo))
12337 {
12338 dwp_file->sections.line.s.section = sectp;
12339 dwp_file->sections.line.size = bfd_section_size (sectp);
12340 }
12341 else if (section_is_p (sectp->name, &names->loc_dwo))
12342 {
12343 dwp_file->sections.loc.s.section = sectp;
12344 dwp_file->sections.loc.size = bfd_section_size (sectp);
12345 }
12346 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12347 {
12348 dwp_file->sections.macinfo.s.section = sectp;
12349 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12350 }
12351 else if (section_is_p (sectp->name, &names->macro_dwo))
12352 {
12353 dwp_file->sections.macro.s.section = sectp;
12354 dwp_file->sections.macro.size = bfd_section_size (sectp);
12355 }
12356 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12357 {
12358 dwp_file->sections.str_offsets.s.section = sectp;
12359 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12360 }
12361 else if (section_is_p (sectp->name, &names->types_dwo))
12362 {
12363 dwp_file->sections.types.s.section = sectp;
12364 dwp_file->sections.types.size = bfd_section_size (sectp);
12365 }
12366 }
12367
12368 /* Hash function for dwp_file loaded CUs/TUs. */
12369
12370 static hashval_t
12371 hash_dwp_loaded_cutus (const void *item)
12372 {
12373 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12374
12375 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12376 return dwo_unit->signature;
12377 }
12378
12379 /* Equality function for dwp_file loaded CUs/TUs. */
12380
12381 static int
12382 eq_dwp_loaded_cutus (const void *a, const void *b)
12383 {
12384 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12385 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12386
12387 return dua->signature == dub->signature;
12388 }
12389
12390 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12391
12392 static htab_up
12393 allocate_dwp_loaded_cutus_table ()
12394 {
12395 return htab_up (htab_create_alloc (3,
12396 hash_dwp_loaded_cutus,
12397 eq_dwp_loaded_cutus,
12398 NULL, xcalloc, xfree));
12399 }
12400
12401 /* Try to open DWP file FILE_NAME.
12402 The result is the bfd handle of the file.
12403 If there is a problem finding or opening the file, return NULL.
12404 Upon success, the canonicalized path of the file is stored in the bfd,
12405 same as symfile_bfd_open. */
12406
12407 static gdb_bfd_ref_ptr
12408 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12409 const char *file_name)
12410 {
12411 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12412 1 /*is_dwp*/,
12413 1 /*search_cwd*/));
12414 if (abfd != NULL)
12415 return abfd;
12416
12417 /* Work around upstream bug 15652.
12418 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12419 [Whether that's a "bug" is debatable, but it is getting in our way.]
12420 We have no real idea where the dwp file is, because gdb's realpath-ing
12421 of the executable's path may have discarded the needed info.
12422 [IWBN if the dwp file name was recorded in the executable, akin to
12423 .gnu_debuglink, but that doesn't exist yet.]
12424 Strip the directory from FILE_NAME and search again. */
12425 if (*debug_file_directory != '\0')
12426 {
12427 /* Don't implicitly search the current directory here.
12428 If the user wants to search "." to handle this case,
12429 it must be added to debug-file-directory. */
12430 return try_open_dwop_file (dwarf2_per_objfile,
12431 lbasename (file_name), 1 /*is_dwp*/,
12432 0 /*search_cwd*/);
12433 }
12434
12435 return NULL;
12436 }
12437
12438 /* Initialize the use of the DWP file for the current objfile.
12439 By convention the name of the DWP file is ${objfile}.dwp.
12440 The result is NULL if it can't be found. */
12441
12442 static std::unique_ptr<struct dwp_file>
12443 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12444 {
12445 struct objfile *objfile = dwarf2_per_objfile->objfile;
12446
12447 /* Try to find first .dwp for the binary file before any symbolic links
12448 resolving. */
12449
12450 /* If the objfile is a debug file, find the name of the real binary
12451 file and get the name of dwp file from there. */
12452 std::string dwp_name;
12453 if (objfile->separate_debug_objfile_backlink != NULL)
12454 {
12455 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12456 const char *backlink_basename = lbasename (backlink->original_name);
12457
12458 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12459 }
12460 else
12461 dwp_name = objfile->original_name;
12462
12463 dwp_name += ".dwp";
12464
12465 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12466 if (dbfd == NULL
12467 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12468 {
12469 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12470 dwp_name = objfile_name (objfile);
12471 dwp_name += ".dwp";
12472 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12473 }
12474
12475 if (dbfd == NULL)
12476 {
12477 if (dwarf_read_debug)
12478 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12479 return std::unique_ptr<dwp_file> ();
12480 }
12481
12482 const char *name = bfd_get_filename (dbfd.get ());
12483 std::unique_ptr<struct dwp_file> dwp_file
12484 (new struct dwp_file (name, std::move (dbfd)));
12485
12486 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12487 dwp_file->elf_sections =
12488 OBSTACK_CALLOC (&objfile->objfile_obstack,
12489 dwp_file->num_sections, asection *);
12490
12491 bfd_map_over_sections (dwp_file->dbfd.get (),
12492 dwarf2_locate_common_dwp_sections,
12493 dwp_file.get ());
12494
12495 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12496 0);
12497
12498 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12499 1);
12500
12501 /* The DWP file version is stored in the hash table. Oh well. */
12502 if (dwp_file->cus && dwp_file->tus
12503 && dwp_file->cus->version != dwp_file->tus->version)
12504 {
12505 /* Technically speaking, we should try to limp along, but this is
12506 pretty bizarre. We use pulongest here because that's the established
12507 portability solution (e.g, we cannot use %u for uint32_t). */
12508 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12509 " TU version %s [in DWP file %s]"),
12510 pulongest (dwp_file->cus->version),
12511 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12512 }
12513
12514 if (dwp_file->cus)
12515 dwp_file->version = dwp_file->cus->version;
12516 else if (dwp_file->tus)
12517 dwp_file->version = dwp_file->tus->version;
12518 else
12519 dwp_file->version = 2;
12520
12521 if (dwp_file->version == 2)
12522 bfd_map_over_sections (dwp_file->dbfd.get (),
12523 dwarf2_locate_v2_dwp_sections,
12524 dwp_file.get ());
12525
12526 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12527 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12528
12529 if (dwarf_read_debug)
12530 {
12531 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12532 fprintf_unfiltered (gdb_stdlog,
12533 " %s CUs, %s TUs\n",
12534 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12535 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12536 }
12537
12538 return dwp_file;
12539 }
12540
12541 /* Wrapper around open_and_init_dwp_file, only open it once. */
12542
12543 static struct dwp_file *
12544 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12545 {
12546 if (! dwarf2_per_objfile->dwp_checked)
12547 {
12548 dwarf2_per_objfile->dwp_file
12549 = open_and_init_dwp_file (dwarf2_per_objfile);
12550 dwarf2_per_objfile->dwp_checked = 1;
12551 }
12552 return dwarf2_per_objfile->dwp_file.get ();
12553 }
12554
12555 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12556 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12557 or in the DWP file for the objfile, referenced by THIS_UNIT.
12558 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12559 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12560
12561 This is called, for example, when wanting to read a variable with a
12562 complex location. Therefore we don't want to do file i/o for every call.
12563 Therefore we don't want to look for a DWO file on every call.
12564 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12565 then we check if we've already seen DWO_NAME, and only THEN do we check
12566 for a DWO file.
12567
12568 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12569 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12570
12571 static struct dwo_unit *
12572 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12573 const char *dwo_name, const char *comp_dir,
12574 ULONGEST signature, int is_debug_types)
12575 {
12576 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12577 struct objfile *objfile = dwarf2_per_objfile->objfile;
12578 const char *kind = is_debug_types ? "TU" : "CU";
12579 void **dwo_file_slot;
12580 struct dwo_file *dwo_file;
12581 struct dwp_file *dwp_file;
12582
12583 /* First see if there's a DWP file.
12584 If we have a DWP file but didn't find the DWO inside it, don't
12585 look for the original DWO file. It makes gdb behave differently
12586 depending on whether one is debugging in the build tree. */
12587
12588 dwp_file = get_dwp_file (dwarf2_per_objfile);
12589 if (dwp_file != NULL)
12590 {
12591 const struct dwp_hash_table *dwp_htab =
12592 is_debug_types ? dwp_file->tus : dwp_file->cus;
12593
12594 if (dwp_htab != NULL)
12595 {
12596 struct dwo_unit *dwo_cutu =
12597 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12598 signature, is_debug_types);
12599
12600 if (dwo_cutu != NULL)
12601 {
12602 if (dwarf_read_debug)
12603 {
12604 fprintf_unfiltered (gdb_stdlog,
12605 "Virtual DWO %s %s found: @%s\n",
12606 kind, hex_string (signature),
12607 host_address_to_string (dwo_cutu));
12608 }
12609 return dwo_cutu;
12610 }
12611 }
12612 }
12613 else
12614 {
12615 /* No DWP file, look for the DWO file. */
12616
12617 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12618 dwo_name, comp_dir);
12619 if (*dwo_file_slot == NULL)
12620 {
12621 /* Read in the file and build a table of the CUs/TUs it contains. */
12622 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12623 }
12624 /* NOTE: This will be NULL if unable to open the file. */
12625 dwo_file = (struct dwo_file *) *dwo_file_slot;
12626
12627 if (dwo_file != NULL)
12628 {
12629 struct dwo_unit *dwo_cutu = NULL;
12630
12631 if (is_debug_types && dwo_file->tus)
12632 {
12633 struct dwo_unit find_dwo_cutu;
12634
12635 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12636 find_dwo_cutu.signature = signature;
12637 dwo_cutu
12638 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12639 &find_dwo_cutu);
12640 }
12641 else if (!is_debug_types && dwo_file->cus)
12642 {
12643 struct dwo_unit find_dwo_cutu;
12644
12645 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12646 find_dwo_cutu.signature = signature;
12647 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12648 &find_dwo_cutu);
12649 }
12650
12651 if (dwo_cutu != NULL)
12652 {
12653 if (dwarf_read_debug)
12654 {
12655 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12656 kind, dwo_name, hex_string (signature),
12657 host_address_to_string (dwo_cutu));
12658 }
12659 return dwo_cutu;
12660 }
12661 }
12662 }
12663
12664 /* We didn't find it. This could mean a dwo_id mismatch, or
12665 someone deleted the DWO/DWP file, or the search path isn't set up
12666 correctly to find the file. */
12667
12668 if (dwarf_read_debug)
12669 {
12670 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12671 kind, dwo_name, hex_string (signature));
12672 }
12673
12674 /* This is a warning and not a complaint because it can be caused by
12675 pilot error (e.g., user accidentally deleting the DWO). */
12676 {
12677 /* Print the name of the DWP file if we looked there, helps the user
12678 better diagnose the problem. */
12679 std::string dwp_text;
12680
12681 if (dwp_file != NULL)
12682 dwp_text = string_printf (" [in DWP file %s]",
12683 lbasename (dwp_file->name));
12684
12685 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12686 " [in module %s]"),
12687 kind, dwo_name, hex_string (signature),
12688 dwp_text.c_str (),
12689 this_unit->is_debug_types ? "TU" : "CU",
12690 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12691 }
12692 return NULL;
12693 }
12694
12695 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12696 See lookup_dwo_cutu_unit for details. */
12697
12698 static struct dwo_unit *
12699 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12700 const char *dwo_name, const char *comp_dir,
12701 ULONGEST signature)
12702 {
12703 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12704 }
12705
12706 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12707 See lookup_dwo_cutu_unit for details. */
12708
12709 static struct dwo_unit *
12710 lookup_dwo_type_unit (struct signatured_type *this_tu,
12711 const char *dwo_name, const char *comp_dir)
12712 {
12713 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12714 }
12715
12716 /* Traversal function for queue_and_load_all_dwo_tus. */
12717
12718 static int
12719 queue_and_load_dwo_tu (void **slot, void *info)
12720 {
12721 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12722 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12723 ULONGEST signature = dwo_unit->signature;
12724 struct signatured_type *sig_type =
12725 lookup_dwo_signatured_type (per_cu->cu, signature);
12726
12727 if (sig_type != NULL)
12728 {
12729 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12730
12731 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12732 a real dependency of PER_CU on SIG_TYPE. That is detected later
12733 while processing PER_CU. */
12734 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12735 load_full_type_unit (sig_cu);
12736 per_cu->imported_symtabs_push (sig_cu);
12737 }
12738
12739 return 1;
12740 }
12741
12742 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12743 The DWO may have the only definition of the type, though it may not be
12744 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12745 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12746
12747 static void
12748 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12749 {
12750 struct dwo_unit *dwo_unit;
12751 struct dwo_file *dwo_file;
12752
12753 gdb_assert (!per_cu->is_debug_types);
12754 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12755 gdb_assert (per_cu->cu != NULL);
12756
12757 dwo_unit = per_cu->cu->dwo_unit;
12758 gdb_assert (dwo_unit != NULL);
12759
12760 dwo_file = dwo_unit->dwo_file;
12761 if (dwo_file->tus != NULL)
12762 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12763 per_cu);
12764 }
12765
12766 /* Read in various DIEs. */
12767
12768 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12769 Inherit only the children of the DW_AT_abstract_origin DIE not being
12770 already referenced by DW_AT_abstract_origin from the children of the
12771 current DIE. */
12772
12773 static void
12774 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12775 {
12776 struct die_info *child_die;
12777 sect_offset *offsetp;
12778 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12779 struct die_info *origin_die;
12780 /* Iterator of the ORIGIN_DIE children. */
12781 struct die_info *origin_child_die;
12782 struct attribute *attr;
12783 struct dwarf2_cu *origin_cu;
12784 struct pending **origin_previous_list_in_scope;
12785
12786 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12787 if (!attr)
12788 return;
12789
12790 /* Note that following die references may follow to a die in a
12791 different cu. */
12792
12793 origin_cu = cu;
12794 origin_die = follow_die_ref (die, attr, &origin_cu);
12795
12796 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12797 symbols in. */
12798 origin_previous_list_in_scope = origin_cu->list_in_scope;
12799 origin_cu->list_in_scope = cu->list_in_scope;
12800
12801 if (die->tag != origin_die->tag
12802 && !(die->tag == DW_TAG_inlined_subroutine
12803 && origin_die->tag == DW_TAG_subprogram))
12804 complaint (_("DIE %s and its abstract origin %s have different tags"),
12805 sect_offset_str (die->sect_off),
12806 sect_offset_str (origin_die->sect_off));
12807
12808 std::vector<sect_offset> offsets;
12809
12810 for (child_die = die->child;
12811 child_die && child_die->tag;
12812 child_die = child_die->sibling)
12813 {
12814 struct die_info *child_origin_die;
12815 struct dwarf2_cu *child_origin_cu;
12816
12817 /* We are trying to process concrete instance entries:
12818 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12819 it's not relevant to our analysis here. i.e. detecting DIEs that are
12820 present in the abstract instance but not referenced in the concrete
12821 one. */
12822 if (child_die->tag == DW_TAG_call_site
12823 || child_die->tag == DW_TAG_GNU_call_site)
12824 continue;
12825
12826 /* For each CHILD_DIE, find the corresponding child of
12827 ORIGIN_DIE. If there is more than one layer of
12828 DW_AT_abstract_origin, follow them all; there shouldn't be,
12829 but GCC versions at least through 4.4 generate this (GCC PR
12830 40573). */
12831 child_origin_die = child_die;
12832 child_origin_cu = cu;
12833 while (1)
12834 {
12835 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12836 child_origin_cu);
12837 if (attr == NULL)
12838 break;
12839 child_origin_die = follow_die_ref (child_origin_die, attr,
12840 &child_origin_cu);
12841 }
12842
12843 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12844 counterpart may exist. */
12845 if (child_origin_die != child_die)
12846 {
12847 if (child_die->tag != child_origin_die->tag
12848 && !(child_die->tag == DW_TAG_inlined_subroutine
12849 && child_origin_die->tag == DW_TAG_subprogram))
12850 complaint (_("Child DIE %s and its abstract origin %s have "
12851 "different tags"),
12852 sect_offset_str (child_die->sect_off),
12853 sect_offset_str (child_origin_die->sect_off));
12854 if (child_origin_die->parent != origin_die)
12855 complaint (_("Child DIE %s and its abstract origin %s have "
12856 "different parents"),
12857 sect_offset_str (child_die->sect_off),
12858 sect_offset_str (child_origin_die->sect_off));
12859 else
12860 offsets.push_back (child_origin_die->sect_off);
12861 }
12862 }
12863 std::sort (offsets.begin (), offsets.end ());
12864 sect_offset *offsets_end = offsets.data () + offsets.size ();
12865 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12866 if (offsetp[-1] == *offsetp)
12867 complaint (_("Multiple children of DIE %s refer "
12868 "to DIE %s as their abstract origin"),
12869 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12870
12871 offsetp = offsets.data ();
12872 origin_child_die = origin_die->child;
12873 while (origin_child_die && origin_child_die->tag)
12874 {
12875 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12876 while (offsetp < offsets_end
12877 && *offsetp < origin_child_die->sect_off)
12878 offsetp++;
12879 if (offsetp >= offsets_end
12880 || *offsetp > origin_child_die->sect_off)
12881 {
12882 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12883 Check whether we're already processing ORIGIN_CHILD_DIE.
12884 This can happen with mutually referenced abstract_origins.
12885 PR 16581. */
12886 if (!origin_child_die->in_process)
12887 process_die (origin_child_die, origin_cu);
12888 }
12889 origin_child_die = origin_child_die->sibling;
12890 }
12891 origin_cu->list_in_scope = origin_previous_list_in_scope;
12892
12893 if (cu != origin_cu)
12894 compute_delayed_physnames (origin_cu);
12895 }
12896
12897 static void
12898 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12899 {
12900 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12901 struct gdbarch *gdbarch = objfile->arch ();
12902 struct context_stack *newobj;
12903 CORE_ADDR lowpc;
12904 CORE_ADDR highpc;
12905 struct die_info *child_die;
12906 struct attribute *attr, *call_line, *call_file;
12907 const char *name;
12908 CORE_ADDR baseaddr;
12909 struct block *block;
12910 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12911 std::vector<struct symbol *> template_args;
12912 struct template_symbol *templ_func = NULL;
12913
12914 if (inlined_func)
12915 {
12916 /* If we do not have call site information, we can't show the
12917 caller of this inlined function. That's too confusing, so
12918 only use the scope for local variables. */
12919 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12920 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12921 if (call_line == NULL || call_file == NULL)
12922 {
12923 read_lexical_block_scope (die, cu);
12924 return;
12925 }
12926 }
12927
12928 baseaddr = objfile->text_section_offset ();
12929
12930 name = dwarf2_name (die, cu);
12931
12932 /* Ignore functions with missing or empty names. These are actually
12933 illegal according to the DWARF standard. */
12934 if (name == NULL)
12935 {
12936 complaint (_("missing name for subprogram DIE at %s"),
12937 sect_offset_str (die->sect_off));
12938 return;
12939 }
12940
12941 /* Ignore functions with missing or invalid low and high pc attributes. */
12942 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12943 <= PC_BOUNDS_INVALID)
12944 {
12945 attr = dwarf2_attr (die, DW_AT_external, cu);
12946 if (!attr || !DW_UNSND (attr))
12947 complaint (_("cannot get low and high bounds "
12948 "for subprogram DIE at %s"),
12949 sect_offset_str (die->sect_off));
12950 return;
12951 }
12952
12953 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12954 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12955
12956 /* If we have any template arguments, then we must allocate a
12957 different sort of symbol. */
12958 for (child_die = die->child; child_die; child_die = child_die->sibling)
12959 {
12960 if (child_die->tag == DW_TAG_template_type_param
12961 || child_die->tag == DW_TAG_template_value_param)
12962 {
12963 templ_func = allocate_template_symbol (objfile);
12964 templ_func->subclass = SYMBOL_TEMPLATE;
12965 break;
12966 }
12967 }
12968
12969 newobj = cu->get_builder ()->push_context (0, lowpc);
12970 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12971 (struct symbol *) templ_func);
12972
12973 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12974 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12975 cu->language);
12976
12977 /* If there is a location expression for DW_AT_frame_base, record
12978 it. */
12979 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12980 if (attr != nullptr)
12981 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12982
12983 /* If there is a location for the static link, record it. */
12984 newobj->static_link = NULL;
12985 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12986 if (attr != nullptr)
12987 {
12988 newobj->static_link
12989 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12990 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12991 cu->per_cu->addr_type ());
12992 }
12993
12994 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12995
12996 if (die->child != NULL)
12997 {
12998 child_die = die->child;
12999 while (child_die && child_die->tag)
13000 {
13001 if (child_die->tag == DW_TAG_template_type_param
13002 || child_die->tag == DW_TAG_template_value_param)
13003 {
13004 struct symbol *arg = new_symbol (child_die, NULL, cu);
13005
13006 if (arg != NULL)
13007 template_args.push_back (arg);
13008 }
13009 else
13010 process_die (child_die, cu);
13011 child_die = child_die->sibling;
13012 }
13013 }
13014
13015 inherit_abstract_dies (die, cu);
13016
13017 /* If we have a DW_AT_specification, we might need to import using
13018 directives from the context of the specification DIE. See the
13019 comment in determine_prefix. */
13020 if (cu->language == language_cplus
13021 && dwarf2_attr (die, DW_AT_specification, cu))
13022 {
13023 struct dwarf2_cu *spec_cu = cu;
13024 struct die_info *spec_die = die_specification (die, &spec_cu);
13025
13026 while (spec_die)
13027 {
13028 child_die = spec_die->child;
13029 while (child_die && child_die->tag)
13030 {
13031 if (child_die->tag == DW_TAG_imported_module)
13032 process_die (child_die, spec_cu);
13033 child_die = child_die->sibling;
13034 }
13035
13036 /* In some cases, GCC generates specification DIEs that
13037 themselves contain DW_AT_specification attributes. */
13038 spec_die = die_specification (spec_die, &spec_cu);
13039 }
13040 }
13041
13042 struct context_stack cstk = cu->get_builder ()->pop_context ();
13043 /* Make a block for the local symbols within. */
13044 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13045 cstk.static_link, lowpc, highpc);
13046
13047 /* For C++, set the block's scope. */
13048 if ((cu->language == language_cplus
13049 || cu->language == language_fortran
13050 || cu->language == language_d
13051 || cu->language == language_rust)
13052 && cu->processing_has_namespace_info)
13053 block_set_scope (block, determine_prefix (die, cu),
13054 &objfile->objfile_obstack);
13055
13056 /* If we have address ranges, record them. */
13057 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13058
13059 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13060
13061 /* Attach template arguments to function. */
13062 if (!template_args.empty ())
13063 {
13064 gdb_assert (templ_func != NULL);
13065
13066 templ_func->n_template_arguments = template_args.size ();
13067 templ_func->template_arguments
13068 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13069 templ_func->n_template_arguments);
13070 memcpy (templ_func->template_arguments,
13071 template_args.data (),
13072 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13073
13074 /* Make sure that the symtab is set on the new symbols. Even
13075 though they don't appear in this symtab directly, other parts
13076 of gdb assume that symbols do, and this is reasonably
13077 true. */
13078 for (symbol *sym : template_args)
13079 symbol_set_symtab (sym, symbol_symtab (templ_func));
13080 }
13081
13082 /* In C++, we can have functions nested inside functions (e.g., when
13083 a function declares a class that has methods). This means that
13084 when we finish processing a function scope, we may need to go
13085 back to building a containing block's symbol lists. */
13086 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13087 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13088
13089 /* If we've finished processing a top-level function, subsequent
13090 symbols go in the file symbol list. */
13091 if (cu->get_builder ()->outermost_context_p ())
13092 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13093 }
13094
13095 /* Process all the DIES contained within a lexical block scope. Start
13096 a new scope, process the dies, and then close the scope. */
13097
13098 static void
13099 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13100 {
13101 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13102 struct gdbarch *gdbarch = objfile->arch ();
13103 CORE_ADDR lowpc, highpc;
13104 struct die_info *child_die;
13105 CORE_ADDR baseaddr;
13106
13107 baseaddr = objfile->text_section_offset ();
13108
13109 /* Ignore blocks with missing or invalid low and high pc attributes. */
13110 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13111 as multiple lexical blocks? Handling children in a sane way would
13112 be nasty. Might be easier to properly extend generic blocks to
13113 describe ranges. */
13114 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13115 {
13116 case PC_BOUNDS_NOT_PRESENT:
13117 /* DW_TAG_lexical_block has no attributes, process its children as if
13118 there was no wrapping by that DW_TAG_lexical_block.
13119 GCC does no longer produces such DWARF since GCC r224161. */
13120 for (child_die = die->child;
13121 child_die != NULL && child_die->tag;
13122 child_die = child_die->sibling)
13123 {
13124 /* We might already be processing this DIE. This can happen
13125 in an unusual circumstance -- where a subroutine A
13126 appears lexically in another subroutine B, but A actually
13127 inlines B. The recursion is broken here, rather than in
13128 inherit_abstract_dies, because it seems better to simply
13129 drop concrete children here. */
13130 if (!child_die->in_process)
13131 process_die (child_die, cu);
13132 }
13133 return;
13134 case PC_BOUNDS_INVALID:
13135 return;
13136 }
13137 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13138 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13139
13140 cu->get_builder ()->push_context (0, lowpc);
13141 if (die->child != NULL)
13142 {
13143 child_die = die->child;
13144 while (child_die && child_die->tag)
13145 {
13146 process_die (child_die, cu);
13147 child_die = child_die->sibling;
13148 }
13149 }
13150 inherit_abstract_dies (die, cu);
13151 struct context_stack cstk = cu->get_builder ()->pop_context ();
13152
13153 if (*cu->get_builder ()->get_local_symbols () != NULL
13154 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13155 {
13156 struct block *block
13157 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13158 cstk.start_addr, highpc);
13159
13160 /* Note that recording ranges after traversing children, as we
13161 do here, means that recording a parent's ranges entails
13162 walking across all its children's ranges as they appear in
13163 the address map, which is quadratic behavior.
13164
13165 It would be nicer to record the parent's ranges before
13166 traversing its children, simply overriding whatever you find
13167 there. But since we don't even decide whether to create a
13168 block until after we've traversed its children, that's hard
13169 to do. */
13170 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13171 }
13172 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13173 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13174 }
13175
13176 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13177
13178 static void
13179 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13180 {
13181 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13182 struct gdbarch *gdbarch = objfile->arch ();
13183 CORE_ADDR pc, baseaddr;
13184 struct attribute *attr;
13185 struct call_site *call_site, call_site_local;
13186 void **slot;
13187 int nparams;
13188 struct die_info *child_die;
13189
13190 baseaddr = objfile->text_section_offset ();
13191
13192 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13193 if (attr == NULL)
13194 {
13195 /* This was a pre-DWARF-5 GNU extension alias
13196 for DW_AT_call_return_pc. */
13197 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13198 }
13199 if (!attr)
13200 {
13201 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13202 "DIE %s [in module %s]"),
13203 sect_offset_str (die->sect_off), objfile_name (objfile));
13204 return;
13205 }
13206 pc = attr->value_as_address () + baseaddr;
13207 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13208
13209 if (cu->call_site_htab == NULL)
13210 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13211 NULL, &objfile->objfile_obstack,
13212 hashtab_obstack_allocate, NULL);
13213 call_site_local.pc = pc;
13214 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13215 if (*slot != NULL)
13216 {
13217 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13218 "DIE %s [in module %s]"),
13219 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13220 objfile_name (objfile));
13221 return;
13222 }
13223
13224 /* Count parameters at the caller. */
13225
13226 nparams = 0;
13227 for (child_die = die->child; child_die && child_die->tag;
13228 child_die = child_die->sibling)
13229 {
13230 if (child_die->tag != DW_TAG_call_site_parameter
13231 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13232 {
13233 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13234 "DW_TAG_call_site child DIE %s [in module %s]"),
13235 child_die->tag, sect_offset_str (child_die->sect_off),
13236 objfile_name (objfile));
13237 continue;
13238 }
13239
13240 nparams++;
13241 }
13242
13243 call_site
13244 = ((struct call_site *)
13245 obstack_alloc (&objfile->objfile_obstack,
13246 sizeof (*call_site)
13247 + (sizeof (*call_site->parameter) * (nparams - 1))));
13248 *slot = call_site;
13249 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13250 call_site->pc = pc;
13251
13252 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13253 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13254 {
13255 struct die_info *func_die;
13256
13257 /* Skip also over DW_TAG_inlined_subroutine. */
13258 for (func_die = die->parent;
13259 func_die && func_die->tag != DW_TAG_subprogram
13260 && func_die->tag != DW_TAG_subroutine_type;
13261 func_die = func_die->parent);
13262
13263 /* DW_AT_call_all_calls is a superset
13264 of DW_AT_call_all_tail_calls. */
13265 if (func_die
13266 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13267 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13268 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13269 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13270 {
13271 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13272 not complete. But keep CALL_SITE for look ups via call_site_htab,
13273 both the initial caller containing the real return address PC and
13274 the final callee containing the current PC of a chain of tail
13275 calls do not need to have the tail call list complete. But any
13276 function candidate for a virtual tail call frame searched via
13277 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13278 determined unambiguously. */
13279 }
13280 else
13281 {
13282 struct type *func_type = NULL;
13283
13284 if (func_die)
13285 func_type = get_die_type (func_die, cu);
13286 if (func_type != NULL)
13287 {
13288 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13289
13290 /* Enlist this call site to the function. */
13291 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13292 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13293 }
13294 else
13295 complaint (_("Cannot find function owning DW_TAG_call_site "
13296 "DIE %s [in module %s]"),
13297 sect_offset_str (die->sect_off), objfile_name (objfile));
13298 }
13299 }
13300
13301 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13302 if (attr == NULL)
13303 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13304 if (attr == NULL)
13305 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13306 if (attr == NULL)
13307 {
13308 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13309 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13310 }
13311 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13312 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13313 /* Keep NULL DWARF_BLOCK. */;
13314 else if (attr->form_is_block ())
13315 {
13316 struct dwarf2_locexpr_baton *dlbaton;
13317
13318 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13319 dlbaton->data = DW_BLOCK (attr)->data;
13320 dlbaton->size = DW_BLOCK (attr)->size;
13321 dlbaton->per_cu = cu->per_cu;
13322
13323 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13324 }
13325 else if (attr->form_is_ref ())
13326 {
13327 struct dwarf2_cu *target_cu = cu;
13328 struct die_info *target_die;
13329
13330 target_die = follow_die_ref (die, attr, &target_cu);
13331 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13332 if (die_is_declaration (target_die, target_cu))
13333 {
13334 const char *target_physname;
13335
13336 /* Prefer the mangled name; otherwise compute the demangled one. */
13337 target_physname = dw2_linkage_name (target_die, target_cu);
13338 if (target_physname == NULL)
13339 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13340 if (target_physname == NULL)
13341 complaint (_("DW_AT_call_target target DIE has invalid "
13342 "physname, for referencing DIE %s [in module %s]"),
13343 sect_offset_str (die->sect_off), objfile_name (objfile));
13344 else
13345 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13346 }
13347 else
13348 {
13349 CORE_ADDR lowpc;
13350
13351 /* DW_AT_entry_pc should be preferred. */
13352 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13353 <= PC_BOUNDS_INVALID)
13354 complaint (_("DW_AT_call_target target DIE has invalid "
13355 "low pc, for referencing DIE %s [in module %s]"),
13356 sect_offset_str (die->sect_off), objfile_name (objfile));
13357 else
13358 {
13359 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13360 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13361 }
13362 }
13363 }
13364 else
13365 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13366 "block nor reference, for DIE %s [in module %s]"),
13367 sect_offset_str (die->sect_off), objfile_name (objfile));
13368
13369 call_site->per_cu = cu->per_cu;
13370
13371 for (child_die = die->child;
13372 child_die && child_die->tag;
13373 child_die = child_die->sibling)
13374 {
13375 struct call_site_parameter *parameter;
13376 struct attribute *loc, *origin;
13377
13378 if (child_die->tag != DW_TAG_call_site_parameter
13379 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13380 {
13381 /* Already printed the complaint above. */
13382 continue;
13383 }
13384
13385 gdb_assert (call_site->parameter_count < nparams);
13386 parameter = &call_site->parameter[call_site->parameter_count];
13387
13388 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13389 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13390 register is contained in DW_AT_call_value. */
13391
13392 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13393 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13394 if (origin == NULL)
13395 {
13396 /* This was a pre-DWARF-5 GNU extension alias
13397 for DW_AT_call_parameter. */
13398 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13399 }
13400 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13401 {
13402 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13403
13404 sect_offset sect_off = origin->get_ref_die_offset ();
13405 if (!cu->header.offset_in_cu_p (sect_off))
13406 {
13407 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13408 binding can be done only inside one CU. Such referenced DIE
13409 therefore cannot be even moved to DW_TAG_partial_unit. */
13410 complaint (_("DW_AT_call_parameter offset is not in CU for "
13411 "DW_TAG_call_site child DIE %s [in module %s]"),
13412 sect_offset_str (child_die->sect_off),
13413 objfile_name (objfile));
13414 continue;
13415 }
13416 parameter->u.param_cu_off
13417 = (cu_offset) (sect_off - cu->header.sect_off);
13418 }
13419 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13420 {
13421 complaint (_("No DW_FORM_block* DW_AT_location for "
13422 "DW_TAG_call_site child DIE %s [in module %s]"),
13423 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13424 continue;
13425 }
13426 else
13427 {
13428 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13429 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13430 if (parameter->u.dwarf_reg != -1)
13431 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13432 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13433 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13434 &parameter->u.fb_offset))
13435 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13436 else
13437 {
13438 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13439 "for DW_FORM_block* DW_AT_location is supported for "
13440 "DW_TAG_call_site child DIE %s "
13441 "[in module %s]"),
13442 sect_offset_str (child_die->sect_off),
13443 objfile_name (objfile));
13444 continue;
13445 }
13446 }
13447
13448 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13449 if (attr == NULL)
13450 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13451 if (attr == NULL || !attr->form_is_block ())
13452 {
13453 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13454 "DW_TAG_call_site child DIE %s [in module %s]"),
13455 sect_offset_str (child_die->sect_off),
13456 objfile_name (objfile));
13457 continue;
13458 }
13459 parameter->value = DW_BLOCK (attr)->data;
13460 parameter->value_size = DW_BLOCK (attr)->size;
13461
13462 /* Parameters are not pre-cleared by memset above. */
13463 parameter->data_value = NULL;
13464 parameter->data_value_size = 0;
13465 call_site->parameter_count++;
13466
13467 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13468 if (attr == NULL)
13469 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13470 if (attr != nullptr)
13471 {
13472 if (!attr->form_is_block ())
13473 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13474 "DW_TAG_call_site child DIE %s [in module %s]"),
13475 sect_offset_str (child_die->sect_off),
13476 objfile_name (objfile));
13477 else
13478 {
13479 parameter->data_value = DW_BLOCK (attr)->data;
13480 parameter->data_value_size = DW_BLOCK (attr)->size;
13481 }
13482 }
13483 }
13484 }
13485
13486 /* Helper function for read_variable. If DIE represents a virtual
13487 table, then return the type of the concrete object that is
13488 associated with the virtual table. Otherwise, return NULL. */
13489
13490 static struct type *
13491 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13492 {
13493 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13494 if (attr == NULL)
13495 return NULL;
13496
13497 /* Find the type DIE. */
13498 struct die_info *type_die = NULL;
13499 struct dwarf2_cu *type_cu = cu;
13500
13501 if (attr->form_is_ref ())
13502 type_die = follow_die_ref (die, attr, &type_cu);
13503 if (type_die == NULL)
13504 return NULL;
13505
13506 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13507 return NULL;
13508 return die_containing_type (type_die, type_cu);
13509 }
13510
13511 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13512
13513 static void
13514 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13515 {
13516 struct rust_vtable_symbol *storage = NULL;
13517
13518 if (cu->language == language_rust)
13519 {
13520 struct type *containing_type = rust_containing_type (die, cu);
13521
13522 if (containing_type != NULL)
13523 {
13524 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13525
13526 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13527 initialize_objfile_symbol (storage);
13528 storage->concrete_type = containing_type;
13529 storage->subclass = SYMBOL_RUST_VTABLE;
13530 }
13531 }
13532
13533 struct symbol *res = new_symbol (die, NULL, cu, storage);
13534 struct attribute *abstract_origin
13535 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13536 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13537 if (res == NULL && loc && abstract_origin)
13538 {
13539 /* We have a variable without a name, but with a location and an abstract
13540 origin. This may be a concrete instance of an abstract variable
13541 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13542 later. */
13543 struct dwarf2_cu *origin_cu = cu;
13544 struct die_info *origin_die
13545 = follow_die_ref (die, abstract_origin, &origin_cu);
13546 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13547 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13548 }
13549 }
13550
13551 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13552 reading .debug_rnglists.
13553 Callback's type should be:
13554 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13555 Return true if the attributes are present and valid, otherwise,
13556 return false. */
13557
13558 template <typename Callback>
13559 static bool
13560 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13561 Callback &&callback)
13562 {
13563 struct dwarf2_per_objfile *dwarf2_per_objfile
13564 = cu->per_cu->dwarf2_per_objfile;
13565 struct objfile *objfile = dwarf2_per_objfile->objfile;
13566 bfd *obfd = objfile->obfd;
13567 /* Base address selection entry. */
13568 gdb::optional<CORE_ADDR> base;
13569 const gdb_byte *buffer;
13570 CORE_ADDR baseaddr;
13571 bool overflow = false;
13572
13573 base = cu->base_address;
13574
13575 dwarf2_per_objfile->rnglists.read (objfile);
13576 if (offset >= dwarf2_per_objfile->rnglists.size)
13577 {
13578 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13579 offset);
13580 return false;
13581 }
13582 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13583
13584 baseaddr = objfile->text_section_offset ();
13585
13586 while (1)
13587 {
13588 /* Initialize it due to a false compiler warning. */
13589 CORE_ADDR range_beginning = 0, range_end = 0;
13590 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13591 + dwarf2_per_objfile->rnglists.size);
13592 unsigned int bytes_read;
13593
13594 if (buffer == buf_end)
13595 {
13596 overflow = true;
13597 break;
13598 }
13599 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13600 switch (rlet)
13601 {
13602 case DW_RLE_end_of_list:
13603 break;
13604 case DW_RLE_base_address:
13605 if (buffer + cu->header.addr_size > buf_end)
13606 {
13607 overflow = true;
13608 break;
13609 }
13610 base = cu->header.read_address (obfd, buffer, &bytes_read);
13611 buffer += bytes_read;
13612 break;
13613 case DW_RLE_start_length:
13614 if (buffer + cu->header.addr_size > buf_end)
13615 {
13616 overflow = true;
13617 break;
13618 }
13619 range_beginning = cu->header.read_address (obfd, buffer,
13620 &bytes_read);
13621 buffer += bytes_read;
13622 range_end = (range_beginning
13623 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13624 buffer += bytes_read;
13625 if (buffer > buf_end)
13626 {
13627 overflow = true;
13628 break;
13629 }
13630 break;
13631 case DW_RLE_offset_pair:
13632 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13633 buffer += bytes_read;
13634 if (buffer > buf_end)
13635 {
13636 overflow = true;
13637 break;
13638 }
13639 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13640 buffer += bytes_read;
13641 if (buffer > buf_end)
13642 {
13643 overflow = true;
13644 break;
13645 }
13646 break;
13647 case DW_RLE_start_end:
13648 if (buffer + 2 * cu->header.addr_size > buf_end)
13649 {
13650 overflow = true;
13651 break;
13652 }
13653 range_beginning = cu->header.read_address (obfd, buffer,
13654 &bytes_read);
13655 buffer += bytes_read;
13656 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13657 buffer += bytes_read;
13658 break;
13659 default:
13660 complaint (_("Invalid .debug_rnglists data (no base address)"));
13661 return false;
13662 }
13663 if (rlet == DW_RLE_end_of_list || overflow)
13664 break;
13665 if (rlet == DW_RLE_base_address)
13666 continue;
13667
13668 if (!base.has_value ())
13669 {
13670 /* We have no valid base address for the ranges
13671 data. */
13672 complaint (_("Invalid .debug_rnglists data (no base address)"));
13673 return false;
13674 }
13675
13676 if (range_beginning > range_end)
13677 {
13678 /* Inverted range entries are invalid. */
13679 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13680 return false;
13681 }
13682
13683 /* Empty range entries have no effect. */
13684 if (range_beginning == range_end)
13685 continue;
13686
13687 range_beginning += *base;
13688 range_end += *base;
13689
13690 /* A not-uncommon case of bad debug info.
13691 Don't pollute the addrmap with bad data. */
13692 if (range_beginning + baseaddr == 0
13693 && !dwarf2_per_objfile->has_section_at_zero)
13694 {
13695 complaint (_(".debug_rnglists entry has start address of zero"
13696 " [in module %s]"), objfile_name (objfile));
13697 continue;
13698 }
13699
13700 callback (range_beginning, range_end);
13701 }
13702
13703 if (overflow)
13704 {
13705 complaint (_("Offset %d is not terminated "
13706 "for DW_AT_ranges attribute"),
13707 offset);
13708 return false;
13709 }
13710
13711 return true;
13712 }
13713
13714 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13715 Callback's type should be:
13716 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13717 Return 1 if the attributes are present and valid, otherwise, return 0. */
13718
13719 template <typename Callback>
13720 static int
13721 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13722 Callback &&callback)
13723 {
13724 struct dwarf2_per_objfile *dwarf2_per_objfile
13725 = cu->per_cu->dwarf2_per_objfile;
13726 struct objfile *objfile = dwarf2_per_objfile->objfile;
13727 struct comp_unit_head *cu_header = &cu->header;
13728 bfd *obfd = objfile->obfd;
13729 unsigned int addr_size = cu_header->addr_size;
13730 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13731 /* Base address selection entry. */
13732 gdb::optional<CORE_ADDR> base;
13733 unsigned int dummy;
13734 const gdb_byte *buffer;
13735 CORE_ADDR baseaddr;
13736
13737 if (cu_header->version >= 5)
13738 return dwarf2_rnglists_process (offset, cu, callback);
13739
13740 base = cu->base_address;
13741
13742 dwarf2_per_objfile->ranges.read (objfile);
13743 if (offset >= dwarf2_per_objfile->ranges.size)
13744 {
13745 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13746 offset);
13747 return 0;
13748 }
13749 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13750
13751 baseaddr = objfile->text_section_offset ();
13752
13753 while (1)
13754 {
13755 CORE_ADDR range_beginning, range_end;
13756
13757 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13758 buffer += addr_size;
13759 range_end = cu->header.read_address (obfd, buffer, &dummy);
13760 buffer += addr_size;
13761 offset += 2 * addr_size;
13762
13763 /* An end of list marker is a pair of zero addresses. */
13764 if (range_beginning == 0 && range_end == 0)
13765 /* Found the end of list entry. */
13766 break;
13767
13768 /* Each base address selection entry is a pair of 2 values.
13769 The first is the largest possible address, the second is
13770 the base address. Check for a base address here. */
13771 if ((range_beginning & mask) == mask)
13772 {
13773 /* If we found the largest possible address, then we already
13774 have the base address in range_end. */
13775 base = range_end;
13776 continue;
13777 }
13778
13779 if (!base.has_value ())
13780 {
13781 /* We have no valid base address for the ranges
13782 data. */
13783 complaint (_("Invalid .debug_ranges data (no base address)"));
13784 return 0;
13785 }
13786
13787 if (range_beginning > range_end)
13788 {
13789 /* Inverted range entries are invalid. */
13790 complaint (_("Invalid .debug_ranges data (inverted range)"));
13791 return 0;
13792 }
13793
13794 /* Empty range entries have no effect. */
13795 if (range_beginning == range_end)
13796 continue;
13797
13798 range_beginning += *base;
13799 range_end += *base;
13800
13801 /* A not-uncommon case of bad debug info.
13802 Don't pollute the addrmap with bad data. */
13803 if (range_beginning + baseaddr == 0
13804 && !dwarf2_per_objfile->has_section_at_zero)
13805 {
13806 complaint (_(".debug_ranges entry has start address of zero"
13807 " [in module %s]"), objfile_name (objfile));
13808 continue;
13809 }
13810
13811 callback (range_beginning, range_end);
13812 }
13813
13814 return 1;
13815 }
13816
13817 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13818 Return 1 if the attributes are present and valid, otherwise, return 0.
13819 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13820
13821 static int
13822 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13823 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13824 dwarf2_psymtab *ranges_pst)
13825 {
13826 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13827 struct gdbarch *gdbarch = objfile->arch ();
13828 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13829 int low_set = 0;
13830 CORE_ADDR low = 0;
13831 CORE_ADDR high = 0;
13832 int retval;
13833
13834 retval = dwarf2_ranges_process (offset, cu,
13835 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13836 {
13837 if (ranges_pst != NULL)
13838 {
13839 CORE_ADDR lowpc;
13840 CORE_ADDR highpc;
13841
13842 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13843 range_beginning + baseaddr)
13844 - baseaddr);
13845 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13846 range_end + baseaddr)
13847 - baseaddr);
13848 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13849 lowpc, highpc - 1, ranges_pst);
13850 }
13851
13852 /* FIXME: This is recording everything as a low-high
13853 segment of consecutive addresses. We should have a
13854 data structure for discontiguous block ranges
13855 instead. */
13856 if (! low_set)
13857 {
13858 low = range_beginning;
13859 high = range_end;
13860 low_set = 1;
13861 }
13862 else
13863 {
13864 if (range_beginning < low)
13865 low = range_beginning;
13866 if (range_end > high)
13867 high = range_end;
13868 }
13869 });
13870 if (!retval)
13871 return 0;
13872
13873 if (! low_set)
13874 /* If the first entry is an end-of-list marker, the range
13875 describes an empty scope, i.e. no instructions. */
13876 return 0;
13877
13878 if (low_return)
13879 *low_return = low;
13880 if (high_return)
13881 *high_return = high;
13882 return 1;
13883 }
13884
13885 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13886 definition for the return value. *LOWPC and *HIGHPC are set iff
13887 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13888
13889 static enum pc_bounds_kind
13890 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13891 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13892 dwarf2_psymtab *pst)
13893 {
13894 struct dwarf2_per_objfile *dwarf2_per_objfile
13895 = cu->per_cu->dwarf2_per_objfile;
13896 struct attribute *attr;
13897 struct attribute *attr_high;
13898 CORE_ADDR low = 0;
13899 CORE_ADDR high = 0;
13900 enum pc_bounds_kind ret;
13901
13902 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13903 if (attr_high)
13904 {
13905 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13906 if (attr != nullptr)
13907 {
13908 low = attr->value_as_address ();
13909 high = attr_high->value_as_address ();
13910 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13911 high += low;
13912 }
13913 else
13914 /* Found high w/o low attribute. */
13915 return PC_BOUNDS_INVALID;
13916
13917 /* Found consecutive range of addresses. */
13918 ret = PC_BOUNDS_HIGH_LOW;
13919 }
13920 else
13921 {
13922 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13923 if (attr != NULL)
13924 {
13925 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13926 We take advantage of the fact that DW_AT_ranges does not appear
13927 in DW_TAG_compile_unit of DWO files. */
13928 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13929 unsigned int ranges_offset = (DW_UNSND (attr)
13930 + (need_ranges_base
13931 ? cu->ranges_base
13932 : 0));
13933
13934 /* Value of the DW_AT_ranges attribute is the offset in the
13935 .debug_ranges section. */
13936 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13937 return PC_BOUNDS_INVALID;
13938 /* Found discontinuous range of addresses. */
13939 ret = PC_BOUNDS_RANGES;
13940 }
13941 else
13942 return PC_BOUNDS_NOT_PRESENT;
13943 }
13944
13945 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13946 if (high <= low)
13947 return PC_BOUNDS_INVALID;
13948
13949 /* When using the GNU linker, .gnu.linkonce. sections are used to
13950 eliminate duplicate copies of functions and vtables and such.
13951 The linker will arbitrarily choose one and discard the others.
13952 The AT_*_pc values for such functions refer to local labels in
13953 these sections. If the section from that file was discarded, the
13954 labels are not in the output, so the relocs get a value of 0.
13955 If this is a discarded function, mark the pc bounds as invalid,
13956 so that GDB will ignore it. */
13957 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13958 return PC_BOUNDS_INVALID;
13959
13960 *lowpc = low;
13961 if (highpc)
13962 *highpc = high;
13963 return ret;
13964 }
13965
13966 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13967 its low and high PC addresses. Do nothing if these addresses could not
13968 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13969 and HIGHPC to the high address if greater than HIGHPC. */
13970
13971 static void
13972 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13973 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13974 struct dwarf2_cu *cu)
13975 {
13976 CORE_ADDR low, high;
13977 struct die_info *child = die->child;
13978
13979 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13980 {
13981 *lowpc = std::min (*lowpc, low);
13982 *highpc = std::max (*highpc, high);
13983 }
13984
13985 /* If the language does not allow nested subprograms (either inside
13986 subprograms or lexical blocks), we're done. */
13987 if (cu->language != language_ada)
13988 return;
13989
13990 /* Check all the children of the given DIE. If it contains nested
13991 subprograms, then check their pc bounds. Likewise, we need to
13992 check lexical blocks as well, as they may also contain subprogram
13993 definitions. */
13994 while (child && child->tag)
13995 {
13996 if (child->tag == DW_TAG_subprogram
13997 || child->tag == DW_TAG_lexical_block)
13998 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13999 child = child->sibling;
14000 }
14001 }
14002
14003 /* Get the low and high pc's represented by the scope DIE, and store
14004 them in *LOWPC and *HIGHPC. If the correct values can't be
14005 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14006
14007 static void
14008 get_scope_pc_bounds (struct die_info *die,
14009 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14010 struct dwarf2_cu *cu)
14011 {
14012 CORE_ADDR best_low = (CORE_ADDR) -1;
14013 CORE_ADDR best_high = (CORE_ADDR) 0;
14014 CORE_ADDR current_low, current_high;
14015
14016 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14017 >= PC_BOUNDS_RANGES)
14018 {
14019 best_low = current_low;
14020 best_high = current_high;
14021 }
14022 else
14023 {
14024 struct die_info *child = die->child;
14025
14026 while (child && child->tag)
14027 {
14028 switch (child->tag) {
14029 case DW_TAG_subprogram:
14030 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14031 break;
14032 case DW_TAG_namespace:
14033 case DW_TAG_module:
14034 /* FIXME: carlton/2004-01-16: Should we do this for
14035 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14036 that current GCC's always emit the DIEs corresponding
14037 to definitions of methods of classes as children of a
14038 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14039 the DIEs giving the declarations, which could be
14040 anywhere). But I don't see any reason why the
14041 standards says that they have to be there. */
14042 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14043
14044 if (current_low != ((CORE_ADDR) -1))
14045 {
14046 best_low = std::min (best_low, current_low);
14047 best_high = std::max (best_high, current_high);
14048 }
14049 break;
14050 default:
14051 /* Ignore. */
14052 break;
14053 }
14054
14055 child = child->sibling;
14056 }
14057 }
14058
14059 *lowpc = best_low;
14060 *highpc = best_high;
14061 }
14062
14063 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14064 in DIE. */
14065
14066 static void
14067 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14068 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14069 {
14070 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14071 struct gdbarch *gdbarch = objfile->arch ();
14072 struct attribute *attr;
14073 struct attribute *attr_high;
14074
14075 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14076 if (attr_high)
14077 {
14078 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14079 if (attr != nullptr)
14080 {
14081 CORE_ADDR low = attr->value_as_address ();
14082 CORE_ADDR high = attr_high->value_as_address ();
14083
14084 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14085 high += low;
14086
14087 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14088 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14089 cu->get_builder ()->record_block_range (block, low, high - 1);
14090 }
14091 }
14092
14093 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14094 if (attr != nullptr)
14095 {
14096 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14097 We take advantage of the fact that DW_AT_ranges does not appear
14098 in DW_TAG_compile_unit of DWO files. */
14099 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14100
14101 /* The value of the DW_AT_ranges attribute is the offset of the
14102 address range list in the .debug_ranges section. */
14103 unsigned long offset = (DW_UNSND (attr)
14104 + (need_ranges_base ? cu->ranges_base : 0));
14105
14106 std::vector<blockrange> blockvec;
14107 dwarf2_ranges_process (offset, cu,
14108 [&] (CORE_ADDR start, CORE_ADDR end)
14109 {
14110 start += baseaddr;
14111 end += baseaddr;
14112 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14113 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14114 cu->get_builder ()->record_block_range (block, start, end - 1);
14115 blockvec.emplace_back (start, end);
14116 });
14117
14118 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14119 }
14120 }
14121
14122 /* Check whether the producer field indicates either of GCC < 4.6, or the
14123 Intel C/C++ compiler, and cache the result in CU. */
14124
14125 static void
14126 check_producer (struct dwarf2_cu *cu)
14127 {
14128 int major, minor;
14129
14130 if (cu->producer == NULL)
14131 {
14132 /* For unknown compilers expect their behavior is DWARF version
14133 compliant.
14134
14135 GCC started to support .debug_types sections by -gdwarf-4 since
14136 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14137 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14138 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14139 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14140 }
14141 else if (producer_is_gcc (cu->producer, &major, &minor))
14142 {
14143 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14144 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14145 }
14146 else if (producer_is_icc (cu->producer, &major, &minor))
14147 {
14148 cu->producer_is_icc = true;
14149 cu->producer_is_icc_lt_14 = major < 14;
14150 }
14151 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14152 cu->producer_is_codewarrior = true;
14153 else
14154 {
14155 /* For other non-GCC compilers, expect their behavior is DWARF version
14156 compliant. */
14157 }
14158
14159 cu->checked_producer = true;
14160 }
14161
14162 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14163 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14164 during 4.6.0 experimental. */
14165
14166 static bool
14167 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14168 {
14169 if (!cu->checked_producer)
14170 check_producer (cu);
14171
14172 return cu->producer_is_gxx_lt_4_6;
14173 }
14174
14175
14176 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14177 with incorrect is_stmt attributes. */
14178
14179 static bool
14180 producer_is_codewarrior (struct dwarf2_cu *cu)
14181 {
14182 if (!cu->checked_producer)
14183 check_producer (cu);
14184
14185 return cu->producer_is_codewarrior;
14186 }
14187
14188 /* Return the default accessibility type if it is not overridden by
14189 DW_AT_accessibility. */
14190
14191 static enum dwarf_access_attribute
14192 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14193 {
14194 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14195 {
14196 /* The default DWARF 2 accessibility for members is public, the default
14197 accessibility for inheritance is private. */
14198
14199 if (die->tag != DW_TAG_inheritance)
14200 return DW_ACCESS_public;
14201 else
14202 return DW_ACCESS_private;
14203 }
14204 else
14205 {
14206 /* DWARF 3+ defines the default accessibility a different way. The same
14207 rules apply now for DW_TAG_inheritance as for the members and it only
14208 depends on the container kind. */
14209
14210 if (die->parent->tag == DW_TAG_class_type)
14211 return DW_ACCESS_private;
14212 else
14213 return DW_ACCESS_public;
14214 }
14215 }
14216
14217 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14218 offset. If the attribute was not found return 0, otherwise return
14219 1. If it was found but could not properly be handled, set *OFFSET
14220 to 0. */
14221
14222 static int
14223 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14224 LONGEST *offset)
14225 {
14226 struct attribute *attr;
14227
14228 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14229 if (attr != NULL)
14230 {
14231 *offset = 0;
14232
14233 /* Note that we do not check for a section offset first here.
14234 This is because DW_AT_data_member_location is new in DWARF 4,
14235 so if we see it, we can assume that a constant form is really
14236 a constant and not a section offset. */
14237 if (attr->form_is_constant ())
14238 *offset = attr->constant_value (0);
14239 else if (attr->form_is_section_offset ())
14240 dwarf2_complex_location_expr_complaint ();
14241 else if (attr->form_is_block ())
14242 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14243 else
14244 dwarf2_complex_location_expr_complaint ();
14245
14246 return 1;
14247 }
14248
14249 return 0;
14250 }
14251
14252 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14253
14254 static void
14255 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14256 struct field *field)
14257 {
14258 struct attribute *attr;
14259
14260 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14261 if (attr != NULL)
14262 {
14263 if (attr->form_is_constant ())
14264 {
14265 LONGEST offset = attr->constant_value (0);
14266 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14267 }
14268 else if (attr->form_is_section_offset ())
14269 dwarf2_complex_location_expr_complaint ();
14270 else if (attr->form_is_block ())
14271 {
14272 bool handled;
14273 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14274 if (handled)
14275 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14276 else
14277 {
14278 struct objfile *objfile
14279 = cu->per_cu->dwarf2_per_objfile->objfile;
14280 struct dwarf2_locexpr_baton *dlbaton
14281 = XOBNEW (&objfile->objfile_obstack,
14282 struct dwarf2_locexpr_baton);
14283 dlbaton->data = DW_BLOCK (attr)->data;
14284 dlbaton->size = DW_BLOCK (attr)->size;
14285 /* When using this baton, we want to compute the address
14286 of the field, not the value. This is why
14287 is_reference is set to false here. */
14288 dlbaton->is_reference = false;
14289 dlbaton->per_cu = cu->per_cu;
14290
14291 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14292 }
14293 }
14294 else
14295 dwarf2_complex_location_expr_complaint ();
14296 }
14297 }
14298
14299 /* Add an aggregate field to the field list. */
14300
14301 static void
14302 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14303 struct dwarf2_cu *cu)
14304 {
14305 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14306 struct gdbarch *gdbarch = objfile->arch ();
14307 struct nextfield *new_field;
14308 struct attribute *attr;
14309 struct field *fp;
14310 const char *fieldname = "";
14311
14312 if (die->tag == DW_TAG_inheritance)
14313 {
14314 fip->baseclasses.emplace_back ();
14315 new_field = &fip->baseclasses.back ();
14316 }
14317 else
14318 {
14319 fip->fields.emplace_back ();
14320 new_field = &fip->fields.back ();
14321 }
14322
14323 new_field->offset = die->sect_off;
14324
14325 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14326 if (attr != nullptr)
14327 new_field->accessibility = DW_UNSND (attr);
14328 else
14329 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14330 if (new_field->accessibility != DW_ACCESS_public)
14331 fip->non_public_fields = 1;
14332
14333 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14334 if (attr != nullptr)
14335 new_field->virtuality = DW_UNSND (attr);
14336 else
14337 new_field->virtuality = DW_VIRTUALITY_none;
14338
14339 fp = &new_field->field;
14340
14341 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14342 {
14343 /* Data member other than a C++ static data member. */
14344
14345 /* Get type of field. */
14346 fp->type = die_type (die, cu);
14347
14348 SET_FIELD_BITPOS (*fp, 0);
14349
14350 /* Get bit size of field (zero if none). */
14351 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14352 if (attr != nullptr)
14353 {
14354 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14355 }
14356 else
14357 {
14358 FIELD_BITSIZE (*fp) = 0;
14359 }
14360
14361 /* Get bit offset of field. */
14362 handle_data_member_location (die, cu, fp);
14363 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14364 if (attr != nullptr)
14365 {
14366 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14367 {
14368 /* For big endian bits, the DW_AT_bit_offset gives the
14369 additional bit offset from the MSB of the containing
14370 anonymous object to the MSB of the field. We don't
14371 have to do anything special since we don't need to
14372 know the size of the anonymous object. */
14373 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14374 }
14375 else
14376 {
14377 /* For little endian bits, compute the bit offset to the
14378 MSB of the anonymous object, subtract off the number of
14379 bits from the MSB of the field to the MSB of the
14380 object, and then subtract off the number of bits of
14381 the field itself. The result is the bit offset of
14382 the LSB of the field. */
14383 int anonymous_size;
14384 int bit_offset = DW_UNSND (attr);
14385
14386 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14387 if (attr != nullptr)
14388 {
14389 /* The size of the anonymous object containing
14390 the bit field is explicit, so use the
14391 indicated size (in bytes). */
14392 anonymous_size = DW_UNSND (attr);
14393 }
14394 else
14395 {
14396 /* The size of the anonymous object containing
14397 the bit field must be inferred from the type
14398 attribute of the data member containing the
14399 bit field. */
14400 anonymous_size = TYPE_LENGTH (fp->type);
14401 }
14402 SET_FIELD_BITPOS (*fp,
14403 (FIELD_BITPOS (*fp)
14404 + anonymous_size * bits_per_byte
14405 - bit_offset - FIELD_BITSIZE (*fp)));
14406 }
14407 }
14408 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14409 if (attr != NULL)
14410 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14411 + attr->constant_value (0)));
14412
14413 /* Get name of field. */
14414 fieldname = dwarf2_name (die, cu);
14415 if (fieldname == NULL)
14416 fieldname = "";
14417
14418 /* The name is already allocated along with this objfile, so we don't
14419 need to duplicate it for the type. */
14420 fp->name = fieldname;
14421
14422 /* Change accessibility for artificial fields (e.g. virtual table
14423 pointer or virtual base class pointer) to private. */
14424 if (dwarf2_attr (die, DW_AT_artificial, cu))
14425 {
14426 FIELD_ARTIFICIAL (*fp) = 1;
14427 new_field->accessibility = DW_ACCESS_private;
14428 fip->non_public_fields = 1;
14429 }
14430 }
14431 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14432 {
14433 /* C++ static member. */
14434
14435 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14436 is a declaration, but all versions of G++ as of this writing
14437 (so through at least 3.2.1) incorrectly generate
14438 DW_TAG_variable tags. */
14439
14440 const char *physname;
14441
14442 /* Get name of field. */
14443 fieldname = dwarf2_name (die, cu);
14444 if (fieldname == NULL)
14445 return;
14446
14447 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14448 if (attr
14449 /* Only create a symbol if this is an external value.
14450 new_symbol checks this and puts the value in the global symbol
14451 table, which we want. If it is not external, new_symbol
14452 will try to put the value in cu->list_in_scope which is wrong. */
14453 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14454 {
14455 /* A static const member, not much different than an enum as far as
14456 we're concerned, except that we can support more types. */
14457 new_symbol (die, NULL, cu);
14458 }
14459
14460 /* Get physical name. */
14461 physname = dwarf2_physname (fieldname, die, cu);
14462
14463 /* The name is already allocated along with this objfile, so we don't
14464 need to duplicate it for the type. */
14465 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14466 FIELD_TYPE (*fp) = die_type (die, cu);
14467 FIELD_NAME (*fp) = fieldname;
14468 }
14469 else if (die->tag == DW_TAG_inheritance)
14470 {
14471 /* C++ base class field. */
14472 handle_data_member_location (die, cu, fp);
14473 FIELD_BITSIZE (*fp) = 0;
14474 FIELD_TYPE (*fp) = die_type (die, cu);
14475 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14476 }
14477 else
14478 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14479 }
14480
14481 /* Can the type given by DIE define another type? */
14482
14483 static bool
14484 type_can_define_types (const struct die_info *die)
14485 {
14486 switch (die->tag)
14487 {
14488 case DW_TAG_typedef:
14489 case DW_TAG_class_type:
14490 case DW_TAG_structure_type:
14491 case DW_TAG_union_type:
14492 case DW_TAG_enumeration_type:
14493 return true;
14494
14495 default:
14496 return false;
14497 }
14498 }
14499
14500 /* Add a type definition defined in the scope of the FIP's class. */
14501
14502 static void
14503 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14504 struct dwarf2_cu *cu)
14505 {
14506 struct decl_field fp;
14507 memset (&fp, 0, sizeof (fp));
14508
14509 gdb_assert (type_can_define_types (die));
14510
14511 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14512 fp.name = dwarf2_name (die, cu);
14513 fp.type = read_type_die (die, cu);
14514
14515 /* Save accessibility. */
14516 enum dwarf_access_attribute accessibility;
14517 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14518 if (attr != NULL)
14519 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14520 else
14521 accessibility = dwarf2_default_access_attribute (die, cu);
14522 switch (accessibility)
14523 {
14524 case DW_ACCESS_public:
14525 /* The assumed value if neither private nor protected. */
14526 break;
14527 case DW_ACCESS_private:
14528 fp.is_private = 1;
14529 break;
14530 case DW_ACCESS_protected:
14531 fp.is_protected = 1;
14532 break;
14533 default:
14534 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14535 }
14536
14537 if (die->tag == DW_TAG_typedef)
14538 fip->typedef_field_list.push_back (fp);
14539 else
14540 fip->nested_types_list.push_back (fp);
14541 }
14542
14543 /* A convenience typedef that's used when finding the discriminant
14544 field for a variant part. */
14545 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14546 offset_map_type;
14547
14548 /* Compute the discriminant range for a given variant. OBSTACK is
14549 where the results will be stored. VARIANT is the variant to
14550 process. IS_UNSIGNED indicates whether the discriminant is signed
14551 or unsigned. */
14552
14553 static const gdb::array_view<discriminant_range>
14554 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14555 bool is_unsigned)
14556 {
14557 std::vector<discriminant_range> ranges;
14558
14559 if (variant.default_branch)
14560 return {};
14561
14562 if (variant.discr_list_data == nullptr)
14563 {
14564 discriminant_range r
14565 = {variant.discriminant_value, variant.discriminant_value};
14566 ranges.push_back (r);
14567 }
14568 else
14569 {
14570 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14571 variant.discr_list_data->size);
14572 while (!data.empty ())
14573 {
14574 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14575 {
14576 complaint (_("invalid discriminant marker: %d"), data[0]);
14577 break;
14578 }
14579 bool is_range = data[0] == DW_DSC_range;
14580 data = data.slice (1);
14581
14582 ULONGEST low, high;
14583 unsigned int bytes_read;
14584
14585 if (data.empty ())
14586 {
14587 complaint (_("DW_AT_discr_list missing low value"));
14588 break;
14589 }
14590 if (is_unsigned)
14591 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14592 else
14593 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14594 &bytes_read);
14595 data = data.slice (bytes_read);
14596
14597 if (is_range)
14598 {
14599 if (data.empty ())
14600 {
14601 complaint (_("DW_AT_discr_list missing high value"));
14602 break;
14603 }
14604 if (is_unsigned)
14605 high = read_unsigned_leb128 (nullptr, data.data (),
14606 &bytes_read);
14607 else
14608 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14609 &bytes_read);
14610 data = data.slice (bytes_read);
14611 }
14612 else
14613 high = low;
14614
14615 ranges.push_back ({ low, high });
14616 }
14617 }
14618
14619 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14620 ranges.size ());
14621 std::copy (ranges.begin (), ranges.end (), result);
14622 return gdb::array_view<discriminant_range> (result, ranges.size ());
14623 }
14624
14625 static const gdb::array_view<variant_part> create_variant_parts
14626 (struct obstack *obstack,
14627 const offset_map_type &offset_map,
14628 struct field_info *fi,
14629 const std::vector<variant_part_builder> &variant_parts);
14630
14631 /* Fill in a "struct variant" for a given variant field. RESULT is
14632 the variant to fill in. OBSTACK is where any needed allocations
14633 will be done. OFFSET_MAP holds the mapping from section offsets to
14634 fields for the type. FI describes the fields of the type we're
14635 processing. FIELD is the variant field we're converting. */
14636
14637 static void
14638 create_one_variant (variant &result, struct obstack *obstack,
14639 const offset_map_type &offset_map,
14640 struct field_info *fi, const variant_field &field)
14641 {
14642 result.discriminants = convert_variant_range (obstack, field, false);
14643 result.first_field = field.first_field + fi->baseclasses.size ();
14644 result.last_field = field.last_field + fi->baseclasses.size ();
14645 result.parts = create_variant_parts (obstack, offset_map, fi,
14646 field.variant_parts);
14647 }
14648
14649 /* Fill in a "struct variant_part" for a given variant part. RESULT
14650 is the variant part to fill in. OBSTACK is where any needed
14651 allocations will be done. OFFSET_MAP holds the mapping from
14652 section offsets to fields for the type. FI describes the fields of
14653 the type we're processing. BUILDER is the variant part to be
14654 converted. */
14655
14656 static void
14657 create_one_variant_part (variant_part &result,
14658 struct obstack *obstack,
14659 const offset_map_type &offset_map,
14660 struct field_info *fi,
14661 const variant_part_builder &builder)
14662 {
14663 auto iter = offset_map.find (builder.discriminant_offset);
14664 if (iter == offset_map.end ())
14665 {
14666 result.discriminant_index = -1;
14667 /* Doesn't matter. */
14668 result.is_unsigned = false;
14669 }
14670 else
14671 {
14672 result.discriminant_index = iter->second;
14673 result.is_unsigned
14674 = TYPE_UNSIGNED (FIELD_TYPE
14675 (fi->fields[result.discriminant_index].field));
14676 }
14677
14678 size_t n = builder.variants.size ();
14679 variant *output = new (obstack) variant[n];
14680 for (size_t i = 0; i < n; ++i)
14681 create_one_variant (output[i], obstack, offset_map, fi,
14682 builder.variants[i]);
14683
14684 result.variants = gdb::array_view<variant> (output, n);
14685 }
14686
14687 /* Create a vector of variant parts that can be attached to a type.
14688 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14689 holds the mapping from section offsets to fields for the type. FI
14690 describes the fields of the type we're processing. VARIANT_PARTS
14691 is the vector to convert. */
14692
14693 static const gdb::array_view<variant_part>
14694 create_variant_parts (struct obstack *obstack,
14695 const offset_map_type &offset_map,
14696 struct field_info *fi,
14697 const std::vector<variant_part_builder> &variant_parts)
14698 {
14699 if (variant_parts.empty ())
14700 return {};
14701
14702 size_t n = variant_parts.size ();
14703 variant_part *result = new (obstack) variant_part[n];
14704 for (size_t i = 0; i < n; ++i)
14705 create_one_variant_part (result[i], obstack, offset_map, fi,
14706 variant_parts[i]);
14707
14708 return gdb::array_view<variant_part> (result, n);
14709 }
14710
14711 /* Compute the variant part vector for FIP, attaching it to TYPE when
14712 done. */
14713
14714 static void
14715 add_variant_property (struct field_info *fip, struct type *type,
14716 struct dwarf2_cu *cu)
14717 {
14718 /* Map section offsets of fields to their field index. Note the
14719 field index here does not take the number of baseclasses into
14720 account. */
14721 offset_map_type offset_map;
14722 for (int i = 0; i < fip->fields.size (); ++i)
14723 offset_map[fip->fields[i].offset] = i;
14724
14725 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14726 gdb::array_view<variant_part> parts
14727 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14728 fip->variant_parts);
14729
14730 struct dynamic_prop prop;
14731 prop.kind = PROP_VARIANT_PARTS;
14732 prop.data.variant_parts
14733 = ((gdb::array_view<variant_part> *)
14734 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14735
14736 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14737 }
14738
14739 /* Create the vector of fields, and attach it to the type. */
14740
14741 static void
14742 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14743 struct dwarf2_cu *cu)
14744 {
14745 int nfields = fip->nfields ();
14746
14747 /* Record the field count, allocate space for the array of fields,
14748 and create blank accessibility bitfields if necessary. */
14749 TYPE_NFIELDS (type) = nfields;
14750 TYPE_FIELDS (type) = (struct field *)
14751 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14752
14753 if (fip->non_public_fields && cu->language != language_ada)
14754 {
14755 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14756
14757 TYPE_FIELD_PRIVATE_BITS (type) =
14758 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14759 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14760
14761 TYPE_FIELD_PROTECTED_BITS (type) =
14762 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14763 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14764
14765 TYPE_FIELD_IGNORE_BITS (type) =
14766 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14767 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14768 }
14769
14770 /* If the type has baseclasses, allocate and clear a bit vector for
14771 TYPE_FIELD_VIRTUAL_BITS. */
14772 if (!fip->baseclasses.empty () && cu->language != language_ada)
14773 {
14774 int num_bytes = B_BYTES (fip->baseclasses.size ());
14775 unsigned char *pointer;
14776
14777 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14778 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14779 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14780 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14781 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14782 }
14783
14784 if (!fip->variant_parts.empty ())
14785 add_variant_property (fip, type, cu);
14786
14787 /* Copy the saved-up fields into the field vector. */
14788 for (int i = 0; i < nfields; ++i)
14789 {
14790 struct nextfield &field
14791 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14792 : fip->fields[i - fip->baseclasses.size ()]);
14793
14794 TYPE_FIELD (type, i) = field.field;
14795 switch (field.accessibility)
14796 {
14797 case DW_ACCESS_private:
14798 if (cu->language != language_ada)
14799 SET_TYPE_FIELD_PRIVATE (type, i);
14800 break;
14801
14802 case DW_ACCESS_protected:
14803 if (cu->language != language_ada)
14804 SET_TYPE_FIELD_PROTECTED (type, i);
14805 break;
14806
14807 case DW_ACCESS_public:
14808 break;
14809
14810 default:
14811 /* Unknown accessibility. Complain and treat it as public. */
14812 {
14813 complaint (_("unsupported accessibility %d"),
14814 field.accessibility);
14815 }
14816 break;
14817 }
14818 if (i < fip->baseclasses.size ())
14819 {
14820 switch (field.virtuality)
14821 {
14822 case DW_VIRTUALITY_virtual:
14823 case DW_VIRTUALITY_pure_virtual:
14824 if (cu->language == language_ada)
14825 error (_("unexpected virtuality in component of Ada type"));
14826 SET_TYPE_FIELD_VIRTUAL (type, i);
14827 break;
14828 }
14829 }
14830 }
14831 }
14832
14833 /* Return true if this member function is a constructor, false
14834 otherwise. */
14835
14836 static int
14837 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14838 {
14839 const char *fieldname;
14840 const char *type_name;
14841 int len;
14842
14843 if (die->parent == NULL)
14844 return 0;
14845
14846 if (die->parent->tag != DW_TAG_structure_type
14847 && die->parent->tag != DW_TAG_union_type
14848 && die->parent->tag != DW_TAG_class_type)
14849 return 0;
14850
14851 fieldname = dwarf2_name (die, cu);
14852 type_name = dwarf2_name (die->parent, cu);
14853 if (fieldname == NULL || type_name == NULL)
14854 return 0;
14855
14856 len = strlen (fieldname);
14857 return (strncmp (fieldname, type_name, len) == 0
14858 && (type_name[len] == '\0' || type_name[len] == '<'));
14859 }
14860
14861 /* Check if the given VALUE is a recognized enum
14862 dwarf_defaulted_attribute constant according to DWARF5 spec,
14863 Table 7.24. */
14864
14865 static bool
14866 is_valid_DW_AT_defaulted (ULONGEST value)
14867 {
14868 switch (value)
14869 {
14870 case DW_DEFAULTED_no:
14871 case DW_DEFAULTED_in_class:
14872 case DW_DEFAULTED_out_of_class:
14873 return true;
14874 }
14875
14876 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14877 return false;
14878 }
14879
14880 /* Add a member function to the proper fieldlist. */
14881
14882 static void
14883 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14884 struct type *type, struct dwarf2_cu *cu)
14885 {
14886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14887 struct attribute *attr;
14888 int i;
14889 struct fnfieldlist *flp = nullptr;
14890 struct fn_field *fnp;
14891 const char *fieldname;
14892 struct type *this_type;
14893 enum dwarf_access_attribute accessibility;
14894
14895 if (cu->language == language_ada)
14896 error (_("unexpected member function in Ada type"));
14897
14898 /* Get name of member function. */
14899 fieldname = dwarf2_name (die, cu);
14900 if (fieldname == NULL)
14901 return;
14902
14903 /* Look up member function name in fieldlist. */
14904 for (i = 0; i < fip->fnfieldlists.size (); i++)
14905 {
14906 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14907 {
14908 flp = &fip->fnfieldlists[i];
14909 break;
14910 }
14911 }
14912
14913 /* Create a new fnfieldlist if necessary. */
14914 if (flp == nullptr)
14915 {
14916 fip->fnfieldlists.emplace_back ();
14917 flp = &fip->fnfieldlists.back ();
14918 flp->name = fieldname;
14919 i = fip->fnfieldlists.size () - 1;
14920 }
14921
14922 /* Create a new member function field and add it to the vector of
14923 fnfieldlists. */
14924 flp->fnfields.emplace_back ();
14925 fnp = &flp->fnfields.back ();
14926
14927 /* Delay processing of the physname until later. */
14928 if (cu->language == language_cplus)
14929 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14930 die, cu);
14931 else
14932 {
14933 const char *physname = dwarf2_physname (fieldname, die, cu);
14934 fnp->physname = physname ? physname : "";
14935 }
14936
14937 fnp->type = alloc_type (objfile);
14938 this_type = read_type_die (die, cu);
14939 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14940 {
14941 int nparams = TYPE_NFIELDS (this_type);
14942
14943 /* TYPE is the domain of this method, and THIS_TYPE is the type
14944 of the method itself (TYPE_CODE_METHOD). */
14945 smash_to_method_type (fnp->type, type,
14946 TYPE_TARGET_TYPE (this_type),
14947 TYPE_FIELDS (this_type),
14948 TYPE_NFIELDS (this_type),
14949 TYPE_VARARGS (this_type));
14950
14951 /* Handle static member functions.
14952 Dwarf2 has no clean way to discern C++ static and non-static
14953 member functions. G++ helps GDB by marking the first
14954 parameter for non-static member functions (which is the this
14955 pointer) as artificial. We obtain this information from
14956 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14957 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14958 fnp->voffset = VOFFSET_STATIC;
14959 }
14960 else
14961 complaint (_("member function type missing for '%s'"),
14962 dwarf2_full_name (fieldname, die, cu));
14963
14964 /* Get fcontext from DW_AT_containing_type if present. */
14965 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14966 fnp->fcontext = die_containing_type (die, cu);
14967
14968 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14969 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14970
14971 /* Get accessibility. */
14972 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14973 if (attr != nullptr)
14974 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14975 else
14976 accessibility = dwarf2_default_access_attribute (die, cu);
14977 switch (accessibility)
14978 {
14979 case DW_ACCESS_private:
14980 fnp->is_private = 1;
14981 break;
14982 case DW_ACCESS_protected:
14983 fnp->is_protected = 1;
14984 break;
14985 }
14986
14987 /* Check for artificial methods. */
14988 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14989 if (attr && DW_UNSND (attr) != 0)
14990 fnp->is_artificial = 1;
14991
14992 /* Check for defaulted methods. */
14993 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14994 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14995 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14996
14997 /* Check for deleted methods. */
14998 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14999 if (attr != nullptr && DW_UNSND (attr) != 0)
15000 fnp->is_deleted = 1;
15001
15002 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15003
15004 /* Get index in virtual function table if it is a virtual member
15005 function. For older versions of GCC, this is an offset in the
15006 appropriate virtual table, as specified by DW_AT_containing_type.
15007 For everyone else, it is an expression to be evaluated relative
15008 to the object address. */
15009
15010 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15011 if (attr != nullptr)
15012 {
15013 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15014 {
15015 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15016 {
15017 /* Old-style GCC. */
15018 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15019 }
15020 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15021 || (DW_BLOCK (attr)->size > 1
15022 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15023 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15024 {
15025 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15026 if ((fnp->voffset % cu->header.addr_size) != 0)
15027 dwarf2_complex_location_expr_complaint ();
15028 else
15029 fnp->voffset /= cu->header.addr_size;
15030 fnp->voffset += 2;
15031 }
15032 else
15033 dwarf2_complex_location_expr_complaint ();
15034
15035 if (!fnp->fcontext)
15036 {
15037 /* If there is no `this' field and no DW_AT_containing_type,
15038 we cannot actually find a base class context for the
15039 vtable! */
15040 if (TYPE_NFIELDS (this_type) == 0
15041 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15042 {
15043 complaint (_("cannot determine context for virtual member "
15044 "function \"%s\" (offset %s)"),
15045 fieldname, sect_offset_str (die->sect_off));
15046 }
15047 else
15048 {
15049 fnp->fcontext
15050 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15051 }
15052 }
15053 }
15054 else if (attr->form_is_section_offset ())
15055 {
15056 dwarf2_complex_location_expr_complaint ();
15057 }
15058 else
15059 {
15060 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15061 fieldname);
15062 }
15063 }
15064 else
15065 {
15066 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15067 if (attr && DW_UNSND (attr))
15068 {
15069 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15070 complaint (_("Member function \"%s\" (offset %s) is virtual "
15071 "but the vtable offset is not specified"),
15072 fieldname, sect_offset_str (die->sect_off));
15073 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15074 TYPE_CPLUS_DYNAMIC (type) = 1;
15075 }
15076 }
15077 }
15078
15079 /* Create the vector of member function fields, and attach it to the type. */
15080
15081 static void
15082 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15083 struct dwarf2_cu *cu)
15084 {
15085 if (cu->language == language_ada)
15086 error (_("unexpected member functions in Ada type"));
15087
15088 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15089 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15090 TYPE_ALLOC (type,
15091 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15092
15093 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15094 {
15095 struct fnfieldlist &nf = fip->fnfieldlists[i];
15096 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15097
15098 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15099 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15100 fn_flp->fn_fields = (struct fn_field *)
15101 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15102
15103 for (int k = 0; k < nf.fnfields.size (); ++k)
15104 fn_flp->fn_fields[k] = nf.fnfields[k];
15105 }
15106
15107 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15108 }
15109
15110 /* Returns non-zero if NAME is the name of a vtable member in CU's
15111 language, zero otherwise. */
15112 static int
15113 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15114 {
15115 static const char vptr[] = "_vptr";
15116
15117 /* Look for the C++ form of the vtable. */
15118 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15119 return 1;
15120
15121 return 0;
15122 }
15123
15124 /* GCC outputs unnamed structures that are really pointers to member
15125 functions, with the ABI-specified layout. If TYPE describes
15126 such a structure, smash it into a member function type.
15127
15128 GCC shouldn't do this; it should just output pointer to member DIEs.
15129 This is GCC PR debug/28767. */
15130
15131 static void
15132 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15133 {
15134 struct type *pfn_type, *self_type, *new_type;
15135
15136 /* Check for a structure with no name and two children. */
15137 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15138 return;
15139
15140 /* Check for __pfn and __delta members. */
15141 if (TYPE_FIELD_NAME (type, 0) == NULL
15142 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15143 || TYPE_FIELD_NAME (type, 1) == NULL
15144 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15145 return;
15146
15147 /* Find the type of the method. */
15148 pfn_type = TYPE_FIELD_TYPE (type, 0);
15149 if (pfn_type == NULL
15150 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15151 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15152 return;
15153
15154 /* Look for the "this" argument. */
15155 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15156 if (TYPE_NFIELDS (pfn_type) == 0
15157 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15158 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15159 return;
15160
15161 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15162 new_type = alloc_type (objfile);
15163 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15164 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15165 TYPE_VARARGS (pfn_type));
15166 smash_to_methodptr_type (type, new_type);
15167 }
15168
15169 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15170 appropriate error checking and issuing complaints if there is a
15171 problem. */
15172
15173 static ULONGEST
15174 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15175 {
15176 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15177
15178 if (attr == nullptr)
15179 return 0;
15180
15181 if (!attr->form_is_constant ())
15182 {
15183 complaint (_("DW_AT_alignment must have constant form"
15184 " - DIE at %s [in module %s]"),
15185 sect_offset_str (die->sect_off),
15186 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15187 return 0;
15188 }
15189
15190 ULONGEST align;
15191 if (attr->form == DW_FORM_sdata)
15192 {
15193 LONGEST val = DW_SND (attr);
15194 if (val < 0)
15195 {
15196 complaint (_("DW_AT_alignment value must not be negative"
15197 " - DIE at %s [in module %s]"),
15198 sect_offset_str (die->sect_off),
15199 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15200 return 0;
15201 }
15202 align = val;
15203 }
15204 else
15205 align = DW_UNSND (attr);
15206
15207 if (align == 0)
15208 {
15209 complaint (_("DW_AT_alignment value must not be zero"
15210 " - DIE at %s [in module %s]"),
15211 sect_offset_str (die->sect_off),
15212 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15213 return 0;
15214 }
15215 if ((align & (align - 1)) != 0)
15216 {
15217 complaint (_("DW_AT_alignment value must be a power of 2"
15218 " - DIE at %s [in module %s]"),
15219 sect_offset_str (die->sect_off),
15220 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15221 return 0;
15222 }
15223
15224 return align;
15225 }
15226
15227 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15228 the alignment for TYPE. */
15229
15230 static void
15231 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15232 struct type *type)
15233 {
15234 if (!set_type_align (type, get_alignment (cu, die)))
15235 complaint (_("DW_AT_alignment value too large"
15236 " - DIE at %s [in module %s]"),
15237 sect_offset_str (die->sect_off),
15238 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15239 }
15240
15241 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15242 constant for a type, according to DWARF5 spec, Table 5.5. */
15243
15244 static bool
15245 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15246 {
15247 switch (value)
15248 {
15249 case DW_CC_normal:
15250 case DW_CC_pass_by_reference:
15251 case DW_CC_pass_by_value:
15252 return true;
15253
15254 default:
15255 complaint (_("unrecognized DW_AT_calling_convention value "
15256 "(%s) for a type"), pulongest (value));
15257 return false;
15258 }
15259 }
15260
15261 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15262 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15263 also according to GNU-specific values (see include/dwarf2.h). */
15264
15265 static bool
15266 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15267 {
15268 switch (value)
15269 {
15270 case DW_CC_normal:
15271 case DW_CC_program:
15272 case DW_CC_nocall:
15273 return true;
15274
15275 case DW_CC_GNU_renesas_sh:
15276 case DW_CC_GNU_borland_fastcall_i386:
15277 case DW_CC_GDB_IBM_OpenCL:
15278 return true;
15279
15280 default:
15281 complaint (_("unrecognized DW_AT_calling_convention value "
15282 "(%s) for a subroutine"), pulongest (value));
15283 return false;
15284 }
15285 }
15286
15287 /* Called when we find the DIE that starts a structure or union scope
15288 (definition) to create a type for the structure or union. Fill in
15289 the type's name and general properties; the members will not be
15290 processed until process_structure_scope. A symbol table entry for
15291 the type will also not be done until process_structure_scope (assuming
15292 the type has a name).
15293
15294 NOTE: we need to call these functions regardless of whether or not the
15295 DIE has a DW_AT_name attribute, since it might be an anonymous
15296 structure or union. This gets the type entered into our set of
15297 user defined types. */
15298
15299 static struct type *
15300 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15301 {
15302 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15303 struct type *type;
15304 struct attribute *attr;
15305 const char *name;
15306
15307 /* If the definition of this type lives in .debug_types, read that type.
15308 Don't follow DW_AT_specification though, that will take us back up
15309 the chain and we want to go down. */
15310 attr = die->attr (DW_AT_signature);
15311 if (attr != nullptr)
15312 {
15313 type = get_DW_AT_signature_type (die, attr, cu);
15314
15315 /* The type's CU may not be the same as CU.
15316 Ensure TYPE is recorded with CU in die_type_hash. */
15317 return set_die_type (die, type, cu);
15318 }
15319
15320 type = alloc_type (objfile);
15321 INIT_CPLUS_SPECIFIC (type);
15322
15323 name = dwarf2_name (die, cu);
15324 if (name != NULL)
15325 {
15326 if (cu->language == language_cplus
15327 || cu->language == language_d
15328 || cu->language == language_rust)
15329 {
15330 const char *full_name = dwarf2_full_name (name, die, cu);
15331
15332 /* dwarf2_full_name might have already finished building the DIE's
15333 type. If so, there is no need to continue. */
15334 if (get_die_type (die, cu) != NULL)
15335 return get_die_type (die, cu);
15336
15337 TYPE_NAME (type) = full_name;
15338 }
15339 else
15340 {
15341 /* The name is already allocated along with this objfile, so
15342 we don't need to duplicate it for the type. */
15343 TYPE_NAME (type) = name;
15344 }
15345 }
15346
15347 if (die->tag == DW_TAG_structure_type)
15348 {
15349 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15350 }
15351 else if (die->tag == DW_TAG_union_type)
15352 {
15353 TYPE_CODE (type) = TYPE_CODE_UNION;
15354 }
15355 else
15356 {
15357 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15358 }
15359
15360 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15361 TYPE_DECLARED_CLASS (type) = 1;
15362
15363 /* Store the calling convention in the type if it's available in
15364 the die. Otherwise the calling convention remains set to
15365 the default value DW_CC_normal. */
15366 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15367 if (attr != nullptr
15368 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15369 {
15370 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15371 TYPE_CPLUS_CALLING_CONVENTION (type)
15372 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15373 }
15374
15375 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15376 if (attr != nullptr)
15377 {
15378 if (attr->form_is_constant ())
15379 TYPE_LENGTH (type) = DW_UNSND (attr);
15380 else
15381 {
15382 struct dynamic_prop prop;
15383 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15384 cu->per_cu->addr_type ()))
15385 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15386 TYPE_LENGTH (type) = 0;
15387 }
15388 }
15389 else
15390 {
15391 TYPE_LENGTH (type) = 0;
15392 }
15393
15394 maybe_set_alignment (cu, die, type);
15395
15396 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15397 {
15398 /* ICC<14 does not output the required DW_AT_declaration on
15399 incomplete types, but gives them a size of zero. */
15400 TYPE_STUB (type) = 1;
15401 }
15402 else
15403 TYPE_STUB_SUPPORTED (type) = 1;
15404
15405 if (die_is_declaration (die, cu))
15406 TYPE_STUB (type) = 1;
15407 else if (attr == NULL && die->child == NULL
15408 && producer_is_realview (cu->producer))
15409 /* RealView does not output the required DW_AT_declaration
15410 on incomplete types. */
15411 TYPE_STUB (type) = 1;
15412
15413 /* We need to add the type field to the die immediately so we don't
15414 infinitely recurse when dealing with pointers to the structure
15415 type within the structure itself. */
15416 set_die_type (die, type, cu);
15417
15418 /* set_die_type should be already done. */
15419 set_descriptive_type (type, die, cu);
15420
15421 return type;
15422 }
15423
15424 static void handle_struct_member_die
15425 (struct die_info *child_die,
15426 struct type *type,
15427 struct field_info *fi,
15428 std::vector<struct symbol *> *template_args,
15429 struct dwarf2_cu *cu);
15430
15431 /* A helper for handle_struct_member_die that handles
15432 DW_TAG_variant_part. */
15433
15434 static void
15435 handle_variant_part (struct die_info *die, struct type *type,
15436 struct field_info *fi,
15437 std::vector<struct symbol *> *template_args,
15438 struct dwarf2_cu *cu)
15439 {
15440 variant_part_builder *new_part;
15441 if (fi->current_variant_part == nullptr)
15442 {
15443 fi->variant_parts.emplace_back ();
15444 new_part = &fi->variant_parts.back ();
15445 }
15446 else if (!fi->current_variant_part->processing_variant)
15447 {
15448 complaint (_("nested DW_TAG_variant_part seen "
15449 "- DIE at %s [in module %s]"),
15450 sect_offset_str (die->sect_off),
15451 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15452 return;
15453 }
15454 else
15455 {
15456 variant_field &current = fi->current_variant_part->variants.back ();
15457 current.variant_parts.emplace_back ();
15458 new_part = &current.variant_parts.back ();
15459 }
15460
15461 /* When we recurse, we want callees to add to this new variant
15462 part. */
15463 scoped_restore save_current_variant_part
15464 = make_scoped_restore (&fi->current_variant_part, new_part);
15465
15466 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15467 if (discr == NULL)
15468 {
15469 /* It's a univariant form, an extension we support. */
15470 }
15471 else if (discr->form_is_ref ())
15472 {
15473 struct dwarf2_cu *target_cu = cu;
15474 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15475
15476 new_part->discriminant_offset = target_die->sect_off;
15477 }
15478 else
15479 {
15480 complaint (_("DW_AT_discr does not have DIE reference form"
15481 " - DIE at %s [in module %s]"),
15482 sect_offset_str (die->sect_off),
15483 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15484 }
15485
15486 for (die_info *child_die = die->child;
15487 child_die != NULL;
15488 child_die = child_die->sibling)
15489 handle_struct_member_die (child_die, type, fi, template_args, cu);
15490 }
15491
15492 /* A helper for handle_struct_member_die that handles
15493 DW_TAG_variant. */
15494
15495 static void
15496 handle_variant (struct die_info *die, struct type *type,
15497 struct field_info *fi,
15498 std::vector<struct symbol *> *template_args,
15499 struct dwarf2_cu *cu)
15500 {
15501 if (fi->current_variant_part == nullptr)
15502 {
15503 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15504 "- DIE at %s [in module %s]"),
15505 sect_offset_str (die->sect_off),
15506 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15507 return;
15508 }
15509 if (fi->current_variant_part->processing_variant)
15510 {
15511 complaint (_("nested DW_TAG_variant seen "
15512 "- DIE at %s [in module %s]"),
15513 sect_offset_str (die->sect_off),
15514 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15515 return;
15516 }
15517
15518 scoped_restore save_processing_variant
15519 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15520 true);
15521
15522 fi->current_variant_part->variants.emplace_back ();
15523 variant_field &variant = fi->current_variant_part->variants.back ();
15524 variant.first_field = fi->fields.size ();
15525
15526 /* In a variant we want to get the discriminant and also add a
15527 field for our sole member child. */
15528 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15529 if (discr == nullptr)
15530 {
15531 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15532 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15533 variant.default_branch = true;
15534 else
15535 variant.discr_list_data = DW_BLOCK (discr);
15536 }
15537 else
15538 variant.discriminant_value = DW_UNSND (discr);
15539
15540 for (die_info *variant_child = die->child;
15541 variant_child != NULL;
15542 variant_child = variant_child->sibling)
15543 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15544
15545 variant.last_field = fi->fields.size ();
15546 }
15547
15548 /* A helper for process_structure_scope that handles a single member
15549 DIE. */
15550
15551 static void
15552 handle_struct_member_die (struct die_info *child_die, struct type *type,
15553 struct field_info *fi,
15554 std::vector<struct symbol *> *template_args,
15555 struct dwarf2_cu *cu)
15556 {
15557 if (child_die->tag == DW_TAG_member
15558 || child_die->tag == DW_TAG_variable)
15559 {
15560 /* NOTE: carlton/2002-11-05: A C++ static data member
15561 should be a DW_TAG_member that is a declaration, but
15562 all versions of G++ as of this writing (so through at
15563 least 3.2.1) incorrectly generate DW_TAG_variable
15564 tags for them instead. */
15565 dwarf2_add_field (fi, child_die, cu);
15566 }
15567 else if (child_die->tag == DW_TAG_subprogram)
15568 {
15569 /* Rust doesn't have member functions in the C++ sense.
15570 However, it does emit ordinary functions as children
15571 of a struct DIE. */
15572 if (cu->language == language_rust)
15573 read_func_scope (child_die, cu);
15574 else
15575 {
15576 /* C++ member function. */
15577 dwarf2_add_member_fn (fi, child_die, type, cu);
15578 }
15579 }
15580 else if (child_die->tag == DW_TAG_inheritance)
15581 {
15582 /* C++ base class field. */
15583 dwarf2_add_field (fi, child_die, cu);
15584 }
15585 else if (type_can_define_types (child_die))
15586 dwarf2_add_type_defn (fi, child_die, cu);
15587 else if (child_die->tag == DW_TAG_template_type_param
15588 || child_die->tag == DW_TAG_template_value_param)
15589 {
15590 struct symbol *arg = new_symbol (child_die, NULL, cu);
15591
15592 if (arg != NULL)
15593 template_args->push_back (arg);
15594 }
15595 else if (child_die->tag == DW_TAG_variant_part)
15596 handle_variant_part (child_die, type, fi, template_args, cu);
15597 else if (child_die->tag == DW_TAG_variant)
15598 handle_variant (child_die, type, fi, template_args, cu);
15599 }
15600
15601 /* Finish creating a structure or union type, including filling in
15602 its members and creating a symbol for it. */
15603
15604 static void
15605 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15606 {
15607 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15608 struct die_info *child_die;
15609 struct type *type;
15610
15611 type = get_die_type (die, cu);
15612 if (type == NULL)
15613 type = read_structure_type (die, cu);
15614
15615 bool has_template_parameters = false;
15616 if (die->child != NULL && ! die_is_declaration (die, cu))
15617 {
15618 struct field_info fi;
15619 std::vector<struct symbol *> template_args;
15620
15621 child_die = die->child;
15622
15623 while (child_die && child_die->tag)
15624 {
15625 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15626 child_die = child_die->sibling;
15627 }
15628
15629 /* Attach template arguments to type. */
15630 if (!template_args.empty ())
15631 {
15632 has_template_parameters = true;
15633 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15634 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15635 TYPE_TEMPLATE_ARGUMENTS (type)
15636 = XOBNEWVEC (&objfile->objfile_obstack,
15637 struct symbol *,
15638 TYPE_N_TEMPLATE_ARGUMENTS (type));
15639 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15640 template_args.data (),
15641 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15642 * sizeof (struct symbol *)));
15643 }
15644
15645 /* Attach fields and member functions to the type. */
15646 if (fi.nfields () > 0)
15647 dwarf2_attach_fields_to_type (&fi, type, cu);
15648 if (!fi.fnfieldlists.empty ())
15649 {
15650 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15651
15652 /* Get the type which refers to the base class (possibly this
15653 class itself) which contains the vtable pointer for the current
15654 class from the DW_AT_containing_type attribute. This use of
15655 DW_AT_containing_type is a GNU extension. */
15656
15657 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15658 {
15659 struct type *t = die_containing_type (die, cu);
15660
15661 set_type_vptr_basetype (type, t);
15662 if (type == t)
15663 {
15664 int i;
15665
15666 /* Our own class provides vtbl ptr. */
15667 for (i = TYPE_NFIELDS (t) - 1;
15668 i >= TYPE_N_BASECLASSES (t);
15669 --i)
15670 {
15671 const char *fieldname = TYPE_FIELD_NAME (t, i);
15672
15673 if (is_vtable_name (fieldname, cu))
15674 {
15675 set_type_vptr_fieldno (type, i);
15676 break;
15677 }
15678 }
15679
15680 /* Complain if virtual function table field not found. */
15681 if (i < TYPE_N_BASECLASSES (t))
15682 complaint (_("virtual function table pointer "
15683 "not found when defining class '%s'"),
15684 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15685 }
15686 else
15687 {
15688 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15689 }
15690 }
15691 else if (cu->producer
15692 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15693 {
15694 /* The IBM XLC compiler does not provide direct indication
15695 of the containing type, but the vtable pointer is
15696 always named __vfp. */
15697
15698 int i;
15699
15700 for (i = TYPE_NFIELDS (type) - 1;
15701 i >= TYPE_N_BASECLASSES (type);
15702 --i)
15703 {
15704 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15705 {
15706 set_type_vptr_fieldno (type, i);
15707 set_type_vptr_basetype (type, type);
15708 break;
15709 }
15710 }
15711 }
15712 }
15713
15714 /* Copy fi.typedef_field_list linked list elements content into the
15715 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15716 if (!fi.typedef_field_list.empty ())
15717 {
15718 int count = fi.typedef_field_list.size ();
15719
15720 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15721 TYPE_TYPEDEF_FIELD_ARRAY (type)
15722 = ((struct decl_field *)
15723 TYPE_ALLOC (type,
15724 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15725 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15726
15727 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15728 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15729 }
15730
15731 /* Copy fi.nested_types_list linked list elements content into the
15732 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15733 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15734 {
15735 int count = fi.nested_types_list.size ();
15736
15737 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15738 TYPE_NESTED_TYPES_ARRAY (type)
15739 = ((struct decl_field *)
15740 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15741 TYPE_NESTED_TYPES_COUNT (type) = count;
15742
15743 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15744 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15745 }
15746 }
15747
15748 quirk_gcc_member_function_pointer (type, objfile);
15749 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15750 cu->rust_unions.push_back (type);
15751
15752 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15753 snapshots) has been known to create a die giving a declaration
15754 for a class that has, as a child, a die giving a definition for a
15755 nested class. So we have to process our children even if the
15756 current die is a declaration. Normally, of course, a declaration
15757 won't have any children at all. */
15758
15759 child_die = die->child;
15760
15761 while (child_die != NULL && child_die->tag)
15762 {
15763 if (child_die->tag == DW_TAG_member
15764 || child_die->tag == DW_TAG_variable
15765 || child_die->tag == DW_TAG_inheritance
15766 || child_die->tag == DW_TAG_template_value_param
15767 || child_die->tag == DW_TAG_template_type_param)
15768 {
15769 /* Do nothing. */
15770 }
15771 else
15772 process_die (child_die, cu);
15773
15774 child_die = child_die->sibling;
15775 }
15776
15777 /* Do not consider external references. According to the DWARF standard,
15778 these DIEs are identified by the fact that they have no byte_size
15779 attribute, and a declaration attribute. */
15780 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15781 || !die_is_declaration (die, cu)
15782 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15783 {
15784 struct symbol *sym = new_symbol (die, type, cu);
15785
15786 if (has_template_parameters)
15787 {
15788 struct symtab *symtab;
15789 if (sym != nullptr)
15790 symtab = symbol_symtab (sym);
15791 else if (cu->line_header != nullptr)
15792 {
15793 /* Any related symtab will do. */
15794 symtab
15795 = cu->line_header->file_names ()[0].symtab;
15796 }
15797 else
15798 {
15799 symtab = nullptr;
15800 complaint (_("could not find suitable "
15801 "symtab for template parameter"
15802 " - DIE at %s [in module %s]"),
15803 sect_offset_str (die->sect_off),
15804 objfile_name (objfile));
15805 }
15806
15807 if (symtab != nullptr)
15808 {
15809 /* Make sure that the symtab is set on the new symbols.
15810 Even though they don't appear in this symtab directly,
15811 other parts of gdb assume that symbols do, and this is
15812 reasonably true. */
15813 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15814 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15815 }
15816 }
15817 }
15818 }
15819
15820 /* Assuming DIE is an enumeration type, and TYPE is its associated
15821 type, update TYPE using some information only available in DIE's
15822 children. In particular, the fields are computed. */
15823
15824 static void
15825 update_enumeration_type_from_children (struct die_info *die,
15826 struct type *type,
15827 struct dwarf2_cu *cu)
15828 {
15829 struct die_info *child_die;
15830 int unsigned_enum = 1;
15831 int flag_enum = 1;
15832
15833 auto_obstack obstack;
15834 std::vector<struct field> fields;
15835
15836 for (child_die = die->child;
15837 child_die != NULL && child_die->tag;
15838 child_die = child_die->sibling)
15839 {
15840 struct attribute *attr;
15841 LONGEST value;
15842 const gdb_byte *bytes;
15843 struct dwarf2_locexpr_baton *baton;
15844 const char *name;
15845
15846 if (child_die->tag != DW_TAG_enumerator)
15847 continue;
15848
15849 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15850 if (attr == NULL)
15851 continue;
15852
15853 name = dwarf2_name (child_die, cu);
15854 if (name == NULL)
15855 name = "<anonymous enumerator>";
15856
15857 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15858 &value, &bytes, &baton);
15859 if (value < 0)
15860 {
15861 unsigned_enum = 0;
15862 flag_enum = 0;
15863 }
15864 else
15865 {
15866 if (count_one_bits_ll (value) >= 2)
15867 flag_enum = 0;
15868 }
15869
15870 fields.emplace_back ();
15871 struct field &field = fields.back ();
15872 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15873 SET_FIELD_ENUMVAL (field, value);
15874 }
15875
15876 if (!fields.empty ())
15877 {
15878 TYPE_NFIELDS (type) = fields.size ();
15879 TYPE_FIELDS (type) = (struct field *)
15880 TYPE_ALLOC (type, sizeof (struct field) * fields.size ());
15881 memcpy (TYPE_FIELDS (type), fields.data (),
15882 sizeof (struct field) * fields.size ());
15883 }
15884
15885 if (unsigned_enum)
15886 TYPE_UNSIGNED (type) = 1;
15887 if (flag_enum)
15888 TYPE_FLAG_ENUM (type) = 1;
15889 }
15890
15891 /* Given a DW_AT_enumeration_type die, set its type. We do not
15892 complete the type's fields yet, or create any symbols. */
15893
15894 static struct type *
15895 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15896 {
15897 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15898 struct type *type;
15899 struct attribute *attr;
15900 const char *name;
15901
15902 /* If the definition of this type lives in .debug_types, read that type.
15903 Don't follow DW_AT_specification though, that will take us back up
15904 the chain and we want to go down. */
15905 attr = die->attr (DW_AT_signature);
15906 if (attr != nullptr)
15907 {
15908 type = get_DW_AT_signature_type (die, attr, cu);
15909
15910 /* The type's CU may not be the same as CU.
15911 Ensure TYPE is recorded with CU in die_type_hash. */
15912 return set_die_type (die, type, cu);
15913 }
15914
15915 type = alloc_type (objfile);
15916
15917 TYPE_CODE (type) = TYPE_CODE_ENUM;
15918 name = dwarf2_full_name (NULL, die, cu);
15919 if (name != NULL)
15920 TYPE_NAME (type) = name;
15921
15922 attr = dwarf2_attr (die, DW_AT_type, cu);
15923 if (attr != NULL)
15924 {
15925 struct type *underlying_type = die_type (die, cu);
15926
15927 TYPE_TARGET_TYPE (type) = underlying_type;
15928 }
15929
15930 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15931 if (attr != nullptr)
15932 {
15933 TYPE_LENGTH (type) = DW_UNSND (attr);
15934 }
15935 else
15936 {
15937 TYPE_LENGTH (type) = 0;
15938 }
15939
15940 maybe_set_alignment (cu, die, type);
15941
15942 /* The enumeration DIE can be incomplete. In Ada, any type can be
15943 declared as private in the package spec, and then defined only
15944 inside the package body. Such types are known as Taft Amendment
15945 Types. When another package uses such a type, an incomplete DIE
15946 may be generated by the compiler. */
15947 if (die_is_declaration (die, cu))
15948 TYPE_STUB (type) = 1;
15949
15950 /* If this type has an underlying type that is not a stub, then we
15951 may use its attributes. We always use the "unsigned" attribute
15952 in this situation, because ordinarily we guess whether the type
15953 is unsigned -- but the guess can be wrong and the underlying type
15954 can tell us the reality. However, we defer to a local size
15955 attribute if one exists, because this lets the compiler override
15956 the underlying type if needed. */
15957 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15958 {
15959 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15960 underlying_type = check_typedef (underlying_type);
15961 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
15962 if (TYPE_LENGTH (type) == 0)
15963 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15964 if (TYPE_RAW_ALIGN (type) == 0
15965 && TYPE_RAW_ALIGN (underlying_type) != 0)
15966 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15967 }
15968
15969 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15970
15971 set_die_type (die, type, cu);
15972
15973 /* Finish the creation of this type by using the enum's children.
15974 Note that, as usual, this must come after set_die_type to avoid
15975 infinite recursion when trying to compute the names of the
15976 enumerators. */
15977 update_enumeration_type_from_children (die, type, cu);
15978
15979 return type;
15980 }
15981
15982 /* Given a pointer to a die which begins an enumeration, process all
15983 the dies that define the members of the enumeration, and create the
15984 symbol for the enumeration type.
15985
15986 NOTE: We reverse the order of the element list. */
15987
15988 static void
15989 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15990 {
15991 struct type *this_type;
15992
15993 this_type = get_die_type (die, cu);
15994 if (this_type == NULL)
15995 this_type = read_enumeration_type (die, cu);
15996
15997 if (die->child != NULL)
15998 {
15999 struct die_info *child_die;
16000 const char *name;
16001
16002 child_die = die->child;
16003 while (child_die && child_die->tag)
16004 {
16005 if (child_die->tag != DW_TAG_enumerator)
16006 {
16007 process_die (child_die, cu);
16008 }
16009 else
16010 {
16011 name = dwarf2_name (child_die, cu);
16012 if (name)
16013 new_symbol (child_die, this_type, cu);
16014 }
16015
16016 child_die = child_die->sibling;
16017 }
16018 }
16019
16020 /* If we are reading an enum from a .debug_types unit, and the enum
16021 is a declaration, and the enum is not the signatured type in the
16022 unit, then we do not want to add a symbol for it. Adding a
16023 symbol would in some cases obscure the true definition of the
16024 enum, giving users an incomplete type when the definition is
16025 actually available. Note that we do not want to do this for all
16026 enums which are just declarations, because C++0x allows forward
16027 enum declarations. */
16028 if (cu->per_cu->is_debug_types
16029 && die_is_declaration (die, cu))
16030 {
16031 struct signatured_type *sig_type;
16032
16033 sig_type = (struct signatured_type *) cu->per_cu;
16034 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16035 if (sig_type->type_offset_in_section != die->sect_off)
16036 return;
16037 }
16038
16039 new_symbol (die, this_type, cu);
16040 }
16041
16042 /* Extract all information from a DW_TAG_array_type DIE and put it in
16043 the DIE's type field. For now, this only handles one dimensional
16044 arrays. */
16045
16046 static struct type *
16047 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16048 {
16049 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16050 struct die_info *child_die;
16051 struct type *type;
16052 struct type *element_type, *range_type, *index_type;
16053 struct attribute *attr;
16054 const char *name;
16055 struct dynamic_prop *byte_stride_prop = NULL;
16056 unsigned int bit_stride = 0;
16057
16058 element_type = die_type (die, cu);
16059
16060 /* The die_type call above may have already set the type for this DIE. */
16061 type = get_die_type (die, cu);
16062 if (type)
16063 return type;
16064
16065 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16066 if (attr != NULL)
16067 {
16068 int stride_ok;
16069 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16070
16071 byte_stride_prop
16072 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16073 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16074 prop_type);
16075 if (!stride_ok)
16076 {
16077 complaint (_("unable to read array DW_AT_byte_stride "
16078 " - DIE at %s [in module %s]"),
16079 sect_offset_str (die->sect_off),
16080 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16081 /* Ignore this attribute. We will likely not be able to print
16082 arrays of this type correctly, but there is little we can do
16083 to help if we cannot read the attribute's value. */
16084 byte_stride_prop = NULL;
16085 }
16086 }
16087
16088 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16089 if (attr != NULL)
16090 bit_stride = DW_UNSND (attr);
16091
16092 /* Irix 6.2 native cc creates array types without children for
16093 arrays with unspecified length. */
16094 if (die->child == NULL)
16095 {
16096 index_type = objfile_type (objfile)->builtin_int;
16097 range_type = create_static_range_type (NULL, index_type, 0, -1);
16098 type = create_array_type_with_stride (NULL, element_type, range_type,
16099 byte_stride_prop, bit_stride);
16100 return set_die_type (die, type, cu);
16101 }
16102
16103 std::vector<struct type *> range_types;
16104 child_die = die->child;
16105 while (child_die && child_die->tag)
16106 {
16107 if (child_die->tag == DW_TAG_subrange_type)
16108 {
16109 struct type *child_type = read_type_die (child_die, cu);
16110
16111 if (child_type != NULL)
16112 {
16113 /* The range type was succesfully read. Save it for the
16114 array type creation. */
16115 range_types.push_back (child_type);
16116 }
16117 }
16118 child_die = child_die->sibling;
16119 }
16120
16121 /* Dwarf2 dimensions are output from left to right, create the
16122 necessary array types in backwards order. */
16123
16124 type = element_type;
16125
16126 if (read_array_order (die, cu) == DW_ORD_col_major)
16127 {
16128 int i = 0;
16129
16130 while (i < range_types.size ())
16131 type = create_array_type_with_stride (NULL, type, range_types[i++],
16132 byte_stride_prop, bit_stride);
16133 }
16134 else
16135 {
16136 size_t ndim = range_types.size ();
16137 while (ndim-- > 0)
16138 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16139 byte_stride_prop, bit_stride);
16140 }
16141
16142 /* Understand Dwarf2 support for vector types (like they occur on
16143 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16144 array type. This is not part of the Dwarf2/3 standard yet, but a
16145 custom vendor extension. The main difference between a regular
16146 array and the vector variant is that vectors are passed by value
16147 to functions. */
16148 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16149 if (attr != nullptr)
16150 make_vector_type (type);
16151
16152 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16153 implementation may choose to implement triple vectors using this
16154 attribute. */
16155 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16156 if (attr != nullptr)
16157 {
16158 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16159 TYPE_LENGTH (type) = DW_UNSND (attr);
16160 else
16161 complaint (_("DW_AT_byte_size for array type smaller "
16162 "than the total size of elements"));
16163 }
16164
16165 name = dwarf2_name (die, cu);
16166 if (name)
16167 TYPE_NAME (type) = name;
16168
16169 maybe_set_alignment (cu, die, type);
16170
16171 /* Install the type in the die. */
16172 set_die_type (die, type, cu);
16173
16174 /* set_die_type should be already done. */
16175 set_descriptive_type (type, die, cu);
16176
16177 return type;
16178 }
16179
16180 static enum dwarf_array_dim_ordering
16181 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16182 {
16183 struct attribute *attr;
16184
16185 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16186
16187 if (attr != nullptr)
16188 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16189
16190 /* GNU F77 is a special case, as at 08/2004 array type info is the
16191 opposite order to the dwarf2 specification, but data is still
16192 laid out as per normal fortran.
16193
16194 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16195 version checking. */
16196
16197 if (cu->language == language_fortran
16198 && cu->producer && strstr (cu->producer, "GNU F77"))
16199 {
16200 return DW_ORD_row_major;
16201 }
16202
16203 switch (cu->language_defn->la_array_ordering)
16204 {
16205 case array_column_major:
16206 return DW_ORD_col_major;
16207 case array_row_major:
16208 default:
16209 return DW_ORD_row_major;
16210 };
16211 }
16212
16213 /* Extract all information from a DW_TAG_set_type DIE and put it in
16214 the DIE's type field. */
16215
16216 static struct type *
16217 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16218 {
16219 struct type *domain_type, *set_type;
16220 struct attribute *attr;
16221
16222 domain_type = die_type (die, cu);
16223
16224 /* The die_type call above may have already set the type for this DIE. */
16225 set_type = get_die_type (die, cu);
16226 if (set_type)
16227 return set_type;
16228
16229 set_type = create_set_type (NULL, domain_type);
16230
16231 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16232 if (attr != nullptr)
16233 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16234
16235 maybe_set_alignment (cu, die, set_type);
16236
16237 return set_die_type (die, set_type, cu);
16238 }
16239
16240 /* A helper for read_common_block that creates a locexpr baton.
16241 SYM is the symbol which we are marking as computed.
16242 COMMON_DIE is the DIE for the common block.
16243 COMMON_LOC is the location expression attribute for the common
16244 block itself.
16245 MEMBER_LOC is the location expression attribute for the particular
16246 member of the common block that we are processing.
16247 CU is the CU from which the above come. */
16248
16249 static void
16250 mark_common_block_symbol_computed (struct symbol *sym,
16251 struct die_info *common_die,
16252 struct attribute *common_loc,
16253 struct attribute *member_loc,
16254 struct dwarf2_cu *cu)
16255 {
16256 struct dwarf2_per_objfile *dwarf2_per_objfile
16257 = cu->per_cu->dwarf2_per_objfile;
16258 struct objfile *objfile = dwarf2_per_objfile->objfile;
16259 struct dwarf2_locexpr_baton *baton;
16260 gdb_byte *ptr;
16261 unsigned int cu_off;
16262 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16263 LONGEST offset = 0;
16264
16265 gdb_assert (common_loc && member_loc);
16266 gdb_assert (common_loc->form_is_block ());
16267 gdb_assert (member_loc->form_is_block ()
16268 || member_loc->form_is_constant ());
16269
16270 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16271 baton->per_cu = cu->per_cu;
16272 gdb_assert (baton->per_cu);
16273
16274 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16275
16276 if (member_loc->form_is_constant ())
16277 {
16278 offset = member_loc->constant_value (0);
16279 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16280 }
16281 else
16282 baton->size += DW_BLOCK (member_loc)->size;
16283
16284 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16285 baton->data = ptr;
16286
16287 *ptr++ = DW_OP_call4;
16288 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16289 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16290 ptr += 4;
16291
16292 if (member_loc->form_is_constant ())
16293 {
16294 *ptr++ = DW_OP_addr;
16295 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16296 ptr += cu->header.addr_size;
16297 }
16298 else
16299 {
16300 /* We have to copy the data here, because DW_OP_call4 will only
16301 use a DW_AT_location attribute. */
16302 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16303 ptr += DW_BLOCK (member_loc)->size;
16304 }
16305
16306 *ptr++ = DW_OP_plus;
16307 gdb_assert (ptr - baton->data == baton->size);
16308
16309 SYMBOL_LOCATION_BATON (sym) = baton;
16310 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16311 }
16312
16313 /* Create appropriate locally-scoped variables for all the
16314 DW_TAG_common_block entries. Also create a struct common_block
16315 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16316 is used to separate the common blocks name namespace from regular
16317 variable names. */
16318
16319 static void
16320 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16321 {
16322 struct attribute *attr;
16323
16324 attr = dwarf2_attr (die, DW_AT_location, cu);
16325 if (attr != nullptr)
16326 {
16327 /* Support the .debug_loc offsets. */
16328 if (attr->form_is_block ())
16329 {
16330 /* Ok. */
16331 }
16332 else if (attr->form_is_section_offset ())
16333 {
16334 dwarf2_complex_location_expr_complaint ();
16335 attr = NULL;
16336 }
16337 else
16338 {
16339 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16340 "common block member");
16341 attr = NULL;
16342 }
16343 }
16344
16345 if (die->child != NULL)
16346 {
16347 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16348 struct die_info *child_die;
16349 size_t n_entries = 0, size;
16350 struct common_block *common_block;
16351 struct symbol *sym;
16352
16353 for (child_die = die->child;
16354 child_die && child_die->tag;
16355 child_die = child_die->sibling)
16356 ++n_entries;
16357
16358 size = (sizeof (struct common_block)
16359 + (n_entries - 1) * sizeof (struct symbol *));
16360 common_block
16361 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16362 size);
16363 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16364 common_block->n_entries = 0;
16365
16366 for (child_die = die->child;
16367 child_die && child_die->tag;
16368 child_die = child_die->sibling)
16369 {
16370 /* Create the symbol in the DW_TAG_common_block block in the current
16371 symbol scope. */
16372 sym = new_symbol (child_die, NULL, cu);
16373 if (sym != NULL)
16374 {
16375 struct attribute *member_loc;
16376
16377 common_block->contents[common_block->n_entries++] = sym;
16378
16379 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16380 cu);
16381 if (member_loc)
16382 {
16383 /* GDB has handled this for a long time, but it is
16384 not specified by DWARF. It seems to have been
16385 emitted by gfortran at least as recently as:
16386 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16387 complaint (_("Variable in common block has "
16388 "DW_AT_data_member_location "
16389 "- DIE at %s [in module %s]"),
16390 sect_offset_str (child_die->sect_off),
16391 objfile_name (objfile));
16392
16393 if (member_loc->form_is_section_offset ())
16394 dwarf2_complex_location_expr_complaint ();
16395 else if (member_loc->form_is_constant ()
16396 || member_loc->form_is_block ())
16397 {
16398 if (attr != nullptr)
16399 mark_common_block_symbol_computed (sym, die, attr,
16400 member_loc, cu);
16401 }
16402 else
16403 dwarf2_complex_location_expr_complaint ();
16404 }
16405 }
16406 }
16407
16408 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16409 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16410 }
16411 }
16412
16413 /* Create a type for a C++ namespace. */
16414
16415 static struct type *
16416 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16417 {
16418 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16419 const char *previous_prefix, *name;
16420 int is_anonymous;
16421 struct type *type;
16422
16423 /* For extensions, reuse the type of the original namespace. */
16424 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16425 {
16426 struct die_info *ext_die;
16427 struct dwarf2_cu *ext_cu = cu;
16428
16429 ext_die = dwarf2_extension (die, &ext_cu);
16430 type = read_type_die (ext_die, ext_cu);
16431
16432 /* EXT_CU may not be the same as CU.
16433 Ensure TYPE is recorded with CU in die_type_hash. */
16434 return set_die_type (die, type, cu);
16435 }
16436
16437 name = namespace_name (die, &is_anonymous, cu);
16438
16439 /* Now build the name of the current namespace. */
16440
16441 previous_prefix = determine_prefix (die, cu);
16442 if (previous_prefix[0] != '\0')
16443 name = typename_concat (&objfile->objfile_obstack,
16444 previous_prefix, name, 0, cu);
16445
16446 /* Create the type. */
16447 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16448
16449 return set_die_type (die, type, cu);
16450 }
16451
16452 /* Read a namespace scope. */
16453
16454 static void
16455 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16456 {
16457 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16458 int is_anonymous;
16459
16460 /* Add a symbol associated to this if we haven't seen the namespace
16461 before. Also, add a using directive if it's an anonymous
16462 namespace. */
16463
16464 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16465 {
16466 struct type *type;
16467
16468 type = read_type_die (die, cu);
16469 new_symbol (die, type, cu);
16470
16471 namespace_name (die, &is_anonymous, cu);
16472 if (is_anonymous)
16473 {
16474 const char *previous_prefix = determine_prefix (die, cu);
16475
16476 std::vector<const char *> excludes;
16477 add_using_directive (using_directives (cu),
16478 previous_prefix, TYPE_NAME (type), NULL,
16479 NULL, excludes, 0, &objfile->objfile_obstack);
16480 }
16481 }
16482
16483 if (die->child != NULL)
16484 {
16485 struct die_info *child_die = die->child;
16486
16487 while (child_die && child_die->tag)
16488 {
16489 process_die (child_die, cu);
16490 child_die = child_die->sibling;
16491 }
16492 }
16493 }
16494
16495 /* Read a Fortran module as type. This DIE can be only a declaration used for
16496 imported module. Still we need that type as local Fortran "use ... only"
16497 declaration imports depend on the created type in determine_prefix. */
16498
16499 static struct type *
16500 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16501 {
16502 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16503 const char *module_name;
16504 struct type *type;
16505
16506 module_name = dwarf2_name (die, cu);
16507 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16508
16509 return set_die_type (die, type, cu);
16510 }
16511
16512 /* Read a Fortran module. */
16513
16514 static void
16515 read_module (struct die_info *die, struct dwarf2_cu *cu)
16516 {
16517 struct die_info *child_die = die->child;
16518 struct type *type;
16519
16520 type = read_type_die (die, cu);
16521 new_symbol (die, type, cu);
16522
16523 while (child_die && child_die->tag)
16524 {
16525 process_die (child_die, cu);
16526 child_die = child_die->sibling;
16527 }
16528 }
16529
16530 /* Return the name of the namespace represented by DIE. Set
16531 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16532 namespace. */
16533
16534 static const char *
16535 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16536 {
16537 struct die_info *current_die;
16538 const char *name = NULL;
16539
16540 /* Loop through the extensions until we find a name. */
16541
16542 for (current_die = die;
16543 current_die != NULL;
16544 current_die = dwarf2_extension (die, &cu))
16545 {
16546 /* We don't use dwarf2_name here so that we can detect the absence
16547 of a name -> anonymous namespace. */
16548 name = dwarf2_string_attr (die, DW_AT_name, cu);
16549
16550 if (name != NULL)
16551 break;
16552 }
16553
16554 /* Is it an anonymous namespace? */
16555
16556 *is_anonymous = (name == NULL);
16557 if (*is_anonymous)
16558 name = CP_ANONYMOUS_NAMESPACE_STR;
16559
16560 return name;
16561 }
16562
16563 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16564 the user defined type vector. */
16565
16566 static struct type *
16567 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16568 {
16569 struct gdbarch *gdbarch
16570 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
16571 struct comp_unit_head *cu_header = &cu->header;
16572 struct type *type;
16573 struct attribute *attr_byte_size;
16574 struct attribute *attr_address_class;
16575 int byte_size, addr_class;
16576 struct type *target_type;
16577
16578 target_type = die_type (die, cu);
16579
16580 /* The die_type call above may have already set the type for this DIE. */
16581 type = get_die_type (die, cu);
16582 if (type)
16583 return type;
16584
16585 type = lookup_pointer_type (target_type);
16586
16587 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16588 if (attr_byte_size)
16589 byte_size = DW_UNSND (attr_byte_size);
16590 else
16591 byte_size = cu_header->addr_size;
16592
16593 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16594 if (attr_address_class)
16595 addr_class = DW_UNSND (attr_address_class);
16596 else
16597 addr_class = DW_ADDR_none;
16598
16599 ULONGEST alignment = get_alignment (cu, die);
16600
16601 /* If the pointer size, alignment, or address class is different
16602 than the default, create a type variant marked as such and set
16603 the length accordingly. */
16604 if (TYPE_LENGTH (type) != byte_size
16605 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16606 && alignment != TYPE_RAW_ALIGN (type))
16607 || addr_class != DW_ADDR_none)
16608 {
16609 if (gdbarch_address_class_type_flags_p (gdbarch))
16610 {
16611 int type_flags;
16612
16613 type_flags = gdbarch_address_class_type_flags
16614 (gdbarch, byte_size, addr_class);
16615 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16616 == 0);
16617 type = make_type_with_address_space (type, type_flags);
16618 }
16619 else if (TYPE_LENGTH (type) != byte_size)
16620 {
16621 complaint (_("invalid pointer size %d"), byte_size);
16622 }
16623 else if (TYPE_RAW_ALIGN (type) != alignment)
16624 {
16625 complaint (_("Invalid DW_AT_alignment"
16626 " - DIE at %s [in module %s]"),
16627 sect_offset_str (die->sect_off),
16628 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16629 }
16630 else
16631 {
16632 /* Should we also complain about unhandled address classes? */
16633 }
16634 }
16635
16636 TYPE_LENGTH (type) = byte_size;
16637 set_type_align (type, alignment);
16638 return set_die_type (die, type, cu);
16639 }
16640
16641 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16642 the user defined type vector. */
16643
16644 static struct type *
16645 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16646 {
16647 struct type *type;
16648 struct type *to_type;
16649 struct type *domain;
16650
16651 to_type = die_type (die, cu);
16652 domain = die_containing_type (die, cu);
16653
16654 /* The calls above may have already set the type for this DIE. */
16655 type = get_die_type (die, cu);
16656 if (type)
16657 return type;
16658
16659 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16660 type = lookup_methodptr_type (to_type);
16661 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16662 {
16663 struct type *new_type
16664 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16665
16666 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16667 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16668 TYPE_VARARGS (to_type));
16669 type = lookup_methodptr_type (new_type);
16670 }
16671 else
16672 type = lookup_memberptr_type (to_type, domain);
16673
16674 return set_die_type (die, type, cu);
16675 }
16676
16677 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16678 the user defined type vector. */
16679
16680 static struct type *
16681 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16682 enum type_code refcode)
16683 {
16684 struct comp_unit_head *cu_header = &cu->header;
16685 struct type *type, *target_type;
16686 struct attribute *attr;
16687
16688 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16689
16690 target_type = die_type (die, cu);
16691
16692 /* The die_type call above may have already set the type for this DIE. */
16693 type = get_die_type (die, cu);
16694 if (type)
16695 return type;
16696
16697 type = lookup_reference_type (target_type, refcode);
16698 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16699 if (attr != nullptr)
16700 {
16701 TYPE_LENGTH (type) = DW_UNSND (attr);
16702 }
16703 else
16704 {
16705 TYPE_LENGTH (type) = cu_header->addr_size;
16706 }
16707 maybe_set_alignment (cu, die, type);
16708 return set_die_type (die, type, cu);
16709 }
16710
16711 /* Add the given cv-qualifiers to the element type of the array. GCC
16712 outputs DWARF type qualifiers that apply to an array, not the
16713 element type. But GDB relies on the array element type to carry
16714 the cv-qualifiers. This mimics section 6.7.3 of the C99
16715 specification. */
16716
16717 static struct type *
16718 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16719 struct type *base_type, int cnst, int voltl)
16720 {
16721 struct type *el_type, *inner_array;
16722
16723 base_type = copy_type (base_type);
16724 inner_array = base_type;
16725
16726 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16727 {
16728 TYPE_TARGET_TYPE (inner_array) =
16729 copy_type (TYPE_TARGET_TYPE (inner_array));
16730 inner_array = TYPE_TARGET_TYPE (inner_array);
16731 }
16732
16733 el_type = TYPE_TARGET_TYPE (inner_array);
16734 cnst |= TYPE_CONST (el_type);
16735 voltl |= TYPE_VOLATILE (el_type);
16736 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16737
16738 return set_die_type (die, base_type, cu);
16739 }
16740
16741 static struct type *
16742 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16743 {
16744 struct type *base_type, *cv_type;
16745
16746 base_type = die_type (die, cu);
16747
16748 /* The die_type call above may have already set the type for this DIE. */
16749 cv_type = get_die_type (die, cu);
16750 if (cv_type)
16751 return cv_type;
16752
16753 /* In case the const qualifier is applied to an array type, the element type
16754 is so qualified, not the array type (section 6.7.3 of C99). */
16755 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16756 return add_array_cv_type (die, cu, base_type, 1, 0);
16757
16758 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16759 return set_die_type (die, cv_type, cu);
16760 }
16761
16762 static struct type *
16763 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16764 {
16765 struct type *base_type, *cv_type;
16766
16767 base_type = die_type (die, cu);
16768
16769 /* The die_type call above may have already set the type for this DIE. */
16770 cv_type = get_die_type (die, cu);
16771 if (cv_type)
16772 return cv_type;
16773
16774 /* In case the volatile qualifier is applied to an array type, the
16775 element type is so qualified, not the array type (section 6.7.3
16776 of C99). */
16777 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16778 return add_array_cv_type (die, cu, base_type, 0, 1);
16779
16780 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16781 return set_die_type (die, cv_type, cu);
16782 }
16783
16784 /* Handle DW_TAG_restrict_type. */
16785
16786 static struct type *
16787 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16788 {
16789 struct type *base_type, *cv_type;
16790
16791 base_type = die_type (die, cu);
16792
16793 /* The die_type call above may have already set the type for this DIE. */
16794 cv_type = get_die_type (die, cu);
16795 if (cv_type)
16796 return cv_type;
16797
16798 cv_type = make_restrict_type (base_type);
16799 return set_die_type (die, cv_type, cu);
16800 }
16801
16802 /* Handle DW_TAG_atomic_type. */
16803
16804 static struct type *
16805 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16806 {
16807 struct type *base_type, *cv_type;
16808
16809 base_type = die_type (die, cu);
16810
16811 /* The die_type call above may have already set the type for this DIE. */
16812 cv_type = get_die_type (die, cu);
16813 if (cv_type)
16814 return cv_type;
16815
16816 cv_type = make_atomic_type (base_type);
16817 return set_die_type (die, cv_type, cu);
16818 }
16819
16820 /* Extract all information from a DW_TAG_string_type DIE and add to
16821 the user defined type vector. It isn't really a user defined type,
16822 but it behaves like one, with other DIE's using an AT_user_def_type
16823 attribute to reference it. */
16824
16825 static struct type *
16826 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16827 {
16828 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16829 struct gdbarch *gdbarch = objfile->arch ();
16830 struct type *type, *range_type, *index_type, *char_type;
16831 struct attribute *attr;
16832 struct dynamic_prop prop;
16833 bool length_is_constant = true;
16834 LONGEST length;
16835
16836 /* There are a couple of places where bit sizes might be made use of
16837 when parsing a DW_TAG_string_type, however, no producer that we know
16838 of make use of these. Handling bit sizes that are a multiple of the
16839 byte size is easy enough, but what about other bit sizes? Lets deal
16840 with that problem when we have to. Warn about these attributes being
16841 unsupported, then parse the type and ignore them like we always
16842 have. */
16843 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16844 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16845 {
16846 static bool warning_printed = false;
16847 if (!warning_printed)
16848 {
16849 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16850 "currently supported on DW_TAG_string_type."));
16851 warning_printed = true;
16852 }
16853 }
16854
16855 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16856 if (attr != nullptr && !attr->form_is_constant ())
16857 {
16858 /* The string length describes the location at which the length of
16859 the string can be found. The size of the length field can be
16860 specified with one of the attributes below. */
16861 struct type *prop_type;
16862 struct attribute *len
16863 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16864 if (len == nullptr)
16865 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16866 if (len != nullptr && len->form_is_constant ())
16867 {
16868 /* Pass 0 as the default as we know this attribute is constant
16869 and the default value will not be returned. */
16870 LONGEST sz = len->constant_value (0);
16871 prop_type = cu->per_cu->int_type (sz, true);
16872 }
16873 else
16874 {
16875 /* If the size is not specified then we assume it is the size of
16876 an address on this target. */
16877 prop_type = cu->per_cu->addr_sized_int_type (true);
16878 }
16879
16880 /* Convert the attribute into a dynamic property. */
16881 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16882 length = 1;
16883 else
16884 length_is_constant = false;
16885 }
16886 else if (attr != nullptr)
16887 {
16888 /* This DW_AT_string_length just contains the length with no
16889 indirection. There's no need to create a dynamic property in this
16890 case. Pass 0 for the default value as we know it will not be
16891 returned in this case. */
16892 length = attr->constant_value (0);
16893 }
16894 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16895 {
16896 /* We don't currently support non-constant byte sizes for strings. */
16897 length = attr->constant_value (1);
16898 }
16899 else
16900 {
16901 /* Use 1 as a fallback length if we have nothing else. */
16902 length = 1;
16903 }
16904
16905 index_type = objfile_type (objfile)->builtin_int;
16906 if (length_is_constant)
16907 range_type = create_static_range_type (NULL, index_type, 1, length);
16908 else
16909 {
16910 struct dynamic_prop low_bound;
16911
16912 low_bound.kind = PROP_CONST;
16913 low_bound.data.const_val = 1;
16914 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16915 }
16916 char_type = language_string_char_type (cu->language_defn, gdbarch);
16917 type = create_string_type (NULL, char_type, range_type);
16918
16919 return set_die_type (die, type, cu);
16920 }
16921
16922 /* Assuming that DIE corresponds to a function, returns nonzero
16923 if the function is prototyped. */
16924
16925 static int
16926 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16927 {
16928 struct attribute *attr;
16929
16930 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16931 if (attr && (DW_UNSND (attr) != 0))
16932 return 1;
16933
16934 /* The DWARF standard implies that the DW_AT_prototyped attribute
16935 is only meaningful for C, but the concept also extends to other
16936 languages that allow unprototyped functions (Eg: Objective C).
16937 For all other languages, assume that functions are always
16938 prototyped. */
16939 if (cu->language != language_c
16940 && cu->language != language_objc
16941 && cu->language != language_opencl)
16942 return 1;
16943
16944 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16945 prototyped and unprototyped functions; default to prototyped,
16946 since that is more common in modern code (and RealView warns
16947 about unprototyped functions). */
16948 if (producer_is_realview (cu->producer))
16949 return 1;
16950
16951 return 0;
16952 }
16953
16954 /* Handle DIES due to C code like:
16955
16956 struct foo
16957 {
16958 int (*funcp)(int a, long l);
16959 int b;
16960 };
16961
16962 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16963
16964 static struct type *
16965 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16966 {
16967 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16968 struct type *type; /* Type that this function returns. */
16969 struct type *ftype; /* Function that returns above type. */
16970 struct attribute *attr;
16971
16972 type = die_type (die, cu);
16973
16974 /* The die_type call above may have already set the type for this DIE. */
16975 ftype = get_die_type (die, cu);
16976 if (ftype)
16977 return ftype;
16978
16979 ftype = lookup_function_type (type);
16980
16981 if (prototyped_function_p (die, cu))
16982 TYPE_PROTOTYPED (ftype) = 1;
16983
16984 /* Store the calling convention in the type if it's available in
16985 the subroutine die. Otherwise set the calling convention to
16986 the default value DW_CC_normal. */
16987 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16988 if (attr != nullptr
16989 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16990 TYPE_CALLING_CONVENTION (ftype)
16991 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16992 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16993 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16994 else
16995 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16996
16997 /* Record whether the function returns normally to its caller or not
16998 if the DWARF producer set that information. */
16999 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17000 if (attr && (DW_UNSND (attr) != 0))
17001 TYPE_NO_RETURN (ftype) = 1;
17002
17003 /* We need to add the subroutine type to the die immediately so
17004 we don't infinitely recurse when dealing with parameters
17005 declared as the same subroutine type. */
17006 set_die_type (die, ftype, cu);
17007
17008 if (die->child != NULL)
17009 {
17010 struct type *void_type = objfile_type (objfile)->builtin_void;
17011 struct die_info *child_die;
17012 int nparams, iparams;
17013
17014 /* Count the number of parameters.
17015 FIXME: GDB currently ignores vararg functions, but knows about
17016 vararg member functions. */
17017 nparams = 0;
17018 child_die = die->child;
17019 while (child_die && child_die->tag)
17020 {
17021 if (child_die->tag == DW_TAG_formal_parameter)
17022 nparams++;
17023 else if (child_die->tag == DW_TAG_unspecified_parameters)
17024 TYPE_VARARGS (ftype) = 1;
17025 child_die = child_die->sibling;
17026 }
17027
17028 /* Allocate storage for parameters and fill them in. */
17029 TYPE_NFIELDS (ftype) = nparams;
17030 TYPE_FIELDS (ftype) = (struct field *)
17031 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17032
17033 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17034 even if we error out during the parameters reading below. */
17035 for (iparams = 0; iparams < nparams; iparams++)
17036 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17037
17038 iparams = 0;
17039 child_die = die->child;
17040 while (child_die && child_die->tag)
17041 {
17042 if (child_die->tag == DW_TAG_formal_parameter)
17043 {
17044 struct type *arg_type;
17045
17046 /* DWARF version 2 has no clean way to discern C++
17047 static and non-static member functions. G++ helps
17048 GDB by marking the first parameter for non-static
17049 member functions (which is the this pointer) as
17050 artificial. We pass this information to
17051 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17052
17053 DWARF version 3 added DW_AT_object_pointer, which GCC
17054 4.5 does not yet generate. */
17055 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17056 if (attr != nullptr)
17057 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17058 else
17059 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17060 arg_type = die_type (child_die, cu);
17061
17062 /* RealView does not mark THIS as const, which the testsuite
17063 expects. GCC marks THIS as const in method definitions,
17064 but not in the class specifications (GCC PR 43053). */
17065 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17066 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17067 {
17068 int is_this = 0;
17069 struct dwarf2_cu *arg_cu = cu;
17070 const char *name = dwarf2_name (child_die, cu);
17071
17072 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17073 if (attr != nullptr)
17074 {
17075 /* If the compiler emits this, use it. */
17076 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17077 is_this = 1;
17078 }
17079 else if (name && strcmp (name, "this") == 0)
17080 /* Function definitions will have the argument names. */
17081 is_this = 1;
17082 else if (name == NULL && iparams == 0)
17083 /* Declarations may not have the names, so like
17084 elsewhere in GDB, assume an artificial first
17085 argument is "this". */
17086 is_this = 1;
17087
17088 if (is_this)
17089 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17090 arg_type, 0);
17091 }
17092
17093 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17094 iparams++;
17095 }
17096 child_die = child_die->sibling;
17097 }
17098 }
17099
17100 return ftype;
17101 }
17102
17103 static struct type *
17104 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17105 {
17106 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17107 const char *name = NULL;
17108 struct type *this_type, *target_type;
17109
17110 name = dwarf2_full_name (NULL, die, cu);
17111 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17112 TYPE_TARGET_STUB (this_type) = 1;
17113 set_die_type (die, this_type, cu);
17114 target_type = die_type (die, cu);
17115 if (target_type != this_type)
17116 TYPE_TARGET_TYPE (this_type) = target_type;
17117 else
17118 {
17119 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17120 spec and cause infinite loops in GDB. */
17121 complaint (_("Self-referential DW_TAG_typedef "
17122 "- DIE at %s [in module %s]"),
17123 sect_offset_str (die->sect_off), objfile_name (objfile));
17124 TYPE_TARGET_TYPE (this_type) = NULL;
17125 }
17126 if (name == NULL)
17127 {
17128 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17129 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17130 Handle these by just returning the target type, rather than
17131 constructing an anonymous typedef type and trying to handle this
17132 elsewhere. */
17133 set_die_type (die, target_type, cu);
17134 return target_type;
17135 }
17136 return this_type;
17137 }
17138
17139 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17140 (which may be different from NAME) to the architecture back-end to allow
17141 it to guess the correct format if necessary. */
17142
17143 static struct type *
17144 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17145 const char *name_hint, enum bfd_endian byte_order)
17146 {
17147 struct gdbarch *gdbarch = objfile->arch ();
17148 const struct floatformat **format;
17149 struct type *type;
17150
17151 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17152 if (format)
17153 type = init_float_type (objfile, bits, name, format, byte_order);
17154 else
17155 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17156
17157 return type;
17158 }
17159
17160 /* Allocate an integer type of size BITS and name NAME. */
17161
17162 static struct type *
17163 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17164 int bits, int unsigned_p, const char *name)
17165 {
17166 struct type *type;
17167
17168 /* Versions of Intel's C Compiler generate an integer type called "void"
17169 instead of using DW_TAG_unspecified_type. This has been seen on
17170 at least versions 14, 17, and 18. */
17171 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17172 && strcmp (name, "void") == 0)
17173 type = objfile_type (objfile)->builtin_void;
17174 else
17175 type = init_integer_type (objfile, bits, unsigned_p, name);
17176
17177 return type;
17178 }
17179
17180 /* Initialise and return a floating point type of size BITS suitable for
17181 use as a component of a complex number. The NAME_HINT is passed through
17182 when initialising the floating point type and is the name of the complex
17183 type.
17184
17185 As DWARF doesn't currently provide an explicit name for the components
17186 of a complex number, but it can be helpful to have these components
17187 named, we try to select a suitable name based on the size of the
17188 component. */
17189 static struct type *
17190 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17191 struct objfile *objfile,
17192 int bits, const char *name_hint,
17193 enum bfd_endian byte_order)
17194 {
17195 gdbarch *gdbarch = objfile->arch ();
17196 struct type *tt = nullptr;
17197
17198 /* Try to find a suitable floating point builtin type of size BITS.
17199 We're going to use the name of this type as the name for the complex
17200 target type that we are about to create. */
17201 switch (cu->language)
17202 {
17203 case language_fortran:
17204 switch (bits)
17205 {
17206 case 32:
17207 tt = builtin_f_type (gdbarch)->builtin_real;
17208 break;
17209 case 64:
17210 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17211 break;
17212 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17213 case 128:
17214 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17215 break;
17216 }
17217 break;
17218 default:
17219 switch (bits)
17220 {
17221 case 32:
17222 tt = builtin_type (gdbarch)->builtin_float;
17223 break;
17224 case 64:
17225 tt = builtin_type (gdbarch)->builtin_double;
17226 break;
17227 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17228 case 128:
17229 tt = builtin_type (gdbarch)->builtin_long_double;
17230 break;
17231 }
17232 break;
17233 }
17234
17235 /* If the type we found doesn't match the size we were looking for, then
17236 pretend we didn't find a type at all, the complex target type we
17237 create will then be nameless. */
17238 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17239 tt = nullptr;
17240
17241 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17242 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17243 }
17244
17245 /* Find a representation of a given base type and install
17246 it in the TYPE field of the die. */
17247
17248 static struct type *
17249 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17250 {
17251 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17252 struct type *type;
17253 struct attribute *attr;
17254 int encoding = 0, bits = 0;
17255 const char *name;
17256 gdbarch *arch;
17257
17258 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17259 if (attr != nullptr)
17260 encoding = DW_UNSND (attr);
17261 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17262 if (attr != nullptr)
17263 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17264 name = dwarf2_name (die, cu);
17265 if (!name)
17266 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17267
17268 arch = objfile->arch ();
17269 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17270
17271 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17272 if (attr)
17273 {
17274 int endianity = DW_UNSND (attr);
17275
17276 switch (endianity)
17277 {
17278 case DW_END_big:
17279 byte_order = BFD_ENDIAN_BIG;
17280 break;
17281 case DW_END_little:
17282 byte_order = BFD_ENDIAN_LITTLE;
17283 break;
17284 default:
17285 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17286 break;
17287 }
17288 }
17289
17290 switch (encoding)
17291 {
17292 case DW_ATE_address:
17293 /* Turn DW_ATE_address into a void * pointer. */
17294 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17295 type = init_pointer_type (objfile, bits, name, type);
17296 break;
17297 case DW_ATE_boolean:
17298 type = init_boolean_type (objfile, bits, 1, name);
17299 break;
17300 case DW_ATE_complex_float:
17301 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17302 byte_order);
17303 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
17304 {
17305 if (name == nullptr)
17306 {
17307 struct obstack *obstack
17308 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17309 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
17310 nullptr);
17311 }
17312 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17313 }
17314 else
17315 type = init_complex_type (name, type);
17316 break;
17317 case DW_ATE_decimal_float:
17318 type = init_decfloat_type (objfile, bits, name);
17319 break;
17320 case DW_ATE_float:
17321 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17322 break;
17323 case DW_ATE_signed:
17324 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17325 break;
17326 case DW_ATE_unsigned:
17327 if (cu->language == language_fortran
17328 && name
17329 && startswith (name, "character("))
17330 type = init_character_type (objfile, bits, 1, name);
17331 else
17332 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17333 break;
17334 case DW_ATE_signed_char:
17335 if (cu->language == language_ada || cu->language == language_m2
17336 || cu->language == language_pascal
17337 || cu->language == language_fortran)
17338 type = init_character_type (objfile, bits, 0, name);
17339 else
17340 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17341 break;
17342 case DW_ATE_unsigned_char:
17343 if (cu->language == language_ada || cu->language == language_m2
17344 || cu->language == language_pascal
17345 || cu->language == language_fortran
17346 || cu->language == language_rust)
17347 type = init_character_type (objfile, bits, 1, name);
17348 else
17349 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17350 break;
17351 case DW_ATE_UTF:
17352 {
17353 if (bits == 16)
17354 type = builtin_type (arch)->builtin_char16;
17355 else if (bits == 32)
17356 type = builtin_type (arch)->builtin_char32;
17357 else
17358 {
17359 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17360 bits);
17361 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17362 }
17363 return set_die_type (die, type, cu);
17364 }
17365 break;
17366
17367 default:
17368 complaint (_("unsupported DW_AT_encoding: '%s'"),
17369 dwarf_type_encoding_name (encoding));
17370 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17371 break;
17372 }
17373
17374 if (name && strcmp (name, "char") == 0)
17375 TYPE_NOSIGN (type) = 1;
17376
17377 maybe_set_alignment (cu, die, type);
17378
17379 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17380
17381 return set_die_type (die, type, cu);
17382 }
17383
17384 /* Parse dwarf attribute if it's a block, reference or constant and put the
17385 resulting value of the attribute into struct bound_prop.
17386 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17387
17388 static int
17389 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17390 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17391 struct type *default_type)
17392 {
17393 struct dwarf2_property_baton *baton;
17394 struct obstack *obstack
17395 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17396
17397 gdb_assert (default_type != NULL);
17398
17399 if (attr == NULL || prop == NULL)
17400 return 0;
17401
17402 if (attr->form_is_block ())
17403 {
17404 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17405 baton->property_type = default_type;
17406 baton->locexpr.per_cu = cu->per_cu;
17407 baton->locexpr.size = DW_BLOCK (attr)->size;
17408 baton->locexpr.data = DW_BLOCK (attr)->data;
17409 switch (attr->name)
17410 {
17411 case DW_AT_string_length:
17412 baton->locexpr.is_reference = true;
17413 break;
17414 default:
17415 baton->locexpr.is_reference = false;
17416 break;
17417 }
17418 prop->data.baton = baton;
17419 prop->kind = PROP_LOCEXPR;
17420 gdb_assert (prop->data.baton != NULL);
17421 }
17422 else if (attr->form_is_ref ())
17423 {
17424 struct dwarf2_cu *target_cu = cu;
17425 struct die_info *target_die;
17426 struct attribute *target_attr;
17427
17428 target_die = follow_die_ref (die, attr, &target_cu);
17429 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17430 if (target_attr == NULL)
17431 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17432 target_cu);
17433 if (target_attr == NULL)
17434 return 0;
17435
17436 switch (target_attr->name)
17437 {
17438 case DW_AT_location:
17439 if (target_attr->form_is_section_offset ())
17440 {
17441 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17442 baton->property_type = die_type (target_die, target_cu);
17443 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17444 prop->data.baton = baton;
17445 prop->kind = PROP_LOCLIST;
17446 gdb_assert (prop->data.baton != NULL);
17447 }
17448 else if (target_attr->form_is_block ())
17449 {
17450 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17451 baton->property_type = die_type (target_die, target_cu);
17452 baton->locexpr.per_cu = cu->per_cu;
17453 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17454 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17455 baton->locexpr.is_reference = true;
17456 prop->data.baton = baton;
17457 prop->kind = PROP_LOCEXPR;
17458 gdb_assert (prop->data.baton != NULL);
17459 }
17460 else
17461 {
17462 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17463 "dynamic property");
17464 return 0;
17465 }
17466 break;
17467 case DW_AT_data_member_location:
17468 {
17469 LONGEST offset;
17470
17471 if (!handle_data_member_location (target_die, target_cu,
17472 &offset))
17473 return 0;
17474
17475 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17476 baton->property_type = read_type_die (target_die->parent,
17477 target_cu);
17478 baton->offset_info.offset = offset;
17479 baton->offset_info.type = die_type (target_die, target_cu);
17480 prop->data.baton = baton;
17481 prop->kind = PROP_ADDR_OFFSET;
17482 break;
17483 }
17484 }
17485 }
17486 else if (attr->form_is_constant ())
17487 {
17488 prop->data.const_val = attr->constant_value (0);
17489 prop->kind = PROP_CONST;
17490 }
17491 else
17492 {
17493 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17494 dwarf2_name (die, cu));
17495 return 0;
17496 }
17497
17498 return 1;
17499 }
17500
17501 /* See read.h. */
17502
17503 struct type *
17504 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17505 {
17506 struct objfile *objfile = dwarf2_per_objfile->objfile;
17507 struct type *int_type;
17508
17509 /* Helper macro to examine the various builtin types. */
17510 #define TRY_TYPE(F) \
17511 int_type = (unsigned_p \
17512 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17513 : objfile_type (objfile)->builtin_ ## F); \
17514 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17515 return int_type
17516
17517 TRY_TYPE (char);
17518 TRY_TYPE (short);
17519 TRY_TYPE (int);
17520 TRY_TYPE (long);
17521 TRY_TYPE (long_long);
17522
17523 #undef TRY_TYPE
17524
17525 gdb_assert_not_reached ("unable to find suitable integer type");
17526 }
17527
17528 /* See read.h. */
17529
17530 struct type *
17531 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17532 {
17533 int addr_size = this->addr_size ();
17534 return int_type (addr_size, unsigned_p);
17535 }
17536
17537 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17538 present (which is valid) then compute the default type based on the
17539 compilation units address size. */
17540
17541 static struct type *
17542 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17543 {
17544 struct type *index_type = die_type (die, cu);
17545
17546 /* Dwarf-2 specifications explicitly allows to create subrange types
17547 without specifying a base type.
17548 In that case, the base type must be set to the type of
17549 the lower bound, upper bound or count, in that order, if any of these
17550 three attributes references an object that has a type.
17551 If no base type is found, the Dwarf-2 specifications say that
17552 a signed integer type of size equal to the size of an address should
17553 be used.
17554 For the following C code: `extern char gdb_int [];'
17555 GCC produces an empty range DIE.
17556 FIXME: muller/2010-05-28: Possible references to object for low bound,
17557 high bound or count are not yet handled by this code. */
17558 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17559 index_type = cu->per_cu->addr_sized_int_type (false);
17560
17561 return index_type;
17562 }
17563
17564 /* Read the given DW_AT_subrange DIE. */
17565
17566 static struct type *
17567 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17568 {
17569 struct type *base_type, *orig_base_type;
17570 struct type *range_type;
17571 struct attribute *attr;
17572 struct dynamic_prop low, high;
17573 int low_default_is_valid;
17574 int high_bound_is_count = 0;
17575 const char *name;
17576 ULONGEST negative_mask;
17577
17578 orig_base_type = read_subrange_index_type (die, cu);
17579
17580 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17581 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17582 creating the range type, but we use the result of check_typedef
17583 when examining properties of the type. */
17584 base_type = check_typedef (orig_base_type);
17585
17586 /* The die_type call above may have already set the type for this DIE. */
17587 range_type = get_die_type (die, cu);
17588 if (range_type)
17589 return range_type;
17590
17591 low.kind = PROP_CONST;
17592 high.kind = PROP_CONST;
17593 high.data.const_val = 0;
17594
17595 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17596 omitting DW_AT_lower_bound. */
17597 switch (cu->language)
17598 {
17599 case language_c:
17600 case language_cplus:
17601 low.data.const_val = 0;
17602 low_default_is_valid = 1;
17603 break;
17604 case language_fortran:
17605 low.data.const_val = 1;
17606 low_default_is_valid = 1;
17607 break;
17608 case language_d:
17609 case language_objc:
17610 case language_rust:
17611 low.data.const_val = 0;
17612 low_default_is_valid = (cu->header.version >= 4);
17613 break;
17614 case language_ada:
17615 case language_m2:
17616 case language_pascal:
17617 low.data.const_val = 1;
17618 low_default_is_valid = (cu->header.version >= 4);
17619 break;
17620 default:
17621 low.data.const_val = 0;
17622 low_default_is_valid = 0;
17623 break;
17624 }
17625
17626 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17627 if (attr != nullptr)
17628 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17629 else if (!low_default_is_valid)
17630 complaint (_("Missing DW_AT_lower_bound "
17631 "- DIE at %s [in module %s]"),
17632 sect_offset_str (die->sect_off),
17633 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17634
17635 struct attribute *attr_ub, *attr_count;
17636 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17637 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17638 {
17639 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17640 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17641 {
17642 /* If bounds are constant do the final calculation here. */
17643 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17644 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17645 else
17646 high_bound_is_count = 1;
17647 }
17648 else
17649 {
17650 if (attr_ub != NULL)
17651 complaint (_("Unresolved DW_AT_upper_bound "
17652 "- DIE at %s [in module %s]"),
17653 sect_offset_str (die->sect_off),
17654 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17655 if (attr_count != NULL)
17656 complaint (_("Unresolved DW_AT_count "
17657 "- DIE at %s [in module %s]"),
17658 sect_offset_str (die->sect_off),
17659 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17660 }
17661 }
17662
17663 LONGEST bias = 0;
17664 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17665 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17666 bias = bias_attr->constant_value (0);
17667
17668 /* Normally, the DWARF producers are expected to use a signed
17669 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17670 But this is unfortunately not always the case, as witnessed
17671 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17672 is used instead. To work around that ambiguity, we treat
17673 the bounds as signed, and thus sign-extend their values, when
17674 the base type is signed. */
17675 negative_mask =
17676 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17677 if (low.kind == PROP_CONST
17678 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17679 low.data.const_val |= negative_mask;
17680 if (high.kind == PROP_CONST
17681 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17682 high.data.const_val |= negative_mask;
17683
17684 /* Check for bit and byte strides. */
17685 struct dynamic_prop byte_stride_prop;
17686 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17687 if (attr_byte_stride != nullptr)
17688 {
17689 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17690 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17691 prop_type);
17692 }
17693
17694 struct dynamic_prop bit_stride_prop;
17695 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17696 if (attr_bit_stride != nullptr)
17697 {
17698 /* It only makes sense to have either a bit or byte stride. */
17699 if (attr_byte_stride != nullptr)
17700 {
17701 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17702 "- DIE at %s [in module %s]"),
17703 sect_offset_str (die->sect_off),
17704 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17705 attr_bit_stride = nullptr;
17706 }
17707 else
17708 {
17709 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17710 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17711 prop_type);
17712 }
17713 }
17714
17715 if (attr_byte_stride != nullptr
17716 || attr_bit_stride != nullptr)
17717 {
17718 bool byte_stride_p = (attr_byte_stride != nullptr);
17719 struct dynamic_prop *stride
17720 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17721
17722 range_type
17723 = create_range_type_with_stride (NULL, orig_base_type, &low,
17724 &high, bias, stride, byte_stride_p);
17725 }
17726 else
17727 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17728
17729 if (high_bound_is_count)
17730 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17731
17732 /* Ada expects an empty array on no boundary attributes. */
17733 if (attr == NULL && cu->language != language_ada)
17734 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17735
17736 name = dwarf2_name (die, cu);
17737 if (name)
17738 TYPE_NAME (range_type) = name;
17739
17740 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17741 if (attr != nullptr)
17742 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17743
17744 maybe_set_alignment (cu, die, range_type);
17745
17746 set_die_type (die, range_type, cu);
17747
17748 /* set_die_type should be already done. */
17749 set_descriptive_type (range_type, die, cu);
17750
17751 return range_type;
17752 }
17753
17754 static struct type *
17755 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17756 {
17757 struct type *type;
17758
17759 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17760 NULL);
17761 TYPE_NAME (type) = dwarf2_name (die, cu);
17762
17763 /* In Ada, an unspecified type is typically used when the description
17764 of the type is deferred to a different unit. When encountering
17765 such a type, we treat it as a stub, and try to resolve it later on,
17766 when needed. */
17767 if (cu->language == language_ada)
17768 TYPE_STUB (type) = 1;
17769
17770 return set_die_type (die, type, cu);
17771 }
17772
17773 /* Read a single die and all its descendents. Set the die's sibling
17774 field to NULL; set other fields in the die correctly, and set all
17775 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17776 location of the info_ptr after reading all of those dies. PARENT
17777 is the parent of the die in question. */
17778
17779 static struct die_info *
17780 read_die_and_children (const struct die_reader_specs *reader,
17781 const gdb_byte *info_ptr,
17782 const gdb_byte **new_info_ptr,
17783 struct die_info *parent)
17784 {
17785 struct die_info *die;
17786 const gdb_byte *cur_ptr;
17787
17788 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17789 if (die == NULL)
17790 {
17791 *new_info_ptr = cur_ptr;
17792 return NULL;
17793 }
17794 store_in_ref_table (die, reader->cu);
17795
17796 if (die->has_children)
17797 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17798 else
17799 {
17800 die->child = NULL;
17801 *new_info_ptr = cur_ptr;
17802 }
17803
17804 die->sibling = NULL;
17805 die->parent = parent;
17806 return die;
17807 }
17808
17809 /* Read a die, all of its descendents, and all of its siblings; set
17810 all of the fields of all of the dies correctly. Arguments are as
17811 in read_die_and_children. */
17812
17813 static struct die_info *
17814 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17815 const gdb_byte *info_ptr,
17816 const gdb_byte **new_info_ptr,
17817 struct die_info *parent)
17818 {
17819 struct die_info *first_die, *last_sibling;
17820 const gdb_byte *cur_ptr;
17821
17822 cur_ptr = info_ptr;
17823 first_die = last_sibling = NULL;
17824
17825 while (1)
17826 {
17827 struct die_info *die
17828 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17829
17830 if (die == NULL)
17831 {
17832 *new_info_ptr = cur_ptr;
17833 return first_die;
17834 }
17835
17836 if (!first_die)
17837 first_die = die;
17838 else
17839 last_sibling->sibling = die;
17840
17841 last_sibling = die;
17842 }
17843 }
17844
17845 /* Read a die, all of its descendents, and all of its siblings; set
17846 all of the fields of all of the dies correctly. Arguments are as
17847 in read_die_and_children.
17848 This the main entry point for reading a DIE and all its children. */
17849
17850 static struct die_info *
17851 read_die_and_siblings (const struct die_reader_specs *reader,
17852 const gdb_byte *info_ptr,
17853 const gdb_byte **new_info_ptr,
17854 struct die_info *parent)
17855 {
17856 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17857 new_info_ptr, parent);
17858
17859 if (dwarf_die_debug)
17860 {
17861 fprintf_unfiltered (gdb_stdlog,
17862 "Read die from %s@0x%x of %s:\n",
17863 reader->die_section->get_name (),
17864 (unsigned) (info_ptr - reader->die_section->buffer),
17865 bfd_get_filename (reader->abfd));
17866 dump_die (die, dwarf_die_debug);
17867 }
17868
17869 return die;
17870 }
17871
17872 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17873 attributes.
17874 The caller is responsible for filling in the extra attributes
17875 and updating (*DIEP)->num_attrs.
17876 Set DIEP to point to a newly allocated die with its information,
17877 except for its child, sibling, and parent fields. */
17878
17879 static const gdb_byte *
17880 read_full_die_1 (const struct die_reader_specs *reader,
17881 struct die_info **diep, const gdb_byte *info_ptr,
17882 int num_extra_attrs)
17883 {
17884 unsigned int abbrev_number, bytes_read, i;
17885 struct abbrev_info *abbrev;
17886 struct die_info *die;
17887 struct dwarf2_cu *cu = reader->cu;
17888 bfd *abfd = reader->abfd;
17889
17890 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17891 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17892 info_ptr += bytes_read;
17893 if (!abbrev_number)
17894 {
17895 *diep = NULL;
17896 return info_ptr;
17897 }
17898
17899 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17900 if (!abbrev)
17901 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17902 abbrev_number,
17903 bfd_get_filename (abfd));
17904
17905 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17906 die->sect_off = sect_off;
17907 die->tag = abbrev->tag;
17908 die->abbrev = abbrev_number;
17909 die->has_children = abbrev->has_children;
17910
17911 /* Make the result usable.
17912 The caller needs to update num_attrs after adding the extra
17913 attributes. */
17914 die->num_attrs = abbrev->num_attrs;
17915
17916 std::vector<int> indexes_that_need_reprocess;
17917 for (i = 0; i < abbrev->num_attrs; ++i)
17918 {
17919 bool need_reprocess;
17920 info_ptr =
17921 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17922 info_ptr, &need_reprocess);
17923 if (need_reprocess)
17924 indexes_that_need_reprocess.push_back (i);
17925 }
17926
17927 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17928 if (attr != nullptr)
17929 cu->str_offsets_base = DW_UNSND (attr);
17930
17931 attr = die->attr (DW_AT_loclists_base);
17932 if (attr != nullptr)
17933 cu->loclist_base = DW_UNSND (attr);
17934
17935 auto maybe_addr_base = die->addr_base ();
17936 if (maybe_addr_base.has_value ())
17937 cu->addr_base = *maybe_addr_base;
17938 for (int index : indexes_that_need_reprocess)
17939 read_attribute_reprocess (reader, &die->attrs[index]);
17940 *diep = die;
17941 return info_ptr;
17942 }
17943
17944 /* Read a die and all its attributes.
17945 Set DIEP to point to a newly allocated die with its information,
17946 except for its child, sibling, and parent fields. */
17947
17948 static const gdb_byte *
17949 read_full_die (const struct die_reader_specs *reader,
17950 struct die_info **diep, const gdb_byte *info_ptr)
17951 {
17952 const gdb_byte *result;
17953
17954 result = read_full_die_1 (reader, diep, info_ptr, 0);
17955
17956 if (dwarf_die_debug)
17957 {
17958 fprintf_unfiltered (gdb_stdlog,
17959 "Read die from %s@0x%x of %s:\n",
17960 reader->die_section->get_name (),
17961 (unsigned) (info_ptr - reader->die_section->buffer),
17962 bfd_get_filename (reader->abfd));
17963 dump_die (*diep, dwarf_die_debug);
17964 }
17965
17966 return result;
17967 }
17968 \f
17969
17970 /* Returns nonzero if TAG represents a type that we might generate a partial
17971 symbol for. */
17972
17973 static int
17974 is_type_tag_for_partial (int tag)
17975 {
17976 switch (tag)
17977 {
17978 #if 0
17979 /* Some types that would be reasonable to generate partial symbols for,
17980 that we don't at present. */
17981 case DW_TAG_array_type:
17982 case DW_TAG_file_type:
17983 case DW_TAG_ptr_to_member_type:
17984 case DW_TAG_set_type:
17985 case DW_TAG_string_type:
17986 case DW_TAG_subroutine_type:
17987 #endif
17988 case DW_TAG_base_type:
17989 case DW_TAG_class_type:
17990 case DW_TAG_interface_type:
17991 case DW_TAG_enumeration_type:
17992 case DW_TAG_structure_type:
17993 case DW_TAG_subrange_type:
17994 case DW_TAG_typedef:
17995 case DW_TAG_union_type:
17996 return 1;
17997 default:
17998 return 0;
17999 }
18000 }
18001
18002 /* Load all DIEs that are interesting for partial symbols into memory. */
18003
18004 static struct partial_die_info *
18005 load_partial_dies (const struct die_reader_specs *reader,
18006 const gdb_byte *info_ptr, int building_psymtab)
18007 {
18008 struct dwarf2_cu *cu = reader->cu;
18009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18010 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18011 unsigned int bytes_read;
18012 unsigned int load_all = 0;
18013 int nesting_level = 1;
18014
18015 parent_die = NULL;
18016 last_die = NULL;
18017
18018 gdb_assert (cu->per_cu != NULL);
18019 if (cu->per_cu->load_all_dies)
18020 load_all = 1;
18021
18022 cu->partial_dies
18023 = htab_create_alloc_ex (cu->header.length / 12,
18024 partial_die_hash,
18025 partial_die_eq,
18026 NULL,
18027 &cu->comp_unit_obstack,
18028 hashtab_obstack_allocate,
18029 dummy_obstack_deallocate);
18030
18031 while (1)
18032 {
18033 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18034
18035 /* A NULL abbrev means the end of a series of children. */
18036 if (abbrev == NULL)
18037 {
18038 if (--nesting_level == 0)
18039 return first_die;
18040
18041 info_ptr += bytes_read;
18042 last_die = parent_die;
18043 parent_die = parent_die->die_parent;
18044 continue;
18045 }
18046
18047 /* Check for template arguments. We never save these; if
18048 they're seen, we just mark the parent, and go on our way. */
18049 if (parent_die != NULL
18050 && cu->language == language_cplus
18051 && (abbrev->tag == DW_TAG_template_type_param
18052 || abbrev->tag == DW_TAG_template_value_param))
18053 {
18054 parent_die->has_template_arguments = 1;
18055
18056 if (!load_all)
18057 {
18058 /* We don't need a partial DIE for the template argument. */
18059 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18060 continue;
18061 }
18062 }
18063
18064 /* We only recurse into c++ subprograms looking for template arguments.
18065 Skip their other children. */
18066 if (!load_all
18067 && cu->language == language_cplus
18068 && parent_die != NULL
18069 && parent_die->tag == DW_TAG_subprogram)
18070 {
18071 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18072 continue;
18073 }
18074
18075 /* Check whether this DIE is interesting enough to save. Normally
18076 we would not be interested in members here, but there may be
18077 later variables referencing them via DW_AT_specification (for
18078 static members). */
18079 if (!load_all
18080 && !is_type_tag_for_partial (abbrev->tag)
18081 && abbrev->tag != DW_TAG_constant
18082 && abbrev->tag != DW_TAG_enumerator
18083 && abbrev->tag != DW_TAG_subprogram
18084 && abbrev->tag != DW_TAG_inlined_subroutine
18085 && abbrev->tag != DW_TAG_lexical_block
18086 && abbrev->tag != DW_TAG_variable
18087 && abbrev->tag != DW_TAG_namespace
18088 && abbrev->tag != DW_TAG_module
18089 && abbrev->tag != DW_TAG_member
18090 && abbrev->tag != DW_TAG_imported_unit
18091 && abbrev->tag != DW_TAG_imported_declaration)
18092 {
18093 /* Otherwise we skip to the next sibling, if any. */
18094 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18095 continue;
18096 }
18097
18098 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18099 abbrev);
18100
18101 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18102
18103 /* This two-pass algorithm for processing partial symbols has a
18104 high cost in cache pressure. Thus, handle some simple cases
18105 here which cover the majority of C partial symbols. DIEs
18106 which neither have specification tags in them, nor could have
18107 specification tags elsewhere pointing at them, can simply be
18108 processed and discarded.
18109
18110 This segment is also optional; scan_partial_symbols and
18111 add_partial_symbol will handle these DIEs if we chain
18112 them in normally. When compilers which do not emit large
18113 quantities of duplicate debug information are more common,
18114 this code can probably be removed. */
18115
18116 /* Any complete simple types at the top level (pretty much all
18117 of them, for a language without namespaces), can be processed
18118 directly. */
18119 if (parent_die == NULL
18120 && pdi.has_specification == 0
18121 && pdi.is_declaration == 0
18122 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18123 || pdi.tag == DW_TAG_base_type
18124 || pdi.tag == DW_TAG_subrange_type))
18125 {
18126 if (building_psymtab && pdi.name != NULL)
18127 add_psymbol_to_list (pdi.name, false,
18128 VAR_DOMAIN, LOC_TYPEDEF, -1,
18129 psymbol_placement::STATIC,
18130 0, cu->language, objfile);
18131 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18132 continue;
18133 }
18134
18135 /* The exception for DW_TAG_typedef with has_children above is
18136 a workaround of GCC PR debug/47510. In the case of this complaint
18137 type_name_or_error will error on such types later.
18138
18139 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18140 it could not find the child DIEs referenced later, this is checked
18141 above. In correct DWARF DW_TAG_typedef should have no children. */
18142
18143 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18144 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18145 "- DIE at %s [in module %s]"),
18146 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18147
18148 /* If we're at the second level, and we're an enumerator, and
18149 our parent has no specification (meaning possibly lives in a
18150 namespace elsewhere), then we can add the partial symbol now
18151 instead of queueing it. */
18152 if (pdi.tag == DW_TAG_enumerator
18153 && parent_die != NULL
18154 && parent_die->die_parent == NULL
18155 && parent_die->tag == DW_TAG_enumeration_type
18156 && parent_die->has_specification == 0)
18157 {
18158 if (pdi.name == NULL)
18159 complaint (_("malformed enumerator DIE ignored"));
18160 else if (building_psymtab)
18161 add_psymbol_to_list (pdi.name, false,
18162 VAR_DOMAIN, LOC_CONST, -1,
18163 cu->language == language_cplus
18164 ? psymbol_placement::GLOBAL
18165 : psymbol_placement::STATIC,
18166 0, cu->language, objfile);
18167
18168 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18169 continue;
18170 }
18171
18172 struct partial_die_info *part_die
18173 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18174
18175 /* We'll save this DIE so link it in. */
18176 part_die->die_parent = parent_die;
18177 part_die->die_sibling = NULL;
18178 part_die->die_child = NULL;
18179
18180 if (last_die && last_die == parent_die)
18181 last_die->die_child = part_die;
18182 else if (last_die)
18183 last_die->die_sibling = part_die;
18184
18185 last_die = part_die;
18186
18187 if (first_die == NULL)
18188 first_die = part_die;
18189
18190 /* Maybe add the DIE to the hash table. Not all DIEs that we
18191 find interesting need to be in the hash table, because we
18192 also have the parent/sibling/child chains; only those that we
18193 might refer to by offset later during partial symbol reading.
18194
18195 For now this means things that might have be the target of a
18196 DW_AT_specification, DW_AT_abstract_origin, or
18197 DW_AT_extension. DW_AT_extension will refer only to
18198 namespaces; DW_AT_abstract_origin refers to functions (and
18199 many things under the function DIE, but we do not recurse
18200 into function DIEs during partial symbol reading) and
18201 possibly variables as well; DW_AT_specification refers to
18202 declarations. Declarations ought to have the DW_AT_declaration
18203 flag. It happens that GCC forgets to put it in sometimes, but
18204 only for functions, not for types.
18205
18206 Adding more things than necessary to the hash table is harmless
18207 except for the performance cost. Adding too few will result in
18208 wasted time in find_partial_die, when we reread the compilation
18209 unit with load_all_dies set. */
18210
18211 if (load_all
18212 || abbrev->tag == DW_TAG_constant
18213 || abbrev->tag == DW_TAG_subprogram
18214 || abbrev->tag == DW_TAG_variable
18215 || abbrev->tag == DW_TAG_namespace
18216 || part_die->is_declaration)
18217 {
18218 void **slot;
18219
18220 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18221 to_underlying (part_die->sect_off),
18222 INSERT);
18223 *slot = part_die;
18224 }
18225
18226 /* For some DIEs we want to follow their children (if any). For C
18227 we have no reason to follow the children of structures; for other
18228 languages we have to, so that we can get at method physnames
18229 to infer fully qualified class names, for DW_AT_specification,
18230 and for C++ template arguments. For C++, we also look one level
18231 inside functions to find template arguments (if the name of the
18232 function does not already contain the template arguments).
18233
18234 For Ada and Fortran, we need to scan the children of subprograms
18235 and lexical blocks as well because these languages allow the
18236 definition of nested entities that could be interesting for the
18237 debugger, such as nested subprograms for instance. */
18238 if (last_die->has_children
18239 && (load_all
18240 || last_die->tag == DW_TAG_namespace
18241 || last_die->tag == DW_TAG_module
18242 || last_die->tag == DW_TAG_enumeration_type
18243 || (cu->language == language_cplus
18244 && last_die->tag == DW_TAG_subprogram
18245 && (last_die->name == NULL
18246 || strchr (last_die->name, '<') == NULL))
18247 || (cu->language != language_c
18248 && (last_die->tag == DW_TAG_class_type
18249 || last_die->tag == DW_TAG_interface_type
18250 || last_die->tag == DW_TAG_structure_type
18251 || last_die->tag == DW_TAG_union_type))
18252 || ((cu->language == language_ada
18253 || cu->language == language_fortran)
18254 && (last_die->tag == DW_TAG_subprogram
18255 || last_die->tag == DW_TAG_lexical_block))))
18256 {
18257 nesting_level++;
18258 parent_die = last_die;
18259 continue;
18260 }
18261
18262 /* Otherwise we skip to the next sibling, if any. */
18263 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18264
18265 /* Back to the top, do it again. */
18266 }
18267 }
18268
18269 partial_die_info::partial_die_info (sect_offset sect_off_,
18270 struct abbrev_info *abbrev)
18271 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18272 {
18273 }
18274
18275 /* Read a minimal amount of information into the minimal die structure.
18276 INFO_PTR should point just after the initial uleb128 of a DIE. */
18277
18278 const gdb_byte *
18279 partial_die_info::read (const struct die_reader_specs *reader,
18280 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18281 {
18282 struct dwarf2_cu *cu = reader->cu;
18283 struct dwarf2_per_objfile *dwarf2_per_objfile
18284 = cu->per_cu->dwarf2_per_objfile;
18285 unsigned int i;
18286 int has_low_pc_attr = 0;
18287 int has_high_pc_attr = 0;
18288 int high_pc_relative = 0;
18289
18290 for (i = 0; i < abbrev.num_attrs; ++i)
18291 {
18292 attribute attr;
18293 bool need_reprocess;
18294 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18295 info_ptr, &need_reprocess);
18296 /* String and address offsets that need to do the reprocessing have
18297 already been read at this point, so there is no need to wait until
18298 the loop terminates to do the reprocessing. */
18299 if (need_reprocess)
18300 read_attribute_reprocess (reader, &attr);
18301 /* Store the data if it is of an attribute we want to keep in a
18302 partial symbol table. */
18303 switch (attr.name)
18304 {
18305 case DW_AT_name:
18306 switch (tag)
18307 {
18308 case DW_TAG_compile_unit:
18309 case DW_TAG_partial_unit:
18310 case DW_TAG_type_unit:
18311 /* Compilation units have a DW_AT_name that is a filename, not
18312 a source language identifier. */
18313 case DW_TAG_enumeration_type:
18314 case DW_TAG_enumerator:
18315 /* These tags always have simple identifiers already; no need
18316 to canonicalize them. */
18317 name = DW_STRING (&attr);
18318 break;
18319 default:
18320 {
18321 struct objfile *objfile = dwarf2_per_objfile->objfile;
18322
18323 name
18324 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18325 }
18326 break;
18327 }
18328 break;
18329 case DW_AT_linkage_name:
18330 case DW_AT_MIPS_linkage_name:
18331 /* Note that both forms of linkage name might appear. We
18332 assume they will be the same, and we only store the last
18333 one we see. */
18334 linkage_name = attr.value_as_string ();
18335 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18336 See https://github.com/rust-lang/rust/issues/32925. */
18337 if (cu->language == language_rust && linkage_name != NULL
18338 && strchr (linkage_name, '{') != NULL)
18339 linkage_name = NULL;
18340 break;
18341 case DW_AT_low_pc:
18342 has_low_pc_attr = 1;
18343 lowpc = attr.value_as_address ();
18344 break;
18345 case DW_AT_high_pc:
18346 has_high_pc_attr = 1;
18347 highpc = attr.value_as_address ();
18348 if (cu->header.version >= 4 && attr.form_is_constant ())
18349 high_pc_relative = 1;
18350 break;
18351 case DW_AT_location:
18352 /* Support the .debug_loc offsets. */
18353 if (attr.form_is_block ())
18354 {
18355 d.locdesc = DW_BLOCK (&attr);
18356 }
18357 else if (attr.form_is_section_offset ())
18358 {
18359 dwarf2_complex_location_expr_complaint ();
18360 }
18361 else
18362 {
18363 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18364 "partial symbol information");
18365 }
18366 break;
18367 case DW_AT_external:
18368 is_external = DW_UNSND (&attr);
18369 break;
18370 case DW_AT_declaration:
18371 is_declaration = DW_UNSND (&attr);
18372 break;
18373 case DW_AT_type:
18374 has_type = 1;
18375 break;
18376 case DW_AT_abstract_origin:
18377 case DW_AT_specification:
18378 case DW_AT_extension:
18379 has_specification = 1;
18380 spec_offset = attr.get_ref_die_offset ();
18381 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18382 || cu->per_cu->is_dwz);
18383 break;
18384 case DW_AT_sibling:
18385 /* Ignore absolute siblings, they might point outside of
18386 the current compile unit. */
18387 if (attr.form == DW_FORM_ref_addr)
18388 complaint (_("ignoring absolute DW_AT_sibling"));
18389 else
18390 {
18391 const gdb_byte *buffer = reader->buffer;
18392 sect_offset off = attr.get_ref_die_offset ();
18393 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18394
18395 if (sibling_ptr < info_ptr)
18396 complaint (_("DW_AT_sibling points backwards"));
18397 else if (sibling_ptr > reader->buffer_end)
18398 reader->die_section->overflow_complaint ();
18399 else
18400 sibling = sibling_ptr;
18401 }
18402 break;
18403 case DW_AT_byte_size:
18404 has_byte_size = 1;
18405 break;
18406 case DW_AT_const_value:
18407 has_const_value = 1;
18408 break;
18409 case DW_AT_calling_convention:
18410 /* DWARF doesn't provide a way to identify a program's source-level
18411 entry point. DW_AT_calling_convention attributes are only meant
18412 to describe functions' calling conventions.
18413
18414 However, because it's a necessary piece of information in
18415 Fortran, and before DWARF 4 DW_CC_program was the only
18416 piece of debugging information whose definition refers to
18417 a 'main program' at all, several compilers marked Fortran
18418 main programs with DW_CC_program --- even when those
18419 functions use the standard calling conventions.
18420
18421 Although DWARF now specifies a way to provide this
18422 information, we support this practice for backward
18423 compatibility. */
18424 if (DW_UNSND (&attr) == DW_CC_program
18425 && cu->language == language_fortran)
18426 main_subprogram = 1;
18427 break;
18428 case DW_AT_inline:
18429 if (DW_UNSND (&attr) == DW_INL_inlined
18430 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18431 may_be_inlined = 1;
18432 break;
18433
18434 case DW_AT_import:
18435 if (tag == DW_TAG_imported_unit)
18436 {
18437 d.sect_off = attr.get_ref_die_offset ();
18438 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18439 || cu->per_cu->is_dwz);
18440 }
18441 break;
18442
18443 case DW_AT_main_subprogram:
18444 main_subprogram = DW_UNSND (&attr);
18445 break;
18446
18447 case DW_AT_ranges:
18448 {
18449 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18450 but that requires a full DIE, so instead we just
18451 reimplement it. */
18452 int need_ranges_base = tag != DW_TAG_compile_unit;
18453 unsigned int ranges_offset = (DW_UNSND (&attr)
18454 + (need_ranges_base
18455 ? cu->ranges_base
18456 : 0));
18457
18458 /* Value of the DW_AT_ranges attribute is the offset in the
18459 .debug_ranges section. */
18460 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18461 nullptr))
18462 has_pc_info = 1;
18463 }
18464 break;
18465
18466 default:
18467 break;
18468 }
18469 }
18470
18471 /* For Ada, if both the name and the linkage name appear, we prefer
18472 the latter. This lets "catch exception" work better, regardless
18473 of the order in which the name and linkage name were emitted.
18474 Really, though, this is just a workaround for the fact that gdb
18475 doesn't store both the name and the linkage name. */
18476 if (cu->language == language_ada && linkage_name != nullptr)
18477 name = linkage_name;
18478
18479 if (high_pc_relative)
18480 highpc += lowpc;
18481
18482 if (has_low_pc_attr && has_high_pc_attr)
18483 {
18484 /* When using the GNU linker, .gnu.linkonce. sections are used to
18485 eliminate duplicate copies of functions and vtables and such.
18486 The linker will arbitrarily choose one and discard the others.
18487 The AT_*_pc values for such functions refer to local labels in
18488 these sections. If the section from that file was discarded, the
18489 labels are not in the output, so the relocs get a value of 0.
18490 If this is a discarded function, mark the pc bounds as invalid,
18491 so that GDB will ignore it. */
18492 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18493 {
18494 struct objfile *objfile = dwarf2_per_objfile->objfile;
18495 struct gdbarch *gdbarch = objfile->arch ();
18496
18497 complaint (_("DW_AT_low_pc %s is zero "
18498 "for DIE at %s [in module %s]"),
18499 paddress (gdbarch, lowpc),
18500 sect_offset_str (sect_off),
18501 objfile_name (objfile));
18502 }
18503 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18504 else if (lowpc >= highpc)
18505 {
18506 struct objfile *objfile = dwarf2_per_objfile->objfile;
18507 struct gdbarch *gdbarch = objfile->arch ();
18508
18509 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18510 "for DIE at %s [in module %s]"),
18511 paddress (gdbarch, lowpc),
18512 paddress (gdbarch, highpc),
18513 sect_offset_str (sect_off),
18514 objfile_name (objfile));
18515 }
18516 else
18517 has_pc_info = 1;
18518 }
18519
18520 return info_ptr;
18521 }
18522
18523 /* Find a cached partial DIE at OFFSET in CU. */
18524
18525 struct partial_die_info *
18526 dwarf2_cu::find_partial_die (sect_offset sect_off)
18527 {
18528 struct partial_die_info *lookup_die = NULL;
18529 struct partial_die_info part_die (sect_off);
18530
18531 lookup_die = ((struct partial_die_info *)
18532 htab_find_with_hash (partial_dies, &part_die,
18533 to_underlying (sect_off)));
18534
18535 return lookup_die;
18536 }
18537
18538 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18539 except in the case of .debug_types DIEs which do not reference
18540 outside their CU (they do however referencing other types via
18541 DW_FORM_ref_sig8). */
18542
18543 static const struct cu_partial_die_info
18544 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18545 {
18546 struct dwarf2_per_objfile *dwarf2_per_objfile
18547 = cu->per_cu->dwarf2_per_objfile;
18548 struct objfile *objfile = dwarf2_per_objfile->objfile;
18549 struct dwarf2_per_cu_data *per_cu = NULL;
18550 struct partial_die_info *pd = NULL;
18551
18552 if (offset_in_dwz == cu->per_cu->is_dwz
18553 && cu->header.offset_in_cu_p (sect_off))
18554 {
18555 pd = cu->find_partial_die (sect_off);
18556 if (pd != NULL)
18557 return { cu, pd };
18558 /* We missed recording what we needed.
18559 Load all dies and try again. */
18560 per_cu = cu->per_cu;
18561 }
18562 else
18563 {
18564 /* TUs don't reference other CUs/TUs (except via type signatures). */
18565 if (cu->per_cu->is_debug_types)
18566 {
18567 error (_("Dwarf Error: Type Unit at offset %s contains"
18568 " external reference to offset %s [in module %s].\n"),
18569 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18570 bfd_get_filename (objfile->obfd));
18571 }
18572 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18573 dwarf2_per_objfile);
18574
18575 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18576 load_partial_comp_unit (per_cu);
18577
18578 per_cu->cu->last_used = 0;
18579 pd = per_cu->cu->find_partial_die (sect_off);
18580 }
18581
18582 /* If we didn't find it, and not all dies have been loaded,
18583 load them all and try again. */
18584
18585 if (pd == NULL && per_cu->load_all_dies == 0)
18586 {
18587 per_cu->load_all_dies = 1;
18588
18589 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18590 THIS_CU->cu may already be in use. So we can't just free it and
18591 replace its DIEs with the ones we read in. Instead, we leave those
18592 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18593 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18594 set. */
18595 load_partial_comp_unit (per_cu);
18596
18597 pd = per_cu->cu->find_partial_die (sect_off);
18598 }
18599
18600 if (pd == NULL)
18601 internal_error (__FILE__, __LINE__,
18602 _("could not find partial DIE %s "
18603 "in cache [from module %s]\n"),
18604 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18605 return { per_cu->cu, pd };
18606 }
18607
18608 /* See if we can figure out if the class lives in a namespace. We do
18609 this by looking for a member function; its demangled name will
18610 contain namespace info, if there is any. */
18611
18612 static void
18613 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18614 struct dwarf2_cu *cu)
18615 {
18616 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18617 what template types look like, because the demangler
18618 frequently doesn't give the same name as the debug info. We
18619 could fix this by only using the demangled name to get the
18620 prefix (but see comment in read_structure_type). */
18621
18622 struct partial_die_info *real_pdi;
18623 struct partial_die_info *child_pdi;
18624
18625 /* If this DIE (this DIE's specification, if any) has a parent, then
18626 we should not do this. We'll prepend the parent's fully qualified
18627 name when we create the partial symbol. */
18628
18629 real_pdi = struct_pdi;
18630 while (real_pdi->has_specification)
18631 {
18632 auto res = find_partial_die (real_pdi->spec_offset,
18633 real_pdi->spec_is_dwz, cu);
18634 real_pdi = res.pdi;
18635 cu = res.cu;
18636 }
18637
18638 if (real_pdi->die_parent != NULL)
18639 return;
18640
18641 for (child_pdi = struct_pdi->die_child;
18642 child_pdi != NULL;
18643 child_pdi = child_pdi->die_sibling)
18644 {
18645 if (child_pdi->tag == DW_TAG_subprogram
18646 && child_pdi->linkage_name != NULL)
18647 {
18648 gdb::unique_xmalloc_ptr<char> actual_class_name
18649 (language_class_name_from_physname (cu->language_defn,
18650 child_pdi->linkage_name));
18651 if (actual_class_name != NULL)
18652 {
18653 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18654 struct_pdi->name = objfile->intern (actual_class_name.get ());
18655 }
18656 break;
18657 }
18658 }
18659 }
18660
18661 /* Return true if a DIE with TAG may have the DW_AT_const_value
18662 attribute. */
18663
18664 static bool
18665 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18666 {
18667 switch (tag)
18668 {
18669 case DW_TAG_constant:
18670 case DW_TAG_enumerator:
18671 case DW_TAG_formal_parameter:
18672 case DW_TAG_template_value_param:
18673 case DW_TAG_variable:
18674 return true;
18675 }
18676
18677 return false;
18678 }
18679
18680 void
18681 partial_die_info::fixup (struct dwarf2_cu *cu)
18682 {
18683 /* Once we've fixed up a die, there's no point in doing so again.
18684 This also avoids a memory leak if we were to call
18685 guess_partial_die_structure_name multiple times. */
18686 if (fixup_called)
18687 return;
18688
18689 /* If we found a reference attribute and the DIE has no name, try
18690 to find a name in the referred to DIE. */
18691
18692 if (name == NULL && has_specification)
18693 {
18694 struct partial_die_info *spec_die;
18695
18696 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18697 spec_die = res.pdi;
18698 cu = res.cu;
18699
18700 spec_die->fixup (cu);
18701
18702 if (spec_die->name)
18703 {
18704 name = spec_die->name;
18705
18706 /* Copy DW_AT_external attribute if it is set. */
18707 if (spec_die->is_external)
18708 is_external = spec_die->is_external;
18709 }
18710 }
18711
18712 if (!has_const_value && has_specification
18713 && can_have_DW_AT_const_value_p (tag))
18714 {
18715 struct partial_die_info *spec_die;
18716
18717 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18718 spec_die = res.pdi;
18719 cu = res.cu;
18720
18721 spec_die->fixup (cu);
18722
18723 if (spec_die->has_const_value)
18724 {
18725 /* Copy DW_AT_const_value attribute if it is set. */
18726 has_const_value = spec_die->has_const_value;
18727 }
18728 }
18729
18730 /* Set default names for some unnamed DIEs. */
18731
18732 if (name == NULL && tag == DW_TAG_namespace)
18733 name = CP_ANONYMOUS_NAMESPACE_STR;
18734
18735 /* If there is no parent die to provide a namespace, and there are
18736 children, see if we can determine the namespace from their linkage
18737 name. */
18738 if (cu->language == language_cplus
18739 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18740 && die_parent == NULL
18741 && has_children
18742 && (tag == DW_TAG_class_type
18743 || tag == DW_TAG_structure_type
18744 || tag == DW_TAG_union_type))
18745 guess_partial_die_structure_name (this, cu);
18746
18747 /* GCC might emit a nameless struct or union that has a linkage
18748 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18749 if (name == NULL
18750 && (tag == DW_TAG_class_type
18751 || tag == DW_TAG_interface_type
18752 || tag == DW_TAG_structure_type
18753 || tag == DW_TAG_union_type)
18754 && linkage_name != NULL)
18755 {
18756 gdb::unique_xmalloc_ptr<char> demangled
18757 (gdb_demangle (linkage_name, DMGL_TYPES));
18758 if (demangled != nullptr)
18759 {
18760 const char *base;
18761
18762 /* Strip any leading namespaces/classes, keep only the base name.
18763 DW_AT_name for named DIEs does not contain the prefixes. */
18764 base = strrchr (demangled.get (), ':');
18765 if (base && base > demangled.get () && base[-1] == ':')
18766 base++;
18767 else
18768 base = demangled.get ();
18769
18770 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18771 name = objfile->intern (base);
18772 }
18773 }
18774
18775 fixup_called = 1;
18776 }
18777
18778 /* Read the .debug_loclists header contents from the given SECTION in the
18779 HEADER. */
18780 static void
18781 read_loclist_header (struct loclist_header *header,
18782 struct dwarf2_section_info *section)
18783 {
18784 unsigned int bytes_read;
18785 bfd *abfd = section->get_bfd_owner ();
18786 const gdb_byte *info_ptr = section->buffer;
18787 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18788 info_ptr += bytes_read;
18789 header->version = read_2_bytes (abfd, info_ptr);
18790 info_ptr += 2;
18791 header->addr_size = read_1_byte (abfd, info_ptr);
18792 info_ptr += 1;
18793 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18794 info_ptr += 1;
18795 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18796 }
18797
18798 /* Return the DW_AT_loclists_base value for the CU. */
18799 static ULONGEST
18800 lookup_loclist_base (struct dwarf2_cu *cu)
18801 {
18802 /* For the .dwo unit, the loclist_base points to the first offset following
18803 the header. The header consists of the following entities-
18804 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18805 bit format)
18806 2. version (2 bytes)
18807 3. address size (1 byte)
18808 4. segment selector size (1 byte)
18809 5. offset entry count (4 bytes)
18810 These sizes are derived as per the DWARFv5 standard. */
18811 if (cu->dwo_unit != nullptr)
18812 {
18813 if (cu->header.initial_length_size == 4)
18814 return LOCLIST_HEADER_SIZE32;
18815 return LOCLIST_HEADER_SIZE64;
18816 }
18817 return cu->loclist_base;
18818 }
18819
18820 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18821 array of offsets in the .debug_loclists section. */
18822 static CORE_ADDR
18823 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18824 {
18825 struct dwarf2_per_objfile *dwarf2_per_objfile
18826 = cu->per_cu->dwarf2_per_objfile;
18827 struct objfile *objfile = dwarf2_per_objfile->objfile;
18828 bfd *abfd = objfile->obfd;
18829 ULONGEST loclist_base = lookup_loclist_base (cu);
18830 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18831
18832 section->read (objfile);
18833 if (section->buffer == NULL)
18834 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18835 "section [in module %s]"), objfile_name (objfile));
18836 struct loclist_header header;
18837 read_loclist_header (&header, section);
18838 if (loclist_index >= header.offset_entry_count)
18839 complaint (_("DW_FORM_loclistx pointing outside of "
18840 ".debug_loclists offset array [in module %s]"),
18841 objfile_name (objfile));
18842 if (loclist_base + loclist_index * cu->header.offset_size
18843 >= section->size)
18844 complaint (_("DW_FORM_loclistx pointing outside of "
18845 ".debug_loclists section [in module %s]"),
18846 objfile_name (objfile));
18847 const gdb_byte *info_ptr
18848 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18849
18850 if (cu->header.offset_size == 4)
18851 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18852 else
18853 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18854 }
18855
18856 /* Process the attributes that had to be skipped in the first round. These
18857 attributes are the ones that need str_offsets_base or addr_base attributes.
18858 They could not have been processed in the first round, because at the time
18859 the values of str_offsets_base or addr_base may not have been known. */
18860 static void
18861 read_attribute_reprocess (const struct die_reader_specs *reader,
18862 struct attribute *attr)
18863 {
18864 struct dwarf2_cu *cu = reader->cu;
18865 switch (attr->form)
18866 {
18867 case DW_FORM_addrx:
18868 case DW_FORM_GNU_addr_index:
18869 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18870 break;
18871 case DW_FORM_loclistx:
18872 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18873 break;
18874 case DW_FORM_strx:
18875 case DW_FORM_strx1:
18876 case DW_FORM_strx2:
18877 case DW_FORM_strx3:
18878 case DW_FORM_strx4:
18879 case DW_FORM_GNU_str_index:
18880 {
18881 unsigned int str_index = DW_UNSND (attr);
18882 if (reader->dwo_file != NULL)
18883 {
18884 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18885 DW_STRING_IS_CANONICAL (attr) = 0;
18886 }
18887 else
18888 {
18889 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18890 DW_STRING_IS_CANONICAL (attr) = 0;
18891 }
18892 break;
18893 }
18894 default:
18895 gdb_assert_not_reached (_("Unexpected DWARF form."));
18896 }
18897 }
18898
18899 /* Read an attribute value described by an attribute form. */
18900
18901 static const gdb_byte *
18902 read_attribute_value (const struct die_reader_specs *reader,
18903 struct attribute *attr, unsigned form,
18904 LONGEST implicit_const, const gdb_byte *info_ptr,
18905 bool *need_reprocess)
18906 {
18907 struct dwarf2_cu *cu = reader->cu;
18908 struct dwarf2_per_objfile *dwarf2_per_objfile
18909 = cu->per_cu->dwarf2_per_objfile;
18910 struct objfile *objfile = dwarf2_per_objfile->objfile;
18911 bfd *abfd = reader->abfd;
18912 struct comp_unit_head *cu_header = &cu->header;
18913 unsigned int bytes_read;
18914 struct dwarf_block *blk;
18915 *need_reprocess = false;
18916
18917 attr->form = (enum dwarf_form) form;
18918 switch (form)
18919 {
18920 case DW_FORM_ref_addr:
18921 if (cu->header.version == 2)
18922 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18923 &bytes_read);
18924 else
18925 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18926 &bytes_read);
18927 info_ptr += bytes_read;
18928 break;
18929 case DW_FORM_GNU_ref_alt:
18930 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18931 info_ptr += bytes_read;
18932 break;
18933 case DW_FORM_addr:
18934 {
18935 struct gdbarch *gdbarch = objfile->arch ();
18936 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18937 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18938 info_ptr += bytes_read;
18939 }
18940 break;
18941 case DW_FORM_block2:
18942 blk = dwarf_alloc_block (cu);
18943 blk->size = read_2_bytes (abfd, info_ptr);
18944 info_ptr += 2;
18945 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18946 info_ptr += blk->size;
18947 DW_BLOCK (attr) = blk;
18948 break;
18949 case DW_FORM_block4:
18950 blk = dwarf_alloc_block (cu);
18951 blk->size = read_4_bytes (abfd, info_ptr);
18952 info_ptr += 4;
18953 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18954 info_ptr += blk->size;
18955 DW_BLOCK (attr) = blk;
18956 break;
18957 case DW_FORM_data2:
18958 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18959 info_ptr += 2;
18960 break;
18961 case DW_FORM_data4:
18962 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18963 info_ptr += 4;
18964 break;
18965 case DW_FORM_data8:
18966 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18967 info_ptr += 8;
18968 break;
18969 case DW_FORM_data16:
18970 blk = dwarf_alloc_block (cu);
18971 blk->size = 16;
18972 blk->data = read_n_bytes (abfd, info_ptr, 16);
18973 info_ptr += 16;
18974 DW_BLOCK (attr) = blk;
18975 break;
18976 case DW_FORM_sec_offset:
18977 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18978 info_ptr += bytes_read;
18979 break;
18980 case DW_FORM_loclistx:
18981 {
18982 *need_reprocess = true;
18983 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18984 info_ptr += bytes_read;
18985 }
18986 break;
18987 case DW_FORM_string:
18988 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18989 DW_STRING_IS_CANONICAL (attr) = 0;
18990 info_ptr += bytes_read;
18991 break;
18992 case DW_FORM_strp:
18993 if (!cu->per_cu->is_dwz)
18994 {
18995 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18996 abfd, info_ptr, cu_header,
18997 &bytes_read);
18998 DW_STRING_IS_CANONICAL (attr) = 0;
18999 info_ptr += bytes_read;
19000 break;
19001 }
19002 /* FALLTHROUGH */
19003 case DW_FORM_line_strp:
19004 if (!cu->per_cu->is_dwz)
19005 {
19006 DW_STRING (attr)
19007 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19008 &bytes_read);
19009 DW_STRING_IS_CANONICAL (attr) = 0;
19010 info_ptr += bytes_read;
19011 break;
19012 }
19013 /* FALLTHROUGH */
19014 case DW_FORM_GNU_strp_alt:
19015 {
19016 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19017 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19018 &bytes_read);
19019
19020 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19021 DW_STRING_IS_CANONICAL (attr) = 0;
19022 info_ptr += bytes_read;
19023 }
19024 break;
19025 case DW_FORM_exprloc:
19026 case DW_FORM_block:
19027 blk = dwarf_alloc_block (cu);
19028 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19029 info_ptr += bytes_read;
19030 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19031 info_ptr += blk->size;
19032 DW_BLOCK (attr) = blk;
19033 break;
19034 case DW_FORM_block1:
19035 blk = dwarf_alloc_block (cu);
19036 blk->size = read_1_byte (abfd, info_ptr);
19037 info_ptr += 1;
19038 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19039 info_ptr += blk->size;
19040 DW_BLOCK (attr) = blk;
19041 break;
19042 case DW_FORM_data1:
19043 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19044 info_ptr += 1;
19045 break;
19046 case DW_FORM_flag:
19047 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19048 info_ptr += 1;
19049 break;
19050 case DW_FORM_flag_present:
19051 DW_UNSND (attr) = 1;
19052 break;
19053 case DW_FORM_sdata:
19054 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19055 info_ptr += bytes_read;
19056 break;
19057 case DW_FORM_udata:
19058 case DW_FORM_rnglistx:
19059 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19060 info_ptr += bytes_read;
19061 break;
19062 case DW_FORM_ref1:
19063 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19064 + read_1_byte (abfd, info_ptr));
19065 info_ptr += 1;
19066 break;
19067 case DW_FORM_ref2:
19068 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19069 + read_2_bytes (abfd, info_ptr));
19070 info_ptr += 2;
19071 break;
19072 case DW_FORM_ref4:
19073 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19074 + read_4_bytes (abfd, info_ptr));
19075 info_ptr += 4;
19076 break;
19077 case DW_FORM_ref8:
19078 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19079 + read_8_bytes (abfd, info_ptr));
19080 info_ptr += 8;
19081 break;
19082 case DW_FORM_ref_sig8:
19083 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19084 info_ptr += 8;
19085 break;
19086 case DW_FORM_ref_udata:
19087 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19088 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19089 info_ptr += bytes_read;
19090 break;
19091 case DW_FORM_indirect:
19092 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19093 info_ptr += bytes_read;
19094 if (form == DW_FORM_implicit_const)
19095 {
19096 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19097 info_ptr += bytes_read;
19098 }
19099 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19100 info_ptr, need_reprocess);
19101 break;
19102 case DW_FORM_implicit_const:
19103 DW_SND (attr) = implicit_const;
19104 break;
19105 case DW_FORM_addrx:
19106 case DW_FORM_GNU_addr_index:
19107 *need_reprocess = true;
19108 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19109 info_ptr += bytes_read;
19110 break;
19111 case DW_FORM_strx:
19112 case DW_FORM_strx1:
19113 case DW_FORM_strx2:
19114 case DW_FORM_strx3:
19115 case DW_FORM_strx4:
19116 case DW_FORM_GNU_str_index:
19117 {
19118 ULONGEST str_index;
19119 if (form == DW_FORM_strx1)
19120 {
19121 str_index = read_1_byte (abfd, info_ptr);
19122 info_ptr += 1;
19123 }
19124 else if (form == DW_FORM_strx2)
19125 {
19126 str_index = read_2_bytes (abfd, info_ptr);
19127 info_ptr += 2;
19128 }
19129 else if (form == DW_FORM_strx3)
19130 {
19131 str_index = read_3_bytes (abfd, info_ptr);
19132 info_ptr += 3;
19133 }
19134 else if (form == DW_FORM_strx4)
19135 {
19136 str_index = read_4_bytes (abfd, info_ptr);
19137 info_ptr += 4;
19138 }
19139 else
19140 {
19141 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19142 info_ptr += bytes_read;
19143 }
19144 *need_reprocess = true;
19145 DW_UNSND (attr) = str_index;
19146 }
19147 break;
19148 default:
19149 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19150 dwarf_form_name (form),
19151 bfd_get_filename (abfd));
19152 }
19153
19154 /* Super hack. */
19155 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19156 attr->form = DW_FORM_GNU_ref_alt;
19157
19158 /* We have seen instances where the compiler tried to emit a byte
19159 size attribute of -1 which ended up being encoded as an unsigned
19160 0xffffffff. Although 0xffffffff is technically a valid size value,
19161 an object of this size seems pretty unlikely so we can relatively
19162 safely treat these cases as if the size attribute was invalid and
19163 treat them as zero by default. */
19164 if (attr->name == DW_AT_byte_size
19165 && form == DW_FORM_data4
19166 && DW_UNSND (attr) >= 0xffffffff)
19167 {
19168 complaint
19169 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19170 hex_string (DW_UNSND (attr)));
19171 DW_UNSND (attr) = 0;
19172 }
19173
19174 return info_ptr;
19175 }
19176
19177 /* Read an attribute described by an abbreviated attribute. */
19178
19179 static const gdb_byte *
19180 read_attribute (const struct die_reader_specs *reader,
19181 struct attribute *attr, struct attr_abbrev *abbrev,
19182 const gdb_byte *info_ptr, bool *need_reprocess)
19183 {
19184 attr->name = abbrev->name;
19185 return read_attribute_value (reader, attr, abbrev->form,
19186 abbrev->implicit_const, info_ptr,
19187 need_reprocess);
19188 }
19189
19190 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19191
19192 static const char *
19193 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19194 LONGEST str_offset)
19195 {
19196 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
19197 str_offset, "DW_FORM_strp");
19198 }
19199
19200 /* Return pointer to string at .debug_str offset as read from BUF.
19201 BUF is assumed to be in a compilation unit described by CU_HEADER.
19202 Return *BYTES_READ_PTR count of bytes read from BUF. */
19203
19204 static const char *
19205 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19206 const gdb_byte *buf,
19207 const struct comp_unit_head *cu_header,
19208 unsigned int *bytes_read_ptr)
19209 {
19210 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19211
19212 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19213 }
19214
19215 /* See read.h. */
19216
19217 const char *
19218 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19219 const struct comp_unit_head *cu_header,
19220 unsigned int *bytes_read_ptr)
19221 {
19222 bfd *abfd = objfile->obfd;
19223 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19224
19225 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19226 }
19227
19228 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19229 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19230 ADDR_SIZE is the size of addresses from the CU header. */
19231
19232 static CORE_ADDR
19233 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19234 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19235 int addr_size)
19236 {
19237 struct objfile *objfile = dwarf2_per_objfile->objfile;
19238 bfd *abfd = objfile->obfd;
19239 const gdb_byte *info_ptr;
19240 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19241
19242 dwarf2_per_objfile->addr.read (objfile);
19243 if (dwarf2_per_objfile->addr.buffer == NULL)
19244 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19245 objfile_name (objfile));
19246 if (addr_base_or_zero + addr_index * addr_size
19247 >= dwarf2_per_objfile->addr.size)
19248 error (_("DW_FORM_addr_index pointing outside of "
19249 ".debug_addr section [in module %s]"),
19250 objfile_name (objfile));
19251 info_ptr = (dwarf2_per_objfile->addr.buffer
19252 + addr_base_or_zero + addr_index * addr_size);
19253 if (addr_size == 4)
19254 return bfd_get_32 (abfd, info_ptr);
19255 else
19256 return bfd_get_64 (abfd, info_ptr);
19257 }
19258
19259 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19260
19261 static CORE_ADDR
19262 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19263 {
19264 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19265 cu->addr_base, cu->header.addr_size);
19266 }
19267
19268 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19269
19270 static CORE_ADDR
19271 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19272 unsigned int *bytes_read)
19273 {
19274 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19275 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19276
19277 return read_addr_index (cu, addr_index);
19278 }
19279
19280 /* See read.h. */
19281
19282 CORE_ADDR
19283 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19284 {
19285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19286 struct dwarf2_cu *cu = per_cu->cu;
19287 gdb::optional<ULONGEST> addr_base;
19288 int addr_size;
19289
19290 /* We need addr_base and addr_size.
19291 If we don't have PER_CU->cu, we have to get it.
19292 Nasty, but the alternative is storing the needed info in PER_CU,
19293 which at this point doesn't seem justified: it's not clear how frequently
19294 it would get used and it would increase the size of every PER_CU.
19295 Entry points like dwarf2_per_cu_addr_size do a similar thing
19296 so we're not in uncharted territory here.
19297 Alas we need to be a bit more complicated as addr_base is contained
19298 in the DIE.
19299
19300 We don't need to read the entire CU(/TU).
19301 We just need the header and top level die.
19302
19303 IWBN to use the aging mechanism to let us lazily later discard the CU.
19304 For now we skip this optimization. */
19305
19306 if (cu != NULL)
19307 {
19308 addr_base = cu->addr_base;
19309 addr_size = cu->header.addr_size;
19310 }
19311 else
19312 {
19313 cutu_reader reader (per_cu, NULL, 0, false);
19314 addr_base = reader.cu->addr_base;
19315 addr_size = reader.cu->header.addr_size;
19316 }
19317
19318 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19319 addr_size);
19320 }
19321
19322 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19323 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19324 DWO file. */
19325
19326 static const char *
19327 read_str_index (struct dwarf2_cu *cu,
19328 struct dwarf2_section_info *str_section,
19329 struct dwarf2_section_info *str_offsets_section,
19330 ULONGEST str_offsets_base, ULONGEST str_index)
19331 {
19332 struct dwarf2_per_objfile *dwarf2_per_objfile
19333 = cu->per_cu->dwarf2_per_objfile;
19334 struct objfile *objfile = dwarf2_per_objfile->objfile;
19335 const char *objf_name = objfile_name (objfile);
19336 bfd *abfd = objfile->obfd;
19337 const gdb_byte *info_ptr;
19338 ULONGEST str_offset;
19339 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19340
19341 str_section->read (objfile);
19342 str_offsets_section->read (objfile);
19343 if (str_section->buffer == NULL)
19344 error (_("%s used without %s section"
19345 " in CU at offset %s [in module %s]"),
19346 form_name, str_section->get_name (),
19347 sect_offset_str (cu->header.sect_off), objf_name);
19348 if (str_offsets_section->buffer == NULL)
19349 error (_("%s used without %s section"
19350 " in CU at offset %s [in module %s]"),
19351 form_name, str_section->get_name (),
19352 sect_offset_str (cu->header.sect_off), objf_name);
19353 info_ptr = (str_offsets_section->buffer
19354 + str_offsets_base
19355 + str_index * cu->header.offset_size);
19356 if (cu->header.offset_size == 4)
19357 str_offset = bfd_get_32 (abfd, info_ptr);
19358 else
19359 str_offset = bfd_get_64 (abfd, info_ptr);
19360 if (str_offset >= str_section->size)
19361 error (_("Offset from %s pointing outside of"
19362 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19363 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19364 return (const char *) (str_section->buffer + str_offset);
19365 }
19366
19367 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19368
19369 static const char *
19370 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19371 {
19372 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19373 ? reader->cu->header.addr_size : 0;
19374 return read_str_index (reader->cu,
19375 &reader->dwo_file->sections.str,
19376 &reader->dwo_file->sections.str_offsets,
19377 str_offsets_base, str_index);
19378 }
19379
19380 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19381
19382 static const char *
19383 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19384 {
19385 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19386 const char *objf_name = objfile_name (objfile);
19387 static const char form_name[] = "DW_FORM_GNU_str_index";
19388 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19389
19390 if (!cu->str_offsets_base.has_value ())
19391 error (_("%s used in Fission stub without %s"
19392 " in CU at offset 0x%lx [in module %s]"),
19393 form_name, str_offsets_attr_name,
19394 (long) cu->header.offset_size, objf_name);
19395
19396 return read_str_index (cu,
19397 &cu->per_cu->dwarf2_per_objfile->str,
19398 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19399 *cu->str_offsets_base, str_index);
19400 }
19401
19402 /* Return the length of an LEB128 number in BUF. */
19403
19404 static int
19405 leb128_size (const gdb_byte *buf)
19406 {
19407 const gdb_byte *begin = buf;
19408 gdb_byte byte;
19409
19410 while (1)
19411 {
19412 byte = *buf++;
19413 if ((byte & 128) == 0)
19414 return buf - begin;
19415 }
19416 }
19417
19418 static void
19419 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19420 {
19421 switch (lang)
19422 {
19423 case DW_LANG_C89:
19424 case DW_LANG_C99:
19425 case DW_LANG_C11:
19426 case DW_LANG_C:
19427 case DW_LANG_UPC:
19428 cu->language = language_c;
19429 break;
19430 case DW_LANG_Java:
19431 case DW_LANG_C_plus_plus:
19432 case DW_LANG_C_plus_plus_11:
19433 case DW_LANG_C_plus_plus_14:
19434 cu->language = language_cplus;
19435 break;
19436 case DW_LANG_D:
19437 cu->language = language_d;
19438 break;
19439 case DW_LANG_Fortran77:
19440 case DW_LANG_Fortran90:
19441 case DW_LANG_Fortran95:
19442 case DW_LANG_Fortran03:
19443 case DW_LANG_Fortran08:
19444 cu->language = language_fortran;
19445 break;
19446 case DW_LANG_Go:
19447 cu->language = language_go;
19448 break;
19449 case DW_LANG_Mips_Assembler:
19450 cu->language = language_asm;
19451 break;
19452 case DW_LANG_Ada83:
19453 case DW_LANG_Ada95:
19454 cu->language = language_ada;
19455 break;
19456 case DW_LANG_Modula2:
19457 cu->language = language_m2;
19458 break;
19459 case DW_LANG_Pascal83:
19460 cu->language = language_pascal;
19461 break;
19462 case DW_LANG_ObjC:
19463 cu->language = language_objc;
19464 break;
19465 case DW_LANG_Rust:
19466 case DW_LANG_Rust_old:
19467 cu->language = language_rust;
19468 break;
19469 case DW_LANG_Cobol74:
19470 case DW_LANG_Cobol85:
19471 default:
19472 cu->language = language_minimal;
19473 break;
19474 }
19475 cu->language_defn = language_def (cu->language);
19476 }
19477
19478 /* Return the named attribute or NULL if not there. */
19479
19480 static struct attribute *
19481 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19482 {
19483 for (;;)
19484 {
19485 unsigned int i;
19486 struct attribute *spec = NULL;
19487
19488 for (i = 0; i < die->num_attrs; ++i)
19489 {
19490 if (die->attrs[i].name == name)
19491 return &die->attrs[i];
19492 if (die->attrs[i].name == DW_AT_specification
19493 || die->attrs[i].name == DW_AT_abstract_origin)
19494 spec = &die->attrs[i];
19495 }
19496
19497 if (!spec)
19498 break;
19499
19500 die = follow_die_ref (die, spec, &cu);
19501 }
19502
19503 return NULL;
19504 }
19505
19506 /* Return the string associated with a string-typed attribute, or NULL if it
19507 is either not found or is of an incorrect type. */
19508
19509 static const char *
19510 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19511 {
19512 struct attribute *attr;
19513 const char *str = NULL;
19514
19515 attr = dwarf2_attr (die, name, cu);
19516
19517 if (attr != NULL)
19518 {
19519 str = attr->value_as_string ();
19520 if (str == nullptr)
19521 complaint (_("string type expected for attribute %s for "
19522 "DIE at %s in module %s"),
19523 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19524 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19525 }
19526
19527 return str;
19528 }
19529
19530 /* Return the dwo name or NULL if not present. If present, it is in either
19531 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19532 static const char *
19533 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19534 {
19535 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19536 if (dwo_name == nullptr)
19537 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19538 return dwo_name;
19539 }
19540
19541 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19542 and holds a non-zero value. This function should only be used for
19543 DW_FORM_flag or DW_FORM_flag_present attributes. */
19544
19545 static int
19546 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19547 {
19548 struct attribute *attr = dwarf2_attr (die, name, cu);
19549
19550 return (attr && DW_UNSND (attr));
19551 }
19552
19553 static int
19554 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19555 {
19556 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19557 which value is non-zero. However, we have to be careful with
19558 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19559 (via dwarf2_flag_true_p) follows this attribute. So we may
19560 end up accidently finding a declaration attribute that belongs
19561 to a different DIE referenced by the specification attribute,
19562 even though the given DIE does not have a declaration attribute. */
19563 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19564 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19565 }
19566
19567 /* Return the die giving the specification for DIE, if there is
19568 one. *SPEC_CU is the CU containing DIE on input, and the CU
19569 containing the return value on output. If there is no
19570 specification, but there is an abstract origin, that is
19571 returned. */
19572
19573 static struct die_info *
19574 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19575 {
19576 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19577 *spec_cu);
19578
19579 if (spec_attr == NULL)
19580 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19581
19582 if (spec_attr == NULL)
19583 return NULL;
19584 else
19585 return follow_die_ref (die, spec_attr, spec_cu);
19586 }
19587
19588 /* Stub for free_line_header to match void * callback types. */
19589
19590 static void
19591 free_line_header_voidp (void *arg)
19592 {
19593 struct line_header *lh = (struct line_header *) arg;
19594
19595 delete lh;
19596 }
19597
19598 /* A convenience function to find the proper .debug_line section for a CU. */
19599
19600 static struct dwarf2_section_info *
19601 get_debug_line_section (struct dwarf2_cu *cu)
19602 {
19603 struct dwarf2_section_info *section;
19604 struct dwarf2_per_objfile *dwarf2_per_objfile
19605 = cu->per_cu->dwarf2_per_objfile;
19606
19607 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19608 DWO file. */
19609 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19610 section = &cu->dwo_unit->dwo_file->sections.line;
19611 else if (cu->per_cu->is_dwz)
19612 {
19613 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19614
19615 section = &dwz->line;
19616 }
19617 else
19618 section = &dwarf2_per_objfile->line;
19619
19620 return section;
19621 }
19622
19623 /* Read the statement program header starting at OFFSET in
19624 .debug_line, or .debug_line.dwo. Return a pointer
19625 to a struct line_header, allocated using xmalloc.
19626 Returns NULL if there is a problem reading the header, e.g., if it
19627 has a version we don't understand.
19628
19629 NOTE: the strings in the include directory and file name tables of
19630 the returned object point into the dwarf line section buffer,
19631 and must not be freed. */
19632
19633 static line_header_up
19634 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19635 {
19636 struct dwarf2_section_info *section;
19637 struct dwarf2_per_objfile *dwarf2_per_objfile
19638 = cu->per_cu->dwarf2_per_objfile;
19639
19640 section = get_debug_line_section (cu);
19641 section->read (dwarf2_per_objfile->objfile);
19642 if (section->buffer == NULL)
19643 {
19644 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19645 complaint (_("missing .debug_line.dwo section"));
19646 else
19647 complaint (_("missing .debug_line section"));
19648 return 0;
19649 }
19650
19651 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19652 dwarf2_per_objfile, section,
19653 &cu->header);
19654 }
19655
19656 /* Subroutine of dwarf_decode_lines to simplify it.
19657 Return the file name of the psymtab for the given file_entry.
19658 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19659 If space for the result is malloc'd, *NAME_HOLDER will be set.
19660 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19661
19662 static const char *
19663 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19664 const dwarf2_psymtab *pst,
19665 const char *comp_dir,
19666 gdb::unique_xmalloc_ptr<char> *name_holder)
19667 {
19668 const char *include_name = fe.name;
19669 const char *include_name_to_compare = include_name;
19670 const char *pst_filename;
19671 int file_is_pst;
19672
19673 const char *dir_name = fe.include_dir (lh);
19674
19675 gdb::unique_xmalloc_ptr<char> hold_compare;
19676 if (!IS_ABSOLUTE_PATH (include_name)
19677 && (dir_name != NULL || comp_dir != NULL))
19678 {
19679 /* Avoid creating a duplicate psymtab for PST.
19680 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19681 Before we do the comparison, however, we need to account
19682 for DIR_NAME and COMP_DIR.
19683 First prepend dir_name (if non-NULL). If we still don't
19684 have an absolute path prepend comp_dir (if non-NULL).
19685 However, the directory we record in the include-file's
19686 psymtab does not contain COMP_DIR (to match the
19687 corresponding symtab(s)).
19688
19689 Example:
19690
19691 bash$ cd /tmp
19692 bash$ gcc -g ./hello.c
19693 include_name = "hello.c"
19694 dir_name = "."
19695 DW_AT_comp_dir = comp_dir = "/tmp"
19696 DW_AT_name = "./hello.c"
19697
19698 */
19699
19700 if (dir_name != NULL)
19701 {
19702 name_holder->reset (concat (dir_name, SLASH_STRING,
19703 include_name, (char *) NULL));
19704 include_name = name_holder->get ();
19705 include_name_to_compare = include_name;
19706 }
19707 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19708 {
19709 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19710 include_name, (char *) NULL));
19711 include_name_to_compare = hold_compare.get ();
19712 }
19713 }
19714
19715 pst_filename = pst->filename;
19716 gdb::unique_xmalloc_ptr<char> copied_name;
19717 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19718 {
19719 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19720 pst_filename, (char *) NULL));
19721 pst_filename = copied_name.get ();
19722 }
19723
19724 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19725
19726 if (file_is_pst)
19727 return NULL;
19728 return include_name;
19729 }
19730
19731 /* State machine to track the state of the line number program. */
19732
19733 class lnp_state_machine
19734 {
19735 public:
19736 /* Initialize a machine state for the start of a line number
19737 program. */
19738 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19739 bool record_lines_p);
19740
19741 file_entry *current_file ()
19742 {
19743 /* lh->file_names is 0-based, but the file name numbers in the
19744 statement program are 1-based. */
19745 return m_line_header->file_name_at (m_file);
19746 }
19747
19748 /* Record the line in the state machine. END_SEQUENCE is true if
19749 we're processing the end of a sequence. */
19750 void record_line (bool end_sequence);
19751
19752 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19753 nop-out rest of the lines in this sequence. */
19754 void check_line_address (struct dwarf2_cu *cu,
19755 const gdb_byte *line_ptr,
19756 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19757
19758 void handle_set_discriminator (unsigned int discriminator)
19759 {
19760 m_discriminator = discriminator;
19761 m_line_has_non_zero_discriminator |= discriminator != 0;
19762 }
19763
19764 /* Handle DW_LNE_set_address. */
19765 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19766 {
19767 m_op_index = 0;
19768 address += baseaddr;
19769 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19770 }
19771
19772 /* Handle DW_LNS_advance_pc. */
19773 void handle_advance_pc (CORE_ADDR adjust);
19774
19775 /* Handle a special opcode. */
19776 void handle_special_opcode (unsigned char op_code);
19777
19778 /* Handle DW_LNS_advance_line. */
19779 void handle_advance_line (int line_delta)
19780 {
19781 advance_line (line_delta);
19782 }
19783
19784 /* Handle DW_LNS_set_file. */
19785 void handle_set_file (file_name_index file);
19786
19787 /* Handle DW_LNS_negate_stmt. */
19788 void handle_negate_stmt ()
19789 {
19790 m_is_stmt = !m_is_stmt;
19791 }
19792
19793 /* Handle DW_LNS_const_add_pc. */
19794 void handle_const_add_pc ();
19795
19796 /* Handle DW_LNS_fixed_advance_pc. */
19797 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19798 {
19799 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19800 m_op_index = 0;
19801 }
19802
19803 /* Handle DW_LNS_copy. */
19804 void handle_copy ()
19805 {
19806 record_line (false);
19807 m_discriminator = 0;
19808 }
19809
19810 /* Handle DW_LNE_end_sequence. */
19811 void handle_end_sequence ()
19812 {
19813 m_currently_recording_lines = true;
19814 }
19815
19816 private:
19817 /* Advance the line by LINE_DELTA. */
19818 void advance_line (int line_delta)
19819 {
19820 m_line += line_delta;
19821
19822 if (line_delta != 0)
19823 m_line_has_non_zero_discriminator = m_discriminator != 0;
19824 }
19825
19826 struct dwarf2_cu *m_cu;
19827
19828 gdbarch *m_gdbarch;
19829
19830 /* True if we're recording lines.
19831 Otherwise we're building partial symtabs and are just interested in
19832 finding include files mentioned by the line number program. */
19833 bool m_record_lines_p;
19834
19835 /* The line number header. */
19836 line_header *m_line_header;
19837
19838 /* These are part of the standard DWARF line number state machine,
19839 and initialized according to the DWARF spec. */
19840
19841 unsigned char m_op_index = 0;
19842 /* The line table index of the current file. */
19843 file_name_index m_file = 1;
19844 unsigned int m_line = 1;
19845
19846 /* These are initialized in the constructor. */
19847
19848 CORE_ADDR m_address;
19849 bool m_is_stmt;
19850 unsigned int m_discriminator;
19851
19852 /* Additional bits of state we need to track. */
19853
19854 /* The last file that we called dwarf2_start_subfile for.
19855 This is only used for TLLs. */
19856 unsigned int m_last_file = 0;
19857 /* The last file a line number was recorded for. */
19858 struct subfile *m_last_subfile = NULL;
19859
19860 /* When true, record the lines we decode. */
19861 bool m_currently_recording_lines = false;
19862
19863 /* The last line number that was recorded, used to coalesce
19864 consecutive entries for the same line. This can happen, for
19865 example, when discriminators are present. PR 17276. */
19866 unsigned int m_last_line = 0;
19867 bool m_line_has_non_zero_discriminator = false;
19868 };
19869
19870 void
19871 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19872 {
19873 CORE_ADDR addr_adj = (((m_op_index + adjust)
19874 / m_line_header->maximum_ops_per_instruction)
19875 * m_line_header->minimum_instruction_length);
19876 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19877 m_op_index = ((m_op_index + adjust)
19878 % m_line_header->maximum_ops_per_instruction);
19879 }
19880
19881 void
19882 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19883 {
19884 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19885 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19886 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19887 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19888 / m_line_header->maximum_ops_per_instruction)
19889 * m_line_header->minimum_instruction_length);
19890 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19891 m_op_index = ((m_op_index + adj_opcode_d)
19892 % m_line_header->maximum_ops_per_instruction);
19893
19894 int line_delta = m_line_header->line_base + adj_opcode_r;
19895 advance_line (line_delta);
19896 record_line (false);
19897 m_discriminator = 0;
19898 }
19899
19900 void
19901 lnp_state_machine::handle_set_file (file_name_index file)
19902 {
19903 m_file = file;
19904
19905 const file_entry *fe = current_file ();
19906 if (fe == NULL)
19907 dwarf2_debug_line_missing_file_complaint ();
19908 else if (m_record_lines_p)
19909 {
19910 const char *dir = fe->include_dir (m_line_header);
19911
19912 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19913 m_line_has_non_zero_discriminator = m_discriminator != 0;
19914 dwarf2_start_subfile (m_cu, fe->name, dir);
19915 }
19916 }
19917
19918 void
19919 lnp_state_machine::handle_const_add_pc ()
19920 {
19921 CORE_ADDR adjust
19922 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19923
19924 CORE_ADDR addr_adj
19925 = (((m_op_index + adjust)
19926 / m_line_header->maximum_ops_per_instruction)
19927 * m_line_header->minimum_instruction_length);
19928
19929 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19930 m_op_index = ((m_op_index + adjust)
19931 % m_line_header->maximum_ops_per_instruction);
19932 }
19933
19934 /* Return non-zero if we should add LINE to the line number table.
19935 LINE is the line to add, LAST_LINE is the last line that was added,
19936 LAST_SUBFILE is the subfile for LAST_LINE.
19937 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19938 had a non-zero discriminator.
19939
19940 We have to be careful in the presence of discriminators.
19941 E.g., for this line:
19942
19943 for (i = 0; i < 100000; i++);
19944
19945 clang can emit four line number entries for that one line,
19946 each with a different discriminator.
19947 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19948
19949 However, we want gdb to coalesce all four entries into one.
19950 Otherwise the user could stepi into the middle of the line and
19951 gdb would get confused about whether the pc really was in the
19952 middle of the line.
19953
19954 Things are further complicated by the fact that two consecutive
19955 line number entries for the same line is a heuristic used by gcc
19956 to denote the end of the prologue. So we can't just discard duplicate
19957 entries, we have to be selective about it. The heuristic we use is
19958 that we only collapse consecutive entries for the same line if at least
19959 one of those entries has a non-zero discriminator. PR 17276.
19960
19961 Note: Addresses in the line number state machine can never go backwards
19962 within one sequence, thus this coalescing is ok. */
19963
19964 static int
19965 dwarf_record_line_p (struct dwarf2_cu *cu,
19966 unsigned int line, unsigned int last_line,
19967 int line_has_non_zero_discriminator,
19968 struct subfile *last_subfile)
19969 {
19970 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19971 return 1;
19972 if (line != last_line)
19973 return 1;
19974 /* Same line for the same file that we've seen already.
19975 As a last check, for pr 17276, only record the line if the line
19976 has never had a non-zero discriminator. */
19977 if (!line_has_non_zero_discriminator)
19978 return 1;
19979 return 0;
19980 }
19981
19982 /* Use the CU's builder to record line number LINE beginning at
19983 address ADDRESS in the line table of subfile SUBFILE. */
19984
19985 static void
19986 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19987 unsigned int line, CORE_ADDR address, bool is_stmt,
19988 struct dwarf2_cu *cu)
19989 {
19990 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19991
19992 if (dwarf_line_debug)
19993 {
19994 fprintf_unfiltered (gdb_stdlog,
19995 "Recording line %u, file %s, address %s\n",
19996 line, lbasename (subfile->name),
19997 paddress (gdbarch, address));
19998 }
19999
20000 if (cu != nullptr)
20001 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20002 }
20003
20004 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20005 Mark the end of a set of line number records.
20006 The arguments are the same as for dwarf_record_line_1.
20007 If SUBFILE is NULL the request is ignored. */
20008
20009 static void
20010 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20011 CORE_ADDR address, struct dwarf2_cu *cu)
20012 {
20013 if (subfile == NULL)
20014 return;
20015
20016 if (dwarf_line_debug)
20017 {
20018 fprintf_unfiltered (gdb_stdlog,
20019 "Finishing current line, file %s, address %s\n",
20020 lbasename (subfile->name),
20021 paddress (gdbarch, address));
20022 }
20023
20024 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20025 }
20026
20027 void
20028 lnp_state_machine::record_line (bool end_sequence)
20029 {
20030 if (dwarf_line_debug)
20031 {
20032 fprintf_unfiltered (gdb_stdlog,
20033 "Processing actual line %u: file %u,"
20034 " address %s, is_stmt %u, discrim %u%s\n",
20035 m_line, m_file,
20036 paddress (m_gdbarch, m_address),
20037 m_is_stmt, m_discriminator,
20038 (end_sequence ? "\t(end sequence)" : ""));
20039 }
20040
20041 file_entry *fe = current_file ();
20042
20043 if (fe == NULL)
20044 dwarf2_debug_line_missing_file_complaint ();
20045 /* For now we ignore lines not starting on an instruction boundary.
20046 But not when processing end_sequence for compatibility with the
20047 previous version of the code. */
20048 else if (m_op_index == 0 || end_sequence)
20049 {
20050 fe->included_p = 1;
20051 if (m_record_lines_p)
20052 {
20053 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20054 || end_sequence)
20055 {
20056 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20057 m_currently_recording_lines ? m_cu : nullptr);
20058 }
20059
20060 if (!end_sequence)
20061 {
20062 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20063
20064 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20065 m_line_has_non_zero_discriminator,
20066 m_last_subfile))
20067 {
20068 buildsym_compunit *builder = m_cu->get_builder ();
20069 dwarf_record_line_1 (m_gdbarch,
20070 builder->get_current_subfile (),
20071 m_line, m_address, is_stmt,
20072 m_currently_recording_lines ? m_cu : nullptr);
20073 }
20074 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20075 m_last_line = m_line;
20076 }
20077 }
20078 }
20079 }
20080
20081 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20082 line_header *lh, bool record_lines_p)
20083 {
20084 m_cu = cu;
20085 m_gdbarch = arch;
20086 m_record_lines_p = record_lines_p;
20087 m_line_header = lh;
20088
20089 m_currently_recording_lines = true;
20090
20091 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20092 was a line entry for it so that the backend has a chance to adjust it
20093 and also record it in case it needs it. This is currently used by MIPS
20094 code, cf. `mips_adjust_dwarf2_line'. */
20095 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20096 m_is_stmt = lh->default_is_stmt;
20097 m_discriminator = 0;
20098 }
20099
20100 void
20101 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20102 const gdb_byte *line_ptr,
20103 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20104 {
20105 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20106 the pc range of the CU. However, we restrict the test to only ADDRESS
20107 values of zero to preserve GDB's previous behaviour which is to handle
20108 the specific case of a function being GC'd by the linker. */
20109
20110 if (address == 0 && address < unrelocated_lowpc)
20111 {
20112 /* This line table is for a function which has been
20113 GCd by the linker. Ignore it. PR gdb/12528 */
20114
20115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20116 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20117
20118 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20119 line_offset, objfile_name (objfile));
20120 m_currently_recording_lines = false;
20121 /* Note: m_currently_recording_lines is left as false until we see
20122 DW_LNE_end_sequence. */
20123 }
20124 }
20125
20126 /* Subroutine of dwarf_decode_lines to simplify it.
20127 Process the line number information in LH.
20128 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20129 program in order to set included_p for every referenced header. */
20130
20131 static void
20132 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20133 const int decode_for_pst_p, CORE_ADDR lowpc)
20134 {
20135 const gdb_byte *line_ptr, *extended_end;
20136 const gdb_byte *line_end;
20137 unsigned int bytes_read, extended_len;
20138 unsigned char op_code, extended_op;
20139 CORE_ADDR baseaddr;
20140 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20141 bfd *abfd = objfile->obfd;
20142 struct gdbarch *gdbarch = objfile->arch ();
20143 /* True if we're recording line info (as opposed to building partial
20144 symtabs and just interested in finding include files mentioned by
20145 the line number program). */
20146 bool record_lines_p = !decode_for_pst_p;
20147
20148 baseaddr = objfile->text_section_offset ();
20149
20150 line_ptr = lh->statement_program_start;
20151 line_end = lh->statement_program_end;
20152
20153 /* Read the statement sequences until there's nothing left. */
20154 while (line_ptr < line_end)
20155 {
20156 /* The DWARF line number program state machine. Reset the state
20157 machine at the start of each sequence. */
20158 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20159 bool end_sequence = false;
20160
20161 if (record_lines_p)
20162 {
20163 /* Start a subfile for the current file of the state
20164 machine. */
20165 const file_entry *fe = state_machine.current_file ();
20166
20167 if (fe != NULL)
20168 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20169 }
20170
20171 /* Decode the table. */
20172 while (line_ptr < line_end && !end_sequence)
20173 {
20174 op_code = read_1_byte (abfd, line_ptr);
20175 line_ptr += 1;
20176
20177 if (op_code >= lh->opcode_base)
20178 {
20179 /* Special opcode. */
20180 state_machine.handle_special_opcode (op_code);
20181 }
20182 else switch (op_code)
20183 {
20184 case DW_LNS_extended_op:
20185 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20186 &bytes_read);
20187 line_ptr += bytes_read;
20188 extended_end = line_ptr + extended_len;
20189 extended_op = read_1_byte (abfd, line_ptr);
20190 line_ptr += 1;
20191 switch (extended_op)
20192 {
20193 case DW_LNE_end_sequence:
20194 state_machine.handle_end_sequence ();
20195 end_sequence = true;
20196 break;
20197 case DW_LNE_set_address:
20198 {
20199 CORE_ADDR address
20200 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20201 line_ptr += bytes_read;
20202
20203 state_machine.check_line_address (cu, line_ptr,
20204 lowpc - baseaddr, address);
20205 state_machine.handle_set_address (baseaddr, address);
20206 }
20207 break;
20208 case DW_LNE_define_file:
20209 {
20210 const char *cur_file;
20211 unsigned int mod_time, length;
20212 dir_index dindex;
20213
20214 cur_file = read_direct_string (abfd, line_ptr,
20215 &bytes_read);
20216 line_ptr += bytes_read;
20217 dindex = (dir_index)
20218 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20219 line_ptr += bytes_read;
20220 mod_time =
20221 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20222 line_ptr += bytes_read;
20223 length =
20224 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20225 line_ptr += bytes_read;
20226 lh->add_file_name (cur_file, dindex, mod_time, length);
20227 }
20228 break;
20229 case DW_LNE_set_discriminator:
20230 {
20231 /* The discriminator is not interesting to the
20232 debugger; just ignore it. We still need to
20233 check its value though:
20234 if there are consecutive entries for the same
20235 (non-prologue) line we want to coalesce them.
20236 PR 17276. */
20237 unsigned int discr
20238 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20239 line_ptr += bytes_read;
20240
20241 state_machine.handle_set_discriminator (discr);
20242 }
20243 break;
20244 default:
20245 complaint (_("mangled .debug_line section"));
20246 return;
20247 }
20248 /* Make sure that we parsed the extended op correctly. If e.g.
20249 we expected a different address size than the producer used,
20250 we may have read the wrong number of bytes. */
20251 if (line_ptr != extended_end)
20252 {
20253 complaint (_("mangled .debug_line section"));
20254 return;
20255 }
20256 break;
20257 case DW_LNS_copy:
20258 state_machine.handle_copy ();
20259 break;
20260 case DW_LNS_advance_pc:
20261 {
20262 CORE_ADDR adjust
20263 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20264 line_ptr += bytes_read;
20265
20266 state_machine.handle_advance_pc (adjust);
20267 }
20268 break;
20269 case DW_LNS_advance_line:
20270 {
20271 int line_delta
20272 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20273 line_ptr += bytes_read;
20274
20275 state_machine.handle_advance_line (line_delta);
20276 }
20277 break;
20278 case DW_LNS_set_file:
20279 {
20280 file_name_index file
20281 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20282 &bytes_read);
20283 line_ptr += bytes_read;
20284
20285 state_machine.handle_set_file (file);
20286 }
20287 break;
20288 case DW_LNS_set_column:
20289 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20290 line_ptr += bytes_read;
20291 break;
20292 case DW_LNS_negate_stmt:
20293 state_machine.handle_negate_stmt ();
20294 break;
20295 case DW_LNS_set_basic_block:
20296 break;
20297 /* Add to the address register of the state machine the
20298 address increment value corresponding to special opcode
20299 255. I.e., this value is scaled by the minimum
20300 instruction length since special opcode 255 would have
20301 scaled the increment. */
20302 case DW_LNS_const_add_pc:
20303 state_machine.handle_const_add_pc ();
20304 break;
20305 case DW_LNS_fixed_advance_pc:
20306 {
20307 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20308 line_ptr += 2;
20309
20310 state_machine.handle_fixed_advance_pc (addr_adj);
20311 }
20312 break;
20313 default:
20314 {
20315 /* Unknown standard opcode, ignore it. */
20316 int i;
20317
20318 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20319 {
20320 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20321 line_ptr += bytes_read;
20322 }
20323 }
20324 }
20325 }
20326
20327 if (!end_sequence)
20328 dwarf2_debug_line_missing_end_sequence_complaint ();
20329
20330 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20331 in which case we still finish recording the last line). */
20332 state_machine.record_line (true);
20333 }
20334 }
20335
20336 /* Decode the Line Number Program (LNP) for the given line_header
20337 structure and CU. The actual information extracted and the type
20338 of structures created from the LNP depends on the value of PST.
20339
20340 1. If PST is NULL, then this procedure uses the data from the program
20341 to create all necessary symbol tables, and their linetables.
20342
20343 2. If PST is not NULL, this procedure reads the program to determine
20344 the list of files included by the unit represented by PST, and
20345 builds all the associated partial symbol tables.
20346
20347 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20348 It is used for relative paths in the line table.
20349 NOTE: When processing partial symtabs (pst != NULL),
20350 comp_dir == pst->dirname.
20351
20352 NOTE: It is important that psymtabs have the same file name (via strcmp)
20353 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20354 symtab we don't use it in the name of the psymtabs we create.
20355 E.g. expand_line_sal requires this when finding psymtabs to expand.
20356 A good testcase for this is mb-inline.exp.
20357
20358 LOWPC is the lowest address in CU (or 0 if not known).
20359
20360 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20361 for its PC<->lines mapping information. Otherwise only the filename
20362 table is read in. */
20363
20364 static void
20365 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20366 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20367 CORE_ADDR lowpc, int decode_mapping)
20368 {
20369 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20370 const int decode_for_pst_p = (pst != NULL);
20371
20372 if (decode_mapping)
20373 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20374
20375 if (decode_for_pst_p)
20376 {
20377 /* Now that we're done scanning the Line Header Program, we can
20378 create the psymtab of each included file. */
20379 for (auto &file_entry : lh->file_names ())
20380 if (file_entry.included_p == 1)
20381 {
20382 gdb::unique_xmalloc_ptr<char> name_holder;
20383 const char *include_name =
20384 psymtab_include_file_name (lh, file_entry, pst,
20385 comp_dir, &name_holder);
20386 if (include_name != NULL)
20387 dwarf2_create_include_psymtab (include_name, pst, objfile);
20388 }
20389 }
20390 else
20391 {
20392 /* Make sure a symtab is created for every file, even files
20393 which contain only variables (i.e. no code with associated
20394 line numbers). */
20395 buildsym_compunit *builder = cu->get_builder ();
20396 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20397
20398 for (auto &fe : lh->file_names ())
20399 {
20400 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20401 if (builder->get_current_subfile ()->symtab == NULL)
20402 {
20403 builder->get_current_subfile ()->symtab
20404 = allocate_symtab (cust,
20405 builder->get_current_subfile ()->name);
20406 }
20407 fe.symtab = builder->get_current_subfile ()->symtab;
20408 }
20409 }
20410 }
20411
20412 /* Start a subfile for DWARF. FILENAME is the name of the file and
20413 DIRNAME the name of the source directory which contains FILENAME
20414 or NULL if not known.
20415 This routine tries to keep line numbers from identical absolute and
20416 relative file names in a common subfile.
20417
20418 Using the `list' example from the GDB testsuite, which resides in
20419 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20420 of /srcdir/list0.c yields the following debugging information for list0.c:
20421
20422 DW_AT_name: /srcdir/list0.c
20423 DW_AT_comp_dir: /compdir
20424 files.files[0].name: list0.h
20425 files.files[0].dir: /srcdir
20426 files.files[1].name: list0.c
20427 files.files[1].dir: /srcdir
20428
20429 The line number information for list0.c has to end up in a single
20430 subfile, so that `break /srcdir/list0.c:1' works as expected.
20431 start_subfile will ensure that this happens provided that we pass the
20432 concatenation of files.files[1].dir and files.files[1].name as the
20433 subfile's name. */
20434
20435 static void
20436 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20437 const char *dirname)
20438 {
20439 gdb::unique_xmalloc_ptr<char> copy;
20440
20441 /* In order not to lose the line information directory,
20442 we concatenate it to the filename when it makes sense.
20443 Note that the Dwarf3 standard says (speaking of filenames in line
20444 information): ``The directory index is ignored for file names
20445 that represent full path names''. Thus ignoring dirname in the
20446 `else' branch below isn't an issue. */
20447
20448 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20449 {
20450 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20451 filename = copy.get ();
20452 }
20453
20454 cu->get_builder ()->start_subfile (filename);
20455 }
20456
20457 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20458 buildsym_compunit constructor. */
20459
20460 struct compunit_symtab *
20461 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20462 CORE_ADDR low_pc)
20463 {
20464 gdb_assert (m_builder == nullptr);
20465
20466 m_builder.reset (new struct buildsym_compunit
20467 (per_cu->dwarf2_per_objfile->objfile,
20468 name, comp_dir, language, low_pc));
20469
20470 list_in_scope = get_builder ()->get_file_symbols ();
20471
20472 get_builder ()->record_debugformat ("DWARF 2");
20473 get_builder ()->record_producer (producer);
20474
20475 processing_has_namespace_info = false;
20476
20477 return get_builder ()->get_compunit_symtab ();
20478 }
20479
20480 static void
20481 var_decode_location (struct attribute *attr, struct symbol *sym,
20482 struct dwarf2_cu *cu)
20483 {
20484 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20485 struct comp_unit_head *cu_header = &cu->header;
20486
20487 /* NOTE drow/2003-01-30: There used to be a comment and some special
20488 code here to turn a symbol with DW_AT_external and a
20489 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20490 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20491 with some versions of binutils) where shared libraries could have
20492 relocations against symbols in their debug information - the
20493 minimal symbol would have the right address, but the debug info
20494 would not. It's no longer necessary, because we will explicitly
20495 apply relocations when we read in the debug information now. */
20496
20497 /* A DW_AT_location attribute with no contents indicates that a
20498 variable has been optimized away. */
20499 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20500 {
20501 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20502 return;
20503 }
20504
20505 /* Handle one degenerate form of location expression specially, to
20506 preserve GDB's previous behavior when section offsets are
20507 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20508 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20509
20510 if (attr->form_is_block ()
20511 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20512 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20513 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20514 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20515 && (DW_BLOCK (attr)->size
20516 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20517 {
20518 unsigned int dummy;
20519
20520 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20521 SET_SYMBOL_VALUE_ADDRESS
20522 (sym, cu->header.read_address (objfile->obfd,
20523 DW_BLOCK (attr)->data + 1,
20524 &dummy));
20525 else
20526 SET_SYMBOL_VALUE_ADDRESS
20527 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20528 &dummy));
20529 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20530 fixup_symbol_section (sym, objfile);
20531 SET_SYMBOL_VALUE_ADDRESS
20532 (sym,
20533 SYMBOL_VALUE_ADDRESS (sym)
20534 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20535 return;
20536 }
20537
20538 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20539 expression evaluator, and use LOC_COMPUTED only when necessary
20540 (i.e. when the value of a register or memory location is
20541 referenced, or a thread-local block, etc.). Then again, it might
20542 not be worthwhile. I'm assuming that it isn't unless performance
20543 or memory numbers show me otherwise. */
20544
20545 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20546
20547 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20548 cu->has_loclist = true;
20549 }
20550
20551 /* Given a pointer to a DWARF information entry, figure out if we need
20552 to make a symbol table entry for it, and if so, create a new entry
20553 and return a pointer to it.
20554 If TYPE is NULL, determine symbol type from the die, otherwise
20555 used the passed type.
20556 If SPACE is not NULL, use it to hold the new symbol. If it is
20557 NULL, allocate a new symbol on the objfile's obstack. */
20558
20559 static struct symbol *
20560 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20561 struct symbol *space)
20562 {
20563 struct dwarf2_per_objfile *dwarf2_per_objfile
20564 = cu->per_cu->dwarf2_per_objfile;
20565 struct objfile *objfile = dwarf2_per_objfile->objfile;
20566 struct gdbarch *gdbarch = objfile->arch ();
20567 struct symbol *sym = NULL;
20568 const char *name;
20569 struct attribute *attr = NULL;
20570 struct attribute *attr2 = NULL;
20571 CORE_ADDR baseaddr;
20572 struct pending **list_to_add = NULL;
20573
20574 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20575
20576 baseaddr = objfile->text_section_offset ();
20577
20578 name = dwarf2_name (die, cu);
20579 if (name)
20580 {
20581 int suppress_add = 0;
20582
20583 if (space)
20584 sym = space;
20585 else
20586 sym = allocate_symbol (objfile);
20587 OBJSTAT (objfile, n_syms++);
20588
20589 /* Cache this symbol's name and the name's demangled form (if any). */
20590 sym->set_language (cu->language, &objfile->objfile_obstack);
20591 /* Fortran does not have mangling standard and the mangling does differ
20592 between gfortran, iFort etc. */
20593 const char *physname
20594 = (cu->language == language_fortran
20595 ? dwarf2_full_name (name, die, cu)
20596 : dwarf2_physname (name, die, cu));
20597 const char *linkagename = dw2_linkage_name (die, cu);
20598
20599 if (linkagename == nullptr || cu->language == language_ada)
20600 sym->set_linkage_name (physname);
20601 else
20602 {
20603 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20604 sym->set_linkage_name (linkagename);
20605 }
20606
20607 /* Default assumptions.
20608 Use the passed type or decode it from the die. */
20609 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20610 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20611 if (type != NULL)
20612 SYMBOL_TYPE (sym) = type;
20613 else
20614 SYMBOL_TYPE (sym) = die_type (die, cu);
20615 attr = dwarf2_attr (die,
20616 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20617 cu);
20618 if (attr != nullptr)
20619 {
20620 SYMBOL_LINE (sym) = DW_UNSND (attr);
20621 }
20622
20623 attr = dwarf2_attr (die,
20624 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20625 cu);
20626 if (attr != nullptr)
20627 {
20628 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20629 struct file_entry *fe;
20630
20631 if (cu->line_header != NULL)
20632 fe = cu->line_header->file_name_at (file_index);
20633 else
20634 fe = NULL;
20635
20636 if (fe == NULL)
20637 complaint (_("file index out of range"));
20638 else
20639 symbol_set_symtab (sym, fe->symtab);
20640 }
20641
20642 switch (die->tag)
20643 {
20644 case DW_TAG_label:
20645 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20646 if (attr != nullptr)
20647 {
20648 CORE_ADDR addr;
20649
20650 addr = attr->value_as_address ();
20651 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20652 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20653 }
20654 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20655 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20656 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20657 add_symbol_to_list (sym, cu->list_in_scope);
20658 break;
20659 case DW_TAG_subprogram:
20660 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20661 finish_block. */
20662 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20663 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20664 if ((attr2 && (DW_UNSND (attr2) != 0))
20665 || cu->language == language_ada
20666 || cu->language == language_fortran)
20667 {
20668 /* Subprograms marked external are stored as a global symbol.
20669 Ada and Fortran subprograms, whether marked external or
20670 not, are always stored as a global symbol, because we want
20671 to be able to access them globally. For instance, we want
20672 to be able to break on a nested subprogram without having
20673 to specify the context. */
20674 list_to_add = cu->get_builder ()->get_global_symbols ();
20675 }
20676 else
20677 {
20678 list_to_add = cu->list_in_scope;
20679 }
20680 break;
20681 case DW_TAG_inlined_subroutine:
20682 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20683 finish_block. */
20684 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20685 SYMBOL_INLINED (sym) = 1;
20686 list_to_add = cu->list_in_scope;
20687 break;
20688 case DW_TAG_template_value_param:
20689 suppress_add = 1;
20690 /* Fall through. */
20691 case DW_TAG_constant:
20692 case DW_TAG_variable:
20693 case DW_TAG_member:
20694 /* Compilation with minimal debug info may result in
20695 variables with missing type entries. Change the
20696 misleading `void' type to something sensible. */
20697 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20698 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20699
20700 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20701 /* In the case of DW_TAG_member, we should only be called for
20702 static const members. */
20703 if (die->tag == DW_TAG_member)
20704 {
20705 /* dwarf2_add_field uses die_is_declaration,
20706 so we do the same. */
20707 gdb_assert (die_is_declaration (die, cu));
20708 gdb_assert (attr);
20709 }
20710 if (attr != nullptr)
20711 {
20712 dwarf2_const_value (attr, sym, cu);
20713 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20714 if (!suppress_add)
20715 {
20716 if (attr2 && (DW_UNSND (attr2) != 0))
20717 list_to_add = cu->get_builder ()->get_global_symbols ();
20718 else
20719 list_to_add = cu->list_in_scope;
20720 }
20721 break;
20722 }
20723 attr = dwarf2_attr (die, DW_AT_location, cu);
20724 if (attr != nullptr)
20725 {
20726 var_decode_location (attr, sym, cu);
20727 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20728
20729 /* Fortran explicitly imports any global symbols to the local
20730 scope by DW_TAG_common_block. */
20731 if (cu->language == language_fortran && die->parent
20732 && die->parent->tag == DW_TAG_common_block)
20733 attr2 = NULL;
20734
20735 if (SYMBOL_CLASS (sym) == LOC_STATIC
20736 && SYMBOL_VALUE_ADDRESS (sym) == 0
20737 && !dwarf2_per_objfile->has_section_at_zero)
20738 {
20739 /* When a static variable is eliminated by the linker,
20740 the corresponding debug information is not stripped
20741 out, but the variable address is set to null;
20742 do not add such variables into symbol table. */
20743 }
20744 else if (attr2 && (DW_UNSND (attr2) != 0))
20745 {
20746 if (SYMBOL_CLASS (sym) == LOC_STATIC
20747 && (objfile->flags & OBJF_MAINLINE) == 0
20748 && dwarf2_per_objfile->can_copy)
20749 {
20750 /* A global static variable might be subject to
20751 copy relocation. We first check for a local
20752 minsym, though, because maybe the symbol was
20753 marked hidden, in which case this would not
20754 apply. */
20755 bound_minimal_symbol found
20756 = (lookup_minimal_symbol_linkage
20757 (sym->linkage_name (), objfile));
20758 if (found.minsym != nullptr)
20759 sym->maybe_copied = 1;
20760 }
20761
20762 /* A variable with DW_AT_external is never static,
20763 but it may be block-scoped. */
20764 list_to_add
20765 = ((cu->list_in_scope
20766 == cu->get_builder ()->get_file_symbols ())
20767 ? cu->get_builder ()->get_global_symbols ()
20768 : cu->list_in_scope);
20769 }
20770 else
20771 list_to_add = cu->list_in_scope;
20772 }
20773 else
20774 {
20775 /* We do not know the address of this symbol.
20776 If it is an external symbol and we have type information
20777 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20778 The address of the variable will then be determined from
20779 the minimal symbol table whenever the variable is
20780 referenced. */
20781 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20782
20783 /* Fortran explicitly imports any global symbols to the local
20784 scope by DW_TAG_common_block. */
20785 if (cu->language == language_fortran && die->parent
20786 && die->parent->tag == DW_TAG_common_block)
20787 {
20788 /* SYMBOL_CLASS doesn't matter here because
20789 read_common_block is going to reset it. */
20790 if (!suppress_add)
20791 list_to_add = cu->list_in_scope;
20792 }
20793 else if (attr2 && (DW_UNSND (attr2) != 0)
20794 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20795 {
20796 /* A variable with DW_AT_external is never static, but it
20797 may be block-scoped. */
20798 list_to_add
20799 = ((cu->list_in_scope
20800 == cu->get_builder ()->get_file_symbols ())
20801 ? cu->get_builder ()->get_global_symbols ()
20802 : cu->list_in_scope);
20803
20804 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20805 }
20806 else if (!die_is_declaration (die, cu))
20807 {
20808 /* Use the default LOC_OPTIMIZED_OUT class. */
20809 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20810 if (!suppress_add)
20811 list_to_add = cu->list_in_scope;
20812 }
20813 }
20814 break;
20815 case DW_TAG_formal_parameter:
20816 {
20817 /* If we are inside a function, mark this as an argument. If
20818 not, we might be looking at an argument to an inlined function
20819 when we do not have enough information to show inlined frames;
20820 pretend it's a local variable in that case so that the user can
20821 still see it. */
20822 struct context_stack *curr
20823 = cu->get_builder ()->get_current_context_stack ();
20824 if (curr != nullptr && curr->name != nullptr)
20825 SYMBOL_IS_ARGUMENT (sym) = 1;
20826 attr = dwarf2_attr (die, DW_AT_location, cu);
20827 if (attr != nullptr)
20828 {
20829 var_decode_location (attr, sym, cu);
20830 }
20831 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20832 if (attr != nullptr)
20833 {
20834 dwarf2_const_value (attr, sym, cu);
20835 }
20836
20837 list_to_add = cu->list_in_scope;
20838 }
20839 break;
20840 case DW_TAG_unspecified_parameters:
20841 /* From varargs functions; gdb doesn't seem to have any
20842 interest in this information, so just ignore it for now.
20843 (FIXME?) */
20844 break;
20845 case DW_TAG_template_type_param:
20846 suppress_add = 1;
20847 /* Fall through. */
20848 case DW_TAG_class_type:
20849 case DW_TAG_interface_type:
20850 case DW_TAG_structure_type:
20851 case DW_TAG_union_type:
20852 case DW_TAG_set_type:
20853 case DW_TAG_enumeration_type:
20854 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20855 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20856
20857 {
20858 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20859 really ever be static objects: otherwise, if you try
20860 to, say, break of a class's method and you're in a file
20861 which doesn't mention that class, it won't work unless
20862 the check for all static symbols in lookup_symbol_aux
20863 saves you. See the OtherFileClass tests in
20864 gdb.c++/namespace.exp. */
20865
20866 if (!suppress_add)
20867 {
20868 buildsym_compunit *builder = cu->get_builder ();
20869 list_to_add
20870 = (cu->list_in_scope == builder->get_file_symbols ()
20871 && cu->language == language_cplus
20872 ? builder->get_global_symbols ()
20873 : cu->list_in_scope);
20874
20875 /* The semantics of C++ state that "struct foo {
20876 ... }" also defines a typedef for "foo". */
20877 if (cu->language == language_cplus
20878 || cu->language == language_ada
20879 || cu->language == language_d
20880 || cu->language == language_rust)
20881 {
20882 /* The symbol's name is already allocated along
20883 with this objfile, so we don't need to
20884 duplicate it for the type. */
20885 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20886 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20887 }
20888 }
20889 }
20890 break;
20891 case DW_TAG_typedef:
20892 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20893 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20894 list_to_add = cu->list_in_scope;
20895 break;
20896 case DW_TAG_base_type:
20897 case DW_TAG_subrange_type:
20898 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20899 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20900 list_to_add = cu->list_in_scope;
20901 break;
20902 case DW_TAG_enumerator:
20903 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20904 if (attr != nullptr)
20905 {
20906 dwarf2_const_value (attr, sym, cu);
20907 }
20908 {
20909 /* NOTE: carlton/2003-11-10: See comment above in the
20910 DW_TAG_class_type, etc. block. */
20911
20912 list_to_add
20913 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20914 && cu->language == language_cplus
20915 ? cu->get_builder ()->get_global_symbols ()
20916 : cu->list_in_scope);
20917 }
20918 break;
20919 case DW_TAG_imported_declaration:
20920 case DW_TAG_namespace:
20921 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20922 list_to_add = cu->get_builder ()->get_global_symbols ();
20923 break;
20924 case DW_TAG_module:
20925 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20926 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20927 list_to_add = cu->get_builder ()->get_global_symbols ();
20928 break;
20929 case DW_TAG_common_block:
20930 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20931 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20932 add_symbol_to_list (sym, cu->list_in_scope);
20933 break;
20934 default:
20935 /* Not a tag we recognize. Hopefully we aren't processing
20936 trash data, but since we must specifically ignore things
20937 we don't recognize, there is nothing else we should do at
20938 this point. */
20939 complaint (_("unsupported tag: '%s'"),
20940 dwarf_tag_name (die->tag));
20941 break;
20942 }
20943
20944 if (suppress_add)
20945 {
20946 sym->hash_next = objfile->template_symbols;
20947 objfile->template_symbols = sym;
20948 list_to_add = NULL;
20949 }
20950
20951 if (list_to_add != NULL)
20952 add_symbol_to_list (sym, list_to_add);
20953
20954 /* For the benefit of old versions of GCC, check for anonymous
20955 namespaces based on the demangled name. */
20956 if (!cu->processing_has_namespace_info
20957 && cu->language == language_cplus)
20958 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20959 }
20960 return (sym);
20961 }
20962
20963 /* Given an attr with a DW_FORM_dataN value in host byte order,
20964 zero-extend it as appropriate for the symbol's type. The DWARF
20965 standard (v4) is not entirely clear about the meaning of using
20966 DW_FORM_dataN for a constant with a signed type, where the type is
20967 wider than the data. The conclusion of a discussion on the DWARF
20968 list was that this is unspecified. We choose to always zero-extend
20969 because that is the interpretation long in use by GCC. */
20970
20971 static gdb_byte *
20972 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20973 struct dwarf2_cu *cu, LONGEST *value, int bits)
20974 {
20975 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20976 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20977 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20978 LONGEST l = DW_UNSND (attr);
20979
20980 if (bits < sizeof (*value) * 8)
20981 {
20982 l &= ((LONGEST) 1 << bits) - 1;
20983 *value = l;
20984 }
20985 else if (bits == sizeof (*value) * 8)
20986 *value = l;
20987 else
20988 {
20989 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20990 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20991 return bytes;
20992 }
20993
20994 return NULL;
20995 }
20996
20997 /* Read a constant value from an attribute. Either set *VALUE, or if
20998 the value does not fit in *VALUE, set *BYTES - either already
20999 allocated on the objfile obstack, or newly allocated on OBSTACK,
21000 or, set *BATON, if we translated the constant to a location
21001 expression. */
21002
21003 static void
21004 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21005 const char *name, struct obstack *obstack,
21006 struct dwarf2_cu *cu,
21007 LONGEST *value, const gdb_byte **bytes,
21008 struct dwarf2_locexpr_baton **baton)
21009 {
21010 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21011 struct comp_unit_head *cu_header = &cu->header;
21012 struct dwarf_block *blk;
21013 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21014 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21015
21016 *value = 0;
21017 *bytes = NULL;
21018 *baton = NULL;
21019
21020 switch (attr->form)
21021 {
21022 case DW_FORM_addr:
21023 case DW_FORM_addrx:
21024 case DW_FORM_GNU_addr_index:
21025 {
21026 gdb_byte *data;
21027
21028 if (TYPE_LENGTH (type) != cu_header->addr_size)
21029 dwarf2_const_value_length_mismatch_complaint (name,
21030 cu_header->addr_size,
21031 TYPE_LENGTH (type));
21032 /* Symbols of this form are reasonably rare, so we just
21033 piggyback on the existing location code rather than writing
21034 a new implementation of symbol_computed_ops. */
21035 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21036 (*baton)->per_cu = cu->per_cu;
21037 gdb_assert ((*baton)->per_cu);
21038
21039 (*baton)->size = 2 + cu_header->addr_size;
21040 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21041 (*baton)->data = data;
21042
21043 data[0] = DW_OP_addr;
21044 store_unsigned_integer (&data[1], cu_header->addr_size,
21045 byte_order, DW_ADDR (attr));
21046 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21047 }
21048 break;
21049 case DW_FORM_string:
21050 case DW_FORM_strp:
21051 case DW_FORM_strx:
21052 case DW_FORM_GNU_str_index:
21053 case DW_FORM_GNU_strp_alt:
21054 /* DW_STRING is already allocated on the objfile obstack, point
21055 directly to it. */
21056 *bytes = (const gdb_byte *) DW_STRING (attr);
21057 break;
21058 case DW_FORM_block1:
21059 case DW_FORM_block2:
21060 case DW_FORM_block4:
21061 case DW_FORM_block:
21062 case DW_FORM_exprloc:
21063 case DW_FORM_data16:
21064 blk = DW_BLOCK (attr);
21065 if (TYPE_LENGTH (type) != blk->size)
21066 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21067 TYPE_LENGTH (type));
21068 *bytes = blk->data;
21069 break;
21070
21071 /* The DW_AT_const_value attributes are supposed to carry the
21072 symbol's value "represented as it would be on the target
21073 architecture." By the time we get here, it's already been
21074 converted to host endianness, so we just need to sign- or
21075 zero-extend it as appropriate. */
21076 case DW_FORM_data1:
21077 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21078 break;
21079 case DW_FORM_data2:
21080 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21081 break;
21082 case DW_FORM_data4:
21083 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21084 break;
21085 case DW_FORM_data8:
21086 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21087 break;
21088
21089 case DW_FORM_sdata:
21090 case DW_FORM_implicit_const:
21091 *value = DW_SND (attr);
21092 break;
21093
21094 case DW_FORM_udata:
21095 *value = DW_UNSND (attr);
21096 break;
21097
21098 default:
21099 complaint (_("unsupported const value attribute form: '%s'"),
21100 dwarf_form_name (attr->form));
21101 *value = 0;
21102 break;
21103 }
21104 }
21105
21106
21107 /* Copy constant value from an attribute to a symbol. */
21108
21109 static void
21110 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21111 struct dwarf2_cu *cu)
21112 {
21113 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21114 LONGEST value;
21115 const gdb_byte *bytes;
21116 struct dwarf2_locexpr_baton *baton;
21117
21118 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21119 sym->print_name (),
21120 &objfile->objfile_obstack, cu,
21121 &value, &bytes, &baton);
21122
21123 if (baton != NULL)
21124 {
21125 SYMBOL_LOCATION_BATON (sym) = baton;
21126 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21127 }
21128 else if (bytes != NULL)
21129 {
21130 SYMBOL_VALUE_BYTES (sym) = bytes;
21131 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21132 }
21133 else
21134 {
21135 SYMBOL_VALUE (sym) = value;
21136 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21137 }
21138 }
21139
21140 /* Return the type of the die in question using its DW_AT_type attribute. */
21141
21142 static struct type *
21143 die_type (struct die_info *die, struct dwarf2_cu *cu)
21144 {
21145 struct attribute *type_attr;
21146
21147 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21148 if (!type_attr)
21149 {
21150 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21151 /* A missing DW_AT_type represents a void type. */
21152 return objfile_type (objfile)->builtin_void;
21153 }
21154
21155 return lookup_die_type (die, type_attr, cu);
21156 }
21157
21158 /* True iff CU's producer generates GNAT Ada auxiliary information
21159 that allows to find parallel types through that information instead
21160 of having to do expensive parallel lookups by type name. */
21161
21162 static int
21163 need_gnat_info (struct dwarf2_cu *cu)
21164 {
21165 /* Assume that the Ada compiler was GNAT, which always produces
21166 the auxiliary information. */
21167 return (cu->language == language_ada);
21168 }
21169
21170 /* Return the auxiliary type of the die in question using its
21171 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21172 attribute is not present. */
21173
21174 static struct type *
21175 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21176 {
21177 struct attribute *type_attr;
21178
21179 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21180 if (!type_attr)
21181 return NULL;
21182
21183 return lookup_die_type (die, type_attr, cu);
21184 }
21185
21186 /* If DIE has a descriptive_type attribute, then set the TYPE's
21187 descriptive type accordingly. */
21188
21189 static void
21190 set_descriptive_type (struct type *type, struct die_info *die,
21191 struct dwarf2_cu *cu)
21192 {
21193 struct type *descriptive_type = die_descriptive_type (die, cu);
21194
21195 if (descriptive_type)
21196 {
21197 ALLOCATE_GNAT_AUX_TYPE (type);
21198 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21199 }
21200 }
21201
21202 /* Return the containing type of the die in question using its
21203 DW_AT_containing_type attribute. */
21204
21205 static struct type *
21206 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21207 {
21208 struct attribute *type_attr;
21209 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21210
21211 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21212 if (!type_attr)
21213 error (_("Dwarf Error: Problem turning containing type into gdb type "
21214 "[in module %s]"), objfile_name (objfile));
21215
21216 return lookup_die_type (die, type_attr, cu);
21217 }
21218
21219 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21220
21221 static struct type *
21222 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21223 {
21224 struct dwarf2_per_objfile *dwarf2_per_objfile
21225 = cu->per_cu->dwarf2_per_objfile;
21226 struct objfile *objfile = dwarf2_per_objfile->objfile;
21227 char *saved;
21228
21229 std::string message
21230 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21231 objfile_name (objfile),
21232 sect_offset_str (cu->header.sect_off),
21233 sect_offset_str (die->sect_off));
21234 saved = obstack_strdup (&objfile->objfile_obstack, message);
21235
21236 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21237 }
21238
21239 /* Look up the type of DIE in CU using its type attribute ATTR.
21240 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21241 DW_AT_containing_type.
21242 If there is no type substitute an error marker. */
21243
21244 static struct type *
21245 lookup_die_type (struct die_info *die, const struct attribute *attr,
21246 struct dwarf2_cu *cu)
21247 {
21248 struct dwarf2_per_objfile *dwarf2_per_objfile
21249 = cu->per_cu->dwarf2_per_objfile;
21250 struct objfile *objfile = dwarf2_per_objfile->objfile;
21251 struct type *this_type;
21252
21253 gdb_assert (attr->name == DW_AT_type
21254 || attr->name == DW_AT_GNAT_descriptive_type
21255 || attr->name == DW_AT_containing_type);
21256
21257 /* First see if we have it cached. */
21258
21259 if (attr->form == DW_FORM_GNU_ref_alt)
21260 {
21261 struct dwarf2_per_cu_data *per_cu;
21262 sect_offset sect_off = attr->get_ref_die_offset ();
21263
21264 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21265 dwarf2_per_objfile);
21266 this_type = get_die_type_at_offset (sect_off, per_cu);
21267 }
21268 else if (attr->form_is_ref ())
21269 {
21270 sect_offset sect_off = attr->get_ref_die_offset ();
21271
21272 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21273 }
21274 else if (attr->form == DW_FORM_ref_sig8)
21275 {
21276 ULONGEST signature = DW_SIGNATURE (attr);
21277
21278 return get_signatured_type (die, signature, cu);
21279 }
21280 else
21281 {
21282 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21283 " at %s [in module %s]"),
21284 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21285 objfile_name (objfile));
21286 return build_error_marker_type (cu, die);
21287 }
21288
21289 /* If not cached we need to read it in. */
21290
21291 if (this_type == NULL)
21292 {
21293 struct die_info *type_die = NULL;
21294 struct dwarf2_cu *type_cu = cu;
21295
21296 if (attr->form_is_ref ())
21297 type_die = follow_die_ref (die, attr, &type_cu);
21298 if (type_die == NULL)
21299 return build_error_marker_type (cu, die);
21300 /* If we find the type now, it's probably because the type came
21301 from an inter-CU reference and the type's CU got expanded before
21302 ours. */
21303 this_type = read_type_die (type_die, type_cu);
21304 }
21305
21306 /* If we still don't have a type use an error marker. */
21307
21308 if (this_type == NULL)
21309 return build_error_marker_type (cu, die);
21310
21311 return this_type;
21312 }
21313
21314 /* Return the type in DIE, CU.
21315 Returns NULL for invalid types.
21316
21317 This first does a lookup in die_type_hash,
21318 and only reads the die in if necessary.
21319
21320 NOTE: This can be called when reading in partial or full symbols. */
21321
21322 static struct type *
21323 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21324 {
21325 struct type *this_type;
21326
21327 this_type = get_die_type (die, cu);
21328 if (this_type)
21329 return this_type;
21330
21331 return read_type_die_1 (die, cu);
21332 }
21333
21334 /* Read the type in DIE, CU.
21335 Returns NULL for invalid types. */
21336
21337 static struct type *
21338 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21339 {
21340 struct type *this_type = NULL;
21341
21342 switch (die->tag)
21343 {
21344 case DW_TAG_class_type:
21345 case DW_TAG_interface_type:
21346 case DW_TAG_structure_type:
21347 case DW_TAG_union_type:
21348 this_type = read_structure_type (die, cu);
21349 break;
21350 case DW_TAG_enumeration_type:
21351 this_type = read_enumeration_type (die, cu);
21352 break;
21353 case DW_TAG_subprogram:
21354 case DW_TAG_subroutine_type:
21355 case DW_TAG_inlined_subroutine:
21356 this_type = read_subroutine_type (die, cu);
21357 break;
21358 case DW_TAG_array_type:
21359 this_type = read_array_type (die, cu);
21360 break;
21361 case DW_TAG_set_type:
21362 this_type = read_set_type (die, cu);
21363 break;
21364 case DW_TAG_pointer_type:
21365 this_type = read_tag_pointer_type (die, cu);
21366 break;
21367 case DW_TAG_ptr_to_member_type:
21368 this_type = read_tag_ptr_to_member_type (die, cu);
21369 break;
21370 case DW_TAG_reference_type:
21371 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21372 break;
21373 case DW_TAG_rvalue_reference_type:
21374 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21375 break;
21376 case DW_TAG_const_type:
21377 this_type = read_tag_const_type (die, cu);
21378 break;
21379 case DW_TAG_volatile_type:
21380 this_type = read_tag_volatile_type (die, cu);
21381 break;
21382 case DW_TAG_restrict_type:
21383 this_type = read_tag_restrict_type (die, cu);
21384 break;
21385 case DW_TAG_string_type:
21386 this_type = read_tag_string_type (die, cu);
21387 break;
21388 case DW_TAG_typedef:
21389 this_type = read_typedef (die, cu);
21390 break;
21391 case DW_TAG_subrange_type:
21392 this_type = read_subrange_type (die, cu);
21393 break;
21394 case DW_TAG_base_type:
21395 this_type = read_base_type (die, cu);
21396 break;
21397 case DW_TAG_unspecified_type:
21398 this_type = read_unspecified_type (die, cu);
21399 break;
21400 case DW_TAG_namespace:
21401 this_type = read_namespace_type (die, cu);
21402 break;
21403 case DW_TAG_module:
21404 this_type = read_module_type (die, cu);
21405 break;
21406 case DW_TAG_atomic_type:
21407 this_type = read_tag_atomic_type (die, cu);
21408 break;
21409 default:
21410 complaint (_("unexpected tag in read_type_die: '%s'"),
21411 dwarf_tag_name (die->tag));
21412 break;
21413 }
21414
21415 return this_type;
21416 }
21417
21418 /* See if we can figure out if the class lives in a namespace. We do
21419 this by looking for a member function; its demangled name will
21420 contain namespace info, if there is any.
21421 Return the computed name or NULL.
21422 Space for the result is allocated on the objfile's obstack.
21423 This is the full-die version of guess_partial_die_structure_name.
21424 In this case we know DIE has no useful parent. */
21425
21426 static const char *
21427 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21428 {
21429 struct die_info *spec_die;
21430 struct dwarf2_cu *spec_cu;
21431 struct die_info *child;
21432 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21433
21434 spec_cu = cu;
21435 spec_die = die_specification (die, &spec_cu);
21436 if (spec_die != NULL)
21437 {
21438 die = spec_die;
21439 cu = spec_cu;
21440 }
21441
21442 for (child = die->child;
21443 child != NULL;
21444 child = child->sibling)
21445 {
21446 if (child->tag == DW_TAG_subprogram)
21447 {
21448 const char *linkage_name = dw2_linkage_name (child, cu);
21449
21450 if (linkage_name != NULL)
21451 {
21452 gdb::unique_xmalloc_ptr<char> actual_name
21453 (language_class_name_from_physname (cu->language_defn,
21454 linkage_name));
21455 const char *name = NULL;
21456
21457 if (actual_name != NULL)
21458 {
21459 const char *die_name = dwarf2_name (die, cu);
21460
21461 if (die_name != NULL
21462 && strcmp (die_name, actual_name.get ()) != 0)
21463 {
21464 /* Strip off the class name from the full name.
21465 We want the prefix. */
21466 int die_name_len = strlen (die_name);
21467 int actual_name_len = strlen (actual_name.get ());
21468 const char *ptr = actual_name.get ();
21469
21470 /* Test for '::' as a sanity check. */
21471 if (actual_name_len > die_name_len + 2
21472 && ptr[actual_name_len - die_name_len - 1] == ':')
21473 name = obstack_strndup (
21474 &objfile->per_bfd->storage_obstack,
21475 ptr, actual_name_len - die_name_len - 2);
21476 }
21477 }
21478 return name;
21479 }
21480 }
21481 }
21482
21483 return NULL;
21484 }
21485
21486 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21487 prefix part in such case. See
21488 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21489
21490 static const char *
21491 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21492 {
21493 struct attribute *attr;
21494 const char *base;
21495
21496 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21497 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21498 return NULL;
21499
21500 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21501 return NULL;
21502
21503 attr = dw2_linkage_name_attr (die, cu);
21504 if (attr == NULL || DW_STRING (attr) == NULL)
21505 return NULL;
21506
21507 /* dwarf2_name had to be already called. */
21508 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21509
21510 /* Strip the base name, keep any leading namespaces/classes. */
21511 base = strrchr (DW_STRING (attr), ':');
21512 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21513 return "";
21514
21515 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21516 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21517 DW_STRING (attr),
21518 &base[-1] - DW_STRING (attr));
21519 }
21520
21521 /* Return the name of the namespace/class that DIE is defined within,
21522 or "" if we can't tell. The caller should not xfree the result.
21523
21524 For example, if we're within the method foo() in the following
21525 code:
21526
21527 namespace N {
21528 class C {
21529 void foo () {
21530 }
21531 };
21532 }
21533
21534 then determine_prefix on foo's die will return "N::C". */
21535
21536 static const char *
21537 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21538 {
21539 struct dwarf2_per_objfile *dwarf2_per_objfile
21540 = cu->per_cu->dwarf2_per_objfile;
21541 struct die_info *parent, *spec_die;
21542 struct dwarf2_cu *spec_cu;
21543 struct type *parent_type;
21544 const char *retval;
21545
21546 if (cu->language != language_cplus
21547 && cu->language != language_fortran && cu->language != language_d
21548 && cu->language != language_rust)
21549 return "";
21550
21551 retval = anonymous_struct_prefix (die, cu);
21552 if (retval)
21553 return retval;
21554
21555 /* We have to be careful in the presence of DW_AT_specification.
21556 For example, with GCC 3.4, given the code
21557
21558 namespace N {
21559 void foo() {
21560 // Definition of N::foo.
21561 }
21562 }
21563
21564 then we'll have a tree of DIEs like this:
21565
21566 1: DW_TAG_compile_unit
21567 2: DW_TAG_namespace // N
21568 3: DW_TAG_subprogram // declaration of N::foo
21569 4: DW_TAG_subprogram // definition of N::foo
21570 DW_AT_specification // refers to die #3
21571
21572 Thus, when processing die #4, we have to pretend that we're in
21573 the context of its DW_AT_specification, namely the contex of die
21574 #3. */
21575 spec_cu = cu;
21576 spec_die = die_specification (die, &spec_cu);
21577 if (spec_die == NULL)
21578 parent = die->parent;
21579 else
21580 {
21581 parent = spec_die->parent;
21582 cu = spec_cu;
21583 }
21584
21585 if (parent == NULL)
21586 return "";
21587 else if (parent->building_fullname)
21588 {
21589 const char *name;
21590 const char *parent_name;
21591
21592 /* It has been seen on RealView 2.2 built binaries,
21593 DW_TAG_template_type_param types actually _defined_ as
21594 children of the parent class:
21595
21596 enum E {};
21597 template class <class Enum> Class{};
21598 Class<enum E> class_e;
21599
21600 1: DW_TAG_class_type (Class)
21601 2: DW_TAG_enumeration_type (E)
21602 3: DW_TAG_enumerator (enum1:0)
21603 3: DW_TAG_enumerator (enum2:1)
21604 ...
21605 2: DW_TAG_template_type_param
21606 DW_AT_type DW_FORM_ref_udata (E)
21607
21608 Besides being broken debug info, it can put GDB into an
21609 infinite loop. Consider:
21610
21611 When we're building the full name for Class<E>, we'll start
21612 at Class, and go look over its template type parameters,
21613 finding E. We'll then try to build the full name of E, and
21614 reach here. We're now trying to build the full name of E,
21615 and look over the parent DIE for containing scope. In the
21616 broken case, if we followed the parent DIE of E, we'd again
21617 find Class, and once again go look at its template type
21618 arguments, etc., etc. Simply don't consider such parent die
21619 as source-level parent of this die (it can't be, the language
21620 doesn't allow it), and break the loop here. */
21621 name = dwarf2_name (die, cu);
21622 parent_name = dwarf2_name (parent, cu);
21623 complaint (_("template param type '%s' defined within parent '%s'"),
21624 name ? name : "<unknown>",
21625 parent_name ? parent_name : "<unknown>");
21626 return "";
21627 }
21628 else
21629 switch (parent->tag)
21630 {
21631 case DW_TAG_namespace:
21632 parent_type = read_type_die (parent, cu);
21633 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21634 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21635 Work around this problem here. */
21636 if (cu->language == language_cplus
21637 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21638 return "";
21639 /* We give a name to even anonymous namespaces. */
21640 return TYPE_NAME (parent_type);
21641 case DW_TAG_class_type:
21642 case DW_TAG_interface_type:
21643 case DW_TAG_structure_type:
21644 case DW_TAG_union_type:
21645 case DW_TAG_module:
21646 parent_type = read_type_die (parent, cu);
21647 if (TYPE_NAME (parent_type) != NULL)
21648 return TYPE_NAME (parent_type);
21649 else
21650 /* An anonymous structure is only allowed non-static data
21651 members; no typedefs, no member functions, et cetera.
21652 So it does not need a prefix. */
21653 return "";
21654 case DW_TAG_compile_unit:
21655 case DW_TAG_partial_unit:
21656 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21657 if (cu->language == language_cplus
21658 && !dwarf2_per_objfile->types.empty ()
21659 && die->child != NULL
21660 && (die->tag == DW_TAG_class_type
21661 || die->tag == DW_TAG_structure_type
21662 || die->tag == DW_TAG_union_type))
21663 {
21664 const char *name = guess_full_die_structure_name (die, cu);
21665 if (name != NULL)
21666 return name;
21667 }
21668 return "";
21669 case DW_TAG_subprogram:
21670 /* Nested subroutines in Fortran get a prefix with the name
21671 of the parent's subroutine. */
21672 if (cu->language == language_fortran)
21673 {
21674 if ((die->tag == DW_TAG_subprogram)
21675 && (dwarf2_name (parent, cu) != NULL))
21676 return dwarf2_name (parent, cu);
21677 }
21678 return determine_prefix (parent, cu);
21679 case DW_TAG_enumeration_type:
21680 parent_type = read_type_die (parent, cu);
21681 if (TYPE_DECLARED_CLASS (parent_type))
21682 {
21683 if (TYPE_NAME (parent_type) != NULL)
21684 return TYPE_NAME (parent_type);
21685 return "";
21686 }
21687 /* Fall through. */
21688 default:
21689 return determine_prefix (parent, cu);
21690 }
21691 }
21692
21693 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21694 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21695 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21696 an obconcat, otherwise allocate storage for the result. The CU argument is
21697 used to determine the language and hence, the appropriate separator. */
21698
21699 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21700
21701 static char *
21702 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21703 int physname, struct dwarf2_cu *cu)
21704 {
21705 const char *lead = "";
21706 const char *sep;
21707
21708 if (suffix == NULL || suffix[0] == '\0'
21709 || prefix == NULL || prefix[0] == '\0')
21710 sep = "";
21711 else if (cu->language == language_d)
21712 {
21713 /* For D, the 'main' function could be defined in any module, but it
21714 should never be prefixed. */
21715 if (strcmp (suffix, "D main") == 0)
21716 {
21717 prefix = "";
21718 sep = "";
21719 }
21720 else
21721 sep = ".";
21722 }
21723 else if (cu->language == language_fortran && physname)
21724 {
21725 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21726 DW_AT_MIPS_linkage_name is preferred and used instead. */
21727
21728 lead = "__";
21729 sep = "_MOD_";
21730 }
21731 else
21732 sep = "::";
21733
21734 if (prefix == NULL)
21735 prefix = "";
21736 if (suffix == NULL)
21737 suffix = "";
21738
21739 if (obs == NULL)
21740 {
21741 char *retval
21742 = ((char *)
21743 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21744
21745 strcpy (retval, lead);
21746 strcat (retval, prefix);
21747 strcat (retval, sep);
21748 strcat (retval, suffix);
21749 return retval;
21750 }
21751 else
21752 {
21753 /* We have an obstack. */
21754 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21755 }
21756 }
21757
21758 /* Get name of a die, return NULL if not found. */
21759
21760 static const char *
21761 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21762 struct objfile *objfile)
21763 {
21764 if (name && cu->language == language_cplus)
21765 {
21766 gdb::unique_xmalloc_ptr<char> canon_name
21767 = cp_canonicalize_string (name);
21768
21769 if (canon_name != nullptr)
21770 name = objfile->intern (canon_name.get ());
21771 }
21772
21773 return name;
21774 }
21775
21776 /* Get name of a die, return NULL if not found.
21777 Anonymous namespaces are converted to their magic string. */
21778
21779 static const char *
21780 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21781 {
21782 struct attribute *attr;
21783 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21784
21785 attr = dwarf2_attr (die, DW_AT_name, cu);
21786 if ((!attr || !DW_STRING (attr))
21787 && die->tag != DW_TAG_namespace
21788 && die->tag != DW_TAG_class_type
21789 && die->tag != DW_TAG_interface_type
21790 && die->tag != DW_TAG_structure_type
21791 && die->tag != DW_TAG_union_type)
21792 return NULL;
21793
21794 switch (die->tag)
21795 {
21796 case DW_TAG_compile_unit:
21797 case DW_TAG_partial_unit:
21798 /* Compilation units have a DW_AT_name that is a filename, not
21799 a source language identifier. */
21800 case DW_TAG_enumeration_type:
21801 case DW_TAG_enumerator:
21802 /* These tags always have simple identifiers already; no need
21803 to canonicalize them. */
21804 return DW_STRING (attr);
21805
21806 case DW_TAG_namespace:
21807 if (attr != NULL && DW_STRING (attr) != NULL)
21808 return DW_STRING (attr);
21809 return CP_ANONYMOUS_NAMESPACE_STR;
21810
21811 case DW_TAG_class_type:
21812 case DW_TAG_interface_type:
21813 case DW_TAG_structure_type:
21814 case DW_TAG_union_type:
21815 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21816 structures or unions. These were of the form "._%d" in GCC 4.1,
21817 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21818 and GCC 4.4. We work around this problem by ignoring these. */
21819 if (attr && DW_STRING (attr)
21820 && (startswith (DW_STRING (attr), "._")
21821 || startswith (DW_STRING (attr), "<anonymous")))
21822 return NULL;
21823
21824 /* GCC might emit a nameless typedef that has a linkage name. See
21825 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21826 if (!attr || DW_STRING (attr) == NULL)
21827 {
21828 attr = dw2_linkage_name_attr (die, cu);
21829 if (attr == NULL || DW_STRING (attr) == NULL)
21830 return NULL;
21831
21832 /* Avoid demangling DW_STRING (attr) the second time on a second
21833 call for the same DIE. */
21834 if (!DW_STRING_IS_CANONICAL (attr))
21835 {
21836 gdb::unique_xmalloc_ptr<char> demangled
21837 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21838 if (demangled == nullptr)
21839 return nullptr;
21840
21841 DW_STRING (attr) = objfile->intern (demangled.get ());
21842 DW_STRING_IS_CANONICAL (attr) = 1;
21843 }
21844
21845 /* Strip any leading namespaces/classes, keep only the base name.
21846 DW_AT_name for named DIEs does not contain the prefixes. */
21847 const char *base = strrchr (DW_STRING (attr), ':');
21848 if (base && base > DW_STRING (attr) && base[-1] == ':')
21849 return &base[1];
21850 else
21851 return DW_STRING (attr);
21852 }
21853 break;
21854
21855 default:
21856 break;
21857 }
21858
21859 if (!DW_STRING_IS_CANONICAL (attr))
21860 {
21861 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21862 objfile);
21863 DW_STRING_IS_CANONICAL (attr) = 1;
21864 }
21865 return DW_STRING (attr);
21866 }
21867
21868 /* Return the die that this die in an extension of, or NULL if there
21869 is none. *EXT_CU is the CU containing DIE on input, and the CU
21870 containing the return value on output. */
21871
21872 static struct die_info *
21873 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21874 {
21875 struct attribute *attr;
21876
21877 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21878 if (attr == NULL)
21879 return NULL;
21880
21881 return follow_die_ref (die, attr, ext_cu);
21882 }
21883
21884 static void
21885 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21886 {
21887 unsigned int i;
21888
21889 print_spaces (indent, f);
21890 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21891 dwarf_tag_name (die->tag), die->abbrev,
21892 sect_offset_str (die->sect_off));
21893
21894 if (die->parent != NULL)
21895 {
21896 print_spaces (indent, f);
21897 fprintf_unfiltered (f, " parent at offset: %s\n",
21898 sect_offset_str (die->parent->sect_off));
21899 }
21900
21901 print_spaces (indent, f);
21902 fprintf_unfiltered (f, " has children: %s\n",
21903 dwarf_bool_name (die->child != NULL));
21904
21905 print_spaces (indent, f);
21906 fprintf_unfiltered (f, " attributes:\n");
21907
21908 for (i = 0; i < die->num_attrs; ++i)
21909 {
21910 print_spaces (indent, f);
21911 fprintf_unfiltered (f, " %s (%s) ",
21912 dwarf_attr_name (die->attrs[i].name),
21913 dwarf_form_name (die->attrs[i].form));
21914
21915 switch (die->attrs[i].form)
21916 {
21917 case DW_FORM_addr:
21918 case DW_FORM_addrx:
21919 case DW_FORM_GNU_addr_index:
21920 fprintf_unfiltered (f, "address: ");
21921 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21922 break;
21923 case DW_FORM_block2:
21924 case DW_FORM_block4:
21925 case DW_FORM_block:
21926 case DW_FORM_block1:
21927 fprintf_unfiltered (f, "block: size %s",
21928 pulongest (DW_BLOCK (&die->attrs[i])->size));
21929 break;
21930 case DW_FORM_exprloc:
21931 fprintf_unfiltered (f, "expression: size %s",
21932 pulongest (DW_BLOCK (&die->attrs[i])->size));
21933 break;
21934 case DW_FORM_data16:
21935 fprintf_unfiltered (f, "constant of 16 bytes");
21936 break;
21937 case DW_FORM_ref_addr:
21938 fprintf_unfiltered (f, "ref address: ");
21939 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21940 break;
21941 case DW_FORM_GNU_ref_alt:
21942 fprintf_unfiltered (f, "alt ref address: ");
21943 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21944 break;
21945 case DW_FORM_ref1:
21946 case DW_FORM_ref2:
21947 case DW_FORM_ref4:
21948 case DW_FORM_ref8:
21949 case DW_FORM_ref_udata:
21950 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21951 (long) (DW_UNSND (&die->attrs[i])));
21952 break;
21953 case DW_FORM_data1:
21954 case DW_FORM_data2:
21955 case DW_FORM_data4:
21956 case DW_FORM_data8:
21957 case DW_FORM_udata:
21958 case DW_FORM_sdata:
21959 fprintf_unfiltered (f, "constant: %s",
21960 pulongest (DW_UNSND (&die->attrs[i])));
21961 break;
21962 case DW_FORM_sec_offset:
21963 fprintf_unfiltered (f, "section offset: %s",
21964 pulongest (DW_UNSND (&die->attrs[i])));
21965 break;
21966 case DW_FORM_ref_sig8:
21967 fprintf_unfiltered (f, "signature: %s",
21968 hex_string (DW_SIGNATURE (&die->attrs[i])));
21969 break;
21970 case DW_FORM_string:
21971 case DW_FORM_strp:
21972 case DW_FORM_line_strp:
21973 case DW_FORM_strx:
21974 case DW_FORM_GNU_str_index:
21975 case DW_FORM_GNU_strp_alt:
21976 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21977 DW_STRING (&die->attrs[i])
21978 ? DW_STRING (&die->attrs[i]) : "",
21979 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21980 break;
21981 case DW_FORM_flag:
21982 if (DW_UNSND (&die->attrs[i]))
21983 fprintf_unfiltered (f, "flag: TRUE");
21984 else
21985 fprintf_unfiltered (f, "flag: FALSE");
21986 break;
21987 case DW_FORM_flag_present:
21988 fprintf_unfiltered (f, "flag: TRUE");
21989 break;
21990 case DW_FORM_indirect:
21991 /* The reader will have reduced the indirect form to
21992 the "base form" so this form should not occur. */
21993 fprintf_unfiltered (f,
21994 "unexpected attribute form: DW_FORM_indirect");
21995 break;
21996 case DW_FORM_implicit_const:
21997 fprintf_unfiltered (f, "constant: %s",
21998 plongest (DW_SND (&die->attrs[i])));
21999 break;
22000 default:
22001 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22002 die->attrs[i].form);
22003 break;
22004 }
22005 fprintf_unfiltered (f, "\n");
22006 }
22007 }
22008
22009 static void
22010 dump_die_for_error (struct die_info *die)
22011 {
22012 dump_die_shallow (gdb_stderr, 0, die);
22013 }
22014
22015 static void
22016 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22017 {
22018 int indent = level * 4;
22019
22020 gdb_assert (die != NULL);
22021
22022 if (level >= max_level)
22023 return;
22024
22025 dump_die_shallow (f, indent, die);
22026
22027 if (die->child != NULL)
22028 {
22029 print_spaces (indent, f);
22030 fprintf_unfiltered (f, " Children:");
22031 if (level + 1 < max_level)
22032 {
22033 fprintf_unfiltered (f, "\n");
22034 dump_die_1 (f, level + 1, max_level, die->child);
22035 }
22036 else
22037 {
22038 fprintf_unfiltered (f,
22039 " [not printed, max nesting level reached]\n");
22040 }
22041 }
22042
22043 if (die->sibling != NULL && level > 0)
22044 {
22045 dump_die_1 (f, level, max_level, die->sibling);
22046 }
22047 }
22048
22049 /* This is called from the pdie macro in gdbinit.in.
22050 It's not static so gcc will keep a copy callable from gdb. */
22051
22052 void
22053 dump_die (struct die_info *die, int max_level)
22054 {
22055 dump_die_1 (gdb_stdlog, 0, max_level, die);
22056 }
22057
22058 static void
22059 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22060 {
22061 void **slot;
22062
22063 slot = htab_find_slot_with_hash (cu->die_hash, die,
22064 to_underlying (die->sect_off),
22065 INSERT);
22066
22067 *slot = die;
22068 }
22069
22070 /* Follow reference or signature attribute ATTR of SRC_DIE.
22071 On entry *REF_CU is the CU of SRC_DIE.
22072 On exit *REF_CU is the CU of the result. */
22073
22074 static struct die_info *
22075 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22076 struct dwarf2_cu **ref_cu)
22077 {
22078 struct die_info *die;
22079
22080 if (attr->form_is_ref ())
22081 die = follow_die_ref (src_die, attr, ref_cu);
22082 else if (attr->form == DW_FORM_ref_sig8)
22083 die = follow_die_sig (src_die, attr, ref_cu);
22084 else
22085 {
22086 dump_die_for_error (src_die);
22087 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22088 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22089 }
22090
22091 return die;
22092 }
22093
22094 /* Follow reference OFFSET.
22095 On entry *REF_CU is the CU of the source die referencing OFFSET.
22096 On exit *REF_CU is the CU of the result.
22097 Returns NULL if OFFSET is invalid. */
22098
22099 static struct die_info *
22100 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22101 struct dwarf2_cu **ref_cu)
22102 {
22103 struct die_info temp_die;
22104 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22105 struct dwarf2_per_objfile *dwarf2_per_objfile
22106 = cu->per_cu->dwarf2_per_objfile;
22107
22108 gdb_assert (cu->per_cu != NULL);
22109
22110 target_cu = cu;
22111
22112 if (cu->per_cu->is_debug_types)
22113 {
22114 /* .debug_types CUs cannot reference anything outside their CU.
22115 If they need to, they have to reference a signatured type via
22116 DW_FORM_ref_sig8. */
22117 if (!cu->header.offset_in_cu_p (sect_off))
22118 return NULL;
22119 }
22120 else if (offset_in_dwz != cu->per_cu->is_dwz
22121 || !cu->header.offset_in_cu_p (sect_off))
22122 {
22123 struct dwarf2_per_cu_data *per_cu;
22124
22125 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22126 dwarf2_per_objfile);
22127
22128 /* If necessary, add it to the queue and load its DIEs. */
22129 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22130 load_full_comp_unit (per_cu, false, cu->language);
22131
22132 target_cu = per_cu->cu;
22133 }
22134 else if (cu->dies == NULL)
22135 {
22136 /* We're loading full DIEs during partial symbol reading. */
22137 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22138 load_full_comp_unit (cu->per_cu, false, language_minimal);
22139 }
22140
22141 *ref_cu = target_cu;
22142 temp_die.sect_off = sect_off;
22143
22144 if (target_cu != cu)
22145 target_cu->ancestor = cu;
22146
22147 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22148 &temp_die,
22149 to_underlying (sect_off));
22150 }
22151
22152 /* Follow reference attribute ATTR of SRC_DIE.
22153 On entry *REF_CU is the CU of SRC_DIE.
22154 On exit *REF_CU is the CU of the result. */
22155
22156 static struct die_info *
22157 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22158 struct dwarf2_cu **ref_cu)
22159 {
22160 sect_offset sect_off = attr->get_ref_die_offset ();
22161 struct dwarf2_cu *cu = *ref_cu;
22162 struct die_info *die;
22163
22164 die = follow_die_offset (sect_off,
22165 (attr->form == DW_FORM_GNU_ref_alt
22166 || cu->per_cu->is_dwz),
22167 ref_cu);
22168 if (!die)
22169 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22170 "at %s [in module %s]"),
22171 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22172 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22173
22174 return die;
22175 }
22176
22177 /* See read.h. */
22178
22179 struct dwarf2_locexpr_baton
22180 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22181 dwarf2_per_cu_data *per_cu,
22182 CORE_ADDR (*get_frame_pc) (void *baton),
22183 void *baton, bool resolve_abstract_p)
22184 {
22185 struct dwarf2_cu *cu;
22186 struct die_info *die;
22187 struct attribute *attr;
22188 struct dwarf2_locexpr_baton retval;
22189 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22190 struct objfile *objfile = dwarf2_per_objfile->objfile;
22191
22192 if (per_cu->cu == NULL)
22193 load_cu (per_cu, false);
22194 cu = per_cu->cu;
22195 if (cu == NULL)
22196 {
22197 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22198 Instead just throw an error, not much else we can do. */
22199 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22200 sect_offset_str (sect_off), objfile_name (objfile));
22201 }
22202
22203 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22204 if (!die)
22205 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22206 sect_offset_str (sect_off), objfile_name (objfile));
22207
22208 attr = dwarf2_attr (die, DW_AT_location, cu);
22209 if (!attr && resolve_abstract_p
22210 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22211 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22212 {
22213 CORE_ADDR pc = (*get_frame_pc) (baton);
22214 CORE_ADDR baseaddr = objfile->text_section_offset ();
22215 struct gdbarch *gdbarch = objfile->arch ();
22216
22217 for (const auto &cand_off
22218 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22219 {
22220 struct dwarf2_cu *cand_cu = cu;
22221 struct die_info *cand
22222 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22223 if (!cand
22224 || !cand->parent
22225 || cand->parent->tag != DW_TAG_subprogram)
22226 continue;
22227
22228 CORE_ADDR pc_low, pc_high;
22229 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22230 if (pc_low == ((CORE_ADDR) -1))
22231 continue;
22232 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22233 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22234 if (!(pc_low <= pc && pc < pc_high))
22235 continue;
22236
22237 die = cand;
22238 attr = dwarf2_attr (die, DW_AT_location, cu);
22239 break;
22240 }
22241 }
22242
22243 if (!attr)
22244 {
22245 /* DWARF: "If there is no such attribute, then there is no effect.".
22246 DATA is ignored if SIZE is 0. */
22247
22248 retval.data = NULL;
22249 retval.size = 0;
22250 }
22251 else if (attr->form_is_section_offset ())
22252 {
22253 struct dwarf2_loclist_baton loclist_baton;
22254 CORE_ADDR pc = (*get_frame_pc) (baton);
22255 size_t size;
22256
22257 fill_in_loclist_baton (cu, &loclist_baton, attr);
22258
22259 retval.data = dwarf2_find_location_expression (&loclist_baton,
22260 &size, pc);
22261 retval.size = size;
22262 }
22263 else
22264 {
22265 if (!attr->form_is_block ())
22266 error (_("Dwarf Error: DIE at %s referenced in module %s "
22267 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22268 sect_offset_str (sect_off), objfile_name (objfile));
22269
22270 retval.data = DW_BLOCK (attr)->data;
22271 retval.size = DW_BLOCK (attr)->size;
22272 }
22273 retval.per_cu = cu->per_cu;
22274
22275 age_cached_comp_units (dwarf2_per_objfile);
22276
22277 return retval;
22278 }
22279
22280 /* See read.h. */
22281
22282 struct dwarf2_locexpr_baton
22283 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22284 dwarf2_per_cu_data *per_cu,
22285 CORE_ADDR (*get_frame_pc) (void *baton),
22286 void *baton)
22287 {
22288 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22289
22290 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22291 }
22292
22293 /* Write a constant of a given type as target-ordered bytes into
22294 OBSTACK. */
22295
22296 static const gdb_byte *
22297 write_constant_as_bytes (struct obstack *obstack,
22298 enum bfd_endian byte_order,
22299 struct type *type,
22300 ULONGEST value,
22301 LONGEST *len)
22302 {
22303 gdb_byte *result;
22304
22305 *len = TYPE_LENGTH (type);
22306 result = (gdb_byte *) obstack_alloc (obstack, *len);
22307 store_unsigned_integer (result, *len, byte_order, value);
22308
22309 return result;
22310 }
22311
22312 /* See read.h. */
22313
22314 const gdb_byte *
22315 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22316 dwarf2_per_cu_data *per_cu,
22317 obstack *obstack,
22318 LONGEST *len)
22319 {
22320 struct dwarf2_cu *cu;
22321 struct die_info *die;
22322 struct attribute *attr;
22323 const gdb_byte *result = NULL;
22324 struct type *type;
22325 LONGEST value;
22326 enum bfd_endian byte_order;
22327 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22328
22329 if (per_cu->cu == NULL)
22330 load_cu (per_cu, false);
22331 cu = per_cu->cu;
22332 if (cu == NULL)
22333 {
22334 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22335 Instead just throw an error, not much else we can do. */
22336 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22337 sect_offset_str (sect_off), objfile_name (objfile));
22338 }
22339
22340 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22341 if (!die)
22342 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22343 sect_offset_str (sect_off), objfile_name (objfile));
22344
22345 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22346 if (attr == NULL)
22347 return NULL;
22348
22349 byte_order = (bfd_big_endian (objfile->obfd)
22350 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22351
22352 switch (attr->form)
22353 {
22354 case DW_FORM_addr:
22355 case DW_FORM_addrx:
22356 case DW_FORM_GNU_addr_index:
22357 {
22358 gdb_byte *tem;
22359
22360 *len = cu->header.addr_size;
22361 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22362 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22363 result = tem;
22364 }
22365 break;
22366 case DW_FORM_string:
22367 case DW_FORM_strp:
22368 case DW_FORM_strx:
22369 case DW_FORM_GNU_str_index:
22370 case DW_FORM_GNU_strp_alt:
22371 /* DW_STRING is already allocated on the objfile obstack, point
22372 directly to it. */
22373 result = (const gdb_byte *) DW_STRING (attr);
22374 *len = strlen (DW_STRING (attr));
22375 break;
22376 case DW_FORM_block1:
22377 case DW_FORM_block2:
22378 case DW_FORM_block4:
22379 case DW_FORM_block:
22380 case DW_FORM_exprloc:
22381 case DW_FORM_data16:
22382 result = DW_BLOCK (attr)->data;
22383 *len = DW_BLOCK (attr)->size;
22384 break;
22385
22386 /* The DW_AT_const_value attributes are supposed to carry the
22387 symbol's value "represented as it would be on the target
22388 architecture." By the time we get here, it's already been
22389 converted to host endianness, so we just need to sign- or
22390 zero-extend it as appropriate. */
22391 case DW_FORM_data1:
22392 type = die_type (die, cu);
22393 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22394 if (result == NULL)
22395 result = write_constant_as_bytes (obstack, byte_order,
22396 type, value, len);
22397 break;
22398 case DW_FORM_data2:
22399 type = die_type (die, cu);
22400 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22401 if (result == NULL)
22402 result = write_constant_as_bytes (obstack, byte_order,
22403 type, value, len);
22404 break;
22405 case DW_FORM_data4:
22406 type = die_type (die, cu);
22407 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22408 if (result == NULL)
22409 result = write_constant_as_bytes (obstack, byte_order,
22410 type, value, len);
22411 break;
22412 case DW_FORM_data8:
22413 type = die_type (die, cu);
22414 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22415 if (result == NULL)
22416 result = write_constant_as_bytes (obstack, byte_order,
22417 type, value, len);
22418 break;
22419
22420 case DW_FORM_sdata:
22421 case DW_FORM_implicit_const:
22422 type = die_type (die, cu);
22423 result = write_constant_as_bytes (obstack, byte_order,
22424 type, DW_SND (attr), len);
22425 break;
22426
22427 case DW_FORM_udata:
22428 type = die_type (die, cu);
22429 result = write_constant_as_bytes (obstack, byte_order,
22430 type, DW_UNSND (attr), len);
22431 break;
22432
22433 default:
22434 complaint (_("unsupported const value attribute form: '%s'"),
22435 dwarf_form_name (attr->form));
22436 break;
22437 }
22438
22439 return result;
22440 }
22441
22442 /* See read.h. */
22443
22444 struct type *
22445 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22446 dwarf2_per_cu_data *per_cu)
22447 {
22448 struct dwarf2_cu *cu;
22449 struct die_info *die;
22450
22451 if (per_cu->cu == NULL)
22452 load_cu (per_cu, false);
22453 cu = per_cu->cu;
22454 if (!cu)
22455 return NULL;
22456
22457 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22458 if (!die)
22459 return NULL;
22460
22461 return die_type (die, cu);
22462 }
22463
22464 /* See read.h. */
22465
22466 struct type *
22467 dwarf2_get_die_type (cu_offset die_offset,
22468 struct dwarf2_per_cu_data *per_cu)
22469 {
22470 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22471 return get_die_type_at_offset (die_offset_sect, per_cu);
22472 }
22473
22474 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22475 On entry *REF_CU is the CU of SRC_DIE.
22476 On exit *REF_CU is the CU of the result.
22477 Returns NULL if the referenced DIE isn't found. */
22478
22479 static struct die_info *
22480 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22481 struct dwarf2_cu **ref_cu)
22482 {
22483 struct die_info temp_die;
22484 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22485 struct die_info *die;
22486
22487 /* While it might be nice to assert sig_type->type == NULL here,
22488 we can get here for DW_AT_imported_declaration where we need
22489 the DIE not the type. */
22490
22491 /* If necessary, add it to the queue and load its DIEs. */
22492
22493 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22494 read_signatured_type (sig_type);
22495
22496 sig_cu = sig_type->per_cu.cu;
22497 gdb_assert (sig_cu != NULL);
22498 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22499 temp_die.sect_off = sig_type->type_offset_in_section;
22500 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22501 to_underlying (temp_die.sect_off));
22502 if (die)
22503 {
22504 struct dwarf2_per_objfile *dwarf2_per_objfile
22505 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22506
22507 /* For .gdb_index version 7 keep track of included TUs.
22508 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22509 if (dwarf2_per_objfile->index_table != NULL
22510 && dwarf2_per_objfile->index_table->version <= 7)
22511 {
22512 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22513 }
22514
22515 *ref_cu = sig_cu;
22516 if (sig_cu != cu)
22517 sig_cu->ancestor = cu;
22518
22519 return die;
22520 }
22521
22522 return NULL;
22523 }
22524
22525 /* Follow signatured type referenced by ATTR in SRC_DIE.
22526 On entry *REF_CU is the CU of SRC_DIE.
22527 On exit *REF_CU is the CU of the result.
22528 The result is the DIE of the type.
22529 If the referenced type cannot be found an error is thrown. */
22530
22531 static struct die_info *
22532 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22533 struct dwarf2_cu **ref_cu)
22534 {
22535 ULONGEST signature = DW_SIGNATURE (attr);
22536 struct signatured_type *sig_type;
22537 struct die_info *die;
22538
22539 gdb_assert (attr->form == DW_FORM_ref_sig8);
22540
22541 sig_type = lookup_signatured_type (*ref_cu, signature);
22542 /* sig_type will be NULL if the signatured type is missing from
22543 the debug info. */
22544 if (sig_type == NULL)
22545 {
22546 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22547 " from DIE at %s [in module %s]"),
22548 hex_string (signature), sect_offset_str (src_die->sect_off),
22549 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22550 }
22551
22552 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22553 if (die == NULL)
22554 {
22555 dump_die_for_error (src_die);
22556 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22557 " from DIE at %s [in module %s]"),
22558 hex_string (signature), sect_offset_str (src_die->sect_off),
22559 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22560 }
22561
22562 return die;
22563 }
22564
22565 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22566 reading in and processing the type unit if necessary. */
22567
22568 static struct type *
22569 get_signatured_type (struct die_info *die, ULONGEST signature,
22570 struct dwarf2_cu *cu)
22571 {
22572 struct dwarf2_per_objfile *dwarf2_per_objfile
22573 = cu->per_cu->dwarf2_per_objfile;
22574 struct signatured_type *sig_type;
22575 struct dwarf2_cu *type_cu;
22576 struct die_info *type_die;
22577 struct type *type;
22578
22579 sig_type = lookup_signatured_type (cu, signature);
22580 /* sig_type will be NULL if the signatured type is missing from
22581 the debug info. */
22582 if (sig_type == NULL)
22583 {
22584 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22585 " from DIE at %s [in module %s]"),
22586 hex_string (signature), sect_offset_str (die->sect_off),
22587 objfile_name (dwarf2_per_objfile->objfile));
22588 return build_error_marker_type (cu, die);
22589 }
22590
22591 /* If we already know the type we're done. */
22592 if (sig_type->type != NULL)
22593 return sig_type->type;
22594
22595 type_cu = cu;
22596 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22597 if (type_die != NULL)
22598 {
22599 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22600 is created. This is important, for example, because for c++ classes
22601 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22602 type = read_type_die (type_die, type_cu);
22603 if (type == NULL)
22604 {
22605 complaint (_("Dwarf Error: Cannot build signatured type %s"
22606 " referenced from DIE at %s [in module %s]"),
22607 hex_string (signature), sect_offset_str (die->sect_off),
22608 objfile_name (dwarf2_per_objfile->objfile));
22609 type = build_error_marker_type (cu, die);
22610 }
22611 }
22612 else
22613 {
22614 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22615 " from DIE at %s [in module %s]"),
22616 hex_string (signature), sect_offset_str (die->sect_off),
22617 objfile_name (dwarf2_per_objfile->objfile));
22618 type = build_error_marker_type (cu, die);
22619 }
22620 sig_type->type = type;
22621
22622 return type;
22623 }
22624
22625 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22626 reading in and processing the type unit if necessary. */
22627
22628 static struct type *
22629 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22630 struct dwarf2_cu *cu) /* ARI: editCase function */
22631 {
22632 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22633 if (attr->form_is_ref ())
22634 {
22635 struct dwarf2_cu *type_cu = cu;
22636 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22637
22638 return read_type_die (type_die, type_cu);
22639 }
22640 else if (attr->form == DW_FORM_ref_sig8)
22641 {
22642 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22643 }
22644 else
22645 {
22646 struct dwarf2_per_objfile *dwarf2_per_objfile
22647 = cu->per_cu->dwarf2_per_objfile;
22648
22649 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22650 " at %s [in module %s]"),
22651 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22652 objfile_name (dwarf2_per_objfile->objfile));
22653 return build_error_marker_type (cu, die);
22654 }
22655 }
22656
22657 /* Load the DIEs associated with type unit PER_CU into memory. */
22658
22659 static void
22660 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22661 {
22662 struct signatured_type *sig_type;
22663
22664 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22665 gdb_assert (! per_cu->type_unit_group_p ());
22666
22667 /* We have the per_cu, but we need the signatured_type.
22668 Fortunately this is an easy translation. */
22669 gdb_assert (per_cu->is_debug_types);
22670 sig_type = (struct signatured_type *) per_cu;
22671
22672 gdb_assert (per_cu->cu == NULL);
22673
22674 read_signatured_type (sig_type);
22675
22676 gdb_assert (per_cu->cu != NULL);
22677 }
22678
22679 /* Read in a signatured type and build its CU and DIEs.
22680 If the type is a stub for the real type in a DWO file,
22681 read in the real type from the DWO file as well. */
22682
22683 static void
22684 read_signatured_type (struct signatured_type *sig_type)
22685 {
22686 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22687
22688 gdb_assert (per_cu->is_debug_types);
22689 gdb_assert (per_cu->cu == NULL);
22690
22691 cutu_reader reader (per_cu, NULL, 0, false);
22692
22693 if (!reader.dummy_p)
22694 {
22695 struct dwarf2_cu *cu = reader.cu;
22696 const gdb_byte *info_ptr = reader.info_ptr;
22697
22698 gdb_assert (cu->die_hash == NULL);
22699 cu->die_hash =
22700 htab_create_alloc_ex (cu->header.length / 12,
22701 die_hash,
22702 die_eq,
22703 NULL,
22704 &cu->comp_unit_obstack,
22705 hashtab_obstack_allocate,
22706 dummy_obstack_deallocate);
22707
22708 if (reader.comp_unit_die->has_children)
22709 reader.comp_unit_die->child
22710 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22711 reader.comp_unit_die);
22712 cu->dies = reader.comp_unit_die;
22713 /* comp_unit_die is not stored in die_hash, no need. */
22714
22715 /* We try not to read any attributes in this function, because
22716 not all CUs needed for references have been loaded yet, and
22717 symbol table processing isn't initialized. But we have to
22718 set the CU language, or we won't be able to build types
22719 correctly. Similarly, if we do not read the producer, we can
22720 not apply producer-specific interpretation. */
22721 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22722
22723 reader.keep ();
22724 }
22725
22726 sig_type->per_cu.tu_read = 1;
22727 }
22728
22729 /* Decode simple location descriptions.
22730 Given a pointer to a dwarf block that defines a location, compute
22731 the location and return the value. If COMPUTED is non-null, it is
22732 set to true to indicate that decoding was successful, and false
22733 otherwise. If COMPUTED is null, then this function may emit a
22734 complaint. */
22735
22736 static CORE_ADDR
22737 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22738 {
22739 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22740 size_t i;
22741 size_t size = blk->size;
22742 const gdb_byte *data = blk->data;
22743 CORE_ADDR stack[64];
22744 int stacki;
22745 unsigned int bytes_read, unsnd;
22746 gdb_byte op;
22747
22748 if (computed != nullptr)
22749 *computed = false;
22750
22751 i = 0;
22752 stacki = 0;
22753 stack[stacki] = 0;
22754 stack[++stacki] = 0;
22755
22756 while (i < size)
22757 {
22758 op = data[i++];
22759 switch (op)
22760 {
22761 case DW_OP_lit0:
22762 case DW_OP_lit1:
22763 case DW_OP_lit2:
22764 case DW_OP_lit3:
22765 case DW_OP_lit4:
22766 case DW_OP_lit5:
22767 case DW_OP_lit6:
22768 case DW_OP_lit7:
22769 case DW_OP_lit8:
22770 case DW_OP_lit9:
22771 case DW_OP_lit10:
22772 case DW_OP_lit11:
22773 case DW_OP_lit12:
22774 case DW_OP_lit13:
22775 case DW_OP_lit14:
22776 case DW_OP_lit15:
22777 case DW_OP_lit16:
22778 case DW_OP_lit17:
22779 case DW_OP_lit18:
22780 case DW_OP_lit19:
22781 case DW_OP_lit20:
22782 case DW_OP_lit21:
22783 case DW_OP_lit22:
22784 case DW_OP_lit23:
22785 case DW_OP_lit24:
22786 case DW_OP_lit25:
22787 case DW_OP_lit26:
22788 case DW_OP_lit27:
22789 case DW_OP_lit28:
22790 case DW_OP_lit29:
22791 case DW_OP_lit30:
22792 case DW_OP_lit31:
22793 stack[++stacki] = op - DW_OP_lit0;
22794 break;
22795
22796 case DW_OP_reg0:
22797 case DW_OP_reg1:
22798 case DW_OP_reg2:
22799 case DW_OP_reg3:
22800 case DW_OP_reg4:
22801 case DW_OP_reg5:
22802 case DW_OP_reg6:
22803 case DW_OP_reg7:
22804 case DW_OP_reg8:
22805 case DW_OP_reg9:
22806 case DW_OP_reg10:
22807 case DW_OP_reg11:
22808 case DW_OP_reg12:
22809 case DW_OP_reg13:
22810 case DW_OP_reg14:
22811 case DW_OP_reg15:
22812 case DW_OP_reg16:
22813 case DW_OP_reg17:
22814 case DW_OP_reg18:
22815 case DW_OP_reg19:
22816 case DW_OP_reg20:
22817 case DW_OP_reg21:
22818 case DW_OP_reg22:
22819 case DW_OP_reg23:
22820 case DW_OP_reg24:
22821 case DW_OP_reg25:
22822 case DW_OP_reg26:
22823 case DW_OP_reg27:
22824 case DW_OP_reg28:
22825 case DW_OP_reg29:
22826 case DW_OP_reg30:
22827 case DW_OP_reg31:
22828 stack[++stacki] = op - DW_OP_reg0;
22829 if (i < size)
22830 {
22831 if (computed == nullptr)
22832 dwarf2_complex_location_expr_complaint ();
22833 else
22834 return 0;
22835 }
22836 break;
22837
22838 case DW_OP_regx:
22839 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22840 i += bytes_read;
22841 stack[++stacki] = unsnd;
22842 if (i < size)
22843 {
22844 if (computed == nullptr)
22845 dwarf2_complex_location_expr_complaint ();
22846 else
22847 return 0;
22848 }
22849 break;
22850
22851 case DW_OP_addr:
22852 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22853 &bytes_read);
22854 i += bytes_read;
22855 break;
22856
22857 case DW_OP_const1u:
22858 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22859 i += 1;
22860 break;
22861
22862 case DW_OP_const1s:
22863 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22864 i += 1;
22865 break;
22866
22867 case DW_OP_const2u:
22868 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22869 i += 2;
22870 break;
22871
22872 case DW_OP_const2s:
22873 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22874 i += 2;
22875 break;
22876
22877 case DW_OP_const4u:
22878 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22879 i += 4;
22880 break;
22881
22882 case DW_OP_const4s:
22883 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22884 i += 4;
22885 break;
22886
22887 case DW_OP_const8u:
22888 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22889 i += 8;
22890 break;
22891
22892 case DW_OP_constu:
22893 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22894 &bytes_read);
22895 i += bytes_read;
22896 break;
22897
22898 case DW_OP_consts:
22899 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22900 i += bytes_read;
22901 break;
22902
22903 case DW_OP_dup:
22904 stack[stacki + 1] = stack[stacki];
22905 stacki++;
22906 break;
22907
22908 case DW_OP_plus:
22909 stack[stacki - 1] += stack[stacki];
22910 stacki--;
22911 break;
22912
22913 case DW_OP_plus_uconst:
22914 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22915 &bytes_read);
22916 i += bytes_read;
22917 break;
22918
22919 case DW_OP_minus:
22920 stack[stacki - 1] -= stack[stacki];
22921 stacki--;
22922 break;
22923
22924 case DW_OP_deref:
22925 /* If we're not the last op, then we definitely can't encode
22926 this using GDB's address_class enum. This is valid for partial
22927 global symbols, although the variable's address will be bogus
22928 in the psymtab. */
22929 if (i < size)
22930 {
22931 if (computed == nullptr)
22932 dwarf2_complex_location_expr_complaint ();
22933 else
22934 return 0;
22935 }
22936 break;
22937
22938 case DW_OP_GNU_push_tls_address:
22939 case DW_OP_form_tls_address:
22940 /* The top of the stack has the offset from the beginning
22941 of the thread control block at which the variable is located. */
22942 /* Nothing should follow this operator, so the top of stack would
22943 be returned. */
22944 /* This is valid for partial global symbols, but the variable's
22945 address will be bogus in the psymtab. Make it always at least
22946 non-zero to not look as a variable garbage collected by linker
22947 which have DW_OP_addr 0. */
22948 if (i < size)
22949 {
22950 if (computed == nullptr)
22951 dwarf2_complex_location_expr_complaint ();
22952 else
22953 return 0;
22954 }
22955 stack[stacki]++;
22956 break;
22957
22958 case DW_OP_GNU_uninit:
22959 if (computed != nullptr)
22960 return 0;
22961 break;
22962
22963 case DW_OP_addrx:
22964 case DW_OP_GNU_addr_index:
22965 case DW_OP_GNU_const_index:
22966 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22967 &bytes_read);
22968 i += bytes_read;
22969 break;
22970
22971 default:
22972 if (computed == nullptr)
22973 {
22974 const char *name = get_DW_OP_name (op);
22975
22976 if (name)
22977 complaint (_("unsupported stack op: '%s'"),
22978 name);
22979 else
22980 complaint (_("unsupported stack op: '%02x'"),
22981 op);
22982 }
22983
22984 return (stack[stacki]);
22985 }
22986
22987 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22988 outside of the allocated space. Also enforce minimum>0. */
22989 if (stacki >= ARRAY_SIZE (stack) - 1)
22990 {
22991 if (computed == nullptr)
22992 complaint (_("location description stack overflow"));
22993 return 0;
22994 }
22995
22996 if (stacki <= 0)
22997 {
22998 if (computed == nullptr)
22999 complaint (_("location description stack underflow"));
23000 return 0;
23001 }
23002 }
23003
23004 if (computed != nullptr)
23005 *computed = true;
23006 return (stack[stacki]);
23007 }
23008
23009 /* memory allocation interface */
23010
23011 static struct dwarf_block *
23012 dwarf_alloc_block (struct dwarf2_cu *cu)
23013 {
23014 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23015 }
23016
23017 static struct die_info *
23018 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23019 {
23020 struct die_info *die;
23021 size_t size = sizeof (struct die_info);
23022
23023 if (num_attrs > 1)
23024 size += (num_attrs - 1) * sizeof (struct attribute);
23025
23026 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23027 memset (die, 0, sizeof (struct die_info));
23028 return (die);
23029 }
23030
23031 \f
23032
23033 /* Macro support. */
23034
23035 /* An overload of dwarf_decode_macros that finds the correct section
23036 and ensures it is read in before calling the other overload. */
23037
23038 static void
23039 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23040 int section_is_gnu)
23041 {
23042 struct dwarf2_per_objfile *dwarf2_per_objfile
23043 = cu->per_cu->dwarf2_per_objfile;
23044 struct objfile *objfile = dwarf2_per_objfile->objfile;
23045 const struct line_header *lh = cu->line_header;
23046 unsigned int offset_size = cu->header.offset_size;
23047 struct dwarf2_section_info *section;
23048 const char *section_name;
23049
23050 if (cu->dwo_unit != nullptr)
23051 {
23052 if (section_is_gnu)
23053 {
23054 section = &cu->dwo_unit->dwo_file->sections.macro;
23055 section_name = ".debug_macro.dwo";
23056 }
23057 else
23058 {
23059 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23060 section_name = ".debug_macinfo.dwo";
23061 }
23062 }
23063 else
23064 {
23065 if (section_is_gnu)
23066 {
23067 section = &dwarf2_per_objfile->macro;
23068 section_name = ".debug_macro";
23069 }
23070 else
23071 {
23072 section = &dwarf2_per_objfile->macinfo;
23073 section_name = ".debug_macinfo";
23074 }
23075 }
23076
23077 section->read (objfile);
23078 if (section->buffer == nullptr)
23079 {
23080 complaint (_("missing %s section"), section_name);
23081 return;
23082 }
23083
23084 buildsym_compunit *builder = cu->get_builder ();
23085
23086 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23087 offset_size, offset, section_is_gnu);
23088 }
23089
23090 /* Return the .debug_loc section to use for CU.
23091 For DWO files use .debug_loc.dwo. */
23092
23093 static struct dwarf2_section_info *
23094 cu_debug_loc_section (struct dwarf2_cu *cu)
23095 {
23096 struct dwarf2_per_objfile *dwarf2_per_objfile
23097 = cu->per_cu->dwarf2_per_objfile;
23098
23099 if (cu->dwo_unit)
23100 {
23101 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23102
23103 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23104 }
23105 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23106 : &dwarf2_per_objfile->loc);
23107 }
23108
23109 /* A helper function that fills in a dwarf2_loclist_baton. */
23110
23111 static void
23112 fill_in_loclist_baton (struct dwarf2_cu *cu,
23113 struct dwarf2_loclist_baton *baton,
23114 const struct attribute *attr)
23115 {
23116 struct dwarf2_per_objfile *dwarf2_per_objfile
23117 = cu->per_cu->dwarf2_per_objfile;
23118 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23119
23120 section->read (dwarf2_per_objfile->objfile);
23121
23122 baton->per_cu = cu->per_cu;
23123 gdb_assert (baton->per_cu);
23124 /* We don't know how long the location list is, but make sure we
23125 don't run off the edge of the section. */
23126 baton->size = section->size - DW_UNSND (attr);
23127 baton->data = section->buffer + DW_UNSND (attr);
23128 if (cu->base_address.has_value ())
23129 baton->base_address = *cu->base_address;
23130 else
23131 baton->base_address = 0;
23132 baton->from_dwo = cu->dwo_unit != NULL;
23133 }
23134
23135 static void
23136 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23137 struct dwarf2_cu *cu, int is_block)
23138 {
23139 struct dwarf2_per_objfile *dwarf2_per_objfile
23140 = cu->per_cu->dwarf2_per_objfile;
23141 struct objfile *objfile = dwarf2_per_objfile->objfile;
23142 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23143
23144 if (attr->form_is_section_offset ()
23145 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23146 the section. If so, fall through to the complaint in the
23147 other branch. */
23148 && DW_UNSND (attr) < section->get_size (objfile))
23149 {
23150 struct dwarf2_loclist_baton *baton;
23151
23152 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23153
23154 fill_in_loclist_baton (cu, baton, attr);
23155
23156 if (!cu->base_address.has_value ())
23157 complaint (_("Location list used without "
23158 "specifying the CU base address."));
23159
23160 SYMBOL_ACLASS_INDEX (sym) = (is_block
23161 ? dwarf2_loclist_block_index
23162 : dwarf2_loclist_index);
23163 SYMBOL_LOCATION_BATON (sym) = baton;
23164 }
23165 else
23166 {
23167 struct dwarf2_locexpr_baton *baton;
23168
23169 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23170 baton->per_cu = cu->per_cu;
23171 gdb_assert (baton->per_cu);
23172
23173 if (attr->form_is_block ())
23174 {
23175 /* Note that we're just copying the block's data pointer
23176 here, not the actual data. We're still pointing into the
23177 info_buffer for SYM's objfile; right now we never release
23178 that buffer, but when we do clean up properly this may
23179 need to change. */
23180 baton->size = DW_BLOCK (attr)->size;
23181 baton->data = DW_BLOCK (attr)->data;
23182 }
23183 else
23184 {
23185 dwarf2_invalid_attrib_class_complaint ("location description",
23186 sym->natural_name ());
23187 baton->size = 0;
23188 }
23189
23190 SYMBOL_ACLASS_INDEX (sym) = (is_block
23191 ? dwarf2_locexpr_block_index
23192 : dwarf2_locexpr_index);
23193 SYMBOL_LOCATION_BATON (sym) = baton;
23194 }
23195 }
23196
23197 /* See read.h. */
23198
23199 struct objfile *
23200 dwarf2_per_cu_data::objfile () const
23201 {
23202 struct objfile *objfile = dwarf2_per_objfile->objfile;
23203
23204 /* Return the master objfile, so that we can report and look up the
23205 correct file containing this variable. */
23206 if (objfile->separate_debug_objfile_backlink)
23207 objfile = objfile->separate_debug_objfile_backlink;
23208
23209 return objfile;
23210 }
23211
23212 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23213 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23214 CU_HEADERP first. */
23215
23216 static const struct comp_unit_head *
23217 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23218 const struct dwarf2_per_cu_data *per_cu)
23219 {
23220 const gdb_byte *info_ptr;
23221
23222 if (per_cu->cu)
23223 return &per_cu->cu->header;
23224
23225 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23226
23227 memset (cu_headerp, 0, sizeof (*cu_headerp));
23228 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23229 rcuh_kind::COMPILE);
23230
23231 return cu_headerp;
23232 }
23233
23234 /* See read.h. */
23235
23236 int
23237 dwarf2_per_cu_data::addr_size () const
23238 {
23239 struct comp_unit_head cu_header_local;
23240 const struct comp_unit_head *cu_headerp;
23241
23242 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23243
23244 return cu_headerp->addr_size;
23245 }
23246
23247 /* See read.h. */
23248
23249 int
23250 dwarf2_per_cu_data::offset_size () const
23251 {
23252 struct comp_unit_head cu_header_local;
23253 const struct comp_unit_head *cu_headerp;
23254
23255 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23256
23257 return cu_headerp->offset_size;
23258 }
23259
23260 /* See read.h. */
23261
23262 int
23263 dwarf2_per_cu_data::ref_addr_size () const
23264 {
23265 struct comp_unit_head cu_header_local;
23266 const struct comp_unit_head *cu_headerp;
23267
23268 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23269
23270 if (cu_headerp->version == 2)
23271 return cu_headerp->addr_size;
23272 else
23273 return cu_headerp->offset_size;
23274 }
23275
23276 /* See read.h. */
23277
23278 CORE_ADDR
23279 dwarf2_per_cu_data::text_offset () const
23280 {
23281 struct objfile *objfile = dwarf2_per_objfile->objfile;
23282
23283 return objfile->text_section_offset ();
23284 }
23285
23286 /* See read.h. */
23287
23288 struct type *
23289 dwarf2_per_cu_data::addr_type () const
23290 {
23291 struct objfile *objfile = dwarf2_per_objfile->objfile;
23292 struct type *void_type = objfile_type (objfile)->builtin_void;
23293 struct type *addr_type = lookup_pointer_type (void_type);
23294 int addr_size = this->addr_size ();
23295
23296 if (TYPE_LENGTH (addr_type) == addr_size)
23297 return addr_type;
23298
23299 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23300 return addr_type;
23301 }
23302
23303 /* A helper function for dwarf2_find_containing_comp_unit that returns
23304 the index of the result, and that searches a vector. It will
23305 return a result even if the offset in question does not actually
23306 occur in any CU. This is separate so that it can be unit
23307 tested. */
23308
23309 static int
23310 dwarf2_find_containing_comp_unit
23311 (sect_offset sect_off,
23312 unsigned int offset_in_dwz,
23313 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23314 {
23315 int low, high;
23316
23317 low = 0;
23318 high = all_comp_units.size () - 1;
23319 while (high > low)
23320 {
23321 struct dwarf2_per_cu_data *mid_cu;
23322 int mid = low + (high - low) / 2;
23323
23324 mid_cu = all_comp_units[mid];
23325 if (mid_cu->is_dwz > offset_in_dwz
23326 || (mid_cu->is_dwz == offset_in_dwz
23327 && mid_cu->sect_off + mid_cu->length > sect_off))
23328 high = mid;
23329 else
23330 low = mid + 1;
23331 }
23332 gdb_assert (low == high);
23333 return low;
23334 }
23335
23336 /* Locate the .debug_info compilation unit from CU's objfile which contains
23337 the DIE at OFFSET. Raises an error on failure. */
23338
23339 static struct dwarf2_per_cu_data *
23340 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23341 unsigned int offset_in_dwz,
23342 struct dwarf2_per_objfile *dwarf2_per_objfile)
23343 {
23344 int low
23345 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23346 dwarf2_per_objfile->all_comp_units);
23347 struct dwarf2_per_cu_data *this_cu
23348 = dwarf2_per_objfile->all_comp_units[low];
23349
23350 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23351 {
23352 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23353 error (_("Dwarf Error: could not find partial DIE containing "
23354 "offset %s [in module %s]"),
23355 sect_offset_str (sect_off),
23356 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23357
23358 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23359 <= sect_off);
23360 return dwarf2_per_objfile->all_comp_units[low-1];
23361 }
23362 else
23363 {
23364 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
23365 && sect_off >= this_cu->sect_off + this_cu->length)
23366 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23367 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23368 return this_cu;
23369 }
23370 }
23371
23372 #if GDB_SELF_TEST
23373
23374 namespace selftests {
23375 namespace find_containing_comp_unit {
23376
23377 static void
23378 run_test ()
23379 {
23380 struct dwarf2_per_cu_data one {};
23381 struct dwarf2_per_cu_data two {};
23382 struct dwarf2_per_cu_data three {};
23383 struct dwarf2_per_cu_data four {};
23384
23385 one.length = 5;
23386 two.sect_off = sect_offset (one.length);
23387 two.length = 7;
23388
23389 three.length = 5;
23390 three.is_dwz = 1;
23391 four.sect_off = sect_offset (three.length);
23392 four.length = 7;
23393 four.is_dwz = 1;
23394
23395 std::vector<dwarf2_per_cu_data *> units;
23396 units.push_back (&one);
23397 units.push_back (&two);
23398 units.push_back (&three);
23399 units.push_back (&four);
23400
23401 int result;
23402
23403 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23404 SELF_CHECK (units[result] == &one);
23405 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23406 SELF_CHECK (units[result] == &one);
23407 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23408 SELF_CHECK (units[result] == &two);
23409
23410 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23411 SELF_CHECK (units[result] == &three);
23412 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23413 SELF_CHECK (units[result] == &three);
23414 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23415 SELF_CHECK (units[result] == &four);
23416 }
23417
23418 }
23419 }
23420
23421 #endif /* GDB_SELF_TEST */
23422
23423 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23424
23425 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23426 : per_cu (per_cu_),
23427 mark (false),
23428 has_loclist (false),
23429 checked_producer (false),
23430 producer_is_gxx_lt_4_6 (false),
23431 producer_is_gcc_lt_4_3 (false),
23432 producer_is_icc (false),
23433 producer_is_icc_lt_14 (false),
23434 producer_is_codewarrior (false),
23435 processing_has_namespace_info (false)
23436 {
23437 per_cu->cu = this;
23438 }
23439
23440 /* Destroy a dwarf2_cu. */
23441
23442 dwarf2_cu::~dwarf2_cu ()
23443 {
23444 per_cu->cu = NULL;
23445 }
23446
23447 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23448
23449 static void
23450 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23451 enum language pretend_language)
23452 {
23453 struct attribute *attr;
23454
23455 /* Set the language we're debugging. */
23456 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23457 if (attr != nullptr)
23458 set_cu_language (DW_UNSND (attr), cu);
23459 else
23460 {
23461 cu->language = pretend_language;
23462 cu->language_defn = language_def (cu->language);
23463 }
23464
23465 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23466 }
23467
23468 /* Increase the age counter on each cached compilation unit, and free
23469 any that are too old. */
23470
23471 static void
23472 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23473 {
23474 struct dwarf2_per_cu_data *per_cu, **last_chain;
23475
23476 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23477 per_cu = dwarf2_per_objfile->read_in_chain;
23478 while (per_cu != NULL)
23479 {
23480 per_cu->cu->last_used ++;
23481 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23482 dwarf2_mark (per_cu->cu);
23483 per_cu = per_cu->cu->read_in_chain;
23484 }
23485
23486 per_cu = dwarf2_per_objfile->read_in_chain;
23487 last_chain = &dwarf2_per_objfile->read_in_chain;
23488 while (per_cu != NULL)
23489 {
23490 struct dwarf2_per_cu_data *next_cu;
23491
23492 next_cu = per_cu->cu->read_in_chain;
23493
23494 if (!per_cu->cu->mark)
23495 {
23496 delete per_cu->cu;
23497 *last_chain = next_cu;
23498 }
23499 else
23500 last_chain = &per_cu->cu->read_in_chain;
23501
23502 per_cu = next_cu;
23503 }
23504 }
23505
23506 /* Remove a single compilation unit from the cache. */
23507
23508 static void
23509 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23510 {
23511 struct dwarf2_per_cu_data *per_cu, **last_chain;
23512 struct dwarf2_per_objfile *dwarf2_per_objfile
23513 = target_per_cu->dwarf2_per_objfile;
23514
23515 per_cu = dwarf2_per_objfile->read_in_chain;
23516 last_chain = &dwarf2_per_objfile->read_in_chain;
23517 while (per_cu != NULL)
23518 {
23519 struct dwarf2_per_cu_data *next_cu;
23520
23521 next_cu = per_cu->cu->read_in_chain;
23522
23523 if (per_cu == target_per_cu)
23524 {
23525 delete per_cu->cu;
23526 per_cu->cu = NULL;
23527 *last_chain = next_cu;
23528 break;
23529 }
23530 else
23531 last_chain = &per_cu->cu->read_in_chain;
23532
23533 per_cu = next_cu;
23534 }
23535 }
23536
23537 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23538 We store these in a hash table separate from the DIEs, and preserve them
23539 when the DIEs are flushed out of cache.
23540
23541 The CU "per_cu" pointer is needed because offset alone is not enough to
23542 uniquely identify the type. A file may have multiple .debug_types sections,
23543 or the type may come from a DWO file. Furthermore, while it's more logical
23544 to use per_cu->section+offset, with Fission the section with the data is in
23545 the DWO file but we don't know that section at the point we need it.
23546 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23547 because we can enter the lookup routine, get_die_type_at_offset, from
23548 outside this file, and thus won't necessarily have PER_CU->cu.
23549 Fortunately, PER_CU is stable for the life of the objfile. */
23550
23551 struct dwarf2_per_cu_offset_and_type
23552 {
23553 const struct dwarf2_per_cu_data *per_cu;
23554 sect_offset sect_off;
23555 struct type *type;
23556 };
23557
23558 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23559
23560 static hashval_t
23561 per_cu_offset_and_type_hash (const void *item)
23562 {
23563 const struct dwarf2_per_cu_offset_and_type *ofs
23564 = (const struct dwarf2_per_cu_offset_and_type *) item;
23565
23566 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23567 }
23568
23569 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23570
23571 static int
23572 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23573 {
23574 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23575 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23576 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23577 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23578
23579 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23580 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23581 }
23582
23583 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23584 table if necessary. For convenience, return TYPE.
23585
23586 The DIEs reading must have careful ordering to:
23587 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23588 reading current DIE.
23589 * Not trying to dereference contents of still incompletely read in types
23590 while reading in other DIEs.
23591 * Enable referencing still incompletely read in types just by a pointer to
23592 the type without accessing its fields.
23593
23594 Therefore caller should follow these rules:
23595 * Try to fetch any prerequisite types we may need to build this DIE type
23596 before building the type and calling set_die_type.
23597 * After building type call set_die_type for current DIE as soon as
23598 possible before fetching more types to complete the current type.
23599 * Make the type as complete as possible before fetching more types. */
23600
23601 static struct type *
23602 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23603 {
23604 struct dwarf2_per_objfile *dwarf2_per_objfile
23605 = cu->per_cu->dwarf2_per_objfile;
23606 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23607 struct objfile *objfile = dwarf2_per_objfile->objfile;
23608 struct attribute *attr;
23609 struct dynamic_prop prop;
23610
23611 /* For Ada types, make sure that the gnat-specific data is always
23612 initialized (if not already set). There are a few types where
23613 we should not be doing so, because the type-specific area is
23614 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23615 where the type-specific area is used to store the floatformat).
23616 But this is not a problem, because the gnat-specific information
23617 is actually not needed for these types. */
23618 if (need_gnat_info (cu)
23619 && TYPE_CODE (type) != TYPE_CODE_FUNC
23620 && TYPE_CODE (type) != TYPE_CODE_FLT
23621 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23622 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23623 && TYPE_CODE (type) != TYPE_CODE_METHOD
23624 && !HAVE_GNAT_AUX_INFO (type))
23625 INIT_GNAT_SPECIFIC (type);
23626
23627 /* Read DW_AT_allocated and set in type. */
23628 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23629 if (attr != NULL && attr->form_is_block ())
23630 {
23631 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23632 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23633 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23634 }
23635 else if (attr != NULL)
23636 {
23637 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23638 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23639 sect_offset_str (die->sect_off));
23640 }
23641
23642 /* Read DW_AT_associated and set in type. */
23643 attr = dwarf2_attr (die, DW_AT_associated, cu);
23644 if (attr != NULL && attr->form_is_block ())
23645 {
23646 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23647 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23648 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23649 }
23650 else if (attr != NULL)
23651 {
23652 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23653 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23654 sect_offset_str (die->sect_off));
23655 }
23656
23657 /* Read DW_AT_data_location and set in type. */
23658 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23659 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23660 cu->per_cu->addr_type ()))
23661 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23662
23663 if (dwarf2_per_objfile->die_type_hash == NULL)
23664 dwarf2_per_objfile->die_type_hash
23665 = htab_up (htab_create_alloc (127,
23666 per_cu_offset_and_type_hash,
23667 per_cu_offset_and_type_eq,
23668 NULL, xcalloc, xfree));
23669
23670 ofs.per_cu = cu->per_cu;
23671 ofs.sect_off = die->sect_off;
23672 ofs.type = type;
23673 slot = (struct dwarf2_per_cu_offset_and_type **)
23674 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23675 if (*slot)
23676 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23677 sect_offset_str (die->sect_off));
23678 *slot = XOBNEW (&objfile->objfile_obstack,
23679 struct dwarf2_per_cu_offset_and_type);
23680 **slot = ofs;
23681 return type;
23682 }
23683
23684 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23685 or return NULL if the die does not have a saved type. */
23686
23687 static struct type *
23688 get_die_type_at_offset (sect_offset sect_off,
23689 struct dwarf2_per_cu_data *per_cu)
23690 {
23691 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23692 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23693
23694 if (dwarf2_per_objfile->die_type_hash == NULL)
23695 return NULL;
23696
23697 ofs.per_cu = per_cu;
23698 ofs.sect_off = sect_off;
23699 slot = ((struct dwarf2_per_cu_offset_and_type *)
23700 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23701 if (slot)
23702 return slot->type;
23703 else
23704 return NULL;
23705 }
23706
23707 /* Look up the type for DIE in CU in die_type_hash,
23708 or return NULL if DIE does not have a saved type. */
23709
23710 static struct type *
23711 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23712 {
23713 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23714 }
23715
23716 /* Add a dependence relationship from CU to REF_PER_CU. */
23717
23718 static void
23719 dwarf2_add_dependence (struct dwarf2_cu *cu,
23720 struct dwarf2_per_cu_data *ref_per_cu)
23721 {
23722 void **slot;
23723
23724 if (cu->dependencies == NULL)
23725 cu->dependencies
23726 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23727 NULL, &cu->comp_unit_obstack,
23728 hashtab_obstack_allocate,
23729 dummy_obstack_deallocate);
23730
23731 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23732 if (*slot == NULL)
23733 *slot = ref_per_cu;
23734 }
23735
23736 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23737 Set the mark field in every compilation unit in the
23738 cache that we must keep because we are keeping CU. */
23739
23740 static int
23741 dwarf2_mark_helper (void **slot, void *data)
23742 {
23743 struct dwarf2_per_cu_data *per_cu;
23744
23745 per_cu = (struct dwarf2_per_cu_data *) *slot;
23746
23747 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23748 reading of the chain. As such dependencies remain valid it is not much
23749 useful to track and undo them during QUIT cleanups. */
23750 if (per_cu->cu == NULL)
23751 return 1;
23752
23753 if (per_cu->cu->mark)
23754 return 1;
23755 per_cu->cu->mark = true;
23756
23757 if (per_cu->cu->dependencies != NULL)
23758 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23759
23760 return 1;
23761 }
23762
23763 /* Set the mark field in CU and in every other compilation unit in the
23764 cache that we must keep because we are keeping CU. */
23765
23766 static void
23767 dwarf2_mark (struct dwarf2_cu *cu)
23768 {
23769 if (cu->mark)
23770 return;
23771 cu->mark = true;
23772 if (cu->dependencies != NULL)
23773 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23774 }
23775
23776 static void
23777 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23778 {
23779 while (per_cu)
23780 {
23781 per_cu->cu->mark = false;
23782 per_cu = per_cu->cu->read_in_chain;
23783 }
23784 }
23785
23786 /* Trivial hash function for partial_die_info: the hash value of a DIE
23787 is its offset in .debug_info for this objfile. */
23788
23789 static hashval_t
23790 partial_die_hash (const void *item)
23791 {
23792 const struct partial_die_info *part_die
23793 = (const struct partial_die_info *) item;
23794
23795 return to_underlying (part_die->sect_off);
23796 }
23797
23798 /* Trivial comparison function for partial_die_info structures: two DIEs
23799 are equal if they have the same offset. */
23800
23801 static int
23802 partial_die_eq (const void *item_lhs, const void *item_rhs)
23803 {
23804 const struct partial_die_info *part_die_lhs
23805 = (const struct partial_die_info *) item_lhs;
23806 const struct partial_die_info *part_die_rhs
23807 = (const struct partial_die_info *) item_rhs;
23808
23809 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23810 }
23811
23812 struct cmd_list_element *set_dwarf_cmdlist;
23813 struct cmd_list_element *show_dwarf_cmdlist;
23814
23815 static void
23816 show_check_physname (struct ui_file *file, int from_tty,
23817 struct cmd_list_element *c, const char *value)
23818 {
23819 fprintf_filtered (file,
23820 _("Whether to check \"physname\" is %s.\n"),
23821 value);
23822 }
23823
23824 void _initialize_dwarf2_read ();
23825 void
23826 _initialize_dwarf2_read ()
23827 {
23828 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23829 Set DWARF specific variables.\n\
23830 Configure DWARF variables such as the cache size."),
23831 &set_dwarf_cmdlist, "maintenance set dwarf ",
23832 0/*allow-unknown*/, &maintenance_set_cmdlist);
23833
23834 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23835 Show DWARF specific variables.\n\
23836 Show DWARF variables such as the cache size."),
23837 &show_dwarf_cmdlist, "maintenance show dwarf ",
23838 0/*allow-unknown*/, &maintenance_show_cmdlist);
23839
23840 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23841 &dwarf_max_cache_age, _("\
23842 Set the upper bound on the age of cached DWARF compilation units."), _("\
23843 Show the upper bound on the age of cached DWARF compilation units."), _("\
23844 A higher limit means that cached compilation units will be stored\n\
23845 in memory longer, and more total memory will be used. Zero disables\n\
23846 caching, which can slow down startup."),
23847 NULL,
23848 show_dwarf_max_cache_age,
23849 &set_dwarf_cmdlist,
23850 &show_dwarf_cmdlist);
23851
23852 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23853 Set debugging of the DWARF reader."), _("\
23854 Show debugging of the DWARF reader."), _("\
23855 When enabled (non-zero), debugging messages are printed during DWARF\n\
23856 reading and symtab expansion. A value of 1 (one) provides basic\n\
23857 information. A value greater than 1 provides more verbose information."),
23858 NULL,
23859 NULL,
23860 &setdebuglist, &showdebuglist);
23861
23862 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23863 Set debugging of the DWARF DIE reader."), _("\
23864 Show debugging of the DWARF DIE reader."), _("\
23865 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23866 The value is the maximum depth to print."),
23867 NULL,
23868 NULL,
23869 &setdebuglist, &showdebuglist);
23870
23871 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23872 Set debugging of the dwarf line reader."), _("\
23873 Show debugging of the dwarf line reader."), _("\
23874 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23875 A value of 1 (one) provides basic information.\n\
23876 A value greater than 1 provides more verbose information."),
23877 NULL,
23878 NULL,
23879 &setdebuglist, &showdebuglist);
23880
23881 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23882 Set cross-checking of \"physname\" code against demangler."), _("\
23883 Show cross-checking of \"physname\" code against demangler."), _("\
23884 When enabled, GDB's internal \"physname\" code is checked against\n\
23885 the demangler."),
23886 NULL, show_check_physname,
23887 &setdebuglist, &showdebuglist);
23888
23889 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23890 no_class, &use_deprecated_index_sections, _("\
23891 Set whether to use deprecated gdb_index sections."), _("\
23892 Show whether to use deprecated gdb_index sections."), _("\
23893 When enabled, deprecated .gdb_index sections are used anyway.\n\
23894 Normally they are ignored either because of a missing feature or\n\
23895 performance issue.\n\
23896 Warning: This option must be enabled before gdb reads the file."),
23897 NULL,
23898 NULL,
23899 &setlist, &showlist);
23900
23901 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23902 &dwarf2_locexpr_funcs);
23903 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23904 &dwarf2_loclist_funcs);
23905
23906 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23907 &dwarf2_block_frame_base_locexpr_funcs);
23908 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23909 &dwarf2_block_frame_base_loclist_funcs);
23910
23911 #if GDB_SELF_TEST
23912 selftests::register_test ("dw2_expand_symtabs_matching",
23913 selftests::dw2_expand_symtabs_matching::run_test);
23914 selftests::register_test ("dwarf2_find_containing_comp_unit",
23915 selftests::find_containing_comp_unit::run_test);
23916 #endif
23917 }
This page took 0.524936 seconds and 3 git commands to generate.