Move more code to line-header.c
[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 "bfd.h"
43 #include "elf-bfd.h"
44 #include "symtab.h"
45 #include "gdbtypes.h"
46 #include "objfiles.h"
47 #include "dwarf2.h"
48 #include "buildsym.h"
49 #include "demangle.h"
50 #include "gdb-demangle.h"
51 #include "filenames.h" /* for DOSish file names */
52 #include "language.h"
53 #include "complaints.h"
54 #include "dwarf2/expr.h"
55 #include "dwarf2/loc.h"
56 #include "cp-support.h"
57 #include "hashtab.h"
58 #include "command.h"
59 #include "gdbcmd.h"
60 #include "block.h"
61 #include "addrmap.h"
62 #include "typeprint.h"
63 #include "psympriv.h"
64 #include "c-lang.h"
65 #include "go-lang.h"
66 #include "valprint.h"
67 #include "gdbcore.h" /* for gnutarget */
68 #include "gdb/gdb-index.h"
69 #include "gdb_bfd.h"
70 #include "f-lang.h"
71 #include "source.h"
72 #include "build-id.h"
73 #include "namespace.h"
74 #include "gdbsupport/function-view.h"
75 #include "gdbsupport/gdb_optional.h"
76 #include "gdbsupport/underlying.h"
77 #include "gdbsupport/hash_enum.h"
78 #include "filename-seen-cache.h"
79 #include "producer.h"
80 #include <fcntl.h>
81 #include <algorithm>
82 #include <unordered_map>
83 #include "gdbsupport/selftest.h"
84 #include "rust-lang.h"
85 #include "gdbsupport/pathstuff.h"
86 #include "count-one-bits.h"
87 #include "debuginfod-support.h"
88
89 /* When == 1, print basic high level tracing messages.
90 When > 1, be more verbose.
91 This is in contrast to the low level DIE reading of dwarf_die_debug. */
92 static unsigned int dwarf_read_debug = 0;
93
94 /* When non-zero, dump DIEs after they are read in. */
95 static unsigned int dwarf_die_debug = 0;
96
97 /* When non-zero, dump line number entries as they are read in. */
98 unsigned int dwarf_line_debug = 0;
99
100 /* When true, cross-check physname against demangler. */
101 static bool check_physname = false;
102
103 /* When true, do not reject deprecated .gdb_index sections. */
104 static bool use_deprecated_index_sections = false;
105
106 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
107
108 /* The "aclass" indices for various kinds of computed DWARF symbols. */
109
110 static int dwarf2_locexpr_index;
111 static int dwarf2_loclist_index;
112 static int dwarf2_locexpr_block_index;
113 static int dwarf2_loclist_block_index;
114
115 /* An index into a (C++) symbol name component in a symbol name as
116 recorded in the mapped_index's symbol table. For each C++ symbol
117 in the symbol table, we record one entry for the start of each
118 component in the symbol in a table of name components, and then
119 sort the table, in order to be able to binary search symbol names,
120 ignoring leading namespaces, both completion and regular look up.
121 For example, for symbol "A::B::C", we'll have an entry that points
122 to "A::B::C", another that points to "B::C", and another for "C".
123 Note that function symbols in GDB index have no parameter
124 information, just the function/method names. You can convert a
125 name_component to a "const char *" using the
126 'mapped_index::symbol_name_at(offset_type)' method. */
127
128 struct name_component
129 {
130 /* Offset in the symbol name where the component starts. Stored as
131 a (32-bit) offset instead of a pointer to save memory and improve
132 locality on 64-bit architectures. */
133 offset_type name_offset;
134
135 /* The symbol's index in the symbol and constant pool tables of a
136 mapped_index. */
137 offset_type idx;
138 };
139
140 /* Base class containing bits shared by both .gdb_index and
141 .debug_name indexes. */
142
143 struct mapped_index_base
144 {
145 mapped_index_base () = default;
146 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
147
148 /* The name_component table (a sorted vector). See name_component's
149 description above. */
150 std::vector<name_component> name_components;
151
152 /* How NAME_COMPONENTS is sorted. */
153 enum case_sensitivity name_components_casing;
154
155 /* Return the number of names in the symbol table. */
156 virtual size_t symbol_name_count () const = 0;
157
158 /* Get the name of the symbol at IDX in the symbol table. */
159 virtual const char *symbol_name_at (offset_type idx) const = 0;
160
161 /* Return whether the name at IDX in the symbol table should be
162 ignored. */
163 virtual bool symbol_name_slot_invalid (offset_type idx) const
164 {
165 return false;
166 }
167
168 /* Build the symbol name component sorted vector, if we haven't
169 yet. */
170 void build_name_components ();
171
172 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
173 possible matches for LN_NO_PARAMS in the name component
174 vector. */
175 std::pair<std::vector<name_component>::const_iterator,
176 std::vector<name_component>::const_iterator>
177 find_name_components_bounds (const lookup_name_info &ln_no_params,
178 enum language lang) const;
179
180 /* Prevent deleting/destroying via a base class pointer. */
181 protected:
182 ~mapped_index_base() = default;
183 };
184
185 /* A description of the mapped index. The file format is described in
186 a comment by the code that writes the index. */
187 struct mapped_index final : public mapped_index_base
188 {
189 /* A slot/bucket in the symbol table hash. */
190 struct symbol_table_slot
191 {
192 const offset_type name;
193 const offset_type vec;
194 };
195
196 /* Index data format version. */
197 int version = 0;
198
199 /* The address table data. */
200 gdb::array_view<const gdb_byte> address_table;
201
202 /* The symbol table, implemented as a hash table. */
203 gdb::array_view<symbol_table_slot> symbol_table;
204
205 /* A pointer to the constant pool. */
206 const char *constant_pool = nullptr;
207
208 bool symbol_name_slot_invalid (offset_type idx) const override
209 {
210 const auto &bucket = this->symbol_table[idx];
211 return bucket.name == 0 && bucket.vec == 0;
212 }
213
214 /* Convenience method to get at the name of the symbol at IDX in the
215 symbol table. */
216 const char *symbol_name_at (offset_type idx) const override
217 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
218
219 size_t symbol_name_count () const override
220 { return this->symbol_table.size (); }
221 };
222
223 /* A description of the mapped .debug_names.
224 Uninitialized map has CU_COUNT 0. */
225 struct mapped_debug_names final : public mapped_index_base
226 {
227 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
228 : dwarf2_per_objfile (dwarf2_per_objfile_)
229 {}
230
231 struct dwarf2_per_objfile *dwarf2_per_objfile;
232 bfd_endian dwarf5_byte_order;
233 bool dwarf5_is_dwarf64;
234 bool augmentation_is_gdb;
235 uint8_t offset_size;
236 uint32_t cu_count = 0;
237 uint32_t tu_count, bucket_count, name_count;
238 const gdb_byte *cu_table_reordered, *tu_table_reordered;
239 const uint32_t *bucket_table_reordered, *hash_table_reordered;
240 const gdb_byte *name_table_string_offs_reordered;
241 const gdb_byte *name_table_entry_offs_reordered;
242 const gdb_byte *entry_pool;
243
244 struct index_val
245 {
246 ULONGEST dwarf_tag;
247 struct attr
248 {
249 /* Attribute name DW_IDX_*. */
250 ULONGEST dw_idx;
251
252 /* Attribute form DW_FORM_*. */
253 ULONGEST form;
254
255 /* Value if FORM is DW_FORM_implicit_const. */
256 LONGEST implicit_const;
257 };
258 std::vector<attr> attr_vec;
259 };
260
261 std::unordered_map<ULONGEST, index_val> abbrev_map;
262
263 const char *namei_to_name (uint32_t namei) const;
264
265 /* Implementation of the mapped_index_base virtual interface, for
266 the name_components cache. */
267
268 const char *symbol_name_at (offset_type idx) const override
269 { return namei_to_name (idx); }
270
271 size_t symbol_name_count () const override
272 { return this->name_count; }
273 };
274
275 /* See dwarf2read.h. */
276
277 dwarf2_per_objfile *
278 get_dwarf2_per_objfile (struct objfile *objfile)
279 {
280 return dwarf2_objfile_data_key.get (objfile);
281 }
282
283 /* Default names of the debugging sections. */
284
285 /* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
288 static const struct dwarf2_debug_sections dwarf2_elf_names =
289 {
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_loclists", ".zdebug_loclists" },
295 { ".debug_macinfo", ".zdebug_macinfo" },
296 { ".debug_macro", ".zdebug_macro" },
297 { ".debug_str", ".zdebug_str" },
298 { ".debug_str_offsets", ".zdebug_str_offsets" },
299 { ".debug_line_str", ".zdebug_line_str" },
300 { ".debug_ranges", ".zdebug_ranges" },
301 { ".debug_rnglists", ".zdebug_rnglists" },
302 { ".debug_types", ".zdebug_types" },
303 { ".debug_addr", ".zdebug_addr" },
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
306 { ".gdb_index", ".zgdb_index" },
307 { ".debug_names", ".zdebug_names" },
308 { ".debug_aranges", ".zdebug_aranges" },
309 23
310 };
311
312 /* List of DWO/DWP sections. */
313
314 static const struct dwop_section_names
315 {
316 struct dwarf2_section_names abbrev_dwo;
317 struct dwarf2_section_names info_dwo;
318 struct dwarf2_section_names line_dwo;
319 struct dwarf2_section_names loc_dwo;
320 struct dwarf2_section_names loclists_dwo;
321 struct dwarf2_section_names macinfo_dwo;
322 struct dwarf2_section_names macro_dwo;
323 struct dwarf2_section_names str_dwo;
324 struct dwarf2_section_names str_offsets_dwo;
325 struct dwarf2_section_names types_dwo;
326 struct dwarf2_section_names cu_index;
327 struct dwarf2_section_names tu_index;
328 }
329 dwop_section_names =
330 {
331 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
332 { ".debug_info.dwo", ".zdebug_info.dwo" },
333 { ".debug_line.dwo", ".zdebug_line.dwo" },
334 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
335 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
336 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
337 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
338 { ".debug_str.dwo", ".zdebug_str.dwo" },
339 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
340 { ".debug_types.dwo", ".zdebug_types.dwo" },
341 { ".debug_cu_index", ".zdebug_cu_index" },
342 { ".debug_tu_index", ".zdebug_tu_index" },
343 };
344
345 /* local data types */
346
347 /* Type used for delaying computation of method physnames.
348 See comments for compute_delayed_physnames. */
349 struct delayed_method_info
350 {
351 /* The type to which the method is attached, i.e., its parent class. */
352 struct type *type;
353
354 /* The index of the method in the type's function fieldlists. */
355 int fnfield_index;
356
357 /* The index of the method in the fieldlist. */
358 int index;
359
360 /* The name of the DIE. */
361 const char *name;
362
363 /* The DIE associated with this method. */
364 struct die_info *die;
365 };
366
367 /* Internal state when decoding a particular compilation unit. */
368 struct dwarf2_cu
369 {
370 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
371 ~dwarf2_cu ();
372
373 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
374
375 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
376 Create the set of symtabs used by this TU, or if this TU is sharing
377 symtabs with another TU and the symtabs have already been created
378 then restore those symtabs in the line header.
379 We don't need the pc/line-number mapping for type units. */
380 void setup_type_unit_groups (struct die_info *die);
381
382 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
383 buildsym_compunit constructor. */
384 struct compunit_symtab *start_symtab (const char *name,
385 const char *comp_dir,
386 CORE_ADDR low_pc);
387
388 /* Reset the builder. */
389 void reset_builder () { m_builder.reset (); }
390
391 /* The header of the compilation unit. */
392 struct comp_unit_head header {};
393
394 /* Base address of this compilation unit. */
395 CORE_ADDR base_address = 0;
396
397 /* Non-zero if base_address has been set. */
398 int base_known = 0;
399
400 /* The language we are debugging. */
401 enum language language = language_unknown;
402 const struct language_defn *language_defn = nullptr;
403
404 const char *producer = nullptr;
405
406 private:
407 /* The symtab builder for this CU. This is only non-NULL when full
408 symbols are being read. */
409 std::unique_ptr<buildsym_compunit> m_builder;
410
411 public:
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope = nullptr;
422
423 /* Hash table holding all the loaded partial DIEs
424 with partial_die->offset.SECT_OFF as hash. */
425 htab_t partial_dies = nullptr;
426
427 /* Storage for things with the same lifetime as this read-in compilation
428 unit, including partial DIEs. */
429 auto_obstack comp_unit_obstack;
430
431 /* When multiple dwarf2_cu structures are living in memory, this field
432 chains them all together, so that they can be released efficiently.
433 We will probably also want a generation counter so that most-recently-used
434 compilation units are cached... */
435 struct dwarf2_per_cu_data *read_in_chain = nullptr;
436
437 /* Backlink to our per_cu entry. */
438 struct dwarf2_per_cu_data *per_cu;
439
440 /* How many compilation units ago was this CU last referenced? */
441 int last_used = 0;
442
443 /* A hash table of DIE cu_offset for following references with
444 die_info->offset.sect_off as hash. */
445 htab_t die_hash = nullptr;
446
447 /* Full DIEs if read in. */
448 struct die_info *dies = nullptr;
449
450 /* A set of pointers to dwarf2_per_cu_data objects for compilation
451 units referenced by this one. Only set during full symbol processing;
452 partial symbol tables do not have dependencies. */
453 htab_t dependencies = nullptr;
454
455 /* Header data from the line table, during full symbol processing. */
456 struct line_header *line_header = nullptr;
457 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
458 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
459 this is the DW_TAG_compile_unit die for this CU. We'll hold on
460 to the line header as long as this DIE is being processed. See
461 process_die_scope. */
462 die_info *line_header_die_owner = nullptr;
463
464 /* A list of methods which need to have physnames computed
465 after all type information has been read. */
466 std::vector<delayed_method_info> method_list;
467
468 /* To be copied to symtab->call_site_htab. */
469 htab_t call_site_htab = nullptr;
470
471 /* Non-NULL if this CU came from a DWO file.
472 There is an invariant here that is important to remember:
473 Except for attributes copied from the top level DIE in the "main"
474 (or "stub") file in preparation for reading the DWO file
475 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
476 Either there isn't a DWO file (in which case this is NULL and the point
477 is moot), or there is and either we're not going to read it (in which
478 case this is NULL) or there is and we are reading it (in which case this
479 is non-NULL). */
480 struct dwo_unit *dwo_unit = nullptr;
481
482 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
483 Note this value comes from the Fission stub CU/TU's DIE. */
484 gdb::optional<ULONGEST> addr_base;
485
486 /* The DW_AT_rnglists_base attribute if present.
487 Note this value comes from the Fission stub CU/TU's DIE.
488 Also note that the value is zero in the non-DWO case so this value can
489 be used without needing to know whether DWO files are in use or not.
490 N.B. This does not apply to DW_AT_ranges appearing in
491 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
492 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
493 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
494 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
495 ULONGEST ranges_base = 0;
496
497 /* When reading debug info generated by older versions of rustc, we
498 have to rewrite some union types to be struct types with a
499 variant part. This rewriting must be done after the CU is fully
500 read in, because otherwise at the point of rewriting some struct
501 type might not have been fully processed. So, we keep a list of
502 all such types here and process them after expansion. */
503 std::vector<struct type *> rust_unions;
504
505 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
506 files, the value is implicitly zero. For DWARF 5 version DWO files, the
507 value is often implicit and is the size of the header of
508 .debug_str_offsets section (8 or 4, depending on the address size). */
509 gdb::optional<ULONGEST> str_offsets_base;
510
511 /* Mark used when releasing cached dies. */
512 bool mark : 1;
513
514 /* This CU references .debug_loc. See the symtab->locations_valid field.
515 This test is imperfect as there may exist optimized debug code not using
516 any location list and still facing inlining issues if handled as
517 unoptimized code. For a future better test see GCC PR other/32998. */
518 bool has_loclist : 1;
519
520 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
521 if all the producer_is_* fields are valid. This information is cached
522 because profiling CU expansion showed excessive time spent in
523 producer_is_gxx_lt_4_6. */
524 bool checked_producer : 1;
525 bool producer_is_gxx_lt_4_6 : 1;
526 bool producer_is_gcc_lt_4_3 : 1;
527 bool producer_is_icc : 1;
528 bool producer_is_icc_lt_14 : 1;
529 bool producer_is_codewarrior : 1;
530
531 /* When true, the file that we're processing is known to have
532 debugging info for C++ namespaces. GCC 3.3.x did not produce
533 this information, but later versions do. */
534
535 bool processing_has_namespace_info : 1;
536
537 struct partial_die_info *find_partial_die (sect_offset sect_off);
538
539 /* If this CU was inherited by another CU (via specification,
540 abstract_origin, etc), this is the ancestor CU. */
541 dwarf2_cu *ancestor;
542
543 /* Get the buildsym_compunit for this CU. */
544 buildsym_compunit *get_builder ()
545 {
546 /* If this CU has a builder associated with it, use that. */
547 if (m_builder != nullptr)
548 return m_builder.get ();
549
550 /* Otherwise, search ancestors for a valid builder. */
551 if (ancestor != nullptr)
552 return ancestor->get_builder ();
553
554 return nullptr;
555 }
556 };
557
558 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
559 This includes type_unit_group and quick_file_names. */
560
561 struct stmt_list_hash
562 {
563 /* The DWO unit this table is from or NULL if there is none. */
564 struct dwo_unit *dwo_unit;
565
566 /* Offset in .debug_line or .debug_line.dwo. */
567 sect_offset line_sect_off;
568 };
569
570 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
571 an object of this type. */
572
573 struct type_unit_group
574 {
575 /* dwarf2read.c's main "handle" on a TU symtab.
576 To simplify things we create an artificial CU that "includes" all the
577 type units using this stmt_list so that the rest of the code still has
578 a "per_cu" handle on the symtab. */
579 struct dwarf2_per_cu_data per_cu;
580
581 /* The TUs that share this DW_AT_stmt_list entry.
582 This is added to while parsing type units to build partial symtabs,
583 and is deleted afterwards and not used again. */
584 std::vector<signatured_type *> *tus;
585
586 /* The compunit symtab.
587 Type units in a group needn't all be defined in the same source file,
588 so we create an essentially anonymous symtab as the compunit symtab. */
589 struct compunit_symtab *compunit_symtab;
590
591 /* The data used to construct the hash key. */
592 struct stmt_list_hash hash;
593
594 /* The symbol tables for this TU (obtained from the files listed in
595 DW_AT_stmt_list).
596 WARNING: The order of entries here must match the order of entries
597 in the line header. After the first TU using this type_unit_group, the
598 line header for the subsequent TUs is recreated from this. This is done
599 because we need to use the same symtabs for each TU using the same
600 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
601 there's no guarantee the line header doesn't have duplicate entries. */
602 struct symtab **symtabs;
603 };
604
605 /* These sections are what may appear in a (real or virtual) DWO file. */
606
607 struct dwo_sections
608 {
609 struct dwarf2_section_info abbrev;
610 struct dwarf2_section_info line;
611 struct dwarf2_section_info loc;
612 struct dwarf2_section_info loclists;
613 struct dwarf2_section_info macinfo;
614 struct dwarf2_section_info macro;
615 struct dwarf2_section_info str;
616 struct dwarf2_section_info str_offsets;
617 /* In the case of a virtual DWO file, these two are unused. */
618 struct dwarf2_section_info info;
619 std::vector<dwarf2_section_info> types;
620 };
621
622 /* CUs/TUs in DWP/DWO files. */
623
624 struct dwo_unit
625 {
626 /* Backlink to the containing struct dwo_file. */
627 struct dwo_file *dwo_file;
628
629 /* The "id" that distinguishes this CU/TU.
630 .debug_info calls this "dwo_id", .debug_types calls this "signature".
631 Since signatures came first, we stick with it for consistency. */
632 ULONGEST signature;
633
634 /* The section this CU/TU lives in, in the DWO file. */
635 struct dwarf2_section_info *section;
636
637 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
638 sect_offset sect_off;
639 unsigned int length;
640
641 /* For types, offset in the type's DIE of the type defined by this TU. */
642 cu_offset type_offset_in_tu;
643 };
644
645 /* include/dwarf2.h defines the DWP section codes.
646 It defines a max value but it doesn't define a min value, which we
647 use for error checking, so provide one. */
648
649 enum dwp_v2_section_ids
650 {
651 DW_SECT_MIN = 1
652 };
653
654 /* Data for one DWO file.
655
656 This includes virtual DWO files (a virtual DWO file is a DWO file as it
657 appears in a DWP file). DWP files don't really have DWO files per se -
658 comdat folding of types "loses" the DWO file they came from, and from
659 a high level view DWP files appear to contain a mass of random types.
660 However, to maintain consistency with the non-DWP case we pretend DWP
661 files contain virtual DWO files, and we assign each TU with one virtual
662 DWO file (generally based on the line and abbrev section offsets -
663 a heuristic that seems to work in practice). */
664
665 struct dwo_file
666 {
667 dwo_file () = default;
668 DISABLE_COPY_AND_ASSIGN (dwo_file);
669
670 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
671 For virtual DWO files the name is constructed from the section offsets
672 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
673 from related CU+TUs. */
674 const char *dwo_name = nullptr;
675
676 /* The DW_AT_comp_dir attribute. */
677 const char *comp_dir = nullptr;
678
679 /* The bfd, when the file is open. Otherwise this is NULL.
680 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
681 gdb_bfd_ref_ptr dbfd;
682
683 /* The sections that make up this DWO file.
684 Remember that for virtual DWO files in DWP V2, these are virtual
685 sections (for lack of a better name). */
686 struct dwo_sections sections {};
687
688 /* The CUs in the file.
689 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
690 an extension to handle LLVM's Link Time Optimization output (where
691 multiple source files may be compiled into a single object/dwo pair). */
692 htab_up cus;
693
694 /* Table of TUs in the file.
695 Each element is a struct dwo_unit. */
696 htab_up tus;
697 };
698
699 /* These sections are what may appear in a DWP file. */
700
701 struct dwp_sections
702 {
703 /* These are used by both DWP version 1 and 2. */
704 struct dwarf2_section_info str;
705 struct dwarf2_section_info cu_index;
706 struct dwarf2_section_info tu_index;
707
708 /* These are only used by DWP version 2 files.
709 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
710 sections are referenced by section number, and are not recorded here.
711 In DWP version 2 there is at most one copy of all these sections, each
712 section being (effectively) comprised of the concatenation of all of the
713 individual sections that exist in the version 1 format.
714 To keep the code simple we treat each of these concatenated pieces as a
715 section itself (a virtual section?). */
716 struct dwarf2_section_info abbrev;
717 struct dwarf2_section_info info;
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
722 struct dwarf2_section_info str_offsets;
723 struct dwarf2_section_info types;
724 };
725
726 /* These sections are what may appear in a virtual DWO file in DWP version 1.
727 A virtual DWO file is a DWO file as it appears in a DWP file. */
728
729 struct virtual_v1_dwo_sections
730 {
731 struct dwarf2_section_info abbrev;
732 struct dwarf2_section_info line;
733 struct dwarf2_section_info loc;
734 struct dwarf2_section_info macinfo;
735 struct dwarf2_section_info macro;
736 struct dwarf2_section_info str_offsets;
737 /* Each DWP hash table entry records one CU or one TU.
738 That is recorded here, and copied to dwo_unit.section. */
739 struct dwarf2_section_info info_or_types;
740 };
741
742 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
743 In version 2, the sections of the DWO files are concatenated together
744 and stored in one section of that name. Thus each ELF section contains
745 several "virtual" sections. */
746
747 struct virtual_v2_dwo_sections
748 {
749 bfd_size_type abbrev_offset;
750 bfd_size_type abbrev_size;
751
752 bfd_size_type line_offset;
753 bfd_size_type line_size;
754
755 bfd_size_type loc_offset;
756 bfd_size_type loc_size;
757
758 bfd_size_type macinfo_offset;
759 bfd_size_type macinfo_size;
760
761 bfd_size_type macro_offset;
762 bfd_size_type macro_size;
763
764 bfd_size_type str_offsets_offset;
765 bfd_size_type str_offsets_size;
766
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.section. */
769 bfd_size_type info_or_types_offset;
770 bfd_size_type info_or_types_size;
771 };
772
773 /* Contents of DWP hash tables. */
774
775 struct dwp_hash_table
776 {
777 uint32_t version, nr_columns;
778 uint32_t nr_units, nr_slots;
779 const gdb_byte *hash_table, *unit_table;
780 union
781 {
782 struct
783 {
784 const gdb_byte *indices;
785 } v1;
786 struct
787 {
788 /* This is indexed by column number and gives the id of the section
789 in that column. */
790 #define MAX_NR_V2_DWO_SECTIONS \
791 (1 /* .debug_info or .debug_types */ \
792 + 1 /* .debug_abbrev */ \
793 + 1 /* .debug_line */ \
794 + 1 /* .debug_loc */ \
795 + 1 /* .debug_str_offsets */ \
796 + 1 /* .debug_macro or .debug_macinfo */)
797 int section_ids[MAX_NR_V2_DWO_SECTIONS];
798 const gdb_byte *offsets;
799 const gdb_byte *sizes;
800 } v2;
801 } section_pool;
802 };
803
804 /* Data for one DWP file. */
805
806 struct dwp_file
807 {
808 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
809 : name (name_),
810 dbfd (std::move (abfd))
811 {
812 }
813
814 /* Name of the file. */
815 const char *name;
816
817 /* File format version. */
818 int version = 0;
819
820 /* The bfd. */
821 gdb_bfd_ref_ptr dbfd;
822
823 /* Section info for this file. */
824 struct dwp_sections sections {};
825
826 /* Table of CUs in the file. */
827 const struct dwp_hash_table *cus = nullptr;
828
829 /* Table of TUs in the file. */
830 const struct dwp_hash_table *tus = nullptr;
831
832 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
833 htab_up loaded_cus;
834 htab_up loaded_tus;
835
836 /* Table to map ELF section numbers to their sections.
837 This is only needed for the DWP V1 file format. */
838 unsigned int num_sections = 0;
839 asection **elf_sections = nullptr;
840 };
841
842 /* Struct used to pass misc. parameters to read_die_and_children, et
843 al. which are used for both .debug_info and .debug_types dies.
844 All parameters here are unchanging for the life of the call. This
845 struct exists to abstract away the constant parameters of die reading. */
846
847 struct die_reader_specs
848 {
849 /* The bfd of die_section. */
850 bfd* abfd;
851
852 /* The CU of the DIE we are parsing. */
853 struct dwarf2_cu *cu;
854
855 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
856 struct dwo_file *dwo_file;
857
858 /* The section the die comes from.
859 This is either .debug_info or .debug_types, or the .dwo variants. */
860 struct dwarf2_section_info *die_section;
861
862 /* die_section->buffer. */
863 const gdb_byte *buffer;
864
865 /* The end of the buffer. */
866 const gdb_byte *buffer_end;
867
868 /* The abbreviation table to use when reading the DIEs. */
869 struct abbrev_table *abbrev_table;
870 };
871
872 /* A subclass of die_reader_specs that holds storage and has complex
873 constructor and destructor behavior. */
874
875 class cutu_reader : public die_reader_specs
876 {
877 public:
878
879 cutu_reader (struct dwarf2_per_cu_data *this_cu,
880 struct abbrev_table *abbrev_table,
881 int use_existing_cu,
882 bool skip_partial);
883
884 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
885 struct dwarf2_cu *parent_cu = nullptr,
886 struct dwo_file *dwo_file = nullptr);
887
888 DISABLE_COPY_AND_ASSIGN (cutu_reader);
889
890 const gdb_byte *info_ptr = nullptr;
891 struct die_info *comp_unit_die = nullptr;
892 bool dummy_p = false;
893
894 /* Release the new CU, putting it on the chain. This cannot be done
895 for dummy CUs. */
896 void keep ();
897
898 private:
899 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
900 int use_existing_cu);
901
902 struct dwarf2_per_cu_data *m_this_cu;
903 std::unique_ptr<dwarf2_cu> m_new_cu;
904
905 /* The ordinary abbreviation table. */
906 abbrev_table_up m_abbrev_table_holder;
907
908 /* The DWO abbreviation table. */
909 abbrev_table_up m_dwo_abbrev_table;
910 };
911
912 /* When we construct a partial symbol table entry we only
913 need this much information. */
914 struct partial_die_info : public allocate_on_obstack
915 {
916 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
917
918 /* Disable assign but still keep copy ctor, which is needed
919 load_partial_dies. */
920 partial_die_info& operator=(const partial_die_info& rhs) = delete;
921
922 /* Adjust the partial die before generating a symbol for it. This
923 function may set the is_external flag or change the DIE's
924 name. */
925 void fixup (struct dwarf2_cu *cu);
926
927 /* Read a minimal amount of information into the minimal die
928 structure. */
929 const gdb_byte *read (const struct die_reader_specs *reader,
930 const struct abbrev_info &abbrev,
931 const gdb_byte *info_ptr);
932
933 /* Offset of this DIE. */
934 const sect_offset sect_off;
935
936 /* DWARF-2 tag for this DIE. */
937 const ENUM_BITFIELD(dwarf_tag) tag : 16;
938
939 /* Assorted flags describing the data found in this DIE. */
940 const unsigned int has_children : 1;
941
942 unsigned int is_external : 1;
943 unsigned int is_declaration : 1;
944 unsigned int has_type : 1;
945 unsigned int has_specification : 1;
946 unsigned int has_pc_info : 1;
947 unsigned int may_be_inlined : 1;
948
949 /* This DIE has been marked DW_AT_main_subprogram. */
950 unsigned int main_subprogram : 1;
951
952 /* Flag set if the SCOPE field of this structure has been
953 computed. */
954 unsigned int scope_set : 1;
955
956 /* Flag set if the DIE has a byte_size attribute. */
957 unsigned int has_byte_size : 1;
958
959 /* Flag set if the DIE has a DW_AT_const_value attribute. */
960 unsigned int has_const_value : 1;
961
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
964
965 /* Flag set if fixup has been called on this die. */
966 unsigned int fixup_called : 1;
967
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
970
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
973
974 /* The name of this DIE. Normally the value of DW_AT_name, but
975 sometimes a default name for unnamed DIEs. */
976 const char *name = nullptr;
977
978 /* The linkage name, if present. */
979 const char *linkage_name = nullptr;
980
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
984 const char *scope = nullptr;
985
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
988 union
989 {
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
993 sect_offset sect_off;
994 } d {};
995
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
997 CORE_ADDR lowpc = 0;
998 CORE_ADDR highpc = 0;
999
1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1001 DW_AT_sibling, if any. */
1002 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1003 could return DW_AT_sibling values to its caller load_partial_dies. */
1004 const gdb_byte *sibling = nullptr;
1005
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
1009 sect_offset spec_offset {};
1010
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1012 if any. */
1013 struct partial_die_info *die_parent = nullptr;
1014 struct partial_die_info *die_child = nullptr;
1015 struct partial_die_info *die_sibling = nullptr;
1016
1017 friend struct partial_die_info *
1018 dwarf2_cu::find_partial_die (sect_offset sect_off);
1019
1020 private:
1021 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1022 partial_die_info (sect_offset sect_off)
1023 : partial_die_info (sect_off, DW_TAG_padding, 0)
1024 {
1025 }
1026
1027 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1028 int has_children_)
1029 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1030 {
1031 is_external = 0;
1032 is_declaration = 0;
1033 has_type = 0;
1034 has_specification = 0;
1035 has_pc_info = 0;
1036 may_be_inlined = 0;
1037 main_subprogram = 0;
1038 scope_set = 0;
1039 has_byte_size = 0;
1040 has_const_value = 0;
1041 has_template_arguments = 0;
1042 fixup_called = 0;
1043 is_dwz = 0;
1044 spec_is_dwz = 0;
1045 }
1046 };
1047
1048 /* This data structure holds a complete die structure. */
1049 struct die_info
1050 {
1051 /* DWARF-2 tag for this DIE. */
1052 ENUM_BITFIELD(dwarf_tag) tag : 16;
1053
1054 /* Number of attributes */
1055 unsigned char num_attrs;
1056
1057 /* True if we're presently building the full type name for the
1058 type derived from this DIE. */
1059 unsigned char building_fullname : 1;
1060
1061 /* True if this die is in process. PR 16581. */
1062 unsigned char in_process : 1;
1063
1064 /* True if this DIE has children. */
1065 unsigned char has_children : 1;
1066
1067 /* Abbrev number */
1068 unsigned int abbrev;
1069
1070 /* Offset in .debug_info or .debug_types section. */
1071 sect_offset sect_off;
1072
1073 /* The dies in a compilation unit form an n-ary tree. PARENT
1074 points to this die's parent; CHILD points to the first child of
1075 this node; and all the children of a given node are chained
1076 together via their SIBLING fields. */
1077 struct die_info *child; /* Its first child, if any. */
1078 struct die_info *sibling; /* Its next sibling, if any. */
1079 struct die_info *parent; /* Its parent, if any. */
1080
1081 /* An array of attributes, with NUM_ATTRS elements. There may be
1082 zero, but it's not common and zero-sized arrays are not
1083 sufficiently portable C. */
1084 struct attribute attrs[1];
1085 };
1086
1087 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090 static int bits_per_byte = 8;
1091
1092 /* When reading a variant or variant part, we track a bit more
1093 information about the field, and store it in an object of this
1094 type. */
1095
1096 struct variant_field
1097 {
1098 /* If we see a DW_TAG_variant, then this will be the discriminant
1099 value. */
1100 ULONGEST discriminant_value;
1101 /* If we see a DW_TAG_variant, then this will be set if this is the
1102 default branch. */
1103 bool default_branch;
1104 /* While reading a DW_TAG_variant_part, this will be set if this
1105 field is the discriminant. */
1106 bool is_discriminant;
1107 };
1108
1109 struct nextfield
1110 {
1111 int accessibility = 0;
1112 int virtuality = 0;
1113 /* Extra information to describe a variant or variant part. */
1114 struct variant_field variant {};
1115 struct field field {};
1116 };
1117
1118 struct fnfieldlist
1119 {
1120 const char *name = nullptr;
1121 std::vector<struct fn_field> fnfields;
1122 };
1123
1124 /* The routines that read and process dies for a C struct or C++ class
1125 pass lists of data member fields and lists of member function fields
1126 in an instance of a field_info structure, as defined below. */
1127 struct field_info
1128 {
1129 /* List of data member and baseclasses fields. */
1130 std::vector<struct nextfield> fields;
1131 std::vector<struct nextfield> baseclasses;
1132
1133 /* Set if the accessibility of one of the fields is not public. */
1134 int non_public_fields = 0;
1135
1136 /* Member function fieldlist array, contains name of possibly overloaded
1137 member function, number of overloaded member functions and a pointer
1138 to the head of the member function field chain. */
1139 std::vector<struct fnfieldlist> fnfieldlists;
1140
1141 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1142 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1143 std::vector<struct decl_field> typedef_field_list;
1144
1145 /* Nested types defined by this class and the number of elements in this
1146 list. */
1147 std::vector<struct decl_field> nested_types_list;
1148
1149 /* Return the total number of fields (including baseclasses). */
1150 int nfields () const
1151 {
1152 return fields.size () + baseclasses.size ();
1153 }
1154 };
1155
1156 /* Loaded secondary compilation units are kept in memory until they
1157 have not been referenced for the processing of this many
1158 compilation units. Set this to zero to disable caching. Cache
1159 sizes of up to at least twenty will improve startup time for
1160 typical inter-CU-reference binaries, at an obvious memory cost. */
1161 static int dwarf_max_cache_age = 5;
1162 static void
1163 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1164 struct cmd_list_element *c, const char *value)
1165 {
1166 fprintf_filtered (file, _("The upper bound on the age of cached "
1167 "DWARF compilation units is %s.\n"),
1168 value);
1169 }
1170 \f
1171 /* local function prototypes */
1172
1173 static void dwarf2_find_base_address (struct die_info *die,
1174 struct dwarf2_cu *cu);
1175
1176 static dwarf2_psymtab *create_partial_symtab
1177 (struct dwarf2_per_cu_data *per_cu, const char *name);
1178
1179 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1180 const gdb_byte *info_ptr,
1181 struct die_info *type_unit_die);
1182
1183 static void dwarf2_build_psymtabs_hard
1184 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1185
1186 static void scan_partial_symbols (struct partial_die_info *,
1187 CORE_ADDR *, CORE_ADDR *,
1188 int, struct dwarf2_cu *);
1189
1190 static void add_partial_symbol (struct partial_die_info *,
1191 struct dwarf2_cu *);
1192
1193 static void add_partial_namespace (struct partial_die_info *pdi,
1194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1195 int set_addrmap, struct dwarf2_cu *cu);
1196
1197 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1198 CORE_ADDR *highpc, int set_addrmap,
1199 struct dwarf2_cu *cu);
1200
1201 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1202 struct dwarf2_cu *cu);
1203
1204 static void add_partial_subprogram (struct partial_die_info *pdi,
1205 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1206 int need_pc, struct dwarf2_cu *cu);
1207
1208 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1209
1210 static struct partial_die_info *load_partial_dies
1211 (const struct die_reader_specs *, const gdb_byte *, int);
1212
1213 /* A pair of partial_die_info and compilation unit. */
1214 struct cu_partial_die_info
1215 {
1216 /* The compilation unit of the partial_die_info. */
1217 struct dwarf2_cu *cu;
1218 /* A partial_die_info. */
1219 struct partial_die_info *pdi;
1220
1221 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1222 : cu (cu),
1223 pdi (pdi)
1224 { /* Nothing. */ }
1225
1226 private:
1227 cu_partial_die_info () = delete;
1228 };
1229
1230 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1231 struct dwarf2_cu *);
1232
1233 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1234 struct attribute *, struct attr_abbrev *,
1235 const gdb_byte *, bool *need_reprocess);
1236
1237 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1238 struct attribute *attr);
1239
1240 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1241
1242 static sect_offset read_abbrev_offset
1243 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1244 struct dwarf2_section_info *, sect_offset);
1245
1246 static const char *read_indirect_string
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1248 const struct comp_unit_head *, unsigned int *);
1249
1250 static const char *read_indirect_string_at_offset
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1252
1253 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1254 const gdb_byte *,
1255 unsigned int *);
1256
1257 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1258 ULONGEST str_index);
1259
1260 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1261 ULONGEST str_index);
1262
1263 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1264
1265 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1266 struct dwarf2_cu *);
1267
1268 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1269 unsigned int);
1270
1271 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1272 struct dwarf2_cu *cu);
1273
1274 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1275
1276 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1277 struct dwarf2_cu *cu);
1278
1279 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1280
1281 static struct die_info *die_specification (struct die_info *die,
1282 struct dwarf2_cu **);
1283
1284 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1285 struct dwarf2_cu *cu);
1286
1287 static void dwarf_decode_lines (struct line_header *, const char *,
1288 struct dwarf2_cu *, dwarf2_psymtab *,
1289 CORE_ADDR, int decode_mapping);
1290
1291 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1292 const char *);
1293
1294 static struct symbol *new_symbol (struct die_info *, struct type *,
1295 struct dwarf2_cu *, struct symbol * = NULL);
1296
1297 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1298 struct dwarf2_cu *);
1299
1300 static void dwarf2_const_value_attr (const struct attribute *attr,
1301 struct type *type,
1302 const char *name,
1303 struct obstack *obstack,
1304 struct dwarf2_cu *cu, LONGEST *value,
1305 const gdb_byte **bytes,
1306 struct dwarf2_locexpr_baton **baton);
1307
1308 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1309
1310 static int need_gnat_info (struct dwarf2_cu *);
1311
1312 static struct type *die_descriptive_type (struct die_info *,
1313 struct dwarf2_cu *);
1314
1315 static void set_descriptive_type (struct type *, struct die_info *,
1316 struct dwarf2_cu *);
1317
1318 static struct type *die_containing_type (struct die_info *,
1319 struct dwarf2_cu *);
1320
1321 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1322 struct dwarf2_cu *);
1323
1324 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1325
1326 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1327
1328 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1329
1330 static char *typename_concat (struct obstack *obs, const char *prefix,
1331 const char *suffix, int physname,
1332 struct dwarf2_cu *cu);
1333
1334 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1335
1336 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1337
1338 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1339
1340 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1341
1342 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1343
1344 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1345
1346 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1347 struct dwarf2_cu *, dwarf2_psymtab *);
1348
1349 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1350 values. Keep the items ordered with increasing constraints compliance. */
1351 enum pc_bounds_kind
1352 {
1353 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1354 PC_BOUNDS_NOT_PRESENT,
1355
1356 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1357 were present but they do not form a valid range of PC addresses. */
1358 PC_BOUNDS_INVALID,
1359
1360 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1361 PC_BOUNDS_RANGES,
1362
1363 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1364 PC_BOUNDS_HIGH_LOW,
1365 };
1366
1367 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1368 CORE_ADDR *, CORE_ADDR *,
1369 struct dwarf2_cu *,
1370 dwarf2_psymtab *);
1371
1372 static void get_scope_pc_bounds (struct die_info *,
1373 CORE_ADDR *, CORE_ADDR *,
1374 struct dwarf2_cu *);
1375
1376 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1377 CORE_ADDR, struct dwarf2_cu *);
1378
1379 static void dwarf2_add_field (struct field_info *, struct die_info *,
1380 struct dwarf2_cu *);
1381
1382 static void dwarf2_attach_fields_to_type (struct field_info *,
1383 struct type *, struct dwarf2_cu *);
1384
1385 static void dwarf2_add_member_fn (struct field_info *,
1386 struct die_info *, struct type *,
1387 struct dwarf2_cu *);
1388
1389 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1390 struct type *,
1391 struct dwarf2_cu *);
1392
1393 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1394
1395 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1396
1397 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1398
1399 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1400
1401 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1402
1403 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1404
1405 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1406
1407 static struct type *read_module_type (struct die_info *die,
1408 struct dwarf2_cu *cu);
1409
1410 static const char *namespace_name (struct die_info *die,
1411 int *is_anonymous, struct dwarf2_cu *);
1412
1413 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1414
1415 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1416
1417 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1418 struct dwarf2_cu *);
1419
1420 static struct die_info *read_die_and_siblings_1
1421 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1422 struct die_info *);
1423
1424 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1425 const gdb_byte *info_ptr,
1426 const gdb_byte **new_info_ptr,
1427 struct die_info *parent);
1428
1429 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1430 struct die_info **, const gdb_byte *,
1431 int);
1432
1433 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1434 struct die_info **, const gdb_byte *);
1435
1436 static void process_die (struct die_info *, struct dwarf2_cu *);
1437
1438 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1439 struct objfile *);
1440
1441 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1442
1443 static const char *dwarf2_full_name (const char *name,
1444 struct die_info *die,
1445 struct dwarf2_cu *cu);
1446
1447 static const char *dwarf2_physname (const char *name, struct die_info *die,
1448 struct dwarf2_cu *cu);
1449
1450 static struct die_info *dwarf2_extension (struct die_info *die,
1451 struct dwarf2_cu **);
1452
1453 static const char *dwarf_tag_name (unsigned int);
1454
1455 static const char *dwarf_attr_name (unsigned int);
1456
1457 static const char *dwarf_form_name (unsigned int);
1458
1459 static const char *dwarf_bool_name (unsigned int);
1460
1461 static const char *dwarf_type_encoding_name (unsigned int);
1462
1463 static struct die_info *sibling_die (struct die_info *);
1464
1465 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1466
1467 static void dump_die_for_error (struct die_info *);
1468
1469 static void dump_die_1 (struct ui_file *, int level, int max_level,
1470 struct die_info *);
1471
1472 /*static*/ void dump_die (struct die_info *, int max_level);
1473
1474 static void store_in_ref_table (struct die_info *,
1475 struct dwarf2_cu *);
1476
1477 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1478
1479 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1480
1481 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1482 const struct attribute *,
1483 struct dwarf2_cu **);
1484
1485 static struct die_info *follow_die_ref (struct die_info *,
1486 const struct attribute *,
1487 struct dwarf2_cu **);
1488
1489 static struct die_info *follow_die_sig (struct die_info *,
1490 const struct attribute *,
1491 struct dwarf2_cu **);
1492
1493 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1494 struct dwarf2_cu *);
1495
1496 static struct type *get_DW_AT_signature_type (struct die_info *,
1497 const struct attribute *,
1498 struct dwarf2_cu *);
1499
1500 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1501
1502 static void read_signatured_type (struct signatured_type *);
1503
1504 static int attr_to_dynamic_prop (const struct attribute *attr,
1505 struct die_info *die, struct dwarf2_cu *cu,
1506 struct dynamic_prop *prop, struct type *type);
1507
1508 /* memory allocation interface */
1509
1510 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1511
1512 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1513
1514 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1515
1516 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1517 struct dwarf2_loclist_baton *baton,
1518 const struct attribute *attr);
1519
1520 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1521 struct symbol *sym,
1522 struct dwarf2_cu *cu,
1523 int is_block);
1524
1525 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1526 const gdb_byte *info_ptr,
1527 struct abbrev_info *abbrev);
1528
1529 static hashval_t partial_die_hash (const void *item);
1530
1531 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1532
1533 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1534 (sect_offset sect_off, unsigned int offset_in_dwz,
1535 struct dwarf2_per_objfile *dwarf2_per_objfile);
1536
1537 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1538 struct die_info *comp_unit_die,
1539 enum language pretend_language);
1540
1541 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1542
1543 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1544
1545 static struct type *set_die_type (struct die_info *, struct type *,
1546 struct dwarf2_cu *);
1547
1548 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1549
1550 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1551
1552 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1553 enum language);
1554
1555 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1556 enum language);
1557
1558 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1559 enum language);
1560
1561 static void dwarf2_add_dependence (struct dwarf2_cu *,
1562 struct dwarf2_per_cu_data *);
1563
1564 static void dwarf2_mark (struct dwarf2_cu *);
1565
1566 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1567
1568 static struct type *get_die_type_at_offset (sect_offset,
1569 struct dwarf2_per_cu_data *);
1570
1571 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1572
1573 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1574 enum language pretend_language);
1575
1576 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1577
1578 /* Class, the destructor of which frees all allocated queue entries. This
1579 will only have work to do if an error was thrown while processing the
1580 dwarf. If no error was thrown then the queue entries should have all
1581 been processed, and freed, as we went along. */
1582
1583 class dwarf2_queue_guard
1584 {
1585 public:
1586 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1587 : m_per_objfile (per_objfile)
1588 {
1589 }
1590
1591 /* Free any entries remaining on the queue. There should only be
1592 entries left if we hit an error while processing the dwarf. */
1593 ~dwarf2_queue_guard ()
1594 {
1595 /* Ensure that no memory is allocated by the queue. */
1596 std::queue<dwarf2_queue_item> empty;
1597 std::swap (m_per_objfile->queue, empty);
1598 }
1599
1600 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1601
1602 private:
1603 dwarf2_per_objfile *m_per_objfile;
1604 };
1605
1606 dwarf2_queue_item::~dwarf2_queue_item ()
1607 {
1608 /* Anything still marked queued is likely to be in an
1609 inconsistent state, so discard it. */
1610 if (per_cu->queued)
1611 {
1612 if (per_cu->cu != NULL)
1613 free_one_cached_comp_unit (per_cu);
1614 per_cu->queued = 0;
1615 }
1616 }
1617
1618 /* The return type of find_file_and_directory. Note, the enclosed
1619 string pointers are only valid while this object is valid. */
1620
1621 struct file_and_directory
1622 {
1623 /* The filename. This is never NULL. */
1624 const char *name;
1625
1626 /* The compilation directory. NULL if not known. If we needed to
1627 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1628 points directly to the DW_AT_comp_dir string attribute owned by
1629 the obstack that owns the DIE. */
1630 const char *comp_dir;
1631
1632 /* If we needed to build a new string for comp_dir, this is what
1633 owns the storage. */
1634 std::string comp_dir_storage;
1635 };
1636
1637 static file_and_directory find_file_and_directory (struct die_info *die,
1638 struct dwarf2_cu *cu);
1639
1640 static htab_up allocate_signatured_type_table ();
1641
1642 static htab_up allocate_dwo_unit_table ();
1643
1644 static struct dwo_unit *lookup_dwo_unit_in_dwp
1645 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1646 struct dwp_file *dwp_file, const char *comp_dir,
1647 ULONGEST signature, int is_debug_types);
1648
1649 static struct dwp_file *get_dwp_file
1650 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1651
1652 static struct dwo_unit *lookup_dwo_comp_unit
1653 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1654
1655 static struct dwo_unit *lookup_dwo_type_unit
1656 (struct signatured_type *, const char *, const char *);
1657
1658 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1659
1660 /* A unique pointer to a dwo_file. */
1661
1662 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1663
1664 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1665
1666 static void check_producer (struct dwarf2_cu *cu);
1667
1668 static void free_line_header_voidp (void *arg);
1669 \f
1670 /* Various complaints about symbol reading that don't abort the process. */
1671
1672 static void
1673 dwarf2_debug_line_missing_file_complaint (void)
1674 {
1675 complaint (_(".debug_line section has line data without a file"));
1676 }
1677
1678 static void
1679 dwarf2_debug_line_missing_end_sequence_complaint (void)
1680 {
1681 complaint (_(".debug_line section has line "
1682 "program sequence without an end"));
1683 }
1684
1685 static void
1686 dwarf2_complex_location_expr_complaint (void)
1687 {
1688 complaint (_("location expression too complex"));
1689 }
1690
1691 static void
1692 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1693 int arg3)
1694 {
1695 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1696 arg1, arg2, arg3);
1697 }
1698
1699 static void
1700 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1701 {
1702 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1703 arg1, arg2);
1704 }
1705
1706 /* Hash function for line_header_hash. */
1707
1708 static hashval_t
1709 line_header_hash (const struct line_header *ofs)
1710 {
1711 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1712 }
1713
1714 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1715
1716 static hashval_t
1717 line_header_hash_voidp (const void *item)
1718 {
1719 const struct line_header *ofs = (const struct line_header *) item;
1720
1721 return line_header_hash (ofs);
1722 }
1723
1724 /* Equality function for line_header_hash. */
1725
1726 static int
1727 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1728 {
1729 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1730 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1731
1732 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1733 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1734 }
1735
1736 \f
1737
1738 /* See declaration. */
1739
1740 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1741 const dwarf2_debug_sections *names,
1742 bool can_copy_)
1743 : objfile (objfile_),
1744 can_copy (can_copy_)
1745 {
1746 if (names == NULL)
1747 names = &dwarf2_elf_names;
1748
1749 bfd *obfd = objfile->obfd;
1750
1751 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1752 locate_sections (obfd, sec, *names);
1753 }
1754
1755 dwarf2_per_objfile::~dwarf2_per_objfile ()
1756 {
1757 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1758 free_cached_comp_units ();
1759
1760 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1761 per_cu->imported_symtabs_free ();
1762
1763 for (signatured_type *sig_type : all_type_units)
1764 sig_type->per_cu.imported_symtabs_free ();
1765
1766 /* Everything else should be on the objfile obstack. */
1767 }
1768
1769 /* See declaration. */
1770
1771 void
1772 dwarf2_per_objfile::free_cached_comp_units ()
1773 {
1774 dwarf2_per_cu_data *per_cu = read_in_chain;
1775 dwarf2_per_cu_data **last_chain = &read_in_chain;
1776 while (per_cu != NULL)
1777 {
1778 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1779
1780 delete per_cu->cu;
1781 *last_chain = next_cu;
1782 per_cu = next_cu;
1783 }
1784 }
1785
1786 /* A helper class that calls free_cached_comp_units on
1787 destruction. */
1788
1789 class free_cached_comp_units
1790 {
1791 public:
1792
1793 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1794 : m_per_objfile (per_objfile)
1795 {
1796 }
1797
1798 ~free_cached_comp_units ()
1799 {
1800 m_per_objfile->free_cached_comp_units ();
1801 }
1802
1803 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1804
1805 private:
1806
1807 dwarf2_per_objfile *m_per_objfile;
1808 };
1809
1810 /* Try to locate the sections we need for DWARF 2 debugging
1811 information and return true if we have enough to do something.
1812 NAMES points to the dwarf2 section names, or is NULL if the standard
1813 ELF names are used. CAN_COPY is true for formats where symbol
1814 interposition is possible and so symbol values must follow copy
1815 relocation rules. */
1816
1817 int
1818 dwarf2_has_info (struct objfile *objfile,
1819 const struct dwarf2_debug_sections *names,
1820 bool can_copy)
1821 {
1822 if (objfile->flags & OBJF_READNEVER)
1823 return 0;
1824
1825 struct dwarf2_per_objfile *dwarf2_per_objfile
1826 = get_dwarf2_per_objfile (objfile);
1827
1828 if (dwarf2_per_objfile == NULL)
1829 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1830 names,
1831 can_copy);
1832
1833 return (!dwarf2_per_objfile->info.is_virtual
1834 && dwarf2_per_objfile->info.s.section != NULL
1835 && !dwarf2_per_objfile->abbrev.is_virtual
1836 && dwarf2_per_objfile->abbrev.s.section != NULL);
1837 }
1838
1839 /* When loading sections, we look either for uncompressed section or for
1840 compressed section names. */
1841
1842 static int
1843 section_is_p (const char *section_name,
1844 const struct dwarf2_section_names *names)
1845 {
1846 if (names->normal != NULL
1847 && strcmp (section_name, names->normal) == 0)
1848 return 1;
1849 if (names->compressed != NULL
1850 && strcmp (section_name, names->compressed) == 0)
1851 return 1;
1852 return 0;
1853 }
1854
1855 /* See declaration. */
1856
1857 void
1858 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1859 const dwarf2_debug_sections &names)
1860 {
1861 flagword aflag = bfd_section_flags (sectp);
1862
1863 if ((aflag & SEC_HAS_CONTENTS) == 0)
1864 {
1865 }
1866 else if (elf_section_data (sectp)->this_hdr.sh_size
1867 > bfd_get_file_size (abfd))
1868 {
1869 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1870 warning (_("Discarding section %s which has a section size (%s"
1871 ") larger than the file size [in module %s]"),
1872 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1873 bfd_get_filename (abfd));
1874 }
1875 else if (section_is_p (sectp->name, &names.info))
1876 {
1877 this->info.s.section = sectp;
1878 this->info.size = bfd_section_size (sectp);
1879 }
1880 else if (section_is_p (sectp->name, &names.abbrev))
1881 {
1882 this->abbrev.s.section = sectp;
1883 this->abbrev.size = bfd_section_size (sectp);
1884 }
1885 else if (section_is_p (sectp->name, &names.line))
1886 {
1887 this->line.s.section = sectp;
1888 this->line.size = bfd_section_size (sectp);
1889 }
1890 else if (section_is_p (sectp->name, &names.loc))
1891 {
1892 this->loc.s.section = sectp;
1893 this->loc.size = bfd_section_size (sectp);
1894 }
1895 else if (section_is_p (sectp->name, &names.loclists))
1896 {
1897 this->loclists.s.section = sectp;
1898 this->loclists.size = bfd_section_size (sectp);
1899 }
1900 else if (section_is_p (sectp->name, &names.macinfo))
1901 {
1902 this->macinfo.s.section = sectp;
1903 this->macinfo.size = bfd_section_size (sectp);
1904 }
1905 else if (section_is_p (sectp->name, &names.macro))
1906 {
1907 this->macro.s.section = sectp;
1908 this->macro.size = bfd_section_size (sectp);
1909 }
1910 else if (section_is_p (sectp->name, &names.str))
1911 {
1912 this->str.s.section = sectp;
1913 this->str.size = bfd_section_size (sectp);
1914 }
1915 else if (section_is_p (sectp->name, &names.str_offsets))
1916 {
1917 this->str_offsets.s.section = sectp;
1918 this->str_offsets.size = bfd_section_size (sectp);
1919 }
1920 else if (section_is_p (sectp->name, &names.line_str))
1921 {
1922 this->line_str.s.section = sectp;
1923 this->line_str.size = bfd_section_size (sectp);
1924 }
1925 else if (section_is_p (sectp->name, &names.addr))
1926 {
1927 this->addr.s.section = sectp;
1928 this->addr.size = bfd_section_size (sectp);
1929 }
1930 else if (section_is_p (sectp->name, &names.frame))
1931 {
1932 this->frame.s.section = sectp;
1933 this->frame.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.eh_frame))
1936 {
1937 this->eh_frame.s.section = sectp;
1938 this->eh_frame.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.ranges))
1941 {
1942 this->ranges.s.section = sectp;
1943 this->ranges.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.rnglists))
1946 {
1947 this->rnglists.s.section = sectp;
1948 this->rnglists.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.types))
1951 {
1952 struct dwarf2_section_info type_section;
1953
1954 memset (&type_section, 0, sizeof (type_section));
1955 type_section.s.section = sectp;
1956 type_section.size = bfd_section_size (sectp);
1957
1958 this->types.push_back (type_section);
1959 }
1960 else if (section_is_p (sectp->name, &names.gdb_index))
1961 {
1962 this->gdb_index.s.section = sectp;
1963 this->gdb_index.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.debug_names))
1966 {
1967 this->debug_names.s.section = sectp;
1968 this->debug_names.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.debug_aranges))
1971 {
1972 this->debug_aranges.s.section = sectp;
1973 this->debug_aranges.size = bfd_section_size (sectp);
1974 }
1975
1976 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1977 && bfd_section_vma (sectp) == 0)
1978 this->has_section_at_zero = true;
1979 }
1980
1981 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1982 SECTION_NAME. */
1983
1984 void
1985 dwarf2_get_section_info (struct objfile *objfile,
1986 enum dwarf2_section_enum sect,
1987 asection **sectp, const gdb_byte **bufp,
1988 bfd_size_type *sizep)
1989 {
1990 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1991 struct dwarf2_section_info *info;
1992
1993 /* We may see an objfile without any DWARF, in which case we just
1994 return nothing. */
1995 if (data == NULL)
1996 {
1997 *sectp = NULL;
1998 *bufp = NULL;
1999 *sizep = 0;
2000 return;
2001 }
2002 switch (sect)
2003 {
2004 case DWARF2_DEBUG_FRAME:
2005 info = &data->frame;
2006 break;
2007 case DWARF2_EH_FRAME:
2008 info = &data->eh_frame;
2009 break;
2010 default:
2011 gdb_assert_not_reached ("unexpected section");
2012 }
2013
2014 info->read (objfile);
2015
2016 *sectp = info->get_bfd_section ();
2017 *bufp = info->buffer;
2018 *sizep = info->size;
2019 }
2020
2021 /* A helper function to find the sections for a .dwz file. */
2022
2023 static void
2024 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2025 {
2026 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2027
2028 /* Note that we only support the standard ELF names, because .dwz
2029 is ELF-only (at the time of writing). */
2030 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2031 {
2032 dwz_file->abbrev.s.section = sectp;
2033 dwz_file->abbrev.size = bfd_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2036 {
2037 dwz_file->info.s.section = sectp;
2038 dwz_file->info.size = bfd_section_size (sectp);
2039 }
2040 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2041 {
2042 dwz_file->str.s.section = sectp;
2043 dwz_file->str.size = bfd_section_size (sectp);
2044 }
2045 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2046 {
2047 dwz_file->line.s.section = sectp;
2048 dwz_file->line.size = bfd_section_size (sectp);
2049 }
2050 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2051 {
2052 dwz_file->macro.s.section = sectp;
2053 dwz_file->macro.size = bfd_section_size (sectp);
2054 }
2055 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2056 {
2057 dwz_file->gdb_index.s.section = sectp;
2058 dwz_file->gdb_index.size = bfd_section_size (sectp);
2059 }
2060 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2061 {
2062 dwz_file->debug_names.s.section = sectp;
2063 dwz_file->debug_names.size = bfd_section_size (sectp);
2064 }
2065 }
2066
2067 /* See dwarf2read.h. */
2068
2069 struct dwz_file *
2070 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2071 {
2072 const char *filename;
2073 bfd_size_type buildid_len_arg;
2074 size_t buildid_len;
2075 bfd_byte *buildid;
2076
2077 if (dwarf2_per_objfile->dwz_file != NULL)
2078 return dwarf2_per_objfile->dwz_file.get ();
2079
2080 bfd_set_error (bfd_error_no_error);
2081 gdb::unique_xmalloc_ptr<char> data
2082 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2083 &buildid_len_arg, &buildid));
2084 if (data == NULL)
2085 {
2086 if (bfd_get_error () == bfd_error_no_error)
2087 return NULL;
2088 error (_("could not read '.gnu_debugaltlink' section: %s"),
2089 bfd_errmsg (bfd_get_error ()));
2090 }
2091
2092 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2093
2094 buildid_len = (size_t) buildid_len_arg;
2095
2096 filename = data.get ();
2097
2098 std::string abs_storage;
2099 if (!IS_ABSOLUTE_PATH (filename))
2100 {
2101 gdb::unique_xmalloc_ptr<char> abs
2102 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2103
2104 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2105 filename = abs_storage.c_str ();
2106 }
2107
2108 /* First try the file name given in the section. If that doesn't
2109 work, try to use the build-id instead. */
2110 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2111 if (dwz_bfd != NULL)
2112 {
2113 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2114 dwz_bfd.reset (nullptr);
2115 }
2116
2117 if (dwz_bfd == NULL)
2118 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2119
2120 if (dwz_bfd == nullptr)
2121 {
2122 gdb::unique_xmalloc_ptr<char> alt_filename;
2123 const char *origname = dwarf2_per_objfile->objfile->original_name;
2124
2125 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2126 buildid_len,
2127 origname,
2128 &alt_filename));
2129
2130 if (fd.get () >= 0)
2131 {
2132 /* File successfully retrieved from server. */
2133 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2134
2135 if (dwz_bfd == nullptr)
2136 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2137 alt_filename.get ());
2138 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2139 dwz_bfd.reset (nullptr);
2140 }
2141 }
2142
2143 if (dwz_bfd == NULL)
2144 error (_("could not find '.gnu_debugaltlink' file for %s"),
2145 objfile_name (dwarf2_per_objfile->objfile));
2146
2147 std::unique_ptr<struct dwz_file> result
2148 (new struct dwz_file (std::move (dwz_bfd)));
2149
2150 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2151 result.get ());
2152
2153 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2154 result->dwz_bfd.get ());
2155 dwarf2_per_objfile->dwz_file = std::move (result);
2156 return dwarf2_per_objfile->dwz_file.get ();
2157 }
2158 \f
2159 /* DWARF quick_symbols_functions support. */
2160
2161 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2162 unique line tables, so we maintain a separate table of all .debug_line
2163 derived entries to support the sharing.
2164 All the quick functions need is the list of file names. We discard the
2165 line_header when we're done and don't need to record it here. */
2166 struct quick_file_names
2167 {
2168 /* The data used to construct the hash key. */
2169 struct stmt_list_hash hash;
2170
2171 /* The number of entries in file_names, real_names. */
2172 unsigned int num_file_names;
2173
2174 /* The file names from the line table, after being run through
2175 file_full_name. */
2176 const char **file_names;
2177
2178 /* The file names from the line table after being run through
2179 gdb_realpath. These are computed lazily. */
2180 const char **real_names;
2181 };
2182
2183 /* When using the index (and thus not using psymtabs), each CU has an
2184 object of this type. This is used to hold information needed by
2185 the various "quick" methods. */
2186 struct dwarf2_per_cu_quick_data
2187 {
2188 /* The file table. This can be NULL if there was no file table
2189 or it's currently not read in.
2190 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2191 struct quick_file_names *file_names;
2192
2193 /* The corresponding symbol table. This is NULL if symbols for this
2194 CU have not yet been read. */
2195 struct compunit_symtab *compunit_symtab;
2196
2197 /* A temporary mark bit used when iterating over all CUs in
2198 expand_symtabs_matching. */
2199 unsigned int mark : 1;
2200
2201 /* True if we've tried to read the file table and found there isn't one.
2202 There will be no point in trying to read it again next time. */
2203 unsigned int no_file_data : 1;
2204 };
2205
2206 /* Utility hash function for a stmt_list_hash. */
2207
2208 static hashval_t
2209 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2210 {
2211 hashval_t v = 0;
2212
2213 if (stmt_list_hash->dwo_unit != NULL)
2214 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2215 v += to_underlying (stmt_list_hash->line_sect_off);
2216 return v;
2217 }
2218
2219 /* Utility equality function for a stmt_list_hash. */
2220
2221 static int
2222 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2223 const struct stmt_list_hash *rhs)
2224 {
2225 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2226 return 0;
2227 if (lhs->dwo_unit != NULL
2228 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2229 return 0;
2230
2231 return lhs->line_sect_off == rhs->line_sect_off;
2232 }
2233
2234 /* Hash function for a quick_file_names. */
2235
2236 static hashval_t
2237 hash_file_name_entry (const void *e)
2238 {
2239 const struct quick_file_names *file_data
2240 = (const struct quick_file_names *) e;
2241
2242 return hash_stmt_list_entry (&file_data->hash);
2243 }
2244
2245 /* Equality function for a quick_file_names. */
2246
2247 static int
2248 eq_file_name_entry (const void *a, const void *b)
2249 {
2250 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2251 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2252
2253 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2254 }
2255
2256 /* Delete function for a quick_file_names. */
2257
2258 static void
2259 delete_file_name_entry (void *e)
2260 {
2261 struct quick_file_names *file_data = (struct quick_file_names *) e;
2262 int i;
2263
2264 for (i = 0; i < file_data->num_file_names; ++i)
2265 {
2266 xfree ((void*) file_data->file_names[i]);
2267 if (file_data->real_names)
2268 xfree ((void*) file_data->real_names[i]);
2269 }
2270
2271 /* The space for the struct itself lives on objfile_obstack,
2272 so we don't free it here. */
2273 }
2274
2275 /* Create a quick_file_names hash table. */
2276
2277 static htab_up
2278 create_quick_file_names_table (unsigned int nr_initial_entries)
2279 {
2280 return htab_up (htab_create_alloc (nr_initial_entries,
2281 hash_file_name_entry, eq_file_name_entry,
2282 delete_file_name_entry, xcalloc, xfree));
2283 }
2284
2285 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2286 have to be created afterwards. You should call age_cached_comp_units after
2287 processing PER_CU->CU. dw2_setup must have been already called. */
2288
2289 static void
2290 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2291 {
2292 if (per_cu->is_debug_types)
2293 load_full_type_unit (per_cu);
2294 else
2295 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2296
2297 if (per_cu->cu == NULL)
2298 return; /* Dummy CU. */
2299
2300 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2301 }
2302
2303 /* Read in the symbols for PER_CU. */
2304
2305 static void
2306 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2307 {
2308 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2309
2310 /* Skip type_unit_groups, reading the type units they contain
2311 is handled elsewhere. */
2312 if (per_cu->type_unit_group_p ())
2313 return;
2314
2315 /* The destructor of dwarf2_queue_guard frees any entries left on
2316 the queue. After this point we're guaranteed to leave this function
2317 with the dwarf queue empty. */
2318 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2319
2320 if (dwarf2_per_objfile->using_index
2321 ? per_cu->v.quick->compunit_symtab == NULL
2322 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2323 {
2324 queue_comp_unit (per_cu, language_minimal);
2325 load_cu (per_cu, skip_partial);
2326
2327 /* If we just loaded a CU from a DWO, and we're working with an index
2328 that may badly handle TUs, load all the TUs in that DWO as well.
2329 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2330 if (!per_cu->is_debug_types
2331 && per_cu->cu != NULL
2332 && per_cu->cu->dwo_unit != NULL
2333 && dwarf2_per_objfile->index_table != NULL
2334 && dwarf2_per_objfile->index_table->version <= 7
2335 /* DWP files aren't supported yet. */
2336 && get_dwp_file (dwarf2_per_objfile) == NULL)
2337 queue_and_load_all_dwo_tus (per_cu);
2338 }
2339
2340 process_queue (dwarf2_per_objfile);
2341
2342 /* Age the cache, releasing compilation units that have not
2343 been used recently. */
2344 age_cached_comp_units (dwarf2_per_objfile);
2345 }
2346
2347 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2348 the objfile from which this CU came. Returns the resulting symbol
2349 table. */
2350
2351 static struct compunit_symtab *
2352 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2353 {
2354 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2355
2356 gdb_assert (dwarf2_per_objfile->using_index);
2357 if (!per_cu->v.quick->compunit_symtab)
2358 {
2359 free_cached_comp_units freer (dwarf2_per_objfile);
2360 scoped_restore decrementer = increment_reading_symtab ();
2361 dw2_do_instantiate_symtab (per_cu, skip_partial);
2362 process_cu_includes (dwarf2_per_objfile);
2363 }
2364
2365 return per_cu->v.quick->compunit_symtab;
2366 }
2367
2368 /* See declaration. */
2369
2370 dwarf2_per_cu_data *
2371 dwarf2_per_objfile::get_cutu (int index)
2372 {
2373 if (index >= this->all_comp_units.size ())
2374 {
2375 index -= this->all_comp_units.size ();
2376 gdb_assert (index < this->all_type_units.size ());
2377 return &this->all_type_units[index]->per_cu;
2378 }
2379
2380 return this->all_comp_units[index];
2381 }
2382
2383 /* See declaration. */
2384
2385 dwarf2_per_cu_data *
2386 dwarf2_per_objfile::get_cu (int index)
2387 {
2388 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2389
2390 return this->all_comp_units[index];
2391 }
2392
2393 /* See declaration. */
2394
2395 signatured_type *
2396 dwarf2_per_objfile::get_tu (int index)
2397 {
2398 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2399
2400 return this->all_type_units[index];
2401 }
2402
2403 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2404 objfile_obstack, and constructed with the specified field
2405 values. */
2406
2407 static dwarf2_per_cu_data *
2408 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2409 struct dwarf2_section_info *section,
2410 int is_dwz,
2411 sect_offset sect_off, ULONGEST length)
2412 {
2413 struct objfile *objfile = dwarf2_per_objfile->objfile;
2414 dwarf2_per_cu_data *the_cu
2415 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2416 struct dwarf2_per_cu_data);
2417 the_cu->sect_off = sect_off;
2418 the_cu->length = length;
2419 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2420 the_cu->section = section;
2421 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2422 struct dwarf2_per_cu_quick_data);
2423 the_cu->is_dwz = is_dwz;
2424 return the_cu;
2425 }
2426
2427 /* A helper for create_cus_from_index that handles a given list of
2428 CUs. */
2429
2430 static void
2431 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2432 const gdb_byte *cu_list, offset_type n_elements,
2433 struct dwarf2_section_info *section,
2434 int is_dwz)
2435 {
2436 for (offset_type i = 0; i < n_elements; i += 2)
2437 {
2438 gdb_static_assert (sizeof (ULONGEST) >= 8);
2439
2440 sect_offset sect_off
2441 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2442 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2443 cu_list += 2 * 8;
2444
2445 dwarf2_per_cu_data *per_cu
2446 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2447 sect_off, length);
2448 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2449 }
2450 }
2451
2452 /* Read the CU list from the mapped index, and use it to create all
2453 the CU objects for this objfile. */
2454
2455 static void
2456 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2457 const gdb_byte *cu_list, offset_type cu_list_elements,
2458 const gdb_byte *dwz_list, offset_type dwz_elements)
2459 {
2460 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2461 dwarf2_per_objfile->all_comp_units.reserve
2462 ((cu_list_elements + dwz_elements) / 2);
2463
2464 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2465 &dwarf2_per_objfile->info, 0);
2466
2467 if (dwz_elements == 0)
2468 return;
2469
2470 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2471 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2472 &dwz->info, 1);
2473 }
2474
2475 /* Create the signatured type hash table from the index. */
2476
2477 static void
2478 create_signatured_type_table_from_index
2479 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2480 struct dwarf2_section_info *section,
2481 const gdb_byte *bytes,
2482 offset_type elements)
2483 {
2484 struct objfile *objfile = dwarf2_per_objfile->objfile;
2485
2486 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2487 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2488
2489 htab_up sig_types_hash = allocate_signatured_type_table ();
2490
2491 for (offset_type i = 0; i < elements; i += 3)
2492 {
2493 struct signatured_type *sig_type;
2494 ULONGEST signature;
2495 void **slot;
2496 cu_offset type_offset_in_tu;
2497
2498 gdb_static_assert (sizeof (ULONGEST) >= 8);
2499 sect_offset sect_off
2500 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2501 type_offset_in_tu
2502 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2503 BFD_ENDIAN_LITTLE);
2504 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2505 bytes += 3 * 8;
2506
2507 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2508 struct signatured_type);
2509 sig_type->signature = signature;
2510 sig_type->type_offset_in_tu = type_offset_in_tu;
2511 sig_type->per_cu.is_debug_types = 1;
2512 sig_type->per_cu.section = section;
2513 sig_type->per_cu.sect_off = sect_off;
2514 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2515 sig_type->per_cu.v.quick
2516 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2517 struct dwarf2_per_cu_quick_data);
2518
2519 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2520 *slot = sig_type;
2521
2522 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2523 }
2524
2525 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2526 }
2527
2528 /* Create the signatured type hash table from .debug_names. */
2529
2530 static void
2531 create_signatured_type_table_from_debug_names
2532 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2533 const mapped_debug_names &map,
2534 struct dwarf2_section_info *section,
2535 struct dwarf2_section_info *abbrev_section)
2536 {
2537 struct objfile *objfile = dwarf2_per_objfile->objfile;
2538
2539 section->read (objfile);
2540 abbrev_section->read (objfile);
2541
2542 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2543 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2544
2545 htab_up sig_types_hash = allocate_signatured_type_table ();
2546
2547 for (uint32_t i = 0; i < map.tu_count; ++i)
2548 {
2549 struct signatured_type *sig_type;
2550 void **slot;
2551
2552 sect_offset sect_off
2553 = (sect_offset) (extract_unsigned_integer
2554 (map.tu_table_reordered + i * map.offset_size,
2555 map.offset_size,
2556 map.dwarf5_byte_order));
2557
2558 comp_unit_head cu_header;
2559 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2560 abbrev_section,
2561 section->buffer + to_underlying (sect_off),
2562 rcuh_kind::TYPE);
2563
2564 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2565 struct signatured_type);
2566 sig_type->signature = cu_header.signature;
2567 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2568 sig_type->per_cu.is_debug_types = 1;
2569 sig_type->per_cu.section = section;
2570 sig_type->per_cu.sect_off = sect_off;
2571 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2572 sig_type->per_cu.v.quick
2573 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2574 struct dwarf2_per_cu_quick_data);
2575
2576 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2577 *slot = sig_type;
2578
2579 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2580 }
2581
2582 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2583 }
2584
2585 /* Read the address map data from the mapped index, and use it to
2586 populate the objfile's psymtabs_addrmap. */
2587
2588 static void
2589 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2590 struct mapped_index *index)
2591 {
2592 struct objfile *objfile = dwarf2_per_objfile->objfile;
2593 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2594 const gdb_byte *iter, *end;
2595 struct addrmap *mutable_map;
2596 CORE_ADDR baseaddr;
2597
2598 auto_obstack temp_obstack;
2599
2600 mutable_map = addrmap_create_mutable (&temp_obstack);
2601
2602 iter = index->address_table.data ();
2603 end = iter + index->address_table.size ();
2604
2605 baseaddr = objfile->text_section_offset ();
2606
2607 while (iter < end)
2608 {
2609 ULONGEST hi, lo, cu_index;
2610 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2611 iter += 8;
2612 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2613 iter += 8;
2614 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2615 iter += 4;
2616
2617 if (lo > hi)
2618 {
2619 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2620 hex_string (lo), hex_string (hi));
2621 continue;
2622 }
2623
2624 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2625 {
2626 complaint (_(".gdb_index address table has invalid CU number %u"),
2627 (unsigned) cu_index);
2628 continue;
2629 }
2630
2631 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2632 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2633 addrmap_set_empty (mutable_map, lo, hi - 1,
2634 dwarf2_per_objfile->get_cu (cu_index));
2635 }
2636
2637 objfile->partial_symtabs->psymtabs_addrmap
2638 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2639 }
2640
2641 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2642 populate the objfile's psymtabs_addrmap. */
2643
2644 static void
2645 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2646 struct dwarf2_section_info *section)
2647 {
2648 struct objfile *objfile = dwarf2_per_objfile->objfile;
2649 bfd *abfd = objfile->obfd;
2650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2651 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2652
2653 auto_obstack temp_obstack;
2654 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2655
2656 std::unordered_map<sect_offset,
2657 dwarf2_per_cu_data *,
2658 gdb::hash_enum<sect_offset>>
2659 debug_info_offset_to_per_cu;
2660 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2661 {
2662 const auto insertpair
2663 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2664 if (!insertpair.second)
2665 {
2666 warning (_("Section .debug_aranges in %s has duplicate "
2667 "debug_info_offset %s, ignoring .debug_aranges."),
2668 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2669 return;
2670 }
2671 }
2672
2673 section->read (objfile);
2674
2675 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2676
2677 const gdb_byte *addr = section->buffer;
2678
2679 while (addr < section->buffer + section->size)
2680 {
2681 const gdb_byte *const entry_addr = addr;
2682 unsigned int bytes_read;
2683
2684 const LONGEST entry_length = read_initial_length (abfd, addr,
2685 &bytes_read);
2686 addr += bytes_read;
2687
2688 const gdb_byte *const entry_end = addr + entry_length;
2689 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2690 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2691 if (addr + entry_length > section->buffer + section->size)
2692 {
2693 warning (_("Section .debug_aranges in %s entry at offset %s "
2694 "length %s exceeds section length %s, "
2695 "ignoring .debug_aranges."),
2696 objfile_name (objfile),
2697 plongest (entry_addr - section->buffer),
2698 plongest (bytes_read + entry_length),
2699 pulongest (section->size));
2700 return;
2701 }
2702
2703 /* The version number. */
2704 const uint16_t version = read_2_bytes (abfd, addr);
2705 addr += 2;
2706 if (version != 2)
2707 {
2708 warning (_("Section .debug_aranges in %s entry at offset %s "
2709 "has unsupported version %d, ignoring .debug_aranges."),
2710 objfile_name (objfile),
2711 plongest (entry_addr - section->buffer), version);
2712 return;
2713 }
2714
2715 const uint64_t debug_info_offset
2716 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2717 addr += offset_size;
2718 const auto per_cu_it
2719 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2720 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2721 {
2722 warning (_("Section .debug_aranges in %s entry at offset %s "
2723 "debug_info_offset %s does not exists, "
2724 "ignoring .debug_aranges."),
2725 objfile_name (objfile),
2726 plongest (entry_addr - section->buffer),
2727 pulongest (debug_info_offset));
2728 return;
2729 }
2730 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2731
2732 const uint8_t address_size = *addr++;
2733 if (address_size < 1 || address_size > 8)
2734 {
2735 warning (_("Section .debug_aranges in %s entry at offset %s "
2736 "address_size %u is invalid, ignoring .debug_aranges."),
2737 objfile_name (objfile),
2738 plongest (entry_addr - section->buffer), address_size);
2739 return;
2740 }
2741
2742 const uint8_t segment_selector_size = *addr++;
2743 if (segment_selector_size != 0)
2744 {
2745 warning (_("Section .debug_aranges in %s entry at offset %s "
2746 "segment_selector_size %u is not supported, "
2747 "ignoring .debug_aranges."),
2748 objfile_name (objfile),
2749 plongest (entry_addr - section->buffer),
2750 segment_selector_size);
2751 return;
2752 }
2753
2754 /* Must pad to an alignment boundary that is twice the address
2755 size. It is undocumented by the DWARF standard but GCC does
2756 use it. */
2757 for (size_t padding = ((-(addr - section->buffer))
2758 & (2 * address_size - 1));
2759 padding > 0; padding--)
2760 if (*addr++ != 0)
2761 {
2762 warning (_("Section .debug_aranges in %s entry at offset %s "
2763 "padding is not zero, ignoring .debug_aranges."),
2764 objfile_name (objfile),
2765 plongest (entry_addr - section->buffer));
2766 return;
2767 }
2768
2769 for (;;)
2770 {
2771 if (addr + 2 * address_size > entry_end)
2772 {
2773 warning (_("Section .debug_aranges in %s entry at offset %s "
2774 "address list is not properly terminated, "
2775 "ignoring .debug_aranges."),
2776 objfile_name (objfile),
2777 plongest (entry_addr - section->buffer));
2778 return;
2779 }
2780 ULONGEST start = extract_unsigned_integer (addr, address_size,
2781 dwarf5_byte_order);
2782 addr += address_size;
2783 ULONGEST length = extract_unsigned_integer (addr, address_size,
2784 dwarf5_byte_order);
2785 addr += address_size;
2786 if (start == 0 && length == 0)
2787 break;
2788 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2789 {
2790 /* Symbol was eliminated due to a COMDAT group. */
2791 continue;
2792 }
2793 ULONGEST end = start + length;
2794 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2795 - baseaddr);
2796 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2797 - baseaddr);
2798 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2799 }
2800 }
2801
2802 objfile->partial_symtabs->psymtabs_addrmap
2803 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2804 }
2805
2806 /* Find a slot in the mapped index INDEX for the object named NAME.
2807 If NAME is found, set *VEC_OUT to point to the CU vector in the
2808 constant pool and return true. If NAME cannot be found, return
2809 false. */
2810
2811 static bool
2812 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2813 offset_type **vec_out)
2814 {
2815 offset_type hash;
2816 offset_type slot, step;
2817 int (*cmp) (const char *, const char *);
2818
2819 gdb::unique_xmalloc_ptr<char> without_params;
2820 if (current_language->la_language == language_cplus
2821 || current_language->la_language == language_fortran
2822 || current_language->la_language == language_d)
2823 {
2824 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2825 not contain any. */
2826
2827 if (strchr (name, '(') != NULL)
2828 {
2829 without_params = cp_remove_params (name);
2830
2831 if (without_params != NULL)
2832 name = without_params.get ();
2833 }
2834 }
2835
2836 /* Index version 4 did not support case insensitive searches. But the
2837 indices for case insensitive languages are built in lowercase, therefore
2838 simulate our NAME being searched is also lowercased. */
2839 hash = mapped_index_string_hash ((index->version == 4
2840 && case_sensitivity == case_sensitive_off
2841 ? 5 : index->version),
2842 name);
2843
2844 slot = hash & (index->symbol_table.size () - 1);
2845 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2846 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2847
2848 for (;;)
2849 {
2850 const char *str;
2851
2852 const auto &bucket = index->symbol_table[slot];
2853 if (bucket.name == 0 && bucket.vec == 0)
2854 return false;
2855
2856 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2857 if (!cmp (name, str))
2858 {
2859 *vec_out = (offset_type *) (index->constant_pool
2860 + MAYBE_SWAP (bucket.vec));
2861 return true;
2862 }
2863
2864 slot = (slot + step) & (index->symbol_table.size () - 1);
2865 }
2866 }
2867
2868 /* A helper function that reads the .gdb_index from BUFFER and fills
2869 in MAP. FILENAME is the name of the file containing the data;
2870 it is used for error reporting. DEPRECATED_OK is true if it is
2871 ok to use deprecated sections.
2872
2873 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2874 out parameters that are filled in with information about the CU and
2875 TU lists in the section.
2876
2877 Returns true if all went well, false otherwise. */
2878
2879 static bool
2880 read_gdb_index_from_buffer (struct objfile *objfile,
2881 const char *filename,
2882 bool deprecated_ok,
2883 gdb::array_view<const gdb_byte> buffer,
2884 struct mapped_index *map,
2885 const gdb_byte **cu_list,
2886 offset_type *cu_list_elements,
2887 const gdb_byte **types_list,
2888 offset_type *types_list_elements)
2889 {
2890 const gdb_byte *addr = &buffer[0];
2891
2892 /* Version check. */
2893 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2894 /* Versions earlier than 3 emitted every copy of a psymbol. This
2895 causes the index to behave very poorly for certain requests. Version 3
2896 contained incomplete addrmap. So, it seems better to just ignore such
2897 indices. */
2898 if (version < 4)
2899 {
2900 static int warning_printed = 0;
2901 if (!warning_printed)
2902 {
2903 warning (_("Skipping obsolete .gdb_index section in %s."),
2904 filename);
2905 warning_printed = 1;
2906 }
2907 return 0;
2908 }
2909 /* Index version 4 uses a different hash function than index version
2910 5 and later.
2911
2912 Versions earlier than 6 did not emit psymbols for inlined
2913 functions. Using these files will cause GDB not to be able to
2914 set breakpoints on inlined functions by name, so we ignore these
2915 indices unless the user has done
2916 "set use-deprecated-index-sections on". */
2917 if (version < 6 && !deprecated_ok)
2918 {
2919 static int warning_printed = 0;
2920 if (!warning_printed)
2921 {
2922 warning (_("\
2923 Skipping deprecated .gdb_index section in %s.\n\
2924 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2925 to use the section anyway."),
2926 filename);
2927 warning_printed = 1;
2928 }
2929 return 0;
2930 }
2931 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2932 of the TU (for symbols coming from TUs),
2933 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2934 Plus gold-generated indices can have duplicate entries for global symbols,
2935 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2936 These are just performance bugs, and we can't distinguish gdb-generated
2937 indices from gold-generated ones, so issue no warning here. */
2938
2939 /* Indexes with higher version than the one supported by GDB may be no
2940 longer backward compatible. */
2941 if (version > 8)
2942 return 0;
2943
2944 map->version = version;
2945
2946 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2947
2948 int i = 0;
2949 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2950 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2951 / 8);
2952 ++i;
2953
2954 *types_list = addr + MAYBE_SWAP (metadata[i]);
2955 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2956 - MAYBE_SWAP (metadata[i]))
2957 / 8);
2958 ++i;
2959
2960 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2961 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2962 map->address_table
2963 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2964 ++i;
2965
2966 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2967 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2968 map->symbol_table
2969 = gdb::array_view<mapped_index::symbol_table_slot>
2970 ((mapped_index::symbol_table_slot *) symbol_table,
2971 (mapped_index::symbol_table_slot *) symbol_table_end);
2972
2973 ++i;
2974 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2975
2976 return 1;
2977 }
2978
2979 /* Callback types for dwarf2_read_gdb_index. */
2980
2981 typedef gdb::function_view
2982 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2983 get_gdb_index_contents_ftype;
2984 typedef gdb::function_view
2985 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2986 get_gdb_index_contents_dwz_ftype;
2987
2988 /* Read .gdb_index. If everything went ok, initialize the "quick"
2989 elements of all the CUs and return 1. Otherwise, return 0. */
2990
2991 static int
2992 dwarf2_read_gdb_index
2993 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2994 get_gdb_index_contents_ftype get_gdb_index_contents,
2995 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2996 {
2997 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2998 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2999 struct dwz_file *dwz;
3000 struct objfile *objfile = dwarf2_per_objfile->objfile;
3001
3002 gdb::array_view<const gdb_byte> main_index_contents
3003 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3004
3005 if (main_index_contents.empty ())
3006 return 0;
3007
3008 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3009 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3010 use_deprecated_index_sections,
3011 main_index_contents, map.get (), &cu_list,
3012 &cu_list_elements, &types_list,
3013 &types_list_elements))
3014 return 0;
3015
3016 /* Don't use the index if it's empty. */
3017 if (map->symbol_table.empty ())
3018 return 0;
3019
3020 /* If there is a .dwz file, read it so we can get its CU list as
3021 well. */
3022 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3023 if (dwz != NULL)
3024 {
3025 struct mapped_index dwz_map;
3026 const gdb_byte *dwz_types_ignore;
3027 offset_type dwz_types_elements_ignore;
3028
3029 gdb::array_view<const gdb_byte> dwz_index_content
3030 = get_gdb_index_contents_dwz (objfile, dwz);
3031
3032 if (dwz_index_content.empty ())
3033 return 0;
3034
3035 if (!read_gdb_index_from_buffer (objfile,
3036 bfd_get_filename (dwz->dwz_bfd.get ()),
3037 1, dwz_index_content, &dwz_map,
3038 &dwz_list, &dwz_list_elements,
3039 &dwz_types_ignore,
3040 &dwz_types_elements_ignore))
3041 {
3042 warning (_("could not read '.gdb_index' section from %s; skipping"),
3043 bfd_get_filename (dwz->dwz_bfd.get ()));
3044 return 0;
3045 }
3046 }
3047
3048 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3049 dwz_list, dwz_list_elements);
3050
3051 if (types_list_elements)
3052 {
3053 /* We can only handle a single .debug_types when we have an
3054 index. */
3055 if (dwarf2_per_objfile->types.size () != 1)
3056 return 0;
3057
3058 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3059
3060 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3061 types_list, types_list_elements);
3062 }
3063
3064 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3065
3066 dwarf2_per_objfile->index_table = std::move (map);
3067 dwarf2_per_objfile->using_index = 1;
3068 dwarf2_per_objfile->quick_file_names_table =
3069 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3070
3071 return 1;
3072 }
3073
3074 /* die_reader_func for dw2_get_file_names. */
3075
3076 static void
3077 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3078 const gdb_byte *info_ptr,
3079 struct die_info *comp_unit_die)
3080 {
3081 struct dwarf2_cu *cu = reader->cu;
3082 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3083 struct dwarf2_per_objfile *dwarf2_per_objfile
3084 = cu->per_cu->dwarf2_per_objfile;
3085 struct objfile *objfile = dwarf2_per_objfile->objfile;
3086 struct dwarf2_per_cu_data *lh_cu;
3087 struct attribute *attr;
3088 void **slot;
3089 struct quick_file_names *qfn;
3090
3091 gdb_assert (! this_cu->is_debug_types);
3092
3093 /* Our callers never want to match partial units -- instead they
3094 will match the enclosing full CU. */
3095 if (comp_unit_die->tag == DW_TAG_partial_unit)
3096 {
3097 this_cu->v.quick->no_file_data = 1;
3098 return;
3099 }
3100
3101 lh_cu = this_cu;
3102 slot = NULL;
3103
3104 line_header_up lh;
3105 sect_offset line_offset {};
3106
3107 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3108 if (attr != nullptr)
3109 {
3110 struct quick_file_names find_entry;
3111
3112 line_offset = (sect_offset) DW_UNSND (attr);
3113
3114 /* We may have already read in this line header (TU line header sharing).
3115 If we have we're done. */
3116 find_entry.hash.dwo_unit = cu->dwo_unit;
3117 find_entry.hash.line_sect_off = line_offset;
3118 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3119 &find_entry, INSERT);
3120 if (*slot != NULL)
3121 {
3122 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3123 return;
3124 }
3125
3126 lh = dwarf_decode_line_header (line_offset, cu);
3127 }
3128 if (lh == NULL)
3129 {
3130 lh_cu->v.quick->no_file_data = 1;
3131 return;
3132 }
3133
3134 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3135 qfn->hash.dwo_unit = cu->dwo_unit;
3136 qfn->hash.line_sect_off = line_offset;
3137 gdb_assert (slot != NULL);
3138 *slot = qfn;
3139
3140 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3141
3142 int offset = 0;
3143 if (strcmp (fnd.name, "<unknown>") != 0)
3144 ++offset;
3145
3146 qfn->num_file_names = offset + lh->file_names_size ();
3147 qfn->file_names =
3148 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3149 if (offset != 0)
3150 qfn->file_names[0] = xstrdup (fnd.name);
3151 for (int i = 0; i < lh->file_names_size (); ++i)
3152 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3153 fnd.comp_dir).release ();
3154 qfn->real_names = NULL;
3155
3156 lh_cu->v.quick->file_names = qfn;
3157 }
3158
3159 /* A helper for the "quick" functions which attempts to read the line
3160 table for THIS_CU. */
3161
3162 static struct quick_file_names *
3163 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3164 {
3165 /* This should never be called for TUs. */
3166 gdb_assert (! this_cu->is_debug_types);
3167 /* Nor type unit groups. */
3168 gdb_assert (! this_cu->type_unit_group_p ());
3169
3170 if (this_cu->v.quick->file_names != NULL)
3171 return this_cu->v.quick->file_names;
3172 /* If we know there is no line data, no point in looking again. */
3173 if (this_cu->v.quick->no_file_data)
3174 return NULL;
3175
3176 cutu_reader reader (this_cu);
3177 if (!reader.dummy_p)
3178 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3179
3180 if (this_cu->v.quick->no_file_data)
3181 return NULL;
3182 return this_cu->v.quick->file_names;
3183 }
3184
3185 /* A helper for the "quick" functions which computes and caches the
3186 real path for a given file name from the line table. */
3187
3188 static const char *
3189 dw2_get_real_path (struct objfile *objfile,
3190 struct quick_file_names *qfn, int index)
3191 {
3192 if (qfn->real_names == NULL)
3193 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3194 qfn->num_file_names, const char *);
3195
3196 if (qfn->real_names[index] == NULL)
3197 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3198
3199 return qfn->real_names[index];
3200 }
3201
3202 static struct symtab *
3203 dw2_find_last_source_symtab (struct objfile *objfile)
3204 {
3205 struct dwarf2_per_objfile *dwarf2_per_objfile
3206 = get_dwarf2_per_objfile (objfile);
3207 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3208 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3209
3210 if (cust == NULL)
3211 return NULL;
3212
3213 return compunit_primary_filetab (cust);
3214 }
3215
3216 /* Traversal function for dw2_forget_cached_source_info. */
3217
3218 static int
3219 dw2_free_cached_file_names (void **slot, void *info)
3220 {
3221 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3222
3223 if (file_data->real_names)
3224 {
3225 int i;
3226
3227 for (i = 0; i < file_data->num_file_names; ++i)
3228 {
3229 xfree ((void*) file_data->real_names[i]);
3230 file_data->real_names[i] = NULL;
3231 }
3232 }
3233
3234 return 1;
3235 }
3236
3237 static void
3238 dw2_forget_cached_source_info (struct objfile *objfile)
3239 {
3240 struct dwarf2_per_objfile *dwarf2_per_objfile
3241 = get_dwarf2_per_objfile (objfile);
3242
3243 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3244 dw2_free_cached_file_names, NULL);
3245 }
3246
3247 /* Helper function for dw2_map_symtabs_matching_filename that expands
3248 the symtabs and calls the iterator. */
3249
3250 static int
3251 dw2_map_expand_apply (struct objfile *objfile,
3252 struct dwarf2_per_cu_data *per_cu,
3253 const char *name, const char *real_path,
3254 gdb::function_view<bool (symtab *)> callback)
3255 {
3256 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3257
3258 /* Don't visit already-expanded CUs. */
3259 if (per_cu->v.quick->compunit_symtab)
3260 return 0;
3261
3262 /* This may expand more than one symtab, and we want to iterate over
3263 all of them. */
3264 dw2_instantiate_symtab (per_cu, false);
3265
3266 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3267 last_made, callback);
3268 }
3269
3270 /* Implementation of the map_symtabs_matching_filename method. */
3271
3272 static bool
3273 dw2_map_symtabs_matching_filename
3274 (struct objfile *objfile, const char *name, const char *real_path,
3275 gdb::function_view<bool (symtab *)> callback)
3276 {
3277 const char *name_basename = lbasename (name);
3278 struct dwarf2_per_objfile *dwarf2_per_objfile
3279 = get_dwarf2_per_objfile (objfile);
3280
3281 /* The rule is CUs specify all the files, including those used by
3282 any TU, so there's no need to scan TUs here. */
3283
3284 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3285 {
3286 /* We only need to look at symtabs not already expanded. */
3287 if (per_cu->v.quick->compunit_symtab)
3288 continue;
3289
3290 quick_file_names *file_data = dw2_get_file_names (per_cu);
3291 if (file_data == NULL)
3292 continue;
3293
3294 for (int j = 0; j < file_data->num_file_names; ++j)
3295 {
3296 const char *this_name = file_data->file_names[j];
3297 const char *this_real_name;
3298
3299 if (compare_filenames_for_search (this_name, name))
3300 {
3301 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3302 callback))
3303 return true;
3304 continue;
3305 }
3306
3307 /* Before we invoke realpath, which can get expensive when many
3308 files are involved, do a quick comparison of the basenames. */
3309 if (! basenames_may_differ
3310 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3311 continue;
3312
3313 this_real_name = dw2_get_real_path (objfile, file_data, j);
3314 if (compare_filenames_for_search (this_real_name, name))
3315 {
3316 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3317 callback))
3318 return true;
3319 continue;
3320 }
3321
3322 if (real_path != NULL)
3323 {
3324 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3325 gdb_assert (IS_ABSOLUTE_PATH (name));
3326 if (this_real_name != NULL
3327 && FILENAME_CMP (real_path, this_real_name) == 0)
3328 {
3329 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3330 callback))
3331 return true;
3332 continue;
3333 }
3334 }
3335 }
3336 }
3337
3338 return false;
3339 }
3340
3341 /* Struct used to manage iterating over all CUs looking for a symbol. */
3342
3343 struct dw2_symtab_iterator
3344 {
3345 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3346 struct dwarf2_per_objfile *dwarf2_per_objfile;
3347 /* If set, only look for symbols that match that block. Valid values are
3348 GLOBAL_BLOCK and STATIC_BLOCK. */
3349 gdb::optional<block_enum> block_index;
3350 /* The kind of symbol we're looking for. */
3351 domain_enum domain;
3352 /* The list of CUs from the index entry of the symbol,
3353 or NULL if not found. */
3354 offset_type *vec;
3355 /* The next element in VEC to look at. */
3356 int next;
3357 /* The number of elements in VEC, or zero if there is no match. */
3358 int length;
3359 /* Have we seen a global version of the symbol?
3360 If so we can ignore all further global instances.
3361 This is to work around gold/15646, inefficient gold-generated
3362 indices. */
3363 int global_seen;
3364 };
3365
3366 /* Initialize the index symtab iterator ITER. */
3367
3368 static void
3369 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3370 struct dwarf2_per_objfile *dwarf2_per_objfile,
3371 gdb::optional<block_enum> block_index,
3372 domain_enum domain,
3373 const char *name)
3374 {
3375 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3376 iter->block_index = block_index;
3377 iter->domain = domain;
3378 iter->next = 0;
3379 iter->global_seen = 0;
3380
3381 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3382
3383 /* index is NULL if OBJF_READNOW. */
3384 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3385 iter->length = MAYBE_SWAP (*iter->vec);
3386 else
3387 {
3388 iter->vec = NULL;
3389 iter->length = 0;
3390 }
3391 }
3392
3393 /* Return the next matching CU or NULL if there are no more. */
3394
3395 static struct dwarf2_per_cu_data *
3396 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3397 {
3398 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3399
3400 for ( ; iter->next < iter->length; ++iter->next)
3401 {
3402 offset_type cu_index_and_attrs =
3403 MAYBE_SWAP (iter->vec[iter->next + 1]);
3404 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3405 gdb_index_symbol_kind symbol_kind =
3406 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3407 /* Only check the symbol attributes if they're present.
3408 Indices prior to version 7 don't record them,
3409 and indices >= 7 may elide them for certain symbols
3410 (gold does this). */
3411 int attrs_valid =
3412 (dwarf2_per_objfile->index_table->version >= 7
3413 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3414
3415 /* Don't crash on bad data. */
3416 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3417 + dwarf2_per_objfile->all_type_units.size ()))
3418 {
3419 complaint (_(".gdb_index entry has bad CU index"
3420 " [in module %s]"),
3421 objfile_name (dwarf2_per_objfile->objfile));
3422 continue;
3423 }
3424
3425 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3426
3427 /* Skip if already read in. */
3428 if (per_cu->v.quick->compunit_symtab)
3429 continue;
3430
3431 /* Check static vs global. */
3432 if (attrs_valid)
3433 {
3434 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3435
3436 if (iter->block_index.has_value ())
3437 {
3438 bool want_static = *iter->block_index == STATIC_BLOCK;
3439
3440 if (is_static != want_static)
3441 continue;
3442 }
3443
3444 /* Work around gold/15646. */
3445 if (!is_static && iter->global_seen)
3446 continue;
3447 if (!is_static)
3448 iter->global_seen = 1;
3449 }
3450
3451 /* Only check the symbol's kind if it has one. */
3452 if (attrs_valid)
3453 {
3454 switch (iter->domain)
3455 {
3456 case VAR_DOMAIN:
3457 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3458 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3459 /* Some types are also in VAR_DOMAIN. */
3460 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3461 continue;
3462 break;
3463 case STRUCT_DOMAIN:
3464 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3465 continue;
3466 break;
3467 case LABEL_DOMAIN:
3468 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3469 continue;
3470 break;
3471 case MODULE_DOMAIN:
3472 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3473 continue;
3474 break;
3475 default:
3476 break;
3477 }
3478 }
3479
3480 ++iter->next;
3481 return per_cu;
3482 }
3483
3484 return NULL;
3485 }
3486
3487 static struct compunit_symtab *
3488 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3489 const char *name, domain_enum domain)
3490 {
3491 struct compunit_symtab *stab_best = NULL;
3492 struct dwarf2_per_objfile *dwarf2_per_objfile
3493 = get_dwarf2_per_objfile (objfile);
3494
3495 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3496
3497 struct dw2_symtab_iterator iter;
3498 struct dwarf2_per_cu_data *per_cu;
3499
3500 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3501
3502 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3503 {
3504 struct symbol *sym, *with_opaque = NULL;
3505 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3506 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3507 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3508
3509 sym = block_find_symbol (block, name, domain,
3510 block_find_non_opaque_type_preferred,
3511 &with_opaque);
3512
3513 /* Some caution must be observed with overloaded functions
3514 and methods, since the index will not contain any overload
3515 information (but NAME might contain it). */
3516
3517 if (sym != NULL
3518 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3519 return stab;
3520 if (with_opaque != NULL
3521 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3522 stab_best = stab;
3523
3524 /* Keep looking through other CUs. */
3525 }
3526
3527 return stab_best;
3528 }
3529
3530 static void
3531 dw2_print_stats (struct objfile *objfile)
3532 {
3533 struct dwarf2_per_objfile *dwarf2_per_objfile
3534 = get_dwarf2_per_objfile (objfile);
3535 int total = (dwarf2_per_objfile->all_comp_units.size ()
3536 + dwarf2_per_objfile->all_type_units.size ());
3537 int count = 0;
3538
3539 for (int i = 0; i < total; ++i)
3540 {
3541 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3542
3543 if (!per_cu->v.quick->compunit_symtab)
3544 ++count;
3545 }
3546 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3547 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3548 }
3549
3550 /* This dumps minimal information about the index.
3551 It is called via "mt print objfiles".
3552 One use is to verify .gdb_index has been loaded by the
3553 gdb.dwarf2/gdb-index.exp testcase. */
3554
3555 static void
3556 dw2_dump (struct objfile *objfile)
3557 {
3558 struct dwarf2_per_objfile *dwarf2_per_objfile
3559 = get_dwarf2_per_objfile (objfile);
3560
3561 gdb_assert (dwarf2_per_objfile->using_index);
3562 printf_filtered (".gdb_index:");
3563 if (dwarf2_per_objfile->index_table != NULL)
3564 {
3565 printf_filtered (" version %d\n",
3566 dwarf2_per_objfile->index_table->version);
3567 }
3568 else
3569 printf_filtered (" faked for \"readnow\"\n");
3570 printf_filtered ("\n");
3571 }
3572
3573 static void
3574 dw2_expand_symtabs_for_function (struct objfile *objfile,
3575 const char *func_name)
3576 {
3577 struct dwarf2_per_objfile *dwarf2_per_objfile
3578 = get_dwarf2_per_objfile (objfile);
3579
3580 struct dw2_symtab_iterator iter;
3581 struct dwarf2_per_cu_data *per_cu;
3582
3583 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3584
3585 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3586 dw2_instantiate_symtab (per_cu, false);
3587
3588 }
3589
3590 static void
3591 dw2_expand_all_symtabs (struct objfile *objfile)
3592 {
3593 struct dwarf2_per_objfile *dwarf2_per_objfile
3594 = get_dwarf2_per_objfile (objfile);
3595 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3596 + dwarf2_per_objfile->all_type_units.size ());
3597
3598 for (int i = 0; i < total_units; ++i)
3599 {
3600 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3601
3602 /* We don't want to directly expand a partial CU, because if we
3603 read it with the wrong language, then assertion failures can
3604 be triggered later on. See PR symtab/23010. So, tell
3605 dw2_instantiate_symtab to skip partial CUs -- any important
3606 partial CU will be read via DW_TAG_imported_unit anyway. */
3607 dw2_instantiate_symtab (per_cu, true);
3608 }
3609 }
3610
3611 static void
3612 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3613 const char *fullname)
3614 {
3615 struct dwarf2_per_objfile *dwarf2_per_objfile
3616 = get_dwarf2_per_objfile (objfile);
3617
3618 /* We don't need to consider type units here.
3619 This is only called for examining code, e.g. expand_line_sal.
3620 There can be an order of magnitude (or more) more type units
3621 than comp units, and we avoid them if we can. */
3622
3623 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3624 {
3625 /* We only need to look at symtabs not already expanded. */
3626 if (per_cu->v.quick->compunit_symtab)
3627 continue;
3628
3629 quick_file_names *file_data = dw2_get_file_names (per_cu);
3630 if (file_data == NULL)
3631 continue;
3632
3633 for (int j = 0; j < file_data->num_file_names; ++j)
3634 {
3635 const char *this_fullname = file_data->file_names[j];
3636
3637 if (filename_cmp (this_fullname, fullname) == 0)
3638 {
3639 dw2_instantiate_symtab (per_cu, false);
3640 break;
3641 }
3642 }
3643 }
3644 }
3645
3646 static void
3647 dw2_map_matching_symbols
3648 (struct objfile *objfile,
3649 const lookup_name_info &name, domain_enum domain,
3650 int global,
3651 gdb::function_view<symbol_found_callback_ftype> callback,
3652 symbol_compare_ftype *ordered_compare)
3653 {
3654 /* Currently unimplemented; used for Ada. The function can be called if the
3655 current language is Ada for a non-Ada objfile using GNU index. As Ada
3656 does not look for non-Ada symbols this function should just return. */
3657 }
3658
3659 /* Starting from a search name, return the string that finds the upper
3660 bound of all strings that start with SEARCH_NAME in a sorted name
3661 list. Returns the empty string to indicate that the upper bound is
3662 the end of the list. */
3663
3664 static std::string
3665 make_sort_after_prefix_name (const char *search_name)
3666 {
3667 /* When looking to complete "func", we find the upper bound of all
3668 symbols that start with "func" by looking for where we'd insert
3669 the closest string that would follow "func" in lexicographical
3670 order. Usually, that's "func"-with-last-character-incremented,
3671 i.e. "fund". Mind non-ASCII characters, though. Usually those
3672 will be UTF-8 multi-byte sequences, but we can't be certain.
3673 Especially mind the 0xff character, which is a valid character in
3674 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3675 rule out compilers allowing it in identifiers. Note that
3676 conveniently, strcmp/strcasecmp are specified to compare
3677 characters interpreted as unsigned char. So what we do is treat
3678 the whole string as a base 256 number composed of a sequence of
3679 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3680 to 0, and carries 1 to the following more-significant position.
3681 If the very first character in SEARCH_NAME ends up incremented
3682 and carries/overflows, then the upper bound is the end of the
3683 list. The string after the empty string is also the empty
3684 string.
3685
3686 Some examples of this operation:
3687
3688 SEARCH_NAME => "+1" RESULT
3689
3690 "abc" => "abd"
3691 "ab\xff" => "ac"
3692 "\xff" "a" "\xff" => "\xff" "b"
3693 "\xff" => ""
3694 "\xff\xff" => ""
3695 "" => ""
3696
3697 Then, with these symbols for example:
3698
3699 func
3700 func1
3701 fund
3702
3703 completing "func" looks for symbols between "func" and
3704 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3705 which finds "func" and "func1", but not "fund".
3706
3707 And with:
3708
3709 funcÿ (Latin1 'ÿ' [0xff])
3710 funcÿ1
3711 fund
3712
3713 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3714 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3715
3716 And with:
3717
3718 ÿÿ (Latin1 'ÿ' [0xff])
3719 ÿÿ1
3720
3721 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3722 the end of the list.
3723 */
3724 std::string after = search_name;
3725 while (!after.empty () && (unsigned char) after.back () == 0xff)
3726 after.pop_back ();
3727 if (!after.empty ())
3728 after.back () = (unsigned char) after.back () + 1;
3729 return after;
3730 }
3731
3732 /* See declaration. */
3733
3734 std::pair<std::vector<name_component>::const_iterator,
3735 std::vector<name_component>::const_iterator>
3736 mapped_index_base::find_name_components_bounds
3737 (const lookup_name_info &lookup_name_without_params, language lang) const
3738 {
3739 auto *name_cmp
3740 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3741
3742 const char *lang_name
3743 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3744
3745 /* Comparison function object for lower_bound that matches against a
3746 given symbol name. */
3747 auto lookup_compare_lower = [&] (const name_component &elem,
3748 const char *name)
3749 {
3750 const char *elem_qualified = this->symbol_name_at (elem.idx);
3751 const char *elem_name = elem_qualified + elem.name_offset;
3752 return name_cmp (elem_name, name) < 0;
3753 };
3754
3755 /* Comparison function object for upper_bound that matches against a
3756 given symbol name. */
3757 auto lookup_compare_upper = [&] (const char *name,
3758 const name_component &elem)
3759 {
3760 const char *elem_qualified = this->symbol_name_at (elem.idx);
3761 const char *elem_name = elem_qualified + elem.name_offset;
3762 return name_cmp (name, elem_name) < 0;
3763 };
3764
3765 auto begin = this->name_components.begin ();
3766 auto end = this->name_components.end ();
3767
3768 /* Find the lower bound. */
3769 auto lower = [&] ()
3770 {
3771 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3772 return begin;
3773 else
3774 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3775 } ();
3776
3777 /* Find the upper bound. */
3778 auto upper = [&] ()
3779 {
3780 if (lookup_name_without_params.completion_mode ())
3781 {
3782 /* In completion mode, we want UPPER to point past all
3783 symbols names that have the same prefix. I.e., with
3784 these symbols, and completing "func":
3785
3786 function << lower bound
3787 function1
3788 other_function << upper bound
3789
3790 We find the upper bound by looking for the insertion
3791 point of "func"-with-last-character-incremented,
3792 i.e. "fund". */
3793 std::string after = make_sort_after_prefix_name (lang_name);
3794 if (after.empty ())
3795 return end;
3796 return std::lower_bound (lower, end, after.c_str (),
3797 lookup_compare_lower);
3798 }
3799 else
3800 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3801 } ();
3802
3803 return {lower, upper};
3804 }
3805
3806 /* See declaration. */
3807
3808 void
3809 mapped_index_base::build_name_components ()
3810 {
3811 if (!this->name_components.empty ())
3812 return;
3813
3814 this->name_components_casing = case_sensitivity;
3815 auto *name_cmp
3816 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3817
3818 /* The code below only knows how to break apart components of C++
3819 symbol names (and other languages that use '::' as
3820 namespace/module separator) and Ada symbol names. */
3821 auto count = this->symbol_name_count ();
3822 for (offset_type idx = 0; idx < count; idx++)
3823 {
3824 if (this->symbol_name_slot_invalid (idx))
3825 continue;
3826
3827 const char *name = this->symbol_name_at (idx);
3828
3829 /* Add each name component to the name component table. */
3830 unsigned int previous_len = 0;
3831
3832 if (strstr (name, "::") != nullptr)
3833 {
3834 for (unsigned int current_len = cp_find_first_component (name);
3835 name[current_len] != '\0';
3836 current_len += cp_find_first_component (name + current_len))
3837 {
3838 gdb_assert (name[current_len] == ':');
3839 this->name_components.push_back ({previous_len, idx});
3840 /* Skip the '::'. */
3841 current_len += 2;
3842 previous_len = current_len;
3843 }
3844 }
3845 else
3846 {
3847 /* Handle the Ada encoded (aka mangled) form here. */
3848 for (const char *iter = strstr (name, "__");
3849 iter != nullptr;
3850 iter = strstr (iter, "__"))
3851 {
3852 this->name_components.push_back ({previous_len, idx});
3853 iter += 2;
3854 previous_len = iter - name;
3855 }
3856 }
3857
3858 this->name_components.push_back ({previous_len, idx});
3859 }
3860
3861 /* Sort name_components elements by name. */
3862 auto name_comp_compare = [&] (const name_component &left,
3863 const name_component &right)
3864 {
3865 const char *left_qualified = this->symbol_name_at (left.idx);
3866 const char *right_qualified = this->symbol_name_at (right.idx);
3867
3868 const char *left_name = left_qualified + left.name_offset;
3869 const char *right_name = right_qualified + right.name_offset;
3870
3871 return name_cmp (left_name, right_name) < 0;
3872 };
3873
3874 std::sort (this->name_components.begin (),
3875 this->name_components.end (),
3876 name_comp_compare);
3877 }
3878
3879 /* Helper for dw2_expand_symtabs_matching that works with a
3880 mapped_index_base instead of the containing objfile. This is split
3881 to a separate function in order to be able to unit test the
3882 name_components matching using a mock mapped_index_base. For each
3883 symbol name that matches, calls MATCH_CALLBACK, passing it the
3884 symbol's index in the mapped_index_base symbol table. */
3885
3886 static void
3887 dw2_expand_symtabs_matching_symbol
3888 (mapped_index_base &index,
3889 const lookup_name_info &lookup_name_in,
3890 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3891 enum search_domain kind,
3892 gdb::function_view<bool (offset_type)> match_callback)
3893 {
3894 lookup_name_info lookup_name_without_params
3895 = lookup_name_in.make_ignore_params ();
3896
3897 /* Build the symbol name component sorted vector, if we haven't
3898 yet. */
3899 index.build_name_components ();
3900
3901 /* The same symbol may appear more than once in the range though.
3902 E.g., if we're looking for symbols that complete "w", and we have
3903 a symbol named "w1::w2", we'll find the two name components for
3904 that same symbol in the range. To be sure we only call the
3905 callback once per symbol, we first collect the symbol name
3906 indexes that matched in a temporary vector and ignore
3907 duplicates. */
3908 std::vector<offset_type> matches;
3909
3910 struct name_and_matcher
3911 {
3912 symbol_name_matcher_ftype *matcher;
3913 const std::string &name;
3914
3915 bool operator== (const name_and_matcher &other) const
3916 {
3917 return matcher == other.matcher && name == other.name;
3918 }
3919 };
3920
3921 /* A vector holding all the different symbol name matchers, for all
3922 languages. */
3923 std::vector<name_and_matcher> matchers;
3924
3925 for (int i = 0; i < nr_languages; i++)
3926 {
3927 enum language lang_e = (enum language) i;
3928
3929 const language_defn *lang = language_def (lang_e);
3930 symbol_name_matcher_ftype *name_matcher
3931 = get_symbol_name_matcher (lang, lookup_name_without_params);
3932
3933 name_and_matcher key {
3934 name_matcher,
3935 lookup_name_without_params.language_lookup_name (lang_e)
3936 };
3937
3938 /* Don't insert the same comparison routine more than once.
3939 Note that we do this linear walk. This is not a problem in
3940 practice because the number of supported languages is
3941 low. */
3942 if (std::find (matchers.begin (), matchers.end (), key)
3943 != matchers.end ())
3944 continue;
3945 matchers.push_back (std::move (key));
3946
3947 auto bounds
3948 = index.find_name_components_bounds (lookup_name_without_params,
3949 lang_e);
3950
3951 /* Now for each symbol name in range, check to see if we have a name
3952 match, and if so, call the MATCH_CALLBACK callback. */
3953
3954 for (; bounds.first != bounds.second; ++bounds.first)
3955 {
3956 const char *qualified = index.symbol_name_at (bounds.first->idx);
3957
3958 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3959 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3960 continue;
3961
3962 matches.push_back (bounds.first->idx);
3963 }
3964 }
3965
3966 std::sort (matches.begin (), matches.end ());
3967
3968 /* Finally call the callback, once per match. */
3969 ULONGEST prev = -1;
3970 for (offset_type idx : matches)
3971 {
3972 if (prev != idx)
3973 {
3974 if (!match_callback (idx))
3975 break;
3976 prev = idx;
3977 }
3978 }
3979
3980 /* Above we use a type wider than idx's for 'prev', since 0 and
3981 (offset_type)-1 are both possible values. */
3982 static_assert (sizeof (prev) > sizeof (offset_type), "");
3983 }
3984
3985 #if GDB_SELF_TEST
3986
3987 namespace selftests { namespace dw2_expand_symtabs_matching {
3988
3989 /* A mock .gdb_index/.debug_names-like name index table, enough to
3990 exercise dw2_expand_symtabs_matching_symbol, which works with the
3991 mapped_index_base interface. Builds an index from the symbol list
3992 passed as parameter to the constructor. */
3993 class mock_mapped_index : public mapped_index_base
3994 {
3995 public:
3996 mock_mapped_index (gdb::array_view<const char *> symbols)
3997 : m_symbol_table (symbols)
3998 {}
3999
4000 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4001
4002 /* Return the number of names in the symbol table. */
4003 size_t symbol_name_count () const override
4004 {
4005 return m_symbol_table.size ();
4006 }
4007
4008 /* Get the name of the symbol at IDX in the symbol table. */
4009 const char *symbol_name_at (offset_type idx) const override
4010 {
4011 return m_symbol_table[idx];
4012 }
4013
4014 private:
4015 gdb::array_view<const char *> m_symbol_table;
4016 };
4017
4018 /* Convenience function that converts a NULL pointer to a "<null>"
4019 string, to pass to print routines. */
4020
4021 static const char *
4022 string_or_null (const char *str)
4023 {
4024 return str != NULL ? str : "<null>";
4025 }
4026
4027 /* Check if a lookup_name_info built from
4028 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4029 index. EXPECTED_LIST is the list of expected matches, in expected
4030 matching order. If no match expected, then an empty list is
4031 specified. Returns true on success. On failure prints a warning
4032 indicating the file:line that failed, and returns false. */
4033
4034 static bool
4035 check_match (const char *file, int line,
4036 mock_mapped_index &mock_index,
4037 const char *name, symbol_name_match_type match_type,
4038 bool completion_mode,
4039 std::initializer_list<const char *> expected_list)
4040 {
4041 lookup_name_info lookup_name (name, match_type, completion_mode);
4042
4043 bool matched = true;
4044
4045 auto mismatch = [&] (const char *expected_str,
4046 const char *got)
4047 {
4048 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4049 "expected=\"%s\", got=\"%s\"\n"),
4050 file, line,
4051 (match_type == symbol_name_match_type::FULL
4052 ? "FULL" : "WILD"),
4053 name, string_or_null (expected_str), string_or_null (got));
4054 matched = false;
4055 };
4056
4057 auto expected_it = expected_list.begin ();
4058 auto expected_end = expected_list.end ();
4059
4060 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4061 NULL, ALL_DOMAIN,
4062 [&] (offset_type idx)
4063 {
4064 const char *matched_name = mock_index.symbol_name_at (idx);
4065 const char *expected_str
4066 = expected_it == expected_end ? NULL : *expected_it++;
4067
4068 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4069 mismatch (expected_str, matched_name);
4070 return true;
4071 });
4072
4073 const char *expected_str
4074 = expected_it == expected_end ? NULL : *expected_it++;
4075 if (expected_str != NULL)
4076 mismatch (expected_str, NULL);
4077
4078 return matched;
4079 }
4080
4081 /* The symbols added to the mock mapped_index for testing (in
4082 canonical form). */
4083 static const char *test_symbols[] = {
4084 "function",
4085 "std::bar",
4086 "std::zfunction",
4087 "std::zfunction2",
4088 "w1::w2",
4089 "ns::foo<char*>",
4090 "ns::foo<int>",
4091 "ns::foo<long>",
4092 "ns2::tmpl<int>::foo2",
4093 "(anonymous namespace)::A::B::C",
4094
4095 /* These are used to check that the increment-last-char in the
4096 matching algorithm for completion doesn't match "t1_fund" when
4097 completing "t1_func". */
4098 "t1_func",
4099 "t1_func1",
4100 "t1_fund",
4101 "t1_fund1",
4102
4103 /* A UTF-8 name with multi-byte sequences to make sure that
4104 cp-name-parser understands this as a single identifier ("função"
4105 is "function" in PT). */
4106 u8"u8função",
4107
4108 /* \377 (0xff) is Latin1 'ÿ'. */
4109 "yfunc\377",
4110
4111 /* \377 (0xff) is Latin1 'ÿ'. */
4112 "\377",
4113 "\377\377123",
4114
4115 /* A name with all sorts of complications. Starts with "z" to make
4116 it easier for the completion tests below. */
4117 #define Z_SYM_NAME \
4118 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4119 "::tuple<(anonymous namespace)::ui*, " \
4120 "std::default_delete<(anonymous namespace)::ui>, void>"
4121
4122 Z_SYM_NAME
4123 };
4124
4125 /* Returns true if the mapped_index_base::find_name_component_bounds
4126 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4127 in completion mode. */
4128
4129 static bool
4130 check_find_bounds_finds (mapped_index_base &index,
4131 const char *search_name,
4132 gdb::array_view<const char *> expected_syms)
4133 {
4134 lookup_name_info lookup_name (search_name,
4135 symbol_name_match_type::FULL, true);
4136
4137 auto bounds = index.find_name_components_bounds (lookup_name,
4138 language_cplus);
4139
4140 size_t distance = std::distance (bounds.first, bounds.second);
4141 if (distance != expected_syms.size ())
4142 return false;
4143
4144 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4145 {
4146 auto nc_elem = bounds.first + exp_elem;
4147 const char *qualified = index.symbol_name_at (nc_elem->idx);
4148 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4149 return false;
4150 }
4151
4152 return true;
4153 }
4154
4155 /* Test the lower-level mapped_index::find_name_component_bounds
4156 method. */
4157
4158 static void
4159 test_mapped_index_find_name_component_bounds ()
4160 {
4161 mock_mapped_index mock_index (test_symbols);
4162
4163 mock_index.build_name_components ();
4164
4165 /* Test the lower-level mapped_index::find_name_component_bounds
4166 method in completion mode. */
4167 {
4168 static const char *expected_syms[] = {
4169 "t1_func",
4170 "t1_func1",
4171 };
4172
4173 SELF_CHECK (check_find_bounds_finds (mock_index,
4174 "t1_func", expected_syms));
4175 }
4176
4177 /* Check that the increment-last-char in the name matching algorithm
4178 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4179 {
4180 static const char *expected_syms1[] = {
4181 "\377",
4182 "\377\377123",
4183 };
4184 SELF_CHECK (check_find_bounds_finds (mock_index,
4185 "\377", expected_syms1));
4186
4187 static const char *expected_syms2[] = {
4188 "\377\377123",
4189 };
4190 SELF_CHECK (check_find_bounds_finds (mock_index,
4191 "\377\377", expected_syms2));
4192 }
4193 }
4194
4195 /* Test dw2_expand_symtabs_matching_symbol. */
4196
4197 static void
4198 test_dw2_expand_symtabs_matching_symbol ()
4199 {
4200 mock_mapped_index mock_index (test_symbols);
4201
4202 /* We let all tests run until the end even if some fails, for debug
4203 convenience. */
4204 bool any_mismatch = false;
4205
4206 /* Create the expected symbols list (an initializer_list). Needed
4207 because lists have commas, and we need to pass them to CHECK,
4208 which is a macro. */
4209 #define EXPECT(...) { __VA_ARGS__ }
4210
4211 /* Wrapper for check_match that passes down the current
4212 __FILE__/__LINE__. */
4213 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4214 any_mismatch |= !check_match (__FILE__, __LINE__, \
4215 mock_index, \
4216 NAME, MATCH_TYPE, COMPLETION_MODE, \
4217 EXPECTED_LIST)
4218
4219 /* Identity checks. */
4220 for (const char *sym : test_symbols)
4221 {
4222 /* Should be able to match all existing symbols. */
4223 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4224 EXPECT (sym));
4225
4226 /* Should be able to match all existing symbols with
4227 parameters. */
4228 std::string with_params = std::string (sym) + "(int)";
4229 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4230 EXPECT (sym));
4231
4232 /* Should be able to match all existing symbols with
4233 parameters and qualifiers. */
4234 with_params = std::string (sym) + " ( int ) const";
4235 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4236 EXPECT (sym));
4237
4238 /* This should really find sym, but cp-name-parser.y doesn't
4239 know about lvalue/rvalue qualifiers yet. */
4240 with_params = std::string (sym) + " ( int ) &&";
4241 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4242 {});
4243 }
4244
4245 /* Check that the name matching algorithm for completion doesn't get
4246 confused with Latin1 'ÿ' / 0xff. */
4247 {
4248 static const char str[] = "\377";
4249 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4250 EXPECT ("\377", "\377\377123"));
4251 }
4252
4253 /* Check that the increment-last-char in the matching algorithm for
4254 completion doesn't match "t1_fund" when completing "t1_func". */
4255 {
4256 static const char str[] = "t1_func";
4257 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4258 EXPECT ("t1_func", "t1_func1"));
4259 }
4260
4261 /* Check that completion mode works at each prefix of the expected
4262 symbol name. */
4263 {
4264 static const char str[] = "function(int)";
4265 size_t len = strlen (str);
4266 std::string lookup;
4267
4268 for (size_t i = 1; i < len; i++)
4269 {
4270 lookup.assign (str, i);
4271 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4272 EXPECT ("function"));
4273 }
4274 }
4275
4276 /* While "w" is a prefix of both components, the match function
4277 should still only be called once. */
4278 {
4279 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4280 EXPECT ("w1::w2"));
4281 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4282 EXPECT ("w1::w2"));
4283 }
4284
4285 /* Same, with a "complicated" symbol. */
4286 {
4287 static const char str[] = Z_SYM_NAME;
4288 size_t len = strlen (str);
4289 std::string lookup;
4290
4291 for (size_t i = 1; i < len; i++)
4292 {
4293 lookup.assign (str, i);
4294 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4295 EXPECT (Z_SYM_NAME));
4296 }
4297 }
4298
4299 /* In FULL mode, an incomplete symbol doesn't match. */
4300 {
4301 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4302 {});
4303 }
4304
4305 /* A complete symbol with parameters matches any overload, since the
4306 index has no overload info. */
4307 {
4308 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4309 EXPECT ("std::zfunction", "std::zfunction2"));
4310 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4311 EXPECT ("std::zfunction", "std::zfunction2"));
4312 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4313 EXPECT ("std::zfunction", "std::zfunction2"));
4314 }
4315
4316 /* Check that whitespace is ignored appropriately. A symbol with a
4317 template argument list. */
4318 {
4319 static const char expected[] = "ns::foo<int>";
4320 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4321 EXPECT (expected));
4322 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4323 EXPECT (expected));
4324 }
4325
4326 /* Check that whitespace is ignored appropriately. A symbol with a
4327 template argument list that includes a pointer. */
4328 {
4329 static const char expected[] = "ns::foo<char*>";
4330 /* Try both completion and non-completion modes. */
4331 static const bool completion_mode[2] = {false, true};
4332 for (size_t i = 0; i < 2; i++)
4333 {
4334 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4335 completion_mode[i], EXPECT (expected));
4336 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4337 completion_mode[i], EXPECT (expected));
4338
4339 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4340 completion_mode[i], EXPECT (expected));
4341 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4342 completion_mode[i], EXPECT (expected));
4343 }
4344 }
4345
4346 {
4347 /* Check method qualifiers are ignored. */
4348 static const char expected[] = "ns::foo<char*>";
4349 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4350 symbol_name_match_type::FULL, true, EXPECT (expected));
4351 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4352 symbol_name_match_type::FULL, true, EXPECT (expected));
4353 CHECK_MATCH ("foo < char * > ( int ) const",
4354 symbol_name_match_type::WILD, true, EXPECT (expected));
4355 CHECK_MATCH ("foo < char * > ( int ) &&",
4356 symbol_name_match_type::WILD, true, EXPECT (expected));
4357 }
4358
4359 /* Test lookup names that don't match anything. */
4360 {
4361 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4362 {});
4363
4364 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4365 {});
4366 }
4367
4368 /* Some wild matching tests, exercising "(anonymous namespace)",
4369 which should not be confused with a parameter list. */
4370 {
4371 static const char *syms[] = {
4372 "A::B::C",
4373 "B::C",
4374 "C",
4375 "A :: B :: C ( int )",
4376 "B :: C ( int )",
4377 "C ( int )",
4378 };
4379
4380 for (const char *s : syms)
4381 {
4382 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4383 EXPECT ("(anonymous namespace)::A::B::C"));
4384 }
4385 }
4386
4387 {
4388 static const char expected[] = "ns2::tmpl<int>::foo2";
4389 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4390 EXPECT (expected));
4391 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4392 EXPECT (expected));
4393 }
4394
4395 SELF_CHECK (!any_mismatch);
4396
4397 #undef EXPECT
4398 #undef CHECK_MATCH
4399 }
4400
4401 static void
4402 run_test ()
4403 {
4404 test_mapped_index_find_name_component_bounds ();
4405 test_dw2_expand_symtabs_matching_symbol ();
4406 }
4407
4408 }} // namespace selftests::dw2_expand_symtabs_matching
4409
4410 #endif /* GDB_SELF_TEST */
4411
4412 /* If FILE_MATCHER is NULL or if PER_CU has
4413 dwarf2_per_cu_quick_data::MARK set (see
4414 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4415 EXPANSION_NOTIFY on it. */
4416
4417 static void
4418 dw2_expand_symtabs_matching_one
4419 (struct dwarf2_per_cu_data *per_cu,
4420 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4421 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4422 {
4423 if (file_matcher == NULL || per_cu->v.quick->mark)
4424 {
4425 bool symtab_was_null
4426 = (per_cu->v.quick->compunit_symtab == NULL);
4427
4428 dw2_instantiate_symtab (per_cu, false);
4429
4430 if (expansion_notify != NULL
4431 && symtab_was_null
4432 && per_cu->v.quick->compunit_symtab != NULL)
4433 expansion_notify (per_cu->v.quick->compunit_symtab);
4434 }
4435 }
4436
4437 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4438 matched, to expand corresponding CUs that were marked. IDX is the
4439 index of the symbol name that matched. */
4440
4441 static void
4442 dw2_expand_marked_cus
4443 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4444 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4445 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4446 search_domain kind)
4447 {
4448 offset_type *vec, vec_len, vec_idx;
4449 bool global_seen = false;
4450 mapped_index &index = *dwarf2_per_objfile->index_table;
4451
4452 vec = (offset_type *) (index.constant_pool
4453 + MAYBE_SWAP (index.symbol_table[idx].vec));
4454 vec_len = MAYBE_SWAP (vec[0]);
4455 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4456 {
4457 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4458 /* This value is only valid for index versions >= 7. */
4459 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4460 gdb_index_symbol_kind symbol_kind =
4461 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4462 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4463 /* Only check the symbol attributes if they're present.
4464 Indices prior to version 7 don't record them,
4465 and indices >= 7 may elide them for certain symbols
4466 (gold does this). */
4467 int attrs_valid =
4468 (index.version >= 7
4469 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4470
4471 /* Work around gold/15646. */
4472 if (attrs_valid)
4473 {
4474 if (!is_static && global_seen)
4475 continue;
4476 if (!is_static)
4477 global_seen = true;
4478 }
4479
4480 /* Only check the symbol's kind if it has one. */
4481 if (attrs_valid)
4482 {
4483 switch (kind)
4484 {
4485 case VARIABLES_DOMAIN:
4486 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4487 continue;
4488 break;
4489 case FUNCTIONS_DOMAIN:
4490 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4491 continue;
4492 break;
4493 case TYPES_DOMAIN:
4494 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4495 continue;
4496 break;
4497 case MODULES_DOMAIN:
4498 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4499 continue;
4500 break;
4501 default:
4502 break;
4503 }
4504 }
4505
4506 /* Don't crash on bad data. */
4507 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4508 + dwarf2_per_objfile->all_type_units.size ()))
4509 {
4510 complaint (_(".gdb_index entry has bad CU index"
4511 " [in module %s]"),
4512 objfile_name (dwarf2_per_objfile->objfile));
4513 continue;
4514 }
4515
4516 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4517 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4518 expansion_notify);
4519 }
4520 }
4521
4522 /* If FILE_MATCHER is non-NULL, set all the
4523 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4524 that match FILE_MATCHER. */
4525
4526 static void
4527 dw_expand_symtabs_matching_file_matcher
4528 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4529 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4530 {
4531 if (file_matcher == NULL)
4532 return;
4533
4534 objfile *const objfile = dwarf2_per_objfile->objfile;
4535
4536 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4537 htab_eq_pointer,
4538 NULL, xcalloc, xfree));
4539 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4540 htab_eq_pointer,
4541 NULL, xcalloc, xfree));
4542
4543 /* The rule is CUs specify all the files, including those used by
4544 any TU, so there's no need to scan TUs here. */
4545
4546 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4547 {
4548 QUIT;
4549
4550 per_cu->v.quick->mark = 0;
4551
4552 /* We only need to look at symtabs not already expanded. */
4553 if (per_cu->v.quick->compunit_symtab)
4554 continue;
4555
4556 quick_file_names *file_data = dw2_get_file_names (per_cu);
4557 if (file_data == NULL)
4558 continue;
4559
4560 if (htab_find (visited_not_found.get (), file_data) != NULL)
4561 continue;
4562 else if (htab_find (visited_found.get (), file_data) != NULL)
4563 {
4564 per_cu->v.quick->mark = 1;
4565 continue;
4566 }
4567
4568 for (int j = 0; j < file_data->num_file_names; ++j)
4569 {
4570 const char *this_real_name;
4571
4572 if (file_matcher (file_data->file_names[j], false))
4573 {
4574 per_cu->v.quick->mark = 1;
4575 break;
4576 }
4577
4578 /* Before we invoke realpath, which can get expensive when many
4579 files are involved, do a quick comparison of the basenames. */
4580 if (!basenames_may_differ
4581 && !file_matcher (lbasename (file_data->file_names[j]),
4582 true))
4583 continue;
4584
4585 this_real_name = dw2_get_real_path (objfile, file_data, j);
4586 if (file_matcher (this_real_name, false))
4587 {
4588 per_cu->v.quick->mark = 1;
4589 break;
4590 }
4591 }
4592
4593 void **slot = htab_find_slot (per_cu->v.quick->mark
4594 ? visited_found.get ()
4595 : visited_not_found.get (),
4596 file_data, INSERT);
4597 *slot = file_data;
4598 }
4599 }
4600
4601 static void
4602 dw2_expand_symtabs_matching
4603 (struct objfile *objfile,
4604 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4605 const lookup_name_info &lookup_name,
4606 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4607 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4608 enum search_domain kind)
4609 {
4610 struct dwarf2_per_objfile *dwarf2_per_objfile
4611 = get_dwarf2_per_objfile (objfile);
4612
4613 /* index_table is NULL if OBJF_READNOW. */
4614 if (!dwarf2_per_objfile->index_table)
4615 return;
4616
4617 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4618
4619 mapped_index &index = *dwarf2_per_objfile->index_table;
4620
4621 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4622 symbol_matcher,
4623 kind, [&] (offset_type idx)
4624 {
4625 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4626 expansion_notify, kind);
4627 return true;
4628 });
4629 }
4630
4631 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4632 symtab. */
4633
4634 static struct compunit_symtab *
4635 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4636 CORE_ADDR pc)
4637 {
4638 int i;
4639
4640 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4641 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4642 return cust;
4643
4644 if (cust->includes == NULL)
4645 return NULL;
4646
4647 for (i = 0; cust->includes[i]; ++i)
4648 {
4649 struct compunit_symtab *s = cust->includes[i];
4650
4651 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4652 if (s != NULL)
4653 return s;
4654 }
4655
4656 return NULL;
4657 }
4658
4659 static struct compunit_symtab *
4660 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4661 struct bound_minimal_symbol msymbol,
4662 CORE_ADDR pc,
4663 struct obj_section *section,
4664 int warn_if_readin)
4665 {
4666 struct dwarf2_per_cu_data *data;
4667 struct compunit_symtab *result;
4668
4669 if (!objfile->partial_symtabs->psymtabs_addrmap)
4670 return NULL;
4671
4672 CORE_ADDR baseaddr = objfile->text_section_offset ();
4673 data = (struct dwarf2_per_cu_data *) addrmap_find
4674 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4675 if (!data)
4676 return NULL;
4677
4678 if (warn_if_readin && data->v.quick->compunit_symtab)
4679 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4680 paddress (get_objfile_arch (objfile), pc));
4681
4682 result
4683 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4684 false),
4685 pc);
4686 gdb_assert (result != NULL);
4687 return result;
4688 }
4689
4690 static void
4691 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4692 void *data, int need_fullname)
4693 {
4694 struct dwarf2_per_objfile *dwarf2_per_objfile
4695 = get_dwarf2_per_objfile (objfile);
4696
4697 if (!dwarf2_per_objfile->filenames_cache)
4698 {
4699 dwarf2_per_objfile->filenames_cache.emplace ();
4700
4701 htab_up visited (htab_create_alloc (10,
4702 htab_hash_pointer, htab_eq_pointer,
4703 NULL, xcalloc, xfree));
4704
4705 /* The rule is CUs specify all the files, including those used
4706 by any TU, so there's no need to scan TUs here. We can
4707 ignore file names coming from already-expanded CUs. */
4708
4709 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4710 {
4711 if (per_cu->v.quick->compunit_symtab)
4712 {
4713 void **slot = htab_find_slot (visited.get (),
4714 per_cu->v.quick->file_names,
4715 INSERT);
4716
4717 *slot = per_cu->v.quick->file_names;
4718 }
4719 }
4720
4721 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4722 {
4723 /* We only need to look at symtabs not already expanded. */
4724 if (per_cu->v.quick->compunit_symtab)
4725 continue;
4726
4727 quick_file_names *file_data = dw2_get_file_names (per_cu);
4728 if (file_data == NULL)
4729 continue;
4730
4731 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4732 if (*slot)
4733 {
4734 /* Already visited. */
4735 continue;
4736 }
4737 *slot = file_data;
4738
4739 for (int j = 0; j < file_data->num_file_names; ++j)
4740 {
4741 const char *filename = file_data->file_names[j];
4742 dwarf2_per_objfile->filenames_cache->seen (filename);
4743 }
4744 }
4745 }
4746
4747 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4748 {
4749 gdb::unique_xmalloc_ptr<char> this_real_name;
4750
4751 if (need_fullname)
4752 this_real_name = gdb_realpath (filename);
4753 (*fun) (filename, this_real_name.get (), data);
4754 });
4755 }
4756
4757 static int
4758 dw2_has_symbols (struct objfile *objfile)
4759 {
4760 return 1;
4761 }
4762
4763 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4764 {
4765 dw2_has_symbols,
4766 dw2_find_last_source_symtab,
4767 dw2_forget_cached_source_info,
4768 dw2_map_symtabs_matching_filename,
4769 dw2_lookup_symbol,
4770 dw2_print_stats,
4771 dw2_dump,
4772 dw2_expand_symtabs_for_function,
4773 dw2_expand_all_symtabs,
4774 dw2_expand_symtabs_with_fullname,
4775 dw2_map_matching_symbols,
4776 dw2_expand_symtabs_matching,
4777 dw2_find_pc_sect_compunit_symtab,
4778 NULL,
4779 dw2_map_symbol_filenames
4780 };
4781
4782 /* DWARF-5 debug_names reader. */
4783
4784 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4785 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4786
4787 /* A helper function that reads the .debug_names section in SECTION
4788 and fills in MAP. FILENAME is the name of the file containing the
4789 section; it is used for error reporting.
4790
4791 Returns true if all went well, false otherwise. */
4792
4793 static bool
4794 read_debug_names_from_section (struct objfile *objfile,
4795 const char *filename,
4796 struct dwarf2_section_info *section,
4797 mapped_debug_names &map)
4798 {
4799 if (section->empty ())
4800 return false;
4801
4802 /* Older elfutils strip versions could keep the section in the main
4803 executable while splitting it for the separate debug info file. */
4804 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4805 return false;
4806
4807 section->read (objfile);
4808
4809 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4810
4811 const gdb_byte *addr = section->buffer;
4812
4813 bfd *const abfd = section->get_bfd_owner ();
4814
4815 unsigned int bytes_read;
4816 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4817 addr += bytes_read;
4818
4819 map.dwarf5_is_dwarf64 = bytes_read != 4;
4820 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4821 if (bytes_read + length != section->size)
4822 {
4823 /* There may be multiple per-CU indices. */
4824 warning (_("Section .debug_names in %s length %s does not match "
4825 "section length %s, ignoring .debug_names."),
4826 filename, plongest (bytes_read + length),
4827 pulongest (section->size));
4828 return false;
4829 }
4830
4831 /* The version number. */
4832 uint16_t version = read_2_bytes (abfd, addr);
4833 addr += 2;
4834 if (version != 5)
4835 {
4836 warning (_("Section .debug_names in %s has unsupported version %d, "
4837 "ignoring .debug_names."),
4838 filename, version);
4839 return false;
4840 }
4841
4842 /* Padding. */
4843 uint16_t padding = read_2_bytes (abfd, addr);
4844 addr += 2;
4845 if (padding != 0)
4846 {
4847 warning (_("Section .debug_names in %s has unsupported padding %d, "
4848 "ignoring .debug_names."),
4849 filename, padding);
4850 return false;
4851 }
4852
4853 /* comp_unit_count - The number of CUs in the CU list. */
4854 map.cu_count = read_4_bytes (abfd, addr);
4855 addr += 4;
4856
4857 /* local_type_unit_count - The number of TUs in the local TU
4858 list. */
4859 map.tu_count = read_4_bytes (abfd, addr);
4860 addr += 4;
4861
4862 /* foreign_type_unit_count - The number of TUs in the foreign TU
4863 list. */
4864 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4865 addr += 4;
4866 if (foreign_tu_count != 0)
4867 {
4868 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4869 "ignoring .debug_names."),
4870 filename, static_cast<unsigned long> (foreign_tu_count));
4871 return false;
4872 }
4873
4874 /* bucket_count - The number of hash buckets in the hash lookup
4875 table. */
4876 map.bucket_count = read_4_bytes (abfd, addr);
4877 addr += 4;
4878
4879 /* name_count - The number of unique names in the index. */
4880 map.name_count = read_4_bytes (abfd, addr);
4881 addr += 4;
4882
4883 /* abbrev_table_size - The size in bytes of the abbreviations
4884 table. */
4885 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4886 addr += 4;
4887
4888 /* augmentation_string_size - The size in bytes of the augmentation
4889 string. This value is rounded up to a multiple of 4. */
4890 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4891 addr += 4;
4892 map.augmentation_is_gdb = ((augmentation_string_size
4893 == sizeof (dwarf5_augmentation))
4894 && memcmp (addr, dwarf5_augmentation,
4895 sizeof (dwarf5_augmentation)) == 0);
4896 augmentation_string_size += (-augmentation_string_size) & 3;
4897 addr += augmentation_string_size;
4898
4899 /* List of CUs */
4900 map.cu_table_reordered = addr;
4901 addr += map.cu_count * map.offset_size;
4902
4903 /* List of Local TUs */
4904 map.tu_table_reordered = addr;
4905 addr += map.tu_count * map.offset_size;
4906
4907 /* Hash Lookup Table */
4908 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4909 addr += map.bucket_count * 4;
4910 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4911 addr += map.name_count * 4;
4912
4913 /* Name Table */
4914 map.name_table_string_offs_reordered = addr;
4915 addr += map.name_count * map.offset_size;
4916 map.name_table_entry_offs_reordered = addr;
4917 addr += map.name_count * map.offset_size;
4918
4919 const gdb_byte *abbrev_table_start = addr;
4920 for (;;)
4921 {
4922 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4923 addr += bytes_read;
4924 if (index_num == 0)
4925 break;
4926
4927 const auto insertpair
4928 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4929 if (!insertpair.second)
4930 {
4931 warning (_("Section .debug_names in %s has duplicate index %s, "
4932 "ignoring .debug_names."),
4933 filename, pulongest (index_num));
4934 return false;
4935 }
4936 mapped_debug_names::index_val &indexval = insertpair.first->second;
4937 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4938 addr += bytes_read;
4939
4940 for (;;)
4941 {
4942 mapped_debug_names::index_val::attr attr;
4943 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4944 addr += bytes_read;
4945 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4946 addr += bytes_read;
4947 if (attr.form == DW_FORM_implicit_const)
4948 {
4949 attr.implicit_const = read_signed_leb128 (abfd, addr,
4950 &bytes_read);
4951 addr += bytes_read;
4952 }
4953 if (attr.dw_idx == 0 && attr.form == 0)
4954 break;
4955 indexval.attr_vec.push_back (std::move (attr));
4956 }
4957 }
4958 if (addr != abbrev_table_start + abbrev_table_size)
4959 {
4960 warning (_("Section .debug_names in %s has abbreviation_table "
4961 "of size %s vs. written as %u, ignoring .debug_names."),
4962 filename, plongest (addr - abbrev_table_start),
4963 abbrev_table_size);
4964 return false;
4965 }
4966 map.entry_pool = addr;
4967
4968 return true;
4969 }
4970
4971 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4972 list. */
4973
4974 static void
4975 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4976 const mapped_debug_names &map,
4977 dwarf2_section_info &section,
4978 bool is_dwz)
4979 {
4980 sect_offset sect_off_prev;
4981 for (uint32_t i = 0; i <= map.cu_count; ++i)
4982 {
4983 sect_offset sect_off_next;
4984 if (i < map.cu_count)
4985 {
4986 sect_off_next
4987 = (sect_offset) (extract_unsigned_integer
4988 (map.cu_table_reordered + i * map.offset_size,
4989 map.offset_size,
4990 map.dwarf5_byte_order));
4991 }
4992 else
4993 sect_off_next = (sect_offset) section.size;
4994 if (i >= 1)
4995 {
4996 const ULONGEST length = sect_off_next - sect_off_prev;
4997 dwarf2_per_cu_data *per_cu
4998 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4999 sect_off_prev, length);
5000 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5001 }
5002 sect_off_prev = sect_off_next;
5003 }
5004 }
5005
5006 /* Read the CU list from the mapped index, and use it to create all
5007 the CU objects for this dwarf2_per_objfile. */
5008
5009 static void
5010 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5011 const mapped_debug_names &map,
5012 const mapped_debug_names &dwz_map)
5013 {
5014 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5015 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5016
5017 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5018 dwarf2_per_objfile->info,
5019 false /* is_dwz */);
5020
5021 if (dwz_map.cu_count == 0)
5022 return;
5023
5024 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5025 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5026 true /* is_dwz */);
5027 }
5028
5029 /* Read .debug_names. If everything went ok, initialize the "quick"
5030 elements of all the CUs and return true. Otherwise, return false. */
5031
5032 static bool
5033 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5034 {
5035 std::unique_ptr<mapped_debug_names> map
5036 (new mapped_debug_names (dwarf2_per_objfile));
5037 mapped_debug_names dwz_map (dwarf2_per_objfile);
5038 struct objfile *objfile = dwarf2_per_objfile->objfile;
5039
5040 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5041 &dwarf2_per_objfile->debug_names,
5042 *map))
5043 return false;
5044
5045 /* Don't use the index if it's empty. */
5046 if (map->name_count == 0)
5047 return false;
5048
5049 /* If there is a .dwz file, read it so we can get its CU list as
5050 well. */
5051 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5052 if (dwz != NULL)
5053 {
5054 if (!read_debug_names_from_section (objfile,
5055 bfd_get_filename (dwz->dwz_bfd.get ()),
5056 &dwz->debug_names, dwz_map))
5057 {
5058 warning (_("could not read '.debug_names' section from %s; skipping"),
5059 bfd_get_filename (dwz->dwz_bfd.get ()));
5060 return false;
5061 }
5062 }
5063
5064 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5065
5066 if (map->tu_count != 0)
5067 {
5068 /* We can only handle a single .debug_types when we have an
5069 index. */
5070 if (dwarf2_per_objfile->types.size () != 1)
5071 return false;
5072
5073 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5074
5075 create_signatured_type_table_from_debug_names
5076 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5077 }
5078
5079 create_addrmap_from_aranges (dwarf2_per_objfile,
5080 &dwarf2_per_objfile->debug_aranges);
5081
5082 dwarf2_per_objfile->debug_names_table = std::move (map);
5083 dwarf2_per_objfile->using_index = 1;
5084 dwarf2_per_objfile->quick_file_names_table =
5085 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5086
5087 return true;
5088 }
5089
5090 /* Type used to manage iterating over all CUs looking for a symbol for
5091 .debug_names. */
5092
5093 class dw2_debug_names_iterator
5094 {
5095 public:
5096 dw2_debug_names_iterator (const mapped_debug_names &map,
5097 gdb::optional<block_enum> block_index,
5098 domain_enum domain,
5099 const char *name)
5100 : m_map (map), m_block_index (block_index), m_domain (domain),
5101 m_addr (find_vec_in_debug_names (map, name))
5102 {}
5103
5104 dw2_debug_names_iterator (const mapped_debug_names &map,
5105 search_domain search, uint32_t namei)
5106 : m_map (map),
5107 m_search (search),
5108 m_addr (find_vec_in_debug_names (map, namei))
5109 {}
5110
5111 dw2_debug_names_iterator (const mapped_debug_names &map,
5112 block_enum block_index, domain_enum domain,
5113 uint32_t namei)
5114 : m_map (map), m_block_index (block_index), m_domain (domain),
5115 m_addr (find_vec_in_debug_names (map, namei))
5116 {}
5117
5118 /* Return the next matching CU or NULL if there are no more. */
5119 dwarf2_per_cu_data *next ();
5120
5121 private:
5122 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5123 const char *name);
5124 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5125 uint32_t namei);
5126
5127 /* The internalized form of .debug_names. */
5128 const mapped_debug_names &m_map;
5129
5130 /* If set, only look for symbols that match that block. Valid values are
5131 GLOBAL_BLOCK and STATIC_BLOCK. */
5132 const gdb::optional<block_enum> m_block_index;
5133
5134 /* The kind of symbol we're looking for. */
5135 const domain_enum m_domain = UNDEF_DOMAIN;
5136 const search_domain m_search = ALL_DOMAIN;
5137
5138 /* The list of CUs from the index entry of the symbol, or NULL if
5139 not found. */
5140 const gdb_byte *m_addr;
5141 };
5142
5143 const char *
5144 mapped_debug_names::namei_to_name (uint32_t namei) const
5145 {
5146 const ULONGEST namei_string_offs
5147 = extract_unsigned_integer ((name_table_string_offs_reordered
5148 + namei * offset_size),
5149 offset_size,
5150 dwarf5_byte_order);
5151 return read_indirect_string_at_offset (dwarf2_per_objfile,
5152 namei_string_offs);
5153 }
5154
5155 /* Find a slot in .debug_names for the object named NAME. If NAME is
5156 found, return pointer to its pool data. If NAME cannot be found,
5157 return NULL. */
5158
5159 const gdb_byte *
5160 dw2_debug_names_iterator::find_vec_in_debug_names
5161 (const mapped_debug_names &map, const char *name)
5162 {
5163 int (*cmp) (const char *, const char *);
5164
5165 gdb::unique_xmalloc_ptr<char> without_params;
5166 if (current_language->la_language == language_cplus
5167 || current_language->la_language == language_fortran
5168 || current_language->la_language == language_d)
5169 {
5170 /* NAME is already canonical. Drop any qualifiers as
5171 .debug_names does not contain any. */
5172
5173 if (strchr (name, '(') != NULL)
5174 {
5175 without_params = cp_remove_params (name);
5176 if (without_params != NULL)
5177 name = without_params.get ();
5178 }
5179 }
5180
5181 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5182
5183 const uint32_t full_hash = dwarf5_djb_hash (name);
5184 uint32_t namei
5185 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5186 (map.bucket_table_reordered
5187 + (full_hash % map.bucket_count)), 4,
5188 map.dwarf5_byte_order);
5189 if (namei == 0)
5190 return NULL;
5191 --namei;
5192 if (namei >= map.name_count)
5193 {
5194 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5195 "[in module %s]"),
5196 namei, map.name_count,
5197 objfile_name (map.dwarf2_per_objfile->objfile));
5198 return NULL;
5199 }
5200
5201 for (;;)
5202 {
5203 const uint32_t namei_full_hash
5204 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5205 (map.hash_table_reordered + namei), 4,
5206 map.dwarf5_byte_order);
5207 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5208 return NULL;
5209
5210 if (full_hash == namei_full_hash)
5211 {
5212 const char *const namei_string = map.namei_to_name (namei);
5213
5214 #if 0 /* An expensive sanity check. */
5215 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5216 {
5217 complaint (_("Wrong .debug_names hash for string at index %u "
5218 "[in module %s]"),
5219 namei, objfile_name (dwarf2_per_objfile->objfile));
5220 return NULL;
5221 }
5222 #endif
5223
5224 if (cmp (namei_string, name) == 0)
5225 {
5226 const ULONGEST namei_entry_offs
5227 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5228 + namei * map.offset_size),
5229 map.offset_size, map.dwarf5_byte_order);
5230 return map.entry_pool + namei_entry_offs;
5231 }
5232 }
5233
5234 ++namei;
5235 if (namei >= map.name_count)
5236 return NULL;
5237 }
5238 }
5239
5240 const gdb_byte *
5241 dw2_debug_names_iterator::find_vec_in_debug_names
5242 (const mapped_debug_names &map, uint32_t namei)
5243 {
5244 if (namei >= map.name_count)
5245 {
5246 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5247 "[in module %s]"),
5248 namei, map.name_count,
5249 objfile_name (map.dwarf2_per_objfile->objfile));
5250 return NULL;
5251 }
5252
5253 const ULONGEST namei_entry_offs
5254 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5255 + namei * map.offset_size),
5256 map.offset_size, map.dwarf5_byte_order);
5257 return map.entry_pool + namei_entry_offs;
5258 }
5259
5260 /* See dw2_debug_names_iterator. */
5261
5262 dwarf2_per_cu_data *
5263 dw2_debug_names_iterator::next ()
5264 {
5265 if (m_addr == NULL)
5266 return NULL;
5267
5268 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5269 struct objfile *objfile = dwarf2_per_objfile->objfile;
5270 bfd *const abfd = objfile->obfd;
5271
5272 again:
5273
5274 unsigned int bytes_read;
5275 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5276 m_addr += bytes_read;
5277 if (abbrev == 0)
5278 return NULL;
5279
5280 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5281 if (indexval_it == m_map.abbrev_map.cend ())
5282 {
5283 complaint (_("Wrong .debug_names undefined abbrev code %s "
5284 "[in module %s]"),
5285 pulongest (abbrev), objfile_name (objfile));
5286 return NULL;
5287 }
5288 const mapped_debug_names::index_val &indexval = indexval_it->second;
5289 enum class symbol_linkage {
5290 unknown,
5291 static_,
5292 extern_,
5293 } symbol_linkage_ = symbol_linkage::unknown;
5294 dwarf2_per_cu_data *per_cu = NULL;
5295 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5296 {
5297 ULONGEST ull;
5298 switch (attr.form)
5299 {
5300 case DW_FORM_implicit_const:
5301 ull = attr.implicit_const;
5302 break;
5303 case DW_FORM_flag_present:
5304 ull = 1;
5305 break;
5306 case DW_FORM_udata:
5307 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5308 m_addr += bytes_read;
5309 break;
5310 default:
5311 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5312 dwarf_form_name (attr.form),
5313 objfile_name (objfile));
5314 return NULL;
5315 }
5316 switch (attr.dw_idx)
5317 {
5318 case DW_IDX_compile_unit:
5319 /* Don't crash on bad data. */
5320 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5321 {
5322 complaint (_(".debug_names entry has bad CU index %s"
5323 " [in module %s]"),
5324 pulongest (ull),
5325 objfile_name (dwarf2_per_objfile->objfile));
5326 continue;
5327 }
5328 per_cu = dwarf2_per_objfile->get_cutu (ull);
5329 break;
5330 case DW_IDX_type_unit:
5331 /* Don't crash on bad data. */
5332 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5333 {
5334 complaint (_(".debug_names entry has bad TU index %s"
5335 " [in module %s]"),
5336 pulongest (ull),
5337 objfile_name (dwarf2_per_objfile->objfile));
5338 continue;
5339 }
5340 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5341 break;
5342 case DW_IDX_GNU_internal:
5343 if (!m_map.augmentation_is_gdb)
5344 break;
5345 symbol_linkage_ = symbol_linkage::static_;
5346 break;
5347 case DW_IDX_GNU_external:
5348 if (!m_map.augmentation_is_gdb)
5349 break;
5350 symbol_linkage_ = symbol_linkage::extern_;
5351 break;
5352 }
5353 }
5354
5355 /* Skip if already read in. */
5356 if (per_cu->v.quick->compunit_symtab)
5357 goto again;
5358
5359 /* Check static vs global. */
5360 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5361 {
5362 const bool want_static = *m_block_index == STATIC_BLOCK;
5363 const bool symbol_is_static =
5364 symbol_linkage_ == symbol_linkage::static_;
5365 if (want_static != symbol_is_static)
5366 goto again;
5367 }
5368
5369 /* Match dw2_symtab_iter_next, symbol_kind
5370 and debug_names::psymbol_tag. */
5371 switch (m_domain)
5372 {
5373 case VAR_DOMAIN:
5374 switch (indexval.dwarf_tag)
5375 {
5376 case DW_TAG_variable:
5377 case DW_TAG_subprogram:
5378 /* Some types are also in VAR_DOMAIN. */
5379 case DW_TAG_typedef:
5380 case DW_TAG_structure_type:
5381 break;
5382 default:
5383 goto again;
5384 }
5385 break;
5386 case STRUCT_DOMAIN:
5387 switch (indexval.dwarf_tag)
5388 {
5389 case DW_TAG_typedef:
5390 case DW_TAG_structure_type:
5391 break;
5392 default:
5393 goto again;
5394 }
5395 break;
5396 case LABEL_DOMAIN:
5397 switch (indexval.dwarf_tag)
5398 {
5399 case 0:
5400 case DW_TAG_variable:
5401 break;
5402 default:
5403 goto again;
5404 }
5405 break;
5406 case MODULE_DOMAIN:
5407 switch (indexval.dwarf_tag)
5408 {
5409 case DW_TAG_module:
5410 break;
5411 default:
5412 goto again;
5413 }
5414 break;
5415 default:
5416 break;
5417 }
5418
5419 /* Match dw2_expand_symtabs_matching, symbol_kind and
5420 debug_names::psymbol_tag. */
5421 switch (m_search)
5422 {
5423 case VARIABLES_DOMAIN:
5424 switch (indexval.dwarf_tag)
5425 {
5426 case DW_TAG_variable:
5427 break;
5428 default:
5429 goto again;
5430 }
5431 break;
5432 case FUNCTIONS_DOMAIN:
5433 switch (indexval.dwarf_tag)
5434 {
5435 case DW_TAG_subprogram:
5436 break;
5437 default:
5438 goto again;
5439 }
5440 break;
5441 case TYPES_DOMAIN:
5442 switch (indexval.dwarf_tag)
5443 {
5444 case DW_TAG_typedef:
5445 case DW_TAG_structure_type:
5446 break;
5447 default:
5448 goto again;
5449 }
5450 break;
5451 case MODULES_DOMAIN:
5452 switch (indexval.dwarf_tag)
5453 {
5454 case DW_TAG_module:
5455 break;
5456 default:
5457 goto again;
5458 }
5459 default:
5460 break;
5461 }
5462
5463 return per_cu;
5464 }
5465
5466 static struct compunit_symtab *
5467 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5468 const char *name, domain_enum domain)
5469 {
5470 struct dwarf2_per_objfile *dwarf2_per_objfile
5471 = get_dwarf2_per_objfile (objfile);
5472
5473 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5474 if (!mapp)
5475 {
5476 /* index is NULL if OBJF_READNOW. */
5477 return NULL;
5478 }
5479 const auto &map = *mapp;
5480
5481 dw2_debug_names_iterator iter (map, block_index, domain, name);
5482
5483 struct compunit_symtab *stab_best = NULL;
5484 struct dwarf2_per_cu_data *per_cu;
5485 while ((per_cu = iter.next ()) != NULL)
5486 {
5487 struct symbol *sym, *with_opaque = NULL;
5488 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5489 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5490 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5491
5492 sym = block_find_symbol (block, name, domain,
5493 block_find_non_opaque_type_preferred,
5494 &with_opaque);
5495
5496 /* Some caution must be observed with overloaded functions and
5497 methods, since the index will not contain any overload
5498 information (but NAME might contain it). */
5499
5500 if (sym != NULL
5501 && strcmp_iw (sym->search_name (), name) == 0)
5502 return stab;
5503 if (with_opaque != NULL
5504 && strcmp_iw (with_opaque->search_name (), name) == 0)
5505 stab_best = stab;
5506
5507 /* Keep looking through other CUs. */
5508 }
5509
5510 return stab_best;
5511 }
5512
5513 /* This dumps minimal information about .debug_names. It is called
5514 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5515 uses this to verify that .debug_names has been loaded. */
5516
5517 static void
5518 dw2_debug_names_dump (struct objfile *objfile)
5519 {
5520 struct dwarf2_per_objfile *dwarf2_per_objfile
5521 = get_dwarf2_per_objfile (objfile);
5522
5523 gdb_assert (dwarf2_per_objfile->using_index);
5524 printf_filtered (".debug_names:");
5525 if (dwarf2_per_objfile->debug_names_table)
5526 printf_filtered (" exists\n");
5527 else
5528 printf_filtered (" faked for \"readnow\"\n");
5529 printf_filtered ("\n");
5530 }
5531
5532 static void
5533 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5534 const char *func_name)
5535 {
5536 struct dwarf2_per_objfile *dwarf2_per_objfile
5537 = get_dwarf2_per_objfile (objfile);
5538
5539 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5540 if (dwarf2_per_objfile->debug_names_table)
5541 {
5542 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5543
5544 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5545
5546 struct dwarf2_per_cu_data *per_cu;
5547 while ((per_cu = iter.next ()) != NULL)
5548 dw2_instantiate_symtab (per_cu, false);
5549 }
5550 }
5551
5552 static void
5553 dw2_debug_names_map_matching_symbols
5554 (struct objfile *objfile,
5555 const lookup_name_info &name, domain_enum domain,
5556 int global,
5557 gdb::function_view<symbol_found_callback_ftype> callback,
5558 symbol_compare_ftype *ordered_compare)
5559 {
5560 struct dwarf2_per_objfile *dwarf2_per_objfile
5561 = get_dwarf2_per_objfile (objfile);
5562
5563 /* debug_names_table is NULL if OBJF_READNOW. */
5564 if (!dwarf2_per_objfile->debug_names_table)
5565 return;
5566
5567 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5568 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5569
5570 const char *match_name = name.ada ().lookup_name ().c_str ();
5571 auto matcher = [&] (const char *symname)
5572 {
5573 if (ordered_compare == nullptr)
5574 return true;
5575 return ordered_compare (symname, match_name) == 0;
5576 };
5577
5578 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5579 [&] (offset_type namei)
5580 {
5581 /* The name was matched, now expand corresponding CUs that were
5582 marked. */
5583 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5584
5585 struct dwarf2_per_cu_data *per_cu;
5586 while ((per_cu = iter.next ()) != NULL)
5587 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5588 return true;
5589 });
5590
5591 /* It's a shame we couldn't do this inside the
5592 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5593 that have already been expanded. Instead, this loop matches what
5594 the psymtab code does. */
5595 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5596 {
5597 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5598 if (cust != nullptr)
5599 {
5600 const struct block *block
5601 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5602 if (!iterate_over_symbols_terminated (block, name,
5603 domain, callback))
5604 break;
5605 }
5606 }
5607 }
5608
5609 static void
5610 dw2_debug_names_expand_symtabs_matching
5611 (struct objfile *objfile,
5612 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5613 const lookup_name_info &lookup_name,
5614 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5615 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5616 enum search_domain kind)
5617 {
5618 struct dwarf2_per_objfile *dwarf2_per_objfile
5619 = get_dwarf2_per_objfile (objfile);
5620
5621 /* debug_names_table is NULL if OBJF_READNOW. */
5622 if (!dwarf2_per_objfile->debug_names_table)
5623 return;
5624
5625 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5626
5627 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5628
5629 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5630 symbol_matcher,
5631 kind, [&] (offset_type namei)
5632 {
5633 /* The name was matched, now expand corresponding CUs that were
5634 marked. */
5635 dw2_debug_names_iterator iter (map, kind, namei);
5636
5637 struct dwarf2_per_cu_data *per_cu;
5638 while ((per_cu = iter.next ()) != NULL)
5639 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5640 expansion_notify);
5641 return true;
5642 });
5643 }
5644
5645 const struct quick_symbol_functions dwarf2_debug_names_functions =
5646 {
5647 dw2_has_symbols,
5648 dw2_find_last_source_symtab,
5649 dw2_forget_cached_source_info,
5650 dw2_map_symtabs_matching_filename,
5651 dw2_debug_names_lookup_symbol,
5652 dw2_print_stats,
5653 dw2_debug_names_dump,
5654 dw2_debug_names_expand_symtabs_for_function,
5655 dw2_expand_all_symtabs,
5656 dw2_expand_symtabs_with_fullname,
5657 dw2_debug_names_map_matching_symbols,
5658 dw2_debug_names_expand_symtabs_matching,
5659 dw2_find_pc_sect_compunit_symtab,
5660 NULL,
5661 dw2_map_symbol_filenames
5662 };
5663
5664 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5665 to either a dwarf2_per_objfile or dwz_file object. */
5666
5667 template <typename T>
5668 static gdb::array_view<const gdb_byte>
5669 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5670 {
5671 dwarf2_section_info *section = &section_owner->gdb_index;
5672
5673 if (section->empty ())
5674 return {};
5675
5676 /* Older elfutils strip versions could keep the section in the main
5677 executable while splitting it for the separate debug info file. */
5678 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5679 return {};
5680
5681 section->read (obj);
5682
5683 /* dwarf2_section_info::size is a bfd_size_type, while
5684 gdb::array_view works with size_t. On 32-bit hosts, with
5685 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5686 is 32-bit. So we need an explicit narrowing conversion here.
5687 This is fine, because it's impossible to allocate or mmap an
5688 array/buffer larger than what size_t can represent. */
5689 return gdb::make_array_view (section->buffer, section->size);
5690 }
5691
5692 /* Lookup the index cache for the contents of the index associated to
5693 DWARF2_OBJ. */
5694
5695 static gdb::array_view<const gdb_byte>
5696 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5697 {
5698 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5699 if (build_id == nullptr)
5700 return {};
5701
5702 return global_index_cache.lookup_gdb_index (build_id,
5703 &dwarf2_obj->index_cache_res);
5704 }
5705
5706 /* Same as the above, but for DWZ. */
5707
5708 static gdb::array_view<const gdb_byte>
5709 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5710 {
5711 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5712 if (build_id == nullptr)
5713 return {};
5714
5715 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5716 }
5717
5718 /* See symfile.h. */
5719
5720 bool
5721 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5722 {
5723 struct dwarf2_per_objfile *dwarf2_per_objfile
5724 = get_dwarf2_per_objfile (objfile);
5725
5726 /* If we're about to read full symbols, don't bother with the
5727 indices. In this case we also don't care if some other debug
5728 format is making psymtabs, because they are all about to be
5729 expanded anyway. */
5730 if ((objfile->flags & OBJF_READNOW))
5731 {
5732 dwarf2_per_objfile->using_index = 1;
5733 create_all_comp_units (dwarf2_per_objfile);
5734 create_all_type_units (dwarf2_per_objfile);
5735 dwarf2_per_objfile->quick_file_names_table
5736 = create_quick_file_names_table
5737 (dwarf2_per_objfile->all_comp_units.size ());
5738
5739 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5740 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5741 {
5742 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5743
5744 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5745 struct dwarf2_per_cu_quick_data);
5746 }
5747
5748 /* Return 1 so that gdb sees the "quick" functions. However,
5749 these functions will be no-ops because we will have expanded
5750 all symtabs. */
5751 *index_kind = dw_index_kind::GDB_INDEX;
5752 return true;
5753 }
5754
5755 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5756 {
5757 *index_kind = dw_index_kind::DEBUG_NAMES;
5758 return true;
5759 }
5760
5761 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5762 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5763 get_gdb_index_contents_from_section<dwz_file>))
5764 {
5765 *index_kind = dw_index_kind::GDB_INDEX;
5766 return true;
5767 }
5768
5769 /* ... otherwise, try to find the index in the index cache. */
5770 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5771 get_gdb_index_contents_from_cache,
5772 get_gdb_index_contents_from_cache_dwz))
5773 {
5774 global_index_cache.hit ();
5775 *index_kind = dw_index_kind::GDB_INDEX;
5776 return true;
5777 }
5778
5779 global_index_cache.miss ();
5780 return false;
5781 }
5782
5783 \f
5784
5785 /* Build a partial symbol table. */
5786
5787 void
5788 dwarf2_build_psymtabs (struct objfile *objfile)
5789 {
5790 struct dwarf2_per_objfile *dwarf2_per_objfile
5791 = get_dwarf2_per_objfile (objfile);
5792
5793 init_psymbol_list (objfile, 1024);
5794
5795 try
5796 {
5797 /* This isn't really ideal: all the data we allocate on the
5798 objfile's obstack is still uselessly kept around. However,
5799 freeing it seems unsafe. */
5800 psymtab_discarder psymtabs (objfile);
5801 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5802 psymtabs.keep ();
5803
5804 /* (maybe) store an index in the cache. */
5805 global_index_cache.store (dwarf2_per_objfile);
5806 }
5807 catch (const gdb_exception_error &except)
5808 {
5809 exception_print (gdb_stderr, except);
5810 }
5811 }
5812
5813 /* Find the base address of the compilation unit for range lists and
5814 location lists. It will normally be specified by DW_AT_low_pc.
5815 In DWARF-3 draft 4, the base address could be overridden by
5816 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5817 compilation units with discontinuous ranges. */
5818
5819 static void
5820 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5821 {
5822 struct attribute *attr;
5823
5824 cu->base_known = 0;
5825 cu->base_address = 0;
5826
5827 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5828 if (attr != nullptr)
5829 {
5830 cu->base_address = attr->value_as_address ();
5831 cu->base_known = 1;
5832 }
5833 else
5834 {
5835 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5836 if (attr != nullptr)
5837 {
5838 cu->base_address = attr->value_as_address ();
5839 cu->base_known = 1;
5840 }
5841 }
5842 }
5843
5844 /* Helper function that returns the proper abbrev section for
5845 THIS_CU. */
5846
5847 static struct dwarf2_section_info *
5848 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5849 {
5850 struct dwarf2_section_info *abbrev;
5851 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5852
5853 if (this_cu->is_dwz)
5854 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5855 else
5856 abbrev = &dwarf2_per_objfile->abbrev;
5857
5858 return abbrev;
5859 }
5860
5861 /* Fetch the abbreviation table offset from a comp or type unit header. */
5862
5863 static sect_offset
5864 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5865 struct dwarf2_section_info *section,
5866 sect_offset sect_off)
5867 {
5868 bfd *abfd = section->get_bfd_owner ();
5869 const gdb_byte *info_ptr;
5870 unsigned int initial_length_size, offset_size;
5871 uint16_t version;
5872
5873 section->read (dwarf2_per_objfile->objfile);
5874 info_ptr = section->buffer + to_underlying (sect_off);
5875 read_initial_length (abfd, info_ptr, &initial_length_size);
5876 offset_size = initial_length_size == 4 ? 4 : 8;
5877 info_ptr += initial_length_size;
5878
5879 version = read_2_bytes (abfd, info_ptr);
5880 info_ptr += 2;
5881 if (version >= 5)
5882 {
5883 /* Skip unit type and address size. */
5884 info_ptr += 2;
5885 }
5886
5887 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5888 }
5889
5890 /* A partial symtab that is used only for include files. */
5891 struct dwarf2_include_psymtab : public partial_symtab
5892 {
5893 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5894 : partial_symtab (filename, objfile)
5895 {
5896 }
5897
5898 void read_symtab (struct objfile *objfile) override
5899 {
5900 expand_psymtab (objfile);
5901 }
5902
5903 void expand_psymtab (struct objfile *objfile) override
5904 {
5905 if (m_readin)
5906 return;
5907 /* It's an include file, no symbols to read for it.
5908 Everything is in the parent symtab. */
5909 read_dependencies (objfile);
5910 m_readin = true;
5911 }
5912
5913 bool readin_p () const override
5914 {
5915 return m_readin;
5916 }
5917
5918 struct compunit_symtab *get_compunit_symtab () const override
5919 {
5920 return nullptr;
5921 }
5922
5923 private:
5924
5925 bool m_readin = false;
5926 };
5927
5928 /* Allocate a new partial symtab for file named NAME and mark this new
5929 partial symtab as being an include of PST. */
5930
5931 static void
5932 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5933 struct objfile *objfile)
5934 {
5935 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5936
5937 if (!IS_ABSOLUTE_PATH (subpst->filename))
5938 {
5939 /* It shares objfile->objfile_obstack. */
5940 subpst->dirname = pst->dirname;
5941 }
5942
5943 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5944 subpst->dependencies[0] = pst;
5945 subpst->number_of_dependencies = 1;
5946 }
5947
5948 /* Read the Line Number Program data and extract the list of files
5949 included by the source file represented by PST. Build an include
5950 partial symtab for each of these included files. */
5951
5952 static void
5953 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5954 struct die_info *die,
5955 dwarf2_psymtab *pst)
5956 {
5957 line_header_up lh;
5958 struct attribute *attr;
5959
5960 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5961 if (attr != nullptr)
5962 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5963 if (lh == NULL)
5964 return; /* No linetable, so no includes. */
5965
5966 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5967 that we pass in the raw text_low here; that is ok because we're
5968 only decoding the line table to make include partial symtabs, and
5969 so the addresses aren't really used. */
5970 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5971 pst->raw_text_low (), 1);
5972 }
5973
5974 static hashval_t
5975 hash_signatured_type (const void *item)
5976 {
5977 const struct signatured_type *sig_type
5978 = (const struct signatured_type *) item;
5979
5980 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5981 return sig_type->signature;
5982 }
5983
5984 static int
5985 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5986 {
5987 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5988 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5989
5990 return lhs->signature == rhs->signature;
5991 }
5992
5993 /* Allocate a hash table for signatured types. */
5994
5995 static htab_up
5996 allocate_signatured_type_table ()
5997 {
5998 return htab_up (htab_create_alloc (41,
5999 hash_signatured_type,
6000 eq_signatured_type,
6001 NULL, xcalloc, xfree));
6002 }
6003
6004 /* A helper function to add a signatured type CU to a table. */
6005
6006 static int
6007 add_signatured_type_cu_to_table (void **slot, void *datum)
6008 {
6009 struct signatured_type *sigt = (struct signatured_type *) *slot;
6010 std::vector<signatured_type *> *all_type_units
6011 = (std::vector<signatured_type *> *) datum;
6012
6013 all_type_units->push_back (sigt);
6014
6015 return 1;
6016 }
6017
6018 /* A helper for create_debug_types_hash_table. Read types from SECTION
6019 and fill them into TYPES_HTAB. It will process only type units,
6020 therefore DW_UT_type. */
6021
6022 static void
6023 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6024 struct dwo_file *dwo_file,
6025 dwarf2_section_info *section, htab_up &types_htab,
6026 rcuh_kind section_kind)
6027 {
6028 struct objfile *objfile = dwarf2_per_objfile->objfile;
6029 struct dwarf2_section_info *abbrev_section;
6030 bfd *abfd;
6031 const gdb_byte *info_ptr, *end_ptr;
6032
6033 abbrev_section = (dwo_file != NULL
6034 ? &dwo_file->sections.abbrev
6035 : &dwarf2_per_objfile->abbrev);
6036
6037 if (dwarf_read_debug)
6038 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6039 section->get_name (),
6040 abbrev_section->get_file_name ());
6041
6042 section->read (objfile);
6043 info_ptr = section->buffer;
6044
6045 if (info_ptr == NULL)
6046 return;
6047
6048 /* We can't set abfd until now because the section may be empty or
6049 not present, in which case the bfd is unknown. */
6050 abfd = section->get_bfd_owner ();
6051
6052 /* We don't use cutu_reader here because we don't need to read
6053 any dies: the signature is in the header. */
6054
6055 end_ptr = info_ptr + section->size;
6056 while (info_ptr < end_ptr)
6057 {
6058 struct signatured_type *sig_type;
6059 struct dwo_unit *dwo_tu;
6060 void **slot;
6061 const gdb_byte *ptr = info_ptr;
6062 struct comp_unit_head header;
6063 unsigned int length;
6064
6065 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6066
6067 /* Initialize it due to a false compiler warning. */
6068 header.signature = -1;
6069 header.type_cu_offset_in_tu = (cu_offset) -1;
6070
6071 /* We need to read the type's signature in order to build the hash
6072 table, but we don't need anything else just yet. */
6073
6074 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6075 abbrev_section, ptr, section_kind);
6076
6077 length = header.get_length ();
6078
6079 /* Skip dummy type units. */
6080 if (ptr >= info_ptr + length
6081 || peek_abbrev_code (abfd, ptr) == 0
6082 || header.unit_type != DW_UT_type)
6083 {
6084 info_ptr += length;
6085 continue;
6086 }
6087
6088 if (types_htab == NULL)
6089 {
6090 if (dwo_file)
6091 types_htab = allocate_dwo_unit_table ();
6092 else
6093 types_htab = allocate_signatured_type_table ();
6094 }
6095
6096 if (dwo_file)
6097 {
6098 sig_type = NULL;
6099 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6100 struct dwo_unit);
6101 dwo_tu->dwo_file = dwo_file;
6102 dwo_tu->signature = header.signature;
6103 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6104 dwo_tu->section = section;
6105 dwo_tu->sect_off = sect_off;
6106 dwo_tu->length = length;
6107 }
6108 else
6109 {
6110 /* N.B.: type_offset is not usable if this type uses a DWO file.
6111 The real type_offset is in the DWO file. */
6112 dwo_tu = NULL;
6113 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6114 struct signatured_type);
6115 sig_type->signature = header.signature;
6116 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6117 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6118 sig_type->per_cu.is_debug_types = 1;
6119 sig_type->per_cu.section = section;
6120 sig_type->per_cu.sect_off = sect_off;
6121 sig_type->per_cu.length = length;
6122 }
6123
6124 slot = htab_find_slot (types_htab.get (),
6125 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6126 INSERT);
6127 gdb_assert (slot != NULL);
6128 if (*slot != NULL)
6129 {
6130 sect_offset dup_sect_off;
6131
6132 if (dwo_file)
6133 {
6134 const struct dwo_unit *dup_tu
6135 = (const struct dwo_unit *) *slot;
6136
6137 dup_sect_off = dup_tu->sect_off;
6138 }
6139 else
6140 {
6141 const struct signatured_type *dup_tu
6142 = (const struct signatured_type *) *slot;
6143
6144 dup_sect_off = dup_tu->per_cu.sect_off;
6145 }
6146
6147 complaint (_("debug type entry at offset %s is duplicate to"
6148 " the entry at offset %s, signature %s"),
6149 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6150 hex_string (header.signature));
6151 }
6152 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6153
6154 if (dwarf_read_debug > 1)
6155 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6156 sect_offset_str (sect_off),
6157 hex_string (header.signature));
6158
6159 info_ptr += length;
6160 }
6161 }
6162
6163 /* Create the hash table of all entries in the .debug_types
6164 (or .debug_types.dwo) section(s).
6165 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6166 otherwise it is NULL.
6167
6168 The result is a pointer to the hash table or NULL if there are no types.
6169
6170 Note: This function processes DWO files only, not DWP files. */
6171
6172 static void
6173 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6174 struct dwo_file *dwo_file,
6175 gdb::array_view<dwarf2_section_info> type_sections,
6176 htab_up &types_htab)
6177 {
6178 for (dwarf2_section_info &section : type_sections)
6179 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6180 types_htab, rcuh_kind::TYPE);
6181 }
6182
6183 /* Create the hash table of all entries in the .debug_types section,
6184 and initialize all_type_units.
6185 The result is zero if there is an error (e.g. missing .debug_types section),
6186 otherwise non-zero. */
6187
6188 static int
6189 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6190 {
6191 htab_up types_htab;
6192
6193 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6194 &dwarf2_per_objfile->info, types_htab,
6195 rcuh_kind::COMPILE);
6196 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6197 dwarf2_per_objfile->types, types_htab);
6198 if (types_htab == NULL)
6199 {
6200 dwarf2_per_objfile->signatured_types = NULL;
6201 return 0;
6202 }
6203
6204 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6205
6206 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6207 dwarf2_per_objfile->all_type_units.reserve
6208 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6209
6210 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6211 add_signatured_type_cu_to_table,
6212 &dwarf2_per_objfile->all_type_units);
6213
6214 return 1;
6215 }
6216
6217 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6218 If SLOT is non-NULL, it is the entry to use in the hash table.
6219 Otherwise we find one. */
6220
6221 static struct signatured_type *
6222 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6223 void **slot)
6224 {
6225 struct objfile *objfile = dwarf2_per_objfile->objfile;
6226
6227 if (dwarf2_per_objfile->all_type_units.size ()
6228 == dwarf2_per_objfile->all_type_units.capacity ())
6229 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6230
6231 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6232 struct signatured_type);
6233
6234 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6235 sig_type->signature = sig;
6236 sig_type->per_cu.is_debug_types = 1;
6237 if (dwarf2_per_objfile->using_index)
6238 {
6239 sig_type->per_cu.v.quick =
6240 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6241 struct dwarf2_per_cu_quick_data);
6242 }
6243
6244 if (slot == NULL)
6245 {
6246 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6247 sig_type, INSERT);
6248 }
6249 gdb_assert (*slot == NULL);
6250 *slot = sig_type;
6251 /* The rest of sig_type must be filled in by the caller. */
6252 return sig_type;
6253 }
6254
6255 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6256 Fill in SIG_ENTRY with DWO_ENTRY. */
6257
6258 static void
6259 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6260 struct signatured_type *sig_entry,
6261 struct dwo_unit *dwo_entry)
6262 {
6263 /* Make sure we're not clobbering something we don't expect to. */
6264 gdb_assert (! sig_entry->per_cu.queued);
6265 gdb_assert (sig_entry->per_cu.cu == NULL);
6266 if (dwarf2_per_objfile->using_index)
6267 {
6268 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6269 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6270 }
6271 else
6272 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6273 gdb_assert (sig_entry->signature == dwo_entry->signature);
6274 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6275 gdb_assert (sig_entry->type_unit_group == NULL);
6276 gdb_assert (sig_entry->dwo_unit == NULL);
6277
6278 sig_entry->per_cu.section = dwo_entry->section;
6279 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6280 sig_entry->per_cu.length = dwo_entry->length;
6281 sig_entry->per_cu.reading_dwo_directly = 1;
6282 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6283 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6284 sig_entry->dwo_unit = dwo_entry;
6285 }
6286
6287 /* Subroutine of lookup_signatured_type.
6288 If we haven't read the TU yet, create the signatured_type data structure
6289 for a TU to be read in directly from a DWO file, bypassing the stub.
6290 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6291 using .gdb_index, then when reading a CU we want to stay in the DWO file
6292 containing that CU. Otherwise we could end up reading several other DWO
6293 files (due to comdat folding) to process the transitive closure of all the
6294 mentioned TUs, and that can be slow. The current DWO file will have every
6295 type signature that it needs.
6296 We only do this for .gdb_index because in the psymtab case we already have
6297 to read all the DWOs to build the type unit groups. */
6298
6299 static struct signatured_type *
6300 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6301 {
6302 struct dwarf2_per_objfile *dwarf2_per_objfile
6303 = cu->per_cu->dwarf2_per_objfile;
6304 struct dwo_file *dwo_file;
6305 struct dwo_unit find_dwo_entry, *dwo_entry;
6306 struct signatured_type find_sig_entry, *sig_entry;
6307 void **slot;
6308
6309 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6310
6311 /* If TU skeletons have been removed then we may not have read in any
6312 TUs yet. */
6313 if (dwarf2_per_objfile->signatured_types == NULL)
6314 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6315
6316 /* We only ever need to read in one copy of a signatured type.
6317 Use the global signatured_types array to do our own comdat-folding
6318 of types. If this is the first time we're reading this TU, and
6319 the TU has an entry in .gdb_index, replace the recorded data from
6320 .gdb_index with this TU. */
6321
6322 find_sig_entry.signature = sig;
6323 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6324 &find_sig_entry, INSERT);
6325 sig_entry = (struct signatured_type *) *slot;
6326
6327 /* We can get here with the TU already read, *or* in the process of being
6328 read. Don't reassign the global entry to point to this DWO if that's
6329 the case. Also note that if the TU is already being read, it may not
6330 have come from a DWO, the program may be a mix of Fission-compiled
6331 code and non-Fission-compiled code. */
6332
6333 /* Have we already tried to read this TU?
6334 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6335 needn't exist in the global table yet). */
6336 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6337 return sig_entry;
6338
6339 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6340 dwo_unit of the TU itself. */
6341 dwo_file = cu->dwo_unit->dwo_file;
6342
6343 /* Ok, this is the first time we're reading this TU. */
6344 if (dwo_file->tus == NULL)
6345 return NULL;
6346 find_dwo_entry.signature = sig;
6347 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6348 &find_dwo_entry);
6349 if (dwo_entry == NULL)
6350 return NULL;
6351
6352 /* If the global table doesn't have an entry for this TU, add one. */
6353 if (sig_entry == NULL)
6354 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6355
6356 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6357 sig_entry->per_cu.tu_read = 1;
6358 return sig_entry;
6359 }
6360
6361 /* Subroutine of lookup_signatured_type.
6362 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6363 then try the DWP file. If the TU stub (skeleton) has been removed then
6364 it won't be in .gdb_index. */
6365
6366 static struct signatured_type *
6367 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6368 {
6369 struct dwarf2_per_objfile *dwarf2_per_objfile
6370 = cu->per_cu->dwarf2_per_objfile;
6371 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6372 struct dwo_unit *dwo_entry;
6373 struct signatured_type find_sig_entry, *sig_entry;
6374 void **slot;
6375
6376 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6377 gdb_assert (dwp_file != NULL);
6378
6379 /* If TU skeletons have been removed then we may not have read in any
6380 TUs yet. */
6381 if (dwarf2_per_objfile->signatured_types == NULL)
6382 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6383
6384 find_sig_entry.signature = sig;
6385 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6386 &find_sig_entry, INSERT);
6387 sig_entry = (struct signatured_type *) *slot;
6388
6389 /* Have we already tried to read this TU?
6390 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6391 needn't exist in the global table yet). */
6392 if (sig_entry != NULL)
6393 return sig_entry;
6394
6395 if (dwp_file->tus == NULL)
6396 return NULL;
6397 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6398 sig, 1 /* is_debug_types */);
6399 if (dwo_entry == NULL)
6400 return NULL;
6401
6402 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6403 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6404
6405 return sig_entry;
6406 }
6407
6408 /* Lookup a signature based type for DW_FORM_ref_sig8.
6409 Returns NULL if signature SIG is not present in the table.
6410 It is up to the caller to complain about this. */
6411
6412 static struct signatured_type *
6413 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6414 {
6415 struct dwarf2_per_objfile *dwarf2_per_objfile
6416 = cu->per_cu->dwarf2_per_objfile;
6417
6418 if (cu->dwo_unit
6419 && dwarf2_per_objfile->using_index)
6420 {
6421 /* We're in a DWO/DWP file, and we're using .gdb_index.
6422 These cases require special processing. */
6423 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6424 return lookup_dwo_signatured_type (cu, sig);
6425 else
6426 return lookup_dwp_signatured_type (cu, sig);
6427 }
6428 else
6429 {
6430 struct signatured_type find_entry, *entry;
6431
6432 if (dwarf2_per_objfile->signatured_types == NULL)
6433 return NULL;
6434 find_entry.signature = sig;
6435 entry = ((struct signatured_type *)
6436 htab_find (dwarf2_per_objfile->signatured_types.get (),
6437 &find_entry));
6438 return entry;
6439 }
6440 }
6441
6442 /* Return the address base of the compile unit, which, if exists, is stored
6443 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6444 static gdb::optional<ULONGEST>
6445 lookup_addr_base (struct die_info *comp_unit_die)
6446 {
6447 struct attribute *attr;
6448 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6449 if (attr == nullptr)
6450 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6451 if (attr == nullptr)
6452 return gdb::optional<ULONGEST> ();
6453 return DW_UNSND (attr);
6454 }
6455
6456 /* Return range lists base of the compile unit, which, if exists, is stored
6457 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6458 static ULONGEST
6459 lookup_ranges_base (struct die_info *comp_unit_die)
6460 {
6461 struct attribute *attr;
6462 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6463 if (attr == nullptr)
6464 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6465 if (attr == nullptr)
6466 return 0;
6467 return DW_UNSND (attr);
6468 }
6469
6470 /* Low level DIE reading support. */
6471
6472 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6473
6474 static void
6475 init_cu_die_reader (struct die_reader_specs *reader,
6476 struct dwarf2_cu *cu,
6477 struct dwarf2_section_info *section,
6478 struct dwo_file *dwo_file,
6479 struct abbrev_table *abbrev_table)
6480 {
6481 gdb_assert (section->readin && section->buffer != NULL);
6482 reader->abfd = section->get_bfd_owner ();
6483 reader->cu = cu;
6484 reader->dwo_file = dwo_file;
6485 reader->die_section = section;
6486 reader->buffer = section->buffer;
6487 reader->buffer_end = section->buffer + section->size;
6488 reader->abbrev_table = abbrev_table;
6489 }
6490
6491 /* Subroutine of cutu_reader to simplify it.
6492 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6493 There's just a lot of work to do, and cutu_reader is big enough
6494 already.
6495
6496 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6497 from it to the DIE in the DWO. If NULL we are skipping the stub.
6498 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6499 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6500 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6501 STUB_COMP_DIR may be non-NULL.
6502 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6503 are filled in with the info of the DIE from the DWO file.
6504 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6505 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6506 kept around for at least as long as *RESULT_READER.
6507
6508 The result is non-zero if a valid (non-dummy) DIE was found. */
6509
6510 static int
6511 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6512 struct dwo_unit *dwo_unit,
6513 struct die_info *stub_comp_unit_die,
6514 const char *stub_comp_dir,
6515 struct die_reader_specs *result_reader,
6516 const gdb_byte **result_info_ptr,
6517 struct die_info **result_comp_unit_die,
6518 abbrev_table_up *result_dwo_abbrev_table)
6519 {
6520 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6521 struct objfile *objfile = dwarf2_per_objfile->objfile;
6522 struct dwarf2_cu *cu = this_cu->cu;
6523 bfd *abfd;
6524 const gdb_byte *begin_info_ptr, *info_ptr;
6525 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6526 int i,num_extra_attrs;
6527 struct dwarf2_section_info *dwo_abbrev_section;
6528 struct die_info *comp_unit_die;
6529
6530 /* At most one of these may be provided. */
6531 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6532
6533 /* These attributes aren't processed until later:
6534 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6535 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6536 referenced later. However, these attributes are found in the stub
6537 which we won't have later. In order to not impose this complication
6538 on the rest of the code, we read them here and copy them to the
6539 DWO CU/TU die. */
6540
6541 stmt_list = NULL;
6542 low_pc = NULL;
6543 high_pc = NULL;
6544 ranges = NULL;
6545 comp_dir = NULL;
6546
6547 if (stub_comp_unit_die != NULL)
6548 {
6549 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6550 DWO file. */
6551 if (! this_cu->is_debug_types)
6552 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6553 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6554 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6555 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6556 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6557
6558 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6559
6560 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6561 here (if needed). We need the value before we can process
6562 DW_AT_ranges. */
6563 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6564 }
6565 else if (stub_comp_dir != NULL)
6566 {
6567 /* Reconstruct the comp_dir attribute to simplify the code below. */
6568 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6569 comp_dir->name = DW_AT_comp_dir;
6570 comp_dir->form = DW_FORM_string;
6571 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6572 DW_STRING (comp_dir) = stub_comp_dir;
6573 }
6574
6575 /* Set up for reading the DWO CU/TU. */
6576 cu->dwo_unit = dwo_unit;
6577 dwarf2_section_info *section = dwo_unit->section;
6578 section->read (objfile);
6579 abfd = section->get_bfd_owner ();
6580 begin_info_ptr = info_ptr = (section->buffer
6581 + to_underlying (dwo_unit->sect_off));
6582 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6583
6584 if (this_cu->is_debug_types)
6585 {
6586 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6587
6588 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6589 &cu->header, section,
6590 dwo_abbrev_section,
6591 info_ptr, rcuh_kind::TYPE);
6592 /* This is not an assert because it can be caused by bad debug info. */
6593 if (sig_type->signature != cu->header.signature)
6594 {
6595 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6596 " TU at offset %s [in module %s]"),
6597 hex_string (sig_type->signature),
6598 hex_string (cu->header.signature),
6599 sect_offset_str (dwo_unit->sect_off),
6600 bfd_get_filename (abfd));
6601 }
6602 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6603 /* For DWOs coming from DWP files, we don't know the CU length
6604 nor the type's offset in the TU until now. */
6605 dwo_unit->length = cu->header.get_length ();
6606 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6607
6608 /* Establish the type offset that can be used to lookup the type.
6609 For DWO files, we don't know it until now. */
6610 sig_type->type_offset_in_section
6611 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6612 }
6613 else
6614 {
6615 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6616 &cu->header, section,
6617 dwo_abbrev_section,
6618 info_ptr, rcuh_kind::COMPILE);
6619 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6620 /* For DWOs coming from DWP files, we don't know the CU length
6621 until now. */
6622 dwo_unit->length = cu->header.get_length ();
6623 }
6624
6625 *result_dwo_abbrev_table
6626 = abbrev_table::read (objfile, dwo_abbrev_section,
6627 cu->header.abbrev_sect_off);
6628 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6629 result_dwo_abbrev_table->get ());
6630
6631 /* Read in the die, but leave space to copy over the attributes
6632 from the stub. This has the benefit of simplifying the rest of
6633 the code - all the work to maintain the illusion of a single
6634 DW_TAG_{compile,type}_unit DIE is done here. */
6635 num_extra_attrs = ((stmt_list != NULL)
6636 + (low_pc != NULL)
6637 + (high_pc != NULL)
6638 + (ranges != NULL)
6639 + (comp_dir != NULL));
6640 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6641 num_extra_attrs);
6642
6643 /* Copy over the attributes from the stub to the DIE we just read in. */
6644 comp_unit_die = *result_comp_unit_die;
6645 i = comp_unit_die->num_attrs;
6646 if (stmt_list != NULL)
6647 comp_unit_die->attrs[i++] = *stmt_list;
6648 if (low_pc != NULL)
6649 comp_unit_die->attrs[i++] = *low_pc;
6650 if (high_pc != NULL)
6651 comp_unit_die->attrs[i++] = *high_pc;
6652 if (ranges != NULL)
6653 comp_unit_die->attrs[i++] = *ranges;
6654 if (comp_dir != NULL)
6655 comp_unit_die->attrs[i++] = *comp_dir;
6656 comp_unit_die->num_attrs += num_extra_attrs;
6657
6658 if (dwarf_die_debug)
6659 {
6660 fprintf_unfiltered (gdb_stdlog,
6661 "Read die from %s@0x%x of %s:\n",
6662 section->get_name (),
6663 (unsigned) (begin_info_ptr - section->buffer),
6664 bfd_get_filename (abfd));
6665 dump_die (comp_unit_die, dwarf_die_debug);
6666 }
6667
6668 /* Skip dummy compilation units. */
6669 if (info_ptr >= begin_info_ptr + dwo_unit->length
6670 || peek_abbrev_code (abfd, info_ptr) == 0)
6671 return 0;
6672
6673 *result_info_ptr = info_ptr;
6674 return 1;
6675 }
6676
6677 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6678 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6679 signature is part of the header. */
6680 static gdb::optional<ULONGEST>
6681 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6682 {
6683 if (cu->header.version >= 5)
6684 return cu->header.signature;
6685 struct attribute *attr;
6686 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6687 if (attr == nullptr)
6688 return gdb::optional<ULONGEST> ();
6689 return DW_UNSND (attr);
6690 }
6691
6692 /* Subroutine of cutu_reader to simplify it.
6693 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6694 Returns NULL if the specified DWO unit cannot be found. */
6695
6696 static struct dwo_unit *
6697 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6698 struct die_info *comp_unit_die,
6699 const char *dwo_name)
6700 {
6701 struct dwarf2_cu *cu = this_cu->cu;
6702 struct dwo_unit *dwo_unit;
6703 const char *comp_dir;
6704
6705 gdb_assert (cu != NULL);
6706
6707 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6708 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6709 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6710
6711 if (this_cu->is_debug_types)
6712 {
6713 struct signatured_type *sig_type;
6714
6715 /* Since this_cu is the first member of struct signatured_type,
6716 we can go from a pointer to one to a pointer to the other. */
6717 sig_type = (struct signatured_type *) this_cu;
6718 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6719 }
6720 else
6721 {
6722 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6723 if (!signature.has_value ())
6724 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6725 " [in module %s]"),
6726 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6727 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6728 *signature);
6729 }
6730
6731 return dwo_unit;
6732 }
6733
6734 /* Subroutine of cutu_reader to simplify it.
6735 See it for a description of the parameters.
6736 Read a TU directly from a DWO file, bypassing the stub. */
6737
6738 void
6739 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6740 int use_existing_cu)
6741 {
6742 struct signatured_type *sig_type;
6743
6744 /* Verify we can do the following downcast, and that we have the
6745 data we need. */
6746 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6747 sig_type = (struct signatured_type *) this_cu;
6748 gdb_assert (sig_type->dwo_unit != NULL);
6749
6750 if (use_existing_cu && this_cu->cu != NULL)
6751 {
6752 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6753 /* There's no need to do the rereading_dwo_cu handling that
6754 cutu_reader does since we don't read the stub. */
6755 }
6756 else
6757 {
6758 /* If !use_existing_cu, this_cu->cu must be NULL. */
6759 gdb_assert (this_cu->cu == NULL);
6760 m_new_cu.reset (new dwarf2_cu (this_cu));
6761 }
6762
6763 /* A future optimization, if needed, would be to use an existing
6764 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6765 could share abbrev tables. */
6766
6767 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6768 NULL /* stub_comp_unit_die */,
6769 sig_type->dwo_unit->dwo_file->comp_dir,
6770 this, &info_ptr,
6771 &comp_unit_die,
6772 &m_dwo_abbrev_table) == 0)
6773 {
6774 /* Dummy die. */
6775 dummy_p = true;
6776 }
6777 }
6778
6779 /* Initialize a CU (or TU) and read its DIEs.
6780 If the CU defers to a DWO file, read the DWO file as well.
6781
6782 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6783 Otherwise the table specified in the comp unit header is read in and used.
6784 This is an optimization for when we already have the abbrev table.
6785
6786 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6787 Otherwise, a new CU is allocated with xmalloc. */
6788
6789 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6790 struct abbrev_table *abbrev_table,
6791 int use_existing_cu,
6792 bool skip_partial)
6793 : die_reader_specs {},
6794 m_this_cu (this_cu)
6795 {
6796 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6797 struct objfile *objfile = dwarf2_per_objfile->objfile;
6798 struct dwarf2_section_info *section = this_cu->section;
6799 bfd *abfd = section->get_bfd_owner ();
6800 struct dwarf2_cu *cu;
6801 const gdb_byte *begin_info_ptr;
6802 struct signatured_type *sig_type = NULL;
6803 struct dwarf2_section_info *abbrev_section;
6804 /* Non-zero if CU currently points to a DWO file and we need to
6805 reread it. When this happens we need to reread the skeleton die
6806 before we can reread the DWO file (this only applies to CUs, not TUs). */
6807 int rereading_dwo_cu = 0;
6808
6809 if (dwarf_die_debug)
6810 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6811 this_cu->is_debug_types ? "type" : "comp",
6812 sect_offset_str (this_cu->sect_off));
6813
6814 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6815 file (instead of going through the stub), short-circuit all of this. */
6816 if (this_cu->reading_dwo_directly)
6817 {
6818 /* Narrow down the scope of possibilities to have to understand. */
6819 gdb_assert (this_cu->is_debug_types);
6820 gdb_assert (abbrev_table == NULL);
6821 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6822 return;
6823 }
6824
6825 /* This is cheap if the section is already read in. */
6826 section->read (objfile);
6827
6828 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6829
6830 abbrev_section = get_abbrev_section_for_cu (this_cu);
6831
6832 if (use_existing_cu && this_cu->cu != NULL)
6833 {
6834 cu = this_cu->cu;
6835 /* If this CU is from a DWO file we need to start over, we need to
6836 refetch the attributes from the skeleton CU.
6837 This could be optimized by retrieving those attributes from when we
6838 were here the first time: the previous comp_unit_die was stored in
6839 comp_unit_obstack. But there's no data yet that we need this
6840 optimization. */
6841 if (cu->dwo_unit != NULL)
6842 rereading_dwo_cu = 1;
6843 }
6844 else
6845 {
6846 /* If !use_existing_cu, this_cu->cu must be NULL. */
6847 gdb_assert (this_cu->cu == NULL);
6848 m_new_cu.reset (new dwarf2_cu (this_cu));
6849 cu = m_new_cu.get ();
6850 }
6851
6852 /* Get the header. */
6853 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6854 {
6855 /* We already have the header, there's no need to read it in again. */
6856 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6857 }
6858 else
6859 {
6860 if (this_cu->is_debug_types)
6861 {
6862 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6863 &cu->header, section,
6864 abbrev_section, info_ptr,
6865 rcuh_kind::TYPE);
6866
6867 /* Since per_cu is the first member of struct signatured_type,
6868 we can go from a pointer to one to a pointer to the other. */
6869 sig_type = (struct signatured_type *) this_cu;
6870 gdb_assert (sig_type->signature == cu->header.signature);
6871 gdb_assert (sig_type->type_offset_in_tu
6872 == cu->header.type_cu_offset_in_tu);
6873 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6874
6875 /* LENGTH has not been set yet for type units if we're
6876 using .gdb_index. */
6877 this_cu->length = cu->header.get_length ();
6878
6879 /* Establish the type offset that can be used to lookup the type. */
6880 sig_type->type_offset_in_section =
6881 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6882
6883 this_cu->dwarf_version = cu->header.version;
6884 }
6885 else
6886 {
6887 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6888 &cu->header, section,
6889 abbrev_section,
6890 info_ptr,
6891 rcuh_kind::COMPILE);
6892
6893 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6894 gdb_assert (this_cu->length == cu->header.get_length ());
6895 this_cu->dwarf_version = cu->header.version;
6896 }
6897 }
6898
6899 /* Skip dummy compilation units. */
6900 if (info_ptr >= begin_info_ptr + this_cu->length
6901 || peek_abbrev_code (abfd, info_ptr) == 0)
6902 {
6903 dummy_p = true;
6904 return;
6905 }
6906
6907 /* If we don't have them yet, read the abbrevs for this compilation unit.
6908 And if we need to read them now, make sure they're freed when we're
6909 done. */
6910 if (abbrev_table != NULL)
6911 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6912 else
6913 {
6914 m_abbrev_table_holder
6915 = abbrev_table::read (objfile, abbrev_section,
6916 cu->header.abbrev_sect_off);
6917 abbrev_table = m_abbrev_table_holder.get ();
6918 }
6919
6920 /* Read the top level CU/TU die. */
6921 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6922 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6923
6924 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6925 {
6926 dummy_p = true;
6927 return;
6928 }
6929
6930 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6931 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6932 table from the DWO file and pass the ownership over to us. It will be
6933 referenced from READER, so we must make sure to free it after we're done
6934 with READER.
6935
6936 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6937 DWO CU, that this test will fail (the attribute will not be present). */
6938 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6939 if (dwo_name != nullptr)
6940 {
6941 struct dwo_unit *dwo_unit;
6942 struct die_info *dwo_comp_unit_die;
6943
6944 if (comp_unit_die->has_children)
6945 {
6946 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6947 " has children (offset %s) [in module %s]"),
6948 sect_offset_str (this_cu->sect_off),
6949 bfd_get_filename (abfd));
6950 }
6951 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6952 if (dwo_unit != NULL)
6953 {
6954 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6955 comp_unit_die, NULL,
6956 this, &info_ptr,
6957 &dwo_comp_unit_die,
6958 &m_dwo_abbrev_table) == 0)
6959 {
6960 /* Dummy die. */
6961 dummy_p = true;
6962 return;
6963 }
6964 comp_unit_die = dwo_comp_unit_die;
6965 }
6966 else
6967 {
6968 /* Yikes, we couldn't find the rest of the DIE, we only have
6969 the stub. A complaint has already been logged. There's
6970 not much more we can do except pass on the stub DIE to
6971 die_reader_func. We don't want to throw an error on bad
6972 debug info. */
6973 }
6974 }
6975 }
6976
6977 void
6978 cutu_reader::keep ()
6979 {
6980 /* Done, clean up. */
6981 gdb_assert (!dummy_p);
6982 if (m_new_cu != NULL)
6983 {
6984 struct dwarf2_per_objfile *dwarf2_per_objfile
6985 = m_this_cu->dwarf2_per_objfile;
6986 /* Link this CU into read_in_chain. */
6987 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6988 dwarf2_per_objfile->read_in_chain = m_this_cu;
6989 /* The chain owns it now. */
6990 m_new_cu.release ();
6991 }
6992 }
6993
6994 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6995 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6996 assumed to have already done the lookup to find the DWO file).
6997
6998 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6999 THIS_CU->is_debug_types, but nothing else.
7000
7001 We fill in THIS_CU->length.
7002
7003 THIS_CU->cu is always freed when done.
7004 This is done in order to not leave THIS_CU->cu in a state where we have
7005 to care whether it refers to the "main" CU or the DWO CU.
7006
7007 When parent_cu is passed, it is used to provide a default value for
7008 str_offsets_base and addr_base from the parent. */
7009
7010 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7011 struct dwarf2_cu *parent_cu,
7012 struct dwo_file *dwo_file)
7013 : die_reader_specs {},
7014 m_this_cu (this_cu)
7015 {
7016 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7017 struct objfile *objfile = dwarf2_per_objfile->objfile;
7018 struct dwarf2_section_info *section = this_cu->section;
7019 bfd *abfd = section->get_bfd_owner ();
7020 struct dwarf2_section_info *abbrev_section;
7021 const gdb_byte *begin_info_ptr, *info_ptr;
7022
7023 if (dwarf_die_debug)
7024 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7025 this_cu->is_debug_types ? "type" : "comp",
7026 sect_offset_str (this_cu->sect_off));
7027
7028 gdb_assert (this_cu->cu == NULL);
7029
7030 abbrev_section = (dwo_file != NULL
7031 ? &dwo_file->sections.abbrev
7032 : get_abbrev_section_for_cu (this_cu));
7033
7034 /* This is cheap if the section is already read in. */
7035 section->read (objfile);
7036
7037 m_new_cu.reset (new dwarf2_cu (this_cu));
7038
7039 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7040 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7041 &m_new_cu->header, section,
7042 abbrev_section, info_ptr,
7043 (this_cu->is_debug_types
7044 ? rcuh_kind::TYPE
7045 : rcuh_kind::COMPILE));
7046
7047 if (parent_cu != nullptr)
7048 {
7049 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7050 m_new_cu->addr_base = parent_cu->addr_base;
7051 }
7052 this_cu->length = m_new_cu->header.get_length ();
7053
7054 /* Skip dummy compilation units. */
7055 if (info_ptr >= begin_info_ptr + this_cu->length
7056 || peek_abbrev_code (abfd, info_ptr) == 0)
7057 {
7058 dummy_p = true;
7059 return;
7060 }
7061
7062 m_abbrev_table_holder
7063 = abbrev_table::read (objfile, abbrev_section,
7064 m_new_cu->header.abbrev_sect_off);
7065
7066 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7067 m_abbrev_table_holder.get ());
7068 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7069 }
7070
7071 \f
7072 /* Type Unit Groups.
7073
7074 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7075 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7076 so that all types coming from the same compilation (.o file) are grouped
7077 together. A future step could be to put the types in the same symtab as
7078 the CU the types ultimately came from. */
7079
7080 static hashval_t
7081 hash_type_unit_group (const void *item)
7082 {
7083 const struct type_unit_group *tu_group
7084 = (const struct type_unit_group *) item;
7085
7086 return hash_stmt_list_entry (&tu_group->hash);
7087 }
7088
7089 static int
7090 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7091 {
7092 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7093 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7094
7095 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7096 }
7097
7098 /* Allocate a hash table for type unit groups. */
7099
7100 static htab_up
7101 allocate_type_unit_groups_table ()
7102 {
7103 return htab_up (htab_create_alloc (3,
7104 hash_type_unit_group,
7105 eq_type_unit_group,
7106 NULL, xcalloc, xfree));
7107 }
7108
7109 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7110 partial symtabs. We combine several TUs per psymtab to not let the size
7111 of any one psymtab grow too big. */
7112 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7113 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7114
7115 /* Helper routine for get_type_unit_group.
7116 Create the type_unit_group object used to hold one or more TUs. */
7117
7118 static struct type_unit_group *
7119 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7120 {
7121 struct dwarf2_per_objfile *dwarf2_per_objfile
7122 = cu->per_cu->dwarf2_per_objfile;
7123 struct objfile *objfile = dwarf2_per_objfile->objfile;
7124 struct dwarf2_per_cu_data *per_cu;
7125 struct type_unit_group *tu_group;
7126
7127 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7128 struct type_unit_group);
7129 per_cu = &tu_group->per_cu;
7130 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7131
7132 if (dwarf2_per_objfile->using_index)
7133 {
7134 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7135 struct dwarf2_per_cu_quick_data);
7136 }
7137 else
7138 {
7139 unsigned int line_offset = to_underlying (line_offset_struct);
7140 dwarf2_psymtab *pst;
7141 std::string name;
7142
7143 /* Give the symtab a useful name for debug purposes. */
7144 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7145 name = string_printf ("<type_units_%d>",
7146 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7147 else
7148 name = string_printf ("<type_units_at_0x%x>", line_offset);
7149
7150 pst = create_partial_symtab (per_cu, name.c_str ());
7151 pst->anonymous = true;
7152 }
7153
7154 tu_group->hash.dwo_unit = cu->dwo_unit;
7155 tu_group->hash.line_sect_off = line_offset_struct;
7156
7157 return tu_group;
7158 }
7159
7160 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7161 STMT_LIST is a DW_AT_stmt_list attribute. */
7162
7163 static struct type_unit_group *
7164 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7165 {
7166 struct dwarf2_per_objfile *dwarf2_per_objfile
7167 = cu->per_cu->dwarf2_per_objfile;
7168 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7169 struct type_unit_group *tu_group;
7170 void **slot;
7171 unsigned int line_offset;
7172 struct type_unit_group type_unit_group_for_lookup;
7173
7174 if (dwarf2_per_objfile->type_unit_groups == NULL)
7175 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7176
7177 /* Do we need to create a new group, or can we use an existing one? */
7178
7179 if (stmt_list)
7180 {
7181 line_offset = DW_UNSND (stmt_list);
7182 ++tu_stats->nr_symtab_sharers;
7183 }
7184 else
7185 {
7186 /* Ugh, no stmt_list. Rare, but we have to handle it.
7187 We can do various things here like create one group per TU or
7188 spread them over multiple groups to split up the expansion work.
7189 To avoid worst case scenarios (too many groups or too large groups)
7190 we, umm, group them in bunches. */
7191 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7192 | (tu_stats->nr_stmt_less_type_units
7193 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7194 ++tu_stats->nr_stmt_less_type_units;
7195 }
7196
7197 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7198 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7199 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7200 &type_unit_group_for_lookup, INSERT);
7201 if (*slot != NULL)
7202 {
7203 tu_group = (struct type_unit_group *) *slot;
7204 gdb_assert (tu_group != NULL);
7205 }
7206 else
7207 {
7208 sect_offset line_offset_struct = (sect_offset) line_offset;
7209 tu_group = create_type_unit_group (cu, line_offset_struct);
7210 *slot = tu_group;
7211 ++tu_stats->nr_symtabs;
7212 }
7213
7214 return tu_group;
7215 }
7216 \f
7217 /* Partial symbol tables. */
7218
7219 /* Create a psymtab named NAME and assign it to PER_CU.
7220
7221 The caller must fill in the following details:
7222 dirname, textlow, texthigh. */
7223
7224 static dwarf2_psymtab *
7225 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7226 {
7227 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7228 dwarf2_psymtab *pst;
7229
7230 pst = new dwarf2_psymtab (name, objfile, 0);
7231
7232 pst->psymtabs_addrmap_supported = true;
7233
7234 /* This is the glue that links PST into GDB's symbol API. */
7235 pst->per_cu_data = per_cu;
7236 per_cu->v.psymtab = pst;
7237
7238 return pst;
7239 }
7240
7241 /* DIE reader function for process_psymtab_comp_unit. */
7242
7243 static void
7244 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7245 const gdb_byte *info_ptr,
7246 struct die_info *comp_unit_die,
7247 enum language pretend_language)
7248 {
7249 struct dwarf2_cu *cu = reader->cu;
7250 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7251 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7252 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7253 CORE_ADDR baseaddr;
7254 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7255 dwarf2_psymtab *pst;
7256 enum pc_bounds_kind cu_bounds_kind;
7257 const char *filename;
7258
7259 gdb_assert (! per_cu->is_debug_types);
7260
7261 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7262
7263 /* Allocate a new partial symbol table structure. */
7264 gdb::unique_xmalloc_ptr<char> debug_filename;
7265 static const char artificial[] = "<artificial>";
7266 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7267 if (filename == NULL)
7268 filename = "";
7269 else if (strcmp (filename, artificial) == 0)
7270 {
7271 debug_filename.reset (concat (artificial, "@",
7272 sect_offset_str (per_cu->sect_off),
7273 (char *) NULL));
7274 filename = debug_filename.get ();
7275 }
7276
7277 pst = create_partial_symtab (per_cu, filename);
7278
7279 /* This must be done before calling dwarf2_build_include_psymtabs. */
7280 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7281
7282 baseaddr = objfile->text_section_offset ();
7283
7284 dwarf2_find_base_address (comp_unit_die, cu);
7285
7286 /* Possibly set the default values of LOWPC and HIGHPC from
7287 `DW_AT_ranges'. */
7288 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7289 &best_highpc, cu, pst);
7290 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7291 {
7292 CORE_ADDR low
7293 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7294 - baseaddr);
7295 CORE_ADDR high
7296 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7297 - baseaddr - 1);
7298 /* Store the contiguous range if it is not empty; it can be
7299 empty for CUs with no code. */
7300 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7301 low, high, pst);
7302 }
7303
7304 /* Check if comp unit has_children.
7305 If so, read the rest of the partial symbols from this comp unit.
7306 If not, there's no more debug_info for this comp unit. */
7307 if (comp_unit_die->has_children)
7308 {
7309 struct partial_die_info *first_die;
7310 CORE_ADDR lowpc, highpc;
7311
7312 lowpc = ((CORE_ADDR) -1);
7313 highpc = ((CORE_ADDR) 0);
7314
7315 first_die = load_partial_dies (reader, info_ptr, 1);
7316
7317 scan_partial_symbols (first_die, &lowpc, &highpc,
7318 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7319
7320 /* If we didn't find a lowpc, set it to highpc to avoid
7321 complaints from `maint check'. */
7322 if (lowpc == ((CORE_ADDR) -1))
7323 lowpc = highpc;
7324
7325 /* If the compilation unit didn't have an explicit address range,
7326 then use the information extracted from its child dies. */
7327 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7328 {
7329 best_lowpc = lowpc;
7330 best_highpc = highpc;
7331 }
7332 }
7333 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7334 best_lowpc + baseaddr)
7335 - baseaddr);
7336 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7337 best_highpc + baseaddr)
7338 - baseaddr);
7339
7340 end_psymtab_common (objfile, pst);
7341
7342 if (!cu->per_cu->imported_symtabs_empty ())
7343 {
7344 int i;
7345 int len = cu->per_cu->imported_symtabs_size ();
7346
7347 /* Fill in 'dependencies' here; we fill in 'users' in a
7348 post-pass. */
7349 pst->number_of_dependencies = len;
7350 pst->dependencies
7351 = objfile->partial_symtabs->allocate_dependencies (len);
7352 for (i = 0; i < len; ++i)
7353 {
7354 pst->dependencies[i]
7355 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7356 }
7357
7358 cu->per_cu->imported_symtabs_free ();
7359 }
7360
7361 /* Get the list of files included in the current compilation unit,
7362 and build a psymtab for each of them. */
7363 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7364
7365 if (dwarf_read_debug)
7366 fprintf_unfiltered (gdb_stdlog,
7367 "Psymtab for %s unit @%s: %s - %s"
7368 ", %d global, %d static syms\n",
7369 per_cu->is_debug_types ? "type" : "comp",
7370 sect_offset_str (per_cu->sect_off),
7371 paddress (gdbarch, pst->text_low (objfile)),
7372 paddress (gdbarch, pst->text_high (objfile)),
7373 pst->n_global_syms, pst->n_static_syms);
7374 }
7375
7376 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7377 Process compilation unit THIS_CU for a psymtab. */
7378
7379 static void
7380 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7381 bool want_partial_unit,
7382 enum language pretend_language)
7383 {
7384 /* If this compilation unit was already read in, free the
7385 cached copy in order to read it in again. This is
7386 necessary because we skipped some symbols when we first
7387 read in the compilation unit (see load_partial_dies).
7388 This problem could be avoided, but the benefit is unclear. */
7389 if (this_cu->cu != NULL)
7390 free_one_cached_comp_unit (this_cu);
7391
7392 cutu_reader reader (this_cu, NULL, 0, false);
7393
7394 switch (reader.comp_unit_die->tag)
7395 {
7396 case DW_TAG_compile_unit:
7397 this_cu->unit_type = DW_UT_compile;
7398 break;
7399 case DW_TAG_partial_unit:
7400 this_cu->unit_type = DW_UT_partial;
7401 break;
7402 default:
7403 abort ();
7404 }
7405
7406 if (reader.dummy_p)
7407 {
7408 /* Nothing. */
7409 }
7410 else if (this_cu->is_debug_types)
7411 build_type_psymtabs_reader (&reader, reader.info_ptr,
7412 reader.comp_unit_die);
7413 else if (want_partial_unit
7414 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7415 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7416 reader.comp_unit_die,
7417 pretend_language);
7418
7419 this_cu->lang = this_cu->cu->language;
7420
7421 /* Age out any secondary CUs. */
7422 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7423 }
7424
7425 /* Reader function for build_type_psymtabs. */
7426
7427 static void
7428 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7429 const gdb_byte *info_ptr,
7430 struct die_info *type_unit_die)
7431 {
7432 struct dwarf2_per_objfile *dwarf2_per_objfile
7433 = reader->cu->per_cu->dwarf2_per_objfile;
7434 struct objfile *objfile = dwarf2_per_objfile->objfile;
7435 struct dwarf2_cu *cu = reader->cu;
7436 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7437 struct signatured_type *sig_type;
7438 struct type_unit_group *tu_group;
7439 struct attribute *attr;
7440 struct partial_die_info *first_die;
7441 CORE_ADDR lowpc, highpc;
7442 dwarf2_psymtab *pst;
7443
7444 gdb_assert (per_cu->is_debug_types);
7445 sig_type = (struct signatured_type *) per_cu;
7446
7447 if (! type_unit_die->has_children)
7448 return;
7449
7450 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7451 tu_group = get_type_unit_group (cu, attr);
7452
7453 if (tu_group->tus == nullptr)
7454 tu_group->tus = new std::vector<signatured_type *>;
7455 tu_group->tus->push_back (sig_type);
7456
7457 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7458 pst = create_partial_symtab (per_cu, "");
7459 pst->anonymous = true;
7460
7461 first_die = load_partial_dies (reader, info_ptr, 1);
7462
7463 lowpc = (CORE_ADDR) -1;
7464 highpc = (CORE_ADDR) 0;
7465 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7466
7467 end_psymtab_common (objfile, pst);
7468 }
7469
7470 /* Struct used to sort TUs by their abbreviation table offset. */
7471
7472 struct tu_abbrev_offset
7473 {
7474 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7475 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7476 {}
7477
7478 signatured_type *sig_type;
7479 sect_offset abbrev_offset;
7480 };
7481
7482 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7483
7484 static bool
7485 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7486 const struct tu_abbrev_offset &b)
7487 {
7488 return a.abbrev_offset < b.abbrev_offset;
7489 }
7490
7491 /* Efficiently read all the type units.
7492 This does the bulk of the work for build_type_psymtabs.
7493
7494 The efficiency is because we sort TUs by the abbrev table they use and
7495 only read each abbrev table once. In one program there are 200K TUs
7496 sharing 8K abbrev tables.
7497
7498 The main purpose of this function is to support building the
7499 dwarf2_per_objfile->type_unit_groups table.
7500 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7501 can collapse the search space by grouping them by stmt_list.
7502 The savings can be significant, in the same program from above the 200K TUs
7503 share 8K stmt_list tables.
7504
7505 FUNC is expected to call get_type_unit_group, which will create the
7506 struct type_unit_group if necessary and add it to
7507 dwarf2_per_objfile->type_unit_groups. */
7508
7509 static void
7510 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7511 {
7512 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7513 abbrev_table_up abbrev_table;
7514 sect_offset abbrev_offset;
7515
7516 /* It's up to the caller to not call us multiple times. */
7517 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7518
7519 if (dwarf2_per_objfile->all_type_units.empty ())
7520 return;
7521
7522 /* TUs typically share abbrev tables, and there can be way more TUs than
7523 abbrev tables. Sort by abbrev table to reduce the number of times we
7524 read each abbrev table in.
7525 Alternatives are to punt or to maintain a cache of abbrev tables.
7526 This is simpler and efficient enough for now.
7527
7528 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7529 symtab to use). Typically TUs with the same abbrev offset have the same
7530 stmt_list value too so in practice this should work well.
7531
7532 The basic algorithm here is:
7533
7534 sort TUs by abbrev table
7535 for each TU with same abbrev table:
7536 read abbrev table if first user
7537 read TU top level DIE
7538 [IWBN if DWO skeletons had DW_AT_stmt_list]
7539 call FUNC */
7540
7541 if (dwarf_read_debug)
7542 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7543
7544 /* Sort in a separate table to maintain the order of all_type_units
7545 for .gdb_index: TU indices directly index all_type_units. */
7546 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7547 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7548
7549 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7550 sorted_by_abbrev.emplace_back
7551 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7552 sig_type->per_cu.section,
7553 sig_type->per_cu.sect_off));
7554
7555 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7556 sort_tu_by_abbrev_offset);
7557
7558 abbrev_offset = (sect_offset) ~(unsigned) 0;
7559
7560 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7561 {
7562 /* Switch to the next abbrev table if necessary. */
7563 if (abbrev_table == NULL
7564 || tu.abbrev_offset != abbrev_offset)
7565 {
7566 abbrev_offset = tu.abbrev_offset;
7567 abbrev_table =
7568 abbrev_table::read (dwarf2_per_objfile->objfile,
7569 &dwarf2_per_objfile->abbrev,
7570 abbrev_offset);
7571 ++tu_stats->nr_uniq_abbrev_tables;
7572 }
7573
7574 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7575 0, false);
7576 if (!reader.dummy_p)
7577 build_type_psymtabs_reader (&reader, reader.info_ptr,
7578 reader.comp_unit_die);
7579 }
7580 }
7581
7582 /* Print collected type unit statistics. */
7583
7584 static void
7585 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7586 {
7587 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7588
7589 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7590 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7591 dwarf2_per_objfile->all_type_units.size ());
7592 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7593 tu_stats->nr_uniq_abbrev_tables);
7594 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7595 tu_stats->nr_symtabs);
7596 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7597 tu_stats->nr_symtab_sharers);
7598 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7599 tu_stats->nr_stmt_less_type_units);
7600 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7601 tu_stats->nr_all_type_units_reallocs);
7602 }
7603
7604 /* Traversal function for build_type_psymtabs. */
7605
7606 static int
7607 build_type_psymtab_dependencies (void **slot, void *info)
7608 {
7609 struct dwarf2_per_objfile *dwarf2_per_objfile
7610 = (struct dwarf2_per_objfile *) info;
7611 struct objfile *objfile = dwarf2_per_objfile->objfile;
7612 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7613 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7614 dwarf2_psymtab *pst = per_cu->v.psymtab;
7615 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7616 int i;
7617
7618 gdb_assert (len > 0);
7619 gdb_assert (per_cu->type_unit_group_p ());
7620
7621 pst->number_of_dependencies = len;
7622 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7623 for (i = 0; i < len; ++i)
7624 {
7625 struct signatured_type *iter = tu_group->tus->at (i);
7626 gdb_assert (iter->per_cu.is_debug_types);
7627 pst->dependencies[i] = iter->per_cu.v.psymtab;
7628 iter->type_unit_group = tu_group;
7629 }
7630
7631 delete tu_group->tus;
7632 tu_group->tus = nullptr;
7633
7634 return 1;
7635 }
7636
7637 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7638 Build partial symbol tables for the .debug_types comp-units. */
7639
7640 static void
7641 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7642 {
7643 if (! create_all_type_units (dwarf2_per_objfile))
7644 return;
7645
7646 build_type_psymtabs_1 (dwarf2_per_objfile);
7647 }
7648
7649 /* Traversal function for process_skeletonless_type_unit.
7650 Read a TU in a DWO file and build partial symbols for it. */
7651
7652 static int
7653 process_skeletonless_type_unit (void **slot, void *info)
7654 {
7655 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7656 struct dwarf2_per_objfile *dwarf2_per_objfile
7657 = (struct dwarf2_per_objfile *) info;
7658 struct signatured_type find_entry, *entry;
7659
7660 /* If this TU doesn't exist in the global table, add it and read it in. */
7661
7662 if (dwarf2_per_objfile->signatured_types == NULL)
7663 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7664
7665 find_entry.signature = dwo_unit->signature;
7666 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7667 &find_entry, INSERT);
7668 /* If we've already seen this type there's nothing to do. What's happening
7669 is we're doing our own version of comdat-folding here. */
7670 if (*slot != NULL)
7671 return 1;
7672
7673 /* This does the job that create_all_type_units would have done for
7674 this TU. */
7675 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7676 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7677 *slot = entry;
7678
7679 /* This does the job that build_type_psymtabs_1 would have done. */
7680 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7681 if (!reader.dummy_p)
7682 build_type_psymtabs_reader (&reader, reader.info_ptr,
7683 reader.comp_unit_die);
7684
7685 return 1;
7686 }
7687
7688 /* Traversal function for process_skeletonless_type_units. */
7689
7690 static int
7691 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7692 {
7693 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7694
7695 if (dwo_file->tus != NULL)
7696 htab_traverse_noresize (dwo_file->tus.get (),
7697 process_skeletonless_type_unit, info);
7698
7699 return 1;
7700 }
7701
7702 /* Scan all TUs of DWO files, verifying we've processed them.
7703 This is needed in case a TU was emitted without its skeleton.
7704 Note: This can't be done until we know what all the DWO files are. */
7705
7706 static void
7707 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7708 {
7709 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7710 if (get_dwp_file (dwarf2_per_objfile) == NULL
7711 && dwarf2_per_objfile->dwo_files != NULL)
7712 {
7713 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7714 process_dwo_file_for_skeletonless_type_units,
7715 dwarf2_per_objfile);
7716 }
7717 }
7718
7719 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7720
7721 static void
7722 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7723 {
7724 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7725 {
7726 dwarf2_psymtab *pst = per_cu->v.psymtab;
7727
7728 if (pst == NULL)
7729 continue;
7730
7731 for (int j = 0; j < pst->number_of_dependencies; ++j)
7732 {
7733 /* Set the 'user' field only if it is not already set. */
7734 if (pst->dependencies[j]->user == NULL)
7735 pst->dependencies[j]->user = pst;
7736 }
7737 }
7738 }
7739
7740 /* Build the partial symbol table by doing a quick pass through the
7741 .debug_info and .debug_abbrev sections. */
7742
7743 static void
7744 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7745 {
7746 struct objfile *objfile = dwarf2_per_objfile->objfile;
7747
7748 if (dwarf_read_debug)
7749 {
7750 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7751 objfile_name (objfile));
7752 }
7753
7754 scoped_restore restore_reading_psyms
7755 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7756 true);
7757
7758 dwarf2_per_objfile->info.read (objfile);
7759
7760 /* Any cached compilation units will be linked by the per-objfile
7761 read_in_chain. Make sure to free them when we're done. */
7762 free_cached_comp_units freer (dwarf2_per_objfile);
7763
7764 build_type_psymtabs (dwarf2_per_objfile);
7765
7766 create_all_comp_units (dwarf2_per_objfile);
7767
7768 /* Create a temporary address map on a temporary obstack. We later
7769 copy this to the final obstack. */
7770 auto_obstack temp_obstack;
7771
7772 scoped_restore save_psymtabs_addrmap
7773 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7774 addrmap_create_mutable (&temp_obstack));
7775
7776 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7777 process_psymtab_comp_unit (per_cu, false, language_minimal);
7778
7779 /* This has to wait until we read the CUs, we need the list of DWOs. */
7780 process_skeletonless_type_units (dwarf2_per_objfile);
7781
7782 /* Now that all TUs have been processed we can fill in the dependencies. */
7783 if (dwarf2_per_objfile->type_unit_groups != NULL)
7784 {
7785 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7786 build_type_psymtab_dependencies, dwarf2_per_objfile);
7787 }
7788
7789 if (dwarf_read_debug)
7790 print_tu_stats (dwarf2_per_objfile);
7791
7792 set_partial_user (dwarf2_per_objfile);
7793
7794 objfile->partial_symtabs->psymtabs_addrmap
7795 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7796 objfile->partial_symtabs->obstack ());
7797 /* At this point we want to keep the address map. */
7798 save_psymtabs_addrmap.release ();
7799
7800 if (dwarf_read_debug)
7801 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7802 objfile_name (objfile));
7803 }
7804
7805 /* Load the partial DIEs for a secondary CU into memory.
7806 This is also used when rereading a primary CU with load_all_dies. */
7807
7808 static void
7809 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7810 {
7811 cutu_reader reader (this_cu, NULL, 1, false);
7812
7813 if (!reader.dummy_p)
7814 {
7815 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7816 language_minimal);
7817
7818 /* Check if comp unit has_children.
7819 If so, read the rest of the partial symbols from this comp unit.
7820 If not, there's no more debug_info for this comp unit. */
7821 if (reader.comp_unit_die->has_children)
7822 load_partial_dies (&reader, reader.info_ptr, 0);
7823
7824 reader.keep ();
7825 }
7826 }
7827
7828 static void
7829 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7830 struct dwarf2_section_info *section,
7831 struct dwarf2_section_info *abbrev_section,
7832 unsigned int is_dwz)
7833 {
7834 const gdb_byte *info_ptr;
7835 struct objfile *objfile = dwarf2_per_objfile->objfile;
7836
7837 if (dwarf_read_debug)
7838 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7839 section->get_name (),
7840 section->get_file_name ());
7841
7842 section->read (objfile);
7843
7844 info_ptr = section->buffer;
7845
7846 while (info_ptr < section->buffer + section->size)
7847 {
7848 struct dwarf2_per_cu_data *this_cu;
7849
7850 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7851
7852 comp_unit_head cu_header;
7853 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7854 abbrev_section, info_ptr,
7855 rcuh_kind::COMPILE);
7856
7857 /* Save the compilation unit for later lookup. */
7858 if (cu_header.unit_type != DW_UT_type)
7859 {
7860 this_cu = XOBNEW (&objfile->objfile_obstack,
7861 struct dwarf2_per_cu_data);
7862 memset (this_cu, 0, sizeof (*this_cu));
7863 }
7864 else
7865 {
7866 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7867 struct signatured_type);
7868 memset (sig_type, 0, sizeof (*sig_type));
7869 sig_type->signature = cu_header.signature;
7870 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7871 this_cu = &sig_type->per_cu;
7872 }
7873 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7874 this_cu->sect_off = sect_off;
7875 this_cu->length = cu_header.length + cu_header.initial_length_size;
7876 this_cu->is_dwz = is_dwz;
7877 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7878 this_cu->section = section;
7879
7880 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7881
7882 info_ptr = info_ptr + this_cu->length;
7883 }
7884 }
7885
7886 /* Create a list of all compilation units in OBJFILE.
7887 This is only done for -readnow and building partial symtabs. */
7888
7889 static void
7890 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7891 {
7892 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7893 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7894 &dwarf2_per_objfile->abbrev, 0);
7895
7896 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7897 if (dwz != NULL)
7898 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7899 1);
7900 }
7901
7902 /* Process all loaded DIEs for compilation unit CU, starting at
7903 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7904 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7905 DW_AT_ranges). See the comments of add_partial_subprogram on how
7906 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7907
7908 static void
7909 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7910 CORE_ADDR *highpc, int set_addrmap,
7911 struct dwarf2_cu *cu)
7912 {
7913 struct partial_die_info *pdi;
7914
7915 /* Now, march along the PDI's, descending into ones which have
7916 interesting children but skipping the children of the other ones,
7917 until we reach the end of the compilation unit. */
7918
7919 pdi = first_die;
7920
7921 while (pdi != NULL)
7922 {
7923 pdi->fixup (cu);
7924
7925 /* Anonymous namespaces or modules have no name but have interesting
7926 children, so we need to look at them. Ditto for anonymous
7927 enums. */
7928
7929 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7930 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7931 || pdi->tag == DW_TAG_imported_unit
7932 || pdi->tag == DW_TAG_inlined_subroutine)
7933 {
7934 switch (pdi->tag)
7935 {
7936 case DW_TAG_subprogram:
7937 case DW_TAG_inlined_subroutine:
7938 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7939 break;
7940 case DW_TAG_constant:
7941 case DW_TAG_variable:
7942 case DW_TAG_typedef:
7943 case DW_TAG_union_type:
7944 if (!pdi->is_declaration)
7945 {
7946 add_partial_symbol (pdi, cu);
7947 }
7948 break;
7949 case DW_TAG_class_type:
7950 case DW_TAG_interface_type:
7951 case DW_TAG_structure_type:
7952 if (!pdi->is_declaration)
7953 {
7954 add_partial_symbol (pdi, cu);
7955 }
7956 if ((cu->language == language_rust
7957 || cu->language == language_cplus) && pdi->has_children)
7958 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7959 set_addrmap, cu);
7960 break;
7961 case DW_TAG_enumeration_type:
7962 if (!pdi->is_declaration)
7963 add_partial_enumeration (pdi, cu);
7964 break;
7965 case DW_TAG_base_type:
7966 case DW_TAG_subrange_type:
7967 /* File scope base type definitions are added to the partial
7968 symbol table. */
7969 add_partial_symbol (pdi, cu);
7970 break;
7971 case DW_TAG_namespace:
7972 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7973 break;
7974 case DW_TAG_module:
7975 if (!pdi->is_declaration)
7976 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7977 break;
7978 case DW_TAG_imported_unit:
7979 {
7980 struct dwarf2_per_cu_data *per_cu;
7981
7982 /* For now we don't handle imported units in type units. */
7983 if (cu->per_cu->is_debug_types)
7984 {
7985 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7986 " supported in type units [in module %s]"),
7987 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7988 }
7989
7990 per_cu = dwarf2_find_containing_comp_unit
7991 (pdi->d.sect_off, pdi->is_dwz,
7992 cu->per_cu->dwarf2_per_objfile);
7993
7994 /* Go read the partial unit, if needed. */
7995 if (per_cu->v.psymtab == NULL)
7996 process_psymtab_comp_unit (per_cu, true, cu->language);
7997
7998 cu->per_cu->imported_symtabs_push (per_cu);
7999 }
8000 break;
8001 case DW_TAG_imported_declaration:
8002 add_partial_symbol (pdi, cu);
8003 break;
8004 default:
8005 break;
8006 }
8007 }
8008
8009 /* If the die has a sibling, skip to the sibling. */
8010
8011 pdi = pdi->die_sibling;
8012 }
8013 }
8014
8015 /* Functions used to compute the fully scoped name of a partial DIE.
8016
8017 Normally, this is simple. For C++, the parent DIE's fully scoped
8018 name is concatenated with "::" and the partial DIE's name.
8019 Enumerators are an exception; they use the scope of their parent
8020 enumeration type, i.e. the name of the enumeration type is not
8021 prepended to the enumerator.
8022
8023 There are two complexities. One is DW_AT_specification; in this
8024 case "parent" means the parent of the target of the specification,
8025 instead of the direct parent of the DIE. The other is compilers
8026 which do not emit DW_TAG_namespace; in this case we try to guess
8027 the fully qualified name of structure types from their members'
8028 linkage names. This must be done using the DIE's children rather
8029 than the children of any DW_AT_specification target. We only need
8030 to do this for structures at the top level, i.e. if the target of
8031 any DW_AT_specification (if any; otherwise the DIE itself) does not
8032 have a parent. */
8033
8034 /* Compute the scope prefix associated with PDI's parent, in
8035 compilation unit CU. The result will be allocated on CU's
8036 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8037 field. NULL is returned if no prefix is necessary. */
8038 static const char *
8039 partial_die_parent_scope (struct partial_die_info *pdi,
8040 struct dwarf2_cu *cu)
8041 {
8042 const char *grandparent_scope;
8043 struct partial_die_info *parent, *real_pdi;
8044
8045 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8046 then this means the parent of the specification DIE. */
8047
8048 real_pdi = pdi;
8049 while (real_pdi->has_specification)
8050 {
8051 auto res = find_partial_die (real_pdi->spec_offset,
8052 real_pdi->spec_is_dwz, cu);
8053 real_pdi = res.pdi;
8054 cu = res.cu;
8055 }
8056
8057 parent = real_pdi->die_parent;
8058 if (parent == NULL)
8059 return NULL;
8060
8061 if (parent->scope_set)
8062 return parent->scope;
8063
8064 parent->fixup (cu);
8065
8066 grandparent_scope = partial_die_parent_scope (parent, cu);
8067
8068 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8069 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8070 Work around this problem here. */
8071 if (cu->language == language_cplus
8072 && parent->tag == DW_TAG_namespace
8073 && strcmp (parent->name, "::") == 0
8074 && grandparent_scope == NULL)
8075 {
8076 parent->scope = NULL;
8077 parent->scope_set = 1;
8078 return NULL;
8079 }
8080
8081 /* Nested subroutines in Fortran get a prefix. */
8082 if (pdi->tag == DW_TAG_enumerator)
8083 /* Enumerators should not get the name of the enumeration as a prefix. */
8084 parent->scope = grandparent_scope;
8085 else if (parent->tag == DW_TAG_namespace
8086 || parent->tag == DW_TAG_module
8087 || parent->tag == DW_TAG_structure_type
8088 || parent->tag == DW_TAG_class_type
8089 || parent->tag == DW_TAG_interface_type
8090 || parent->tag == DW_TAG_union_type
8091 || parent->tag == DW_TAG_enumeration_type
8092 || (cu->language == language_fortran
8093 && parent->tag == DW_TAG_subprogram
8094 && pdi->tag == DW_TAG_subprogram))
8095 {
8096 if (grandparent_scope == NULL)
8097 parent->scope = parent->name;
8098 else
8099 parent->scope = typename_concat (&cu->comp_unit_obstack,
8100 grandparent_scope,
8101 parent->name, 0, cu);
8102 }
8103 else
8104 {
8105 /* FIXME drow/2004-04-01: What should we be doing with
8106 function-local names? For partial symbols, we should probably be
8107 ignoring them. */
8108 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8109 dwarf_tag_name (parent->tag),
8110 sect_offset_str (pdi->sect_off));
8111 parent->scope = grandparent_scope;
8112 }
8113
8114 parent->scope_set = 1;
8115 return parent->scope;
8116 }
8117
8118 /* Return the fully scoped name associated with PDI, from compilation unit
8119 CU. The result will be allocated with malloc. */
8120
8121 static gdb::unique_xmalloc_ptr<char>
8122 partial_die_full_name (struct partial_die_info *pdi,
8123 struct dwarf2_cu *cu)
8124 {
8125 const char *parent_scope;
8126
8127 /* If this is a template instantiation, we can not work out the
8128 template arguments from partial DIEs. So, unfortunately, we have
8129 to go through the full DIEs. At least any work we do building
8130 types here will be reused if full symbols are loaded later. */
8131 if (pdi->has_template_arguments)
8132 {
8133 pdi->fixup (cu);
8134
8135 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8136 {
8137 struct die_info *die;
8138 struct attribute attr;
8139 struct dwarf2_cu *ref_cu = cu;
8140
8141 /* DW_FORM_ref_addr is using section offset. */
8142 attr.name = (enum dwarf_attribute) 0;
8143 attr.form = DW_FORM_ref_addr;
8144 attr.u.unsnd = to_underlying (pdi->sect_off);
8145 die = follow_die_ref (NULL, &attr, &ref_cu);
8146
8147 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8148 }
8149 }
8150
8151 parent_scope = partial_die_parent_scope (pdi, cu);
8152 if (parent_scope == NULL)
8153 return NULL;
8154 else
8155 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8156 pdi->name, 0, cu));
8157 }
8158
8159 static void
8160 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8161 {
8162 struct dwarf2_per_objfile *dwarf2_per_objfile
8163 = cu->per_cu->dwarf2_per_objfile;
8164 struct objfile *objfile = dwarf2_per_objfile->objfile;
8165 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8166 CORE_ADDR addr = 0;
8167 const char *actual_name = NULL;
8168 CORE_ADDR baseaddr;
8169
8170 baseaddr = objfile->text_section_offset ();
8171
8172 gdb::unique_xmalloc_ptr<char> built_actual_name
8173 = partial_die_full_name (pdi, cu);
8174 if (built_actual_name != NULL)
8175 actual_name = built_actual_name.get ();
8176
8177 if (actual_name == NULL)
8178 actual_name = pdi->name;
8179
8180 switch (pdi->tag)
8181 {
8182 case DW_TAG_inlined_subroutine:
8183 case DW_TAG_subprogram:
8184 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8185 - baseaddr);
8186 if (pdi->is_external
8187 || cu->language == language_ada
8188 || (cu->language == language_fortran
8189 && pdi->die_parent != NULL
8190 && pdi->die_parent->tag == DW_TAG_subprogram))
8191 {
8192 /* Normally, only "external" DIEs are part of the global scope.
8193 But in Ada and Fortran, we want to be able to access nested
8194 procedures globally. So all Ada and Fortran subprograms are
8195 stored in the global scope. */
8196 add_psymbol_to_list (actual_name,
8197 built_actual_name != NULL,
8198 VAR_DOMAIN, LOC_BLOCK,
8199 SECT_OFF_TEXT (objfile),
8200 psymbol_placement::GLOBAL,
8201 addr,
8202 cu->language, objfile);
8203 }
8204 else
8205 {
8206 add_psymbol_to_list (actual_name,
8207 built_actual_name != NULL,
8208 VAR_DOMAIN, LOC_BLOCK,
8209 SECT_OFF_TEXT (objfile),
8210 psymbol_placement::STATIC,
8211 addr, cu->language, objfile);
8212 }
8213
8214 if (pdi->main_subprogram && actual_name != NULL)
8215 set_objfile_main_name (objfile, actual_name, cu->language);
8216 break;
8217 case DW_TAG_constant:
8218 add_psymbol_to_list (actual_name,
8219 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8220 -1, (pdi->is_external
8221 ? psymbol_placement::GLOBAL
8222 : psymbol_placement::STATIC),
8223 0, cu->language, objfile);
8224 break;
8225 case DW_TAG_variable:
8226 if (pdi->d.locdesc)
8227 addr = decode_locdesc (pdi->d.locdesc, cu);
8228
8229 if (pdi->d.locdesc
8230 && addr == 0
8231 && !dwarf2_per_objfile->has_section_at_zero)
8232 {
8233 /* A global or static variable may also have been stripped
8234 out by the linker if unused, in which case its address
8235 will be nullified; do not add such variables into partial
8236 symbol table then. */
8237 }
8238 else if (pdi->is_external)
8239 {
8240 /* Global Variable.
8241 Don't enter into the minimal symbol tables as there is
8242 a minimal symbol table entry from the ELF symbols already.
8243 Enter into partial symbol table if it has a location
8244 descriptor or a type.
8245 If the location descriptor is missing, new_symbol will create
8246 a LOC_UNRESOLVED symbol, the address of the variable will then
8247 be determined from the minimal symbol table whenever the variable
8248 is referenced.
8249 The address for the partial symbol table entry is not
8250 used by GDB, but it comes in handy for debugging partial symbol
8251 table building. */
8252
8253 if (pdi->d.locdesc || pdi->has_type)
8254 add_psymbol_to_list (actual_name,
8255 built_actual_name != NULL,
8256 VAR_DOMAIN, LOC_STATIC,
8257 SECT_OFF_TEXT (objfile),
8258 psymbol_placement::GLOBAL,
8259 addr, cu->language, objfile);
8260 }
8261 else
8262 {
8263 int has_loc = pdi->d.locdesc != NULL;
8264
8265 /* Static Variable. Skip symbols whose value we cannot know (those
8266 without location descriptors or constant values). */
8267 if (!has_loc && !pdi->has_const_value)
8268 return;
8269
8270 add_psymbol_to_list (actual_name,
8271 built_actual_name != NULL,
8272 VAR_DOMAIN, LOC_STATIC,
8273 SECT_OFF_TEXT (objfile),
8274 psymbol_placement::STATIC,
8275 has_loc ? addr : 0,
8276 cu->language, objfile);
8277 }
8278 break;
8279 case DW_TAG_typedef:
8280 case DW_TAG_base_type:
8281 case DW_TAG_subrange_type:
8282 add_psymbol_to_list (actual_name,
8283 built_actual_name != NULL,
8284 VAR_DOMAIN, LOC_TYPEDEF, -1,
8285 psymbol_placement::STATIC,
8286 0, cu->language, objfile);
8287 break;
8288 case DW_TAG_imported_declaration:
8289 case DW_TAG_namespace:
8290 add_psymbol_to_list (actual_name,
8291 built_actual_name != NULL,
8292 VAR_DOMAIN, LOC_TYPEDEF, -1,
8293 psymbol_placement::GLOBAL,
8294 0, cu->language, objfile);
8295 break;
8296 case DW_TAG_module:
8297 /* With Fortran 77 there might be a "BLOCK DATA" module
8298 available without any name. If so, we skip the module as it
8299 doesn't bring any value. */
8300 if (actual_name != nullptr)
8301 add_psymbol_to_list (actual_name,
8302 built_actual_name != NULL,
8303 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8304 psymbol_placement::GLOBAL,
8305 0, cu->language, objfile);
8306 break;
8307 case DW_TAG_class_type:
8308 case DW_TAG_interface_type:
8309 case DW_TAG_structure_type:
8310 case DW_TAG_union_type:
8311 case DW_TAG_enumeration_type:
8312 /* Skip external references. The DWARF standard says in the section
8313 about "Structure, Union, and Class Type Entries": "An incomplete
8314 structure, union or class type is represented by a structure,
8315 union or class entry that does not have a byte size attribute
8316 and that has a DW_AT_declaration attribute." */
8317 if (!pdi->has_byte_size && pdi->is_declaration)
8318 return;
8319
8320 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8321 static vs. global. */
8322 add_psymbol_to_list (actual_name,
8323 built_actual_name != NULL,
8324 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8325 cu->language == language_cplus
8326 ? psymbol_placement::GLOBAL
8327 : psymbol_placement::STATIC,
8328 0, cu->language, objfile);
8329
8330 break;
8331 case DW_TAG_enumerator:
8332 add_psymbol_to_list (actual_name,
8333 built_actual_name != NULL,
8334 VAR_DOMAIN, LOC_CONST, -1,
8335 cu->language == language_cplus
8336 ? psymbol_placement::GLOBAL
8337 : psymbol_placement::STATIC,
8338 0, cu->language, objfile);
8339 break;
8340 default:
8341 break;
8342 }
8343 }
8344
8345 /* Read a partial die corresponding to a namespace; also, add a symbol
8346 corresponding to that namespace to the symbol table. NAMESPACE is
8347 the name of the enclosing namespace. */
8348
8349 static void
8350 add_partial_namespace (struct partial_die_info *pdi,
8351 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8352 int set_addrmap, struct dwarf2_cu *cu)
8353 {
8354 /* Add a symbol for the namespace. */
8355
8356 add_partial_symbol (pdi, cu);
8357
8358 /* Now scan partial symbols in that namespace. */
8359
8360 if (pdi->has_children)
8361 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8362 }
8363
8364 /* Read a partial die corresponding to a Fortran module. */
8365
8366 static void
8367 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8368 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8369 {
8370 /* Add a symbol for the namespace. */
8371
8372 add_partial_symbol (pdi, cu);
8373
8374 /* Now scan partial symbols in that module. */
8375
8376 if (pdi->has_children)
8377 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8378 }
8379
8380 /* Read a partial die corresponding to a subprogram or an inlined
8381 subprogram and create a partial symbol for that subprogram.
8382 When the CU language allows it, this routine also defines a partial
8383 symbol for each nested subprogram that this subprogram contains.
8384 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8385 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8386
8387 PDI may also be a lexical block, in which case we simply search
8388 recursively for subprograms defined inside that lexical block.
8389 Again, this is only performed when the CU language allows this
8390 type of definitions. */
8391
8392 static void
8393 add_partial_subprogram (struct partial_die_info *pdi,
8394 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8395 int set_addrmap, struct dwarf2_cu *cu)
8396 {
8397 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8398 {
8399 if (pdi->has_pc_info)
8400 {
8401 if (pdi->lowpc < *lowpc)
8402 *lowpc = pdi->lowpc;
8403 if (pdi->highpc > *highpc)
8404 *highpc = pdi->highpc;
8405 if (set_addrmap)
8406 {
8407 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8408 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8409 CORE_ADDR baseaddr;
8410 CORE_ADDR this_highpc;
8411 CORE_ADDR this_lowpc;
8412
8413 baseaddr = objfile->text_section_offset ();
8414 this_lowpc
8415 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8416 pdi->lowpc + baseaddr)
8417 - baseaddr);
8418 this_highpc
8419 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8420 pdi->highpc + baseaddr)
8421 - baseaddr);
8422 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8423 this_lowpc, this_highpc - 1,
8424 cu->per_cu->v.psymtab);
8425 }
8426 }
8427
8428 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8429 {
8430 if (!pdi->is_declaration)
8431 /* Ignore subprogram DIEs that do not have a name, they are
8432 illegal. Do not emit a complaint at this point, we will
8433 do so when we convert this psymtab into a symtab. */
8434 if (pdi->name)
8435 add_partial_symbol (pdi, cu);
8436 }
8437 }
8438
8439 if (! pdi->has_children)
8440 return;
8441
8442 if (cu->language == language_ada || cu->language == language_fortran)
8443 {
8444 pdi = pdi->die_child;
8445 while (pdi != NULL)
8446 {
8447 pdi->fixup (cu);
8448 if (pdi->tag == DW_TAG_subprogram
8449 || pdi->tag == DW_TAG_inlined_subroutine
8450 || pdi->tag == DW_TAG_lexical_block)
8451 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8452 pdi = pdi->die_sibling;
8453 }
8454 }
8455 }
8456
8457 /* Read a partial die corresponding to an enumeration type. */
8458
8459 static void
8460 add_partial_enumeration (struct partial_die_info *enum_pdi,
8461 struct dwarf2_cu *cu)
8462 {
8463 struct partial_die_info *pdi;
8464
8465 if (enum_pdi->name != NULL)
8466 add_partial_symbol (enum_pdi, cu);
8467
8468 pdi = enum_pdi->die_child;
8469 while (pdi)
8470 {
8471 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8472 complaint (_("malformed enumerator DIE ignored"));
8473 else
8474 add_partial_symbol (pdi, cu);
8475 pdi = pdi->die_sibling;
8476 }
8477 }
8478
8479 /* Return the initial uleb128 in the die at INFO_PTR. */
8480
8481 static unsigned int
8482 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8483 {
8484 unsigned int bytes_read;
8485
8486 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8487 }
8488
8489 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8490 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8491
8492 Return the corresponding abbrev, or NULL if the number is zero (indicating
8493 an empty DIE). In either case *BYTES_READ will be set to the length of
8494 the initial number. */
8495
8496 static struct abbrev_info *
8497 peek_die_abbrev (const die_reader_specs &reader,
8498 const gdb_byte *info_ptr, unsigned int *bytes_read)
8499 {
8500 dwarf2_cu *cu = reader.cu;
8501 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8502 unsigned int abbrev_number
8503 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8504
8505 if (abbrev_number == 0)
8506 return NULL;
8507
8508 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8509 if (!abbrev)
8510 {
8511 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8512 " at offset %s [in module %s]"),
8513 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8514 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8515 }
8516
8517 return abbrev;
8518 }
8519
8520 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8521 Returns a pointer to the end of a series of DIEs, terminated by an empty
8522 DIE. Any children of the skipped DIEs will also be skipped. */
8523
8524 static const gdb_byte *
8525 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8526 {
8527 while (1)
8528 {
8529 unsigned int bytes_read;
8530 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8531
8532 if (abbrev == NULL)
8533 return info_ptr + bytes_read;
8534 else
8535 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8536 }
8537 }
8538
8539 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8540 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8541 abbrev corresponding to that skipped uleb128 should be passed in
8542 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8543 children. */
8544
8545 static const gdb_byte *
8546 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8547 struct abbrev_info *abbrev)
8548 {
8549 unsigned int bytes_read;
8550 struct attribute attr;
8551 bfd *abfd = reader->abfd;
8552 struct dwarf2_cu *cu = reader->cu;
8553 const gdb_byte *buffer = reader->buffer;
8554 const gdb_byte *buffer_end = reader->buffer_end;
8555 unsigned int form, i;
8556
8557 for (i = 0; i < abbrev->num_attrs; i++)
8558 {
8559 /* The only abbrev we care about is DW_AT_sibling. */
8560 if (abbrev->attrs[i].name == DW_AT_sibling)
8561 {
8562 bool ignored;
8563 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8564 &ignored);
8565 if (attr.form == DW_FORM_ref_addr)
8566 complaint (_("ignoring absolute DW_AT_sibling"));
8567 else
8568 {
8569 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8570 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8571
8572 if (sibling_ptr < info_ptr)
8573 complaint (_("DW_AT_sibling points backwards"));
8574 else if (sibling_ptr > reader->buffer_end)
8575 reader->die_section->overflow_complaint ();
8576 else
8577 return sibling_ptr;
8578 }
8579 }
8580
8581 /* If it isn't DW_AT_sibling, skip this attribute. */
8582 form = abbrev->attrs[i].form;
8583 skip_attribute:
8584 switch (form)
8585 {
8586 case DW_FORM_ref_addr:
8587 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8588 and later it is offset sized. */
8589 if (cu->header.version == 2)
8590 info_ptr += cu->header.addr_size;
8591 else
8592 info_ptr += cu->header.offset_size;
8593 break;
8594 case DW_FORM_GNU_ref_alt:
8595 info_ptr += cu->header.offset_size;
8596 break;
8597 case DW_FORM_addr:
8598 info_ptr += cu->header.addr_size;
8599 break;
8600 case DW_FORM_data1:
8601 case DW_FORM_ref1:
8602 case DW_FORM_flag:
8603 case DW_FORM_strx1:
8604 info_ptr += 1;
8605 break;
8606 case DW_FORM_flag_present:
8607 case DW_FORM_implicit_const:
8608 break;
8609 case DW_FORM_data2:
8610 case DW_FORM_ref2:
8611 case DW_FORM_strx2:
8612 info_ptr += 2;
8613 break;
8614 case DW_FORM_strx3:
8615 info_ptr += 3;
8616 break;
8617 case DW_FORM_data4:
8618 case DW_FORM_ref4:
8619 case DW_FORM_strx4:
8620 info_ptr += 4;
8621 break;
8622 case DW_FORM_data8:
8623 case DW_FORM_ref8:
8624 case DW_FORM_ref_sig8:
8625 info_ptr += 8;
8626 break;
8627 case DW_FORM_data16:
8628 info_ptr += 16;
8629 break;
8630 case DW_FORM_string:
8631 read_direct_string (abfd, info_ptr, &bytes_read);
8632 info_ptr += bytes_read;
8633 break;
8634 case DW_FORM_sec_offset:
8635 case DW_FORM_strp:
8636 case DW_FORM_GNU_strp_alt:
8637 info_ptr += cu->header.offset_size;
8638 break;
8639 case DW_FORM_exprloc:
8640 case DW_FORM_block:
8641 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8642 info_ptr += bytes_read;
8643 break;
8644 case DW_FORM_block1:
8645 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8646 break;
8647 case DW_FORM_block2:
8648 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8649 break;
8650 case DW_FORM_block4:
8651 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8652 break;
8653 case DW_FORM_addrx:
8654 case DW_FORM_strx:
8655 case DW_FORM_sdata:
8656 case DW_FORM_udata:
8657 case DW_FORM_ref_udata:
8658 case DW_FORM_GNU_addr_index:
8659 case DW_FORM_GNU_str_index:
8660 case DW_FORM_rnglistx:
8661 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8662 break;
8663 case DW_FORM_indirect:
8664 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8665 info_ptr += bytes_read;
8666 /* We need to continue parsing from here, so just go back to
8667 the top. */
8668 goto skip_attribute;
8669
8670 default:
8671 error (_("Dwarf Error: Cannot handle %s "
8672 "in DWARF reader [in module %s]"),
8673 dwarf_form_name (form),
8674 bfd_get_filename (abfd));
8675 }
8676 }
8677
8678 if (abbrev->has_children)
8679 return skip_children (reader, info_ptr);
8680 else
8681 return info_ptr;
8682 }
8683
8684 /* Locate ORIG_PDI's sibling.
8685 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8686
8687 static const gdb_byte *
8688 locate_pdi_sibling (const struct die_reader_specs *reader,
8689 struct partial_die_info *orig_pdi,
8690 const gdb_byte *info_ptr)
8691 {
8692 /* Do we know the sibling already? */
8693
8694 if (orig_pdi->sibling)
8695 return orig_pdi->sibling;
8696
8697 /* Are there any children to deal with? */
8698
8699 if (!orig_pdi->has_children)
8700 return info_ptr;
8701
8702 /* Skip the children the long way. */
8703
8704 return skip_children (reader, info_ptr);
8705 }
8706
8707 /* Expand this partial symbol table into a full symbol table. SELF is
8708 not NULL. */
8709
8710 void
8711 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8712 {
8713 struct dwarf2_per_objfile *dwarf2_per_objfile
8714 = get_dwarf2_per_objfile (objfile);
8715
8716 gdb_assert (!readin);
8717 /* If this psymtab is constructed from a debug-only objfile, the
8718 has_section_at_zero flag will not necessarily be correct. We
8719 can get the correct value for this flag by looking at the data
8720 associated with the (presumably stripped) associated objfile. */
8721 if (objfile->separate_debug_objfile_backlink)
8722 {
8723 struct dwarf2_per_objfile *dpo_backlink
8724 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8725
8726 dwarf2_per_objfile->has_section_at_zero
8727 = dpo_backlink->has_section_at_zero;
8728 }
8729
8730 expand_psymtab (objfile);
8731
8732 process_cu_includes (dwarf2_per_objfile);
8733 }
8734 \f
8735 /* Reading in full CUs. */
8736
8737 /* Add PER_CU to the queue. */
8738
8739 static void
8740 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8741 enum language pretend_language)
8742 {
8743 per_cu->queued = 1;
8744 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8745 }
8746
8747 /* If PER_CU is not yet queued, add it to the queue.
8748 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8749 dependency.
8750 The result is non-zero if PER_CU was queued, otherwise the result is zero
8751 meaning either PER_CU is already queued or it is already loaded.
8752
8753 N.B. There is an invariant here that if a CU is queued then it is loaded.
8754 The caller is required to load PER_CU if we return non-zero. */
8755
8756 static int
8757 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8758 struct dwarf2_per_cu_data *per_cu,
8759 enum language pretend_language)
8760 {
8761 /* We may arrive here during partial symbol reading, if we need full
8762 DIEs to process an unusual case (e.g. template arguments). Do
8763 not queue PER_CU, just tell our caller to load its DIEs. */
8764 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8765 {
8766 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8767 return 1;
8768 return 0;
8769 }
8770
8771 /* Mark the dependence relation so that we don't flush PER_CU
8772 too early. */
8773 if (dependent_cu != NULL)
8774 dwarf2_add_dependence (dependent_cu, per_cu);
8775
8776 /* If it's already on the queue, we have nothing to do. */
8777 if (per_cu->queued)
8778 return 0;
8779
8780 /* If the compilation unit is already loaded, just mark it as
8781 used. */
8782 if (per_cu->cu != NULL)
8783 {
8784 per_cu->cu->last_used = 0;
8785 return 0;
8786 }
8787
8788 /* Add it to the queue. */
8789 queue_comp_unit (per_cu, pretend_language);
8790
8791 return 1;
8792 }
8793
8794 /* Process the queue. */
8795
8796 static void
8797 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8798 {
8799 if (dwarf_read_debug)
8800 {
8801 fprintf_unfiltered (gdb_stdlog,
8802 "Expanding one or more symtabs of objfile %s ...\n",
8803 objfile_name (dwarf2_per_objfile->objfile));
8804 }
8805
8806 /* The queue starts out with one item, but following a DIE reference
8807 may load a new CU, adding it to the end of the queue. */
8808 while (!dwarf2_per_objfile->queue.empty ())
8809 {
8810 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8811
8812 if ((dwarf2_per_objfile->using_index
8813 ? !item.per_cu->v.quick->compunit_symtab
8814 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8815 /* Skip dummy CUs. */
8816 && item.per_cu->cu != NULL)
8817 {
8818 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8819 unsigned int debug_print_threshold;
8820 char buf[100];
8821
8822 if (per_cu->is_debug_types)
8823 {
8824 struct signatured_type *sig_type =
8825 (struct signatured_type *) per_cu;
8826
8827 sprintf (buf, "TU %s at offset %s",
8828 hex_string (sig_type->signature),
8829 sect_offset_str (per_cu->sect_off));
8830 /* There can be 100s of TUs.
8831 Only print them in verbose mode. */
8832 debug_print_threshold = 2;
8833 }
8834 else
8835 {
8836 sprintf (buf, "CU at offset %s",
8837 sect_offset_str (per_cu->sect_off));
8838 debug_print_threshold = 1;
8839 }
8840
8841 if (dwarf_read_debug >= debug_print_threshold)
8842 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8843
8844 if (per_cu->is_debug_types)
8845 process_full_type_unit (per_cu, item.pretend_language);
8846 else
8847 process_full_comp_unit (per_cu, item.pretend_language);
8848
8849 if (dwarf_read_debug >= debug_print_threshold)
8850 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8851 }
8852
8853 item.per_cu->queued = 0;
8854 dwarf2_per_objfile->queue.pop ();
8855 }
8856
8857 if (dwarf_read_debug)
8858 {
8859 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8860 objfile_name (dwarf2_per_objfile->objfile));
8861 }
8862 }
8863
8864 /* Read in full symbols for PST, and anything it depends on. */
8865
8866 void
8867 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8868 {
8869 if (readin)
8870 return;
8871
8872 read_dependencies (objfile);
8873
8874 dw2_do_instantiate_symtab (per_cu_data, false);
8875 gdb_assert (get_compunit_symtab () != nullptr);
8876 }
8877
8878 /* Trivial hash function for die_info: the hash value of a DIE
8879 is its offset in .debug_info for this objfile. */
8880
8881 static hashval_t
8882 die_hash (const void *item)
8883 {
8884 const struct die_info *die = (const struct die_info *) item;
8885
8886 return to_underlying (die->sect_off);
8887 }
8888
8889 /* Trivial comparison function for die_info structures: two DIEs
8890 are equal if they have the same offset. */
8891
8892 static int
8893 die_eq (const void *item_lhs, const void *item_rhs)
8894 {
8895 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8896 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8897
8898 return die_lhs->sect_off == die_rhs->sect_off;
8899 }
8900
8901 /* Load the DIEs associated with PER_CU into memory. */
8902
8903 static void
8904 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8905 bool skip_partial,
8906 enum language pretend_language)
8907 {
8908 gdb_assert (! this_cu->is_debug_types);
8909
8910 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8911 if (reader.dummy_p)
8912 return;
8913
8914 struct dwarf2_cu *cu = reader.cu;
8915 const gdb_byte *info_ptr = reader.info_ptr;
8916
8917 gdb_assert (cu->die_hash == NULL);
8918 cu->die_hash =
8919 htab_create_alloc_ex (cu->header.length / 12,
8920 die_hash,
8921 die_eq,
8922 NULL,
8923 &cu->comp_unit_obstack,
8924 hashtab_obstack_allocate,
8925 dummy_obstack_deallocate);
8926
8927 if (reader.comp_unit_die->has_children)
8928 reader.comp_unit_die->child
8929 = read_die_and_siblings (&reader, reader.info_ptr,
8930 &info_ptr, reader.comp_unit_die);
8931 cu->dies = reader.comp_unit_die;
8932 /* comp_unit_die is not stored in die_hash, no need. */
8933
8934 /* We try not to read any attributes in this function, because not
8935 all CUs needed for references have been loaded yet, and symbol
8936 table processing isn't initialized. But we have to set the CU language,
8937 or we won't be able to build types correctly.
8938 Similarly, if we do not read the producer, we can not apply
8939 producer-specific interpretation. */
8940 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8941
8942 reader.keep ();
8943 }
8944
8945 /* Add a DIE to the delayed physname list. */
8946
8947 static void
8948 add_to_method_list (struct type *type, int fnfield_index, int index,
8949 const char *name, struct die_info *die,
8950 struct dwarf2_cu *cu)
8951 {
8952 struct delayed_method_info mi;
8953 mi.type = type;
8954 mi.fnfield_index = fnfield_index;
8955 mi.index = index;
8956 mi.name = name;
8957 mi.die = die;
8958 cu->method_list.push_back (mi);
8959 }
8960
8961 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8962 "const" / "volatile". If so, decrements LEN by the length of the
8963 modifier and return true. Otherwise return false. */
8964
8965 template<size_t N>
8966 static bool
8967 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8968 {
8969 size_t mod_len = sizeof (mod) - 1;
8970 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8971 {
8972 len -= mod_len;
8973 return true;
8974 }
8975 return false;
8976 }
8977
8978 /* Compute the physnames of any methods on the CU's method list.
8979
8980 The computation of method physnames is delayed in order to avoid the
8981 (bad) condition that one of the method's formal parameters is of an as yet
8982 incomplete type. */
8983
8984 static void
8985 compute_delayed_physnames (struct dwarf2_cu *cu)
8986 {
8987 /* Only C++ delays computing physnames. */
8988 if (cu->method_list.empty ())
8989 return;
8990 gdb_assert (cu->language == language_cplus);
8991
8992 for (const delayed_method_info &mi : cu->method_list)
8993 {
8994 const char *physname;
8995 struct fn_fieldlist *fn_flp
8996 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8997 physname = dwarf2_physname (mi.name, mi.die, cu);
8998 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8999 = physname ? physname : "";
9000
9001 /* Since there's no tag to indicate whether a method is a
9002 const/volatile overload, extract that information out of the
9003 demangled name. */
9004 if (physname != NULL)
9005 {
9006 size_t len = strlen (physname);
9007
9008 while (1)
9009 {
9010 if (physname[len] == ')') /* shortcut */
9011 break;
9012 else if (check_modifier (physname, len, " const"))
9013 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9014 else if (check_modifier (physname, len, " volatile"))
9015 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9016 else
9017 break;
9018 }
9019 }
9020 }
9021
9022 /* The list is no longer needed. */
9023 cu->method_list.clear ();
9024 }
9025
9026 /* Go objects should be embedded in a DW_TAG_module DIE,
9027 and it's not clear if/how imported objects will appear.
9028 To keep Go support simple until that's worked out,
9029 go back through what we've read and create something usable.
9030 We could do this while processing each DIE, and feels kinda cleaner,
9031 but that way is more invasive.
9032 This is to, for example, allow the user to type "p var" or "b main"
9033 without having to specify the package name, and allow lookups
9034 of module.object to work in contexts that use the expression
9035 parser. */
9036
9037 static void
9038 fixup_go_packaging (struct dwarf2_cu *cu)
9039 {
9040 gdb::unique_xmalloc_ptr<char> package_name;
9041 struct pending *list;
9042 int i;
9043
9044 for (list = *cu->get_builder ()->get_global_symbols ();
9045 list != NULL;
9046 list = list->next)
9047 {
9048 for (i = 0; i < list->nsyms; ++i)
9049 {
9050 struct symbol *sym = list->symbol[i];
9051
9052 if (sym->language () == language_go
9053 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9054 {
9055 gdb::unique_xmalloc_ptr<char> this_package_name
9056 (go_symbol_package_name (sym));
9057
9058 if (this_package_name == NULL)
9059 continue;
9060 if (package_name == NULL)
9061 package_name = std::move (this_package_name);
9062 else
9063 {
9064 struct objfile *objfile
9065 = cu->per_cu->dwarf2_per_objfile->objfile;
9066 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9067 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9068 (symbol_symtab (sym) != NULL
9069 ? symtab_to_filename_for_display
9070 (symbol_symtab (sym))
9071 : objfile_name (objfile)),
9072 this_package_name.get (), package_name.get ());
9073 }
9074 }
9075 }
9076 }
9077
9078 if (package_name != NULL)
9079 {
9080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9081 const char *saved_package_name = objfile->intern (package_name.get ());
9082 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9083 saved_package_name);
9084 struct symbol *sym;
9085
9086 sym = allocate_symbol (objfile);
9087 sym->set_language (language_go, &objfile->objfile_obstack);
9088 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9089 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9090 e.g., "main" finds the "main" module and not C's main(). */
9091 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9092 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9093 SYMBOL_TYPE (sym) = type;
9094
9095 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9096 }
9097 }
9098
9099 /* Allocate a fully-qualified name consisting of the two parts on the
9100 obstack. */
9101
9102 static const char *
9103 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9104 {
9105 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9106 }
9107
9108 /* A helper that allocates a struct discriminant_info to attach to a
9109 union type. */
9110
9111 static struct discriminant_info *
9112 alloc_discriminant_info (struct type *type, int discriminant_index,
9113 int default_index)
9114 {
9115 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9116 gdb_assert (discriminant_index == -1
9117 || (discriminant_index >= 0
9118 && discriminant_index < TYPE_NFIELDS (type)));
9119 gdb_assert (default_index == -1
9120 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9121
9122 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9123
9124 struct discriminant_info *disc
9125 = ((struct discriminant_info *)
9126 TYPE_ZALLOC (type,
9127 offsetof (struct discriminant_info, discriminants)
9128 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9129 disc->default_index = default_index;
9130 disc->discriminant_index = discriminant_index;
9131
9132 struct dynamic_prop prop;
9133 prop.kind = PROP_UNDEFINED;
9134 prop.data.baton = disc;
9135
9136 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9137
9138 return disc;
9139 }
9140
9141 /* Some versions of rustc emitted enums in an unusual way.
9142
9143 Ordinary enums were emitted as unions. The first element of each
9144 structure in the union was named "RUST$ENUM$DISR". This element
9145 held the discriminant.
9146
9147 These versions of Rust also implemented the "non-zero"
9148 optimization. When the enum had two values, and one is empty and
9149 the other holds a pointer that cannot be zero, the pointer is used
9150 as the discriminant, with a zero value meaning the empty variant.
9151 Here, the union's first member is of the form
9152 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9153 where the fieldnos are the indices of the fields that should be
9154 traversed in order to find the field (which may be several fields deep)
9155 and the variantname is the name of the variant of the case when the
9156 field is zero.
9157
9158 This function recognizes whether TYPE is of one of these forms,
9159 and, if so, smashes it to be a variant type. */
9160
9161 static void
9162 quirk_rust_enum (struct type *type, struct objfile *objfile)
9163 {
9164 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9165
9166 /* We don't need to deal with empty enums. */
9167 if (TYPE_NFIELDS (type) == 0)
9168 return;
9169
9170 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9171 if (TYPE_NFIELDS (type) == 1
9172 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9173 {
9174 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9175
9176 /* Decode the field name to find the offset of the
9177 discriminant. */
9178 ULONGEST bit_offset = 0;
9179 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9180 while (name[0] >= '0' && name[0] <= '9')
9181 {
9182 char *tail;
9183 unsigned long index = strtoul (name, &tail, 10);
9184 name = tail;
9185 if (*name != '$'
9186 || index >= TYPE_NFIELDS (field_type)
9187 || (TYPE_FIELD_LOC_KIND (field_type, index)
9188 != FIELD_LOC_KIND_BITPOS))
9189 {
9190 complaint (_("Could not parse Rust enum encoding string \"%s\""
9191 "[in module %s]"),
9192 TYPE_FIELD_NAME (type, 0),
9193 objfile_name (objfile));
9194 return;
9195 }
9196 ++name;
9197
9198 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9199 field_type = TYPE_FIELD_TYPE (field_type, index);
9200 }
9201
9202 /* Make a union to hold the variants. */
9203 struct type *union_type = alloc_type (objfile);
9204 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9205 TYPE_NFIELDS (union_type) = 3;
9206 TYPE_FIELDS (union_type)
9207 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9208 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9209 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9210
9211 /* Put the discriminant must at index 0. */
9212 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9213 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9214 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9215 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9216
9217 /* The order of fields doesn't really matter, so put the real
9218 field at index 1 and the data-less field at index 2. */
9219 struct discriminant_info *disc
9220 = alloc_discriminant_info (union_type, 0, 1);
9221 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9222 TYPE_FIELD_NAME (union_type, 1)
9223 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9224 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9225 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9226 TYPE_FIELD_NAME (union_type, 1));
9227
9228 const char *dataless_name
9229 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9230 name);
9231 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9232 dataless_name);
9233 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9234 /* NAME points into the original discriminant name, which
9235 already has the correct lifetime. */
9236 TYPE_FIELD_NAME (union_type, 2) = name;
9237 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9238 disc->discriminants[2] = 0;
9239
9240 /* Smash this type to be a structure type. We have to do this
9241 because the type has already been recorded. */
9242 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9243 TYPE_NFIELDS (type) = 1;
9244 TYPE_FIELDS (type)
9245 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9246
9247 /* Install the variant part. */
9248 TYPE_FIELD_TYPE (type, 0) = union_type;
9249 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9250 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9251 }
9252 /* A union with a single anonymous field is probably an old-style
9253 univariant enum. */
9254 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9255 {
9256 /* Smash this type to be a structure type. We have to do this
9257 because the type has already been recorded. */
9258 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9259
9260 /* Make a union to hold the variants. */
9261 struct type *union_type = alloc_type (objfile);
9262 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9263 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9264 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9265 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9266 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9267
9268 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9269 const char *variant_name
9270 = rust_last_path_segment (TYPE_NAME (field_type));
9271 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9272 TYPE_NAME (field_type)
9273 = rust_fully_qualify (&objfile->objfile_obstack,
9274 TYPE_NAME (type), variant_name);
9275
9276 /* Install the union in the outer struct type. */
9277 TYPE_NFIELDS (type) = 1;
9278 TYPE_FIELDS (type)
9279 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9280 TYPE_FIELD_TYPE (type, 0) = union_type;
9281 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9282 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9283
9284 alloc_discriminant_info (union_type, -1, 0);
9285 }
9286 else
9287 {
9288 struct type *disr_type = nullptr;
9289 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9290 {
9291 disr_type = TYPE_FIELD_TYPE (type, i);
9292
9293 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9294 {
9295 /* All fields of a true enum will be structs. */
9296 return;
9297 }
9298 else if (TYPE_NFIELDS (disr_type) == 0)
9299 {
9300 /* Could be data-less variant, so keep going. */
9301 disr_type = nullptr;
9302 }
9303 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9304 "RUST$ENUM$DISR") != 0)
9305 {
9306 /* Not a Rust enum. */
9307 return;
9308 }
9309 else
9310 {
9311 /* Found one. */
9312 break;
9313 }
9314 }
9315
9316 /* If we got here without a discriminant, then it's probably
9317 just a union. */
9318 if (disr_type == nullptr)
9319 return;
9320
9321 /* Smash this type to be a structure type. We have to do this
9322 because the type has already been recorded. */
9323 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9324
9325 /* Make a union to hold the variants. */
9326 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9327 struct type *union_type = alloc_type (objfile);
9328 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9329 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9330 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9331 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9332 TYPE_FIELDS (union_type)
9333 = (struct field *) TYPE_ZALLOC (union_type,
9334 (TYPE_NFIELDS (union_type)
9335 * sizeof (struct field)));
9336
9337 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9338 TYPE_NFIELDS (type) * sizeof (struct field));
9339
9340 /* Install the discriminant at index 0 in the union. */
9341 TYPE_FIELD (union_type, 0) = *disr_field;
9342 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9343 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9344
9345 /* Install the union in the outer struct type. */
9346 TYPE_FIELD_TYPE (type, 0) = union_type;
9347 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9348 TYPE_NFIELDS (type) = 1;
9349
9350 /* Set the size and offset of the union type. */
9351 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9352
9353 /* We need a way to find the correct discriminant given a
9354 variant name. For convenience we build a map here. */
9355 struct type *enum_type = FIELD_TYPE (*disr_field);
9356 std::unordered_map<std::string, ULONGEST> discriminant_map;
9357 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9358 {
9359 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9360 {
9361 const char *name
9362 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9363 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9364 }
9365 }
9366
9367 int n_fields = TYPE_NFIELDS (union_type);
9368 struct discriminant_info *disc
9369 = alloc_discriminant_info (union_type, 0, -1);
9370 /* Skip the discriminant here. */
9371 for (int i = 1; i < n_fields; ++i)
9372 {
9373 /* Find the final word in the name of this variant's type.
9374 That name can be used to look up the correct
9375 discriminant. */
9376 const char *variant_name
9377 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9378 i)));
9379
9380 auto iter = discriminant_map.find (variant_name);
9381 if (iter != discriminant_map.end ())
9382 disc->discriminants[i] = iter->second;
9383
9384 /* Remove the discriminant field, if it exists. */
9385 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9386 if (TYPE_NFIELDS (sub_type) > 0)
9387 {
9388 --TYPE_NFIELDS (sub_type);
9389 ++TYPE_FIELDS (sub_type);
9390 }
9391 TYPE_FIELD_NAME (union_type, i) = variant_name;
9392 TYPE_NAME (sub_type)
9393 = rust_fully_qualify (&objfile->objfile_obstack,
9394 TYPE_NAME (type), variant_name);
9395 }
9396 }
9397 }
9398
9399 /* Rewrite some Rust unions to be structures with variants parts. */
9400
9401 static void
9402 rust_union_quirks (struct dwarf2_cu *cu)
9403 {
9404 gdb_assert (cu->language == language_rust);
9405 for (type *type_ : cu->rust_unions)
9406 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9407 /* We don't need this any more. */
9408 cu->rust_unions.clear ();
9409 }
9410
9411 /* Return the symtab for PER_CU. This works properly regardless of
9412 whether we're using the index or psymtabs. */
9413
9414 static struct compunit_symtab *
9415 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9416 {
9417 return (per_cu->dwarf2_per_objfile->using_index
9418 ? per_cu->v.quick->compunit_symtab
9419 : per_cu->v.psymtab->compunit_symtab);
9420 }
9421
9422 /* A helper function for computing the list of all symbol tables
9423 included by PER_CU. */
9424
9425 static void
9426 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9427 htab_t all_children, htab_t all_type_symtabs,
9428 struct dwarf2_per_cu_data *per_cu,
9429 struct compunit_symtab *immediate_parent)
9430 {
9431 void **slot;
9432 struct compunit_symtab *cust;
9433
9434 slot = htab_find_slot (all_children, per_cu, INSERT);
9435 if (*slot != NULL)
9436 {
9437 /* This inclusion and its children have been processed. */
9438 return;
9439 }
9440
9441 *slot = per_cu;
9442 /* Only add a CU if it has a symbol table. */
9443 cust = get_compunit_symtab (per_cu);
9444 if (cust != NULL)
9445 {
9446 /* If this is a type unit only add its symbol table if we haven't
9447 seen it yet (type unit per_cu's can share symtabs). */
9448 if (per_cu->is_debug_types)
9449 {
9450 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9451 if (*slot == NULL)
9452 {
9453 *slot = cust;
9454 result->push_back (cust);
9455 if (cust->user == NULL)
9456 cust->user = immediate_parent;
9457 }
9458 }
9459 else
9460 {
9461 result->push_back (cust);
9462 if (cust->user == NULL)
9463 cust->user = immediate_parent;
9464 }
9465 }
9466
9467 if (!per_cu->imported_symtabs_empty ())
9468 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9469 {
9470 recursively_compute_inclusions (result, all_children,
9471 all_type_symtabs, ptr, cust);
9472 }
9473 }
9474
9475 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9476 PER_CU. */
9477
9478 static void
9479 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9480 {
9481 gdb_assert (! per_cu->is_debug_types);
9482
9483 if (!per_cu->imported_symtabs_empty ())
9484 {
9485 int len;
9486 std::vector<compunit_symtab *> result_symtabs;
9487 htab_t all_children, all_type_symtabs;
9488 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9489
9490 /* If we don't have a symtab, we can just skip this case. */
9491 if (cust == NULL)
9492 return;
9493
9494 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9495 NULL, xcalloc, xfree);
9496 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9497 NULL, xcalloc, xfree);
9498
9499 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9500 {
9501 recursively_compute_inclusions (&result_symtabs, all_children,
9502 all_type_symtabs, ptr, cust);
9503 }
9504
9505 /* Now we have a transitive closure of all the included symtabs. */
9506 len = result_symtabs.size ();
9507 cust->includes
9508 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9509 struct compunit_symtab *, len + 1);
9510 memcpy (cust->includes, result_symtabs.data (),
9511 len * sizeof (compunit_symtab *));
9512 cust->includes[len] = NULL;
9513
9514 htab_delete (all_children);
9515 htab_delete (all_type_symtabs);
9516 }
9517 }
9518
9519 /* Compute the 'includes' field for the symtabs of all the CUs we just
9520 read. */
9521
9522 static void
9523 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9524 {
9525 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9526 {
9527 if (! iter->is_debug_types)
9528 compute_compunit_symtab_includes (iter);
9529 }
9530
9531 dwarf2_per_objfile->just_read_cus.clear ();
9532 }
9533
9534 /* Generate full symbol information for PER_CU, whose DIEs have
9535 already been loaded into memory. */
9536
9537 static void
9538 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9539 enum language pretend_language)
9540 {
9541 struct dwarf2_cu *cu = per_cu->cu;
9542 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9543 struct objfile *objfile = dwarf2_per_objfile->objfile;
9544 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9545 CORE_ADDR lowpc, highpc;
9546 struct compunit_symtab *cust;
9547 CORE_ADDR baseaddr;
9548 struct block *static_block;
9549 CORE_ADDR addr;
9550
9551 baseaddr = objfile->text_section_offset ();
9552
9553 /* Clear the list here in case something was left over. */
9554 cu->method_list.clear ();
9555
9556 cu->language = pretend_language;
9557 cu->language_defn = language_def (cu->language);
9558
9559 /* Do line number decoding in read_file_scope () */
9560 process_die (cu->dies, cu);
9561
9562 /* For now fudge the Go package. */
9563 if (cu->language == language_go)
9564 fixup_go_packaging (cu);
9565
9566 /* Now that we have processed all the DIEs in the CU, all the types
9567 should be complete, and it should now be safe to compute all of the
9568 physnames. */
9569 compute_delayed_physnames (cu);
9570
9571 if (cu->language == language_rust)
9572 rust_union_quirks (cu);
9573
9574 /* Some compilers don't define a DW_AT_high_pc attribute for the
9575 compilation unit. If the DW_AT_high_pc is missing, synthesize
9576 it, by scanning the DIE's below the compilation unit. */
9577 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9578
9579 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9580 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9581
9582 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9583 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9584 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9585 addrmap to help ensure it has an accurate map of pc values belonging to
9586 this comp unit. */
9587 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9588
9589 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9590 SECT_OFF_TEXT (objfile),
9591 0);
9592
9593 if (cust != NULL)
9594 {
9595 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9596
9597 /* Set symtab language to language from DW_AT_language. If the
9598 compilation is from a C file generated by language preprocessors, do
9599 not set the language if it was already deduced by start_subfile. */
9600 if (!(cu->language == language_c
9601 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9602 COMPUNIT_FILETABS (cust)->language = cu->language;
9603
9604 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9605 produce DW_AT_location with location lists but it can be possibly
9606 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9607 there were bugs in prologue debug info, fixed later in GCC-4.5
9608 by "unwind info for epilogues" patch (which is not directly related).
9609
9610 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9611 needed, it would be wrong due to missing DW_AT_producer there.
9612
9613 Still one can confuse GDB by using non-standard GCC compilation
9614 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9615 */
9616 if (cu->has_loclist && gcc_4_minor >= 5)
9617 cust->locations_valid = 1;
9618
9619 if (gcc_4_minor >= 5)
9620 cust->epilogue_unwind_valid = 1;
9621
9622 cust->call_site_htab = cu->call_site_htab;
9623 }
9624
9625 if (dwarf2_per_objfile->using_index)
9626 per_cu->v.quick->compunit_symtab = cust;
9627 else
9628 {
9629 dwarf2_psymtab *pst = per_cu->v.psymtab;
9630 pst->compunit_symtab = cust;
9631 pst->readin = true;
9632 }
9633
9634 /* Push it for inclusion processing later. */
9635 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9636
9637 /* Not needed any more. */
9638 cu->reset_builder ();
9639 }
9640
9641 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9642 already been loaded into memory. */
9643
9644 static void
9645 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9646 enum language pretend_language)
9647 {
9648 struct dwarf2_cu *cu = per_cu->cu;
9649 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9650 struct objfile *objfile = dwarf2_per_objfile->objfile;
9651 struct compunit_symtab *cust;
9652 struct signatured_type *sig_type;
9653
9654 gdb_assert (per_cu->is_debug_types);
9655 sig_type = (struct signatured_type *) per_cu;
9656
9657 /* Clear the list here in case something was left over. */
9658 cu->method_list.clear ();
9659
9660 cu->language = pretend_language;
9661 cu->language_defn = language_def (cu->language);
9662
9663 /* The symbol tables are set up in read_type_unit_scope. */
9664 process_die (cu->dies, cu);
9665
9666 /* For now fudge the Go package. */
9667 if (cu->language == language_go)
9668 fixup_go_packaging (cu);
9669
9670 /* Now that we have processed all the DIEs in the CU, all the types
9671 should be complete, and it should now be safe to compute all of the
9672 physnames. */
9673 compute_delayed_physnames (cu);
9674
9675 if (cu->language == language_rust)
9676 rust_union_quirks (cu);
9677
9678 /* TUs share symbol tables.
9679 If this is the first TU to use this symtab, complete the construction
9680 of it with end_expandable_symtab. Otherwise, complete the addition of
9681 this TU's symbols to the existing symtab. */
9682 if (sig_type->type_unit_group->compunit_symtab == NULL)
9683 {
9684 buildsym_compunit *builder = cu->get_builder ();
9685 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9686 sig_type->type_unit_group->compunit_symtab = cust;
9687
9688 if (cust != NULL)
9689 {
9690 /* Set symtab language to language from DW_AT_language. If the
9691 compilation is from a C file generated by language preprocessors,
9692 do not set the language if it was already deduced by
9693 start_subfile. */
9694 if (!(cu->language == language_c
9695 && COMPUNIT_FILETABS (cust)->language != language_c))
9696 COMPUNIT_FILETABS (cust)->language = cu->language;
9697 }
9698 }
9699 else
9700 {
9701 cu->get_builder ()->augment_type_symtab ();
9702 cust = sig_type->type_unit_group->compunit_symtab;
9703 }
9704
9705 if (dwarf2_per_objfile->using_index)
9706 per_cu->v.quick->compunit_symtab = cust;
9707 else
9708 {
9709 dwarf2_psymtab *pst = per_cu->v.psymtab;
9710 pst->compunit_symtab = cust;
9711 pst->readin = true;
9712 }
9713
9714 /* Not needed any more. */
9715 cu->reset_builder ();
9716 }
9717
9718 /* Process an imported unit DIE. */
9719
9720 static void
9721 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9722 {
9723 struct attribute *attr;
9724
9725 /* For now we don't handle imported units in type units. */
9726 if (cu->per_cu->is_debug_types)
9727 {
9728 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9729 " supported in type units [in module %s]"),
9730 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9731 }
9732
9733 attr = dwarf2_attr (die, DW_AT_import, cu);
9734 if (attr != NULL)
9735 {
9736 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9737 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9738 dwarf2_per_cu_data *per_cu
9739 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9740 cu->per_cu->dwarf2_per_objfile);
9741
9742 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9743 into another compilation unit, at root level. Regard this as a hint,
9744 and ignore it. */
9745 if (die->parent && die->parent->parent == NULL
9746 && per_cu->unit_type == DW_UT_compile
9747 && per_cu->lang == language_cplus)
9748 return;
9749
9750 /* If necessary, add it to the queue and load its DIEs. */
9751 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9752 load_full_comp_unit (per_cu, false, cu->language);
9753
9754 cu->per_cu->imported_symtabs_push (per_cu);
9755 }
9756 }
9757
9758 /* RAII object that represents a process_die scope: i.e.,
9759 starts/finishes processing a DIE. */
9760 class process_die_scope
9761 {
9762 public:
9763 process_die_scope (die_info *die, dwarf2_cu *cu)
9764 : m_die (die), m_cu (cu)
9765 {
9766 /* We should only be processing DIEs not already in process. */
9767 gdb_assert (!m_die->in_process);
9768 m_die->in_process = true;
9769 }
9770
9771 ~process_die_scope ()
9772 {
9773 m_die->in_process = false;
9774
9775 /* If we're done processing the DIE for the CU that owns the line
9776 header, we don't need the line header anymore. */
9777 if (m_cu->line_header_die_owner == m_die)
9778 {
9779 delete m_cu->line_header;
9780 m_cu->line_header = NULL;
9781 m_cu->line_header_die_owner = NULL;
9782 }
9783 }
9784
9785 private:
9786 die_info *m_die;
9787 dwarf2_cu *m_cu;
9788 };
9789
9790 /* Process a die and its children. */
9791
9792 static void
9793 process_die (struct die_info *die, struct dwarf2_cu *cu)
9794 {
9795 process_die_scope scope (die, cu);
9796
9797 switch (die->tag)
9798 {
9799 case DW_TAG_padding:
9800 break;
9801 case DW_TAG_compile_unit:
9802 case DW_TAG_partial_unit:
9803 read_file_scope (die, cu);
9804 break;
9805 case DW_TAG_type_unit:
9806 read_type_unit_scope (die, cu);
9807 break;
9808 case DW_TAG_subprogram:
9809 /* Nested subprograms in Fortran get a prefix. */
9810 if (cu->language == language_fortran
9811 && die->parent != NULL
9812 && die->parent->tag == DW_TAG_subprogram)
9813 cu->processing_has_namespace_info = true;
9814 /* Fall through. */
9815 case DW_TAG_inlined_subroutine:
9816 read_func_scope (die, cu);
9817 break;
9818 case DW_TAG_lexical_block:
9819 case DW_TAG_try_block:
9820 case DW_TAG_catch_block:
9821 read_lexical_block_scope (die, cu);
9822 break;
9823 case DW_TAG_call_site:
9824 case DW_TAG_GNU_call_site:
9825 read_call_site_scope (die, cu);
9826 break;
9827 case DW_TAG_class_type:
9828 case DW_TAG_interface_type:
9829 case DW_TAG_structure_type:
9830 case DW_TAG_union_type:
9831 process_structure_scope (die, cu);
9832 break;
9833 case DW_TAG_enumeration_type:
9834 process_enumeration_scope (die, cu);
9835 break;
9836
9837 /* These dies have a type, but processing them does not create
9838 a symbol or recurse to process the children. Therefore we can
9839 read them on-demand through read_type_die. */
9840 case DW_TAG_subroutine_type:
9841 case DW_TAG_set_type:
9842 case DW_TAG_array_type:
9843 case DW_TAG_pointer_type:
9844 case DW_TAG_ptr_to_member_type:
9845 case DW_TAG_reference_type:
9846 case DW_TAG_rvalue_reference_type:
9847 case DW_TAG_string_type:
9848 break;
9849
9850 case DW_TAG_base_type:
9851 case DW_TAG_subrange_type:
9852 case DW_TAG_typedef:
9853 /* Add a typedef symbol for the type definition, if it has a
9854 DW_AT_name. */
9855 new_symbol (die, read_type_die (die, cu), cu);
9856 break;
9857 case DW_TAG_common_block:
9858 read_common_block (die, cu);
9859 break;
9860 case DW_TAG_common_inclusion:
9861 break;
9862 case DW_TAG_namespace:
9863 cu->processing_has_namespace_info = true;
9864 read_namespace (die, cu);
9865 break;
9866 case DW_TAG_module:
9867 cu->processing_has_namespace_info = true;
9868 read_module (die, cu);
9869 break;
9870 case DW_TAG_imported_declaration:
9871 cu->processing_has_namespace_info = true;
9872 if (read_namespace_alias (die, cu))
9873 break;
9874 /* The declaration is not a global namespace alias. */
9875 /* Fall through. */
9876 case DW_TAG_imported_module:
9877 cu->processing_has_namespace_info = true;
9878 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9879 || cu->language != language_fortran))
9880 complaint (_("Tag '%s' has unexpected children"),
9881 dwarf_tag_name (die->tag));
9882 read_import_statement (die, cu);
9883 break;
9884
9885 case DW_TAG_imported_unit:
9886 process_imported_unit_die (die, cu);
9887 break;
9888
9889 case DW_TAG_variable:
9890 read_variable (die, cu);
9891 break;
9892
9893 default:
9894 new_symbol (die, NULL, cu);
9895 break;
9896 }
9897 }
9898 \f
9899 /* DWARF name computation. */
9900
9901 /* A helper function for dwarf2_compute_name which determines whether DIE
9902 needs to have the name of the scope prepended to the name listed in the
9903 die. */
9904
9905 static int
9906 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9907 {
9908 struct attribute *attr;
9909
9910 switch (die->tag)
9911 {
9912 case DW_TAG_namespace:
9913 case DW_TAG_typedef:
9914 case DW_TAG_class_type:
9915 case DW_TAG_interface_type:
9916 case DW_TAG_structure_type:
9917 case DW_TAG_union_type:
9918 case DW_TAG_enumeration_type:
9919 case DW_TAG_enumerator:
9920 case DW_TAG_subprogram:
9921 case DW_TAG_inlined_subroutine:
9922 case DW_TAG_member:
9923 case DW_TAG_imported_declaration:
9924 return 1;
9925
9926 case DW_TAG_variable:
9927 case DW_TAG_constant:
9928 /* We only need to prefix "globally" visible variables. These include
9929 any variable marked with DW_AT_external or any variable that
9930 lives in a namespace. [Variables in anonymous namespaces
9931 require prefixing, but they are not DW_AT_external.] */
9932
9933 if (dwarf2_attr (die, DW_AT_specification, cu))
9934 {
9935 struct dwarf2_cu *spec_cu = cu;
9936
9937 return die_needs_namespace (die_specification (die, &spec_cu),
9938 spec_cu);
9939 }
9940
9941 attr = dwarf2_attr (die, DW_AT_external, cu);
9942 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9943 && die->parent->tag != DW_TAG_module)
9944 return 0;
9945 /* A variable in a lexical block of some kind does not need a
9946 namespace, even though in C++ such variables may be external
9947 and have a mangled name. */
9948 if (die->parent->tag == DW_TAG_lexical_block
9949 || die->parent->tag == DW_TAG_try_block
9950 || die->parent->tag == DW_TAG_catch_block
9951 || die->parent->tag == DW_TAG_subprogram)
9952 return 0;
9953 return 1;
9954
9955 default:
9956 return 0;
9957 }
9958 }
9959
9960 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9961 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9962 defined for the given DIE. */
9963
9964 static struct attribute *
9965 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9966 {
9967 struct attribute *attr;
9968
9969 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9970 if (attr == NULL)
9971 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9972
9973 return attr;
9974 }
9975
9976 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9977 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9978 defined for the given DIE. */
9979
9980 static const char *
9981 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9982 {
9983 const char *linkage_name;
9984
9985 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9986 if (linkage_name == NULL)
9987 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9988
9989 return linkage_name;
9990 }
9991
9992 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9993 compute the physname for the object, which include a method's:
9994 - formal parameters (C++),
9995 - receiver type (Go),
9996
9997 The term "physname" is a bit confusing.
9998 For C++, for example, it is the demangled name.
9999 For Go, for example, it's the mangled name.
10000
10001 For Ada, return the DIE's linkage name rather than the fully qualified
10002 name. PHYSNAME is ignored..
10003
10004 The result is allocated on the objfile_obstack and canonicalized. */
10005
10006 static const char *
10007 dwarf2_compute_name (const char *name,
10008 struct die_info *die, struct dwarf2_cu *cu,
10009 int physname)
10010 {
10011 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10012
10013 if (name == NULL)
10014 name = dwarf2_name (die, cu);
10015
10016 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10017 but otherwise compute it by typename_concat inside GDB.
10018 FIXME: Actually this is not really true, or at least not always true.
10019 It's all very confusing. compute_and_set_names doesn't try to demangle
10020 Fortran names because there is no mangling standard. So new_symbol
10021 will set the demangled name to the result of dwarf2_full_name, and it is
10022 the demangled name that GDB uses if it exists. */
10023 if (cu->language == language_ada
10024 || (cu->language == language_fortran && physname))
10025 {
10026 /* For Ada unit, we prefer the linkage name over the name, as
10027 the former contains the exported name, which the user expects
10028 to be able to reference. Ideally, we want the user to be able
10029 to reference this entity using either natural or linkage name,
10030 but we haven't started looking at this enhancement yet. */
10031 const char *linkage_name = dw2_linkage_name (die, cu);
10032
10033 if (linkage_name != NULL)
10034 return linkage_name;
10035 }
10036
10037 /* These are the only languages we know how to qualify names in. */
10038 if (name != NULL
10039 && (cu->language == language_cplus
10040 || cu->language == language_fortran || cu->language == language_d
10041 || cu->language == language_rust))
10042 {
10043 if (die_needs_namespace (die, cu))
10044 {
10045 const char *prefix;
10046 const char *canonical_name = NULL;
10047
10048 string_file buf;
10049
10050 prefix = determine_prefix (die, cu);
10051 if (*prefix != '\0')
10052 {
10053 gdb::unique_xmalloc_ptr<char> prefixed_name
10054 (typename_concat (NULL, prefix, name, physname, cu));
10055
10056 buf.puts (prefixed_name.get ());
10057 }
10058 else
10059 buf.puts (name);
10060
10061 /* Template parameters may be specified in the DIE's DW_AT_name, or
10062 as children with DW_TAG_template_type_param or
10063 DW_TAG_value_type_param. If the latter, add them to the name
10064 here. If the name already has template parameters, then
10065 skip this step; some versions of GCC emit both, and
10066 it is more efficient to use the pre-computed name.
10067
10068 Something to keep in mind about this process: it is very
10069 unlikely, or in some cases downright impossible, to produce
10070 something that will match the mangled name of a function.
10071 If the definition of the function has the same debug info,
10072 we should be able to match up with it anyway. But fallbacks
10073 using the minimal symbol, for instance to find a method
10074 implemented in a stripped copy of libstdc++, will not work.
10075 If we do not have debug info for the definition, we will have to
10076 match them up some other way.
10077
10078 When we do name matching there is a related problem with function
10079 templates; two instantiated function templates are allowed to
10080 differ only by their return types, which we do not add here. */
10081
10082 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10083 {
10084 struct attribute *attr;
10085 struct die_info *child;
10086 int first = 1;
10087
10088 die->building_fullname = 1;
10089
10090 for (child = die->child; child != NULL; child = child->sibling)
10091 {
10092 struct type *type;
10093 LONGEST value;
10094 const gdb_byte *bytes;
10095 struct dwarf2_locexpr_baton *baton;
10096 struct value *v;
10097
10098 if (child->tag != DW_TAG_template_type_param
10099 && child->tag != DW_TAG_template_value_param)
10100 continue;
10101
10102 if (first)
10103 {
10104 buf.puts ("<");
10105 first = 0;
10106 }
10107 else
10108 buf.puts (", ");
10109
10110 attr = dwarf2_attr (child, DW_AT_type, cu);
10111 if (attr == NULL)
10112 {
10113 complaint (_("template parameter missing DW_AT_type"));
10114 buf.puts ("UNKNOWN_TYPE");
10115 continue;
10116 }
10117 type = die_type (child, cu);
10118
10119 if (child->tag == DW_TAG_template_type_param)
10120 {
10121 c_print_type (type, "", &buf, -1, 0, cu->language,
10122 &type_print_raw_options);
10123 continue;
10124 }
10125
10126 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10127 if (attr == NULL)
10128 {
10129 complaint (_("template parameter missing "
10130 "DW_AT_const_value"));
10131 buf.puts ("UNKNOWN_VALUE");
10132 continue;
10133 }
10134
10135 dwarf2_const_value_attr (attr, type, name,
10136 &cu->comp_unit_obstack, cu,
10137 &value, &bytes, &baton);
10138
10139 if (TYPE_NOSIGN (type))
10140 /* GDB prints characters as NUMBER 'CHAR'. If that's
10141 changed, this can use value_print instead. */
10142 c_printchar (value, type, &buf);
10143 else
10144 {
10145 struct value_print_options opts;
10146
10147 if (baton != NULL)
10148 v = dwarf2_evaluate_loc_desc (type, NULL,
10149 baton->data,
10150 baton->size,
10151 baton->per_cu);
10152 else if (bytes != NULL)
10153 {
10154 v = allocate_value (type);
10155 memcpy (value_contents_writeable (v), bytes,
10156 TYPE_LENGTH (type));
10157 }
10158 else
10159 v = value_from_longest (type, value);
10160
10161 /* Specify decimal so that we do not depend on
10162 the radix. */
10163 get_formatted_print_options (&opts, 'd');
10164 opts.raw = 1;
10165 value_print (v, &buf, &opts);
10166 release_value (v);
10167 }
10168 }
10169
10170 die->building_fullname = 0;
10171
10172 if (!first)
10173 {
10174 /* Close the argument list, with a space if necessary
10175 (nested templates). */
10176 if (!buf.empty () && buf.string ().back () == '>')
10177 buf.puts (" >");
10178 else
10179 buf.puts (">");
10180 }
10181 }
10182
10183 /* For C++ methods, append formal parameter type
10184 information, if PHYSNAME. */
10185
10186 if (physname && die->tag == DW_TAG_subprogram
10187 && cu->language == language_cplus)
10188 {
10189 struct type *type = read_type_die (die, cu);
10190
10191 c_type_print_args (type, &buf, 1, cu->language,
10192 &type_print_raw_options);
10193
10194 if (cu->language == language_cplus)
10195 {
10196 /* Assume that an artificial first parameter is
10197 "this", but do not crash if it is not. RealView
10198 marks unnamed (and thus unused) parameters as
10199 artificial; there is no way to differentiate
10200 the two cases. */
10201 if (TYPE_NFIELDS (type) > 0
10202 && TYPE_FIELD_ARTIFICIAL (type, 0)
10203 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10204 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10205 0))))
10206 buf.puts (" const");
10207 }
10208 }
10209
10210 const std::string &intermediate_name = buf.string ();
10211
10212 if (cu->language == language_cplus)
10213 canonical_name
10214 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10215 objfile);
10216
10217 /* If we only computed INTERMEDIATE_NAME, or if
10218 INTERMEDIATE_NAME is already canonical, then we need to
10219 intern it. */
10220 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10221 name = objfile->intern (intermediate_name);
10222 else
10223 name = canonical_name;
10224 }
10225 }
10226
10227 return name;
10228 }
10229
10230 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10231 If scope qualifiers are appropriate they will be added. The result
10232 will be allocated on the storage_obstack, or NULL if the DIE does
10233 not have a name. NAME may either be from a previous call to
10234 dwarf2_name or NULL.
10235
10236 The output string will be canonicalized (if C++). */
10237
10238 static const char *
10239 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10240 {
10241 return dwarf2_compute_name (name, die, cu, 0);
10242 }
10243
10244 /* Construct a physname for the given DIE in CU. NAME may either be
10245 from a previous call to dwarf2_name or NULL. The result will be
10246 allocated on the objfile_objstack or NULL if the DIE does not have a
10247 name.
10248
10249 The output string will be canonicalized (if C++). */
10250
10251 static const char *
10252 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10253 {
10254 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10255 const char *retval, *mangled = NULL, *canon = NULL;
10256 int need_copy = 1;
10257
10258 /* In this case dwarf2_compute_name is just a shortcut not building anything
10259 on its own. */
10260 if (!die_needs_namespace (die, cu))
10261 return dwarf2_compute_name (name, die, cu, 1);
10262
10263 mangled = dw2_linkage_name (die, cu);
10264
10265 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10266 See https://github.com/rust-lang/rust/issues/32925. */
10267 if (cu->language == language_rust && mangled != NULL
10268 && strchr (mangled, '{') != NULL)
10269 mangled = NULL;
10270
10271 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10272 has computed. */
10273 gdb::unique_xmalloc_ptr<char> demangled;
10274 if (mangled != NULL)
10275 {
10276
10277 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10278 {
10279 /* Do nothing (do not demangle the symbol name). */
10280 }
10281 else if (cu->language == language_go)
10282 {
10283 /* This is a lie, but we already lie to the caller new_symbol.
10284 new_symbol assumes we return the mangled name.
10285 This just undoes that lie until things are cleaned up. */
10286 }
10287 else
10288 {
10289 /* Use DMGL_RET_DROP for C++ template functions to suppress
10290 their return type. It is easier for GDB users to search
10291 for such functions as `name(params)' than `long name(params)'.
10292 In such case the minimal symbol names do not match the full
10293 symbol names but for template functions there is never a need
10294 to look up their definition from their declaration so
10295 the only disadvantage remains the minimal symbol variant
10296 `long name(params)' does not have the proper inferior type. */
10297 demangled.reset (gdb_demangle (mangled,
10298 (DMGL_PARAMS | DMGL_ANSI
10299 | DMGL_RET_DROP)));
10300 }
10301 if (demangled)
10302 canon = demangled.get ();
10303 else
10304 {
10305 canon = mangled;
10306 need_copy = 0;
10307 }
10308 }
10309
10310 if (canon == NULL || check_physname)
10311 {
10312 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10313
10314 if (canon != NULL && strcmp (physname, canon) != 0)
10315 {
10316 /* It may not mean a bug in GDB. The compiler could also
10317 compute DW_AT_linkage_name incorrectly. But in such case
10318 GDB would need to be bug-to-bug compatible. */
10319
10320 complaint (_("Computed physname <%s> does not match demangled <%s> "
10321 "(from linkage <%s>) - DIE at %s [in module %s]"),
10322 physname, canon, mangled, sect_offset_str (die->sect_off),
10323 objfile_name (objfile));
10324
10325 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10326 is available here - over computed PHYSNAME. It is safer
10327 against both buggy GDB and buggy compilers. */
10328
10329 retval = canon;
10330 }
10331 else
10332 {
10333 retval = physname;
10334 need_copy = 0;
10335 }
10336 }
10337 else
10338 retval = canon;
10339
10340 if (need_copy)
10341 retval = objfile->intern (retval);
10342
10343 return retval;
10344 }
10345
10346 /* Inspect DIE in CU for a namespace alias. If one exists, record
10347 a new symbol for it.
10348
10349 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10350
10351 static int
10352 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10353 {
10354 struct attribute *attr;
10355
10356 /* If the die does not have a name, this is not a namespace
10357 alias. */
10358 attr = dwarf2_attr (die, DW_AT_name, cu);
10359 if (attr != NULL)
10360 {
10361 int num;
10362 struct die_info *d = die;
10363 struct dwarf2_cu *imported_cu = cu;
10364
10365 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10366 keep inspecting DIEs until we hit the underlying import. */
10367 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10368 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10369 {
10370 attr = dwarf2_attr (d, DW_AT_import, cu);
10371 if (attr == NULL)
10372 break;
10373
10374 d = follow_die_ref (d, attr, &imported_cu);
10375 if (d->tag != DW_TAG_imported_declaration)
10376 break;
10377 }
10378
10379 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10380 {
10381 complaint (_("DIE at %s has too many recursively imported "
10382 "declarations"), sect_offset_str (d->sect_off));
10383 return 0;
10384 }
10385
10386 if (attr != NULL)
10387 {
10388 struct type *type;
10389 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10390
10391 type = get_die_type_at_offset (sect_off, cu->per_cu);
10392 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10393 {
10394 /* This declaration is a global namespace alias. Add
10395 a symbol for it whose type is the aliased namespace. */
10396 new_symbol (die, type, cu);
10397 return 1;
10398 }
10399 }
10400 }
10401
10402 return 0;
10403 }
10404
10405 /* Return the using directives repository (global or local?) to use in the
10406 current context for CU.
10407
10408 For Ada, imported declarations can materialize renamings, which *may* be
10409 global. However it is impossible (for now?) in DWARF to distinguish
10410 "external" imported declarations and "static" ones. As all imported
10411 declarations seem to be static in all other languages, make them all CU-wide
10412 global only in Ada. */
10413
10414 static struct using_direct **
10415 using_directives (struct dwarf2_cu *cu)
10416 {
10417 if (cu->language == language_ada
10418 && cu->get_builder ()->outermost_context_p ())
10419 return cu->get_builder ()->get_global_using_directives ();
10420 else
10421 return cu->get_builder ()->get_local_using_directives ();
10422 }
10423
10424 /* Read the import statement specified by the given die and record it. */
10425
10426 static void
10427 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10428 {
10429 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10430 struct attribute *import_attr;
10431 struct die_info *imported_die, *child_die;
10432 struct dwarf2_cu *imported_cu;
10433 const char *imported_name;
10434 const char *imported_name_prefix;
10435 const char *canonical_name;
10436 const char *import_alias;
10437 const char *imported_declaration = NULL;
10438 const char *import_prefix;
10439 std::vector<const char *> excludes;
10440
10441 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10442 if (import_attr == NULL)
10443 {
10444 complaint (_("Tag '%s' has no DW_AT_import"),
10445 dwarf_tag_name (die->tag));
10446 return;
10447 }
10448
10449 imported_cu = cu;
10450 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10451 imported_name = dwarf2_name (imported_die, imported_cu);
10452 if (imported_name == NULL)
10453 {
10454 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10455
10456 The import in the following code:
10457 namespace A
10458 {
10459 typedef int B;
10460 }
10461
10462 int main ()
10463 {
10464 using A::B;
10465 B b;
10466 return b;
10467 }
10468
10469 ...
10470 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10471 <52> DW_AT_decl_file : 1
10472 <53> DW_AT_decl_line : 6
10473 <54> DW_AT_import : <0x75>
10474 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10475 <59> DW_AT_name : B
10476 <5b> DW_AT_decl_file : 1
10477 <5c> DW_AT_decl_line : 2
10478 <5d> DW_AT_type : <0x6e>
10479 ...
10480 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10481 <76> DW_AT_byte_size : 4
10482 <77> DW_AT_encoding : 5 (signed)
10483
10484 imports the wrong die ( 0x75 instead of 0x58 ).
10485 This case will be ignored until the gcc bug is fixed. */
10486 return;
10487 }
10488
10489 /* Figure out the local name after import. */
10490 import_alias = dwarf2_name (die, cu);
10491
10492 /* Figure out where the statement is being imported to. */
10493 import_prefix = determine_prefix (die, cu);
10494
10495 /* Figure out what the scope of the imported die is and prepend it
10496 to the name of the imported die. */
10497 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10498
10499 if (imported_die->tag != DW_TAG_namespace
10500 && imported_die->tag != DW_TAG_module)
10501 {
10502 imported_declaration = imported_name;
10503 canonical_name = imported_name_prefix;
10504 }
10505 else if (strlen (imported_name_prefix) > 0)
10506 canonical_name = obconcat (&objfile->objfile_obstack,
10507 imported_name_prefix,
10508 (cu->language == language_d ? "." : "::"),
10509 imported_name, (char *) NULL);
10510 else
10511 canonical_name = imported_name;
10512
10513 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10514 for (child_die = die->child; child_die && child_die->tag;
10515 child_die = sibling_die (child_die))
10516 {
10517 /* DWARF-4: A Fortran use statement with a “rename list” may be
10518 represented by an imported module entry with an import attribute
10519 referring to the module and owned entries corresponding to those
10520 entities that are renamed as part of being imported. */
10521
10522 if (child_die->tag != DW_TAG_imported_declaration)
10523 {
10524 complaint (_("child DW_TAG_imported_declaration expected "
10525 "- DIE at %s [in module %s]"),
10526 sect_offset_str (child_die->sect_off),
10527 objfile_name (objfile));
10528 continue;
10529 }
10530
10531 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10532 if (import_attr == NULL)
10533 {
10534 complaint (_("Tag '%s' has no DW_AT_import"),
10535 dwarf_tag_name (child_die->tag));
10536 continue;
10537 }
10538
10539 imported_cu = cu;
10540 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10541 &imported_cu);
10542 imported_name = dwarf2_name (imported_die, imported_cu);
10543 if (imported_name == NULL)
10544 {
10545 complaint (_("child DW_TAG_imported_declaration has unknown "
10546 "imported name - DIE at %s [in module %s]"),
10547 sect_offset_str (child_die->sect_off),
10548 objfile_name (objfile));
10549 continue;
10550 }
10551
10552 excludes.push_back (imported_name);
10553
10554 process_die (child_die, cu);
10555 }
10556
10557 add_using_directive (using_directives (cu),
10558 import_prefix,
10559 canonical_name,
10560 import_alias,
10561 imported_declaration,
10562 excludes,
10563 0,
10564 &objfile->objfile_obstack);
10565 }
10566
10567 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10568 types, but gives them a size of zero. Starting with version 14,
10569 ICC is compatible with GCC. */
10570
10571 static bool
10572 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10573 {
10574 if (!cu->checked_producer)
10575 check_producer (cu);
10576
10577 return cu->producer_is_icc_lt_14;
10578 }
10579
10580 /* ICC generates a DW_AT_type for C void functions. This was observed on
10581 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10582 which says that void functions should not have a DW_AT_type. */
10583
10584 static bool
10585 producer_is_icc (struct dwarf2_cu *cu)
10586 {
10587 if (!cu->checked_producer)
10588 check_producer (cu);
10589
10590 return cu->producer_is_icc;
10591 }
10592
10593 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10594 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10595 this, it was first present in GCC release 4.3.0. */
10596
10597 static bool
10598 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10599 {
10600 if (!cu->checked_producer)
10601 check_producer (cu);
10602
10603 return cu->producer_is_gcc_lt_4_3;
10604 }
10605
10606 static file_and_directory
10607 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10608 {
10609 file_and_directory res;
10610
10611 /* Find the filename. Do not use dwarf2_name here, since the filename
10612 is not a source language identifier. */
10613 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10614 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10615
10616 if (res.comp_dir == NULL
10617 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10618 && IS_ABSOLUTE_PATH (res.name))
10619 {
10620 res.comp_dir_storage = ldirname (res.name);
10621 if (!res.comp_dir_storage.empty ())
10622 res.comp_dir = res.comp_dir_storage.c_str ();
10623 }
10624 if (res.comp_dir != NULL)
10625 {
10626 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10627 directory, get rid of it. */
10628 const char *cp = strchr (res.comp_dir, ':');
10629
10630 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10631 res.comp_dir = cp + 1;
10632 }
10633
10634 if (res.name == NULL)
10635 res.name = "<unknown>";
10636
10637 return res;
10638 }
10639
10640 /* Handle DW_AT_stmt_list for a compilation unit.
10641 DIE is the DW_TAG_compile_unit die for CU.
10642 COMP_DIR is the compilation directory. LOWPC is passed to
10643 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10644
10645 static void
10646 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10647 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10648 {
10649 struct dwarf2_per_objfile *dwarf2_per_objfile
10650 = cu->per_cu->dwarf2_per_objfile;
10651 struct attribute *attr;
10652 struct line_header line_header_local;
10653 hashval_t line_header_local_hash;
10654 void **slot;
10655 int decode_mapping;
10656
10657 gdb_assert (! cu->per_cu->is_debug_types);
10658
10659 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10660 if (attr == NULL)
10661 return;
10662
10663 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10664
10665 /* The line header hash table is only created if needed (it exists to
10666 prevent redundant reading of the line table for partial_units).
10667 If we're given a partial_unit, we'll need it. If we're given a
10668 compile_unit, then use the line header hash table if it's already
10669 created, but don't create one just yet. */
10670
10671 if (dwarf2_per_objfile->line_header_hash == NULL
10672 && die->tag == DW_TAG_partial_unit)
10673 {
10674 dwarf2_per_objfile->line_header_hash
10675 .reset (htab_create_alloc (127, line_header_hash_voidp,
10676 line_header_eq_voidp,
10677 free_line_header_voidp,
10678 xcalloc, xfree));
10679 }
10680
10681 line_header_local.sect_off = line_offset;
10682 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10683 line_header_local_hash = line_header_hash (&line_header_local);
10684 if (dwarf2_per_objfile->line_header_hash != NULL)
10685 {
10686 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10687 &line_header_local,
10688 line_header_local_hash, NO_INSERT);
10689
10690 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10691 is not present in *SLOT (since if there is something in *SLOT then
10692 it will be for a partial_unit). */
10693 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10694 {
10695 gdb_assert (*slot != NULL);
10696 cu->line_header = (struct line_header *) *slot;
10697 return;
10698 }
10699 }
10700
10701 /* dwarf_decode_line_header does not yet provide sufficient information.
10702 We always have to call also dwarf_decode_lines for it. */
10703 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10704 if (lh == NULL)
10705 return;
10706
10707 cu->line_header = lh.release ();
10708 cu->line_header_die_owner = die;
10709
10710 if (dwarf2_per_objfile->line_header_hash == NULL)
10711 slot = NULL;
10712 else
10713 {
10714 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10715 &line_header_local,
10716 line_header_local_hash, INSERT);
10717 gdb_assert (slot != NULL);
10718 }
10719 if (slot != NULL && *slot == NULL)
10720 {
10721 /* This newly decoded line number information unit will be owned
10722 by line_header_hash hash table. */
10723 *slot = cu->line_header;
10724 cu->line_header_die_owner = NULL;
10725 }
10726 else
10727 {
10728 /* We cannot free any current entry in (*slot) as that struct line_header
10729 may be already used by multiple CUs. Create only temporary decoded
10730 line_header for this CU - it may happen at most once for each line
10731 number information unit. And if we're not using line_header_hash
10732 then this is what we want as well. */
10733 gdb_assert (die->tag != DW_TAG_partial_unit);
10734 }
10735 decode_mapping = (die->tag != DW_TAG_partial_unit);
10736 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10737 decode_mapping);
10738
10739 }
10740
10741 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10742
10743 static void
10744 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10745 {
10746 struct dwarf2_per_objfile *dwarf2_per_objfile
10747 = cu->per_cu->dwarf2_per_objfile;
10748 struct objfile *objfile = dwarf2_per_objfile->objfile;
10749 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10750 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10751 CORE_ADDR highpc = ((CORE_ADDR) 0);
10752 struct attribute *attr;
10753 struct die_info *child_die;
10754 CORE_ADDR baseaddr;
10755
10756 prepare_one_comp_unit (cu, die, cu->language);
10757 baseaddr = objfile->text_section_offset ();
10758
10759 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10760
10761 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10762 from finish_block. */
10763 if (lowpc == ((CORE_ADDR) -1))
10764 lowpc = highpc;
10765 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10766
10767 file_and_directory fnd = find_file_and_directory (die, cu);
10768
10769 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10770 standardised yet. As a workaround for the language detection we fall
10771 back to the DW_AT_producer string. */
10772 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10773 cu->language = language_opencl;
10774
10775 /* Similar hack for Go. */
10776 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10777 set_cu_language (DW_LANG_Go, cu);
10778
10779 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10780
10781 /* Decode line number information if present. We do this before
10782 processing child DIEs, so that the line header table is available
10783 for DW_AT_decl_file. */
10784 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10785
10786 /* Process all dies in compilation unit. */
10787 if (die->child != NULL)
10788 {
10789 child_die = die->child;
10790 while (child_die && child_die->tag)
10791 {
10792 process_die (child_die, cu);
10793 child_die = sibling_die (child_die);
10794 }
10795 }
10796
10797 /* Decode macro information, if present. Dwarf 2 macro information
10798 refers to information in the line number info statement program
10799 header, so we can only read it if we've read the header
10800 successfully. */
10801 attr = dwarf2_attr (die, DW_AT_macros, cu);
10802 if (attr == NULL)
10803 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10804 if (attr && cu->line_header)
10805 {
10806 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10807 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10808
10809 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10810 }
10811 else
10812 {
10813 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10814 if (attr && cu->line_header)
10815 {
10816 unsigned int macro_offset = DW_UNSND (attr);
10817
10818 dwarf_decode_macros (cu, macro_offset, 0);
10819 }
10820 }
10821 }
10822
10823 void
10824 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10825 {
10826 struct type_unit_group *tu_group;
10827 int first_time;
10828 struct attribute *attr;
10829 unsigned int i;
10830 struct signatured_type *sig_type;
10831
10832 gdb_assert (per_cu->is_debug_types);
10833 sig_type = (struct signatured_type *) per_cu;
10834
10835 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10836
10837 /* If we're using .gdb_index (includes -readnow) then
10838 per_cu->type_unit_group may not have been set up yet. */
10839 if (sig_type->type_unit_group == NULL)
10840 sig_type->type_unit_group = get_type_unit_group (this, attr);
10841 tu_group = sig_type->type_unit_group;
10842
10843 /* If we've already processed this stmt_list there's no real need to
10844 do it again, we could fake it and just recreate the part we need
10845 (file name,index -> symtab mapping). If data shows this optimization
10846 is useful we can do it then. */
10847 first_time = tu_group->compunit_symtab == NULL;
10848
10849 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10850 debug info. */
10851 line_header_up lh;
10852 if (attr != NULL)
10853 {
10854 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10855 lh = dwarf_decode_line_header (line_offset, this);
10856 }
10857 if (lh == NULL)
10858 {
10859 if (first_time)
10860 start_symtab ("", NULL, 0);
10861 else
10862 {
10863 gdb_assert (tu_group->symtabs == NULL);
10864 gdb_assert (m_builder == nullptr);
10865 struct compunit_symtab *cust = tu_group->compunit_symtab;
10866 m_builder.reset (new struct buildsym_compunit
10867 (COMPUNIT_OBJFILE (cust), "",
10868 COMPUNIT_DIRNAME (cust),
10869 compunit_language (cust),
10870 0, cust));
10871 }
10872 return;
10873 }
10874
10875 line_header = lh.release ();
10876 line_header_die_owner = die;
10877
10878 if (first_time)
10879 {
10880 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10881
10882 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10883 still initializing it, and our caller (a few levels up)
10884 process_full_type_unit still needs to know if this is the first
10885 time. */
10886
10887 tu_group->symtabs
10888 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10889 struct symtab *, line_header->file_names_size ());
10890
10891 auto &file_names = line_header->file_names ();
10892 for (i = 0; i < file_names.size (); ++i)
10893 {
10894 file_entry &fe = file_names[i];
10895 dwarf2_start_subfile (this, fe.name,
10896 fe.include_dir (line_header));
10897 buildsym_compunit *b = get_builder ();
10898 if (b->get_current_subfile ()->symtab == NULL)
10899 {
10900 /* NOTE: start_subfile will recognize when it's been
10901 passed a file it has already seen. So we can't
10902 assume there's a simple mapping from
10903 cu->line_header->file_names to subfiles, plus
10904 cu->line_header->file_names may contain dups. */
10905 b->get_current_subfile ()->symtab
10906 = allocate_symtab (cust, b->get_current_subfile ()->name);
10907 }
10908
10909 fe.symtab = b->get_current_subfile ()->symtab;
10910 tu_group->symtabs[i] = fe.symtab;
10911 }
10912 }
10913 else
10914 {
10915 gdb_assert (m_builder == nullptr);
10916 struct compunit_symtab *cust = tu_group->compunit_symtab;
10917 m_builder.reset (new struct buildsym_compunit
10918 (COMPUNIT_OBJFILE (cust), "",
10919 COMPUNIT_DIRNAME (cust),
10920 compunit_language (cust),
10921 0, cust));
10922
10923 auto &file_names = line_header->file_names ();
10924 for (i = 0; i < file_names.size (); ++i)
10925 {
10926 file_entry &fe = file_names[i];
10927 fe.symtab = tu_group->symtabs[i];
10928 }
10929 }
10930
10931 /* The main symtab is allocated last. Type units don't have DW_AT_name
10932 so they don't have a "real" (so to speak) symtab anyway.
10933 There is later code that will assign the main symtab to all symbols
10934 that don't have one. We need to handle the case of a symbol with a
10935 missing symtab (DW_AT_decl_file) anyway. */
10936 }
10937
10938 /* Process DW_TAG_type_unit.
10939 For TUs we want to skip the first top level sibling if it's not the
10940 actual type being defined by this TU. In this case the first top
10941 level sibling is there to provide context only. */
10942
10943 static void
10944 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10945 {
10946 struct die_info *child_die;
10947
10948 prepare_one_comp_unit (cu, die, language_minimal);
10949
10950 /* Initialize (or reinitialize) the machinery for building symtabs.
10951 We do this before processing child DIEs, so that the line header table
10952 is available for DW_AT_decl_file. */
10953 cu->setup_type_unit_groups (die);
10954
10955 if (die->child != NULL)
10956 {
10957 child_die = die->child;
10958 while (child_die && child_die->tag)
10959 {
10960 process_die (child_die, cu);
10961 child_die = sibling_die (child_die);
10962 }
10963 }
10964 }
10965 \f
10966 /* DWO/DWP files.
10967
10968 http://gcc.gnu.org/wiki/DebugFission
10969 http://gcc.gnu.org/wiki/DebugFissionDWP
10970
10971 To simplify handling of both DWO files ("object" files with the DWARF info)
10972 and DWP files (a file with the DWOs packaged up into one file), we treat
10973 DWP files as having a collection of virtual DWO files. */
10974
10975 static hashval_t
10976 hash_dwo_file (const void *item)
10977 {
10978 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10979 hashval_t hash;
10980
10981 hash = htab_hash_string (dwo_file->dwo_name);
10982 if (dwo_file->comp_dir != NULL)
10983 hash += htab_hash_string (dwo_file->comp_dir);
10984 return hash;
10985 }
10986
10987 static int
10988 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10989 {
10990 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10991 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10992
10993 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10994 return 0;
10995 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10996 return lhs->comp_dir == rhs->comp_dir;
10997 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10998 }
10999
11000 /* Allocate a hash table for DWO files. */
11001
11002 static htab_up
11003 allocate_dwo_file_hash_table ()
11004 {
11005 auto delete_dwo_file = [] (void *item)
11006 {
11007 struct dwo_file *dwo_file = (struct dwo_file *) item;
11008
11009 delete dwo_file;
11010 };
11011
11012 return htab_up (htab_create_alloc (41,
11013 hash_dwo_file,
11014 eq_dwo_file,
11015 delete_dwo_file,
11016 xcalloc, xfree));
11017 }
11018
11019 /* Lookup DWO file DWO_NAME. */
11020
11021 static void **
11022 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11023 const char *dwo_name,
11024 const char *comp_dir)
11025 {
11026 struct dwo_file find_entry;
11027 void **slot;
11028
11029 if (dwarf2_per_objfile->dwo_files == NULL)
11030 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11031
11032 find_entry.dwo_name = dwo_name;
11033 find_entry.comp_dir = comp_dir;
11034 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11035 INSERT);
11036
11037 return slot;
11038 }
11039
11040 static hashval_t
11041 hash_dwo_unit (const void *item)
11042 {
11043 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11044
11045 /* This drops the top 32 bits of the id, but is ok for a hash. */
11046 return dwo_unit->signature;
11047 }
11048
11049 static int
11050 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11051 {
11052 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11053 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11054
11055 /* The signature is assumed to be unique within the DWO file.
11056 So while object file CU dwo_id's always have the value zero,
11057 that's OK, assuming each object file DWO file has only one CU,
11058 and that's the rule for now. */
11059 return lhs->signature == rhs->signature;
11060 }
11061
11062 /* Allocate a hash table for DWO CUs,TUs.
11063 There is one of these tables for each of CUs,TUs for each DWO file. */
11064
11065 static htab_up
11066 allocate_dwo_unit_table ()
11067 {
11068 /* Start out with a pretty small number.
11069 Generally DWO files contain only one CU and maybe some TUs. */
11070 return htab_up (htab_create_alloc (3,
11071 hash_dwo_unit,
11072 eq_dwo_unit,
11073 NULL, xcalloc, xfree));
11074 }
11075
11076 /* die_reader_func for create_dwo_cu. */
11077
11078 static void
11079 create_dwo_cu_reader (const struct die_reader_specs *reader,
11080 const gdb_byte *info_ptr,
11081 struct die_info *comp_unit_die,
11082 struct dwo_file *dwo_file,
11083 struct dwo_unit *dwo_unit)
11084 {
11085 struct dwarf2_cu *cu = reader->cu;
11086 sect_offset sect_off = cu->per_cu->sect_off;
11087 struct dwarf2_section_info *section = cu->per_cu->section;
11088
11089 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11090 if (!signature.has_value ())
11091 {
11092 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11093 " its dwo_id [in module %s]"),
11094 sect_offset_str (sect_off), dwo_file->dwo_name);
11095 return;
11096 }
11097
11098 dwo_unit->dwo_file = dwo_file;
11099 dwo_unit->signature = *signature;
11100 dwo_unit->section = section;
11101 dwo_unit->sect_off = sect_off;
11102 dwo_unit->length = cu->per_cu->length;
11103
11104 if (dwarf_read_debug)
11105 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11106 sect_offset_str (sect_off),
11107 hex_string (dwo_unit->signature));
11108 }
11109
11110 /* Create the dwo_units for the CUs in a DWO_FILE.
11111 Note: This function processes DWO files only, not DWP files. */
11112
11113 static void
11114 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11115 dwarf2_cu *cu, struct dwo_file &dwo_file,
11116 dwarf2_section_info &section, htab_up &cus_htab)
11117 {
11118 struct objfile *objfile = dwarf2_per_objfile->objfile;
11119 const gdb_byte *info_ptr, *end_ptr;
11120
11121 section.read (objfile);
11122 info_ptr = section.buffer;
11123
11124 if (info_ptr == NULL)
11125 return;
11126
11127 if (dwarf_read_debug)
11128 {
11129 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11130 section.get_name (),
11131 section.get_file_name ());
11132 }
11133
11134 end_ptr = info_ptr + section.size;
11135 while (info_ptr < end_ptr)
11136 {
11137 struct dwarf2_per_cu_data per_cu;
11138 struct dwo_unit read_unit {};
11139 struct dwo_unit *dwo_unit;
11140 void **slot;
11141 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11142
11143 memset (&per_cu, 0, sizeof (per_cu));
11144 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11145 per_cu.is_debug_types = 0;
11146 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11147 per_cu.section = &section;
11148
11149 cutu_reader reader (&per_cu, cu, &dwo_file);
11150 if (!reader.dummy_p)
11151 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11152 &dwo_file, &read_unit);
11153 info_ptr += per_cu.length;
11154
11155 // If the unit could not be parsed, skip it.
11156 if (read_unit.dwo_file == NULL)
11157 continue;
11158
11159 if (cus_htab == NULL)
11160 cus_htab = allocate_dwo_unit_table ();
11161
11162 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11163 *dwo_unit = read_unit;
11164 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11165 gdb_assert (slot != NULL);
11166 if (*slot != NULL)
11167 {
11168 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11169 sect_offset dup_sect_off = dup_cu->sect_off;
11170
11171 complaint (_("debug cu entry at offset %s is duplicate to"
11172 " the entry at offset %s, signature %s"),
11173 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11174 hex_string (dwo_unit->signature));
11175 }
11176 *slot = (void *)dwo_unit;
11177 }
11178 }
11179
11180 /* DWP file .debug_{cu,tu}_index section format:
11181 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11182
11183 DWP Version 1:
11184
11185 Both index sections have the same format, and serve to map a 64-bit
11186 signature to a set of section numbers. Each section begins with a header,
11187 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11188 indexes, and a pool of 32-bit section numbers. The index sections will be
11189 aligned at 8-byte boundaries in the file.
11190
11191 The index section header consists of:
11192
11193 V, 32 bit version number
11194 -, 32 bits unused
11195 N, 32 bit number of compilation units or type units in the index
11196 M, 32 bit number of slots in the hash table
11197
11198 Numbers are recorded using the byte order of the application binary.
11199
11200 The hash table begins at offset 16 in the section, and consists of an array
11201 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11202 order of the application binary). Unused slots in the hash table are 0.
11203 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11204
11205 The parallel table begins immediately after the hash table
11206 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11207 array of 32-bit indexes (using the byte order of the application binary),
11208 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11209 table contains a 32-bit index into the pool of section numbers. For unused
11210 hash table slots, the corresponding entry in the parallel table will be 0.
11211
11212 The pool of section numbers begins immediately following the hash table
11213 (at offset 16 + 12 * M from the beginning of the section). The pool of
11214 section numbers consists of an array of 32-bit words (using the byte order
11215 of the application binary). Each item in the array is indexed starting
11216 from 0. The hash table entry provides the index of the first section
11217 number in the set. Additional section numbers in the set follow, and the
11218 set is terminated by a 0 entry (section number 0 is not used in ELF).
11219
11220 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11221 section must be the first entry in the set, and the .debug_abbrev.dwo must
11222 be the second entry. Other members of the set may follow in any order.
11223
11224 ---
11225
11226 DWP Version 2:
11227
11228 DWP Version 2 combines all the .debug_info, etc. sections into one,
11229 and the entries in the index tables are now offsets into these sections.
11230 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11231 section.
11232
11233 Index Section Contents:
11234 Header
11235 Hash Table of Signatures dwp_hash_table.hash_table
11236 Parallel Table of Indices dwp_hash_table.unit_table
11237 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11238 Table of Section Sizes dwp_hash_table.v2.sizes
11239
11240 The index section header consists of:
11241
11242 V, 32 bit version number
11243 L, 32 bit number of columns in the table of section offsets
11244 N, 32 bit number of compilation units or type units in the index
11245 M, 32 bit number of slots in the hash table
11246
11247 Numbers are recorded using the byte order of the application binary.
11248
11249 The hash table has the same format as version 1.
11250 The parallel table of indices has the same format as version 1,
11251 except that the entries are origin-1 indices into the table of sections
11252 offsets and the table of section sizes.
11253
11254 The table of offsets begins immediately following the parallel table
11255 (at offset 16 + 12 * M from the beginning of the section). The table is
11256 a two-dimensional array of 32-bit words (using the byte order of the
11257 application binary), with L columns and N+1 rows, in row-major order.
11258 Each row in the array is indexed starting from 0. The first row provides
11259 a key to the remaining rows: each column in this row provides an identifier
11260 for a debug section, and the offsets in the same column of subsequent rows
11261 refer to that section. The section identifiers are:
11262
11263 DW_SECT_INFO 1 .debug_info.dwo
11264 DW_SECT_TYPES 2 .debug_types.dwo
11265 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11266 DW_SECT_LINE 4 .debug_line.dwo
11267 DW_SECT_LOC 5 .debug_loc.dwo
11268 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11269 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11270 DW_SECT_MACRO 8 .debug_macro.dwo
11271
11272 The offsets provided by the CU and TU index sections are the base offsets
11273 for the contributions made by each CU or TU to the corresponding section
11274 in the package file. Each CU and TU header contains an abbrev_offset
11275 field, used to find the abbreviations table for that CU or TU within the
11276 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11277 be interpreted as relative to the base offset given in the index section.
11278 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11279 should be interpreted as relative to the base offset for .debug_line.dwo,
11280 and offsets into other debug sections obtained from DWARF attributes should
11281 also be interpreted as relative to the corresponding base offset.
11282
11283 The table of sizes begins immediately following the table of offsets.
11284 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11285 with L columns and N rows, in row-major order. Each row in the array is
11286 indexed starting from 1 (row 0 is shared by the two tables).
11287
11288 ---
11289
11290 Hash table lookup is handled the same in version 1 and 2:
11291
11292 We assume that N and M will not exceed 2^32 - 1.
11293 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11294
11295 Given a 64-bit compilation unit signature or a type signature S, an entry
11296 in the hash table is located as follows:
11297
11298 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11299 the low-order k bits all set to 1.
11300
11301 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11302
11303 3) If the hash table entry at index H matches the signature, use that
11304 entry. If the hash table entry at index H is unused (all zeroes),
11305 terminate the search: the signature is not present in the table.
11306
11307 4) Let H = (H + H') modulo M. Repeat at Step 3.
11308
11309 Because M > N and H' and M are relatively prime, the search is guaranteed
11310 to stop at an unused slot or find the match. */
11311
11312 /* Create a hash table to map DWO IDs to their CU/TU entry in
11313 .debug_{info,types}.dwo in DWP_FILE.
11314 Returns NULL if there isn't one.
11315 Note: This function processes DWP files only, not DWO files. */
11316
11317 static struct dwp_hash_table *
11318 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11319 struct dwp_file *dwp_file, int is_debug_types)
11320 {
11321 struct objfile *objfile = dwarf2_per_objfile->objfile;
11322 bfd *dbfd = dwp_file->dbfd.get ();
11323 const gdb_byte *index_ptr, *index_end;
11324 struct dwarf2_section_info *index;
11325 uint32_t version, nr_columns, nr_units, nr_slots;
11326 struct dwp_hash_table *htab;
11327
11328 if (is_debug_types)
11329 index = &dwp_file->sections.tu_index;
11330 else
11331 index = &dwp_file->sections.cu_index;
11332
11333 if (index->empty ())
11334 return NULL;
11335 index->read (objfile);
11336
11337 index_ptr = index->buffer;
11338 index_end = index_ptr + index->size;
11339
11340 version = read_4_bytes (dbfd, index_ptr);
11341 index_ptr += 4;
11342 if (version == 2)
11343 nr_columns = read_4_bytes (dbfd, index_ptr);
11344 else
11345 nr_columns = 0;
11346 index_ptr += 4;
11347 nr_units = read_4_bytes (dbfd, index_ptr);
11348 index_ptr += 4;
11349 nr_slots = read_4_bytes (dbfd, index_ptr);
11350 index_ptr += 4;
11351
11352 if (version != 1 && version != 2)
11353 {
11354 error (_("Dwarf Error: unsupported DWP file version (%s)"
11355 " [in module %s]"),
11356 pulongest (version), dwp_file->name);
11357 }
11358 if (nr_slots != (nr_slots & -nr_slots))
11359 {
11360 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11361 " is not power of 2 [in module %s]"),
11362 pulongest (nr_slots), dwp_file->name);
11363 }
11364
11365 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11366 htab->version = version;
11367 htab->nr_columns = nr_columns;
11368 htab->nr_units = nr_units;
11369 htab->nr_slots = nr_slots;
11370 htab->hash_table = index_ptr;
11371 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11372
11373 /* Exit early if the table is empty. */
11374 if (nr_slots == 0 || nr_units == 0
11375 || (version == 2 && nr_columns == 0))
11376 {
11377 /* All must be zero. */
11378 if (nr_slots != 0 || nr_units != 0
11379 || (version == 2 && nr_columns != 0))
11380 {
11381 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11382 " all zero [in modules %s]"),
11383 dwp_file->name);
11384 }
11385 return htab;
11386 }
11387
11388 if (version == 1)
11389 {
11390 htab->section_pool.v1.indices =
11391 htab->unit_table + sizeof (uint32_t) * nr_slots;
11392 /* It's harder to decide whether the section is too small in v1.
11393 V1 is deprecated anyway so we punt. */
11394 }
11395 else
11396 {
11397 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11398 int *ids = htab->section_pool.v2.section_ids;
11399 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11400 /* Reverse map for error checking. */
11401 int ids_seen[DW_SECT_MAX + 1];
11402 int i;
11403
11404 if (nr_columns < 2)
11405 {
11406 error (_("Dwarf Error: bad DWP hash table, too few columns"
11407 " in section table [in module %s]"),
11408 dwp_file->name);
11409 }
11410 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11411 {
11412 error (_("Dwarf Error: bad DWP hash table, too many columns"
11413 " in section table [in module %s]"),
11414 dwp_file->name);
11415 }
11416 memset (ids, 255, sizeof_ids);
11417 memset (ids_seen, 255, sizeof (ids_seen));
11418 for (i = 0; i < nr_columns; ++i)
11419 {
11420 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11421
11422 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11423 {
11424 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11425 " in section table [in module %s]"),
11426 id, dwp_file->name);
11427 }
11428 if (ids_seen[id] != -1)
11429 {
11430 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11431 " id %d in section table [in module %s]"),
11432 id, dwp_file->name);
11433 }
11434 ids_seen[id] = i;
11435 ids[i] = id;
11436 }
11437 /* Must have exactly one info or types section. */
11438 if (((ids_seen[DW_SECT_INFO] != -1)
11439 + (ids_seen[DW_SECT_TYPES] != -1))
11440 != 1)
11441 {
11442 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11443 " DWO info/types section [in module %s]"),
11444 dwp_file->name);
11445 }
11446 /* Must have an abbrev section. */
11447 if (ids_seen[DW_SECT_ABBREV] == -1)
11448 {
11449 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11450 " section [in module %s]"),
11451 dwp_file->name);
11452 }
11453 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11454 htab->section_pool.v2.sizes =
11455 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11456 * nr_units * nr_columns);
11457 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11458 * nr_units * nr_columns))
11459 > index_end)
11460 {
11461 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11462 " [in module %s]"),
11463 dwp_file->name);
11464 }
11465 }
11466
11467 return htab;
11468 }
11469
11470 /* Update SECTIONS with the data from SECTP.
11471
11472 This function is like the other "locate" section routines that are
11473 passed to bfd_map_over_sections, but in this context the sections to
11474 read comes from the DWP V1 hash table, not the full ELF section table.
11475
11476 The result is non-zero for success, or zero if an error was found. */
11477
11478 static int
11479 locate_v1_virtual_dwo_sections (asection *sectp,
11480 struct virtual_v1_dwo_sections *sections)
11481 {
11482 const struct dwop_section_names *names = &dwop_section_names;
11483
11484 if (section_is_p (sectp->name, &names->abbrev_dwo))
11485 {
11486 /* There can be only one. */
11487 if (sections->abbrev.s.section != NULL)
11488 return 0;
11489 sections->abbrev.s.section = sectp;
11490 sections->abbrev.size = bfd_section_size (sectp);
11491 }
11492 else if (section_is_p (sectp->name, &names->info_dwo)
11493 || section_is_p (sectp->name, &names->types_dwo))
11494 {
11495 /* There can be only one. */
11496 if (sections->info_or_types.s.section != NULL)
11497 return 0;
11498 sections->info_or_types.s.section = sectp;
11499 sections->info_or_types.size = bfd_section_size (sectp);
11500 }
11501 else if (section_is_p (sectp->name, &names->line_dwo))
11502 {
11503 /* There can be only one. */
11504 if (sections->line.s.section != NULL)
11505 return 0;
11506 sections->line.s.section = sectp;
11507 sections->line.size = bfd_section_size (sectp);
11508 }
11509 else if (section_is_p (sectp->name, &names->loc_dwo))
11510 {
11511 /* There can be only one. */
11512 if (sections->loc.s.section != NULL)
11513 return 0;
11514 sections->loc.s.section = sectp;
11515 sections->loc.size = bfd_section_size (sectp);
11516 }
11517 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11518 {
11519 /* There can be only one. */
11520 if (sections->macinfo.s.section != NULL)
11521 return 0;
11522 sections->macinfo.s.section = sectp;
11523 sections->macinfo.size = bfd_section_size (sectp);
11524 }
11525 else if (section_is_p (sectp->name, &names->macro_dwo))
11526 {
11527 /* There can be only one. */
11528 if (sections->macro.s.section != NULL)
11529 return 0;
11530 sections->macro.s.section = sectp;
11531 sections->macro.size = bfd_section_size (sectp);
11532 }
11533 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11534 {
11535 /* There can be only one. */
11536 if (sections->str_offsets.s.section != NULL)
11537 return 0;
11538 sections->str_offsets.s.section = sectp;
11539 sections->str_offsets.size = bfd_section_size (sectp);
11540 }
11541 else
11542 {
11543 /* No other kind of section is valid. */
11544 return 0;
11545 }
11546
11547 return 1;
11548 }
11549
11550 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11551 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11552 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11553 This is for DWP version 1 files. */
11554
11555 static struct dwo_unit *
11556 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11557 struct dwp_file *dwp_file,
11558 uint32_t unit_index,
11559 const char *comp_dir,
11560 ULONGEST signature, int is_debug_types)
11561 {
11562 struct objfile *objfile = dwarf2_per_objfile->objfile;
11563 const struct dwp_hash_table *dwp_htab =
11564 is_debug_types ? dwp_file->tus : dwp_file->cus;
11565 bfd *dbfd = dwp_file->dbfd.get ();
11566 const char *kind = is_debug_types ? "TU" : "CU";
11567 struct dwo_file *dwo_file;
11568 struct dwo_unit *dwo_unit;
11569 struct virtual_v1_dwo_sections sections;
11570 void **dwo_file_slot;
11571 int i;
11572
11573 gdb_assert (dwp_file->version == 1);
11574
11575 if (dwarf_read_debug)
11576 {
11577 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11578 kind,
11579 pulongest (unit_index), hex_string (signature),
11580 dwp_file->name);
11581 }
11582
11583 /* Fetch the sections of this DWO unit.
11584 Put a limit on the number of sections we look for so that bad data
11585 doesn't cause us to loop forever. */
11586
11587 #define MAX_NR_V1_DWO_SECTIONS \
11588 (1 /* .debug_info or .debug_types */ \
11589 + 1 /* .debug_abbrev */ \
11590 + 1 /* .debug_line */ \
11591 + 1 /* .debug_loc */ \
11592 + 1 /* .debug_str_offsets */ \
11593 + 1 /* .debug_macro or .debug_macinfo */ \
11594 + 1 /* trailing zero */)
11595
11596 memset (&sections, 0, sizeof (sections));
11597
11598 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11599 {
11600 asection *sectp;
11601 uint32_t section_nr =
11602 read_4_bytes (dbfd,
11603 dwp_htab->section_pool.v1.indices
11604 + (unit_index + i) * sizeof (uint32_t));
11605
11606 if (section_nr == 0)
11607 break;
11608 if (section_nr >= dwp_file->num_sections)
11609 {
11610 error (_("Dwarf Error: bad DWP hash table, section number too large"
11611 " [in module %s]"),
11612 dwp_file->name);
11613 }
11614
11615 sectp = dwp_file->elf_sections[section_nr];
11616 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11617 {
11618 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11619 " [in module %s]"),
11620 dwp_file->name);
11621 }
11622 }
11623
11624 if (i < 2
11625 || sections.info_or_types.empty ()
11626 || sections.abbrev.empty ())
11627 {
11628 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11629 " [in module %s]"),
11630 dwp_file->name);
11631 }
11632 if (i == MAX_NR_V1_DWO_SECTIONS)
11633 {
11634 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11635 " [in module %s]"),
11636 dwp_file->name);
11637 }
11638
11639 /* It's easier for the rest of the code if we fake a struct dwo_file and
11640 have dwo_unit "live" in that. At least for now.
11641
11642 The DWP file can be made up of a random collection of CUs and TUs.
11643 However, for each CU + set of TUs that came from the same original DWO
11644 file, we can combine them back into a virtual DWO file to save space
11645 (fewer struct dwo_file objects to allocate). Remember that for really
11646 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11647
11648 std::string virtual_dwo_name =
11649 string_printf ("virtual-dwo/%d-%d-%d-%d",
11650 sections.abbrev.get_id (),
11651 sections.line.get_id (),
11652 sections.loc.get_id (),
11653 sections.str_offsets.get_id ());
11654 /* Can we use an existing virtual DWO file? */
11655 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11656 virtual_dwo_name.c_str (),
11657 comp_dir);
11658 /* Create one if necessary. */
11659 if (*dwo_file_slot == NULL)
11660 {
11661 if (dwarf_read_debug)
11662 {
11663 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11664 virtual_dwo_name.c_str ());
11665 }
11666 dwo_file = new struct dwo_file;
11667 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11668 dwo_file->comp_dir = comp_dir;
11669 dwo_file->sections.abbrev = sections.abbrev;
11670 dwo_file->sections.line = sections.line;
11671 dwo_file->sections.loc = sections.loc;
11672 dwo_file->sections.macinfo = sections.macinfo;
11673 dwo_file->sections.macro = sections.macro;
11674 dwo_file->sections.str_offsets = sections.str_offsets;
11675 /* The "str" section is global to the entire DWP file. */
11676 dwo_file->sections.str = dwp_file->sections.str;
11677 /* The info or types section is assigned below to dwo_unit,
11678 there's no need to record it in dwo_file.
11679 Also, we can't simply record type sections in dwo_file because
11680 we record a pointer into the vector in dwo_unit. As we collect more
11681 types we'll grow the vector and eventually have to reallocate space
11682 for it, invalidating all copies of pointers into the previous
11683 contents. */
11684 *dwo_file_slot = dwo_file;
11685 }
11686 else
11687 {
11688 if (dwarf_read_debug)
11689 {
11690 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11691 virtual_dwo_name.c_str ());
11692 }
11693 dwo_file = (struct dwo_file *) *dwo_file_slot;
11694 }
11695
11696 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11697 dwo_unit->dwo_file = dwo_file;
11698 dwo_unit->signature = signature;
11699 dwo_unit->section =
11700 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11701 *dwo_unit->section = sections.info_or_types;
11702 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11703
11704 return dwo_unit;
11705 }
11706
11707 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11708 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11709 piece within that section used by a TU/CU, return a virtual section
11710 of just that piece. */
11711
11712 static struct dwarf2_section_info
11713 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11714 struct dwarf2_section_info *section,
11715 bfd_size_type offset, bfd_size_type size)
11716 {
11717 struct dwarf2_section_info result;
11718 asection *sectp;
11719
11720 gdb_assert (section != NULL);
11721 gdb_assert (!section->is_virtual);
11722
11723 memset (&result, 0, sizeof (result));
11724 result.s.containing_section = section;
11725 result.is_virtual = true;
11726
11727 if (size == 0)
11728 return result;
11729
11730 sectp = section->get_bfd_section ();
11731
11732 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11733 bounds of the real section. This is a pretty-rare event, so just
11734 flag an error (easier) instead of a warning and trying to cope. */
11735 if (sectp == NULL
11736 || offset + size > bfd_section_size (sectp))
11737 {
11738 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11739 " in section %s [in module %s]"),
11740 sectp ? bfd_section_name (sectp) : "<unknown>",
11741 objfile_name (dwarf2_per_objfile->objfile));
11742 }
11743
11744 result.virtual_offset = offset;
11745 result.size = size;
11746 return result;
11747 }
11748
11749 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11750 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11751 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11752 This is for DWP version 2 files. */
11753
11754 static struct dwo_unit *
11755 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11756 struct dwp_file *dwp_file,
11757 uint32_t unit_index,
11758 const char *comp_dir,
11759 ULONGEST signature, int is_debug_types)
11760 {
11761 struct objfile *objfile = dwarf2_per_objfile->objfile;
11762 const struct dwp_hash_table *dwp_htab =
11763 is_debug_types ? dwp_file->tus : dwp_file->cus;
11764 bfd *dbfd = dwp_file->dbfd.get ();
11765 const char *kind = is_debug_types ? "TU" : "CU";
11766 struct dwo_file *dwo_file;
11767 struct dwo_unit *dwo_unit;
11768 struct virtual_v2_dwo_sections sections;
11769 void **dwo_file_slot;
11770 int i;
11771
11772 gdb_assert (dwp_file->version == 2);
11773
11774 if (dwarf_read_debug)
11775 {
11776 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11777 kind,
11778 pulongest (unit_index), hex_string (signature),
11779 dwp_file->name);
11780 }
11781
11782 /* Fetch the section offsets of this DWO unit. */
11783
11784 memset (&sections, 0, sizeof (sections));
11785
11786 for (i = 0; i < dwp_htab->nr_columns; ++i)
11787 {
11788 uint32_t offset = read_4_bytes (dbfd,
11789 dwp_htab->section_pool.v2.offsets
11790 + (((unit_index - 1) * dwp_htab->nr_columns
11791 + i)
11792 * sizeof (uint32_t)));
11793 uint32_t size = read_4_bytes (dbfd,
11794 dwp_htab->section_pool.v2.sizes
11795 + (((unit_index - 1) * dwp_htab->nr_columns
11796 + i)
11797 * sizeof (uint32_t)));
11798
11799 switch (dwp_htab->section_pool.v2.section_ids[i])
11800 {
11801 case DW_SECT_INFO:
11802 case DW_SECT_TYPES:
11803 sections.info_or_types_offset = offset;
11804 sections.info_or_types_size = size;
11805 break;
11806 case DW_SECT_ABBREV:
11807 sections.abbrev_offset = offset;
11808 sections.abbrev_size = size;
11809 break;
11810 case DW_SECT_LINE:
11811 sections.line_offset = offset;
11812 sections.line_size = size;
11813 break;
11814 case DW_SECT_LOC:
11815 sections.loc_offset = offset;
11816 sections.loc_size = size;
11817 break;
11818 case DW_SECT_STR_OFFSETS:
11819 sections.str_offsets_offset = offset;
11820 sections.str_offsets_size = size;
11821 break;
11822 case DW_SECT_MACINFO:
11823 sections.macinfo_offset = offset;
11824 sections.macinfo_size = size;
11825 break;
11826 case DW_SECT_MACRO:
11827 sections.macro_offset = offset;
11828 sections.macro_size = size;
11829 break;
11830 }
11831 }
11832
11833 /* It's easier for the rest of the code if we fake a struct dwo_file and
11834 have dwo_unit "live" in that. At least for now.
11835
11836 The DWP file can be made up of a random collection of CUs and TUs.
11837 However, for each CU + set of TUs that came from the same original DWO
11838 file, we can combine them back into a virtual DWO file to save space
11839 (fewer struct dwo_file objects to allocate). Remember that for really
11840 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11841
11842 std::string virtual_dwo_name =
11843 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11844 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11845 (long) (sections.line_size ? sections.line_offset : 0),
11846 (long) (sections.loc_size ? sections.loc_offset : 0),
11847 (long) (sections.str_offsets_size
11848 ? sections.str_offsets_offset : 0));
11849 /* Can we use an existing virtual DWO file? */
11850 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11851 virtual_dwo_name.c_str (),
11852 comp_dir);
11853 /* Create one if necessary. */
11854 if (*dwo_file_slot == NULL)
11855 {
11856 if (dwarf_read_debug)
11857 {
11858 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11859 virtual_dwo_name.c_str ());
11860 }
11861 dwo_file = new struct dwo_file;
11862 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11863 dwo_file->comp_dir = comp_dir;
11864 dwo_file->sections.abbrev =
11865 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11866 sections.abbrev_offset, sections.abbrev_size);
11867 dwo_file->sections.line =
11868 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11869 sections.line_offset, sections.line_size);
11870 dwo_file->sections.loc =
11871 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11872 sections.loc_offset, sections.loc_size);
11873 dwo_file->sections.macinfo =
11874 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11875 sections.macinfo_offset, sections.macinfo_size);
11876 dwo_file->sections.macro =
11877 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11878 sections.macro_offset, sections.macro_size);
11879 dwo_file->sections.str_offsets =
11880 create_dwp_v2_section (dwarf2_per_objfile,
11881 &dwp_file->sections.str_offsets,
11882 sections.str_offsets_offset,
11883 sections.str_offsets_size);
11884 /* The "str" section is global to the entire DWP file. */
11885 dwo_file->sections.str = dwp_file->sections.str;
11886 /* The info or types section is assigned below to dwo_unit,
11887 there's no need to record it in dwo_file.
11888 Also, we can't simply record type sections in dwo_file because
11889 we record a pointer into the vector in dwo_unit. As we collect more
11890 types we'll grow the vector and eventually have to reallocate space
11891 for it, invalidating all copies of pointers into the previous
11892 contents. */
11893 *dwo_file_slot = dwo_file;
11894 }
11895 else
11896 {
11897 if (dwarf_read_debug)
11898 {
11899 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11900 virtual_dwo_name.c_str ());
11901 }
11902 dwo_file = (struct dwo_file *) *dwo_file_slot;
11903 }
11904
11905 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11906 dwo_unit->dwo_file = dwo_file;
11907 dwo_unit->signature = signature;
11908 dwo_unit->section =
11909 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11910 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11911 is_debug_types
11912 ? &dwp_file->sections.types
11913 : &dwp_file->sections.info,
11914 sections.info_or_types_offset,
11915 sections.info_or_types_size);
11916 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11917
11918 return dwo_unit;
11919 }
11920
11921 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11922 Returns NULL if the signature isn't found. */
11923
11924 static struct dwo_unit *
11925 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11926 struct dwp_file *dwp_file, const char *comp_dir,
11927 ULONGEST signature, int is_debug_types)
11928 {
11929 const struct dwp_hash_table *dwp_htab =
11930 is_debug_types ? dwp_file->tus : dwp_file->cus;
11931 bfd *dbfd = dwp_file->dbfd.get ();
11932 uint32_t mask = dwp_htab->nr_slots - 1;
11933 uint32_t hash = signature & mask;
11934 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11935 unsigned int i;
11936 void **slot;
11937 struct dwo_unit find_dwo_cu;
11938
11939 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11940 find_dwo_cu.signature = signature;
11941 slot = htab_find_slot (is_debug_types
11942 ? dwp_file->loaded_tus.get ()
11943 : dwp_file->loaded_cus.get (),
11944 &find_dwo_cu, INSERT);
11945
11946 if (*slot != NULL)
11947 return (struct dwo_unit *) *slot;
11948
11949 /* Use a for loop so that we don't loop forever on bad debug info. */
11950 for (i = 0; i < dwp_htab->nr_slots; ++i)
11951 {
11952 ULONGEST signature_in_table;
11953
11954 signature_in_table =
11955 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11956 if (signature_in_table == signature)
11957 {
11958 uint32_t unit_index =
11959 read_4_bytes (dbfd,
11960 dwp_htab->unit_table + hash * sizeof (uint32_t));
11961
11962 if (dwp_file->version == 1)
11963 {
11964 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11965 dwp_file, unit_index,
11966 comp_dir, signature,
11967 is_debug_types);
11968 }
11969 else
11970 {
11971 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11972 dwp_file, unit_index,
11973 comp_dir, signature,
11974 is_debug_types);
11975 }
11976 return (struct dwo_unit *) *slot;
11977 }
11978 if (signature_in_table == 0)
11979 return NULL;
11980 hash = (hash + hash2) & mask;
11981 }
11982
11983 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11984 " [in module %s]"),
11985 dwp_file->name);
11986 }
11987
11988 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11989 Open the file specified by FILE_NAME and hand it off to BFD for
11990 preliminary analysis. Return a newly initialized bfd *, which
11991 includes a canonicalized copy of FILE_NAME.
11992 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11993 SEARCH_CWD is true if the current directory is to be searched.
11994 It will be searched before debug-file-directory.
11995 If successful, the file is added to the bfd include table of the
11996 objfile's bfd (see gdb_bfd_record_inclusion).
11997 If unable to find/open the file, return NULL.
11998 NOTE: This function is derived from symfile_bfd_open. */
11999
12000 static gdb_bfd_ref_ptr
12001 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12002 const char *file_name, int is_dwp, int search_cwd)
12003 {
12004 int desc;
12005 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12006 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12007 to debug_file_directory. */
12008 const char *search_path;
12009 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12010
12011 gdb::unique_xmalloc_ptr<char> search_path_holder;
12012 if (search_cwd)
12013 {
12014 if (*debug_file_directory != '\0')
12015 {
12016 search_path_holder.reset (concat (".", dirname_separator_string,
12017 debug_file_directory,
12018 (char *) NULL));
12019 search_path = search_path_holder.get ();
12020 }
12021 else
12022 search_path = ".";
12023 }
12024 else
12025 search_path = debug_file_directory;
12026
12027 openp_flags flags = OPF_RETURN_REALPATH;
12028 if (is_dwp)
12029 flags |= OPF_SEARCH_IN_PATH;
12030
12031 gdb::unique_xmalloc_ptr<char> absolute_name;
12032 desc = openp (search_path, flags, file_name,
12033 O_RDONLY | O_BINARY, &absolute_name);
12034 if (desc < 0)
12035 return NULL;
12036
12037 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12038 gnutarget, desc));
12039 if (sym_bfd == NULL)
12040 return NULL;
12041 bfd_set_cacheable (sym_bfd.get (), 1);
12042
12043 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12044 return NULL;
12045
12046 /* Success. Record the bfd as having been included by the objfile's bfd.
12047 This is important because things like demangled_names_hash lives in the
12048 objfile's per_bfd space and may have references to things like symbol
12049 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12050 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12051
12052 return sym_bfd;
12053 }
12054
12055 /* Try to open DWO file FILE_NAME.
12056 COMP_DIR is the DW_AT_comp_dir attribute.
12057 The result is the bfd handle of the file.
12058 If there is a problem finding or opening the file, return NULL.
12059 Upon success, the canonicalized path of the file is stored in the bfd,
12060 same as symfile_bfd_open. */
12061
12062 static gdb_bfd_ref_ptr
12063 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12064 const char *file_name, const char *comp_dir)
12065 {
12066 if (IS_ABSOLUTE_PATH (file_name))
12067 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12068 0 /*is_dwp*/, 0 /*search_cwd*/);
12069
12070 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12071
12072 if (comp_dir != NULL)
12073 {
12074 gdb::unique_xmalloc_ptr<char> path_to_try
12075 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12076
12077 /* NOTE: If comp_dir is a relative path, this will also try the
12078 search path, which seems useful. */
12079 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12080 path_to_try.get (),
12081 0 /*is_dwp*/,
12082 1 /*search_cwd*/));
12083 if (abfd != NULL)
12084 return abfd;
12085 }
12086
12087 /* That didn't work, try debug-file-directory, which, despite its name,
12088 is a list of paths. */
12089
12090 if (*debug_file_directory == '\0')
12091 return NULL;
12092
12093 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12094 0 /*is_dwp*/, 1 /*search_cwd*/);
12095 }
12096
12097 /* This function is mapped across the sections and remembers the offset and
12098 size of each of the DWO debugging sections we are interested in. */
12099
12100 static void
12101 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12102 {
12103 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12104 const struct dwop_section_names *names = &dwop_section_names;
12105
12106 if (section_is_p (sectp->name, &names->abbrev_dwo))
12107 {
12108 dwo_sections->abbrev.s.section = sectp;
12109 dwo_sections->abbrev.size = bfd_section_size (sectp);
12110 }
12111 else if (section_is_p (sectp->name, &names->info_dwo))
12112 {
12113 dwo_sections->info.s.section = sectp;
12114 dwo_sections->info.size = bfd_section_size (sectp);
12115 }
12116 else if (section_is_p (sectp->name, &names->line_dwo))
12117 {
12118 dwo_sections->line.s.section = sectp;
12119 dwo_sections->line.size = bfd_section_size (sectp);
12120 }
12121 else if (section_is_p (sectp->name, &names->loc_dwo))
12122 {
12123 dwo_sections->loc.s.section = sectp;
12124 dwo_sections->loc.size = bfd_section_size (sectp);
12125 }
12126 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12127 {
12128 dwo_sections->macinfo.s.section = sectp;
12129 dwo_sections->macinfo.size = bfd_section_size (sectp);
12130 }
12131 else if (section_is_p (sectp->name, &names->macro_dwo))
12132 {
12133 dwo_sections->macro.s.section = sectp;
12134 dwo_sections->macro.size = bfd_section_size (sectp);
12135 }
12136 else if (section_is_p (sectp->name, &names->str_dwo))
12137 {
12138 dwo_sections->str.s.section = sectp;
12139 dwo_sections->str.size = bfd_section_size (sectp);
12140 }
12141 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12142 {
12143 dwo_sections->str_offsets.s.section = sectp;
12144 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12145 }
12146 else if (section_is_p (sectp->name, &names->types_dwo))
12147 {
12148 struct dwarf2_section_info type_section;
12149
12150 memset (&type_section, 0, sizeof (type_section));
12151 type_section.s.section = sectp;
12152 type_section.size = bfd_section_size (sectp);
12153 dwo_sections->types.push_back (type_section);
12154 }
12155 }
12156
12157 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12158 by PER_CU. This is for the non-DWP case.
12159 The result is NULL if DWO_NAME can't be found. */
12160
12161 static struct dwo_file *
12162 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12163 const char *dwo_name, const char *comp_dir)
12164 {
12165 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12166
12167 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12168 if (dbfd == NULL)
12169 {
12170 if (dwarf_read_debug)
12171 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12172 return NULL;
12173 }
12174
12175 dwo_file_up dwo_file (new struct dwo_file);
12176 dwo_file->dwo_name = dwo_name;
12177 dwo_file->comp_dir = comp_dir;
12178 dwo_file->dbfd = std::move (dbfd);
12179
12180 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12181 &dwo_file->sections);
12182
12183 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12184 dwo_file->sections.info, dwo_file->cus);
12185
12186 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12187 dwo_file->sections.types, dwo_file->tus);
12188
12189 if (dwarf_read_debug)
12190 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12191
12192 return dwo_file.release ();
12193 }
12194
12195 /* This function is mapped across the sections and remembers the offset and
12196 size of each of the DWP debugging sections common to version 1 and 2 that
12197 we are interested in. */
12198
12199 static void
12200 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12201 void *dwp_file_ptr)
12202 {
12203 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12204 const struct dwop_section_names *names = &dwop_section_names;
12205 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12206
12207 /* Record the ELF section number for later lookup: this is what the
12208 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12209 gdb_assert (elf_section_nr < dwp_file->num_sections);
12210 dwp_file->elf_sections[elf_section_nr] = sectp;
12211
12212 /* Look for specific sections that we need. */
12213 if (section_is_p (sectp->name, &names->str_dwo))
12214 {
12215 dwp_file->sections.str.s.section = sectp;
12216 dwp_file->sections.str.size = bfd_section_size (sectp);
12217 }
12218 else if (section_is_p (sectp->name, &names->cu_index))
12219 {
12220 dwp_file->sections.cu_index.s.section = sectp;
12221 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12222 }
12223 else if (section_is_p (sectp->name, &names->tu_index))
12224 {
12225 dwp_file->sections.tu_index.s.section = sectp;
12226 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12227 }
12228 }
12229
12230 /* This function is mapped across the sections and remembers the offset and
12231 size of each of the DWP version 2 debugging sections that we are interested
12232 in. This is split into a separate function because we don't know if we
12233 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12234
12235 static void
12236 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12237 {
12238 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12239 const struct dwop_section_names *names = &dwop_section_names;
12240 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12241
12242 /* Record the ELF section number for later lookup: this is what the
12243 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12244 gdb_assert (elf_section_nr < dwp_file->num_sections);
12245 dwp_file->elf_sections[elf_section_nr] = sectp;
12246
12247 /* Look for specific sections that we need. */
12248 if (section_is_p (sectp->name, &names->abbrev_dwo))
12249 {
12250 dwp_file->sections.abbrev.s.section = sectp;
12251 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12252 }
12253 else if (section_is_p (sectp->name, &names->info_dwo))
12254 {
12255 dwp_file->sections.info.s.section = sectp;
12256 dwp_file->sections.info.size = bfd_section_size (sectp);
12257 }
12258 else if (section_is_p (sectp->name, &names->line_dwo))
12259 {
12260 dwp_file->sections.line.s.section = sectp;
12261 dwp_file->sections.line.size = bfd_section_size (sectp);
12262 }
12263 else if (section_is_p (sectp->name, &names->loc_dwo))
12264 {
12265 dwp_file->sections.loc.s.section = sectp;
12266 dwp_file->sections.loc.size = bfd_section_size (sectp);
12267 }
12268 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12269 {
12270 dwp_file->sections.macinfo.s.section = sectp;
12271 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12272 }
12273 else if (section_is_p (sectp->name, &names->macro_dwo))
12274 {
12275 dwp_file->sections.macro.s.section = sectp;
12276 dwp_file->sections.macro.size = bfd_section_size (sectp);
12277 }
12278 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12279 {
12280 dwp_file->sections.str_offsets.s.section = sectp;
12281 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12282 }
12283 else if (section_is_p (sectp->name, &names->types_dwo))
12284 {
12285 dwp_file->sections.types.s.section = sectp;
12286 dwp_file->sections.types.size = bfd_section_size (sectp);
12287 }
12288 }
12289
12290 /* Hash function for dwp_file loaded CUs/TUs. */
12291
12292 static hashval_t
12293 hash_dwp_loaded_cutus (const void *item)
12294 {
12295 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12296
12297 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12298 return dwo_unit->signature;
12299 }
12300
12301 /* Equality function for dwp_file loaded CUs/TUs. */
12302
12303 static int
12304 eq_dwp_loaded_cutus (const void *a, const void *b)
12305 {
12306 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12307 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12308
12309 return dua->signature == dub->signature;
12310 }
12311
12312 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12313
12314 static htab_up
12315 allocate_dwp_loaded_cutus_table ()
12316 {
12317 return htab_up (htab_create_alloc (3,
12318 hash_dwp_loaded_cutus,
12319 eq_dwp_loaded_cutus,
12320 NULL, xcalloc, xfree));
12321 }
12322
12323 /* Try to open DWP file FILE_NAME.
12324 The result is the bfd handle of the file.
12325 If there is a problem finding or opening the file, return NULL.
12326 Upon success, the canonicalized path of the file is stored in the bfd,
12327 same as symfile_bfd_open. */
12328
12329 static gdb_bfd_ref_ptr
12330 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12331 const char *file_name)
12332 {
12333 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12334 1 /*is_dwp*/,
12335 1 /*search_cwd*/));
12336 if (abfd != NULL)
12337 return abfd;
12338
12339 /* Work around upstream bug 15652.
12340 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12341 [Whether that's a "bug" is debatable, but it is getting in our way.]
12342 We have no real idea where the dwp file is, because gdb's realpath-ing
12343 of the executable's path may have discarded the needed info.
12344 [IWBN if the dwp file name was recorded in the executable, akin to
12345 .gnu_debuglink, but that doesn't exist yet.]
12346 Strip the directory from FILE_NAME and search again. */
12347 if (*debug_file_directory != '\0')
12348 {
12349 /* Don't implicitly search the current directory here.
12350 If the user wants to search "." to handle this case,
12351 it must be added to debug-file-directory. */
12352 return try_open_dwop_file (dwarf2_per_objfile,
12353 lbasename (file_name), 1 /*is_dwp*/,
12354 0 /*search_cwd*/);
12355 }
12356
12357 return NULL;
12358 }
12359
12360 /* Initialize the use of the DWP file for the current objfile.
12361 By convention the name of the DWP file is ${objfile}.dwp.
12362 The result is NULL if it can't be found. */
12363
12364 static std::unique_ptr<struct dwp_file>
12365 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12366 {
12367 struct objfile *objfile = dwarf2_per_objfile->objfile;
12368
12369 /* Try to find first .dwp for the binary file before any symbolic links
12370 resolving. */
12371
12372 /* If the objfile is a debug file, find the name of the real binary
12373 file and get the name of dwp file from there. */
12374 std::string dwp_name;
12375 if (objfile->separate_debug_objfile_backlink != NULL)
12376 {
12377 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12378 const char *backlink_basename = lbasename (backlink->original_name);
12379
12380 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12381 }
12382 else
12383 dwp_name = objfile->original_name;
12384
12385 dwp_name += ".dwp";
12386
12387 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12388 if (dbfd == NULL
12389 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12390 {
12391 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12392 dwp_name = objfile_name (objfile);
12393 dwp_name += ".dwp";
12394 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12395 }
12396
12397 if (dbfd == NULL)
12398 {
12399 if (dwarf_read_debug)
12400 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12401 return std::unique_ptr<dwp_file> ();
12402 }
12403
12404 const char *name = bfd_get_filename (dbfd.get ());
12405 std::unique_ptr<struct dwp_file> dwp_file
12406 (new struct dwp_file (name, std::move (dbfd)));
12407
12408 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12409 dwp_file->elf_sections =
12410 OBSTACK_CALLOC (&objfile->objfile_obstack,
12411 dwp_file->num_sections, asection *);
12412
12413 bfd_map_over_sections (dwp_file->dbfd.get (),
12414 dwarf2_locate_common_dwp_sections,
12415 dwp_file.get ());
12416
12417 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12418 0);
12419
12420 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12421 1);
12422
12423 /* The DWP file version is stored in the hash table. Oh well. */
12424 if (dwp_file->cus && dwp_file->tus
12425 && dwp_file->cus->version != dwp_file->tus->version)
12426 {
12427 /* Technically speaking, we should try to limp along, but this is
12428 pretty bizarre. We use pulongest here because that's the established
12429 portability solution (e.g, we cannot use %u for uint32_t). */
12430 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12431 " TU version %s [in DWP file %s]"),
12432 pulongest (dwp_file->cus->version),
12433 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12434 }
12435
12436 if (dwp_file->cus)
12437 dwp_file->version = dwp_file->cus->version;
12438 else if (dwp_file->tus)
12439 dwp_file->version = dwp_file->tus->version;
12440 else
12441 dwp_file->version = 2;
12442
12443 if (dwp_file->version == 2)
12444 bfd_map_over_sections (dwp_file->dbfd.get (),
12445 dwarf2_locate_v2_dwp_sections,
12446 dwp_file.get ());
12447
12448 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12449 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12450
12451 if (dwarf_read_debug)
12452 {
12453 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12454 fprintf_unfiltered (gdb_stdlog,
12455 " %s CUs, %s TUs\n",
12456 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12457 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12458 }
12459
12460 return dwp_file;
12461 }
12462
12463 /* Wrapper around open_and_init_dwp_file, only open it once. */
12464
12465 static struct dwp_file *
12466 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12467 {
12468 if (! dwarf2_per_objfile->dwp_checked)
12469 {
12470 dwarf2_per_objfile->dwp_file
12471 = open_and_init_dwp_file (dwarf2_per_objfile);
12472 dwarf2_per_objfile->dwp_checked = 1;
12473 }
12474 return dwarf2_per_objfile->dwp_file.get ();
12475 }
12476
12477 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12478 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12479 or in the DWP file for the objfile, referenced by THIS_UNIT.
12480 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12481 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12482
12483 This is called, for example, when wanting to read a variable with a
12484 complex location. Therefore we don't want to do file i/o for every call.
12485 Therefore we don't want to look for a DWO file on every call.
12486 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12487 then we check if we've already seen DWO_NAME, and only THEN do we check
12488 for a DWO file.
12489
12490 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12491 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12492
12493 static struct dwo_unit *
12494 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12495 const char *dwo_name, const char *comp_dir,
12496 ULONGEST signature, int is_debug_types)
12497 {
12498 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12499 struct objfile *objfile = dwarf2_per_objfile->objfile;
12500 const char *kind = is_debug_types ? "TU" : "CU";
12501 void **dwo_file_slot;
12502 struct dwo_file *dwo_file;
12503 struct dwp_file *dwp_file;
12504
12505 /* First see if there's a DWP file.
12506 If we have a DWP file but didn't find the DWO inside it, don't
12507 look for the original DWO file. It makes gdb behave differently
12508 depending on whether one is debugging in the build tree. */
12509
12510 dwp_file = get_dwp_file (dwarf2_per_objfile);
12511 if (dwp_file != NULL)
12512 {
12513 const struct dwp_hash_table *dwp_htab =
12514 is_debug_types ? dwp_file->tus : dwp_file->cus;
12515
12516 if (dwp_htab != NULL)
12517 {
12518 struct dwo_unit *dwo_cutu =
12519 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12520 signature, is_debug_types);
12521
12522 if (dwo_cutu != NULL)
12523 {
12524 if (dwarf_read_debug)
12525 {
12526 fprintf_unfiltered (gdb_stdlog,
12527 "Virtual DWO %s %s found: @%s\n",
12528 kind, hex_string (signature),
12529 host_address_to_string (dwo_cutu));
12530 }
12531 return dwo_cutu;
12532 }
12533 }
12534 }
12535 else
12536 {
12537 /* No DWP file, look for the DWO file. */
12538
12539 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12540 dwo_name, comp_dir);
12541 if (*dwo_file_slot == NULL)
12542 {
12543 /* Read in the file and build a table of the CUs/TUs it contains. */
12544 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12545 }
12546 /* NOTE: This will be NULL if unable to open the file. */
12547 dwo_file = (struct dwo_file *) *dwo_file_slot;
12548
12549 if (dwo_file != NULL)
12550 {
12551 struct dwo_unit *dwo_cutu = NULL;
12552
12553 if (is_debug_types && dwo_file->tus)
12554 {
12555 struct dwo_unit find_dwo_cutu;
12556
12557 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12558 find_dwo_cutu.signature = signature;
12559 dwo_cutu
12560 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12561 &find_dwo_cutu);
12562 }
12563 else if (!is_debug_types && dwo_file->cus)
12564 {
12565 struct dwo_unit find_dwo_cutu;
12566
12567 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12568 find_dwo_cutu.signature = signature;
12569 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12570 &find_dwo_cutu);
12571 }
12572
12573 if (dwo_cutu != NULL)
12574 {
12575 if (dwarf_read_debug)
12576 {
12577 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12578 kind, dwo_name, hex_string (signature),
12579 host_address_to_string (dwo_cutu));
12580 }
12581 return dwo_cutu;
12582 }
12583 }
12584 }
12585
12586 /* We didn't find it. This could mean a dwo_id mismatch, or
12587 someone deleted the DWO/DWP file, or the search path isn't set up
12588 correctly to find the file. */
12589
12590 if (dwarf_read_debug)
12591 {
12592 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12593 kind, dwo_name, hex_string (signature));
12594 }
12595
12596 /* This is a warning and not a complaint because it can be caused by
12597 pilot error (e.g., user accidentally deleting the DWO). */
12598 {
12599 /* Print the name of the DWP file if we looked there, helps the user
12600 better diagnose the problem. */
12601 std::string dwp_text;
12602
12603 if (dwp_file != NULL)
12604 dwp_text = string_printf (" [in DWP file %s]",
12605 lbasename (dwp_file->name));
12606
12607 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12608 " [in module %s]"),
12609 kind, dwo_name, hex_string (signature),
12610 dwp_text.c_str (),
12611 this_unit->is_debug_types ? "TU" : "CU",
12612 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12613 }
12614 return NULL;
12615 }
12616
12617 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12618 See lookup_dwo_cutu_unit for details. */
12619
12620 static struct dwo_unit *
12621 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12622 const char *dwo_name, const char *comp_dir,
12623 ULONGEST signature)
12624 {
12625 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12626 }
12627
12628 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12629 See lookup_dwo_cutu_unit for details. */
12630
12631 static struct dwo_unit *
12632 lookup_dwo_type_unit (struct signatured_type *this_tu,
12633 const char *dwo_name, const char *comp_dir)
12634 {
12635 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12636 }
12637
12638 /* Traversal function for queue_and_load_all_dwo_tus. */
12639
12640 static int
12641 queue_and_load_dwo_tu (void **slot, void *info)
12642 {
12643 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12644 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12645 ULONGEST signature = dwo_unit->signature;
12646 struct signatured_type *sig_type =
12647 lookup_dwo_signatured_type (per_cu->cu, signature);
12648
12649 if (sig_type != NULL)
12650 {
12651 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12652
12653 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12654 a real dependency of PER_CU on SIG_TYPE. That is detected later
12655 while processing PER_CU. */
12656 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12657 load_full_type_unit (sig_cu);
12658 per_cu->imported_symtabs_push (sig_cu);
12659 }
12660
12661 return 1;
12662 }
12663
12664 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12665 The DWO may have the only definition of the type, though it may not be
12666 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12667 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12668
12669 static void
12670 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12671 {
12672 struct dwo_unit *dwo_unit;
12673 struct dwo_file *dwo_file;
12674
12675 gdb_assert (!per_cu->is_debug_types);
12676 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12677 gdb_assert (per_cu->cu != NULL);
12678
12679 dwo_unit = per_cu->cu->dwo_unit;
12680 gdb_assert (dwo_unit != NULL);
12681
12682 dwo_file = dwo_unit->dwo_file;
12683 if (dwo_file->tus != NULL)
12684 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12685 per_cu);
12686 }
12687
12688 /* Read in various DIEs. */
12689
12690 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12691 Inherit only the children of the DW_AT_abstract_origin DIE not being
12692 already referenced by DW_AT_abstract_origin from the children of the
12693 current DIE. */
12694
12695 static void
12696 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12697 {
12698 struct die_info *child_die;
12699 sect_offset *offsetp;
12700 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12701 struct die_info *origin_die;
12702 /* Iterator of the ORIGIN_DIE children. */
12703 struct die_info *origin_child_die;
12704 struct attribute *attr;
12705 struct dwarf2_cu *origin_cu;
12706 struct pending **origin_previous_list_in_scope;
12707
12708 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12709 if (!attr)
12710 return;
12711
12712 /* Note that following die references may follow to a die in a
12713 different cu. */
12714
12715 origin_cu = cu;
12716 origin_die = follow_die_ref (die, attr, &origin_cu);
12717
12718 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12719 symbols in. */
12720 origin_previous_list_in_scope = origin_cu->list_in_scope;
12721 origin_cu->list_in_scope = cu->list_in_scope;
12722
12723 if (die->tag != origin_die->tag
12724 && !(die->tag == DW_TAG_inlined_subroutine
12725 && origin_die->tag == DW_TAG_subprogram))
12726 complaint (_("DIE %s and its abstract origin %s have different tags"),
12727 sect_offset_str (die->sect_off),
12728 sect_offset_str (origin_die->sect_off));
12729
12730 std::vector<sect_offset> offsets;
12731
12732 for (child_die = die->child;
12733 child_die && child_die->tag;
12734 child_die = sibling_die (child_die))
12735 {
12736 struct die_info *child_origin_die;
12737 struct dwarf2_cu *child_origin_cu;
12738
12739 /* We are trying to process concrete instance entries:
12740 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12741 it's not relevant to our analysis here. i.e. detecting DIEs that are
12742 present in the abstract instance but not referenced in the concrete
12743 one. */
12744 if (child_die->tag == DW_TAG_call_site
12745 || child_die->tag == DW_TAG_GNU_call_site)
12746 continue;
12747
12748 /* For each CHILD_DIE, find the corresponding child of
12749 ORIGIN_DIE. If there is more than one layer of
12750 DW_AT_abstract_origin, follow them all; there shouldn't be,
12751 but GCC versions at least through 4.4 generate this (GCC PR
12752 40573). */
12753 child_origin_die = child_die;
12754 child_origin_cu = cu;
12755 while (1)
12756 {
12757 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12758 child_origin_cu);
12759 if (attr == NULL)
12760 break;
12761 child_origin_die = follow_die_ref (child_origin_die, attr,
12762 &child_origin_cu);
12763 }
12764
12765 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12766 counterpart may exist. */
12767 if (child_origin_die != child_die)
12768 {
12769 if (child_die->tag != child_origin_die->tag
12770 && !(child_die->tag == DW_TAG_inlined_subroutine
12771 && child_origin_die->tag == DW_TAG_subprogram))
12772 complaint (_("Child DIE %s and its abstract origin %s have "
12773 "different tags"),
12774 sect_offset_str (child_die->sect_off),
12775 sect_offset_str (child_origin_die->sect_off));
12776 if (child_origin_die->parent != origin_die)
12777 complaint (_("Child DIE %s and its abstract origin %s have "
12778 "different parents"),
12779 sect_offset_str (child_die->sect_off),
12780 sect_offset_str (child_origin_die->sect_off));
12781 else
12782 offsets.push_back (child_origin_die->sect_off);
12783 }
12784 }
12785 std::sort (offsets.begin (), offsets.end ());
12786 sect_offset *offsets_end = offsets.data () + offsets.size ();
12787 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12788 if (offsetp[-1] == *offsetp)
12789 complaint (_("Multiple children of DIE %s refer "
12790 "to DIE %s as their abstract origin"),
12791 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12792
12793 offsetp = offsets.data ();
12794 origin_child_die = origin_die->child;
12795 while (origin_child_die && origin_child_die->tag)
12796 {
12797 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12798 while (offsetp < offsets_end
12799 && *offsetp < origin_child_die->sect_off)
12800 offsetp++;
12801 if (offsetp >= offsets_end
12802 || *offsetp > origin_child_die->sect_off)
12803 {
12804 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12805 Check whether we're already processing ORIGIN_CHILD_DIE.
12806 This can happen with mutually referenced abstract_origins.
12807 PR 16581. */
12808 if (!origin_child_die->in_process)
12809 process_die (origin_child_die, origin_cu);
12810 }
12811 origin_child_die = sibling_die (origin_child_die);
12812 }
12813 origin_cu->list_in_scope = origin_previous_list_in_scope;
12814
12815 if (cu != origin_cu)
12816 compute_delayed_physnames (origin_cu);
12817 }
12818
12819 static void
12820 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12821 {
12822 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12823 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12824 struct context_stack *newobj;
12825 CORE_ADDR lowpc;
12826 CORE_ADDR highpc;
12827 struct die_info *child_die;
12828 struct attribute *attr, *call_line, *call_file;
12829 const char *name;
12830 CORE_ADDR baseaddr;
12831 struct block *block;
12832 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12833 std::vector<struct symbol *> template_args;
12834 struct template_symbol *templ_func = NULL;
12835
12836 if (inlined_func)
12837 {
12838 /* If we do not have call site information, we can't show the
12839 caller of this inlined function. That's too confusing, so
12840 only use the scope for local variables. */
12841 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12842 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12843 if (call_line == NULL || call_file == NULL)
12844 {
12845 read_lexical_block_scope (die, cu);
12846 return;
12847 }
12848 }
12849
12850 baseaddr = objfile->text_section_offset ();
12851
12852 name = dwarf2_name (die, cu);
12853
12854 /* Ignore functions with missing or empty names. These are actually
12855 illegal according to the DWARF standard. */
12856 if (name == NULL)
12857 {
12858 complaint (_("missing name for subprogram DIE at %s"),
12859 sect_offset_str (die->sect_off));
12860 return;
12861 }
12862
12863 /* Ignore functions with missing or invalid low and high pc attributes. */
12864 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12865 <= PC_BOUNDS_INVALID)
12866 {
12867 attr = dwarf2_attr (die, DW_AT_external, cu);
12868 if (!attr || !DW_UNSND (attr))
12869 complaint (_("cannot get low and high bounds "
12870 "for subprogram DIE at %s"),
12871 sect_offset_str (die->sect_off));
12872 return;
12873 }
12874
12875 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12876 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12877
12878 /* If we have any template arguments, then we must allocate a
12879 different sort of symbol. */
12880 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12881 {
12882 if (child_die->tag == DW_TAG_template_type_param
12883 || child_die->tag == DW_TAG_template_value_param)
12884 {
12885 templ_func = allocate_template_symbol (objfile);
12886 templ_func->subclass = SYMBOL_TEMPLATE;
12887 break;
12888 }
12889 }
12890
12891 newobj = cu->get_builder ()->push_context (0, lowpc);
12892 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12893 (struct symbol *) templ_func);
12894
12895 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12896 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12897 cu->language);
12898
12899 /* If there is a location expression for DW_AT_frame_base, record
12900 it. */
12901 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12902 if (attr != nullptr)
12903 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12904
12905 /* If there is a location for the static link, record it. */
12906 newobj->static_link = NULL;
12907 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12908 if (attr != nullptr)
12909 {
12910 newobj->static_link
12911 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12912 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12913 cu->per_cu->addr_type ());
12914 }
12915
12916 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12917
12918 if (die->child != NULL)
12919 {
12920 child_die = die->child;
12921 while (child_die && child_die->tag)
12922 {
12923 if (child_die->tag == DW_TAG_template_type_param
12924 || child_die->tag == DW_TAG_template_value_param)
12925 {
12926 struct symbol *arg = new_symbol (child_die, NULL, cu);
12927
12928 if (arg != NULL)
12929 template_args.push_back (arg);
12930 }
12931 else
12932 process_die (child_die, cu);
12933 child_die = sibling_die (child_die);
12934 }
12935 }
12936
12937 inherit_abstract_dies (die, cu);
12938
12939 /* If we have a DW_AT_specification, we might need to import using
12940 directives from the context of the specification DIE. See the
12941 comment in determine_prefix. */
12942 if (cu->language == language_cplus
12943 && dwarf2_attr (die, DW_AT_specification, cu))
12944 {
12945 struct dwarf2_cu *spec_cu = cu;
12946 struct die_info *spec_die = die_specification (die, &spec_cu);
12947
12948 while (spec_die)
12949 {
12950 child_die = spec_die->child;
12951 while (child_die && child_die->tag)
12952 {
12953 if (child_die->tag == DW_TAG_imported_module)
12954 process_die (child_die, spec_cu);
12955 child_die = sibling_die (child_die);
12956 }
12957
12958 /* In some cases, GCC generates specification DIEs that
12959 themselves contain DW_AT_specification attributes. */
12960 spec_die = die_specification (spec_die, &spec_cu);
12961 }
12962 }
12963
12964 struct context_stack cstk = cu->get_builder ()->pop_context ();
12965 /* Make a block for the local symbols within. */
12966 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12967 cstk.static_link, lowpc, highpc);
12968
12969 /* For C++, set the block's scope. */
12970 if ((cu->language == language_cplus
12971 || cu->language == language_fortran
12972 || cu->language == language_d
12973 || cu->language == language_rust)
12974 && cu->processing_has_namespace_info)
12975 block_set_scope (block, determine_prefix (die, cu),
12976 &objfile->objfile_obstack);
12977
12978 /* If we have address ranges, record them. */
12979 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12980
12981 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12982
12983 /* Attach template arguments to function. */
12984 if (!template_args.empty ())
12985 {
12986 gdb_assert (templ_func != NULL);
12987
12988 templ_func->n_template_arguments = template_args.size ();
12989 templ_func->template_arguments
12990 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12991 templ_func->n_template_arguments);
12992 memcpy (templ_func->template_arguments,
12993 template_args.data (),
12994 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12995
12996 /* Make sure that the symtab is set on the new symbols. Even
12997 though they don't appear in this symtab directly, other parts
12998 of gdb assume that symbols do, and this is reasonably
12999 true. */
13000 for (symbol *sym : template_args)
13001 symbol_set_symtab (sym, symbol_symtab (templ_func));
13002 }
13003
13004 /* In C++, we can have functions nested inside functions (e.g., when
13005 a function declares a class that has methods). This means that
13006 when we finish processing a function scope, we may need to go
13007 back to building a containing block's symbol lists. */
13008 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13009 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13010
13011 /* If we've finished processing a top-level function, subsequent
13012 symbols go in the file symbol list. */
13013 if (cu->get_builder ()->outermost_context_p ())
13014 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13015 }
13016
13017 /* Process all the DIES contained within a lexical block scope. Start
13018 a new scope, process the dies, and then close the scope. */
13019
13020 static void
13021 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13022 {
13023 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13024 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13025 CORE_ADDR lowpc, highpc;
13026 struct die_info *child_die;
13027 CORE_ADDR baseaddr;
13028
13029 baseaddr = objfile->text_section_offset ();
13030
13031 /* Ignore blocks with missing or invalid low and high pc attributes. */
13032 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13033 as multiple lexical blocks? Handling children in a sane way would
13034 be nasty. Might be easier to properly extend generic blocks to
13035 describe ranges. */
13036 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13037 {
13038 case PC_BOUNDS_NOT_PRESENT:
13039 /* DW_TAG_lexical_block has no attributes, process its children as if
13040 there was no wrapping by that DW_TAG_lexical_block.
13041 GCC does no longer produces such DWARF since GCC r224161. */
13042 for (child_die = die->child;
13043 child_die != NULL && child_die->tag;
13044 child_die = sibling_die (child_die))
13045 process_die (child_die, cu);
13046 return;
13047 case PC_BOUNDS_INVALID:
13048 return;
13049 }
13050 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13051 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13052
13053 cu->get_builder ()->push_context (0, lowpc);
13054 if (die->child != NULL)
13055 {
13056 child_die = die->child;
13057 while (child_die && child_die->tag)
13058 {
13059 process_die (child_die, cu);
13060 child_die = sibling_die (child_die);
13061 }
13062 }
13063 inherit_abstract_dies (die, cu);
13064 struct context_stack cstk = cu->get_builder ()->pop_context ();
13065
13066 if (*cu->get_builder ()->get_local_symbols () != NULL
13067 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13068 {
13069 struct block *block
13070 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13071 cstk.start_addr, highpc);
13072
13073 /* Note that recording ranges after traversing children, as we
13074 do here, means that recording a parent's ranges entails
13075 walking across all its children's ranges as they appear in
13076 the address map, which is quadratic behavior.
13077
13078 It would be nicer to record the parent's ranges before
13079 traversing its children, simply overriding whatever you find
13080 there. But since we don't even decide whether to create a
13081 block until after we've traversed its children, that's hard
13082 to do. */
13083 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13084 }
13085 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13086 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13087 }
13088
13089 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13090
13091 static void
13092 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13093 {
13094 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13095 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13096 CORE_ADDR pc, baseaddr;
13097 struct attribute *attr;
13098 struct call_site *call_site, call_site_local;
13099 void **slot;
13100 int nparams;
13101 struct die_info *child_die;
13102
13103 baseaddr = objfile->text_section_offset ();
13104
13105 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13106 if (attr == NULL)
13107 {
13108 /* This was a pre-DWARF-5 GNU extension alias
13109 for DW_AT_call_return_pc. */
13110 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13111 }
13112 if (!attr)
13113 {
13114 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13115 "DIE %s [in module %s]"),
13116 sect_offset_str (die->sect_off), objfile_name (objfile));
13117 return;
13118 }
13119 pc = attr->value_as_address () + baseaddr;
13120 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13121
13122 if (cu->call_site_htab == NULL)
13123 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13124 NULL, &objfile->objfile_obstack,
13125 hashtab_obstack_allocate, NULL);
13126 call_site_local.pc = pc;
13127 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13128 if (*slot != NULL)
13129 {
13130 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13131 "DIE %s [in module %s]"),
13132 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13133 objfile_name (objfile));
13134 return;
13135 }
13136
13137 /* Count parameters at the caller. */
13138
13139 nparams = 0;
13140 for (child_die = die->child; child_die && child_die->tag;
13141 child_die = sibling_die (child_die))
13142 {
13143 if (child_die->tag != DW_TAG_call_site_parameter
13144 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13145 {
13146 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13147 "DW_TAG_call_site child DIE %s [in module %s]"),
13148 child_die->tag, sect_offset_str (child_die->sect_off),
13149 objfile_name (objfile));
13150 continue;
13151 }
13152
13153 nparams++;
13154 }
13155
13156 call_site
13157 = ((struct call_site *)
13158 obstack_alloc (&objfile->objfile_obstack,
13159 sizeof (*call_site)
13160 + (sizeof (*call_site->parameter) * (nparams - 1))));
13161 *slot = call_site;
13162 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13163 call_site->pc = pc;
13164
13165 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13166 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13167 {
13168 struct die_info *func_die;
13169
13170 /* Skip also over DW_TAG_inlined_subroutine. */
13171 for (func_die = die->parent;
13172 func_die && func_die->tag != DW_TAG_subprogram
13173 && func_die->tag != DW_TAG_subroutine_type;
13174 func_die = func_die->parent);
13175
13176 /* DW_AT_call_all_calls is a superset
13177 of DW_AT_call_all_tail_calls. */
13178 if (func_die
13179 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13180 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13181 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13182 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13183 {
13184 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13185 not complete. But keep CALL_SITE for look ups via call_site_htab,
13186 both the initial caller containing the real return address PC and
13187 the final callee containing the current PC of a chain of tail
13188 calls do not need to have the tail call list complete. But any
13189 function candidate for a virtual tail call frame searched via
13190 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13191 determined unambiguously. */
13192 }
13193 else
13194 {
13195 struct type *func_type = NULL;
13196
13197 if (func_die)
13198 func_type = get_die_type (func_die, cu);
13199 if (func_type != NULL)
13200 {
13201 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13202
13203 /* Enlist this call site to the function. */
13204 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13205 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13206 }
13207 else
13208 complaint (_("Cannot find function owning DW_TAG_call_site "
13209 "DIE %s [in module %s]"),
13210 sect_offset_str (die->sect_off), objfile_name (objfile));
13211 }
13212 }
13213
13214 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13215 if (attr == NULL)
13216 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13217 if (attr == NULL)
13218 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13219 if (attr == NULL)
13220 {
13221 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13222 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13223 }
13224 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13225 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13226 /* Keep NULL DWARF_BLOCK. */;
13227 else if (attr->form_is_block ())
13228 {
13229 struct dwarf2_locexpr_baton *dlbaton;
13230
13231 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13232 dlbaton->data = DW_BLOCK (attr)->data;
13233 dlbaton->size = DW_BLOCK (attr)->size;
13234 dlbaton->per_cu = cu->per_cu;
13235
13236 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13237 }
13238 else if (attr->form_is_ref ())
13239 {
13240 struct dwarf2_cu *target_cu = cu;
13241 struct die_info *target_die;
13242
13243 target_die = follow_die_ref (die, attr, &target_cu);
13244 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13245 if (die_is_declaration (target_die, target_cu))
13246 {
13247 const char *target_physname;
13248
13249 /* Prefer the mangled name; otherwise compute the demangled one. */
13250 target_physname = dw2_linkage_name (target_die, target_cu);
13251 if (target_physname == NULL)
13252 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13253 if (target_physname == NULL)
13254 complaint (_("DW_AT_call_target target DIE has invalid "
13255 "physname, for referencing DIE %s [in module %s]"),
13256 sect_offset_str (die->sect_off), objfile_name (objfile));
13257 else
13258 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13259 }
13260 else
13261 {
13262 CORE_ADDR lowpc;
13263
13264 /* DW_AT_entry_pc should be preferred. */
13265 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13266 <= PC_BOUNDS_INVALID)
13267 complaint (_("DW_AT_call_target target DIE has invalid "
13268 "low pc, for referencing DIE %s [in module %s]"),
13269 sect_offset_str (die->sect_off), objfile_name (objfile));
13270 else
13271 {
13272 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13273 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13274 }
13275 }
13276 }
13277 else
13278 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13279 "block nor reference, for DIE %s [in module %s]"),
13280 sect_offset_str (die->sect_off), objfile_name (objfile));
13281
13282 call_site->per_cu = cu->per_cu;
13283
13284 for (child_die = die->child;
13285 child_die && child_die->tag;
13286 child_die = sibling_die (child_die))
13287 {
13288 struct call_site_parameter *parameter;
13289 struct attribute *loc, *origin;
13290
13291 if (child_die->tag != DW_TAG_call_site_parameter
13292 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13293 {
13294 /* Already printed the complaint above. */
13295 continue;
13296 }
13297
13298 gdb_assert (call_site->parameter_count < nparams);
13299 parameter = &call_site->parameter[call_site->parameter_count];
13300
13301 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13302 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13303 register is contained in DW_AT_call_value. */
13304
13305 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13306 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13307 if (origin == NULL)
13308 {
13309 /* This was a pre-DWARF-5 GNU extension alias
13310 for DW_AT_call_parameter. */
13311 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13312 }
13313 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13314 {
13315 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13316
13317 sect_offset sect_off
13318 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13319 if (!cu->header.offset_in_cu_p (sect_off))
13320 {
13321 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13322 binding can be done only inside one CU. Such referenced DIE
13323 therefore cannot be even moved to DW_TAG_partial_unit. */
13324 complaint (_("DW_AT_call_parameter offset is not in CU for "
13325 "DW_TAG_call_site child DIE %s [in module %s]"),
13326 sect_offset_str (child_die->sect_off),
13327 objfile_name (objfile));
13328 continue;
13329 }
13330 parameter->u.param_cu_off
13331 = (cu_offset) (sect_off - cu->header.sect_off);
13332 }
13333 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13334 {
13335 complaint (_("No DW_FORM_block* DW_AT_location for "
13336 "DW_TAG_call_site child DIE %s [in module %s]"),
13337 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13338 continue;
13339 }
13340 else
13341 {
13342 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13343 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13344 if (parameter->u.dwarf_reg != -1)
13345 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13346 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13347 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13348 &parameter->u.fb_offset))
13349 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13350 else
13351 {
13352 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13353 "for DW_FORM_block* DW_AT_location is supported for "
13354 "DW_TAG_call_site child DIE %s "
13355 "[in module %s]"),
13356 sect_offset_str (child_die->sect_off),
13357 objfile_name (objfile));
13358 continue;
13359 }
13360 }
13361
13362 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13363 if (attr == NULL)
13364 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13365 if (attr == NULL || !attr->form_is_block ())
13366 {
13367 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13368 "DW_TAG_call_site child DIE %s [in module %s]"),
13369 sect_offset_str (child_die->sect_off),
13370 objfile_name (objfile));
13371 continue;
13372 }
13373 parameter->value = DW_BLOCK (attr)->data;
13374 parameter->value_size = DW_BLOCK (attr)->size;
13375
13376 /* Parameters are not pre-cleared by memset above. */
13377 parameter->data_value = NULL;
13378 parameter->data_value_size = 0;
13379 call_site->parameter_count++;
13380
13381 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13382 if (attr == NULL)
13383 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13384 if (attr != nullptr)
13385 {
13386 if (!attr->form_is_block ())
13387 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13388 "DW_TAG_call_site child DIE %s [in module %s]"),
13389 sect_offset_str (child_die->sect_off),
13390 objfile_name (objfile));
13391 else
13392 {
13393 parameter->data_value = DW_BLOCK (attr)->data;
13394 parameter->data_value_size = DW_BLOCK (attr)->size;
13395 }
13396 }
13397 }
13398 }
13399
13400 /* Helper function for read_variable. If DIE represents a virtual
13401 table, then return the type of the concrete object that is
13402 associated with the virtual table. Otherwise, return NULL. */
13403
13404 static struct type *
13405 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13406 {
13407 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13408 if (attr == NULL)
13409 return NULL;
13410
13411 /* Find the type DIE. */
13412 struct die_info *type_die = NULL;
13413 struct dwarf2_cu *type_cu = cu;
13414
13415 if (attr->form_is_ref ())
13416 type_die = follow_die_ref (die, attr, &type_cu);
13417 if (type_die == NULL)
13418 return NULL;
13419
13420 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13421 return NULL;
13422 return die_containing_type (type_die, type_cu);
13423 }
13424
13425 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13426
13427 static void
13428 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13429 {
13430 struct rust_vtable_symbol *storage = NULL;
13431
13432 if (cu->language == language_rust)
13433 {
13434 struct type *containing_type = rust_containing_type (die, cu);
13435
13436 if (containing_type != NULL)
13437 {
13438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13439
13440 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13441 initialize_objfile_symbol (storage);
13442 storage->concrete_type = containing_type;
13443 storage->subclass = SYMBOL_RUST_VTABLE;
13444 }
13445 }
13446
13447 struct symbol *res = new_symbol (die, NULL, cu, storage);
13448 struct attribute *abstract_origin
13449 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13450 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13451 if (res == NULL && loc && abstract_origin)
13452 {
13453 /* We have a variable without a name, but with a location and an abstract
13454 origin. This may be a concrete instance of an abstract variable
13455 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13456 later. */
13457 struct dwarf2_cu *origin_cu = cu;
13458 struct die_info *origin_die
13459 = follow_die_ref (die, abstract_origin, &origin_cu);
13460 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13461 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13462 }
13463 }
13464
13465 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13466 reading .debug_rnglists.
13467 Callback's type should be:
13468 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13469 Return true if the attributes are present and valid, otherwise,
13470 return false. */
13471
13472 template <typename Callback>
13473 static bool
13474 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13475 Callback &&callback)
13476 {
13477 struct dwarf2_per_objfile *dwarf2_per_objfile
13478 = cu->per_cu->dwarf2_per_objfile;
13479 struct objfile *objfile = dwarf2_per_objfile->objfile;
13480 bfd *obfd = objfile->obfd;
13481 /* Base address selection entry. */
13482 CORE_ADDR base;
13483 int found_base;
13484 const gdb_byte *buffer;
13485 CORE_ADDR baseaddr;
13486 bool overflow = false;
13487
13488 found_base = cu->base_known;
13489 base = cu->base_address;
13490
13491 dwarf2_per_objfile->rnglists.read (objfile);
13492 if (offset >= dwarf2_per_objfile->rnglists.size)
13493 {
13494 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13495 offset);
13496 return false;
13497 }
13498 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13499
13500 baseaddr = objfile->text_section_offset ();
13501
13502 while (1)
13503 {
13504 /* Initialize it due to a false compiler warning. */
13505 CORE_ADDR range_beginning = 0, range_end = 0;
13506 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13507 + dwarf2_per_objfile->rnglists.size);
13508 unsigned int bytes_read;
13509
13510 if (buffer == buf_end)
13511 {
13512 overflow = true;
13513 break;
13514 }
13515 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13516 switch (rlet)
13517 {
13518 case DW_RLE_end_of_list:
13519 break;
13520 case DW_RLE_base_address:
13521 if (buffer + cu->header.addr_size > buf_end)
13522 {
13523 overflow = true;
13524 break;
13525 }
13526 base = cu->header.read_address (obfd, buffer, &bytes_read);
13527 found_base = 1;
13528 buffer += bytes_read;
13529 break;
13530 case DW_RLE_start_length:
13531 if (buffer + cu->header.addr_size > buf_end)
13532 {
13533 overflow = true;
13534 break;
13535 }
13536 range_beginning = cu->header.read_address (obfd, buffer,
13537 &bytes_read);
13538 buffer += bytes_read;
13539 range_end = (range_beginning
13540 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13541 buffer += bytes_read;
13542 if (buffer > buf_end)
13543 {
13544 overflow = true;
13545 break;
13546 }
13547 break;
13548 case DW_RLE_offset_pair:
13549 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13550 buffer += bytes_read;
13551 if (buffer > buf_end)
13552 {
13553 overflow = true;
13554 break;
13555 }
13556 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13557 buffer += bytes_read;
13558 if (buffer > buf_end)
13559 {
13560 overflow = true;
13561 break;
13562 }
13563 break;
13564 case DW_RLE_start_end:
13565 if (buffer + 2 * cu->header.addr_size > buf_end)
13566 {
13567 overflow = true;
13568 break;
13569 }
13570 range_beginning = cu->header.read_address (obfd, buffer,
13571 &bytes_read);
13572 buffer += bytes_read;
13573 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13574 buffer += bytes_read;
13575 break;
13576 default:
13577 complaint (_("Invalid .debug_rnglists data (no base address)"));
13578 return false;
13579 }
13580 if (rlet == DW_RLE_end_of_list || overflow)
13581 break;
13582 if (rlet == DW_RLE_base_address)
13583 continue;
13584
13585 if (!found_base)
13586 {
13587 /* We have no valid base address for the ranges
13588 data. */
13589 complaint (_("Invalid .debug_rnglists data (no base address)"));
13590 return false;
13591 }
13592
13593 if (range_beginning > range_end)
13594 {
13595 /* Inverted range entries are invalid. */
13596 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13597 return false;
13598 }
13599
13600 /* Empty range entries have no effect. */
13601 if (range_beginning == range_end)
13602 continue;
13603
13604 range_beginning += base;
13605 range_end += base;
13606
13607 /* A not-uncommon case of bad debug info.
13608 Don't pollute the addrmap with bad data. */
13609 if (range_beginning + baseaddr == 0
13610 && !dwarf2_per_objfile->has_section_at_zero)
13611 {
13612 complaint (_(".debug_rnglists entry has start address of zero"
13613 " [in module %s]"), objfile_name (objfile));
13614 continue;
13615 }
13616
13617 callback (range_beginning, range_end);
13618 }
13619
13620 if (overflow)
13621 {
13622 complaint (_("Offset %d is not terminated "
13623 "for DW_AT_ranges attribute"),
13624 offset);
13625 return false;
13626 }
13627
13628 return true;
13629 }
13630
13631 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13632 Callback's type should be:
13633 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13634 Return 1 if the attributes are present and valid, otherwise, return 0. */
13635
13636 template <typename Callback>
13637 static int
13638 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13639 Callback &&callback)
13640 {
13641 struct dwarf2_per_objfile *dwarf2_per_objfile
13642 = cu->per_cu->dwarf2_per_objfile;
13643 struct objfile *objfile = dwarf2_per_objfile->objfile;
13644 struct comp_unit_head *cu_header = &cu->header;
13645 bfd *obfd = objfile->obfd;
13646 unsigned int addr_size = cu_header->addr_size;
13647 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13648 /* Base address selection entry. */
13649 CORE_ADDR base;
13650 int found_base;
13651 unsigned int dummy;
13652 const gdb_byte *buffer;
13653 CORE_ADDR baseaddr;
13654
13655 if (cu_header->version >= 5)
13656 return dwarf2_rnglists_process (offset, cu, callback);
13657
13658 found_base = cu->base_known;
13659 base = cu->base_address;
13660
13661 dwarf2_per_objfile->ranges.read (objfile);
13662 if (offset >= dwarf2_per_objfile->ranges.size)
13663 {
13664 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13665 offset);
13666 return 0;
13667 }
13668 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13669
13670 baseaddr = objfile->text_section_offset ();
13671
13672 while (1)
13673 {
13674 CORE_ADDR range_beginning, range_end;
13675
13676 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13677 buffer += addr_size;
13678 range_end = cu->header.read_address (obfd, buffer, &dummy);
13679 buffer += addr_size;
13680 offset += 2 * addr_size;
13681
13682 /* An end of list marker is a pair of zero addresses. */
13683 if (range_beginning == 0 && range_end == 0)
13684 /* Found the end of list entry. */
13685 break;
13686
13687 /* Each base address selection entry is a pair of 2 values.
13688 The first is the largest possible address, the second is
13689 the base address. Check for a base address here. */
13690 if ((range_beginning & mask) == mask)
13691 {
13692 /* If we found the largest possible address, then we already
13693 have the base address in range_end. */
13694 base = range_end;
13695 found_base = 1;
13696 continue;
13697 }
13698
13699 if (!found_base)
13700 {
13701 /* We have no valid base address for the ranges
13702 data. */
13703 complaint (_("Invalid .debug_ranges data (no base address)"));
13704 return 0;
13705 }
13706
13707 if (range_beginning > range_end)
13708 {
13709 /* Inverted range entries are invalid. */
13710 complaint (_("Invalid .debug_ranges data (inverted range)"));
13711 return 0;
13712 }
13713
13714 /* Empty range entries have no effect. */
13715 if (range_beginning == range_end)
13716 continue;
13717
13718 range_beginning += base;
13719 range_end += base;
13720
13721 /* A not-uncommon case of bad debug info.
13722 Don't pollute the addrmap with bad data. */
13723 if (range_beginning + baseaddr == 0
13724 && !dwarf2_per_objfile->has_section_at_zero)
13725 {
13726 complaint (_(".debug_ranges entry has start address of zero"
13727 " [in module %s]"), objfile_name (objfile));
13728 continue;
13729 }
13730
13731 callback (range_beginning, range_end);
13732 }
13733
13734 return 1;
13735 }
13736
13737 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13738 Return 1 if the attributes are present and valid, otherwise, return 0.
13739 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13740
13741 static int
13742 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13743 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13744 dwarf2_psymtab *ranges_pst)
13745 {
13746 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13747 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13748 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13749 int low_set = 0;
13750 CORE_ADDR low = 0;
13751 CORE_ADDR high = 0;
13752 int retval;
13753
13754 retval = dwarf2_ranges_process (offset, cu,
13755 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13756 {
13757 if (ranges_pst != NULL)
13758 {
13759 CORE_ADDR lowpc;
13760 CORE_ADDR highpc;
13761
13762 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13763 range_beginning + baseaddr)
13764 - baseaddr);
13765 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13766 range_end + baseaddr)
13767 - baseaddr);
13768 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13769 lowpc, highpc - 1, ranges_pst);
13770 }
13771
13772 /* FIXME: This is recording everything as a low-high
13773 segment of consecutive addresses. We should have a
13774 data structure for discontiguous block ranges
13775 instead. */
13776 if (! low_set)
13777 {
13778 low = range_beginning;
13779 high = range_end;
13780 low_set = 1;
13781 }
13782 else
13783 {
13784 if (range_beginning < low)
13785 low = range_beginning;
13786 if (range_end > high)
13787 high = range_end;
13788 }
13789 });
13790 if (!retval)
13791 return 0;
13792
13793 if (! low_set)
13794 /* If the first entry is an end-of-list marker, the range
13795 describes an empty scope, i.e. no instructions. */
13796 return 0;
13797
13798 if (low_return)
13799 *low_return = low;
13800 if (high_return)
13801 *high_return = high;
13802 return 1;
13803 }
13804
13805 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13806 definition for the return value. *LOWPC and *HIGHPC are set iff
13807 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13808
13809 static enum pc_bounds_kind
13810 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13811 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13812 dwarf2_psymtab *pst)
13813 {
13814 struct dwarf2_per_objfile *dwarf2_per_objfile
13815 = cu->per_cu->dwarf2_per_objfile;
13816 struct attribute *attr;
13817 struct attribute *attr_high;
13818 CORE_ADDR low = 0;
13819 CORE_ADDR high = 0;
13820 enum pc_bounds_kind ret;
13821
13822 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13823 if (attr_high)
13824 {
13825 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13826 if (attr != nullptr)
13827 {
13828 low = attr->value_as_address ();
13829 high = attr_high->value_as_address ();
13830 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13831 high += low;
13832 }
13833 else
13834 /* Found high w/o low attribute. */
13835 return PC_BOUNDS_INVALID;
13836
13837 /* Found consecutive range of addresses. */
13838 ret = PC_BOUNDS_HIGH_LOW;
13839 }
13840 else
13841 {
13842 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13843 if (attr != NULL)
13844 {
13845 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13846 We take advantage of the fact that DW_AT_ranges does not appear
13847 in DW_TAG_compile_unit of DWO files. */
13848 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13849 unsigned int ranges_offset = (DW_UNSND (attr)
13850 + (need_ranges_base
13851 ? cu->ranges_base
13852 : 0));
13853
13854 /* Value of the DW_AT_ranges attribute is the offset in the
13855 .debug_ranges section. */
13856 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13857 return PC_BOUNDS_INVALID;
13858 /* Found discontinuous range of addresses. */
13859 ret = PC_BOUNDS_RANGES;
13860 }
13861 else
13862 return PC_BOUNDS_NOT_PRESENT;
13863 }
13864
13865 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13866 if (high <= low)
13867 return PC_BOUNDS_INVALID;
13868
13869 /* When using the GNU linker, .gnu.linkonce. sections are used to
13870 eliminate duplicate copies of functions and vtables and such.
13871 The linker will arbitrarily choose one and discard the others.
13872 The AT_*_pc values for such functions refer to local labels in
13873 these sections. If the section from that file was discarded, the
13874 labels are not in the output, so the relocs get a value of 0.
13875 If this is a discarded function, mark the pc bounds as invalid,
13876 so that GDB will ignore it. */
13877 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13878 return PC_BOUNDS_INVALID;
13879
13880 *lowpc = low;
13881 if (highpc)
13882 *highpc = high;
13883 return ret;
13884 }
13885
13886 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13887 its low and high PC addresses. Do nothing if these addresses could not
13888 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13889 and HIGHPC to the high address if greater than HIGHPC. */
13890
13891 static void
13892 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13893 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13894 struct dwarf2_cu *cu)
13895 {
13896 CORE_ADDR low, high;
13897 struct die_info *child = die->child;
13898
13899 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13900 {
13901 *lowpc = std::min (*lowpc, low);
13902 *highpc = std::max (*highpc, high);
13903 }
13904
13905 /* If the language does not allow nested subprograms (either inside
13906 subprograms or lexical blocks), we're done. */
13907 if (cu->language != language_ada)
13908 return;
13909
13910 /* Check all the children of the given DIE. If it contains nested
13911 subprograms, then check their pc bounds. Likewise, we need to
13912 check lexical blocks as well, as they may also contain subprogram
13913 definitions. */
13914 while (child && child->tag)
13915 {
13916 if (child->tag == DW_TAG_subprogram
13917 || child->tag == DW_TAG_lexical_block)
13918 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13919 child = sibling_die (child);
13920 }
13921 }
13922
13923 /* Get the low and high pc's represented by the scope DIE, and store
13924 them in *LOWPC and *HIGHPC. If the correct values can't be
13925 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13926
13927 static void
13928 get_scope_pc_bounds (struct die_info *die,
13929 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13930 struct dwarf2_cu *cu)
13931 {
13932 CORE_ADDR best_low = (CORE_ADDR) -1;
13933 CORE_ADDR best_high = (CORE_ADDR) 0;
13934 CORE_ADDR current_low, current_high;
13935
13936 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13937 >= PC_BOUNDS_RANGES)
13938 {
13939 best_low = current_low;
13940 best_high = current_high;
13941 }
13942 else
13943 {
13944 struct die_info *child = die->child;
13945
13946 while (child && child->tag)
13947 {
13948 switch (child->tag) {
13949 case DW_TAG_subprogram:
13950 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13951 break;
13952 case DW_TAG_namespace:
13953 case DW_TAG_module:
13954 /* FIXME: carlton/2004-01-16: Should we do this for
13955 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13956 that current GCC's always emit the DIEs corresponding
13957 to definitions of methods of classes as children of a
13958 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13959 the DIEs giving the declarations, which could be
13960 anywhere). But I don't see any reason why the
13961 standards says that they have to be there. */
13962 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13963
13964 if (current_low != ((CORE_ADDR) -1))
13965 {
13966 best_low = std::min (best_low, current_low);
13967 best_high = std::max (best_high, current_high);
13968 }
13969 break;
13970 default:
13971 /* Ignore. */
13972 break;
13973 }
13974
13975 child = sibling_die (child);
13976 }
13977 }
13978
13979 *lowpc = best_low;
13980 *highpc = best_high;
13981 }
13982
13983 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13984 in DIE. */
13985
13986 static void
13987 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13988 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13989 {
13990 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13991 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13992 struct attribute *attr;
13993 struct attribute *attr_high;
13994
13995 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13996 if (attr_high)
13997 {
13998 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13999 if (attr != nullptr)
14000 {
14001 CORE_ADDR low = attr->value_as_address ();
14002 CORE_ADDR high = attr_high->value_as_address ();
14003
14004 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14005 high += low;
14006
14007 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14008 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14009 cu->get_builder ()->record_block_range (block, low, high - 1);
14010 }
14011 }
14012
14013 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14014 if (attr != nullptr)
14015 {
14016 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14017 We take advantage of the fact that DW_AT_ranges does not appear
14018 in DW_TAG_compile_unit of DWO files. */
14019 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14020
14021 /* The value of the DW_AT_ranges attribute is the offset of the
14022 address range list in the .debug_ranges section. */
14023 unsigned long offset = (DW_UNSND (attr)
14024 + (need_ranges_base ? cu->ranges_base : 0));
14025
14026 std::vector<blockrange> blockvec;
14027 dwarf2_ranges_process (offset, cu,
14028 [&] (CORE_ADDR start, CORE_ADDR end)
14029 {
14030 start += baseaddr;
14031 end += baseaddr;
14032 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14033 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14034 cu->get_builder ()->record_block_range (block, start, end - 1);
14035 blockvec.emplace_back (start, end);
14036 });
14037
14038 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14039 }
14040 }
14041
14042 /* Check whether the producer field indicates either of GCC < 4.6, or the
14043 Intel C/C++ compiler, and cache the result in CU. */
14044
14045 static void
14046 check_producer (struct dwarf2_cu *cu)
14047 {
14048 int major, minor;
14049
14050 if (cu->producer == NULL)
14051 {
14052 /* For unknown compilers expect their behavior is DWARF version
14053 compliant.
14054
14055 GCC started to support .debug_types sections by -gdwarf-4 since
14056 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14057 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14058 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14059 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14060 }
14061 else if (producer_is_gcc (cu->producer, &major, &minor))
14062 {
14063 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14064 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14065 }
14066 else if (producer_is_icc (cu->producer, &major, &minor))
14067 {
14068 cu->producer_is_icc = true;
14069 cu->producer_is_icc_lt_14 = major < 14;
14070 }
14071 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14072 cu->producer_is_codewarrior = true;
14073 else
14074 {
14075 /* For other non-GCC compilers, expect their behavior is DWARF version
14076 compliant. */
14077 }
14078
14079 cu->checked_producer = true;
14080 }
14081
14082 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14083 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14084 during 4.6.0 experimental. */
14085
14086 static bool
14087 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14088 {
14089 if (!cu->checked_producer)
14090 check_producer (cu);
14091
14092 return cu->producer_is_gxx_lt_4_6;
14093 }
14094
14095
14096 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14097 with incorrect is_stmt attributes. */
14098
14099 static bool
14100 producer_is_codewarrior (struct dwarf2_cu *cu)
14101 {
14102 if (!cu->checked_producer)
14103 check_producer (cu);
14104
14105 return cu->producer_is_codewarrior;
14106 }
14107
14108 /* Return the default accessibility type if it is not overridden by
14109 DW_AT_accessibility. */
14110
14111 static enum dwarf_access_attribute
14112 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14113 {
14114 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14115 {
14116 /* The default DWARF 2 accessibility for members is public, the default
14117 accessibility for inheritance is private. */
14118
14119 if (die->tag != DW_TAG_inheritance)
14120 return DW_ACCESS_public;
14121 else
14122 return DW_ACCESS_private;
14123 }
14124 else
14125 {
14126 /* DWARF 3+ defines the default accessibility a different way. The same
14127 rules apply now for DW_TAG_inheritance as for the members and it only
14128 depends on the container kind. */
14129
14130 if (die->parent->tag == DW_TAG_class_type)
14131 return DW_ACCESS_private;
14132 else
14133 return DW_ACCESS_public;
14134 }
14135 }
14136
14137 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14138 offset. If the attribute was not found return 0, otherwise return
14139 1. If it was found but could not properly be handled, set *OFFSET
14140 to 0. */
14141
14142 static int
14143 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14144 LONGEST *offset)
14145 {
14146 struct attribute *attr;
14147
14148 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14149 if (attr != NULL)
14150 {
14151 *offset = 0;
14152
14153 /* Note that we do not check for a section offset first here.
14154 This is because DW_AT_data_member_location is new in DWARF 4,
14155 so if we see it, we can assume that a constant form is really
14156 a constant and not a section offset. */
14157 if (attr->form_is_constant ())
14158 *offset = dwarf2_get_attr_constant_value (attr, 0);
14159 else if (attr->form_is_section_offset ())
14160 dwarf2_complex_location_expr_complaint ();
14161 else if (attr->form_is_block ())
14162 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14163 else
14164 dwarf2_complex_location_expr_complaint ();
14165
14166 return 1;
14167 }
14168
14169 return 0;
14170 }
14171
14172 /* Add an aggregate field to the field list. */
14173
14174 static void
14175 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14176 struct dwarf2_cu *cu)
14177 {
14178 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14179 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14180 struct nextfield *new_field;
14181 struct attribute *attr;
14182 struct field *fp;
14183 const char *fieldname = "";
14184
14185 if (die->tag == DW_TAG_inheritance)
14186 {
14187 fip->baseclasses.emplace_back ();
14188 new_field = &fip->baseclasses.back ();
14189 }
14190 else
14191 {
14192 fip->fields.emplace_back ();
14193 new_field = &fip->fields.back ();
14194 }
14195
14196 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14197 if (attr != nullptr)
14198 new_field->accessibility = DW_UNSND (attr);
14199 else
14200 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14201 if (new_field->accessibility != DW_ACCESS_public)
14202 fip->non_public_fields = 1;
14203
14204 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14205 if (attr != nullptr)
14206 new_field->virtuality = DW_UNSND (attr);
14207 else
14208 new_field->virtuality = DW_VIRTUALITY_none;
14209
14210 fp = &new_field->field;
14211
14212 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14213 {
14214 LONGEST offset;
14215
14216 /* Data member other than a C++ static data member. */
14217
14218 /* Get type of field. */
14219 fp->type = die_type (die, cu);
14220
14221 SET_FIELD_BITPOS (*fp, 0);
14222
14223 /* Get bit size of field (zero if none). */
14224 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14225 if (attr != nullptr)
14226 {
14227 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14228 }
14229 else
14230 {
14231 FIELD_BITSIZE (*fp) = 0;
14232 }
14233
14234 /* Get bit offset of field. */
14235 if (handle_data_member_location (die, cu, &offset))
14236 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14237 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14238 if (attr != nullptr)
14239 {
14240 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14241 {
14242 /* For big endian bits, the DW_AT_bit_offset gives the
14243 additional bit offset from the MSB of the containing
14244 anonymous object to the MSB of the field. We don't
14245 have to do anything special since we don't need to
14246 know the size of the anonymous object. */
14247 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14248 }
14249 else
14250 {
14251 /* For little endian bits, compute the bit offset to the
14252 MSB of the anonymous object, subtract off the number of
14253 bits from the MSB of the field to the MSB of the
14254 object, and then subtract off the number of bits of
14255 the field itself. The result is the bit offset of
14256 the LSB of the field. */
14257 int anonymous_size;
14258 int bit_offset = DW_UNSND (attr);
14259
14260 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14261 if (attr != nullptr)
14262 {
14263 /* The size of the anonymous object containing
14264 the bit field is explicit, so use the
14265 indicated size (in bytes). */
14266 anonymous_size = DW_UNSND (attr);
14267 }
14268 else
14269 {
14270 /* The size of the anonymous object containing
14271 the bit field must be inferred from the type
14272 attribute of the data member containing the
14273 bit field. */
14274 anonymous_size = TYPE_LENGTH (fp->type);
14275 }
14276 SET_FIELD_BITPOS (*fp,
14277 (FIELD_BITPOS (*fp)
14278 + anonymous_size * bits_per_byte
14279 - bit_offset - FIELD_BITSIZE (*fp)));
14280 }
14281 }
14282 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14283 if (attr != NULL)
14284 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14285 + dwarf2_get_attr_constant_value (attr, 0)));
14286
14287 /* Get name of field. */
14288 fieldname = dwarf2_name (die, cu);
14289 if (fieldname == NULL)
14290 fieldname = "";
14291
14292 /* The name is already allocated along with this objfile, so we don't
14293 need to duplicate it for the type. */
14294 fp->name = fieldname;
14295
14296 /* Change accessibility for artificial fields (e.g. virtual table
14297 pointer or virtual base class pointer) to private. */
14298 if (dwarf2_attr (die, DW_AT_artificial, cu))
14299 {
14300 FIELD_ARTIFICIAL (*fp) = 1;
14301 new_field->accessibility = DW_ACCESS_private;
14302 fip->non_public_fields = 1;
14303 }
14304 }
14305 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14306 {
14307 /* C++ static member. */
14308
14309 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14310 is a declaration, but all versions of G++ as of this writing
14311 (so through at least 3.2.1) incorrectly generate
14312 DW_TAG_variable tags. */
14313
14314 const char *physname;
14315
14316 /* Get name of field. */
14317 fieldname = dwarf2_name (die, cu);
14318 if (fieldname == NULL)
14319 return;
14320
14321 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14322 if (attr
14323 /* Only create a symbol if this is an external value.
14324 new_symbol checks this and puts the value in the global symbol
14325 table, which we want. If it is not external, new_symbol
14326 will try to put the value in cu->list_in_scope which is wrong. */
14327 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14328 {
14329 /* A static const member, not much different than an enum as far as
14330 we're concerned, except that we can support more types. */
14331 new_symbol (die, NULL, cu);
14332 }
14333
14334 /* Get physical name. */
14335 physname = dwarf2_physname (fieldname, die, cu);
14336
14337 /* The name is already allocated along with this objfile, so we don't
14338 need to duplicate it for the type. */
14339 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14340 FIELD_TYPE (*fp) = die_type (die, cu);
14341 FIELD_NAME (*fp) = fieldname;
14342 }
14343 else if (die->tag == DW_TAG_inheritance)
14344 {
14345 LONGEST offset;
14346
14347 /* C++ base class field. */
14348 if (handle_data_member_location (die, cu, &offset))
14349 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14350 FIELD_BITSIZE (*fp) = 0;
14351 FIELD_TYPE (*fp) = die_type (die, cu);
14352 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14353 }
14354 else if (die->tag == DW_TAG_variant_part)
14355 {
14356 /* process_structure_scope will treat this DIE as a union. */
14357 process_structure_scope (die, cu);
14358
14359 /* The variant part is relative to the start of the enclosing
14360 structure. */
14361 SET_FIELD_BITPOS (*fp, 0);
14362 fp->type = get_die_type (die, cu);
14363 fp->artificial = 1;
14364 fp->name = "<<variant>>";
14365
14366 /* Normally a DW_TAG_variant_part won't have a size, but our
14367 representation requires one, so set it to the maximum of the
14368 child sizes, being sure to account for the offset at which
14369 each child is seen. */
14370 if (TYPE_LENGTH (fp->type) == 0)
14371 {
14372 unsigned max = 0;
14373 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14374 {
14375 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14376 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14377 if (len > max)
14378 max = len;
14379 }
14380 TYPE_LENGTH (fp->type) = max;
14381 }
14382 }
14383 else
14384 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14385 }
14386
14387 /* Can the type given by DIE define another type? */
14388
14389 static bool
14390 type_can_define_types (const struct die_info *die)
14391 {
14392 switch (die->tag)
14393 {
14394 case DW_TAG_typedef:
14395 case DW_TAG_class_type:
14396 case DW_TAG_structure_type:
14397 case DW_TAG_union_type:
14398 case DW_TAG_enumeration_type:
14399 return true;
14400
14401 default:
14402 return false;
14403 }
14404 }
14405
14406 /* Add a type definition defined in the scope of the FIP's class. */
14407
14408 static void
14409 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14410 struct dwarf2_cu *cu)
14411 {
14412 struct decl_field fp;
14413 memset (&fp, 0, sizeof (fp));
14414
14415 gdb_assert (type_can_define_types (die));
14416
14417 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14418 fp.name = dwarf2_name (die, cu);
14419 fp.type = read_type_die (die, cu);
14420
14421 /* Save accessibility. */
14422 enum dwarf_access_attribute accessibility;
14423 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14424 if (attr != NULL)
14425 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14426 else
14427 accessibility = dwarf2_default_access_attribute (die, cu);
14428 switch (accessibility)
14429 {
14430 case DW_ACCESS_public:
14431 /* The assumed value if neither private nor protected. */
14432 break;
14433 case DW_ACCESS_private:
14434 fp.is_private = 1;
14435 break;
14436 case DW_ACCESS_protected:
14437 fp.is_protected = 1;
14438 break;
14439 default:
14440 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14441 }
14442
14443 if (die->tag == DW_TAG_typedef)
14444 fip->typedef_field_list.push_back (fp);
14445 else
14446 fip->nested_types_list.push_back (fp);
14447 }
14448
14449 /* Create the vector of fields, and attach it to the type. */
14450
14451 static void
14452 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14453 struct dwarf2_cu *cu)
14454 {
14455 int nfields = fip->nfields ();
14456
14457 /* Record the field count, allocate space for the array of fields,
14458 and create blank accessibility bitfields if necessary. */
14459 TYPE_NFIELDS (type) = nfields;
14460 TYPE_FIELDS (type) = (struct field *)
14461 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14462
14463 if (fip->non_public_fields && cu->language != language_ada)
14464 {
14465 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14466
14467 TYPE_FIELD_PRIVATE_BITS (type) =
14468 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14469 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14470
14471 TYPE_FIELD_PROTECTED_BITS (type) =
14472 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14473 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14474
14475 TYPE_FIELD_IGNORE_BITS (type) =
14476 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14477 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14478 }
14479
14480 /* If the type has baseclasses, allocate and clear a bit vector for
14481 TYPE_FIELD_VIRTUAL_BITS. */
14482 if (!fip->baseclasses.empty () && cu->language != language_ada)
14483 {
14484 int num_bytes = B_BYTES (fip->baseclasses.size ());
14485 unsigned char *pointer;
14486
14487 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14488 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14489 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14490 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14491 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14492 }
14493
14494 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14495 {
14496 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14497
14498 for (int index = 0; index < nfields; ++index)
14499 {
14500 struct nextfield &field = fip->fields[index];
14501
14502 if (field.variant.is_discriminant)
14503 di->discriminant_index = index;
14504 else if (field.variant.default_branch)
14505 di->default_index = index;
14506 else
14507 di->discriminants[index] = field.variant.discriminant_value;
14508 }
14509 }
14510
14511 /* Copy the saved-up fields into the field vector. */
14512 for (int i = 0; i < nfields; ++i)
14513 {
14514 struct nextfield &field
14515 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14516 : fip->fields[i - fip->baseclasses.size ()]);
14517
14518 TYPE_FIELD (type, i) = field.field;
14519 switch (field.accessibility)
14520 {
14521 case DW_ACCESS_private:
14522 if (cu->language != language_ada)
14523 SET_TYPE_FIELD_PRIVATE (type, i);
14524 break;
14525
14526 case DW_ACCESS_protected:
14527 if (cu->language != language_ada)
14528 SET_TYPE_FIELD_PROTECTED (type, i);
14529 break;
14530
14531 case DW_ACCESS_public:
14532 break;
14533
14534 default:
14535 /* Unknown accessibility. Complain and treat it as public. */
14536 {
14537 complaint (_("unsupported accessibility %d"),
14538 field.accessibility);
14539 }
14540 break;
14541 }
14542 if (i < fip->baseclasses.size ())
14543 {
14544 switch (field.virtuality)
14545 {
14546 case DW_VIRTUALITY_virtual:
14547 case DW_VIRTUALITY_pure_virtual:
14548 if (cu->language == language_ada)
14549 error (_("unexpected virtuality in component of Ada type"));
14550 SET_TYPE_FIELD_VIRTUAL (type, i);
14551 break;
14552 }
14553 }
14554 }
14555 }
14556
14557 /* Return true if this member function is a constructor, false
14558 otherwise. */
14559
14560 static int
14561 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14562 {
14563 const char *fieldname;
14564 const char *type_name;
14565 int len;
14566
14567 if (die->parent == NULL)
14568 return 0;
14569
14570 if (die->parent->tag != DW_TAG_structure_type
14571 && die->parent->tag != DW_TAG_union_type
14572 && die->parent->tag != DW_TAG_class_type)
14573 return 0;
14574
14575 fieldname = dwarf2_name (die, cu);
14576 type_name = dwarf2_name (die->parent, cu);
14577 if (fieldname == NULL || type_name == NULL)
14578 return 0;
14579
14580 len = strlen (fieldname);
14581 return (strncmp (fieldname, type_name, len) == 0
14582 && (type_name[len] == '\0' || type_name[len] == '<'));
14583 }
14584
14585 /* Check if the given VALUE is a recognized enum
14586 dwarf_defaulted_attribute constant according to DWARF5 spec,
14587 Table 7.24. */
14588
14589 static bool
14590 is_valid_DW_AT_defaulted (ULONGEST value)
14591 {
14592 switch (value)
14593 {
14594 case DW_DEFAULTED_no:
14595 case DW_DEFAULTED_in_class:
14596 case DW_DEFAULTED_out_of_class:
14597 return true;
14598 }
14599
14600 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14601 return false;
14602 }
14603
14604 /* Add a member function to the proper fieldlist. */
14605
14606 static void
14607 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14608 struct type *type, struct dwarf2_cu *cu)
14609 {
14610 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14611 struct attribute *attr;
14612 int i;
14613 struct fnfieldlist *flp = nullptr;
14614 struct fn_field *fnp;
14615 const char *fieldname;
14616 struct type *this_type;
14617 enum dwarf_access_attribute accessibility;
14618
14619 if (cu->language == language_ada)
14620 error (_("unexpected member function in Ada type"));
14621
14622 /* Get name of member function. */
14623 fieldname = dwarf2_name (die, cu);
14624 if (fieldname == NULL)
14625 return;
14626
14627 /* Look up member function name in fieldlist. */
14628 for (i = 0; i < fip->fnfieldlists.size (); i++)
14629 {
14630 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14631 {
14632 flp = &fip->fnfieldlists[i];
14633 break;
14634 }
14635 }
14636
14637 /* Create a new fnfieldlist if necessary. */
14638 if (flp == nullptr)
14639 {
14640 fip->fnfieldlists.emplace_back ();
14641 flp = &fip->fnfieldlists.back ();
14642 flp->name = fieldname;
14643 i = fip->fnfieldlists.size () - 1;
14644 }
14645
14646 /* Create a new member function field and add it to the vector of
14647 fnfieldlists. */
14648 flp->fnfields.emplace_back ();
14649 fnp = &flp->fnfields.back ();
14650
14651 /* Delay processing of the physname until later. */
14652 if (cu->language == language_cplus)
14653 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14654 die, cu);
14655 else
14656 {
14657 const char *physname = dwarf2_physname (fieldname, die, cu);
14658 fnp->physname = physname ? physname : "";
14659 }
14660
14661 fnp->type = alloc_type (objfile);
14662 this_type = read_type_die (die, cu);
14663 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14664 {
14665 int nparams = TYPE_NFIELDS (this_type);
14666
14667 /* TYPE is the domain of this method, and THIS_TYPE is the type
14668 of the method itself (TYPE_CODE_METHOD). */
14669 smash_to_method_type (fnp->type, type,
14670 TYPE_TARGET_TYPE (this_type),
14671 TYPE_FIELDS (this_type),
14672 TYPE_NFIELDS (this_type),
14673 TYPE_VARARGS (this_type));
14674
14675 /* Handle static member functions.
14676 Dwarf2 has no clean way to discern C++ static and non-static
14677 member functions. G++ helps GDB by marking the first
14678 parameter for non-static member functions (which is the this
14679 pointer) as artificial. We obtain this information from
14680 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14681 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14682 fnp->voffset = VOFFSET_STATIC;
14683 }
14684 else
14685 complaint (_("member function type missing for '%s'"),
14686 dwarf2_full_name (fieldname, die, cu));
14687
14688 /* Get fcontext from DW_AT_containing_type if present. */
14689 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14690 fnp->fcontext = die_containing_type (die, cu);
14691
14692 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14693 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14694
14695 /* Get accessibility. */
14696 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14697 if (attr != nullptr)
14698 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14699 else
14700 accessibility = dwarf2_default_access_attribute (die, cu);
14701 switch (accessibility)
14702 {
14703 case DW_ACCESS_private:
14704 fnp->is_private = 1;
14705 break;
14706 case DW_ACCESS_protected:
14707 fnp->is_protected = 1;
14708 break;
14709 }
14710
14711 /* Check for artificial methods. */
14712 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14713 if (attr && DW_UNSND (attr) != 0)
14714 fnp->is_artificial = 1;
14715
14716 /* Check for defaulted methods. */
14717 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14718 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14719 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14720
14721 /* Check for deleted methods. */
14722 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14723 if (attr != nullptr && DW_UNSND (attr) != 0)
14724 fnp->is_deleted = 1;
14725
14726 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14727
14728 /* Get index in virtual function table if it is a virtual member
14729 function. For older versions of GCC, this is an offset in the
14730 appropriate virtual table, as specified by DW_AT_containing_type.
14731 For everyone else, it is an expression to be evaluated relative
14732 to the object address. */
14733
14734 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14735 if (attr != nullptr)
14736 {
14737 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14738 {
14739 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14740 {
14741 /* Old-style GCC. */
14742 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14743 }
14744 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14745 || (DW_BLOCK (attr)->size > 1
14746 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14747 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14748 {
14749 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14750 if ((fnp->voffset % cu->header.addr_size) != 0)
14751 dwarf2_complex_location_expr_complaint ();
14752 else
14753 fnp->voffset /= cu->header.addr_size;
14754 fnp->voffset += 2;
14755 }
14756 else
14757 dwarf2_complex_location_expr_complaint ();
14758
14759 if (!fnp->fcontext)
14760 {
14761 /* If there is no `this' field and no DW_AT_containing_type,
14762 we cannot actually find a base class context for the
14763 vtable! */
14764 if (TYPE_NFIELDS (this_type) == 0
14765 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14766 {
14767 complaint (_("cannot determine context for virtual member "
14768 "function \"%s\" (offset %s)"),
14769 fieldname, sect_offset_str (die->sect_off));
14770 }
14771 else
14772 {
14773 fnp->fcontext
14774 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14775 }
14776 }
14777 }
14778 else if (attr->form_is_section_offset ())
14779 {
14780 dwarf2_complex_location_expr_complaint ();
14781 }
14782 else
14783 {
14784 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14785 fieldname);
14786 }
14787 }
14788 else
14789 {
14790 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14791 if (attr && DW_UNSND (attr))
14792 {
14793 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14794 complaint (_("Member function \"%s\" (offset %s) is virtual "
14795 "but the vtable offset is not specified"),
14796 fieldname, sect_offset_str (die->sect_off));
14797 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14798 TYPE_CPLUS_DYNAMIC (type) = 1;
14799 }
14800 }
14801 }
14802
14803 /* Create the vector of member function fields, and attach it to the type. */
14804
14805 static void
14806 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14807 struct dwarf2_cu *cu)
14808 {
14809 if (cu->language == language_ada)
14810 error (_("unexpected member functions in Ada type"));
14811
14812 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14813 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14814 TYPE_ALLOC (type,
14815 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14816
14817 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14818 {
14819 struct fnfieldlist &nf = fip->fnfieldlists[i];
14820 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14821
14822 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14823 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14824 fn_flp->fn_fields = (struct fn_field *)
14825 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14826
14827 for (int k = 0; k < nf.fnfields.size (); ++k)
14828 fn_flp->fn_fields[k] = nf.fnfields[k];
14829 }
14830
14831 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14832 }
14833
14834 /* Returns non-zero if NAME is the name of a vtable member in CU's
14835 language, zero otherwise. */
14836 static int
14837 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14838 {
14839 static const char vptr[] = "_vptr";
14840
14841 /* Look for the C++ form of the vtable. */
14842 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14843 return 1;
14844
14845 return 0;
14846 }
14847
14848 /* GCC outputs unnamed structures that are really pointers to member
14849 functions, with the ABI-specified layout. If TYPE describes
14850 such a structure, smash it into a member function type.
14851
14852 GCC shouldn't do this; it should just output pointer to member DIEs.
14853 This is GCC PR debug/28767. */
14854
14855 static void
14856 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14857 {
14858 struct type *pfn_type, *self_type, *new_type;
14859
14860 /* Check for a structure with no name and two children. */
14861 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14862 return;
14863
14864 /* Check for __pfn and __delta members. */
14865 if (TYPE_FIELD_NAME (type, 0) == NULL
14866 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14867 || TYPE_FIELD_NAME (type, 1) == NULL
14868 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14869 return;
14870
14871 /* Find the type of the method. */
14872 pfn_type = TYPE_FIELD_TYPE (type, 0);
14873 if (pfn_type == NULL
14874 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14875 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14876 return;
14877
14878 /* Look for the "this" argument. */
14879 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14880 if (TYPE_NFIELDS (pfn_type) == 0
14881 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14882 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14883 return;
14884
14885 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14886 new_type = alloc_type (objfile);
14887 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14888 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14889 TYPE_VARARGS (pfn_type));
14890 smash_to_methodptr_type (type, new_type);
14891 }
14892
14893 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14894 appropriate error checking and issuing complaints if there is a
14895 problem. */
14896
14897 static ULONGEST
14898 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14899 {
14900 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14901
14902 if (attr == nullptr)
14903 return 0;
14904
14905 if (!attr->form_is_constant ())
14906 {
14907 complaint (_("DW_AT_alignment must have constant form"
14908 " - DIE at %s [in module %s]"),
14909 sect_offset_str (die->sect_off),
14910 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14911 return 0;
14912 }
14913
14914 ULONGEST align;
14915 if (attr->form == DW_FORM_sdata)
14916 {
14917 LONGEST val = DW_SND (attr);
14918 if (val < 0)
14919 {
14920 complaint (_("DW_AT_alignment value must not be negative"
14921 " - DIE at %s [in module %s]"),
14922 sect_offset_str (die->sect_off),
14923 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14924 return 0;
14925 }
14926 align = val;
14927 }
14928 else
14929 align = DW_UNSND (attr);
14930
14931 if (align == 0)
14932 {
14933 complaint (_("DW_AT_alignment value must not be zero"
14934 " - DIE at %s [in module %s]"),
14935 sect_offset_str (die->sect_off),
14936 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14937 return 0;
14938 }
14939 if ((align & (align - 1)) != 0)
14940 {
14941 complaint (_("DW_AT_alignment value must be a power of 2"
14942 " - DIE at %s [in module %s]"),
14943 sect_offset_str (die->sect_off),
14944 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14945 return 0;
14946 }
14947
14948 return align;
14949 }
14950
14951 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14952 the alignment for TYPE. */
14953
14954 static void
14955 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14956 struct type *type)
14957 {
14958 if (!set_type_align (type, get_alignment (cu, die)))
14959 complaint (_("DW_AT_alignment value too large"
14960 " - DIE at %s [in module %s]"),
14961 sect_offset_str (die->sect_off),
14962 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14963 }
14964
14965 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14966 constant for a type, according to DWARF5 spec, Table 5.5. */
14967
14968 static bool
14969 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14970 {
14971 switch (value)
14972 {
14973 case DW_CC_normal:
14974 case DW_CC_pass_by_reference:
14975 case DW_CC_pass_by_value:
14976 return true;
14977
14978 default:
14979 complaint (_("unrecognized DW_AT_calling_convention value "
14980 "(%s) for a type"), pulongest (value));
14981 return false;
14982 }
14983 }
14984
14985 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14986 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14987 also according to GNU-specific values (see include/dwarf2.h). */
14988
14989 static bool
14990 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14991 {
14992 switch (value)
14993 {
14994 case DW_CC_normal:
14995 case DW_CC_program:
14996 case DW_CC_nocall:
14997 return true;
14998
14999 case DW_CC_GNU_renesas_sh:
15000 case DW_CC_GNU_borland_fastcall_i386:
15001 case DW_CC_GDB_IBM_OpenCL:
15002 return true;
15003
15004 default:
15005 complaint (_("unrecognized DW_AT_calling_convention value "
15006 "(%s) for a subroutine"), pulongest (value));
15007 return false;
15008 }
15009 }
15010
15011 /* Called when we find the DIE that starts a structure or union scope
15012 (definition) to create a type for the structure or union. Fill in
15013 the type's name and general properties; the members will not be
15014 processed until process_structure_scope. A symbol table entry for
15015 the type will also not be done until process_structure_scope (assuming
15016 the type has a name).
15017
15018 NOTE: we need to call these functions regardless of whether or not the
15019 DIE has a DW_AT_name attribute, since it might be an anonymous
15020 structure or union. This gets the type entered into our set of
15021 user defined types. */
15022
15023 static struct type *
15024 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15025 {
15026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15027 struct type *type;
15028 struct attribute *attr;
15029 const char *name;
15030
15031 /* If the definition of this type lives in .debug_types, read that type.
15032 Don't follow DW_AT_specification though, that will take us back up
15033 the chain and we want to go down. */
15034 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15035 if (attr != nullptr)
15036 {
15037 type = get_DW_AT_signature_type (die, attr, cu);
15038
15039 /* The type's CU may not be the same as CU.
15040 Ensure TYPE is recorded with CU in die_type_hash. */
15041 return set_die_type (die, type, cu);
15042 }
15043
15044 type = alloc_type (objfile);
15045 INIT_CPLUS_SPECIFIC (type);
15046
15047 name = dwarf2_name (die, cu);
15048 if (name != NULL)
15049 {
15050 if (cu->language == language_cplus
15051 || cu->language == language_d
15052 || cu->language == language_rust)
15053 {
15054 const char *full_name = dwarf2_full_name (name, die, cu);
15055
15056 /* dwarf2_full_name might have already finished building the DIE's
15057 type. If so, there is no need to continue. */
15058 if (get_die_type (die, cu) != NULL)
15059 return get_die_type (die, cu);
15060
15061 TYPE_NAME (type) = full_name;
15062 }
15063 else
15064 {
15065 /* The name is already allocated along with this objfile, so
15066 we don't need to duplicate it for the type. */
15067 TYPE_NAME (type) = name;
15068 }
15069 }
15070
15071 if (die->tag == DW_TAG_structure_type)
15072 {
15073 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15074 }
15075 else if (die->tag == DW_TAG_union_type)
15076 {
15077 TYPE_CODE (type) = TYPE_CODE_UNION;
15078 }
15079 else if (die->tag == DW_TAG_variant_part)
15080 {
15081 TYPE_CODE (type) = TYPE_CODE_UNION;
15082 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15083 }
15084 else
15085 {
15086 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15087 }
15088
15089 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15090 TYPE_DECLARED_CLASS (type) = 1;
15091
15092 /* Store the calling convention in the type if it's available in
15093 the die. Otherwise the calling convention remains set to
15094 the default value DW_CC_normal. */
15095 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15096 if (attr != nullptr
15097 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15098 {
15099 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15100 TYPE_CPLUS_CALLING_CONVENTION (type)
15101 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15102 }
15103
15104 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15105 if (attr != nullptr)
15106 {
15107 if (attr->form_is_constant ())
15108 TYPE_LENGTH (type) = DW_UNSND (attr);
15109 else
15110 {
15111 /* For the moment, dynamic type sizes are not supported
15112 by GDB's struct type. The actual size is determined
15113 on-demand when resolving the type of a given object,
15114 so set the type's length to zero for now. Otherwise,
15115 we record an expression as the length, and that expression
15116 could lead to a very large value, which could eventually
15117 lead to us trying to allocate that much memory when creating
15118 a value of that type. */
15119 TYPE_LENGTH (type) = 0;
15120 }
15121 }
15122 else
15123 {
15124 TYPE_LENGTH (type) = 0;
15125 }
15126
15127 maybe_set_alignment (cu, die, type);
15128
15129 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15130 {
15131 /* ICC<14 does not output the required DW_AT_declaration on
15132 incomplete types, but gives them a size of zero. */
15133 TYPE_STUB (type) = 1;
15134 }
15135 else
15136 TYPE_STUB_SUPPORTED (type) = 1;
15137
15138 if (die_is_declaration (die, cu))
15139 TYPE_STUB (type) = 1;
15140 else if (attr == NULL && die->child == NULL
15141 && producer_is_realview (cu->producer))
15142 /* RealView does not output the required DW_AT_declaration
15143 on incomplete types. */
15144 TYPE_STUB (type) = 1;
15145
15146 /* We need to add the type field to the die immediately so we don't
15147 infinitely recurse when dealing with pointers to the structure
15148 type within the structure itself. */
15149 set_die_type (die, type, cu);
15150
15151 /* set_die_type should be already done. */
15152 set_descriptive_type (type, die, cu);
15153
15154 return type;
15155 }
15156
15157 /* A helper for process_structure_scope that handles a single member
15158 DIE. */
15159
15160 static void
15161 handle_struct_member_die (struct die_info *child_die, struct type *type,
15162 struct field_info *fi,
15163 std::vector<struct symbol *> *template_args,
15164 struct dwarf2_cu *cu)
15165 {
15166 if (child_die->tag == DW_TAG_member
15167 || child_die->tag == DW_TAG_variable
15168 || child_die->tag == DW_TAG_variant_part)
15169 {
15170 /* NOTE: carlton/2002-11-05: A C++ static data member
15171 should be a DW_TAG_member that is a declaration, but
15172 all versions of G++ as of this writing (so through at
15173 least 3.2.1) incorrectly generate DW_TAG_variable
15174 tags for them instead. */
15175 dwarf2_add_field (fi, child_die, cu);
15176 }
15177 else if (child_die->tag == DW_TAG_subprogram)
15178 {
15179 /* Rust doesn't have member functions in the C++ sense.
15180 However, it does emit ordinary functions as children
15181 of a struct DIE. */
15182 if (cu->language == language_rust)
15183 read_func_scope (child_die, cu);
15184 else
15185 {
15186 /* C++ member function. */
15187 dwarf2_add_member_fn (fi, child_die, type, cu);
15188 }
15189 }
15190 else if (child_die->tag == DW_TAG_inheritance)
15191 {
15192 /* C++ base class field. */
15193 dwarf2_add_field (fi, child_die, cu);
15194 }
15195 else if (type_can_define_types (child_die))
15196 dwarf2_add_type_defn (fi, child_die, cu);
15197 else if (child_die->tag == DW_TAG_template_type_param
15198 || child_die->tag == DW_TAG_template_value_param)
15199 {
15200 struct symbol *arg = new_symbol (child_die, NULL, cu);
15201
15202 if (arg != NULL)
15203 template_args->push_back (arg);
15204 }
15205 else if (child_die->tag == DW_TAG_variant)
15206 {
15207 /* In a variant we want to get the discriminant and also add a
15208 field for our sole member child. */
15209 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15210
15211 for (die_info *variant_child = child_die->child;
15212 variant_child != NULL;
15213 variant_child = sibling_die (variant_child))
15214 {
15215 if (variant_child->tag == DW_TAG_member)
15216 {
15217 handle_struct_member_die (variant_child, type, fi,
15218 template_args, cu);
15219 /* Only handle the one. */
15220 break;
15221 }
15222 }
15223
15224 /* We don't handle this but we might as well report it if we see
15225 it. */
15226 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15227 complaint (_("DW_AT_discr_list is not supported yet"
15228 " - DIE at %s [in module %s]"),
15229 sect_offset_str (child_die->sect_off),
15230 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15231
15232 /* The first field was just added, so we can stash the
15233 discriminant there. */
15234 gdb_assert (!fi->fields.empty ());
15235 if (discr == NULL)
15236 fi->fields.back ().variant.default_branch = true;
15237 else
15238 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15239 }
15240 }
15241
15242 /* Finish creating a structure or union type, including filling in
15243 its members and creating a symbol for it. */
15244
15245 static void
15246 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15247 {
15248 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15249 struct die_info *child_die;
15250 struct type *type;
15251
15252 type = get_die_type (die, cu);
15253 if (type == NULL)
15254 type = read_structure_type (die, cu);
15255
15256 /* When reading a DW_TAG_variant_part, we need to notice when we
15257 read the discriminant member, so we can record it later in the
15258 discriminant_info. */
15259 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15260 sect_offset discr_offset {};
15261 bool has_template_parameters = false;
15262
15263 if (is_variant_part)
15264 {
15265 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15266 if (discr == NULL)
15267 {
15268 /* Maybe it's a univariant form, an extension we support.
15269 In this case arrange not to check the offset. */
15270 is_variant_part = false;
15271 }
15272 else if (discr->form_is_ref ())
15273 {
15274 struct dwarf2_cu *target_cu = cu;
15275 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15276
15277 discr_offset = target_die->sect_off;
15278 }
15279 else
15280 {
15281 complaint (_("DW_AT_discr does not have DIE reference form"
15282 " - DIE at %s [in module %s]"),
15283 sect_offset_str (die->sect_off),
15284 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15285 is_variant_part = false;
15286 }
15287 }
15288
15289 if (die->child != NULL && ! die_is_declaration (die, cu))
15290 {
15291 struct field_info fi;
15292 std::vector<struct symbol *> template_args;
15293
15294 child_die = die->child;
15295
15296 while (child_die && child_die->tag)
15297 {
15298 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15299
15300 if (is_variant_part && discr_offset == child_die->sect_off)
15301 fi.fields.back ().variant.is_discriminant = true;
15302
15303 child_die = sibling_die (child_die);
15304 }
15305
15306 /* Attach template arguments to type. */
15307 if (!template_args.empty ())
15308 {
15309 has_template_parameters = true;
15310 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15311 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15312 TYPE_TEMPLATE_ARGUMENTS (type)
15313 = XOBNEWVEC (&objfile->objfile_obstack,
15314 struct symbol *,
15315 TYPE_N_TEMPLATE_ARGUMENTS (type));
15316 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15317 template_args.data (),
15318 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15319 * sizeof (struct symbol *)));
15320 }
15321
15322 /* Attach fields and member functions to the type. */
15323 if (fi.nfields () > 0)
15324 dwarf2_attach_fields_to_type (&fi, type, cu);
15325 if (!fi.fnfieldlists.empty ())
15326 {
15327 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15328
15329 /* Get the type which refers to the base class (possibly this
15330 class itself) which contains the vtable pointer for the current
15331 class from the DW_AT_containing_type attribute. This use of
15332 DW_AT_containing_type is a GNU extension. */
15333
15334 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15335 {
15336 struct type *t = die_containing_type (die, cu);
15337
15338 set_type_vptr_basetype (type, t);
15339 if (type == t)
15340 {
15341 int i;
15342
15343 /* Our own class provides vtbl ptr. */
15344 for (i = TYPE_NFIELDS (t) - 1;
15345 i >= TYPE_N_BASECLASSES (t);
15346 --i)
15347 {
15348 const char *fieldname = TYPE_FIELD_NAME (t, i);
15349
15350 if (is_vtable_name (fieldname, cu))
15351 {
15352 set_type_vptr_fieldno (type, i);
15353 break;
15354 }
15355 }
15356
15357 /* Complain if virtual function table field not found. */
15358 if (i < TYPE_N_BASECLASSES (t))
15359 complaint (_("virtual function table pointer "
15360 "not found when defining class '%s'"),
15361 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15362 }
15363 else
15364 {
15365 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15366 }
15367 }
15368 else if (cu->producer
15369 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15370 {
15371 /* The IBM XLC compiler does not provide direct indication
15372 of the containing type, but the vtable pointer is
15373 always named __vfp. */
15374
15375 int i;
15376
15377 for (i = TYPE_NFIELDS (type) - 1;
15378 i >= TYPE_N_BASECLASSES (type);
15379 --i)
15380 {
15381 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15382 {
15383 set_type_vptr_fieldno (type, i);
15384 set_type_vptr_basetype (type, type);
15385 break;
15386 }
15387 }
15388 }
15389 }
15390
15391 /* Copy fi.typedef_field_list linked list elements content into the
15392 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15393 if (!fi.typedef_field_list.empty ())
15394 {
15395 int count = fi.typedef_field_list.size ();
15396
15397 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15398 TYPE_TYPEDEF_FIELD_ARRAY (type)
15399 = ((struct decl_field *)
15400 TYPE_ALLOC (type,
15401 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15402 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15403
15404 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15405 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15406 }
15407
15408 /* Copy fi.nested_types_list linked list elements content into the
15409 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15410 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15411 {
15412 int count = fi.nested_types_list.size ();
15413
15414 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15415 TYPE_NESTED_TYPES_ARRAY (type)
15416 = ((struct decl_field *)
15417 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15418 TYPE_NESTED_TYPES_COUNT (type) = count;
15419
15420 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15421 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15422 }
15423 }
15424
15425 quirk_gcc_member_function_pointer (type, objfile);
15426 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15427 cu->rust_unions.push_back (type);
15428
15429 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15430 snapshots) has been known to create a die giving a declaration
15431 for a class that has, as a child, a die giving a definition for a
15432 nested class. So we have to process our children even if the
15433 current die is a declaration. Normally, of course, a declaration
15434 won't have any children at all. */
15435
15436 child_die = die->child;
15437
15438 while (child_die != NULL && child_die->tag)
15439 {
15440 if (child_die->tag == DW_TAG_member
15441 || child_die->tag == DW_TAG_variable
15442 || child_die->tag == DW_TAG_inheritance
15443 || child_die->tag == DW_TAG_template_value_param
15444 || child_die->tag == DW_TAG_template_type_param)
15445 {
15446 /* Do nothing. */
15447 }
15448 else
15449 process_die (child_die, cu);
15450
15451 child_die = sibling_die (child_die);
15452 }
15453
15454 /* Do not consider external references. According to the DWARF standard,
15455 these DIEs are identified by the fact that they have no byte_size
15456 attribute, and a declaration attribute. */
15457 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15458 || !die_is_declaration (die, cu))
15459 {
15460 struct symbol *sym = new_symbol (die, type, cu);
15461
15462 if (has_template_parameters)
15463 {
15464 struct symtab *symtab;
15465 if (sym != nullptr)
15466 symtab = symbol_symtab (sym);
15467 else if (cu->line_header != nullptr)
15468 {
15469 /* Any related symtab will do. */
15470 symtab
15471 = cu->line_header->file_names ()[0].symtab;
15472 }
15473 else
15474 {
15475 symtab = nullptr;
15476 complaint (_("could not find suitable "
15477 "symtab for template parameter"
15478 " - DIE at %s [in module %s]"),
15479 sect_offset_str (die->sect_off),
15480 objfile_name (objfile));
15481 }
15482
15483 if (symtab != nullptr)
15484 {
15485 /* Make sure that the symtab is set on the new symbols.
15486 Even though they don't appear in this symtab directly,
15487 other parts of gdb assume that symbols do, and this is
15488 reasonably true. */
15489 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15490 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15491 }
15492 }
15493 }
15494 }
15495
15496 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15497 update TYPE using some information only available in DIE's children. */
15498
15499 static void
15500 update_enumeration_type_from_children (struct die_info *die,
15501 struct type *type,
15502 struct dwarf2_cu *cu)
15503 {
15504 struct die_info *child_die;
15505 int unsigned_enum = 1;
15506 int flag_enum = 1;
15507
15508 auto_obstack obstack;
15509
15510 for (child_die = die->child;
15511 child_die != NULL && child_die->tag;
15512 child_die = sibling_die (child_die))
15513 {
15514 struct attribute *attr;
15515 LONGEST value;
15516 const gdb_byte *bytes;
15517 struct dwarf2_locexpr_baton *baton;
15518 const char *name;
15519
15520 if (child_die->tag != DW_TAG_enumerator)
15521 continue;
15522
15523 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15524 if (attr == NULL)
15525 continue;
15526
15527 name = dwarf2_name (child_die, cu);
15528 if (name == NULL)
15529 name = "<anonymous enumerator>";
15530
15531 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15532 &value, &bytes, &baton);
15533 if (value < 0)
15534 {
15535 unsigned_enum = 0;
15536 flag_enum = 0;
15537 }
15538 else
15539 {
15540 if (count_one_bits_ll (value) >= 2)
15541 flag_enum = 0;
15542 }
15543
15544 /* If we already know that the enum type is neither unsigned, nor
15545 a flag type, no need to look at the rest of the enumerates. */
15546 if (!unsigned_enum && !flag_enum)
15547 break;
15548 }
15549
15550 if (unsigned_enum)
15551 TYPE_UNSIGNED (type) = 1;
15552 if (flag_enum)
15553 TYPE_FLAG_ENUM (type) = 1;
15554 }
15555
15556 /* Given a DW_AT_enumeration_type die, set its type. We do not
15557 complete the type's fields yet, or create any symbols. */
15558
15559 static struct type *
15560 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15561 {
15562 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15563 struct type *type;
15564 struct attribute *attr;
15565 const char *name;
15566
15567 /* If the definition of this type lives in .debug_types, read that type.
15568 Don't follow DW_AT_specification though, that will take us back up
15569 the chain and we want to go down. */
15570 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15571 if (attr != nullptr)
15572 {
15573 type = get_DW_AT_signature_type (die, attr, cu);
15574
15575 /* The type's CU may not be the same as CU.
15576 Ensure TYPE is recorded with CU in die_type_hash. */
15577 return set_die_type (die, type, cu);
15578 }
15579
15580 type = alloc_type (objfile);
15581
15582 TYPE_CODE (type) = TYPE_CODE_ENUM;
15583 name = dwarf2_full_name (NULL, die, cu);
15584 if (name != NULL)
15585 TYPE_NAME (type) = name;
15586
15587 attr = dwarf2_attr (die, DW_AT_type, cu);
15588 if (attr != NULL)
15589 {
15590 struct type *underlying_type = die_type (die, cu);
15591
15592 TYPE_TARGET_TYPE (type) = underlying_type;
15593 }
15594
15595 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15596 if (attr != nullptr)
15597 {
15598 TYPE_LENGTH (type) = DW_UNSND (attr);
15599 }
15600 else
15601 {
15602 TYPE_LENGTH (type) = 0;
15603 }
15604
15605 maybe_set_alignment (cu, die, type);
15606
15607 /* The enumeration DIE can be incomplete. In Ada, any type can be
15608 declared as private in the package spec, and then defined only
15609 inside the package body. Such types are known as Taft Amendment
15610 Types. When another package uses such a type, an incomplete DIE
15611 may be generated by the compiler. */
15612 if (die_is_declaration (die, cu))
15613 TYPE_STUB (type) = 1;
15614
15615 /* Finish the creation of this type by using the enum's children.
15616 We must call this even when the underlying type has been provided
15617 so that we can determine if we're looking at a "flag" enum. */
15618 update_enumeration_type_from_children (die, type, cu);
15619
15620 /* If this type has an underlying type that is not a stub, then we
15621 may use its attributes. We always use the "unsigned" attribute
15622 in this situation, because ordinarily we guess whether the type
15623 is unsigned -- but the guess can be wrong and the underlying type
15624 can tell us the reality. However, we defer to a local size
15625 attribute if one exists, because this lets the compiler override
15626 the underlying type if needed. */
15627 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15628 {
15629 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15630 if (TYPE_LENGTH (type) == 0)
15631 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15632 if (TYPE_RAW_ALIGN (type) == 0
15633 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15634 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15635 }
15636
15637 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15638
15639 return set_die_type (die, type, cu);
15640 }
15641
15642 /* Given a pointer to a die which begins an enumeration, process all
15643 the dies that define the members of the enumeration, and create the
15644 symbol for the enumeration type.
15645
15646 NOTE: We reverse the order of the element list. */
15647
15648 static void
15649 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15650 {
15651 struct type *this_type;
15652
15653 this_type = get_die_type (die, cu);
15654 if (this_type == NULL)
15655 this_type = read_enumeration_type (die, cu);
15656
15657 if (die->child != NULL)
15658 {
15659 struct die_info *child_die;
15660 struct symbol *sym;
15661 std::vector<struct field> fields;
15662 const char *name;
15663
15664 child_die = die->child;
15665 while (child_die && child_die->tag)
15666 {
15667 if (child_die->tag != DW_TAG_enumerator)
15668 {
15669 process_die (child_die, cu);
15670 }
15671 else
15672 {
15673 name = dwarf2_name (child_die, cu);
15674 if (name)
15675 {
15676 sym = new_symbol (child_die, this_type, cu);
15677
15678 fields.emplace_back ();
15679 struct field &field = fields.back ();
15680
15681 FIELD_NAME (field) = sym->linkage_name ();
15682 FIELD_TYPE (field) = NULL;
15683 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15684 FIELD_BITSIZE (field) = 0;
15685 }
15686 }
15687
15688 child_die = sibling_die (child_die);
15689 }
15690
15691 if (!fields.empty ())
15692 {
15693 TYPE_NFIELDS (this_type) = fields.size ();
15694 TYPE_FIELDS (this_type) = (struct field *)
15695 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15696 memcpy (TYPE_FIELDS (this_type), fields.data (),
15697 sizeof (struct field) * fields.size ());
15698 }
15699 }
15700
15701 /* If we are reading an enum from a .debug_types unit, and the enum
15702 is a declaration, and the enum is not the signatured type in the
15703 unit, then we do not want to add a symbol for it. Adding a
15704 symbol would in some cases obscure the true definition of the
15705 enum, giving users an incomplete type when the definition is
15706 actually available. Note that we do not want to do this for all
15707 enums which are just declarations, because C++0x allows forward
15708 enum declarations. */
15709 if (cu->per_cu->is_debug_types
15710 && die_is_declaration (die, cu))
15711 {
15712 struct signatured_type *sig_type;
15713
15714 sig_type = (struct signatured_type *) cu->per_cu;
15715 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15716 if (sig_type->type_offset_in_section != die->sect_off)
15717 return;
15718 }
15719
15720 new_symbol (die, this_type, cu);
15721 }
15722
15723 /* Extract all information from a DW_TAG_array_type DIE and put it in
15724 the DIE's type field. For now, this only handles one dimensional
15725 arrays. */
15726
15727 static struct type *
15728 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15729 {
15730 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15731 struct die_info *child_die;
15732 struct type *type;
15733 struct type *element_type, *range_type, *index_type;
15734 struct attribute *attr;
15735 const char *name;
15736 struct dynamic_prop *byte_stride_prop = NULL;
15737 unsigned int bit_stride = 0;
15738
15739 element_type = die_type (die, cu);
15740
15741 /* The die_type call above may have already set the type for this DIE. */
15742 type = get_die_type (die, cu);
15743 if (type)
15744 return type;
15745
15746 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15747 if (attr != NULL)
15748 {
15749 int stride_ok;
15750 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15751
15752 byte_stride_prop
15753 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15754 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15755 prop_type);
15756 if (!stride_ok)
15757 {
15758 complaint (_("unable to read array DW_AT_byte_stride "
15759 " - DIE at %s [in module %s]"),
15760 sect_offset_str (die->sect_off),
15761 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15762 /* Ignore this attribute. We will likely not be able to print
15763 arrays of this type correctly, but there is little we can do
15764 to help if we cannot read the attribute's value. */
15765 byte_stride_prop = NULL;
15766 }
15767 }
15768
15769 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15770 if (attr != NULL)
15771 bit_stride = DW_UNSND (attr);
15772
15773 /* Irix 6.2 native cc creates array types without children for
15774 arrays with unspecified length. */
15775 if (die->child == NULL)
15776 {
15777 index_type = objfile_type (objfile)->builtin_int;
15778 range_type = create_static_range_type (NULL, index_type, 0, -1);
15779 type = create_array_type_with_stride (NULL, element_type, range_type,
15780 byte_stride_prop, bit_stride);
15781 return set_die_type (die, type, cu);
15782 }
15783
15784 std::vector<struct type *> range_types;
15785 child_die = die->child;
15786 while (child_die && child_die->tag)
15787 {
15788 if (child_die->tag == DW_TAG_subrange_type)
15789 {
15790 struct type *child_type = read_type_die (child_die, cu);
15791
15792 if (child_type != NULL)
15793 {
15794 /* The range type was succesfully read. Save it for the
15795 array type creation. */
15796 range_types.push_back (child_type);
15797 }
15798 }
15799 child_die = sibling_die (child_die);
15800 }
15801
15802 /* Dwarf2 dimensions are output from left to right, create the
15803 necessary array types in backwards order. */
15804
15805 type = element_type;
15806
15807 if (read_array_order (die, cu) == DW_ORD_col_major)
15808 {
15809 int i = 0;
15810
15811 while (i < range_types.size ())
15812 type = create_array_type_with_stride (NULL, type, range_types[i++],
15813 byte_stride_prop, bit_stride);
15814 }
15815 else
15816 {
15817 size_t ndim = range_types.size ();
15818 while (ndim-- > 0)
15819 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15820 byte_stride_prop, bit_stride);
15821 }
15822
15823 /* Understand Dwarf2 support for vector types (like they occur on
15824 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15825 array type. This is not part of the Dwarf2/3 standard yet, but a
15826 custom vendor extension. The main difference between a regular
15827 array and the vector variant is that vectors are passed by value
15828 to functions. */
15829 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15830 if (attr != nullptr)
15831 make_vector_type (type);
15832
15833 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15834 implementation may choose to implement triple vectors using this
15835 attribute. */
15836 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15837 if (attr != nullptr)
15838 {
15839 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15840 TYPE_LENGTH (type) = DW_UNSND (attr);
15841 else
15842 complaint (_("DW_AT_byte_size for array type smaller "
15843 "than the total size of elements"));
15844 }
15845
15846 name = dwarf2_name (die, cu);
15847 if (name)
15848 TYPE_NAME (type) = name;
15849
15850 maybe_set_alignment (cu, die, type);
15851
15852 /* Install the type in the die. */
15853 set_die_type (die, type, cu);
15854
15855 /* set_die_type should be already done. */
15856 set_descriptive_type (type, die, cu);
15857
15858 return type;
15859 }
15860
15861 static enum dwarf_array_dim_ordering
15862 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15863 {
15864 struct attribute *attr;
15865
15866 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15867
15868 if (attr != nullptr)
15869 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15870
15871 /* GNU F77 is a special case, as at 08/2004 array type info is the
15872 opposite order to the dwarf2 specification, but data is still
15873 laid out as per normal fortran.
15874
15875 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15876 version checking. */
15877
15878 if (cu->language == language_fortran
15879 && cu->producer && strstr (cu->producer, "GNU F77"))
15880 {
15881 return DW_ORD_row_major;
15882 }
15883
15884 switch (cu->language_defn->la_array_ordering)
15885 {
15886 case array_column_major:
15887 return DW_ORD_col_major;
15888 case array_row_major:
15889 default:
15890 return DW_ORD_row_major;
15891 };
15892 }
15893
15894 /* Extract all information from a DW_TAG_set_type DIE and put it in
15895 the DIE's type field. */
15896
15897 static struct type *
15898 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15899 {
15900 struct type *domain_type, *set_type;
15901 struct attribute *attr;
15902
15903 domain_type = die_type (die, cu);
15904
15905 /* The die_type call above may have already set the type for this DIE. */
15906 set_type = get_die_type (die, cu);
15907 if (set_type)
15908 return set_type;
15909
15910 set_type = create_set_type (NULL, domain_type);
15911
15912 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15913 if (attr != nullptr)
15914 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15915
15916 maybe_set_alignment (cu, die, set_type);
15917
15918 return set_die_type (die, set_type, cu);
15919 }
15920
15921 /* A helper for read_common_block that creates a locexpr baton.
15922 SYM is the symbol which we are marking as computed.
15923 COMMON_DIE is the DIE for the common block.
15924 COMMON_LOC is the location expression attribute for the common
15925 block itself.
15926 MEMBER_LOC is the location expression attribute for the particular
15927 member of the common block that we are processing.
15928 CU is the CU from which the above come. */
15929
15930 static void
15931 mark_common_block_symbol_computed (struct symbol *sym,
15932 struct die_info *common_die,
15933 struct attribute *common_loc,
15934 struct attribute *member_loc,
15935 struct dwarf2_cu *cu)
15936 {
15937 struct dwarf2_per_objfile *dwarf2_per_objfile
15938 = cu->per_cu->dwarf2_per_objfile;
15939 struct objfile *objfile = dwarf2_per_objfile->objfile;
15940 struct dwarf2_locexpr_baton *baton;
15941 gdb_byte *ptr;
15942 unsigned int cu_off;
15943 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15944 LONGEST offset = 0;
15945
15946 gdb_assert (common_loc && member_loc);
15947 gdb_assert (common_loc->form_is_block ());
15948 gdb_assert (member_loc->form_is_block ()
15949 || member_loc->form_is_constant ());
15950
15951 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15952 baton->per_cu = cu->per_cu;
15953 gdb_assert (baton->per_cu);
15954
15955 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15956
15957 if (member_loc->form_is_constant ())
15958 {
15959 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15960 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15961 }
15962 else
15963 baton->size += DW_BLOCK (member_loc)->size;
15964
15965 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15966 baton->data = ptr;
15967
15968 *ptr++ = DW_OP_call4;
15969 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15970 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15971 ptr += 4;
15972
15973 if (member_loc->form_is_constant ())
15974 {
15975 *ptr++ = DW_OP_addr;
15976 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15977 ptr += cu->header.addr_size;
15978 }
15979 else
15980 {
15981 /* We have to copy the data here, because DW_OP_call4 will only
15982 use a DW_AT_location attribute. */
15983 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15984 ptr += DW_BLOCK (member_loc)->size;
15985 }
15986
15987 *ptr++ = DW_OP_plus;
15988 gdb_assert (ptr - baton->data == baton->size);
15989
15990 SYMBOL_LOCATION_BATON (sym) = baton;
15991 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15992 }
15993
15994 /* Create appropriate locally-scoped variables for all the
15995 DW_TAG_common_block entries. Also create a struct common_block
15996 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15997 is used to separate the common blocks name namespace from regular
15998 variable names. */
15999
16000 static void
16001 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16002 {
16003 struct attribute *attr;
16004
16005 attr = dwarf2_attr (die, DW_AT_location, cu);
16006 if (attr != nullptr)
16007 {
16008 /* Support the .debug_loc offsets. */
16009 if (attr->form_is_block ())
16010 {
16011 /* Ok. */
16012 }
16013 else if (attr->form_is_section_offset ())
16014 {
16015 dwarf2_complex_location_expr_complaint ();
16016 attr = NULL;
16017 }
16018 else
16019 {
16020 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16021 "common block member");
16022 attr = NULL;
16023 }
16024 }
16025
16026 if (die->child != NULL)
16027 {
16028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16029 struct die_info *child_die;
16030 size_t n_entries = 0, size;
16031 struct common_block *common_block;
16032 struct symbol *sym;
16033
16034 for (child_die = die->child;
16035 child_die && child_die->tag;
16036 child_die = sibling_die (child_die))
16037 ++n_entries;
16038
16039 size = (sizeof (struct common_block)
16040 + (n_entries - 1) * sizeof (struct symbol *));
16041 common_block
16042 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16043 size);
16044 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16045 common_block->n_entries = 0;
16046
16047 for (child_die = die->child;
16048 child_die && child_die->tag;
16049 child_die = sibling_die (child_die))
16050 {
16051 /* Create the symbol in the DW_TAG_common_block block in the current
16052 symbol scope. */
16053 sym = new_symbol (child_die, NULL, cu);
16054 if (sym != NULL)
16055 {
16056 struct attribute *member_loc;
16057
16058 common_block->contents[common_block->n_entries++] = sym;
16059
16060 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16061 cu);
16062 if (member_loc)
16063 {
16064 /* GDB has handled this for a long time, but it is
16065 not specified by DWARF. It seems to have been
16066 emitted by gfortran at least as recently as:
16067 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16068 complaint (_("Variable in common block has "
16069 "DW_AT_data_member_location "
16070 "- DIE at %s [in module %s]"),
16071 sect_offset_str (child_die->sect_off),
16072 objfile_name (objfile));
16073
16074 if (member_loc->form_is_section_offset ())
16075 dwarf2_complex_location_expr_complaint ();
16076 else if (member_loc->form_is_constant ()
16077 || member_loc->form_is_block ())
16078 {
16079 if (attr != nullptr)
16080 mark_common_block_symbol_computed (sym, die, attr,
16081 member_loc, cu);
16082 }
16083 else
16084 dwarf2_complex_location_expr_complaint ();
16085 }
16086 }
16087 }
16088
16089 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16090 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16091 }
16092 }
16093
16094 /* Create a type for a C++ namespace. */
16095
16096 static struct type *
16097 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16098 {
16099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16100 const char *previous_prefix, *name;
16101 int is_anonymous;
16102 struct type *type;
16103
16104 /* For extensions, reuse the type of the original namespace. */
16105 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16106 {
16107 struct die_info *ext_die;
16108 struct dwarf2_cu *ext_cu = cu;
16109
16110 ext_die = dwarf2_extension (die, &ext_cu);
16111 type = read_type_die (ext_die, ext_cu);
16112
16113 /* EXT_CU may not be the same as CU.
16114 Ensure TYPE is recorded with CU in die_type_hash. */
16115 return set_die_type (die, type, cu);
16116 }
16117
16118 name = namespace_name (die, &is_anonymous, cu);
16119
16120 /* Now build the name of the current namespace. */
16121
16122 previous_prefix = determine_prefix (die, cu);
16123 if (previous_prefix[0] != '\0')
16124 name = typename_concat (&objfile->objfile_obstack,
16125 previous_prefix, name, 0, cu);
16126
16127 /* Create the type. */
16128 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16129
16130 return set_die_type (die, type, cu);
16131 }
16132
16133 /* Read a namespace scope. */
16134
16135 static void
16136 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16137 {
16138 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16139 int is_anonymous;
16140
16141 /* Add a symbol associated to this if we haven't seen the namespace
16142 before. Also, add a using directive if it's an anonymous
16143 namespace. */
16144
16145 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16146 {
16147 struct type *type;
16148
16149 type = read_type_die (die, cu);
16150 new_symbol (die, type, cu);
16151
16152 namespace_name (die, &is_anonymous, cu);
16153 if (is_anonymous)
16154 {
16155 const char *previous_prefix = determine_prefix (die, cu);
16156
16157 std::vector<const char *> excludes;
16158 add_using_directive (using_directives (cu),
16159 previous_prefix, TYPE_NAME (type), NULL,
16160 NULL, excludes, 0, &objfile->objfile_obstack);
16161 }
16162 }
16163
16164 if (die->child != NULL)
16165 {
16166 struct die_info *child_die = die->child;
16167
16168 while (child_die && child_die->tag)
16169 {
16170 process_die (child_die, cu);
16171 child_die = sibling_die (child_die);
16172 }
16173 }
16174 }
16175
16176 /* Read a Fortran module as type. This DIE can be only a declaration used for
16177 imported module. Still we need that type as local Fortran "use ... only"
16178 declaration imports depend on the created type in determine_prefix. */
16179
16180 static struct type *
16181 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16182 {
16183 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16184 const char *module_name;
16185 struct type *type;
16186
16187 module_name = dwarf2_name (die, cu);
16188 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16189
16190 return set_die_type (die, type, cu);
16191 }
16192
16193 /* Read a Fortran module. */
16194
16195 static void
16196 read_module (struct die_info *die, struct dwarf2_cu *cu)
16197 {
16198 struct die_info *child_die = die->child;
16199 struct type *type;
16200
16201 type = read_type_die (die, cu);
16202 new_symbol (die, type, cu);
16203
16204 while (child_die && child_die->tag)
16205 {
16206 process_die (child_die, cu);
16207 child_die = sibling_die (child_die);
16208 }
16209 }
16210
16211 /* Return the name of the namespace represented by DIE. Set
16212 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16213 namespace. */
16214
16215 static const char *
16216 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16217 {
16218 struct die_info *current_die;
16219 const char *name = NULL;
16220
16221 /* Loop through the extensions until we find a name. */
16222
16223 for (current_die = die;
16224 current_die != NULL;
16225 current_die = dwarf2_extension (die, &cu))
16226 {
16227 /* We don't use dwarf2_name here so that we can detect the absence
16228 of a name -> anonymous namespace. */
16229 name = dwarf2_string_attr (die, DW_AT_name, cu);
16230
16231 if (name != NULL)
16232 break;
16233 }
16234
16235 /* Is it an anonymous namespace? */
16236
16237 *is_anonymous = (name == NULL);
16238 if (*is_anonymous)
16239 name = CP_ANONYMOUS_NAMESPACE_STR;
16240
16241 return name;
16242 }
16243
16244 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16245 the user defined type vector. */
16246
16247 static struct type *
16248 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16249 {
16250 struct gdbarch *gdbarch
16251 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16252 struct comp_unit_head *cu_header = &cu->header;
16253 struct type *type;
16254 struct attribute *attr_byte_size;
16255 struct attribute *attr_address_class;
16256 int byte_size, addr_class;
16257 struct type *target_type;
16258
16259 target_type = die_type (die, cu);
16260
16261 /* The die_type call above may have already set the type for this DIE. */
16262 type = get_die_type (die, cu);
16263 if (type)
16264 return type;
16265
16266 type = lookup_pointer_type (target_type);
16267
16268 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16269 if (attr_byte_size)
16270 byte_size = DW_UNSND (attr_byte_size);
16271 else
16272 byte_size = cu_header->addr_size;
16273
16274 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16275 if (attr_address_class)
16276 addr_class = DW_UNSND (attr_address_class);
16277 else
16278 addr_class = DW_ADDR_none;
16279
16280 ULONGEST alignment = get_alignment (cu, die);
16281
16282 /* If the pointer size, alignment, or address class is different
16283 than the default, create a type variant marked as such and set
16284 the length accordingly. */
16285 if (TYPE_LENGTH (type) != byte_size
16286 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16287 && alignment != TYPE_RAW_ALIGN (type))
16288 || addr_class != DW_ADDR_none)
16289 {
16290 if (gdbarch_address_class_type_flags_p (gdbarch))
16291 {
16292 int type_flags;
16293
16294 type_flags = gdbarch_address_class_type_flags
16295 (gdbarch, byte_size, addr_class);
16296 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16297 == 0);
16298 type = make_type_with_address_space (type, type_flags);
16299 }
16300 else if (TYPE_LENGTH (type) != byte_size)
16301 {
16302 complaint (_("invalid pointer size %d"), byte_size);
16303 }
16304 else if (TYPE_RAW_ALIGN (type) != alignment)
16305 {
16306 complaint (_("Invalid DW_AT_alignment"
16307 " - DIE at %s [in module %s]"),
16308 sect_offset_str (die->sect_off),
16309 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16310 }
16311 else
16312 {
16313 /* Should we also complain about unhandled address classes? */
16314 }
16315 }
16316
16317 TYPE_LENGTH (type) = byte_size;
16318 set_type_align (type, alignment);
16319 return set_die_type (die, type, cu);
16320 }
16321
16322 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16323 the user defined type vector. */
16324
16325 static struct type *
16326 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16327 {
16328 struct type *type;
16329 struct type *to_type;
16330 struct type *domain;
16331
16332 to_type = die_type (die, cu);
16333 domain = die_containing_type (die, cu);
16334
16335 /* The calls above may have already set the type for this DIE. */
16336 type = get_die_type (die, cu);
16337 if (type)
16338 return type;
16339
16340 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16341 type = lookup_methodptr_type (to_type);
16342 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16343 {
16344 struct type *new_type
16345 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16346
16347 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16348 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16349 TYPE_VARARGS (to_type));
16350 type = lookup_methodptr_type (new_type);
16351 }
16352 else
16353 type = lookup_memberptr_type (to_type, domain);
16354
16355 return set_die_type (die, type, cu);
16356 }
16357
16358 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16359 the user defined type vector. */
16360
16361 static struct type *
16362 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16363 enum type_code refcode)
16364 {
16365 struct comp_unit_head *cu_header = &cu->header;
16366 struct type *type, *target_type;
16367 struct attribute *attr;
16368
16369 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16370
16371 target_type = die_type (die, cu);
16372
16373 /* The die_type call above may have already set the type for this DIE. */
16374 type = get_die_type (die, cu);
16375 if (type)
16376 return type;
16377
16378 type = lookup_reference_type (target_type, refcode);
16379 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16380 if (attr != nullptr)
16381 {
16382 TYPE_LENGTH (type) = DW_UNSND (attr);
16383 }
16384 else
16385 {
16386 TYPE_LENGTH (type) = cu_header->addr_size;
16387 }
16388 maybe_set_alignment (cu, die, type);
16389 return set_die_type (die, type, cu);
16390 }
16391
16392 /* Add the given cv-qualifiers to the element type of the array. GCC
16393 outputs DWARF type qualifiers that apply to an array, not the
16394 element type. But GDB relies on the array element type to carry
16395 the cv-qualifiers. This mimics section 6.7.3 of the C99
16396 specification. */
16397
16398 static struct type *
16399 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16400 struct type *base_type, int cnst, int voltl)
16401 {
16402 struct type *el_type, *inner_array;
16403
16404 base_type = copy_type (base_type);
16405 inner_array = base_type;
16406
16407 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16408 {
16409 TYPE_TARGET_TYPE (inner_array) =
16410 copy_type (TYPE_TARGET_TYPE (inner_array));
16411 inner_array = TYPE_TARGET_TYPE (inner_array);
16412 }
16413
16414 el_type = TYPE_TARGET_TYPE (inner_array);
16415 cnst |= TYPE_CONST (el_type);
16416 voltl |= TYPE_VOLATILE (el_type);
16417 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16418
16419 return set_die_type (die, base_type, cu);
16420 }
16421
16422 static struct type *
16423 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16424 {
16425 struct type *base_type, *cv_type;
16426
16427 base_type = die_type (die, cu);
16428
16429 /* The die_type call above may have already set the type for this DIE. */
16430 cv_type = get_die_type (die, cu);
16431 if (cv_type)
16432 return cv_type;
16433
16434 /* In case the const qualifier is applied to an array type, the element type
16435 is so qualified, not the array type (section 6.7.3 of C99). */
16436 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16437 return add_array_cv_type (die, cu, base_type, 1, 0);
16438
16439 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16440 return set_die_type (die, cv_type, cu);
16441 }
16442
16443 static struct type *
16444 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16445 {
16446 struct type *base_type, *cv_type;
16447
16448 base_type = die_type (die, cu);
16449
16450 /* The die_type call above may have already set the type for this DIE. */
16451 cv_type = get_die_type (die, cu);
16452 if (cv_type)
16453 return cv_type;
16454
16455 /* In case the volatile qualifier is applied to an array type, the
16456 element type is so qualified, not the array type (section 6.7.3
16457 of C99). */
16458 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16459 return add_array_cv_type (die, cu, base_type, 0, 1);
16460
16461 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16462 return set_die_type (die, cv_type, cu);
16463 }
16464
16465 /* Handle DW_TAG_restrict_type. */
16466
16467 static struct type *
16468 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16469 {
16470 struct type *base_type, *cv_type;
16471
16472 base_type = die_type (die, cu);
16473
16474 /* The die_type call above may have already set the type for this DIE. */
16475 cv_type = get_die_type (die, cu);
16476 if (cv_type)
16477 return cv_type;
16478
16479 cv_type = make_restrict_type (base_type);
16480 return set_die_type (die, cv_type, cu);
16481 }
16482
16483 /* Handle DW_TAG_atomic_type. */
16484
16485 static struct type *
16486 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16487 {
16488 struct type *base_type, *cv_type;
16489
16490 base_type = die_type (die, cu);
16491
16492 /* The die_type call above may have already set the type for this DIE. */
16493 cv_type = get_die_type (die, cu);
16494 if (cv_type)
16495 return cv_type;
16496
16497 cv_type = make_atomic_type (base_type);
16498 return set_die_type (die, cv_type, cu);
16499 }
16500
16501 /* Extract all information from a DW_TAG_string_type DIE and add to
16502 the user defined type vector. It isn't really a user defined type,
16503 but it behaves like one, with other DIE's using an AT_user_def_type
16504 attribute to reference it. */
16505
16506 static struct type *
16507 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16508 {
16509 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16510 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16511 struct type *type, *range_type, *index_type, *char_type;
16512 struct attribute *attr;
16513 struct dynamic_prop prop;
16514 bool length_is_constant = true;
16515 LONGEST length;
16516
16517 /* There are a couple of places where bit sizes might be made use of
16518 when parsing a DW_TAG_string_type, however, no producer that we know
16519 of make use of these. Handling bit sizes that are a multiple of the
16520 byte size is easy enough, but what about other bit sizes? Lets deal
16521 with that problem when we have to. Warn about these attributes being
16522 unsupported, then parse the type and ignore them like we always
16523 have. */
16524 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16525 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16526 {
16527 static bool warning_printed = false;
16528 if (!warning_printed)
16529 {
16530 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16531 "currently supported on DW_TAG_string_type."));
16532 warning_printed = true;
16533 }
16534 }
16535
16536 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16537 if (attr != nullptr && !attr->form_is_constant ())
16538 {
16539 /* The string length describes the location at which the length of
16540 the string can be found. The size of the length field can be
16541 specified with one of the attributes below. */
16542 struct type *prop_type;
16543 struct attribute *len
16544 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16545 if (len == nullptr)
16546 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16547 if (len != nullptr && len->form_is_constant ())
16548 {
16549 /* Pass 0 as the default as we know this attribute is constant
16550 and the default value will not be returned. */
16551 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16552 prop_type = cu->per_cu->int_type (sz, true);
16553 }
16554 else
16555 {
16556 /* If the size is not specified then we assume it is the size of
16557 an address on this target. */
16558 prop_type = cu->per_cu->addr_sized_int_type (true);
16559 }
16560
16561 /* Convert the attribute into a dynamic property. */
16562 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16563 length = 1;
16564 else
16565 length_is_constant = false;
16566 }
16567 else if (attr != nullptr)
16568 {
16569 /* This DW_AT_string_length just contains the length with no
16570 indirection. There's no need to create a dynamic property in this
16571 case. Pass 0 for the default value as we know it will not be
16572 returned in this case. */
16573 length = dwarf2_get_attr_constant_value (attr, 0);
16574 }
16575 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16576 {
16577 /* We don't currently support non-constant byte sizes for strings. */
16578 length = dwarf2_get_attr_constant_value (attr, 1);
16579 }
16580 else
16581 {
16582 /* Use 1 as a fallback length if we have nothing else. */
16583 length = 1;
16584 }
16585
16586 index_type = objfile_type (objfile)->builtin_int;
16587 if (length_is_constant)
16588 range_type = create_static_range_type (NULL, index_type, 1, length);
16589 else
16590 {
16591 struct dynamic_prop low_bound;
16592
16593 low_bound.kind = PROP_CONST;
16594 low_bound.data.const_val = 1;
16595 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16596 }
16597 char_type = language_string_char_type (cu->language_defn, gdbarch);
16598 type = create_string_type (NULL, char_type, range_type);
16599
16600 return set_die_type (die, type, cu);
16601 }
16602
16603 /* Assuming that DIE corresponds to a function, returns nonzero
16604 if the function is prototyped. */
16605
16606 static int
16607 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16608 {
16609 struct attribute *attr;
16610
16611 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16612 if (attr && (DW_UNSND (attr) != 0))
16613 return 1;
16614
16615 /* The DWARF standard implies that the DW_AT_prototyped attribute
16616 is only meaningful for C, but the concept also extends to other
16617 languages that allow unprototyped functions (Eg: Objective C).
16618 For all other languages, assume that functions are always
16619 prototyped. */
16620 if (cu->language != language_c
16621 && cu->language != language_objc
16622 && cu->language != language_opencl)
16623 return 1;
16624
16625 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16626 prototyped and unprototyped functions; default to prototyped,
16627 since that is more common in modern code (and RealView warns
16628 about unprototyped functions). */
16629 if (producer_is_realview (cu->producer))
16630 return 1;
16631
16632 return 0;
16633 }
16634
16635 /* Handle DIES due to C code like:
16636
16637 struct foo
16638 {
16639 int (*funcp)(int a, long l);
16640 int b;
16641 };
16642
16643 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16644
16645 static struct type *
16646 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16647 {
16648 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16649 struct type *type; /* Type that this function returns. */
16650 struct type *ftype; /* Function that returns above type. */
16651 struct attribute *attr;
16652
16653 type = die_type (die, cu);
16654
16655 /* The die_type call above may have already set the type for this DIE. */
16656 ftype = get_die_type (die, cu);
16657 if (ftype)
16658 return ftype;
16659
16660 ftype = lookup_function_type (type);
16661
16662 if (prototyped_function_p (die, cu))
16663 TYPE_PROTOTYPED (ftype) = 1;
16664
16665 /* Store the calling convention in the type if it's available in
16666 the subroutine die. Otherwise set the calling convention to
16667 the default value DW_CC_normal. */
16668 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16669 if (attr != nullptr
16670 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16671 TYPE_CALLING_CONVENTION (ftype)
16672 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16673 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16674 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16675 else
16676 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16677
16678 /* Record whether the function returns normally to its caller or not
16679 if the DWARF producer set that information. */
16680 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16681 if (attr && (DW_UNSND (attr) != 0))
16682 TYPE_NO_RETURN (ftype) = 1;
16683
16684 /* We need to add the subroutine type to the die immediately so
16685 we don't infinitely recurse when dealing with parameters
16686 declared as the same subroutine type. */
16687 set_die_type (die, ftype, cu);
16688
16689 if (die->child != NULL)
16690 {
16691 struct type *void_type = objfile_type (objfile)->builtin_void;
16692 struct die_info *child_die;
16693 int nparams, iparams;
16694
16695 /* Count the number of parameters.
16696 FIXME: GDB currently ignores vararg functions, but knows about
16697 vararg member functions. */
16698 nparams = 0;
16699 child_die = die->child;
16700 while (child_die && child_die->tag)
16701 {
16702 if (child_die->tag == DW_TAG_formal_parameter)
16703 nparams++;
16704 else if (child_die->tag == DW_TAG_unspecified_parameters)
16705 TYPE_VARARGS (ftype) = 1;
16706 child_die = sibling_die (child_die);
16707 }
16708
16709 /* Allocate storage for parameters and fill them in. */
16710 TYPE_NFIELDS (ftype) = nparams;
16711 TYPE_FIELDS (ftype) = (struct field *)
16712 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16713
16714 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16715 even if we error out during the parameters reading below. */
16716 for (iparams = 0; iparams < nparams; iparams++)
16717 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16718
16719 iparams = 0;
16720 child_die = die->child;
16721 while (child_die && child_die->tag)
16722 {
16723 if (child_die->tag == DW_TAG_formal_parameter)
16724 {
16725 struct type *arg_type;
16726
16727 /* DWARF version 2 has no clean way to discern C++
16728 static and non-static member functions. G++ helps
16729 GDB by marking the first parameter for non-static
16730 member functions (which is the this pointer) as
16731 artificial. We pass this information to
16732 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16733
16734 DWARF version 3 added DW_AT_object_pointer, which GCC
16735 4.5 does not yet generate. */
16736 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16737 if (attr != nullptr)
16738 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16739 else
16740 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16741 arg_type = die_type (child_die, cu);
16742
16743 /* RealView does not mark THIS as const, which the testsuite
16744 expects. GCC marks THIS as const in method definitions,
16745 but not in the class specifications (GCC PR 43053). */
16746 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16747 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16748 {
16749 int is_this = 0;
16750 struct dwarf2_cu *arg_cu = cu;
16751 const char *name = dwarf2_name (child_die, cu);
16752
16753 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16754 if (attr != nullptr)
16755 {
16756 /* If the compiler emits this, use it. */
16757 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16758 is_this = 1;
16759 }
16760 else if (name && strcmp (name, "this") == 0)
16761 /* Function definitions will have the argument names. */
16762 is_this = 1;
16763 else if (name == NULL && iparams == 0)
16764 /* Declarations may not have the names, so like
16765 elsewhere in GDB, assume an artificial first
16766 argument is "this". */
16767 is_this = 1;
16768
16769 if (is_this)
16770 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16771 arg_type, 0);
16772 }
16773
16774 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16775 iparams++;
16776 }
16777 child_die = sibling_die (child_die);
16778 }
16779 }
16780
16781 return ftype;
16782 }
16783
16784 static struct type *
16785 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16786 {
16787 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16788 const char *name = NULL;
16789 struct type *this_type, *target_type;
16790
16791 name = dwarf2_full_name (NULL, die, cu);
16792 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16793 TYPE_TARGET_STUB (this_type) = 1;
16794 set_die_type (die, this_type, cu);
16795 target_type = die_type (die, cu);
16796 if (target_type != this_type)
16797 TYPE_TARGET_TYPE (this_type) = target_type;
16798 else
16799 {
16800 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16801 spec and cause infinite loops in GDB. */
16802 complaint (_("Self-referential DW_TAG_typedef "
16803 "- DIE at %s [in module %s]"),
16804 sect_offset_str (die->sect_off), objfile_name (objfile));
16805 TYPE_TARGET_TYPE (this_type) = NULL;
16806 }
16807 if (name == NULL)
16808 {
16809 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16810 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16811 Handle these by just returning the target type, rather than
16812 constructing an anonymous typedef type and trying to handle this
16813 elsewhere. */
16814 set_die_type (die, target_type, cu);
16815 return target_type;
16816 }
16817 return this_type;
16818 }
16819
16820 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16821 (which may be different from NAME) to the architecture back-end to allow
16822 it to guess the correct format if necessary. */
16823
16824 static struct type *
16825 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16826 const char *name_hint, enum bfd_endian byte_order)
16827 {
16828 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16829 const struct floatformat **format;
16830 struct type *type;
16831
16832 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16833 if (format)
16834 type = init_float_type (objfile, bits, name, format, byte_order);
16835 else
16836 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16837
16838 return type;
16839 }
16840
16841 /* Allocate an integer type of size BITS and name NAME. */
16842
16843 static struct type *
16844 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16845 int bits, int unsigned_p, const char *name)
16846 {
16847 struct type *type;
16848
16849 /* Versions of Intel's C Compiler generate an integer type called "void"
16850 instead of using DW_TAG_unspecified_type. This has been seen on
16851 at least versions 14, 17, and 18. */
16852 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16853 && strcmp (name, "void") == 0)
16854 type = objfile_type (objfile)->builtin_void;
16855 else
16856 type = init_integer_type (objfile, bits, unsigned_p, name);
16857
16858 return type;
16859 }
16860
16861 /* Initialise and return a floating point type of size BITS suitable for
16862 use as a component of a complex number. The NAME_HINT is passed through
16863 when initialising the floating point type and is the name of the complex
16864 type.
16865
16866 As DWARF doesn't currently provide an explicit name for the components
16867 of a complex number, but it can be helpful to have these components
16868 named, we try to select a suitable name based on the size of the
16869 component. */
16870 static struct type *
16871 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16872 struct objfile *objfile,
16873 int bits, const char *name_hint,
16874 enum bfd_endian byte_order)
16875 {
16876 gdbarch *gdbarch = get_objfile_arch (objfile);
16877 struct type *tt = nullptr;
16878
16879 /* Try to find a suitable floating point builtin type of size BITS.
16880 We're going to use the name of this type as the name for the complex
16881 target type that we are about to create. */
16882 switch (cu->language)
16883 {
16884 case language_fortran:
16885 switch (bits)
16886 {
16887 case 32:
16888 tt = builtin_f_type (gdbarch)->builtin_real;
16889 break;
16890 case 64:
16891 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16892 break;
16893 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16894 case 128:
16895 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16896 break;
16897 }
16898 break;
16899 default:
16900 switch (bits)
16901 {
16902 case 32:
16903 tt = builtin_type (gdbarch)->builtin_float;
16904 break;
16905 case 64:
16906 tt = builtin_type (gdbarch)->builtin_double;
16907 break;
16908 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16909 case 128:
16910 tt = builtin_type (gdbarch)->builtin_long_double;
16911 break;
16912 }
16913 break;
16914 }
16915
16916 /* If the type we found doesn't match the size we were looking for, then
16917 pretend we didn't find a type at all, the complex target type we
16918 create will then be nameless. */
16919 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16920 tt = nullptr;
16921
16922 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16923 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16924 }
16925
16926 /* Find a representation of a given base type and install
16927 it in the TYPE field of the die. */
16928
16929 static struct type *
16930 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16931 {
16932 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16933 struct type *type;
16934 struct attribute *attr;
16935 int encoding = 0, bits = 0;
16936 const char *name;
16937 gdbarch *arch;
16938
16939 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16940 if (attr != nullptr)
16941 encoding = DW_UNSND (attr);
16942 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16943 if (attr != nullptr)
16944 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16945 name = dwarf2_name (die, cu);
16946 if (!name)
16947 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16948
16949 arch = get_objfile_arch (objfile);
16950 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16951
16952 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16953 if (attr)
16954 {
16955 int endianity = DW_UNSND (attr);
16956
16957 switch (endianity)
16958 {
16959 case DW_END_big:
16960 byte_order = BFD_ENDIAN_BIG;
16961 break;
16962 case DW_END_little:
16963 byte_order = BFD_ENDIAN_LITTLE;
16964 break;
16965 default:
16966 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16967 break;
16968 }
16969 }
16970
16971 switch (encoding)
16972 {
16973 case DW_ATE_address:
16974 /* Turn DW_ATE_address into a void * pointer. */
16975 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16976 type = init_pointer_type (objfile, bits, name, type);
16977 break;
16978 case DW_ATE_boolean:
16979 type = init_boolean_type (objfile, bits, 1, name);
16980 break;
16981 case DW_ATE_complex_float:
16982 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16983 byte_order);
16984 type = init_complex_type (objfile, name, type);
16985 break;
16986 case DW_ATE_decimal_float:
16987 type = init_decfloat_type (objfile, bits, name);
16988 break;
16989 case DW_ATE_float:
16990 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16991 break;
16992 case DW_ATE_signed:
16993 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16994 break;
16995 case DW_ATE_unsigned:
16996 if (cu->language == language_fortran
16997 && name
16998 && startswith (name, "character("))
16999 type = init_character_type (objfile, bits, 1, name);
17000 else
17001 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17002 break;
17003 case DW_ATE_signed_char:
17004 if (cu->language == language_ada || cu->language == language_m2
17005 || cu->language == language_pascal
17006 || cu->language == language_fortran)
17007 type = init_character_type (objfile, bits, 0, name);
17008 else
17009 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17010 break;
17011 case DW_ATE_unsigned_char:
17012 if (cu->language == language_ada || cu->language == language_m2
17013 || cu->language == language_pascal
17014 || cu->language == language_fortran
17015 || cu->language == language_rust)
17016 type = init_character_type (objfile, bits, 1, name);
17017 else
17018 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17019 break;
17020 case DW_ATE_UTF:
17021 {
17022 if (bits == 16)
17023 type = builtin_type (arch)->builtin_char16;
17024 else if (bits == 32)
17025 type = builtin_type (arch)->builtin_char32;
17026 else
17027 {
17028 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17029 bits);
17030 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17031 }
17032 return set_die_type (die, type, cu);
17033 }
17034 break;
17035
17036 default:
17037 complaint (_("unsupported DW_AT_encoding: '%s'"),
17038 dwarf_type_encoding_name (encoding));
17039 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17040 break;
17041 }
17042
17043 if (name && strcmp (name, "char") == 0)
17044 TYPE_NOSIGN (type) = 1;
17045
17046 maybe_set_alignment (cu, die, type);
17047
17048 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17049
17050 return set_die_type (die, type, cu);
17051 }
17052
17053 /* Parse dwarf attribute if it's a block, reference or constant and put the
17054 resulting value of the attribute into struct bound_prop.
17055 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17056
17057 static int
17058 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17059 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17060 struct type *default_type)
17061 {
17062 struct dwarf2_property_baton *baton;
17063 struct obstack *obstack
17064 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17065
17066 gdb_assert (default_type != NULL);
17067
17068 if (attr == NULL || prop == NULL)
17069 return 0;
17070
17071 if (attr->form_is_block ())
17072 {
17073 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17074 baton->property_type = default_type;
17075 baton->locexpr.per_cu = cu->per_cu;
17076 baton->locexpr.size = DW_BLOCK (attr)->size;
17077 baton->locexpr.data = DW_BLOCK (attr)->data;
17078 switch (attr->name)
17079 {
17080 case DW_AT_string_length:
17081 baton->locexpr.is_reference = true;
17082 break;
17083 default:
17084 baton->locexpr.is_reference = false;
17085 break;
17086 }
17087 prop->data.baton = baton;
17088 prop->kind = PROP_LOCEXPR;
17089 gdb_assert (prop->data.baton != NULL);
17090 }
17091 else if (attr->form_is_ref ())
17092 {
17093 struct dwarf2_cu *target_cu = cu;
17094 struct die_info *target_die;
17095 struct attribute *target_attr;
17096
17097 target_die = follow_die_ref (die, attr, &target_cu);
17098 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17099 if (target_attr == NULL)
17100 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17101 target_cu);
17102 if (target_attr == NULL)
17103 return 0;
17104
17105 switch (target_attr->name)
17106 {
17107 case DW_AT_location:
17108 if (target_attr->form_is_section_offset ())
17109 {
17110 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17111 baton->property_type = die_type (target_die, target_cu);
17112 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17113 prop->data.baton = baton;
17114 prop->kind = PROP_LOCLIST;
17115 gdb_assert (prop->data.baton != NULL);
17116 }
17117 else if (target_attr->form_is_block ())
17118 {
17119 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17120 baton->property_type = die_type (target_die, target_cu);
17121 baton->locexpr.per_cu = cu->per_cu;
17122 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17123 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17124 baton->locexpr.is_reference = true;
17125 prop->data.baton = baton;
17126 prop->kind = PROP_LOCEXPR;
17127 gdb_assert (prop->data.baton != NULL);
17128 }
17129 else
17130 {
17131 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17132 "dynamic property");
17133 return 0;
17134 }
17135 break;
17136 case DW_AT_data_member_location:
17137 {
17138 LONGEST offset;
17139
17140 if (!handle_data_member_location (target_die, target_cu,
17141 &offset))
17142 return 0;
17143
17144 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17145 baton->property_type = read_type_die (target_die->parent,
17146 target_cu);
17147 baton->offset_info.offset = offset;
17148 baton->offset_info.type = die_type (target_die, target_cu);
17149 prop->data.baton = baton;
17150 prop->kind = PROP_ADDR_OFFSET;
17151 break;
17152 }
17153 }
17154 }
17155 else if (attr->form_is_constant ())
17156 {
17157 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17158 prop->kind = PROP_CONST;
17159 }
17160 else
17161 {
17162 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17163 dwarf2_name (die, cu));
17164 return 0;
17165 }
17166
17167 return 1;
17168 }
17169
17170 /* See read.h. */
17171
17172 struct type *
17173 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17174 {
17175 struct objfile *objfile = dwarf2_per_objfile->objfile;
17176 struct type *int_type;
17177
17178 /* Helper macro to examine the various builtin types. */
17179 #define TRY_TYPE(F) \
17180 int_type = (unsigned_p \
17181 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17182 : objfile_type (objfile)->builtin_ ## F); \
17183 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17184 return int_type
17185
17186 TRY_TYPE (char);
17187 TRY_TYPE (short);
17188 TRY_TYPE (int);
17189 TRY_TYPE (long);
17190 TRY_TYPE (long_long);
17191
17192 #undef TRY_TYPE
17193
17194 gdb_assert_not_reached ("unable to find suitable integer type");
17195 }
17196
17197 /* See read.h. */
17198
17199 struct type *
17200 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17201 {
17202 int addr_size = this->addr_size ();
17203 return int_type (addr_size, unsigned_p);
17204 }
17205
17206 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17207 present (which is valid) then compute the default type based on the
17208 compilation units address size. */
17209
17210 static struct type *
17211 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17212 {
17213 struct type *index_type = die_type (die, cu);
17214
17215 /* Dwarf-2 specifications explicitly allows to create subrange types
17216 without specifying a base type.
17217 In that case, the base type must be set to the type of
17218 the lower bound, upper bound or count, in that order, if any of these
17219 three attributes references an object that has a type.
17220 If no base type is found, the Dwarf-2 specifications say that
17221 a signed integer type of size equal to the size of an address should
17222 be used.
17223 For the following C code: `extern char gdb_int [];'
17224 GCC produces an empty range DIE.
17225 FIXME: muller/2010-05-28: Possible references to object for low bound,
17226 high bound or count are not yet handled by this code. */
17227 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17228 index_type = cu->per_cu->addr_sized_int_type (false);
17229
17230 return index_type;
17231 }
17232
17233 /* Read the given DW_AT_subrange DIE. */
17234
17235 static struct type *
17236 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17237 {
17238 struct type *base_type, *orig_base_type;
17239 struct type *range_type;
17240 struct attribute *attr;
17241 struct dynamic_prop low, high;
17242 int low_default_is_valid;
17243 int high_bound_is_count = 0;
17244 const char *name;
17245 ULONGEST negative_mask;
17246
17247 orig_base_type = read_subrange_index_type (die, cu);
17248
17249 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17250 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17251 creating the range type, but we use the result of check_typedef
17252 when examining properties of the type. */
17253 base_type = check_typedef (orig_base_type);
17254
17255 /* The die_type call above may have already set the type for this DIE. */
17256 range_type = get_die_type (die, cu);
17257 if (range_type)
17258 return range_type;
17259
17260 low.kind = PROP_CONST;
17261 high.kind = PROP_CONST;
17262 high.data.const_val = 0;
17263
17264 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17265 omitting DW_AT_lower_bound. */
17266 switch (cu->language)
17267 {
17268 case language_c:
17269 case language_cplus:
17270 low.data.const_val = 0;
17271 low_default_is_valid = 1;
17272 break;
17273 case language_fortran:
17274 low.data.const_val = 1;
17275 low_default_is_valid = 1;
17276 break;
17277 case language_d:
17278 case language_objc:
17279 case language_rust:
17280 low.data.const_val = 0;
17281 low_default_is_valid = (cu->header.version >= 4);
17282 break;
17283 case language_ada:
17284 case language_m2:
17285 case language_pascal:
17286 low.data.const_val = 1;
17287 low_default_is_valid = (cu->header.version >= 4);
17288 break;
17289 default:
17290 low.data.const_val = 0;
17291 low_default_is_valid = 0;
17292 break;
17293 }
17294
17295 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17296 if (attr != nullptr)
17297 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17298 else if (!low_default_is_valid)
17299 complaint (_("Missing DW_AT_lower_bound "
17300 "- DIE at %s [in module %s]"),
17301 sect_offset_str (die->sect_off),
17302 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17303
17304 struct attribute *attr_ub, *attr_count;
17305 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17306 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17307 {
17308 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17309 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17310 {
17311 /* If bounds are constant do the final calculation here. */
17312 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17313 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17314 else
17315 high_bound_is_count = 1;
17316 }
17317 else
17318 {
17319 if (attr_ub != NULL)
17320 complaint (_("Unresolved DW_AT_upper_bound "
17321 "- DIE at %s [in module %s]"),
17322 sect_offset_str (die->sect_off),
17323 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17324 if (attr_count != NULL)
17325 complaint (_("Unresolved DW_AT_count "
17326 "- DIE at %s [in module %s]"),
17327 sect_offset_str (die->sect_off),
17328 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17329 }
17330 }
17331
17332 LONGEST bias = 0;
17333 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17334 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17335 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17336
17337 /* Normally, the DWARF producers are expected to use a signed
17338 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17339 But this is unfortunately not always the case, as witnessed
17340 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17341 is used instead. To work around that ambiguity, we treat
17342 the bounds as signed, and thus sign-extend their values, when
17343 the base type is signed. */
17344 negative_mask =
17345 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17346 if (low.kind == PROP_CONST
17347 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17348 low.data.const_val |= negative_mask;
17349 if (high.kind == PROP_CONST
17350 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17351 high.data.const_val |= negative_mask;
17352
17353 /* Check for bit and byte strides. */
17354 struct dynamic_prop byte_stride_prop;
17355 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17356 if (attr_byte_stride != nullptr)
17357 {
17358 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17359 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17360 prop_type);
17361 }
17362
17363 struct dynamic_prop bit_stride_prop;
17364 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17365 if (attr_bit_stride != nullptr)
17366 {
17367 /* It only makes sense to have either a bit or byte stride. */
17368 if (attr_byte_stride != nullptr)
17369 {
17370 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17371 "- DIE at %s [in module %s]"),
17372 sect_offset_str (die->sect_off),
17373 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17374 attr_bit_stride = nullptr;
17375 }
17376 else
17377 {
17378 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17379 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17380 prop_type);
17381 }
17382 }
17383
17384 if (attr_byte_stride != nullptr
17385 || attr_bit_stride != nullptr)
17386 {
17387 bool byte_stride_p = (attr_byte_stride != nullptr);
17388 struct dynamic_prop *stride
17389 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17390
17391 range_type
17392 = create_range_type_with_stride (NULL, orig_base_type, &low,
17393 &high, bias, stride, byte_stride_p);
17394 }
17395 else
17396 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17397
17398 if (high_bound_is_count)
17399 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17400
17401 /* Ada expects an empty array on no boundary attributes. */
17402 if (attr == NULL && cu->language != language_ada)
17403 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17404
17405 name = dwarf2_name (die, cu);
17406 if (name)
17407 TYPE_NAME (range_type) = name;
17408
17409 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17410 if (attr != nullptr)
17411 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17412
17413 maybe_set_alignment (cu, die, range_type);
17414
17415 set_die_type (die, range_type, cu);
17416
17417 /* set_die_type should be already done. */
17418 set_descriptive_type (range_type, die, cu);
17419
17420 return range_type;
17421 }
17422
17423 static struct type *
17424 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17425 {
17426 struct type *type;
17427
17428 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17429 NULL);
17430 TYPE_NAME (type) = dwarf2_name (die, cu);
17431
17432 /* In Ada, an unspecified type is typically used when the description
17433 of the type is deferred to a different unit. When encountering
17434 such a type, we treat it as a stub, and try to resolve it later on,
17435 when needed. */
17436 if (cu->language == language_ada)
17437 TYPE_STUB (type) = 1;
17438
17439 return set_die_type (die, type, cu);
17440 }
17441
17442 /* Read a single die and all its descendents. Set the die's sibling
17443 field to NULL; set other fields in the die correctly, and set all
17444 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17445 location of the info_ptr after reading all of those dies. PARENT
17446 is the parent of the die in question. */
17447
17448 static struct die_info *
17449 read_die_and_children (const struct die_reader_specs *reader,
17450 const gdb_byte *info_ptr,
17451 const gdb_byte **new_info_ptr,
17452 struct die_info *parent)
17453 {
17454 struct die_info *die;
17455 const gdb_byte *cur_ptr;
17456
17457 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17458 if (die == NULL)
17459 {
17460 *new_info_ptr = cur_ptr;
17461 return NULL;
17462 }
17463 store_in_ref_table (die, reader->cu);
17464
17465 if (die->has_children)
17466 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17467 else
17468 {
17469 die->child = NULL;
17470 *new_info_ptr = cur_ptr;
17471 }
17472
17473 die->sibling = NULL;
17474 die->parent = parent;
17475 return die;
17476 }
17477
17478 /* Read a die, all of its descendents, and all of its siblings; set
17479 all of the fields of all of the dies correctly. Arguments are as
17480 in read_die_and_children. */
17481
17482 static struct die_info *
17483 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17484 const gdb_byte *info_ptr,
17485 const gdb_byte **new_info_ptr,
17486 struct die_info *parent)
17487 {
17488 struct die_info *first_die, *last_sibling;
17489 const gdb_byte *cur_ptr;
17490
17491 cur_ptr = info_ptr;
17492 first_die = last_sibling = NULL;
17493
17494 while (1)
17495 {
17496 struct die_info *die
17497 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17498
17499 if (die == NULL)
17500 {
17501 *new_info_ptr = cur_ptr;
17502 return first_die;
17503 }
17504
17505 if (!first_die)
17506 first_die = die;
17507 else
17508 last_sibling->sibling = die;
17509
17510 last_sibling = die;
17511 }
17512 }
17513
17514 /* Read a die, all of its descendents, and all of its siblings; set
17515 all of the fields of all of the dies correctly. Arguments are as
17516 in read_die_and_children.
17517 This the main entry point for reading a DIE and all its children. */
17518
17519 static struct die_info *
17520 read_die_and_siblings (const struct die_reader_specs *reader,
17521 const gdb_byte *info_ptr,
17522 const gdb_byte **new_info_ptr,
17523 struct die_info *parent)
17524 {
17525 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17526 new_info_ptr, parent);
17527
17528 if (dwarf_die_debug)
17529 {
17530 fprintf_unfiltered (gdb_stdlog,
17531 "Read die from %s@0x%x of %s:\n",
17532 reader->die_section->get_name (),
17533 (unsigned) (info_ptr - reader->die_section->buffer),
17534 bfd_get_filename (reader->abfd));
17535 dump_die (die, dwarf_die_debug);
17536 }
17537
17538 return die;
17539 }
17540
17541 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17542 attributes.
17543 The caller is responsible for filling in the extra attributes
17544 and updating (*DIEP)->num_attrs.
17545 Set DIEP to point to a newly allocated die with its information,
17546 except for its child, sibling, and parent fields. */
17547
17548 static const gdb_byte *
17549 read_full_die_1 (const struct die_reader_specs *reader,
17550 struct die_info **diep, const gdb_byte *info_ptr,
17551 int num_extra_attrs)
17552 {
17553 unsigned int abbrev_number, bytes_read, i;
17554 struct abbrev_info *abbrev;
17555 struct die_info *die;
17556 struct dwarf2_cu *cu = reader->cu;
17557 bfd *abfd = reader->abfd;
17558
17559 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17560 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17561 info_ptr += bytes_read;
17562 if (!abbrev_number)
17563 {
17564 *diep = NULL;
17565 return info_ptr;
17566 }
17567
17568 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17569 if (!abbrev)
17570 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17571 abbrev_number,
17572 bfd_get_filename (abfd));
17573
17574 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17575 die->sect_off = sect_off;
17576 die->tag = abbrev->tag;
17577 die->abbrev = abbrev_number;
17578 die->has_children = abbrev->has_children;
17579
17580 /* Make the result usable.
17581 The caller needs to update num_attrs after adding the extra
17582 attributes. */
17583 die->num_attrs = abbrev->num_attrs;
17584
17585 std::vector<int> indexes_that_need_reprocess;
17586 for (i = 0; i < abbrev->num_attrs; ++i)
17587 {
17588 bool need_reprocess;
17589 info_ptr =
17590 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17591 info_ptr, &need_reprocess);
17592 if (need_reprocess)
17593 indexes_that_need_reprocess.push_back (i);
17594 }
17595
17596 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17597 if (attr != nullptr)
17598 cu->str_offsets_base = DW_UNSND (attr);
17599
17600 auto maybe_addr_base = lookup_addr_base(die);
17601 if (maybe_addr_base.has_value ())
17602 cu->addr_base = *maybe_addr_base;
17603 for (int index : indexes_that_need_reprocess)
17604 read_attribute_reprocess (reader, &die->attrs[index]);
17605 *diep = die;
17606 return info_ptr;
17607 }
17608
17609 /* Read a die and all its attributes.
17610 Set DIEP to point to a newly allocated die with its information,
17611 except for its child, sibling, and parent fields. */
17612
17613 static const gdb_byte *
17614 read_full_die (const struct die_reader_specs *reader,
17615 struct die_info **diep, const gdb_byte *info_ptr)
17616 {
17617 const gdb_byte *result;
17618
17619 result = read_full_die_1 (reader, diep, info_ptr, 0);
17620
17621 if (dwarf_die_debug)
17622 {
17623 fprintf_unfiltered (gdb_stdlog,
17624 "Read die from %s@0x%x of %s:\n",
17625 reader->die_section->get_name (),
17626 (unsigned) (info_ptr - reader->die_section->buffer),
17627 bfd_get_filename (reader->abfd));
17628 dump_die (*diep, dwarf_die_debug);
17629 }
17630
17631 return result;
17632 }
17633 \f
17634
17635 /* Returns nonzero if TAG represents a type that we might generate a partial
17636 symbol for. */
17637
17638 static int
17639 is_type_tag_for_partial (int tag)
17640 {
17641 switch (tag)
17642 {
17643 #if 0
17644 /* Some types that would be reasonable to generate partial symbols for,
17645 that we don't at present. */
17646 case DW_TAG_array_type:
17647 case DW_TAG_file_type:
17648 case DW_TAG_ptr_to_member_type:
17649 case DW_TAG_set_type:
17650 case DW_TAG_string_type:
17651 case DW_TAG_subroutine_type:
17652 #endif
17653 case DW_TAG_base_type:
17654 case DW_TAG_class_type:
17655 case DW_TAG_interface_type:
17656 case DW_TAG_enumeration_type:
17657 case DW_TAG_structure_type:
17658 case DW_TAG_subrange_type:
17659 case DW_TAG_typedef:
17660 case DW_TAG_union_type:
17661 return 1;
17662 default:
17663 return 0;
17664 }
17665 }
17666
17667 /* Load all DIEs that are interesting for partial symbols into memory. */
17668
17669 static struct partial_die_info *
17670 load_partial_dies (const struct die_reader_specs *reader,
17671 const gdb_byte *info_ptr, int building_psymtab)
17672 {
17673 struct dwarf2_cu *cu = reader->cu;
17674 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17675 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17676 unsigned int bytes_read;
17677 unsigned int load_all = 0;
17678 int nesting_level = 1;
17679
17680 parent_die = NULL;
17681 last_die = NULL;
17682
17683 gdb_assert (cu->per_cu != NULL);
17684 if (cu->per_cu->load_all_dies)
17685 load_all = 1;
17686
17687 cu->partial_dies
17688 = htab_create_alloc_ex (cu->header.length / 12,
17689 partial_die_hash,
17690 partial_die_eq,
17691 NULL,
17692 &cu->comp_unit_obstack,
17693 hashtab_obstack_allocate,
17694 dummy_obstack_deallocate);
17695
17696 while (1)
17697 {
17698 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17699
17700 /* A NULL abbrev means the end of a series of children. */
17701 if (abbrev == NULL)
17702 {
17703 if (--nesting_level == 0)
17704 return first_die;
17705
17706 info_ptr += bytes_read;
17707 last_die = parent_die;
17708 parent_die = parent_die->die_parent;
17709 continue;
17710 }
17711
17712 /* Check for template arguments. We never save these; if
17713 they're seen, we just mark the parent, and go on our way. */
17714 if (parent_die != NULL
17715 && cu->language == language_cplus
17716 && (abbrev->tag == DW_TAG_template_type_param
17717 || abbrev->tag == DW_TAG_template_value_param))
17718 {
17719 parent_die->has_template_arguments = 1;
17720
17721 if (!load_all)
17722 {
17723 /* We don't need a partial DIE for the template argument. */
17724 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17725 continue;
17726 }
17727 }
17728
17729 /* We only recurse into c++ subprograms looking for template arguments.
17730 Skip their other children. */
17731 if (!load_all
17732 && cu->language == language_cplus
17733 && parent_die != NULL
17734 && parent_die->tag == DW_TAG_subprogram)
17735 {
17736 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17737 continue;
17738 }
17739
17740 /* Check whether this DIE is interesting enough to save. Normally
17741 we would not be interested in members here, but there may be
17742 later variables referencing them via DW_AT_specification (for
17743 static members). */
17744 if (!load_all
17745 && !is_type_tag_for_partial (abbrev->tag)
17746 && abbrev->tag != DW_TAG_constant
17747 && abbrev->tag != DW_TAG_enumerator
17748 && abbrev->tag != DW_TAG_subprogram
17749 && abbrev->tag != DW_TAG_inlined_subroutine
17750 && abbrev->tag != DW_TAG_lexical_block
17751 && abbrev->tag != DW_TAG_variable
17752 && abbrev->tag != DW_TAG_namespace
17753 && abbrev->tag != DW_TAG_module
17754 && abbrev->tag != DW_TAG_member
17755 && abbrev->tag != DW_TAG_imported_unit
17756 && abbrev->tag != DW_TAG_imported_declaration)
17757 {
17758 /* Otherwise we skip to the next sibling, if any. */
17759 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17760 continue;
17761 }
17762
17763 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17764 abbrev);
17765
17766 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17767
17768 /* This two-pass algorithm for processing partial symbols has a
17769 high cost in cache pressure. Thus, handle some simple cases
17770 here which cover the majority of C partial symbols. DIEs
17771 which neither have specification tags in them, nor could have
17772 specification tags elsewhere pointing at them, can simply be
17773 processed and discarded.
17774
17775 This segment is also optional; scan_partial_symbols and
17776 add_partial_symbol will handle these DIEs if we chain
17777 them in normally. When compilers which do not emit large
17778 quantities of duplicate debug information are more common,
17779 this code can probably be removed. */
17780
17781 /* Any complete simple types at the top level (pretty much all
17782 of them, for a language without namespaces), can be processed
17783 directly. */
17784 if (parent_die == NULL
17785 && pdi.has_specification == 0
17786 && pdi.is_declaration == 0
17787 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17788 || pdi.tag == DW_TAG_base_type
17789 || pdi.tag == DW_TAG_subrange_type))
17790 {
17791 if (building_psymtab && pdi.name != NULL)
17792 add_psymbol_to_list (pdi.name, false,
17793 VAR_DOMAIN, LOC_TYPEDEF, -1,
17794 psymbol_placement::STATIC,
17795 0, cu->language, objfile);
17796 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17797 continue;
17798 }
17799
17800 /* The exception for DW_TAG_typedef with has_children above is
17801 a workaround of GCC PR debug/47510. In the case of this complaint
17802 type_name_or_error will error on such types later.
17803
17804 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17805 it could not find the child DIEs referenced later, this is checked
17806 above. In correct DWARF DW_TAG_typedef should have no children. */
17807
17808 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17809 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17810 "- DIE at %s [in module %s]"),
17811 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17812
17813 /* If we're at the second level, and we're an enumerator, and
17814 our parent has no specification (meaning possibly lives in a
17815 namespace elsewhere), then we can add the partial symbol now
17816 instead of queueing it. */
17817 if (pdi.tag == DW_TAG_enumerator
17818 && parent_die != NULL
17819 && parent_die->die_parent == NULL
17820 && parent_die->tag == DW_TAG_enumeration_type
17821 && parent_die->has_specification == 0)
17822 {
17823 if (pdi.name == NULL)
17824 complaint (_("malformed enumerator DIE ignored"));
17825 else if (building_psymtab)
17826 add_psymbol_to_list (pdi.name, false,
17827 VAR_DOMAIN, LOC_CONST, -1,
17828 cu->language == language_cplus
17829 ? psymbol_placement::GLOBAL
17830 : psymbol_placement::STATIC,
17831 0, cu->language, objfile);
17832
17833 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17834 continue;
17835 }
17836
17837 struct partial_die_info *part_die
17838 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17839
17840 /* We'll save this DIE so link it in. */
17841 part_die->die_parent = parent_die;
17842 part_die->die_sibling = NULL;
17843 part_die->die_child = NULL;
17844
17845 if (last_die && last_die == parent_die)
17846 last_die->die_child = part_die;
17847 else if (last_die)
17848 last_die->die_sibling = part_die;
17849
17850 last_die = part_die;
17851
17852 if (first_die == NULL)
17853 first_die = part_die;
17854
17855 /* Maybe add the DIE to the hash table. Not all DIEs that we
17856 find interesting need to be in the hash table, because we
17857 also have the parent/sibling/child chains; only those that we
17858 might refer to by offset later during partial symbol reading.
17859
17860 For now this means things that might have be the target of a
17861 DW_AT_specification, DW_AT_abstract_origin, or
17862 DW_AT_extension. DW_AT_extension will refer only to
17863 namespaces; DW_AT_abstract_origin refers to functions (and
17864 many things under the function DIE, but we do not recurse
17865 into function DIEs during partial symbol reading) and
17866 possibly variables as well; DW_AT_specification refers to
17867 declarations. Declarations ought to have the DW_AT_declaration
17868 flag. It happens that GCC forgets to put it in sometimes, but
17869 only for functions, not for types.
17870
17871 Adding more things than necessary to the hash table is harmless
17872 except for the performance cost. Adding too few will result in
17873 wasted time in find_partial_die, when we reread the compilation
17874 unit with load_all_dies set. */
17875
17876 if (load_all
17877 || abbrev->tag == DW_TAG_constant
17878 || abbrev->tag == DW_TAG_subprogram
17879 || abbrev->tag == DW_TAG_variable
17880 || abbrev->tag == DW_TAG_namespace
17881 || part_die->is_declaration)
17882 {
17883 void **slot;
17884
17885 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17886 to_underlying (part_die->sect_off),
17887 INSERT);
17888 *slot = part_die;
17889 }
17890
17891 /* For some DIEs we want to follow their children (if any). For C
17892 we have no reason to follow the children of structures; for other
17893 languages we have to, so that we can get at method physnames
17894 to infer fully qualified class names, for DW_AT_specification,
17895 and for C++ template arguments. For C++, we also look one level
17896 inside functions to find template arguments (if the name of the
17897 function does not already contain the template arguments).
17898
17899 For Ada and Fortran, we need to scan the children of subprograms
17900 and lexical blocks as well because these languages allow the
17901 definition of nested entities that could be interesting for the
17902 debugger, such as nested subprograms for instance. */
17903 if (last_die->has_children
17904 && (load_all
17905 || last_die->tag == DW_TAG_namespace
17906 || last_die->tag == DW_TAG_module
17907 || last_die->tag == DW_TAG_enumeration_type
17908 || (cu->language == language_cplus
17909 && last_die->tag == DW_TAG_subprogram
17910 && (last_die->name == NULL
17911 || strchr (last_die->name, '<') == NULL))
17912 || (cu->language != language_c
17913 && (last_die->tag == DW_TAG_class_type
17914 || last_die->tag == DW_TAG_interface_type
17915 || last_die->tag == DW_TAG_structure_type
17916 || last_die->tag == DW_TAG_union_type))
17917 || ((cu->language == language_ada
17918 || cu->language == language_fortran)
17919 && (last_die->tag == DW_TAG_subprogram
17920 || last_die->tag == DW_TAG_lexical_block))))
17921 {
17922 nesting_level++;
17923 parent_die = last_die;
17924 continue;
17925 }
17926
17927 /* Otherwise we skip to the next sibling, if any. */
17928 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17929
17930 /* Back to the top, do it again. */
17931 }
17932 }
17933
17934 partial_die_info::partial_die_info (sect_offset sect_off_,
17935 struct abbrev_info *abbrev)
17936 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17937 {
17938 }
17939
17940 /* Read a minimal amount of information into the minimal die structure.
17941 INFO_PTR should point just after the initial uleb128 of a DIE. */
17942
17943 const gdb_byte *
17944 partial_die_info::read (const struct die_reader_specs *reader,
17945 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17946 {
17947 struct dwarf2_cu *cu = reader->cu;
17948 struct dwarf2_per_objfile *dwarf2_per_objfile
17949 = cu->per_cu->dwarf2_per_objfile;
17950 unsigned int i;
17951 int has_low_pc_attr = 0;
17952 int has_high_pc_attr = 0;
17953 int high_pc_relative = 0;
17954
17955 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17956 for (i = 0; i < abbrev.num_attrs; ++i)
17957 {
17958 bool need_reprocess;
17959 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17960 info_ptr, &need_reprocess);
17961 /* String and address offsets that need to do the reprocessing have
17962 already been read at this point, so there is no need to wait until
17963 the loop terminates to do the reprocessing. */
17964 if (need_reprocess)
17965 read_attribute_reprocess (reader, &attr_vec[i]);
17966 attribute &attr = attr_vec[i];
17967 /* Store the data if it is of an attribute we want to keep in a
17968 partial symbol table. */
17969 switch (attr.name)
17970 {
17971 case DW_AT_name:
17972 switch (tag)
17973 {
17974 case DW_TAG_compile_unit:
17975 case DW_TAG_partial_unit:
17976 case DW_TAG_type_unit:
17977 /* Compilation units have a DW_AT_name that is a filename, not
17978 a source language identifier. */
17979 case DW_TAG_enumeration_type:
17980 case DW_TAG_enumerator:
17981 /* These tags always have simple identifiers already; no need
17982 to canonicalize them. */
17983 name = DW_STRING (&attr);
17984 break;
17985 default:
17986 {
17987 struct objfile *objfile = dwarf2_per_objfile->objfile;
17988
17989 name
17990 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17991 }
17992 break;
17993 }
17994 break;
17995 case DW_AT_linkage_name:
17996 case DW_AT_MIPS_linkage_name:
17997 /* Note that both forms of linkage name might appear. We
17998 assume they will be the same, and we only store the last
17999 one we see. */
18000 linkage_name = DW_STRING (&attr);
18001 break;
18002 case DW_AT_low_pc:
18003 has_low_pc_attr = 1;
18004 lowpc = attr.value_as_address ();
18005 break;
18006 case DW_AT_high_pc:
18007 has_high_pc_attr = 1;
18008 highpc = attr.value_as_address ();
18009 if (cu->header.version >= 4 && attr.form_is_constant ())
18010 high_pc_relative = 1;
18011 break;
18012 case DW_AT_location:
18013 /* Support the .debug_loc offsets. */
18014 if (attr.form_is_block ())
18015 {
18016 d.locdesc = DW_BLOCK (&attr);
18017 }
18018 else if (attr.form_is_section_offset ())
18019 {
18020 dwarf2_complex_location_expr_complaint ();
18021 }
18022 else
18023 {
18024 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18025 "partial symbol information");
18026 }
18027 break;
18028 case DW_AT_external:
18029 is_external = DW_UNSND (&attr);
18030 break;
18031 case DW_AT_declaration:
18032 is_declaration = DW_UNSND (&attr);
18033 break;
18034 case DW_AT_type:
18035 has_type = 1;
18036 break;
18037 case DW_AT_abstract_origin:
18038 case DW_AT_specification:
18039 case DW_AT_extension:
18040 has_specification = 1;
18041 spec_offset = dwarf2_get_ref_die_offset (&attr);
18042 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18043 || cu->per_cu->is_dwz);
18044 break;
18045 case DW_AT_sibling:
18046 /* Ignore absolute siblings, they might point outside of
18047 the current compile unit. */
18048 if (attr.form == DW_FORM_ref_addr)
18049 complaint (_("ignoring absolute DW_AT_sibling"));
18050 else
18051 {
18052 const gdb_byte *buffer = reader->buffer;
18053 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18054 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18055
18056 if (sibling_ptr < info_ptr)
18057 complaint (_("DW_AT_sibling points backwards"));
18058 else if (sibling_ptr > reader->buffer_end)
18059 reader->die_section->overflow_complaint ();
18060 else
18061 sibling = sibling_ptr;
18062 }
18063 break;
18064 case DW_AT_byte_size:
18065 has_byte_size = 1;
18066 break;
18067 case DW_AT_const_value:
18068 has_const_value = 1;
18069 break;
18070 case DW_AT_calling_convention:
18071 /* DWARF doesn't provide a way to identify a program's source-level
18072 entry point. DW_AT_calling_convention attributes are only meant
18073 to describe functions' calling conventions.
18074
18075 However, because it's a necessary piece of information in
18076 Fortran, and before DWARF 4 DW_CC_program was the only
18077 piece of debugging information whose definition refers to
18078 a 'main program' at all, several compilers marked Fortran
18079 main programs with DW_CC_program --- even when those
18080 functions use the standard calling conventions.
18081
18082 Although DWARF now specifies a way to provide this
18083 information, we support this practice for backward
18084 compatibility. */
18085 if (DW_UNSND (&attr) == DW_CC_program
18086 && cu->language == language_fortran)
18087 main_subprogram = 1;
18088 break;
18089 case DW_AT_inline:
18090 if (DW_UNSND (&attr) == DW_INL_inlined
18091 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18092 may_be_inlined = 1;
18093 break;
18094
18095 case DW_AT_import:
18096 if (tag == DW_TAG_imported_unit)
18097 {
18098 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18099 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18100 || cu->per_cu->is_dwz);
18101 }
18102 break;
18103
18104 case DW_AT_main_subprogram:
18105 main_subprogram = DW_UNSND (&attr);
18106 break;
18107
18108 case DW_AT_ranges:
18109 {
18110 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18111 but that requires a full DIE, so instead we just
18112 reimplement it. */
18113 int need_ranges_base = tag != DW_TAG_compile_unit;
18114 unsigned int ranges_offset = (DW_UNSND (&attr)
18115 + (need_ranges_base
18116 ? cu->ranges_base
18117 : 0));
18118
18119 /* Value of the DW_AT_ranges attribute is the offset in the
18120 .debug_ranges section. */
18121 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18122 nullptr))
18123 has_pc_info = 1;
18124 }
18125 break;
18126
18127 default:
18128 break;
18129 }
18130 }
18131
18132 /* For Ada, if both the name and the linkage name appear, we prefer
18133 the latter. This lets "catch exception" work better, regardless
18134 of the order in which the name and linkage name were emitted.
18135 Really, though, this is just a workaround for the fact that gdb
18136 doesn't store both the name and the linkage name. */
18137 if (cu->language == language_ada && linkage_name != nullptr)
18138 name = linkage_name;
18139
18140 if (high_pc_relative)
18141 highpc += lowpc;
18142
18143 if (has_low_pc_attr && has_high_pc_attr)
18144 {
18145 /* When using the GNU linker, .gnu.linkonce. sections are used to
18146 eliminate duplicate copies of functions and vtables and such.
18147 The linker will arbitrarily choose one and discard the others.
18148 The AT_*_pc values for such functions refer to local labels in
18149 these sections. If the section from that file was discarded, the
18150 labels are not in the output, so the relocs get a value of 0.
18151 If this is a discarded function, mark the pc bounds as invalid,
18152 so that GDB will ignore it. */
18153 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18154 {
18155 struct objfile *objfile = dwarf2_per_objfile->objfile;
18156 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18157
18158 complaint (_("DW_AT_low_pc %s is zero "
18159 "for DIE at %s [in module %s]"),
18160 paddress (gdbarch, lowpc),
18161 sect_offset_str (sect_off),
18162 objfile_name (objfile));
18163 }
18164 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18165 else if (lowpc >= highpc)
18166 {
18167 struct objfile *objfile = dwarf2_per_objfile->objfile;
18168 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18169
18170 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18171 "for DIE at %s [in module %s]"),
18172 paddress (gdbarch, lowpc),
18173 paddress (gdbarch, highpc),
18174 sect_offset_str (sect_off),
18175 objfile_name (objfile));
18176 }
18177 else
18178 has_pc_info = 1;
18179 }
18180
18181 return info_ptr;
18182 }
18183
18184 /* Find a cached partial DIE at OFFSET in CU. */
18185
18186 struct partial_die_info *
18187 dwarf2_cu::find_partial_die (sect_offset sect_off)
18188 {
18189 struct partial_die_info *lookup_die = NULL;
18190 struct partial_die_info part_die (sect_off);
18191
18192 lookup_die = ((struct partial_die_info *)
18193 htab_find_with_hash (partial_dies, &part_die,
18194 to_underlying (sect_off)));
18195
18196 return lookup_die;
18197 }
18198
18199 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18200 except in the case of .debug_types DIEs which do not reference
18201 outside their CU (they do however referencing other types via
18202 DW_FORM_ref_sig8). */
18203
18204 static const struct cu_partial_die_info
18205 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18206 {
18207 struct dwarf2_per_objfile *dwarf2_per_objfile
18208 = cu->per_cu->dwarf2_per_objfile;
18209 struct objfile *objfile = dwarf2_per_objfile->objfile;
18210 struct dwarf2_per_cu_data *per_cu = NULL;
18211 struct partial_die_info *pd = NULL;
18212
18213 if (offset_in_dwz == cu->per_cu->is_dwz
18214 && cu->header.offset_in_cu_p (sect_off))
18215 {
18216 pd = cu->find_partial_die (sect_off);
18217 if (pd != NULL)
18218 return { cu, pd };
18219 /* We missed recording what we needed.
18220 Load all dies and try again. */
18221 per_cu = cu->per_cu;
18222 }
18223 else
18224 {
18225 /* TUs don't reference other CUs/TUs (except via type signatures). */
18226 if (cu->per_cu->is_debug_types)
18227 {
18228 error (_("Dwarf Error: Type Unit at offset %s contains"
18229 " external reference to offset %s [in module %s].\n"),
18230 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18231 bfd_get_filename (objfile->obfd));
18232 }
18233 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18234 dwarf2_per_objfile);
18235
18236 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18237 load_partial_comp_unit (per_cu);
18238
18239 per_cu->cu->last_used = 0;
18240 pd = per_cu->cu->find_partial_die (sect_off);
18241 }
18242
18243 /* If we didn't find it, and not all dies have been loaded,
18244 load them all and try again. */
18245
18246 if (pd == NULL && per_cu->load_all_dies == 0)
18247 {
18248 per_cu->load_all_dies = 1;
18249
18250 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18251 THIS_CU->cu may already be in use. So we can't just free it and
18252 replace its DIEs with the ones we read in. Instead, we leave those
18253 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18254 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18255 set. */
18256 load_partial_comp_unit (per_cu);
18257
18258 pd = per_cu->cu->find_partial_die (sect_off);
18259 }
18260
18261 if (pd == NULL)
18262 internal_error (__FILE__, __LINE__,
18263 _("could not find partial DIE %s "
18264 "in cache [from module %s]\n"),
18265 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18266 return { per_cu->cu, pd };
18267 }
18268
18269 /* See if we can figure out if the class lives in a namespace. We do
18270 this by looking for a member function; its demangled name will
18271 contain namespace info, if there is any. */
18272
18273 static void
18274 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18275 struct dwarf2_cu *cu)
18276 {
18277 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18278 what template types look like, because the demangler
18279 frequently doesn't give the same name as the debug info. We
18280 could fix this by only using the demangled name to get the
18281 prefix (but see comment in read_structure_type). */
18282
18283 struct partial_die_info *real_pdi;
18284 struct partial_die_info *child_pdi;
18285
18286 /* If this DIE (this DIE's specification, if any) has a parent, then
18287 we should not do this. We'll prepend the parent's fully qualified
18288 name when we create the partial symbol. */
18289
18290 real_pdi = struct_pdi;
18291 while (real_pdi->has_specification)
18292 {
18293 auto res = find_partial_die (real_pdi->spec_offset,
18294 real_pdi->spec_is_dwz, cu);
18295 real_pdi = res.pdi;
18296 cu = res.cu;
18297 }
18298
18299 if (real_pdi->die_parent != NULL)
18300 return;
18301
18302 for (child_pdi = struct_pdi->die_child;
18303 child_pdi != NULL;
18304 child_pdi = child_pdi->die_sibling)
18305 {
18306 if (child_pdi->tag == DW_TAG_subprogram
18307 && child_pdi->linkage_name != NULL)
18308 {
18309 gdb::unique_xmalloc_ptr<char> actual_class_name
18310 (language_class_name_from_physname (cu->language_defn,
18311 child_pdi->linkage_name));
18312 if (actual_class_name != NULL)
18313 {
18314 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18315 struct_pdi->name = objfile->intern (actual_class_name.get ());
18316 }
18317 break;
18318 }
18319 }
18320 }
18321
18322 void
18323 partial_die_info::fixup (struct dwarf2_cu *cu)
18324 {
18325 /* Once we've fixed up a die, there's no point in doing so again.
18326 This also avoids a memory leak if we were to call
18327 guess_partial_die_structure_name multiple times. */
18328 if (fixup_called)
18329 return;
18330
18331 /* If we found a reference attribute and the DIE has no name, try
18332 to find a name in the referred to DIE. */
18333
18334 if (name == NULL && has_specification)
18335 {
18336 struct partial_die_info *spec_die;
18337
18338 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18339 spec_die = res.pdi;
18340 cu = res.cu;
18341
18342 spec_die->fixup (cu);
18343
18344 if (spec_die->name)
18345 {
18346 name = spec_die->name;
18347
18348 /* Copy DW_AT_external attribute if it is set. */
18349 if (spec_die->is_external)
18350 is_external = spec_die->is_external;
18351 }
18352 }
18353
18354 /* Set default names for some unnamed DIEs. */
18355
18356 if (name == NULL && tag == DW_TAG_namespace)
18357 name = CP_ANONYMOUS_NAMESPACE_STR;
18358
18359 /* If there is no parent die to provide a namespace, and there are
18360 children, see if we can determine the namespace from their linkage
18361 name. */
18362 if (cu->language == language_cplus
18363 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18364 && die_parent == NULL
18365 && has_children
18366 && (tag == DW_TAG_class_type
18367 || tag == DW_TAG_structure_type
18368 || tag == DW_TAG_union_type))
18369 guess_partial_die_structure_name (this, cu);
18370
18371 /* GCC might emit a nameless struct or union that has a linkage
18372 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18373 if (name == NULL
18374 && (tag == DW_TAG_class_type
18375 || tag == DW_TAG_interface_type
18376 || tag == DW_TAG_structure_type
18377 || tag == DW_TAG_union_type)
18378 && linkage_name != NULL)
18379 {
18380 gdb::unique_xmalloc_ptr<char> demangled
18381 (gdb_demangle (linkage_name, DMGL_TYPES));
18382 if (demangled != nullptr)
18383 {
18384 const char *base;
18385
18386 /* Strip any leading namespaces/classes, keep only the base name.
18387 DW_AT_name for named DIEs does not contain the prefixes. */
18388 base = strrchr (demangled.get (), ':');
18389 if (base && base > demangled.get () && base[-1] == ':')
18390 base++;
18391 else
18392 base = demangled.get ();
18393
18394 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18395 name = objfile->intern (base);
18396 }
18397 }
18398
18399 fixup_called = 1;
18400 }
18401
18402 /* Process the attributes that had to be skipped in the first round. These
18403 attributes are the ones that need str_offsets_base or addr_base attributes.
18404 They could not have been processed in the first round, because at the time
18405 the values of str_offsets_base or addr_base may not have been known. */
18406 void read_attribute_reprocess (const struct die_reader_specs *reader,
18407 struct attribute *attr)
18408 {
18409 struct dwarf2_cu *cu = reader->cu;
18410 switch (attr->form)
18411 {
18412 case DW_FORM_addrx:
18413 case DW_FORM_GNU_addr_index:
18414 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18415 break;
18416 case DW_FORM_strx:
18417 case DW_FORM_strx1:
18418 case DW_FORM_strx2:
18419 case DW_FORM_strx3:
18420 case DW_FORM_strx4:
18421 case DW_FORM_GNU_str_index:
18422 {
18423 unsigned int str_index = DW_UNSND (attr);
18424 if (reader->dwo_file != NULL)
18425 {
18426 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18427 DW_STRING_IS_CANONICAL (attr) = 0;
18428 }
18429 else
18430 {
18431 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18432 DW_STRING_IS_CANONICAL (attr) = 0;
18433 }
18434 break;
18435 }
18436 default:
18437 gdb_assert_not_reached (_("Unexpected DWARF form."));
18438 }
18439 }
18440
18441 /* Read an attribute value described by an attribute form. */
18442
18443 static const gdb_byte *
18444 read_attribute_value (const struct die_reader_specs *reader,
18445 struct attribute *attr, unsigned form,
18446 LONGEST implicit_const, const gdb_byte *info_ptr,
18447 bool *need_reprocess)
18448 {
18449 struct dwarf2_cu *cu = reader->cu;
18450 struct dwarf2_per_objfile *dwarf2_per_objfile
18451 = cu->per_cu->dwarf2_per_objfile;
18452 struct objfile *objfile = dwarf2_per_objfile->objfile;
18453 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18454 bfd *abfd = reader->abfd;
18455 struct comp_unit_head *cu_header = &cu->header;
18456 unsigned int bytes_read;
18457 struct dwarf_block *blk;
18458 *need_reprocess = false;
18459
18460 attr->form = (enum dwarf_form) form;
18461 switch (form)
18462 {
18463 case DW_FORM_ref_addr:
18464 if (cu->header.version == 2)
18465 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18466 &bytes_read);
18467 else
18468 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18469 &bytes_read);
18470 info_ptr += bytes_read;
18471 break;
18472 case DW_FORM_GNU_ref_alt:
18473 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18474 info_ptr += bytes_read;
18475 break;
18476 case DW_FORM_addr:
18477 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18478 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18479 info_ptr += bytes_read;
18480 break;
18481 case DW_FORM_block2:
18482 blk = dwarf_alloc_block (cu);
18483 blk->size = read_2_bytes (abfd, info_ptr);
18484 info_ptr += 2;
18485 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18486 info_ptr += blk->size;
18487 DW_BLOCK (attr) = blk;
18488 break;
18489 case DW_FORM_block4:
18490 blk = dwarf_alloc_block (cu);
18491 blk->size = read_4_bytes (abfd, info_ptr);
18492 info_ptr += 4;
18493 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18494 info_ptr += blk->size;
18495 DW_BLOCK (attr) = blk;
18496 break;
18497 case DW_FORM_data2:
18498 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18499 info_ptr += 2;
18500 break;
18501 case DW_FORM_data4:
18502 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18503 info_ptr += 4;
18504 break;
18505 case DW_FORM_data8:
18506 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18507 info_ptr += 8;
18508 break;
18509 case DW_FORM_data16:
18510 blk = dwarf_alloc_block (cu);
18511 blk->size = 16;
18512 blk->data = read_n_bytes (abfd, info_ptr, 16);
18513 info_ptr += 16;
18514 DW_BLOCK (attr) = blk;
18515 break;
18516 case DW_FORM_sec_offset:
18517 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18518 info_ptr += bytes_read;
18519 break;
18520 case DW_FORM_string:
18521 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18522 DW_STRING_IS_CANONICAL (attr) = 0;
18523 info_ptr += bytes_read;
18524 break;
18525 case DW_FORM_strp:
18526 if (!cu->per_cu->is_dwz)
18527 {
18528 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18529 abfd, info_ptr, cu_header,
18530 &bytes_read);
18531 DW_STRING_IS_CANONICAL (attr) = 0;
18532 info_ptr += bytes_read;
18533 break;
18534 }
18535 /* FALLTHROUGH */
18536 case DW_FORM_line_strp:
18537 if (!cu->per_cu->is_dwz)
18538 {
18539 DW_STRING (attr)
18540 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18541 &bytes_read);
18542 DW_STRING_IS_CANONICAL (attr) = 0;
18543 info_ptr += bytes_read;
18544 break;
18545 }
18546 /* FALLTHROUGH */
18547 case DW_FORM_GNU_strp_alt:
18548 {
18549 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18550 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18551 &bytes_read);
18552
18553 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18554 DW_STRING_IS_CANONICAL (attr) = 0;
18555 info_ptr += bytes_read;
18556 }
18557 break;
18558 case DW_FORM_exprloc:
18559 case DW_FORM_block:
18560 blk = dwarf_alloc_block (cu);
18561 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18562 info_ptr += bytes_read;
18563 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18564 info_ptr += blk->size;
18565 DW_BLOCK (attr) = blk;
18566 break;
18567 case DW_FORM_block1:
18568 blk = dwarf_alloc_block (cu);
18569 blk->size = read_1_byte (abfd, info_ptr);
18570 info_ptr += 1;
18571 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18572 info_ptr += blk->size;
18573 DW_BLOCK (attr) = blk;
18574 break;
18575 case DW_FORM_data1:
18576 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18577 info_ptr += 1;
18578 break;
18579 case DW_FORM_flag:
18580 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18581 info_ptr += 1;
18582 break;
18583 case DW_FORM_flag_present:
18584 DW_UNSND (attr) = 1;
18585 break;
18586 case DW_FORM_sdata:
18587 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18588 info_ptr += bytes_read;
18589 break;
18590 case DW_FORM_udata:
18591 case DW_FORM_rnglistx:
18592 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18593 info_ptr += bytes_read;
18594 break;
18595 case DW_FORM_ref1:
18596 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18597 + read_1_byte (abfd, info_ptr));
18598 info_ptr += 1;
18599 break;
18600 case DW_FORM_ref2:
18601 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18602 + read_2_bytes (abfd, info_ptr));
18603 info_ptr += 2;
18604 break;
18605 case DW_FORM_ref4:
18606 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18607 + read_4_bytes (abfd, info_ptr));
18608 info_ptr += 4;
18609 break;
18610 case DW_FORM_ref8:
18611 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18612 + read_8_bytes (abfd, info_ptr));
18613 info_ptr += 8;
18614 break;
18615 case DW_FORM_ref_sig8:
18616 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18617 info_ptr += 8;
18618 break;
18619 case DW_FORM_ref_udata:
18620 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18621 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18622 info_ptr += bytes_read;
18623 break;
18624 case DW_FORM_indirect:
18625 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18626 info_ptr += bytes_read;
18627 if (form == DW_FORM_implicit_const)
18628 {
18629 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18630 info_ptr += bytes_read;
18631 }
18632 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18633 info_ptr, need_reprocess);
18634 break;
18635 case DW_FORM_implicit_const:
18636 DW_SND (attr) = implicit_const;
18637 break;
18638 case DW_FORM_addrx:
18639 case DW_FORM_GNU_addr_index:
18640 *need_reprocess = true;
18641 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18642 info_ptr += bytes_read;
18643 break;
18644 case DW_FORM_strx:
18645 case DW_FORM_strx1:
18646 case DW_FORM_strx2:
18647 case DW_FORM_strx3:
18648 case DW_FORM_strx4:
18649 case DW_FORM_GNU_str_index:
18650 {
18651 ULONGEST str_index;
18652 if (form == DW_FORM_strx1)
18653 {
18654 str_index = read_1_byte (abfd, info_ptr);
18655 info_ptr += 1;
18656 }
18657 else if (form == DW_FORM_strx2)
18658 {
18659 str_index = read_2_bytes (abfd, info_ptr);
18660 info_ptr += 2;
18661 }
18662 else if (form == DW_FORM_strx3)
18663 {
18664 str_index = read_3_bytes (abfd, info_ptr);
18665 info_ptr += 3;
18666 }
18667 else if (form == DW_FORM_strx4)
18668 {
18669 str_index = read_4_bytes (abfd, info_ptr);
18670 info_ptr += 4;
18671 }
18672 else
18673 {
18674 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18675 info_ptr += bytes_read;
18676 }
18677 *need_reprocess = true;
18678 DW_UNSND (attr) = str_index;
18679 }
18680 break;
18681 default:
18682 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18683 dwarf_form_name (form),
18684 bfd_get_filename (abfd));
18685 }
18686
18687 /* Super hack. */
18688 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18689 attr->form = DW_FORM_GNU_ref_alt;
18690
18691 /* We have seen instances where the compiler tried to emit a byte
18692 size attribute of -1 which ended up being encoded as an unsigned
18693 0xffffffff. Although 0xffffffff is technically a valid size value,
18694 an object of this size seems pretty unlikely so we can relatively
18695 safely treat these cases as if the size attribute was invalid and
18696 treat them as zero by default. */
18697 if (attr->name == DW_AT_byte_size
18698 && form == DW_FORM_data4
18699 && DW_UNSND (attr) >= 0xffffffff)
18700 {
18701 complaint
18702 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18703 hex_string (DW_UNSND (attr)));
18704 DW_UNSND (attr) = 0;
18705 }
18706
18707 return info_ptr;
18708 }
18709
18710 /* Read an attribute described by an abbreviated attribute. */
18711
18712 static const gdb_byte *
18713 read_attribute (const struct die_reader_specs *reader,
18714 struct attribute *attr, struct attr_abbrev *abbrev,
18715 const gdb_byte *info_ptr, bool *need_reprocess)
18716 {
18717 attr->name = abbrev->name;
18718 return read_attribute_value (reader, attr, abbrev->form,
18719 abbrev->implicit_const, info_ptr,
18720 need_reprocess);
18721 }
18722
18723 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18724
18725 static const char *
18726 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18727 LONGEST str_offset)
18728 {
18729 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18730 str_offset, "DW_FORM_strp");
18731 }
18732
18733 /* Return pointer to string at .debug_str offset as read from BUF.
18734 BUF is assumed to be in a compilation unit described by CU_HEADER.
18735 Return *BYTES_READ_PTR count of bytes read from BUF. */
18736
18737 static const char *
18738 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18739 const gdb_byte *buf,
18740 const struct comp_unit_head *cu_header,
18741 unsigned int *bytes_read_ptr)
18742 {
18743 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18744
18745 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18746 }
18747
18748 /* See read.h. */
18749
18750 const char *
18751 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18752 const struct comp_unit_head *cu_header,
18753 unsigned int *bytes_read_ptr)
18754 {
18755 bfd *abfd = objfile->obfd;
18756 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18757
18758 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18759 }
18760
18761 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18762 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18763 ADDR_SIZE is the size of addresses from the CU header. */
18764
18765 static CORE_ADDR
18766 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18767 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18768 int addr_size)
18769 {
18770 struct objfile *objfile = dwarf2_per_objfile->objfile;
18771 bfd *abfd = objfile->obfd;
18772 const gdb_byte *info_ptr;
18773 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18774
18775 dwarf2_per_objfile->addr.read (objfile);
18776 if (dwarf2_per_objfile->addr.buffer == NULL)
18777 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18778 objfile_name (objfile));
18779 if (addr_base_or_zero + addr_index * addr_size
18780 >= dwarf2_per_objfile->addr.size)
18781 error (_("DW_FORM_addr_index pointing outside of "
18782 ".debug_addr section [in module %s]"),
18783 objfile_name (objfile));
18784 info_ptr = (dwarf2_per_objfile->addr.buffer
18785 + addr_base_or_zero + addr_index * addr_size);
18786 if (addr_size == 4)
18787 return bfd_get_32 (abfd, info_ptr);
18788 else
18789 return bfd_get_64 (abfd, info_ptr);
18790 }
18791
18792 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18793
18794 static CORE_ADDR
18795 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18796 {
18797 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18798 cu->addr_base, cu->header.addr_size);
18799 }
18800
18801 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18802
18803 static CORE_ADDR
18804 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18805 unsigned int *bytes_read)
18806 {
18807 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18808 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18809
18810 return read_addr_index (cu, addr_index);
18811 }
18812
18813 /* See read.h. */
18814
18815 CORE_ADDR
18816 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18817 {
18818 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18819 struct dwarf2_cu *cu = per_cu->cu;
18820 gdb::optional<ULONGEST> addr_base;
18821 int addr_size;
18822
18823 /* We need addr_base and addr_size.
18824 If we don't have PER_CU->cu, we have to get it.
18825 Nasty, but the alternative is storing the needed info in PER_CU,
18826 which at this point doesn't seem justified: it's not clear how frequently
18827 it would get used and it would increase the size of every PER_CU.
18828 Entry points like dwarf2_per_cu_addr_size do a similar thing
18829 so we're not in uncharted territory here.
18830 Alas we need to be a bit more complicated as addr_base is contained
18831 in the DIE.
18832
18833 We don't need to read the entire CU(/TU).
18834 We just need the header and top level die.
18835
18836 IWBN to use the aging mechanism to let us lazily later discard the CU.
18837 For now we skip this optimization. */
18838
18839 if (cu != NULL)
18840 {
18841 addr_base = cu->addr_base;
18842 addr_size = cu->header.addr_size;
18843 }
18844 else
18845 {
18846 cutu_reader reader (per_cu, NULL, 0, false);
18847 addr_base = reader.cu->addr_base;
18848 addr_size = reader.cu->header.addr_size;
18849 }
18850
18851 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18852 addr_size);
18853 }
18854
18855 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18856 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18857 DWO file. */
18858
18859 static const char *
18860 read_str_index (struct dwarf2_cu *cu,
18861 struct dwarf2_section_info *str_section,
18862 struct dwarf2_section_info *str_offsets_section,
18863 ULONGEST str_offsets_base, ULONGEST str_index)
18864 {
18865 struct dwarf2_per_objfile *dwarf2_per_objfile
18866 = cu->per_cu->dwarf2_per_objfile;
18867 struct objfile *objfile = dwarf2_per_objfile->objfile;
18868 const char *objf_name = objfile_name (objfile);
18869 bfd *abfd = objfile->obfd;
18870 const gdb_byte *info_ptr;
18871 ULONGEST str_offset;
18872 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18873
18874 str_section->read (objfile);
18875 str_offsets_section->read (objfile);
18876 if (str_section->buffer == NULL)
18877 error (_("%s used without %s section"
18878 " in CU at offset %s [in module %s]"),
18879 form_name, str_section->get_name (),
18880 sect_offset_str (cu->header.sect_off), objf_name);
18881 if (str_offsets_section->buffer == NULL)
18882 error (_("%s used without %s section"
18883 " in CU at offset %s [in module %s]"),
18884 form_name, str_section->get_name (),
18885 sect_offset_str (cu->header.sect_off), objf_name);
18886 info_ptr = (str_offsets_section->buffer
18887 + str_offsets_base
18888 + str_index * cu->header.offset_size);
18889 if (cu->header.offset_size == 4)
18890 str_offset = bfd_get_32 (abfd, info_ptr);
18891 else
18892 str_offset = bfd_get_64 (abfd, info_ptr);
18893 if (str_offset >= str_section->size)
18894 error (_("Offset from %s pointing outside of"
18895 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18896 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18897 return (const char *) (str_section->buffer + str_offset);
18898 }
18899
18900 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18901
18902 static const char *
18903 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18904 {
18905 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18906 ? reader->cu->header.addr_size : 0;
18907 return read_str_index (reader->cu,
18908 &reader->dwo_file->sections.str,
18909 &reader->dwo_file->sections.str_offsets,
18910 str_offsets_base, str_index);
18911 }
18912
18913 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18914
18915 static const char *
18916 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18917 {
18918 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18919 const char *objf_name = objfile_name (objfile);
18920 static const char form_name[] = "DW_FORM_GNU_str_index";
18921 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18922
18923 if (!cu->str_offsets_base.has_value ())
18924 error (_("%s used in Fission stub without %s"
18925 " in CU at offset 0x%lx [in module %s]"),
18926 form_name, str_offsets_attr_name,
18927 (long) cu->header.offset_size, objf_name);
18928
18929 return read_str_index (cu,
18930 &cu->per_cu->dwarf2_per_objfile->str,
18931 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18932 *cu->str_offsets_base, str_index);
18933 }
18934
18935 /* Return the length of an LEB128 number in BUF. */
18936
18937 static int
18938 leb128_size (const gdb_byte *buf)
18939 {
18940 const gdb_byte *begin = buf;
18941 gdb_byte byte;
18942
18943 while (1)
18944 {
18945 byte = *buf++;
18946 if ((byte & 128) == 0)
18947 return buf - begin;
18948 }
18949 }
18950
18951 static void
18952 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18953 {
18954 switch (lang)
18955 {
18956 case DW_LANG_C89:
18957 case DW_LANG_C99:
18958 case DW_LANG_C11:
18959 case DW_LANG_C:
18960 case DW_LANG_UPC:
18961 cu->language = language_c;
18962 break;
18963 case DW_LANG_Java:
18964 case DW_LANG_C_plus_plus:
18965 case DW_LANG_C_plus_plus_11:
18966 case DW_LANG_C_plus_plus_14:
18967 cu->language = language_cplus;
18968 break;
18969 case DW_LANG_D:
18970 cu->language = language_d;
18971 break;
18972 case DW_LANG_Fortran77:
18973 case DW_LANG_Fortran90:
18974 case DW_LANG_Fortran95:
18975 case DW_LANG_Fortran03:
18976 case DW_LANG_Fortran08:
18977 cu->language = language_fortran;
18978 break;
18979 case DW_LANG_Go:
18980 cu->language = language_go;
18981 break;
18982 case DW_LANG_Mips_Assembler:
18983 cu->language = language_asm;
18984 break;
18985 case DW_LANG_Ada83:
18986 case DW_LANG_Ada95:
18987 cu->language = language_ada;
18988 break;
18989 case DW_LANG_Modula2:
18990 cu->language = language_m2;
18991 break;
18992 case DW_LANG_Pascal83:
18993 cu->language = language_pascal;
18994 break;
18995 case DW_LANG_ObjC:
18996 cu->language = language_objc;
18997 break;
18998 case DW_LANG_Rust:
18999 case DW_LANG_Rust_old:
19000 cu->language = language_rust;
19001 break;
19002 case DW_LANG_Cobol74:
19003 case DW_LANG_Cobol85:
19004 default:
19005 cu->language = language_minimal;
19006 break;
19007 }
19008 cu->language_defn = language_def (cu->language);
19009 }
19010
19011 /* Return the named attribute or NULL if not there. */
19012
19013 static struct attribute *
19014 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19015 {
19016 for (;;)
19017 {
19018 unsigned int i;
19019 struct attribute *spec = NULL;
19020
19021 for (i = 0; i < die->num_attrs; ++i)
19022 {
19023 if (die->attrs[i].name == name)
19024 return &die->attrs[i];
19025 if (die->attrs[i].name == DW_AT_specification
19026 || die->attrs[i].name == DW_AT_abstract_origin)
19027 spec = &die->attrs[i];
19028 }
19029
19030 if (!spec)
19031 break;
19032
19033 die = follow_die_ref (die, spec, &cu);
19034 }
19035
19036 return NULL;
19037 }
19038
19039 /* Return the named attribute or NULL if not there,
19040 but do not follow DW_AT_specification, etc.
19041 This is for use in contexts where we're reading .debug_types dies.
19042 Following DW_AT_specification, DW_AT_abstract_origin will take us
19043 back up the chain, and we want to go down. */
19044
19045 static struct attribute *
19046 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19047 {
19048 unsigned int i;
19049
19050 for (i = 0; i < die->num_attrs; ++i)
19051 if (die->attrs[i].name == name)
19052 return &die->attrs[i];
19053
19054 return NULL;
19055 }
19056
19057 /* Return the string associated with a string-typed attribute, or NULL if it
19058 is either not found or is of an incorrect type. */
19059
19060 static const char *
19061 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19062 {
19063 struct attribute *attr;
19064 const char *str = NULL;
19065
19066 attr = dwarf2_attr (die, name, cu);
19067
19068 if (attr != NULL)
19069 {
19070 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19071 || attr->form == DW_FORM_string
19072 || attr->form == DW_FORM_strx
19073 || attr->form == DW_FORM_strx1
19074 || attr->form == DW_FORM_strx2
19075 || attr->form == DW_FORM_strx3
19076 || attr->form == DW_FORM_strx4
19077 || attr->form == DW_FORM_GNU_str_index
19078 || attr->form == DW_FORM_GNU_strp_alt)
19079 str = DW_STRING (attr);
19080 else
19081 complaint (_("string type expected for attribute %s for "
19082 "DIE at %s in module %s"),
19083 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19084 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19085 }
19086
19087 return str;
19088 }
19089
19090 /* Return the dwo name or NULL if not present. If present, it is in either
19091 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19092 static const char *
19093 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19094 {
19095 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19096 if (dwo_name == nullptr)
19097 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19098 return dwo_name;
19099 }
19100
19101 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19102 and holds a non-zero value. This function should only be used for
19103 DW_FORM_flag or DW_FORM_flag_present attributes. */
19104
19105 static int
19106 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19107 {
19108 struct attribute *attr = dwarf2_attr (die, name, cu);
19109
19110 return (attr && DW_UNSND (attr));
19111 }
19112
19113 static int
19114 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19115 {
19116 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19117 which value is non-zero. However, we have to be careful with
19118 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19119 (via dwarf2_flag_true_p) follows this attribute. So we may
19120 end up accidently finding a declaration attribute that belongs
19121 to a different DIE referenced by the specification attribute,
19122 even though the given DIE does not have a declaration attribute. */
19123 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19124 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19125 }
19126
19127 /* Return the die giving the specification for DIE, if there is
19128 one. *SPEC_CU is the CU containing DIE on input, and the CU
19129 containing the return value on output. If there is no
19130 specification, but there is an abstract origin, that is
19131 returned. */
19132
19133 static struct die_info *
19134 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19135 {
19136 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19137 *spec_cu);
19138
19139 if (spec_attr == NULL)
19140 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19141
19142 if (spec_attr == NULL)
19143 return NULL;
19144 else
19145 return follow_die_ref (die, spec_attr, spec_cu);
19146 }
19147
19148 /* Stub for free_line_header to match void * callback types. */
19149
19150 static void
19151 free_line_header_voidp (void *arg)
19152 {
19153 struct line_header *lh = (struct line_header *) arg;
19154
19155 delete lh;
19156 }
19157
19158 /* A convenience function to find the proper .debug_line section for a CU. */
19159
19160 static struct dwarf2_section_info *
19161 get_debug_line_section (struct dwarf2_cu *cu)
19162 {
19163 struct dwarf2_section_info *section;
19164 struct dwarf2_per_objfile *dwarf2_per_objfile
19165 = cu->per_cu->dwarf2_per_objfile;
19166
19167 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19168 DWO file. */
19169 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19170 section = &cu->dwo_unit->dwo_file->sections.line;
19171 else if (cu->per_cu->is_dwz)
19172 {
19173 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19174
19175 section = &dwz->line;
19176 }
19177 else
19178 section = &dwarf2_per_objfile->line;
19179
19180 return section;
19181 }
19182
19183 /* Read the statement program header starting at OFFSET in
19184 .debug_line, or .debug_line.dwo. Return a pointer
19185 to a struct line_header, allocated using xmalloc.
19186 Returns NULL if there is a problem reading the header, e.g., if it
19187 has a version we don't understand.
19188
19189 NOTE: the strings in the include directory and file name tables of
19190 the returned object point into the dwarf line section buffer,
19191 and must not be freed. */
19192
19193 static line_header_up
19194 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19195 {
19196 struct dwarf2_section_info *section;
19197 struct dwarf2_per_objfile *dwarf2_per_objfile
19198 = cu->per_cu->dwarf2_per_objfile;
19199
19200 section = get_debug_line_section (cu);
19201 section->read (dwarf2_per_objfile->objfile);
19202 if (section->buffer == NULL)
19203 {
19204 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19205 complaint (_("missing .debug_line.dwo section"));
19206 else
19207 complaint (_("missing .debug_line section"));
19208 return 0;
19209 }
19210
19211 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19212 dwarf2_per_objfile, section,
19213 &cu->header);
19214 }
19215
19216 /* Subroutine of dwarf_decode_lines to simplify it.
19217 Return the file name of the psymtab for the given file_entry.
19218 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19219 If space for the result is malloc'd, *NAME_HOLDER will be set.
19220 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19221
19222 static const char *
19223 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19224 const dwarf2_psymtab *pst,
19225 const char *comp_dir,
19226 gdb::unique_xmalloc_ptr<char> *name_holder)
19227 {
19228 const char *include_name = fe.name;
19229 const char *include_name_to_compare = include_name;
19230 const char *pst_filename;
19231 int file_is_pst;
19232
19233 const char *dir_name = fe.include_dir (lh);
19234
19235 gdb::unique_xmalloc_ptr<char> hold_compare;
19236 if (!IS_ABSOLUTE_PATH (include_name)
19237 && (dir_name != NULL || comp_dir != NULL))
19238 {
19239 /* Avoid creating a duplicate psymtab for PST.
19240 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19241 Before we do the comparison, however, we need to account
19242 for DIR_NAME and COMP_DIR.
19243 First prepend dir_name (if non-NULL). If we still don't
19244 have an absolute path prepend comp_dir (if non-NULL).
19245 However, the directory we record in the include-file's
19246 psymtab does not contain COMP_DIR (to match the
19247 corresponding symtab(s)).
19248
19249 Example:
19250
19251 bash$ cd /tmp
19252 bash$ gcc -g ./hello.c
19253 include_name = "hello.c"
19254 dir_name = "."
19255 DW_AT_comp_dir = comp_dir = "/tmp"
19256 DW_AT_name = "./hello.c"
19257
19258 */
19259
19260 if (dir_name != NULL)
19261 {
19262 name_holder->reset (concat (dir_name, SLASH_STRING,
19263 include_name, (char *) NULL));
19264 include_name = name_holder->get ();
19265 include_name_to_compare = include_name;
19266 }
19267 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19268 {
19269 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19270 include_name, (char *) NULL));
19271 include_name_to_compare = hold_compare.get ();
19272 }
19273 }
19274
19275 pst_filename = pst->filename;
19276 gdb::unique_xmalloc_ptr<char> copied_name;
19277 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19278 {
19279 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19280 pst_filename, (char *) NULL));
19281 pst_filename = copied_name.get ();
19282 }
19283
19284 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19285
19286 if (file_is_pst)
19287 return NULL;
19288 return include_name;
19289 }
19290
19291 /* State machine to track the state of the line number program. */
19292
19293 class lnp_state_machine
19294 {
19295 public:
19296 /* Initialize a machine state for the start of a line number
19297 program. */
19298 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19299 bool record_lines_p);
19300
19301 file_entry *current_file ()
19302 {
19303 /* lh->file_names is 0-based, but the file name numbers in the
19304 statement program are 1-based. */
19305 return m_line_header->file_name_at (m_file);
19306 }
19307
19308 /* Record the line in the state machine. END_SEQUENCE is true if
19309 we're processing the end of a sequence. */
19310 void record_line (bool end_sequence);
19311
19312 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19313 nop-out rest of the lines in this sequence. */
19314 void check_line_address (struct dwarf2_cu *cu,
19315 const gdb_byte *line_ptr,
19316 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19317
19318 void handle_set_discriminator (unsigned int discriminator)
19319 {
19320 m_discriminator = discriminator;
19321 m_line_has_non_zero_discriminator |= discriminator != 0;
19322 }
19323
19324 /* Handle DW_LNE_set_address. */
19325 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19326 {
19327 m_op_index = 0;
19328 address += baseaddr;
19329 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19330 }
19331
19332 /* Handle DW_LNS_advance_pc. */
19333 void handle_advance_pc (CORE_ADDR adjust);
19334
19335 /* Handle a special opcode. */
19336 void handle_special_opcode (unsigned char op_code);
19337
19338 /* Handle DW_LNS_advance_line. */
19339 void handle_advance_line (int line_delta)
19340 {
19341 advance_line (line_delta);
19342 }
19343
19344 /* Handle DW_LNS_set_file. */
19345 void handle_set_file (file_name_index file);
19346
19347 /* Handle DW_LNS_negate_stmt. */
19348 void handle_negate_stmt ()
19349 {
19350 m_is_stmt = !m_is_stmt;
19351 }
19352
19353 /* Handle DW_LNS_const_add_pc. */
19354 void handle_const_add_pc ();
19355
19356 /* Handle DW_LNS_fixed_advance_pc. */
19357 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19358 {
19359 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19360 m_op_index = 0;
19361 }
19362
19363 /* Handle DW_LNS_copy. */
19364 void handle_copy ()
19365 {
19366 record_line (false);
19367 m_discriminator = 0;
19368 }
19369
19370 /* Handle DW_LNE_end_sequence. */
19371 void handle_end_sequence ()
19372 {
19373 m_currently_recording_lines = true;
19374 }
19375
19376 private:
19377 /* Advance the line by LINE_DELTA. */
19378 void advance_line (int line_delta)
19379 {
19380 m_line += line_delta;
19381
19382 if (line_delta != 0)
19383 m_line_has_non_zero_discriminator = m_discriminator != 0;
19384 }
19385
19386 struct dwarf2_cu *m_cu;
19387
19388 gdbarch *m_gdbarch;
19389
19390 /* True if we're recording lines.
19391 Otherwise we're building partial symtabs and are just interested in
19392 finding include files mentioned by the line number program. */
19393 bool m_record_lines_p;
19394
19395 /* The line number header. */
19396 line_header *m_line_header;
19397
19398 /* These are part of the standard DWARF line number state machine,
19399 and initialized according to the DWARF spec. */
19400
19401 unsigned char m_op_index = 0;
19402 /* The line table index of the current file. */
19403 file_name_index m_file = 1;
19404 unsigned int m_line = 1;
19405
19406 /* These are initialized in the constructor. */
19407
19408 CORE_ADDR m_address;
19409 bool m_is_stmt;
19410 unsigned int m_discriminator;
19411
19412 /* Additional bits of state we need to track. */
19413
19414 /* The last file that we called dwarf2_start_subfile for.
19415 This is only used for TLLs. */
19416 unsigned int m_last_file = 0;
19417 /* The last file a line number was recorded for. */
19418 struct subfile *m_last_subfile = NULL;
19419
19420 /* When true, record the lines we decode. */
19421 bool m_currently_recording_lines = false;
19422
19423 /* The last line number that was recorded, used to coalesce
19424 consecutive entries for the same line. This can happen, for
19425 example, when discriminators are present. PR 17276. */
19426 unsigned int m_last_line = 0;
19427 bool m_line_has_non_zero_discriminator = false;
19428 };
19429
19430 void
19431 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19432 {
19433 CORE_ADDR addr_adj = (((m_op_index + adjust)
19434 / m_line_header->maximum_ops_per_instruction)
19435 * m_line_header->minimum_instruction_length);
19436 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19437 m_op_index = ((m_op_index + adjust)
19438 % m_line_header->maximum_ops_per_instruction);
19439 }
19440
19441 void
19442 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19443 {
19444 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19445 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19446 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19447 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19448 / m_line_header->maximum_ops_per_instruction)
19449 * m_line_header->minimum_instruction_length);
19450 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19451 m_op_index = ((m_op_index + adj_opcode_d)
19452 % m_line_header->maximum_ops_per_instruction);
19453
19454 int line_delta = m_line_header->line_base + adj_opcode_r;
19455 advance_line (line_delta);
19456 record_line (false);
19457 m_discriminator = 0;
19458 }
19459
19460 void
19461 lnp_state_machine::handle_set_file (file_name_index file)
19462 {
19463 m_file = file;
19464
19465 const file_entry *fe = current_file ();
19466 if (fe == NULL)
19467 dwarf2_debug_line_missing_file_complaint ();
19468 else if (m_record_lines_p)
19469 {
19470 const char *dir = fe->include_dir (m_line_header);
19471
19472 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19473 m_line_has_non_zero_discriminator = m_discriminator != 0;
19474 dwarf2_start_subfile (m_cu, fe->name, dir);
19475 }
19476 }
19477
19478 void
19479 lnp_state_machine::handle_const_add_pc ()
19480 {
19481 CORE_ADDR adjust
19482 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19483
19484 CORE_ADDR addr_adj
19485 = (((m_op_index + adjust)
19486 / m_line_header->maximum_ops_per_instruction)
19487 * m_line_header->minimum_instruction_length);
19488
19489 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19490 m_op_index = ((m_op_index + adjust)
19491 % m_line_header->maximum_ops_per_instruction);
19492 }
19493
19494 /* Return non-zero if we should add LINE to the line number table.
19495 LINE is the line to add, LAST_LINE is the last line that was added,
19496 LAST_SUBFILE is the subfile for LAST_LINE.
19497 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19498 had a non-zero discriminator.
19499
19500 We have to be careful in the presence of discriminators.
19501 E.g., for this line:
19502
19503 for (i = 0; i < 100000; i++);
19504
19505 clang can emit four line number entries for that one line,
19506 each with a different discriminator.
19507 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19508
19509 However, we want gdb to coalesce all four entries into one.
19510 Otherwise the user could stepi into the middle of the line and
19511 gdb would get confused about whether the pc really was in the
19512 middle of the line.
19513
19514 Things are further complicated by the fact that two consecutive
19515 line number entries for the same line is a heuristic used by gcc
19516 to denote the end of the prologue. So we can't just discard duplicate
19517 entries, we have to be selective about it. The heuristic we use is
19518 that we only collapse consecutive entries for the same line if at least
19519 one of those entries has a non-zero discriminator. PR 17276.
19520
19521 Note: Addresses in the line number state machine can never go backwards
19522 within one sequence, thus this coalescing is ok. */
19523
19524 static int
19525 dwarf_record_line_p (struct dwarf2_cu *cu,
19526 unsigned int line, unsigned int last_line,
19527 int line_has_non_zero_discriminator,
19528 struct subfile *last_subfile)
19529 {
19530 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19531 return 1;
19532 if (line != last_line)
19533 return 1;
19534 /* Same line for the same file that we've seen already.
19535 As a last check, for pr 17276, only record the line if the line
19536 has never had a non-zero discriminator. */
19537 if (!line_has_non_zero_discriminator)
19538 return 1;
19539 return 0;
19540 }
19541
19542 /* Use the CU's builder to record line number LINE beginning at
19543 address ADDRESS in the line table of subfile SUBFILE. */
19544
19545 static void
19546 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19547 unsigned int line, CORE_ADDR address, bool is_stmt,
19548 struct dwarf2_cu *cu)
19549 {
19550 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19551
19552 if (dwarf_line_debug)
19553 {
19554 fprintf_unfiltered (gdb_stdlog,
19555 "Recording line %u, file %s, address %s\n",
19556 line, lbasename (subfile->name),
19557 paddress (gdbarch, address));
19558 }
19559
19560 if (cu != nullptr)
19561 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19562 }
19563
19564 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19565 Mark the end of a set of line number records.
19566 The arguments are the same as for dwarf_record_line_1.
19567 If SUBFILE is NULL the request is ignored. */
19568
19569 static void
19570 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19571 CORE_ADDR address, struct dwarf2_cu *cu)
19572 {
19573 if (subfile == NULL)
19574 return;
19575
19576 if (dwarf_line_debug)
19577 {
19578 fprintf_unfiltered (gdb_stdlog,
19579 "Finishing current line, file %s, address %s\n",
19580 lbasename (subfile->name),
19581 paddress (gdbarch, address));
19582 }
19583
19584 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19585 }
19586
19587 void
19588 lnp_state_machine::record_line (bool end_sequence)
19589 {
19590 if (dwarf_line_debug)
19591 {
19592 fprintf_unfiltered (gdb_stdlog,
19593 "Processing actual line %u: file %u,"
19594 " address %s, is_stmt %u, discrim %u%s\n",
19595 m_line, m_file,
19596 paddress (m_gdbarch, m_address),
19597 m_is_stmt, m_discriminator,
19598 (end_sequence ? "\t(end sequence)" : ""));
19599 }
19600
19601 file_entry *fe = current_file ();
19602
19603 if (fe == NULL)
19604 dwarf2_debug_line_missing_file_complaint ();
19605 /* For now we ignore lines not starting on an instruction boundary.
19606 But not when processing end_sequence for compatibility with the
19607 previous version of the code. */
19608 else if (m_op_index == 0 || end_sequence)
19609 {
19610 fe->included_p = 1;
19611 if (m_record_lines_p)
19612 {
19613 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19614 || end_sequence)
19615 {
19616 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19617 m_currently_recording_lines ? m_cu : nullptr);
19618 }
19619
19620 if (!end_sequence)
19621 {
19622 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19623
19624 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19625 m_line_has_non_zero_discriminator,
19626 m_last_subfile))
19627 {
19628 buildsym_compunit *builder = m_cu->get_builder ();
19629 dwarf_record_line_1 (m_gdbarch,
19630 builder->get_current_subfile (),
19631 m_line, m_address, is_stmt,
19632 m_currently_recording_lines ? m_cu : nullptr);
19633 }
19634 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19635 m_last_line = m_line;
19636 }
19637 }
19638 }
19639 }
19640
19641 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19642 line_header *lh, bool record_lines_p)
19643 {
19644 m_cu = cu;
19645 m_gdbarch = arch;
19646 m_record_lines_p = record_lines_p;
19647 m_line_header = lh;
19648
19649 m_currently_recording_lines = true;
19650
19651 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19652 was a line entry for it so that the backend has a chance to adjust it
19653 and also record it in case it needs it. This is currently used by MIPS
19654 code, cf. `mips_adjust_dwarf2_line'. */
19655 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19656 m_is_stmt = lh->default_is_stmt;
19657 m_discriminator = 0;
19658 }
19659
19660 void
19661 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19662 const gdb_byte *line_ptr,
19663 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19664 {
19665 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19666 the pc range of the CU. However, we restrict the test to only ADDRESS
19667 values of zero to preserve GDB's previous behaviour which is to handle
19668 the specific case of a function being GC'd by the linker. */
19669
19670 if (address == 0 && address < unrelocated_lowpc)
19671 {
19672 /* This line table is for a function which has been
19673 GCd by the linker. Ignore it. PR gdb/12528 */
19674
19675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19676 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19677
19678 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19679 line_offset, objfile_name (objfile));
19680 m_currently_recording_lines = false;
19681 /* Note: m_currently_recording_lines is left as false until we see
19682 DW_LNE_end_sequence. */
19683 }
19684 }
19685
19686 /* Subroutine of dwarf_decode_lines to simplify it.
19687 Process the line number information in LH.
19688 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19689 program in order to set included_p for every referenced header. */
19690
19691 static void
19692 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19693 const int decode_for_pst_p, CORE_ADDR lowpc)
19694 {
19695 const gdb_byte *line_ptr, *extended_end;
19696 const gdb_byte *line_end;
19697 unsigned int bytes_read, extended_len;
19698 unsigned char op_code, extended_op;
19699 CORE_ADDR baseaddr;
19700 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19701 bfd *abfd = objfile->obfd;
19702 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19703 /* True if we're recording line info (as opposed to building partial
19704 symtabs and just interested in finding include files mentioned by
19705 the line number program). */
19706 bool record_lines_p = !decode_for_pst_p;
19707
19708 baseaddr = objfile->text_section_offset ();
19709
19710 line_ptr = lh->statement_program_start;
19711 line_end = lh->statement_program_end;
19712
19713 /* Read the statement sequences until there's nothing left. */
19714 while (line_ptr < line_end)
19715 {
19716 /* The DWARF line number program state machine. Reset the state
19717 machine at the start of each sequence. */
19718 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19719 bool end_sequence = false;
19720
19721 if (record_lines_p)
19722 {
19723 /* Start a subfile for the current file of the state
19724 machine. */
19725 const file_entry *fe = state_machine.current_file ();
19726
19727 if (fe != NULL)
19728 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19729 }
19730
19731 /* Decode the table. */
19732 while (line_ptr < line_end && !end_sequence)
19733 {
19734 op_code = read_1_byte (abfd, line_ptr);
19735 line_ptr += 1;
19736
19737 if (op_code >= lh->opcode_base)
19738 {
19739 /* Special opcode. */
19740 state_machine.handle_special_opcode (op_code);
19741 }
19742 else switch (op_code)
19743 {
19744 case DW_LNS_extended_op:
19745 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19746 &bytes_read);
19747 line_ptr += bytes_read;
19748 extended_end = line_ptr + extended_len;
19749 extended_op = read_1_byte (abfd, line_ptr);
19750 line_ptr += 1;
19751 switch (extended_op)
19752 {
19753 case DW_LNE_end_sequence:
19754 state_machine.handle_end_sequence ();
19755 end_sequence = true;
19756 break;
19757 case DW_LNE_set_address:
19758 {
19759 CORE_ADDR address
19760 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19761 line_ptr += bytes_read;
19762
19763 state_machine.check_line_address (cu, line_ptr,
19764 lowpc - baseaddr, address);
19765 state_machine.handle_set_address (baseaddr, address);
19766 }
19767 break;
19768 case DW_LNE_define_file:
19769 {
19770 const char *cur_file;
19771 unsigned int mod_time, length;
19772 dir_index dindex;
19773
19774 cur_file = read_direct_string (abfd, line_ptr,
19775 &bytes_read);
19776 line_ptr += bytes_read;
19777 dindex = (dir_index)
19778 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19779 line_ptr += bytes_read;
19780 mod_time =
19781 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19782 line_ptr += bytes_read;
19783 length =
19784 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19785 line_ptr += bytes_read;
19786 lh->add_file_name (cur_file, dindex, mod_time, length);
19787 }
19788 break;
19789 case DW_LNE_set_discriminator:
19790 {
19791 /* The discriminator is not interesting to the
19792 debugger; just ignore it. We still need to
19793 check its value though:
19794 if there are consecutive entries for the same
19795 (non-prologue) line we want to coalesce them.
19796 PR 17276. */
19797 unsigned int discr
19798 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19799 line_ptr += bytes_read;
19800
19801 state_machine.handle_set_discriminator (discr);
19802 }
19803 break;
19804 default:
19805 complaint (_("mangled .debug_line section"));
19806 return;
19807 }
19808 /* Make sure that we parsed the extended op correctly. If e.g.
19809 we expected a different address size than the producer used,
19810 we may have read the wrong number of bytes. */
19811 if (line_ptr != extended_end)
19812 {
19813 complaint (_("mangled .debug_line section"));
19814 return;
19815 }
19816 break;
19817 case DW_LNS_copy:
19818 state_machine.handle_copy ();
19819 break;
19820 case DW_LNS_advance_pc:
19821 {
19822 CORE_ADDR adjust
19823 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19824 line_ptr += bytes_read;
19825
19826 state_machine.handle_advance_pc (adjust);
19827 }
19828 break;
19829 case DW_LNS_advance_line:
19830 {
19831 int line_delta
19832 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19833 line_ptr += bytes_read;
19834
19835 state_machine.handle_advance_line (line_delta);
19836 }
19837 break;
19838 case DW_LNS_set_file:
19839 {
19840 file_name_index file
19841 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19842 &bytes_read);
19843 line_ptr += bytes_read;
19844
19845 state_machine.handle_set_file (file);
19846 }
19847 break;
19848 case DW_LNS_set_column:
19849 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19850 line_ptr += bytes_read;
19851 break;
19852 case DW_LNS_negate_stmt:
19853 state_machine.handle_negate_stmt ();
19854 break;
19855 case DW_LNS_set_basic_block:
19856 break;
19857 /* Add to the address register of the state machine the
19858 address increment value corresponding to special opcode
19859 255. I.e., this value is scaled by the minimum
19860 instruction length since special opcode 255 would have
19861 scaled the increment. */
19862 case DW_LNS_const_add_pc:
19863 state_machine.handle_const_add_pc ();
19864 break;
19865 case DW_LNS_fixed_advance_pc:
19866 {
19867 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19868 line_ptr += 2;
19869
19870 state_machine.handle_fixed_advance_pc (addr_adj);
19871 }
19872 break;
19873 default:
19874 {
19875 /* Unknown standard opcode, ignore it. */
19876 int i;
19877
19878 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19879 {
19880 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19881 line_ptr += bytes_read;
19882 }
19883 }
19884 }
19885 }
19886
19887 if (!end_sequence)
19888 dwarf2_debug_line_missing_end_sequence_complaint ();
19889
19890 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19891 in which case we still finish recording the last line). */
19892 state_machine.record_line (true);
19893 }
19894 }
19895
19896 /* Decode the Line Number Program (LNP) for the given line_header
19897 structure and CU. The actual information extracted and the type
19898 of structures created from the LNP depends on the value of PST.
19899
19900 1. If PST is NULL, then this procedure uses the data from the program
19901 to create all necessary symbol tables, and their linetables.
19902
19903 2. If PST is not NULL, this procedure reads the program to determine
19904 the list of files included by the unit represented by PST, and
19905 builds all the associated partial symbol tables.
19906
19907 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19908 It is used for relative paths in the line table.
19909 NOTE: When processing partial symtabs (pst != NULL),
19910 comp_dir == pst->dirname.
19911
19912 NOTE: It is important that psymtabs have the same file name (via strcmp)
19913 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19914 symtab we don't use it in the name of the psymtabs we create.
19915 E.g. expand_line_sal requires this when finding psymtabs to expand.
19916 A good testcase for this is mb-inline.exp.
19917
19918 LOWPC is the lowest address in CU (or 0 if not known).
19919
19920 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19921 for its PC<->lines mapping information. Otherwise only the filename
19922 table is read in. */
19923
19924 static void
19925 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19926 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19927 CORE_ADDR lowpc, int decode_mapping)
19928 {
19929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19930 const int decode_for_pst_p = (pst != NULL);
19931
19932 if (decode_mapping)
19933 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19934
19935 if (decode_for_pst_p)
19936 {
19937 /* Now that we're done scanning the Line Header Program, we can
19938 create the psymtab of each included file. */
19939 for (auto &file_entry : lh->file_names ())
19940 if (file_entry.included_p == 1)
19941 {
19942 gdb::unique_xmalloc_ptr<char> name_holder;
19943 const char *include_name =
19944 psymtab_include_file_name (lh, file_entry, pst,
19945 comp_dir, &name_holder);
19946 if (include_name != NULL)
19947 dwarf2_create_include_psymtab (include_name, pst, objfile);
19948 }
19949 }
19950 else
19951 {
19952 /* Make sure a symtab is created for every file, even files
19953 which contain only variables (i.e. no code with associated
19954 line numbers). */
19955 buildsym_compunit *builder = cu->get_builder ();
19956 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19957
19958 for (auto &fe : lh->file_names ())
19959 {
19960 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19961 if (builder->get_current_subfile ()->symtab == NULL)
19962 {
19963 builder->get_current_subfile ()->symtab
19964 = allocate_symtab (cust,
19965 builder->get_current_subfile ()->name);
19966 }
19967 fe.symtab = builder->get_current_subfile ()->symtab;
19968 }
19969 }
19970 }
19971
19972 /* Start a subfile for DWARF. FILENAME is the name of the file and
19973 DIRNAME the name of the source directory which contains FILENAME
19974 or NULL if not known.
19975 This routine tries to keep line numbers from identical absolute and
19976 relative file names in a common subfile.
19977
19978 Using the `list' example from the GDB testsuite, which resides in
19979 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19980 of /srcdir/list0.c yields the following debugging information for list0.c:
19981
19982 DW_AT_name: /srcdir/list0.c
19983 DW_AT_comp_dir: /compdir
19984 files.files[0].name: list0.h
19985 files.files[0].dir: /srcdir
19986 files.files[1].name: list0.c
19987 files.files[1].dir: /srcdir
19988
19989 The line number information for list0.c has to end up in a single
19990 subfile, so that `break /srcdir/list0.c:1' works as expected.
19991 start_subfile will ensure that this happens provided that we pass the
19992 concatenation of files.files[1].dir and files.files[1].name as the
19993 subfile's name. */
19994
19995 static void
19996 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19997 const char *dirname)
19998 {
19999 gdb::unique_xmalloc_ptr<char> copy;
20000
20001 /* In order not to lose the line information directory,
20002 we concatenate it to the filename when it makes sense.
20003 Note that the Dwarf3 standard says (speaking of filenames in line
20004 information): ``The directory index is ignored for file names
20005 that represent full path names''. Thus ignoring dirname in the
20006 `else' branch below isn't an issue. */
20007
20008 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20009 {
20010 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20011 filename = copy.get ();
20012 }
20013
20014 cu->get_builder ()->start_subfile (filename);
20015 }
20016
20017 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20018 buildsym_compunit constructor. */
20019
20020 struct compunit_symtab *
20021 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20022 CORE_ADDR low_pc)
20023 {
20024 gdb_assert (m_builder == nullptr);
20025
20026 m_builder.reset (new struct buildsym_compunit
20027 (per_cu->dwarf2_per_objfile->objfile,
20028 name, comp_dir, language, low_pc));
20029
20030 list_in_scope = get_builder ()->get_file_symbols ();
20031
20032 get_builder ()->record_debugformat ("DWARF 2");
20033 get_builder ()->record_producer (producer);
20034
20035 processing_has_namespace_info = false;
20036
20037 return get_builder ()->get_compunit_symtab ();
20038 }
20039
20040 static void
20041 var_decode_location (struct attribute *attr, struct symbol *sym,
20042 struct dwarf2_cu *cu)
20043 {
20044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20045 struct comp_unit_head *cu_header = &cu->header;
20046
20047 /* NOTE drow/2003-01-30: There used to be a comment and some special
20048 code here to turn a symbol with DW_AT_external and a
20049 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20050 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20051 with some versions of binutils) where shared libraries could have
20052 relocations against symbols in their debug information - the
20053 minimal symbol would have the right address, but the debug info
20054 would not. It's no longer necessary, because we will explicitly
20055 apply relocations when we read in the debug information now. */
20056
20057 /* A DW_AT_location attribute with no contents indicates that a
20058 variable has been optimized away. */
20059 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20060 {
20061 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20062 return;
20063 }
20064
20065 /* Handle one degenerate form of location expression specially, to
20066 preserve GDB's previous behavior when section offsets are
20067 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20068 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20069
20070 if (attr->form_is_block ()
20071 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20072 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20073 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20074 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20075 && (DW_BLOCK (attr)->size
20076 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20077 {
20078 unsigned int dummy;
20079
20080 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20081 SET_SYMBOL_VALUE_ADDRESS
20082 (sym, cu->header.read_address (objfile->obfd,
20083 DW_BLOCK (attr)->data + 1,
20084 &dummy));
20085 else
20086 SET_SYMBOL_VALUE_ADDRESS
20087 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20088 &dummy));
20089 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20090 fixup_symbol_section (sym, objfile);
20091 SET_SYMBOL_VALUE_ADDRESS
20092 (sym,
20093 SYMBOL_VALUE_ADDRESS (sym)
20094 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20095 return;
20096 }
20097
20098 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20099 expression evaluator, and use LOC_COMPUTED only when necessary
20100 (i.e. when the value of a register or memory location is
20101 referenced, or a thread-local block, etc.). Then again, it might
20102 not be worthwhile. I'm assuming that it isn't unless performance
20103 or memory numbers show me otherwise. */
20104
20105 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20106
20107 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20108 cu->has_loclist = true;
20109 }
20110
20111 /* Given a pointer to a DWARF information entry, figure out if we need
20112 to make a symbol table entry for it, and if so, create a new entry
20113 and return a pointer to it.
20114 If TYPE is NULL, determine symbol type from the die, otherwise
20115 used the passed type.
20116 If SPACE is not NULL, use it to hold the new symbol. If it is
20117 NULL, allocate a new symbol on the objfile's obstack. */
20118
20119 static struct symbol *
20120 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20121 struct symbol *space)
20122 {
20123 struct dwarf2_per_objfile *dwarf2_per_objfile
20124 = cu->per_cu->dwarf2_per_objfile;
20125 struct objfile *objfile = dwarf2_per_objfile->objfile;
20126 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20127 struct symbol *sym = NULL;
20128 const char *name;
20129 struct attribute *attr = NULL;
20130 struct attribute *attr2 = NULL;
20131 CORE_ADDR baseaddr;
20132 struct pending **list_to_add = NULL;
20133
20134 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20135
20136 baseaddr = objfile->text_section_offset ();
20137
20138 name = dwarf2_name (die, cu);
20139 if (name)
20140 {
20141 const char *linkagename;
20142 int suppress_add = 0;
20143
20144 if (space)
20145 sym = space;
20146 else
20147 sym = allocate_symbol (objfile);
20148 OBJSTAT (objfile, n_syms++);
20149
20150 /* Cache this symbol's name and the name's demangled form (if any). */
20151 sym->set_language (cu->language, &objfile->objfile_obstack);
20152 linkagename = dwarf2_physname (name, die, cu);
20153 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20154
20155 /* Fortran does not have mangling standard and the mangling does differ
20156 between gfortran, iFort etc. */
20157 if (cu->language == language_fortran
20158 && symbol_get_demangled_name (sym) == NULL)
20159 symbol_set_demangled_name (sym,
20160 dwarf2_full_name (name, die, cu),
20161 NULL);
20162
20163 /* Default assumptions.
20164 Use the passed type or decode it from the die. */
20165 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20166 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20167 if (type != NULL)
20168 SYMBOL_TYPE (sym) = type;
20169 else
20170 SYMBOL_TYPE (sym) = die_type (die, cu);
20171 attr = dwarf2_attr (die,
20172 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20173 cu);
20174 if (attr != nullptr)
20175 {
20176 SYMBOL_LINE (sym) = DW_UNSND (attr);
20177 }
20178
20179 attr = dwarf2_attr (die,
20180 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20181 cu);
20182 if (attr != nullptr)
20183 {
20184 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20185 struct file_entry *fe;
20186
20187 if (cu->line_header != NULL)
20188 fe = cu->line_header->file_name_at (file_index);
20189 else
20190 fe = NULL;
20191
20192 if (fe == NULL)
20193 complaint (_("file index out of range"));
20194 else
20195 symbol_set_symtab (sym, fe->symtab);
20196 }
20197
20198 switch (die->tag)
20199 {
20200 case DW_TAG_label:
20201 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20202 if (attr != nullptr)
20203 {
20204 CORE_ADDR addr;
20205
20206 addr = attr->value_as_address ();
20207 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20208 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20209 }
20210 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20211 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20212 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20213 add_symbol_to_list (sym, cu->list_in_scope);
20214 break;
20215 case DW_TAG_subprogram:
20216 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20217 finish_block. */
20218 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20219 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20220 if ((attr2 && (DW_UNSND (attr2) != 0))
20221 || cu->language == language_ada
20222 || cu->language == language_fortran)
20223 {
20224 /* Subprograms marked external are stored as a global symbol.
20225 Ada and Fortran subprograms, whether marked external or
20226 not, are always stored as a global symbol, because we want
20227 to be able to access them globally. For instance, we want
20228 to be able to break on a nested subprogram without having
20229 to specify the context. */
20230 list_to_add = cu->get_builder ()->get_global_symbols ();
20231 }
20232 else
20233 {
20234 list_to_add = cu->list_in_scope;
20235 }
20236 break;
20237 case DW_TAG_inlined_subroutine:
20238 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20239 finish_block. */
20240 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20241 SYMBOL_INLINED (sym) = 1;
20242 list_to_add = cu->list_in_scope;
20243 break;
20244 case DW_TAG_template_value_param:
20245 suppress_add = 1;
20246 /* Fall through. */
20247 case DW_TAG_constant:
20248 case DW_TAG_variable:
20249 case DW_TAG_member:
20250 /* Compilation with minimal debug info may result in
20251 variables with missing type entries. Change the
20252 misleading `void' type to something sensible. */
20253 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20254 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20255
20256 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20257 /* In the case of DW_TAG_member, we should only be called for
20258 static const members. */
20259 if (die->tag == DW_TAG_member)
20260 {
20261 /* dwarf2_add_field uses die_is_declaration,
20262 so we do the same. */
20263 gdb_assert (die_is_declaration (die, cu));
20264 gdb_assert (attr);
20265 }
20266 if (attr != nullptr)
20267 {
20268 dwarf2_const_value (attr, sym, cu);
20269 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20270 if (!suppress_add)
20271 {
20272 if (attr2 && (DW_UNSND (attr2) != 0))
20273 list_to_add = cu->get_builder ()->get_global_symbols ();
20274 else
20275 list_to_add = cu->list_in_scope;
20276 }
20277 break;
20278 }
20279 attr = dwarf2_attr (die, DW_AT_location, cu);
20280 if (attr != nullptr)
20281 {
20282 var_decode_location (attr, sym, cu);
20283 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20284
20285 /* Fortran explicitly imports any global symbols to the local
20286 scope by DW_TAG_common_block. */
20287 if (cu->language == language_fortran && die->parent
20288 && die->parent->tag == DW_TAG_common_block)
20289 attr2 = NULL;
20290
20291 if (SYMBOL_CLASS (sym) == LOC_STATIC
20292 && SYMBOL_VALUE_ADDRESS (sym) == 0
20293 && !dwarf2_per_objfile->has_section_at_zero)
20294 {
20295 /* When a static variable is eliminated by the linker,
20296 the corresponding debug information is not stripped
20297 out, but the variable address is set to null;
20298 do not add such variables into symbol table. */
20299 }
20300 else if (attr2 && (DW_UNSND (attr2) != 0))
20301 {
20302 if (SYMBOL_CLASS (sym) == LOC_STATIC
20303 && (objfile->flags & OBJF_MAINLINE) == 0
20304 && dwarf2_per_objfile->can_copy)
20305 {
20306 /* A global static variable might be subject to
20307 copy relocation. We first check for a local
20308 minsym, though, because maybe the symbol was
20309 marked hidden, in which case this would not
20310 apply. */
20311 bound_minimal_symbol found
20312 = (lookup_minimal_symbol_linkage
20313 (sym->linkage_name (), objfile));
20314 if (found.minsym != nullptr)
20315 sym->maybe_copied = 1;
20316 }
20317
20318 /* A variable with DW_AT_external is never static,
20319 but it may be block-scoped. */
20320 list_to_add
20321 = ((cu->list_in_scope
20322 == cu->get_builder ()->get_file_symbols ())
20323 ? cu->get_builder ()->get_global_symbols ()
20324 : cu->list_in_scope);
20325 }
20326 else
20327 list_to_add = cu->list_in_scope;
20328 }
20329 else
20330 {
20331 /* We do not know the address of this symbol.
20332 If it is an external symbol and we have type information
20333 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20334 The address of the variable will then be determined from
20335 the minimal symbol table whenever the variable is
20336 referenced. */
20337 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20338
20339 /* Fortran explicitly imports any global symbols to the local
20340 scope by DW_TAG_common_block. */
20341 if (cu->language == language_fortran && die->parent
20342 && die->parent->tag == DW_TAG_common_block)
20343 {
20344 /* SYMBOL_CLASS doesn't matter here because
20345 read_common_block is going to reset it. */
20346 if (!suppress_add)
20347 list_to_add = cu->list_in_scope;
20348 }
20349 else if (attr2 && (DW_UNSND (attr2) != 0)
20350 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20351 {
20352 /* A variable with DW_AT_external is never static, but it
20353 may be block-scoped. */
20354 list_to_add
20355 = ((cu->list_in_scope
20356 == cu->get_builder ()->get_file_symbols ())
20357 ? cu->get_builder ()->get_global_symbols ()
20358 : cu->list_in_scope);
20359
20360 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20361 }
20362 else if (!die_is_declaration (die, cu))
20363 {
20364 /* Use the default LOC_OPTIMIZED_OUT class. */
20365 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20366 if (!suppress_add)
20367 list_to_add = cu->list_in_scope;
20368 }
20369 }
20370 break;
20371 case DW_TAG_formal_parameter:
20372 {
20373 /* If we are inside a function, mark this as an argument. If
20374 not, we might be looking at an argument to an inlined function
20375 when we do not have enough information to show inlined frames;
20376 pretend it's a local variable in that case so that the user can
20377 still see it. */
20378 struct context_stack *curr
20379 = cu->get_builder ()->get_current_context_stack ();
20380 if (curr != nullptr && curr->name != nullptr)
20381 SYMBOL_IS_ARGUMENT (sym) = 1;
20382 attr = dwarf2_attr (die, DW_AT_location, cu);
20383 if (attr != nullptr)
20384 {
20385 var_decode_location (attr, sym, cu);
20386 }
20387 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20388 if (attr != nullptr)
20389 {
20390 dwarf2_const_value (attr, sym, cu);
20391 }
20392
20393 list_to_add = cu->list_in_scope;
20394 }
20395 break;
20396 case DW_TAG_unspecified_parameters:
20397 /* From varargs functions; gdb doesn't seem to have any
20398 interest in this information, so just ignore it for now.
20399 (FIXME?) */
20400 break;
20401 case DW_TAG_template_type_param:
20402 suppress_add = 1;
20403 /* Fall through. */
20404 case DW_TAG_class_type:
20405 case DW_TAG_interface_type:
20406 case DW_TAG_structure_type:
20407 case DW_TAG_union_type:
20408 case DW_TAG_set_type:
20409 case DW_TAG_enumeration_type:
20410 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20411 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20412
20413 {
20414 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20415 really ever be static objects: otherwise, if you try
20416 to, say, break of a class's method and you're in a file
20417 which doesn't mention that class, it won't work unless
20418 the check for all static symbols in lookup_symbol_aux
20419 saves you. See the OtherFileClass tests in
20420 gdb.c++/namespace.exp. */
20421
20422 if (!suppress_add)
20423 {
20424 buildsym_compunit *builder = cu->get_builder ();
20425 list_to_add
20426 = (cu->list_in_scope == builder->get_file_symbols ()
20427 && cu->language == language_cplus
20428 ? builder->get_global_symbols ()
20429 : cu->list_in_scope);
20430
20431 /* The semantics of C++ state that "struct foo {
20432 ... }" also defines a typedef for "foo". */
20433 if (cu->language == language_cplus
20434 || cu->language == language_ada
20435 || cu->language == language_d
20436 || cu->language == language_rust)
20437 {
20438 /* The symbol's name is already allocated along
20439 with this objfile, so we don't need to
20440 duplicate it for the type. */
20441 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20442 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20443 }
20444 }
20445 }
20446 break;
20447 case DW_TAG_typedef:
20448 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20449 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20450 list_to_add = cu->list_in_scope;
20451 break;
20452 case DW_TAG_base_type:
20453 case DW_TAG_subrange_type:
20454 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20455 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20456 list_to_add = cu->list_in_scope;
20457 break;
20458 case DW_TAG_enumerator:
20459 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20460 if (attr != nullptr)
20461 {
20462 dwarf2_const_value (attr, sym, cu);
20463 }
20464 {
20465 /* NOTE: carlton/2003-11-10: See comment above in the
20466 DW_TAG_class_type, etc. block. */
20467
20468 list_to_add
20469 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20470 && cu->language == language_cplus
20471 ? cu->get_builder ()->get_global_symbols ()
20472 : cu->list_in_scope);
20473 }
20474 break;
20475 case DW_TAG_imported_declaration:
20476 case DW_TAG_namespace:
20477 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20478 list_to_add = cu->get_builder ()->get_global_symbols ();
20479 break;
20480 case DW_TAG_module:
20481 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20482 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20483 list_to_add = cu->get_builder ()->get_global_symbols ();
20484 break;
20485 case DW_TAG_common_block:
20486 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20487 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20488 add_symbol_to_list (sym, cu->list_in_scope);
20489 break;
20490 default:
20491 /* Not a tag we recognize. Hopefully we aren't processing
20492 trash data, but since we must specifically ignore things
20493 we don't recognize, there is nothing else we should do at
20494 this point. */
20495 complaint (_("unsupported tag: '%s'"),
20496 dwarf_tag_name (die->tag));
20497 break;
20498 }
20499
20500 if (suppress_add)
20501 {
20502 sym->hash_next = objfile->template_symbols;
20503 objfile->template_symbols = sym;
20504 list_to_add = NULL;
20505 }
20506
20507 if (list_to_add != NULL)
20508 add_symbol_to_list (sym, list_to_add);
20509
20510 /* For the benefit of old versions of GCC, check for anonymous
20511 namespaces based on the demangled name. */
20512 if (!cu->processing_has_namespace_info
20513 && cu->language == language_cplus)
20514 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20515 }
20516 return (sym);
20517 }
20518
20519 /* Given an attr with a DW_FORM_dataN value in host byte order,
20520 zero-extend it as appropriate for the symbol's type. The DWARF
20521 standard (v4) is not entirely clear about the meaning of using
20522 DW_FORM_dataN for a constant with a signed type, where the type is
20523 wider than the data. The conclusion of a discussion on the DWARF
20524 list was that this is unspecified. We choose to always zero-extend
20525 because that is the interpretation long in use by GCC. */
20526
20527 static gdb_byte *
20528 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20529 struct dwarf2_cu *cu, LONGEST *value, int bits)
20530 {
20531 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20532 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20533 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20534 LONGEST l = DW_UNSND (attr);
20535
20536 if (bits < sizeof (*value) * 8)
20537 {
20538 l &= ((LONGEST) 1 << bits) - 1;
20539 *value = l;
20540 }
20541 else if (bits == sizeof (*value) * 8)
20542 *value = l;
20543 else
20544 {
20545 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20546 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20547 return bytes;
20548 }
20549
20550 return NULL;
20551 }
20552
20553 /* Read a constant value from an attribute. Either set *VALUE, or if
20554 the value does not fit in *VALUE, set *BYTES - either already
20555 allocated on the objfile obstack, or newly allocated on OBSTACK,
20556 or, set *BATON, if we translated the constant to a location
20557 expression. */
20558
20559 static void
20560 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20561 const char *name, struct obstack *obstack,
20562 struct dwarf2_cu *cu,
20563 LONGEST *value, const gdb_byte **bytes,
20564 struct dwarf2_locexpr_baton **baton)
20565 {
20566 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20567 struct comp_unit_head *cu_header = &cu->header;
20568 struct dwarf_block *blk;
20569 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20570 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20571
20572 *value = 0;
20573 *bytes = NULL;
20574 *baton = NULL;
20575
20576 switch (attr->form)
20577 {
20578 case DW_FORM_addr:
20579 case DW_FORM_addrx:
20580 case DW_FORM_GNU_addr_index:
20581 {
20582 gdb_byte *data;
20583
20584 if (TYPE_LENGTH (type) != cu_header->addr_size)
20585 dwarf2_const_value_length_mismatch_complaint (name,
20586 cu_header->addr_size,
20587 TYPE_LENGTH (type));
20588 /* Symbols of this form are reasonably rare, so we just
20589 piggyback on the existing location code rather than writing
20590 a new implementation of symbol_computed_ops. */
20591 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20592 (*baton)->per_cu = cu->per_cu;
20593 gdb_assert ((*baton)->per_cu);
20594
20595 (*baton)->size = 2 + cu_header->addr_size;
20596 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20597 (*baton)->data = data;
20598
20599 data[0] = DW_OP_addr;
20600 store_unsigned_integer (&data[1], cu_header->addr_size,
20601 byte_order, DW_ADDR (attr));
20602 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20603 }
20604 break;
20605 case DW_FORM_string:
20606 case DW_FORM_strp:
20607 case DW_FORM_strx:
20608 case DW_FORM_GNU_str_index:
20609 case DW_FORM_GNU_strp_alt:
20610 /* DW_STRING is already allocated on the objfile obstack, point
20611 directly to it. */
20612 *bytes = (const gdb_byte *) DW_STRING (attr);
20613 break;
20614 case DW_FORM_block1:
20615 case DW_FORM_block2:
20616 case DW_FORM_block4:
20617 case DW_FORM_block:
20618 case DW_FORM_exprloc:
20619 case DW_FORM_data16:
20620 blk = DW_BLOCK (attr);
20621 if (TYPE_LENGTH (type) != blk->size)
20622 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20623 TYPE_LENGTH (type));
20624 *bytes = blk->data;
20625 break;
20626
20627 /* The DW_AT_const_value attributes are supposed to carry the
20628 symbol's value "represented as it would be on the target
20629 architecture." By the time we get here, it's already been
20630 converted to host endianness, so we just need to sign- or
20631 zero-extend it as appropriate. */
20632 case DW_FORM_data1:
20633 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20634 break;
20635 case DW_FORM_data2:
20636 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20637 break;
20638 case DW_FORM_data4:
20639 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20640 break;
20641 case DW_FORM_data8:
20642 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20643 break;
20644
20645 case DW_FORM_sdata:
20646 case DW_FORM_implicit_const:
20647 *value = DW_SND (attr);
20648 break;
20649
20650 case DW_FORM_udata:
20651 *value = DW_UNSND (attr);
20652 break;
20653
20654 default:
20655 complaint (_("unsupported const value attribute form: '%s'"),
20656 dwarf_form_name (attr->form));
20657 *value = 0;
20658 break;
20659 }
20660 }
20661
20662
20663 /* Copy constant value from an attribute to a symbol. */
20664
20665 static void
20666 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20667 struct dwarf2_cu *cu)
20668 {
20669 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20670 LONGEST value;
20671 const gdb_byte *bytes;
20672 struct dwarf2_locexpr_baton *baton;
20673
20674 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20675 sym->print_name (),
20676 &objfile->objfile_obstack, cu,
20677 &value, &bytes, &baton);
20678
20679 if (baton != NULL)
20680 {
20681 SYMBOL_LOCATION_BATON (sym) = baton;
20682 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20683 }
20684 else if (bytes != NULL)
20685 {
20686 SYMBOL_VALUE_BYTES (sym) = bytes;
20687 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20688 }
20689 else
20690 {
20691 SYMBOL_VALUE (sym) = value;
20692 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20693 }
20694 }
20695
20696 /* Return the type of the die in question using its DW_AT_type attribute. */
20697
20698 static struct type *
20699 die_type (struct die_info *die, struct dwarf2_cu *cu)
20700 {
20701 struct attribute *type_attr;
20702
20703 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20704 if (!type_attr)
20705 {
20706 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20707 /* A missing DW_AT_type represents a void type. */
20708 return objfile_type (objfile)->builtin_void;
20709 }
20710
20711 return lookup_die_type (die, type_attr, cu);
20712 }
20713
20714 /* True iff CU's producer generates GNAT Ada auxiliary information
20715 that allows to find parallel types through that information instead
20716 of having to do expensive parallel lookups by type name. */
20717
20718 static int
20719 need_gnat_info (struct dwarf2_cu *cu)
20720 {
20721 /* Assume that the Ada compiler was GNAT, which always produces
20722 the auxiliary information. */
20723 return (cu->language == language_ada);
20724 }
20725
20726 /* Return the auxiliary type of the die in question using its
20727 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20728 attribute is not present. */
20729
20730 static struct type *
20731 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20732 {
20733 struct attribute *type_attr;
20734
20735 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20736 if (!type_attr)
20737 return NULL;
20738
20739 return lookup_die_type (die, type_attr, cu);
20740 }
20741
20742 /* If DIE has a descriptive_type attribute, then set the TYPE's
20743 descriptive type accordingly. */
20744
20745 static void
20746 set_descriptive_type (struct type *type, struct die_info *die,
20747 struct dwarf2_cu *cu)
20748 {
20749 struct type *descriptive_type = die_descriptive_type (die, cu);
20750
20751 if (descriptive_type)
20752 {
20753 ALLOCATE_GNAT_AUX_TYPE (type);
20754 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20755 }
20756 }
20757
20758 /* Return the containing type of the die in question using its
20759 DW_AT_containing_type attribute. */
20760
20761 static struct type *
20762 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20763 {
20764 struct attribute *type_attr;
20765 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20766
20767 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20768 if (!type_attr)
20769 error (_("Dwarf Error: Problem turning containing type into gdb type "
20770 "[in module %s]"), objfile_name (objfile));
20771
20772 return lookup_die_type (die, type_attr, cu);
20773 }
20774
20775 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20776
20777 static struct type *
20778 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20779 {
20780 struct dwarf2_per_objfile *dwarf2_per_objfile
20781 = cu->per_cu->dwarf2_per_objfile;
20782 struct objfile *objfile = dwarf2_per_objfile->objfile;
20783 char *saved;
20784
20785 std::string message
20786 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20787 objfile_name (objfile),
20788 sect_offset_str (cu->header.sect_off),
20789 sect_offset_str (die->sect_off));
20790 saved = obstack_strdup (&objfile->objfile_obstack, message);
20791
20792 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20793 }
20794
20795 /* Look up the type of DIE in CU using its type attribute ATTR.
20796 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20797 DW_AT_containing_type.
20798 If there is no type substitute an error marker. */
20799
20800 static struct type *
20801 lookup_die_type (struct die_info *die, const struct attribute *attr,
20802 struct dwarf2_cu *cu)
20803 {
20804 struct dwarf2_per_objfile *dwarf2_per_objfile
20805 = cu->per_cu->dwarf2_per_objfile;
20806 struct objfile *objfile = dwarf2_per_objfile->objfile;
20807 struct type *this_type;
20808
20809 gdb_assert (attr->name == DW_AT_type
20810 || attr->name == DW_AT_GNAT_descriptive_type
20811 || attr->name == DW_AT_containing_type);
20812
20813 /* First see if we have it cached. */
20814
20815 if (attr->form == DW_FORM_GNU_ref_alt)
20816 {
20817 struct dwarf2_per_cu_data *per_cu;
20818 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20819
20820 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20821 dwarf2_per_objfile);
20822 this_type = get_die_type_at_offset (sect_off, per_cu);
20823 }
20824 else if (attr->form_is_ref ())
20825 {
20826 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20827
20828 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20829 }
20830 else if (attr->form == DW_FORM_ref_sig8)
20831 {
20832 ULONGEST signature = DW_SIGNATURE (attr);
20833
20834 return get_signatured_type (die, signature, cu);
20835 }
20836 else
20837 {
20838 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20839 " at %s [in module %s]"),
20840 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20841 objfile_name (objfile));
20842 return build_error_marker_type (cu, die);
20843 }
20844
20845 /* If not cached we need to read it in. */
20846
20847 if (this_type == NULL)
20848 {
20849 struct die_info *type_die = NULL;
20850 struct dwarf2_cu *type_cu = cu;
20851
20852 if (attr->form_is_ref ())
20853 type_die = follow_die_ref (die, attr, &type_cu);
20854 if (type_die == NULL)
20855 return build_error_marker_type (cu, die);
20856 /* If we find the type now, it's probably because the type came
20857 from an inter-CU reference and the type's CU got expanded before
20858 ours. */
20859 this_type = read_type_die (type_die, type_cu);
20860 }
20861
20862 /* If we still don't have a type use an error marker. */
20863
20864 if (this_type == NULL)
20865 return build_error_marker_type (cu, die);
20866
20867 return this_type;
20868 }
20869
20870 /* Return the type in DIE, CU.
20871 Returns NULL for invalid types.
20872
20873 This first does a lookup in die_type_hash,
20874 and only reads the die in if necessary.
20875
20876 NOTE: This can be called when reading in partial or full symbols. */
20877
20878 static struct type *
20879 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20880 {
20881 struct type *this_type;
20882
20883 this_type = get_die_type (die, cu);
20884 if (this_type)
20885 return this_type;
20886
20887 return read_type_die_1 (die, cu);
20888 }
20889
20890 /* Read the type in DIE, CU.
20891 Returns NULL for invalid types. */
20892
20893 static struct type *
20894 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20895 {
20896 struct type *this_type = NULL;
20897
20898 switch (die->tag)
20899 {
20900 case DW_TAG_class_type:
20901 case DW_TAG_interface_type:
20902 case DW_TAG_structure_type:
20903 case DW_TAG_union_type:
20904 this_type = read_structure_type (die, cu);
20905 break;
20906 case DW_TAG_enumeration_type:
20907 this_type = read_enumeration_type (die, cu);
20908 break;
20909 case DW_TAG_subprogram:
20910 case DW_TAG_subroutine_type:
20911 case DW_TAG_inlined_subroutine:
20912 this_type = read_subroutine_type (die, cu);
20913 break;
20914 case DW_TAG_array_type:
20915 this_type = read_array_type (die, cu);
20916 break;
20917 case DW_TAG_set_type:
20918 this_type = read_set_type (die, cu);
20919 break;
20920 case DW_TAG_pointer_type:
20921 this_type = read_tag_pointer_type (die, cu);
20922 break;
20923 case DW_TAG_ptr_to_member_type:
20924 this_type = read_tag_ptr_to_member_type (die, cu);
20925 break;
20926 case DW_TAG_reference_type:
20927 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20928 break;
20929 case DW_TAG_rvalue_reference_type:
20930 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20931 break;
20932 case DW_TAG_const_type:
20933 this_type = read_tag_const_type (die, cu);
20934 break;
20935 case DW_TAG_volatile_type:
20936 this_type = read_tag_volatile_type (die, cu);
20937 break;
20938 case DW_TAG_restrict_type:
20939 this_type = read_tag_restrict_type (die, cu);
20940 break;
20941 case DW_TAG_string_type:
20942 this_type = read_tag_string_type (die, cu);
20943 break;
20944 case DW_TAG_typedef:
20945 this_type = read_typedef (die, cu);
20946 break;
20947 case DW_TAG_subrange_type:
20948 this_type = read_subrange_type (die, cu);
20949 break;
20950 case DW_TAG_base_type:
20951 this_type = read_base_type (die, cu);
20952 break;
20953 case DW_TAG_unspecified_type:
20954 this_type = read_unspecified_type (die, cu);
20955 break;
20956 case DW_TAG_namespace:
20957 this_type = read_namespace_type (die, cu);
20958 break;
20959 case DW_TAG_module:
20960 this_type = read_module_type (die, cu);
20961 break;
20962 case DW_TAG_atomic_type:
20963 this_type = read_tag_atomic_type (die, cu);
20964 break;
20965 default:
20966 complaint (_("unexpected tag in read_type_die: '%s'"),
20967 dwarf_tag_name (die->tag));
20968 break;
20969 }
20970
20971 return this_type;
20972 }
20973
20974 /* See if we can figure out if the class lives in a namespace. We do
20975 this by looking for a member function; its demangled name will
20976 contain namespace info, if there is any.
20977 Return the computed name or NULL.
20978 Space for the result is allocated on the objfile's obstack.
20979 This is the full-die version of guess_partial_die_structure_name.
20980 In this case we know DIE has no useful parent. */
20981
20982 static const char *
20983 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20984 {
20985 struct die_info *spec_die;
20986 struct dwarf2_cu *spec_cu;
20987 struct die_info *child;
20988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20989
20990 spec_cu = cu;
20991 spec_die = die_specification (die, &spec_cu);
20992 if (spec_die != NULL)
20993 {
20994 die = spec_die;
20995 cu = spec_cu;
20996 }
20997
20998 for (child = die->child;
20999 child != NULL;
21000 child = child->sibling)
21001 {
21002 if (child->tag == DW_TAG_subprogram)
21003 {
21004 const char *linkage_name = dw2_linkage_name (child, cu);
21005
21006 if (linkage_name != NULL)
21007 {
21008 gdb::unique_xmalloc_ptr<char> actual_name
21009 (language_class_name_from_physname (cu->language_defn,
21010 linkage_name));
21011 const char *name = NULL;
21012
21013 if (actual_name != NULL)
21014 {
21015 const char *die_name = dwarf2_name (die, cu);
21016
21017 if (die_name != NULL
21018 && strcmp (die_name, actual_name.get ()) != 0)
21019 {
21020 /* Strip off the class name from the full name.
21021 We want the prefix. */
21022 int die_name_len = strlen (die_name);
21023 int actual_name_len = strlen (actual_name.get ());
21024 const char *ptr = actual_name.get ();
21025
21026 /* Test for '::' as a sanity check. */
21027 if (actual_name_len > die_name_len + 2
21028 && ptr[actual_name_len - die_name_len - 1] == ':')
21029 name = obstack_strndup (
21030 &objfile->per_bfd->storage_obstack,
21031 ptr, actual_name_len - die_name_len - 2);
21032 }
21033 }
21034 return name;
21035 }
21036 }
21037 }
21038
21039 return NULL;
21040 }
21041
21042 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21043 prefix part in such case. See
21044 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21045
21046 static const char *
21047 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21048 {
21049 struct attribute *attr;
21050 const char *base;
21051
21052 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21053 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21054 return NULL;
21055
21056 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21057 return NULL;
21058
21059 attr = dw2_linkage_name_attr (die, cu);
21060 if (attr == NULL || DW_STRING (attr) == NULL)
21061 return NULL;
21062
21063 /* dwarf2_name had to be already called. */
21064 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21065
21066 /* Strip the base name, keep any leading namespaces/classes. */
21067 base = strrchr (DW_STRING (attr), ':');
21068 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21069 return "";
21070
21071 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21072 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21073 DW_STRING (attr),
21074 &base[-1] - DW_STRING (attr));
21075 }
21076
21077 /* Return the name of the namespace/class that DIE is defined within,
21078 or "" if we can't tell. The caller should not xfree the result.
21079
21080 For example, if we're within the method foo() in the following
21081 code:
21082
21083 namespace N {
21084 class C {
21085 void foo () {
21086 }
21087 };
21088 }
21089
21090 then determine_prefix on foo's die will return "N::C". */
21091
21092 static const char *
21093 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21094 {
21095 struct dwarf2_per_objfile *dwarf2_per_objfile
21096 = cu->per_cu->dwarf2_per_objfile;
21097 struct die_info *parent, *spec_die;
21098 struct dwarf2_cu *spec_cu;
21099 struct type *parent_type;
21100 const char *retval;
21101
21102 if (cu->language != language_cplus
21103 && cu->language != language_fortran && cu->language != language_d
21104 && cu->language != language_rust)
21105 return "";
21106
21107 retval = anonymous_struct_prefix (die, cu);
21108 if (retval)
21109 return retval;
21110
21111 /* We have to be careful in the presence of DW_AT_specification.
21112 For example, with GCC 3.4, given the code
21113
21114 namespace N {
21115 void foo() {
21116 // Definition of N::foo.
21117 }
21118 }
21119
21120 then we'll have a tree of DIEs like this:
21121
21122 1: DW_TAG_compile_unit
21123 2: DW_TAG_namespace // N
21124 3: DW_TAG_subprogram // declaration of N::foo
21125 4: DW_TAG_subprogram // definition of N::foo
21126 DW_AT_specification // refers to die #3
21127
21128 Thus, when processing die #4, we have to pretend that we're in
21129 the context of its DW_AT_specification, namely the contex of die
21130 #3. */
21131 spec_cu = cu;
21132 spec_die = die_specification (die, &spec_cu);
21133 if (spec_die == NULL)
21134 parent = die->parent;
21135 else
21136 {
21137 parent = spec_die->parent;
21138 cu = spec_cu;
21139 }
21140
21141 if (parent == NULL)
21142 return "";
21143 else if (parent->building_fullname)
21144 {
21145 const char *name;
21146 const char *parent_name;
21147
21148 /* It has been seen on RealView 2.2 built binaries,
21149 DW_TAG_template_type_param types actually _defined_ as
21150 children of the parent class:
21151
21152 enum E {};
21153 template class <class Enum> Class{};
21154 Class<enum E> class_e;
21155
21156 1: DW_TAG_class_type (Class)
21157 2: DW_TAG_enumeration_type (E)
21158 3: DW_TAG_enumerator (enum1:0)
21159 3: DW_TAG_enumerator (enum2:1)
21160 ...
21161 2: DW_TAG_template_type_param
21162 DW_AT_type DW_FORM_ref_udata (E)
21163
21164 Besides being broken debug info, it can put GDB into an
21165 infinite loop. Consider:
21166
21167 When we're building the full name for Class<E>, we'll start
21168 at Class, and go look over its template type parameters,
21169 finding E. We'll then try to build the full name of E, and
21170 reach here. We're now trying to build the full name of E,
21171 and look over the parent DIE for containing scope. In the
21172 broken case, if we followed the parent DIE of E, we'd again
21173 find Class, and once again go look at its template type
21174 arguments, etc., etc. Simply don't consider such parent die
21175 as source-level parent of this die (it can't be, the language
21176 doesn't allow it), and break the loop here. */
21177 name = dwarf2_name (die, cu);
21178 parent_name = dwarf2_name (parent, cu);
21179 complaint (_("template param type '%s' defined within parent '%s'"),
21180 name ? name : "<unknown>",
21181 parent_name ? parent_name : "<unknown>");
21182 return "";
21183 }
21184 else
21185 switch (parent->tag)
21186 {
21187 case DW_TAG_namespace:
21188 parent_type = read_type_die (parent, cu);
21189 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21190 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21191 Work around this problem here. */
21192 if (cu->language == language_cplus
21193 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21194 return "";
21195 /* We give a name to even anonymous namespaces. */
21196 return TYPE_NAME (parent_type);
21197 case DW_TAG_class_type:
21198 case DW_TAG_interface_type:
21199 case DW_TAG_structure_type:
21200 case DW_TAG_union_type:
21201 case DW_TAG_module:
21202 parent_type = read_type_die (parent, cu);
21203 if (TYPE_NAME (parent_type) != NULL)
21204 return TYPE_NAME (parent_type);
21205 else
21206 /* An anonymous structure is only allowed non-static data
21207 members; no typedefs, no member functions, et cetera.
21208 So it does not need a prefix. */
21209 return "";
21210 case DW_TAG_compile_unit:
21211 case DW_TAG_partial_unit:
21212 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21213 if (cu->language == language_cplus
21214 && !dwarf2_per_objfile->types.empty ()
21215 && die->child != NULL
21216 && (die->tag == DW_TAG_class_type
21217 || die->tag == DW_TAG_structure_type
21218 || die->tag == DW_TAG_union_type))
21219 {
21220 const char *name = guess_full_die_structure_name (die, cu);
21221 if (name != NULL)
21222 return name;
21223 }
21224 return "";
21225 case DW_TAG_subprogram:
21226 /* Nested subroutines in Fortran get a prefix with the name
21227 of the parent's subroutine. */
21228 if (cu->language == language_fortran)
21229 {
21230 if ((die->tag == DW_TAG_subprogram)
21231 && (dwarf2_name (parent, cu) != NULL))
21232 return dwarf2_name (parent, cu);
21233 }
21234 return determine_prefix (parent, cu);
21235 case DW_TAG_enumeration_type:
21236 parent_type = read_type_die (parent, cu);
21237 if (TYPE_DECLARED_CLASS (parent_type))
21238 {
21239 if (TYPE_NAME (parent_type) != NULL)
21240 return TYPE_NAME (parent_type);
21241 return "";
21242 }
21243 /* Fall through. */
21244 default:
21245 return determine_prefix (parent, cu);
21246 }
21247 }
21248
21249 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21250 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21251 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21252 an obconcat, otherwise allocate storage for the result. The CU argument is
21253 used to determine the language and hence, the appropriate separator. */
21254
21255 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21256
21257 static char *
21258 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21259 int physname, struct dwarf2_cu *cu)
21260 {
21261 const char *lead = "";
21262 const char *sep;
21263
21264 if (suffix == NULL || suffix[0] == '\0'
21265 || prefix == NULL || prefix[0] == '\0')
21266 sep = "";
21267 else if (cu->language == language_d)
21268 {
21269 /* For D, the 'main' function could be defined in any module, but it
21270 should never be prefixed. */
21271 if (strcmp (suffix, "D main") == 0)
21272 {
21273 prefix = "";
21274 sep = "";
21275 }
21276 else
21277 sep = ".";
21278 }
21279 else if (cu->language == language_fortran && physname)
21280 {
21281 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21282 DW_AT_MIPS_linkage_name is preferred and used instead. */
21283
21284 lead = "__";
21285 sep = "_MOD_";
21286 }
21287 else
21288 sep = "::";
21289
21290 if (prefix == NULL)
21291 prefix = "";
21292 if (suffix == NULL)
21293 suffix = "";
21294
21295 if (obs == NULL)
21296 {
21297 char *retval
21298 = ((char *)
21299 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21300
21301 strcpy (retval, lead);
21302 strcat (retval, prefix);
21303 strcat (retval, sep);
21304 strcat (retval, suffix);
21305 return retval;
21306 }
21307 else
21308 {
21309 /* We have an obstack. */
21310 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21311 }
21312 }
21313
21314 /* Return sibling of die, NULL if no sibling. */
21315
21316 static struct die_info *
21317 sibling_die (struct die_info *die)
21318 {
21319 return die->sibling;
21320 }
21321
21322 /* Get name of a die, return NULL if not found. */
21323
21324 static const char *
21325 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21326 struct objfile *objfile)
21327 {
21328 if (name && cu->language == language_cplus)
21329 {
21330 std::string canon_name = cp_canonicalize_string (name);
21331
21332 if (!canon_name.empty ())
21333 {
21334 if (canon_name != name)
21335 name = objfile->intern (canon_name);
21336 }
21337 }
21338
21339 return name;
21340 }
21341
21342 /* Get name of a die, return NULL if not found.
21343 Anonymous namespaces are converted to their magic string. */
21344
21345 static const char *
21346 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21347 {
21348 struct attribute *attr;
21349 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21350
21351 attr = dwarf2_attr (die, DW_AT_name, cu);
21352 if ((!attr || !DW_STRING (attr))
21353 && die->tag != DW_TAG_namespace
21354 && die->tag != DW_TAG_class_type
21355 && die->tag != DW_TAG_interface_type
21356 && die->tag != DW_TAG_structure_type
21357 && die->tag != DW_TAG_union_type)
21358 return NULL;
21359
21360 switch (die->tag)
21361 {
21362 case DW_TAG_compile_unit:
21363 case DW_TAG_partial_unit:
21364 /* Compilation units have a DW_AT_name that is a filename, not
21365 a source language identifier. */
21366 case DW_TAG_enumeration_type:
21367 case DW_TAG_enumerator:
21368 /* These tags always have simple identifiers already; no need
21369 to canonicalize them. */
21370 return DW_STRING (attr);
21371
21372 case DW_TAG_namespace:
21373 if (attr != NULL && DW_STRING (attr) != NULL)
21374 return DW_STRING (attr);
21375 return CP_ANONYMOUS_NAMESPACE_STR;
21376
21377 case DW_TAG_class_type:
21378 case DW_TAG_interface_type:
21379 case DW_TAG_structure_type:
21380 case DW_TAG_union_type:
21381 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21382 structures or unions. These were of the form "._%d" in GCC 4.1,
21383 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21384 and GCC 4.4. We work around this problem by ignoring these. */
21385 if (attr && DW_STRING (attr)
21386 && (startswith (DW_STRING (attr), "._")
21387 || startswith (DW_STRING (attr), "<anonymous")))
21388 return NULL;
21389
21390 /* GCC might emit a nameless typedef that has a linkage name. See
21391 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21392 if (!attr || DW_STRING (attr) == NULL)
21393 {
21394 attr = dw2_linkage_name_attr (die, cu);
21395 if (attr == NULL || DW_STRING (attr) == NULL)
21396 return NULL;
21397
21398 /* Avoid demangling DW_STRING (attr) the second time on a second
21399 call for the same DIE. */
21400 if (!DW_STRING_IS_CANONICAL (attr))
21401 {
21402 gdb::unique_xmalloc_ptr<char> demangled
21403 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21404 if (demangled == nullptr)
21405 return nullptr;
21406
21407 DW_STRING (attr) = objfile->intern (demangled.get ());
21408 DW_STRING_IS_CANONICAL (attr) = 1;
21409 }
21410
21411 /* Strip any leading namespaces/classes, keep only the base name.
21412 DW_AT_name for named DIEs does not contain the prefixes. */
21413 const char *base = strrchr (DW_STRING (attr), ':');
21414 if (base && base > DW_STRING (attr) && base[-1] == ':')
21415 return &base[1];
21416 else
21417 return DW_STRING (attr);
21418 }
21419 break;
21420
21421 default:
21422 break;
21423 }
21424
21425 if (!DW_STRING_IS_CANONICAL (attr))
21426 {
21427 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21428 objfile);
21429 DW_STRING_IS_CANONICAL (attr) = 1;
21430 }
21431 return DW_STRING (attr);
21432 }
21433
21434 /* Return the die that this die in an extension of, or NULL if there
21435 is none. *EXT_CU is the CU containing DIE on input, and the CU
21436 containing the return value on output. */
21437
21438 static struct die_info *
21439 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21440 {
21441 struct attribute *attr;
21442
21443 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21444 if (attr == NULL)
21445 return NULL;
21446
21447 return follow_die_ref (die, attr, ext_cu);
21448 }
21449
21450 /* A convenience function that returns an "unknown" DWARF name,
21451 including the value of V. STR is the name of the entity being
21452 printed, e.g., "TAG". */
21453
21454 static const char *
21455 dwarf_unknown (const char *str, unsigned v)
21456 {
21457 char *cell = get_print_cell ();
21458 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21459 return cell;
21460 }
21461
21462 /* Convert a DIE tag into its string name. */
21463
21464 static const char *
21465 dwarf_tag_name (unsigned tag)
21466 {
21467 const char *name = get_DW_TAG_name (tag);
21468
21469 if (name == NULL)
21470 return dwarf_unknown ("TAG", tag);
21471
21472 return name;
21473 }
21474
21475 /* Convert a DWARF attribute code into its string name. */
21476
21477 static const char *
21478 dwarf_attr_name (unsigned attr)
21479 {
21480 const char *name;
21481
21482 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21483 if (attr == DW_AT_MIPS_fde)
21484 return "DW_AT_MIPS_fde";
21485 #else
21486 if (attr == DW_AT_HP_block_index)
21487 return "DW_AT_HP_block_index";
21488 #endif
21489
21490 name = get_DW_AT_name (attr);
21491
21492 if (name == NULL)
21493 return dwarf_unknown ("AT", attr);
21494
21495 return name;
21496 }
21497
21498 /* Convert a DWARF value form code into its string name. */
21499
21500 static const char *
21501 dwarf_form_name (unsigned form)
21502 {
21503 const char *name = get_DW_FORM_name (form);
21504
21505 if (name == NULL)
21506 return dwarf_unknown ("FORM", form);
21507
21508 return name;
21509 }
21510
21511 static const char *
21512 dwarf_bool_name (unsigned mybool)
21513 {
21514 if (mybool)
21515 return "TRUE";
21516 else
21517 return "FALSE";
21518 }
21519
21520 /* Convert a DWARF type code into its string name. */
21521
21522 static const char *
21523 dwarf_type_encoding_name (unsigned enc)
21524 {
21525 const char *name = get_DW_ATE_name (enc);
21526
21527 if (name == NULL)
21528 return dwarf_unknown ("ATE", enc);
21529
21530 return name;
21531 }
21532
21533 static void
21534 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21535 {
21536 unsigned int i;
21537
21538 print_spaces (indent, f);
21539 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21540 dwarf_tag_name (die->tag), die->abbrev,
21541 sect_offset_str (die->sect_off));
21542
21543 if (die->parent != NULL)
21544 {
21545 print_spaces (indent, f);
21546 fprintf_unfiltered (f, " parent at offset: %s\n",
21547 sect_offset_str (die->parent->sect_off));
21548 }
21549
21550 print_spaces (indent, f);
21551 fprintf_unfiltered (f, " has children: %s\n",
21552 dwarf_bool_name (die->child != NULL));
21553
21554 print_spaces (indent, f);
21555 fprintf_unfiltered (f, " attributes:\n");
21556
21557 for (i = 0; i < die->num_attrs; ++i)
21558 {
21559 print_spaces (indent, f);
21560 fprintf_unfiltered (f, " %s (%s) ",
21561 dwarf_attr_name (die->attrs[i].name),
21562 dwarf_form_name (die->attrs[i].form));
21563
21564 switch (die->attrs[i].form)
21565 {
21566 case DW_FORM_addr:
21567 case DW_FORM_addrx:
21568 case DW_FORM_GNU_addr_index:
21569 fprintf_unfiltered (f, "address: ");
21570 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21571 break;
21572 case DW_FORM_block2:
21573 case DW_FORM_block4:
21574 case DW_FORM_block:
21575 case DW_FORM_block1:
21576 fprintf_unfiltered (f, "block: size %s",
21577 pulongest (DW_BLOCK (&die->attrs[i])->size));
21578 break;
21579 case DW_FORM_exprloc:
21580 fprintf_unfiltered (f, "expression: size %s",
21581 pulongest (DW_BLOCK (&die->attrs[i])->size));
21582 break;
21583 case DW_FORM_data16:
21584 fprintf_unfiltered (f, "constant of 16 bytes");
21585 break;
21586 case DW_FORM_ref_addr:
21587 fprintf_unfiltered (f, "ref address: ");
21588 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21589 break;
21590 case DW_FORM_GNU_ref_alt:
21591 fprintf_unfiltered (f, "alt ref address: ");
21592 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21593 break;
21594 case DW_FORM_ref1:
21595 case DW_FORM_ref2:
21596 case DW_FORM_ref4:
21597 case DW_FORM_ref8:
21598 case DW_FORM_ref_udata:
21599 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21600 (long) (DW_UNSND (&die->attrs[i])));
21601 break;
21602 case DW_FORM_data1:
21603 case DW_FORM_data2:
21604 case DW_FORM_data4:
21605 case DW_FORM_data8:
21606 case DW_FORM_udata:
21607 case DW_FORM_sdata:
21608 fprintf_unfiltered (f, "constant: %s",
21609 pulongest (DW_UNSND (&die->attrs[i])));
21610 break;
21611 case DW_FORM_sec_offset:
21612 fprintf_unfiltered (f, "section offset: %s",
21613 pulongest (DW_UNSND (&die->attrs[i])));
21614 break;
21615 case DW_FORM_ref_sig8:
21616 fprintf_unfiltered (f, "signature: %s",
21617 hex_string (DW_SIGNATURE (&die->attrs[i])));
21618 break;
21619 case DW_FORM_string:
21620 case DW_FORM_strp:
21621 case DW_FORM_line_strp:
21622 case DW_FORM_strx:
21623 case DW_FORM_GNU_str_index:
21624 case DW_FORM_GNU_strp_alt:
21625 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21626 DW_STRING (&die->attrs[i])
21627 ? DW_STRING (&die->attrs[i]) : "",
21628 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21629 break;
21630 case DW_FORM_flag:
21631 if (DW_UNSND (&die->attrs[i]))
21632 fprintf_unfiltered (f, "flag: TRUE");
21633 else
21634 fprintf_unfiltered (f, "flag: FALSE");
21635 break;
21636 case DW_FORM_flag_present:
21637 fprintf_unfiltered (f, "flag: TRUE");
21638 break;
21639 case DW_FORM_indirect:
21640 /* The reader will have reduced the indirect form to
21641 the "base form" so this form should not occur. */
21642 fprintf_unfiltered (f,
21643 "unexpected attribute form: DW_FORM_indirect");
21644 break;
21645 case DW_FORM_implicit_const:
21646 fprintf_unfiltered (f, "constant: %s",
21647 plongest (DW_SND (&die->attrs[i])));
21648 break;
21649 default:
21650 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21651 die->attrs[i].form);
21652 break;
21653 }
21654 fprintf_unfiltered (f, "\n");
21655 }
21656 }
21657
21658 static void
21659 dump_die_for_error (struct die_info *die)
21660 {
21661 dump_die_shallow (gdb_stderr, 0, die);
21662 }
21663
21664 static void
21665 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21666 {
21667 int indent = level * 4;
21668
21669 gdb_assert (die != NULL);
21670
21671 if (level >= max_level)
21672 return;
21673
21674 dump_die_shallow (f, indent, die);
21675
21676 if (die->child != NULL)
21677 {
21678 print_spaces (indent, f);
21679 fprintf_unfiltered (f, " Children:");
21680 if (level + 1 < max_level)
21681 {
21682 fprintf_unfiltered (f, "\n");
21683 dump_die_1 (f, level + 1, max_level, die->child);
21684 }
21685 else
21686 {
21687 fprintf_unfiltered (f,
21688 " [not printed, max nesting level reached]\n");
21689 }
21690 }
21691
21692 if (die->sibling != NULL && level > 0)
21693 {
21694 dump_die_1 (f, level, max_level, die->sibling);
21695 }
21696 }
21697
21698 /* This is called from the pdie macro in gdbinit.in.
21699 It's not static so gcc will keep a copy callable from gdb. */
21700
21701 void
21702 dump_die (struct die_info *die, int max_level)
21703 {
21704 dump_die_1 (gdb_stdlog, 0, max_level, die);
21705 }
21706
21707 static void
21708 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21709 {
21710 void **slot;
21711
21712 slot = htab_find_slot_with_hash (cu->die_hash, die,
21713 to_underlying (die->sect_off),
21714 INSERT);
21715
21716 *slot = die;
21717 }
21718
21719 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21720 required kind. */
21721
21722 static sect_offset
21723 dwarf2_get_ref_die_offset (const struct attribute *attr)
21724 {
21725 if (attr->form_is_ref ())
21726 return (sect_offset) DW_UNSND (attr);
21727
21728 complaint (_("unsupported die ref attribute form: '%s'"),
21729 dwarf_form_name (attr->form));
21730 return {};
21731 }
21732
21733 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21734 * the value held by the attribute is not constant. */
21735
21736 static LONGEST
21737 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21738 {
21739 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21740 return DW_SND (attr);
21741 else if (attr->form == DW_FORM_udata
21742 || attr->form == DW_FORM_data1
21743 || attr->form == DW_FORM_data2
21744 || attr->form == DW_FORM_data4
21745 || attr->form == DW_FORM_data8)
21746 return DW_UNSND (attr);
21747 else
21748 {
21749 /* For DW_FORM_data16 see attribute::form_is_constant. */
21750 complaint (_("Attribute value is not a constant (%s)"),
21751 dwarf_form_name (attr->form));
21752 return default_value;
21753 }
21754 }
21755
21756 /* Follow reference or signature attribute ATTR of SRC_DIE.
21757 On entry *REF_CU is the CU of SRC_DIE.
21758 On exit *REF_CU is the CU of the result. */
21759
21760 static struct die_info *
21761 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21762 struct dwarf2_cu **ref_cu)
21763 {
21764 struct die_info *die;
21765
21766 if (attr->form_is_ref ())
21767 die = follow_die_ref (src_die, attr, ref_cu);
21768 else if (attr->form == DW_FORM_ref_sig8)
21769 die = follow_die_sig (src_die, attr, ref_cu);
21770 else
21771 {
21772 dump_die_for_error (src_die);
21773 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21774 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21775 }
21776
21777 return die;
21778 }
21779
21780 /* Follow reference OFFSET.
21781 On entry *REF_CU is the CU of the source die referencing OFFSET.
21782 On exit *REF_CU is the CU of the result.
21783 Returns NULL if OFFSET is invalid. */
21784
21785 static struct die_info *
21786 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21787 struct dwarf2_cu **ref_cu)
21788 {
21789 struct die_info temp_die;
21790 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21791 struct dwarf2_per_objfile *dwarf2_per_objfile
21792 = cu->per_cu->dwarf2_per_objfile;
21793
21794 gdb_assert (cu->per_cu != NULL);
21795
21796 target_cu = cu;
21797
21798 if (cu->per_cu->is_debug_types)
21799 {
21800 /* .debug_types CUs cannot reference anything outside their CU.
21801 If they need to, they have to reference a signatured type via
21802 DW_FORM_ref_sig8. */
21803 if (!cu->header.offset_in_cu_p (sect_off))
21804 return NULL;
21805 }
21806 else if (offset_in_dwz != cu->per_cu->is_dwz
21807 || !cu->header.offset_in_cu_p (sect_off))
21808 {
21809 struct dwarf2_per_cu_data *per_cu;
21810
21811 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21812 dwarf2_per_objfile);
21813
21814 /* If necessary, add it to the queue and load its DIEs. */
21815 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21816 load_full_comp_unit (per_cu, false, cu->language);
21817
21818 target_cu = per_cu->cu;
21819 }
21820 else if (cu->dies == NULL)
21821 {
21822 /* We're loading full DIEs during partial symbol reading. */
21823 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21824 load_full_comp_unit (cu->per_cu, false, language_minimal);
21825 }
21826
21827 *ref_cu = target_cu;
21828 temp_die.sect_off = sect_off;
21829
21830 if (target_cu != cu)
21831 target_cu->ancestor = cu;
21832
21833 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21834 &temp_die,
21835 to_underlying (sect_off));
21836 }
21837
21838 /* Follow reference attribute ATTR of SRC_DIE.
21839 On entry *REF_CU is the CU of SRC_DIE.
21840 On exit *REF_CU is the CU of the result. */
21841
21842 static struct die_info *
21843 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21844 struct dwarf2_cu **ref_cu)
21845 {
21846 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21847 struct dwarf2_cu *cu = *ref_cu;
21848 struct die_info *die;
21849
21850 die = follow_die_offset (sect_off,
21851 (attr->form == DW_FORM_GNU_ref_alt
21852 || cu->per_cu->is_dwz),
21853 ref_cu);
21854 if (!die)
21855 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21856 "at %s [in module %s]"),
21857 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21858 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21859
21860 return die;
21861 }
21862
21863 /* See read.h. */
21864
21865 struct dwarf2_locexpr_baton
21866 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21867 dwarf2_per_cu_data *per_cu,
21868 CORE_ADDR (*get_frame_pc) (void *baton),
21869 void *baton, bool resolve_abstract_p)
21870 {
21871 struct dwarf2_cu *cu;
21872 struct die_info *die;
21873 struct attribute *attr;
21874 struct dwarf2_locexpr_baton retval;
21875 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21876 struct objfile *objfile = dwarf2_per_objfile->objfile;
21877
21878 if (per_cu->cu == NULL)
21879 load_cu (per_cu, false);
21880 cu = per_cu->cu;
21881 if (cu == NULL)
21882 {
21883 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21884 Instead just throw an error, not much else we can do. */
21885 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21886 sect_offset_str (sect_off), objfile_name (objfile));
21887 }
21888
21889 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21890 if (!die)
21891 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21892 sect_offset_str (sect_off), objfile_name (objfile));
21893
21894 attr = dwarf2_attr (die, DW_AT_location, cu);
21895 if (!attr && resolve_abstract_p
21896 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21897 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21898 {
21899 CORE_ADDR pc = (*get_frame_pc) (baton);
21900 CORE_ADDR baseaddr = objfile->text_section_offset ();
21901 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21902
21903 for (const auto &cand_off
21904 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21905 {
21906 struct dwarf2_cu *cand_cu = cu;
21907 struct die_info *cand
21908 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21909 if (!cand
21910 || !cand->parent
21911 || cand->parent->tag != DW_TAG_subprogram)
21912 continue;
21913
21914 CORE_ADDR pc_low, pc_high;
21915 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21916 if (pc_low == ((CORE_ADDR) -1))
21917 continue;
21918 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21919 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21920 if (!(pc_low <= pc && pc < pc_high))
21921 continue;
21922
21923 die = cand;
21924 attr = dwarf2_attr (die, DW_AT_location, cu);
21925 break;
21926 }
21927 }
21928
21929 if (!attr)
21930 {
21931 /* DWARF: "If there is no such attribute, then there is no effect.".
21932 DATA is ignored if SIZE is 0. */
21933
21934 retval.data = NULL;
21935 retval.size = 0;
21936 }
21937 else if (attr->form_is_section_offset ())
21938 {
21939 struct dwarf2_loclist_baton loclist_baton;
21940 CORE_ADDR pc = (*get_frame_pc) (baton);
21941 size_t size;
21942
21943 fill_in_loclist_baton (cu, &loclist_baton, attr);
21944
21945 retval.data = dwarf2_find_location_expression (&loclist_baton,
21946 &size, pc);
21947 retval.size = size;
21948 }
21949 else
21950 {
21951 if (!attr->form_is_block ())
21952 error (_("Dwarf Error: DIE at %s referenced in module %s "
21953 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21954 sect_offset_str (sect_off), objfile_name (objfile));
21955
21956 retval.data = DW_BLOCK (attr)->data;
21957 retval.size = DW_BLOCK (attr)->size;
21958 }
21959 retval.per_cu = cu->per_cu;
21960
21961 age_cached_comp_units (dwarf2_per_objfile);
21962
21963 return retval;
21964 }
21965
21966 /* See read.h. */
21967
21968 struct dwarf2_locexpr_baton
21969 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21970 dwarf2_per_cu_data *per_cu,
21971 CORE_ADDR (*get_frame_pc) (void *baton),
21972 void *baton)
21973 {
21974 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21975
21976 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21977 }
21978
21979 /* Write a constant of a given type as target-ordered bytes into
21980 OBSTACK. */
21981
21982 static const gdb_byte *
21983 write_constant_as_bytes (struct obstack *obstack,
21984 enum bfd_endian byte_order,
21985 struct type *type,
21986 ULONGEST value,
21987 LONGEST *len)
21988 {
21989 gdb_byte *result;
21990
21991 *len = TYPE_LENGTH (type);
21992 result = (gdb_byte *) obstack_alloc (obstack, *len);
21993 store_unsigned_integer (result, *len, byte_order, value);
21994
21995 return result;
21996 }
21997
21998 /* See read.h. */
21999
22000 const gdb_byte *
22001 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22002 dwarf2_per_cu_data *per_cu,
22003 obstack *obstack,
22004 LONGEST *len)
22005 {
22006 struct dwarf2_cu *cu;
22007 struct die_info *die;
22008 struct attribute *attr;
22009 const gdb_byte *result = NULL;
22010 struct type *type;
22011 LONGEST value;
22012 enum bfd_endian byte_order;
22013 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22014
22015 if (per_cu->cu == NULL)
22016 load_cu (per_cu, false);
22017 cu = per_cu->cu;
22018 if (cu == NULL)
22019 {
22020 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22021 Instead just throw an error, not much else we can do. */
22022 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22023 sect_offset_str (sect_off), objfile_name (objfile));
22024 }
22025
22026 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22027 if (!die)
22028 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22029 sect_offset_str (sect_off), objfile_name (objfile));
22030
22031 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22032 if (attr == NULL)
22033 return NULL;
22034
22035 byte_order = (bfd_big_endian (objfile->obfd)
22036 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22037
22038 switch (attr->form)
22039 {
22040 case DW_FORM_addr:
22041 case DW_FORM_addrx:
22042 case DW_FORM_GNU_addr_index:
22043 {
22044 gdb_byte *tem;
22045
22046 *len = cu->header.addr_size;
22047 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22048 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22049 result = tem;
22050 }
22051 break;
22052 case DW_FORM_string:
22053 case DW_FORM_strp:
22054 case DW_FORM_strx:
22055 case DW_FORM_GNU_str_index:
22056 case DW_FORM_GNU_strp_alt:
22057 /* DW_STRING is already allocated on the objfile obstack, point
22058 directly to it. */
22059 result = (const gdb_byte *) DW_STRING (attr);
22060 *len = strlen (DW_STRING (attr));
22061 break;
22062 case DW_FORM_block1:
22063 case DW_FORM_block2:
22064 case DW_FORM_block4:
22065 case DW_FORM_block:
22066 case DW_FORM_exprloc:
22067 case DW_FORM_data16:
22068 result = DW_BLOCK (attr)->data;
22069 *len = DW_BLOCK (attr)->size;
22070 break;
22071
22072 /* The DW_AT_const_value attributes are supposed to carry the
22073 symbol's value "represented as it would be on the target
22074 architecture." By the time we get here, it's already been
22075 converted to host endianness, so we just need to sign- or
22076 zero-extend it as appropriate. */
22077 case DW_FORM_data1:
22078 type = die_type (die, cu);
22079 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22080 if (result == NULL)
22081 result = write_constant_as_bytes (obstack, byte_order,
22082 type, value, len);
22083 break;
22084 case DW_FORM_data2:
22085 type = die_type (die, cu);
22086 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22087 if (result == NULL)
22088 result = write_constant_as_bytes (obstack, byte_order,
22089 type, value, len);
22090 break;
22091 case DW_FORM_data4:
22092 type = die_type (die, cu);
22093 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22094 if (result == NULL)
22095 result = write_constant_as_bytes (obstack, byte_order,
22096 type, value, len);
22097 break;
22098 case DW_FORM_data8:
22099 type = die_type (die, cu);
22100 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22101 if (result == NULL)
22102 result = write_constant_as_bytes (obstack, byte_order,
22103 type, value, len);
22104 break;
22105
22106 case DW_FORM_sdata:
22107 case DW_FORM_implicit_const:
22108 type = die_type (die, cu);
22109 result = write_constant_as_bytes (obstack, byte_order,
22110 type, DW_SND (attr), len);
22111 break;
22112
22113 case DW_FORM_udata:
22114 type = die_type (die, cu);
22115 result = write_constant_as_bytes (obstack, byte_order,
22116 type, DW_UNSND (attr), len);
22117 break;
22118
22119 default:
22120 complaint (_("unsupported const value attribute form: '%s'"),
22121 dwarf_form_name (attr->form));
22122 break;
22123 }
22124
22125 return result;
22126 }
22127
22128 /* See read.h. */
22129
22130 struct type *
22131 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22132 dwarf2_per_cu_data *per_cu)
22133 {
22134 struct dwarf2_cu *cu;
22135 struct die_info *die;
22136
22137 if (per_cu->cu == NULL)
22138 load_cu (per_cu, false);
22139 cu = per_cu->cu;
22140 if (!cu)
22141 return NULL;
22142
22143 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22144 if (!die)
22145 return NULL;
22146
22147 return die_type (die, cu);
22148 }
22149
22150 /* See read.h. */
22151
22152 struct type *
22153 dwarf2_get_die_type (cu_offset die_offset,
22154 struct dwarf2_per_cu_data *per_cu)
22155 {
22156 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22157 return get_die_type_at_offset (die_offset_sect, per_cu);
22158 }
22159
22160 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22161 On entry *REF_CU is the CU of SRC_DIE.
22162 On exit *REF_CU is the CU of the result.
22163 Returns NULL if the referenced DIE isn't found. */
22164
22165 static struct die_info *
22166 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22167 struct dwarf2_cu **ref_cu)
22168 {
22169 struct die_info temp_die;
22170 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22171 struct die_info *die;
22172
22173 /* While it might be nice to assert sig_type->type == NULL here,
22174 we can get here for DW_AT_imported_declaration where we need
22175 the DIE not the type. */
22176
22177 /* If necessary, add it to the queue and load its DIEs. */
22178
22179 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22180 read_signatured_type (sig_type);
22181
22182 sig_cu = sig_type->per_cu.cu;
22183 gdb_assert (sig_cu != NULL);
22184 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22185 temp_die.sect_off = sig_type->type_offset_in_section;
22186 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22187 to_underlying (temp_die.sect_off));
22188 if (die)
22189 {
22190 struct dwarf2_per_objfile *dwarf2_per_objfile
22191 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22192
22193 /* For .gdb_index version 7 keep track of included TUs.
22194 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22195 if (dwarf2_per_objfile->index_table != NULL
22196 && dwarf2_per_objfile->index_table->version <= 7)
22197 {
22198 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22199 }
22200
22201 *ref_cu = sig_cu;
22202 if (sig_cu != cu)
22203 sig_cu->ancestor = cu;
22204
22205 return die;
22206 }
22207
22208 return NULL;
22209 }
22210
22211 /* Follow signatured type referenced by ATTR in SRC_DIE.
22212 On entry *REF_CU is the CU of SRC_DIE.
22213 On exit *REF_CU is the CU of the result.
22214 The result is the DIE of the type.
22215 If the referenced type cannot be found an error is thrown. */
22216
22217 static struct die_info *
22218 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22219 struct dwarf2_cu **ref_cu)
22220 {
22221 ULONGEST signature = DW_SIGNATURE (attr);
22222 struct signatured_type *sig_type;
22223 struct die_info *die;
22224
22225 gdb_assert (attr->form == DW_FORM_ref_sig8);
22226
22227 sig_type = lookup_signatured_type (*ref_cu, signature);
22228 /* sig_type will be NULL if the signatured type is missing from
22229 the debug info. */
22230 if (sig_type == NULL)
22231 {
22232 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22233 " from DIE at %s [in module %s]"),
22234 hex_string (signature), sect_offset_str (src_die->sect_off),
22235 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22236 }
22237
22238 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22239 if (die == NULL)
22240 {
22241 dump_die_for_error (src_die);
22242 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22243 " from DIE at %s [in module %s]"),
22244 hex_string (signature), sect_offset_str (src_die->sect_off),
22245 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22246 }
22247
22248 return die;
22249 }
22250
22251 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22252 reading in and processing the type unit if necessary. */
22253
22254 static struct type *
22255 get_signatured_type (struct die_info *die, ULONGEST signature,
22256 struct dwarf2_cu *cu)
22257 {
22258 struct dwarf2_per_objfile *dwarf2_per_objfile
22259 = cu->per_cu->dwarf2_per_objfile;
22260 struct signatured_type *sig_type;
22261 struct dwarf2_cu *type_cu;
22262 struct die_info *type_die;
22263 struct type *type;
22264
22265 sig_type = lookup_signatured_type (cu, signature);
22266 /* sig_type will be NULL if the signatured type is missing from
22267 the debug info. */
22268 if (sig_type == NULL)
22269 {
22270 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22271 " from DIE at %s [in module %s]"),
22272 hex_string (signature), sect_offset_str (die->sect_off),
22273 objfile_name (dwarf2_per_objfile->objfile));
22274 return build_error_marker_type (cu, die);
22275 }
22276
22277 /* If we already know the type we're done. */
22278 if (sig_type->type != NULL)
22279 return sig_type->type;
22280
22281 type_cu = cu;
22282 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22283 if (type_die != NULL)
22284 {
22285 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22286 is created. This is important, for example, because for c++ classes
22287 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22288 type = read_type_die (type_die, type_cu);
22289 if (type == NULL)
22290 {
22291 complaint (_("Dwarf Error: Cannot build signatured type %s"
22292 " referenced from DIE at %s [in module %s]"),
22293 hex_string (signature), sect_offset_str (die->sect_off),
22294 objfile_name (dwarf2_per_objfile->objfile));
22295 type = build_error_marker_type (cu, die);
22296 }
22297 }
22298 else
22299 {
22300 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22301 " from DIE at %s [in module %s]"),
22302 hex_string (signature), sect_offset_str (die->sect_off),
22303 objfile_name (dwarf2_per_objfile->objfile));
22304 type = build_error_marker_type (cu, die);
22305 }
22306 sig_type->type = type;
22307
22308 return type;
22309 }
22310
22311 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22312 reading in and processing the type unit if necessary. */
22313
22314 static struct type *
22315 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22316 struct dwarf2_cu *cu) /* ARI: editCase function */
22317 {
22318 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22319 if (attr->form_is_ref ())
22320 {
22321 struct dwarf2_cu *type_cu = cu;
22322 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22323
22324 return read_type_die (type_die, type_cu);
22325 }
22326 else if (attr->form == DW_FORM_ref_sig8)
22327 {
22328 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22329 }
22330 else
22331 {
22332 struct dwarf2_per_objfile *dwarf2_per_objfile
22333 = cu->per_cu->dwarf2_per_objfile;
22334
22335 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22336 " at %s [in module %s]"),
22337 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22338 objfile_name (dwarf2_per_objfile->objfile));
22339 return build_error_marker_type (cu, die);
22340 }
22341 }
22342
22343 /* Load the DIEs associated with type unit PER_CU into memory. */
22344
22345 static void
22346 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22347 {
22348 struct signatured_type *sig_type;
22349
22350 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22351 gdb_assert (! per_cu->type_unit_group_p ());
22352
22353 /* We have the per_cu, but we need the signatured_type.
22354 Fortunately this is an easy translation. */
22355 gdb_assert (per_cu->is_debug_types);
22356 sig_type = (struct signatured_type *) per_cu;
22357
22358 gdb_assert (per_cu->cu == NULL);
22359
22360 read_signatured_type (sig_type);
22361
22362 gdb_assert (per_cu->cu != NULL);
22363 }
22364
22365 /* Read in a signatured type and build its CU and DIEs.
22366 If the type is a stub for the real type in a DWO file,
22367 read in the real type from the DWO file as well. */
22368
22369 static void
22370 read_signatured_type (struct signatured_type *sig_type)
22371 {
22372 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22373
22374 gdb_assert (per_cu->is_debug_types);
22375 gdb_assert (per_cu->cu == NULL);
22376
22377 cutu_reader reader (per_cu, NULL, 0, false);
22378
22379 if (!reader.dummy_p)
22380 {
22381 struct dwarf2_cu *cu = reader.cu;
22382 const gdb_byte *info_ptr = reader.info_ptr;
22383
22384 gdb_assert (cu->die_hash == NULL);
22385 cu->die_hash =
22386 htab_create_alloc_ex (cu->header.length / 12,
22387 die_hash,
22388 die_eq,
22389 NULL,
22390 &cu->comp_unit_obstack,
22391 hashtab_obstack_allocate,
22392 dummy_obstack_deallocate);
22393
22394 if (reader.comp_unit_die->has_children)
22395 reader.comp_unit_die->child
22396 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22397 reader.comp_unit_die);
22398 cu->dies = reader.comp_unit_die;
22399 /* comp_unit_die is not stored in die_hash, no need. */
22400
22401 /* We try not to read any attributes in this function, because
22402 not all CUs needed for references have been loaded yet, and
22403 symbol table processing isn't initialized. But we have to
22404 set the CU language, or we won't be able to build types
22405 correctly. Similarly, if we do not read the producer, we can
22406 not apply producer-specific interpretation. */
22407 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22408
22409 reader.keep ();
22410 }
22411
22412 sig_type->per_cu.tu_read = 1;
22413 }
22414
22415 /* Decode simple location descriptions.
22416 Given a pointer to a dwarf block that defines a location, compute
22417 the location and return the value.
22418
22419 NOTE drow/2003-11-18: This function is called in two situations
22420 now: for the address of static or global variables (partial symbols
22421 only) and for offsets into structures which are expected to be
22422 (more or less) constant. The partial symbol case should go away,
22423 and only the constant case should remain. That will let this
22424 function complain more accurately. A few special modes are allowed
22425 without complaint for global variables (for instance, global
22426 register values and thread-local values).
22427
22428 A location description containing no operations indicates that the
22429 object is optimized out. The return value is 0 for that case.
22430 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22431 callers will only want a very basic result and this can become a
22432 complaint.
22433
22434 Note that stack[0] is unused except as a default error return. */
22435
22436 static CORE_ADDR
22437 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22438 {
22439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22440 size_t i;
22441 size_t size = blk->size;
22442 const gdb_byte *data = blk->data;
22443 CORE_ADDR stack[64];
22444 int stacki;
22445 unsigned int bytes_read, unsnd;
22446 gdb_byte op;
22447
22448 i = 0;
22449 stacki = 0;
22450 stack[stacki] = 0;
22451 stack[++stacki] = 0;
22452
22453 while (i < size)
22454 {
22455 op = data[i++];
22456 switch (op)
22457 {
22458 case DW_OP_lit0:
22459 case DW_OP_lit1:
22460 case DW_OP_lit2:
22461 case DW_OP_lit3:
22462 case DW_OP_lit4:
22463 case DW_OP_lit5:
22464 case DW_OP_lit6:
22465 case DW_OP_lit7:
22466 case DW_OP_lit8:
22467 case DW_OP_lit9:
22468 case DW_OP_lit10:
22469 case DW_OP_lit11:
22470 case DW_OP_lit12:
22471 case DW_OP_lit13:
22472 case DW_OP_lit14:
22473 case DW_OP_lit15:
22474 case DW_OP_lit16:
22475 case DW_OP_lit17:
22476 case DW_OP_lit18:
22477 case DW_OP_lit19:
22478 case DW_OP_lit20:
22479 case DW_OP_lit21:
22480 case DW_OP_lit22:
22481 case DW_OP_lit23:
22482 case DW_OP_lit24:
22483 case DW_OP_lit25:
22484 case DW_OP_lit26:
22485 case DW_OP_lit27:
22486 case DW_OP_lit28:
22487 case DW_OP_lit29:
22488 case DW_OP_lit30:
22489 case DW_OP_lit31:
22490 stack[++stacki] = op - DW_OP_lit0;
22491 break;
22492
22493 case DW_OP_reg0:
22494 case DW_OP_reg1:
22495 case DW_OP_reg2:
22496 case DW_OP_reg3:
22497 case DW_OP_reg4:
22498 case DW_OP_reg5:
22499 case DW_OP_reg6:
22500 case DW_OP_reg7:
22501 case DW_OP_reg8:
22502 case DW_OP_reg9:
22503 case DW_OP_reg10:
22504 case DW_OP_reg11:
22505 case DW_OP_reg12:
22506 case DW_OP_reg13:
22507 case DW_OP_reg14:
22508 case DW_OP_reg15:
22509 case DW_OP_reg16:
22510 case DW_OP_reg17:
22511 case DW_OP_reg18:
22512 case DW_OP_reg19:
22513 case DW_OP_reg20:
22514 case DW_OP_reg21:
22515 case DW_OP_reg22:
22516 case DW_OP_reg23:
22517 case DW_OP_reg24:
22518 case DW_OP_reg25:
22519 case DW_OP_reg26:
22520 case DW_OP_reg27:
22521 case DW_OP_reg28:
22522 case DW_OP_reg29:
22523 case DW_OP_reg30:
22524 case DW_OP_reg31:
22525 stack[++stacki] = op - DW_OP_reg0;
22526 if (i < size)
22527 dwarf2_complex_location_expr_complaint ();
22528 break;
22529
22530 case DW_OP_regx:
22531 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22532 i += bytes_read;
22533 stack[++stacki] = unsnd;
22534 if (i < size)
22535 dwarf2_complex_location_expr_complaint ();
22536 break;
22537
22538 case DW_OP_addr:
22539 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22540 &bytes_read);
22541 i += bytes_read;
22542 break;
22543
22544 case DW_OP_const1u:
22545 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22546 i += 1;
22547 break;
22548
22549 case DW_OP_const1s:
22550 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22551 i += 1;
22552 break;
22553
22554 case DW_OP_const2u:
22555 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22556 i += 2;
22557 break;
22558
22559 case DW_OP_const2s:
22560 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22561 i += 2;
22562 break;
22563
22564 case DW_OP_const4u:
22565 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22566 i += 4;
22567 break;
22568
22569 case DW_OP_const4s:
22570 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22571 i += 4;
22572 break;
22573
22574 case DW_OP_const8u:
22575 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22576 i += 8;
22577 break;
22578
22579 case DW_OP_constu:
22580 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22581 &bytes_read);
22582 i += bytes_read;
22583 break;
22584
22585 case DW_OP_consts:
22586 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22587 i += bytes_read;
22588 break;
22589
22590 case DW_OP_dup:
22591 stack[stacki + 1] = stack[stacki];
22592 stacki++;
22593 break;
22594
22595 case DW_OP_plus:
22596 stack[stacki - 1] += stack[stacki];
22597 stacki--;
22598 break;
22599
22600 case DW_OP_plus_uconst:
22601 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22602 &bytes_read);
22603 i += bytes_read;
22604 break;
22605
22606 case DW_OP_minus:
22607 stack[stacki - 1] -= stack[stacki];
22608 stacki--;
22609 break;
22610
22611 case DW_OP_deref:
22612 /* If we're not the last op, then we definitely can't encode
22613 this using GDB's address_class enum. This is valid for partial
22614 global symbols, although the variable's address will be bogus
22615 in the psymtab. */
22616 if (i < size)
22617 dwarf2_complex_location_expr_complaint ();
22618 break;
22619
22620 case DW_OP_GNU_push_tls_address:
22621 case DW_OP_form_tls_address:
22622 /* The top of the stack has the offset from the beginning
22623 of the thread control block at which the variable is located. */
22624 /* Nothing should follow this operator, so the top of stack would
22625 be returned. */
22626 /* This is valid for partial global symbols, but the variable's
22627 address will be bogus in the psymtab. Make it always at least
22628 non-zero to not look as a variable garbage collected by linker
22629 which have DW_OP_addr 0. */
22630 if (i < size)
22631 dwarf2_complex_location_expr_complaint ();
22632 stack[stacki]++;
22633 break;
22634
22635 case DW_OP_GNU_uninit:
22636 break;
22637
22638 case DW_OP_addrx:
22639 case DW_OP_GNU_addr_index:
22640 case DW_OP_GNU_const_index:
22641 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22642 &bytes_read);
22643 i += bytes_read;
22644 break;
22645
22646 default:
22647 {
22648 const char *name = get_DW_OP_name (op);
22649
22650 if (name)
22651 complaint (_("unsupported stack op: '%s'"),
22652 name);
22653 else
22654 complaint (_("unsupported stack op: '%02x'"),
22655 op);
22656 }
22657
22658 return (stack[stacki]);
22659 }
22660
22661 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22662 outside of the allocated space. Also enforce minimum>0. */
22663 if (stacki >= ARRAY_SIZE (stack) - 1)
22664 {
22665 complaint (_("location description stack overflow"));
22666 return 0;
22667 }
22668
22669 if (stacki <= 0)
22670 {
22671 complaint (_("location description stack underflow"));
22672 return 0;
22673 }
22674 }
22675 return (stack[stacki]);
22676 }
22677
22678 /* memory allocation interface */
22679
22680 static struct dwarf_block *
22681 dwarf_alloc_block (struct dwarf2_cu *cu)
22682 {
22683 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22684 }
22685
22686 static struct die_info *
22687 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22688 {
22689 struct die_info *die;
22690 size_t size = sizeof (struct die_info);
22691
22692 if (num_attrs > 1)
22693 size += (num_attrs - 1) * sizeof (struct attribute);
22694
22695 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22696 memset (die, 0, sizeof (struct die_info));
22697 return (die);
22698 }
22699
22700 \f
22701
22702 /* Macro support. */
22703
22704 /* An overload of dwarf_decode_macros that finds the correct section
22705 and ensures it is read in before calling the other overload. */
22706
22707 static void
22708 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22709 int section_is_gnu)
22710 {
22711 struct dwarf2_per_objfile *dwarf2_per_objfile
22712 = cu->per_cu->dwarf2_per_objfile;
22713 struct objfile *objfile = dwarf2_per_objfile->objfile;
22714 const struct line_header *lh = cu->line_header;
22715 unsigned int offset_size = cu->header.offset_size;
22716 struct dwarf2_section_info *section;
22717 const char *section_name;
22718
22719 if (cu->dwo_unit != nullptr)
22720 {
22721 if (section_is_gnu)
22722 {
22723 section = &cu->dwo_unit->dwo_file->sections.macro;
22724 section_name = ".debug_macro.dwo";
22725 }
22726 else
22727 {
22728 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22729 section_name = ".debug_macinfo.dwo";
22730 }
22731 }
22732 else
22733 {
22734 if (section_is_gnu)
22735 {
22736 section = &dwarf2_per_objfile->macro;
22737 section_name = ".debug_macro";
22738 }
22739 else
22740 {
22741 section = &dwarf2_per_objfile->macinfo;
22742 section_name = ".debug_macinfo";
22743 }
22744 }
22745
22746 section->read (objfile);
22747 if (section->buffer == nullptr)
22748 {
22749 complaint (_("missing %s section"), section_name);
22750 return;
22751 }
22752
22753 buildsym_compunit *builder = cu->get_builder ();
22754
22755 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22756 offset_size, offset, section_is_gnu);
22757 }
22758
22759 /* Return the .debug_loc section to use for CU.
22760 For DWO files use .debug_loc.dwo. */
22761
22762 static struct dwarf2_section_info *
22763 cu_debug_loc_section (struct dwarf2_cu *cu)
22764 {
22765 struct dwarf2_per_objfile *dwarf2_per_objfile
22766 = cu->per_cu->dwarf2_per_objfile;
22767
22768 if (cu->dwo_unit)
22769 {
22770 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22771
22772 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22773 }
22774 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22775 : &dwarf2_per_objfile->loc);
22776 }
22777
22778 /* A helper function that fills in a dwarf2_loclist_baton. */
22779
22780 static void
22781 fill_in_loclist_baton (struct dwarf2_cu *cu,
22782 struct dwarf2_loclist_baton *baton,
22783 const struct attribute *attr)
22784 {
22785 struct dwarf2_per_objfile *dwarf2_per_objfile
22786 = cu->per_cu->dwarf2_per_objfile;
22787 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22788
22789 section->read (dwarf2_per_objfile->objfile);
22790
22791 baton->per_cu = cu->per_cu;
22792 gdb_assert (baton->per_cu);
22793 /* We don't know how long the location list is, but make sure we
22794 don't run off the edge of the section. */
22795 baton->size = section->size - DW_UNSND (attr);
22796 baton->data = section->buffer + DW_UNSND (attr);
22797 baton->base_address = cu->base_address;
22798 baton->from_dwo = cu->dwo_unit != NULL;
22799 }
22800
22801 static void
22802 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22803 struct dwarf2_cu *cu, int is_block)
22804 {
22805 struct dwarf2_per_objfile *dwarf2_per_objfile
22806 = cu->per_cu->dwarf2_per_objfile;
22807 struct objfile *objfile = dwarf2_per_objfile->objfile;
22808 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22809
22810 if (attr->form_is_section_offset ()
22811 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22812 the section. If so, fall through to the complaint in the
22813 other branch. */
22814 && DW_UNSND (attr) < section->get_size (objfile))
22815 {
22816 struct dwarf2_loclist_baton *baton;
22817
22818 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22819
22820 fill_in_loclist_baton (cu, baton, attr);
22821
22822 if (cu->base_known == 0)
22823 complaint (_("Location list used without "
22824 "specifying the CU base address."));
22825
22826 SYMBOL_ACLASS_INDEX (sym) = (is_block
22827 ? dwarf2_loclist_block_index
22828 : dwarf2_loclist_index);
22829 SYMBOL_LOCATION_BATON (sym) = baton;
22830 }
22831 else
22832 {
22833 struct dwarf2_locexpr_baton *baton;
22834
22835 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22836 baton->per_cu = cu->per_cu;
22837 gdb_assert (baton->per_cu);
22838
22839 if (attr->form_is_block ())
22840 {
22841 /* Note that we're just copying the block's data pointer
22842 here, not the actual data. We're still pointing into the
22843 info_buffer for SYM's objfile; right now we never release
22844 that buffer, but when we do clean up properly this may
22845 need to change. */
22846 baton->size = DW_BLOCK (attr)->size;
22847 baton->data = DW_BLOCK (attr)->data;
22848 }
22849 else
22850 {
22851 dwarf2_invalid_attrib_class_complaint ("location description",
22852 sym->natural_name ());
22853 baton->size = 0;
22854 }
22855
22856 SYMBOL_ACLASS_INDEX (sym) = (is_block
22857 ? dwarf2_locexpr_block_index
22858 : dwarf2_locexpr_index);
22859 SYMBOL_LOCATION_BATON (sym) = baton;
22860 }
22861 }
22862
22863 /* See read.h. */
22864
22865 struct objfile *
22866 dwarf2_per_cu_data::objfile () const
22867 {
22868 struct objfile *objfile = dwarf2_per_objfile->objfile;
22869
22870 /* Return the master objfile, so that we can report and look up the
22871 correct file containing this variable. */
22872 if (objfile->separate_debug_objfile_backlink)
22873 objfile = objfile->separate_debug_objfile_backlink;
22874
22875 return objfile;
22876 }
22877
22878 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22879 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22880 CU_HEADERP first. */
22881
22882 static const struct comp_unit_head *
22883 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22884 const struct dwarf2_per_cu_data *per_cu)
22885 {
22886 const gdb_byte *info_ptr;
22887
22888 if (per_cu->cu)
22889 return &per_cu->cu->header;
22890
22891 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22892
22893 memset (cu_headerp, 0, sizeof (*cu_headerp));
22894 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22895 rcuh_kind::COMPILE);
22896
22897 return cu_headerp;
22898 }
22899
22900 /* See read.h. */
22901
22902 int
22903 dwarf2_per_cu_data::addr_size () const
22904 {
22905 struct comp_unit_head cu_header_local;
22906 const struct comp_unit_head *cu_headerp;
22907
22908 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22909
22910 return cu_headerp->addr_size;
22911 }
22912
22913 /* See read.h. */
22914
22915 int
22916 dwarf2_per_cu_data::offset_size () const
22917 {
22918 struct comp_unit_head cu_header_local;
22919 const struct comp_unit_head *cu_headerp;
22920
22921 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22922
22923 return cu_headerp->offset_size;
22924 }
22925
22926 /* See read.h. */
22927
22928 int
22929 dwarf2_per_cu_data::ref_addr_size () const
22930 {
22931 struct comp_unit_head cu_header_local;
22932 const struct comp_unit_head *cu_headerp;
22933
22934 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22935
22936 if (cu_headerp->version == 2)
22937 return cu_headerp->addr_size;
22938 else
22939 return cu_headerp->offset_size;
22940 }
22941
22942 /* See read.h. */
22943
22944 CORE_ADDR
22945 dwarf2_per_cu_data::text_offset () const
22946 {
22947 struct objfile *objfile = dwarf2_per_objfile->objfile;
22948
22949 return objfile->text_section_offset ();
22950 }
22951
22952 /* See read.h. */
22953
22954 struct type *
22955 dwarf2_per_cu_data::addr_type () const
22956 {
22957 struct objfile *objfile = dwarf2_per_objfile->objfile;
22958 struct type *void_type = objfile_type (objfile)->builtin_void;
22959 struct type *addr_type = lookup_pointer_type (void_type);
22960 int addr_size = this->addr_size ();
22961
22962 if (TYPE_LENGTH (addr_type) == addr_size)
22963 return addr_type;
22964
22965 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22966 return addr_type;
22967 }
22968
22969 /* A helper function for dwarf2_find_containing_comp_unit that returns
22970 the index of the result, and that searches a vector. It will
22971 return a result even if the offset in question does not actually
22972 occur in any CU. This is separate so that it can be unit
22973 tested. */
22974
22975 static int
22976 dwarf2_find_containing_comp_unit
22977 (sect_offset sect_off,
22978 unsigned int offset_in_dwz,
22979 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22980 {
22981 int low, high;
22982
22983 low = 0;
22984 high = all_comp_units.size () - 1;
22985 while (high > low)
22986 {
22987 struct dwarf2_per_cu_data *mid_cu;
22988 int mid = low + (high - low) / 2;
22989
22990 mid_cu = all_comp_units[mid];
22991 if (mid_cu->is_dwz > offset_in_dwz
22992 || (mid_cu->is_dwz == offset_in_dwz
22993 && mid_cu->sect_off + mid_cu->length > sect_off))
22994 high = mid;
22995 else
22996 low = mid + 1;
22997 }
22998 gdb_assert (low == high);
22999 return low;
23000 }
23001
23002 /* Locate the .debug_info compilation unit from CU's objfile which contains
23003 the DIE at OFFSET. Raises an error on failure. */
23004
23005 static struct dwarf2_per_cu_data *
23006 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23007 unsigned int offset_in_dwz,
23008 struct dwarf2_per_objfile *dwarf2_per_objfile)
23009 {
23010 int low
23011 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23012 dwarf2_per_objfile->all_comp_units);
23013 struct dwarf2_per_cu_data *this_cu
23014 = dwarf2_per_objfile->all_comp_units[low];
23015
23016 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23017 {
23018 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23019 error (_("Dwarf Error: could not find partial DIE containing "
23020 "offset %s [in module %s]"),
23021 sect_offset_str (sect_off),
23022 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23023
23024 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23025 <= sect_off);
23026 return dwarf2_per_objfile->all_comp_units[low-1];
23027 }
23028 else
23029 {
23030 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
23031 && sect_off >= this_cu->sect_off + this_cu->length)
23032 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23033 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23034 return this_cu;
23035 }
23036 }
23037
23038 #if GDB_SELF_TEST
23039
23040 namespace selftests {
23041 namespace find_containing_comp_unit {
23042
23043 static void
23044 run_test ()
23045 {
23046 struct dwarf2_per_cu_data one {};
23047 struct dwarf2_per_cu_data two {};
23048 struct dwarf2_per_cu_data three {};
23049 struct dwarf2_per_cu_data four {};
23050
23051 one.length = 5;
23052 two.sect_off = sect_offset (one.length);
23053 two.length = 7;
23054
23055 three.length = 5;
23056 three.is_dwz = 1;
23057 four.sect_off = sect_offset (three.length);
23058 four.length = 7;
23059 four.is_dwz = 1;
23060
23061 std::vector<dwarf2_per_cu_data *> units;
23062 units.push_back (&one);
23063 units.push_back (&two);
23064 units.push_back (&three);
23065 units.push_back (&four);
23066
23067 int result;
23068
23069 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23070 SELF_CHECK (units[result] == &one);
23071 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23072 SELF_CHECK (units[result] == &one);
23073 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23074 SELF_CHECK (units[result] == &two);
23075
23076 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23077 SELF_CHECK (units[result] == &three);
23078 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23079 SELF_CHECK (units[result] == &three);
23080 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23081 SELF_CHECK (units[result] == &four);
23082 }
23083
23084 }
23085 }
23086
23087 #endif /* GDB_SELF_TEST */
23088
23089 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23090
23091 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23092 : per_cu (per_cu_),
23093 mark (false),
23094 has_loclist (false),
23095 checked_producer (false),
23096 producer_is_gxx_lt_4_6 (false),
23097 producer_is_gcc_lt_4_3 (false),
23098 producer_is_icc (false),
23099 producer_is_icc_lt_14 (false),
23100 producer_is_codewarrior (false),
23101 processing_has_namespace_info (false)
23102 {
23103 per_cu->cu = this;
23104 }
23105
23106 /* Destroy a dwarf2_cu. */
23107
23108 dwarf2_cu::~dwarf2_cu ()
23109 {
23110 per_cu->cu = NULL;
23111 }
23112
23113 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23114
23115 static void
23116 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23117 enum language pretend_language)
23118 {
23119 struct attribute *attr;
23120
23121 /* Set the language we're debugging. */
23122 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23123 if (attr != nullptr)
23124 set_cu_language (DW_UNSND (attr), cu);
23125 else
23126 {
23127 cu->language = pretend_language;
23128 cu->language_defn = language_def (cu->language);
23129 }
23130
23131 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23132 }
23133
23134 /* Increase the age counter on each cached compilation unit, and free
23135 any that are too old. */
23136
23137 static void
23138 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23139 {
23140 struct dwarf2_per_cu_data *per_cu, **last_chain;
23141
23142 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23143 per_cu = dwarf2_per_objfile->read_in_chain;
23144 while (per_cu != NULL)
23145 {
23146 per_cu->cu->last_used ++;
23147 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23148 dwarf2_mark (per_cu->cu);
23149 per_cu = per_cu->cu->read_in_chain;
23150 }
23151
23152 per_cu = dwarf2_per_objfile->read_in_chain;
23153 last_chain = &dwarf2_per_objfile->read_in_chain;
23154 while (per_cu != NULL)
23155 {
23156 struct dwarf2_per_cu_data *next_cu;
23157
23158 next_cu = per_cu->cu->read_in_chain;
23159
23160 if (!per_cu->cu->mark)
23161 {
23162 delete per_cu->cu;
23163 *last_chain = next_cu;
23164 }
23165 else
23166 last_chain = &per_cu->cu->read_in_chain;
23167
23168 per_cu = next_cu;
23169 }
23170 }
23171
23172 /* Remove a single compilation unit from the cache. */
23173
23174 static void
23175 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23176 {
23177 struct dwarf2_per_cu_data *per_cu, **last_chain;
23178 struct dwarf2_per_objfile *dwarf2_per_objfile
23179 = target_per_cu->dwarf2_per_objfile;
23180
23181 per_cu = dwarf2_per_objfile->read_in_chain;
23182 last_chain = &dwarf2_per_objfile->read_in_chain;
23183 while (per_cu != NULL)
23184 {
23185 struct dwarf2_per_cu_data *next_cu;
23186
23187 next_cu = per_cu->cu->read_in_chain;
23188
23189 if (per_cu == target_per_cu)
23190 {
23191 delete per_cu->cu;
23192 per_cu->cu = NULL;
23193 *last_chain = next_cu;
23194 break;
23195 }
23196 else
23197 last_chain = &per_cu->cu->read_in_chain;
23198
23199 per_cu = next_cu;
23200 }
23201 }
23202
23203 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23204 We store these in a hash table separate from the DIEs, and preserve them
23205 when the DIEs are flushed out of cache.
23206
23207 The CU "per_cu" pointer is needed because offset alone is not enough to
23208 uniquely identify the type. A file may have multiple .debug_types sections,
23209 or the type may come from a DWO file. Furthermore, while it's more logical
23210 to use per_cu->section+offset, with Fission the section with the data is in
23211 the DWO file but we don't know that section at the point we need it.
23212 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23213 because we can enter the lookup routine, get_die_type_at_offset, from
23214 outside this file, and thus won't necessarily have PER_CU->cu.
23215 Fortunately, PER_CU is stable for the life of the objfile. */
23216
23217 struct dwarf2_per_cu_offset_and_type
23218 {
23219 const struct dwarf2_per_cu_data *per_cu;
23220 sect_offset sect_off;
23221 struct type *type;
23222 };
23223
23224 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23225
23226 static hashval_t
23227 per_cu_offset_and_type_hash (const void *item)
23228 {
23229 const struct dwarf2_per_cu_offset_and_type *ofs
23230 = (const struct dwarf2_per_cu_offset_and_type *) item;
23231
23232 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23233 }
23234
23235 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23236
23237 static int
23238 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23239 {
23240 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23241 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23242 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23243 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23244
23245 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23246 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23247 }
23248
23249 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23250 table if necessary. For convenience, return TYPE.
23251
23252 The DIEs reading must have careful ordering to:
23253 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23254 reading current DIE.
23255 * Not trying to dereference contents of still incompletely read in types
23256 while reading in other DIEs.
23257 * Enable referencing still incompletely read in types just by a pointer to
23258 the type without accessing its fields.
23259
23260 Therefore caller should follow these rules:
23261 * Try to fetch any prerequisite types we may need to build this DIE type
23262 before building the type and calling set_die_type.
23263 * After building type call set_die_type for current DIE as soon as
23264 possible before fetching more types to complete the current type.
23265 * Make the type as complete as possible before fetching more types. */
23266
23267 static struct type *
23268 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23269 {
23270 struct dwarf2_per_objfile *dwarf2_per_objfile
23271 = cu->per_cu->dwarf2_per_objfile;
23272 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23273 struct objfile *objfile = dwarf2_per_objfile->objfile;
23274 struct attribute *attr;
23275 struct dynamic_prop prop;
23276
23277 /* For Ada types, make sure that the gnat-specific data is always
23278 initialized (if not already set). There are a few types where
23279 we should not be doing so, because the type-specific area is
23280 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23281 where the type-specific area is used to store the floatformat).
23282 But this is not a problem, because the gnat-specific information
23283 is actually not needed for these types. */
23284 if (need_gnat_info (cu)
23285 && TYPE_CODE (type) != TYPE_CODE_FUNC
23286 && TYPE_CODE (type) != TYPE_CODE_FLT
23287 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23288 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23289 && TYPE_CODE (type) != TYPE_CODE_METHOD
23290 && !HAVE_GNAT_AUX_INFO (type))
23291 INIT_GNAT_SPECIFIC (type);
23292
23293 /* Read DW_AT_allocated and set in type. */
23294 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23295 if (attr != NULL && attr->form_is_block ())
23296 {
23297 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23298 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23299 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23300 }
23301 else if (attr != NULL)
23302 {
23303 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23304 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23305 sect_offset_str (die->sect_off));
23306 }
23307
23308 /* Read DW_AT_associated and set in type. */
23309 attr = dwarf2_attr (die, DW_AT_associated, cu);
23310 if (attr != NULL && attr->form_is_block ())
23311 {
23312 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23313 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23314 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23315 }
23316 else if (attr != NULL)
23317 {
23318 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23319 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23320 sect_offset_str (die->sect_off));
23321 }
23322
23323 /* Read DW_AT_data_location and set in type. */
23324 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23325 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23326 cu->per_cu->addr_type ()))
23327 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23328
23329 if (dwarf2_per_objfile->die_type_hash == NULL)
23330 dwarf2_per_objfile->die_type_hash
23331 = htab_up (htab_create_alloc (127,
23332 per_cu_offset_and_type_hash,
23333 per_cu_offset_and_type_eq,
23334 NULL, xcalloc, xfree));
23335
23336 ofs.per_cu = cu->per_cu;
23337 ofs.sect_off = die->sect_off;
23338 ofs.type = type;
23339 slot = (struct dwarf2_per_cu_offset_and_type **)
23340 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23341 if (*slot)
23342 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23343 sect_offset_str (die->sect_off));
23344 *slot = XOBNEW (&objfile->objfile_obstack,
23345 struct dwarf2_per_cu_offset_and_type);
23346 **slot = ofs;
23347 return type;
23348 }
23349
23350 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23351 or return NULL if the die does not have a saved type. */
23352
23353 static struct type *
23354 get_die_type_at_offset (sect_offset sect_off,
23355 struct dwarf2_per_cu_data *per_cu)
23356 {
23357 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23358 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23359
23360 if (dwarf2_per_objfile->die_type_hash == NULL)
23361 return NULL;
23362
23363 ofs.per_cu = per_cu;
23364 ofs.sect_off = sect_off;
23365 slot = ((struct dwarf2_per_cu_offset_and_type *)
23366 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23367 if (slot)
23368 return slot->type;
23369 else
23370 return NULL;
23371 }
23372
23373 /* Look up the type for DIE in CU in die_type_hash,
23374 or return NULL if DIE does not have a saved type. */
23375
23376 static struct type *
23377 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23378 {
23379 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23380 }
23381
23382 /* Add a dependence relationship from CU to REF_PER_CU. */
23383
23384 static void
23385 dwarf2_add_dependence (struct dwarf2_cu *cu,
23386 struct dwarf2_per_cu_data *ref_per_cu)
23387 {
23388 void **slot;
23389
23390 if (cu->dependencies == NULL)
23391 cu->dependencies
23392 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23393 NULL, &cu->comp_unit_obstack,
23394 hashtab_obstack_allocate,
23395 dummy_obstack_deallocate);
23396
23397 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23398 if (*slot == NULL)
23399 *slot = ref_per_cu;
23400 }
23401
23402 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23403 Set the mark field in every compilation unit in the
23404 cache that we must keep because we are keeping CU. */
23405
23406 static int
23407 dwarf2_mark_helper (void **slot, void *data)
23408 {
23409 struct dwarf2_per_cu_data *per_cu;
23410
23411 per_cu = (struct dwarf2_per_cu_data *) *slot;
23412
23413 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23414 reading of the chain. As such dependencies remain valid it is not much
23415 useful to track and undo them during QUIT cleanups. */
23416 if (per_cu->cu == NULL)
23417 return 1;
23418
23419 if (per_cu->cu->mark)
23420 return 1;
23421 per_cu->cu->mark = true;
23422
23423 if (per_cu->cu->dependencies != NULL)
23424 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23425
23426 return 1;
23427 }
23428
23429 /* Set the mark field in CU and in every other compilation unit in the
23430 cache that we must keep because we are keeping CU. */
23431
23432 static void
23433 dwarf2_mark (struct dwarf2_cu *cu)
23434 {
23435 if (cu->mark)
23436 return;
23437 cu->mark = true;
23438 if (cu->dependencies != NULL)
23439 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23440 }
23441
23442 static void
23443 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23444 {
23445 while (per_cu)
23446 {
23447 per_cu->cu->mark = false;
23448 per_cu = per_cu->cu->read_in_chain;
23449 }
23450 }
23451
23452 /* Trivial hash function for partial_die_info: the hash value of a DIE
23453 is its offset in .debug_info for this objfile. */
23454
23455 static hashval_t
23456 partial_die_hash (const void *item)
23457 {
23458 const struct partial_die_info *part_die
23459 = (const struct partial_die_info *) item;
23460
23461 return to_underlying (part_die->sect_off);
23462 }
23463
23464 /* Trivial comparison function for partial_die_info structures: two DIEs
23465 are equal if they have the same offset. */
23466
23467 static int
23468 partial_die_eq (const void *item_lhs, const void *item_rhs)
23469 {
23470 const struct partial_die_info *part_die_lhs
23471 = (const struct partial_die_info *) item_lhs;
23472 const struct partial_die_info *part_die_rhs
23473 = (const struct partial_die_info *) item_rhs;
23474
23475 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23476 }
23477
23478 struct cmd_list_element *set_dwarf_cmdlist;
23479 struct cmd_list_element *show_dwarf_cmdlist;
23480
23481 static void
23482 set_dwarf_cmd (const char *args, int from_tty)
23483 {
23484 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23485 gdb_stdout);
23486 }
23487
23488 static void
23489 show_dwarf_cmd (const char *args, int from_tty)
23490 {
23491 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23492 }
23493
23494 static void
23495 show_check_physname (struct ui_file *file, int from_tty,
23496 struct cmd_list_element *c, const char *value)
23497 {
23498 fprintf_filtered (file,
23499 _("Whether to check \"physname\" is %s.\n"),
23500 value);
23501 }
23502
23503 void _initialize_dwarf2_read ();
23504 void
23505 _initialize_dwarf2_read ()
23506 {
23507 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23508 Set DWARF specific variables.\n\
23509 Configure DWARF variables such as the cache size."),
23510 &set_dwarf_cmdlist, "maintenance set dwarf ",
23511 0/*allow-unknown*/, &maintenance_set_cmdlist);
23512
23513 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23514 Show DWARF specific variables.\n\
23515 Show DWARF variables such as the cache size."),
23516 &show_dwarf_cmdlist, "maintenance show dwarf ",
23517 0/*allow-unknown*/, &maintenance_show_cmdlist);
23518
23519 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23520 &dwarf_max_cache_age, _("\
23521 Set the upper bound on the age of cached DWARF compilation units."), _("\
23522 Show the upper bound on the age of cached DWARF compilation units."), _("\
23523 A higher limit means that cached compilation units will be stored\n\
23524 in memory longer, and more total memory will be used. Zero disables\n\
23525 caching, which can slow down startup."),
23526 NULL,
23527 show_dwarf_max_cache_age,
23528 &set_dwarf_cmdlist,
23529 &show_dwarf_cmdlist);
23530
23531 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23532 Set debugging of the DWARF reader."), _("\
23533 Show debugging of the DWARF reader."), _("\
23534 When enabled (non-zero), debugging messages are printed during DWARF\n\
23535 reading and symtab expansion. A value of 1 (one) provides basic\n\
23536 information. A value greater than 1 provides more verbose information."),
23537 NULL,
23538 NULL,
23539 &setdebuglist, &showdebuglist);
23540
23541 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23542 Set debugging of the DWARF DIE reader."), _("\
23543 Show debugging of the DWARF DIE reader."), _("\
23544 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23545 The value is the maximum depth to print."),
23546 NULL,
23547 NULL,
23548 &setdebuglist, &showdebuglist);
23549
23550 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23551 Set debugging of the dwarf line reader."), _("\
23552 Show debugging of the dwarf line reader."), _("\
23553 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23554 A value of 1 (one) provides basic information.\n\
23555 A value greater than 1 provides more verbose information."),
23556 NULL,
23557 NULL,
23558 &setdebuglist, &showdebuglist);
23559
23560 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23561 Set cross-checking of \"physname\" code against demangler."), _("\
23562 Show cross-checking of \"physname\" code against demangler."), _("\
23563 When enabled, GDB's internal \"physname\" code is checked against\n\
23564 the demangler."),
23565 NULL, show_check_physname,
23566 &setdebuglist, &showdebuglist);
23567
23568 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23569 no_class, &use_deprecated_index_sections, _("\
23570 Set whether to use deprecated gdb_index sections."), _("\
23571 Show whether to use deprecated gdb_index sections."), _("\
23572 When enabled, deprecated .gdb_index sections are used anyway.\n\
23573 Normally they are ignored either because of a missing feature or\n\
23574 performance issue.\n\
23575 Warning: This option must be enabled before gdb reads the file."),
23576 NULL,
23577 NULL,
23578 &setlist, &showlist);
23579
23580 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23581 &dwarf2_locexpr_funcs);
23582 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23583 &dwarf2_loclist_funcs);
23584
23585 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23586 &dwarf2_block_frame_base_locexpr_funcs);
23587 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23588 &dwarf2_block_frame_base_loclist_funcs);
23589
23590 #if GDB_SELF_TEST
23591 selftests::register_test ("dw2_expand_symtabs_matching",
23592 selftests::dw2_expand_symtabs_matching::run_test);
23593 selftests::register_test ("dwarf2_find_containing_comp_unit",
23594 selftests::find_containing_comp_unit::run_test);
23595 #endif
23596 }
This page took 0.500107 seconds and 5 git commands to generate.