Convert read_indirect_line_string to a method
[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 LONGEST read_checked_initial_length_and_offset
1243 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1244 unsigned int *, unsigned int *);
1245
1246 static sect_offset read_abbrev_offset
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1248 struct dwarf2_section_info *, sect_offset);
1249
1250 static const char *read_indirect_string
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1252 const struct comp_unit_head *, unsigned int *);
1253
1254 static const char *read_indirect_string_at_offset
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1256
1257 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1258 const gdb_byte *,
1259 unsigned int *);
1260
1261 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1262 ULONGEST str_index);
1263
1264 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1265 ULONGEST str_index);
1266
1267 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1268
1269 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1270 struct dwarf2_cu *);
1271
1272 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1273 unsigned int);
1274
1275 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1276 struct dwarf2_cu *cu);
1277
1278 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1279
1280 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1281 struct dwarf2_cu *cu);
1282
1283 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1284
1285 static struct die_info *die_specification (struct die_info *die,
1286 struct dwarf2_cu **);
1287
1288 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1289 struct dwarf2_cu *cu);
1290
1291 static void dwarf_decode_lines (struct line_header *, const char *,
1292 struct dwarf2_cu *, dwarf2_psymtab *,
1293 CORE_ADDR, int decode_mapping);
1294
1295 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1296 const char *);
1297
1298 static struct symbol *new_symbol (struct die_info *, struct type *,
1299 struct dwarf2_cu *, struct symbol * = NULL);
1300
1301 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1302 struct dwarf2_cu *);
1303
1304 static void dwarf2_const_value_attr (const struct attribute *attr,
1305 struct type *type,
1306 const char *name,
1307 struct obstack *obstack,
1308 struct dwarf2_cu *cu, LONGEST *value,
1309 const gdb_byte **bytes,
1310 struct dwarf2_locexpr_baton **baton);
1311
1312 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1313
1314 static int need_gnat_info (struct dwarf2_cu *);
1315
1316 static struct type *die_descriptive_type (struct die_info *,
1317 struct dwarf2_cu *);
1318
1319 static void set_descriptive_type (struct type *, struct die_info *,
1320 struct dwarf2_cu *);
1321
1322 static struct type *die_containing_type (struct die_info *,
1323 struct dwarf2_cu *);
1324
1325 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1326 struct dwarf2_cu *);
1327
1328 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1329
1330 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1331
1332 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1333
1334 static char *typename_concat (struct obstack *obs, const char *prefix,
1335 const char *suffix, int physname,
1336 struct dwarf2_cu *cu);
1337
1338 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1339
1340 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1341
1342 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1343
1344 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1345
1346 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1347
1348 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1349
1350 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1351 struct dwarf2_cu *, dwarf2_psymtab *);
1352
1353 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1354 values. Keep the items ordered with increasing constraints compliance. */
1355 enum pc_bounds_kind
1356 {
1357 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1358 PC_BOUNDS_NOT_PRESENT,
1359
1360 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1361 were present but they do not form a valid range of PC addresses. */
1362 PC_BOUNDS_INVALID,
1363
1364 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1365 PC_BOUNDS_RANGES,
1366
1367 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1368 PC_BOUNDS_HIGH_LOW,
1369 };
1370
1371 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1372 CORE_ADDR *, CORE_ADDR *,
1373 struct dwarf2_cu *,
1374 dwarf2_psymtab *);
1375
1376 static void get_scope_pc_bounds (struct die_info *,
1377 CORE_ADDR *, CORE_ADDR *,
1378 struct dwarf2_cu *);
1379
1380 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1381 CORE_ADDR, struct dwarf2_cu *);
1382
1383 static void dwarf2_add_field (struct field_info *, struct die_info *,
1384 struct dwarf2_cu *);
1385
1386 static void dwarf2_attach_fields_to_type (struct field_info *,
1387 struct type *, struct dwarf2_cu *);
1388
1389 static void dwarf2_add_member_fn (struct field_info *,
1390 struct die_info *, struct type *,
1391 struct dwarf2_cu *);
1392
1393 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1394 struct type *,
1395 struct dwarf2_cu *);
1396
1397 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1398
1399 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1400
1401 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1402
1403 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1404
1405 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1406
1407 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1408
1409 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1410
1411 static struct type *read_module_type (struct die_info *die,
1412 struct dwarf2_cu *cu);
1413
1414 static const char *namespace_name (struct die_info *die,
1415 int *is_anonymous, struct dwarf2_cu *);
1416
1417 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1418
1419 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1420
1421 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1422 struct dwarf2_cu *);
1423
1424 static struct die_info *read_die_and_siblings_1
1425 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1426 struct die_info *);
1427
1428 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1429 const gdb_byte *info_ptr,
1430 const gdb_byte **new_info_ptr,
1431 struct die_info *parent);
1432
1433 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1434 struct die_info **, const gdb_byte *,
1435 int);
1436
1437 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1438 struct die_info **, const gdb_byte *);
1439
1440 static void process_die (struct die_info *, struct dwarf2_cu *);
1441
1442 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1443 struct objfile *);
1444
1445 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1446
1447 static const char *dwarf2_full_name (const char *name,
1448 struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
1451 static const char *dwarf2_physname (const char *name, struct die_info *die,
1452 struct dwarf2_cu *cu);
1453
1454 static struct die_info *dwarf2_extension (struct die_info *die,
1455 struct dwarf2_cu **);
1456
1457 static const char *dwarf_tag_name (unsigned int);
1458
1459 static const char *dwarf_attr_name (unsigned int);
1460
1461 static const char *dwarf_form_name (unsigned int);
1462
1463 static const char *dwarf_bool_name (unsigned int);
1464
1465 static const char *dwarf_type_encoding_name (unsigned int);
1466
1467 static struct die_info *sibling_die (struct die_info *);
1468
1469 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1470
1471 static void dump_die_for_error (struct die_info *);
1472
1473 static void dump_die_1 (struct ui_file *, int level, int max_level,
1474 struct die_info *);
1475
1476 /*static*/ void dump_die (struct die_info *, int max_level);
1477
1478 static void store_in_ref_table (struct die_info *,
1479 struct dwarf2_cu *);
1480
1481 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1482
1483 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1484
1485 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1486 const struct attribute *,
1487 struct dwarf2_cu **);
1488
1489 static struct die_info *follow_die_ref (struct die_info *,
1490 const struct attribute *,
1491 struct dwarf2_cu **);
1492
1493 static struct die_info *follow_die_sig (struct die_info *,
1494 const struct attribute *,
1495 struct dwarf2_cu **);
1496
1497 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1498 struct dwarf2_cu *);
1499
1500 static struct type *get_DW_AT_signature_type (struct die_info *,
1501 const struct attribute *,
1502 struct dwarf2_cu *);
1503
1504 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1505
1506 static void read_signatured_type (struct signatured_type *);
1507
1508 static int attr_to_dynamic_prop (const struct attribute *attr,
1509 struct die_info *die, struct dwarf2_cu *cu,
1510 struct dynamic_prop *prop, struct type *type);
1511
1512 /* memory allocation interface */
1513
1514 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1515
1516 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1517
1518 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1519
1520 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1521 struct dwarf2_loclist_baton *baton,
1522 const struct attribute *attr);
1523
1524 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1525 struct symbol *sym,
1526 struct dwarf2_cu *cu,
1527 int is_block);
1528
1529 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1530 const gdb_byte *info_ptr,
1531 struct abbrev_info *abbrev);
1532
1533 static hashval_t partial_die_hash (const void *item);
1534
1535 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1536
1537 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1538 (sect_offset sect_off, unsigned int offset_in_dwz,
1539 struct dwarf2_per_objfile *dwarf2_per_objfile);
1540
1541 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1542 struct die_info *comp_unit_die,
1543 enum language pretend_language);
1544
1545 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1546
1547 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1548
1549 static struct type *set_die_type (struct die_info *, struct type *,
1550 struct dwarf2_cu *);
1551
1552 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1553
1554 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1555
1556 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1557 enum language);
1558
1559 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1560 enum language);
1561
1562 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1563 enum language);
1564
1565 static void dwarf2_add_dependence (struct dwarf2_cu *,
1566 struct dwarf2_per_cu_data *);
1567
1568 static void dwarf2_mark (struct dwarf2_cu *);
1569
1570 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1571
1572 static struct type *get_die_type_at_offset (sect_offset,
1573 struct dwarf2_per_cu_data *);
1574
1575 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1576
1577 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1578 enum language pretend_language);
1579
1580 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1581
1582 /* Class, the destructor of which frees all allocated queue entries. This
1583 will only have work to do if an error was thrown while processing the
1584 dwarf. If no error was thrown then the queue entries should have all
1585 been processed, and freed, as we went along. */
1586
1587 class dwarf2_queue_guard
1588 {
1589 public:
1590 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1591 : m_per_objfile (per_objfile)
1592 {
1593 }
1594
1595 /* Free any entries remaining on the queue. There should only be
1596 entries left if we hit an error while processing the dwarf. */
1597 ~dwarf2_queue_guard ()
1598 {
1599 /* Ensure that no memory is allocated by the queue. */
1600 std::queue<dwarf2_queue_item> empty;
1601 std::swap (m_per_objfile->queue, empty);
1602 }
1603
1604 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1605
1606 private:
1607 dwarf2_per_objfile *m_per_objfile;
1608 };
1609
1610 dwarf2_queue_item::~dwarf2_queue_item ()
1611 {
1612 /* Anything still marked queued is likely to be in an
1613 inconsistent state, so discard it. */
1614 if (per_cu->queued)
1615 {
1616 if (per_cu->cu != NULL)
1617 free_one_cached_comp_unit (per_cu);
1618 per_cu->queued = 0;
1619 }
1620 }
1621
1622 /* The return type of find_file_and_directory. Note, the enclosed
1623 string pointers are only valid while this object is valid. */
1624
1625 struct file_and_directory
1626 {
1627 /* The filename. This is never NULL. */
1628 const char *name;
1629
1630 /* The compilation directory. NULL if not known. If we needed to
1631 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1632 points directly to the DW_AT_comp_dir string attribute owned by
1633 the obstack that owns the DIE. */
1634 const char *comp_dir;
1635
1636 /* If we needed to build a new string for comp_dir, this is what
1637 owns the storage. */
1638 std::string comp_dir_storage;
1639 };
1640
1641 static file_and_directory find_file_and_directory (struct die_info *die,
1642 struct dwarf2_cu *cu);
1643
1644 static htab_up allocate_signatured_type_table ();
1645
1646 static htab_up allocate_dwo_unit_table ();
1647
1648 static struct dwo_unit *lookup_dwo_unit_in_dwp
1649 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1650 struct dwp_file *dwp_file, const char *comp_dir,
1651 ULONGEST signature, int is_debug_types);
1652
1653 static struct dwp_file *get_dwp_file
1654 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1655
1656 static struct dwo_unit *lookup_dwo_comp_unit
1657 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1658
1659 static struct dwo_unit *lookup_dwo_type_unit
1660 (struct signatured_type *, const char *, const char *);
1661
1662 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1663
1664 /* A unique pointer to a dwo_file. */
1665
1666 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1667
1668 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1669
1670 static void check_producer (struct dwarf2_cu *cu);
1671
1672 static void free_line_header_voidp (void *arg);
1673 \f
1674 /* Various complaints about symbol reading that don't abort the process. */
1675
1676 static void
1677 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1678 {
1679 complaint (_("statement list doesn't fit in .debug_line section"));
1680 }
1681
1682 static void
1683 dwarf2_debug_line_missing_file_complaint (void)
1684 {
1685 complaint (_(".debug_line section has line data without a file"));
1686 }
1687
1688 static void
1689 dwarf2_debug_line_missing_end_sequence_complaint (void)
1690 {
1691 complaint (_(".debug_line section has line "
1692 "program sequence without an end"));
1693 }
1694
1695 static void
1696 dwarf2_complex_location_expr_complaint (void)
1697 {
1698 complaint (_("location expression too complex"));
1699 }
1700
1701 static void
1702 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1703 int arg3)
1704 {
1705 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1706 arg1, arg2, arg3);
1707 }
1708
1709 static void
1710 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1711 {
1712 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1713 arg1, arg2);
1714 }
1715
1716 /* Hash function for line_header_hash. */
1717
1718 static hashval_t
1719 line_header_hash (const struct line_header *ofs)
1720 {
1721 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1722 }
1723
1724 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1725
1726 static hashval_t
1727 line_header_hash_voidp (const void *item)
1728 {
1729 const struct line_header *ofs = (const struct line_header *) item;
1730
1731 return line_header_hash (ofs);
1732 }
1733
1734 /* Equality function for line_header_hash. */
1735
1736 static int
1737 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1738 {
1739 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1740 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1741
1742 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1743 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1744 }
1745
1746 \f
1747
1748 /* See declaration. */
1749
1750 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1751 const dwarf2_debug_sections *names,
1752 bool can_copy_)
1753 : objfile (objfile_),
1754 can_copy (can_copy_)
1755 {
1756 if (names == NULL)
1757 names = &dwarf2_elf_names;
1758
1759 bfd *obfd = objfile->obfd;
1760
1761 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1762 locate_sections (obfd, sec, *names);
1763 }
1764
1765 dwarf2_per_objfile::~dwarf2_per_objfile ()
1766 {
1767 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1768 free_cached_comp_units ();
1769
1770 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1771 per_cu->imported_symtabs_free ();
1772
1773 for (signatured_type *sig_type : all_type_units)
1774 sig_type->per_cu.imported_symtabs_free ();
1775
1776 /* Everything else should be on the objfile obstack. */
1777 }
1778
1779 /* See declaration. */
1780
1781 void
1782 dwarf2_per_objfile::free_cached_comp_units ()
1783 {
1784 dwarf2_per_cu_data *per_cu = read_in_chain;
1785 dwarf2_per_cu_data **last_chain = &read_in_chain;
1786 while (per_cu != NULL)
1787 {
1788 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1789
1790 delete per_cu->cu;
1791 *last_chain = next_cu;
1792 per_cu = next_cu;
1793 }
1794 }
1795
1796 /* A helper class that calls free_cached_comp_units on
1797 destruction. */
1798
1799 class free_cached_comp_units
1800 {
1801 public:
1802
1803 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1804 : m_per_objfile (per_objfile)
1805 {
1806 }
1807
1808 ~free_cached_comp_units ()
1809 {
1810 m_per_objfile->free_cached_comp_units ();
1811 }
1812
1813 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1814
1815 private:
1816
1817 dwarf2_per_objfile *m_per_objfile;
1818 };
1819
1820 /* Try to locate the sections we need for DWARF 2 debugging
1821 information and return true if we have enough to do something.
1822 NAMES points to the dwarf2 section names, or is NULL if the standard
1823 ELF names are used. CAN_COPY is true for formats where symbol
1824 interposition is possible and so symbol values must follow copy
1825 relocation rules. */
1826
1827 int
1828 dwarf2_has_info (struct objfile *objfile,
1829 const struct dwarf2_debug_sections *names,
1830 bool can_copy)
1831 {
1832 if (objfile->flags & OBJF_READNEVER)
1833 return 0;
1834
1835 struct dwarf2_per_objfile *dwarf2_per_objfile
1836 = get_dwarf2_per_objfile (objfile);
1837
1838 if (dwarf2_per_objfile == NULL)
1839 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1840 names,
1841 can_copy);
1842
1843 return (!dwarf2_per_objfile->info.is_virtual
1844 && dwarf2_per_objfile->info.s.section != NULL
1845 && !dwarf2_per_objfile->abbrev.is_virtual
1846 && dwarf2_per_objfile->abbrev.s.section != NULL);
1847 }
1848
1849 /* When loading sections, we look either for uncompressed section or for
1850 compressed section names. */
1851
1852 static int
1853 section_is_p (const char *section_name,
1854 const struct dwarf2_section_names *names)
1855 {
1856 if (names->normal != NULL
1857 && strcmp (section_name, names->normal) == 0)
1858 return 1;
1859 if (names->compressed != NULL
1860 && strcmp (section_name, names->compressed) == 0)
1861 return 1;
1862 return 0;
1863 }
1864
1865 /* See declaration. */
1866
1867 void
1868 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1869 const dwarf2_debug_sections &names)
1870 {
1871 flagword aflag = bfd_section_flags (sectp);
1872
1873 if ((aflag & SEC_HAS_CONTENTS) == 0)
1874 {
1875 }
1876 else if (elf_section_data (sectp)->this_hdr.sh_size
1877 > bfd_get_file_size (abfd))
1878 {
1879 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1880 warning (_("Discarding section %s which has a section size (%s"
1881 ") larger than the file size [in module %s]"),
1882 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1883 bfd_get_filename (abfd));
1884 }
1885 else if (section_is_p (sectp->name, &names.info))
1886 {
1887 this->info.s.section = sectp;
1888 this->info.size = bfd_section_size (sectp);
1889 }
1890 else if (section_is_p (sectp->name, &names.abbrev))
1891 {
1892 this->abbrev.s.section = sectp;
1893 this->abbrev.size = bfd_section_size (sectp);
1894 }
1895 else if (section_is_p (sectp->name, &names.line))
1896 {
1897 this->line.s.section = sectp;
1898 this->line.size = bfd_section_size (sectp);
1899 }
1900 else if (section_is_p (sectp->name, &names.loc))
1901 {
1902 this->loc.s.section = sectp;
1903 this->loc.size = bfd_section_size (sectp);
1904 }
1905 else if (section_is_p (sectp->name, &names.loclists))
1906 {
1907 this->loclists.s.section = sectp;
1908 this->loclists.size = bfd_section_size (sectp);
1909 }
1910 else if (section_is_p (sectp->name, &names.macinfo))
1911 {
1912 this->macinfo.s.section = sectp;
1913 this->macinfo.size = bfd_section_size (sectp);
1914 }
1915 else if (section_is_p (sectp->name, &names.macro))
1916 {
1917 this->macro.s.section = sectp;
1918 this->macro.size = bfd_section_size (sectp);
1919 }
1920 else if (section_is_p (sectp->name, &names.str))
1921 {
1922 this->str.s.section = sectp;
1923 this->str.size = bfd_section_size (sectp);
1924 }
1925 else if (section_is_p (sectp->name, &names.str_offsets))
1926 {
1927 this->str_offsets.s.section = sectp;
1928 this->str_offsets.size = bfd_section_size (sectp);
1929 }
1930 else if (section_is_p (sectp->name, &names.line_str))
1931 {
1932 this->line_str.s.section = sectp;
1933 this->line_str.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.addr))
1936 {
1937 this->addr.s.section = sectp;
1938 this->addr.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.frame))
1941 {
1942 this->frame.s.section = sectp;
1943 this->frame.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.eh_frame))
1946 {
1947 this->eh_frame.s.section = sectp;
1948 this->eh_frame.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.ranges))
1951 {
1952 this->ranges.s.section = sectp;
1953 this->ranges.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.rnglists))
1956 {
1957 this->rnglists.s.section = sectp;
1958 this->rnglists.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.types))
1961 {
1962 struct dwarf2_section_info type_section;
1963
1964 memset (&type_section, 0, sizeof (type_section));
1965 type_section.s.section = sectp;
1966 type_section.size = bfd_section_size (sectp);
1967
1968 this->types.push_back (type_section);
1969 }
1970 else if (section_is_p (sectp->name, &names.gdb_index))
1971 {
1972 this->gdb_index.s.section = sectp;
1973 this->gdb_index.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.debug_names))
1976 {
1977 this->debug_names.s.section = sectp;
1978 this->debug_names.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.debug_aranges))
1981 {
1982 this->debug_aranges.s.section = sectp;
1983 this->debug_aranges.size = bfd_section_size (sectp);
1984 }
1985
1986 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1987 && bfd_section_vma (sectp) == 0)
1988 this->has_section_at_zero = true;
1989 }
1990
1991 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1992 SECTION_NAME. */
1993
1994 void
1995 dwarf2_get_section_info (struct objfile *objfile,
1996 enum dwarf2_section_enum sect,
1997 asection **sectp, const gdb_byte **bufp,
1998 bfd_size_type *sizep)
1999 {
2000 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2001 struct dwarf2_section_info *info;
2002
2003 /* We may see an objfile without any DWARF, in which case we just
2004 return nothing. */
2005 if (data == NULL)
2006 {
2007 *sectp = NULL;
2008 *bufp = NULL;
2009 *sizep = 0;
2010 return;
2011 }
2012 switch (sect)
2013 {
2014 case DWARF2_DEBUG_FRAME:
2015 info = &data->frame;
2016 break;
2017 case DWARF2_EH_FRAME:
2018 info = &data->eh_frame;
2019 break;
2020 default:
2021 gdb_assert_not_reached ("unexpected section");
2022 }
2023
2024 info->read (objfile);
2025
2026 *sectp = info->get_bfd_section ();
2027 *bufp = info->buffer;
2028 *sizep = info->size;
2029 }
2030
2031 /* A helper function to find the sections for a .dwz file. */
2032
2033 static void
2034 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2035 {
2036 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2037
2038 /* Note that we only support the standard ELF names, because .dwz
2039 is ELF-only (at the time of writing). */
2040 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2041 {
2042 dwz_file->abbrev.s.section = sectp;
2043 dwz_file->abbrev.size = bfd_section_size (sectp);
2044 }
2045 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2046 {
2047 dwz_file->info.s.section = sectp;
2048 dwz_file->info.size = bfd_section_size (sectp);
2049 }
2050 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2051 {
2052 dwz_file->str.s.section = sectp;
2053 dwz_file->str.size = bfd_section_size (sectp);
2054 }
2055 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2056 {
2057 dwz_file->line.s.section = sectp;
2058 dwz_file->line.size = bfd_section_size (sectp);
2059 }
2060 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2061 {
2062 dwz_file->macro.s.section = sectp;
2063 dwz_file->macro.size = bfd_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2066 {
2067 dwz_file->gdb_index.s.section = sectp;
2068 dwz_file->gdb_index.size = bfd_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2071 {
2072 dwz_file->debug_names.s.section = sectp;
2073 dwz_file->debug_names.size = bfd_section_size (sectp);
2074 }
2075 }
2076
2077 /* See dwarf2read.h. */
2078
2079 struct dwz_file *
2080 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2081 {
2082 const char *filename;
2083 bfd_size_type buildid_len_arg;
2084 size_t buildid_len;
2085 bfd_byte *buildid;
2086
2087 if (dwarf2_per_objfile->dwz_file != NULL)
2088 return dwarf2_per_objfile->dwz_file.get ();
2089
2090 bfd_set_error (bfd_error_no_error);
2091 gdb::unique_xmalloc_ptr<char> data
2092 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2093 &buildid_len_arg, &buildid));
2094 if (data == NULL)
2095 {
2096 if (bfd_get_error () == bfd_error_no_error)
2097 return NULL;
2098 error (_("could not read '.gnu_debugaltlink' section: %s"),
2099 bfd_errmsg (bfd_get_error ()));
2100 }
2101
2102 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2103
2104 buildid_len = (size_t) buildid_len_arg;
2105
2106 filename = data.get ();
2107
2108 std::string abs_storage;
2109 if (!IS_ABSOLUTE_PATH (filename))
2110 {
2111 gdb::unique_xmalloc_ptr<char> abs
2112 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2113
2114 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2115 filename = abs_storage.c_str ();
2116 }
2117
2118 /* First try the file name given in the section. If that doesn't
2119 work, try to use the build-id instead. */
2120 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2121 if (dwz_bfd != NULL)
2122 {
2123 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2124 dwz_bfd.reset (nullptr);
2125 }
2126
2127 if (dwz_bfd == NULL)
2128 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2129
2130 if (dwz_bfd == nullptr)
2131 {
2132 gdb::unique_xmalloc_ptr<char> alt_filename;
2133 const char *origname = dwarf2_per_objfile->objfile->original_name;
2134
2135 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2136 buildid_len,
2137 origname,
2138 &alt_filename));
2139
2140 if (fd.get () >= 0)
2141 {
2142 /* File successfully retrieved from server. */
2143 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2144
2145 if (dwz_bfd == nullptr)
2146 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2147 alt_filename.get ());
2148 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2149 dwz_bfd.reset (nullptr);
2150 }
2151 }
2152
2153 if (dwz_bfd == NULL)
2154 error (_("could not find '.gnu_debugaltlink' file for %s"),
2155 objfile_name (dwarf2_per_objfile->objfile));
2156
2157 std::unique_ptr<struct dwz_file> result
2158 (new struct dwz_file (std::move (dwz_bfd)));
2159
2160 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2161 result.get ());
2162
2163 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2164 result->dwz_bfd.get ());
2165 dwarf2_per_objfile->dwz_file = std::move (result);
2166 return dwarf2_per_objfile->dwz_file.get ();
2167 }
2168 \f
2169 /* DWARF quick_symbols_functions support. */
2170
2171 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2172 unique line tables, so we maintain a separate table of all .debug_line
2173 derived entries to support the sharing.
2174 All the quick functions need is the list of file names. We discard the
2175 line_header when we're done and don't need to record it here. */
2176 struct quick_file_names
2177 {
2178 /* The data used to construct the hash key. */
2179 struct stmt_list_hash hash;
2180
2181 /* The number of entries in file_names, real_names. */
2182 unsigned int num_file_names;
2183
2184 /* The file names from the line table, after being run through
2185 file_full_name. */
2186 const char **file_names;
2187
2188 /* The file names from the line table after being run through
2189 gdb_realpath. These are computed lazily. */
2190 const char **real_names;
2191 };
2192
2193 /* When using the index (and thus not using psymtabs), each CU has an
2194 object of this type. This is used to hold information needed by
2195 the various "quick" methods. */
2196 struct dwarf2_per_cu_quick_data
2197 {
2198 /* The file table. This can be NULL if there was no file table
2199 or it's currently not read in.
2200 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2201 struct quick_file_names *file_names;
2202
2203 /* The corresponding symbol table. This is NULL if symbols for this
2204 CU have not yet been read. */
2205 struct compunit_symtab *compunit_symtab;
2206
2207 /* A temporary mark bit used when iterating over all CUs in
2208 expand_symtabs_matching. */
2209 unsigned int mark : 1;
2210
2211 /* True if we've tried to read the file table and found there isn't one.
2212 There will be no point in trying to read it again next time. */
2213 unsigned int no_file_data : 1;
2214 };
2215
2216 /* Utility hash function for a stmt_list_hash. */
2217
2218 static hashval_t
2219 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2220 {
2221 hashval_t v = 0;
2222
2223 if (stmt_list_hash->dwo_unit != NULL)
2224 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2225 v += to_underlying (stmt_list_hash->line_sect_off);
2226 return v;
2227 }
2228
2229 /* Utility equality function for a stmt_list_hash. */
2230
2231 static int
2232 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2233 const struct stmt_list_hash *rhs)
2234 {
2235 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2236 return 0;
2237 if (lhs->dwo_unit != NULL
2238 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2239 return 0;
2240
2241 return lhs->line_sect_off == rhs->line_sect_off;
2242 }
2243
2244 /* Hash function for a quick_file_names. */
2245
2246 static hashval_t
2247 hash_file_name_entry (const void *e)
2248 {
2249 const struct quick_file_names *file_data
2250 = (const struct quick_file_names *) e;
2251
2252 return hash_stmt_list_entry (&file_data->hash);
2253 }
2254
2255 /* Equality function for a quick_file_names. */
2256
2257 static int
2258 eq_file_name_entry (const void *a, const void *b)
2259 {
2260 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2261 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2262
2263 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2264 }
2265
2266 /* Delete function for a quick_file_names. */
2267
2268 static void
2269 delete_file_name_entry (void *e)
2270 {
2271 struct quick_file_names *file_data = (struct quick_file_names *) e;
2272 int i;
2273
2274 for (i = 0; i < file_data->num_file_names; ++i)
2275 {
2276 xfree ((void*) file_data->file_names[i]);
2277 if (file_data->real_names)
2278 xfree ((void*) file_data->real_names[i]);
2279 }
2280
2281 /* The space for the struct itself lives on objfile_obstack,
2282 so we don't free it here. */
2283 }
2284
2285 /* Create a quick_file_names hash table. */
2286
2287 static htab_up
2288 create_quick_file_names_table (unsigned int nr_initial_entries)
2289 {
2290 return htab_up (htab_create_alloc (nr_initial_entries,
2291 hash_file_name_entry, eq_file_name_entry,
2292 delete_file_name_entry, xcalloc, xfree));
2293 }
2294
2295 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2296 have to be created afterwards. You should call age_cached_comp_units after
2297 processing PER_CU->CU. dw2_setup must have been already called. */
2298
2299 static void
2300 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2301 {
2302 if (per_cu->is_debug_types)
2303 load_full_type_unit (per_cu);
2304 else
2305 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2306
2307 if (per_cu->cu == NULL)
2308 return; /* Dummy CU. */
2309
2310 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2311 }
2312
2313 /* Read in the symbols for PER_CU. */
2314
2315 static void
2316 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2317 {
2318 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2319
2320 /* Skip type_unit_groups, reading the type units they contain
2321 is handled elsewhere. */
2322 if (per_cu->type_unit_group_p ())
2323 return;
2324
2325 /* The destructor of dwarf2_queue_guard frees any entries left on
2326 the queue. After this point we're guaranteed to leave this function
2327 with the dwarf queue empty. */
2328 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2329
2330 if (dwarf2_per_objfile->using_index
2331 ? per_cu->v.quick->compunit_symtab == NULL
2332 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2333 {
2334 queue_comp_unit (per_cu, language_minimal);
2335 load_cu (per_cu, skip_partial);
2336
2337 /* If we just loaded a CU from a DWO, and we're working with an index
2338 that may badly handle TUs, load all the TUs in that DWO as well.
2339 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2340 if (!per_cu->is_debug_types
2341 && per_cu->cu != NULL
2342 && per_cu->cu->dwo_unit != NULL
2343 && dwarf2_per_objfile->index_table != NULL
2344 && dwarf2_per_objfile->index_table->version <= 7
2345 /* DWP files aren't supported yet. */
2346 && get_dwp_file (dwarf2_per_objfile) == NULL)
2347 queue_and_load_all_dwo_tus (per_cu);
2348 }
2349
2350 process_queue (dwarf2_per_objfile);
2351
2352 /* Age the cache, releasing compilation units that have not
2353 been used recently. */
2354 age_cached_comp_units (dwarf2_per_objfile);
2355 }
2356
2357 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2358 the objfile from which this CU came. Returns the resulting symbol
2359 table. */
2360
2361 static struct compunit_symtab *
2362 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2363 {
2364 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2365
2366 gdb_assert (dwarf2_per_objfile->using_index);
2367 if (!per_cu->v.quick->compunit_symtab)
2368 {
2369 free_cached_comp_units freer (dwarf2_per_objfile);
2370 scoped_restore decrementer = increment_reading_symtab ();
2371 dw2_do_instantiate_symtab (per_cu, skip_partial);
2372 process_cu_includes (dwarf2_per_objfile);
2373 }
2374
2375 return per_cu->v.quick->compunit_symtab;
2376 }
2377
2378 /* See declaration. */
2379
2380 dwarf2_per_cu_data *
2381 dwarf2_per_objfile::get_cutu (int index)
2382 {
2383 if (index >= this->all_comp_units.size ())
2384 {
2385 index -= this->all_comp_units.size ();
2386 gdb_assert (index < this->all_type_units.size ());
2387 return &this->all_type_units[index]->per_cu;
2388 }
2389
2390 return this->all_comp_units[index];
2391 }
2392
2393 /* See declaration. */
2394
2395 dwarf2_per_cu_data *
2396 dwarf2_per_objfile::get_cu (int index)
2397 {
2398 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2399
2400 return this->all_comp_units[index];
2401 }
2402
2403 /* See declaration. */
2404
2405 signatured_type *
2406 dwarf2_per_objfile::get_tu (int index)
2407 {
2408 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2409
2410 return this->all_type_units[index];
2411 }
2412
2413 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2414 objfile_obstack, and constructed with the specified field
2415 values. */
2416
2417 static dwarf2_per_cu_data *
2418 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2419 struct dwarf2_section_info *section,
2420 int is_dwz,
2421 sect_offset sect_off, ULONGEST length)
2422 {
2423 struct objfile *objfile = dwarf2_per_objfile->objfile;
2424 dwarf2_per_cu_data *the_cu
2425 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2426 struct dwarf2_per_cu_data);
2427 the_cu->sect_off = sect_off;
2428 the_cu->length = length;
2429 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2430 the_cu->section = section;
2431 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2432 struct dwarf2_per_cu_quick_data);
2433 the_cu->is_dwz = is_dwz;
2434 return the_cu;
2435 }
2436
2437 /* A helper for create_cus_from_index that handles a given list of
2438 CUs. */
2439
2440 static void
2441 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2442 const gdb_byte *cu_list, offset_type n_elements,
2443 struct dwarf2_section_info *section,
2444 int is_dwz)
2445 {
2446 for (offset_type i = 0; i < n_elements; i += 2)
2447 {
2448 gdb_static_assert (sizeof (ULONGEST) >= 8);
2449
2450 sect_offset sect_off
2451 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2452 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2453 cu_list += 2 * 8;
2454
2455 dwarf2_per_cu_data *per_cu
2456 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2457 sect_off, length);
2458 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2459 }
2460 }
2461
2462 /* Read the CU list from the mapped index, and use it to create all
2463 the CU objects for this objfile. */
2464
2465 static void
2466 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2467 const gdb_byte *cu_list, offset_type cu_list_elements,
2468 const gdb_byte *dwz_list, offset_type dwz_elements)
2469 {
2470 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2471 dwarf2_per_objfile->all_comp_units.reserve
2472 ((cu_list_elements + dwz_elements) / 2);
2473
2474 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2475 &dwarf2_per_objfile->info, 0);
2476
2477 if (dwz_elements == 0)
2478 return;
2479
2480 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2481 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2482 &dwz->info, 1);
2483 }
2484
2485 /* Create the signatured type hash table from the index. */
2486
2487 static void
2488 create_signatured_type_table_from_index
2489 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2490 struct dwarf2_section_info *section,
2491 const gdb_byte *bytes,
2492 offset_type elements)
2493 {
2494 struct objfile *objfile = dwarf2_per_objfile->objfile;
2495
2496 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2497 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2498
2499 htab_up sig_types_hash = allocate_signatured_type_table ();
2500
2501 for (offset_type i = 0; i < elements; i += 3)
2502 {
2503 struct signatured_type *sig_type;
2504 ULONGEST signature;
2505 void **slot;
2506 cu_offset type_offset_in_tu;
2507
2508 gdb_static_assert (sizeof (ULONGEST) >= 8);
2509 sect_offset sect_off
2510 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2511 type_offset_in_tu
2512 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2513 BFD_ENDIAN_LITTLE);
2514 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2515 bytes += 3 * 8;
2516
2517 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2518 struct signatured_type);
2519 sig_type->signature = signature;
2520 sig_type->type_offset_in_tu = type_offset_in_tu;
2521 sig_type->per_cu.is_debug_types = 1;
2522 sig_type->per_cu.section = section;
2523 sig_type->per_cu.sect_off = sect_off;
2524 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2525 sig_type->per_cu.v.quick
2526 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2527 struct dwarf2_per_cu_quick_data);
2528
2529 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2530 *slot = sig_type;
2531
2532 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2533 }
2534
2535 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2536 }
2537
2538 /* Create the signatured type hash table from .debug_names. */
2539
2540 static void
2541 create_signatured_type_table_from_debug_names
2542 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2543 const mapped_debug_names &map,
2544 struct dwarf2_section_info *section,
2545 struct dwarf2_section_info *abbrev_section)
2546 {
2547 struct objfile *objfile = dwarf2_per_objfile->objfile;
2548
2549 section->read (objfile);
2550 abbrev_section->read (objfile);
2551
2552 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2553 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2554
2555 htab_up sig_types_hash = allocate_signatured_type_table ();
2556
2557 for (uint32_t i = 0; i < map.tu_count; ++i)
2558 {
2559 struct signatured_type *sig_type;
2560 void **slot;
2561
2562 sect_offset sect_off
2563 = (sect_offset) (extract_unsigned_integer
2564 (map.tu_table_reordered + i * map.offset_size,
2565 map.offset_size,
2566 map.dwarf5_byte_order));
2567
2568 comp_unit_head cu_header;
2569 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2570 abbrev_section,
2571 section->buffer + to_underlying (sect_off),
2572 rcuh_kind::TYPE);
2573
2574 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2575 struct signatured_type);
2576 sig_type->signature = cu_header.signature;
2577 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2578 sig_type->per_cu.is_debug_types = 1;
2579 sig_type->per_cu.section = section;
2580 sig_type->per_cu.sect_off = sect_off;
2581 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2582 sig_type->per_cu.v.quick
2583 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2584 struct dwarf2_per_cu_quick_data);
2585
2586 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2587 *slot = sig_type;
2588
2589 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2590 }
2591
2592 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2593 }
2594
2595 /* Read the address map data from the mapped index, and use it to
2596 populate the objfile's psymtabs_addrmap. */
2597
2598 static void
2599 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2600 struct mapped_index *index)
2601 {
2602 struct objfile *objfile = dwarf2_per_objfile->objfile;
2603 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2604 const gdb_byte *iter, *end;
2605 struct addrmap *mutable_map;
2606 CORE_ADDR baseaddr;
2607
2608 auto_obstack temp_obstack;
2609
2610 mutable_map = addrmap_create_mutable (&temp_obstack);
2611
2612 iter = index->address_table.data ();
2613 end = iter + index->address_table.size ();
2614
2615 baseaddr = objfile->text_section_offset ();
2616
2617 while (iter < end)
2618 {
2619 ULONGEST hi, lo, cu_index;
2620 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2621 iter += 8;
2622 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2623 iter += 8;
2624 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2625 iter += 4;
2626
2627 if (lo > hi)
2628 {
2629 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2630 hex_string (lo), hex_string (hi));
2631 continue;
2632 }
2633
2634 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2635 {
2636 complaint (_(".gdb_index address table has invalid CU number %u"),
2637 (unsigned) cu_index);
2638 continue;
2639 }
2640
2641 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2642 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2643 addrmap_set_empty (mutable_map, lo, hi - 1,
2644 dwarf2_per_objfile->get_cu (cu_index));
2645 }
2646
2647 objfile->partial_symtabs->psymtabs_addrmap
2648 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2649 }
2650
2651 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2652 populate the objfile's psymtabs_addrmap. */
2653
2654 static void
2655 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2656 struct dwarf2_section_info *section)
2657 {
2658 struct objfile *objfile = dwarf2_per_objfile->objfile;
2659 bfd *abfd = objfile->obfd;
2660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2661 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2662
2663 auto_obstack temp_obstack;
2664 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2665
2666 std::unordered_map<sect_offset,
2667 dwarf2_per_cu_data *,
2668 gdb::hash_enum<sect_offset>>
2669 debug_info_offset_to_per_cu;
2670 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2671 {
2672 const auto insertpair
2673 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2674 if (!insertpair.second)
2675 {
2676 warning (_("Section .debug_aranges in %s has duplicate "
2677 "debug_info_offset %s, ignoring .debug_aranges."),
2678 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2679 return;
2680 }
2681 }
2682
2683 section->read (objfile);
2684
2685 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2686
2687 const gdb_byte *addr = section->buffer;
2688
2689 while (addr < section->buffer + section->size)
2690 {
2691 const gdb_byte *const entry_addr = addr;
2692 unsigned int bytes_read;
2693
2694 const LONGEST entry_length = read_initial_length (abfd, addr,
2695 &bytes_read);
2696 addr += bytes_read;
2697
2698 const gdb_byte *const entry_end = addr + entry_length;
2699 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2700 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2701 if (addr + entry_length > section->buffer + section->size)
2702 {
2703 warning (_("Section .debug_aranges in %s entry at offset %s "
2704 "length %s exceeds section length %s, "
2705 "ignoring .debug_aranges."),
2706 objfile_name (objfile),
2707 plongest (entry_addr - section->buffer),
2708 plongest (bytes_read + entry_length),
2709 pulongest (section->size));
2710 return;
2711 }
2712
2713 /* The version number. */
2714 const uint16_t version = read_2_bytes (abfd, addr);
2715 addr += 2;
2716 if (version != 2)
2717 {
2718 warning (_("Section .debug_aranges in %s entry at offset %s "
2719 "has unsupported version %d, ignoring .debug_aranges."),
2720 objfile_name (objfile),
2721 plongest (entry_addr - section->buffer), version);
2722 return;
2723 }
2724
2725 const uint64_t debug_info_offset
2726 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2727 addr += offset_size;
2728 const auto per_cu_it
2729 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2730 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2731 {
2732 warning (_("Section .debug_aranges in %s entry at offset %s "
2733 "debug_info_offset %s does not exists, "
2734 "ignoring .debug_aranges."),
2735 objfile_name (objfile),
2736 plongest (entry_addr - section->buffer),
2737 pulongest (debug_info_offset));
2738 return;
2739 }
2740 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2741
2742 const uint8_t address_size = *addr++;
2743 if (address_size < 1 || address_size > 8)
2744 {
2745 warning (_("Section .debug_aranges in %s entry at offset %s "
2746 "address_size %u is invalid, ignoring .debug_aranges."),
2747 objfile_name (objfile),
2748 plongest (entry_addr - section->buffer), address_size);
2749 return;
2750 }
2751
2752 const uint8_t segment_selector_size = *addr++;
2753 if (segment_selector_size != 0)
2754 {
2755 warning (_("Section .debug_aranges in %s entry at offset %s "
2756 "segment_selector_size %u is not supported, "
2757 "ignoring .debug_aranges."),
2758 objfile_name (objfile),
2759 plongest (entry_addr - section->buffer),
2760 segment_selector_size);
2761 return;
2762 }
2763
2764 /* Must pad to an alignment boundary that is twice the address
2765 size. It is undocumented by the DWARF standard but GCC does
2766 use it. */
2767 for (size_t padding = ((-(addr - section->buffer))
2768 & (2 * address_size - 1));
2769 padding > 0; padding--)
2770 if (*addr++ != 0)
2771 {
2772 warning (_("Section .debug_aranges in %s entry at offset %s "
2773 "padding is not zero, ignoring .debug_aranges."),
2774 objfile_name (objfile),
2775 plongest (entry_addr - section->buffer));
2776 return;
2777 }
2778
2779 for (;;)
2780 {
2781 if (addr + 2 * address_size > entry_end)
2782 {
2783 warning (_("Section .debug_aranges in %s entry at offset %s "
2784 "address list is not properly terminated, "
2785 "ignoring .debug_aranges."),
2786 objfile_name (objfile),
2787 plongest (entry_addr - section->buffer));
2788 return;
2789 }
2790 ULONGEST start = extract_unsigned_integer (addr, address_size,
2791 dwarf5_byte_order);
2792 addr += address_size;
2793 ULONGEST length = extract_unsigned_integer (addr, address_size,
2794 dwarf5_byte_order);
2795 addr += address_size;
2796 if (start == 0 && length == 0)
2797 break;
2798 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2799 {
2800 /* Symbol was eliminated due to a COMDAT group. */
2801 continue;
2802 }
2803 ULONGEST end = start + length;
2804 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2805 - baseaddr);
2806 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2807 - baseaddr);
2808 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2809 }
2810 }
2811
2812 objfile->partial_symtabs->psymtabs_addrmap
2813 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2814 }
2815
2816 /* Find a slot in the mapped index INDEX for the object named NAME.
2817 If NAME is found, set *VEC_OUT to point to the CU vector in the
2818 constant pool and return true. If NAME cannot be found, return
2819 false. */
2820
2821 static bool
2822 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2823 offset_type **vec_out)
2824 {
2825 offset_type hash;
2826 offset_type slot, step;
2827 int (*cmp) (const char *, const char *);
2828
2829 gdb::unique_xmalloc_ptr<char> without_params;
2830 if (current_language->la_language == language_cplus
2831 || current_language->la_language == language_fortran
2832 || current_language->la_language == language_d)
2833 {
2834 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2835 not contain any. */
2836
2837 if (strchr (name, '(') != NULL)
2838 {
2839 without_params = cp_remove_params (name);
2840
2841 if (without_params != NULL)
2842 name = without_params.get ();
2843 }
2844 }
2845
2846 /* Index version 4 did not support case insensitive searches. But the
2847 indices for case insensitive languages are built in lowercase, therefore
2848 simulate our NAME being searched is also lowercased. */
2849 hash = mapped_index_string_hash ((index->version == 4
2850 && case_sensitivity == case_sensitive_off
2851 ? 5 : index->version),
2852 name);
2853
2854 slot = hash & (index->symbol_table.size () - 1);
2855 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2856 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2857
2858 for (;;)
2859 {
2860 const char *str;
2861
2862 const auto &bucket = index->symbol_table[slot];
2863 if (bucket.name == 0 && bucket.vec == 0)
2864 return false;
2865
2866 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2867 if (!cmp (name, str))
2868 {
2869 *vec_out = (offset_type *) (index->constant_pool
2870 + MAYBE_SWAP (bucket.vec));
2871 return true;
2872 }
2873
2874 slot = (slot + step) & (index->symbol_table.size () - 1);
2875 }
2876 }
2877
2878 /* A helper function that reads the .gdb_index from BUFFER and fills
2879 in MAP. FILENAME is the name of the file containing the data;
2880 it is used for error reporting. DEPRECATED_OK is true if it is
2881 ok to use deprecated sections.
2882
2883 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2884 out parameters that are filled in with information about the CU and
2885 TU lists in the section.
2886
2887 Returns true if all went well, false otherwise. */
2888
2889 static bool
2890 read_gdb_index_from_buffer (struct objfile *objfile,
2891 const char *filename,
2892 bool deprecated_ok,
2893 gdb::array_view<const gdb_byte> buffer,
2894 struct mapped_index *map,
2895 const gdb_byte **cu_list,
2896 offset_type *cu_list_elements,
2897 const gdb_byte **types_list,
2898 offset_type *types_list_elements)
2899 {
2900 const gdb_byte *addr = &buffer[0];
2901
2902 /* Version check. */
2903 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2904 /* Versions earlier than 3 emitted every copy of a psymbol. This
2905 causes the index to behave very poorly for certain requests. Version 3
2906 contained incomplete addrmap. So, it seems better to just ignore such
2907 indices. */
2908 if (version < 4)
2909 {
2910 static int warning_printed = 0;
2911 if (!warning_printed)
2912 {
2913 warning (_("Skipping obsolete .gdb_index section in %s."),
2914 filename);
2915 warning_printed = 1;
2916 }
2917 return 0;
2918 }
2919 /* Index version 4 uses a different hash function than index version
2920 5 and later.
2921
2922 Versions earlier than 6 did not emit psymbols for inlined
2923 functions. Using these files will cause GDB not to be able to
2924 set breakpoints on inlined functions by name, so we ignore these
2925 indices unless the user has done
2926 "set use-deprecated-index-sections on". */
2927 if (version < 6 && !deprecated_ok)
2928 {
2929 static int warning_printed = 0;
2930 if (!warning_printed)
2931 {
2932 warning (_("\
2933 Skipping deprecated .gdb_index section in %s.\n\
2934 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2935 to use the section anyway."),
2936 filename);
2937 warning_printed = 1;
2938 }
2939 return 0;
2940 }
2941 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2942 of the TU (for symbols coming from TUs),
2943 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2944 Plus gold-generated indices can have duplicate entries for global symbols,
2945 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2946 These are just performance bugs, and we can't distinguish gdb-generated
2947 indices from gold-generated ones, so issue no warning here. */
2948
2949 /* Indexes with higher version than the one supported by GDB may be no
2950 longer backward compatible. */
2951 if (version > 8)
2952 return 0;
2953
2954 map->version = version;
2955
2956 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2957
2958 int i = 0;
2959 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2960 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2961 / 8);
2962 ++i;
2963
2964 *types_list = addr + MAYBE_SWAP (metadata[i]);
2965 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2966 - MAYBE_SWAP (metadata[i]))
2967 / 8);
2968 ++i;
2969
2970 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2971 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2972 map->address_table
2973 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2974 ++i;
2975
2976 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2977 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2978 map->symbol_table
2979 = gdb::array_view<mapped_index::symbol_table_slot>
2980 ((mapped_index::symbol_table_slot *) symbol_table,
2981 (mapped_index::symbol_table_slot *) symbol_table_end);
2982
2983 ++i;
2984 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2985
2986 return 1;
2987 }
2988
2989 /* Callback types for dwarf2_read_gdb_index. */
2990
2991 typedef gdb::function_view
2992 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2993 get_gdb_index_contents_ftype;
2994 typedef gdb::function_view
2995 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2996 get_gdb_index_contents_dwz_ftype;
2997
2998 /* Read .gdb_index. If everything went ok, initialize the "quick"
2999 elements of all the CUs and return 1. Otherwise, return 0. */
3000
3001 static int
3002 dwarf2_read_gdb_index
3003 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3004 get_gdb_index_contents_ftype get_gdb_index_contents,
3005 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3006 {
3007 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3008 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3009 struct dwz_file *dwz;
3010 struct objfile *objfile = dwarf2_per_objfile->objfile;
3011
3012 gdb::array_view<const gdb_byte> main_index_contents
3013 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3014
3015 if (main_index_contents.empty ())
3016 return 0;
3017
3018 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3019 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3020 use_deprecated_index_sections,
3021 main_index_contents, map.get (), &cu_list,
3022 &cu_list_elements, &types_list,
3023 &types_list_elements))
3024 return 0;
3025
3026 /* Don't use the index if it's empty. */
3027 if (map->symbol_table.empty ())
3028 return 0;
3029
3030 /* If there is a .dwz file, read it so we can get its CU list as
3031 well. */
3032 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3033 if (dwz != NULL)
3034 {
3035 struct mapped_index dwz_map;
3036 const gdb_byte *dwz_types_ignore;
3037 offset_type dwz_types_elements_ignore;
3038
3039 gdb::array_view<const gdb_byte> dwz_index_content
3040 = get_gdb_index_contents_dwz (objfile, dwz);
3041
3042 if (dwz_index_content.empty ())
3043 return 0;
3044
3045 if (!read_gdb_index_from_buffer (objfile,
3046 bfd_get_filename (dwz->dwz_bfd.get ()),
3047 1, dwz_index_content, &dwz_map,
3048 &dwz_list, &dwz_list_elements,
3049 &dwz_types_ignore,
3050 &dwz_types_elements_ignore))
3051 {
3052 warning (_("could not read '.gdb_index' section from %s; skipping"),
3053 bfd_get_filename (dwz->dwz_bfd.get ()));
3054 return 0;
3055 }
3056 }
3057
3058 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3059 dwz_list, dwz_list_elements);
3060
3061 if (types_list_elements)
3062 {
3063 /* We can only handle a single .debug_types when we have an
3064 index. */
3065 if (dwarf2_per_objfile->types.size () != 1)
3066 return 0;
3067
3068 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3069
3070 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3071 types_list, types_list_elements);
3072 }
3073
3074 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3075
3076 dwarf2_per_objfile->index_table = std::move (map);
3077 dwarf2_per_objfile->using_index = 1;
3078 dwarf2_per_objfile->quick_file_names_table =
3079 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3080
3081 return 1;
3082 }
3083
3084 /* die_reader_func for dw2_get_file_names. */
3085
3086 static void
3087 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3088 const gdb_byte *info_ptr,
3089 struct die_info *comp_unit_die)
3090 {
3091 struct dwarf2_cu *cu = reader->cu;
3092 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3093 struct dwarf2_per_objfile *dwarf2_per_objfile
3094 = cu->per_cu->dwarf2_per_objfile;
3095 struct objfile *objfile = dwarf2_per_objfile->objfile;
3096 struct dwarf2_per_cu_data *lh_cu;
3097 struct attribute *attr;
3098 void **slot;
3099 struct quick_file_names *qfn;
3100
3101 gdb_assert (! this_cu->is_debug_types);
3102
3103 /* Our callers never want to match partial units -- instead they
3104 will match the enclosing full CU. */
3105 if (comp_unit_die->tag == DW_TAG_partial_unit)
3106 {
3107 this_cu->v.quick->no_file_data = 1;
3108 return;
3109 }
3110
3111 lh_cu = this_cu;
3112 slot = NULL;
3113
3114 line_header_up lh;
3115 sect_offset line_offset {};
3116
3117 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3118 if (attr != nullptr)
3119 {
3120 struct quick_file_names find_entry;
3121
3122 line_offset = (sect_offset) DW_UNSND (attr);
3123
3124 /* We may have already read in this line header (TU line header sharing).
3125 If we have we're done. */
3126 find_entry.hash.dwo_unit = cu->dwo_unit;
3127 find_entry.hash.line_sect_off = line_offset;
3128 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3129 &find_entry, INSERT);
3130 if (*slot != NULL)
3131 {
3132 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3133 return;
3134 }
3135
3136 lh = dwarf_decode_line_header (line_offset, cu);
3137 }
3138 if (lh == NULL)
3139 {
3140 lh_cu->v.quick->no_file_data = 1;
3141 return;
3142 }
3143
3144 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3145 qfn->hash.dwo_unit = cu->dwo_unit;
3146 qfn->hash.line_sect_off = line_offset;
3147 gdb_assert (slot != NULL);
3148 *slot = qfn;
3149
3150 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3151
3152 int offset = 0;
3153 if (strcmp (fnd.name, "<unknown>") != 0)
3154 ++offset;
3155
3156 qfn->num_file_names = offset + lh->file_names_size ();
3157 qfn->file_names =
3158 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3159 if (offset != 0)
3160 qfn->file_names[0] = xstrdup (fnd.name);
3161 for (int i = 0; i < lh->file_names_size (); ++i)
3162 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3163 fnd.comp_dir).release ();
3164 qfn->real_names = NULL;
3165
3166 lh_cu->v.quick->file_names = qfn;
3167 }
3168
3169 /* A helper for the "quick" functions which attempts to read the line
3170 table for THIS_CU. */
3171
3172 static struct quick_file_names *
3173 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3174 {
3175 /* This should never be called for TUs. */
3176 gdb_assert (! this_cu->is_debug_types);
3177 /* Nor type unit groups. */
3178 gdb_assert (! this_cu->type_unit_group_p ());
3179
3180 if (this_cu->v.quick->file_names != NULL)
3181 return this_cu->v.quick->file_names;
3182 /* If we know there is no line data, no point in looking again. */
3183 if (this_cu->v.quick->no_file_data)
3184 return NULL;
3185
3186 cutu_reader reader (this_cu);
3187 if (!reader.dummy_p)
3188 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3189
3190 if (this_cu->v.quick->no_file_data)
3191 return NULL;
3192 return this_cu->v.quick->file_names;
3193 }
3194
3195 /* A helper for the "quick" functions which computes and caches the
3196 real path for a given file name from the line table. */
3197
3198 static const char *
3199 dw2_get_real_path (struct objfile *objfile,
3200 struct quick_file_names *qfn, int index)
3201 {
3202 if (qfn->real_names == NULL)
3203 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3204 qfn->num_file_names, const char *);
3205
3206 if (qfn->real_names[index] == NULL)
3207 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3208
3209 return qfn->real_names[index];
3210 }
3211
3212 static struct symtab *
3213 dw2_find_last_source_symtab (struct objfile *objfile)
3214 {
3215 struct dwarf2_per_objfile *dwarf2_per_objfile
3216 = get_dwarf2_per_objfile (objfile);
3217 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3218 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3219
3220 if (cust == NULL)
3221 return NULL;
3222
3223 return compunit_primary_filetab (cust);
3224 }
3225
3226 /* Traversal function for dw2_forget_cached_source_info. */
3227
3228 static int
3229 dw2_free_cached_file_names (void **slot, void *info)
3230 {
3231 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3232
3233 if (file_data->real_names)
3234 {
3235 int i;
3236
3237 for (i = 0; i < file_data->num_file_names; ++i)
3238 {
3239 xfree ((void*) file_data->real_names[i]);
3240 file_data->real_names[i] = NULL;
3241 }
3242 }
3243
3244 return 1;
3245 }
3246
3247 static void
3248 dw2_forget_cached_source_info (struct objfile *objfile)
3249 {
3250 struct dwarf2_per_objfile *dwarf2_per_objfile
3251 = get_dwarf2_per_objfile (objfile);
3252
3253 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3254 dw2_free_cached_file_names, NULL);
3255 }
3256
3257 /* Helper function for dw2_map_symtabs_matching_filename that expands
3258 the symtabs and calls the iterator. */
3259
3260 static int
3261 dw2_map_expand_apply (struct objfile *objfile,
3262 struct dwarf2_per_cu_data *per_cu,
3263 const char *name, const char *real_path,
3264 gdb::function_view<bool (symtab *)> callback)
3265 {
3266 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3267
3268 /* Don't visit already-expanded CUs. */
3269 if (per_cu->v.quick->compunit_symtab)
3270 return 0;
3271
3272 /* This may expand more than one symtab, and we want to iterate over
3273 all of them. */
3274 dw2_instantiate_symtab (per_cu, false);
3275
3276 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3277 last_made, callback);
3278 }
3279
3280 /* Implementation of the map_symtabs_matching_filename method. */
3281
3282 static bool
3283 dw2_map_symtabs_matching_filename
3284 (struct objfile *objfile, const char *name, const char *real_path,
3285 gdb::function_view<bool (symtab *)> callback)
3286 {
3287 const char *name_basename = lbasename (name);
3288 struct dwarf2_per_objfile *dwarf2_per_objfile
3289 = get_dwarf2_per_objfile (objfile);
3290
3291 /* The rule is CUs specify all the files, including those used by
3292 any TU, so there's no need to scan TUs here. */
3293
3294 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3295 {
3296 /* We only need to look at symtabs not already expanded. */
3297 if (per_cu->v.quick->compunit_symtab)
3298 continue;
3299
3300 quick_file_names *file_data = dw2_get_file_names (per_cu);
3301 if (file_data == NULL)
3302 continue;
3303
3304 for (int j = 0; j < file_data->num_file_names; ++j)
3305 {
3306 const char *this_name = file_data->file_names[j];
3307 const char *this_real_name;
3308
3309 if (compare_filenames_for_search (this_name, name))
3310 {
3311 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3312 callback))
3313 return true;
3314 continue;
3315 }
3316
3317 /* Before we invoke realpath, which can get expensive when many
3318 files are involved, do a quick comparison of the basenames. */
3319 if (! basenames_may_differ
3320 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3321 continue;
3322
3323 this_real_name = dw2_get_real_path (objfile, file_data, j);
3324 if (compare_filenames_for_search (this_real_name, name))
3325 {
3326 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3327 callback))
3328 return true;
3329 continue;
3330 }
3331
3332 if (real_path != NULL)
3333 {
3334 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3335 gdb_assert (IS_ABSOLUTE_PATH (name));
3336 if (this_real_name != NULL
3337 && FILENAME_CMP (real_path, this_real_name) == 0)
3338 {
3339 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3340 callback))
3341 return true;
3342 continue;
3343 }
3344 }
3345 }
3346 }
3347
3348 return false;
3349 }
3350
3351 /* Struct used to manage iterating over all CUs looking for a symbol. */
3352
3353 struct dw2_symtab_iterator
3354 {
3355 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3356 struct dwarf2_per_objfile *dwarf2_per_objfile;
3357 /* If set, only look for symbols that match that block. Valid values are
3358 GLOBAL_BLOCK and STATIC_BLOCK. */
3359 gdb::optional<block_enum> block_index;
3360 /* The kind of symbol we're looking for. */
3361 domain_enum domain;
3362 /* The list of CUs from the index entry of the symbol,
3363 or NULL if not found. */
3364 offset_type *vec;
3365 /* The next element in VEC to look at. */
3366 int next;
3367 /* The number of elements in VEC, or zero if there is no match. */
3368 int length;
3369 /* Have we seen a global version of the symbol?
3370 If so we can ignore all further global instances.
3371 This is to work around gold/15646, inefficient gold-generated
3372 indices. */
3373 int global_seen;
3374 };
3375
3376 /* Initialize the index symtab iterator ITER. */
3377
3378 static void
3379 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3380 struct dwarf2_per_objfile *dwarf2_per_objfile,
3381 gdb::optional<block_enum> block_index,
3382 domain_enum domain,
3383 const char *name)
3384 {
3385 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3386 iter->block_index = block_index;
3387 iter->domain = domain;
3388 iter->next = 0;
3389 iter->global_seen = 0;
3390
3391 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3392
3393 /* index is NULL if OBJF_READNOW. */
3394 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3395 iter->length = MAYBE_SWAP (*iter->vec);
3396 else
3397 {
3398 iter->vec = NULL;
3399 iter->length = 0;
3400 }
3401 }
3402
3403 /* Return the next matching CU or NULL if there are no more. */
3404
3405 static struct dwarf2_per_cu_data *
3406 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3407 {
3408 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3409
3410 for ( ; iter->next < iter->length; ++iter->next)
3411 {
3412 offset_type cu_index_and_attrs =
3413 MAYBE_SWAP (iter->vec[iter->next + 1]);
3414 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3415 gdb_index_symbol_kind symbol_kind =
3416 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3417 /* Only check the symbol attributes if they're present.
3418 Indices prior to version 7 don't record them,
3419 and indices >= 7 may elide them for certain symbols
3420 (gold does this). */
3421 int attrs_valid =
3422 (dwarf2_per_objfile->index_table->version >= 7
3423 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3424
3425 /* Don't crash on bad data. */
3426 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3427 + dwarf2_per_objfile->all_type_units.size ()))
3428 {
3429 complaint (_(".gdb_index entry has bad CU index"
3430 " [in module %s]"),
3431 objfile_name (dwarf2_per_objfile->objfile));
3432 continue;
3433 }
3434
3435 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3436
3437 /* Skip if already read in. */
3438 if (per_cu->v.quick->compunit_symtab)
3439 continue;
3440
3441 /* Check static vs global. */
3442 if (attrs_valid)
3443 {
3444 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3445
3446 if (iter->block_index.has_value ())
3447 {
3448 bool want_static = *iter->block_index == STATIC_BLOCK;
3449
3450 if (is_static != want_static)
3451 continue;
3452 }
3453
3454 /* Work around gold/15646. */
3455 if (!is_static && iter->global_seen)
3456 continue;
3457 if (!is_static)
3458 iter->global_seen = 1;
3459 }
3460
3461 /* Only check the symbol's kind if it has one. */
3462 if (attrs_valid)
3463 {
3464 switch (iter->domain)
3465 {
3466 case VAR_DOMAIN:
3467 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3468 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3469 /* Some types are also in VAR_DOMAIN. */
3470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3471 continue;
3472 break;
3473 case STRUCT_DOMAIN:
3474 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3475 continue;
3476 break;
3477 case LABEL_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3479 continue;
3480 break;
3481 case MODULE_DOMAIN:
3482 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3483 continue;
3484 break;
3485 default:
3486 break;
3487 }
3488 }
3489
3490 ++iter->next;
3491 return per_cu;
3492 }
3493
3494 return NULL;
3495 }
3496
3497 static struct compunit_symtab *
3498 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3499 const char *name, domain_enum domain)
3500 {
3501 struct compunit_symtab *stab_best = NULL;
3502 struct dwarf2_per_objfile *dwarf2_per_objfile
3503 = get_dwarf2_per_objfile (objfile);
3504
3505 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3506
3507 struct dw2_symtab_iterator iter;
3508 struct dwarf2_per_cu_data *per_cu;
3509
3510 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3511
3512 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3513 {
3514 struct symbol *sym, *with_opaque = NULL;
3515 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3516 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3517 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3518
3519 sym = block_find_symbol (block, name, domain,
3520 block_find_non_opaque_type_preferred,
3521 &with_opaque);
3522
3523 /* Some caution must be observed with overloaded functions
3524 and methods, since the index will not contain any overload
3525 information (but NAME might contain it). */
3526
3527 if (sym != NULL
3528 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3529 return stab;
3530 if (with_opaque != NULL
3531 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3532 stab_best = stab;
3533
3534 /* Keep looking through other CUs. */
3535 }
3536
3537 return stab_best;
3538 }
3539
3540 static void
3541 dw2_print_stats (struct objfile *objfile)
3542 {
3543 struct dwarf2_per_objfile *dwarf2_per_objfile
3544 = get_dwarf2_per_objfile (objfile);
3545 int total = (dwarf2_per_objfile->all_comp_units.size ()
3546 + dwarf2_per_objfile->all_type_units.size ());
3547 int count = 0;
3548
3549 for (int i = 0; i < total; ++i)
3550 {
3551 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3552
3553 if (!per_cu->v.quick->compunit_symtab)
3554 ++count;
3555 }
3556 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3557 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3558 }
3559
3560 /* This dumps minimal information about the index.
3561 It is called via "mt print objfiles".
3562 One use is to verify .gdb_index has been loaded by the
3563 gdb.dwarf2/gdb-index.exp testcase. */
3564
3565 static void
3566 dw2_dump (struct objfile *objfile)
3567 {
3568 struct dwarf2_per_objfile *dwarf2_per_objfile
3569 = get_dwarf2_per_objfile (objfile);
3570
3571 gdb_assert (dwarf2_per_objfile->using_index);
3572 printf_filtered (".gdb_index:");
3573 if (dwarf2_per_objfile->index_table != NULL)
3574 {
3575 printf_filtered (" version %d\n",
3576 dwarf2_per_objfile->index_table->version);
3577 }
3578 else
3579 printf_filtered (" faked for \"readnow\"\n");
3580 printf_filtered ("\n");
3581 }
3582
3583 static void
3584 dw2_expand_symtabs_for_function (struct objfile *objfile,
3585 const char *func_name)
3586 {
3587 struct dwarf2_per_objfile *dwarf2_per_objfile
3588 = get_dwarf2_per_objfile (objfile);
3589
3590 struct dw2_symtab_iterator iter;
3591 struct dwarf2_per_cu_data *per_cu;
3592
3593 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3594
3595 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3596 dw2_instantiate_symtab (per_cu, false);
3597
3598 }
3599
3600 static void
3601 dw2_expand_all_symtabs (struct objfile *objfile)
3602 {
3603 struct dwarf2_per_objfile *dwarf2_per_objfile
3604 = get_dwarf2_per_objfile (objfile);
3605 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3606 + dwarf2_per_objfile->all_type_units.size ());
3607
3608 for (int i = 0; i < total_units; ++i)
3609 {
3610 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3611
3612 /* We don't want to directly expand a partial CU, because if we
3613 read it with the wrong language, then assertion failures can
3614 be triggered later on. See PR symtab/23010. So, tell
3615 dw2_instantiate_symtab to skip partial CUs -- any important
3616 partial CU will be read via DW_TAG_imported_unit anyway. */
3617 dw2_instantiate_symtab (per_cu, true);
3618 }
3619 }
3620
3621 static void
3622 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3623 const char *fullname)
3624 {
3625 struct dwarf2_per_objfile *dwarf2_per_objfile
3626 = get_dwarf2_per_objfile (objfile);
3627
3628 /* We don't need to consider type units here.
3629 This is only called for examining code, e.g. expand_line_sal.
3630 There can be an order of magnitude (or more) more type units
3631 than comp units, and we avoid them if we can. */
3632
3633 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3634 {
3635 /* We only need to look at symtabs not already expanded. */
3636 if (per_cu->v.quick->compunit_symtab)
3637 continue;
3638
3639 quick_file_names *file_data = dw2_get_file_names (per_cu);
3640 if (file_data == NULL)
3641 continue;
3642
3643 for (int j = 0; j < file_data->num_file_names; ++j)
3644 {
3645 const char *this_fullname = file_data->file_names[j];
3646
3647 if (filename_cmp (this_fullname, fullname) == 0)
3648 {
3649 dw2_instantiate_symtab (per_cu, false);
3650 break;
3651 }
3652 }
3653 }
3654 }
3655
3656 static void
3657 dw2_map_matching_symbols
3658 (struct objfile *objfile,
3659 const lookup_name_info &name, domain_enum domain,
3660 int global,
3661 gdb::function_view<symbol_found_callback_ftype> callback,
3662 symbol_compare_ftype *ordered_compare)
3663 {
3664 /* Currently unimplemented; used for Ada. The function can be called if the
3665 current language is Ada for a non-Ada objfile using GNU index. As Ada
3666 does not look for non-Ada symbols this function should just return. */
3667 }
3668
3669 /* Starting from a search name, return the string that finds the upper
3670 bound of all strings that start with SEARCH_NAME in a sorted name
3671 list. Returns the empty string to indicate that the upper bound is
3672 the end of the list. */
3673
3674 static std::string
3675 make_sort_after_prefix_name (const char *search_name)
3676 {
3677 /* When looking to complete "func", we find the upper bound of all
3678 symbols that start with "func" by looking for where we'd insert
3679 the closest string that would follow "func" in lexicographical
3680 order. Usually, that's "func"-with-last-character-incremented,
3681 i.e. "fund". Mind non-ASCII characters, though. Usually those
3682 will be UTF-8 multi-byte sequences, but we can't be certain.
3683 Especially mind the 0xff character, which is a valid character in
3684 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3685 rule out compilers allowing it in identifiers. Note that
3686 conveniently, strcmp/strcasecmp are specified to compare
3687 characters interpreted as unsigned char. So what we do is treat
3688 the whole string as a base 256 number composed of a sequence of
3689 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3690 to 0, and carries 1 to the following more-significant position.
3691 If the very first character in SEARCH_NAME ends up incremented
3692 and carries/overflows, then the upper bound is the end of the
3693 list. The string after the empty string is also the empty
3694 string.
3695
3696 Some examples of this operation:
3697
3698 SEARCH_NAME => "+1" RESULT
3699
3700 "abc" => "abd"
3701 "ab\xff" => "ac"
3702 "\xff" "a" "\xff" => "\xff" "b"
3703 "\xff" => ""
3704 "\xff\xff" => ""
3705 "" => ""
3706
3707 Then, with these symbols for example:
3708
3709 func
3710 func1
3711 fund
3712
3713 completing "func" looks for symbols between "func" and
3714 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3715 which finds "func" and "func1", but not "fund".
3716
3717 And with:
3718
3719 funcÿ (Latin1 'ÿ' [0xff])
3720 funcÿ1
3721 fund
3722
3723 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3724 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3725
3726 And with:
3727
3728 ÿÿ (Latin1 'ÿ' [0xff])
3729 ÿÿ1
3730
3731 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3732 the end of the list.
3733 */
3734 std::string after = search_name;
3735 while (!after.empty () && (unsigned char) after.back () == 0xff)
3736 after.pop_back ();
3737 if (!after.empty ())
3738 after.back () = (unsigned char) after.back () + 1;
3739 return after;
3740 }
3741
3742 /* See declaration. */
3743
3744 std::pair<std::vector<name_component>::const_iterator,
3745 std::vector<name_component>::const_iterator>
3746 mapped_index_base::find_name_components_bounds
3747 (const lookup_name_info &lookup_name_without_params, language lang) const
3748 {
3749 auto *name_cmp
3750 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3751
3752 const char *lang_name
3753 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3754
3755 /* Comparison function object for lower_bound that matches against a
3756 given symbol name. */
3757 auto lookup_compare_lower = [&] (const name_component &elem,
3758 const char *name)
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 (elem_name, name) < 0;
3763 };
3764
3765 /* Comparison function object for upper_bound that matches against a
3766 given symbol name. */
3767 auto lookup_compare_upper = [&] (const char *name,
3768 const name_component &elem)
3769 {
3770 const char *elem_qualified = this->symbol_name_at (elem.idx);
3771 const char *elem_name = elem_qualified + elem.name_offset;
3772 return name_cmp (name, elem_name) < 0;
3773 };
3774
3775 auto begin = this->name_components.begin ();
3776 auto end = this->name_components.end ();
3777
3778 /* Find the lower bound. */
3779 auto lower = [&] ()
3780 {
3781 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3782 return begin;
3783 else
3784 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3785 } ();
3786
3787 /* Find the upper bound. */
3788 auto upper = [&] ()
3789 {
3790 if (lookup_name_without_params.completion_mode ())
3791 {
3792 /* In completion mode, we want UPPER to point past all
3793 symbols names that have the same prefix. I.e., with
3794 these symbols, and completing "func":
3795
3796 function << lower bound
3797 function1
3798 other_function << upper bound
3799
3800 We find the upper bound by looking for the insertion
3801 point of "func"-with-last-character-incremented,
3802 i.e. "fund". */
3803 std::string after = make_sort_after_prefix_name (lang_name);
3804 if (after.empty ())
3805 return end;
3806 return std::lower_bound (lower, end, after.c_str (),
3807 lookup_compare_lower);
3808 }
3809 else
3810 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3811 } ();
3812
3813 return {lower, upper};
3814 }
3815
3816 /* See declaration. */
3817
3818 void
3819 mapped_index_base::build_name_components ()
3820 {
3821 if (!this->name_components.empty ())
3822 return;
3823
3824 this->name_components_casing = case_sensitivity;
3825 auto *name_cmp
3826 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3827
3828 /* The code below only knows how to break apart components of C++
3829 symbol names (and other languages that use '::' as
3830 namespace/module separator) and Ada symbol names. */
3831 auto count = this->symbol_name_count ();
3832 for (offset_type idx = 0; idx < count; idx++)
3833 {
3834 if (this->symbol_name_slot_invalid (idx))
3835 continue;
3836
3837 const char *name = this->symbol_name_at (idx);
3838
3839 /* Add each name component to the name component table. */
3840 unsigned int previous_len = 0;
3841
3842 if (strstr (name, "::") != nullptr)
3843 {
3844 for (unsigned int current_len = cp_find_first_component (name);
3845 name[current_len] != '\0';
3846 current_len += cp_find_first_component (name + current_len))
3847 {
3848 gdb_assert (name[current_len] == ':');
3849 this->name_components.push_back ({previous_len, idx});
3850 /* Skip the '::'. */
3851 current_len += 2;
3852 previous_len = current_len;
3853 }
3854 }
3855 else
3856 {
3857 /* Handle the Ada encoded (aka mangled) form here. */
3858 for (const char *iter = strstr (name, "__");
3859 iter != nullptr;
3860 iter = strstr (iter, "__"))
3861 {
3862 this->name_components.push_back ({previous_len, idx});
3863 iter += 2;
3864 previous_len = iter - name;
3865 }
3866 }
3867
3868 this->name_components.push_back ({previous_len, idx});
3869 }
3870
3871 /* Sort name_components elements by name. */
3872 auto name_comp_compare = [&] (const name_component &left,
3873 const name_component &right)
3874 {
3875 const char *left_qualified = this->symbol_name_at (left.idx);
3876 const char *right_qualified = this->symbol_name_at (right.idx);
3877
3878 const char *left_name = left_qualified + left.name_offset;
3879 const char *right_name = right_qualified + right.name_offset;
3880
3881 return name_cmp (left_name, right_name) < 0;
3882 };
3883
3884 std::sort (this->name_components.begin (),
3885 this->name_components.end (),
3886 name_comp_compare);
3887 }
3888
3889 /* Helper for dw2_expand_symtabs_matching that works with a
3890 mapped_index_base instead of the containing objfile. This is split
3891 to a separate function in order to be able to unit test the
3892 name_components matching using a mock mapped_index_base. For each
3893 symbol name that matches, calls MATCH_CALLBACK, passing it the
3894 symbol's index in the mapped_index_base symbol table. */
3895
3896 static void
3897 dw2_expand_symtabs_matching_symbol
3898 (mapped_index_base &index,
3899 const lookup_name_info &lookup_name_in,
3900 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3901 enum search_domain kind,
3902 gdb::function_view<bool (offset_type)> match_callback)
3903 {
3904 lookup_name_info lookup_name_without_params
3905 = lookup_name_in.make_ignore_params ();
3906
3907 /* Build the symbol name component sorted vector, if we haven't
3908 yet. */
3909 index.build_name_components ();
3910
3911 /* The same symbol may appear more than once in the range though.
3912 E.g., if we're looking for symbols that complete "w", and we have
3913 a symbol named "w1::w2", we'll find the two name components for
3914 that same symbol in the range. To be sure we only call the
3915 callback once per symbol, we first collect the symbol name
3916 indexes that matched in a temporary vector and ignore
3917 duplicates. */
3918 std::vector<offset_type> matches;
3919
3920 struct name_and_matcher
3921 {
3922 symbol_name_matcher_ftype *matcher;
3923 const std::string &name;
3924
3925 bool operator== (const name_and_matcher &other) const
3926 {
3927 return matcher == other.matcher && name == other.name;
3928 }
3929 };
3930
3931 /* A vector holding all the different symbol name matchers, for all
3932 languages. */
3933 std::vector<name_and_matcher> matchers;
3934
3935 for (int i = 0; i < nr_languages; i++)
3936 {
3937 enum language lang_e = (enum language) i;
3938
3939 const language_defn *lang = language_def (lang_e);
3940 symbol_name_matcher_ftype *name_matcher
3941 = get_symbol_name_matcher (lang, lookup_name_without_params);
3942
3943 name_and_matcher key {
3944 name_matcher,
3945 lookup_name_without_params.language_lookup_name (lang_e)
3946 };
3947
3948 /* Don't insert the same comparison routine more than once.
3949 Note that we do this linear walk. This is not a problem in
3950 practice because the number of supported languages is
3951 low. */
3952 if (std::find (matchers.begin (), matchers.end (), key)
3953 != matchers.end ())
3954 continue;
3955 matchers.push_back (std::move (key));
3956
3957 auto bounds
3958 = index.find_name_components_bounds (lookup_name_without_params,
3959 lang_e);
3960
3961 /* Now for each symbol name in range, check to see if we have a name
3962 match, and if so, call the MATCH_CALLBACK callback. */
3963
3964 for (; bounds.first != bounds.second; ++bounds.first)
3965 {
3966 const char *qualified = index.symbol_name_at (bounds.first->idx);
3967
3968 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3969 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3970 continue;
3971
3972 matches.push_back (bounds.first->idx);
3973 }
3974 }
3975
3976 std::sort (matches.begin (), matches.end ());
3977
3978 /* Finally call the callback, once per match. */
3979 ULONGEST prev = -1;
3980 for (offset_type idx : matches)
3981 {
3982 if (prev != idx)
3983 {
3984 if (!match_callback (idx))
3985 break;
3986 prev = idx;
3987 }
3988 }
3989
3990 /* Above we use a type wider than idx's for 'prev', since 0 and
3991 (offset_type)-1 are both possible values. */
3992 static_assert (sizeof (prev) > sizeof (offset_type), "");
3993 }
3994
3995 #if GDB_SELF_TEST
3996
3997 namespace selftests { namespace dw2_expand_symtabs_matching {
3998
3999 /* A mock .gdb_index/.debug_names-like name index table, enough to
4000 exercise dw2_expand_symtabs_matching_symbol, which works with the
4001 mapped_index_base interface. Builds an index from the symbol list
4002 passed as parameter to the constructor. */
4003 class mock_mapped_index : public mapped_index_base
4004 {
4005 public:
4006 mock_mapped_index (gdb::array_view<const char *> symbols)
4007 : m_symbol_table (symbols)
4008 {}
4009
4010 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4011
4012 /* Return the number of names in the symbol table. */
4013 size_t symbol_name_count () const override
4014 {
4015 return m_symbol_table.size ();
4016 }
4017
4018 /* Get the name of the symbol at IDX in the symbol table. */
4019 const char *symbol_name_at (offset_type idx) const override
4020 {
4021 return m_symbol_table[idx];
4022 }
4023
4024 private:
4025 gdb::array_view<const char *> m_symbol_table;
4026 };
4027
4028 /* Convenience function that converts a NULL pointer to a "<null>"
4029 string, to pass to print routines. */
4030
4031 static const char *
4032 string_or_null (const char *str)
4033 {
4034 return str != NULL ? str : "<null>";
4035 }
4036
4037 /* Check if a lookup_name_info built from
4038 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4039 index. EXPECTED_LIST is the list of expected matches, in expected
4040 matching order. If no match expected, then an empty list is
4041 specified. Returns true on success. On failure prints a warning
4042 indicating the file:line that failed, and returns false. */
4043
4044 static bool
4045 check_match (const char *file, int line,
4046 mock_mapped_index &mock_index,
4047 const char *name, symbol_name_match_type match_type,
4048 bool completion_mode,
4049 std::initializer_list<const char *> expected_list)
4050 {
4051 lookup_name_info lookup_name (name, match_type, completion_mode);
4052
4053 bool matched = true;
4054
4055 auto mismatch = [&] (const char *expected_str,
4056 const char *got)
4057 {
4058 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4059 "expected=\"%s\", got=\"%s\"\n"),
4060 file, line,
4061 (match_type == symbol_name_match_type::FULL
4062 ? "FULL" : "WILD"),
4063 name, string_or_null (expected_str), string_or_null (got));
4064 matched = false;
4065 };
4066
4067 auto expected_it = expected_list.begin ();
4068 auto expected_end = expected_list.end ();
4069
4070 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4071 NULL, ALL_DOMAIN,
4072 [&] (offset_type idx)
4073 {
4074 const char *matched_name = mock_index.symbol_name_at (idx);
4075 const char *expected_str
4076 = expected_it == expected_end ? NULL : *expected_it++;
4077
4078 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4079 mismatch (expected_str, matched_name);
4080 return true;
4081 });
4082
4083 const char *expected_str
4084 = expected_it == expected_end ? NULL : *expected_it++;
4085 if (expected_str != NULL)
4086 mismatch (expected_str, NULL);
4087
4088 return matched;
4089 }
4090
4091 /* The symbols added to the mock mapped_index for testing (in
4092 canonical form). */
4093 static const char *test_symbols[] = {
4094 "function",
4095 "std::bar",
4096 "std::zfunction",
4097 "std::zfunction2",
4098 "w1::w2",
4099 "ns::foo<char*>",
4100 "ns::foo<int>",
4101 "ns::foo<long>",
4102 "ns2::tmpl<int>::foo2",
4103 "(anonymous namespace)::A::B::C",
4104
4105 /* These are used to check that the increment-last-char in the
4106 matching algorithm for completion doesn't match "t1_fund" when
4107 completing "t1_func". */
4108 "t1_func",
4109 "t1_func1",
4110 "t1_fund",
4111 "t1_fund1",
4112
4113 /* A UTF-8 name with multi-byte sequences to make sure that
4114 cp-name-parser understands this as a single identifier ("função"
4115 is "function" in PT). */
4116 u8"u8função",
4117
4118 /* \377 (0xff) is Latin1 'ÿ'. */
4119 "yfunc\377",
4120
4121 /* \377 (0xff) is Latin1 'ÿ'. */
4122 "\377",
4123 "\377\377123",
4124
4125 /* A name with all sorts of complications. Starts with "z" to make
4126 it easier for the completion tests below. */
4127 #define Z_SYM_NAME \
4128 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4129 "::tuple<(anonymous namespace)::ui*, " \
4130 "std::default_delete<(anonymous namespace)::ui>, void>"
4131
4132 Z_SYM_NAME
4133 };
4134
4135 /* Returns true if the mapped_index_base::find_name_component_bounds
4136 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4137 in completion mode. */
4138
4139 static bool
4140 check_find_bounds_finds (mapped_index_base &index,
4141 const char *search_name,
4142 gdb::array_view<const char *> expected_syms)
4143 {
4144 lookup_name_info lookup_name (search_name,
4145 symbol_name_match_type::FULL, true);
4146
4147 auto bounds = index.find_name_components_bounds (lookup_name,
4148 language_cplus);
4149
4150 size_t distance = std::distance (bounds.first, bounds.second);
4151 if (distance != expected_syms.size ())
4152 return false;
4153
4154 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4155 {
4156 auto nc_elem = bounds.first + exp_elem;
4157 const char *qualified = index.symbol_name_at (nc_elem->idx);
4158 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4159 return false;
4160 }
4161
4162 return true;
4163 }
4164
4165 /* Test the lower-level mapped_index::find_name_component_bounds
4166 method. */
4167
4168 static void
4169 test_mapped_index_find_name_component_bounds ()
4170 {
4171 mock_mapped_index mock_index (test_symbols);
4172
4173 mock_index.build_name_components ();
4174
4175 /* Test the lower-level mapped_index::find_name_component_bounds
4176 method in completion mode. */
4177 {
4178 static const char *expected_syms[] = {
4179 "t1_func",
4180 "t1_func1",
4181 };
4182
4183 SELF_CHECK (check_find_bounds_finds (mock_index,
4184 "t1_func", expected_syms));
4185 }
4186
4187 /* Check that the increment-last-char in the name matching algorithm
4188 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4189 {
4190 static const char *expected_syms1[] = {
4191 "\377",
4192 "\377\377123",
4193 };
4194 SELF_CHECK (check_find_bounds_finds (mock_index,
4195 "\377", expected_syms1));
4196
4197 static const char *expected_syms2[] = {
4198 "\377\377123",
4199 };
4200 SELF_CHECK (check_find_bounds_finds (mock_index,
4201 "\377\377", expected_syms2));
4202 }
4203 }
4204
4205 /* Test dw2_expand_symtabs_matching_symbol. */
4206
4207 static void
4208 test_dw2_expand_symtabs_matching_symbol ()
4209 {
4210 mock_mapped_index mock_index (test_symbols);
4211
4212 /* We let all tests run until the end even if some fails, for debug
4213 convenience. */
4214 bool any_mismatch = false;
4215
4216 /* Create the expected symbols list (an initializer_list). Needed
4217 because lists have commas, and we need to pass them to CHECK,
4218 which is a macro. */
4219 #define EXPECT(...) { __VA_ARGS__ }
4220
4221 /* Wrapper for check_match that passes down the current
4222 __FILE__/__LINE__. */
4223 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4224 any_mismatch |= !check_match (__FILE__, __LINE__, \
4225 mock_index, \
4226 NAME, MATCH_TYPE, COMPLETION_MODE, \
4227 EXPECTED_LIST)
4228
4229 /* Identity checks. */
4230 for (const char *sym : test_symbols)
4231 {
4232 /* Should be able to match all existing symbols. */
4233 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4234 EXPECT (sym));
4235
4236 /* Should be able to match all existing symbols with
4237 parameters. */
4238 std::string with_params = std::string (sym) + "(int)";
4239 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4240 EXPECT (sym));
4241
4242 /* Should be able to match all existing symbols with
4243 parameters and qualifiers. */
4244 with_params = std::string (sym) + " ( int ) const";
4245 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4246 EXPECT (sym));
4247
4248 /* This should really find sym, but cp-name-parser.y doesn't
4249 know about lvalue/rvalue qualifiers yet. */
4250 with_params = std::string (sym) + " ( int ) &&";
4251 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4252 {});
4253 }
4254
4255 /* Check that the name matching algorithm for completion doesn't get
4256 confused with Latin1 'ÿ' / 0xff. */
4257 {
4258 static const char str[] = "\377";
4259 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4260 EXPECT ("\377", "\377\377123"));
4261 }
4262
4263 /* Check that the increment-last-char in the matching algorithm for
4264 completion doesn't match "t1_fund" when completing "t1_func". */
4265 {
4266 static const char str[] = "t1_func";
4267 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4268 EXPECT ("t1_func", "t1_func1"));
4269 }
4270
4271 /* Check that completion mode works at each prefix of the expected
4272 symbol name. */
4273 {
4274 static const char str[] = "function(int)";
4275 size_t len = strlen (str);
4276 std::string lookup;
4277
4278 for (size_t i = 1; i < len; i++)
4279 {
4280 lookup.assign (str, i);
4281 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4282 EXPECT ("function"));
4283 }
4284 }
4285
4286 /* While "w" is a prefix of both components, the match function
4287 should still only be called once. */
4288 {
4289 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4290 EXPECT ("w1::w2"));
4291 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4292 EXPECT ("w1::w2"));
4293 }
4294
4295 /* Same, with a "complicated" symbol. */
4296 {
4297 static const char str[] = Z_SYM_NAME;
4298 size_t len = strlen (str);
4299 std::string lookup;
4300
4301 for (size_t i = 1; i < len; i++)
4302 {
4303 lookup.assign (str, i);
4304 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4305 EXPECT (Z_SYM_NAME));
4306 }
4307 }
4308
4309 /* In FULL mode, an incomplete symbol doesn't match. */
4310 {
4311 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4312 {});
4313 }
4314
4315 /* A complete symbol with parameters matches any overload, since the
4316 index has no overload info. */
4317 {
4318 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4319 EXPECT ("std::zfunction", "std::zfunction2"));
4320 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4321 EXPECT ("std::zfunction", "std::zfunction2"));
4322 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4323 EXPECT ("std::zfunction", "std::zfunction2"));
4324 }
4325
4326 /* Check that whitespace is ignored appropriately. A symbol with a
4327 template argument list. */
4328 {
4329 static const char expected[] = "ns::foo<int>";
4330 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4331 EXPECT (expected));
4332 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4333 EXPECT (expected));
4334 }
4335
4336 /* Check that whitespace is ignored appropriately. A symbol with a
4337 template argument list that includes a pointer. */
4338 {
4339 static const char expected[] = "ns::foo<char*>";
4340 /* Try both completion and non-completion modes. */
4341 static const bool completion_mode[2] = {false, true};
4342 for (size_t i = 0; i < 2; i++)
4343 {
4344 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4345 completion_mode[i], EXPECT (expected));
4346 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4347 completion_mode[i], EXPECT (expected));
4348
4349 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4350 completion_mode[i], EXPECT (expected));
4351 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4352 completion_mode[i], EXPECT (expected));
4353 }
4354 }
4355
4356 {
4357 /* Check method qualifiers are ignored. */
4358 static const char expected[] = "ns::foo<char*>";
4359 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4360 symbol_name_match_type::FULL, true, EXPECT (expected));
4361 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4362 symbol_name_match_type::FULL, true, EXPECT (expected));
4363 CHECK_MATCH ("foo < char * > ( int ) const",
4364 symbol_name_match_type::WILD, true, EXPECT (expected));
4365 CHECK_MATCH ("foo < char * > ( int ) &&",
4366 symbol_name_match_type::WILD, true, EXPECT (expected));
4367 }
4368
4369 /* Test lookup names that don't match anything. */
4370 {
4371 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4372 {});
4373
4374 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4375 {});
4376 }
4377
4378 /* Some wild matching tests, exercising "(anonymous namespace)",
4379 which should not be confused with a parameter list. */
4380 {
4381 static const char *syms[] = {
4382 "A::B::C",
4383 "B::C",
4384 "C",
4385 "A :: B :: C ( int )",
4386 "B :: C ( int )",
4387 "C ( int )",
4388 };
4389
4390 for (const char *s : syms)
4391 {
4392 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4393 EXPECT ("(anonymous namespace)::A::B::C"));
4394 }
4395 }
4396
4397 {
4398 static const char expected[] = "ns2::tmpl<int>::foo2";
4399 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4400 EXPECT (expected));
4401 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4402 EXPECT (expected));
4403 }
4404
4405 SELF_CHECK (!any_mismatch);
4406
4407 #undef EXPECT
4408 #undef CHECK_MATCH
4409 }
4410
4411 static void
4412 run_test ()
4413 {
4414 test_mapped_index_find_name_component_bounds ();
4415 test_dw2_expand_symtabs_matching_symbol ();
4416 }
4417
4418 }} // namespace selftests::dw2_expand_symtabs_matching
4419
4420 #endif /* GDB_SELF_TEST */
4421
4422 /* If FILE_MATCHER is NULL or if PER_CU has
4423 dwarf2_per_cu_quick_data::MARK set (see
4424 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4425 EXPANSION_NOTIFY on it. */
4426
4427 static void
4428 dw2_expand_symtabs_matching_one
4429 (struct dwarf2_per_cu_data *per_cu,
4430 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4431 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4432 {
4433 if (file_matcher == NULL || per_cu->v.quick->mark)
4434 {
4435 bool symtab_was_null
4436 = (per_cu->v.quick->compunit_symtab == NULL);
4437
4438 dw2_instantiate_symtab (per_cu, false);
4439
4440 if (expansion_notify != NULL
4441 && symtab_was_null
4442 && per_cu->v.quick->compunit_symtab != NULL)
4443 expansion_notify (per_cu->v.quick->compunit_symtab);
4444 }
4445 }
4446
4447 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4448 matched, to expand corresponding CUs that were marked. IDX is the
4449 index of the symbol name that matched. */
4450
4451 static void
4452 dw2_expand_marked_cus
4453 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4454 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4455 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4456 search_domain kind)
4457 {
4458 offset_type *vec, vec_len, vec_idx;
4459 bool global_seen = false;
4460 mapped_index &index = *dwarf2_per_objfile->index_table;
4461
4462 vec = (offset_type *) (index.constant_pool
4463 + MAYBE_SWAP (index.symbol_table[idx].vec));
4464 vec_len = MAYBE_SWAP (vec[0]);
4465 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4466 {
4467 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4468 /* This value is only valid for index versions >= 7. */
4469 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4470 gdb_index_symbol_kind symbol_kind =
4471 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4472 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4473 /* Only check the symbol attributes if they're present.
4474 Indices prior to version 7 don't record them,
4475 and indices >= 7 may elide them for certain symbols
4476 (gold does this). */
4477 int attrs_valid =
4478 (index.version >= 7
4479 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4480
4481 /* Work around gold/15646. */
4482 if (attrs_valid)
4483 {
4484 if (!is_static && global_seen)
4485 continue;
4486 if (!is_static)
4487 global_seen = true;
4488 }
4489
4490 /* Only check the symbol's kind if it has one. */
4491 if (attrs_valid)
4492 {
4493 switch (kind)
4494 {
4495 case VARIABLES_DOMAIN:
4496 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4497 continue;
4498 break;
4499 case FUNCTIONS_DOMAIN:
4500 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4501 continue;
4502 break;
4503 case TYPES_DOMAIN:
4504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4505 continue;
4506 break;
4507 case MODULES_DOMAIN:
4508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4509 continue;
4510 break;
4511 default:
4512 break;
4513 }
4514 }
4515
4516 /* Don't crash on bad data. */
4517 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4518 + dwarf2_per_objfile->all_type_units.size ()))
4519 {
4520 complaint (_(".gdb_index entry has bad CU index"
4521 " [in module %s]"),
4522 objfile_name (dwarf2_per_objfile->objfile));
4523 continue;
4524 }
4525
4526 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4527 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4528 expansion_notify);
4529 }
4530 }
4531
4532 /* If FILE_MATCHER is non-NULL, set all the
4533 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4534 that match FILE_MATCHER. */
4535
4536 static void
4537 dw_expand_symtabs_matching_file_matcher
4538 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4539 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4540 {
4541 if (file_matcher == NULL)
4542 return;
4543
4544 objfile *const objfile = dwarf2_per_objfile->objfile;
4545
4546 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4547 htab_eq_pointer,
4548 NULL, xcalloc, xfree));
4549 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4550 htab_eq_pointer,
4551 NULL, xcalloc, xfree));
4552
4553 /* The rule is CUs specify all the files, including those used by
4554 any TU, so there's no need to scan TUs here. */
4555
4556 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4557 {
4558 QUIT;
4559
4560 per_cu->v.quick->mark = 0;
4561
4562 /* We only need to look at symtabs not already expanded. */
4563 if (per_cu->v.quick->compunit_symtab)
4564 continue;
4565
4566 quick_file_names *file_data = dw2_get_file_names (per_cu);
4567 if (file_data == NULL)
4568 continue;
4569
4570 if (htab_find (visited_not_found.get (), file_data) != NULL)
4571 continue;
4572 else if (htab_find (visited_found.get (), file_data) != NULL)
4573 {
4574 per_cu->v.quick->mark = 1;
4575 continue;
4576 }
4577
4578 for (int j = 0; j < file_data->num_file_names; ++j)
4579 {
4580 const char *this_real_name;
4581
4582 if (file_matcher (file_data->file_names[j], false))
4583 {
4584 per_cu->v.quick->mark = 1;
4585 break;
4586 }
4587
4588 /* Before we invoke realpath, which can get expensive when many
4589 files are involved, do a quick comparison of the basenames. */
4590 if (!basenames_may_differ
4591 && !file_matcher (lbasename (file_data->file_names[j]),
4592 true))
4593 continue;
4594
4595 this_real_name = dw2_get_real_path (objfile, file_data, j);
4596 if (file_matcher (this_real_name, false))
4597 {
4598 per_cu->v.quick->mark = 1;
4599 break;
4600 }
4601 }
4602
4603 void **slot = htab_find_slot (per_cu->v.quick->mark
4604 ? visited_found.get ()
4605 : visited_not_found.get (),
4606 file_data, INSERT);
4607 *slot = file_data;
4608 }
4609 }
4610
4611 static void
4612 dw2_expand_symtabs_matching
4613 (struct objfile *objfile,
4614 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4615 const lookup_name_info &lookup_name,
4616 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4617 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4618 enum search_domain kind)
4619 {
4620 struct dwarf2_per_objfile *dwarf2_per_objfile
4621 = get_dwarf2_per_objfile (objfile);
4622
4623 /* index_table is NULL if OBJF_READNOW. */
4624 if (!dwarf2_per_objfile->index_table)
4625 return;
4626
4627 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4628
4629 mapped_index &index = *dwarf2_per_objfile->index_table;
4630
4631 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4632 symbol_matcher,
4633 kind, [&] (offset_type idx)
4634 {
4635 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4636 expansion_notify, kind);
4637 return true;
4638 });
4639 }
4640
4641 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4642 symtab. */
4643
4644 static struct compunit_symtab *
4645 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4646 CORE_ADDR pc)
4647 {
4648 int i;
4649
4650 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4651 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4652 return cust;
4653
4654 if (cust->includes == NULL)
4655 return NULL;
4656
4657 for (i = 0; cust->includes[i]; ++i)
4658 {
4659 struct compunit_symtab *s = cust->includes[i];
4660
4661 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4662 if (s != NULL)
4663 return s;
4664 }
4665
4666 return NULL;
4667 }
4668
4669 static struct compunit_symtab *
4670 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4671 struct bound_minimal_symbol msymbol,
4672 CORE_ADDR pc,
4673 struct obj_section *section,
4674 int warn_if_readin)
4675 {
4676 struct dwarf2_per_cu_data *data;
4677 struct compunit_symtab *result;
4678
4679 if (!objfile->partial_symtabs->psymtabs_addrmap)
4680 return NULL;
4681
4682 CORE_ADDR baseaddr = objfile->text_section_offset ();
4683 data = (struct dwarf2_per_cu_data *) addrmap_find
4684 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4685 if (!data)
4686 return NULL;
4687
4688 if (warn_if_readin && data->v.quick->compunit_symtab)
4689 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4690 paddress (get_objfile_arch (objfile), pc));
4691
4692 result
4693 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4694 false),
4695 pc);
4696 gdb_assert (result != NULL);
4697 return result;
4698 }
4699
4700 static void
4701 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4702 void *data, int need_fullname)
4703 {
4704 struct dwarf2_per_objfile *dwarf2_per_objfile
4705 = get_dwarf2_per_objfile (objfile);
4706
4707 if (!dwarf2_per_objfile->filenames_cache)
4708 {
4709 dwarf2_per_objfile->filenames_cache.emplace ();
4710
4711 htab_up visited (htab_create_alloc (10,
4712 htab_hash_pointer, htab_eq_pointer,
4713 NULL, xcalloc, xfree));
4714
4715 /* The rule is CUs specify all the files, including those used
4716 by any TU, so there's no need to scan TUs here. We can
4717 ignore file names coming from already-expanded CUs. */
4718
4719 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4720 {
4721 if (per_cu->v.quick->compunit_symtab)
4722 {
4723 void **slot = htab_find_slot (visited.get (),
4724 per_cu->v.quick->file_names,
4725 INSERT);
4726
4727 *slot = per_cu->v.quick->file_names;
4728 }
4729 }
4730
4731 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4732 {
4733 /* We only need to look at symtabs not already expanded. */
4734 if (per_cu->v.quick->compunit_symtab)
4735 continue;
4736
4737 quick_file_names *file_data = dw2_get_file_names (per_cu);
4738 if (file_data == NULL)
4739 continue;
4740
4741 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4742 if (*slot)
4743 {
4744 /* Already visited. */
4745 continue;
4746 }
4747 *slot = file_data;
4748
4749 for (int j = 0; j < file_data->num_file_names; ++j)
4750 {
4751 const char *filename = file_data->file_names[j];
4752 dwarf2_per_objfile->filenames_cache->seen (filename);
4753 }
4754 }
4755 }
4756
4757 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4758 {
4759 gdb::unique_xmalloc_ptr<char> this_real_name;
4760
4761 if (need_fullname)
4762 this_real_name = gdb_realpath (filename);
4763 (*fun) (filename, this_real_name.get (), data);
4764 });
4765 }
4766
4767 static int
4768 dw2_has_symbols (struct objfile *objfile)
4769 {
4770 return 1;
4771 }
4772
4773 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4774 {
4775 dw2_has_symbols,
4776 dw2_find_last_source_symtab,
4777 dw2_forget_cached_source_info,
4778 dw2_map_symtabs_matching_filename,
4779 dw2_lookup_symbol,
4780 dw2_print_stats,
4781 dw2_dump,
4782 dw2_expand_symtabs_for_function,
4783 dw2_expand_all_symtabs,
4784 dw2_expand_symtabs_with_fullname,
4785 dw2_map_matching_symbols,
4786 dw2_expand_symtabs_matching,
4787 dw2_find_pc_sect_compunit_symtab,
4788 NULL,
4789 dw2_map_symbol_filenames
4790 };
4791
4792 /* DWARF-5 debug_names reader. */
4793
4794 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4795 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4796
4797 /* A helper function that reads the .debug_names section in SECTION
4798 and fills in MAP. FILENAME is the name of the file containing the
4799 section; it is used for error reporting.
4800
4801 Returns true if all went well, false otherwise. */
4802
4803 static bool
4804 read_debug_names_from_section (struct objfile *objfile,
4805 const char *filename,
4806 struct dwarf2_section_info *section,
4807 mapped_debug_names &map)
4808 {
4809 if (section->empty ())
4810 return false;
4811
4812 /* Older elfutils strip versions could keep the section in the main
4813 executable while splitting it for the separate debug info file. */
4814 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4815 return false;
4816
4817 section->read (objfile);
4818
4819 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4820
4821 const gdb_byte *addr = section->buffer;
4822
4823 bfd *const abfd = section->get_bfd_owner ();
4824
4825 unsigned int bytes_read;
4826 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4827 addr += bytes_read;
4828
4829 map.dwarf5_is_dwarf64 = bytes_read != 4;
4830 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4831 if (bytes_read + length != section->size)
4832 {
4833 /* There may be multiple per-CU indices. */
4834 warning (_("Section .debug_names in %s length %s does not match "
4835 "section length %s, ignoring .debug_names."),
4836 filename, plongest (bytes_read + length),
4837 pulongest (section->size));
4838 return false;
4839 }
4840
4841 /* The version number. */
4842 uint16_t version = read_2_bytes (abfd, addr);
4843 addr += 2;
4844 if (version != 5)
4845 {
4846 warning (_("Section .debug_names in %s has unsupported version %d, "
4847 "ignoring .debug_names."),
4848 filename, version);
4849 return false;
4850 }
4851
4852 /* Padding. */
4853 uint16_t padding = read_2_bytes (abfd, addr);
4854 addr += 2;
4855 if (padding != 0)
4856 {
4857 warning (_("Section .debug_names in %s has unsupported padding %d, "
4858 "ignoring .debug_names."),
4859 filename, padding);
4860 return false;
4861 }
4862
4863 /* comp_unit_count - The number of CUs in the CU list. */
4864 map.cu_count = read_4_bytes (abfd, addr);
4865 addr += 4;
4866
4867 /* local_type_unit_count - The number of TUs in the local TU
4868 list. */
4869 map.tu_count = read_4_bytes (abfd, addr);
4870 addr += 4;
4871
4872 /* foreign_type_unit_count - The number of TUs in the foreign TU
4873 list. */
4874 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4875 addr += 4;
4876 if (foreign_tu_count != 0)
4877 {
4878 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4879 "ignoring .debug_names."),
4880 filename, static_cast<unsigned long> (foreign_tu_count));
4881 return false;
4882 }
4883
4884 /* bucket_count - The number of hash buckets in the hash lookup
4885 table. */
4886 map.bucket_count = read_4_bytes (abfd, addr);
4887 addr += 4;
4888
4889 /* name_count - The number of unique names in the index. */
4890 map.name_count = read_4_bytes (abfd, addr);
4891 addr += 4;
4892
4893 /* abbrev_table_size - The size in bytes of the abbreviations
4894 table. */
4895 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4896 addr += 4;
4897
4898 /* augmentation_string_size - The size in bytes of the augmentation
4899 string. This value is rounded up to a multiple of 4. */
4900 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4901 addr += 4;
4902 map.augmentation_is_gdb = ((augmentation_string_size
4903 == sizeof (dwarf5_augmentation))
4904 && memcmp (addr, dwarf5_augmentation,
4905 sizeof (dwarf5_augmentation)) == 0);
4906 augmentation_string_size += (-augmentation_string_size) & 3;
4907 addr += augmentation_string_size;
4908
4909 /* List of CUs */
4910 map.cu_table_reordered = addr;
4911 addr += map.cu_count * map.offset_size;
4912
4913 /* List of Local TUs */
4914 map.tu_table_reordered = addr;
4915 addr += map.tu_count * map.offset_size;
4916
4917 /* Hash Lookup Table */
4918 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4919 addr += map.bucket_count * 4;
4920 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4921 addr += map.name_count * 4;
4922
4923 /* Name Table */
4924 map.name_table_string_offs_reordered = addr;
4925 addr += map.name_count * map.offset_size;
4926 map.name_table_entry_offs_reordered = addr;
4927 addr += map.name_count * map.offset_size;
4928
4929 const gdb_byte *abbrev_table_start = addr;
4930 for (;;)
4931 {
4932 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4933 addr += bytes_read;
4934 if (index_num == 0)
4935 break;
4936
4937 const auto insertpair
4938 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4939 if (!insertpair.second)
4940 {
4941 warning (_("Section .debug_names in %s has duplicate index %s, "
4942 "ignoring .debug_names."),
4943 filename, pulongest (index_num));
4944 return false;
4945 }
4946 mapped_debug_names::index_val &indexval = insertpair.first->second;
4947 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4948 addr += bytes_read;
4949
4950 for (;;)
4951 {
4952 mapped_debug_names::index_val::attr attr;
4953 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4954 addr += bytes_read;
4955 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4956 addr += bytes_read;
4957 if (attr.form == DW_FORM_implicit_const)
4958 {
4959 attr.implicit_const = read_signed_leb128 (abfd, addr,
4960 &bytes_read);
4961 addr += bytes_read;
4962 }
4963 if (attr.dw_idx == 0 && attr.form == 0)
4964 break;
4965 indexval.attr_vec.push_back (std::move (attr));
4966 }
4967 }
4968 if (addr != abbrev_table_start + abbrev_table_size)
4969 {
4970 warning (_("Section .debug_names in %s has abbreviation_table "
4971 "of size %s vs. written as %u, ignoring .debug_names."),
4972 filename, plongest (addr - abbrev_table_start),
4973 abbrev_table_size);
4974 return false;
4975 }
4976 map.entry_pool = addr;
4977
4978 return true;
4979 }
4980
4981 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4982 list. */
4983
4984 static void
4985 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4986 const mapped_debug_names &map,
4987 dwarf2_section_info &section,
4988 bool is_dwz)
4989 {
4990 sect_offset sect_off_prev;
4991 for (uint32_t i = 0; i <= map.cu_count; ++i)
4992 {
4993 sect_offset sect_off_next;
4994 if (i < map.cu_count)
4995 {
4996 sect_off_next
4997 = (sect_offset) (extract_unsigned_integer
4998 (map.cu_table_reordered + i * map.offset_size,
4999 map.offset_size,
5000 map.dwarf5_byte_order));
5001 }
5002 else
5003 sect_off_next = (sect_offset) section.size;
5004 if (i >= 1)
5005 {
5006 const ULONGEST length = sect_off_next - sect_off_prev;
5007 dwarf2_per_cu_data *per_cu
5008 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5009 sect_off_prev, length);
5010 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5011 }
5012 sect_off_prev = sect_off_next;
5013 }
5014 }
5015
5016 /* Read the CU list from the mapped index, and use it to create all
5017 the CU objects for this dwarf2_per_objfile. */
5018
5019 static void
5020 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5021 const mapped_debug_names &map,
5022 const mapped_debug_names &dwz_map)
5023 {
5024 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5025 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5026
5027 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5028 dwarf2_per_objfile->info,
5029 false /* is_dwz */);
5030
5031 if (dwz_map.cu_count == 0)
5032 return;
5033
5034 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5035 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5036 true /* is_dwz */);
5037 }
5038
5039 /* Read .debug_names. If everything went ok, initialize the "quick"
5040 elements of all the CUs and return true. Otherwise, return false. */
5041
5042 static bool
5043 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5044 {
5045 std::unique_ptr<mapped_debug_names> map
5046 (new mapped_debug_names (dwarf2_per_objfile));
5047 mapped_debug_names dwz_map (dwarf2_per_objfile);
5048 struct objfile *objfile = dwarf2_per_objfile->objfile;
5049
5050 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5051 &dwarf2_per_objfile->debug_names,
5052 *map))
5053 return false;
5054
5055 /* Don't use the index if it's empty. */
5056 if (map->name_count == 0)
5057 return false;
5058
5059 /* If there is a .dwz file, read it so we can get its CU list as
5060 well. */
5061 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5062 if (dwz != NULL)
5063 {
5064 if (!read_debug_names_from_section (objfile,
5065 bfd_get_filename (dwz->dwz_bfd.get ()),
5066 &dwz->debug_names, dwz_map))
5067 {
5068 warning (_("could not read '.debug_names' section from %s; skipping"),
5069 bfd_get_filename (dwz->dwz_bfd.get ()));
5070 return false;
5071 }
5072 }
5073
5074 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5075
5076 if (map->tu_count != 0)
5077 {
5078 /* We can only handle a single .debug_types when we have an
5079 index. */
5080 if (dwarf2_per_objfile->types.size () != 1)
5081 return false;
5082
5083 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5084
5085 create_signatured_type_table_from_debug_names
5086 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5087 }
5088
5089 create_addrmap_from_aranges (dwarf2_per_objfile,
5090 &dwarf2_per_objfile->debug_aranges);
5091
5092 dwarf2_per_objfile->debug_names_table = std::move (map);
5093 dwarf2_per_objfile->using_index = 1;
5094 dwarf2_per_objfile->quick_file_names_table =
5095 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5096
5097 return true;
5098 }
5099
5100 /* Type used to manage iterating over all CUs looking for a symbol for
5101 .debug_names. */
5102
5103 class dw2_debug_names_iterator
5104 {
5105 public:
5106 dw2_debug_names_iterator (const mapped_debug_names &map,
5107 gdb::optional<block_enum> block_index,
5108 domain_enum domain,
5109 const char *name)
5110 : m_map (map), m_block_index (block_index), m_domain (domain),
5111 m_addr (find_vec_in_debug_names (map, name))
5112 {}
5113
5114 dw2_debug_names_iterator (const mapped_debug_names &map,
5115 search_domain search, uint32_t namei)
5116 : m_map (map),
5117 m_search (search),
5118 m_addr (find_vec_in_debug_names (map, namei))
5119 {}
5120
5121 dw2_debug_names_iterator (const mapped_debug_names &map,
5122 block_enum block_index, domain_enum domain,
5123 uint32_t namei)
5124 : m_map (map), m_block_index (block_index), m_domain (domain),
5125 m_addr (find_vec_in_debug_names (map, namei))
5126 {}
5127
5128 /* Return the next matching CU or NULL if there are no more. */
5129 dwarf2_per_cu_data *next ();
5130
5131 private:
5132 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5133 const char *name);
5134 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5135 uint32_t namei);
5136
5137 /* The internalized form of .debug_names. */
5138 const mapped_debug_names &m_map;
5139
5140 /* If set, only look for symbols that match that block. Valid values are
5141 GLOBAL_BLOCK and STATIC_BLOCK. */
5142 const gdb::optional<block_enum> m_block_index;
5143
5144 /* The kind of symbol we're looking for. */
5145 const domain_enum m_domain = UNDEF_DOMAIN;
5146 const search_domain m_search = ALL_DOMAIN;
5147
5148 /* The list of CUs from the index entry of the symbol, or NULL if
5149 not found. */
5150 const gdb_byte *m_addr;
5151 };
5152
5153 const char *
5154 mapped_debug_names::namei_to_name (uint32_t namei) const
5155 {
5156 const ULONGEST namei_string_offs
5157 = extract_unsigned_integer ((name_table_string_offs_reordered
5158 + namei * offset_size),
5159 offset_size,
5160 dwarf5_byte_order);
5161 return read_indirect_string_at_offset (dwarf2_per_objfile,
5162 namei_string_offs);
5163 }
5164
5165 /* Find a slot in .debug_names for the object named NAME. If NAME is
5166 found, return pointer to its pool data. If NAME cannot be found,
5167 return NULL. */
5168
5169 const gdb_byte *
5170 dw2_debug_names_iterator::find_vec_in_debug_names
5171 (const mapped_debug_names &map, const char *name)
5172 {
5173 int (*cmp) (const char *, const char *);
5174
5175 gdb::unique_xmalloc_ptr<char> without_params;
5176 if (current_language->la_language == language_cplus
5177 || current_language->la_language == language_fortran
5178 || current_language->la_language == language_d)
5179 {
5180 /* NAME is already canonical. Drop any qualifiers as
5181 .debug_names does not contain any. */
5182
5183 if (strchr (name, '(') != NULL)
5184 {
5185 without_params = cp_remove_params (name);
5186 if (without_params != NULL)
5187 name = without_params.get ();
5188 }
5189 }
5190
5191 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5192
5193 const uint32_t full_hash = dwarf5_djb_hash (name);
5194 uint32_t namei
5195 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5196 (map.bucket_table_reordered
5197 + (full_hash % map.bucket_count)), 4,
5198 map.dwarf5_byte_order);
5199 if (namei == 0)
5200 return NULL;
5201 --namei;
5202 if (namei >= map.name_count)
5203 {
5204 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5205 "[in module %s]"),
5206 namei, map.name_count,
5207 objfile_name (map.dwarf2_per_objfile->objfile));
5208 return NULL;
5209 }
5210
5211 for (;;)
5212 {
5213 const uint32_t namei_full_hash
5214 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5215 (map.hash_table_reordered + namei), 4,
5216 map.dwarf5_byte_order);
5217 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5218 return NULL;
5219
5220 if (full_hash == namei_full_hash)
5221 {
5222 const char *const namei_string = map.namei_to_name (namei);
5223
5224 #if 0 /* An expensive sanity check. */
5225 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5226 {
5227 complaint (_("Wrong .debug_names hash for string at index %u "
5228 "[in module %s]"),
5229 namei, objfile_name (dwarf2_per_objfile->objfile));
5230 return NULL;
5231 }
5232 #endif
5233
5234 if (cmp (namei_string, name) == 0)
5235 {
5236 const ULONGEST namei_entry_offs
5237 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5238 + namei * map.offset_size),
5239 map.offset_size, map.dwarf5_byte_order);
5240 return map.entry_pool + namei_entry_offs;
5241 }
5242 }
5243
5244 ++namei;
5245 if (namei >= map.name_count)
5246 return NULL;
5247 }
5248 }
5249
5250 const gdb_byte *
5251 dw2_debug_names_iterator::find_vec_in_debug_names
5252 (const mapped_debug_names &map, uint32_t namei)
5253 {
5254 if (namei >= map.name_count)
5255 {
5256 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5257 "[in module %s]"),
5258 namei, map.name_count,
5259 objfile_name (map.dwarf2_per_objfile->objfile));
5260 return NULL;
5261 }
5262
5263 const ULONGEST namei_entry_offs
5264 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5265 + namei * map.offset_size),
5266 map.offset_size, map.dwarf5_byte_order);
5267 return map.entry_pool + namei_entry_offs;
5268 }
5269
5270 /* See dw2_debug_names_iterator. */
5271
5272 dwarf2_per_cu_data *
5273 dw2_debug_names_iterator::next ()
5274 {
5275 if (m_addr == NULL)
5276 return NULL;
5277
5278 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5279 struct objfile *objfile = dwarf2_per_objfile->objfile;
5280 bfd *const abfd = objfile->obfd;
5281
5282 again:
5283
5284 unsigned int bytes_read;
5285 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5286 m_addr += bytes_read;
5287 if (abbrev == 0)
5288 return NULL;
5289
5290 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5291 if (indexval_it == m_map.abbrev_map.cend ())
5292 {
5293 complaint (_("Wrong .debug_names undefined abbrev code %s "
5294 "[in module %s]"),
5295 pulongest (abbrev), objfile_name (objfile));
5296 return NULL;
5297 }
5298 const mapped_debug_names::index_val &indexval = indexval_it->second;
5299 enum class symbol_linkage {
5300 unknown,
5301 static_,
5302 extern_,
5303 } symbol_linkage_ = symbol_linkage::unknown;
5304 dwarf2_per_cu_data *per_cu = NULL;
5305 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5306 {
5307 ULONGEST ull;
5308 switch (attr.form)
5309 {
5310 case DW_FORM_implicit_const:
5311 ull = attr.implicit_const;
5312 break;
5313 case DW_FORM_flag_present:
5314 ull = 1;
5315 break;
5316 case DW_FORM_udata:
5317 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5318 m_addr += bytes_read;
5319 break;
5320 default:
5321 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5322 dwarf_form_name (attr.form),
5323 objfile_name (objfile));
5324 return NULL;
5325 }
5326 switch (attr.dw_idx)
5327 {
5328 case DW_IDX_compile_unit:
5329 /* Don't crash on bad data. */
5330 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5331 {
5332 complaint (_(".debug_names entry has bad CU index %s"
5333 " [in module %s]"),
5334 pulongest (ull),
5335 objfile_name (dwarf2_per_objfile->objfile));
5336 continue;
5337 }
5338 per_cu = dwarf2_per_objfile->get_cutu (ull);
5339 break;
5340 case DW_IDX_type_unit:
5341 /* Don't crash on bad data. */
5342 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5343 {
5344 complaint (_(".debug_names entry has bad TU index %s"
5345 " [in module %s]"),
5346 pulongest (ull),
5347 objfile_name (dwarf2_per_objfile->objfile));
5348 continue;
5349 }
5350 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5351 break;
5352 case DW_IDX_GNU_internal:
5353 if (!m_map.augmentation_is_gdb)
5354 break;
5355 symbol_linkage_ = symbol_linkage::static_;
5356 break;
5357 case DW_IDX_GNU_external:
5358 if (!m_map.augmentation_is_gdb)
5359 break;
5360 symbol_linkage_ = symbol_linkage::extern_;
5361 break;
5362 }
5363 }
5364
5365 /* Skip if already read in. */
5366 if (per_cu->v.quick->compunit_symtab)
5367 goto again;
5368
5369 /* Check static vs global. */
5370 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5371 {
5372 const bool want_static = *m_block_index == STATIC_BLOCK;
5373 const bool symbol_is_static =
5374 symbol_linkage_ == symbol_linkage::static_;
5375 if (want_static != symbol_is_static)
5376 goto again;
5377 }
5378
5379 /* Match dw2_symtab_iter_next, symbol_kind
5380 and debug_names::psymbol_tag. */
5381 switch (m_domain)
5382 {
5383 case VAR_DOMAIN:
5384 switch (indexval.dwarf_tag)
5385 {
5386 case DW_TAG_variable:
5387 case DW_TAG_subprogram:
5388 /* Some types are also in VAR_DOMAIN. */
5389 case DW_TAG_typedef:
5390 case DW_TAG_structure_type:
5391 break;
5392 default:
5393 goto again;
5394 }
5395 break;
5396 case STRUCT_DOMAIN:
5397 switch (indexval.dwarf_tag)
5398 {
5399 case DW_TAG_typedef:
5400 case DW_TAG_structure_type:
5401 break;
5402 default:
5403 goto again;
5404 }
5405 break;
5406 case LABEL_DOMAIN:
5407 switch (indexval.dwarf_tag)
5408 {
5409 case 0:
5410 case DW_TAG_variable:
5411 break;
5412 default:
5413 goto again;
5414 }
5415 break;
5416 case MODULE_DOMAIN:
5417 switch (indexval.dwarf_tag)
5418 {
5419 case DW_TAG_module:
5420 break;
5421 default:
5422 goto again;
5423 }
5424 break;
5425 default:
5426 break;
5427 }
5428
5429 /* Match dw2_expand_symtabs_matching, symbol_kind and
5430 debug_names::psymbol_tag. */
5431 switch (m_search)
5432 {
5433 case VARIABLES_DOMAIN:
5434 switch (indexval.dwarf_tag)
5435 {
5436 case DW_TAG_variable:
5437 break;
5438 default:
5439 goto again;
5440 }
5441 break;
5442 case FUNCTIONS_DOMAIN:
5443 switch (indexval.dwarf_tag)
5444 {
5445 case DW_TAG_subprogram:
5446 break;
5447 default:
5448 goto again;
5449 }
5450 break;
5451 case TYPES_DOMAIN:
5452 switch (indexval.dwarf_tag)
5453 {
5454 case DW_TAG_typedef:
5455 case DW_TAG_structure_type:
5456 break;
5457 default:
5458 goto again;
5459 }
5460 break;
5461 case MODULES_DOMAIN:
5462 switch (indexval.dwarf_tag)
5463 {
5464 case DW_TAG_module:
5465 break;
5466 default:
5467 goto again;
5468 }
5469 default:
5470 break;
5471 }
5472
5473 return per_cu;
5474 }
5475
5476 static struct compunit_symtab *
5477 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5478 const char *name, domain_enum domain)
5479 {
5480 struct dwarf2_per_objfile *dwarf2_per_objfile
5481 = get_dwarf2_per_objfile (objfile);
5482
5483 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5484 if (!mapp)
5485 {
5486 /* index is NULL if OBJF_READNOW. */
5487 return NULL;
5488 }
5489 const auto &map = *mapp;
5490
5491 dw2_debug_names_iterator iter (map, block_index, domain, name);
5492
5493 struct compunit_symtab *stab_best = NULL;
5494 struct dwarf2_per_cu_data *per_cu;
5495 while ((per_cu = iter.next ()) != NULL)
5496 {
5497 struct symbol *sym, *with_opaque = NULL;
5498 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5499 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5500 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5501
5502 sym = block_find_symbol (block, name, domain,
5503 block_find_non_opaque_type_preferred,
5504 &with_opaque);
5505
5506 /* Some caution must be observed with overloaded functions and
5507 methods, since the index will not contain any overload
5508 information (but NAME might contain it). */
5509
5510 if (sym != NULL
5511 && strcmp_iw (sym->search_name (), name) == 0)
5512 return stab;
5513 if (with_opaque != NULL
5514 && strcmp_iw (with_opaque->search_name (), name) == 0)
5515 stab_best = stab;
5516
5517 /* Keep looking through other CUs. */
5518 }
5519
5520 return stab_best;
5521 }
5522
5523 /* This dumps minimal information about .debug_names. It is called
5524 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5525 uses this to verify that .debug_names has been loaded. */
5526
5527 static void
5528 dw2_debug_names_dump (struct objfile *objfile)
5529 {
5530 struct dwarf2_per_objfile *dwarf2_per_objfile
5531 = get_dwarf2_per_objfile (objfile);
5532
5533 gdb_assert (dwarf2_per_objfile->using_index);
5534 printf_filtered (".debug_names:");
5535 if (dwarf2_per_objfile->debug_names_table)
5536 printf_filtered (" exists\n");
5537 else
5538 printf_filtered (" faked for \"readnow\"\n");
5539 printf_filtered ("\n");
5540 }
5541
5542 static void
5543 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5544 const char *func_name)
5545 {
5546 struct dwarf2_per_objfile *dwarf2_per_objfile
5547 = get_dwarf2_per_objfile (objfile);
5548
5549 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5550 if (dwarf2_per_objfile->debug_names_table)
5551 {
5552 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5553
5554 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5555
5556 struct dwarf2_per_cu_data *per_cu;
5557 while ((per_cu = iter.next ()) != NULL)
5558 dw2_instantiate_symtab (per_cu, false);
5559 }
5560 }
5561
5562 static void
5563 dw2_debug_names_map_matching_symbols
5564 (struct objfile *objfile,
5565 const lookup_name_info &name, domain_enum domain,
5566 int global,
5567 gdb::function_view<symbol_found_callback_ftype> callback,
5568 symbol_compare_ftype *ordered_compare)
5569 {
5570 struct dwarf2_per_objfile *dwarf2_per_objfile
5571 = get_dwarf2_per_objfile (objfile);
5572
5573 /* debug_names_table is NULL if OBJF_READNOW. */
5574 if (!dwarf2_per_objfile->debug_names_table)
5575 return;
5576
5577 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5578 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5579
5580 const char *match_name = name.ada ().lookup_name ().c_str ();
5581 auto matcher = [&] (const char *symname)
5582 {
5583 if (ordered_compare == nullptr)
5584 return true;
5585 return ordered_compare (symname, match_name) == 0;
5586 };
5587
5588 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5589 [&] (offset_type namei)
5590 {
5591 /* The name was matched, now expand corresponding CUs that were
5592 marked. */
5593 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5594
5595 struct dwarf2_per_cu_data *per_cu;
5596 while ((per_cu = iter.next ()) != NULL)
5597 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5598 return true;
5599 });
5600
5601 /* It's a shame we couldn't do this inside the
5602 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5603 that have already been expanded. Instead, this loop matches what
5604 the psymtab code does. */
5605 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5606 {
5607 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5608 if (cust != nullptr)
5609 {
5610 const struct block *block
5611 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5612 if (!iterate_over_symbols_terminated (block, name,
5613 domain, callback))
5614 break;
5615 }
5616 }
5617 }
5618
5619 static void
5620 dw2_debug_names_expand_symtabs_matching
5621 (struct objfile *objfile,
5622 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5623 const lookup_name_info &lookup_name,
5624 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5625 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5626 enum search_domain kind)
5627 {
5628 struct dwarf2_per_objfile *dwarf2_per_objfile
5629 = get_dwarf2_per_objfile (objfile);
5630
5631 /* debug_names_table is NULL if OBJF_READNOW. */
5632 if (!dwarf2_per_objfile->debug_names_table)
5633 return;
5634
5635 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5636
5637 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5638
5639 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5640 symbol_matcher,
5641 kind, [&] (offset_type namei)
5642 {
5643 /* The name was matched, now expand corresponding CUs that were
5644 marked. */
5645 dw2_debug_names_iterator iter (map, kind, namei);
5646
5647 struct dwarf2_per_cu_data *per_cu;
5648 while ((per_cu = iter.next ()) != NULL)
5649 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5650 expansion_notify);
5651 return true;
5652 });
5653 }
5654
5655 const struct quick_symbol_functions dwarf2_debug_names_functions =
5656 {
5657 dw2_has_symbols,
5658 dw2_find_last_source_symtab,
5659 dw2_forget_cached_source_info,
5660 dw2_map_symtabs_matching_filename,
5661 dw2_debug_names_lookup_symbol,
5662 dw2_print_stats,
5663 dw2_debug_names_dump,
5664 dw2_debug_names_expand_symtabs_for_function,
5665 dw2_expand_all_symtabs,
5666 dw2_expand_symtabs_with_fullname,
5667 dw2_debug_names_map_matching_symbols,
5668 dw2_debug_names_expand_symtabs_matching,
5669 dw2_find_pc_sect_compunit_symtab,
5670 NULL,
5671 dw2_map_symbol_filenames
5672 };
5673
5674 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5675 to either a dwarf2_per_objfile or dwz_file object. */
5676
5677 template <typename T>
5678 static gdb::array_view<const gdb_byte>
5679 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5680 {
5681 dwarf2_section_info *section = &section_owner->gdb_index;
5682
5683 if (section->empty ())
5684 return {};
5685
5686 /* Older elfutils strip versions could keep the section in the main
5687 executable while splitting it for the separate debug info file. */
5688 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5689 return {};
5690
5691 section->read (obj);
5692
5693 /* dwarf2_section_info::size is a bfd_size_type, while
5694 gdb::array_view works with size_t. On 32-bit hosts, with
5695 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5696 is 32-bit. So we need an explicit narrowing conversion here.
5697 This is fine, because it's impossible to allocate or mmap an
5698 array/buffer larger than what size_t can represent. */
5699 return gdb::make_array_view (section->buffer, section->size);
5700 }
5701
5702 /* Lookup the index cache for the contents of the index associated to
5703 DWARF2_OBJ. */
5704
5705 static gdb::array_view<const gdb_byte>
5706 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5707 {
5708 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5709 if (build_id == nullptr)
5710 return {};
5711
5712 return global_index_cache.lookup_gdb_index (build_id,
5713 &dwarf2_obj->index_cache_res);
5714 }
5715
5716 /* Same as the above, but for DWZ. */
5717
5718 static gdb::array_view<const gdb_byte>
5719 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5720 {
5721 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5722 if (build_id == nullptr)
5723 return {};
5724
5725 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5726 }
5727
5728 /* See symfile.h. */
5729
5730 bool
5731 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5732 {
5733 struct dwarf2_per_objfile *dwarf2_per_objfile
5734 = get_dwarf2_per_objfile (objfile);
5735
5736 /* If we're about to read full symbols, don't bother with the
5737 indices. In this case we also don't care if some other debug
5738 format is making psymtabs, because they are all about to be
5739 expanded anyway. */
5740 if ((objfile->flags & OBJF_READNOW))
5741 {
5742 dwarf2_per_objfile->using_index = 1;
5743 create_all_comp_units (dwarf2_per_objfile);
5744 create_all_type_units (dwarf2_per_objfile);
5745 dwarf2_per_objfile->quick_file_names_table
5746 = create_quick_file_names_table
5747 (dwarf2_per_objfile->all_comp_units.size ());
5748
5749 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5750 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5751 {
5752 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5753
5754 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5755 struct dwarf2_per_cu_quick_data);
5756 }
5757
5758 /* Return 1 so that gdb sees the "quick" functions. However,
5759 these functions will be no-ops because we will have expanded
5760 all symtabs. */
5761 *index_kind = dw_index_kind::GDB_INDEX;
5762 return true;
5763 }
5764
5765 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5766 {
5767 *index_kind = dw_index_kind::DEBUG_NAMES;
5768 return true;
5769 }
5770
5771 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5772 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5773 get_gdb_index_contents_from_section<dwz_file>))
5774 {
5775 *index_kind = dw_index_kind::GDB_INDEX;
5776 return true;
5777 }
5778
5779 /* ... otherwise, try to find the index in the index cache. */
5780 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5781 get_gdb_index_contents_from_cache,
5782 get_gdb_index_contents_from_cache_dwz))
5783 {
5784 global_index_cache.hit ();
5785 *index_kind = dw_index_kind::GDB_INDEX;
5786 return true;
5787 }
5788
5789 global_index_cache.miss ();
5790 return false;
5791 }
5792
5793 \f
5794
5795 /* Build a partial symbol table. */
5796
5797 void
5798 dwarf2_build_psymtabs (struct objfile *objfile)
5799 {
5800 struct dwarf2_per_objfile *dwarf2_per_objfile
5801 = get_dwarf2_per_objfile (objfile);
5802
5803 init_psymbol_list (objfile, 1024);
5804
5805 try
5806 {
5807 /* This isn't really ideal: all the data we allocate on the
5808 objfile's obstack is still uselessly kept around. However,
5809 freeing it seems unsafe. */
5810 psymtab_discarder psymtabs (objfile);
5811 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5812 psymtabs.keep ();
5813
5814 /* (maybe) store an index in the cache. */
5815 global_index_cache.store (dwarf2_per_objfile);
5816 }
5817 catch (const gdb_exception_error &except)
5818 {
5819 exception_print (gdb_stderr, except);
5820 }
5821 }
5822
5823 /* Find the base address of the compilation unit for range lists and
5824 location lists. It will normally be specified by DW_AT_low_pc.
5825 In DWARF-3 draft 4, the base address could be overridden by
5826 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5827 compilation units with discontinuous ranges. */
5828
5829 static void
5830 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5831 {
5832 struct attribute *attr;
5833
5834 cu->base_known = 0;
5835 cu->base_address = 0;
5836
5837 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5838 if (attr != nullptr)
5839 {
5840 cu->base_address = attr->value_as_address ();
5841 cu->base_known = 1;
5842 }
5843 else
5844 {
5845 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5846 if (attr != nullptr)
5847 {
5848 cu->base_address = attr->value_as_address ();
5849 cu->base_known = 1;
5850 }
5851 }
5852 }
5853
5854 /* Helper function that returns the proper abbrev section for
5855 THIS_CU. */
5856
5857 static struct dwarf2_section_info *
5858 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5859 {
5860 struct dwarf2_section_info *abbrev;
5861 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5862
5863 if (this_cu->is_dwz)
5864 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5865 else
5866 abbrev = &dwarf2_per_objfile->abbrev;
5867
5868 return abbrev;
5869 }
5870
5871 /* Fetch the abbreviation table offset from a comp or type unit header. */
5872
5873 static sect_offset
5874 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5875 struct dwarf2_section_info *section,
5876 sect_offset sect_off)
5877 {
5878 bfd *abfd = section->get_bfd_owner ();
5879 const gdb_byte *info_ptr;
5880 unsigned int initial_length_size, offset_size;
5881 uint16_t version;
5882
5883 section->read (dwarf2_per_objfile->objfile);
5884 info_ptr = section->buffer + to_underlying (sect_off);
5885 read_initial_length (abfd, info_ptr, &initial_length_size);
5886 offset_size = initial_length_size == 4 ? 4 : 8;
5887 info_ptr += initial_length_size;
5888
5889 version = read_2_bytes (abfd, info_ptr);
5890 info_ptr += 2;
5891 if (version >= 5)
5892 {
5893 /* Skip unit type and address size. */
5894 info_ptr += 2;
5895 }
5896
5897 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5898 }
5899
5900 /* A partial symtab that is used only for include files. */
5901 struct dwarf2_include_psymtab : public partial_symtab
5902 {
5903 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5904 : partial_symtab (filename, objfile)
5905 {
5906 }
5907
5908 void read_symtab (struct objfile *objfile) override
5909 {
5910 expand_psymtab (objfile);
5911 }
5912
5913 void expand_psymtab (struct objfile *objfile) override
5914 {
5915 if (m_readin)
5916 return;
5917 /* It's an include file, no symbols to read for it.
5918 Everything is in the parent symtab. */
5919 read_dependencies (objfile);
5920 m_readin = true;
5921 }
5922
5923 bool readin_p () const override
5924 {
5925 return m_readin;
5926 }
5927
5928 struct compunit_symtab *get_compunit_symtab () const override
5929 {
5930 return nullptr;
5931 }
5932
5933 private:
5934
5935 bool m_readin = false;
5936 };
5937
5938 /* Allocate a new partial symtab for file named NAME and mark this new
5939 partial symtab as being an include of PST. */
5940
5941 static void
5942 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5943 struct objfile *objfile)
5944 {
5945 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5946
5947 if (!IS_ABSOLUTE_PATH (subpst->filename))
5948 {
5949 /* It shares objfile->objfile_obstack. */
5950 subpst->dirname = pst->dirname;
5951 }
5952
5953 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5954 subpst->dependencies[0] = pst;
5955 subpst->number_of_dependencies = 1;
5956 }
5957
5958 /* Read the Line Number Program data and extract the list of files
5959 included by the source file represented by PST. Build an include
5960 partial symtab for each of these included files. */
5961
5962 static void
5963 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5964 struct die_info *die,
5965 dwarf2_psymtab *pst)
5966 {
5967 line_header_up lh;
5968 struct attribute *attr;
5969
5970 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5971 if (attr != nullptr)
5972 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5973 if (lh == NULL)
5974 return; /* No linetable, so no includes. */
5975
5976 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5977 that we pass in the raw text_low here; that is ok because we're
5978 only decoding the line table to make include partial symtabs, and
5979 so the addresses aren't really used. */
5980 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5981 pst->raw_text_low (), 1);
5982 }
5983
5984 static hashval_t
5985 hash_signatured_type (const void *item)
5986 {
5987 const struct signatured_type *sig_type
5988 = (const struct signatured_type *) item;
5989
5990 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5991 return sig_type->signature;
5992 }
5993
5994 static int
5995 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5996 {
5997 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5998 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5999
6000 return lhs->signature == rhs->signature;
6001 }
6002
6003 /* Allocate a hash table for signatured types. */
6004
6005 static htab_up
6006 allocate_signatured_type_table ()
6007 {
6008 return htab_up (htab_create_alloc (41,
6009 hash_signatured_type,
6010 eq_signatured_type,
6011 NULL, xcalloc, xfree));
6012 }
6013
6014 /* A helper function to add a signatured type CU to a table. */
6015
6016 static int
6017 add_signatured_type_cu_to_table (void **slot, void *datum)
6018 {
6019 struct signatured_type *sigt = (struct signatured_type *) *slot;
6020 std::vector<signatured_type *> *all_type_units
6021 = (std::vector<signatured_type *> *) datum;
6022
6023 all_type_units->push_back (sigt);
6024
6025 return 1;
6026 }
6027
6028 /* A helper for create_debug_types_hash_table. Read types from SECTION
6029 and fill them into TYPES_HTAB. It will process only type units,
6030 therefore DW_UT_type. */
6031
6032 static void
6033 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6034 struct dwo_file *dwo_file,
6035 dwarf2_section_info *section, htab_up &types_htab,
6036 rcuh_kind section_kind)
6037 {
6038 struct objfile *objfile = dwarf2_per_objfile->objfile;
6039 struct dwarf2_section_info *abbrev_section;
6040 bfd *abfd;
6041 const gdb_byte *info_ptr, *end_ptr;
6042
6043 abbrev_section = (dwo_file != NULL
6044 ? &dwo_file->sections.abbrev
6045 : &dwarf2_per_objfile->abbrev);
6046
6047 if (dwarf_read_debug)
6048 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6049 section->get_name (),
6050 abbrev_section->get_file_name ());
6051
6052 section->read (objfile);
6053 info_ptr = section->buffer;
6054
6055 if (info_ptr == NULL)
6056 return;
6057
6058 /* We can't set abfd until now because the section may be empty or
6059 not present, in which case the bfd is unknown. */
6060 abfd = section->get_bfd_owner ();
6061
6062 /* We don't use cutu_reader here because we don't need to read
6063 any dies: the signature is in the header. */
6064
6065 end_ptr = info_ptr + section->size;
6066 while (info_ptr < end_ptr)
6067 {
6068 struct signatured_type *sig_type;
6069 struct dwo_unit *dwo_tu;
6070 void **slot;
6071 const gdb_byte *ptr = info_ptr;
6072 struct comp_unit_head header;
6073 unsigned int length;
6074
6075 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6076
6077 /* Initialize it due to a false compiler warning. */
6078 header.signature = -1;
6079 header.type_cu_offset_in_tu = (cu_offset) -1;
6080
6081 /* We need to read the type's signature in order to build the hash
6082 table, but we don't need anything else just yet. */
6083
6084 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6085 abbrev_section, ptr, section_kind);
6086
6087 length = header.get_length ();
6088
6089 /* Skip dummy type units. */
6090 if (ptr >= info_ptr + length
6091 || peek_abbrev_code (abfd, ptr) == 0
6092 || header.unit_type != DW_UT_type)
6093 {
6094 info_ptr += length;
6095 continue;
6096 }
6097
6098 if (types_htab == NULL)
6099 {
6100 if (dwo_file)
6101 types_htab = allocate_dwo_unit_table ();
6102 else
6103 types_htab = allocate_signatured_type_table ();
6104 }
6105
6106 if (dwo_file)
6107 {
6108 sig_type = NULL;
6109 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6110 struct dwo_unit);
6111 dwo_tu->dwo_file = dwo_file;
6112 dwo_tu->signature = header.signature;
6113 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6114 dwo_tu->section = section;
6115 dwo_tu->sect_off = sect_off;
6116 dwo_tu->length = length;
6117 }
6118 else
6119 {
6120 /* N.B.: type_offset is not usable if this type uses a DWO file.
6121 The real type_offset is in the DWO file. */
6122 dwo_tu = NULL;
6123 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6124 struct signatured_type);
6125 sig_type->signature = header.signature;
6126 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6127 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6128 sig_type->per_cu.is_debug_types = 1;
6129 sig_type->per_cu.section = section;
6130 sig_type->per_cu.sect_off = sect_off;
6131 sig_type->per_cu.length = length;
6132 }
6133
6134 slot = htab_find_slot (types_htab.get (),
6135 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6136 INSERT);
6137 gdb_assert (slot != NULL);
6138 if (*slot != NULL)
6139 {
6140 sect_offset dup_sect_off;
6141
6142 if (dwo_file)
6143 {
6144 const struct dwo_unit *dup_tu
6145 = (const struct dwo_unit *) *slot;
6146
6147 dup_sect_off = dup_tu->sect_off;
6148 }
6149 else
6150 {
6151 const struct signatured_type *dup_tu
6152 = (const struct signatured_type *) *slot;
6153
6154 dup_sect_off = dup_tu->per_cu.sect_off;
6155 }
6156
6157 complaint (_("debug type entry at offset %s is duplicate to"
6158 " the entry at offset %s, signature %s"),
6159 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6160 hex_string (header.signature));
6161 }
6162 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6163
6164 if (dwarf_read_debug > 1)
6165 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6166 sect_offset_str (sect_off),
6167 hex_string (header.signature));
6168
6169 info_ptr += length;
6170 }
6171 }
6172
6173 /* Create the hash table of all entries in the .debug_types
6174 (or .debug_types.dwo) section(s).
6175 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6176 otherwise it is NULL.
6177
6178 The result is a pointer to the hash table or NULL if there are no types.
6179
6180 Note: This function processes DWO files only, not DWP files. */
6181
6182 static void
6183 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6184 struct dwo_file *dwo_file,
6185 gdb::array_view<dwarf2_section_info> type_sections,
6186 htab_up &types_htab)
6187 {
6188 for (dwarf2_section_info &section : type_sections)
6189 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6190 types_htab, rcuh_kind::TYPE);
6191 }
6192
6193 /* Create the hash table of all entries in the .debug_types section,
6194 and initialize all_type_units.
6195 The result is zero if there is an error (e.g. missing .debug_types section),
6196 otherwise non-zero. */
6197
6198 static int
6199 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6200 {
6201 htab_up types_htab;
6202
6203 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6204 &dwarf2_per_objfile->info, types_htab,
6205 rcuh_kind::COMPILE);
6206 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6207 dwarf2_per_objfile->types, types_htab);
6208 if (types_htab == NULL)
6209 {
6210 dwarf2_per_objfile->signatured_types = NULL;
6211 return 0;
6212 }
6213
6214 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6215
6216 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6217 dwarf2_per_objfile->all_type_units.reserve
6218 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6219
6220 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6221 add_signatured_type_cu_to_table,
6222 &dwarf2_per_objfile->all_type_units);
6223
6224 return 1;
6225 }
6226
6227 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6228 If SLOT is non-NULL, it is the entry to use in the hash table.
6229 Otherwise we find one. */
6230
6231 static struct signatured_type *
6232 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6233 void **slot)
6234 {
6235 struct objfile *objfile = dwarf2_per_objfile->objfile;
6236
6237 if (dwarf2_per_objfile->all_type_units.size ()
6238 == dwarf2_per_objfile->all_type_units.capacity ())
6239 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6240
6241 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6242 struct signatured_type);
6243
6244 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6245 sig_type->signature = sig;
6246 sig_type->per_cu.is_debug_types = 1;
6247 if (dwarf2_per_objfile->using_index)
6248 {
6249 sig_type->per_cu.v.quick =
6250 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6251 struct dwarf2_per_cu_quick_data);
6252 }
6253
6254 if (slot == NULL)
6255 {
6256 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6257 sig_type, INSERT);
6258 }
6259 gdb_assert (*slot == NULL);
6260 *slot = sig_type;
6261 /* The rest of sig_type must be filled in by the caller. */
6262 return sig_type;
6263 }
6264
6265 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6266 Fill in SIG_ENTRY with DWO_ENTRY. */
6267
6268 static void
6269 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6270 struct signatured_type *sig_entry,
6271 struct dwo_unit *dwo_entry)
6272 {
6273 /* Make sure we're not clobbering something we don't expect to. */
6274 gdb_assert (! sig_entry->per_cu.queued);
6275 gdb_assert (sig_entry->per_cu.cu == NULL);
6276 if (dwarf2_per_objfile->using_index)
6277 {
6278 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6279 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6280 }
6281 else
6282 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6283 gdb_assert (sig_entry->signature == dwo_entry->signature);
6284 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6285 gdb_assert (sig_entry->type_unit_group == NULL);
6286 gdb_assert (sig_entry->dwo_unit == NULL);
6287
6288 sig_entry->per_cu.section = dwo_entry->section;
6289 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6290 sig_entry->per_cu.length = dwo_entry->length;
6291 sig_entry->per_cu.reading_dwo_directly = 1;
6292 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6293 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6294 sig_entry->dwo_unit = dwo_entry;
6295 }
6296
6297 /* Subroutine of lookup_signatured_type.
6298 If we haven't read the TU yet, create the signatured_type data structure
6299 for a TU to be read in directly from a DWO file, bypassing the stub.
6300 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6301 using .gdb_index, then when reading a CU we want to stay in the DWO file
6302 containing that CU. Otherwise we could end up reading several other DWO
6303 files (due to comdat folding) to process the transitive closure of all the
6304 mentioned TUs, and that can be slow. The current DWO file will have every
6305 type signature that it needs.
6306 We only do this for .gdb_index because in the psymtab case we already have
6307 to read all the DWOs to build the type unit groups. */
6308
6309 static struct signatured_type *
6310 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6311 {
6312 struct dwarf2_per_objfile *dwarf2_per_objfile
6313 = cu->per_cu->dwarf2_per_objfile;
6314 struct dwo_file *dwo_file;
6315 struct dwo_unit find_dwo_entry, *dwo_entry;
6316 struct signatured_type find_sig_entry, *sig_entry;
6317 void **slot;
6318
6319 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6320
6321 /* If TU skeletons have been removed then we may not have read in any
6322 TUs yet. */
6323 if (dwarf2_per_objfile->signatured_types == NULL)
6324 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6325
6326 /* We only ever need to read in one copy of a signatured type.
6327 Use the global signatured_types array to do our own comdat-folding
6328 of types. If this is the first time we're reading this TU, and
6329 the TU has an entry in .gdb_index, replace the recorded data from
6330 .gdb_index with this TU. */
6331
6332 find_sig_entry.signature = sig;
6333 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6334 &find_sig_entry, INSERT);
6335 sig_entry = (struct signatured_type *) *slot;
6336
6337 /* We can get here with the TU already read, *or* in the process of being
6338 read. Don't reassign the global entry to point to this DWO if that's
6339 the case. Also note that if the TU is already being read, it may not
6340 have come from a DWO, the program may be a mix of Fission-compiled
6341 code and non-Fission-compiled code. */
6342
6343 /* Have we already tried to read this TU?
6344 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6345 needn't exist in the global table yet). */
6346 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6347 return sig_entry;
6348
6349 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6350 dwo_unit of the TU itself. */
6351 dwo_file = cu->dwo_unit->dwo_file;
6352
6353 /* Ok, this is the first time we're reading this TU. */
6354 if (dwo_file->tus == NULL)
6355 return NULL;
6356 find_dwo_entry.signature = sig;
6357 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6358 &find_dwo_entry);
6359 if (dwo_entry == NULL)
6360 return NULL;
6361
6362 /* If the global table doesn't have an entry for this TU, add one. */
6363 if (sig_entry == NULL)
6364 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6365
6366 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6367 sig_entry->per_cu.tu_read = 1;
6368 return sig_entry;
6369 }
6370
6371 /* Subroutine of lookup_signatured_type.
6372 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6373 then try the DWP file. If the TU stub (skeleton) has been removed then
6374 it won't be in .gdb_index. */
6375
6376 static struct signatured_type *
6377 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6378 {
6379 struct dwarf2_per_objfile *dwarf2_per_objfile
6380 = cu->per_cu->dwarf2_per_objfile;
6381 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6382 struct dwo_unit *dwo_entry;
6383 struct signatured_type find_sig_entry, *sig_entry;
6384 void **slot;
6385
6386 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6387 gdb_assert (dwp_file != NULL);
6388
6389 /* If TU skeletons have been removed then we may not have read in any
6390 TUs yet. */
6391 if (dwarf2_per_objfile->signatured_types == NULL)
6392 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6393
6394 find_sig_entry.signature = sig;
6395 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6396 &find_sig_entry, INSERT);
6397 sig_entry = (struct signatured_type *) *slot;
6398
6399 /* Have we already tried to read this TU?
6400 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6401 needn't exist in the global table yet). */
6402 if (sig_entry != NULL)
6403 return sig_entry;
6404
6405 if (dwp_file->tus == NULL)
6406 return NULL;
6407 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6408 sig, 1 /* is_debug_types */);
6409 if (dwo_entry == NULL)
6410 return NULL;
6411
6412 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6413 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6414
6415 return sig_entry;
6416 }
6417
6418 /* Lookup a signature based type for DW_FORM_ref_sig8.
6419 Returns NULL if signature SIG is not present in the table.
6420 It is up to the caller to complain about this. */
6421
6422 static struct signatured_type *
6423 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6424 {
6425 struct dwarf2_per_objfile *dwarf2_per_objfile
6426 = cu->per_cu->dwarf2_per_objfile;
6427
6428 if (cu->dwo_unit
6429 && dwarf2_per_objfile->using_index)
6430 {
6431 /* We're in a DWO/DWP file, and we're using .gdb_index.
6432 These cases require special processing. */
6433 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6434 return lookup_dwo_signatured_type (cu, sig);
6435 else
6436 return lookup_dwp_signatured_type (cu, sig);
6437 }
6438 else
6439 {
6440 struct signatured_type find_entry, *entry;
6441
6442 if (dwarf2_per_objfile->signatured_types == NULL)
6443 return NULL;
6444 find_entry.signature = sig;
6445 entry = ((struct signatured_type *)
6446 htab_find (dwarf2_per_objfile->signatured_types.get (),
6447 &find_entry));
6448 return entry;
6449 }
6450 }
6451
6452 /* Return the address base of the compile unit, which, if exists, is stored
6453 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6454 static gdb::optional<ULONGEST>
6455 lookup_addr_base (struct die_info *comp_unit_die)
6456 {
6457 struct attribute *attr;
6458 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6459 if (attr == nullptr)
6460 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6461 if (attr == nullptr)
6462 return gdb::optional<ULONGEST> ();
6463 return DW_UNSND (attr);
6464 }
6465
6466 /* Return range lists base of the compile unit, which, if exists, is stored
6467 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6468 static ULONGEST
6469 lookup_ranges_base (struct die_info *comp_unit_die)
6470 {
6471 struct attribute *attr;
6472 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6473 if (attr == nullptr)
6474 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6475 if (attr == nullptr)
6476 return 0;
6477 return DW_UNSND (attr);
6478 }
6479
6480 /* Low level DIE reading support. */
6481
6482 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6483
6484 static void
6485 init_cu_die_reader (struct die_reader_specs *reader,
6486 struct dwarf2_cu *cu,
6487 struct dwarf2_section_info *section,
6488 struct dwo_file *dwo_file,
6489 struct abbrev_table *abbrev_table)
6490 {
6491 gdb_assert (section->readin && section->buffer != NULL);
6492 reader->abfd = section->get_bfd_owner ();
6493 reader->cu = cu;
6494 reader->dwo_file = dwo_file;
6495 reader->die_section = section;
6496 reader->buffer = section->buffer;
6497 reader->buffer_end = section->buffer + section->size;
6498 reader->abbrev_table = abbrev_table;
6499 }
6500
6501 /* Subroutine of cutu_reader to simplify it.
6502 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6503 There's just a lot of work to do, and cutu_reader is big enough
6504 already.
6505
6506 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6507 from it to the DIE in the DWO. If NULL we are skipping the stub.
6508 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6509 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6510 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6511 STUB_COMP_DIR may be non-NULL.
6512 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6513 are filled in with the info of the DIE from the DWO file.
6514 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6515 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6516 kept around for at least as long as *RESULT_READER.
6517
6518 The result is non-zero if a valid (non-dummy) DIE was found. */
6519
6520 static int
6521 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6522 struct dwo_unit *dwo_unit,
6523 struct die_info *stub_comp_unit_die,
6524 const char *stub_comp_dir,
6525 struct die_reader_specs *result_reader,
6526 const gdb_byte **result_info_ptr,
6527 struct die_info **result_comp_unit_die,
6528 abbrev_table_up *result_dwo_abbrev_table)
6529 {
6530 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6531 struct objfile *objfile = dwarf2_per_objfile->objfile;
6532 struct dwarf2_cu *cu = this_cu->cu;
6533 bfd *abfd;
6534 const gdb_byte *begin_info_ptr, *info_ptr;
6535 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6536 int i,num_extra_attrs;
6537 struct dwarf2_section_info *dwo_abbrev_section;
6538 struct die_info *comp_unit_die;
6539
6540 /* At most one of these may be provided. */
6541 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6542
6543 /* These attributes aren't processed until later:
6544 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6545 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6546 referenced later. However, these attributes are found in the stub
6547 which we won't have later. In order to not impose this complication
6548 on the rest of the code, we read them here and copy them to the
6549 DWO CU/TU die. */
6550
6551 stmt_list = NULL;
6552 low_pc = NULL;
6553 high_pc = NULL;
6554 ranges = NULL;
6555 comp_dir = NULL;
6556
6557 if (stub_comp_unit_die != NULL)
6558 {
6559 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6560 DWO file. */
6561 if (! this_cu->is_debug_types)
6562 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6563 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6564 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6565 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6566 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6567
6568 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6569
6570 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6571 here (if needed). We need the value before we can process
6572 DW_AT_ranges. */
6573 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6574 }
6575 else if (stub_comp_dir != NULL)
6576 {
6577 /* Reconstruct the comp_dir attribute to simplify the code below. */
6578 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6579 comp_dir->name = DW_AT_comp_dir;
6580 comp_dir->form = DW_FORM_string;
6581 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6582 DW_STRING (comp_dir) = stub_comp_dir;
6583 }
6584
6585 /* Set up for reading the DWO CU/TU. */
6586 cu->dwo_unit = dwo_unit;
6587 dwarf2_section_info *section = dwo_unit->section;
6588 section->read (objfile);
6589 abfd = section->get_bfd_owner ();
6590 begin_info_ptr = info_ptr = (section->buffer
6591 + to_underlying (dwo_unit->sect_off));
6592 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6593
6594 if (this_cu->is_debug_types)
6595 {
6596 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6597
6598 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6599 &cu->header, section,
6600 dwo_abbrev_section,
6601 info_ptr, rcuh_kind::TYPE);
6602 /* This is not an assert because it can be caused by bad debug info. */
6603 if (sig_type->signature != cu->header.signature)
6604 {
6605 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6606 " TU at offset %s [in module %s]"),
6607 hex_string (sig_type->signature),
6608 hex_string (cu->header.signature),
6609 sect_offset_str (dwo_unit->sect_off),
6610 bfd_get_filename (abfd));
6611 }
6612 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6613 /* For DWOs coming from DWP files, we don't know the CU length
6614 nor the type's offset in the TU until now. */
6615 dwo_unit->length = cu->header.get_length ();
6616 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6617
6618 /* Establish the type offset that can be used to lookup the type.
6619 For DWO files, we don't know it until now. */
6620 sig_type->type_offset_in_section
6621 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6622 }
6623 else
6624 {
6625 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6626 &cu->header, section,
6627 dwo_abbrev_section,
6628 info_ptr, rcuh_kind::COMPILE);
6629 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6630 /* For DWOs coming from DWP files, we don't know the CU length
6631 until now. */
6632 dwo_unit->length = cu->header.get_length ();
6633 }
6634
6635 *result_dwo_abbrev_table
6636 = abbrev_table::read (objfile, dwo_abbrev_section,
6637 cu->header.abbrev_sect_off);
6638 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6639 result_dwo_abbrev_table->get ());
6640
6641 /* Read in the die, but leave space to copy over the attributes
6642 from the stub. This has the benefit of simplifying the rest of
6643 the code - all the work to maintain the illusion of a single
6644 DW_TAG_{compile,type}_unit DIE is done here. */
6645 num_extra_attrs = ((stmt_list != NULL)
6646 + (low_pc != NULL)
6647 + (high_pc != NULL)
6648 + (ranges != NULL)
6649 + (comp_dir != NULL));
6650 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6651 num_extra_attrs);
6652
6653 /* Copy over the attributes from the stub to the DIE we just read in. */
6654 comp_unit_die = *result_comp_unit_die;
6655 i = comp_unit_die->num_attrs;
6656 if (stmt_list != NULL)
6657 comp_unit_die->attrs[i++] = *stmt_list;
6658 if (low_pc != NULL)
6659 comp_unit_die->attrs[i++] = *low_pc;
6660 if (high_pc != NULL)
6661 comp_unit_die->attrs[i++] = *high_pc;
6662 if (ranges != NULL)
6663 comp_unit_die->attrs[i++] = *ranges;
6664 if (comp_dir != NULL)
6665 comp_unit_die->attrs[i++] = *comp_dir;
6666 comp_unit_die->num_attrs += num_extra_attrs;
6667
6668 if (dwarf_die_debug)
6669 {
6670 fprintf_unfiltered (gdb_stdlog,
6671 "Read die from %s@0x%x of %s:\n",
6672 section->get_name (),
6673 (unsigned) (begin_info_ptr - section->buffer),
6674 bfd_get_filename (abfd));
6675 dump_die (comp_unit_die, dwarf_die_debug);
6676 }
6677
6678 /* Skip dummy compilation units. */
6679 if (info_ptr >= begin_info_ptr + dwo_unit->length
6680 || peek_abbrev_code (abfd, info_ptr) == 0)
6681 return 0;
6682
6683 *result_info_ptr = info_ptr;
6684 return 1;
6685 }
6686
6687 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6688 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6689 signature is part of the header. */
6690 static gdb::optional<ULONGEST>
6691 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6692 {
6693 if (cu->header.version >= 5)
6694 return cu->header.signature;
6695 struct attribute *attr;
6696 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6697 if (attr == nullptr)
6698 return gdb::optional<ULONGEST> ();
6699 return DW_UNSND (attr);
6700 }
6701
6702 /* Subroutine of cutu_reader to simplify it.
6703 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6704 Returns NULL if the specified DWO unit cannot be found. */
6705
6706 static struct dwo_unit *
6707 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6708 struct die_info *comp_unit_die,
6709 const char *dwo_name)
6710 {
6711 struct dwarf2_cu *cu = this_cu->cu;
6712 struct dwo_unit *dwo_unit;
6713 const char *comp_dir;
6714
6715 gdb_assert (cu != NULL);
6716
6717 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6718 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6719 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6720
6721 if (this_cu->is_debug_types)
6722 {
6723 struct signatured_type *sig_type;
6724
6725 /* Since this_cu is the first member of struct signatured_type,
6726 we can go from a pointer to one to a pointer to the other. */
6727 sig_type = (struct signatured_type *) this_cu;
6728 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6729 }
6730 else
6731 {
6732 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6733 if (!signature.has_value ())
6734 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6735 " [in module %s]"),
6736 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6737 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6738 *signature);
6739 }
6740
6741 return dwo_unit;
6742 }
6743
6744 /* Subroutine of cutu_reader to simplify it.
6745 See it for a description of the parameters.
6746 Read a TU directly from a DWO file, bypassing the stub. */
6747
6748 void
6749 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6750 int use_existing_cu)
6751 {
6752 struct signatured_type *sig_type;
6753
6754 /* Verify we can do the following downcast, and that we have the
6755 data we need. */
6756 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6757 sig_type = (struct signatured_type *) this_cu;
6758 gdb_assert (sig_type->dwo_unit != NULL);
6759
6760 if (use_existing_cu && this_cu->cu != NULL)
6761 {
6762 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6763 /* There's no need to do the rereading_dwo_cu handling that
6764 cutu_reader does since we don't read the stub. */
6765 }
6766 else
6767 {
6768 /* If !use_existing_cu, this_cu->cu must be NULL. */
6769 gdb_assert (this_cu->cu == NULL);
6770 m_new_cu.reset (new dwarf2_cu (this_cu));
6771 }
6772
6773 /* A future optimization, if needed, would be to use an existing
6774 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6775 could share abbrev tables. */
6776
6777 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6778 NULL /* stub_comp_unit_die */,
6779 sig_type->dwo_unit->dwo_file->comp_dir,
6780 this, &info_ptr,
6781 &comp_unit_die,
6782 &m_dwo_abbrev_table) == 0)
6783 {
6784 /* Dummy die. */
6785 dummy_p = true;
6786 }
6787 }
6788
6789 /* Initialize a CU (or TU) and read its DIEs.
6790 If the CU defers to a DWO file, read the DWO file as well.
6791
6792 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6793 Otherwise the table specified in the comp unit header is read in and used.
6794 This is an optimization for when we already have the abbrev table.
6795
6796 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6797 Otherwise, a new CU is allocated with xmalloc. */
6798
6799 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6800 struct abbrev_table *abbrev_table,
6801 int use_existing_cu,
6802 bool skip_partial)
6803 : die_reader_specs {},
6804 m_this_cu (this_cu)
6805 {
6806 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6807 struct objfile *objfile = dwarf2_per_objfile->objfile;
6808 struct dwarf2_section_info *section = this_cu->section;
6809 bfd *abfd = section->get_bfd_owner ();
6810 struct dwarf2_cu *cu;
6811 const gdb_byte *begin_info_ptr;
6812 struct signatured_type *sig_type = NULL;
6813 struct dwarf2_section_info *abbrev_section;
6814 /* Non-zero if CU currently points to a DWO file and we need to
6815 reread it. When this happens we need to reread the skeleton die
6816 before we can reread the DWO file (this only applies to CUs, not TUs). */
6817 int rereading_dwo_cu = 0;
6818
6819 if (dwarf_die_debug)
6820 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6821 this_cu->is_debug_types ? "type" : "comp",
6822 sect_offset_str (this_cu->sect_off));
6823
6824 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6825 file (instead of going through the stub), short-circuit all of this. */
6826 if (this_cu->reading_dwo_directly)
6827 {
6828 /* Narrow down the scope of possibilities to have to understand. */
6829 gdb_assert (this_cu->is_debug_types);
6830 gdb_assert (abbrev_table == NULL);
6831 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6832 return;
6833 }
6834
6835 /* This is cheap if the section is already read in. */
6836 section->read (objfile);
6837
6838 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6839
6840 abbrev_section = get_abbrev_section_for_cu (this_cu);
6841
6842 if (use_existing_cu && this_cu->cu != NULL)
6843 {
6844 cu = this_cu->cu;
6845 /* If this CU is from a DWO file we need to start over, we need to
6846 refetch the attributes from the skeleton CU.
6847 This could be optimized by retrieving those attributes from when we
6848 were here the first time: the previous comp_unit_die was stored in
6849 comp_unit_obstack. But there's no data yet that we need this
6850 optimization. */
6851 if (cu->dwo_unit != NULL)
6852 rereading_dwo_cu = 1;
6853 }
6854 else
6855 {
6856 /* If !use_existing_cu, this_cu->cu must be NULL. */
6857 gdb_assert (this_cu->cu == NULL);
6858 m_new_cu.reset (new dwarf2_cu (this_cu));
6859 cu = m_new_cu.get ();
6860 }
6861
6862 /* Get the header. */
6863 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6864 {
6865 /* We already have the header, there's no need to read it in again. */
6866 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6867 }
6868 else
6869 {
6870 if (this_cu->is_debug_types)
6871 {
6872 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6873 &cu->header, section,
6874 abbrev_section, info_ptr,
6875 rcuh_kind::TYPE);
6876
6877 /* Since per_cu is the first member of struct signatured_type,
6878 we can go from a pointer to one to a pointer to the other. */
6879 sig_type = (struct signatured_type *) this_cu;
6880 gdb_assert (sig_type->signature == cu->header.signature);
6881 gdb_assert (sig_type->type_offset_in_tu
6882 == cu->header.type_cu_offset_in_tu);
6883 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6884
6885 /* LENGTH has not been set yet for type units if we're
6886 using .gdb_index. */
6887 this_cu->length = cu->header.get_length ();
6888
6889 /* Establish the type offset that can be used to lookup the type. */
6890 sig_type->type_offset_in_section =
6891 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6892
6893 this_cu->dwarf_version = cu->header.version;
6894 }
6895 else
6896 {
6897 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6898 &cu->header, section,
6899 abbrev_section,
6900 info_ptr,
6901 rcuh_kind::COMPILE);
6902
6903 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6904 gdb_assert (this_cu->length == cu->header.get_length ());
6905 this_cu->dwarf_version = cu->header.version;
6906 }
6907 }
6908
6909 /* Skip dummy compilation units. */
6910 if (info_ptr >= begin_info_ptr + this_cu->length
6911 || peek_abbrev_code (abfd, info_ptr) == 0)
6912 {
6913 dummy_p = true;
6914 return;
6915 }
6916
6917 /* If we don't have them yet, read the abbrevs for this compilation unit.
6918 And if we need to read them now, make sure they're freed when we're
6919 done. */
6920 if (abbrev_table != NULL)
6921 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6922 else
6923 {
6924 m_abbrev_table_holder
6925 = abbrev_table::read (objfile, abbrev_section,
6926 cu->header.abbrev_sect_off);
6927 abbrev_table = m_abbrev_table_holder.get ();
6928 }
6929
6930 /* Read the top level CU/TU die. */
6931 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6932 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6933
6934 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6935 {
6936 dummy_p = true;
6937 return;
6938 }
6939
6940 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6941 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6942 table from the DWO file and pass the ownership over to us. It will be
6943 referenced from READER, so we must make sure to free it after we're done
6944 with READER.
6945
6946 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6947 DWO CU, that this test will fail (the attribute will not be present). */
6948 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6949 if (dwo_name != nullptr)
6950 {
6951 struct dwo_unit *dwo_unit;
6952 struct die_info *dwo_comp_unit_die;
6953
6954 if (comp_unit_die->has_children)
6955 {
6956 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6957 " has children (offset %s) [in module %s]"),
6958 sect_offset_str (this_cu->sect_off),
6959 bfd_get_filename (abfd));
6960 }
6961 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6962 if (dwo_unit != NULL)
6963 {
6964 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6965 comp_unit_die, NULL,
6966 this, &info_ptr,
6967 &dwo_comp_unit_die,
6968 &m_dwo_abbrev_table) == 0)
6969 {
6970 /* Dummy die. */
6971 dummy_p = true;
6972 return;
6973 }
6974 comp_unit_die = dwo_comp_unit_die;
6975 }
6976 else
6977 {
6978 /* Yikes, we couldn't find the rest of the DIE, we only have
6979 the stub. A complaint has already been logged. There's
6980 not much more we can do except pass on the stub DIE to
6981 die_reader_func. We don't want to throw an error on bad
6982 debug info. */
6983 }
6984 }
6985 }
6986
6987 void
6988 cutu_reader::keep ()
6989 {
6990 /* Done, clean up. */
6991 gdb_assert (!dummy_p);
6992 if (m_new_cu != NULL)
6993 {
6994 struct dwarf2_per_objfile *dwarf2_per_objfile
6995 = m_this_cu->dwarf2_per_objfile;
6996 /* Link this CU into read_in_chain. */
6997 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6998 dwarf2_per_objfile->read_in_chain = m_this_cu;
6999 /* The chain owns it now. */
7000 m_new_cu.release ();
7001 }
7002 }
7003
7004 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7005 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7006 assumed to have already done the lookup to find the DWO file).
7007
7008 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7009 THIS_CU->is_debug_types, but nothing else.
7010
7011 We fill in THIS_CU->length.
7012
7013 THIS_CU->cu is always freed when done.
7014 This is done in order to not leave THIS_CU->cu in a state where we have
7015 to care whether it refers to the "main" CU or the DWO CU.
7016
7017 When parent_cu is passed, it is used to provide a default value for
7018 str_offsets_base and addr_base from the parent. */
7019
7020 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7021 struct dwarf2_cu *parent_cu,
7022 struct dwo_file *dwo_file)
7023 : die_reader_specs {},
7024 m_this_cu (this_cu)
7025 {
7026 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7027 struct objfile *objfile = dwarf2_per_objfile->objfile;
7028 struct dwarf2_section_info *section = this_cu->section;
7029 bfd *abfd = section->get_bfd_owner ();
7030 struct dwarf2_section_info *abbrev_section;
7031 const gdb_byte *begin_info_ptr, *info_ptr;
7032
7033 if (dwarf_die_debug)
7034 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7035 this_cu->is_debug_types ? "type" : "comp",
7036 sect_offset_str (this_cu->sect_off));
7037
7038 gdb_assert (this_cu->cu == NULL);
7039
7040 abbrev_section = (dwo_file != NULL
7041 ? &dwo_file->sections.abbrev
7042 : get_abbrev_section_for_cu (this_cu));
7043
7044 /* This is cheap if the section is already read in. */
7045 section->read (objfile);
7046
7047 m_new_cu.reset (new dwarf2_cu (this_cu));
7048
7049 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7050 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7051 &m_new_cu->header, section,
7052 abbrev_section, info_ptr,
7053 (this_cu->is_debug_types
7054 ? rcuh_kind::TYPE
7055 : rcuh_kind::COMPILE));
7056
7057 if (parent_cu != nullptr)
7058 {
7059 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7060 m_new_cu->addr_base = parent_cu->addr_base;
7061 }
7062 this_cu->length = m_new_cu->header.get_length ();
7063
7064 /* Skip dummy compilation units. */
7065 if (info_ptr >= begin_info_ptr + this_cu->length
7066 || peek_abbrev_code (abfd, info_ptr) == 0)
7067 {
7068 dummy_p = true;
7069 return;
7070 }
7071
7072 m_abbrev_table_holder
7073 = abbrev_table::read (objfile, abbrev_section,
7074 m_new_cu->header.abbrev_sect_off);
7075
7076 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7077 m_abbrev_table_holder.get ());
7078 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7079 }
7080
7081 \f
7082 /* Type Unit Groups.
7083
7084 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7085 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7086 so that all types coming from the same compilation (.o file) are grouped
7087 together. A future step could be to put the types in the same symtab as
7088 the CU the types ultimately came from. */
7089
7090 static hashval_t
7091 hash_type_unit_group (const void *item)
7092 {
7093 const struct type_unit_group *tu_group
7094 = (const struct type_unit_group *) item;
7095
7096 return hash_stmt_list_entry (&tu_group->hash);
7097 }
7098
7099 static int
7100 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7101 {
7102 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7103 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7104
7105 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7106 }
7107
7108 /* Allocate a hash table for type unit groups. */
7109
7110 static htab_up
7111 allocate_type_unit_groups_table ()
7112 {
7113 return htab_up (htab_create_alloc (3,
7114 hash_type_unit_group,
7115 eq_type_unit_group,
7116 NULL, xcalloc, xfree));
7117 }
7118
7119 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7120 partial symtabs. We combine several TUs per psymtab to not let the size
7121 of any one psymtab grow too big. */
7122 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7123 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7124
7125 /* Helper routine for get_type_unit_group.
7126 Create the type_unit_group object used to hold one or more TUs. */
7127
7128 static struct type_unit_group *
7129 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7130 {
7131 struct dwarf2_per_objfile *dwarf2_per_objfile
7132 = cu->per_cu->dwarf2_per_objfile;
7133 struct objfile *objfile = dwarf2_per_objfile->objfile;
7134 struct dwarf2_per_cu_data *per_cu;
7135 struct type_unit_group *tu_group;
7136
7137 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7138 struct type_unit_group);
7139 per_cu = &tu_group->per_cu;
7140 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7141
7142 if (dwarf2_per_objfile->using_index)
7143 {
7144 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7145 struct dwarf2_per_cu_quick_data);
7146 }
7147 else
7148 {
7149 unsigned int line_offset = to_underlying (line_offset_struct);
7150 dwarf2_psymtab *pst;
7151 std::string name;
7152
7153 /* Give the symtab a useful name for debug purposes. */
7154 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7155 name = string_printf ("<type_units_%d>",
7156 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7157 else
7158 name = string_printf ("<type_units_at_0x%x>", line_offset);
7159
7160 pst = create_partial_symtab (per_cu, name.c_str ());
7161 pst->anonymous = true;
7162 }
7163
7164 tu_group->hash.dwo_unit = cu->dwo_unit;
7165 tu_group->hash.line_sect_off = line_offset_struct;
7166
7167 return tu_group;
7168 }
7169
7170 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7171 STMT_LIST is a DW_AT_stmt_list attribute. */
7172
7173 static struct type_unit_group *
7174 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7175 {
7176 struct dwarf2_per_objfile *dwarf2_per_objfile
7177 = cu->per_cu->dwarf2_per_objfile;
7178 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7179 struct type_unit_group *tu_group;
7180 void **slot;
7181 unsigned int line_offset;
7182 struct type_unit_group type_unit_group_for_lookup;
7183
7184 if (dwarf2_per_objfile->type_unit_groups == NULL)
7185 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7186
7187 /* Do we need to create a new group, or can we use an existing one? */
7188
7189 if (stmt_list)
7190 {
7191 line_offset = DW_UNSND (stmt_list);
7192 ++tu_stats->nr_symtab_sharers;
7193 }
7194 else
7195 {
7196 /* Ugh, no stmt_list. Rare, but we have to handle it.
7197 We can do various things here like create one group per TU or
7198 spread them over multiple groups to split up the expansion work.
7199 To avoid worst case scenarios (too many groups or too large groups)
7200 we, umm, group them in bunches. */
7201 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7202 | (tu_stats->nr_stmt_less_type_units
7203 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7204 ++tu_stats->nr_stmt_less_type_units;
7205 }
7206
7207 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7208 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7209 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7210 &type_unit_group_for_lookup, INSERT);
7211 if (*slot != NULL)
7212 {
7213 tu_group = (struct type_unit_group *) *slot;
7214 gdb_assert (tu_group != NULL);
7215 }
7216 else
7217 {
7218 sect_offset line_offset_struct = (sect_offset) line_offset;
7219 tu_group = create_type_unit_group (cu, line_offset_struct);
7220 *slot = tu_group;
7221 ++tu_stats->nr_symtabs;
7222 }
7223
7224 return tu_group;
7225 }
7226 \f
7227 /* Partial symbol tables. */
7228
7229 /* Create a psymtab named NAME and assign it to PER_CU.
7230
7231 The caller must fill in the following details:
7232 dirname, textlow, texthigh. */
7233
7234 static dwarf2_psymtab *
7235 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7236 {
7237 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7238 dwarf2_psymtab *pst;
7239
7240 pst = new dwarf2_psymtab (name, objfile, 0);
7241
7242 pst->psymtabs_addrmap_supported = true;
7243
7244 /* This is the glue that links PST into GDB's symbol API. */
7245 pst->per_cu_data = per_cu;
7246 per_cu->v.psymtab = pst;
7247
7248 return pst;
7249 }
7250
7251 /* DIE reader function for process_psymtab_comp_unit. */
7252
7253 static void
7254 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7255 const gdb_byte *info_ptr,
7256 struct die_info *comp_unit_die,
7257 enum language pretend_language)
7258 {
7259 struct dwarf2_cu *cu = reader->cu;
7260 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7262 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7263 CORE_ADDR baseaddr;
7264 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7265 dwarf2_psymtab *pst;
7266 enum pc_bounds_kind cu_bounds_kind;
7267 const char *filename;
7268
7269 gdb_assert (! per_cu->is_debug_types);
7270
7271 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7272
7273 /* Allocate a new partial symbol table structure. */
7274 gdb::unique_xmalloc_ptr<char> debug_filename;
7275 static const char artificial[] = "<artificial>";
7276 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7277 if (filename == NULL)
7278 filename = "";
7279 else if (strcmp (filename, artificial) == 0)
7280 {
7281 debug_filename.reset (concat (artificial, "@",
7282 sect_offset_str (per_cu->sect_off),
7283 (char *) NULL));
7284 filename = debug_filename.get ();
7285 }
7286
7287 pst = create_partial_symtab (per_cu, filename);
7288
7289 /* This must be done before calling dwarf2_build_include_psymtabs. */
7290 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7291
7292 baseaddr = objfile->text_section_offset ();
7293
7294 dwarf2_find_base_address (comp_unit_die, cu);
7295
7296 /* Possibly set the default values of LOWPC and HIGHPC from
7297 `DW_AT_ranges'. */
7298 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7299 &best_highpc, cu, pst);
7300 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7301 {
7302 CORE_ADDR low
7303 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7304 - baseaddr);
7305 CORE_ADDR high
7306 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7307 - baseaddr - 1);
7308 /* Store the contiguous range if it is not empty; it can be
7309 empty for CUs with no code. */
7310 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7311 low, high, pst);
7312 }
7313
7314 /* Check if comp unit has_children.
7315 If so, read the rest of the partial symbols from this comp unit.
7316 If not, there's no more debug_info for this comp unit. */
7317 if (comp_unit_die->has_children)
7318 {
7319 struct partial_die_info *first_die;
7320 CORE_ADDR lowpc, highpc;
7321
7322 lowpc = ((CORE_ADDR) -1);
7323 highpc = ((CORE_ADDR) 0);
7324
7325 first_die = load_partial_dies (reader, info_ptr, 1);
7326
7327 scan_partial_symbols (first_die, &lowpc, &highpc,
7328 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7329
7330 /* If we didn't find a lowpc, set it to highpc to avoid
7331 complaints from `maint check'. */
7332 if (lowpc == ((CORE_ADDR) -1))
7333 lowpc = highpc;
7334
7335 /* If the compilation unit didn't have an explicit address range,
7336 then use the information extracted from its child dies. */
7337 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7338 {
7339 best_lowpc = lowpc;
7340 best_highpc = highpc;
7341 }
7342 }
7343 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7344 best_lowpc + baseaddr)
7345 - baseaddr);
7346 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7347 best_highpc + baseaddr)
7348 - baseaddr);
7349
7350 end_psymtab_common (objfile, pst);
7351
7352 if (!cu->per_cu->imported_symtabs_empty ())
7353 {
7354 int i;
7355 int len = cu->per_cu->imported_symtabs_size ();
7356
7357 /* Fill in 'dependencies' here; we fill in 'users' in a
7358 post-pass. */
7359 pst->number_of_dependencies = len;
7360 pst->dependencies
7361 = objfile->partial_symtabs->allocate_dependencies (len);
7362 for (i = 0; i < len; ++i)
7363 {
7364 pst->dependencies[i]
7365 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7366 }
7367
7368 cu->per_cu->imported_symtabs_free ();
7369 }
7370
7371 /* Get the list of files included in the current compilation unit,
7372 and build a psymtab for each of them. */
7373 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7374
7375 if (dwarf_read_debug)
7376 fprintf_unfiltered (gdb_stdlog,
7377 "Psymtab for %s unit @%s: %s - %s"
7378 ", %d global, %d static syms\n",
7379 per_cu->is_debug_types ? "type" : "comp",
7380 sect_offset_str (per_cu->sect_off),
7381 paddress (gdbarch, pst->text_low (objfile)),
7382 paddress (gdbarch, pst->text_high (objfile)),
7383 pst->n_global_syms, pst->n_static_syms);
7384 }
7385
7386 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7387 Process compilation unit THIS_CU for a psymtab. */
7388
7389 static void
7390 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7391 bool want_partial_unit,
7392 enum language pretend_language)
7393 {
7394 /* If this compilation unit was already read in, free the
7395 cached copy in order to read it in again. This is
7396 necessary because we skipped some symbols when we first
7397 read in the compilation unit (see load_partial_dies).
7398 This problem could be avoided, but the benefit is unclear. */
7399 if (this_cu->cu != NULL)
7400 free_one_cached_comp_unit (this_cu);
7401
7402 cutu_reader reader (this_cu, NULL, 0, false);
7403
7404 switch (reader.comp_unit_die->tag)
7405 {
7406 case DW_TAG_compile_unit:
7407 this_cu->unit_type = DW_UT_compile;
7408 break;
7409 case DW_TAG_partial_unit:
7410 this_cu->unit_type = DW_UT_partial;
7411 break;
7412 default:
7413 abort ();
7414 }
7415
7416 if (reader.dummy_p)
7417 {
7418 /* Nothing. */
7419 }
7420 else if (this_cu->is_debug_types)
7421 build_type_psymtabs_reader (&reader, reader.info_ptr,
7422 reader.comp_unit_die);
7423 else if (want_partial_unit
7424 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7425 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7426 reader.comp_unit_die,
7427 pretend_language);
7428
7429 this_cu->lang = this_cu->cu->language;
7430
7431 /* Age out any secondary CUs. */
7432 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7433 }
7434
7435 /* Reader function for build_type_psymtabs. */
7436
7437 static void
7438 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7439 const gdb_byte *info_ptr,
7440 struct die_info *type_unit_die)
7441 {
7442 struct dwarf2_per_objfile *dwarf2_per_objfile
7443 = reader->cu->per_cu->dwarf2_per_objfile;
7444 struct objfile *objfile = dwarf2_per_objfile->objfile;
7445 struct dwarf2_cu *cu = reader->cu;
7446 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7447 struct signatured_type *sig_type;
7448 struct type_unit_group *tu_group;
7449 struct attribute *attr;
7450 struct partial_die_info *first_die;
7451 CORE_ADDR lowpc, highpc;
7452 dwarf2_psymtab *pst;
7453
7454 gdb_assert (per_cu->is_debug_types);
7455 sig_type = (struct signatured_type *) per_cu;
7456
7457 if (! type_unit_die->has_children)
7458 return;
7459
7460 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7461 tu_group = get_type_unit_group (cu, attr);
7462
7463 if (tu_group->tus == nullptr)
7464 tu_group->tus = new std::vector<signatured_type *>;
7465 tu_group->tus->push_back (sig_type);
7466
7467 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7468 pst = create_partial_symtab (per_cu, "");
7469 pst->anonymous = true;
7470
7471 first_die = load_partial_dies (reader, info_ptr, 1);
7472
7473 lowpc = (CORE_ADDR) -1;
7474 highpc = (CORE_ADDR) 0;
7475 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7476
7477 end_psymtab_common (objfile, pst);
7478 }
7479
7480 /* Struct used to sort TUs by their abbreviation table offset. */
7481
7482 struct tu_abbrev_offset
7483 {
7484 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7485 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7486 {}
7487
7488 signatured_type *sig_type;
7489 sect_offset abbrev_offset;
7490 };
7491
7492 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7493
7494 static bool
7495 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7496 const struct tu_abbrev_offset &b)
7497 {
7498 return a.abbrev_offset < b.abbrev_offset;
7499 }
7500
7501 /* Efficiently read all the type units.
7502 This does the bulk of the work for build_type_psymtabs.
7503
7504 The efficiency is because we sort TUs by the abbrev table they use and
7505 only read each abbrev table once. In one program there are 200K TUs
7506 sharing 8K abbrev tables.
7507
7508 The main purpose of this function is to support building the
7509 dwarf2_per_objfile->type_unit_groups table.
7510 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7511 can collapse the search space by grouping them by stmt_list.
7512 The savings can be significant, in the same program from above the 200K TUs
7513 share 8K stmt_list tables.
7514
7515 FUNC is expected to call get_type_unit_group, which will create the
7516 struct type_unit_group if necessary and add it to
7517 dwarf2_per_objfile->type_unit_groups. */
7518
7519 static void
7520 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7521 {
7522 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7523 abbrev_table_up abbrev_table;
7524 sect_offset abbrev_offset;
7525
7526 /* It's up to the caller to not call us multiple times. */
7527 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7528
7529 if (dwarf2_per_objfile->all_type_units.empty ())
7530 return;
7531
7532 /* TUs typically share abbrev tables, and there can be way more TUs than
7533 abbrev tables. Sort by abbrev table to reduce the number of times we
7534 read each abbrev table in.
7535 Alternatives are to punt or to maintain a cache of abbrev tables.
7536 This is simpler and efficient enough for now.
7537
7538 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7539 symtab to use). Typically TUs with the same abbrev offset have the same
7540 stmt_list value too so in practice this should work well.
7541
7542 The basic algorithm here is:
7543
7544 sort TUs by abbrev table
7545 for each TU with same abbrev table:
7546 read abbrev table if first user
7547 read TU top level DIE
7548 [IWBN if DWO skeletons had DW_AT_stmt_list]
7549 call FUNC */
7550
7551 if (dwarf_read_debug)
7552 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7553
7554 /* Sort in a separate table to maintain the order of all_type_units
7555 for .gdb_index: TU indices directly index all_type_units. */
7556 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7557 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7558
7559 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7560 sorted_by_abbrev.emplace_back
7561 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7562 sig_type->per_cu.section,
7563 sig_type->per_cu.sect_off));
7564
7565 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7566 sort_tu_by_abbrev_offset);
7567
7568 abbrev_offset = (sect_offset) ~(unsigned) 0;
7569
7570 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7571 {
7572 /* Switch to the next abbrev table if necessary. */
7573 if (abbrev_table == NULL
7574 || tu.abbrev_offset != abbrev_offset)
7575 {
7576 abbrev_offset = tu.abbrev_offset;
7577 abbrev_table =
7578 abbrev_table::read (dwarf2_per_objfile->objfile,
7579 &dwarf2_per_objfile->abbrev,
7580 abbrev_offset);
7581 ++tu_stats->nr_uniq_abbrev_tables;
7582 }
7583
7584 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7585 0, false);
7586 if (!reader.dummy_p)
7587 build_type_psymtabs_reader (&reader, reader.info_ptr,
7588 reader.comp_unit_die);
7589 }
7590 }
7591
7592 /* Print collected type unit statistics. */
7593
7594 static void
7595 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7596 {
7597 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7598
7599 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7600 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7601 dwarf2_per_objfile->all_type_units.size ());
7602 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7603 tu_stats->nr_uniq_abbrev_tables);
7604 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7605 tu_stats->nr_symtabs);
7606 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7607 tu_stats->nr_symtab_sharers);
7608 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7609 tu_stats->nr_stmt_less_type_units);
7610 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7611 tu_stats->nr_all_type_units_reallocs);
7612 }
7613
7614 /* Traversal function for build_type_psymtabs. */
7615
7616 static int
7617 build_type_psymtab_dependencies (void **slot, void *info)
7618 {
7619 struct dwarf2_per_objfile *dwarf2_per_objfile
7620 = (struct dwarf2_per_objfile *) info;
7621 struct objfile *objfile = dwarf2_per_objfile->objfile;
7622 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7623 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7624 dwarf2_psymtab *pst = per_cu->v.psymtab;
7625 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7626 int i;
7627
7628 gdb_assert (len > 0);
7629 gdb_assert (per_cu->type_unit_group_p ());
7630
7631 pst->number_of_dependencies = len;
7632 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7633 for (i = 0; i < len; ++i)
7634 {
7635 struct signatured_type *iter = tu_group->tus->at (i);
7636 gdb_assert (iter->per_cu.is_debug_types);
7637 pst->dependencies[i] = iter->per_cu.v.psymtab;
7638 iter->type_unit_group = tu_group;
7639 }
7640
7641 delete tu_group->tus;
7642 tu_group->tus = nullptr;
7643
7644 return 1;
7645 }
7646
7647 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7648 Build partial symbol tables for the .debug_types comp-units. */
7649
7650 static void
7651 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7652 {
7653 if (! create_all_type_units (dwarf2_per_objfile))
7654 return;
7655
7656 build_type_psymtabs_1 (dwarf2_per_objfile);
7657 }
7658
7659 /* Traversal function for process_skeletonless_type_unit.
7660 Read a TU in a DWO file and build partial symbols for it. */
7661
7662 static int
7663 process_skeletonless_type_unit (void **slot, void *info)
7664 {
7665 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7666 struct dwarf2_per_objfile *dwarf2_per_objfile
7667 = (struct dwarf2_per_objfile *) info;
7668 struct signatured_type find_entry, *entry;
7669
7670 /* If this TU doesn't exist in the global table, add it and read it in. */
7671
7672 if (dwarf2_per_objfile->signatured_types == NULL)
7673 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7674
7675 find_entry.signature = dwo_unit->signature;
7676 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7677 &find_entry, INSERT);
7678 /* If we've already seen this type there's nothing to do. What's happening
7679 is we're doing our own version of comdat-folding here. */
7680 if (*slot != NULL)
7681 return 1;
7682
7683 /* This does the job that create_all_type_units would have done for
7684 this TU. */
7685 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7686 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7687 *slot = entry;
7688
7689 /* This does the job that build_type_psymtabs_1 would have done. */
7690 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7691 if (!reader.dummy_p)
7692 build_type_psymtabs_reader (&reader, reader.info_ptr,
7693 reader.comp_unit_die);
7694
7695 return 1;
7696 }
7697
7698 /* Traversal function for process_skeletonless_type_units. */
7699
7700 static int
7701 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7702 {
7703 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7704
7705 if (dwo_file->tus != NULL)
7706 htab_traverse_noresize (dwo_file->tus.get (),
7707 process_skeletonless_type_unit, info);
7708
7709 return 1;
7710 }
7711
7712 /* Scan all TUs of DWO files, verifying we've processed them.
7713 This is needed in case a TU was emitted without its skeleton.
7714 Note: This can't be done until we know what all the DWO files are. */
7715
7716 static void
7717 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7718 {
7719 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7720 if (get_dwp_file (dwarf2_per_objfile) == NULL
7721 && dwarf2_per_objfile->dwo_files != NULL)
7722 {
7723 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7724 process_dwo_file_for_skeletonless_type_units,
7725 dwarf2_per_objfile);
7726 }
7727 }
7728
7729 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7730
7731 static void
7732 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7733 {
7734 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7735 {
7736 dwarf2_psymtab *pst = per_cu->v.psymtab;
7737
7738 if (pst == NULL)
7739 continue;
7740
7741 for (int j = 0; j < pst->number_of_dependencies; ++j)
7742 {
7743 /* Set the 'user' field only if it is not already set. */
7744 if (pst->dependencies[j]->user == NULL)
7745 pst->dependencies[j]->user = pst;
7746 }
7747 }
7748 }
7749
7750 /* Build the partial symbol table by doing a quick pass through the
7751 .debug_info and .debug_abbrev sections. */
7752
7753 static void
7754 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7755 {
7756 struct objfile *objfile = dwarf2_per_objfile->objfile;
7757
7758 if (dwarf_read_debug)
7759 {
7760 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7761 objfile_name (objfile));
7762 }
7763
7764 scoped_restore restore_reading_psyms
7765 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7766 true);
7767
7768 dwarf2_per_objfile->info.read (objfile);
7769
7770 /* Any cached compilation units will be linked by the per-objfile
7771 read_in_chain. Make sure to free them when we're done. */
7772 free_cached_comp_units freer (dwarf2_per_objfile);
7773
7774 build_type_psymtabs (dwarf2_per_objfile);
7775
7776 create_all_comp_units (dwarf2_per_objfile);
7777
7778 /* Create a temporary address map on a temporary obstack. We later
7779 copy this to the final obstack. */
7780 auto_obstack temp_obstack;
7781
7782 scoped_restore save_psymtabs_addrmap
7783 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7784 addrmap_create_mutable (&temp_obstack));
7785
7786 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7787 process_psymtab_comp_unit (per_cu, false, language_minimal);
7788
7789 /* This has to wait until we read the CUs, we need the list of DWOs. */
7790 process_skeletonless_type_units (dwarf2_per_objfile);
7791
7792 /* Now that all TUs have been processed we can fill in the dependencies. */
7793 if (dwarf2_per_objfile->type_unit_groups != NULL)
7794 {
7795 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7796 build_type_psymtab_dependencies, dwarf2_per_objfile);
7797 }
7798
7799 if (dwarf_read_debug)
7800 print_tu_stats (dwarf2_per_objfile);
7801
7802 set_partial_user (dwarf2_per_objfile);
7803
7804 objfile->partial_symtabs->psymtabs_addrmap
7805 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7806 objfile->partial_symtabs->obstack ());
7807 /* At this point we want to keep the address map. */
7808 save_psymtabs_addrmap.release ();
7809
7810 if (dwarf_read_debug)
7811 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7812 objfile_name (objfile));
7813 }
7814
7815 /* Load the partial DIEs for a secondary CU into memory.
7816 This is also used when rereading a primary CU with load_all_dies. */
7817
7818 static void
7819 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7820 {
7821 cutu_reader reader (this_cu, NULL, 1, false);
7822
7823 if (!reader.dummy_p)
7824 {
7825 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7826 language_minimal);
7827
7828 /* Check if comp unit has_children.
7829 If so, read the rest of the partial symbols from this comp unit.
7830 If not, there's no more debug_info for this comp unit. */
7831 if (reader.comp_unit_die->has_children)
7832 load_partial_dies (&reader, reader.info_ptr, 0);
7833
7834 reader.keep ();
7835 }
7836 }
7837
7838 static void
7839 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7840 struct dwarf2_section_info *section,
7841 struct dwarf2_section_info *abbrev_section,
7842 unsigned int is_dwz)
7843 {
7844 const gdb_byte *info_ptr;
7845 struct objfile *objfile = dwarf2_per_objfile->objfile;
7846
7847 if (dwarf_read_debug)
7848 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7849 section->get_name (),
7850 section->get_file_name ());
7851
7852 section->read (objfile);
7853
7854 info_ptr = section->buffer;
7855
7856 while (info_ptr < section->buffer + section->size)
7857 {
7858 struct dwarf2_per_cu_data *this_cu;
7859
7860 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7861
7862 comp_unit_head cu_header;
7863 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7864 abbrev_section, info_ptr,
7865 rcuh_kind::COMPILE);
7866
7867 /* Save the compilation unit for later lookup. */
7868 if (cu_header.unit_type != DW_UT_type)
7869 {
7870 this_cu = XOBNEW (&objfile->objfile_obstack,
7871 struct dwarf2_per_cu_data);
7872 memset (this_cu, 0, sizeof (*this_cu));
7873 }
7874 else
7875 {
7876 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7877 struct signatured_type);
7878 memset (sig_type, 0, sizeof (*sig_type));
7879 sig_type->signature = cu_header.signature;
7880 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7881 this_cu = &sig_type->per_cu;
7882 }
7883 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7884 this_cu->sect_off = sect_off;
7885 this_cu->length = cu_header.length + cu_header.initial_length_size;
7886 this_cu->is_dwz = is_dwz;
7887 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7888 this_cu->section = section;
7889
7890 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7891
7892 info_ptr = info_ptr + this_cu->length;
7893 }
7894 }
7895
7896 /* Create a list of all compilation units in OBJFILE.
7897 This is only done for -readnow and building partial symtabs. */
7898
7899 static void
7900 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7901 {
7902 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7903 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7904 &dwarf2_per_objfile->abbrev, 0);
7905
7906 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7907 if (dwz != NULL)
7908 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7909 1);
7910 }
7911
7912 /* Process all loaded DIEs for compilation unit CU, starting at
7913 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7914 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7915 DW_AT_ranges). See the comments of add_partial_subprogram on how
7916 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7917
7918 static void
7919 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7920 CORE_ADDR *highpc, int set_addrmap,
7921 struct dwarf2_cu *cu)
7922 {
7923 struct partial_die_info *pdi;
7924
7925 /* Now, march along the PDI's, descending into ones which have
7926 interesting children but skipping the children of the other ones,
7927 until we reach the end of the compilation unit. */
7928
7929 pdi = first_die;
7930
7931 while (pdi != NULL)
7932 {
7933 pdi->fixup (cu);
7934
7935 /* Anonymous namespaces or modules have no name but have interesting
7936 children, so we need to look at them. Ditto for anonymous
7937 enums. */
7938
7939 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7940 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7941 || pdi->tag == DW_TAG_imported_unit
7942 || pdi->tag == DW_TAG_inlined_subroutine)
7943 {
7944 switch (pdi->tag)
7945 {
7946 case DW_TAG_subprogram:
7947 case DW_TAG_inlined_subroutine:
7948 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7949 break;
7950 case DW_TAG_constant:
7951 case DW_TAG_variable:
7952 case DW_TAG_typedef:
7953 case DW_TAG_union_type:
7954 if (!pdi->is_declaration)
7955 {
7956 add_partial_symbol (pdi, cu);
7957 }
7958 break;
7959 case DW_TAG_class_type:
7960 case DW_TAG_interface_type:
7961 case DW_TAG_structure_type:
7962 if (!pdi->is_declaration)
7963 {
7964 add_partial_symbol (pdi, cu);
7965 }
7966 if ((cu->language == language_rust
7967 || cu->language == language_cplus) && pdi->has_children)
7968 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7969 set_addrmap, cu);
7970 break;
7971 case DW_TAG_enumeration_type:
7972 if (!pdi->is_declaration)
7973 add_partial_enumeration (pdi, cu);
7974 break;
7975 case DW_TAG_base_type:
7976 case DW_TAG_subrange_type:
7977 /* File scope base type definitions are added to the partial
7978 symbol table. */
7979 add_partial_symbol (pdi, cu);
7980 break;
7981 case DW_TAG_namespace:
7982 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7983 break;
7984 case DW_TAG_module:
7985 if (!pdi->is_declaration)
7986 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7987 break;
7988 case DW_TAG_imported_unit:
7989 {
7990 struct dwarf2_per_cu_data *per_cu;
7991
7992 /* For now we don't handle imported units in type units. */
7993 if (cu->per_cu->is_debug_types)
7994 {
7995 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7996 " supported in type units [in module %s]"),
7997 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7998 }
7999
8000 per_cu = dwarf2_find_containing_comp_unit
8001 (pdi->d.sect_off, pdi->is_dwz,
8002 cu->per_cu->dwarf2_per_objfile);
8003
8004 /* Go read the partial unit, if needed. */
8005 if (per_cu->v.psymtab == NULL)
8006 process_psymtab_comp_unit (per_cu, true, cu->language);
8007
8008 cu->per_cu->imported_symtabs_push (per_cu);
8009 }
8010 break;
8011 case DW_TAG_imported_declaration:
8012 add_partial_symbol (pdi, cu);
8013 break;
8014 default:
8015 break;
8016 }
8017 }
8018
8019 /* If the die has a sibling, skip to the sibling. */
8020
8021 pdi = pdi->die_sibling;
8022 }
8023 }
8024
8025 /* Functions used to compute the fully scoped name of a partial DIE.
8026
8027 Normally, this is simple. For C++, the parent DIE's fully scoped
8028 name is concatenated with "::" and the partial DIE's name.
8029 Enumerators are an exception; they use the scope of their parent
8030 enumeration type, i.e. the name of the enumeration type is not
8031 prepended to the enumerator.
8032
8033 There are two complexities. One is DW_AT_specification; in this
8034 case "parent" means the parent of the target of the specification,
8035 instead of the direct parent of the DIE. The other is compilers
8036 which do not emit DW_TAG_namespace; in this case we try to guess
8037 the fully qualified name of structure types from their members'
8038 linkage names. This must be done using the DIE's children rather
8039 than the children of any DW_AT_specification target. We only need
8040 to do this for structures at the top level, i.e. if the target of
8041 any DW_AT_specification (if any; otherwise the DIE itself) does not
8042 have a parent. */
8043
8044 /* Compute the scope prefix associated with PDI's parent, in
8045 compilation unit CU. The result will be allocated on CU's
8046 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8047 field. NULL is returned if no prefix is necessary. */
8048 static const char *
8049 partial_die_parent_scope (struct partial_die_info *pdi,
8050 struct dwarf2_cu *cu)
8051 {
8052 const char *grandparent_scope;
8053 struct partial_die_info *parent, *real_pdi;
8054
8055 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8056 then this means the parent of the specification DIE. */
8057
8058 real_pdi = pdi;
8059 while (real_pdi->has_specification)
8060 {
8061 auto res = find_partial_die (real_pdi->spec_offset,
8062 real_pdi->spec_is_dwz, cu);
8063 real_pdi = res.pdi;
8064 cu = res.cu;
8065 }
8066
8067 parent = real_pdi->die_parent;
8068 if (parent == NULL)
8069 return NULL;
8070
8071 if (parent->scope_set)
8072 return parent->scope;
8073
8074 parent->fixup (cu);
8075
8076 grandparent_scope = partial_die_parent_scope (parent, cu);
8077
8078 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8079 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8080 Work around this problem here. */
8081 if (cu->language == language_cplus
8082 && parent->tag == DW_TAG_namespace
8083 && strcmp (parent->name, "::") == 0
8084 && grandparent_scope == NULL)
8085 {
8086 parent->scope = NULL;
8087 parent->scope_set = 1;
8088 return NULL;
8089 }
8090
8091 /* Nested subroutines in Fortran get a prefix. */
8092 if (pdi->tag == DW_TAG_enumerator)
8093 /* Enumerators should not get the name of the enumeration as a prefix. */
8094 parent->scope = grandparent_scope;
8095 else if (parent->tag == DW_TAG_namespace
8096 || parent->tag == DW_TAG_module
8097 || parent->tag == DW_TAG_structure_type
8098 || parent->tag == DW_TAG_class_type
8099 || parent->tag == DW_TAG_interface_type
8100 || parent->tag == DW_TAG_union_type
8101 || parent->tag == DW_TAG_enumeration_type
8102 || (cu->language == language_fortran
8103 && parent->tag == DW_TAG_subprogram
8104 && pdi->tag == DW_TAG_subprogram))
8105 {
8106 if (grandparent_scope == NULL)
8107 parent->scope = parent->name;
8108 else
8109 parent->scope = typename_concat (&cu->comp_unit_obstack,
8110 grandparent_scope,
8111 parent->name, 0, cu);
8112 }
8113 else
8114 {
8115 /* FIXME drow/2004-04-01: What should we be doing with
8116 function-local names? For partial symbols, we should probably be
8117 ignoring them. */
8118 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8119 dwarf_tag_name (parent->tag),
8120 sect_offset_str (pdi->sect_off));
8121 parent->scope = grandparent_scope;
8122 }
8123
8124 parent->scope_set = 1;
8125 return parent->scope;
8126 }
8127
8128 /* Return the fully scoped name associated with PDI, from compilation unit
8129 CU. The result will be allocated with malloc. */
8130
8131 static gdb::unique_xmalloc_ptr<char>
8132 partial_die_full_name (struct partial_die_info *pdi,
8133 struct dwarf2_cu *cu)
8134 {
8135 const char *parent_scope;
8136
8137 /* If this is a template instantiation, we can not work out the
8138 template arguments from partial DIEs. So, unfortunately, we have
8139 to go through the full DIEs. At least any work we do building
8140 types here will be reused if full symbols are loaded later. */
8141 if (pdi->has_template_arguments)
8142 {
8143 pdi->fixup (cu);
8144
8145 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8146 {
8147 struct die_info *die;
8148 struct attribute attr;
8149 struct dwarf2_cu *ref_cu = cu;
8150
8151 /* DW_FORM_ref_addr is using section offset. */
8152 attr.name = (enum dwarf_attribute) 0;
8153 attr.form = DW_FORM_ref_addr;
8154 attr.u.unsnd = to_underlying (pdi->sect_off);
8155 die = follow_die_ref (NULL, &attr, &ref_cu);
8156
8157 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8158 }
8159 }
8160
8161 parent_scope = partial_die_parent_scope (pdi, cu);
8162 if (parent_scope == NULL)
8163 return NULL;
8164 else
8165 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8166 pdi->name, 0, cu));
8167 }
8168
8169 static void
8170 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8171 {
8172 struct dwarf2_per_objfile *dwarf2_per_objfile
8173 = cu->per_cu->dwarf2_per_objfile;
8174 struct objfile *objfile = dwarf2_per_objfile->objfile;
8175 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8176 CORE_ADDR addr = 0;
8177 const char *actual_name = NULL;
8178 CORE_ADDR baseaddr;
8179
8180 baseaddr = objfile->text_section_offset ();
8181
8182 gdb::unique_xmalloc_ptr<char> built_actual_name
8183 = partial_die_full_name (pdi, cu);
8184 if (built_actual_name != NULL)
8185 actual_name = built_actual_name.get ();
8186
8187 if (actual_name == NULL)
8188 actual_name = pdi->name;
8189
8190 switch (pdi->tag)
8191 {
8192 case DW_TAG_inlined_subroutine:
8193 case DW_TAG_subprogram:
8194 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8195 - baseaddr);
8196 if (pdi->is_external
8197 || cu->language == language_ada
8198 || (cu->language == language_fortran
8199 && pdi->die_parent != NULL
8200 && pdi->die_parent->tag == DW_TAG_subprogram))
8201 {
8202 /* Normally, only "external" DIEs are part of the global scope.
8203 But in Ada and Fortran, we want to be able to access nested
8204 procedures globally. So all Ada and Fortran subprograms are
8205 stored in the global scope. */
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::GLOBAL,
8211 addr,
8212 cu->language, objfile);
8213 }
8214 else
8215 {
8216 add_psymbol_to_list (actual_name,
8217 built_actual_name != NULL,
8218 VAR_DOMAIN, LOC_BLOCK,
8219 SECT_OFF_TEXT (objfile),
8220 psymbol_placement::STATIC,
8221 addr, cu->language, objfile);
8222 }
8223
8224 if (pdi->main_subprogram && actual_name != NULL)
8225 set_objfile_main_name (objfile, actual_name, cu->language);
8226 break;
8227 case DW_TAG_constant:
8228 add_psymbol_to_list (actual_name,
8229 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8230 -1, (pdi->is_external
8231 ? psymbol_placement::GLOBAL
8232 : psymbol_placement::STATIC),
8233 0, cu->language, objfile);
8234 break;
8235 case DW_TAG_variable:
8236 if (pdi->d.locdesc)
8237 addr = decode_locdesc (pdi->d.locdesc, cu);
8238
8239 if (pdi->d.locdesc
8240 && addr == 0
8241 && !dwarf2_per_objfile->has_section_at_zero)
8242 {
8243 /* A global or static variable may also have been stripped
8244 out by the linker if unused, in which case its address
8245 will be nullified; do not add such variables into partial
8246 symbol table then. */
8247 }
8248 else if (pdi->is_external)
8249 {
8250 /* Global Variable.
8251 Don't enter into the minimal symbol tables as there is
8252 a minimal symbol table entry from the ELF symbols already.
8253 Enter into partial symbol table if it has a location
8254 descriptor or a type.
8255 If the location descriptor is missing, new_symbol will create
8256 a LOC_UNRESOLVED symbol, the address of the variable will then
8257 be determined from the minimal symbol table whenever the variable
8258 is referenced.
8259 The address for the partial symbol table entry is not
8260 used by GDB, but it comes in handy for debugging partial symbol
8261 table building. */
8262
8263 if (pdi->d.locdesc || pdi->has_type)
8264 add_psymbol_to_list (actual_name,
8265 built_actual_name != NULL,
8266 VAR_DOMAIN, LOC_STATIC,
8267 SECT_OFF_TEXT (objfile),
8268 psymbol_placement::GLOBAL,
8269 addr, cu->language, objfile);
8270 }
8271 else
8272 {
8273 int has_loc = pdi->d.locdesc != NULL;
8274
8275 /* Static Variable. Skip symbols whose value we cannot know (those
8276 without location descriptors or constant values). */
8277 if (!has_loc && !pdi->has_const_value)
8278 return;
8279
8280 add_psymbol_to_list (actual_name,
8281 built_actual_name != NULL,
8282 VAR_DOMAIN, LOC_STATIC,
8283 SECT_OFF_TEXT (objfile),
8284 psymbol_placement::STATIC,
8285 has_loc ? addr : 0,
8286 cu->language, objfile);
8287 }
8288 break;
8289 case DW_TAG_typedef:
8290 case DW_TAG_base_type:
8291 case DW_TAG_subrange_type:
8292 add_psymbol_to_list (actual_name,
8293 built_actual_name != NULL,
8294 VAR_DOMAIN, LOC_TYPEDEF, -1,
8295 psymbol_placement::STATIC,
8296 0, cu->language, objfile);
8297 break;
8298 case DW_TAG_imported_declaration:
8299 case DW_TAG_namespace:
8300 add_psymbol_to_list (actual_name,
8301 built_actual_name != NULL,
8302 VAR_DOMAIN, LOC_TYPEDEF, -1,
8303 psymbol_placement::GLOBAL,
8304 0, cu->language, objfile);
8305 break;
8306 case DW_TAG_module:
8307 /* With Fortran 77 there might be a "BLOCK DATA" module
8308 available without any name. If so, we skip the module as it
8309 doesn't bring any value. */
8310 if (actual_name != nullptr)
8311 add_psymbol_to_list (actual_name,
8312 built_actual_name != NULL,
8313 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8314 psymbol_placement::GLOBAL,
8315 0, cu->language, objfile);
8316 break;
8317 case DW_TAG_class_type:
8318 case DW_TAG_interface_type:
8319 case DW_TAG_structure_type:
8320 case DW_TAG_union_type:
8321 case DW_TAG_enumeration_type:
8322 /* Skip external references. The DWARF standard says in the section
8323 about "Structure, Union, and Class Type Entries": "An incomplete
8324 structure, union or class type is represented by a structure,
8325 union or class entry that does not have a byte size attribute
8326 and that has a DW_AT_declaration attribute." */
8327 if (!pdi->has_byte_size && pdi->is_declaration)
8328 return;
8329
8330 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8331 static vs. global. */
8332 add_psymbol_to_list (actual_name,
8333 built_actual_name != NULL,
8334 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8335 cu->language == language_cplus
8336 ? psymbol_placement::GLOBAL
8337 : psymbol_placement::STATIC,
8338 0, cu->language, objfile);
8339
8340 break;
8341 case DW_TAG_enumerator:
8342 add_psymbol_to_list (actual_name,
8343 built_actual_name != NULL,
8344 VAR_DOMAIN, LOC_CONST, -1,
8345 cu->language == language_cplus
8346 ? psymbol_placement::GLOBAL
8347 : psymbol_placement::STATIC,
8348 0, cu->language, objfile);
8349 break;
8350 default:
8351 break;
8352 }
8353 }
8354
8355 /* Read a partial die corresponding to a namespace; also, add a symbol
8356 corresponding to that namespace to the symbol table. NAMESPACE is
8357 the name of the enclosing namespace. */
8358
8359 static void
8360 add_partial_namespace (struct partial_die_info *pdi,
8361 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8362 int set_addrmap, struct dwarf2_cu *cu)
8363 {
8364 /* Add a symbol for the namespace. */
8365
8366 add_partial_symbol (pdi, cu);
8367
8368 /* Now scan partial symbols in that namespace. */
8369
8370 if (pdi->has_children)
8371 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8372 }
8373
8374 /* Read a partial die corresponding to a Fortran module. */
8375
8376 static void
8377 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8378 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8379 {
8380 /* Add a symbol for the namespace. */
8381
8382 add_partial_symbol (pdi, cu);
8383
8384 /* Now scan partial symbols in that module. */
8385
8386 if (pdi->has_children)
8387 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8388 }
8389
8390 /* Read a partial die corresponding to a subprogram or an inlined
8391 subprogram and create a partial symbol for that subprogram.
8392 When the CU language allows it, this routine also defines a partial
8393 symbol for each nested subprogram that this subprogram contains.
8394 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8395 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8396
8397 PDI may also be a lexical block, in which case we simply search
8398 recursively for subprograms defined inside that lexical block.
8399 Again, this is only performed when the CU language allows this
8400 type of definitions. */
8401
8402 static void
8403 add_partial_subprogram (struct partial_die_info *pdi,
8404 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8405 int set_addrmap, struct dwarf2_cu *cu)
8406 {
8407 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8408 {
8409 if (pdi->has_pc_info)
8410 {
8411 if (pdi->lowpc < *lowpc)
8412 *lowpc = pdi->lowpc;
8413 if (pdi->highpc > *highpc)
8414 *highpc = pdi->highpc;
8415 if (set_addrmap)
8416 {
8417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8418 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8419 CORE_ADDR baseaddr;
8420 CORE_ADDR this_highpc;
8421 CORE_ADDR this_lowpc;
8422
8423 baseaddr = objfile->text_section_offset ();
8424 this_lowpc
8425 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8426 pdi->lowpc + baseaddr)
8427 - baseaddr);
8428 this_highpc
8429 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8430 pdi->highpc + baseaddr)
8431 - baseaddr);
8432 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8433 this_lowpc, this_highpc - 1,
8434 cu->per_cu->v.psymtab);
8435 }
8436 }
8437
8438 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8439 {
8440 if (!pdi->is_declaration)
8441 /* Ignore subprogram DIEs that do not have a name, they are
8442 illegal. Do not emit a complaint at this point, we will
8443 do so when we convert this psymtab into a symtab. */
8444 if (pdi->name)
8445 add_partial_symbol (pdi, cu);
8446 }
8447 }
8448
8449 if (! pdi->has_children)
8450 return;
8451
8452 if (cu->language == language_ada || cu->language == language_fortran)
8453 {
8454 pdi = pdi->die_child;
8455 while (pdi != NULL)
8456 {
8457 pdi->fixup (cu);
8458 if (pdi->tag == DW_TAG_subprogram
8459 || pdi->tag == DW_TAG_inlined_subroutine
8460 || pdi->tag == DW_TAG_lexical_block)
8461 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8462 pdi = pdi->die_sibling;
8463 }
8464 }
8465 }
8466
8467 /* Read a partial die corresponding to an enumeration type. */
8468
8469 static void
8470 add_partial_enumeration (struct partial_die_info *enum_pdi,
8471 struct dwarf2_cu *cu)
8472 {
8473 struct partial_die_info *pdi;
8474
8475 if (enum_pdi->name != NULL)
8476 add_partial_symbol (enum_pdi, cu);
8477
8478 pdi = enum_pdi->die_child;
8479 while (pdi)
8480 {
8481 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8482 complaint (_("malformed enumerator DIE ignored"));
8483 else
8484 add_partial_symbol (pdi, cu);
8485 pdi = pdi->die_sibling;
8486 }
8487 }
8488
8489 /* Return the initial uleb128 in the die at INFO_PTR. */
8490
8491 static unsigned int
8492 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8493 {
8494 unsigned int bytes_read;
8495
8496 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8497 }
8498
8499 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8500 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8501
8502 Return the corresponding abbrev, or NULL if the number is zero (indicating
8503 an empty DIE). In either case *BYTES_READ will be set to the length of
8504 the initial number. */
8505
8506 static struct abbrev_info *
8507 peek_die_abbrev (const die_reader_specs &reader,
8508 const gdb_byte *info_ptr, unsigned int *bytes_read)
8509 {
8510 dwarf2_cu *cu = reader.cu;
8511 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8512 unsigned int abbrev_number
8513 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8514
8515 if (abbrev_number == 0)
8516 return NULL;
8517
8518 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8519 if (!abbrev)
8520 {
8521 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8522 " at offset %s [in module %s]"),
8523 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8524 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8525 }
8526
8527 return abbrev;
8528 }
8529
8530 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8531 Returns a pointer to the end of a series of DIEs, terminated by an empty
8532 DIE. Any children of the skipped DIEs will also be skipped. */
8533
8534 static const gdb_byte *
8535 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8536 {
8537 while (1)
8538 {
8539 unsigned int bytes_read;
8540 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8541
8542 if (abbrev == NULL)
8543 return info_ptr + bytes_read;
8544 else
8545 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8546 }
8547 }
8548
8549 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8550 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8551 abbrev corresponding to that skipped uleb128 should be passed in
8552 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8553 children. */
8554
8555 static const gdb_byte *
8556 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8557 struct abbrev_info *abbrev)
8558 {
8559 unsigned int bytes_read;
8560 struct attribute attr;
8561 bfd *abfd = reader->abfd;
8562 struct dwarf2_cu *cu = reader->cu;
8563 const gdb_byte *buffer = reader->buffer;
8564 const gdb_byte *buffer_end = reader->buffer_end;
8565 unsigned int form, i;
8566
8567 for (i = 0; i < abbrev->num_attrs; i++)
8568 {
8569 /* The only abbrev we care about is DW_AT_sibling. */
8570 if (abbrev->attrs[i].name == DW_AT_sibling)
8571 {
8572 bool ignored;
8573 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8574 &ignored);
8575 if (attr.form == DW_FORM_ref_addr)
8576 complaint (_("ignoring absolute DW_AT_sibling"));
8577 else
8578 {
8579 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8580 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8581
8582 if (sibling_ptr < info_ptr)
8583 complaint (_("DW_AT_sibling points backwards"));
8584 else if (sibling_ptr > reader->buffer_end)
8585 reader->die_section->overflow_complaint ();
8586 else
8587 return sibling_ptr;
8588 }
8589 }
8590
8591 /* If it isn't DW_AT_sibling, skip this attribute. */
8592 form = abbrev->attrs[i].form;
8593 skip_attribute:
8594 switch (form)
8595 {
8596 case DW_FORM_ref_addr:
8597 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8598 and later it is offset sized. */
8599 if (cu->header.version == 2)
8600 info_ptr += cu->header.addr_size;
8601 else
8602 info_ptr += cu->header.offset_size;
8603 break;
8604 case DW_FORM_GNU_ref_alt:
8605 info_ptr += cu->header.offset_size;
8606 break;
8607 case DW_FORM_addr:
8608 info_ptr += cu->header.addr_size;
8609 break;
8610 case DW_FORM_data1:
8611 case DW_FORM_ref1:
8612 case DW_FORM_flag:
8613 case DW_FORM_strx1:
8614 info_ptr += 1;
8615 break;
8616 case DW_FORM_flag_present:
8617 case DW_FORM_implicit_const:
8618 break;
8619 case DW_FORM_data2:
8620 case DW_FORM_ref2:
8621 case DW_FORM_strx2:
8622 info_ptr += 2;
8623 break;
8624 case DW_FORM_strx3:
8625 info_ptr += 3;
8626 break;
8627 case DW_FORM_data4:
8628 case DW_FORM_ref4:
8629 case DW_FORM_strx4:
8630 info_ptr += 4;
8631 break;
8632 case DW_FORM_data8:
8633 case DW_FORM_ref8:
8634 case DW_FORM_ref_sig8:
8635 info_ptr += 8;
8636 break;
8637 case DW_FORM_data16:
8638 info_ptr += 16;
8639 break;
8640 case DW_FORM_string:
8641 read_direct_string (abfd, info_ptr, &bytes_read);
8642 info_ptr += bytes_read;
8643 break;
8644 case DW_FORM_sec_offset:
8645 case DW_FORM_strp:
8646 case DW_FORM_GNU_strp_alt:
8647 info_ptr += cu->header.offset_size;
8648 break;
8649 case DW_FORM_exprloc:
8650 case DW_FORM_block:
8651 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8652 info_ptr += bytes_read;
8653 break;
8654 case DW_FORM_block1:
8655 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8656 break;
8657 case DW_FORM_block2:
8658 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8659 break;
8660 case DW_FORM_block4:
8661 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8662 break;
8663 case DW_FORM_addrx:
8664 case DW_FORM_strx:
8665 case DW_FORM_sdata:
8666 case DW_FORM_udata:
8667 case DW_FORM_ref_udata:
8668 case DW_FORM_GNU_addr_index:
8669 case DW_FORM_GNU_str_index:
8670 case DW_FORM_rnglistx:
8671 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8672 break;
8673 case DW_FORM_indirect:
8674 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8675 info_ptr += bytes_read;
8676 /* We need to continue parsing from here, so just go back to
8677 the top. */
8678 goto skip_attribute;
8679
8680 default:
8681 error (_("Dwarf Error: Cannot handle %s "
8682 "in DWARF reader [in module %s]"),
8683 dwarf_form_name (form),
8684 bfd_get_filename (abfd));
8685 }
8686 }
8687
8688 if (abbrev->has_children)
8689 return skip_children (reader, info_ptr);
8690 else
8691 return info_ptr;
8692 }
8693
8694 /* Locate ORIG_PDI's sibling.
8695 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8696
8697 static const gdb_byte *
8698 locate_pdi_sibling (const struct die_reader_specs *reader,
8699 struct partial_die_info *orig_pdi,
8700 const gdb_byte *info_ptr)
8701 {
8702 /* Do we know the sibling already? */
8703
8704 if (orig_pdi->sibling)
8705 return orig_pdi->sibling;
8706
8707 /* Are there any children to deal with? */
8708
8709 if (!orig_pdi->has_children)
8710 return info_ptr;
8711
8712 /* Skip the children the long way. */
8713
8714 return skip_children (reader, info_ptr);
8715 }
8716
8717 /* Expand this partial symbol table into a full symbol table. SELF is
8718 not NULL. */
8719
8720 void
8721 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8722 {
8723 struct dwarf2_per_objfile *dwarf2_per_objfile
8724 = get_dwarf2_per_objfile (objfile);
8725
8726 gdb_assert (!readin);
8727 /* If this psymtab is constructed from a debug-only objfile, the
8728 has_section_at_zero flag will not necessarily be correct. We
8729 can get the correct value for this flag by looking at the data
8730 associated with the (presumably stripped) associated objfile. */
8731 if (objfile->separate_debug_objfile_backlink)
8732 {
8733 struct dwarf2_per_objfile *dpo_backlink
8734 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8735
8736 dwarf2_per_objfile->has_section_at_zero
8737 = dpo_backlink->has_section_at_zero;
8738 }
8739
8740 expand_psymtab (objfile);
8741
8742 process_cu_includes (dwarf2_per_objfile);
8743 }
8744 \f
8745 /* Reading in full CUs. */
8746
8747 /* Add PER_CU to the queue. */
8748
8749 static void
8750 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8751 enum language pretend_language)
8752 {
8753 per_cu->queued = 1;
8754 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8755 }
8756
8757 /* If PER_CU is not yet queued, add it to the queue.
8758 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8759 dependency.
8760 The result is non-zero if PER_CU was queued, otherwise the result is zero
8761 meaning either PER_CU is already queued or it is already loaded.
8762
8763 N.B. There is an invariant here that if a CU is queued then it is loaded.
8764 The caller is required to load PER_CU if we return non-zero. */
8765
8766 static int
8767 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8768 struct dwarf2_per_cu_data *per_cu,
8769 enum language pretend_language)
8770 {
8771 /* We may arrive here during partial symbol reading, if we need full
8772 DIEs to process an unusual case (e.g. template arguments). Do
8773 not queue PER_CU, just tell our caller to load its DIEs. */
8774 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8775 {
8776 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8777 return 1;
8778 return 0;
8779 }
8780
8781 /* Mark the dependence relation so that we don't flush PER_CU
8782 too early. */
8783 if (dependent_cu != NULL)
8784 dwarf2_add_dependence (dependent_cu, per_cu);
8785
8786 /* If it's already on the queue, we have nothing to do. */
8787 if (per_cu->queued)
8788 return 0;
8789
8790 /* If the compilation unit is already loaded, just mark it as
8791 used. */
8792 if (per_cu->cu != NULL)
8793 {
8794 per_cu->cu->last_used = 0;
8795 return 0;
8796 }
8797
8798 /* Add it to the queue. */
8799 queue_comp_unit (per_cu, pretend_language);
8800
8801 return 1;
8802 }
8803
8804 /* Process the queue. */
8805
8806 static void
8807 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8808 {
8809 if (dwarf_read_debug)
8810 {
8811 fprintf_unfiltered (gdb_stdlog,
8812 "Expanding one or more symtabs of objfile %s ...\n",
8813 objfile_name (dwarf2_per_objfile->objfile));
8814 }
8815
8816 /* The queue starts out with one item, but following a DIE reference
8817 may load a new CU, adding it to the end of the queue. */
8818 while (!dwarf2_per_objfile->queue.empty ())
8819 {
8820 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8821
8822 if ((dwarf2_per_objfile->using_index
8823 ? !item.per_cu->v.quick->compunit_symtab
8824 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8825 /* Skip dummy CUs. */
8826 && item.per_cu->cu != NULL)
8827 {
8828 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8829 unsigned int debug_print_threshold;
8830 char buf[100];
8831
8832 if (per_cu->is_debug_types)
8833 {
8834 struct signatured_type *sig_type =
8835 (struct signatured_type *) per_cu;
8836
8837 sprintf (buf, "TU %s at offset %s",
8838 hex_string (sig_type->signature),
8839 sect_offset_str (per_cu->sect_off));
8840 /* There can be 100s of TUs.
8841 Only print them in verbose mode. */
8842 debug_print_threshold = 2;
8843 }
8844 else
8845 {
8846 sprintf (buf, "CU at offset %s",
8847 sect_offset_str (per_cu->sect_off));
8848 debug_print_threshold = 1;
8849 }
8850
8851 if (dwarf_read_debug >= debug_print_threshold)
8852 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8853
8854 if (per_cu->is_debug_types)
8855 process_full_type_unit (per_cu, item.pretend_language);
8856 else
8857 process_full_comp_unit (per_cu, item.pretend_language);
8858
8859 if (dwarf_read_debug >= debug_print_threshold)
8860 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8861 }
8862
8863 item.per_cu->queued = 0;
8864 dwarf2_per_objfile->queue.pop ();
8865 }
8866
8867 if (dwarf_read_debug)
8868 {
8869 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8870 objfile_name (dwarf2_per_objfile->objfile));
8871 }
8872 }
8873
8874 /* Read in full symbols for PST, and anything it depends on. */
8875
8876 void
8877 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8878 {
8879 if (readin)
8880 return;
8881
8882 read_dependencies (objfile);
8883
8884 dw2_do_instantiate_symtab (per_cu_data, false);
8885 gdb_assert (get_compunit_symtab () != nullptr);
8886 }
8887
8888 /* Trivial hash function for die_info: the hash value of a DIE
8889 is its offset in .debug_info for this objfile. */
8890
8891 static hashval_t
8892 die_hash (const void *item)
8893 {
8894 const struct die_info *die = (const struct die_info *) item;
8895
8896 return to_underlying (die->sect_off);
8897 }
8898
8899 /* Trivial comparison function for die_info structures: two DIEs
8900 are equal if they have the same offset. */
8901
8902 static int
8903 die_eq (const void *item_lhs, const void *item_rhs)
8904 {
8905 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8906 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8907
8908 return die_lhs->sect_off == die_rhs->sect_off;
8909 }
8910
8911 /* Load the DIEs associated with PER_CU into memory. */
8912
8913 static void
8914 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8915 bool skip_partial,
8916 enum language pretend_language)
8917 {
8918 gdb_assert (! this_cu->is_debug_types);
8919
8920 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8921 if (reader.dummy_p)
8922 return;
8923
8924 struct dwarf2_cu *cu = reader.cu;
8925 const gdb_byte *info_ptr = reader.info_ptr;
8926
8927 gdb_assert (cu->die_hash == NULL);
8928 cu->die_hash =
8929 htab_create_alloc_ex (cu->header.length / 12,
8930 die_hash,
8931 die_eq,
8932 NULL,
8933 &cu->comp_unit_obstack,
8934 hashtab_obstack_allocate,
8935 dummy_obstack_deallocate);
8936
8937 if (reader.comp_unit_die->has_children)
8938 reader.comp_unit_die->child
8939 = read_die_and_siblings (&reader, reader.info_ptr,
8940 &info_ptr, reader.comp_unit_die);
8941 cu->dies = reader.comp_unit_die;
8942 /* comp_unit_die is not stored in die_hash, no need. */
8943
8944 /* We try not to read any attributes in this function, because not
8945 all CUs needed for references have been loaded yet, and symbol
8946 table processing isn't initialized. But we have to set the CU language,
8947 or we won't be able to build types correctly.
8948 Similarly, if we do not read the producer, we can not apply
8949 producer-specific interpretation. */
8950 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8951
8952 reader.keep ();
8953 }
8954
8955 /* Add a DIE to the delayed physname list. */
8956
8957 static void
8958 add_to_method_list (struct type *type, int fnfield_index, int index,
8959 const char *name, struct die_info *die,
8960 struct dwarf2_cu *cu)
8961 {
8962 struct delayed_method_info mi;
8963 mi.type = type;
8964 mi.fnfield_index = fnfield_index;
8965 mi.index = index;
8966 mi.name = name;
8967 mi.die = die;
8968 cu->method_list.push_back (mi);
8969 }
8970
8971 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8972 "const" / "volatile". If so, decrements LEN by the length of the
8973 modifier and return true. Otherwise return false. */
8974
8975 template<size_t N>
8976 static bool
8977 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8978 {
8979 size_t mod_len = sizeof (mod) - 1;
8980 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8981 {
8982 len -= mod_len;
8983 return true;
8984 }
8985 return false;
8986 }
8987
8988 /* Compute the physnames of any methods on the CU's method list.
8989
8990 The computation of method physnames is delayed in order to avoid the
8991 (bad) condition that one of the method's formal parameters is of an as yet
8992 incomplete type. */
8993
8994 static void
8995 compute_delayed_physnames (struct dwarf2_cu *cu)
8996 {
8997 /* Only C++ delays computing physnames. */
8998 if (cu->method_list.empty ())
8999 return;
9000 gdb_assert (cu->language == language_cplus);
9001
9002 for (const delayed_method_info &mi : cu->method_list)
9003 {
9004 const char *physname;
9005 struct fn_fieldlist *fn_flp
9006 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9007 physname = dwarf2_physname (mi.name, mi.die, cu);
9008 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9009 = physname ? physname : "";
9010
9011 /* Since there's no tag to indicate whether a method is a
9012 const/volatile overload, extract that information out of the
9013 demangled name. */
9014 if (physname != NULL)
9015 {
9016 size_t len = strlen (physname);
9017
9018 while (1)
9019 {
9020 if (physname[len] == ')') /* shortcut */
9021 break;
9022 else if (check_modifier (physname, len, " const"))
9023 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9024 else if (check_modifier (physname, len, " volatile"))
9025 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9026 else
9027 break;
9028 }
9029 }
9030 }
9031
9032 /* The list is no longer needed. */
9033 cu->method_list.clear ();
9034 }
9035
9036 /* Go objects should be embedded in a DW_TAG_module DIE,
9037 and it's not clear if/how imported objects will appear.
9038 To keep Go support simple until that's worked out,
9039 go back through what we've read and create something usable.
9040 We could do this while processing each DIE, and feels kinda cleaner,
9041 but that way is more invasive.
9042 This is to, for example, allow the user to type "p var" or "b main"
9043 without having to specify the package name, and allow lookups
9044 of module.object to work in contexts that use the expression
9045 parser. */
9046
9047 static void
9048 fixup_go_packaging (struct dwarf2_cu *cu)
9049 {
9050 gdb::unique_xmalloc_ptr<char> package_name;
9051 struct pending *list;
9052 int i;
9053
9054 for (list = *cu->get_builder ()->get_global_symbols ();
9055 list != NULL;
9056 list = list->next)
9057 {
9058 for (i = 0; i < list->nsyms; ++i)
9059 {
9060 struct symbol *sym = list->symbol[i];
9061
9062 if (sym->language () == language_go
9063 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9064 {
9065 gdb::unique_xmalloc_ptr<char> this_package_name
9066 (go_symbol_package_name (sym));
9067
9068 if (this_package_name == NULL)
9069 continue;
9070 if (package_name == NULL)
9071 package_name = std::move (this_package_name);
9072 else
9073 {
9074 struct objfile *objfile
9075 = cu->per_cu->dwarf2_per_objfile->objfile;
9076 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9077 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9078 (symbol_symtab (sym) != NULL
9079 ? symtab_to_filename_for_display
9080 (symbol_symtab (sym))
9081 : objfile_name (objfile)),
9082 this_package_name.get (), package_name.get ());
9083 }
9084 }
9085 }
9086 }
9087
9088 if (package_name != NULL)
9089 {
9090 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9091 const char *saved_package_name = objfile->intern (package_name.get ());
9092 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9093 saved_package_name);
9094 struct symbol *sym;
9095
9096 sym = allocate_symbol (objfile);
9097 sym->set_language (language_go, &objfile->objfile_obstack);
9098 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9099 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9100 e.g., "main" finds the "main" module and not C's main(). */
9101 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9102 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9103 SYMBOL_TYPE (sym) = type;
9104
9105 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9106 }
9107 }
9108
9109 /* Allocate a fully-qualified name consisting of the two parts on the
9110 obstack. */
9111
9112 static const char *
9113 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9114 {
9115 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9116 }
9117
9118 /* A helper that allocates a struct discriminant_info to attach to a
9119 union type. */
9120
9121 static struct discriminant_info *
9122 alloc_discriminant_info (struct type *type, int discriminant_index,
9123 int default_index)
9124 {
9125 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9126 gdb_assert (discriminant_index == -1
9127 || (discriminant_index >= 0
9128 && discriminant_index < TYPE_NFIELDS (type)));
9129 gdb_assert (default_index == -1
9130 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9131
9132 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9133
9134 struct discriminant_info *disc
9135 = ((struct discriminant_info *)
9136 TYPE_ZALLOC (type,
9137 offsetof (struct discriminant_info, discriminants)
9138 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9139 disc->default_index = default_index;
9140 disc->discriminant_index = discriminant_index;
9141
9142 struct dynamic_prop prop;
9143 prop.kind = PROP_UNDEFINED;
9144 prop.data.baton = disc;
9145
9146 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9147
9148 return disc;
9149 }
9150
9151 /* Some versions of rustc emitted enums in an unusual way.
9152
9153 Ordinary enums were emitted as unions. The first element of each
9154 structure in the union was named "RUST$ENUM$DISR". This element
9155 held the discriminant.
9156
9157 These versions of Rust also implemented the "non-zero"
9158 optimization. When the enum had two values, and one is empty and
9159 the other holds a pointer that cannot be zero, the pointer is used
9160 as the discriminant, with a zero value meaning the empty variant.
9161 Here, the union's first member is of the form
9162 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9163 where the fieldnos are the indices of the fields that should be
9164 traversed in order to find the field (which may be several fields deep)
9165 and the variantname is the name of the variant of the case when the
9166 field is zero.
9167
9168 This function recognizes whether TYPE is of one of these forms,
9169 and, if so, smashes it to be a variant type. */
9170
9171 static void
9172 quirk_rust_enum (struct type *type, struct objfile *objfile)
9173 {
9174 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9175
9176 /* We don't need to deal with empty enums. */
9177 if (TYPE_NFIELDS (type) == 0)
9178 return;
9179
9180 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9181 if (TYPE_NFIELDS (type) == 1
9182 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9183 {
9184 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9185
9186 /* Decode the field name to find the offset of the
9187 discriminant. */
9188 ULONGEST bit_offset = 0;
9189 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9190 while (name[0] >= '0' && name[0] <= '9')
9191 {
9192 char *tail;
9193 unsigned long index = strtoul (name, &tail, 10);
9194 name = tail;
9195 if (*name != '$'
9196 || index >= TYPE_NFIELDS (field_type)
9197 || (TYPE_FIELD_LOC_KIND (field_type, index)
9198 != FIELD_LOC_KIND_BITPOS))
9199 {
9200 complaint (_("Could not parse Rust enum encoding string \"%s\""
9201 "[in module %s]"),
9202 TYPE_FIELD_NAME (type, 0),
9203 objfile_name (objfile));
9204 return;
9205 }
9206 ++name;
9207
9208 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9209 field_type = TYPE_FIELD_TYPE (field_type, index);
9210 }
9211
9212 /* Make a union to hold the variants. */
9213 struct type *union_type = alloc_type (objfile);
9214 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9215 TYPE_NFIELDS (union_type) = 3;
9216 TYPE_FIELDS (union_type)
9217 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9218 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9219 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9220
9221 /* Put the discriminant must at index 0. */
9222 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9223 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9224 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9225 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9226
9227 /* The order of fields doesn't really matter, so put the real
9228 field at index 1 and the data-less field at index 2. */
9229 struct discriminant_info *disc
9230 = alloc_discriminant_info (union_type, 0, 1);
9231 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9232 TYPE_FIELD_NAME (union_type, 1)
9233 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9234 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9235 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9236 TYPE_FIELD_NAME (union_type, 1));
9237
9238 const char *dataless_name
9239 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9240 name);
9241 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9242 dataless_name);
9243 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9244 /* NAME points into the original discriminant name, which
9245 already has the correct lifetime. */
9246 TYPE_FIELD_NAME (union_type, 2) = name;
9247 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9248 disc->discriminants[2] = 0;
9249
9250 /* Smash this type to be a structure type. We have to do this
9251 because the type has already been recorded. */
9252 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9253 TYPE_NFIELDS (type) = 1;
9254 TYPE_FIELDS (type)
9255 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9256
9257 /* Install the variant part. */
9258 TYPE_FIELD_TYPE (type, 0) = union_type;
9259 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9260 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9261 }
9262 /* A union with a single anonymous field is probably an old-style
9263 univariant enum. */
9264 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9265 {
9266 /* Smash this type to be a structure type. We have to do this
9267 because the type has already been recorded. */
9268 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9269
9270 /* Make a union to hold the variants. */
9271 struct type *union_type = alloc_type (objfile);
9272 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9273 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9274 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9275 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9276 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9277
9278 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9279 const char *variant_name
9280 = rust_last_path_segment (TYPE_NAME (field_type));
9281 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9282 TYPE_NAME (field_type)
9283 = rust_fully_qualify (&objfile->objfile_obstack,
9284 TYPE_NAME (type), variant_name);
9285
9286 /* Install the union in the outer struct type. */
9287 TYPE_NFIELDS (type) = 1;
9288 TYPE_FIELDS (type)
9289 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9290 TYPE_FIELD_TYPE (type, 0) = union_type;
9291 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9292 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9293
9294 alloc_discriminant_info (union_type, -1, 0);
9295 }
9296 else
9297 {
9298 struct type *disr_type = nullptr;
9299 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9300 {
9301 disr_type = TYPE_FIELD_TYPE (type, i);
9302
9303 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9304 {
9305 /* All fields of a true enum will be structs. */
9306 return;
9307 }
9308 else if (TYPE_NFIELDS (disr_type) == 0)
9309 {
9310 /* Could be data-less variant, so keep going. */
9311 disr_type = nullptr;
9312 }
9313 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9314 "RUST$ENUM$DISR") != 0)
9315 {
9316 /* Not a Rust enum. */
9317 return;
9318 }
9319 else
9320 {
9321 /* Found one. */
9322 break;
9323 }
9324 }
9325
9326 /* If we got here without a discriminant, then it's probably
9327 just a union. */
9328 if (disr_type == nullptr)
9329 return;
9330
9331 /* Smash this type to be a structure type. We have to do this
9332 because the type has already been recorded. */
9333 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9334
9335 /* Make a union to hold the variants. */
9336 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9337 struct type *union_type = alloc_type (objfile);
9338 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9339 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9340 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9341 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9342 TYPE_FIELDS (union_type)
9343 = (struct field *) TYPE_ZALLOC (union_type,
9344 (TYPE_NFIELDS (union_type)
9345 * sizeof (struct field)));
9346
9347 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9348 TYPE_NFIELDS (type) * sizeof (struct field));
9349
9350 /* Install the discriminant at index 0 in the union. */
9351 TYPE_FIELD (union_type, 0) = *disr_field;
9352 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9353 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9354
9355 /* Install the union in the outer struct type. */
9356 TYPE_FIELD_TYPE (type, 0) = union_type;
9357 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9358 TYPE_NFIELDS (type) = 1;
9359
9360 /* Set the size and offset of the union type. */
9361 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9362
9363 /* We need a way to find the correct discriminant given a
9364 variant name. For convenience we build a map here. */
9365 struct type *enum_type = FIELD_TYPE (*disr_field);
9366 std::unordered_map<std::string, ULONGEST> discriminant_map;
9367 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9368 {
9369 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9370 {
9371 const char *name
9372 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9373 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9374 }
9375 }
9376
9377 int n_fields = TYPE_NFIELDS (union_type);
9378 struct discriminant_info *disc
9379 = alloc_discriminant_info (union_type, 0, -1);
9380 /* Skip the discriminant here. */
9381 for (int i = 1; i < n_fields; ++i)
9382 {
9383 /* Find the final word in the name of this variant's type.
9384 That name can be used to look up the correct
9385 discriminant. */
9386 const char *variant_name
9387 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9388 i)));
9389
9390 auto iter = discriminant_map.find (variant_name);
9391 if (iter != discriminant_map.end ())
9392 disc->discriminants[i] = iter->second;
9393
9394 /* Remove the discriminant field, if it exists. */
9395 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9396 if (TYPE_NFIELDS (sub_type) > 0)
9397 {
9398 --TYPE_NFIELDS (sub_type);
9399 ++TYPE_FIELDS (sub_type);
9400 }
9401 TYPE_FIELD_NAME (union_type, i) = variant_name;
9402 TYPE_NAME (sub_type)
9403 = rust_fully_qualify (&objfile->objfile_obstack,
9404 TYPE_NAME (type), variant_name);
9405 }
9406 }
9407 }
9408
9409 /* Rewrite some Rust unions to be structures with variants parts. */
9410
9411 static void
9412 rust_union_quirks (struct dwarf2_cu *cu)
9413 {
9414 gdb_assert (cu->language == language_rust);
9415 for (type *type_ : cu->rust_unions)
9416 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9417 /* We don't need this any more. */
9418 cu->rust_unions.clear ();
9419 }
9420
9421 /* Return the symtab for PER_CU. This works properly regardless of
9422 whether we're using the index or psymtabs. */
9423
9424 static struct compunit_symtab *
9425 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9426 {
9427 return (per_cu->dwarf2_per_objfile->using_index
9428 ? per_cu->v.quick->compunit_symtab
9429 : per_cu->v.psymtab->compunit_symtab);
9430 }
9431
9432 /* A helper function for computing the list of all symbol tables
9433 included by PER_CU. */
9434
9435 static void
9436 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9437 htab_t all_children, htab_t all_type_symtabs,
9438 struct dwarf2_per_cu_data *per_cu,
9439 struct compunit_symtab *immediate_parent)
9440 {
9441 void **slot;
9442 struct compunit_symtab *cust;
9443
9444 slot = htab_find_slot (all_children, per_cu, INSERT);
9445 if (*slot != NULL)
9446 {
9447 /* This inclusion and its children have been processed. */
9448 return;
9449 }
9450
9451 *slot = per_cu;
9452 /* Only add a CU if it has a symbol table. */
9453 cust = get_compunit_symtab (per_cu);
9454 if (cust != NULL)
9455 {
9456 /* If this is a type unit only add its symbol table if we haven't
9457 seen it yet (type unit per_cu's can share symtabs). */
9458 if (per_cu->is_debug_types)
9459 {
9460 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9461 if (*slot == NULL)
9462 {
9463 *slot = cust;
9464 result->push_back (cust);
9465 if (cust->user == NULL)
9466 cust->user = immediate_parent;
9467 }
9468 }
9469 else
9470 {
9471 result->push_back (cust);
9472 if (cust->user == NULL)
9473 cust->user = immediate_parent;
9474 }
9475 }
9476
9477 if (!per_cu->imported_symtabs_empty ())
9478 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9479 {
9480 recursively_compute_inclusions (result, all_children,
9481 all_type_symtabs, ptr, cust);
9482 }
9483 }
9484
9485 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9486 PER_CU. */
9487
9488 static void
9489 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9490 {
9491 gdb_assert (! per_cu->is_debug_types);
9492
9493 if (!per_cu->imported_symtabs_empty ())
9494 {
9495 int len;
9496 std::vector<compunit_symtab *> result_symtabs;
9497 htab_t all_children, all_type_symtabs;
9498 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9499
9500 /* If we don't have a symtab, we can just skip this case. */
9501 if (cust == NULL)
9502 return;
9503
9504 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9505 NULL, xcalloc, xfree);
9506 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9507 NULL, xcalloc, xfree);
9508
9509 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9510 {
9511 recursively_compute_inclusions (&result_symtabs, all_children,
9512 all_type_symtabs, ptr, cust);
9513 }
9514
9515 /* Now we have a transitive closure of all the included symtabs. */
9516 len = result_symtabs.size ();
9517 cust->includes
9518 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9519 struct compunit_symtab *, len + 1);
9520 memcpy (cust->includes, result_symtabs.data (),
9521 len * sizeof (compunit_symtab *));
9522 cust->includes[len] = NULL;
9523
9524 htab_delete (all_children);
9525 htab_delete (all_type_symtabs);
9526 }
9527 }
9528
9529 /* Compute the 'includes' field for the symtabs of all the CUs we just
9530 read. */
9531
9532 static void
9533 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9534 {
9535 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9536 {
9537 if (! iter->is_debug_types)
9538 compute_compunit_symtab_includes (iter);
9539 }
9540
9541 dwarf2_per_objfile->just_read_cus.clear ();
9542 }
9543
9544 /* Generate full symbol information for PER_CU, whose DIEs have
9545 already been loaded into memory. */
9546
9547 static void
9548 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9549 enum language pretend_language)
9550 {
9551 struct dwarf2_cu *cu = per_cu->cu;
9552 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9553 struct objfile *objfile = dwarf2_per_objfile->objfile;
9554 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9555 CORE_ADDR lowpc, highpc;
9556 struct compunit_symtab *cust;
9557 CORE_ADDR baseaddr;
9558 struct block *static_block;
9559 CORE_ADDR addr;
9560
9561 baseaddr = objfile->text_section_offset ();
9562
9563 /* Clear the list here in case something was left over. */
9564 cu->method_list.clear ();
9565
9566 cu->language = pretend_language;
9567 cu->language_defn = language_def (cu->language);
9568
9569 /* Do line number decoding in read_file_scope () */
9570 process_die (cu->dies, cu);
9571
9572 /* For now fudge the Go package. */
9573 if (cu->language == language_go)
9574 fixup_go_packaging (cu);
9575
9576 /* Now that we have processed all the DIEs in the CU, all the types
9577 should be complete, and it should now be safe to compute all of the
9578 physnames. */
9579 compute_delayed_physnames (cu);
9580
9581 if (cu->language == language_rust)
9582 rust_union_quirks (cu);
9583
9584 /* Some compilers don't define a DW_AT_high_pc attribute for the
9585 compilation unit. If the DW_AT_high_pc is missing, synthesize
9586 it, by scanning the DIE's below the compilation unit. */
9587 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9588
9589 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9590 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9591
9592 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9593 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9594 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9595 addrmap to help ensure it has an accurate map of pc values belonging to
9596 this comp unit. */
9597 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9598
9599 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9600 SECT_OFF_TEXT (objfile),
9601 0);
9602
9603 if (cust != NULL)
9604 {
9605 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9606
9607 /* Set symtab language to language from DW_AT_language. If the
9608 compilation is from a C file generated by language preprocessors, do
9609 not set the language if it was already deduced by start_subfile. */
9610 if (!(cu->language == language_c
9611 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9612 COMPUNIT_FILETABS (cust)->language = cu->language;
9613
9614 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9615 produce DW_AT_location with location lists but it can be possibly
9616 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9617 there were bugs in prologue debug info, fixed later in GCC-4.5
9618 by "unwind info for epilogues" patch (which is not directly related).
9619
9620 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9621 needed, it would be wrong due to missing DW_AT_producer there.
9622
9623 Still one can confuse GDB by using non-standard GCC compilation
9624 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9625 */
9626 if (cu->has_loclist && gcc_4_minor >= 5)
9627 cust->locations_valid = 1;
9628
9629 if (gcc_4_minor >= 5)
9630 cust->epilogue_unwind_valid = 1;
9631
9632 cust->call_site_htab = cu->call_site_htab;
9633 }
9634
9635 if (dwarf2_per_objfile->using_index)
9636 per_cu->v.quick->compunit_symtab = cust;
9637 else
9638 {
9639 dwarf2_psymtab *pst = per_cu->v.psymtab;
9640 pst->compunit_symtab = cust;
9641 pst->readin = true;
9642 }
9643
9644 /* Push it for inclusion processing later. */
9645 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9646
9647 /* Not needed any more. */
9648 cu->reset_builder ();
9649 }
9650
9651 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9652 already been loaded into memory. */
9653
9654 static void
9655 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9656 enum language pretend_language)
9657 {
9658 struct dwarf2_cu *cu = per_cu->cu;
9659 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9660 struct objfile *objfile = dwarf2_per_objfile->objfile;
9661 struct compunit_symtab *cust;
9662 struct signatured_type *sig_type;
9663
9664 gdb_assert (per_cu->is_debug_types);
9665 sig_type = (struct signatured_type *) per_cu;
9666
9667 /* Clear the list here in case something was left over. */
9668 cu->method_list.clear ();
9669
9670 cu->language = pretend_language;
9671 cu->language_defn = language_def (cu->language);
9672
9673 /* The symbol tables are set up in read_type_unit_scope. */
9674 process_die (cu->dies, cu);
9675
9676 /* For now fudge the Go package. */
9677 if (cu->language == language_go)
9678 fixup_go_packaging (cu);
9679
9680 /* Now that we have processed all the DIEs in the CU, all the types
9681 should be complete, and it should now be safe to compute all of the
9682 physnames. */
9683 compute_delayed_physnames (cu);
9684
9685 if (cu->language == language_rust)
9686 rust_union_quirks (cu);
9687
9688 /* TUs share symbol tables.
9689 If this is the first TU to use this symtab, complete the construction
9690 of it with end_expandable_symtab. Otherwise, complete the addition of
9691 this TU's symbols to the existing symtab. */
9692 if (sig_type->type_unit_group->compunit_symtab == NULL)
9693 {
9694 buildsym_compunit *builder = cu->get_builder ();
9695 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9696 sig_type->type_unit_group->compunit_symtab = cust;
9697
9698 if (cust != NULL)
9699 {
9700 /* Set symtab language to language from DW_AT_language. If the
9701 compilation is from a C file generated by language preprocessors,
9702 do not set the language if it was already deduced by
9703 start_subfile. */
9704 if (!(cu->language == language_c
9705 && COMPUNIT_FILETABS (cust)->language != language_c))
9706 COMPUNIT_FILETABS (cust)->language = cu->language;
9707 }
9708 }
9709 else
9710 {
9711 cu->get_builder ()->augment_type_symtab ();
9712 cust = sig_type->type_unit_group->compunit_symtab;
9713 }
9714
9715 if (dwarf2_per_objfile->using_index)
9716 per_cu->v.quick->compunit_symtab = cust;
9717 else
9718 {
9719 dwarf2_psymtab *pst = per_cu->v.psymtab;
9720 pst->compunit_symtab = cust;
9721 pst->readin = true;
9722 }
9723
9724 /* Not needed any more. */
9725 cu->reset_builder ();
9726 }
9727
9728 /* Process an imported unit DIE. */
9729
9730 static void
9731 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9732 {
9733 struct attribute *attr;
9734
9735 /* For now we don't handle imported units in type units. */
9736 if (cu->per_cu->is_debug_types)
9737 {
9738 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9739 " supported in type units [in module %s]"),
9740 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9741 }
9742
9743 attr = dwarf2_attr (die, DW_AT_import, cu);
9744 if (attr != NULL)
9745 {
9746 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9747 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9748 dwarf2_per_cu_data *per_cu
9749 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9750 cu->per_cu->dwarf2_per_objfile);
9751
9752 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9753 into another compilation unit, at root level. Regard this as a hint,
9754 and ignore it. */
9755 if (die->parent && die->parent->parent == NULL
9756 && per_cu->unit_type == DW_UT_compile
9757 && per_cu->lang == language_cplus)
9758 return;
9759
9760 /* If necessary, add it to the queue and load its DIEs. */
9761 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9762 load_full_comp_unit (per_cu, false, cu->language);
9763
9764 cu->per_cu->imported_symtabs_push (per_cu);
9765 }
9766 }
9767
9768 /* RAII object that represents a process_die scope: i.e.,
9769 starts/finishes processing a DIE. */
9770 class process_die_scope
9771 {
9772 public:
9773 process_die_scope (die_info *die, dwarf2_cu *cu)
9774 : m_die (die), m_cu (cu)
9775 {
9776 /* We should only be processing DIEs not already in process. */
9777 gdb_assert (!m_die->in_process);
9778 m_die->in_process = true;
9779 }
9780
9781 ~process_die_scope ()
9782 {
9783 m_die->in_process = false;
9784
9785 /* If we're done processing the DIE for the CU that owns the line
9786 header, we don't need the line header anymore. */
9787 if (m_cu->line_header_die_owner == m_die)
9788 {
9789 delete m_cu->line_header;
9790 m_cu->line_header = NULL;
9791 m_cu->line_header_die_owner = NULL;
9792 }
9793 }
9794
9795 private:
9796 die_info *m_die;
9797 dwarf2_cu *m_cu;
9798 };
9799
9800 /* Process a die and its children. */
9801
9802 static void
9803 process_die (struct die_info *die, struct dwarf2_cu *cu)
9804 {
9805 process_die_scope scope (die, cu);
9806
9807 switch (die->tag)
9808 {
9809 case DW_TAG_padding:
9810 break;
9811 case DW_TAG_compile_unit:
9812 case DW_TAG_partial_unit:
9813 read_file_scope (die, cu);
9814 break;
9815 case DW_TAG_type_unit:
9816 read_type_unit_scope (die, cu);
9817 break;
9818 case DW_TAG_subprogram:
9819 /* Nested subprograms in Fortran get a prefix. */
9820 if (cu->language == language_fortran
9821 && die->parent != NULL
9822 && die->parent->tag == DW_TAG_subprogram)
9823 cu->processing_has_namespace_info = true;
9824 /* Fall through. */
9825 case DW_TAG_inlined_subroutine:
9826 read_func_scope (die, cu);
9827 break;
9828 case DW_TAG_lexical_block:
9829 case DW_TAG_try_block:
9830 case DW_TAG_catch_block:
9831 read_lexical_block_scope (die, cu);
9832 break;
9833 case DW_TAG_call_site:
9834 case DW_TAG_GNU_call_site:
9835 read_call_site_scope (die, cu);
9836 break;
9837 case DW_TAG_class_type:
9838 case DW_TAG_interface_type:
9839 case DW_TAG_structure_type:
9840 case DW_TAG_union_type:
9841 process_structure_scope (die, cu);
9842 break;
9843 case DW_TAG_enumeration_type:
9844 process_enumeration_scope (die, cu);
9845 break;
9846
9847 /* These dies have a type, but processing them does not create
9848 a symbol or recurse to process the children. Therefore we can
9849 read them on-demand through read_type_die. */
9850 case DW_TAG_subroutine_type:
9851 case DW_TAG_set_type:
9852 case DW_TAG_array_type:
9853 case DW_TAG_pointer_type:
9854 case DW_TAG_ptr_to_member_type:
9855 case DW_TAG_reference_type:
9856 case DW_TAG_rvalue_reference_type:
9857 case DW_TAG_string_type:
9858 break;
9859
9860 case DW_TAG_base_type:
9861 case DW_TAG_subrange_type:
9862 case DW_TAG_typedef:
9863 /* Add a typedef symbol for the type definition, if it has a
9864 DW_AT_name. */
9865 new_symbol (die, read_type_die (die, cu), cu);
9866 break;
9867 case DW_TAG_common_block:
9868 read_common_block (die, cu);
9869 break;
9870 case DW_TAG_common_inclusion:
9871 break;
9872 case DW_TAG_namespace:
9873 cu->processing_has_namespace_info = true;
9874 read_namespace (die, cu);
9875 break;
9876 case DW_TAG_module:
9877 cu->processing_has_namespace_info = true;
9878 read_module (die, cu);
9879 break;
9880 case DW_TAG_imported_declaration:
9881 cu->processing_has_namespace_info = true;
9882 if (read_namespace_alias (die, cu))
9883 break;
9884 /* The declaration is not a global namespace alias. */
9885 /* Fall through. */
9886 case DW_TAG_imported_module:
9887 cu->processing_has_namespace_info = true;
9888 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9889 || cu->language != language_fortran))
9890 complaint (_("Tag '%s' has unexpected children"),
9891 dwarf_tag_name (die->tag));
9892 read_import_statement (die, cu);
9893 break;
9894
9895 case DW_TAG_imported_unit:
9896 process_imported_unit_die (die, cu);
9897 break;
9898
9899 case DW_TAG_variable:
9900 read_variable (die, cu);
9901 break;
9902
9903 default:
9904 new_symbol (die, NULL, cu);
9905 break;
9906 }
9907 }
9908 \f
9909 /* DWARF name computation. */
9910
9911 /* A helper function for dwarf2_compute_name which determines whether DIE
9912 needs to have the name of the scope prepended to the name listed in the
9913 die. */
9914
9915 static int
9916 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9917 {
9918 struct attribute *attr;
9919
9920 switch (die->tag)
9921 {
9922 case DW_TAG_namespace:
9923 case DW_TAG_typedef:
9924 case DW_TAG_class_type:
9925 case DW_TAG_interface_type:
9926 case DW_TAG_structure_type:
9927 case DW_TAG_union_type:
9928 case DW_TAG_enumeration_type:
9929 case DW_TAG_enumerator:
9930 case DW_TAG_subprogram:
9931 case DW_TAG_inlined_subroutine:
9932 case DW_TAG_member:
9933 case DW_TAG_imported_declaration:
9934 return 1;
9935
9936 case DW_TAG_variable:
9937 case DW_TAG_constant:
9938 /* We only need to prefix "globally" visible variables. These include
9939 any variable marked with DW_AT_external or any variable that
9940 lives in a namespace. [Variables in anonymous namespaces
9941 require prefixing, but they are not DW_AT_external.] */
9942
9943 if (dwarf2_attr (die, DW_AT_specification, cu))
9944 {
9945 struct dwarf2_cu *spec_cu = cu;
9946
9947 return die_needs_namespace (die_specification (die, &spec_cu),
9948 spec_cu);
9949 }
9950
9951 attr = dwarf2_attr (die, DW_AT_external, cu);
9952 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9953 && die->parent->tag != DW_TAG_module)
9954 return 0;
9955 /* A variable in a lexical block of some kind does not need a
9956 namespace, even though in C++ such variables may be external
9957 and have a mangled name. */
9958 if (die->parent->tag == DW_TAG_lexical_block
9959 || die->parent->tag == DW_TAG_try_block
9960 || die->parent->tag == DW_TAG_catch_block
9961 || die->parent->tag == DW_TAG_subprogram)
9962 return 0;
9963 return 1;
9964
9965 default:
9966 return 0;
9967 }
9968 }
9969
9970 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9971 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9972 defined for the given DIE. */
9973
9974 static struct attribute *
9975 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9976 {
9977 struct attribute *attr;
9978
9979 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9980 if (attr == NULL)
9981 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9982
9983 return attr;
9984 }
9985
9986 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9987 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9988 defined for the given DIE. */
9989
9990 static const char *
9991 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9992 {
9993 const char *linkage_name;
9994
9995 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9996 if (linkage_name == NULL)
9997 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9998
9999 return linkage_name;
10000 }
10001
10002 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10003 compute the physname for the object, which include a method's:
10004 - formal parameters (C++),
10005 - receiver type (Go),
10006
10007 The term "physname" is a bit confusing.
10008 For C++, for example, it is the demangled name.
10009 For Go, for example, it's the mangled name.
10010
10011 For Ada, return the DIE's linkage name rather than the fully qualified
10012 name. PHYSNAME is ignored..
10013
10014 The result is allocated on the objfile_obstack and canonicalized. */
10015
10016 static const char *
10017 dwarf2_compute_name (const char *name,
10018 struct die_info *die, struct dwarf2_cu *cu,
10019 int physname)
10020 {
10021 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10022
10023 if (name == NULL)
10024 name = dwarf2_name (die, cu);
10025
10026 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10027 but otherwise compute it by typename_concat inside GDB.
10028 FIXME: Actually this is not really true, or at least not always true.
10029 It's all very confusing. compute_and_set_names doesn't try to demangle
10030 Fortran names because there is no mangling standard. So new_symbol
10031 will set the demangled name to the result of dwarf2_full_name, and it is
10032 the demangled name that GDB uses if it exists. */
10033 if (cu->language == language_ada
10034 || (cu->language == language_fortran && physname))
10035 {
10036 /* For Ada unit, we prefer the linkage name over the name, as
10037 the former contains the exported name, which the user expects
10038 to be able to reference. Ideally, we want the user to be able
10039 to reference this entity using either natural or linkage name,
10040 but we haven't started looking at this enhancement yet. */
10041 const char *linkage_name = dw2_linkage_name (die, cu);
10042
10043 if (linkage_name != NULL)
10044 return linkage_name;
10045 }
10046
10047 /* These are the only languages we know how to qualify names in. */
10048 if (name != NULL
10049 && (cu->language == language_cplus
10050 || cu->language == language_fortran || cu->language == language_d
10051 || cu->language == language_rust))
10052 {
10053 if (die_needs_namespace (die, cu))
10054 {
10055 const char *prefix;
10056 const char *canonical_name = NULL;
10057
10058 string_file buf;
10059
10060 prefix = determine_prefix (die, cu);
10061 if (*prefix != '\0')
10062 {
10063 gdb::unique_xmalloc_ptr<char> prefixed_name
10064 (typename_concat (NULL, prefix, name, physname, cu));
10065
10066 buf.puts (prefixed_name.get ());
10067 }
10068 else
10069 buf.puts (name);
10070
10071 /* Template parameters may be specified in the DIE's DW_AT_name, or
10072 as children with DW_TAG_template_type_param or
10073 DW_TAG_value_type_param. If the latter, add them to the name
10074 here. If the name already has template parameters, then
10075 skip this step; some versions of GCC emit both, and
10076 it is more efficient to use the pre-computed name.
10077
10078 Something to keep in mind about this process: it is very
10079 unlikely, or in some cases downright impossible, to produce
10080 something that will match the mangled name of a function.
10081 If the definition of the function has the same debug info,
10082 we should be able to match up with it anyway. But fallbacks
10083 using the minimal symbol, for instance to find a method
10084 implemented in a stripped copy of libstdc++, will not work.
10085 If we do not have debug info for the definition, we will have to
10086 match them up some other way.
10087
10088 When we do name matching there is a related problem with function
10089 templates; two instantiated function templates are allowed to
10090 differ only by their return types, which we do not add here. */
10091
10092 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10093 {
10094 struct attribute *attr;
10095 struct die_info *child;
10096 int first = 1;
10097
10098 die->building_fullname = 1;
10099
10100 for (child = die->child; child != NULL; child = child->sibling)
10101 {
10102 struct type *type;
10103 LONGEST value;
10104 const gdb_byte *bytes;
10105 struct dwarf2_locexpr_baton *baton;
10106 struct value *v;
10107
10108 if (child->tag != DW_TAG_template_type_param
10109 && child->tag != DW_TAG_template_value_param)
10110 continue;
10111
10112 if (first)
10113 {
10114 buf.puts ("<");
10115 first = 0;
10116 }
10117 else
10118 buf.puts (", ");
10119
10120 attr = dwarf2_attr (child, DW_AT_type, cu);
10121 if (attr == NULL)
10122 {
10123 complaint (_("template parameter missing DW_AT_type"));
10124 buf.puts ("UNKNOWN_TYPE");
10125 continue;
10126 }
10127 type = die_type (child, cu);
10128
10129 if (child->tag == DW_TAG_template_type_param)
10130 {
10131 c_print_type (type, "", &buf, -1, 0, cu->language,
10132 &type_print_raw_options);
10133 continue;
10134 }
10135
10136 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10137 if (attr == NULL)
10138 {
10139 complaint (_("template parameter missing "
10140 "DW_AT_const_value"));
10141 buf.puts ("UNKNOWN_VALUE");
10142 continue;
10143 }
10144
10145 dwarf2_const_value_attr (attr, type, name,
10146 &cu->comp_unit_obstack, cu,
10147 &value, &bytes, &baton);
10148
10149 if (TYPE_NOSIGN (type))
10150 /* GDB prints characters as NUMBER 'CHAR'. If that's
10151 changed, this can use value_print instead. */
10152 c_printchar (value, type, &buf);
10153 else
10154 {
10155 struct value_print_options opts;
10156
10157 if (baton != NULL)
10158 v = dwarf2_evaluate_loc_desc (type, NULL,
10159 baton->data,
10160 baton->size,
10161 baton->per_cu);
10162 else if (bytes != NULL)
10163 {
10164 v = allocate_value (type);
10165 memcpy (value_contents_writeable (v), bytes,
10166 TYPE_LENGTH (type));
10167 }
10168 else
10169 v = value_from_longest (type, value);
10170
10171 /* Specify decimal so that we do not depend on
10172 the radix. */
10173 get_formatted_print_options (&opts, 'd');
10174 opts.raw = 1;
10175 value_print (v, &buf, &opts);
10176 release_value (v);
10177 }
10178 }
10179
10180 die->building_fullname = 0;
10181
10182 if (!first)
10183 {
10184 /* Close the argument list, with a space if necessary
10185 (nested templates). */
10186 if (!buf.empty () && buf.string ().back () == '>')
10187 buf.puts (" >");
10188 else
10189 buf.puts (">");
10190 }
10191 }
10192
10193 /* For C++ methods, append formal parameter type
10194 information, if PHYSNAME. */
10195
10196 if (physname && die->tag == DW_TAG_subprogram
10197 && cu->language == language_cplus)
10198 {
10199 struct type *type = read_type_die (die, cu);
10200
10201 c_type_print_args (type, &buf, 1, cu->language,
10202 &type_print_raw_options);
10203
10204 if (cu->language == language_cplus)
10205 {
10206 /* Assume that an artificial first parameter is
10207 "this", but do not crash if it is not. RealView
10208 marks unnamed (and thus unused) parameters as
10209 artificial; there is no way to differentiate
10210 the two cases. */
10211 if (TYPE_NFIELDS (type) > 0
10212 && TYPE_FIELD_ARTIFICIAL (type, 0)
10213 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10214 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10215 0))))
10216 buf.puts (" const");
10217 }
10218 }
10219
10220 const std::string &intermediate_name = buf.string ();
10221
10222 if (cu->language == language_cplus)
10223 canonical_name
10224 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10225 objfile);
10226
10227 /* If we only computed INTERMEDIATE_NAME, or if
10228 INTERMEDIATE_NAME is already canonical, then we need to
10229 intern it. */
10230 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10231 name = objfile->intern (intermediate_name);
10232 else
10233 name = canonical_name;
10234 }
10235 }
10236
10237 return name;
10238 }
10239
10240 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10241 If scope qualifiers are appropriate they will be added. The result
10242 will be allocated on the storage_obstack, or NULL if the DIE does
10243 not have a name. NAME may either be from a previous call to
10244 dwarf2_name or NULL.
10245
10246 The output string will be canonicalized (if C++). */
10247
10248 static const char *
10249 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10250 {
10251 return dwarf2_compute_name (name, die, cu, 0);
10252 }
10253
10254 /* Construct a physname for the given DIE in CU. NAME may either be
10255 from a previous call to dwarf2_name or NULL. The result will be
10256 allocated on the objfile_objstack or NULL if the DIE does not have a
10257 name.
10258
10259 The output string will be canonicalized (if C++). */
10260
10261 static const char *
10262 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10263 {
10264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10265 const char *retval, *mangled = NULL, *canon = NULL;
10266 int need_copy = 1;
10267
10268 /* In this case dwarf2_compute_name is just a shortcut not building anything
10269 on its own. */
10270 if (!die_needs_namespace (die, cu))
10271 return dwarf2_compute_name (name, die, cu, 1);
10272
10273 mangled = dw2_linkage_name (die, cu);
10274
10275 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10276 See https://github.com/rust-lang/rust/issues/32925. */
10277 if (cu->language == language_rust && mangled != NULL
10278 && strchr (mangled, '{') != NULL)
10279 mangled = NULL;
10280
10281 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10282 has computed. */
10283 gdb::unique_xmalloc_ptr<char> demangled;
10284 if (mangled != NULL)
10285 {
10286
10287 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10288 {
10289 /* Do nothing (do not demangle the symbol name). */
10290 }
10291 else if (cu->language == language_go)
10292 {
10293 /* This is a lie, but we already lie to the caller new_symbol.
10294 new_symbol assumes we return the mangled name.
10295 This just undoes that lie until things are cleaned up. */
10296 }
10297 else
10298 {
10299 /* Use DMGL_RET_DROP for C++ template functions to suppress
10300 their return type. It is easier for GDB users to search
10301 for such functions as `name(params)' than `long name(params)'.
10302 In such case the minimal symbol names do not match the full
10303 symbol names but for template functions there is never a need
10304 to look up their definition from their declaration so
10305 the only disadvantage remains the minimal symbol variant
10306 `long name(params)' does not have the proper inferior type. */
10307 demangled.reset (gdb_demangle (mangled,
10308 (DMGL_PARAMS | DMGL_ANSI
10309 | DMGL_RET_DROP)));
10310 }
10311 if (demangled)
10312 canon = demangled.get ();
10313 else
10314 {
10315 canon = mangled;
10316 need_copy = 0;
10317 }
10318 }
10319
10320 if (canon == NULL || check_physname)
10321 {
10322 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10323
10324 if (canon != NULL && strcmp (physname, canon) != 0)
10325 {
10326 /* It may not mean a bug in GDB. The compiler could also
10327 compute DW_AT_linkage_name incorrectly. But in such case
10328 GDB would need to be bug-to-bug compatible. */
10329
10330 complaint (_("Computed physname <%s> does not match demangled <%s> "
10331 "(from linkage <%s>) - DIE at %s [in module %s]"),
10332 physname, canon, mangled, sect_offset_str (die->sect_off),
10333 objfile_name (objfile));
10334
10335 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10336 is available here - over computed PHYSNAME. It is safer
10337 against both buggy GDB and buggy compilers. */
10338
10339 retval = canon;
10340 }
10341 else
10342 {
10343 retval = physname;
10344 need_copy = 0;
10345 }
10346 }
10347 else
10348 retval = canon;
10349
10350 if (need_copy)
10351 retval = objfile->intern (retval);
10352
10353 return retval;
10354 }
10355
10356 /* Inspect DIE in CU for a namespace alias. If one exists, record
10357 a new symbol for it.
10358
10359 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10360
10361 static int
10362 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10363 {
10364 struct attribute *attr;
10365
10366 /* If the die does not have a name, this is not a namespace
10367 alias. */
10368 attr = dwarf2_attr (die, DW_AT_name, cu);
10369 if (attr != NULL)
10370 {
10371 int num;
10372 struct die_info *d = die;
10373 struct dwarf2_cu *imported_cu = cu;
10374
10375 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10376 keep inspecting DIEs until we hit the underlying import. */
10377 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10378 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10379 {
10380 attr = dwarf2_attr (d, DW_AT_import, cu);
10381 if (attr == NULL)
10382 break;
10383
10384 d = follow_die_ref (d, attr, &imported_cu);
10385 if (d->tag != DW_TAG_imported_declaration)
10386 break;
10387 }
10388
10389 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10390 {
10391 complaint (_("DIE at %s has too many recursively imported "
10392 "declarations"), sect_offset_str (d->sect_off));
10393 return 0;
10394 }
10395
10396 if (attr != NULL)
10397 {
10398 struct type *type;
10399 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10400
10401 type = get_die_type_at_offset (sect_off, cu->per_cu);
10402 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10403 {
10404 /* This declaration is a global namespace alias. Add
10405 a symbol for it whose type is the aliased namespace. */
10406 new_symbol (die, type, cu);
10407 return 1;
10408 }
10409 }
10410 }
10411
10412 return 0;
10413 }
10414
10415 /* Return the using directives repository (global or local?) to use in the
10416 current context for CU.
10417
10418 For Ada, imported declarations can materialize renamings, which *may* be
10419 global. However it is impossible (for now?) in DWARF to distinguish
10420 "external" imported declarations and "static" ones. As all imported
10421 declarations seem to be static in all other languages, make them all CU-wide
10422 global only in Ada. */
10423
10424 static struct using_direct **
10425 using_directives (struct dwarf2_cu *cu)
10426 {
10427 if (cu->language == language_ada
10428 && cu->get_builder ()->outermost_context_p ())
10429 return cu->get_builder ()->get_global_using_directives ();
10430 else
10431 return cu->get_builder ()->get_local_using_directives ();
10432 }
10433
10434 /* Read the import statement specified by the given die and record it. */
10435
10436 static void
10437 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10438 {
10439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10440 struct attribute *import_attr;
10441 struct die_info *imported_die, *child_die;
10442 struct dwarf2_cu *imported_cu;
10443 const char *imported_name;
10444 const char *imported_name_prefix;
10445 const char *canonical_name;
10446 const char *import_alias;
10447 const char *imported_declaration = NULL;
10448 const char *import_prefix;
10449 std::vector<const char *> excludes;
10450
10451 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10452 if (import_attr == NULL)
10453 {
10454 complaint (_("Tag '%s' has no DW_AT_import"),
10455 dwarf_tag_name (die->tag));
10456 return;
10457 }
10458
10459 imported_cu = cu;
10460 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10461 imported_name = dwarf2_name (imported_die, imported_cu);
10462 if (imported_name == NULL)
10463 {
10464 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10465
10466 The import in the following code:
10467 namespace A
10468 {
10469 typedef int B;
10470 }
10471
10472 int main ()
10473 {
10474 using A::B;
10475 B b;
10476 return b;
10477 }
10478
10479 ...
10480 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10481 <52> DW_AT_decl_file : 1
10482 <53> DW_AT_decl_line : 6
10483 <54> DW_AT_import : <0x75>
10484 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10485 <59> DW_AT_name : B
10486 <5b> DW_AT_decl_file : 1
10487 <5c> DW_AT_decl_line : 2
10488 <5d> DW_AT_type : <0x6e>
10489 ...
10490 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10491 <76> DW_AT_byte_size : 4
10492 <77> DW_AT_encoding : 5 (signed)
10493
10494 imports the wrong die ( 0x75 instead of 0x58 ).
10495 This case will be ignored until the gcc bug is fixed. */
10496 return;
10497 }
10498
10499 /* Figure out the local name after import. */
10500 import_alias = dwarf2_name (die, cu);
10501
10502 /* Figure out where the statement is being imported to. */
10503 import_prefix = determine_prefix (die, cu);
10504
10505 /* Figure out what the scope of the imported die is and prepend it
10506 to the name of the imported die. */
10507 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10508
10509 if (imported_die->tag != DW_TAG_namespace
10510 && imported_die->tag != DW_TAG_module)
10511 {
10512 imported_declaration = imported_name;
10513 canonical_name = imported_name_prefix;
10514 }
10515 else if (strlen (imported_name_prefix) > 0)
10516 canonical_name = obconcat (&objfile->objfile_obstack,
10517 imported_name_prefix,
10518 (cu->language == language_d ? "." : "::"),
10519 imported_name, (char *) NULL);
10520 else
10521 canonical_name = imported_name;
10522
10523 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10524 for (child_die = die->child; child_die && child_die->tag;
10525 child_die = sibling_die (child_die))
10526 {
10527 /* DWARF-4: A Fortran use statement with a “rename list” may be
10528 represented by an imported module entry with an import attribute
10529 referring to the module and owned entries corresponding to those
10530 entities that are renamed as part of being imported. */
10531
10532 if (child_die->tag != DW_TAG_imported_declaration)
10533 {
10534 complaint (_("child DW_TAG_imported_declaration expected "
10535 "- DIE at %s [in module %s]"),
10536 sect_offset_str (child_die->sect_off),
10537 objfile_name (objfile));
10538 continue;
10539 }
10540
10541 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10542 if (import_attr == NULL)
10543 {
10544 complaint (_("Tag '%s' has no DW_AT_import"),
10545 dwarf_tag_name (child_die->tag));
10546 continue;
10547 }
10548
10549 imported_cu = cu;
10550 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10551 &imported_cu);
10552 imported_name = dwarf2_name (imported_die, imported_cu);
10553 if (imported_name == NULL)
10554 {
10555 complaint (_("child DW_TAG_imported_declaration has unknown "
10556 "imported name - DIE at %s [in module %s]"),
10557 sect_offset_str (child_die->sect_off),
10558 objfile_name (objfile));
10559 continue;
10560 }
10561
10562 excludes.push_back (imported_name);
10563
10564 process_die (child_die, cu);
10565 }
10566
10567 add_using_directive (using_directives (cu),
10568 import_prefix,
10569 canonical_name,
10570 import_alias,
10571 imported_declaration,
10572 excludes,
10573 0,
10574 &objfile->objfile_obstack);
10575 }
10576
10577 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10578 types, but gives them a size of zero. Starting with version 14,
10579 ICC is compatible with GCC. */
10580
10581 static bool
10582 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10583 {
10584 if (!cu->checked_producer)
10585 check_producer (cu);
10586
10587 return cu->producer_is_icc_lt_14;
10588 }
10589
10590 /* ICC generates a DW_AT_type for C void functions. This was observed on
10591 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10592 which says that void functions should not have a DW_AT_type. */
10593
10594 static bool
10595 producer_is_icc (struct dwarf2_cu *cu)
10596 {
10597 if (!cu->checked_producer)
10598 check_producer (cu);
10599
10600 return cu->producer_is_icc;
10601 }
10602
10603 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10604 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10605 this, it was first present in GCC release 4.3.0. */
10606
10607 static bool
10608 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10609 {
10610 if (!cu->checked_producer)
10611 check_producer (cu);
10612
10613 return cu->producer_is_gcc_lt_4_3;
10614 }
10615
10616 static file_and_directory
10617 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10618 {
10619 file_and_directory res;
10620
10621 /* Find the filename. Do not use dwarf2_name here, since the filename
10622 is not a source language identifier. */
10623 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10624 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10625
10626 if (res.comp_dir == NULL
10627 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10628 && IS_ABSOLUTE_PATH (res.name))
10629 {
10630 res.comp_dir_storage = ldirname (res.name);
10631 if (!res.comp_dir_storage.empty ())
10632 res.comp_dir = res.comp_dir_storage.c_str ();
10633 }
10634 if (res.comp_dir != NULL)
10635 {
10636 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10637 directory, get rid of it. */
10638 const char *cp = strchr (res.comp_dir, ':');
10639
10640 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10641 res.comp_dir = cp + 1;
10642 }
10643
10644 if (res.name == NULL)
10645 res.name = "<unknown>";
10646
10647 return res;
10648 }
10649
10650 /* Handle DW_AT_stmt_list for a compilation unit.
10651 DIE is the DW_TAG_compile_unit die for CU.
10652 COMP_DIR is the compilation directory. LOWPC is passed to
10653 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10654
10655 static void
10656 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10657 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10658 {
10659 struct dwarf2_per_objfile *dwarf2_per_objfile
10660 = cu->per_cu->dwarf2_per_objfile;
10661 struct attribute *attr;
10662 struct line_header line_header_local;
10663 hashval_t line_header_local_hash;
10664 void **slot;
10665 int decode_mapping;
10666
10667 gdb_assert (! cu->per_cu->is_debug_types);
10668
10669 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10670 if (attr == NULL)
10671 return;
10672
10673 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10674
10675 /* The line header hash table is only created if needed (it exists to
10676 prevent redundant reading of the line table for partial_units).
10677 If we're given a partial_unit, we'll need it. If we're given a
10678 compile_unit, then use the line header hash table if it's already
10679 created, but don't create one just yet. */
10680
10681 if (dwarf2_per_objfile->line_header_hash == NULL
10682 && die->tag == DW_TAG_partial_unit)
10683 {
10684 dwarf2_per_objfile->line_header_hash
10685 .reset (htab_create_alloc (127, line_header_hash_voidp,
10686 line_header_eq_voidp,
10687 free_line_header_voidp,
10688 xcalloc, xfree));
10689 }
10690
10691 line_header_local.sect_off = line_offset;
10692 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10693 line_header_local_hash = line_header_hash (&line_header_local);
10694 if (dwarf2_per_objfile->line_header_hash != NULL)
10695 {
10696 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10697 &line_header_local,
10698 line_header_local_hash, NO_INSERT);
10699
10700 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10701 is not present in *SLOT (since if there is something in *SLOT then
10702 it will be for a partial_unit). */
10703 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10704 {
10705 gdb_assert (*slot != NULL);
10706 cu->line_header = (struct line_header *) *slot;
10707 return;
10708 }
10709 }
10710
10711 /* dwarf_decode_line_header does not yet provide sufficient information.
10712 We always have to call also dwarf_decode_lines for it. */
10713 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10714 if (lh == NULL)
10715 return;
10716
10717 cu->line_header = lh.release ();
10718 cu->line_header_die_owner = die;
10719
10720 if (dwarf2_per_objfile->line_header_hash == NULL)
10721 slot = NULL;
10722 else
10723 {
10724 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10725 &line_header_local,
10726 line_header_local_hash, INSERT);
10727 gdb_assert (slot != NULL);
10728 }
10729 if (slot != NULL && *slot == NULL)
10730 {
10731 /* This newly decoded line number information unit will be owned
10732 by line_header_hash hash table. */
10733 *slot = cu->line_header;
10734 cu->line_header_die_owner = NULL;
10735 }
10736 else
10737 {
10738 /* We cannot free any current entry in (*slot) as that struct line_header
10739 may be already used by multiple CUs. Create only temporary decoded
10740 line_header for this CU - it may happen at most once for each line
10741 number information unit. And if we're not using line_header_hash
10742 then this is what we want as well. */
10743 gdb_assert (die->tag != DW_TAG_partial_unit);
10744 }
10745 decode_mapping = (die->tag != DW_TAG_partial_unit);
10746 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10747 decode_mapping);
10748
10749 }
10750
10751 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10752
10753 static void
10754 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10755 {
10756 struct dwarf2_per_objfile *dwarf2_per_objfile
10757 = cu->per_cu->dwarf2_per_objfile;
10758 struct objfile *objfile = dwarf2_per_objfile->objfile;
10759 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10760 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10761 CORE_ADDR highpc = ((CORE_ADDR) 0);
10762 struct attribute *attr;
10763 struct die_info *child_die;
10764 CORE_ADDR baseaddr;
10765
10766 prepare_one_comp_unit (cu, die, cu->language);
10767 baseaddr = objfile->text_section_offset ();
10768
10769 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10770
10771 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10772 from finish_block. */
10773 if (lowpc == ((CORE_ADDR) -1))
10774 lowpc = highpc;
10775 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10776
10777 file_and_directory fnd = find_file_and_directory (die, cu);
10778
10779 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10780 standardised yet. As a workaround for the language detection we fall
10781 back to the DW_AT_producer string. */
10782 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10783 cu->language = language_opencl;
10784
10785 /* Similar hack for Go. */
10786 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10787 set_cu_language (DW_LANG_Go, cu);
10788
10789 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10790
10791 /* Decode line number information if present. We do this before
10792 processing child DIEs, so that the line header table is available
10793 for DW_AT_decl_file. */
10794 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10795
10796 /* Process all dies in compilation unit. */
10797 if (die->child != NULL)
10798 {
10799 child_die = die->child;
10800 while (child_die && child_die->tag)
10801 {
10802 process_die (child_die, cu);
10803 child_die = sibling_die (child_die);
10804 }
10805 }
10806
10807 /* Decode macro information, if present. Dwarf 2 macro information
10808 refers to information in the line number info statement program
10809 header, so we can only read it if we've read the header
10810 successfully. */
10811 attr = dwarf2_attr (die, DW_AT_macros, cu);
10812 if (attr == NULL)
10813 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10814 if (attr && cu->line_header)
10815 {
10816 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10817 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10818
10819 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10820 }
10821 else
10822 {
10823 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10824 if (attr && cu->line_header)
10825 {
10826 unsigned int macro_offset = DW_UNSND (attr);
10827
10828 dwarf_decode_macros (cu, macro_offset, 0);
10829 }
10830 }
10831 }
10832
10833 void
10834 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10835 {
10836 struct type_unit_group *tu_group;
10837 int first_time;
10838 struct attribute *attr;
10839 unsigned int i;
10840 struct signatured_type *sig_type;
10841
10842 gdb_assert (per_cu->is_debug_types);
10843 sig_type = (struct signatured_type *) per_cu;
10844
10845 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10846
10847 /* If we're using .gdb_index (includes -readnow) then
10848 per_cu->type_unit_group may not have been set up yet. */
10849 if (sig_type->type_unit_group == NULL)
10850 sig_type->type_unit_group = get_type_unit_group (this, attr);
10851 tu_group = sig_type->type_unit_group;
10852
10853 /* If we've already processed this stmt_list there's no real need to
10854 do it again, we could fake it and just recreate the part we need
10855 (file name,index -> symtab mapping). If data shows this optimization
10856 is useful we can do it then. */
10857 first_time = tu_group->compunit_symtab == NULL;
10858
10859 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10860 debug info. */
10861 line_header_up lh;
10862 if (attr != NULL)
10863 {
10864 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10865 lh = dwarf_decode_line_header (line_offset, this);
10866 }
10867 if (lh == NULL)
10868 {
10869 if (first_time)
10870 start_symtab ("", NULL, 0);
10871 else
10872 {
10873 gdb_assert (tu_group->symtabs == NULL);
10874 gdb_assert (m_builder == nullptr);
10875 struct compunit_symtab *cust = tu_group->compunit_symtab;
10876 m_builder.reset (new struct buildsym_compunit
10877 (COMPUNIT_OBJFILE (cust), "",
10878 COMPUNIT_DIRNAME (cust),
10879 compunit_language (cust),
10880 0, cust));
10881 }
10882 return;
10883 }
10884
10885 line_header = lh.release ();
10886 line_header_die_owner = die;
10887
10888 if (first_time)
10889 {
10890 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10891
10892 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10893 still initializing it, and our caller (a few levels up)
10894 process_full_type_unit still needs to know if this is the first
10895 time. */
10896
10897 tu_group->symtabs
10898 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10899 struct symtab *, line_header->file_names_size ());
10900
10901 auto &file_names = line_header->file_names ();
10902 for (i = 0; i < file_names.size (); ++i)
10903 {
10904 file_entry &fe = file_names[i];
10905 dwarf2_start_subfile (this, fe.name,
10906 fe.include_dir (line_header));
10907 buildsym_compunit *b = get_builder ();
10908 if (b->get_current_subfile ()->symtab == NULL)
10909 {
10910 /* NOTE: start_subfile will recognize when it's been
10911 passed a file it has already seen. So we can't
10912 assume there's a simple mapping from
10913 cu->line_header->file_names to subfiles, plus
10914 cu->line_header->file_names may contain dups. */
10915 b->get_current_subfile ()->symtab
10916 = allocate_symtab (cust, b->get_current_subfile ()->name);
10917 }
10918
10919 fe.symtab = b->get_current_subfile ()->symtab;
10920 tu_group->symtabs[i] = fe.symtab;
10921 }
10922 }
10923 else
10924 {
10925 gdb_assert (m_builder == nullptr);
10926 struct compunit_symtab *cust = tu_group->compunit_symtab;
10927 m_builder.reset (new struct buildsym_compunit
10928 (COMPUNIT_OBJFILE (cust), "",
10929 COMPUNIT_DIRNAME (cust),
10930 compunit_language (cust),
10931 0, cust));
10932
10933 auto &file_names = line_header->file_names ();
10934 for (i = 0; i < file_names.size (); ++i)
10935 {
10936 file_entry &fe = file_names[i];
10937 fe.symtab = tu_group->symtabs[i];
10938 }
10939 }
10940
10941 /* The main symtab is allocated last. Type units don't have DW_AT_name
10942 so they don't have a "real" (so to speak) symtab anyway.
10943 There is later code that will assign the main symtab to all symbols
10944 that don't have one. We need to handle the case of a symbol with a
10945 missing symtab (DW_AT_decl_file) anyway. */
10946 }
10947
10948 /* Process DW_TAG_type_unit.
10949 For TUs we want to skip the first top level sibling if it's not the
10950 actual type being defined by this TU. In this case the first top
10951 level sibling is there to provide context only. */
10952
10953 static void
10954 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10955 {
10956 struct die_info *child_die;
10957
10958 prepare_one_comp_unit (cu, die, language_minimal);
10959
10960 /* Initialize (or reinitialize) the machinery for building symtabs.
10961 We do this before processing child DIEs, so that the line header table
10962 is available for DW_AT_decl_file. */
10963 cu->setup_type_unit_groups (die);
10964
10965 if (die->child != NULL)
10966 {
10967 child_die = die->child;
10968 while (child_die && child_die->tag)
10969 {
10970 process_die (child_die, cu);
10971 child_die = sibling_die (child_die);
10972 }
10973 }
10974 }
10975 \f
10976 /* DWO/DWP files.
10977
10978 http://gcc.gnu.org/wiki/DebugFission
10979 http://gcc.gnu.org/wiki/DebugFissionDWP
10980
10981 To simplify handling of both DWO files ("object" files with the DWARF info)
10982 and DWP files (a file with the DWOs packaged up into one file), we treat
10983 DWP files as having a collection of virtual DWO files. */
10984
10985 static hashval_t
10986 hash_dwo_file (const void *item)
10987 {
10988 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10989 hashval_t hash;
10990
10991 hash = htab_hash_string (dwo_file->dwo_name);
10992 if (dwo_file->comp_dir != NULL)
10993 hash += htab_hash_string (dwo_file->comp_dir);
10994 return hash;
10995 }
10996
10997 static int
10998 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10999 {
11000 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11001 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11002
11003 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11004 return 0;
11005 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11006 return lhs->comp_dir == rhs->comp_dir;
11007 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11008 }
11009
11010 /* Allocate a hash table for DWO files. */
11011
11012 static htab_up
11013 allocate_dwo_file_hash_table ()
11014 {
11015 auto delete_dwo_file = [] (void *item)
11016 {
11017 struct dwo_file *dwo_file = (struct dwo_file *) item;
11018
11019 delete dwo_file;
11020 };
11021
11022 return htab_up (htab_create_alloc (41,
11023 hash_dwo_file,
11024 eq_dwo_file,
11025 delete_dwo_file,
11026 xcalloc, xfree));
11027 }
11028
11029 /* Lookup DWO file DWO_NAME. */
11030
11031 static void **
11032 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11033 const char *dwo_name,
11034 const char *comp_dir)
11035 {
11036 struct dwo_file find_entry;
11037 void **slot;
11038
11039 if (dwarf2_per_objfile->dwo_files == NULL)
11040 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11041
11042 find_entry.dwo_name = dwo_name;
11043 find_entry.comp_dir = comp_dir;
11044 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11045 INSERT);
11046
11047 return slot;
11048 }
11049
11050 static hashval_t
11051 hash_dwo_unit (const void *item)
11052 {
11053 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11054
11055 /* This drops the top 32 bits of the id, but is ok for a hash. */
11056 return dwo_unit->signature;
11057 }
11058
11059 static int
11060 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11061 {
11062 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11063 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11064
11065 /* The signature is assumed to be unique within the DWO file.
11066 So while object file CU dwo_id's always have the value zero,
11067 that's OK, assuming each object file DWO file has only one CU,
11068 and that's the rule for now. */
11069 return lhs->signature == rhs->signature;
11070 }
11071
11072 /* Allocate a hash table for DWO CUs,TUs.
11073 There is one of these tables for each of CUs,TUs for each DWO file. */
11074
11075 static htab_up
11076 allocate_dwo_unit_table ()
11077 {
11078 /* Start out with a pretty small number.
11079 Generally DWO files contain only one CU and maybe some TUs. */
11080 return htab_up (htab_create_alloc (3,
11081 hash_dwo_unit,
11082 eq_dwo_unit,
11083 NULL, xcalloc, xfree));
11084 }
11085
11086 /* die_reader_func for create_dwo_cu. */
11087
11088 static void
11089 create_dwo_cu_reader (const struct die_reader_specs *reader,
11090 const gdb_byte *info_ptr,
11091 struct die_info *comp_unit_die,
11092 struct dwo_file *dwo_file,
11093 struct dwo_unit *dwo_unit)
11094 {
11095 struct dwarf2_cu *cu = reader->cu;
11096 sect_offset sect_off = cu->per_cu->sect_off;
11097 struct dwarf2_section_info *section = cu->per_cu->section;
11098
11099 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11100 if (!signature.has_value ())
11101 {
11102 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11103 " its dwo_id [in module %s]"),
11104 sect_offset_str (sect_off), dwo_file->dwo_name);
11105 return;
11106 }
11107
11108 dwo_unit->dwo_file = dwo_file;
11109 dwo_unit->signature = *signature;
11110 dwo_unit->section = section;
11111 dwo_unit->sect_off = sect_off;
11112 dwo_unit->length = cu->per_cu->length;
11113
11114 if (dwarf_read_debug)
11115 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11116 sect_offset_str (sect_off),
11117 hex_string (dwo_unit->signature));
11118 }
11119
11120 /* Create the dwo_units for the CUs in a DWO_FILE.
11121 Note: This function processes DWO files only, not DWP files. */
11122
11123 static void
11124 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11125 dwarf2_cu *cu, struct dwo_file &dwo_file,
11126 dwarf2_section_info &section, htab_up &cus_htab)
11127 {
11128 struct objfile *objfile = dwarf2_per_objfile->objfile;
11129 const gdb_byte *info_ptr, *end_ptr;
11130
11131 section.read (objfile);
11132 info_ptr = section.buffer;
11133
11134 if (info_ptr == NULL)
11135 return;
11136
11137 if (dwarf_read_debug)
11138 {
11139 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11140 section.get_name (),
11141 section.get_file_name ());
11142 }
11143
11144 end_ptr = info_ptr + section.size;
11145 while (info_ptr < end_ptr)
11146 {
11147 struct dwarf2_per_cu_data per_cu;
11148 struct dwo_unit read_unit {};
11149 struct dwo_unit *dwo_unit;
11150 void **slot;
11151 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11152
11153 memset (&per_cu, 0, sizeof (per_cu));
11154 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11155 per_cu.is_debug_types = 0;
11156 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11157 per_cu.section = &section;
11158
11159 cutu_reader reader (&per_cu, cu, &dwo_file);
11160 if (!reader.dummy_p)
11161 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11162 &dwo_file, &read_unit);
11163 info_ptr += per_cu.length;
11164
11165 // If the unit could not be parsed, skip it.
11166 if (read_unit.dwo_file == NULL)
11167 continue;
11168
11169 if (cus_htab == NULL)
11170 cus_htab = allocate_dwo_unit_table ();
11171
11172 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11173 *dwo_unit = read_unit;
11174 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11175 gdb_assert (slot != NULL);
11176 if (*slot != NULL)
11177 {
11178 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11179 sect_offset dup_sect_off = dup_cu->sect_off;
11180
11181 complaint (_("debug cu entry at offset %s is duplicate to"
11182 " the entry at offset %s, signature %s"),
11183 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11184 hex_string (dwo_unit->signature));
11185 }
11186 *slot = (void *)dwo_unit;
11187 }
11188 }
11189
11190 /* DWP file .debug_{cu,tu}_index section format:
11191 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11192
11193 DWP Version 1:
11194
11195 Both index sections have the same format, and serve to map a 64-bit
11196 signature to a set of section numbers. Each section begins with a header,
11197 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11198 indexes, and a pool of 32-bit section numbers. The index sections will be
11199 aligned at 8-byte boundaries in the file.
11200
11201 The index section header consists of:
11202
11203 V, 32 bit version number
11204 -, 32 bits unused
11205 N, 32 bit number of compilation units or type units in the index
11206 M, 32 bit number of slots in the hash table
11207
11208 Numbers are recorded using the byte order of the application binary.
11209
11210 The hash table begins at offset 16 in the section, and consists of an array
11211 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11212 order of the application binary). Unused slots in the hash table are 0.
11213 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11214
11215 The parallel table begins immediately after the hash table
11216 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11217 array of 32-bit indexes (using the byte order of the application binary),
11218 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11219 table contains a 32-bit index into the pool of section numbers. For unused
11220 hash table slots, the corresponding entry in the parallel table will be 0.
11221
11222 The pool of section numbers begins immediately following the hash table
11223 (at offset 16 + 12 * M from the beginning of the section). The pool of
11224 section numbers consists of an array of 32-bit words (using the byte order
11225 of the application binary). Each item in the array is indexed starting
11226 from 0. The hash table entry provides the index of the first section
11227 number in the set. Additional section numbers in the set follow, and the
11228 set is terminated by a 0 entry (section number 0 is not used in ELF).
11229
11230 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11231 section must be the first entry in the set, and the .debug_abbrev.dwo must
11232 be the second entry. Other members of the set may follow in any order.
11233
11234 ---
11235
11236 DWP Version 2:
11237
11238 DWP Version 2 combines all the .debug_info, etc. sections into one,
11239 and the entries in the index tables are now offsets into these sections.
11240 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11241 section.
11242
11243 Index Section Contents:
11244 Header
11245 Hash Table of Signatures dwp_hash_table.hash_table
11246 Parallel Table of Indices dwp_hash_table.unit_table
11247 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11248 Table of Section Sizes dwp_hash_table.v2.sizes
11249
11250 The index section header consists of:
11251
11252 V, 32 bit version number
11253 L, 32 bit number of columns in the table of section offsets
11254 N, 32 bit number of compilation units or type units in the index
11255 M, 32 bit number of slots in the hash table
11256
11257 Numbers are recorded using the byte order of the application binary.
11258
11259 The hash table has the same format as version 1.
11260 The parallel table of indices has the same format as version 1,
11261 except that the entries are origin-1 indices into the table of sections
11262 offsets and the table of section sizes.
11263
11264 The table of offsets begins immediately following the parallel table
11265 (at offset 16 + 12 * M from the beginning of the section). The table is
11266 a two-dimensional array of 32-bit words (using the byte order of the
11267 application binary), with L columns and N+1 rows, in row-major order.
11268 Each row in the array is indexed starting from 0. The first row provides
11269 a key to the remaining rows: each column in this row provides an identifier
11270 for a debug section, and the offsets in the same column of subsequent rows
11271 refer to that section. The section identifiers are:
11272
11273 DW_SECT_INFO 1 .debug_info.dwo
11274 DW_SECT_TYPES 2 .debug_types.dwo
11275 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11276 DW_SECT_LINE 4 .debug_line.dwo
11277 DW_SECT_LOC 5 .debug_loc.dwo
11278 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11279 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11280 DW_SECT_MACRO 8 .debug_macro.dwo
11281
11282 The offsets provided by the CU and TU index sections are the base offsets
11283 for the contributions made by each CU or TU to the corresponding section
11284 in the package file. Each CU and TU header contains an abbrev_offset
11285 field, used to find the abbreviations table for that CU or TU within the
11286 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11287 be interpreted as relative to the base offset given in the index section.
11288 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11289 should be interpreted as relative to the base offset for .debug_line.dwo,
11290 and offsets into other debug sections obtained from DWARF attributes should
11291 also be interpreted as relative to the corresponding base offset.
11292
11293 The table of sizes begins immediately following the table of offsets.
11294 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11295 with L columns and N rows, in row-major order. Each row in the array is
11296 indexed starting from 1 (row 0 is shared by the two tables).
11297
11298 ---
11299
11300 Hash table lookup is handled the same in version 1 and 2:
11301
11302 We assume that N and M will not exceed 2^32 - 1.
11303 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11304
11305 Given a 64-bit compilation unit signature or a type signature S, an entry
11306 in the hash table is located as follows:
11307
11308 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11309 the low-order k bits all set to 1.
11310
11311 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11312
11313 3) If the hash table entry at index H matches the signature, use that
11314 entry. If the hash table entry at index H is unused (all zeroes),
11315 terminate the search: the signature is not present in the table.
11316
11317 4) Let H = (H + H') modulo M. Repeat at Step 3.
11318
11319 Because M > N and H' and M are relatively prime, the search is guaranteed
11320 to stop at an unused slot or find the match. */
11321
11322 /* Create a hash table to map DWO IDs to their CU/TU entry in
11323 .debug_{info,types}.dwo in DWP_FILE.
11324 Returns NULL if there isn't one.
11325 Note: This function processes DWP files only, not DWO files. */
11326
11327 static struct dwp_hash_table *
11328 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11329 struct dwp_file *dwp_file, int is_debug_types)
11330 {
11331 struct objfile *objfile = dwarf2_per_objfile->objfile;
11332 bfd *dbfd = dwp_file->dbfd.get ();
11333 const gdb_byte *index_ptr, *index_end;
11334 struct dwarf2_section_info *index;
11335 uint32_t version, nr_columns, nr_units, nr_slots;
11336 struct dwp_hash_table *htab;
11337
11338 if (is_debug_types)
11339 index = &dwp_file->sections.tu_index;
11340 else
11341 index = &dwp_file->sections.cu_index;
11342
11343 if (index->empty ())
11344 return NULL;
11345 index->read (objfile);
11346
11347 index_ptr = index->buffer;
11348 index_end = index_ptr + index->size;
11349
11350 version = read_4_bytes (dbfd, index_ptr);
11351 index_ptr += 4;
11352 if (version == 2)
11353 nr_columns = read_4_bytes (dbfd, index_ptr);
11354 else
11355 nr_columns = 0;
11356 index_ptr += 4;
11357 nr_units = read_4_bytes (dbfd, index_ptr);
11358 index_ptr += 4;
11359 nr_slots = read_4_bytes (dbfd, index_ptr);
11360 index_ptr += 4;
11361
11362 if (version != 1 && version != 2)
11363 {
11364 error (_("Dwarf Error: unsupported DWP file version (%s)"
11365 " [in module %s]"),
11366 pulongest (version), dwp_file->name);
11367 }
11368 if (nr_slots != (nr_slots & -nr_slots))
11369 {
11370 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11371 " is not power of 2 [in module %s]"),
11372 pulongest (nr_slots), dwp_file->name);
11373 }
11374
11375 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11376 htab->version = version;
11377 htab->nr_columns = nr_columns;
11378 htab->nr_units = nr_units;
11379 htab->nr_slots = nr_slots;
11380 htab->hash_table = index_ptr;
11381 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11382
11383 /* Exit early if the table is empty. */
11384 if (nr_slots == 0 || nr_units == 0
11385 || (version == 2 && nr_columns == 0))
11386 {
11387 /* All must be zero. */
11388 if (nr_slots != 0 || nr_units != 0
11389 || (version == 2 && nr_columns != 0))
11390 {
11391 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11392 " all zero [in modules %s]"),
11393 dwp_file->name);
11394 }
11395 return htab;
11396 }
11397
11398 if (version == 1)
11399 {
11400 htab->section_pool.v1.indices =
11401 htab->unit_table + sizeof (uint32_t) * nr_slots;
11402 /* It's harder to decide whether the section is too small in v1.
11403 V1 is deprecated anyway so we punt. */
11404 }
11405 else
11406 {
11407 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11408 int *ids = htab->section_pool.v2.section_ids;
11409 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11410 /* Reverse map for error checking. */
11411 int ids_seen[DW_SECT_MAX + 1];
11412 int i;
11413
11414 if (nr_columns < 2)
11415 {
11416 error (_("Dwarf Error: bad DWP hash table, too few columns"
11417 " in section table [in module %s]"),
11418 dwp_file->name);
11419 }
11420 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11421 {
11422 error (_("Dwarf Error: bad DWP hash table, too many columns"
11423 " in section table [in module %s]"),
11424 dwp_file->name);
11425 }
11426 memset (ids, 255, sizeof_ids);
11427 memset (ids_seen, 255, sizeof (ids_seen));
11428 for (i = 0; i < nr_columns; ++i)
11429 {
11430 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11431
11432 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11433 {
11434 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11435 " in section table [in module %s]"),
11436 id, dwp_file->name);
11437 }
11438 if (ids_seen[id] != -1)
11439 {
11440 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11441 " id %d in section table [in module %s]"),
11442 id, dwp_file->name);
11443 }
11444 ids_seen[id] = i;
11445 ids[i] = id;
11446 }
11447 /* Must have exactly one info or types section. */
11448 if (((ids_seen[DW_SECT_INFO] != -1)
11449 + (ids_seen[DW_SECT_TYPES] != -1))
11450 != 1)
11451 {
11452 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11453 " DWO info/types section [in module %s]"),
11454 dwp_file->name);
11455 }
11456 /* Must have an abbrev section. */
11457 if (ids_seen[DW_SECT_ABBREV] == -1)
11458 {
11459 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11460 " section [in module %s]"),
11461 dwp_file->name);
11462 }
11463 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11464 htab->section_pool.v2.sizes =
11465 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11466 * nr_units * nr_columns);
11467 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11468 * nr_units * nr_columns))
11469 > index_end)
11470 {
11471 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11472 " [in module %s]"),
11473 dwp_file->name);
11474 }
11475 }
11476
11477 return htab;
11478 }
11479
11480 /* Update SECTIONS with the data from SECTP.
11481
11482 This function is like the other "locate" section routines that are
11483 passed to bfd_map_over_sections, but in this context the sections to
11484 read comes from the DWP V1 hash table, not the full ELF section table.
11485
11486 The result is non-zero for success, or zero if an error was found. */
11487
11488 static int
11489 locate_v1_virtual_dwo_sections (asection *sectp,
11490 struct virtual_v1_dwo_sections *sections)
11491 {
11492 const struct dwop_section_names *names = &dwop_section_names;
11493
11494 if (section_is_p (sectp->name, &names->abbrev_dwo))
11495 {
11496 /* There can be only one. */
11497 if (sections->abbrev.s.section != NULL)
11498 return 0;
11499 sections->abbrev.s.section = sectp;
11500 sections->abbrev.size = bfd_section_size (sectp);
11501 }
11502 else if (section_is_p (sectp->name, &names->info_dwo)
11503 || section_is_p (sectp->name, &names->types_dwo))
11504 {
11505 /* There can be only one. */
11506 if (sections->info_or_types.s.section != NULL)
11507 return 0;
11508 sections->info_or_types.s.section = sectp;
11509 sections->info_or_types.size = bfd_section_size (sectp);
11510 }
11511 else if (section_is_p (sectp->name, &names->line_dwo))
11512 {
11513 /* There can be only one. */
11514 if (sections->line.s.section != NULL)
11515 return 0;
11516 sections->line.s.section = sectp;
11517 sections->line.size = bfd_section_size (sectp);
11518 }
11519 else if (section_is_p (sectp->name, &names->loc_dwo))
11520 {
11521 /* There can be only one. */
11522 if (sections->loc.s.section != NULL)
11523 return 0;
11524 sections->loc.s.section = sectp;
11525 sections->loc.size = bfd_section_size (sectp);
11526 }
11527 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11528 {
11529 /* There can be only one. */
11530 if (sections->macinfo.s.section != NULL)
11531 return 0;
11532 sections->macinfo.s.section = sectp;
11533 sections->macinfo.size = bfd_section_size (sectp);
11534 }
11535 else if (section_is_p (sectp->name, &names->macro_dwo))
11536 {
11537 /* There can be only one. */
11538 if (sections->macro.s.section != NULL)
11539 return 0;
11540 sections->macro.s.section = sectp;
11541 sections->macro.size = bfd_section_size (sectp);
11542 }
11543 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11544 {
11545 /* There can be only one. */
11546 if (sections->str_offsets.s.section != NULL)
11547 return 0;
11548 sections->str_offsets.s.section = sectp;
11549 sections->str_offsets.size = bfd_section_size (sectp);
11550 }
11551 else
11552 {
11553 /* No other kind of section is valid. */
11554 return 0;
11555 }
11556
11557 return 1;
11558 }
11559
11560 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11561 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11562 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11563 This is for DWP version 1 files. */
11564
11565 static struct dwo_unit *
11566 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11567 struct dwp_file *dwp_file,
11568 uint32_t unit_index,
11569 const char *comp_dir,
11570 ULONGEST signature, int is_debug_types)
11571 {
11572 struct objfile *objfile = dwarf2_per_objfile->objfile;
11573 const struct dwp_hash_table *dwp_htab =
11574 is_debug_types ? dwp_file->tus : dwp_file->cus;
11575 bfd *dbfd = dwp_file->dbfd.get ();
11576 const char *kind = is_debug_types ? "TU" : "CU";
11577 struct dwo_file *dwo_file;
11578 struct dwo_unit *dwo_unit;
11579 struct virtual_v1_dwo_sections sections;
11580 void **dwo_file_slot;
11581 int i;
11582
11583 gdb_assert (dwp_file->version == 1);
11584
11585 if (dwarf_read_debug)
11586 {
11587 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11588 kind,
11589 pulongest (unit_index), hex_string (signature),
11590 dwp_file->name);
11591 }
11592
11593 /* Fetch the sections of this DWO unit.
11594 Put a limit on the number of sections we look for so that bad data
11595 doesn't cause us to loop forever. */
11596
11597 #define MAX_NR_V1_DWO_SECTIONS \
11598 (1 /* .debug_info or .debug_types */ \
11599 + 1 /* .debug_abbrev */ \
11600 + 1 /* .debug_line */ \
11601 + 1 /* .debug_loc */ \
11602 + 1 /* .debug_str_offsets */ \
11603 + 1 /* .debug_macro or .debug_macinfo */ \
11604 + 1 /* trailing zero */)
11605
11606 memset (&sections, 0, sizeof (sections));
11607
11608 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11609 {
11610 asection *sectp;
11611 uint32_t section_nr =
11612 read_4_bytes (dbfd,
11613 dwp_htab->section_pool.v1.indices
11614 + (unit_index + i) * sizeof (uint32_t));
11615
11616 if (section_nr == 0)
11617 break;
11618 if (section_nr >= dwp_file->num_sections)
11619 {
11620 error (_("Dwarf Error: bad DWP hash table, section number too large"
11621 " [in module %s]"),
11622 dwp_file->name);
11623 }
11624
11625 sectp = dwp_file->elf_sections[section_nr];
11626 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11627 {
11628 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11629 " [in module %s]"),
11630 dwp_file->name);
11631 }
11632 }
11633
11634 if (i < 2
11635 || sections.info_or_types.empty ()
11636 || sections.abbrev.empty ())
11637 {
11638 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11639 " [in module %s]"),
11640 dwp_file->name);
11641 }
11642 if (i == MAX_NR_V1_DWO_SECTIONS)
11643 {
11644 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11645 " [in module %s]"),
11646 dwp_file->name);
11647 }
11648
11649 /* It's easier for the rest of the code if we fake a struct dwo_file and
11650 have dwo_unit "live" in that. At least for now.
11651
11652 The DWP file can be made up of a random collection of CUs and TUs.
11653 However, for each CU + set of TUs that came from the same original DWO
11654 file, we can combine them back into a virtual DWO file to save space
11655 (fewer struct dwo_file objects to allocate). Remember that for really
11656 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11657
11658 std::string virtual_dwo_name =
11659 string_printf ("virtual-dwo/%d-%d-%d-%d",
11660 sections.abbrev.get_id (),
11661 sections.line.get_id (),
11662 sections.loc.get_id (),
11663 sections.str_offsets.get_id ());
11664 /* Can we use an existing virtual DWO file? */
11665 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11666 virtual_dwo_name.c_str (),
11667 comp_dir);
11668 /* Create one if necessary. */
11669 if (*dwo_file_slot == NULL)
11670 {
11671 if (dwarf_read_debug)
11672 {
11673 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11674 virtual_dwo_name.c_str ());
11675 }
11676 dwo_file = new struct dwo_file;
11677 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11678 dwo_file->comp_dir = comp_dir;
11679 dwo_file->sections.abbrev = sections.abbrev;
11680 dwo_file->sections.line = sections.line;
11681 dwo_file->sections.loc = sections.loc;
11682 dwo_file->sections.macinfo = sections.macinfo;
11683 dwo_file->sections.macro = sections.macro;
11684 dwo_file->sections.str_offsets = sections.str_offsets;
11685 /* The "str" section is global to the entire DWP file. */
11686 dwo_file->sections.str = dwp_file->sections.str;
11687 /* The info or types section is assigned below to dwo_unit,
11688 there's no need to record it in dwo_file.
11689 Also, we can't simply record type sections in dwo_file because
11690 we record a pointer into the vector in dwo_unit. As we collect more
11691 types we'll grow the vector and eventually have to reallocate space
11692 for it, invalidating all copies of pointers into the previous
11693 contents. */
11694 *dwo_file_slot = dwo_file;
11695 }
11696 else
11697 {
11698 if (dwarf_read_debug)
11699 {
11700 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11701 virtual_dwo_name.c_str ());
11702 }
11703 dwo_file = (struct dwo_file *) *dwo_file_slot;
11704 }
11705
11706 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11707 dwo_unit->dwo_file = dwo_file;
11708 dwo_unit->signature = signature;
11709 dwo_unit->section =
11710 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11711 *dwo_unit->section = sections.info_or_types;
11712 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11713
11714 return dwo_unit;
11715 }
11716
11717 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11718 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11719 piece within that section used by a TU/CU, return a virtual section
11720 of just that piece. */
11721
11722 static struct dwarf2_section_info
11723 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11724 struct dwarf2_section_info *section,
11725 bfd_size_type offset, bfd_size_type size)
11726 {
11727 struct dwarf2_section_info result;
11728 asection *sectp;
11729
11730 gdb_assert (section != NULL);
11731 gdb_assert (!section->is_virtual);
11732
11733 memset (&result, 0, sizeof (result));
11734 result.s.containing_section = section;
11735 result.is_virtual = true;
11736
11737 if (size == 0)
11738 return result;
11739
11740 sectp = section->get_bfd_section ();
11741
11742 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11743 bounds of the real section. This is a pretty-rare event, so just
11744 flag an error (easier) instead of a warning and trying to cope. */
11745 if (sectp == NULL
11746 || offset + size > bfd_section_size (sectp))
11747 {
11748 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11749 " in section %s [in module %s]"),
11750 sectp ? bfd_section_name (sectp) : "<unknown>",
11751 objfile_name (dwarf2_per_objfile->objfile));
11752 }
11753
11754 result.virtual_offset = offset;
11755 result.size = size;
11756 return result;
11757 }
11758
11759 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11760 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11761 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11762 This is for DWP version 2 files. */
11763
11764 static struct dwo_unit *
11765 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11766 struct dwp_file *dwp_file,
11767 uint32_t unit_index,
11768 const char *comp_dir,
11769 ULONGEST signature, int is_debug_types)
11770 {
11771 struct objfile *objfile = dwarf2_per_objfile->objfile;
11772 const struct dwp_hash_table *dwp_htab =
11773 is_debug_types ? dwp_file->tus : dwp_file->cus;
11774 bfd *dbfd = dwp_file->dbfd.get ();
11775 const char *kind = is_debug_types ? "TU" : "CU";
11776 struct dwo_file *dwo_file;
11777 struct dwo_unit *dwo_unit;
11778 struct virtual_v2_dwo_sections sections;
11779 void **dwo_file_slot;
11780 int i;
11781
11782 gdb_assert (dwp_file->version == 2);
11783
11784 if (dwarf_read_debug)
11785 {
11786 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11787 kind,
11788 pulongest (unit_index), hex_string (signature),
11789 dwp_file->name);
11790 }
11791
11792 /* Fetch the section offsets of this DWO unit. */
11793
11794 memset (&sections, 0, sizeof (sections));
11795
11796 for (i = 0; i < dwp_htab->nr_columns; ++i)
11797 {
11798 uint32_t offset = read_4_bytes (dbfd,
11799 dwp_htab->section_pool.v2.offsets
11800 + (((unit_index - 1) * dwp_htab->nr_columns
11801 + i)
11802 * sizeof (uint32_t)));
11803 uint32_t size = read_4_bytes (dbfd,
11804 dwp_htab->section_pool.v2.sizes
11805 + (((unit_index - 1) * dwp_htab->nr_columns
11806 + i)
11807 * sizeof (uint32_t)));
11808
11809 switch (dwp_htab->section_pool.v2.section_ids[i])
11810 {
11811 case DW_SECT_INFO:
11812 case DW_SECT_TYPES:
11813 sections.info_or_types_offset = offset;
11814 sections.info_or_types_size = size;
11815 break;
11816 case DW_SECT_ABBREV:
11817 sections.abbrev_offset = offset;
11818 sections.abbrev_size = size;
11819 break;
11820 case DW_SECT_LINE:
11821 sections.line_offset = offset;
11822 sections.line_size = size;
11823 break;
11824 case DW_SECT_LOC:
11825 sections.loc_offset = offset;
11826 sections.loc_size = size;
11827 break;
11828 case DW_SECT_STR_OFFSETS:
11829 sections.str_offsets_offset = offset;
11830 sections.str_offsets_size = size;
11831 break;
11832 case DW_SECT_MACINFO:
11833 sections.macinfo_offset = offset;
11834 sections.macinfo_size = size;
11835 break;
11836 case DW_SECT_MACRO:
11837 sections.macro_offset = offset;
11838 sections.macro_size = size;
11839 break;
11840 }
11841 }
11842
11843 /* It's easier for the rest of the code if we fake a struct dwo_file and
11844 have dwo_unit "live" in that. At least for now.
11845
11846 The DWP file can be made up of a random collection of CUs and TUs.
11847 However, for each CU + set of TUs that came from the same original DWO
11848 file, we can combine them back into a virtual DWO file to save space
11849 (fewer struct dwo_file objects to allocate). Remember that for really
11850 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11851
11852 std::string virtual_dwo_name =
11853 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11854 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11855 (long) (sections.line_size ? sections.line_offset : 0),
11856 (long) (sections.loc_size ? sections.loc_offset : 0),
11857 (long) (sections.str_offsets_size
11858 ? sections.str_offsets_offset : 0));
11859 /* Can we use an existing virtual DWO file? */
11860 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11861 virtual_dwo_name.c_str (),
11862 comp_dir);
11863 /* Create one if necessary. */
11864 if (*dwo_file_slot == NULL)
11865 {
11866 if (dwarf_read_debug)
11867 {
11868 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11869 virtual_dwo_name.c_str ());
11870 }
11871 dwo_file = new struct dwo_file;
11872 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11873 dwo_file->comp_dir = comp_dir;
11874 dwo_file->sections.abbrev =
11875 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11876 sections.abbrev_offset, sections.abbrev_size);
11877 dwo_file->sections.line =
11878 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11879 sections.line_offset, sections.line_size);
11880 dwo_file->sections.loc =
11881 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11882 sections.loc_offset, sections.loc_size);
11883 dwo_file->sections.macinfo =
11884 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11885 sections.macinfo_offset, sections.macinfo_size);
11886 dwo_file->sections.macro =
11887 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11888 sections.macro_offset, sections.macro_size);
11889 dwo_file->sections.str_offsets =
11890 create_dwp_v2_section (dwarf2_per_objfile,
11891 &dwp_file->sections.str_offsets,
11892 sections.str_offsets_offset,
11893 sections.str_offsets_size);
11894 /* The "str" section is global to the entire DWP file. */
11895 dwo_file->sections.str = dwp_file->sections.str;
11896 /* The info or types section is assigned below to dwo_unit,
11897 there's no need to record it in dwo_file.
11898 Also, we can't simply record type sections in dwo_file because
11899 we record a pointer into the vector in dwo_unit. As we collect more
11900 types we'll grow the vector and eventually have to reallocate space
11901 for it, invalidating all copies of pointers into the previous
11902 contents. */
11903 *dwo_file_slot = dwo_file;
11904 }
11905 else
11906 {
11907 if (dwarf_read_debug)
11908 {
11909 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11910 virtual_dwo_name.c_str ());
11911 }
11912 dwo_file = (struct dwo_file *) *dwo_file_slot;
11913 }
11914
11915 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11916 dwo_unit->dwo_file = dwo_file;
11917 dwo_unit->signature = signature;
11918 dwo_unit->section =
11919 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11920 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11921 is_debug_types
11922 ? &dwp_file->sections.types
11923 : &dwp_file->sections.info,
11924 sections.info_or_types_offset,
11925 sections.info_or_types_size);
11926 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11927
11928 return dwo_unit;
11929 }
11930
11931 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11932 Returns NULL if the signature isn't found. */
11933
11934 static struct dwo_unit *
11935 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11936 struct dwp_file *dwp_file, const char *comp_dir,
11937 ULONGEST signature, int is_debug_types)
11938 {
11939 const struct dwp_hash_table *dwp_htab =
11940 is_debug_types ? dwp_file->tus : dwp_file->cus;
11941 bfd *dbfd = dwp_file->dbfd.get ();
11942 uint32_t mask = dwp_htab->nr_slots - 1;
11943 uint32_t hash = signature & mask;
11944 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11945 unsigned int i;
11946 void **slot;
11947 struct dwo_unit find_dwo_cu;
11948
11949 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11950 find_dwo_cu.signature = signature;
11951 slot = htab_find_slot (is_debug_types
11952 ? dwp_file->loaded_tus.get ()
11953 : dwp_file->loaded_cus.get (),
11954 &find_dwo_cu, INSERT);
11955
11956 if (*slot != NULL)
11957 return (struct dwo_unit *) *slot;
11958
11959 /* Use a for loop so that we don't loop forever on bad debug info. */
11960 for (i = 0; i < dwp_htab->nr_slots; ++i)
11961 {
11962 ULONGEST signature_in_table;
11963
11964 signature_in_table =
11965 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11966 if (signature_in_table == signature)
11967 {
11968 uint32_t unit_index =
11969 read_4_bytes (dbfd,
11970 dwp_htab->unit_table + hash * sizeof (uint32_t));
11971
11972 if (dwp_file->version == 1)
11973 {
11974 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11975 dwp_file, unit_index,
11976 comp_dir, signature,
11977 is_debug_types);
11978 }
11979 else
11980 {
11981 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11982 dwp_file, unit_index,
11983 comp_dir, signature,
11984 is_debug_types);
11985 }
11986 return (struct dwo_unit *) *slot;
11987 }
11988 if (signature_in_table == 0)
11989 return NULL;
11990 hash = (hash + hash2) & mask;
11991 }
11992
11993 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11994 " [in module %s]"),
11995 dwp_file->name);
11996 }
11997
11998 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11999 Open the file specified by FILE_NAME and hand it off to BFD for
12000 preliminary analysis. Return a newly initialized bfd *, which
12001 includes a canonicalized copy of FILE_NAME.
12002 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12003 SEARCH_CWD is true if the current directory is to be searched.
12004 It will be searched before debug-file-directory.
12005 If successful, the file is added to the bfd include table of the
12006 objfile's bfd (see gdb_bfd_record_inclusion).
12007 If unable to find/open the file, return NULL.
12008 NOTE: This function is derived from symfile_bfd_open. */
12009
12010 static gdb_bfd_ref_ptr
12011 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12012 const char *file_name, int is_dwp, int search_cwd)
12013 {
12014 int desc;
12015 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12016 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12017 to debug_file_directory. */
12018 const char *search_path;
12019 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12020
12021 gdb::unique_xmalloc_ptr<char> search_path_holder;
12022 if (search_cwd)
12023 {
12024 if (*debug_file_directory != '\0')
12025 {
12026 search_path_holder.reset (concat (".", dirname_separator_string,
12027 debug_file_directory,
12028 (char *) NULL));
12029 search_path = search_path_holder.get ();
12030 }
12031 else
12032 search_path = ".";
12033 }
12034 else
12035 search_path = debug_file_directory;
12036
12037 openp_flags flags = OPF_RETURN_REALPATH;
12038 if (is_dwp)
12039 flags |= OPF_SEARCH_IN_PATH;
12040
12041 gdb::unique_xmalloc_ptr<char> absolute_name;
12042 desc = openp (search_path, flags, file_name,
12043 O_RDONLY | O_BINARY, &absolute_name);
12044 if (desc < 0)
12045 return NULL;
12046
12047 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12048 gnutarget, desc));
12049 if (sym_bfd == NULL)
12050 return NULL;
12051 bfd_set_cacheable (sym_bfd.get (), 1);
12052
12053 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12054 return NULL;
12055
12056 /* Success. Record the bfd as having been included by the objfile's bfd.
12057 This is important because things like demangled_names_hash lives in the
12058 objfile's per_bfd space and may have references to things like symbol
12059 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12060 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12061
12062 return sym_bfd;
12063 }
12064
12065 /* Try to open DWO file FILE_NAME.
12066 COMP_DIR is the DW_AT_comp_dir attribute.
12067 The result is the bfd handle of the file.
12068 If there is a problem finding or opening the file, return NULL.
12069 Upon success, the canonicalized path of the file is stored in the bfd,
12070 same as symfile_bfd_open. */
12071
12072 static gdb_bfd_ref_ptr
12073 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12074 const char *file_name, const char *comp_dir)
12075 {
12076 if (IS_ABSOLUTE_PATH (file_name))
12077 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12078 0 /*is_dwp*/, 0 /*search_cwd*/);
12079
12080 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12081
12082 if (comp_dir != NULL)
12083 {
12084 gdb::unique_xmalloc_ptr<char> path_to_try
12085 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12086
12087 /* NOTE: If comp_dir is a relative path, this will also try the
12088 search path, which seems useful. */
12089 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12090 path_to_try.get (),
12091 0 /*is_dwp*/,
12092 1 /*search_cwd*/));
12093 if (abfd != NULL)
12094 return abfd;
12095 }
12096
12097 /* That didn't work, try debug-file-directory, which, despite its name,
12098 is a list of paths. */
12099
12100 if (*debug_file_directory == '\0')
12101 return NULL;
12102
12103 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12104 0 /*is_dwp*/, 1 /*search_cwd*/);
12105 }
12106
12107 /* This function is mapped across the sections and remembers the offset and
12108 size of each of the DWO debugging sections we are interested in. */
12109
12110 static void
12111 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12112 {
12113 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12114 const struct dwop_section_names *names = &dwop_section_names;
12115
12116 if (section_is_p (sectp->name, &names->abbrev_dwo))
12117 {
12118 dwo_sections->abbrev.s.section = sectp;
12119 dwo_sections->abbrev.size = bfd_section_size (sectp);
12120 }
12121 else if (section_is_p (sectp->name, &names->info_dwo))
12122 {
12123 dwo_sections->info.s.section = sectp;
12124 dwo_sections->info.size = bfd_section_size (sectp);
12125 }
12126 else if (section_is_p (sectp->name, &names->line_dwo))
12127 {
12128 dwo_sections->line.s.section = sectp;
12129 dwo_sections->line.size = bfd_section_size (sectp);
12130 }
12131 else if (section_is_p (sectp->name, &names->loc_dwo))
12132 {
12133 dwo_sections->loc.s.section = sectp;
12134 dwo_sections->loc.size = bfd_section_size (sectp);
12135 }
12136 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12137 {
12138 dwo_sections->macinfo.s.section = sectp;
12139 dwo_sections->macinfo.size = bfd_section_size (sectp);
12140 }
12141 else if (section_is_p (sectp->name, &names->macro_dwo))
12142 {
12143 dwo_sections->macro.s.section = sectp;
12144 dwo_sections->macro.size = bfd_section_size (sectp);
12145 }
12146 else if (section_is_p (sectp->name, &names->str_dwo))
12147 {
12148 dwo_sections->str.s.section = sectp;
12149 dwo_sections->str.size = bfd_section_size (sectp);
12150 }
12151 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12152 {
12153 dwo_sections->str_offsets.s.section = sectp;
12154 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12155 }
12156 else if (section_is_p (sectp->name, &names->types_dwo))
12157 {
12158 struct dwarf2_section_info type_section;
12159
12160 memset (&type_section, 0, sizeof (type_section));
12161 type_section.s.section = sectp;
12162 type_section.size = bfd_section_size (sectp);
12163 dwo_sections->types.push_back (type_section);
12164 }
12165 }
12166
12167 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12168 by PER_CU. This is for the non-DWP case.
12169 The result is NULL if DWO_NAME can't be found. */
12170
12171 static struct dwo_file *
12172 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12173 const char *dwo_name, const char *comp_dir)
12174 {
12175 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12176
12177 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12178 if (dbfd == NULL)
12179 {
12180 if (dwarf_read_debug)
12181 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12182 return NULL;
12183 }
12184
12185 dwo_file_up dwo_file (new struct dwo_file);
12186 dwo_file->dwo_name = dwo_name;
12187 dwo_file->comp_dir = comp_dir;
12188 dwo_file->dbfd = std::move (dbfd);
12189
12190 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12191 &dwo_file->sections);
12192
12193 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12194 dwo_file->sections.info, dwo_file->cus);
12195
12196 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12197 dwo_file->sections.types, dwo_file->tus);
12198
12199 if (dwarf_read_debug)
12200 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12201
12202 return dwo_file.release ();
12203 }
12204
12205 /* This function is mapped across the sections and remembers the offset and
12206 size of each of the DWP debugging sections common to version 1 and 2 that
12207 we are interested in. */
12208
12209 static void
12210 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12211 void *dwp_file_ptr)
12212 {
12213 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12214 const struct dwop_section_names *names = &dwop_section_names;
12215 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12216
12217 /* Record the ELF section number for later lookup: this is what the
12218 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12219 gdb_assert (elf_section_nr < dwp_file->num_sections);
12220 dwp_file->elf_sections[elf_section_nr] = sectp;
12221
12222 /* Look for specific sections that we need. */
12223 if (section_is_p (sectp->name, &names->str_dwo))
12224 {
12225 dwp_file->sections.str.s.section = sectp;
12226 dwp_file->sections.str.size = bfd_section_size (sectp);
12227 }
12228 else if (section_is_p (sectp->name, &names->cu_index))
12229 {
12230 dwp_file->sections.cu_index.s.section = sectp;
12231 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12232 }
12233 else if (section_is_p (sectp->name, &names->tu_index))
12234 {
12235 dwp_file->sections.tu_index.s.section = sectp;
12236 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12237 }
12238 }
12239
12240 /* This function is mapped across the sections and remembers the offset and
12241 size of each of the DWP version 2 debugging sections that we are interested
12242 in. This is split into a separate function because we don't know if we
12243 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12244
12245 static void
12246 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12247 {
12248 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12249 const struct dwop_section_names *names = &dwop_section_names;
12250 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12251
12252 /* Record the ELF section number for later lookup: this is what the
12253 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12254 gdb_assert (elf_section_nr < dwp_file->num_sections);
12255 dwp_file->elf_sections[elf_section_nr] = sectp;
12256
12257 /* Look for specific sections that we need. */
12258 if (section_is_p (sectp->name, &names->abbrev_dwo))
12259 {
12260 dwp_file->sections.abbrev.s.section = sectp;
12261 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12262 }
12263 else if (section_is_p (sectp->name, &names->info_dwo))
12264 {
12265 dwp_file->sections.info.s.section = sectp;
12266 dwp_file->sections.info.size = bfd_section_size (sectp);
12267 }
12268 else if (section_is_p (sectp->name, &names->line_dwo))
12269 {
12270 dwp_file->sections.line.s.section = sectp;
12271 dwp_file->sections.line.size = bfd_section_size (sectp);
12272 }
12273 else if (section_is_p (sectp->name, &names->loc_dwo))
12274 {
12275 dwp_file->sections.loc.s.section = sectp;
12276 dwp_file->sections.loc.size = bfd_section_size (sectp);
12277 }
12278 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12279 {
12280 dwp_file->sections.macinfo.s.section = sectp;
12281 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12282 }
12283 else if (section_is_p (sectp->name, &names->macro_dwo))
12284 {
12285 dwp_file->sections.macro.s.section = sectp;
12286 dwp_file->sections.macro.size = bfd_section_size (sectp);
12287 }
12288 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12289 {
12290 dwp_file->sections.str_offsets.s.section = sectp;
12291 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12292 }
12293 else if (section_is_p (sectp->name, &names->types_dwo))
12294 {
12295 dwp_file->sections.types.s.section = sectp;
12296 dwp_file->sections.types.size = bfd_section_size (sectp);
12297 }
12298 }
12299
12300 /* Hash function for dwp_file loaded CUs/TUs. */
12301
12302 static hashval_t
12303 hash_dwp_loaded_cutus (const void *item)
12304 {
12305 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12306
12307 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12308 return dwo_unit->signature;
12309 }
12310
12311 /* Equality function for dwp_file loaded CUs/TUs. */
12312
12313 static int
12314 eq_dwp_loaded_cutus (const void *a, const void *b)
12315 {
12316 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12317 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12318
12319 return dua->signature == dub->signature;
12320 }
12321
12322 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12323
12324 static htab_up
12325 allocate_dwp_loaded_cutus_table ()
12326 {
12327 return htab_up (htab_create_alloc (3,
12328 hash_dwp_loaded_cutus,
12329 eq_dwp_loaded_cutus,
12330 NULL, xcalloc, xfree));
12331 }
12332
12333 /* Try to open DWP file FILE_NAME.
12334 The result is the bfd handle of the file.
12335 If there is a problem finding or opening the file, return NULL.
12336 Upon success, the canonicalized path of the file is stored in the bfd,
12337 same as symfile_bfd_open. */
12338
12339 static gdb_bfd_ref_ptr
12340 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12341 const char *file_name)
12342 {
12343 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12344 1 /*is_dwp*/,
12345 1 /*search_cwd*/));
12346 if (abfd != NULL)
12347 return abfd;
12348
12349 /* Work around upstream bug 15652.
12350 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12351 [Whether that's a "bug" is debatable, but it is getting in our way.]
12352 We have no real idea where the dwp file is, because gdb's realpath-ing
12353 of the executable's path may have discarded the needed info.
12354 [IWBN if the dwp file name was recorded in the executable, akin to
12355 .gnu_debuglink, but that doesn't exist yet.]
12356 Strip the directory from FILE_NAME and search again. */
12357 if (*debug_file_directory != '\0')
12358 {
12359 /* Don't implicitly search the current directory here.
12360 If the user wants to search "." to handle this case,
12361 it must be added to debug-file-directory. */
12362 return try_open_dwop_file (dwarf2_per_objfile,
12363 lbasename (file_name), 1 /*is_dwp*/,
12364 0 /*search_cwd*/);
12365 }
12366
12367 return NULL;
12368 }
12369
12370 /* Initialize the use of the DWP file for the current objfile.
12371 By convention the name of the DWP file is ${objfile}.dwp.
12372 The result is NULL if it can't be found. */
12373
12374 static std::unique_ptr<struct dwp_file>
12375 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12376 {
12377 struct objfile *objfile = dwarf2_per_objfile->objfile;
12378
12379 /* Try to find first .dwp for the binary file before any symbolic links
12380 resolving. */
12381
12382 /* If the objfile is a debug file, find the name of the real binary
12383 file and get the name of dwp file from there. */
12384 std::string dwp_name;
12385 if (objfile->separate_debug_objfile_backlink != NULL)
12386 {
12387 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12388 const char *backlink_basename = lbasename (backlink->original_name);
12389
12390 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12391 }
12392 else
12393 dwp_name = objfile->original_name;
12394
12395 dwp_name += ".dwp";
12396
12397 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12398 if (dbfd == NULL
12399 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12400 {
12401 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12402 dwp_name = objfile_name (objfile);
12403 dwp_name += ".dwp";
12404 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12405 }
12406
12407 if (dbfd == NULL)
12408 {
12409 if (dwarf_read_debug)
12410 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12411 return std::unique_ptr<dwp_file> ();
12412 }
12413
12414 const char *name = bfd_get_filename (dbfd.get ());
12415 std::unique_ptr<struct dwp_file> dwp_file
12416 (new struct dwp_file (name, std::move (dbfd)));
12417
12418 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12419 dwp_file->elf_sections =
12420 OBSTACK_CALLOC (&objfile->objfile_obstack,
12421 dwp_file->num_sections, asection *);
12422
12423 bfd_map_over_sections (dwp_file->dbfd.get (),
12424 dwarf2_locate_common_dwp_sections,
12425 dwp_file.get ());
12426
12427 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12428 0);
12429
12430 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12431 1);
12432
12433 /* The DWP file version is stored in the hash table. Oh well. */
12434 if (dwp_file->cus && dwp_file->tus
12435 && dwp_file->cus->version != dwp_file->tus->version)
12436 {
12437 /* Technically speaking, we should try to limp along, but this is
12438 pretty bizarre. We use pulongest here because that's the established
12439 portability solution (e.g, we cannot use %u for uint32_t). */
12440 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12441 " TU version %s [in DWP file %s]"),
12442 pulongest (dwp_file->cus->version),
12443 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12444 }
12445
12446 if (dwp_file->cus)
12447 dwp_file->version = dwp_file->cus->version;
12448 else if (dwp_file->tus)
12449 dwp_file->version = dwp_file->tus->version;
12450 else
12451 dwp_file->version = 2;
12452
12453 if (dwp_file->version == 2)
12454 bfd_map_over_sections (dwp_file->dbfd.get (),
12455 dwarf2_locate_v2_dwp_sections,
12456 dwp_file.get ());
12457
12458 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12459 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12460
12461 if (dwarf_read_debug)
12462 {
12463 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12464 fprintf_unfiltered (gdb_stdlog,
12465 " %s CUs, %s TUs\n",
12466 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12467 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12468 }
12469
12470 return dwp_file;
12471 }
12472
12473 /* Wrapper around open_and_init_dwp_file, only open it once. */
12474
12475 static struct dwp_file *
12476 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12477 {
12478 if (! dwarf2_per_objfile->dwp_checked)
12479 {
12480 dwarf2_per_objfile->dwp_file
12481 = open_and_init_dwp_file (dwarf2_per_objfile);
12482 dwarf2_per_objfile->dwp_checked = 1;
12483 }
12484 return dwarf2_per_objfile->dwp_file.get ();
12485 }
12486
12487 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12488 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12489 or in the DWP file for the objfile, referenced by THIS_UNIT.
12490 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12491 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12492
12493 This is called, for example, when wanting to read a variable with a
12494 complex location. Therefore we don't want to do file i/o for every call.
12495 Therefore we don't want to look for a DWO file on every call.
12496 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12497 then we check if we've already seen DWO_NAME, and only THEN do we check
12498 for a DWO file.
12499
12500 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12501 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12502
12503 static struct dwo_unit *
12504 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12505 const char *dwo_name, const char *comp_dir,
12506 ULONGEST signature, int is_debug_types)
12507 {
12508 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12509 struct objfile *objfile = dwarf2_per_objfile->objfile;
12510 const char *kind = is_debug_types ? "TU" : "CU";
12511 void **dwo_file_slot;
12512 struct dwo_file *dwo_file;
12513 struct dwp_file *dwp_file;
12514
12515 /* First see if there's a DWP file.
12516 If we have a DWP file but didn't find the DWO inside it, don't
12517 look for the original DWO file. It makes gdb behave differently
12518 depending on whether one is debugging in the build tree. */
12519
12520 dwp_file = get_dwp_file (dwarf2_per_objfile);
12521 if (dwp_file != NULL)
12522 {
12523 const struct dwp_hash_table *dwp_htab =
12524 is_debug_types ? dwp_file->tus : dwp_file->cus;
12525
12526 if (dwp_htab != NULL)
12527 {
12528 struct dwo_unit *dwo_cutu =
12529 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12530 signature, is_debug_types);
12531
12532 if (dwo_cutu != NULL)
12533 {
12534 if (dwarf_read_debug)
12535 {
12536 fprintf_unfiltered (gdb_stdlog,
12537 "Virtual DWO %s %s found: @%s\n",
12538 kind, hex_string (signature),
12539 host_address_to_string (dwo_cutu));
12540 }
12541 return dwo_cutu;
12542 }
12543 }
12544 }
12545 else
12546 {
12547 /* No DWP file, look for the DWO file. */
12548
12549 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12550 dwo_name, comp_dir);
12551 if (*dwo_file_slot == NULL)
12552 {
12553 /* Read in the file and build a table of the CUs/TUs it contains. */
12554 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12555 }
12556 /* NOTE: This will be NULL if unable to open the file. */
12557 dwo_file = (struct dwo_file *) *dwo_file_slot;
12558
12559 if (dwo_file != NULL)
12560 {
12561 struct dwo_unit *dwo_cutu = NULL;
12562
12563 if (is_debug_types && dwo_file->tus)
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
12570 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12571 &find_dwo_cutu);
12572 }
12573 else if (!is_debug_types && dwo_file->cus)
12574 {
12575 struct dwo_unit find_dwo_cutu;
12576
12577 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12578 find_dwo_cutu.signature = signature;
12579 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12580 &find_dwo_cutu);
12581 }
12582
12583 if (dwo_cutu != NULL)
12584 {
12585 if (dwarf_read_debug)
12586 {
12587 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12588 kind, dwo_name, hex_string (signature),
12589 host_address_to_string (dwo_cutu));
12590 }
12591 return dwo_cutu;
12592 }
12593 }
12594 }
12595
12596 /* We didn't find it. This could mean a dwo_id mismatch, or
12597 someone deleted the DWO/DWP file, or the search path isn't set up
12598 correctly to find the file. */
12599
12600 if (dwarf_read_debug)
12601 {
12602 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12603 kind, dwo_name, hex_string (signature));
12604 }
12605
12606 /* This is a warning and not a complaint because it can be caused by
12607 pilot error (e.g., user accidentally deleting the DWO). */
12608 {
12609 /* Print the name of the DWP file if we looked there, helps the user
12610 better diagnose the problem. */
12611 std::string dwp_text;
12612
12613 if (dwp_file != NULL)
12614 dwp_text = string_printf (" [in DWP file %s]",
12615 lbasename (dwp_file->name));
12616
12617 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12618 " [in module %s]"),
12619 kind, dwo_name, hex_string (signature),
12620 dwp_text.c_str (),
12621 this_unit->is_debug_types ? "TU" : "CU",
12622 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12623 }
12624 return NULL;
12625 }
12626
12627 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12628 See lookup_dwo_cutu_unit for details. */
12629
12630 static struct dwo_unit *
12631 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12632 const char *dwo_name, const char *comp_dir,
12633 ULONGEST signature)
12634 {
12635 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12636 }
12637
12638 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12639 See lookup_dwo_cutu_unit for details. */
12640
12641 static struct dwo_unit *
12642 lookup_dwo_type_unit (struct signatured_type *this_tu,
12643 const char *dwo_name, const char *comp_dir)
12644 {
12645 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12646 }
12647
12648 /* Traversal function for queue_and_load_all_dwo_tus. */
12649
12650 static int
12651 queue_and_load_dwo_tu (void **slot, void *info)
12652 {
12653 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12654 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12655 ULONGEST signature = dwo_unit->signature;
12656 struct signatured_type *sig_type =
12657 lookup_dwo_signatured_type (per_cu->cu, signature);
12658
12659 if (sig_type != NULL)
12660 {
12661 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12662
12663 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12664 a real dependency of PER_CU on SIG_TYPE. That is detected later
12665 while processing PER_CU. */
12666 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12667 load_full_type_unit (sig_cu);
12668 per_cu->imported_symtabs_push (sig_cu);
12669 }
12670
12671 return 1;
12672 }
12673
12674 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12675 The DWO may have the only definition of the type, though it may not be
12676 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12677 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12678
12679 static void
12680 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12681 {
12682 struct dwo_unit *dwo_unit;
12683 struct dwo_file *dwo_file;
12684
12685 gdb_assert (!per_cu->is_debug_types);
12686 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12687 gdb_assert (per_cu->cu != NULL);
12688
12689 dwo_unit = per_cu->cu->dwo_unit;
12690 gdb_assert (dwo_unit != NULL);
12691
12692 dwo_file = dwo_unit->dwo_file;
12693 if (dwo_file->tus != NULL)
12694 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12695 per_cu);
12696 }
12697
12698 /* Read in various DIEs. */
12699
12700 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12701 Inherit only the children of the DW_AT_abstract_origin DIE not being
12702 already referenced by DW_AT_abstract_origin from the children of the
12703 current DIE. */
12704
12705 static void
12706 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12707 {
12708 struct die_info *child_die;
12709 sect_offset *offsetp;
12710 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12711 struct die_info *origin_die;
12712 /* Iterator of the ORIGIN_DIE children. */
12713 struct die_info *origin_child_die;
12714 struct attribute *attr;
12715 struct dwarf2_cu *origin_cu;
12716 struct pending **origin_previous_list_in_scope;
12717
12718 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12719 if (!attr)
12720 return;
12721
12722 /* Note that following die references may follow to a die in a
12723 different cu. */
12724
12725 origin_cu = cu;
12726 origin_die = follow_die_ref (die, attr, &origin_cu);
12727
12728 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12729 symbols in. */
12730 origin_previous_list_in_scope = origin_cu->list_in_scope;
12731 origin_cu->list_in_scope = cu->list_in_scope;
12732
12733 if (die->tag != origin_die->tag
12734 && !(die->tag == DW_TAG_inlined_subroutine
12735 && origin_die->tag == DW_TAG_subprogram))
12736 complaint (_("DIE %s and its abstract origin %s have different tags"),
12737 sect_offset_str (die->sect_off),
12738 sect_offset_str (origin_die->sect_off));
12739
12740 std::vector<sect_offset> offsets;
12741
12742 for (child_die = die->child;
12743 child_die && child_die->tag;
12744 child_die = sibling_die (child_die))
12745 {
12746 struct die_info *child_origin_die;
12747 struct dwarf2_cu *child_origin_cu;
12748
12749 /* We are trying to process concrete instance entries:
12750 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12751 it's not relevant to our analysis here. i.e. detecting DIEs that are
12752 present in the abstract instance but not referenced in the concrete
12753 one. */
12754 if (child_die->tag == DW_TAG_call_site
12755 || child_die->tag == DW_TAG_GNU_call_site)
12756 continue;
12757
12758 /* For each CHILD_DIE, find the corresponding child of
12759 ORIGIN_DIE. If there is more than one layer of
12760 DW_AT_abstract_origin, follow them all; there shouldn't be,
12761 but GCC versions at least through 4.4 generate this (GCC PR
12762 40573). */
12763 child_origin_die = child_die;
12764 child_origin_cu = cu;
12765 while (1)
12766 {
12767 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12768 child_origin_cu);
12769 if (attr == NULL)
12770 break;
12771 child_origin_die = follow_die_ref (child_origin_die, attr,
12772 &child_origin_cu);
12773 }
12774
12775 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12776 counterpart may exist. */
12777 if (child_origin_die != child_die)
12778 {
12779 if (child_die->tag != child_origin_die->tag
12780 && !(child_die->tag == DW_TAG_inlined_subroutine
12781 && child_origin_die->tag == DW_TAG_subprogram))
12782 complaint (_("Child DIE %s and its abstract origin %s have "
12783 "different tags"),
12784 sect_offset_str (child_die->sect_off),
12785 sect_offset_str (child_origin_die->sect_off));
12786 if (child_origin_die->parent != origin_die)
12787 complaint (_("Child DIE %s and its abstract origin %s have "
12788 "different parents"),
12789 sect_offset_str (child_die->sect_off),
12790 sect_offset_str (child_origin_die->sect_off));
12791 else
12792 offsets.push_back (child_origin_die->sect_off);
12793 }
12794 }
12795 std::sort (offsets.begin (), offsets.end ());
12796 sect_offset *offsets_end = offsets.data () + offsets.size ();
12797 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12798 if (offsetp[-1] == *offsetp)
12799 complaint (_("Multiple children of DIE %s refer "
12800 "to DIE %s as their abstract origin"),
12801 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12802
12803 offsetp = offsets.data ();
12804 origin_child_die = origin_die->child;
12805 while (origin_child_die && origin_child_die->tag)
12806 {
12807 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12808 while (offsetp < offsets_end
12809 && *offsetp < origin_child_die->sect_off)
12810 offsetp++;
12811 if (offsetp >= offsets_end
12812 || *offsetp > origin_child_die->sect_off)
12813 {
12814 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12815 Check whether we're already processing ORIGIN_CHILD_DIE.
12816 This can happen with mutually referenced abstract_origins.
12817 PR 16581. */
12818 if (!origin_child_die->in_process)
12819 process_die (origin_child_die, origin_cu);
12820 }
12821 origin_child_die = sibling_die (origin_child_die);
12822 }
12823 origin_cu->list_in_scope = origin_previous_list_in_scope;
12824
12825 if (cu != origin_cu)
12826 compute_delayed_physnames (origin_cu);
12827 }
12828
12829 static void
12830 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12831 {
12832 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12833 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12834 struct context_stack *newobj;
12835 CORE_ADDR lowpc;
12836 CORE_ADDR highpc;
12837 struct die_info *child_die;
12838 struct attribute *attr, *call_line, *call_file;
12839 const char *name;
12840 CORE_ADDR baseaddr;
12841 struct block *block;
12842 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12843 std::vector<struct symbol *> template_args;
12844 struct template_symbol *templ_func = NULL;
12845
12846 if (inlined_func)
12847 {
12848 /* If we do not have call site information, we can't show the
12849 caller of this inlined function. That's too confusing, so
12850 only use the scope for local variables. */
12851 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12852 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12853 if (call_line == NULL || call_file == NULL)
12854 {
12855 read_lexical_block_scope (die, cu);
12856 return;
12857 }
12858 }
12859
12860 baseaddr = objfile->text_section_offset ();
12861
12862 name = dwarf2_name (die, cu);
12863
12864 /* Ignore functions with missing or empty names. These are actually
12865 illegal according to the DWARF standard. */
12866 if (name == NULL)
12867 {
12868 complaint (_("missing name for subprogram DIE at %s"),
12869 sect_offset_str (die->sect_off));
12870 return;
12871 }
12872
12873 /* Ignore functions with missing or invalid low and high pc attributes. */
12874 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12875 <= PC_BOUNDS_INVALID)
12876 {
12877 attr = dwarf2_attr (die, DW_AT_external, cu);
12878 if (!attr || !DW_UNSND (attr))
12879 complaint (_("cannot get low and high bounds "
12880 "for subprogram DIE at %s"),
12881 sect_offset_str (die->sect_off));
12882 return;
12883 }
12884
12885 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12886 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12887
12888 /* If we have any template arguments, then we must allocate a
12889 different sort of symbol. */
12890 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12891 {
12892 if (child_die->tag == DW_TAG_template_type_param
12893 || child_die->tag == DW_TAG_template_value_param)
12894 {
12895 templ_func = allocate_template_symbol (objfile);
12896 templ_func->subclass = SYMBOL_TEMPLATE;
12897 break;
12898 }
12899 }
12900
12901 newobj = cu->get_builder ()->push_context (0, lowpc);
12902 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12903 (struct symbol *) templ_func);
12904
12905 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12906 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12907 cu->language);
12908
12909 /* If there is a location expression for DW_AT_frame_base, record
12910 it. */
12911 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12912 if (attr != nullptr)
12913 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12914
12915 /* If there is a location for the static link, record it. */
12916 newobj->static_link = NULL;
12917 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12918 if (attr != nullptr)
12919 {
12920 newobj->static_link
12921 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12922 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12923 cu->per_cu->addr_type ());
12924 }
12925
12926 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12927
12928 if (die->child != NULL)
12929 {
12930 child_die = die->child;
12931 while (child_die && child_die->tag)
12932 {
12933 if (child_die->tag == DW_TAG_template_type_param
12934 || child_die->tag == DW_TAG_template_value_param)
12935 {
12936 struct symbol *arg = new_symbol (child_die, NULL, cu);
12937
12938 if (arg != NULL)
12939 template_args.push_back (arg);
12940 }
12941 else
12942 process_die (child_die, cu);
12943 child_die = sibling_die (child_die);
12944 }
12945 }
12946
12947 inherit_abstract_dies (die, cu);
12948
12949 /* If we have a DW_AT_specification, we might need to import using
12950 directives from the context of the specification DIE. See the
12951 comment in determine_prefix. */
12952 if (cu->language == language_cplus
12953 && dwarf2_attr (die, DW_AT_specification, cu))
12954 {
12955 struct dwarf2_cu *spec_cu = cu;
12956 struct die_info *spec_die = die_specification (die, &spec_cu);
12957
12958 while (spec_die)
12959 {
12960 child_die = spec_die->child;
12961 while (child_die && child_die->tag)
12962 {
12963 if (child_die->tag == DW_TAG_imported_module)
12964 process_die (child_die, spec_cu);
12965 child_die = sibling_die (child_die);
12966 }
12967
12968 /* In some cases, GCC generates specification DIEs that
12969 themselves contain DW_AT_specification attributes. */
12970 spec_die = die_specification (spec_die, &spec_cu);
12971 }
12972 }
12973
12974 struct context_stack cstk = cu->get_builder ()->pop_context ();
12975 /* Make a block for the local symbols within. */
12976 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12977 cstk.static_link, lowpc, highpc);
12978
12979 /* For C++, set the block's scope. */
12980 if ((cu->language == language_cplus
12981 || cu->language == language_fortran
12982 || cu->language == language_d
12983 || cu->language == language_rust)
12984 && cu->processing_has_namespace_info)
12985 block_set_scope (block, determine_prefix (die, cu),
12986 &objfile->objfile_obstack);
12987
12988 /* If we have address ranges, record them. */
12989 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12990
12991 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12992
12993 /* Attach template arguments to function. */
12994 if (!template_args.empty ())
12995 {
12996 gdb_assert (templ_func != NULL);
12997
12998 templ_func->n_template_arguments = template_args.size ();
12999 templ_func->template_arguments
13000 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13001 templ_func->n_template_arguments);
13002 memcpy (templ_func->template_arguments,
13003 template_args.data (),
13004 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13005
13006 /* Make sure that the symtab is set on the new symbols. Even
13007 though they don't appear in this symtab directly, other parts
13008 of gdb assume that symbols do, and this is reasonably
13009 true. */
13010 for (symbol *sym : template_args)
13011 symbol_set_symtab (sym, symbol_symtab (templ_func));
13012 }
13013
13014 /* In C++, we can have functions nested inside functions (e.g., when
13015 a function declares a class that has methods). This means that
13016 when we finish processing a function scope, we may need to go
13017 back to building a containing block's symbol lists. */
13018 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13019 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13020
13021 /* If we've finished processing a top-level function, subsequent
13022 symbols go in the file symbol list. */
13023 if (cu->get_builder ()->outermost_context_p ())
13024 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13025 }
13026
13027 /* Process all the DIES contained within a lexical block scope. Start
13028 a new scope, process the dies, and then close the scope. */
13029
13030 static void
13031 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13032 {
13033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13034 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13035 CORE_ADDR lowpc, highpc;
13036 struct die_info *child_die;
13037 CORE_ADDR baseaddr;
13038
13039 baseaddr = objfile->text_section_offset ();
13040
13041 /* Ignore blocks with missing or invalid low and high pc attributes. */
13042 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13043 as multiple lexical blocks? Handling children in a sane way would
13044 be nasty. Might be easier to properly extend generic blocks to
13045 describe ranges. */
13046 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13047 {
13048 case PC_BOUNDS_NOT_PRESENT:
13049 /* DW_TAG_lexical_block has no attributes, process its children as if
13050 there was no wrapping by that DW_TAG_lexical_block.
13051 GCC does no longer produces such DWARF since GCC r224161. */
13052 for (child_die = die->child;
13053 child_die != NULL && child_die->tag;
13054 child_die = sibling_die (child_die))
13055 process_die (child_die, cu);
13056 return;
13057 case PC_BOUNDS_INVALID:
13058 return;
13059 }
13060 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13061 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13062
13063 cu->get_builder ()->push_context (0, lowpc);
13064 if (die->child != NULL)
13065 {
13066 child_die = die->child;
13067 while (child_die && child_die->tag)
13068 {
13069 process_die (child_die, cu);
13070 child_die = sibling_die (child_die);
13071 }
13072 }
13073 inherit_abstract_dies (die, cu);
13074 struct context_stack cstk = cu->get_builder ()->pop_context ();
13075
13076 if (*cu->get_builder ()->get_local_symbols () != NULL
13077 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13078 {
13079 struct block *block
13080 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13081 cstk.start_addr, highpc);
13082
13083 /* Note that recording ranges after traversing children, as we
13084 do here, means that recording a parent's ranges entails
13085 walking across all its children's ranges as they appear in
13086 the address map, which is quadratic behavior.
13087
13088 It would be nicer to record the parent's ranges before
13089 traversing its children, simply overriding whatever you find
13090 there. But since we don't even decide whether to create a
13091 block until after we've traversed its children, that's hard
13092 to do. */
13093 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13094 }
13095 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13096 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13097 }
13098
13099 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13100
13101 static void
13102 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13103 {
13104 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13105 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13106 CORE_ADDR pc, baseaddr;
13107 struct attribute *attr;
13108 struct call_site *call_site, call_site_local;
13109 void **slot;
13110 int nparams;
13111 struct die_info *child_die;
13112
13113 baseaddr = objfile->text_section_offset ();
13114
13115 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13116 if (attr == NULL)
13117 {
13118 /* This was a pre-DWARF-5 GNU extension alias
13119 for DW_AT_call_return_pc. */
13120 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13121 }
13122 if (!attr)
13123 {
13124 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13125 "DIE %s [in module %s]"),
13126 sect_offset_str (die->sect_off), objfile_name (objfile));
13127 return;
13128 }
13129 pc = attr->value_as_address () + baseaddr;
13130 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13131
13132 if (cu->call_site_htab == NULL)
13133 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13134 NULL, &objfile->objfile_obstack,
13135 hashtab_obstack_allocate, NULL);
13136 call_site_local.pc = pc;
13137 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13138 if (*slot != NULL)
13139 {
13140 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13141 "DIE %s [in module %s]"),
13142 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13143 objfile_name (objfile));
13144 return;
13145 }
13146
13147 /* Count parameters at the caller. */
13148
13149 nparams = 0;
13150 for (child_die = die->child; child_die && child_die->tag;
13151 child_die = sibling_die (child_die))
13152 {
13153 if (child_die->tag != DW_TAG_call_site_parameter
13154 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13155 {
13156 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13157 "DW_TAG_call_site child DIE %s [in module %s]"),
13158 child_die->tag, sect_offset_str (child_die->sect_off),
13159 objfile_name (objfile));
13160 continue;
13161 }
13162
13163 nparams++;
13164 }
13165
13166 call_site
13167 = ((struct call_site *)
13168 obstack_alloc (&objfile->objfile_obstack,
13169 sizeof (*call_site)
13170 + (sizeof (*call_site->parameter) * (nparams - 1))));
13171 *slot = call_site;
13172 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13173 call_site->pc = pc;
13174
13175 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13176 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13177 {
13178 struct die_info *func_die;
13179
13180 /* Skip also over DW_TAG_inlined_subroutine. */
13181 for (func_die = die->parent;
13182 func_die && func_die->tag != DW_TAG_subprogram
13183 && func_die->tag != DW_TAG_subroutine_type;
13184 func_die = func_die->parent);
13185
13186 /* DW_AT_call_all_calls is a superset
13187 of DW_AT_call_all_tail_calls. */
13188 if (func_die
13189 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13190 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13191 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13192 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13193 {
13194 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13195 not complete. But keep CALL_SITE for look ups via call_site_htab,
13196 both the initial caller containing the real return address PC and
13197 the final callee containing the current PC of a chain of tail
13198 calls do not need to have the tail call list complete. But any
13199 function candidate for a virtual tail call frame searched via
13200 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13201 determined unambiguously. */
13202 }
13203 else
13204 {
13205 struct type *func_type = NULL;
13206
13207 if (func_die)
13208 func_type = get_die_type (func_die, cu);
13209 if (func_type != NULL)
13210 {
13211 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13212
13213 /* Enlist this call site to the function. */
13214 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13215 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13216 }
13217 else
13218 complaint (_("Cannot find function owning DW_TAG_call_site "
13219 "DIE %s [in module %s]"),
13220 sect_offset_str (die->sect_off), objfile_name (objfile));
13221 }
13222 }
13223
13224 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13225 if (attr == NULL)
13226 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13227 if (attr == NULL)
13228 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13229 if (attr == NULL)
13230 {
13231 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13232 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13233 }
13234 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13235 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13236 /* Keep NULL DWARF_BLOCK. */;
13237 else if (attr->form_is_block ())
13238 {
13239 struct dwarf2_locexpr_baton *dlbaton;
13240
13241 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13242 dlbaton->data = DW_BLOCK (attr)->data;
13243 dlbaton->size = DW_BLOCK (attr)->size;
13244 dlbaton->per_cu = cu->per_cu;
13245
13246 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13247 }
13248 else if (attr->form_is_ref ())
13249 {
13250 struct dwarf2_cu *target_cu = cu;
13251 struct die_info *target_die;
13252
13253 target_die = follow_die_ref (die, attr, &target_cu);
13254 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13255 if (die_is_declaration (target_die, target_cu))
13256 {
13257 const char *target_physname;
13258
13259 /* Prefer the mangled name; otherwise compute the demangled one. */
13260 target_physname = dw2_linkage_name (target_die, target_cu);
13261 if (target_physname == NULL)
13262 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13263 if (target_physname == NULL)
13264 complaint (_("DW_AT_call_target target DIE has invalid "
13265 "physname, for referencing DIE %s [in module %s]"),
13266 sect_offset_str (die->sect_off), objfile_name (objfile));
13267 else
13268 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13269 }
13270 else
13271 {
13272 CORE_ADDR lowpc;
13273
13274 /* DW_AT_entry_pc should be preferred. */
13275 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13276 <= PC_BOUNDS_INVALID)
13277 complaint (_("DW_AT_call_target target DIE has invalid "
13278 "low pc, for referencing DIE %s [in module %s]"),
13279 sect_offset_str (die->sect_off), objfile_name (objfile));
13280 else
13281 {
13282 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13283 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13284 }
13285 }
13286 }
13287 else
13288 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13289 "block nor reference, for DIE %s [in module %s]"),
13290 sect_offset_str (die->sect_off), objfile_name (objfile));
13291
13292 call_site->per_cu = cu->per_cu;
13293
13294 for (child_die = die->child;
13295 child_die && child_die->tag;
13296 child_die = sibling_die (child_die))
13297 {
13298 struct call_site_parameter *parameter;
13299 struct attribute *loc, *origin;
13300
13301 if (child_die->tag != DW_TAG_call_site_parameter
13302 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13303 {
13304 /* Already printed the complaint above. */
13305 continue;
13306 }
13307
13308 gdb_assert (call_site->parameter_count < nparams);
13309 parameter = &call_site->parameter[call_site->parameter_count];
13310
13311 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13312 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13313 register is contained in DW_AT_call_value. */
13314
13315 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13316 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13317 if (origin == NULL)
13318 {
13319 /* This was a pre-DWARF-5 GNU extension alias
13320 for DW_AT_call_parameter. */
13321 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13322 }
13323 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13324 {
13325 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13326
13327 sect_offset sect_off
13328 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13329 if (!cu->header.offset_in_cu_p (sect_off))
13330 {
13331 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13332 binding can be done only inside one CU. Such referenced DIE
13333 therefore cannot be even moved to DW_TAG_partial_unit. */
13334 complaint (_("DW_AT_call_parameter offset is not in CU for "
13335 "DW_TAG_call_site child DIE %s [in module %s]"),
13336 sect_offset_str (child_die->sect_off),
13337 objfile_name (objfile));
13338 continue;
13339 }
13340 parameter->u.param_cu_off
13341 = (cu_offset) (sect_off - cu->header.sect_off);
13342 }
13343 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13344 {
13345 complaint (_("No DW_FORM_block* DW_AT_location for "
13346 "DW_TAG_call_site child DIE %s [in module %s]"),
13347 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13348 continue;
13349 }
13350 else
13351 {
13352 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13353 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13354 if (parameter->u.dwarf_reg != -1)
13355 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13356 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13357 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13358 &parameter->u.fb_offset))
13359 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13360 else
13361 {
13362 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13363 "for DW_FORM_block* DW_AT_location is supported for "
13364 "DW_TAG_call_site child DIE %s "
13365 "[in module %s]"),
13366 sect_offset_str (child_die->sect_off),
13367 objfile_name (objfile));
13368 continue;
13369 }
13370 }
13371
13372 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13373 if (attr == NULL)
13374 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13375 if (attr == NULL || !attr->form_is_block ())
13376 {
13377 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13378 "DW_TAG_call_site child DIE %s [in module %s]"),
13379 sect_offset_str (child_die->sect_off),
13380 objfile_name (objfile));
13381 continue;
13382 }
13383 parameter->value = DW_BLOCK (attr)->data;
13384 parameter->value_size = DW_BLOCK (attr)->size;
13385
13386 /* Parameters are not pre-cleared by memset above. */
13387 parameter->data_value = NULL;
13388 parameter->data_value_size = 0;
13389 call_site->parameter_count++;
13390
13391 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13392 if (attr == NULL)
13393 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13394 if (attr != nullptr)
13395 {
13396 if (!attr->form_is_block ())
13397 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13398 "DW_TAG_call_site child DIE %s [in module %s]"),
13399 sect_offset_str (child_die->sect_off),
13400 objfile_name (objfile));
13401 else
13402 {
13403 parameter->data_value = DW_BLOCK (attr)->data;
13404 parameter->data_value_size = DW_BLOCK (attr)->size;
13405 }
13406 }
13407 }
13408 }
13409
13410 /* Helper function for read_variable. If DIE represents a virtual
13411 table, then return the type of the concrete object that is
13412 associated with the virtual table. Otherwise, return NULL. */
13413
13414 static struct type *
13415 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13416 {
13417 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13418 if (attr == NULL)
13419 return NULL;
13420
13421 /* Find the type DIE. */
13422 struct die_info *type_die = NULL;
13423 struct dwarf2_cu *type_cu = cu;
13424
13425 if (attr->form_is_ref ())
13426 type_die = follow_die_ref (die, attr, &type_cu);
13427 if (type_die == NULL)
13428 return NULL;
13429
13430 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13431 return NULL;
13432 return die_containing_type (type_die, type_cu);
13433 }
13434
13435 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13436
13437 static void
13438 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13439 {
13440 struct rust_vtable_symbol *storage = NULL;
13441
13442 if (cu->language == language_rust)
13443 {
13444 struct type *containing_type = rust_containing_type (die, cu);
13445
13446 if (containing_type != NULL)
13447 {
13448 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13449
13450 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13451 initialize_objfile_symbol (storage);
13452 storage->concrete_type = containing_type;
13453 storage->subclass = SYMBOL_RUST_VTABLE;
13454 }
13455 }
13456
13457 struct symbol *res = new_symbol (die, NULL, cu, storage);
13458 struct attribute *abstract_origin
13459 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13460 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13461 if (res == NULL && loc && abstract_origin)
13462 {
13463 /* We have a variable without a name, but with a location and an abstract
13464 origin. This may be a concrete instance of an abstract variable
13465 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13466 later. */
13467 struct dwarf2_cu *origin_cu = cu;
13468 struct die_info *origin_die
13469 = follow_die_ref (die, abstract_origin, &origin_cu);
13470 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13471 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13472 }
13473 }
13474
13475 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13476 reading .debug_rnglists.
13477 Callback's type should be:
13478 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13479 Return true if the attributes are present and valid, otherwise,
13480 return false. */
13481
13482 template <typename Callback>
13483 static bool
13484 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13485 Callback &&callback)
13486 {
13487 struct dwarf2_per_objfile *dwarf2_per_objfile
13488 = cu->per_cu->dwarf2_per_objfile;
13489 struct objfile *objfile = dwarf2_per_objfile->objfile;
13490 bfd *obfd = objfile->obfd;
13491 /* Base address selection entry. */
13492 CORE_ADDR base;
13493 int found_base;
13494 const gdb_byte *buffer;
13495 CORE_ADDR baseaddr;
13496 bool overflow = false;
13497
13498 found_base = cu->base_known;
13499 base = cu->base_address;
13500
13501 dwarf2_per_objfile->rnglists.read (objfile);
13502 if (offset >= dwarf2_per_objfile->rnglists.size)
13503 {
13504 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13505 offset);
13506 return false;
13507 }
13508 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13509
13510 baseaddr = objfile->text_section_offset ();
13511
13512 while (1)
13513 {
13514 /* Initialize it due to a false compiler warning. */
13515 CORE_ADDR range_beginning = 0, range_end = 0;
13516 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13517 + dwarf2_per_objfile->rnglists.size);
13518 unsigned int bytes_read;
13519
13520 if (buffer == buf_end)
13521 {
13522 overflow = true;
13523 break;
13524 }
13525 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13526 switch (rlet)
13527 {
13528 case DW_RLE_end_of_list:
13529 break;
13530 case DW_RLE_base_address:
13531 if (buffer + cu->header.addr_size > buf_end)
13532 {
13533 overflow = true;
13534 break;
13535 }
13536 base = cu->header.read_address (obfd, buffer, &bytes_read);
13537 found_base = 1;
13538 buffer += bytes_read;
13539 break;
13540 case DW_RLE_start_length:
13541 if (buffer + cu->header.addr_size > buf_end)
13542 {
13543 overflow = true;
13544 break;
13545 }
13546 range_beginning = cu->header.read_address (obfd, buffer,
13547 &bytes_read);
13548 buffer += bytes_read;
13549 range_end = (range_beginning
13550 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13551 buffer += bytes_read;
13552 if (buffer > buf_end)
13553 {
13554 overflow = true;
13555 break;
13556 }
13557 break;
13558 case DW_RLE_offset_pair:
13559 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13560 buffer += bytes_read;
13561 if (buffer > buf_end)
13562 {
13563 overflow = true;
13564 break;
13565 }
13566 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13567 buffer += bytes_read;
13568 if (buffer > buf_end)
13569 {
13570 overflow = true;
13571 break;
13572 }
13573 break;
13574 case DW_RLE_start_end:
13575 if (buffer + 2 * cu->header.addr_size > buf_end)
13576 {
13577 overflow = true;
13578 break;
13579 }
13580 range_beginning = cu->header.read_address (obfd, buffer,
13581 &bytes_read);
13582 buffer += bytes_read;
13583 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13584 buffer += bytes_read;
13585 break;
13586 default:
13587 complaint (_("Invalid .debug_rnglists data (no base address)"));
13588 return false;
13589 }
13590 if (rlet == DW_RLE_end_of_list || overflow)
13591 break;
13592 if (rlet == DW_RLE_base_address)
13593 continue;
13594
13595 if (!found_base)
13596 {
13597 /* We have no valid base address for the ranges
13598 data. */
13599 complaint (_("Invalid .debug_rnglists data (no base address)"));
13600 return false;
13601 }
13602
13603 if (range_beginning > range_end)
13604 {
13605 /* Inverted range entries are invalid. */
13606 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13607 return false;
13608 }
13609
13610 /* Empty range entries have no effect. */
13611 if (range_beginning == range_end)
13612 continue;
13613
13614 range_beginning += base;
13615 range_end += base;
13616
13617 /* A not-uncommon case of bad debug info.
13618 Don't pollute the addrmap with bad data. */
13619 if (range_beginning + baseaddr == 0
13620 && !dwarf2_per_objfile->has_section_at_zero)
13621 {
13622 complaint (_(".debug_rnglists entry has start address of zero"
13623 " [in module %s]"), objfile_name (objfile));
13624 continue;
13625 }
13626
13627 callback (range_beginning, range_end);
13628 }
13629
13630 if (overflow)
13631 {
13632 complaint (_("Offset %d is not terminated "
13633 "for DW_AT_ranges attribute"),
13634 offset);
13635 return false;
13636 }
13637
13638 return true;
13639 }
13640
13641 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13642 Callback's type should be:
13643 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13644 Return 1 if the attributes are present and valid, otherwise, return 0. */
13645
13646 template <typename Callback>
13647 static int
13648 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13649 Callback &&callback)
13650 {
13651 struct dwarf2_per_objfile *dwarf2_per_objfile
13652 = cu->per_cu->dwarf2_per_objfile;
13653 struct objfile *objfile = dwarf2_per_objfile->objfile;
13654 struct comp_unit_head *cu_header = &cu->header;
13655 bfd *obfd = objfile->obfd;
13656 unsigned int addr_size = cu_header->addr_size;
13657 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13658 /* Base address selection entry. */
13659 CORE_ADDR base;
13660 int found_base;
13661 unsigned int dummy;
13662 const gdb_byte *buffer;
13663 CORE_ADDR baseaddr;
13664
13665 if (cu_header->version >= 5)
13666 return dwarf2_rnglists_process (offset, cu, callback);
13667
13668 found_base = cu->base_known;
13669 base = cu->base_address;
13670
13671 dwarf2_per_objfile->ranges.read (objfile);
13672 if (offset >= dwarf2_per_objfile->ranges.size)
13673 {
13674 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13675 offset);
13676 return 0;
13677 }
13678 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13679
13680 baseaddr = objfile->text_section_offset ();
13681
13682 while (1)
13683 {
13684 CORE_ADDR range_beginning, range_end;
13685
13686 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13687 buffer += addr_size;
13688 range_end = cu->header.read_address (obfd, buffer, &dummy);
13689 buffer += addr_size;
13690 offset += 2 * addr_size;
13691
13692 /* An end of list marker is a pair of zero addresses. */
13693 if (range_beginning == 0 && range_end == 0)
13694 /* Found the end of list entry. */
13695 break;
13696
13697 /* Each base address selection entry is a pair of 2 values.
13698 The first is the largest possible address, the second is
13699 the base address. Check for a base address here. */
13700 if ((range_beginning & mask) == mask)
13701 {
13702 /* If we found the largest possible address, then we already
13703 have the base address in range_end. */
13704 base = range_end;
13705 found_base = 1;
13706 continue;
13707 }
13708
13709 if (!found_base)
13710 {
13711 /* We have no valid base address for the ranges
13712 data. */
13713 complaint (_("Invalid .debug_ranges data (no base address)"));
13714 return 0;
13715 }
13716
13717 if (range_beginning > range_end)
13718 {
13719 /* Inverted range entries are invalid. */
13720 complaint (_("Invalid .debug_ranges data (inverted range)"));
13721 return 0;
13722 }
13723
13724 /* Empty range entries have no effect. */
13725 if (range_beginning == range_end)
13726 continue;
13727
13728 range_beginning += base;
13729 range_end += base;
13730
13731 /* A not-uncommon case of bad debug info.
13732 Don't pollute the addrmap with bad data. */
13733 if (range_beginning + baseaddr == 0
13734 && !dwarf2_per_objfile->has_section_at_zero)
13735 {
13736 complaint (_(".debug_ranges entry has start address of zero"
13737 " [in module %s]"), objfile_name (objfile));
13738 continue;
13739 }
13740
13741 callback (range_beginning, range_end);
13742 }
13743
13744 return 1;
13745 }
13746
13747 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13748 Return 1 if the attributes are present and valid, otherwise, return 0.
13749 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13750
13751 static int
13752 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13753 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13754 dwarf2_psymtab *ranges_pst)
13755 {
13756 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13758 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13759 int low_set = 0;
13760 CORE_ADDR low = 0;
13761 CORE_ADDR high = 0;
13762 int retval;
13763
13764 retval = dwarf2_ranges_process (offset, cu,
13765 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13766 {
13767 if (ranges_pst != NULL)
13768 {
13769 CORE_ADDR lowpc;
13770 CORE_ADDR highpc;
13771
13772 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13773 range_beginning + baseaddr)
13774 - baseaddr);
13775 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13776 range_end + baseaddr)
13777 - baseaddr);
13778 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13779 lowpc, highpc - 1, ranges_pst);
13780 }
13781
13782 /* FIXME: This is recording everything as a low-high
13783 segment of consecutive addresses. We should have a
13784 data structure for discontiguous block ranges
13785 instead. */
13786 if (! low_set)
13787 {
13788 low = range_beginning;
13789 high = range_end;
13790 low_set = 1;
13791 }
13792 else
13793 {
13794 if (range_beginning < low)
13795 low = range_beginning;
13796 if (range_end > high)
13797 high = range_end;
13798 }
13799 });
13800 if (!retval)
13801 return 0;
13802
13803 if (! low_set)
13804 /* If the first entry is an end-of-list marker, the range
13805 describes an empty scope, i.e. no instructions. */
13806 return 0;
13807
13808 if (low_return)
13809 *low_return = low;
13810 if (high_return)
13811 *high_return = high;
13812 return 1;
13813 }
13814
13815 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13816 definition for the return value. *LOWPC and *HIGHPC are set iff
13817 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13818
13819 static enum pc_bounds_kind
13820 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13821 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13822 dwarf2_psymtab *pst)
13823 {
13824 struct dwarf2_per_objfile *dwarf2_per_objfile
13825 = cu->per_cu->dwarf2_per_objfile;
13826 struct attribute *attr;
13827 struct attribute *attr_high;
13828 CORE_ADDR low = 0;
13829 CORE_ADDR high = 0;
13830 enum pc_bounds_kind ret;
13831
13832 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13833 if (attr_high)
13834 {
13835 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13836 if (attr != nullptr)
13837 {
13838 low = attr->value_as_address ();
13839 high = attr_high->value_as_address ();
13840 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13841 high += low;
13842 }
13843 else
13844 /* Found high w/o low attribute. */
13845 return PC_BOUNDS_INVALID;
13846
13847 /* Found consecutive range of addresses. */
13848 ret = PC_BOUNDS_HIGH_LOW;
13849 }
13850 else
13851 {
13852 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13853 if (attr != NULL)
13854 {
13855 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13856 We take advantage of the fact that DW_AT_ranges does not appear
13857 in DW_TAG_compile_unit of DWO files. */
13858 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13859 unsigned int ranges_offset = (DW_UNSND (attr)
13860 + (need_ranges_base
13861 ? cu->ranges_base
13862 : 0));
13863
13864 /* Value of the DW_AT_ranges attribute is the offset in the
13865 .debug_ranges section. */
13866 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13867 return PC_BOUNDS_INVALID;
13868 /* Found discontinuous range of addresses. */
13869 ret = PC_BOUNDS_RANGES;
13870 }
13871 else
13872 return PC_BOUNDS_NOT_PRESENT;
13873 }
13874
13875 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13876 if (high <= low)
13877 return PC_BOUNDS_INVALID;
13878
13879 /* When using the GNU linker, .gnu.linkonce. sections are used to
13880 eliminate duplicate copies of functions and vtables and such.
13881 The linker will arbitrarily choose one and discard the others.
13882 The AT_*_pc values for such functions refer to local labels in
13883 these sections. If the section from that file was discarded, the
13884 labels are not in the output, so the relocs get a value of 0.
13885 If this is a discarded function, mark the pc bounds as invalid,
13886 so that GDB will ignore it. */
13887 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13888 return PC_BOUNDS_INVALID;
13889
13890 *lowpc = low;
13891 if (highpc)
13892 *highpc = high;
13893 return ret;
13894 }
13895
13896 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13897 its low and high PC addresses. Do nothing if these addresses could not
13898 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13899 and HIGHPC to the high address if greater than HIGHPC. */
13900
13901 static void
13902 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13903 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13904 struct dwarf2_cu *cu)
13905 {
13906 CORE_ADDR low, high;
13907 struct die_info *child = die->child;
13908
13909 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13910 {
13911 *lowpc = std::min (*lowpc, low);
13912 *highpc = std::max (*highpc, high);
13913 }
13914
13915 /* If the language does not allow nested subprograms (either inside
13916 subprograms or lexical blocks), we're done. */
13917 if (cu->language != language_ada)
13918 return;
13919
13920 /* Check all the children of the given DIE. If it contains nested
13921 subprograms, then check their pc bounds. Likewise, we need to
13922 check lexical blocks as well, as they may also contain subprogram
13923 definitions. */
13924 while (child && child->tag)
13925 {
13926 if (child->tag == DW_TAG_subprogram
13927 || child->tag == DW_TAG_lexical_block)
13928 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13929 child = sibling_die (child);
13930 }
13931 }
13932
13933 /* Get the low and high pc's represented by the scope DIE, and store
13934 them in *LOWPC and *HIGHPC. If the correct values can't be
13935 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13936
13937 static void
13938 get_scope_pc_bounds (struct die_info *die,
13939 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13940 struct dwarf2_cu *cu)
13941 {
13942 CORE_ADDR best_low = (CORE_ADDR) -1;
13943 CORE_ADDR best_high = (CORE_ADDR) 0;
13944 CORE_ADDR current_low, current_high;
13945
13946 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13947 >= PC_BOUNDS_RANGES)
13948 {
13949 best_low = current_low;
13950 best_high = current_high;
13951 }
13952 else
13953 {
13954 struct die_info *child = die->child;
13955
13956 while (child && child->tag)
13957 {
13958 switch (child->tag) {
13959 case DW_TAG_subprogram:
13960 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13961 break;
13962 case DW_TAG_namespace:
13963 case DW_TAG_module:
13964 /* FIXME: carlton/2004-01-16: Should we do this for
13965 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13966 that current GCC's always emit the DIEs corresponding
13967 to definitions of methods of classes as children of a
13968 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13969 the DIEs giving the declarations, which could be
13970 anywhere). But I don't see any reason why the
13971 standards says that they have to be there. */
13972 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13973
13974 if (current_low != ((CORE_ADDR) -1))
13975 {
13976 best_low = std::min (best_low, current_low);
13977 best_high = std::max (best_high, current_high);
13978 }
13979 break;
13980 default:
13981 /* Ignore. */
13982 break;
13983 }
13984
13985 child = sibling_die (child);
13986 }
13987 }
13988
13989 *lowpc = best_low;
13990 *highpc = best_high;
13991 }
13992
13993 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13994 in DIE. */
13995
13996 static void
13997 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13998 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13999 {
14000 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14001 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14002 struct attribute *attr;
14003 struct attribute *attr_high;
14004
14005 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14006 if (attr_high)
14007 {
14008 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14009 if (attr != nullptr)
14010 {
14011 CORE_ADDR low = attr->value_as_address ();
14012 CORE_ADDR high = attr_high->value_as_address ();
14013
14014 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14015 high += low;
14016
14017 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14018 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14019 cu->get_builder ()->record_block_range (block, low, high - 1);
14020 }
14021 }
14022
14023 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14024 if (attr != nullptr)
14025 {
14026 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14027 We take advantage of the fact that DW_AT_ranges does not appear
14028 in DW_TAG_compile_unit of DWO files. */
14029 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14030
14031 /* The value of the DW_AT_ranges attribute is the offset of the
14032 address range list in the .debug_ranges section. */
14033 unsigned long offset = (DW_UNSND (attr)
14034 + (need_ranges_base ? cu->ranges_base : 0));
14035
14036 std::vector<blockrange> blockvec;
14037 dwarf2_ranges_process (offset, cu,
14038 [&] (CORE_ADDR start, CORE_ADDR end)
14039 {
14040 start += baseaddr;
14041 end += baseaddr;
14042 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14043 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14044 cu->get_builder ()->record_block_range (block, start, end - 1);
14045 blockvec.emplace_back (start, end);
14046 });
14047
14048 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14049 }
14050 }
14051
14052 /* Check whether the producer field indicates either of GCC < 4.6, or the
14053 Intel C/C++ compiler, and cache the result in CU. */
14054
14055 static void
14056 check_producer (struct dwarf2_cu *cu)
14057 {
14058 int major, minor;
14059
14060 if (cu->producer == NULL)
14061 {
14062 /* For unknown compilers expect their behavior is DWARF version
14063 compliant.
14064
14065 GCC started to support .debug_types sections by -gdwarf-4 since
14066 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14067 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14068 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14069 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14070 }
14071 else if (producer_is_gcc (cu->producer, &major, &minor))
14072 {
14073 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14074 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14075 }
14076 else if (producer_is_icc (cu->producer, &major, &minor))
14077 {
14078 cu->producer_is_icc = true;
14079 cu->producer_is_icc_lt_14 = major < 14;
14080 }
14081 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14082 cu->producer_is_codewarrior = true;
14083 else
14084 {
14085 /* For other non-GCC compilers, expect their behavior is DWARF version
14086 compliant. */
14087 }
14088
14089 cu->checked_producer = true;
14090 }
14091
14092 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14093 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14094 during 4.6.0 experimental. */
14095
14096 static bool
14097 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14098 {
14099 if (!cu->checked_producer)
14100 check_producer (cu);
14101
14102 return cu->producer_is_gxx_lt_4_6;
14103 }
14104
14105
14106 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14107 with incorrect is_stmt attributes. */
14108
14109 static bool
14110 producer_is_codewarrior (struct dwarf2_cu *cu)
14111 {
14112 if (!cu->checked_producer)
14113 check_producer (cu);
14114
14115 return cu->producer_is_codewarrior;
14116 }
14117
14118 /* Return the default accessibility type if it is not overridden by
14119 DW_AT_accessibility. */
14120
14121 static enum dwarf_access_attribute
14122 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14123 {
14124 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14125 {
14126 /* The default DWARF 2 accessibility for members is public, the default
14127 accessibility for inheritance is private. */
14128
14129 if (die->tag != DW_TAG_inheritance)
14130 return DW_ACCESS_public;
14131 else
14132 return DW_ACCESS_private;
14133 }
14134 else
14135 {
14136 /* DWARF 3+ defines the default accessibility a different way. The same
14137 rules apply now for DW_TAG_inheritance as for the members and it only
14138 depends on the container kind. */
14139
14140 if (die->parent->tag == DW_TAG_class_type)
14141 return DW_ACCESS_private;
14142 else
14143 return DW_ACCESS_public;
14144 }
14145 }
14146
14147 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14148 offset. If the attribute was not found return 0, otherwise return
14149 1. If it was found but could not properly be handled, set *OFFSET
14150 to 0. */
14151
14152 static int
14153 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14154 LONGEST *offset)
14155 {
14156 struct attribute *attr;
14157
14158 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14159 if (attr != NULL)
14160 {
14161 *offset = 0;
14162
14163 /* Note that we do not check for a section offset first here.
14164 This is because DW_AT_data_member_location is new in DWARF 4,
14165 so if we see it, we can assume that a constant form is really
14166 a constant and not a section offset. */
14167 if (attr->form_is_constant ())
14168 *offset = dwarf2_get_attr_constant_value (attr, 0);
14169 else if (attr->form_is_section_offset ())
14170 dwarf2_complex_location_expr_complaint ();
14171 else if (attr->form_is_block ())
14172 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14173 else
14174 dwarf2_complex_location_expr_complaint ();
14175
14176 return 1;
14177 }
14178
14179 return 0;
14180 }
14181
14182 /* Add an aggregate field to the field list. */
14183
14184 static void
14185 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14186 struct dwarf2_cu *cu)
14187 {
14188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14189 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14190 struct nextfield *new_field;
14191 struct attribute *attr;
14192 struct field *fp;
14193 const char *fieldname = "";
14194
14195 if (die->tag == DW_TAG_inheritance)
14196 {
14197 fip->baseclasses.emplace_back ();
14198 new_field = &fip->baseclasses.back ();
14199 }
14200 else
14201 {
14202 fip->fields.emplace_back ();
14203 new_field = &fip->fields.back ();
14204 }
14205
14206 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14207 if (attr != nullptr)
14208 new_field->accessibility = DW_UNSND (attr);
14209 else
14210 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14211 if (new_field->accessibility != DW_ACCESS_public)
14212 fip->non_public_fields = 1;
14213
14214 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14215 if (attr != nullptr)
14216 new_field->virtuality = DW_UNSND (attr);
14217 else
14218 new_field->virtuality = DW_VIRTUALITY_none;
14219
14220 fp = &new_field->field;
14221
14222 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14223 {
14224 LONGEST offset;
14225
14226 /* Data member other than a C++ static data member. */
14227
14228 /* Get type of field. */
14229 fp->type = die_type (die, cu);
14230
14231 SET_FIELD_BITPOS (*fp, 0);
14232
14233 /* Get bit size of field (zero if none). */
14234 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14235 if (attr != nullptr)
14236 {
14237 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14238 }
14239 else
14240 {
14241 FIELD_BITSIZE (*fp) = 0;
14242 }
14243
14244 /* Get bit offset of field. */
14245 if (handle_data_member_location (die, cu, &offset))
14246 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14247 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14248 if (attr != nullptr)
14249 {
14250 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14251 {
14252 /* For big endian bits, the DW_AT_bit_offset gives the
14253 additional bit offset from the MSB of the containing
14254 anonymous object to the MSB of the field. We don't
14255 have to do anything special since we don't need to
14256 know the size of the anonymous object. */
14257 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14258 }
14259 else
14260 {
14261 /* For little endian bits, compute the bit offset to the
14262 MSB of the anonymous object, subtract off the number of
14263 bits from the MSB of the field to the MSB of the
14264 object, and then subtract off the number of bits of
14265 the field itself. The result is the bit offset of
14266 the LSB of the field. */
14267 int anonymous_size;
14268 int bit_offset = DW_UNSND (attr);
14269
14270 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14271 if (attr != nullptr)
14272 {
14273 /* The size of the anonymous object containing
14274 the bit field is explicit, so use the
14275 indicated size (in bytes). */
14276 anonymous_size = DW_UNSND (attr);
14277 }
14278 else
14279 {
14280 /* The size of the anonymous object containing
14281 the bit field must be inferred from the type
14282 attribute of the data member containing the
14283 bit field. */
14284 anonymous_size = TYPE_LENGTH (fp->type);
14285 }
14286 SET_FIELD_BITPOS (*fp,
14287 (FIELD_BITPOS (*fp)
14288 + anonymous_size * bits_per_byte
14289 - bit_offset - FIELD_BITSIZE (*fp)));
14290 }
14291 }
14292 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14293 if (attr != NULL)
14294 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14295 + dwarf2_get_attr_constant_value (attr, 0)));
14296
14297 /* Get name of field. */
14298 fieldname = dwarf2_name (die, cu);
14299 if (fieldname == NULL)
14300 fieldname = "";
14301
14302 /* The name is already allocated along with this objfile, so we don't
14303 need to duplicate it for the type. */
14304 fp->name = fieldname;
14305
14306 /* Change accessibility for artificial fields (e.g. virtual table
14307 pointer or virtual base class pointer) to private. */
14308 if (dwarf2_attr (die, DW_AT_artificial, cu))
14309 {
14310 FIELD_ARTIFICIAL (*fp) = 1;
14311 new_field->accessibility = DW_ACCESS_private;
14312 fip->non_public_fields = 1;
14313 }
14314 }
14315 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14316 {
14317 /* C++ static member. */
14318
14319 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14320 is a declaration, but all versions of G++ as of this writing
14321 (so through at least 3.2.1) incorrectly generate
14322 DW_TAG_variable tags. */
14323
14324 const char *physname;
14325
14326 /* Get name of field. */
14327 fieldname = dwarf2_name (die, cu);
14328 if (fieldname == NULL)
14329 return;
14330
14331 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14332 if (attr
14333 /* Only create a symbol if this is an external value.
14334 new_symbol checks this and puts the value in the global symbol
14335 table, which we want. If it is not external, new_symbol
14336 will try to put the value in cu->list_in_scope which is wrong. */
14337 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14338 {
14339 /* A static const member, not much different than an enum as far as
14340 we're concerned, except that we can support more types. */
14341 new_symbol (die, NULL, cu);
14342 }
14343
14344 /* Get physical name. */
14345 physname = dwarf2_physname (fieldname, die, cu);
14346
14347 /* The name is already allocated along with this objfile, so we don't
14348 need to duplicate it for the type. */
14349 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14350 FIELD_TYPE (*fp) = die_type (die, cu);
14351 FIELD_NAME (*fp) = fieldname;
14352 }
14353 else if (die->tag == DW_TAG_inheritance)
14354 {
14355 LONGEST offset;
14356
14357 /* C++ base class field. */
14358 if (handle_data_member_location (die, cu, &offset))
14359 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14360 FIELD_BITSIZE (*fp) = 0;
14361 FIELD_TYPE (*fp) = die_type (die, cu);
14362 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14363 }
14364 else if (die->tag == DW_TAG_variant_part)
14365 {
14366 /* process_structure_scope will treat this DIE as a union. */
14367 process_structure_scope (die, cu);
14368
14369 /* The variant part is relative to the start of the enclosing
14370 structure. */
14371 SET_FIELD_BITPOS (*fp, 0);
14372 fp->type = get_die_type (die, cu);
14373 fp->artificial = 1;
14374 fp->name = "<<variant>>";
14375
14376 /* Normally a DW_TAG_variant_part won't have a size, but our
14377 representation requires one, so set it to the maximum of the
14378 child sizes, being sure to account for the offset at which
14379 each child is seen. */
14380 if (TYPE_LENGTH (fp->type) == 0)
14381 {
14382 unsigned max = 0;
14383 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14384 {
14385 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14386 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14387 if (len > max)
14388 max = len;
14389 }
14390 TYPE_LENGTH (fp->type) = max;
14391 }
14392 }
14393 else
14394 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14395 }
14396
14397 /* Can the type given by DIE define another type? */
14398
14399 static bool
14400 type_can_define_types (const struct die_info *die)
14401 {
14402 switch (die->tag)
14403 {
14404 case DW_TAG_typedef:
14405 case DW_TAG_class_type:
14406 case DW_TAG_structure_type:
14407 case DW_TAG_union_type:
14408 case DW_TAG_enumeration_type:
14409 return true;
14410
14411 default:
14412 return false;
14413 }
14414 }
14415
14416 /* Add a type definition defined in the scope of the FIP's class. */
14417
14418 static void
14419 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14420 struct dwarf2_cu *cu)
14421 {
14422 struct decl_field fp;
14423 memset (&fp, 0, sizeof (fp));
14424
14425 gdb_assert (type_can_define_types (die));
14426
14427 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14428 fp.name = dwarf2_name (die, cu);
14429 fp.type = read_type_die (die, cu);
14430
14431 /* Save accessibility. */
14432 enum dwarf_access_attribute accessibility;
14433 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14434 if (attr != NULL)
14435 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14436 else
14437 accessibility = dwarf2_default_access_attribute (die, cu);
14438 switch (accessibility)
14439 {
14440 case DW_ACCESS_public:
14441 /* The assumed value if neither private nor protected. */
14442 break;
14443 case DW_ACCESS_private:
14444 fp.is_private = 1;
14445 break;
14446 case DW_ACCESS_protected:
14447 fp.is_protected = 1;
14448 break;
14449 default:
14450 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14451 }
14452
14453 if (die->tag == DW_TAG_typedef)
14454 fip->typedef_field_list.push_back (fp);
14455 else
14456 fip->nested_types_list.push_back (fp);
14457 }
14458
14459 /* Create the vector of fields, and attach it to the type. */
14460
14461 static void
14462 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14463 struct dwarf2_cu *cu)
14464 {
14465 int nfields = fip->nfields ();
14466
14467 /* Record the field count, allocate space for the array of fields,
14468 and create blank accessibility bitfields if necessary. */
14469 TYPE_NFIELDS (type) = nfields;
14470 TYPE_FIELDS (type) = (struct field *)
14471 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14472
14473 if (fip->non_public_fields && cu->language != language_ada)
14474 {
14475 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14476
14477 TYPE_FIELD_PRIVATE_BITS (type) =
14478 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14479 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14480
14481 TYPE_FIELD_PROTECTED_BITS (type) =
14482 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14483 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14484
14485 TYPE_FIELD_IGNORE_BITS (type) =
14486 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14487 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14488 }
14489
14490 /* If the type has baseclasses, allocate and clear a bit vector for
14491 TYPE_FIELD_VIRTUAL_BITS. */
14492 if (!fip->baseclasses.empty () && cu->language != language_ada)
14493 {
14494 int num_bytes = B_BYTES (fip->baseclasses.size ());
14495 unsigned char *pointer;
14496
14497 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14498 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14499 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14500 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14501 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14502 }
14503
14504 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14505 {
14506 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14507
14508 for (int index = 0; index < nfields; ++index)
14509 {
14510 struct nextfield &field = fip->fields[index];
14511
14512 if (field.variant.is_discriminant)
14513 di->discriminant_index = index;
14514 else if (field.variant.default_branch)
14515 di->default_index = index;
14516 else
14517 di->discriminants[index] = field.variant.discriminant_value;
14518 }
14519 }
14520
14521 /* Copy the saved-up fields into the field vector. */
14522 for (int i = 0; i < nfields; ++i)
14523 {
14524 struct nextfield &field
14525 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14526 : fip->fields[i - fip->baseclasses.size ()]);
14527
14528 TYPE_FIELD (type, i) = field.field;
14529 switch (field.accessibility)
14530 {
14531 case DW_ACCESS_private:
14532 if (cu->language != language_ada)
14533 SET_TYPE_FIELD_PRIVATE (type, i);
14534 break;
14535
14536 case DW_ACCESS_protected:
14537 if (cu->language != language_ada)
14538 SET_TYPE_FIELD_PROTECTED (type, i);
14539 break;
14540
14541 case DW_ACCESS_public:
14542 break;
14543
14544 default:
14545 /* Unknown accessibility. Complain and treat it as public. */
14546 {
14547 complaint (_("unsupported accessibility %d"),
14548 field.accessibility);
14549 }
14550 break;
14551 }
14552 if (i < fip->baseclasses.size ())
14553 {
14554 switch (field.virtuality)
14555 {
14556 case DW_VIRTUALITY_virtual:
14557 case DW_VIRTUALITY_pure_virtual:
14558 if (cu->language == language_ada)
14559 error (_("unexpected virtuality in component of Ada type"));
14560 SET_TYPE_FIELD_VIRTUAL (type, i);
14561 break;
14562 }
14563 }
14564 }
14565 }
14566
14567 /* Return true if this member function is a constructor, false
14568 otherwise. */
14569
14570 static int
14571 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14572 {
14573 const char *fieldname;
14574 const char *type_name;
14575 int len;
14576
14577 if (die->parent == NULL)
14578 return 0;
14579
14580 if (die->parent->tag != DW_TAG_structure_type
14581 && die->parent->tag != DW_TAG_union_type
14582 && die->parent->tag != DW_TAG_class_type)
14583 return 0;
14584
14585 fieldname = dwarf2_name (die, cu);
14586 type_name = dwarf2_name (die->parent, cu);
14587 if (fieldname == NULL || type_name == NULL)
14588 return 0;
14589
14590 len = strlen (fieldname);
14591 return (strncmp (fieldname, type_name, len) == 0
14592 && (type_name[len] == '\0' || type_name[len] == '<'));
14593 }
14594
14595 /* Check if the given VALUE is a recognized enum
14596 dwarf_defaulted_attribute constant according to DWARF5 spec,
14597 Table 7.24. */
14598
14599 static bool
14600 is_valid_DW_AT_defaulted (ULONGEST value)
14601 {
14602 switch (value)
14603 {
14604 case DW_DEFAULTED_no:
14605 case DW_DEFAULTED_in_class:
14606 case DW_DEFAULTED_out_of_class:
14607 return true;
14608 }
14609
14610 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14611 return false;
14612 }
14613
14614 /* Add a member function to the proper fieldlist. */
14615
14616 static void
14617 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14618 struct type *type, struct dwarf2_cu *cu)
14619 {
14620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14621 struct attribute *attr;
14622 int i;
14623 struct fnfieldlist *flp = nullptr;
14624 struct fn_field *fnp;
14625 const char *fieldname;
14626 struct type *this_type;
14627 enum dwarf_access_attribute accessibility;
14628
14629 if (cu->language == language_ada)
14630 error (_("unexpected member function in Ada type"));
14631
14632 /* Get name of member function. */
14633 fieldname = dwarf2_name (die, cu);
14634 if (fieldname == NULL)
14635 return;
14636
14637 /* Look up member function name in fieldlist. */
14638 for (i = 0; i < fip->fnfieldlists.size (); i++)
14639 {
14640 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14641 {
14642 flp = &fip->fnfieldlists[i];
14643 break;
14644 }
14645 }
14646
14647 /* Create a new fnfieldlist if necessary. */
14648 if (flp == nullptr)
14649 {
14650 fip->fnfieldlists.emplace_back ();
14651 flp = &fip->fnfieldlists.back ();
14652 flp->name = fieldname;
14653 i = fip->fnfieldlists.size () - 1;
14654 }
14655
14656 /* Create a new member function field and add it to the vector of
14657 fnfieldlists. */
14658 flp->fnfields.emplace_back ();
14659 fnp = &flp->fnfields.back ();
14660
14661 /* Delay processing of the physname until later. */
14662 if (cu->language == language_cplus)
14663 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14664 die, cu);
14665 else
14666 {
14667 const char *physname = dwarf2_physname (fieldname, die, cu);
14668 fnp->physname = physname ? physname : "";
14669 }
14670
14671 fnp->type = alloc_type (objfile);
14672 this_type = read_type_die (die, cu);
14673 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14674 {
14675 int nparams = TYPE_NFIELDS (this_type);
14676
14677 /* TYPE is the domain of this method, and THIS_TYPE is the type
14678 of the method itself (TYPE_CODE_METHOD). */
14679 smash_to_method_type (fnp->type, type,
14680 TYPE_TARGET_TYPE (this_type),
14681 TYPE_FIELDS (this_type),
14682 TYPE_NFIELDS (this_type),
14683 TYPE_VARARGS (this_type));
14684
14685 /* Handle static member functions.
14686 Dwarf2 has no clean way to discern C++ static and non-static
14687 member functions. G++ helps GDB by marking the first
14688 parameter for non-static member functions (which is the this
14689 pointer) as artificial. We obtain this information from
14690 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14691 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14692 fnp->voffset = VOFFSET_STATIC;
14693 }
14694 else
14695 complaint (_("member function type missing for '%s'"),
14696 dwarf2_full_name (fieldname, die, cu));
14697
14698 /* Get fcontext from DW_AT_containing_type if present. */
14699 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14700 fnp->fcontext = die_containing_type (die, cu);
14701
14702 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14703 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14704
14705 /* Get accessibility. */
14706 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14707 if (attr != nullptr)
14708 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14709 else
14710 accessibility = dwarf2_default_access_attribute (die, cu);
14711 switch (accessibility)
14712 {
14713 case DW_ACCESS_private:
14714 fnp->is_private = 1;
14715 break;
14716 case DW_ACCESS_protected:
14717 fnp->is_protected = 1;
14718 break;
14719 }
14720
14721 /* Check for artificial methods. */
14722 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14723 if (attr && DW_UNSND (attr) != 0)
14724 fnp->is_artificial = 1;
14725
14726 /* Check for defaulted methods. */
14727 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14728 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14729 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14730
14731 /* Check for deleted methods. */
14732 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14733 if (attr != nullptr && DW_UNSND (attr) != 0)
14734 fnp->is_deleted = 1;
14735
14736 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14737
14738 /* Get index in virtual function table if it is a virtual member
14739 function. For older versions of GCC, this is an offset in the
14740 appropriate virtual table, as specified by DW_AT_containing_type.
14741 For everyone else, it is an expression to be evaluated relative
14742 to the object address. */
14743
14744 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14745 if (attr != nullptr)
14746 {
14747 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14748 {
14749 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14750 {
14751 /* Old-style GCC. */
14752 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14753 }
14754 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14755 || (DW_BLOCK (attr)->size > 1
14756 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14757 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14758 {
14759 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14760 if ((fnp->voffset % cu->header.addr_size) != 0)
14761 dwarf2_complex_location_expr_complaint ();
14762 else
14763 fnp->voffset /= cu->header.addr_size;
14764 fnp->voffset += 2;
14765 }
14766 else
14767 dwarf2_complex_location_expr_complaint ();
14768
14769 if (!fnp->fcontext)
14770 {
14771 /* If there is no `this' field and no DW_AT_containing_type,
14772 we cannot actually find a base class context for the
14773 vtable! */
14774 if (TYPE_NFIELDS (this_type) == 0
14775 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14776 {
14777 complaint (_("cannot determine context for virtual member "
14778 "function \"%s\" (offset %s)"),
14779 fieldname, sect_offset_str (die->sect_off));
14780 }
14781 else
14782 {
14783 fnp->fcontext
14784 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14785 }
14786 }
14787 }
14788 else if (attr->form_is_section_offset ())
14789 {
14790 dwarf2_complex_location_expr_complaint ();
14791 }
14792 else
14793 {
14794 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14795 fieldname);
14796 }
14797 }
14798 else
14799 {
14800 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14801 if (attr && DW_UNSND (attr))
14802 {
14803 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14804 complaint (_("Member function \"%s\" (offset %s) is virtual "
14805 "but the vtable offset is not specified"),
14806 fieldname, sect_offset_str (die->sect_off));
14807 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14808 TYPE_CPLUS_DYNAMIC (type) = 1;
14809 }
14810 }
14811 }
14812
14813 /* Create the vector of member function fields, and attach it to the type. */
14814
14815 static void
14816 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14817 struct dwarf2_cu *cu)
14818 {
14819 if (cu->language == language_ada)
14820 error (_("unexpected member functions in Ada type"));
14821
14822 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14823 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14824 TYPE_ALLOC (type,
14825 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14826
14827 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14828 {
14829 struct fnfieldlist &nf = fip->fnfieldlists[i];
14830 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14831
14832 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14833 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14834 fn_flp->fn_fields = (struct fn_field *)
14835 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14836
14837 for (int k = 0; k < nf.fnfields.size (); ++k)
14838 fn_flp->fn_fields[k] = nf.fnfields[k];
14839 }
14840
14841 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14842 }
14843
14844 /* Returns non-zero if NAME is the name of a vtable member in CU's
14845 language, zero otherwise. */
14846 static int
14847 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14848 {
14849 static const char vptr[] = "_vptr";
14850
14851 /* Look for the C++ form of the vtable. */
14852 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14853 return 1;
14854
14855 return 0;
14856 }
14857
14858 /* GCC outputs unnamed structures that are really pointers to member
14859 functions, with the ABI-specified layout. If TYPE describes
14860 such a structure, smash it into a member function type.
14861
14862 GCC shouldn't do this; it should just output pointer to member DIEs.
14863 This is GCC PR debug/28767. */
14864
14865 static void
14866 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14867 {
14868 struct type *pfn_type, *self_type, *new_type;
14869
14870 /* Check for a structure with no name and two children. */
14871 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14872 return;
14873
14874 /* Check for __pfn and __delta members. */
14875 if (TYPE_FIELD_NAME (type, 0) == NULL
14876 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14877 || TYPE_FIELD_NAME (type, 1) == NULL
14878 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14879 return;
14880
14881 /* Find the type of the method. */
14882 pfn_type = TYPE_FIELD_TYPE (type, 0);
14883 if (pfn_type == NULL
14884 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14885 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14886 return;
14887
14888 /* Look for the "this" argument. */
14889 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14890 if (TYPE_NFIELDS (pfn_type) == 0
14891 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14892 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14893 return;
14894
14895 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14896 new_type = alloc_type (objfile);
14897 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14898 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14899 TYPE_VARARGS (pfn_type));
14900 smash_to_methodptr_type (type, new_type);
14901 }
14902
14903 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14904 appropriate error checking and issuing complaints if there is a
14905 problem. */
14906
14907 static ULONGEST
14908 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14909 {
14910 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14911
14912 if (attr == nullptr)
14913 return 0;
14914
14915 if (!attr->form_is_constant ())
14916 {
14917 complaint (_("DW_AT_alignment must have constant form"
14918 " - DIE at %s [in module %s]"),
14919 sect_offset_str (die->sect_off),
14920 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14921 return 0;
14922 }
14923
14924 ULONGEST align;
14925 if (attr->form == DW_FORM_sdata)
14926 {
14927 LONGEST val = DW_SND (attr);
14928 if (val < 0)
14929 {
14930 complaint (_("DW_AT_alignment value must not be negative"
14931 " - DIE at %s [in module %s]"),
14932 sect_offset_str (die->sect_off),
14933 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14934 return 0;
14935 }
14936 align = val;
14937 }
14938 else
14939 align = DW_UNSND (attr);
14940
14941 if (align == 0)
14942 {
14943 complaint (_("DW_AT_alignment value must not be zero"
14944 " - DIE at %s [in module %s]"),
14945 sect_offset_str (die->sect_off),
14946 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14947 return 0;
14948 }
14949 if ((align & (align - 1)) != 0)
14950 {
14951 complaint (_("DW_AT_alignment value must be a power of 2"
14952 " - DIE at %s [in module %s]"),
14953 sect_offset_str (die->sect_off),
14954 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14955 return 0;
14956 }
14957
14958 return align;
14959 }
14960
14961 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14962 the alignment for TYPE. */
14963
14964 static void
14965 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14966 struct type *type)
14967 {
14968 if (!set_type_align (type, get_alignment (cu, die)))
14969 complaint (_("DW_AT_alignment value too large"
14970 " - DIE at %s [in module %s]"),
14971 sect_offset_str (die->sect_off),
14972 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14973 }
14974
14975 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14976 constant for a type, according to DWARF5 spec, Table 5.5. */
14977
14978 static bool
14979 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14980 {
14981 switch (value)
14982 {
14983 case DW_CC_normal:
14984 case DW_CC_pass_by_reference:
14985 case DW_CC_pass_by_value:
14986 return true;
14987
14988 default:
14989 complaint (_("unrecognized DW_AT_calling_convention value "
14990 "(%s) for a type"), pulongest (value));
14991 return false;
14992 }
14993 }
14994
14995 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14996 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14997 also according to GNU-specific values (see include/dwarf2.h). */
14998
14999 static bool
15000 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15001 {
15002 switch (value)
15003 {
15004 case DW_CC_normal:
15005 case DW_CC_program:
15006 case DW_CC_nocall:
15007 return true;
15008
15009 case DW_CC_GNU_renesas_sh:
15010 case DW_CC_GNU_borland_fastcall_i386:
15011 case DW_CC_GDB_IBM_OpenCL:
15012 return true;
15013
15014 default:
15015 complaint (_("unrecognized DW_AT_calling_convention value "
15016 "(%s) for a subroutine"), pulongest (value));
15017 return false;
15018 }
15019 }
15020
15021 /* Called when we find the DIE that starts a structure or union scope
15022 (definition) to create a type for the structure or union. Fill in
15023 the type's name and general properties; the members will not be
15024 processed until process_structure_scope. A symbol table entry for
15025 the type will also not be done until process_structure_scope (assuming
15026 the type has a name).
15027
15028 NOTE: we need to call these functions regardless of whether or not the
15029 DIE has a DW_AT_name attribute, since it might be an anonymous
15030 structure or union. This gets the type entered into our set of
15031 user defined types. */
15032
15033 static struct type *
15034 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15035 {
15036 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15037 struct type *type;
15038 struct attribute *attr;
15039 const char *name;
15040
15041 /* If the definition of this type lives in .debug_types, read that type.
15042 Don't follow DW_AT_specification though, that will take us back up
15043 the chain and we want to go down. */
15044 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15045 if (attr != nullptr)
15046 {
15047 type = get_DW_AT_signature_type (die, attr, cu);
15048
15049 /* The type's CU may not be the same as CU.
15050 Ensure TYPE is recorded with CU in die_type_hash. */
15051 return set_die_type (die, type, cu);
15052 }
15053
15054 type = alloc_type (objfile);
15055 INIT_CPLUS_SPECIFIC (type);
15056
15057 name = dwarf2_name (die, cu);
15058 if (name != NULL)
15059 {
15060 if (cu->language == language_cplus
15061 || cu->language == language_d
15062 || cu->language == language_rust)
15063 {
15064 const char *full_name = dwarf2_full_name (name, die, cu);
15065
15066 /* dwarf2_full_name might have already finished building the DIE's
15067 type. If so, there is no need to continue. */
15068 if (get_die_type (die, cu) != NULL)
15069 return get_die_type (die, cu);
15070
15071 TYPE_NAME (type) = full_name;
15072 }
15073 else
15074 {
15075 /* The name is already allocated along with this objfile, so
15076 we don't need to duplicate it for the type. */
15077 TYPE_NAME (type) = name;
15078 }
15079 }
15080
15081 if (die->tag == DW_TAG_structure_type)
15082 {
15083 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15084 }
15085 else if (die->tag == DW_TAG_union_type)
15086 {
15087 TYPE_CODE (type) = TYPE_CODE_UNION;
15088 }
15089 else if (die->tag == DW_TAG_variant_part)
15090 {
15091 TYPE_CODE (type) = TYPE_CODE_UNION;
15092 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15093 }
15094 else
15095 {
15096 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15097 }
15098
15099 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15100 TYPE_DECLARED_CLASS (type) = 1;
15101
15102 /* Store the calling convention in the type if it's available in
15103 the die. Otherwise the calling convention remains set to
15104 the default value DW_CC_normal. */
15105 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15106 if (attr != nullptr
15107 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15108 {
15109 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15110 TYPE_CPLUS_CALLING_CONVENTION (type)
15111 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15112 }
15113
15114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15115 if (attr != nullptr)
15116 {
15117 if (attr->form_is_constant ())
15118 TYPE_LENGTH (type) = DW_UNSND (attr);
15119 else
15120 {
15121 /* For the moment, dynamic type sizes are not supported
15122 by GDB's struct type. The actual size is determined
15123 on-demand when resolving the type of a given object,
15124 so set the type's length to zero for now. Otherwise,
15125 we record an expression as the length, and that expression
15126 could lead to a very large value, which could eventually
15127 lead to us trying to allocate that much memory when creating
15128 a value of that type. */
15129 TYPE_LENGTH (type) = 0;
15130 }
15131 }
15132 else
15133 {
15134 TYPE_LENGTH (type) = 0;
15135 }
15136
15137 maybe_set_alignment (cu, die, type);
15138
15139 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15140 {
15141 /* ICC<14 does not output the required DW_AT_declaration on
15142 incomplete types, but gives them a size of zero. */
15143 TYPE_STUB (type) = 1;
15144 }
15145 else
15146 TYPE_STUB_SUPPORTED (type) = 1;
15147
15148 if (die_is_declaration (die, cu))
15149 TYPE_STUB (type) = 1;
15150 else if (attr == NULL && die->child == NULL
15151 && producer_is_realview (cu->producer))
15152 /* RealView does not output the required DW_AT_declaration
15153 on incomplete types. */
15154 TYPE_STUB (type) = 1;
15155
15156 /* We need to add the type field to the die immediately so we don't
15157 infinitely recurse when dealing with pointers to the structure
15158 type within the structure itself. */
15159 set_die_type (die, type, cu);
15160
15161 /* set_die_type should be already done. */
15162 set_descriptive_type (type, die, cu);
15163
15164 return type;
15165 }
15166
15167 /* A helper for process_structure_scope that handles a single member
15168 DIE. */
15169
15170 static void
15171 handle_struct_member_die (struct die_info *child_die, struct type *type,
15172 struct field_info *fi,
15173 std::vector<struct symbol *> *template_args,
15174 struct dwarf2_cu *cu)
15175 {
15176 if (child_die->tag == DW_TAG_member
15177 || child_die->tag == DW_TAG_variable
15178 || child_die->tag == DW_TAG_variant_part)
15179 {
15180 /* NOTE: carlton/2002-11-05: A C++ static data member
15181 should be a DW_TAG_member that is a declaration, but
15182 all versions of G++ as of this writing (so through at
15183 least 3.2.1) incorrectly generate DW_TAG_variable
15184 tags for them instead. */
15185 dwarf2_add_field (fi, child_die, cu);
15186 }
15187 else if (child_die->tag == DW_TAG_subprogram)
15188 {
15189 /* Rust doesn't have member functions in the C++ sense.
15190 However, it does emit ordinary functions as children
15191 of a struct DIE. */
15192 if (cu->language == language_rust)
15193 read_func_scope (child_die, cu);
15194 else
15195 {
15196 /* C++ member function. */
15197 dwarf2_add_member_fn (fi, child_die, type, cu);
15198 }
15199 }
15200 else if (child_die->tag == DW_TAG_inheritance)
15201 {
15202 /* C++ base class field. */
15203 dwarf2_add_field (fi, child_die, cu);
15204 }
15205 else if (type_can_define_types (child_die))
15206 dwarf2_add_type_defn (fi, child_die, cu);
15207 else if (child_die->tag == DW_TAG_template_type_param
15208 || child_die->tag == DW_TAG_template_value_param)
15209 {
15210 struct symbol *arg = new_symbol (child_die, NULL, cu);
15211
15212 if (arg != NULL)
15213 template_args->push_back (arg);
15214 }
15215 else if (child_die->tag == DW_TAG_variant)
15216 {
15217 /* In a variant we want to get the discriminant and also add a
15218 field for our sole member child. */
15219 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15220
15221 for (die_info *variant_child = child_die->child;
15222 variant_child != NULL;
15223 variant_child = sibling_die (variant_child))
15224 {
15225 if (variant_child->tag == DW_TAG_member)
15226 {
15227 handle_struct_member_die (variant_child, type, fi,
15228 template_args, cu);
15229 /* Only handle the one. */
15230 break;
15231 }
15232 }
15233
15234 /* We don't handle this but we might as well report it if we see
15235 it. */
15236 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15237 complaint (_("DW_AT_discr_list is not supported yet"
15238 " - DIE at %s [in module %s]"),
15239 sect_offset_str (child_die->sect_off),
15240 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15241
15242 /* The first field was just added, so we can stash the
15243 discriminant there. */
15244 gdb_assert (!fi->fields.empty ());
15245 if (discr == NULL)
15246 fi->fields.back ().variant.default_branch = true;
15247 else
15248 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15249 }
15250 }
15251
15252 /* Finish creating a structure or union type, including filling in
15253 its members and creating a symbol for it. */
15254
15255 static void
15256 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15257 {
15258 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15259 struct die_info *child_die;
15260 struct type *type;
15261
15262 type = get_die_type (die, cu);
15263 if (type == NULL)
15264 type = read_structure_type (die, cu);
15265
15266 /* When reading a DW_TAG_variant_part, we need to notice when we
15267 read the discriminant member, so we can record it later in the
15268 discriminant_info. */
15269 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15270 sect_offset discr_offset {};
15271 bool has_template_parameters = false;
15272
15273 if (is_variant_part)
15274 {
15275 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15276 if (discr == NULL)
15277 {
15278 /* Maybe it's a univariant form, an extension we support.
15279 In this case arrange not to check the offset. */
15280 is_variant_part = false;
15281 }
15282 else if (discr->form_is_ref ())
15283 {
15284 struct dwarf2_cu *target_cu = cu;
15285 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15286
15287 discr_offset = target_die->sect_off;
15288 }
15289 else
15290 {
15291 complaint (_("DW_AT_discr does not have DIE reference form"
15292 " - DIE at %s [in module %s]"),
15293 sect_offset_str (die->sect_off),
15294 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15295 is_variant_part = false;
15296 }
15297 }
15298
15299 if (die->child != NULL && ! die_is_declaration (die, cu))
15300 {
15301 struct field_info fi;
15302 std::vector<struct symbol *> template_args;
15303
15304 child_die = die->child;
15305
15306 while (child_die && child_die->tag)
15307 {
15308 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15309
15310 if (is_variant_part && discr_offset == child_die->sect_off)
15311 fi.fields.back ().variant.is_discriminant = true;
15312
15313 child_die = sibling_die (child_die);
15314 }
15315
15316 /* Attach template arguments to type. */
15317 if (!template_args.empty ())
15318 {
15319 has_template_parameters = true;
15320 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15321 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15322 TYPE_TEMPLATE_ARGUMENTS (type)
15323 = XOBNEWVEC (&objfile->objfile_obstack,
15324 struct symbol *,
15325 TYPE_N_TEMPLATE_ARGUMENTS (type));
15326 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15327 template_args.data (),
15328 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15329 * sizeof (struct symbol *)));
15330 }
15331
15332 /* Attach fields and member functions to the type. */
15333 if (fi.nfields () > 0)
15334 dwarf2_attach_fields_to_type (&fi, type, cu);
15335 if (!fi.fnfieldlists.empty ())
15336 {
15337 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15338
15339 /* Get the type which refers to the base class (possibly this
15340 class itself) which contains the vtable pointer for the current
15341 class from the DW_AT_containing_type attribute. This use of
15342 DW_AT_containing_type is a GNU extension. */
15343
15344 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15345 {
15346 struct type *t = die_containing_type (die, cu);
15347
15348 set_type_vptr_basetype (type, t);
15349 if (type == t)
15350 {
15351 int i;
15352
15353 /* Our own class provides vtbl ptr. */
15354 for (i = TYPE_NFIELDS (t) - 1;
15355 i >= TYPE_N_BASECLASSES (t);
15356 --i)
15357 {
15358 const char *fieldname = TYPE_FIELD_NAME (t, i);
15359
15360 if (is_vtable_name (fieldname, cu))
15361 {
15362 set_type_vptr_fieldno (type, i);
15363 break;
15364 }
15365 }
15366
15367 /* Complain if virtual function table field not found. */
15368 if (i < TYPE_N_BASECLASSES (t))
15369 complaint (_("virtual function table pointer "
15370 "not found when defining class '%s'"),
15371 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15372 }
15373 else
15374 {
15375 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15376 }
15377 }
15378 else if (cu->producer
15379 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15380 {
15381 /* The IBM XLC compiler does not provide direct indication
15382 of the containing type, but the vtable pointer is
15383 always named __vfp. */
15384
15385 int i;
15386
15387 for (i = TYPE_NFIELDS (type) - 1;
15388 i >= TYPE_N_BASECLASSES (type);
15389 --i)
15390 {
15391 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15392 {
15393 set_type_vptr_fieldno (type, i);
15394 set_type_vptr_basetype (type, type);
15395 break;
15396 }
15397 }
15398 }
15399 }
15400
15401 /* Copy fi.typedef_field_list linked list elements content into the
15402 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15403 if (!fi.typedef_field_list.empty ())
15404 {
15405 int count = fi.typedef_field_list.size ();
15406
15407 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15408 TYPE_TYPEDEF_FIELD_ARRAY (type)
15409 = ((struct decl_field *)
15410 TYPE_ALLOC (type,
15411 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15412 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15413
15414 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15415 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15416 }
15417
15418 /* Copy fi.nested_types_list linked list elements content into the
15419 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15420 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15421 {
15422 int count = fi.nested_types_list.size ();
15423
15424 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15425 TYPE_NESTED_TYPES_ARRAY (type)
15426 = ((struct decl_field *)
15427 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15428 TYPE_NESTED_TYPES_COUNT (type) = count;
15429
15430 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15431 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15432 }
15433 }
15434
15435 quirk_gcc_member_function_pointer (type, objfile);
15436 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15437 cu->rust_unions.push_back (type);
15438
15439 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15440 snapshots) has been known to create a die giving a declaration
15441 for a class that has, as a child, a die giving a definition for a
15442 nested class. So we have to process our children even if the
15443 current die is a declaration. Normally, of course, a declaration
15444 won't have any children at all. */
15445
15446 child_die = die->child;
15447
15448 while (child_die != NULL && child_die->tag)
15449 {
15450 if (child_die->tag == DW_TAG_member
15451 || child_die->tag == DW_TAG_variable
15452 || child_die->tag == DW_TAG_inheritance
15453 || child_die->tag == DW_TAG_template_value_param
15454 || child_die->tag == DW_TAG_template_type_param)
15455 {
15456 /* Do nothing. */
15457 }
15458 else
15459 process_die (child_die, cu);
15460
15461 child_die = sibling_die (child_die);
15462 }
15463
15464 /* Do not consider external references. According to the DWARF standard,
15465 these DIEs are identified by the fact that they have no byte_size
15466 attribute, and a declaration attribute. */
15467 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15468 || !die_is_declaration (die, cu))
15469 {
15470 struct symbol *sym = new_symbol (die, type, cu);
15471
15472 if (has_template_parameters)
15473 {
15474 struct symtab *symtab;
15475 if (sym != nullptr)
15476 symtab = symbol_symtab (sym);
15477 else if (cu->line_header != nullptr)
15478 {
15479 /* Any related symtab will do. */
15480 symtab
15481 = cu->line_header->file_names ()[0].symtab;
15482 }
15483 else
15484 {
15485 symtab = nullptr;
15486 complaint (_("could not find suitable "
15487 "symtab for template parameter"
15488 " - DIE at %s [in module %s]"),
15489 sect_offset_str (die->sect_off),
15490 objfile_name (objfile));
15491 }
15492
15493 if (symtab != nullptr)
15494 {
15495 /* Make sure that the symtab is set on the new symbols.
15496 Even though they don't appear in this symtab directly,
15497 other parts of gdb assume that symbols do, and this is
15498 reasonably true. */
15499 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15500 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15501 }
15502 }
15503 }
15504 }
15505
15506 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15507 update TYPE using some information only available in DIE's children. */
15508
15509 static void
15510 update_enumeration_type_from_children (struct die_info *die,
15511 struct type *type,
15512 struct dwarf2_cu *cu)
15513 {
15514 struct die_info *child_die;
15515 int unsigned_enum = 1;
15516 int flag_enum = 1;
15517
15518 auto_obstack obstack;
15519
15520 for (child_die = die->child;
15521 child_die != NULL && child_die->tag;
15522 child_die = sibling_die (child_die))
15523 {
15524 struct attribute *attr;
15525 LONGEST value;
15526 const gdb_byte *bytes;
15527 struct dwarf2_locexpr_baton *baton;
15528 const char *name;
15529
15530 if (child_die->tag != DW_TAG_enumerator)
15531 continue;
15532
15533 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15534 if (attr == NULL)
15535 continue;
15536
15537 name = dwarf2_name (child_die, cu);
15538 if (name == NULL)
15539 name = "<anonymous enumerator>";
15540
15541 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15542 &value, &bytes, &baton);
15543 if (value < 0)
15544 {
15545 unsigned_enum = 0;
15546 flag_enum = 0;
15547 }
15548 else
15549 {
15550 if (count_one_bits_ll (value) >= 2)
15551 flag_enum = 0;
15552 }
15553
15554 /* If we already know that the enum type is neither unsigned, nor
15555 a flag type, no need to look at the rest of the enumerates. */
15556 if (!unsigned_enum && !flag_enum)
15557 break;
15558 }
15559
15560 if (unsigned_enum)
15561 TYPE_UNSIGNED (type) = 1;
15562 if (flag_enum)
15563 TYPE_FLAG_ENUM (type) = 1;
15564 }
15565
15566 /* Given a DW_AT_enumeration_type die, set its type. We do not
15567 complete the type's fields yet, or create any symbols. */
15568
15569 static struct type *
15570 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15571 {
15572 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15573 struct type *type;
15574 struct attribute *attr;
15575 const char *name;
15576
15577 /* If the definition of this type lives in .debug_types, read that type.
15578 Don't follow DW_AT_specification though, that will take us back up
15579 the chain and we want to go down. */
15580 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15581 if (attr != nullptr)
15582 {
15583 type = get_DW_AT_signature_type (die, attr, cu);
15584
15585 /* The type's CU may not be the same as CU.
15586 Ensure TYPE is recorded with CU in die_type_hash. */
15587 return set_die_type (die, type, cu);
15588 }
15589
15590 type = alloc_type (objfile);
15591
15592 TYPE_CODE (type) = TYPE_CODE_ENUM;
15593 name = dwarf2_full_name (NULL, die, cu);
15594 if (name != NULL)
15595 TYPE_NAME (type) = name;
15596
15597 attr = dwarf2_attr (die, DW_AT_type, cu);
15598 if (attr != NULL)
15599 {
15600 struct type *underlying_type = die_type (die, cu);
15601
15602 TYPE_TARGET_TYPE (type) = underlying_type;
15603 }
15604
15605 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15606 if (attr != nullptr)
15607 {
15608 TYPE_LENGTH (type) = DW_UNSND (attr);
15609 }
15610 else
15611 {
15612 TYPE_LENGTH (type) = 0;
15613 }
15614
15615 maybe_set_alignment (cu, die, type);
15616
15617 /* The enumeration DIE can be incomplete. In Ada, any type can be
15618 declared as private in the package spec, and then defined only
15619 inside the package body. Such types are known as Taft Amendment
15620 Types. When another package uses such a type, an incomplete DIE
15621 may be generated by the compiler. */
15622 if (die_is_declaration (die, cu))
15623 TYPE_STUB (type) = 1;
15624
15625 /* Finish the creation of this type by using the enum's children.
15626 We must call this even when the underlying type has been provided
15627 so that we can determine if we're looking at a "flag" enum. */
15628 update_enumeration_type_from_children (die, type, cu);
15629
15630 /* If this type has an underlying type that is not a stub, then we
15631 may use its attributes. We always use the "unsigned" attribute
15632 in this situation, because ordinarily we guess whether the type
15633 is unsigned -- but the guess can be wrong and the underlying type
15634 can tell us the reality. However, we defer to a local size
15635 attribute if one exists, because this lets the compiler override
15636 the underlying type if needed. */
15637 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15638 {
15639 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15640 if (TYPE_LENGTH (type) == 0)
15641 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15642 if (TYPE_RAW_ALIGN (type) == 0
15643 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15644 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15645 }
15646
15647 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15648
15649 return set_die_type (die, type, cu);
15650 }
15651
15652 /* Given a pointer to a die which begins an enumeration, process all
15653 the dies that define the members of the enumeration, and create the
15654 symbol for the enumeration type.
15655
15656 NOTE: We reverse the order of the element list. */
15657
15658 static void
15659 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15660 {
15661 struct type *this_type;
15662
15663 this_type = get_die_type (die, cu);
15664 if (this_type == NULL)
15665 this_type = read_enumeration_type (die, cu);
15666
15667 if (die->child != NULL)
15668 {
15669 struct die_info *child_die;
15670 struct symbol *sym;
15671 std::vector<struct field> fields;
15672 const char *name;
15673
15674 child_die = die->child;
15675 while (child_die && child_die->tag)
15676 {
15677 if (child_die->tag != DW_TAG_enumerator)
15678 {
15679 process_die (child_die, cu);
15680 }
15681 else
15682 {
15683 name = dwarf2_name (child_die, cu);
15684 if (name)
15685 {
15686 sym = new_symbol (child_die, this_type, cu);
15687
15688 fields.emplace_back ();
15689 struct field &field = fields.back ();
15690
15691 FIELD_NAME (field) = sym->linkage_name ();
15692 FIELD_TYPE (field) = NULL;
15693 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15694 FIELD_BITSIZE (field) = 0;
15695 }
15696 }
15697
15698 child_die = sibling_die (child_die);
15699 }
15700
15701 if (!fields.empty ())
15702 {
15703 TYPE_NFIELDS (this_type) = fields.size ();
15704 TYPE_FIELDS (this_type) = (struct field *)
15705 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15706 memcpy (TYPE_FIELDS (this_type), fields.data (),
15707 sizeof (struct field) * fields.size ());
15708 }
15709 }
15710
15711 /* If we are reading an enum from a .debug_types unit, and the enum
15712 is a declaration, and the enum is not the signatured type in the
15713 unit, then we do not want to add a symbol for it. Adding a
15714 symbol would in some cases obscure the true definition of the
15715 enum, giving users an incomplete type when the definition is
15716 actually available. Note that we do not want to do this for all
15717 enums which are just declarations, because C++0x allows forward
15718 enum declarations. */
15719 if (cu->per_cu->is_debug_types
15720 && die_is_declaration (die, cu))
15721 {
15722 struct signatured_type *sig_type;
15723
15724 sig_type = (struct signatured_type *) cu->per_cu;
15725 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15726 if (sig_type->type_offset_in_section != die->sect_off)
15727 return;
15728 }
15729
15730 new_symbol (die, this_type, cu);
15731 }
15732
15733 /* Extract all information from a DW_TAG_array_type DIE and put it in
15734 the DIE's type field. For now, this only handles one dimensional
15735 arrays. */
15736
15737 static struct type *
15738 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15739 {
15740 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15741 struct die_info *child_die;
15742 struct type *type;
15743 struct type *element_type, *range_type, *index_type;
15744 struct attribute *attr;
15745 const char *name;
15746 struct dynamic_prop *byte_stride_prop = NULL;
15747 unsigned int bit_stride = 0;
15748
15749 element_type = die_type (die, cu);
15750
15751 /* The die_type call above may have already set the type for this DIE. */
15752 type = get_die_type (die, cu);
15753 if (type)
15754 return type;
15755
15756 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15757 if (attr != NULL)
15758 {
15759 int stride_ok;
15760 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15761
15762 byte_stride_prop
15763 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15764 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15765 prop_type);
15766 if (!stride_ok)
15767 {
15768 complaint (_("unable to read array DW_AT_byte_stride "
15769 " - DIE at %s [in module %s]"),
15770 sect_offset_str (die->sect_off),
15771 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15772 /* Ignore this attribute. We will likely not be able to print
15773 arrays of this type correctly, but there is little we can do
15774 to help if we cannot read the attribute's value. */
15775 byte_stride_prop = NULL;
15776 }
15777 }
15778
15779 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15780 if (attr != NULL)
15781 bit_stride = DW_UNSND (attr);
15782
15783 /* Irix 6.2 native cc creates array types without children for
15784 arrays with unspecified length. */
15785 if (die->child == NULL)
15786 {
15787 index_type = objfile_type (objfile)->builtin_int;
15788 range_type = create_static_range_type (NULL, index_type, 0, -1);
15789 type = create_array_type_with_stride (NULL, element_type, range_type,
15790 byte_stride_prop, bit_stride);
15791 return set_die_type (die, type, cu);
15792 }
15793
15794 std::vector<struct type *> range_types;
15795 child_die = die->child;
15796 while (child_die && child_die->tag)
15797 {
15798 if (child_die->tag == DW_TAG_subrange_type)
15799 {
15800 struct type *child_type = read_type_die (child_die, cu);
15801
15802 if (child_type != NULL)
15803 {
15804 /* The range type was succesfully read. Save it for the
15805 array type creation. */
15806 range_types.push_back (child_type);
15807 }
15808 }
15809 child_die = sibling_die (child_die);
15810 }
15811
15812 /* Dwarf2 dimensions are output from left to right, create the
15813 necessary array types in backwards order. */
15814
15815 type = element_type;
15816
15817 if (read_array_order (die, cu) == DW_ORD_col_major)
15818 {
15819 int i = 0;
15820
15821 while (i < range_types.size ())
15822 type = create_array_type_with_stride (NULL, type, range_types[i++],
15823 byte_stride_prop, bit_stride);
15824 }
15825 else
15826 {
15827 size_t ndim = range_types.size ();
15828 while (ndim-- > 0)
15829 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15830 byte_stride_prop, bit_stride);
15831 }
15832
15833 /* Understand Dwarf2 support for vector types (like they occur on
15834 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15835 array type. This is not part of the Dwarf2/3 standard yet, but a
15836 custom vendor extension. The main difference between a regular
15837 array and the vector variant is that vectors are passed by value
15838 to functions. */
15839 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15840 if (attr != nullptr)
15841 make_vector_type (type);
15842
15843 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15844 implementation may choose to implement triple vectors using this
15845 attribute. */
15846 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15847 if (attr != nullptr)
15848 {
15849 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15850 TYPE_LENGTH (type) = DW_UNSND (attr);
15851 else
15852 complaint (_("DW_AT_byte_size for array type smaller "
15853 "than the total size of elements"));
15854 }
15855
15856 name = dwarf2_name (die, cu);
15857 if (name)
15858 TYPE_NAME (type) = name;
15859
15860 maybe_set_alignment (cu, die, type);
15861
15862 /* Install the type in the die. */
15863 set_die_type (die, type, cu);
15864
15865 /* set_die_type should be already done. */
15866 set_descriptive_type (type, die, cu);
15867
15868 return type;
15869 }
15870
15871 static enum dwarf_array_dim_ordering
15872 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15873 {
15874 struct attribute *attr;
15875
15876 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15877
15878 if (attr != nullptr)
15879 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15880
15881 /* GNU F77 is a special case, as at 08/2004 array type info is the
15882 opposite order to the dwarf2 specification, but data is still
15883 laid out as per normal fortran.
15884
15885 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15886 version checking. */
15887
15888 if (cu->language == language_fortran
15889 && cu->producer && strstr (cu->producer, "GNU F77"))
15890 {
15891 return DW_ORD_row_major;
15892 }
15893
15894 switch (cu->language_defn->la_array_ordering)
15895 {
15896 case array_column_major:
15897 return DW_ORD_col_major;
15898 case array_row_major:
15899 default:
15900 return DW_ORD_row_major;
15901 };
15902 }
15903
15904 /* Extract all information from a DW_TAG_set_type DIE and put it in
15905 the DIE's type field. */
15906
15907 static struct type *
15908 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15909 {
15910 struct type *domain_type, *set_type;
15911 struct attribute *attr;
15912
15913 domain_type = die_type (die, cu);
15914
15915 /* The die_type call above may have already set the type for this DIE. */
15916 set_type = get_die_type (die, cu);
15917 if (set_type)
15918 return set_type;
15919
15920 set_type = create_set_type (NULL, domain_type);
15921
15922 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15923 if (attr != nullptr)
15924 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15925
15926 maybe_set_alignment (cu, die, set_type);
15927
15928 return set_die_type (die, set_type, cu);
15929 }
15930
15931 /* A helper for read_common_block that creates a locexpr baton.
15932 SYM is the symbol which we are marking as computed.
15933 COMMON_DIE is the DIE for the common block.
15934 COMMON_LOC is the location expression attribute for the common
15935 block itself.
15936 MEMBER_LOC is the location expression attribute for the particular
15937 member of the common block that we are processing.
15938 CU is the CU from which the above come. */
15939
15940 static void
15941 mark_common_block_symbol_computed (struct symbol *sym,
15942 struct die_info *common_die,
15943 struct attribute *common_loc,
15944 struct attribute *member_loc,
15945 struct dwarf2_cu *cu)
15946 {
15947 struct dwarf2_per_objfile *dwarf2_per_objfile
15948 = cu->per_cu->dwarf2_per_objfile;
15949 struct objfile *objfile = dwarf2_per_objfile->objfile;
15950 struct dwarf2_locexpr_baton *baton;
15951 gdb_byte *ptr;
15952 unsigned int cu_off;
15953 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15954 LONGEST offset = 0;
15955
15956 gdb_assert (common_loc && member_loc);
15957 gdb_assert (common_loc->form_is_block ());
15958 gdb_assert (member_loc->form_is_block ()
15959 || member_loc->form_is_constant ());
15960
15961 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15962 baton->per_cu = cu->per_cu;
15963 gdb_assert (baton->per_cu);
15964
15965 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15966
15967 if (member_loc->form_is_constant ())
15968 {
15969 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15970 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15971 }
15972 else
15973 baton->size += DW_BLOCK (member_loc)->size;
15974
15975 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15976 baton->data = ptr;
15977
15978 *ptr++ = DW_OP_call4;
15979 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15980 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15981 ptr += 4;
15982
15983 if (member_loc->form_is_constant ())
15984 {
15985 *ptr++ = DW_OP_addr;
15986 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15987 ptr += cu->header.addr_size;
15988 }
15989 else
15990 {
15991 /* We have to copy the data here, because DW_OP_call4 will only
15992 use a DW_AT_location attribute. */
15993 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15994 ptr += DW_BLOCK (member_loc)->size;
15995 }
15996
15997 *ptr++ = DW_OP_plus;
15998 gdb_assert (ptr - baton->data == baton->size);
15999
16000 SYMBOL_LOCATION_BATON (sym) = baton;
16001 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16002 }
16003
16004 /* Create appropriate locally-scoped variables for all the
16005 DW_TAG_common_block entries. Also create a struct common_block
16006 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16007 is used to separate the common blocks name namespace from regular
16008 variable names. */
16009
16010 static void
16011 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16012 {
16013 struct attribute *attr;
16014
16015 attr = dwarf2_attr (die, DW_AT_location, cu);
16016 if (attr != nullptr)
16017 {
16018 /* Support the .debug_loc offsets. */
16019 if (attr->form_is_block ())
16020 {
16021 /* Ok. */
16022 }
16023 else if (attr->form_is_section_offset ())
16024 {
16025 dwarf2_complex_location_expr_complaint ();
16026 attr = NULL;
16027 }
16028 else
16029 {
16030 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16031 "common block member");
16032 attr = NULL;
16033 }
16034 }
16035
16036 if (die->child != NULL)
16037 {
16038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16039 struct die_info *child_die;
16040 size_t n_entries = 0, size;
16041 struct common_block *common_block;
16042 struct symbol *sym;
16043
16044 for (child_die = die->child;
16045 child_die && child_die->tag;
16046 child_die = sibling_die (child_die))
16047 ++n_entries;
16048
16049 size = (sizeof (struct common_block)
16050 + (n_entries - 1) * sizeof (struct symbol *));
16051 common_block
16052 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16053 size);
16054 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16055 common_block->n_entries = 0;
16056
16057 for (child_die = die->child;
16058 child_die && child_die->tag;
16059 child_die = sibling_die (child_die))
16060 {
16061 /* Create the symbol in the DW_TAG_common_block block in the current
16062 symbol scope. */
16063 sym = new_symbol (child_die, NULL, cu);
16064 if (sym != NULL)
16065 {
16066 struct attribute *member_loc;
16067
16068 common_block->contents[common_block->n_entries++] = sym;
16069
16070 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16071 cu);
16072 if (member_loc)
16073 {
16074 /* GDB has handled this for a long time, but it is
16075 not specified by DWARF. It seems to have been
16076 emitted by gfortran at least as recently as:
16077 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16078 complaint (_("Variable in common block has "
16079 "DW_AT_data_member_location "
16080 "- DIE at %s [in module %s]"),
16081 sect_offset_str (child_die->sect_off),
16082 objfile_name (objfile));
16083
16084 if (member_loc->form_is_section_offset ())
16085 dwarf2_complex_location_expr_complaint ();
16086 else if (member_loc->form_is_constant ()
16087 || member_loc->form_is_block ())
16088 {
16089 if (attr != nullptr)
16090 mark_common_block_symbol_computed (sym, die, attr,
16091 member_loc, cu);
16092 }
16093 else
16094 dwarf2_complex_location_expr_complaint ();
16095 }
16096 }
16097 }
16098
16099 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16100 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16101 }
16102 }
16103
16104 /* Create a type for a C++ namespace. */
16105
16106 static struct type *
16107 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16108 {
16109 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16110 const char *previous_prefix, *name;
16111 int is_anonymous;
16112 struct type *type;
16113
16114 /* For extensions, reuse the type of the original namespace. */
16115 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16116 {
16117 struct die_info *ext_die;
16118 struct dwarf2_cu *ext_cu = cu;
16119
16120 ext_die = dwarf2_extension (die, &ext_cu);
16121 type = read_type_die (ext_die, ext_cu);
16122
16123 /* EXT_CU may not be the same as CU.
16124 Ensure TYPE is recorded with CU in die_type_hash. */
16125 return set_die_type (die, type, cu);
16126 }
16127
16128 name = namespace_name (die, &is_anonymous, cu);
16129
16130 /* Now build the name of the current namespace. */
16131
16132 previous_prefix = determine_prefix (die, cu);
16133 if (previous_prefix[0] != '\0')
16134 name = typename_concat (&objfile->objfile_obstack,
16135 previous_prefix, name, 0, cu);
16136
16137 /* Create the type. */
16138 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16139
16140 return set_die_type (die, type, cu);
16141 }
16142
16143 /* Read a namespace scope. */
16144
16145 static void
16146 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16147 {
16148 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16149 int is_anonymous;
16150
16151 /* Add a symbol associated to this if we haven't seen the namespace
16152 before. Also, add a using directive if it's an anonymous
16153 namespace. */
16154
16155 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16156 {
16157 struct type *type;
16158
16159 type = read_type_die (die, cu);
16160 new_symbol (die, type, cu);
16161
16162 namespace_name (die, &is_anonymous, cu);
16163 if (is_anonymous)
16164 {
16165 const char *previous_prefix = determine_prefix (die, cu);
16166
16167 std::vector<const char *> excludes;
16168 add_using_directive (using_directives (cu),
16169 previous_prefix, TYPE_NAME (type), NULL,
16170 NULL, excludes, 0, &objfile->objfile_obstack);
16171 }
16172 }
16173
16174 if (die->child != NULL)
16175 {
16176 struct die_info *child_die = die->child;
16177
16178 while (child_die && child_die->tag)
16179 {
16180 process_die (child_die, cu);
16181 child_die = sibling_die (child_die);
16182 }
16183 }
16184 }
16185
16186 /* Read a Fortran module as type. This DIE can be only a declaration used for
16187 imported module. Still we need that type as local Fortran "use ... only"
16188 declaration imports depend on the created type in determine_prefix. */
16189
16190 static struct type *
16191 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16192 {
16193 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16194 const char *module_name;
16195 struct type *type;
16196
16197 module_name = dwarf2_name (die, cu);
16198 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16199
16200 return set_die_type (die, type, cu);
16201 }
16202
16203 /* Read a Fortran module. */
16204
16205 static void
16206 read_module (struct die_info *die, struct dwarf2_cu *cu)
16207 {
16208 struct die_info *child_die = die->child;
16209 struct type *type;
16210
16211 type = read_type_die (die, cu);
16212 new_symbol (die, type, cu);
16213
16214 while (child_die && child_die->tag)
16215 {
16216 process_die (child_die, cu);
16217 child_die = sibling_die (child_die);
16218 }
16219 }
16220
16221 /* Return the name of the namespace represented by DIE. Set
16222 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16223 namespace. */
16224
16225 static const char *
16226 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16227 {
16228 struct die_info *current_die;
16229 const char *name = NULL;
16230
16231 /* Loop through the extensions until we find a name. */
16232
16233 for (current_die = die;
16234 current_die != NULL;
16235 current_die = dwarf2_extension (die, &cu))
16236 {
16237 /* We don't use dwarf2_name here so that we can detect the absence
16238 of a name -> anonymous namespace. */
16239 name = dwarf2_string_attr (die, DW_AT_name, cu);
16240
16241 if (name != NULL)
16242 break;
16243 }
16244
16245 /* Is it an anonymous namespace? */
16246
16247 *is_anonymous = (name == NULL);
16248 if (*is_anonymous)
16249 name = CP_ANONYMOUS_NAMESPACE_STR;
16250
16251 return name;
16252 }
16253
16254 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16255 the user defined type vector. */
16256
16257 static struct type *
16258 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16259 {
16260 struct gdbarch *gdbarch
16261 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16262 struct comp_unit_head *cu_header = &cu->header;
16263 struct type *type;
16264 struct attribute *attr_byte_size;
16265 struct attribute *attr_address_class;
16266 int byte_size, addr_class;
16267 struct type *target_type;
16268
16269 target_type = die_type (die, cu);
16270
16271 /* The die_type call above may have already set the type for this DIE. */
16272 type = get_die_type (die, cu);
16273 if (type)
16274 return type;
16275
16276 type = lookup_pointer_type (target_type);
16277
16278 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16279 if (attr_byte_size)
16280 byte_size = DW_UNSND (attr_byte_size);
16281 else
16282 byte_size = cu_header->addr_size;
16283
16284 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16285 if (attr_address_class)
16286 addr_class = DW_UNSND (attr_address_class);
16287 else
16288 addr_class = DW_ADDR_none;
16289
16290 ULONGEST alignment = get_alignment (cu, die);
16291
16292 /* If the pointer size, alignment, or address class is different
16293 than the default, create a type variant marked as such and set
16294 the length accordingly. */
16295 if (TYPE_LENGTH (type) != byte_size
16296 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16297 && alignment != TYPE_RAW_ALIGN (type))
16298 || addr_class != DW_ADDR_none)
16299 {
16300 if (gdbarch_address_class_type_flags_p (gdbarch))
16301 {
16302 int type_flags;
16303
16304 type_flags = gdbarch_address_class_type_flags
16305 (gdbarch, byte_size, addr_class);
16306 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16307 == 0);
16308 type = make_type_with_address_space (type, type_flags);
16309 }
16310 else if (TYPE_LENGTH (type) != byte_size)
16311 {
16312 complaint (_("invalid pointer size %d"), byte_size);
16313 }
16314 else if (TYPE_RAW_ALIGN (type) != alignment)
16315 {
16316 complaint (_("Invalid DW_AT_alignment"
16317 " - DIE at %s [in module %s]"),
16318 sect_offset_str (die->sect_off),
16319 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16320 }
16321 else
16322 {
16323 /* Should we also complain about unhandled address classes? */
16324 }
16325 }
16326
16327 TYPE_LENGTH (type) = byte_size;
16328 set_type_align (type, alignment);
16329 return set_die_type (die, type, cu);
16330 }
16331
16332 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16333 the user defined type vector. */
16334
16335 static struct type *
16336 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16337 {
16338 struct type *type;
16339 struct type *to_type;
16340 struct type *domain;
16341
16342 to_type = die_type (die, cu);
16343 domain = die_containing_type (die, cu);
16344
16345 /* The calls above may have already set the type for this DIE. */
16346 type = get_die_type (die, cu);
16347 if (type)
16348 return type;
16349
16350 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16351 type = lookup_methodptr_type (to_type);
16352 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16353 {
16354 struct type *new_type
16355 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16356
16357 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16358 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16359 TYPE_VARARGS (to_type));
16360 type = lookup_methodptr_type (new_type);
16361 }
16362 else
16363 type = lookup_memberptr_type (to_type, domain);
16364
16365 return set_die_type (die, type, cu);
16366 }
16367
16368 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16369 the user defined type vector. */
16370
16371 static struct type *
16372 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16373 enum type_code refcode)
16374 {
16375 struct comp_unit_head *cu_header = &cu->header;
16376 struct type *type, *target_type;
16377 struct attribute *attr;
16378
16379 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16380
16381 target_type = die_type (die, cu);
16382
16383 /* The die_type call above may have already set the type for this DIE. */
16384 type = get_die_type (die, cu);
16385 if (type)
16386 return type;
16387
16388 type = lookup_reference_type (target_type, refcode);
16389 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16390 if (attr != nullptr)
16391 {
16392 TYPE_LENGTH (type) = DW_UNSND (attr);
16393 }
16394 else
16395 {
16396 TYPE_LENGTH (type) = cu_header->addr_size;
16397 }
16398 maybe_set_alignment (cu, die, type);
16399 return set_die_type (die, type, cu);
16400 }
16401
16402 /* Add the given cv-qualifiers to the element type of the array. GCC
16403 outputs DWARF type qualifiers that apply to an array, not the
16404 element type. But GDB relies on the array element type to carry
16405 the cv-qualifiers. This mimics section 6.7.3 of the C99
16406 specification. */
16407
16408 static struct type *
16409 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16410 struct type *base_type, int cnst, int voltl)
16411 {
16412 struct type *el_type, *inner_array;
16413
16414 base_type = copy_type (base_type);
16415 inner_array = base_type;
16416
16417 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16418 {
16419 TYPE_TARGET_TYPE (inner_array) =
16420 copy_type (TYPE_TARGET_TYPE (inner_array));
16421 inner_array = TYPE_TARGET_TYPE (inner_array);
16422 }
16423
16424 el_type = TYPE_TARGET_TYPE (inner_array);
16425 cnst |= TYPE_CONST (el_type);
16426 voltl |= TYPE_VOLATILE (el_type);
16427 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16428
16429 return set_die_type (die, base_type, cu);
16430 }
16431
16432 static struct type *
16433 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16434 {
16435 struct type *base_type, *cv_type;
16436
16437 base_type = die_type (die, cu);
16438
16439 /* The die_type call above may have already set the type for this DIE. */
16440 cv_type = get_die_type (die, cu);
16441 if (cv_type)
16442 return cv_type;
16443
16444 /* In case the const qualifier is applied to an array type, the element type
16445 is so qualified, not the array type (section 6.7.3 of C99). */
16446 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16447 return add_array_cv_type (die, cu, base_type, 1, 0);
16448
16449 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16450 return set_die_type (die, cv_type, cu);
16451 }
16452
16453 static struct type *
16454 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16455 {
16456 struct type *base_type, *cv_type;
16457
16458 base_type = die_type (die, cu);
16459
16460 /* The die_type call above may have already set the type for this DIE. */
16461 cv_type = get_die_type (die, cu);
16462 if (cv_type)
16463 return cv_type;
16464
16465 /* In case the volatile qualifier is applied to an array type, the
16466 element type is so qualified, not the array type (section 6.7.3
16467 of C99). */
16468 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16469 return add_array_cv_type (die, cu, base_type, 0, 1);
16470
16471 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16472 return set_die_type (die, cv_type, cu);
16473 }
16474
16475 /* Handle DW_TAG_restrict_type. */
16476
16477 static struct type *
16478 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16479 {
16480 struct type *base_type, *cv_type;
16481
16482 base_type = die_type (die, cu);
16483
16484 /* The die_type call above may have already set the type for this DIE. */
16485 cv_type = get_die_type (die, cu);
16486 if (cv_type)
16487 return cv_type;
16488
16489 cv_type = make_restrict_type (base_type);
16490 return set_die_type (die, cv_type, cu);
16491 }
16492
16493 /* Handle DW_TAG_atomic_type. */
16494
16495 static struct type *
16496 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16497 {
16498 struct type *base_type, *cv_type;
16499
16500 base_type = die_type (die, cu);
16501
16502 /* The die_type call above may have already set the type for this DIE. */
16503 cv_type = get_die_type (die, cu);
16504 if (cv_type)
16505 return cv_type;
16506
16507 cv_type = make_atomic_type (base_type);
16508 return set_die_type (die, cv_type, cu);
16509 }
16510
16511 /* Extract all information from a DW_TAG_string_type DIE and add to
16512 the user defined type vector. It isn't really a user defined type,
16513 but it behaves like one, with other DIE's using an AT_user_def_type
16514 attribute to reference it. */
16515
16516 static struct type *
16517 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16518 {
16519 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16521 struct type *type, *range_type, *index_type, *char_type;
16522 struct attribute *attr;
16523 struct dynamic_prop prop;
16524 bool length_is_constant = true;
16525 LONGEST length;
16526
16527 /* There are a couple of places where bit sizes might be made use of
16528 when parsing a DW_TAG_string_type, however, no producer that we know
16529 of make use of these. Handling bit sizes that are a multiple of the
16530 byte size is easy enough, but what about other bit sizes? Lets deal
16531 with that problem when we have to. Warn about these attributes being
16532 unsupported, then parse the type and ignore them like we always
16533 have. */
16534 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16535 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16536 {
16537 static bool warning_printed = false;
16538 if (!warning_printed)
16539 {
16540 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16541 "currently supported on DW_TAG_string_type."));
16542 warning_printed = true;
16543 }
16544 }
16545
16546 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16547 if (attr != nullptr && !attr->form_is_constant ())
16548 {
16549 /* The string length describes the location at which the length of
16550 the string can be found. The size of the length field can be
16551 specified with one of the attributes below. */
16552 struct type *prop_type;
16553 struct attribute *len
16554 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16555 if (len == nullptr)
16556 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16557 if (len != nullptr && len->form_is_constant ())
16558 {
16559 /* Pass 0 as the default as we know this attribute is constant
16560 and the default value will not be returned. */
16561 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16562 prop_type = cu->per_cu->int_type (sz, true);
16563 }
16564 else
16565 {
16566 /* If the size is not specified then we assume it is the size of
16567 an address on this target. */
16568 prop_type = cu->per_cu->addr_sized_int_type (true);
16569 }
16570
16571 /* Convert the attribute into a dynamic property. */
16572 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16573 length = 1;
16574 else
16575 length_is_constant = false;
16576 }
16577 else if (attr != nullptr)
16578 {
16579 /* This DW_AT_string_length just contains the length with no
16580 indirection. There's no need to create a dynamic property in this
16581 case. Pass 0 for the default value as we know it will not be
16582 returned in this case. */
16583 length = dwarf2_get_attr_constant_value (attr, 0);
16584 }
16585 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16586 {
16587 /* We don't currently support non-constant byte sizes for strings. */
16588 length = dwarf2_get_attr_constant_value (attr, 1);
16589 }
16590 else
16591 {
16592 /* Use 1 as a fallback length if we have nothing else. */
16593 length = 1;
16594 }
16595
16596 index_type = objfile_type (objfile)->builtin_int;
16597 if (length_is_constant)
16598 range_type = create_static_range_type (NULL, index_type, 1, length);
16599 else
16600 {
16601 struct dynamic_prop low_bound;
16602
16603 low_bound.kind = PROP_CONST;
16604 low_bound.data.const_val = 1;
16605 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16606 }
16607 char_type = language_string_char_type (cu->language_defn, gdbarch);
16608 type = create_string_type (NULL, char_type, range_type);
16609
16610 return set_die_type (die, type, cu);
16611 }
16612
16613 /* Assuming that DIE corresponds to a function, returns nonzero
16614 if the function is prototyped. */
16615
16616 static int
16617 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16618 {
16619 struct attribute *attr;
16620
16621 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16622 if (attr && (DW_UNSND (attr) != 0))
16623 return 1;
16624
16625 /* The DWARF standard implies that the DW_AT_prototyped attribute
16626 is only meaningful for C, but the concept also extends to other
16627 languages that allow unprototyped functions (Eg: Objective C).
16628 For all other languages, assume that functions are always
16629 prototyped. */
16630 if (cu->language != language_c
16631 && cu->language != language_objc
16632 && cu->language != language_opencl)
16633 return 1;
16634
16635 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16636 prototyped and unprototyped functions; default to prototyped,
16637 since that is more common in modern code (and RealView warns
16638 about unprototyped functions). */
16639 if (producer_is_realview (cu->producer))
16640 return 1;
16641
16642 return 0;
16643 }
16644
16645 /* Handle DIES due to C code like:
16646
16647 struct foo
16648 {
16649 int (*funcp)(int a, long l);
16650 int b;
16651 };
16652
16653 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16654
16655 static struct type *
16656 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16657 {
16658 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16659 struct type *type; /* Type that this function returns. */
16660 struct type *ftype; /* Function that returns above type. */
16661 struct attribute *attr;
16662
16663 type = die_type (die, cu);
16664
16665 /* The die_type call above may have already set the type for this DIE. */
16666 ftype = get_die_type (die, cu);
16667 if (ftype)
16668 return ftype;
16669
16670 ftype = lookup_function_type (type);
16671
16672 if (prototyped_function_p (die, cu))
16673 TYPE_PROTOTYPED (ftype) = 1;
16674
16675 /* Store the calling convention in the type if it's available in
16676 the subroutine die. Otherwise set the calling convention to
16677 the default value DW_CC_normal. */
16678 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16679 if (attr != nullptr
16680 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16681 TYPE_CALLING_CONVENTION (ftype)
16682 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16683 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16684 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16685 else
16686 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16687
16688 /* Record whether the function returns normally to its caller or not
16689 if the DWARF producer set that information. */
16690 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16691 if (attr && (DW_UNSND (attr) != 0))
16692 TYPE_NO_RETURN (ftype) = 1;
16693
16694 /* We need to add the subroutine type to the die immediately so
16695 we don't infinitely recurse when dealing with parameters
16696 declared as the same subroutine type. */
16697 set_die_type (die, ftype, cu);
16698
16699 if (die->child != NULL)
16700 {
16701 struct type *void_type = objfile_type (objfile)->builtin_void;
16702 struct die_info *child_die;
16703 int nparams, iparams;
16704
16705 /* Count the number of parameters.
16706 FIXME: GDB currently ignores vararg functions, but knows about
16707 vararg member functions. */
16708 nparams = 0;
16709 child_die = die->child;
16710 while (child_die && child_die->tag)
16711 {
16712 if (child_die->tag == DW_TAG_formal_parameter)
16713 nparams++;
16714 else if (child_die->tag == DW_TAG_unspecified_parameters)
16715 TYPE_VARARGS (ftype) = 1;
16716 child_die = sibling_die (child_die);
16717 }
16718
16719 /* Allocate storage for parameters and fill them in. */
16720 TYPE_NFIELDS (ftype) = nparams;
16721 TYPE_FIELDS (ftype) = (struct field *)
16722 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16723
16724 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16725 even if we error out during the parameters reading below. */
16726 for (iparams = 0; iparams < nparams; iparams++)
16727 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16728
16729 iparams = 0;
16730 child_die = die->child;
16731 while (child_die && child_die->tag)
16732 {
16733 if (child_die->tag == DW_TAG_formal_parameter)
16734 {
16735 struct type *arg_type;
16736
16737 /* DWARF version 2 has no clean way to discern C++
16738 static and non-static member functions. G++ helps
16739 GDB by marking the first parameter for non-static
16740 member functions (which is the this pointer) as
16741 artificial. We pass this information to
16742 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16743
16744 DWARF version 3 added DW_AT_object_pointer, which GCC
16745 4.5 does not yet generate. */
16746 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16747 if (attr != nullptr)
16748 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16749 else
16750 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16751 arg_type = die_type (child_die, cu);
16752
16753 /* RealView does not mark THIS as const, which the testsuite
16754 expects. GCC marks THIS as const in method definitions,
16755 but not in the class specifications (GCC PR 43053). */
16756 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16757 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16758 {
16759 int is_this = 0;
16760 struct dwarf2_cu *arg_cu = cu;
16761 const char *name = dwarf2_name (child_die, cu);
16762
16763 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16764 if (attr != nullptr)
16765 {
16766 /* If the compiler emits this, use it. */
16767 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16768 is_this = 1;
16769 }
16770 else if (name && strcmp (name, "this") == 0)
16771 /* Function definitions will have the argument names. */
16772 is_this = 1;
16773 else if (name == NULL && iparams == 0)
16774 /* Declarations may not have the names, so like
16775 elsewhere in GDB, assume an artificial first
16776 argument is "this". */
16777 is_this = 1;
16778
16779 if (is_this)
16780 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16781 arg_type, 0);
16782 }
16783
16784 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16785 iparams++;
16786 }
16787 child_die = sibling_die (child_die);
16788 }
16789 }
16790
16791 return ftype;
16792 }
16793
16794 static struct type *
16795 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16796 {
16797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16798 const char *name = NULL;
16799 struct type *this_type, *target_type;
16800
16801 name = dwarf2_full_name (NULL, die, cu);
16802 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16803 TYPE_TARGET_STUB (this_type) = 1;
16804 set_die_type (die, this_type, cu);
16805 target_type = die_type (die, cu);
16806 if (target_type != this_type)
16807 TYPE_TARGET_TYPE (this_type) = target_type;
16808 else
16809 {
16810 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16811 spec and cause infinite loops in GDB. */
16812 complaint (_("Self-referential DW_TAG_typedef "
16813 "- DIE at %s [in module %s]"),
16814 sect_offset_str (die->sect_off), objfile_name (objfile));
16815 TYPE_TARGET_TYPE (this_type) = NULL;
16816 }
16817 if (name == NULL)
16818 {
16819 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16820 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16821 Handle these by just returning the target type, rather than
16822 constructing an anonymous typedef type and trying to handle this
16823 elsewhere. */
16824 set_die_type (die, target_type, cu);
16825 return target_type;
16826 }
16827 return this_type;
16828 }
16829
16830 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16831 (which may be different from NAME) to the architecture back-end to allow
16832 it to guess the correct format if necessary. */
16833
16834 static struct type *
16835 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16836 const char *name_hint, enum bfd_endian byte_order)
16837 {
16838 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16839 const struct floatformat **format;
16840 struct type *type;
16841
16842 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16843 if (format)
16844 type = init_float_type (objfile, bits, name, format, byte_order);
16845 else
16846 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16847
16848 return type;
16849 }
16850
16851 /* Allocate an integer type of size BITS and name NAME. */
16852
16853 static struct type *
16854 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16855 int bits, int unsigned_p, const char *name)
16856 {
16857 struct type *type;
16858
16859 /* Versions of Intel's C Compiler generate an integer type called "void"
16860 instead of using DW_TAG_unspecified_type. This has been seen on
16861 at least versions 14, 17, and 18. */
16862 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16863 && strcmp (name, "void") == 0)
16864 type = objfile_type (objfile)->builtin_void;
16865 else
16866 type = init_integer_type (objfile, bits, unsigned_p, name);
16867
16868 return type;
16869 }
16870
16871 /* Initialise and return a floating point type of size BITS suitable for
16872 use as a component of a complex number. The NAME_HINT is passed through
16873 when initialising the floating point type and is the name of the complex
16874 type.
16875
16876 As DWARF doesn't currently provide an explicit name for the components
16877 of a complex number, but it can be helpful to have these components
16878 named, we try to select a suitable name based on the size of the
16879 component. */
16880 static struct type *
16881 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16882 struct objfile *objfile,
16883 int bits, const char *name_hint,
16884 enum bfd_endian byte_order)
16885 {
16886 gdbarch *gdbarch = get_objfile_arch (objfile);
16887 struct type *tt = nullptr;
16888
16889 /* Try to find a suitable floating point builtin type of size BITS.
16890 We're going to use the name of this type as the name for the complex
16891 target type that we are about to create. */
16892 switch (cu->language)
16893 {
16894 case language_fortran:
16895 switch (bits)
16896 {
16897 case 32:
16898 tt = builtin_f_type (gdbarch)->builtin_real;
16899 break;
16900 case 64:
16901 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16902 break;
16903 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16904 case 128:
16905 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16906 break;
16907 }
16908 break;
16909 default:
16910 switch (bits)
16911 {
16912 case 32:
16913 tt = builtin_type (gdbarch)->builtin_float;
16914 break;
16915 case 64:
16916 tt = builtin_type (gdbarch)->builtin_double;
16917 break;
16918 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16919 case 128:
16920 tt = builtin_type (gdbarch)->builtin_long_double;
16921 break;
16922 }
16923 break;
16924 }
16925
16926 /* If the type we found doesn't match the size we were looking for, then
16927 pretend we didn't find a type at all, the complex target type we
16928 create will then be nameless. */
16929 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16930 tt = nullptr;
16931
16932 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16933 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16934 }
16935
16936 /* Find a representation of a given base type and install
16937 it in the TYPE field of the die. */
16938
16939 static struct type *
16940 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16941 {
16942 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16943 struct type *type;
16944 struct attribute *attr;
16945 int encoding = 0, bits = 0;
16946 const char *name;
16947 gdbarch *arch;
16948
16949 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16950 if (attr != nullptr)
16951 encoding = DW_UNSND (attr);
16952 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16953 if (attr != nullptr)
16954 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16955 name = dwarf2_name (die, cu);
16956 if (!name)
16957 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16958
16959 arch = get_objfile_arch (objfile);
16960 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16961
16962 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16963 if (attr)
16964 {
16965 int endianity = DW_UNSND (attr);
16966
16967 switch (endianity)
16968 {
16969 case DW_END_big:
16970 byte_order = BFD_ENDIAN_BIG;
16971 break;
16972 case DW_END_little:
16973 byte_order = BFD_ENDIAN_LITTLE;
16974 break;
16975 default:
16976 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16977 break;
16978 }
16979 }
16980
16981 switch (encoding)
16982 {
16983 case DW_ATE_address:
16984 /* Turn DW_ATE_address into a void * pointer. */
16985 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16986 type = init_pointer_type (objfile, bits, name, type);
16987 break;
16988 case DW_ATE_boolean:
16989 type = init_boolean_type (objfile, bits, 1, name);
16990 break;
16991 case DW_ATE_complex_float:
16992 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16993 byte_order);
16994 type = init_complex_type (objfile, name, type);
16995 break;
16996 case DW_ATE_decimal_float:
16997 type = init_decfloat_type (objfile, bits, name);
16998 break;
16999 case DW_ATE_float:
17000 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17001 break;
17002 case DW_ATE_signed:
17003 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17004 break;
17005 case DW_ATE_unsigned:
17006 if (cu->language == language_fortran
17007 && name
17008 && startswith (name, "character("))
17009 type = init_character_type (objfile, bits, 1, name);
17010 else
17011 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17012 break;
17013 case DW_ATE_signed_char:
17014 if (cu->language == language_ada || cu->language == language_m2
17015 || cu->language == language_pascal
17016 || cu->language == language_fortran)
17017 type = init_character_type (objfile, bits, 0, name);
17018 else
17019 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17020 break;
17021 case DW_ATE_unsigned_char:
17022 if (cu->language == language_ada || cu->language == language_m2
17023 || cu->language == language_pascal
17024 || cu->language == language_fortran
17025 || cu->language == language_rust)
17026 type = init_character_type (objfile, bits, 1, name);
17027 else
17028 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17029 break;
17030 case DW_ATE_UTF:
17031 {
17032 if (bits == 16)
17033 type = builtin_type (arch)->builtin_char16;
17034 else if (bits == 32)
17035 type = builtin_type (arch)->builtin_char32;
17036 else
17037 {
17038 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17039 bits);
17040 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17041 }
17042 return set_die_type (die, type, cu);
17043 }
17044 break;
17045
17046 default:
17047 complaint (_("unsupported DW_AT_encoding: '%s'"),
17048 dwarf_type_encoding_name (encoding));
17049 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17050 break;
17051 }
17052
17053 if (name && strcmp (name, "char") == 0)
17054 TYPE_NOSIGN (type) = 1;
17055
17056 maybe_set_alignment (cu, die, type);
17057
17058 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17059
17060 return set_die_type (die, type, cu);
17061 }
17062
17063 /* Parse dwarf attribute if it's a block, reference or constant and put the
17064 resulting value of the attribute into struct bound_prop.
17065 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17066
17067 static int
17068 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17069 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17070 struct type *default_type)
17071 {
17072 struct dwarf2_property_baton *baton;
17073 struct obstack *obstack
17074 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17075
17076 gdb_assert (default_type != NULL);
17077
17078 if (attr == NULL || prop == NULL)
17079 return 0;
17080
17081 if (attr->form_is_block ())
17082 {
17083 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17084 baton->property_type = default_type;
17085 baton->locexpr.per_cu = cu->per_cu;
17086 baton->locexpr.size = DW_BLOCK (attr)->size;
17087 baton->locexpr.data = DW_BLOCK (attr)->data;
17088 switch (attr->name)
17089 {
17090 case DW_AT_string_length:
17091 baton->locexpr.is_reference = true;
17092 break;
17093 default:
17094 baton->locexpr.is_reference = false;
17095 break;
17096 }
17097 prop->data.baton = baton;
17098 prop->kind = PROP_LOCEXPR;
17099 gdb_assert (prop->data.baton != NULL);
17100 }
17101 else if (attr->form_is_ref ())
17102 {
17103 struct dwarf2_cu *target_cu = cu;
17104 struct die_info *target_die;
17105 struct attribute *target_attr;
17106
17107 target_die = follow_die_ref (die, attr, &target_cu);
17108 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17109 if (target_attr == NULL)
17110 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17111 target_cu);
17112 if (target_attr == NULL)
17113 return 0;
17114
17115 switch (target_attr->name)
17116 {
17117 case DW_AT_location:
17118 if (target_attr->form_is_section_offset ())
17119 {
17120 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17121 baton->property_type = die_type (target_die, target_cu);
17122 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17123 prop->data.baton = baton;
17124 prop->kind = PROP_LOCLIST;
17125 gdb_assert (prop->data.baton != NULL);
17126 }
17127 else if (target_attr->form_is_block ())
17128 {
17129 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17130 baton->property_type = die_type (target_die, target_cu);
17131 baton->locexpr.per_cu = cu->per_cu;
17132 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17133 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17134 baton->locexpr.is_reference = true;
17135 prop->data.baton = baton;
17136 prop->kind = PROP_LOCEXPR;
17137 gdb_assert (prop->data.baton != NULL);
17138 }
17139 else
17140 {
17141 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17142 "dynamic property");
17143 return 0;
17144 }
17145 break;
17146 case DW_AT_data_member_location:
17147 {
17148 LONGEST offset;
17149
17150 if (!handle_data_member_location (target_die, target_cu,
17151 &offset))
17152 return 0;
17153
17154 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17155 baton->property_type = read_type_die (target_die->parent,
17156 target_cu);
17157 baton->offset_info.offset = offset;
17158 baton->offset_info.type = die_type (target_die, target_cu);
17159 prop->data.baton = baton;
17160 prop->kind = PROP_ADDR_OFFSET;
17161 break;
17162 }
17163 }
17164 }
17165 else if (attr->form_is_constant ())
17166 {
17167 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17168 prop->kind = PROP_CONST;
17169 }
17170 else
17171 {
17172 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17173 dwarf2_name (die, cu));
17174 return 0;
17175 }
17176
17177 return 1;
17178 }
17179
17180 /* See read.h. */
17181
17182 struct type *
17183 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17184 {
17185 struct objfile *objfile = dwarf2_per_objfile->objfile;
17186 struct type *int_type;
17187
17188 /* Helper macro to examine the various builtin types. */
17189 #define TRY_TYPE(F) \
17190 int_type = (unsigned_p \
17191 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17192 : objfile_type (objfile)->builtin_ ## F); \
17193 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17194 return int_type
17195
17196 TRY_TYPE (char);
17197 TRY_TYPE (short);
17198 TRY_TYPE (int);
17199 TRY_TYPE (long);
17200 TRY_TYPE (long_long);
17201
17202 #undef TRY_TYPE
17203
17204 gdb_assert_not_reached ("unable to find suitable integer type");
17205 }
17206
17207 /* See read.h. */
17208
17209 struct type *
17210 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17211 {
17212 int addr_size = this->addr_size ();
17213 return int_type (addr_size, unsigned_p);
17214 }
17215
17216 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17217 present (which is valid) then compute the default type based on the
17218 compilation units address size. */
17219
17220 static struct type *
17221 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17222 {
17223 struct type *index_type = die_type (die, cu);
17224
17225 /* Dwarf-2 specifications explicitly allows to create subrange types
17226 without specifying a base type.
17227 In that case, the base type must be set to the type of
17228 the lower bound, upper bound or count, in that order, if any of these
17229 three attributes references an object that has a type.
17230 If no base type is found, the Dwarf-2 specifications say that
17231 a signed integer type of size equal to the size of an address should
17232 be used.
17233 For the following C code: `extern char gdb_int [];'
17234 GCC produces an empty range DIE.
17235 FIXME: muller/2010-05-28: Possible references to object for low bound,
17236 high bound or count are not yet handled by this code. */
17237 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17238 index_type = cu->per_cu->addr_sized_int_type (false);
17239
17240 return index_type;
17241 }
17242
17243 /* Read the given DW_AT_subrange DIE. */
17244
17245 static struct type *
17246 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17247 {
17248 struct type *base_type, *orig_base_type;
17249 struct type *range_type;
17250 struct attribute *attr;
17251 struct dynamic_prop low, high;
17252 int low_default_is_valid;
17253 int high_bound_is_count = 0;
17254 const char *name;
17255 ULONGEST negative_mask;
17256
17257 orig_base_type = read_subrange_index_type (die, cu);
17258
17259 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17260 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17261 creating the range type, but we use the result of check_typedef
17262 when examining properties of the type. */
17263 base_type = check_typedef (orig_base_type);
17264
17265 /* The die_type call above may have already set the type for this DIE. */
17266 range_type = get_die_type (die, cu);
17267 if (range_type)
17268 return range_type;
17269
17270 low.kind = PROP_CONST;
17271 high.kind = PROP_CONST;
17272 high.data.const_val = 0;
17273
17274 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17275 omitting DW_AT_lower_bound. */
17276 switch (cu->language)
17277 {
17278 case language_c:
17279 case language_cplus:
17280 low.data.const_val = 0;
17281 low_default_is_valid = 1;
17282 break;
17283 case language_fortran:
17284 low.data.const_val = 1;
17285 low_default_is_valid = 1;
17286 break;
17287 case language_d:
17288 case language_objc:
17289 case language_rust:
17290 low.data.const_val = 0;
17291 low_default_is_valid = (cu->header.version >= 4);
17292 break;
17293 case language_ada:
17294 case language_m2:
17295 case language_pascal:
17296 low.data.const_val = 1;
17297 low_default_is_valid = (cu->header.version >= 4);
17298 break;
17299 default:
17300 low.data.const_val = 0;
17301 low_default_is_valid = 0;
17302 break;
17303 }
17304
17305 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17306 if (attr != nullptr)
17307 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17308 else if (!low_default_is_valid)
17309 complaint (_("Missing DW_AT_lower_bound "
17310 "- DIE at %s [in module %s]"),
17311 sect_offset_str (die->sect_off),
17312 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17313
17314 struct attribute *attr_ub, *attr_count;
17315 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17316 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17317 {
17318 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17319 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17320 {
17321 /* If bounds are constant do the final calculation here. */
17322 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17323 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17324 else
17325 high_bound_is_count = 1;
17326 }
17327 else
17328 {
17329 if (attr_ub != NULL)
17330 complaint (_("Unresolved DW_AT_upper_bound "
17331 "- DIE at %s [in module %s]"),
17332 sect_offset_str (die->sect_off),
17333 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17334 if (attr_count != NULL)
17335 complaint (_("Unresolved DW_AT_count "
17336 "- DIE at %s [in module %s]"),
17337 sect_offset_str (die->sect_off),
17338 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17339 }
17340 }
17341
17342 LONGEST bias = 0;
17343 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17344 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17345 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17346
17347 /* Normally, the DWARF producers are expected to use a signed
17348 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17349 But this is unfortunately not always the case, as witnessed
17350 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17351 is used instead. To work around that ambiguity, we treat
17352 the bounds as signed, and thus sign-extend their values, when
17353 the base type is signed. */
17354 negative_mask =
17355 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17356 if (low.kind == PROP_CONST
17357 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17358 low.data.const_val |= negative_mask;
17359 if (high.kind == PROP_CONST
17360 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17361 high.data.const_val |= negative_mask;
17362
17363 /* Check for bit and byte strides. */
17364 struct dynamic_prop byte_stride_prop;
17365 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17366 if (attr_byte_stride != nullptr)
17367 {
17368 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17369 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17370 prop_type);
17371 }
17372
17373 struct dynamic_prop bit_stride_prop;
17374 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17375 if (attr_bit_stride != nullptr)
17376 {
17377 /* It only makes sense to have either a bit or byte stride. */
17378 if (attr_byte_stride != nullptr)
17379 {
17380 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17381 "- DIE at %s [in module %s]"),
17382 sect_offset_str (die->sect_off),
17383 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17384 attr_bit_stride = nullptr;
17385 }
17386 else
17387 {
17388 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17389 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17390 prop_type);
17391 }
17392 }
17393
17394 if (attr_byte_stride != nullptr
17395 || attr_bit_stride != nullptr)
17396 {
17397 bool byte_stride_p = (attr_byte_stride != nullptr);
17398 struct dynamic_prop *stride
17399 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17400
17401 range_type
17402 = create_range_type_with_stride (NULL, orig_base_type, &low,
17403 &high, bias, stride, byte_stride_p);
17404 }
17405 else
17406 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17407
17408 if (high_bound_is_count)
17409 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17410
17411 /* Ada expects an empty array on no boundary attributes. */
17412 if (attr == NULL && cu->language != language_ada)
17413 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17414
17415 name = dwarf2_name (die, cu);
17416 if (name)
17417 TYPE_NAME (range_type) = name;
17418
17419 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17420 if (attr != nullptr)
17421 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17422
17423 maybe_set_alignment (cu, die, range_type);
17424
17425 set_die_type (die, range_type, cu);
17426
17427 /* set_die_type should be already done. */
17428 set_descriptive_type (range_type, die, cu);
17429
17430 return range_type;
17431 }
17432
17433 static struct type *
17434 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17435 {
17436 struct type *type;
17437
17438 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17439 NULL);
17440 TYPE_NAME (type) = dwarf2_name (die, cu);
17441
17442 /* In Ada, an unspecified type is typically used when the description
17443 of the type is deferred to a different unit. When encountering
17444 such a type, we treat it as a stub, and try to resolve it later on,
17445 when needed. */
17446 if (cu->language == language_ada)
17447 TYPE_STUB (type) = 1;
17448
17449 return set_die_type (die, type, cu);
17450 }
17451
17452 /* Read a single die and all its descendents. Set the die's sibling
17453 field to NULL; set other fields in the die correctly, and set all
17454 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17455 location of the info_ptr after reading all of those dies. PARENT
17456 is the parent of the die in question. */
17457
17458 static struct die_info *
17459 read_die_and_children (const struct die_reader_specs *reader,
17460 const gdb_byte *info_ptr,
17461 const gdb_byte **new_info_ptr,
17462 struct die_info *parent)
17463 {
17464 struct die_info *die;
17465 const gdb_byte *cur_ptr;
17466
17467 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17468 if (die == NULL)
17469 {
17470 *new_info_ptr = cur_ptr;
17471 return NULL;
17472 }
17473 store_in_ref_table (die, reader->cu);
17474
17475 if (die->has_children)
17476 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17477 else
17478 {
17479 die->child = NULL;
17480 *new_info_ptr = cur_ptr;
17481 }
17482
17483 die->sibling = NULL;
17484 die->parent = parent;
17485 return die;
17486 }
17487
17488 /* Read a die, all of its descendents, and all of its siblings; set
17489 all of the fields of all of the dies correctly. Arguments are as
17490 in read_die_and_children. */
17491
17492 static struct die_info *
17493 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17494 const gdb_byte *info_ptr,
17495 const gdb_byte **new_info_ptr,
17496 struct die_info *parent)
17497 {
17498 struct die_info *first_die, *last_sibling;
17499 const gdb_byte *cur_ptr;
17500
17501 cur_ptr = info_ptr;
17502 first_die = last_sibling = NULL;
17503
17504 while (1)
17505 {
17506 struct die_info *die
17507 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17508
17509 if (die == NULL)
17510 {
17511 *new_info_ptr = cur_ptr;
17512 return first_die;
17513 }
17514
17515 if (!first_die)
17516 first_die = die;
17517 else
17518 last_sibling->sibling = die;
17519
17520 last_sibling = die;
17521 }
17522 }
17523
17524 /* Read a die, all of its descendents, and all of its siblings; set
17525 all of the fields of all of the dies correctly. Arguments are as
17526 in read_die_and_children.
17527 This the main entry point for reading a DIE and all its children. */
17528
17529 static struct die_info *
17530 read_die_and_siblings (const struct die_reader_specs *reader,
17531 const gdb_byte *info_ptr,
17532 const gdb_byte **new_info_ptr,
17533 struct die_info *parent)
17534 {
17535 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17536 new_info_ptr, parent);
17537
17538 if (dwarf_die_debug)
17539 {
17540 fprintf_unfiltered (gdb_stdlog,
17541 "Read die from %s@0x%x of %s:\n",
17542 reader->die_section->get_name (),
17543 (unsigned) (info_ptr - reader->die_section->buffer),
17544 bfd_get_filename (reader->abfd));
17545 dump_die (die, dwarf_die_debug);
17546 }
17547
17548 return die;
17549 }
17550
17551 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17552 attributes.
17553 The caller is responsible for filling in the extra attributes
17554 and updating (*DIEP)->num_attrs.
17555 Set DIEP to point to a newly allocated die with its information,
17556 except for its child, sibling, and parent fields. */
17557
17558 static const gdb_byte *
17559 read_full_die_1 (const struct die_reader_specs *reader,
17560 struct die_info **diep, const gdb_byte *info_ptr,
17561 int num_extra_attrs)
17562 {
17563 unsigned int abbrev_number, bytes_read, i;
17564 struct abbrev_info *abbrev;
17565 struct die_info *die;
17566 struct dwarf2_cu *cu = reader->cu;
17567 bfd *abfd = reader->abfd;
17568
17569 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17570 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17571 info_ptr += bytes_read;
17572 if (!abbrev_number)
17573 {
17574 *diep = NULL;
17575 return info_ptr;
17576 }
17577
17578 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17579 if (!abbrev)
17580 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17581 abbrev_number,
17582 bfd_get_filename (abfd));
17583
17584 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17585 die->sect_off = sect_off;
17586 die->tag = abbrev->tag;
17587 die->abbrev = abbrev_number;
17588 die->has_children = abbrev->has_children;
17589
17590 /* Make the result usable.
17591 The caller needs to update num_attrs after adding the extra
17592 attributes. */
17593 die->num_attrs = abbrev->num_attrs;
17594
17595 std::vector<int> indexes_that_need_reprocess;
17596 for (i = 0; i < abbrev->num_attrs; ++i)
17597 {
17598 bool need_reprocess;
17599 info_ptr =
17600 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17601 info_ptr, &need_reprocess);
17602 if (need_reprocess)
17603 indexes_that_need_reprocess.push_back (i);
17604 }
17605
17606 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17607 if (attr != nullptr)
17608 cu->str_offsets_base = DW_UNSND (attr);
17609
17610 auto maybe_addr_base = lookup_addr_base(die);
17611 if (maybe_addr_base.has_value ())
17612 cu->addr_base = *maybe_addr_base;
17613 for (int index : indexes_that_need_reprocess)
17614 read_attribute_reprocess (reader, &die->attrs[index]);
17615 *diep = die;
17616 return info_ptr;
17617 }
17618
17619 /* Read a die and all its attributes.
17620 Set DIEP to point to a newly allocated die with its information,
17621 except for its child, sibling, and parent fields. */
17622
17623 static const gdb_byte *
17624 read_full_die (const struct die_reader_specs *reader,
17625 struct die_info **diep, const gdb_byte *info_ptr)
17626 {
17627 const gdb_byte *result;
17628
17629 result = read_full_die_1 (reader, diep, info_ptr, 0);
17630
17631 if (dwarf_die_debug)
17632 {
17633 fprintf_unfiltered (gdb_stdlog,
17634 "Read die from %s@0x%x of %s:\n",
17635 reader->die_section->get_name (),
17636 (unsigned) (info_ptr - reader->die_section->buffer),
17637 bfd_get_filename (reader->abfd));
17638 dump_die (*diep, dwarf_die_debug);
17639 }
17640
17641 return result;
17642 }
17643 \f
17644
17645 /* Returns nonzero if TAG represents a type that we might generate a partial
17646 symbol for. */
17647
17648 static int
17649 is_type_tag_for_partial (int tag)
17650 {
17651 switch (tag)
17652 {
17653 #if 0
17654 /* Some types that would be reasonable to generate partial symbols for,
17655 that we don't at present. */
17656 case DW_TAG_array_type:
17657 case DW_TAG_file_type:
17658 case DW_TAG_ptr_to_member_type:
17659 case DW_TAG_set_type:
17660 case DW_TAG_string_type:
17661 case DW_TAG_subroutine_type:
17662 #endif
17663 case DW_TAG_base_type:
17664 case DW_TAG_class_type:
17665 case DW_TAG_interface_type:
17666 case DW_TAG_enumeration_type:
17667 case DW_TAG_structure_type:
17668 case DW_TAG_subrange_type:
17669 case DW_TAG_typedef:
17670 case DW_TAG_union_type:
17671 return 1;
17672 default:
17673 return 0;
17674 }
17675 }
17676
17677 /* Load all DIEs that are interesting for partial symbols into memory. */
17678
17679 static struct partial_die_info *
17680 load_partial_dies (const struct die_reader_specs *reader,
17681 const gdb_byte *info_ptr, int building_psymtab)
17682 {
17683 struct dwarf2_cu *cu = reader->cu;
17684 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17685 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17686 unsigned int bytes_read;
17687 unsigned int load_all = 0;
17688 int nesting_level = 1;
17689
17690 parent_die = NULL;
17691 last_die = NULL;
17692
17693 gdb_assert (cu->per_cu != NULL);
17694 if (cu->per_cu->load_all_dies)
17695 load_all = 1;
17696
17697 cu->partial_dies
17698 = htab_create_alloc_ex (cu->header.length / 12,
17699 partial_die_hash,
17700 partial_die_eq,
17701 NULL,
17702 &cu->comp_unit_obstack,
17703 hashtab_obstack_allocate,
17704 dummy_obstack_deallocate);
17705
17706 while (1)
17707 {
17708 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17709
17710 /* A NULL abbrev means the end of a series of children. */
17711 if (abbrev == NULL)
17712 {
17713 if (--nesting_level == 0)
17714 return first_die;
17715
17716 info_ptr += bytes_read;
17717 last_die = parent_die;
17718 parent_die = parent_die->die_parent;
17719 continue;
17720 }
17721
17722 /* Check for template arguments. We never save these; if
17723 they're seen, we just mark the parent, and go on our way. */
17724 if (parent_die != NULL
17725 && cu->language == language_cplus
17726 && (abbrev->tag == DW_TAG_template_type_param
17727 || abbrev->tag == DW_TAG_template_value_param))
17728 {
17729 parent_die->has_template_arguments = 1;
17730
17731 if (!load_all)
17732 {
17733 /* We don't need a partial DIE for the template argument. */
17734 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17735 continue;
17736 }
17737 }
17738
17739 /* We only recurse into c++ subprograms looking for template arguments.
17740 Skip their other children. */
17741 if (!load_all
17742 && cu->language == language_cplus
17743 && parent_die != NULL
17744 && parent_die->tag == DW_TAG_subprogram)
17745 {
17746 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17747 continue;
17748 }
17749
17750 /* Check whether this DIE is interesting enough to save. Normally
17751 we would not be interested in members here, but there may be
17752 later variables referencing them via DW_AT_specification (for
17753 static members). */
17754 if (!load_all
17755 && !is_type_tag_for_partial (abbrev->tag)
17756 && abbrev->tag != DW_TAG_constant
17757 && abbrev->tag != DW_TAG_enumerator
17758 && abbrev->tag != DW_TAG_subprogram
17759 && abbrev->tag != DW_TAG_inlined_subroutine
17760 && abbrev->tag != DW_TAG_lexical_block
17761 && abbrev->tag != DW_TAG_variable
17762 && abbrev->tag != DW_TAG_namespace
17763 && abbrev->tag != DW_TAG_module
17764 && abbrev->tag != DW_TAG_member
17765 && abbrev->tag != DW_TAG_imported_unit
17766 && abbrev->tag != DW_TAG_imported_declaration)
17767 {
17768 /* Otherwise we skip to the next sibling, if any. */
17769 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17770 continue;
17771 }
17772
17773 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17774 abbrev);
17775
17776 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17777
17778 /* This two-pass algorithm for processing partial symbols has a
17779 high cost in cache pressure. Thus, handle some simple cases
17780 here which cover the majority of C partial symbols. DIEs
17781 which neither have specification tags in them, nor could have
17782 specification tags elsewhere pointing at them, can simply be
17783 processed and discarded.
17784
17785 This segment is also optional; scan_partial_symbols and
17786 add_partial_symbol will handle these DIEs if we chain
17787 them in normally. When compilers which do not emit large
17788 quantities of duplicate debug information are more common,
17789 this code can probably be removed. */
17790
17791 /* Any complete simple types at the top level (pretty much all
17792 of them, for a language without namespaces), can be processed
17793 directly. */
17794 if (parent_die == NULL
17795 && pdi.has_specification == 0
17796 && pdi.is_declaration == 0
17797 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17798 || pdi.tag == DW_TAG_base_type
17799 || pdi.tag == DW_TAG_subrange_type))
17800 {
17801 if (building_psymtab && pdi.name != NULL)
17802 add_psymbol_to_list (pdi.name, false,
17803 VAR_DOMAIN, LOC_TYPEDEF, -1,
17804 psymbol_placement::STATIC,
17805 0, cu->language, objfile);
17806 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17807 continue;
17808 }
17809
17810 /* The exception for DW_TAG_typedef with has_children above is
17811 a workaround of GCC PR debug/47510. In the case of this complaint
17812 type_name_or_error will error on such types later.
17813
17814 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17815 it could not find the child DIEs referenced later, this is checked
17816 above. In correct DWARF DW_TAG_typedef should have no children. */
17817
17818 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17819 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17820 "- DIE at %s [in module %s]"),
17821 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17822
17823 /* If we're at the second level, and we're an enumerator, and
17824 our parent has no specification (meaning possibly lives in a
17825 namespace elsewhere), then we can add the partial symbol now
17826 instead of queueing it. */
17827 if (pdi.tag == DW_TAG_enumerator
17828 && parent_die != NULL
17829 && parent_die->die_parent == NULL
17830 && parent_die->tag == DW_TAG_enumeration_type
17831 && parent_die->has_specification == 0)
17832 {
17833 if (pdi.name == NULL)
17834 complaint (_("malformed enumerator DIE ignored"));
17835 else if (building_psymtab)
17836 add_psymbol_to_list (pdi.name, false,
17837 VAR_DOMAIN, LOC_CONST, -1,
17838 cu->language == language_cplus
17839 ? psymbol_placement::GLOBAL
17840 : psymbol_placement::STATIC,
17841 0, cu->language, objfile);
17842
17843 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17844 continue;
17845 }
17846
17847 struct partial_die_info *part_die
17848 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17849
17850 /* We'll save this DIE so link it in. */
17851 part_die->die_parent = parent_die;
17852 part_die->die_sibling = NULL;
17853 part_die->die_child = NULL;
17854
17855 if (last_die && last_die == parent_die)
17856 last_die->die_child = part_die;
17857 else if (last_die)
17858 last_die->die_sibling = part_die;
17859
17860 last_die = part_die;
17861
17862 if (first_die == NULL)
17863 first_die = part_die;
17864
17865 /* Maybe add the DIE to the hash table. Not all DIEs that we
17866 find interesting need to be in the hash table, because we
17867 also have the parent/sibling/child chains; only those that we
17868 might refer to by offset later during partial symbol reading.
17869
17870 For now this means things that might have be the target of a
17871 DW_AT_specification, DW_AT_abstract_origin, or
17872 DW_AT_extension. DW_AT_extension will refer only to
17873 namespaces; DW_AT_abstract_origin refers to functions (and
17874 many things under the function DIE, but we do not recurse
17875 into function DIEs during partial symbol reading) and
17876 possibly variables as well; DW_AT_specification refers to
17877 declarations. Declarations ought to have the DW_AT_declaration
17878 flag. It happens that GCC forgets to put it in sometimes, but
17879 only for functions, not for types.
17880
17881 Adding more things than necessary to the hash table is harmless
17882 except for the performance cost. Adding too few will result in
17883 wasted time in find_partial_die, when we reread the compilation
17884 unit with load_all_dies set. */
17885
17886 if (load_all
17887 || abbrev->tag == DW_TAG_constant
17888 || abbrev->tag == DW_TAG_subprogram
17889 || abbrev->tag == DW_TAG_variable
17890 || abbrev->tag == DW_TAG_namespace
17891 || part_die->is_declaration)
17892 {
17893 void **slot;
17894
17895 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17896 to_underlying (part_die->sect_off),
17897 INSERT);
17898 *slot = part_die;
17899 }
17900
17901 /* For some DIEs we want to follow their children (if any). For C
17902 we have no reason to follow the children of structures; for other
17903 languages we have to, so that we can get at method physnames
17904 to infer fully qualified class names, for DW_AT_specification,
17905 and for C++ template arguments. For C++, we also look one level
17906 inside functions to find template arguments (if the name of the
17907 function does not already contain the template arguments).
17908
17909 For Ada and Fortran, we need to scan the children of subprograms
17910 and lexical blocks as well because these languages allow the
17911 definition of nested entities that could be interesting for the
17912 debugger, such as nested subprograms for instance. */
17913 if (last_die->has_children
17914 && (load_all
17915 || last_die->tag == DW_TAG_namespace
17916 || last_die->tag == DW_TAG_module
17917 || last_die->tag == DW_TAG_enumeration_type
17918 || (cu->language == language_cplus
17919 && last_die->tag == DW_TAG_subprogram
17920 && (last_die->name == NULL
17921 || strchr (last_die->name, '<') == NULL))
17922 || (cu->language != language_c
17923 && (last_die->tag == DW_TAG_class_type
17924 || last_die->tag == DW_TAG_interface_type
17925 || last_die->tag == DW_TAG_structure_type
17926 || last_die->tag == DW_TAG_union_type))
17927 || ((cu->language == language_ada
17928 || cu->language == language_fortran)
17929 && (last_die->tag == DW_TAG_subprogram
17930 || last_die->tag == DW_TAG_lexical_block))))
17931 {
17932 nesting_level++;
17933 parent_die = last_die;
17934 continue;
17935 }
17936
17937 /* Otherwise we skip to the next sibling, if any. */
17938 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17939
17940 /* Back to the top, do it again. */
17941 }
17942 }
17943
17944 partial_die_info::partial_die_info (sect_offset sect_off_,
17945 struct abbrev_info *abbrev)
17946 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17947 {
17948 }
17949
17950 /* Read a minimal amount of information into the minimal die structure.
17951 INFO_PTR should point just after the initial uleb128 of a DIE. */
17952
17953 const gdb_byte *
17954 partial_die_info::read (const struct die_reader_specs *reader,
17955 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17956 {
17957 struct dwarf2_cu *cu = reader->cu;
17958 struct dwarf2_per_objfile *dwarf2_per_objfile
17959 = cu->per_cu->dwarf2_per_objfile;
17960 unsigned int i;
17961 int has_low_pc_attr = 0;
17962 int has_high_pc_attr = 0;
17963 int high_pc_relative = 0;
17964
17965 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17966 for (i = 0; i < abbrev.num_attrs; ++i)
17967 {
17968 bool need_reprocess;
17969 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17970 info_ptr, &need_reprocess);
17971 /* String and address offsets that need to do the reprocessing have
17972 already been read at this point, so there is no need to wait until
17973 the loop terminates to do the reprocessing. */
17974 if (need_reprocess)
17975 read_attribute_reprocess (reader, &attr_vec[i]);
17976 attribute &attr = attr_vec[i];
17977 /* Store the data if it is of an attribute we want to keep in a
17978 partial symbol table. */
17979 switch (attr.name)
17980 {
17981 case DW_AT_name:
17982 switch (tag)
17983 {
17984 case DW_TAG_compile_unit:
17985 case DW_TAG_partial_unit:
17986 case DW_TAG_type_unit:
17987 /* Compilation units have a DW_AT_name that is a filename, not
17988 a source language identifier. */
17989 case DW_TAG_enumeration_type:
17990 case DW_TAG_enumerator:
17991 /* These tags always have simple identifiers already; no need
17992 to canonicalize them. */
17993 name = DW_STRING (&attr);
17994 break;
17995 default:
17996 {
17997 struct objfile *objfile = dwarf2_per_objfile->objfile;
17998
17999 name
18000 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18001 }
18002 break;
18003 }
18004 break;
18005 case DW_AT_linkage_name:
18006 case DW_AT_MIPS_linkage_name:
18007 /* Note that both forms of linkage name might appear. We
18008 assume they will be the same, and we only store the last
18009 one we see. */
18010 linkage_name = DW_STRING (&attr);
18011 break;
18012 case DW_AT_low_pc:
18013 has_low_pc_attr = 1;
18014 lowpc = attr.value_as_address ();
18015 break;
18016 case DW_AT_high_pc:
18017 has_high_pc_attr = 1;
18018 highpc = attr.value_as_address ();
18019 if (cu->header.version >= 4 && attr.form_is_constant ())
18020 high_pc_relative = 1;
18021 break;
18022 case DW_AT_location:
18023 /* Support the .debug_loc offsets. */
18024 if (attr.form_is_block ())
18025 {
18026 d.locdesc = DW_BLOCK (&attr);
18027 }
18028 else if (attr.form_is_section_offset ())
18029 {
18030 dwarf2_complex_location_expr_complaint ();
18031 }
18032 else
18033 {
18034 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18035 "partial symbol information");
18036 }
18037 break;
18038 case DW_AT_external:
18039 is_external = DW_UNSND (&attr);
18040 break;
18041 case DW_AT_declaration:
18042 is_declaration = DW_UNSND (&attr);
18043 break;
18044 case DW_AT_type:
18045 has_type = 1;
18046 break;
18047 case DW_AT_abstract_origin:
18048 case DW_AT_specification:
18049 case DW_AT_extension:
18050 has_specification = 1;
18051 spec_offset = dwarf2_get_ref_die_offset (&attr);
18052 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18053 || cu->per_cu->is_dwz);
18054 break;
18055 case DW_AT_sibling:
18056 /* Ignore absolute siblings, they might point outside of
18057 the current compile unit. */
18058 if (attr.form == DW_FORM_ref_addr)
18059 complaint (_("ignoring absolute DW_AT_sibling"));
18060 else
18061 {
18062 const gdb_byte *buffer = reader->buffer;
18063 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18064 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18065
18066 if (sibling_ptr < info_ptr)
18067 complaint (_("DW_AT_sibling points backwards"));
18068 else if (sibling_ptr > reader->buffer_end)
18069 reader->die_section->overflow_complaint ();
18070 else
18071 sibling = sibling_ptr;
18072 }
18073 break;
18074 case DW_AT_byte_size:
18075 has_byte_size = 1;
18076 break;
18077 case DW_AT_const_value:
18078 has_const_value = 1;
18079 break;
18080 case DW_AT_calling_convention:
18081 /* DWARF doesn't provide a way to identify a program's source-level
18082 entry point. DW_AT_calling_convention attributes are only meant
18083 to describe functions' calling conventions.
18084
18085 However, because it's a necessary piece of information in
18086 Fortran, and before DWARF 4 DW_CC_program was the only
18087 piece of debugging information whose definition refers to
18088 a 'main program' at all, several compilers marked Fortran
18089 main programs with DW_CC_program --- even when those
18090 functions use the standard calling conventions.
18091
18092 Although DWARF now specifies a way to provide this
18093 information, we support this practice for backward
18094 compatibility. */
18095 if (DW_UNSND (&attr) == DW_CC_program
18096 && cu->language == language_fortran)
18097 main_subprogram = 1;
18098 break;
18099 case DW_AT_inline:
18100 if (DW_UNSND (&attr) == DW_INL_inlined
18101 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18102 may_be_inlined = 1;
18103 break;
18104
18105 case DW_AT_import:
18106 if (tag == DW_TAG_imported_unit)
18107 {
18108 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18109 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18110 || cu->per_cu->is_dwz);
18111 }
18112 break;
18113
18114 case DW_AT_main_subprogram:
18115 main_subprogram = DW_UNSND (&attr);
18116 break;
18117
18118 case DW_AT_ranges:
18119 {
18120 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18121 but that requires a full DIE, so instead we just
18122 reimplement it. */
18123 int need_ranges_base = tag != DW_TAG_compile_unit;
18124 unsigned int ranges_offset = (DW_UNSND (&attr)
18125 + (need_ranges_base
18126 ? cu->ranges_base
18127 : 0));
18128
18129 /* Value of the DW_AT_ranges attribute is the offset in the
18130 .debug_ranges section. */
18131 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18132 nullptr))
18133 has_pc_info = 1;
18134 }
18135 break;
18136
18137 default:
18138 break;
18139 }
18140 }
18141
18142 /* For Ada, if both the name and the linkage name appear, we prefer
18143 the latter. This lets "catch exception" work better, regardless
18144 of the order in which the name and linkage name were emitted.
18145 Really, though, this is just a workaround for the fact that gdb
18146 doesn't store both the name and the linkage name. */
18147 if (cu->language == language_ada && linkage_name != nullptr)
18148 name = linkage_name;
18149
18150 if (high_pc_relative)
18151 highpc += lowpc;
18152
18153 if (has_low_pc_attr && has_high_pc_attr)
18154 {
18155 /* When using the GNU linker, .gnu.linkonce. sections are used to
18156 eliminate duplicate copies of functions and vtables and such.
18157 The linker will arbitrarily choose one and discard the others.
18158 The AT_*_pc values for such functions refer to local labels in
18159 these sections. If the section from that file was discarded, the
18160 labels are not in the output, so the relocs get a value of 0.
18161 If this is a discarded function, mark the pc bounds as invalid,
18162 so that GDB will ignore it. */
18163 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18164 {
18165 struct objfile *objfile = dwarf2_per_objfile->objfile;
18166 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18167
18168 complaint (_("DW_AT_low_pc %s is zero "
18169 "for DIE at %s [in module %s]"),
18170 paddress (gdbarch, lowpc),
18171 sect_offset_str (sect_off),
18172 objfile_name (objfile));
18173 }
18174 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18175 else if (lowpc >= highpc)
18176 {
18177 struct objfile *objfile = dwarf2_per_objfile->objfile;
18178 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18179
18180 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18181 "for DIE at %s [in module %s]"),
18182 paddress (gdbarch, lowpc),
18183 paddress (gdbarch, highpc),
18184 sect_offset_str (sect_off),
18185 objfile_name (objfile));
18186 }
18187 else
18188 has_pc_info = 1;
18189 }
18190
18191 return info_ptr;
18192 }
18193
18194 /* Find a cached partial DIE at OFFSET in CU. */
18195
18196 struct partial_die_info *
18197 dwarf2_cu::find_partial_die (sect_offset sect_off)
18198 {
18199 struct partial_die_info *lookup_die = NULL;
18200 struct partial_die_info part_die (sect_off);
18201
18202 lookup_die = ((struct partial_die_info *)
18203 htab_find_with_hash (partial_dies, &part_die,
18204 to_underlying (sect_off)));
18205
18206 return lookup_die;
18207 }
18208
18209 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18210 except in the case of .debug_types DIEs which do not reference
18211 outside their CU (they do however referencing other types via
18212 DW_FORM_ref_sig8). */
18213
18214 static const struct cu_partial_die_info
18215 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18216 {
18217 struct dwarf2_per_objfile *dwarf2_per_objfile
18218 = cu->per_cu->dwarf2_per_objfile;
18219 struct objfile *objfile = dwarf2_per_objfile->objfile;
18220 struct dwarf2_per_cu_data *per_cu = NULL;
18221 struct partial_die_info *pd = NULL;
18222
18223 if (offset_in_dwz == cu->per_cu->is_dwz
18224 && cu->header.offset_in_cu_p (sect_off))
18225 {
18226 pd = cu->find_partial_die (sect_off);
18227 if (pd != NULL)
18228 return { cu, pd };
18229 /* We missed recording what we needed.
18230 Load all dies and try again. */
18231 per_cu = cu->per_cu;
18232 }
18233 else
18234 {
18235 /* TUs don't reference other CUs/TUs (except via type signatures). */
18236 if (cu->per_cu->is_debug_types)
18237 {
18238 error (_("Dwarf Error: Type Unit at offset %s contains"
18239 " external reference to offset %s [in module %s].\n"),
18240 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18241 bfd_get_filename (objfile->obfd));
18242 }
18243 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18244 dwarf2_per_objfile);
18245
18246 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18247 load_partial_comp_unit (per_cu);
18248
18249 per_cu->cu->last_used = 0;
18250 pd = per_cu->cu->find_partial_die (sect_off);
18251 }
18252
18253 /* If we didn't find it, and not all dies have been loaded,
18254 load them all and try again. */
18255
18256 if (pd == NULL && per_cu->load_all_dies == 0)
18257 {
18258 per_cu->load_all_dies = 1;
18259
18260 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18261 THIS_CU->cu may already be in use. So we can't just free it and
18262 replace its DIEs with the ones we read in. Instead, we leave those
18263 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18264 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18265 set. */
18266 load_partial_comp_unit (per_cu);
18267
18268 pd = per_cu->cu->find_partial_die (sect_off);
18269 }
18270
18271 if (pd == NULL)
18272 internal_error (__FILE__, __LINE__,
18273 _("could not find partial DIE %s "
18274 "in cache [from module %s]\n"),
18275 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18276 return { per_cu->cu, pd };
18277 }
18278
18279 /* See if we can figure out if the class lives in a namespace. We do
18280 this by looking for a member function; its demangled name will
18281 contain namespace info, if there is any. */
18282
18283 static void
18284 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18285 struct dwarf2_cu *cu)
18286 {
18287 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18288 what template types look like, because the demangler
18289 frequently doesn't give the same name as the debug info. We
18290 could fix this by only using the demangled name to get the
18291 prefix (but see comment in read_structure_type). */
18292
18293 struct partial_die_info *real_pdi;
18294 struct partial_die_info *child_pdi;
18295
18296 /* If this DIE (this DIE's specification, if any) has a parent, then
18297 we should not do this. We'll prepend the parent's fully qualified
18298 name when we create the partial symbol. */
18299
18300 real_pdi = struct_pdi;
18301 while (real_pdi->has_specification)
18302 {
18303 auto res = find_partial_die (real_pdi->spec_offset,
18304 real_pdi->spec_is_dwz, cu);
18305 real_pdi = res.pdi;
18306 cu = res.cu;
18307 }
18308
18309 if (real_pdi->die_parent != NULL)
18310 return;
18311
18312 for (child_pdi = struct_pdi->die_child;
18313 child_pdi != NULL;
18314 child_pdi = child_pdi->die_sibling)
18315 {
18316 if (child_pdi->tag == DW_TAG_subprogram
18317 && child_pdi->linkage_name != NULL)
18318 {
18319 gdb::unique_xmalloc_ptr<char> actual_class_name
18320 (language_class_name_from_physname (cu->language_defn,
18321 child_pdi->linkage_name));
18322 if (actual_class_name != NULL)
18323 {
18324 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18325 struct_pdi->name = objfile->intern (actual_class_name.get ());
18326 }
18327 break;
18328 }
18329 }
18330 }
18331
18332 void
18333 partial_die_info::fixup (struct dwarf2_cu *cu)
18334 {
18335 /* Once we've fixed up a die, there's no point in doing so again.
18336 This also avoids a memory leak if we were to call
18337 guess_partial_die_structure_name multiple times. */
18338 if (fixup_called)
18339 return;
18340
18341 /* If we found a reference attribute and the DIE has no name, try
18342 to find a name in the referred to DIE. */
18343
18344 if (name == NULL && has_specification)
18345 {
18346 struct partial_die_info *spec_die;
18347
18348 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18349 spec_die = res.pdi;
18350 cu = res.cu;
18351
18352 spec_die->fixup (cu);
18353
18354 if (spec_die->name)
18355 {
18356 name = spec_die->name;
18357
18358 /* Copy DW_AT_external attribute if it is set. */
18359 if (spec_die->is_external)
18360 is_external = spec_die->is_external;
18361 }
18362 }
18363
18364 /* Set default names for some unnamed DIEs. */
18365
18366 if (name == NULL && tag == DW_TAG_namespace)
18367 name = CP_ANONYMOUS_NAMESPACE_STR;
18368
18369 /* If there is no parent die to provide a namespace, and there are
18370 children, see if we can determine the namespace from their linkage
18371 name. */
18372 if (cu->language == language_cplus
18373 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18374 && die_parent == NULL
18375 && has_children
18376 && (tag == DW_TAG_class_type
18377 || tag == DW_TAG_structure_type
18378 || tag == DW_TAG_union_type))
18379 guess_partial_die_structure_name (this, cu);
18380
18381 /* GCC might emit a nameless struct or union that has a linkage
18382 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18383 if (name == NULL
18384 && (tag == DW_TAG_class_type
18385 || tag == DW_TAG_interface_type
18386 || tag == DW_TAG_structure_type
18387 || tag == DW_TAG_union_type)
18388 && linkage_name != NULL)
18389 {
18390 gdb::unique_xmalloc_ptr<char> demangled
18391 (gdb_demangle (linkage_name, DMGL_TYPES));
18392 if (demangled != nullptr)
18393 {
18394 const char *base;
18395
18396 /* Strip any leading namespaces/classes, keep only the base name.
18397 DW_AT_name for named DIEs does not contain the prefixes. */
18398 base = strrchr (demangled.get (), ':');
18399 if (base && base > demangled.get () && base[-1] == ':')
18400 base++;
18401 else
18402 base = demangled.get ();
18403
18404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18405 name = objfile->intern (base);
18406 }
18407 }
18408
18409 fixup_called = 1;
18410 }
18411
18412 /* Process the attributes that had to be skipped in the first round. These
18413 attributes are the ones that need str_offsets_base or addr_base attributes.
18414 They could not have been processed in the first round, because at the time
18415 the values of str_offsets_base or addr_base may not have been known. */
18416 void read_attribute_reprocess (const struct die_reader_specs *reader,
18417 struct attribute *attr)
18418 {
18419 struct dwarf2_cu *cu = reader->cu;
18420 switch (attr->form)
18421 {
18422 case DW_FORM_addrx:
18423 case DW_FORM_GNU_addr_index:
18424 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18425 break;
18426 case DW_FORM_strx:
18427 case DW_FORM_strx1:
18428 case DW_FORM_strx2:
18429 case DW_FORM_strx3:
18430 case DW_FORM_strx4:
18431 case DW_FORM_GNU_str_index:
18432 {
18433 unsigned int str_index = DW_UNSND (attr);
18434 if (reader->dwo_file != NULL)
18435 {
18436 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18437 DW_STRING_IS_CANONICAL (attr) = 0;
18438 }
18439 else
18440 {
18441 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18442 DW_STRING_IS_CANONICAL (attr) = 0;
18443 }
18444 break;
18445 }
18446 default:
18447 gdb_assert_not_reached (_("Unexpected DWARF form."));
18448 }
18449 }
18450
18451 /* Read an attribute value described by an attribute form. */
18452
18453 static const gdb_byte *
18454 read_attribute_value (const struct die_reader_specs *reader,
18455 struct attribute *attr, unsigned form,
18456 LONGEST implicit_const, const gdb_byte *info_ptr,
18457 bool *need_reprocess)
18458 {
18459 struct dwarf2_cu *cu = reader->cu;
18460 struct dwarf2_per_objfile *dwarf2_per_objfile
18461 = cu->per_cu->dwarf2_per_objfile;
18462 struct objfile *objfile = dwarf2_per_objfile->objfile;
18463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18464 bfd *abfd = reader->abfd;
18465 struct comp_unit_head *cu_header = &cu->header;
18466 unsigned int bytes_read;
18467 struct dwarf_block *blk;
18468 *need_reprocess = false;
18469
18470 attr->form = (enum dwarf_form) form;
18471 switch (form)
18472 {
18473 case DW_FORM_ref_addr:
18474 if (cu->header.version == 2)
18475 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18476 &bytes_read);
18477 else
18478 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18479 &bytes_read);
18480 info_ptr += bytes_read;
18481 break;
18482 case DW_FORM_GNU_ref_alt:
18483 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18484 info_ptr += bytes_read;
18485 break;
18486 case DW_FORM_addr:
18487 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18488 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18489 info_ptr += bytes_read;
18490 break;
18491 case DW_FORM_block2:
18492 blk = dwarf_alloc_block (cu);
18493 blk->size = read_2_bytes (abfd, info_ptr);
18494 info_ptr += 2;
18495 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18496 info_ptr += blk->size;
18497 DW_BLOCK (attr) = blk;
18498 break;
18499 case DW_FORM_block4:
18500 blk = dwarf_alloc_block (cu);
18501 blk->size = read_4_bytes (abfd, info_ptr);
18502 info_ptr += 4;
18503 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18504 info_ptr += blk->size;
18505 DW_BLOCK (attr) = blk;
18506 break;
18507 case DW_FORM_data2:
18508 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18509 info_ptr += 2;
18510 break;
18511 case DW_FORM_data4:
18512 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18513 info_ptr += 4;
18514 break;
18515 case DW_FORM_data8:
18516 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18517 info_ptr += 8;
18518 break;
18519 case DW_FORM_data16:
18520 blk = dwarf_alloc_block (cu);
18521 blk->size = 16;
18522 blk->data = read_n_bytes (abfd, info_ptr, 16);
18523 info_ptr += 16;
18524 DW_BLOCK (attr) = blk;
18525 break;
18526 case DW_FORM_sec_offset:
18527 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18528 info_ptr += bytes_read;
18529 break;
18530 case DW_FORM_string:
18531 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18532 DW_STRING_IS_CANONICAL (attr) = 0;
18533 info_ptr += bytes_read;
18534 break;
18535 case DW_FORM_strp:
18536 if (!cu->per_cu->is_dwz)
18537 {
18538 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18539 abfd, info_ptr, cu_header,
18540 &bytes_read);
18541 DW_STRING_IS_CANONICAL (attr) = 0;
18542 info_ptr += bytes_read;
18543 break;
18544 }
18545 /* FALLTHROUGH */
18546 case DW_FORM_line_strp:
18547 if (!cu->per_cu->is_dwz)
18548 {
18549 DW_STRING (attr)
18550 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18551 &bytes_read);
18552 DW_STRING_IS_CANONICAL (attr) = 0;
18553 info_ptr += bytes_read;
18554 break;
18555 }
18556 /* FALLTHROUGH */
18557 case DW_FORM_GNU_strp_alt:
18558 {
18559 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18560 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18561 &bytes_read);
18562
18563 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18564 DW_STRING_IS_CANONICAL (attr) = 0;
18565 info_ptr += bytes_read;
18566 }
18567 break;
18568 case DW_FORM_exprloc:
18569 case DW_FORM_block:
18570 blk = dwarf_alloc_block (cu);
18571 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18572 info_ptr += bytes_read;
18573 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18574 info_ptr += blk->size;
18575 DW_BLOCK (attr) = blk;
18576 break;
18577 case DW_FORM_block1:
18578 blk = dwarf_alloc_block (cu);
18579 blk->size = read_1_byte (abfd, info_ptr);
18580 info_ptr += 1;
18581 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18582 info_ptr += blk->size;
18583 DW_BLOCK (attr) = blk;
18584 break;
18585 case DW_FORM_data1:
18586 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18587 info_ptr += 1;
18588 break;
18589 case DW_FORM_flag:
18590 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18591 info_ptr += 1;
18592 break;
18593 case DW_FORM_flag_present:
18594 DW_UNSND (attr) = 1;
18595 break;
18596 case DW_FORM_sdata:
18597 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18598 info_ptr += bytes_read;
18599 break;
18600 case DW_FORM_udata:
18601 case DW_FORM_rnglistx:
18602 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18603 info_ptr += bytes_read;
18604 break;
18605 case DW_FORM_ref1:
18606 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18607 + read_1_byte (abfd, info_ptr));
18608 info_ptr += 1;
18609 break;
18610 case DW_FORM_ref2:
18611 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18612 + read_2_bytes (abfd, info_ptr));
18613 info_ptr += 2;
18614 break;
18615 case DW_FORM_ref4:
18616 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18617 + read_4_bytes (abfd, info_ptr));
18618 info_ptr += 4;
18619 break;
18620 case DW_FORM_ref8:
18621 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18622 + read_8_bytes (abfd, info_ptr));
18623 info_ptr += 8;
18624 break;
18625 case DW_FORM_ref_sig8:
18626 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18627 info_ptr += 8;
18628 break;
18629 case DW_FORM_ref_udata:
18630 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18631 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18632 info_ptr += bytes_read;
18633 break;
18634 case DW_FORM_indirect:
18635 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18636 info_ptr += bytes_read;
18637 if (form == DW_FORM_implicit_const)
18638 {
18639 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18640 info_ptr += bytes_read;
18641 }
18642 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18643 info_ptr, need_reprocess);
18644 break;
18645 case DW_FORM_implicit_const:
18646 DW_SND (attr) = implicit_const;
18647 break;
18648 case DW_FORM_addrx:
18649 case DW_FORM_GNU_addr_index:
18650 *need_reprocess = true;
18651 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18652 info_ptr += bytes_read;
18653 break;
18654 case DW_FORM_strx:
18655 case DW_FORM_strx1:
18656 case DW_FORM_strx2:
18657 case DW_FORM_strx3:
18658 case DW_FORM_strx4:
18659 case DW_FORM_GNU_str_index:
18660 {
18661 ULONGEST str_index;
18662 if (form == DW_FORM_strx1)
18663 {
18664 str_index = read_1_byte (abfd, info_ptr);
18665 info_ptr += 1;
18666 }
18667 else if (form == DW_FORM_strx2)
18668 {
18669 str_index = read_2_bytes (abfd, info_ptr);
18670 info_ptr += 2;
18671 }
18672 else if (form == DW_FORM_strx3)
18673 {
18674 str_index = read_3_bytes (abfd, info_ptr);
18675 info_ptr += 3;
18676 }
18677 else if (form == DW_FORM_strx4)
18678 {
18679 str_index = read_4_bytes (abfd, info_ptr);
18680 info_ptr += 4;
18681 }
18682 else
18683 {
18684 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18685 info_ptr += bytes_read;
18686 }
18687 *need_reprocess = true;
18688 DW_UNSND (attr) = str_index;
18689 }
18690 break;
18691 default:
18692 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18693 dwarf_form_name (form),
18694 bfd_get_filename (abfd));
18695 }
18696
18697 /* Super hack. */
18698 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18699 attr->form = DW_FORM_GNU_ref_alt;
18700
18701 /* We have seen instances where the compiler tried to emit a byte
18702 size attribute of -1 which ended up being encoded as an unsigned
18703 0xffffffff. Although 0xffffffff is technically a valid size value,
18704 an object of this size seems pretty unlikely so we can relatively
18705 safely treat these cases as if the size attribute was invalid and
18706 treat them as zero by default. */
18707 if (attr->name == DW_AT_byte_size
18708 && form == DW_FORM_data4
18709 && DW_UNSND (attr) >= 0xffffffff)
18710 {
18711 complaint
18712 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18713 hex_string (DW_UNSND (attr)));
18714 DW_UNSND (attr) = 0;
18715 }
18716
18717 return info_ptr;
18718 }
18719
18720 /* Read an attribute described by an abbreviated attribute. */
18721
18722 static const gdb_byte *
18723 read_attribute (const struct die_reader_specs *reader,
18724 struct attribute *attr, struct attr_abbrev *abbrev,
18725 const gdb_byte *info_ptr, bool *need_reprocess)
18726 {
18727 attr->name = abbrev->name;
18728 return read_attribute_value (reader, attr, abbrev->form,
18729 abbrev->implicit_const, info_ptr,
18730 need_reprocess);
18731 }
18732
18733 /* Cover function for read_initial_length.
18734 Returns the length of the object at BUF, and stores the size of the
18735 initial length in *BYTES_READ and stores the size that offsets will be in
18736 *OFFSET_SIZE.
18737 If the initial length size is not equivalent to that specified in
18738 CU_HEADER then issue a complaint.
18739 This is useful when reading non-comp-unit headers. */
18740
18741 static LONGEST
18742 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18743 const struct comp_unit_head *cu_header,
18744 unsigned int *bytes_read,
18745 unsigned int *offset_size)
18746 {
18747 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18748
18749 gdb_assert (cu_header->initial_length_size == 4
18750 || cu_header->initial_length_size == 8
18751 || cu_header->initial_length_size == 12);
18752
18753 if (cu_header->initial_length_size != *bytes_read)
18754 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18755
18756 *offset_size = (*bytes_read == 4) ? 4 : 8;
18757 return length;
18758 }
18759
18760 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18761
18762 static const char *
18763 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18764 LONGEST str_offset)
18765 {
18766 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18767 str_offset, "DW_FORM_strp");
18768 }
18769
18770 /* Return pointer to string at .debug_str offset as read from BUF.
18771 BUF is assumed to be in a compilation unit described by CU_HEADER.
18772 Return *BYTES_READ_PTR count of bytes read from BUF. */
18773
18774 static const char *
18775 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18776 const gdb_byte *buf,
18777 const struct comp_unit_head *cu_header,
18778 unsigned int *bytes_read_ptr)
18779 {
18780 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18781
18782 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18783 }
18784
18785 /* See read.h. */
18786
18787 const char *
18788 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18789 const struct comp_unit_head *cu_header,
18790 unsigned int *bytes_read_ptr)
18791 {
18792 bfd *abfd = objfile->obfd;
18793 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18794
18795 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18796 }
18797
18798 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18799 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18800 ADDR_SIZE is the size of addresses from the CU header. */
18801
18802 static CORE_ADDR
18803 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18804 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18805 int addr_size)
18806 {
18807 struct objfile *objfile = dwarf2_per_objfile->objfile;
18808 bfd *abfd = objfile->obfd;
18809 const gdb_byte *info_ptr;
18810 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18811
18812 dwarf2_per_objfile->addr.read (objfile);
18813 if (dwarf2_per_objfile->addr.buffer == NULL)
18814 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18815 objfile_name (objfile));
18816 if (addr_base_or_zero + addr_index * addr_size
18817 >= dwarf2_per_objfile->addr.size)
18818 error (_("DW_FORM_addr_index pointing outside of "
18819 ".debug_addr section [in module %s]"),
18820 objfile_name (objfile));
18821 info_ptr = (dwarf2_per_objfile->addr.buffer
18822 + addr_base_or_zero + addr_index * addr_size);
18823 if (addr_size == 4)
18824 return bfd_get_32 (abfd, info_ptr);
18825 else
18826 return bfd_get_64 (abfd, info_ptr);
18827 }
18828
18829 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18830
18831 static CORE_ADDR
18832 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18833 {
18834 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18835 cu->addr_base, cu->header.addr_size);
18836 }
18837
18838 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18839
18840 static CORE_ADDR
18841 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18842 unsigned int *bytes_read)
18843 {
18844 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18845 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18846
18847 return read_addr_index (cu, addr_index);
18848 }
18849
18850 /* See read.h. */
18851
18852 CORE_ADDR
18853 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18854 {
18855 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18856 struct dwarf2_cu *cu = per_cu->cu;
18857 gdb::optional<ULONGEST> addr_base;
18858 int addr_size;
18859
18860 /* We need addr_base and addr_size.
18861 If we don't have PER_CU->cu, we have to get it.
18862 Nasty, but the alternative is storing the needed info in PER_CU,
18863 which at this point doesn't seem justified: it's not clear how frequently
18864 it would get used and it would increase the size of every PER_CU.
18865 Entry points like dwarf2_per_cu_addr_size do a similar thing
18866 so we're not in uncharted territory here.
18867 Alas we need to be a bit more complicated as addr_base is contained
18868 in the DIE.
18869
18870 We don't need to read the entire CU(/TU).
18871 We just need the header and top level die.
18872
18873 IWBN to use the aging mechanism to let us lazily later discard the CU.
18874 For now we skip this optimization. */
18875
18876 if (cu != NULL)
18877 {
18878 addr_base = cu->addr_base;
18879 addr_size = cu->header.addr_size;
18880 }
18881 else
18882 {
18883 cutu_reader reader (per_cu, NULL, 0, false);
18884 addr_base = reader.cu->addr_base;
18885 addr_size = reader.cu->header.addr_size;
18886 }
18887
18888 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18889 addr_size);
18890 }
18891
18892 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18893 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18894 DWO file. */
18895
18896 static const char *
18897 read_str_index (struct dwarf2_cu *cu,
18898 struct dwarf2_section_info *str_section,
18899 struct dwarf2_section_info *str_offsets_section,
18900 ULONGEST str_offsets_base, ULONGEST str_index)
18901 {
18902 struct dwarf2_per_objfile *dwarf2_per_objfile
18903 = cu->per_cu->dwarf2_per_objfile;
18904 struct objfile *objfile = dwarf2_per_objfile->objfile;
18905 const char *objf_name = objfile_name (objfile);
18906 bfd *abfd = objfile->obfd;
18907 const gdb_byte *info_ptr;
18908 ULONGEST str_offset;
18909 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18910
18911 str_section->read (objfile);
18912 str_offsets_section->read (objfile);
18913 if (str_section->buffer == NULL)
18914 error (_("%s used without %s section"
18915 " in CU at offset %s [in module %s]"),
18916 form_name, str_section->get_name (),
18917 sect_offset_str (cu->header.sect_off), objf_name);
18918 if (str_offsets_section->buffer == NULL)
18919 error (_("%s used without %s section"
18920 " in CU at offset %s [in module %s]"),
18921 form_name, str_section->get_name (),
18922 sect_offset_str (cu->header.sect_off), objf_name);
18923 info_ptr = (str_offsets_section->buffer
18924 + str_offsets_base
18925 + str_index * cu->header.offset_size);
18926 if (cu->header.offset_size == 4)
18927 str_offset = bfd_get_32 (abfd, info_ptr);
18928 else
18929 str_offset = bfd_get_64 (abfd, info_ptr);
18930 if (str_offset >= str_section->size)
18931 error (_("Offset from %s pointing outside of"
18932 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18933 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18934 return (const char *) (str_section->buffer + str_offset);
18935 }
18936
18937 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18938
18939 static const char *
18940 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18941 {
18942 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18943 ? reader->cu->header.addr_size : 0;
18944 return read_str_index (reader->cu,
18945 &reader->dwo_file->sections.str,
18946 &reader->dwo_file->sections.str_offsets,
18947 str_offsets_base, str_index);
18948 }
18949
18950 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18951
18952 static const char *
18953 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18954 {
18955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18956 const char *objf_name = objfile_name (objfile);
18957 static const char form_name[] = "DW_FORM_GNU_str_index";
18958 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18959
18960 if (!cu->str_offsets_base.has_value ())
18961 error (_("%s used in Fission stub without %s"
18962 " in CU at offset 0x%lx [in module %s]"),
18963 form_name, str_offsets_attr_name,
18964 (long) cu->header.offset_size, objf_name);
18965
18966 return read_str_index (cu,
18967 &cu->per_cu->dwarf2_per_objfile->str,
18968 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18969 *cu->str_offsets_base, str_index);
18970 }
18971
18972 /* Return the length of an LEB128 number in BUF. */
18973
18974 static int
18975 leb128_size (const gdb_byte *buf)
18976 {
18977 const gdb_byte *begin = buf;
18978 gdb_byte byte;
18979
18980 while (1)
18981 {
18982 byte = *buf++;
18983 if ((byte & 128) == 0)
18984 return buf - begin;
18985 }
18986 }
18987
18988 static void
18989 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18990 {
18991 switch (lang)
18992 {
18993 case DW_LANG_C89:
18994 case DW_LANG_C99:
18995 case DW_LANG_C11:
18996 case DW_LANG_C:
18997 case DW_LANG_UPC:
18998 cu->language = language_c;
18999 break;
19000 case DW_LANG_Java:
19001 case DW_LANG_C_plus_plus:
19002 case DW_LANG_C_plus_plus_11:
19003 case DW_LANG_C_plus_plus_14:
19004 cu->language = language_cplus;
19005 break;
19006 case DW_LANG_D:
19007 cu->language = language_d;
19008 break;
19009 case DW_LANG_Fortran77:
19010 case DW_LANG_Fortran90:
19011 case DW_LANG_Fortran95:
19012 case DW_LANG_Fortran03:
19013 case DW_LANG_Fortran08:
19014 cu->language = language_fortran;
19015 break;
19016 case DW_LANG_Go:
19017 cu->language = language_go;
19018 break;
19019 case DW_LANG_Mips_Assembler:
19020 cu->language = language_asm;
19021 break;
19022 case DW_LANG_Ada83:
19023 case DW_LANG_Ada95:
19024 cu->language = language_ada;
19025 break;
19026 case DW_LANG_Modula2:
19027 cu->language = language_m2;
19028 break;
19029 case DW_LANG_Pascal83:
19030 cu->language = language_pascal;
19031 break;
19032 case DW_LANG_ObjC:
19033 cu->language = language_objc;
19034 break;
19035 case DW_LANG_Rust:
19036 case DW_LANG_Rust_old:
19037 cu->language = language_rust;
19038 break;
19039 case DW_LANG_Cobol74:
19040 case DW_LANG_Cobol85:
19041 default:
19042 cu->language = language_minimal;
19043 break;
19044 }
19045 cu->language_defn = language_def (cu->language);
19046 }
19047
19048 /* Return the named attribute or NULL if not there. */
19049
19050 static struct attribute *
19051 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19052 {
19053 for (;;)
19054 {
19055 unsigned int i;
19056 struct attribute *spec = NULL;
19057
19058 for (i = 0; i < die->num_attrs; ++i)
19059 {
19060 if (die->attrs[i].name == name)
19061 return &die->attrs[i];
19062 if (die->attrs[i].name == DW_AT_specification
19063 || die->attrs[i].name == DW_AT_abstract_origin)
19064 spec = &die->attrs[i];
19065 }
19066
19067 if (!spec)
19068 break;
19069
19070 die = follow_die_ref (die, spec, &cu);
19071 }
19072
19073 return NULL;
19074 }
19075
19076 /* Return the named attribute or NULL if not there,
19077 but do not follow DW_AT_specification, etc.
19078 This is for use in contexts where we're reading .debug_types dies.
19079 Following DW_AT_specification, DW_AT_abstract_origin will take us
19080 back up the chain, and we want to go down. */
19081
19082 static struct attribute *
19083 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19084 {
19085 unsigned int i;
19086
19087 for (i = 0; i < die->num_attrs; ++i)
19088 if (die->attrs[i].name == name)
19089 return &die->attrs[i];
19090
19091 return NULL;
19092 }
19093
19094 /* Return the string associated with a string-typed attribute, or NULL if it
19095 is either not found or is of an incorrect type. */
19096
19097 static const char *
19098 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19099 {
19100 struct attribute *attr;
19101 const char *str = NULL;
19102
19103 attr = dwarf2_attr (die, name, cu);
19104
19105 if (attr != NULL)
19106 {
19107 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19108 || attr->form == DW_FORM_string
19109 || attr->form == DW_FORM_strx
19110 || attr->form == DW_FORM_strx1
19111 || attr->form == DW_FORM_strx2
19112 || attr->form == DW_FORM_strx3
19113 || attr->form == DW_FORM_strx4
19114 || attr->form == DW_FORM_GNU_str_index
19115 || attr->form == DW_FORM_GNU_strp_alt)
19116 str = DW_STRING (attr);
19117 else
19118 complaint (_("string type expected for attribute %s for "
19119 "DIE at %s in module %s"),
19120 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19121 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19122 }
19123
19124 return str;
19125 }
19126
19127 /* Return the dwo name or NULL if not present. If present, it is in either
19128 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19129 static const char *
19130 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19131 {
19132 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19133 if (dwo_name == nullptr)
19134 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19135 return dwo_name;
19136 }
19137
19138 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19139 and holds a non-zero value. This function should only be used for
19140 DW_FORM_flag or DW_FORM_flag_present attributes. */
19141
19142 static int
19143 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19144 {
19145 struct attribute *attr = dwarf2_attr (die, name, cu);
19146
19147 return (attr && DW_UNSND (attr));
19148 }
19149
19150 static int
19151 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19152 {
19153 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19154 which value is non-zero. However, we have to be careful with
19155 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19156 (via dwarf2_flag_true_p) follows this attribute. So we may
19157 end up accidently finding a declaration attribute that belongs
19158 to a different DIE referenced by the specification attribute,
19159 even though the given DIE does not have a declaration attribute. */
19160 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19161 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19162 }
19163
19164 /* Return the die giving the specification for DIE, if there is
19165 one. *SPEC_CU is the CU containing DIE on input, and the CU
19166 containing the return value on output. If there is no
19167 specification, but there is an abstract origin, that is
19168 returned. */
19169
19170 static struct die_info *
19171 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19172 {
19173 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19174 *spec_cu);
19175
19176 if (spec_attr == NULL)
19177 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19178
19179 if (spec_attr == NULL)
19180 return NULL;
19181 else
19182 return follow_die_ref (die, spec_attr, spec_cu);
19183 }
19184
19185 /* Stub for free_line_header to match void * callback types. */
19186
19187 static void
19188 free_line_header_voidp (void *arg)
19189 {
19190 struct line_header *lh = (struct line_header *) arg;
19191
19192 delete lh;
19193 }
19194
19195 /* A convenience function to find the proper .debug_line section for a CU. */
19196
19197 static struct dwarf2_section_info *
19198 get_debug_line_section (struct dwarf2_cu *cu)
19199 {
19200 struct dwarf2_section_info *section;
19201 struct dwarf2_per_objfile *dwarf2_per_objfile
19202 = cu->per_cu->dwarf2_per_objfile;
19203
19204 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19205 DWO file. */
19206 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19207 section = &cu->dwo_unit->dwo_file->sections.line;
19208 else if (cu->per_cu->is_dwz)
19209 {
19210 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19211
19212 section = &dwz->line;
19213 }
19214 else
19215 section = &dwarf2_per_objfile->line;
19216
19217 return section;
19218 }
19219
19220 /* Read directory or file name entry format, starting with byte of
19221 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19222 entries count and the entries themselves in the described entry
19223 format. */
19224
19225 static void
19226 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19227 bfd *abfd, const gdb_byte **bufp,
19228 struct line_header *lh,
19229 const struct comp_unit_head *cu_header,
19230 void (*callback) (struct line_header *lh,
19231 const char *name,
19232 dir_index d_index,
19233 unsigned int mod_time,
19234 unsigned int length))
19235 {
19236 gdb_byte format_count, formati;
19237 ULONGEST data_count, datai;
19238 const gdb_byte *buf = *bufp;
19239 const gdb_byte *format_header_data;
19240 unsigned int bytes_read;
19241
19242 format_count = read_1_byte (abfd, buf);
19243 buf += 1;
19244 format_header_data = buf;
19245 for (formati = 0; formati < format_count; formati++)
19246 {
19247 read_unsigned_leb128 (abfd, buf, &bytes_read);
19248 buf += bytes_read;
19249 read_unsigned_leb128 (abfd, buf, &bytes_read);
19250 buf += bytes_read;
19251 }
19252
19253 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19254 buf += bytes_read;
19255 for (datai = 0; datai < data_count; datai++)
19256 {
19257 const gdb_byte *format = format_header_data;
19258 struct file_entry fe;
19259
19260 for (formati = 0; formati < format_count; formati++)
19261 {
19262 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19263 format += bytes_read;
19264
19265 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19266 format += bytes_read;
19267
19268 gdb::optional<const char *> string;
19269 gdb::optional<unsigned int> uint;
19270
19271 switch (form)
19272 {
19273 case DW_FORM_string:
19274 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19275 buf += bytes_read;
19276 break;
19277
19278 case DW_FORM_line_strp:
19279 string.emplace
19280 (dwarf2_per_objfile->read_line_string (buf,
19281 cu_header,
19282 &bytes_read));
19283 buf += bytes_read;
19284 break;
19285
19286 case DW_FORM_data1:
19287 uint.emplace (read_1_byte (abfd, buf));
19288 buf += 1;
19289 break;
19290
19291 case DW_FORM_data2:
19292 uint.emplace (read_2_bytes (abfd, buf));
19293 buf += 2;
19294 break;
19295
19296 case DW_FORM_data4:
19297 uint.emplace (read_4_bytes (abfd, buf));
19298 buf += 4;
19299 break;
19300
19301 case DW_FORM_data8:
19302 uint.emplace (read_8_bytes (abfd, buf));
19303 buf += 8;
19304 break;
19305
19306 case DW_FORM_data16:
19307 /* This is used for MD5, but file_entry does not record MD5s. */
19308 buf += 16;
19309 break;
19310
19311 case DW_FORM_udata:
19312 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19313 buf += bytes_read;
19314 break;
19315
19316 case DW_FORM_block:
19317 /* It is valid only for DW_LNCT_timestamp which is ignored by
19318 current GDB. */
19319 break;
19320 }
19321
19322 switch (content_type)
19323 {
19324 case DW_LNCT_path:
19325 if (string.has_value ())
19326 fe.name = *string;
19327 break;
19328 case DW_LNCT_directory_index:
19329 if (uint.has_value ())
19330 fe.d_index = (dir_index) *uint;
19331 break;
19332 case DW_LNCT_timestamp:
19333 if (uint.has_value ())
19334 fe.mod_time = *uint;
19335 break;
19336 case DW_LNCT_size:
19337 if (uint.has_value ())
19338 fe.length = *uint;
19339 break;
19340 case DW_LNCT_MD5:
19341 break;
19342 default:
19343 complaint (_("Unknown format content type %s"),
19344 pulongest (content_type));
19345 }
19346 }
19347
19348 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19349 }
19350
19351 *bufp = buf;
19352 }
19353
19354 /* Read the statement program header starting at OFFSET in
19355 .debug_line, or .debug_line.dwo. Return a pointer
19356 to a struct line_header, allocated using xmalloc.
19357 Returns NULL if there is a problem reading the header, e.g., if it
19358 has a version we don't understand.
19359
19360 NOTE: the strings in the include directory and file name tables of
19361 the returned object point into the dwarf line section buffer,
19362 and must not be freed. */
19363
19364 static line_header_up
19365 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19366 {
19367 const gdb_byte *line_ptr;
19368 unsigned int bytes_read, offset_size;
19369 int i;
19370 const char *cur_dir, *cur_file;
19371 struct dwarf2_section_info *section;
19372 bfd *abfd;
19373 struct dwarf2_per_objfile *dwarf2_per_objfile
19374 = cu->per_cu->dwarf2_per_objfile;
19375
19376 section = get_debug_line_section (cu);
19377 section->read (dwarf2_per_objfile->objfile);
19378 if (section->buffer == NULL)
19379 {
19380 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19381 complaint (_("missing .debug_line.dwo section"));
19382 else
19383 complaint (_("missing .debug_line section"));
19384 return 0;
19385 }
19386
19387 /* We can't do this until we know the section is non-empty.
19388 Only then do we know we have such a section. */
19389 abfd = section->get_bfd_owner ();
19390
19391 /* Make sure that at least there's room for the total_length field.
19392 That could be 12 bytes long, but we're just going to fudge that. */
19393 if (to_underlying (sect_off) + 4 >= section->size)
19394 {
19395 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19396 return 0;
19397 }
19398
19399 line_header_up lh (new line_header ());
19400
19401 lh->sect_off = sect_off;
19402 lh->offset_in_dwz = cu->per_cu->is_dwz;
19403
19404 line_ptr = section->buffer + to_underlying (sect_off);
19405
19406 /* Read in the header. */
19407 lh->total_length =
19408 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19409 &bytes_read, &offset_size);
19410 line_ptr += bytes_read;
19411
19412 const gdb_byte *start_here = line_ptr;
19413
19414 if (line_ptr + lh->total_length > (section->buffer + section->size))
19415 {
19416 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19417 return 0;
19418 }
19419 lh->statement_program_end = start_here + lh->total_length;
19420 lh->version = read_2_bytes (abfd, line_ptr);
19421 line_ptr += 2;
19422 if (lh->version > 5)
19423 {
19424 /* This is a version we don't understand. The format could have
19425 changed in ways we don't handle properly so just punt. */
19426 complaint (_("unsupported version in .debug_line section"));
19427 return NULL;
19428 }
19429 if (lh->version >= 5)
19430 {
19431 gdb_byte segment_selector_size;
19432
19433 /* Skip address size. */
19434 read_1_byte (abfd, line_ptr);
19435 line_ptr += 1;
19436
19437 segment_selector_size = read_1_byte (abfd, line_ptr);
19438 line_ptr += 1;
19439 if (segment_selector_size != 0)
19440 {
19441 complaint (_("unsupported segment selector size %u "
19442 "in .debug_line section"),
19443 segment_selector_size);
19444 return NULL;
19445 }
19446 }
19447 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19448 line_ptr += offset_size;
19449 lh->statement_program_start = line_ptr + lh->header_length;
19450 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19451 line_ptr += 1;
19452 if (lh->version >= 4)
19453 {
19454 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19455 line_ptr += 1;
19456 }
19457 else
19458 lh->maximum_ops_per_instruction = 1;
19459
19460 if (lh->maximum_ops_per_instruction == 0)
19461 {
19462 lh->maximum_ops_per_instruction = 1;
19463 complaint (_("invalid maximum_ops_per_instruction "
19464 "in `.debug_line' section"));
19465 }
19466
19467 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19468 line_ptr += 1;
19469 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19470 line_ptr += 1;
19471 lh->line_range = read_1_byte (abfd, line_ptr);
19472 line_ptr += 1;
19473 lh->opcode_base = read_1_byte (abfd, line_ptr);
19474 line_ptr += 1;
19475 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19476
19477 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19478 for (i = 1; i < lh->opcode_base; ++i)
19479 {
19480 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19481 line_ptr += 1;
19482 }
19483
19484 if (lh->version >= 5)
19485 {
19486 /* Read directory table. */
19487 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19488 &cu->header,
19489 [] (struct line_header *header, const char *name,
19490 dir_index d_index, unsigned int mod_time,
19491 unsigned int length)
19492 {
19493 header->add_include_dir (name);
19494 });
19495
19496 /* Read file name table. */
19497 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19498 &cu->header,
19499 [] (struct line_header *header, const char *name,
19500 dir_index d_index, unsigned int mod_time,
19501 unsigned int length)
19502 {
19503 header->add_file_name (name, d_index, mod_time, length);
19504 });
19505 }
19506 else
19507 {
19508 /* Read directory table. */
19509 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19510 {
19511 line_ptr += bytes_read;
19512 lh->add_include_dir (cur_dir);
19513 }
19514 line_ptr += bytes_read;
19515
19516 /* Read file name table. */
19517 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19518 {
19519 unsigned int mod_time, length;
19520 dir_index d_index;
19521
19522 line_ptr += bytes_read;
19523 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19524 line_ptr += bytes_read;
19525 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19526 line_ptr += bytes_read;
19527 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19528 line_ptr += bytes_read;
19529
19530 lh->add_file_name (cur_file, d_index, mod_time, length);
19531 }
19532 line_ptr += bytes_read;
19533 }
19534
19535 if (line_ptr > (section->buffer + section->size))
19536 complaint (_("line number info header doesn't "
19537 "fit in `.debug_line' section"));
19538
19539 return lh;
19540 }
19541
19542 /* Subroutine of dwarf_decode_lines to simplify it.
19543 Return the file name of the psymtab for the given file_entry.
19544 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19545 If space for the result is malloc'd, *NAME_HOLDER will be set.
19546 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19547
19548 static const char *
19549 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19550 const dwarf2_psymtab *pst,
19551 const char *comp_dir,
19552 gdb::unique_xmalloc_ptr<char> *name_holder)
19553 {
19554 const char *include_name = fe.name;
19555 const char *include_name_to_compare = include_name;
19556 const char *pst_filename;
19557 int file_is_pst;
19558
19559 const char *dir_name = fe.include_dir (lh);
19560
19561 gdb::unique_xmalloc_ptr<char> hold_compare;
19562 if (!IS_ABSOLUTE_PATH (include_name)
19563 && (dir_name != NULL || comp_dir != NULL))
19564 {
19565 /* Avoid creating a duplicate psymtab for PST.
19566 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19567 Before we do the comparison, however, we need to account
19568 for DIR_NAME and COMP_DIR.
19569 First prepend dir_name (if non-NULL). If we still don't
19570 have an absolute path prepend comp_dir (if non-NULL).
19571 However, the directory we record in the include-file's
19572 psymtab does not contain COMP_DIR (to match the
19573 corresponding symtab(s)).
19574
19575 Example:
19576
19577 bash$ cd /tmp
19578 bash$ gcc -g ./hello.c
19579 include_name = "hello.c"
19580 dir_name = "."
19581 DW_AT_comp_dir = comp_dir = "/tmp"
19582 DW_AT_name = "./hello.c"
19583
19584 */
19585
19586 if (dir_name != NULL)
19587 {
19588 name_holder->reset (concat (dir_name, SLASH_STRING,
19589 include_name, (char *) NULL));
19590 include_name = name_holder->get ();
19591 include_name_to_compare = include_name;
19592 }
19593 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19594 {
19595 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19596 include_name, (char *) NULL));
19597 include_name_to_compare = hold_compare.get ();
19598 }
19599 }
19600
19601 pst_filename = pst->filename;
19602 gdb::unique_xmalloc_ptr<char> copied_name;
19603 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19604 {
19605 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19606 pst_filename, (char *) NULL));
19607 pst_filename = copied_name.get ();
19608 }
19609
19610 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19611
19612 if (file_is_pst)
19613 return NULL;
19614 return include_name;
19615 }
19616
19617 /* State machine to track the state of the line number program. */
19618
19619 class lnp_state_machine
19620 {
19621 public:
19622 /* Initialize a machine state for the start of a line number
19623 program. */
19624 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19625 bool record_lines_p);
19626
19627 file_entry *current_file ()
19628 {
19629 /* lh->file_names is 0-based, but the file name numbers in the
19630 statement program are 1-based. */
19631 return m_line_header->file_name_at (m_file);
19632 }
19633
19634 /* Record the line in the state machine. END_SEQUENCE is true if
19635 we're processing the end of a sequence. */
19636 void record_line (bool end_sequence);
19637
19638 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19639 nop-out rest of the lines in this sequence. */
19640 void check_line_address (struct dwarf2_cu *cu,
19641 const gdb_byte *line_ptr,
19642 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19643
19644 void handle_set_discriminator (unsigned int discriminator)
19645 {
19646 m_discriminator = discriminator;
19647 m_line_has_non_zero_discriminator |= discriminator != 0;
19648 }
19649
19650 /* Handle DW_LNE_set_address. */
19651 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19652 {
19653 m_op_index = 0;
19654 address += baseaddr;
19655 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19656 }
19657
19658 /* Handle DW_LNS_advance_pc. */
19659 void handle_advance_pc (CORE_ADDR adjust);
19660
19661 /* Handle a special opcode. */
19662 void handle_special_opcode (unsigned char op_code);
19663
19664 /* Handle DW_LNS_advance_line. */
19665 void handle_advance_line (int line_delta)
19666 {
19667 advance_line (line_delta);
19668 }
19669
19670 /* Handle DW_LNS_set_file. */
19671 void handle_set_file (file_name_index file);
19672
19673 /* Handle DW_LNS_negate_stmt. */
19674 void handle_negate_stmt ()
19675 {
19676 m_is_stmt = !m_is_stmt;
19677 }
19678
19679 /* Handle DW_LNS_const_add_pc. */
19680 void handle_const_add_pc ();
19681
19682 /* Handle DW_LNS_fixed_advance_pc. */
19683 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19684 {
19685 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19686 m_op_index = 0;
19687 }
19688
19689 /* Handle DW_LNS_copy. */
19690 void handle_copy ()
19691 {
19692 record_line (false);
19693 m_discriminator = 0;
19694 }
19695
19696 /* Handle DW_LNE_end_sequence. */
19697 void handle_end_sequence ()
19698 {
19699 m_currently_recording_lines = true;
19700 }
19701
19702 private:
19703 /* Advance the line by LINE_DELTA. */
19704 void advance_line (int line_delta)
19705 {
19706 m_line += line_delta;
19707
19708 if (line_delta != 0)
19709 m_line_has_non_zero_discriminator = m_discriminator != 0;
19710 }
19711
19712 struct dwarf2_cu *m_cu;
19713
19714 gdbarch *m_gdbarch;
19715
19716 /* True if we're recording lines.
19717 Otherwise we're building partial symtabs and are just interested in
19718 finding include files mentioned by the line number program. */
19719 bool m_record_lines_p;
19720
19721 /* The line number header. */
19722 line_header *m_line_header;
19723
19724 /* These are part of the standard DWARF line number state machine,
19725 and initialized according to the DWARF spec. */
19726
19727 unsigned char m_op_index = 0;
19728 /* The line table index of the current file. */
19729 file_name_index m_file = 1;
19730 unsigned int m_line = 1;
19731
19732 /* These are initialized in the constructor. */
19733
19734 CORE_ADDR m_address;
19735 bool m_is_stmt;
19736 unsigned int m_discriminator;
19737
19738 /* Additional bits of state we need to track. */
19739
19740 /* The last file that we called dwarf2_start_subfile for.
19741 This is only used for TLLs. */
19742 unsigned int m_last_file = 0;
19743 /* The last file a line number was recorded for. */
19744 struct subfile *m_last_subfile = NULL;
19745
19746 /* When true, record the lines we decode. */
19747 bool m_currently_recording_lines = false;
19748
19749 /* The last line number that was recorded, used to coalesce
19750 consecutive entries for the same line. This can happen, for
19751 example, when discriminators are present. PR 17276. */
19752 unsigned int m_last_line = 0;
19753 bool m_line_has_non_zero_discriminator = false;
19754 };
19755
19756 void
19757 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19758 {
19759 CORE_ADDR addr_adj = (((m_op_index + adjust)
19760 / m_line_header->maximum_ops_per_instruction)
19761 * m_line_header->minimum_instruction_length);
19762 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19763 m_op_index = ((m_op_index + adjust)
19764 % m_line_header->maximum_ops_per_instruction);
19765 }
19766
19767 void
19768 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19769 {
19770 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19771 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19772 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19773 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19774 / m_line_header->maximum_ops_per_instruction)
19775 * m_line_header->minimum_instruction_length);
19776 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19777 m_op_index = ((m_op_index + adj_opcode_d)
19778 % m_line_header->maximum_ops_per_instruction);
19779
19780 int line_delta = m_line_header->line_base + adj_opcode_r;
19781 advance_line (line_delta);
19782 record_line (false);
19783 m_discriminator = 0;
19784 }
19785
19786 void
19787 lnp_state_machine::handle_set_file (file_name_index file)
19788 {
19789 m_file = file;
19790
19791 const file_entry *fe = current_file ();
19792 if (fe == NULL)
19793 dwarf2_debug_line_missing_file_complaint ();
19794 else if (m_record_lines_p)
19795 {
19796 const char *dir = fe->include_dir (m_line_header);
19797
19798 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19799 m_line_has_non_zero_discriminator = m_discriminator != 0;
19800 dwarf2_start_subfile (m_cu, fe->name, dir);
19801 }
19802 }
19803
19804 void
19805 lnp_state_machine::handle_const_add_pc ()
19806 {
19807 CORE_ADDR adjust
19808 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19809
19810 CORE_ADDR addr_adj
19811 = (((m_op_index + adjust)
19812 / m_line_header->maximum_ops_per_instruction)
19813 * m_line_header->minimum_instruction_length);
19814
19815 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19816 m_op_index = ((m_op_index + adjust)
19817 % m_line_header->maximum_ops_per_instruction);
19818 }
19819
19820 /* Return non-zero if we should add LINE to the line number table.
19821 LINE is the line to add, LAST_LINE is the last line that was added,
19822 LAST_SUBFILE is the subfile for LAST_LINE.
19823 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19824 had a non-zero discriminator.
19825
19826 We have to be careful in the presence of discriminators.
19827 E.g., for this line:
19828
19829 for (i = 0; i < 100000; i++);
19830
19831 clang can emit four line number entries for that one line,
19832 each with a different discriminator.
19833 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19834
19835 However, we want gdb to coalesce all four entries into one.
19836 Otherwise the user could stepi into the middle of the line and
19837 gdb would get confused about whether the pc really was in the
19838 middle of the line.
19839
19840 Things are further complicated by the fact that two consecutive
19841 line number entries for the same line is a heuristic used by gcc
19842 to denote the end of the prologue. So we can't just discard duplicate
19843 entries, we have to be selective about it. The heuristic we use is
19844 that we only collapse consecutive entries for the same line if at least
19845 one of those entries has a non-zero discriminator. PR 17276.
19846
19847 Note: Addresses in the line number state machine can never go backwards
19848 within one sequence, thus this coalescing is ok. */
19849
19850 static int
19851 dwarf_record_line_p (struct dwarf2_cu *cu,
19852 unsigned int line, unsigned int last_line,
19853 int line_has_non_zero_discriminator,
19854 struct subfile *last_subfile)
19855 {
19856 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19857 return 1;
19858 if (line != last_line)
19859 return 1;
19860 /* Same line for the same file that we've seen already.
19861 As a last check, for pr 17276, only record the line if the line
19862 has never had a non-zero discriminator. */
19863 if (!line_has_non_zero_discriminator)
19864 return 1;
19865 return 0;
19866 }
19867
19868 /* Use the CU's builder to record line number LINE beginning at
19869 address ADDRESS in the line table of subfile SUBFILE. */
19870
19871 static void
19872 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19873 unsigned int line, CORE_ADDR address, bool is_stmt,
19874 struct dwarf2_cu *cu)
19875 {
19876 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19877
19878 if (dwarf_line_debug)
19879 {
19880 fprintf_unfiltered (gdb_stdlog,
19881 "Recording line %u, file %s, address %s\n",
19882 line, lbasename (subfile->name),
19883 paddress (gdbarch, address));
19884 }
19885
19886 if (cu != nullptr)
19887 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19888 }
19889
19890 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19891 Mark the end of a set of line number records.
19892 The arguments are the same as for dwarf_record_line_1.
19893 If SUBFILE is NULL the request is ignored. */
19894
19895 static void
19896 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19897 CORE_ADDR address, struct dwarf2_cu *cu)
19898 {
19899 if (subfile == NULL)
19900 return;
19901
19902 if (dwarf_line_debug)
19903 {
19904 fprintf_unfiltered (gdb_stdlog,
19905 "Finishing current line, file %s, address %s\n",
19906 lbasename (subfile->name),
19907 paddress (gdbarch, address));
19908 }
19909
19910 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19911 }
19912
19913 void
19914 lnp_state_machine::record_line (bool end_sequence)
19915 {
19916 if (dwarf_line_debug)
19917 {
19918 fprintf_unfiltered (gdb_stdlog,
19919 "Processing actual line %u: file %u,"
19920 " address %s, is_stmt %u, discrim %u%s\n",
19921 m_line, m_file,
19922 paddress (m_gdbarch, m_address),
19923 m_is_stmt, m_discriminator,
19924 (end_sequence ? "\t(end sequence)" : ""));
19925 }
19926
19927 file_entry *fe = current_file ();
19928
19929 if (fe == NULL)
19930 dwarf2_debug_line_missing_file_complaint ();
19931 /* For now we ignore lines not starting on an instruction boundary.
19932 But not when processing end_sequence for compatibility with the
19933 previous version of the code. */
19934 else if (m_op_index == 0 || end_sequence)
19935 {
19936 fe->included_p = 1;
19937 if (m_record_lines_p)
19938 {
19939 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19940 || end_sequence)
19941 {
19942 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19943 m_currently_recording_lines ? m_cu : nullptr);
19944 }
19945
19946 if (!end_sequence)
19947 {
19948 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19949
19950 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19951 m_line_has_non_zero_discriminator,
19952 m_last_subfile))
19953 {
19954 buildsym_compunit *builder = m_cu->get_builder ();
19955 dwarf_record_line_1 (m_gdbarch,
19956 builder->get_current_subfile (),
19957 m_line, m_address, is_stmt,
19958 m_currently_recording_lines ? m_cu : nullptr);
19959 }
19960 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19961 m_last_line = m_line;
19962 }
19963 }
19964 }
19965 }
19966
19967 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19968 line_header *lh, bool record_lines_p)
19969 {
19970 m_cu = cu;
19971 m_gdbarch = arch;
19972 m_record_lines_p = record_lines_p;
19973 m_line_header = lh;
19974
19975 m_currently_recording_lines = true;
19976
19977 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19978 was a line entry for it so that the backend has a chance to adjust it
19979 and also record it in case it needs it. This is currently used by MIPS
19980 code, cf. `mips_adjust_dwarf2_line'. */
19981 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19982 m_is_stmt = lh->default_is_stmt;
19983 m_discriminator = 0;
19984 }
19985
19986 void
19987 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19988 const gdb_byte *line_ptr,
19989 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19990 {
19991 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19992 the pc range of the CU. However, we restrict the test to only ADDRESS
19993 values of zero to preserve GDB's previous behaviour which is to handle
19994 the specific case of a function being GC'd by the linker. */
19995
19996 if (address == 0 && address < unrelocated_lowpc)
19997 {
19998 /* This line table is for a function which has been
19999 GCd by the linker. Ignore it. PR gdb/12528 */
20000
20001 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20002 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20003
20004 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20005 line_offset, objfile_name (objfile));
20006 m_currently_recording_lines = false;
20007 /* Note: m_currently_recording_lines is left as false until we see
20008 DW_LNE_end_sequence. */
20009 }
20010 }
20011
20012 /* Subroutine of dwarf_decode_lines to simplify it.
20013 Process the line number information in LH.
20014 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20015 program in order to set included_p for every referenced header. */
20016
20017 static void
20018 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20019 const int decode_for_pst_p, CORE_ADDR lowpc)
20020 {
20021 const gdb_byte *line_ptr, *extended_end;
20022 const gdb_byte *line_end;
20023 unsigned int bytes_read, extended_len;
20024 unsigned char op_code, extended_op;
20025 CORE_ADDR baseaddr;
20026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20027 bfd *abfd = objfile->obfd;
20028 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20029 /* True if we're recording line info (as opposed to building partial
20030 symtabs and just interested in finding include files mentioned by
20031 the line number program). */
20032 bool record_lines_p = !decode_for_pst_p;
20033
20034 baseaddr = objfile->text_section_offset ();
20035
20036 line_ptr = lh->statement_program_start;
20037 line_end = lh->statement_program_end;
20038
20039 /* Read the statement sequences until there's nothing left. */
20040 while (line_ptr < line_end)
20041 {
20042 /* The DWARF line number program state machine. Reset the state
20043 machine at the start of each sequence. */
20044 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20045 bool end_sequence = false;
20046
20047 if (record_lines_p)
20048 {
20049 /* Start a subfile for the current file of the state
20050 machine. */
20051 const file_entry *fe = state_machine.current_file ();
20052
20053 if (fe != NULL)
20054 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20055 }
20056
20057 /* Decode the table. */
20058 while (line_ptr < line_end && !end_sequence)
20059 {
20060 op_code = read_1_byte (abfd, line_ptr);
20061 line_ptr += 1;
20062
20063 if (op_code >= lh->opcode_base)
20064 {
20065 /* Special opcode. */
20066 state_machine.handle_special_opcode (op_code);
20067 }
20068 else switch (op_code)
20069 {
20070 case DW_LNS_extended_op:
20071 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20072 &bytes_read);
20073 line_ptr += bytes_read;
20074 extended_end = line_ptr + extended_len;
20075 extended_op = read_1_byte (abfd, line_ptr);
20076 line_ptr += 1;
20077 switch (extended_op)
20078 {
20079 case DW_LNE_end_sequence:
20080 state_machine.handle_end_sequence ();
20081 end_sequence = true;
20082 break;
20083 case DW_LNE_set_address:
20084 {
20085 CORE_ADDR address
20086 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20087 line_ptr += bytes_read;
20088
20089 state_machine.check_line_address (cu, line_ptr,
20090 lowpc - baseaddr, address);
20091 state_machine.handle_set_address (baseaddr, address);
20092 }
20093 break;
20094 case DW_LNE_define_file:
20095 {
20096 const char *cur_file;
20097 unsigned int mod_time, length;
20098 dir_index dindex;
20099
20100 cur_file = read_direct_string (abfd, line_ptr,
20101 &bytes_read);
20102 line_ptr += bytes_read;
20103 dindex = (dir_index)
20104 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20105 line_ptr += bytes_read;
20106 mod_time =
20107 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20108 line_ptr += bytes_read;
20109 length =
20110 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20111 line_ptr += bytes_read;
20112 lh->add_file_name (cur_file, dindex, mod_time, length);
20113 }
20114 break;
20115 case DW_LNE_set_discriminator:
20116 {
20117 /* The discriminator is not interesting to the
20118 debugger; just ignore it. We still need to
20119 check its value though:
20120 if there are consecutive entries for the same
20121 (non-prologue) line we want to coalesce them.
20122 PR 17276. */
20123 unsigned int discr
20124 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20125 line_ptr += bytes_read;
20126
20127 state_machine.handle_set_discriminator (discr);
20128 }
20129 break;
20130 default:
20131 complaint (_("mangled .debug_line section"));
20132 return;
20133 }
20134 /* Make sure that we parsed the extended op correctly. If e.g.
20135 we expected a different address size than the producer used,
20136 we may have read the wrong number of bytes. */
20137 if (line_ptr != extended_end)
20138 {
20139 complaint (_("mangled .debug_line section"));
20140 return;
20141 }
20142 break;
20143 case DW_LNS_copy:
20144 state_machine.handle_copy ();
20145 break;
20146 case DW_LNS_advance_pc:
20147 {
20148 CORE_ADDR adjust
20149 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20150 line_ptr += bytes_read;
20151
20152 state_machine.handle_advance_pc (adjust);
20153 }
20154 break;
20155 case DW_LNS_advance_line:
20156 {
20157 int line_delta
20158 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20159 line_ptr += bytes_read;
20160
20161 state_machine.handle_advance_line (line_delta);
20162 }
20163 break;
20164 case DW_LNS_set_file:
20165 {
20166 file_name_index file
20167 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20168 &bytes_read);
20169 line_ptr += bytes_read;
20170
20171 state_machine.handle_set_file (file);
20172 }
20173 break;
20174 case DW_LNS_set_column:
20175 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20176 line_ptr += bytes_read;
20177 break;
20178 case DW_LNS_negate_stmt:
20179 state_machine.handle_negate_stmt ();
20180 break;
20181 case DW_LNS_set_basic_block:
20182 break;
20183 /* Add to the address register of the state machine the
20184 address increment value corresponding to special opcode
20185 255. I.e., this value is scaled by the minimum
20186 instruction length since special opcode 255 would have
20187 scaled the increment. */
20188 case DW_LNS_const_add_pc:
20189 state_machine.handle_const_add_pc ();
20190 break;
20191 case DW_LNS_fixed_advance_pc:
20192 {
20193 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20194 line_ptr += 2;
20195
20196 state_machine.handle_fixed_advance_pc (addr_adj);
20197 }
20198 break;
20199 default:
20200 {
20201 /* Unknown standard opcode, ignore it. */
20202 int i;
20203
20204 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20205 {
20206 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20207 line_ptr += bytes_read;
20208 }
20209 }
20210 }
20211 }
20212
20213 if (!end_sequence)
20214 dwarf2_debug_line_missing_end_sequence_complaint ();
20215
20216 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20217 in which case we still finish recording the last line). */
20218 state_machine.record_line (true);
20219 }
20220 }
20221
20222 /* Decode the Line Number Program (LNP) for the given line_header
20223 structure and CU. The actual information extracted and the type
20224 of structures created from the LNP depends on the value of PST.
20225
20226 1. If PST is NULL, then this procedure uses the data from the program
20227 to create all necessary symbol tables, and their linetables.
20228
20229 2. If PST is not NULL, this procedure reads the program to determine
20230 the list of files included by the unit represented by PST, and
20231 builds all the associated partial symbol tables.
20232
20233 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20234 It is used for relative paths in the line table.
20235 NOTE: When processing partial symtabs (pst != NULL),
20236 comp_dir == pst->dirname.
20237
20238 NOTE: It is important that psymtabs have the same file name (via strcmp)
20239 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20240 symtab we don't use it in the name of the psymtabs we create.
20241 E.g. expand_line_sal requires this when finding psymtabs to expand.
20242 A good testcase for this is mb-inline.exp.
20243
20244 LOWPC is the lowest address in CU (or 0 if not known).
20245
20246 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20247 for its PC<->lines mapping information. Otherwise only the filename
20248 table is read in. */
20249
20250 static void
20251 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20252 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20253 CORE_ADDR lowpc, int decode_mapping)
20254 {
20255 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20256 const int decode_for_pst_p = (pst != NULL);
20257
20258 if (decode_mapping)
20259 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20260
20261 if (decode_for_pst_p)
20262 {
20263 /* Now that we're done scanning the Line Header Program, we can
20264 create the psymtab of each included file. */
20265 for (auto &file_entry : lh->file_names ())
20266 if (file_entry.included_p == 1)
20267 {
20268 gdb::unique_xmalloc_ptr<char> name_holder;
20269 const char *include_name =
20270 psymtab_include_file_name (lh, file_entry, pst,
20271 comp_dir, &name_holder);
20272 if (include_name != NULL)
20273 dwarf2_create_include_psymtab (include_name, pst, objfile);
20274 }
20275 }
20276 else
20277 {
20278 /* Make sure a symtab is created for every file, even files
20279 which contain only variables (i.e. no code with associated
20280 line numbers). */
20281 buildsym_compunit *builder = cu->get_builder ();
20282 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20283
20284 for (auto &fe : lh->file_names ())
20285 {
20286 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20287 if (builder->get_current_subfile ()->symtab == NULL)
20288 {
20289 builder->get_current_subfile ()->symtab
20290 = allocate_symtab (cust,
20291 builder->get_current_subfile ()->name);
20292 }
20293 fe.symtab = builder->get_current_subfile ()->symtab;
20294 }
20295 }
20296 }
20297
20298 /* Start a subfile for DWARF. FILENAME is the name of the file and
20299 DIRNAME the name of the source directory which contains FILENAME
20300 or NULL if not known.
20301 This routine tries to keep line numbers from identical absolute and
20302 relative file names in a common subfile.
20303
20304 Using the `list' example from the GDB testsuite, which resides in
20305 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20306 of /srcdir/list0.c yields the following debugging information for list0.c:
20307
20308 DW_AT_name: /srcdir/list0.c
20309 DW_AT_comp_dir: /compdir
20310 files.files[0].name: list0.h
20311 files.files[0].dir: /srcdir
20312 files.files[1].name: list0.c
20313 files.files[1].dir: /srcdir
20314
20315 The line number information for list0.c has to end up in a single
20316 subfile, so that `break /srcdir/list0.c:1' works as expected.
20317 start_subfile will ensure that this happens provided that we pass the
20318 concatenation of files.files[1].dir and files.files[1].name as the
20319 subfile's name. */
20320
20321 static void
20322 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20323 const char *dirname)
20324 {
20325 gdb::unique_xmalloc_ptr<char> copy;
20326
20327 /* In order not to lose the line information directory,
20328 we concatenate it to the filename when it makes sense.
20329 Note that the Dwarf3 standard says (speaking of filenames in line
20330 information): ``The directory index is ignored for file names
20331 that represent full path names''. Thus ignoring dirname in the
20332 `else' branch below isn't an issue. */
20333
20334 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20335 {
20336 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20337 filename = copy.get ();
20338 }
20339
20340 cu->get_builder ()->start_subfile (filename);
20341 }
20342
20343 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20344 buildsym_compunit constructor. */
20345
20346 struct compunit_symtab *
20347 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20348 CORE_ADDR low_pc)
20349 {
20350 gdb_assert (m_builder == nullptr);
20351
20352 m_builder.reset (new struct buildsym_compunit
20353 (per_cu->dwarf2_per_objfile->objfile,
20354 name, comp_dir, language, low_pc));
20355
20356 list_in_scope = get_builder ()->get_file_symbols ();
20357
20358 get_builder ()->record_debugformat ("DWARF 2");
20359 get_builder ()->record_producer (producer);
20360
20361 processing_has_namespace_info = false;
20362
20363 return get_builder ()->get_compunit_symtab ();
20364 }
20365
20366 static void
20367 var_decode_location (struct attribute *attr, struct symbol *sym,
20368 struct dwarf2_cu *cu)
20369 {
20370 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20371 struct comp_unit_head *cu_header = &cu->header;
20372
20373 /* NOTE drow/2003-01-30: There used to be a comment and some special
20374 code here to turn a symbol with DW_AT_external and a
20375 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20376 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20377 with some versions of binutils) where shared libraries could have
20378 relocations against symbols in their debug information - the
20379 minimal symbol would have the right address, but the debug info
20380 would not. It's no longer necessary, because we will explicitly
20381 apply relocations when we read in the debug information now. */
20382
20383 /* A DW_AT_location attribute with no contents indicates that a
20384 variable has been optimized away. */
20385 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20386 {
20387 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20388 return;
20389 }
20390
20391 /* Handle one degenerate form of location expression specially, to
20392 preserve GDB's previous behavior when section offsets are
20393 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20394 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20395
20396 if (attr->form_is_block ()
20397 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20398 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20399 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20400 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20401 && (DW_BLOCK (attr)->size
20402 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20403 {
20404 unsigned int dummy;
20405
20406 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20407 SET_SYMBOL_VALUE_ADDRESS
20408 (sym, cu->header.read_address (objfile->obfd,
20409 DW_BLOCK (attr)->data + 1,
20410 &dummy));
20411 else
20412 SET_SYMBOL_VALUE_ADDRESS
20413 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20414 &dummy));
20415 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20416 fixup_symbol_section (sym, objfile);
20417 SET_SYMBOL_VALUE_ADDRESS
20418 (sym,
20419 SYMBOL_VALUE_ADDRESS (sym)
20420 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20421 return;
20422 }
20423
20424 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20425 expression evaluator, and use LOC_COMPUTED only when necessary
20426 (i.e. when the value of a register or memory location is
20427 referenced, or a thread-local block, etc.). Then again, it might
20428 not be worthwhile. I'm assuming that it isn't unless performance
20429 or memory numbers show me otherwise. */
20430
20431 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20432
20433 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20434 cu->has_loclist = true;
20435 }
20436
20437 /* Given a pointer to a DWARF information entry, figure out if we need
20438 to make a symbol table entry for it, and if so, create a new entry
20439 and return a pointer to it.
20440 If TYPE is NULL, determine symbol type from the die, otherwise
20441 used the passed type.
20442 If SPACE is not NULL, use it to hold the new symbol. If it is
20443 NULL, allocate a new symbol on the objfile's obstack. */
20444
20445 static struct symbol *
20446 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20447 struct symbol *space)
20448 {
20449 struct dwarf2_per_objfile *dwarf2_per_objfile
20450 = cu->per_cu->dwarf2_per_objfile;
20451 struct objfile *objfile = dwarf2_per_objfile->objfile;
20452 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20453 struct symbol *sym = NULL;
20454 const char *name;
20455 struct attribute *attr = NULL;
20456 struct attribute *attr2 = NULL;
20457 CORE_ADDR baseaddr;
20458 struct pending **list_to_add = NULL;
20459
20460 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20461
20462 baseaddr = objfile->text_section_offset ();
20463
20464 name = dwarf2_name (die, cu);
20465 if (name)
20466 {
20467 const char *linkagename;
20468 int suppress_add = 0;
20469
20470 if (space)
20471 sym = space;
20472 else
20473 sym = allocate_symbol (objfile);
20474 OBJSTAT (objfile, n_syms++);
20475
20476 /* Cache this symbol's name and the name's demangled form (if any). */
20477 sym->set_language (cu->language, &objfile->objfile_obstack);
20478 linkagename = dwarf2_physname (name, die, cu);
20479 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20480
20481 /* Fortran does not have mangling standard and the mangling does differ
20482 between gfortran, iFort etc. */
20483 if (cu->language == language_fortran
20484 && symbol_get_demangled_name (sym) == NULL)
20485 symbol_set_demangled_name (sym,
20486 dwarf2_full_name (name, die, cu),
20487 NULL);
20488
20489 /* Default assumptions.
20490 Use the passed type or decode it from the die. */
20491 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20492 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20493 if (type != NULL)
20494 SYMBOL_TYPE (sym) = type;
20495 else
20496 SYMBOL_TYPE (sym) = die_type (die, cu);
20497 attr = dwarf2_attr (die,
20498 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20499 cu);
20500 if (attr != nullptr)
20501 {
20502 SYMBOL_LINE (sym) = DW_UNSND (attr);
20503 }
20504
20505 attr = dwarf2_attr (die,
20506 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20507 cu);
20508 if (attr != nullptr)
20509 {
20510 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20511 struct file_entry *fe;
20512
20513 if (cu->line_header != NULL)
20514 fe = cu->line_header->file_name_at (file_index);
20515 else
20516 fe = NULL;
20517
20518 if (fe == NULL)
20519 complaint (_("file index out of range"));
20520 else
20521 symbol_set_symtab (sym, fe->symtab);
20522 }
20523
20524 switch (die->tag)
20525 {
20526 case DW_TAG_label:
20527 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20528 if (attr != nullptr)
20529 {
20530 CORE_ADDR addr;
20531
20532 addr = attr->value_as_address ();
20533 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20534 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20535 }
20536 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20537 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20538 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20539 add_symbol_to_list (sym, cu->list_in_scope);
20540 break;
20541 case DW_TAG_subprogram:
20542 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20543 finish_block. */
20544 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20545 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20546 if ((attr2 && (DW_UNSND (attr2) != 0))
20547 || cu->language == language_ada
20548 || cu->language == language_fortran)
20549 {
20550 /* Subprograms marked external are stored as a global symbol.
20551 Ada and Fortran subprograms, whether marked external or
20552 not, are always stored as a global symbol, because we want
20553 to be able to access them globally. For instance, we want
20554 to be able to break on a nested subprogram without having
20555 to specify the context. */
20556 list_to_add = cu->get_builder ()->get_global_symbols ();
20557 }
20558 else
20559 {
20560 list_to_add = cu->list_in_scope;
20561 }
20562 break;
20563 case DW_TAG_inlined_subroutine:
20564 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20565 finish_block. */
20566 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20567 SYMBOL_INLINED (sym) = 1;
20568 list_to_add = cu->list_in_scope;
20569 break;
20570 case DW_TAG_template_value_param:
20571 suppress_add = 1;
20572 /* Fall through. */
20573 case DW_TAG_constant:
20574 case DW_TAG_variable:
20575 case DW_TAG_member:
20576 /* Compilation with minimal debug info may result in
20577 variables with missing type entries. Change the
20578 misleading `void' type to something sensible. */
20579 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20580 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20581
20582 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20583 /* In the case of DW_TAG_member, we should only be called for
20584 static const members. */
20585 if (die->tag == DW_TAG_member)
20586 {
20587 /* dwarf2_add_field uses die_is_declaration,
20588 so we do the same. */
20589 gdb_assert (die_is_declaration (die, cu));
20590 gdb_assert (attr);
20591 }
20592 if (attr != nullptr)
20593 {
20594 dwarf2_const_value (attr, sym, cu);
20595 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20596 if (!suppress_add)
20597 {
20598 if (attr2 && (DW_UNSND (attr2) != 0))
20599 list_to_add = cu->get_builder ()->get_global_symbols ();
20600 else
20601 list_to_add = cu->list_in_scope;
20602 }
20603 break;
20604 }
20605 attr = dwarf2_attr (die, DW_AT_location, cu);
20606 if (attr != nullptr)
20607 {
20608 var_decode_location (attr, sym, cu);
20609 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20610
20611 /* Fortran explicitly imports any global symbols to the local
20612 scope by DW_TAG_common_block. */
20613 if (cu->language == language_fortran && die->parent
20614 && die->parent->tag == DW_TAG_common_block)
20615 attr2 = NULL;
20616
20617 if (SYMBOL_CLASS (sym) == LOC_STATIC
20618 && SYMBOL_VALUE_ADDRESS (sym) == 0
20619 && !dwarf2_per_objfile->has_section_at_zero)
20620 {
20621 /* When a static variable is eliminated by the linker,
20622 the corresponding debug information is not stripped
20623 out, but the variable address is set to null;
20624 do not add such variables into symbol table. */
20625 }
20626 else if (attr2 && (DW_UNSND (attr2) != 0))
20627 {
20628 if (SYMBOL_CLASS (sym) == LOC_STATIC
20629 && (objfile->flags & OBJF_MAINLINE) == 0
20630 && dwarf2_per_objfile->can_copy)
20631 {
20632 /* A global static variable might be subject to
20633 copy relocation. We first check for a local
20634 minsym, though, because maybe the symbol was
20635 marked hidden, in which case this would not
20636 apply. */
20637 bound_minimal_symbol found
20638 = (lookup_minimal_symbol_linkage
20639 (sym->linkage_name (), objfile));
20640 if (found.minsym != nullptr)
20641 sym->maybe_copied = 1;
20642 }
20643
20644 /* A variable with DW_AT_external is never static,
20645 but it may be block-scoped. */
20646 list_to_add
20647 = ((cu->list_in_scope
20648 == cu->get_builder ()->get_file_symbols ())
20649 ? cu->get_builder ()->get_global_symbols ()
20650 : cu->list_in_scope);
20651 }
20652 else
20653 list_to_add = cu->list_in_scope;
20654 }
20655 else
20656 {
20657 /* We do not know the address of this symbol.
20658 If it is an external symbol and we have type information
20659 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20660 The address of the variable will then be determined from
20661 the minimal symbol table whenever the variable is
20662 referenced. */
20663 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20664
20665 /* Fortran explicitly imports any global symbols to the local
20666 scope by DW_TAG_common_block. */
20667 if (cu->language == language_fortran && die->parent
20668 && die->parent->tag == DW_TAG_common_block)
20669 {
20670 /* SYMBOL_CLASS doesn't matter here because
20671 read_common_block is going to reset it. */
20672 if (!suppress_add)
20673 list_to_add = cu->list_in_scope;
20674 }
20675 else if (attr2 && (DW_UNSND (attr2) != 0)
20676 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20677 {
20678 /* A variable with DW_AT_external is never static, but it
20679 may be block-scoped. */
20680 list_to_add
20681 = ((cu->list_in_scope
20682 == cu->get_builder ()->get_file_symbols ())
20683 ? cu->get_builder ()->get_global_symbols ()
20684 : cu->list_in_scope);
20685
20686 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20687 }
20688 else if (!die_is_declaration (die, cu))
20689 {
20690 /* Use the default LOC_OPTIMIZED_OUT class. */
20691 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20692 if (!suppress_add)
20693 list_to_add = cu->list_in_scope;
20694 }
20695 }
20696 break;
20697 case DW_TAG_formal_parameter:
20698 {
20699 /* If we are inside a function, mark this as an argument. If
20700 not, we might be looking at an argument to an inlined function
20701 when we do not have enough information to show inlined frames;
20702 pretend it's a local variable in that case so that the user can
20703 still see it. */
20704 struct context_stack *curr
20705 = cu->get_builder ()->get_current_context_stack ();
20706 if (curr != nullptr && curr->name != nullptr)
20707 SYMBOL_IS_ARGUMENT (sym) = 1;
20708 attr = dwarf2_attr (die, DW_AT_location, cu);
20709 if (attr != nullptr)
20710 {
20711 var_decode_location (attr, sym, cu);
20712 }
20713 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20714 if (attr != nullptr)
20715 {
20716 dwarf2_const_value (attr, sym, cu);
20717 }
20718
20719 list_to_add = cu->list_in_scope;
20720 }
20721 break;
20722 case DW_TAG_unspecified_parameters:
20723 /* From varargs functions; gdb doesn't seem to have any
20724 interest in this information, so just ignore it for now.
20725 (FIXME?) */
20726 break;
20727 case DW_TAG_template_type_param:
20728 suppress_add = 1;
20729 /* Fall through. */
20730 case DW_TAG_class_type:
20731 case DW_TAG_interface_type:
20732 case DW_TAG_structure_type:
20733 case DW_TAG_union_type:
20734 case DW_TAG_set_type:
20735 case DW_TAG_enumeration_type:
20736 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20737 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20738
20739 {
20740 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20741 really ever be static objects: otherwise, if you try
20742 to, say, break of a class's method and you're in a file
20743 which doesn't mention that class, it won't work unless
20744 the check for all static symbols in lookup_symbol_aux
20745 saves you. See the OtherFileClass tests in
20746 gdb.c++/namespace.exp. */
20747
20748 if (!suppress_add)
20749 {
20750 buildsym_compunit *builder = cu->get_builder ();
20751 list_to_add
20752 = (cu->list_in_scope == builder->get_file_symbols ()
20753 && cu->language == language_cplus
20754 ? builder->get_global_symbols ()
20755 : cu->list_in_scope);
20756
20757 /* The semantics of C++ state that "struct foo {
20758 ... }" also defines a typedef for "foo". */
20759 if (cu->language == language_cplus
20760 || cu->language == language_ada
20761 || cu->language == language_d
20762 || cu->language == language_rust)
20763 {
20764 /* The symbol's name is already allocated along
20765 with this objfile, so we don't need to
20766 duplicate it for the type. */
20767 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20768 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20769 }
20770 }
20771 }
20772 break;
20773 case DW_TAG_typedef:
20774 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20775 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20776 list_to_add = cu->list_in_scope;
20777 break;
20778 case DW_TAG_base_type:
20779 case DW_TAG_subrange_type:
20780 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20781 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20782 list_to_add = cu->list_in_scope;
20783 break;
20784 case DW_TAG_enumerator:
20785 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20786 if (attr != nullptr)
20787 {
20788 dwarf2_const_value (attr, sym, cu);
20789 }
20790 {
20791 /* NOTE: carlton/2003-11-10: See comment above in the
20792 DW_TAG_class_type, etc. block. */
20793
20794 list_to_add
20795 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20796 && cu->language == language_cplus
20797 ? cu->get_builder ()->get_global_symbols ()
20798 : cu->list_in_scope);
20799 }
20800 break;
20801 case DW_TAG_imported_declaration:
20802 case DW_TAG_namespace:
20803 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20804 list_to_add = cu->get_builder ()->get_global_symbols ();
20805 break;
20806 case DW_TAG_module:
20807 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20808 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20809 list_to_add = cu->get_builder ()->get_global_symbols ();
20810 break;
20811 case DW_TAG_common_block:
20812 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20813 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20814 add_symbol_to_list (sym, cu->list_in_scope);
20815 break;
20816 default:
20817 /* Not a tag we recognize. Hopefully we aren't processing
20818 trash data, but since we must specifically ignore things
20819 we don't recognize, there is nothing else we should do at
20820 this point. */
20821 complaint (_("unsupported tag: '%s'"),
20822 dwarf_tag_name (die->tag));
20823 break;
20824 }
20825
20826 if (suppress_add)
20827 {
20828 sym->hash_next = objfile->template_symbols;
20829 objfile->template_symbols = sym;
20830 list_to_add = NULL;
20831 }
20832
20833 if (list_to_add != NULL)
20834 add_symbol_to_list (sym, list_to_add);
20835
20836 /* For the benefit of old versions of GCC, check for anonymous
20837 namespaces based on the demangled name. */
20838 if (!cu->processing_has_namespace_info
20839 && cu->language == language_cplus)
20840 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20841 }
20842 return (sym);
20843 }
20844
20845 /* Given an attr with a DW_FORM_dataN value in host byte order,
20846 zero-extend it as appropriate for the symbol's type. The DWARF
20847 standard (v4) is not entirely clear about the meaning of using
20848 DW_FORM_dataN for a constant with a signed type, where the type is
20849 wider than the data. The conclusion of a discussion on the DWARF
20850 list was that this is unspecified. We choose to always zero-extend
20851 because that is the interpretation long in use by GCC. */
20852
20853 static gdb_byte *
20854 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20855 struct dwarf2_cu *cu, LONGEST *value, int bits)
20856 {
20857 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20858 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20859 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20860 LONGEST l = DW_UNSND (attr);
20861
20862 if (bits < sizeof (*value) * 8)
20863 {
20864 l &= ((LONGEST) 1 << bits) - 1;
20865 *value = l;
20866 }
20867 else if (bits == sizeof (*value) * 8)
20868 *value = l;
20869 else
20870 {
20871 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20872 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20873 return bytes;
20874 }
20875
20876 return NULL;
20877 }
20878
20879 /* Read a constant value from an attribute. Either set *VALUE, or if
20880 the value does not fit in *VALUE, set *BYTES - either already
20881 allocated on the objfile obstack, or newly allocated on OBSTACK,
20882 or, set *BATON, if we translated the constant to a location
20883 expression. */
20884
20885 static void
20886 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20887 const char *name, struct obstack *obstack,
20888 struct dwarf2_cu *cu,
20889 LONGEST *value, const gdb_byte **bytes,
20890 struct dwarf2_locexpr_baton **baton)
20891 {
20892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20893 struct comp_unit_head *cu_header = &cu->header;
20894 struct dwarf_block *blk;
20895 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20896 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20897
20898 *value = 0;
20899 *bytes = NULL;
20900 *baton = NULL;
20901
20902 switch (attr->form)
20903 {
20904 case DW_FORM_addr:
20905 case DW_FORM_addrx:
20906 case DW_FORM_GNU_addr_index:
20907 {
20908 gdb_byte *data;
20909
20910 if (TYPE_LENGTH (type) != cu_header->addr_size)
20911 dwarf2_const_value_length_mismatch_complaint (name,
20912 cu_header->addr_size,
20913 TYPE_LENGTH (type));
20914 /* Symbols of this form are reasonably rare, so we just
20915 piggyback on the existing location code rather than writing
20916 a new implementation of symbol_computed_ops. */
20917 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20918 (*baton)->per_cu = cu->per_cu;
20919 gdb_assert ((*baton)->per_cu);
20920
20921 (*baton)->size = 2 + cu_header->addr_size;
20922 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20923 (*baton)->data = data;
20924
20925 data[0] = DW_OP_addr;
20926 store_unsigned_integer (&data[1], cu_header->addr_size,
20927 byte_order, DW_ADDR (attr));
20928 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20929 }
20930 break;
20931 case DW_FORM_string:
20932 case DW_FORM_strp:
20933 case DW_FORM_strx:
20934 case DW_FORM_GNU_str_index:
20935 case DW_FORM_GNU_strp_alt:
20936 /* DW_STRING is already allocated on the objfile obstack, point
20937 directly to it. */
20938 *bytes = (const gdb_byte *) DW_STRING (attr);
20939 break;
20940 case DW_FORM_block1:
20941 case DW_FORM_block2:
20942 case DW_FORM_block4:
20943 case DW_FORM_block:
20944 case DW_FORM_exprloc:
20945 case DW_FORM_data16:
20946 blk = DW_BLOCK (attr);
20947 if (TYPE_LENGTH (type) != blk->size)
20948 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20949 TYPE_LENGTH (type));
20950 *bytes = blk->data;
20951 break;
20952
20953 /* The DW_AT_const_value attributes are supposed to carry the
20954 symbol's value "represented as it would be on the target
20955 architecture." By the time we get here, it's already been
20956 converted to host endianness, so we just need to sign- or
20957 zero-extend it as appropriate. */
20958 case DW_FORM_data1:
20959 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20960 break;
20961 case DW_FORM_data2:
20962 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20963 break;
20964 case DW_FORM_data4:
20965 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20966 break;
20967 case DW_FORM_data8:
20968 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20969 break;
20970
20971 case DW_FORM_sdata:
20972 case DW_FORM_implicit_const:
20973 *value = DW_SND (attr);
20974 break;
20975
20976 case DW_FORM_udata:
20977 *value = DW_UNSND (attr);
20978 break;
20979
20980 default:
20981 complaint (_("unsupported const value attribute form: '%s'"),
20982 dwarf_form_name (attr->form));
20983 *value = 0;
20984 break;
20985 }
20986 }
20987
20988
20989 /* Copy constant value from an attribute to a symbol. */
20990
20991 static void
20992 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20993 struct dwarf2_cu *cu)
20994 {
20995 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20996 LONGEST value;
20997 const gdb_byte *bytes;
20998 struct dwarf2_locexpr_baton *baton;
20999
21000 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21001 sym->print_name (),
21002 &objfile->objfile_obstack, cu,
21003 &value, &bytes, &baton);
21004
21005 if (baton != NULL)
21006 {
21007 SYMBOL_LOCATION_BATON (sym) = baton;
21008 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21009 }
21010 else if (bytes != NULL)
21011 {
21012 SYMBOL_VALUE_BYTES (sym) = bytes;
21013 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21014 }
21015 else
21016 {
21017 SYMBOL_VALUE (sym) = value;
21018 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21019 }
21020 }
21021
21022 /* Return the type of the die in question using its DW_AT_type attribute. */
21023
21024 static struct type *
21025 die_type (struct die_info *die, struct dwarf2_cu *cu)
21026 {
21027 struct attribute *type_attr;
21028
21029 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21030 if (!type_attr)
21031 {
21032 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21033 /* A missing DW_AT_type represents a void type. */
21034 return objfile_type (objfile)->builtin_void;
21035 }
21036
21037 return lookup_die_type (die, type_attr, cu);
21038 }
21039
21040 /* True iff CU's producer generates GNAT Ada auxiliary information
21041 that allows to find parallel types through that information instead
21042 of having to do expensive parallel lookups by type name. */
21043
21044 static int
21045 need_gnat_info (struct dwarf2_cu *cu)
21046 {
21047 /* Assume that the Ada compiler was GNAT, which always produces
21048 the auxiliary information. */
21049 return (cu->language == language_ada);
21050 }
21051
21052 /* Return the auxiliary type of the die in question using its
21053 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21054 attribute is not present. */
21055
21056 static struct type *
21057 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21058 {
21059 struct attribute *type_attr;
21060
21061 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21062 if (!type_attr)
21063 return NULL;
21064
21065 return lookup_die_type (die, type_attr, cu);
21066 }
21067
21068 /* If DIE has a descriptive_type attribute, then set the TYPE's
21069 descriptive type accordingly. */
21070
21071 static void
21072 set_descriptive_type (struct type *type, struct die_info *die,
21073 struct dwarf2_cu *cu)
21074 {
21075 struct type *descriptive_type = die_descriptive_type (die, cu);
21076
21077 if (descriptive_type)
21078 {
21079 ALLOCATE_GNAT_AUX_TYPE (type);
21080 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21081 }
21082 }
21083
21084 /* Return the containing type of the die in question using its
21085 DW_AT_containing_type attribute. */
21086
21087 static struct type *
21088 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21089 {
21090 struct attribute *type_attr;
21091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21092
21093 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21094 if (!type_attr)
21095 error (_("Dwarf Error: Problem turning containing type into gdb type "
21096 "[in module %s]"), objfile_name (objfile));
21097
21098 return lookup_die_type (die, type_attr, cu);
21099 }
21100
21101 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21102
21103 static struct type *
21104 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21105 {
21106 struct dwarf2_per_objfile *dwarf2_per_objfile
21107 = cu->per_cu->dwarf2_per_objfile;
21108 struct objfile *objfile = dwarf2_per_objfile->objfile;
21109 char *saved;
21110
21111 std::string message
21112 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21113 objfile_name (objfile),
21114 sect_offset_str (cu->header.sect_off),
21115 sect_offset_str (die->sect_off));
21116 saved = obstack_strdup (&objfile->objfile_obstack, message);
21117
21118 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21119 }
21120
21121 /* Look up the type of DIE in CU using its type attribute ATTR.
21122 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21123 DW_AT_containing_type.
21124 If there is no type substitute an error marker. */
21125
21126 static struct type *
21127 lookup_die_type (struct die_info *die, const struct attribute *attr,
21128 struct dwarf2_cu *cu)
21129 {
21130 struct dwarf2_per_objfile *dwarf2_per_objfile
21131 = cu->per_cu->dwarf2_per_objfile;
21132 struct objfile *objfile = dwarf2_per_objfile->objfile;
21133 struct type *this_type;
21134
21135 gdb_assert (attr->name == DW_AT_type
21136 || attr->name == DW_AT_GNAT_descriptive_type
21137 || attr->name == DW_AT_containing_type);
21138
21139 /* First see if we have it cached. */
21140
21141 if (attr->form == DW_FORM_GNU_ref_alt)
21142 {
21143 struct dwarf2_per_cu_data *per_cu;
21144 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21145
21146 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21147 dwarf2_per_objfile);
21148 this_type = get_die_type_at_offset (sect_off, per_cu);
21149 }
21150 else if (attr->form_is_ref ())
21151 {
21152 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21153
21154 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21155 }
21156 else if (attr->form == DW_FORM_ref_sig8)
21157 {
21158 ULONGEST signature = DW_SIGNATURE (attr);
21159
21160 return get_signatured_type (die, signature, cu);
21161 }
21162 else
21163 {
21164 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21165 " at %s [in module %s]"),
21166 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21167 objfile_name (objfile));
21168 return build_error_marker_type (cu, die);
21169 }
21170
21171 /* If not cached we need to read it in. */
21172
21173 if (this_type == NULL)
21174 {
21175 struct die_info *type_die = NULL;
21176 struct dwarf2_cu *type_cu = cu;
21177
21178 if (attr->form_is_ref ())
21179 type_die = follow_die_ref (die, attr, &type_cu);
21180 if (type_die == NULL)
21181 return build_error_marker_type (cu, die);
21182 /* If we find the type now, it's probably because the type came
21183 from an inter-CU reference and the type's CU got expanded before
21184 ours. */
21185 this_type = read_type_die (type_die, type_cu);
21186 }
21187
21188 /* If we still don't have a type use an error marker. */
21189
21190 if (this_type == NULL)
21191 return build_error_marker_type (cu, die);
21192
21193 return this_type;
21194 }
21195
21196 /* Return the type in DIE, CU.
21197 Returns NULL for invalid types.
21198
21199 This first does a lookup in die_type_hash,
21200 and only reads the die in if necessary.
21201
21202 NOTE: This can be called when reading in partial or full symbols. */
21203
21204 static struct type *
21205 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21206 {
21207 struct type *this_type;
21208
21209 this_type = get_die_type (die, cu);
21210 if (this_type)
21211 return this_type;
21212
21213 return read_type_die_1 (die, cu);
21214 }
21215
21216 /* Read the type in DIE, CU.
21217 Returns NULL for invalid types. */
21218
21219 static struct type *
21220 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21221 {
21222 struct type *this_type = NULL;
21223
21224 switch (die->tag)
21225 {
21226 case DW_TAG_class_type:
21227 case DW_TAG_interface_type:
21228 case DW_TAG_structure_type:
21229 case DW_TAG_union_type:
21230 this_type = read_structure_type (die, cu);
21231 break;
21232 case DW_TAG_enumeration_type:
21233 this_type = read_enumeration_type (die, cu);
21234 break;
21235 case DW_TAG_subprogram:
21236 case DW_TAG_subroutine_type:
21237 case DW_TAG_inlined_subroutine:
21238 this_type = read_subroutine_type (die, cu);
21239 break;
21240 case DW_TAG_array_type:
21241 this_type = read_array_type (die, cu);
21242 break;
21243 case DW_TAG_set_type:
21244 this_type = read_set_type (die, cu);
21245 break;
21246 case DW_TAG_pointer_type:
21247 this_type = read_tag_pointer_type (die, cu);
21248 break;
21249 case DW_TAG_ptr_to_member_type:
21250 this_type = read_tag_ptr_to_member_type (die, cu);
21251 break;
21252 case DW_TAG_reference_type:
21253 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21254 break;
21255 case DW_TAG_rvalue_reference_type:
21256 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21257 break;
21258 case DW_TAG_const_type:
21259 this_type = read_tag_const_type (die, cu);
21260 break;
21261 case DW_TAG_volatile_type:
21262 this_type = read_tag_volatile_type (die, cu);
21263 break;
21264 case DW_TAG_restrict_type:
21265 this_type = read_tag_restrict_type (die, cu);
21266 break;
21267 case DW_TAG_string_type:
21268 this_type = read_tag_string_type (die, cu);
21269 break;
21270 case DW_TAG_typedef:
21271 this_type = read_typedef (die, cu);
21272 break;
21273 case DW_TAG_subrange_type:
21274 this_type = read_subrange_type (die, cu);
21275 break;
21276 case DW_TAG_base_type:
21277 this_type = read_base_type (die, cu);
21278 break;
21279 case DW_TAG_unspecified_type:
21280 this_type = read_unspecified_type (die, cu);
21281 break;
21282 case DW_TAG_namespace:
21283 this_type = read_namespace_type (die, cu);
21284 break;
21285 case DW_TAG_module:
21286 this_type = read_module_type (die, cu);
21287 break;
21288 case DW_TAG_atomic_type:
21289 this_type = read_tag_atomic_type (die, cu);
21290 break;
21291 default:
21292 complaint (_("unexpected tag in read_type_die: '%s'"),
21293 dwarf_tag_name (die->tag));
21294 break;
21295 }
21296
21297 return this_type;
21298 }
21299
21300 /* See if we can figure out if the class lives in a namespace. We do
21301 this by looking for a member function; its demangled name will
21302 contain namespace info, if there is any.
21303 Return the computed name or NULL.
21304 Space for the result is allocated on the objfile's obstack.
21305 This is the full-die version of guess_partial_die_structure_name.
21306 In this case we know DIE has no useful parent. */
21307
21308 static const char *
21309 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21310 {
21311 struct die_info *spec_die;
21312 struct dwarf2_cu *spec_cu;
21313 struct die_info *child;
21314 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21315
21316 spec_cu = cu;
21317 spec_die = die_specification (die, &spec_cu);
21318 if (spec_die != NULL)
21319 {
21320 die = spec_die;
21321 cu = spec_cu;
21322 }
21323
21324 for (child = die->child;
21325 child != NULL;
21326 child = child->sibling)
21327 {
21328 if (child->tag == DW_TAG_subprogram)
21329 {
21330 const char *linkage_name = dw2_linkage_name (child, cu);
21331
21332 if (linkage_name != NULL)
21333 {
21334 gdb::unique_xmalloc_ptr<char> actual_name
21335 (language_class_name_from_physname (cu->language_defn,
21336 linkage_name));
21337 const char *name = NULL;
21338
21339 if (actual_name != NULL)
21340 {
21341 const char *die_name = dwarf2_name (die, cu);
21342
21343 if (die_name != NULL
21344 && strcmp (die_name, actual_name.get ()) != 0)
21345 {
21346 /* Strip off the class name from the full name.
21347 We want the prefix. */
21348 int die_name_len = strlen (die_name);
21349 int actual_name_len = strlen (actual_name.get ());
21350 const char *ptr = actual_name.get ();
21351
21352 /* Test for '::' as a sanity check. */
21353 if (actual_name_len > die_name_len + 2
21354 && ptr[actual_name_len - die_name_len - 1] == ':')
21355 name = obstack_strndup (
21356 &objfile->per_bfd->storage_obstack,
21357 ptr, actual_name_len - die_name_len - 2);
21358 }
21359 }
21360 return name;
21361 }
21362 }
21363 }
21364
21365 return NULL;
21366 }
21367
21368 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21369 prefix part in such case. See
21370 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21371
21372 static const char *
21373 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21374 {
21375 struct attribute *attr;
21376 const char *base;
21377
21378 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21379 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21380 return NULL;
21381
21382 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21383 return NULL;
21384
21385 attr = dw2_linkage_name_attr (die, cu);
21386 if (attr == NULL || DW_STRING (attr) == NULL)
21387 return NULL;
21388
21389 /* dwarf2_name had to be already called. */
21390 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21391
21392 /* Strip the base name, keep any leading namespaces/classes. */
21393 base = strrchr (DW_STRING (attr), ':');
21394 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21395 return "";
21396
21397 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21398 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21399 DW_STRING (attr),
21400 &base[-1] - DW_STRING (attr));
21401 }
21402
21403 /* Return the name of the namespace/class that DIE is defined within,
21404 or "" if we can't tell. The caller should not xfree the result.
21405
21406 For example, if we're within the method foo() in the following
21407 code:
21408
21409 namespace N {
21410 class C {
21411 void foo () {
21412 }
21413 };
21414 }
21415
21416 then determine_prefix on foo's die will return "N::C". */
21417
21418 static const char *
21419 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21420 {
21421 struct dwarf2_per_objfile *dwarf2_per_objfile
21422 = cu->per_cu->dwarf2_per_objfile;
21423 struct die_info *parent, *spec_die;
21424 struct dwarf2_cu *spec_cu;
21425 struct type *parent_type;
21426 const char *retval;
21427
21428 if (cu->language != language_cplus
21429 && cu->language != language_fortran && cu->language != language_d
21430 && cu->language != language_rust)
21431 return "";
21432
21433 retval = anonymous_struct_prefix (die, cu);
21434 if (retval)
21435 return retval;
21436
21437 /* We have to be careful in the presence of DW_AT_specification.
21438 For example, with GCC 3.4, given the code
21439
21440 namespace N {
21441 void foo() {
21442 // Definition of N::foo.
21443 }
21444 }
21445
21446 then we'll have a tree of DIEs like this:
21447
21448 1: DW_TAG_compile_unit
21449 2: DW_TAG_namespace // N
21450 3: DW_TAG_subprogram // declaration of N::foo
21451 4: DW_TAG_subprogram // definition of N::foo
21452 DW_AT_specification // refers to die #3
21453
21454 Thus, when processing die #4, we have to pretend that we're in
21455 the context of its DW_AT_specification, namely the contex of die
21456 #3. */
21457 spec_cu = cu;
21458 spec_die = die_specification (die, &spec_cu);
21459 if (spec_die == NULL)
21460 parent = die->parent;
21461 else
21462 {
21463 parent = spec_die->parent;
21464 cu = spec_cu;
21465 }
21466
21467 if (parent == NULL)
21468 return "";
21469 else if (parent->building_fullname)
21470 {
21471 const char *name;
21472 const char *parent_name;
21473
21474 /* It has been seen on RealView 2.2 built binaries,
21475 DW_TAG_template_type_param types actually _defined_ as
21476 children of the parent class:
21477
21478 enum E {};
21479 template class <class Enum> Class{};
21480 Class<enum E> class_e;
21481
21482 1: DW_TAG_class_type (Class)
21483 2: DW_TAG_enumeration_type (E)
21484 3: DW_TAG_enumerator (enum1:0)
21485 3: DW_TAG_enumerator (enum2:1)
21486 ...
21487 2: DW_TAG_template_type_param
21488 DW_AT_type DW_FORM_ref_udata (E)
21489
21490 Besides being broken debug info, it can put GDB into an
21491 infinite loop. Consider:
21492
21493 When we're building the full name for Class<E>, we'll start
21494 at Class, and go look over its template type parameters,
21495 finding E. We'll then try to build the full name of E, and
21496 reach here. We're now trying to build the full name of E,
21497 and look over the parent DIE for containing scope. In the
21498 broken case, if we followed the parent DIE of E, we'd again
21499 find Class, and once again go look at its template type
21500 arguments, etc., etc. Simply don't consider such parent die
21501 as source-level parent of this die (it can't be, the language
21502 doesn't allow it), and break the loop here. */
21503 name = dwarf2_name (die, cu);
21504 parent_name = dwarf2_name (parent, cu);
21505 complaint (_("template param type '%s' defined within parent '%s'"),
21506 name ? name : "<unknown>",
21507 parent_name ? parent_name : "<unknown>");
21508 return "";
21509 }
21510 else
21511 switch (parent->tag)
21512 {
21513 case DW_TAG_namespace:
21514 parent_type = read_type_die (parent, cu);
21515 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21516 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21517 Work around this problem here. */
21518 if (cu->language == language_cplus
21519 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21520 return "";
21521 /* We give a name to even anonymous namespaces. */
21522 return TYPE_NAME (parent_type);
21523 case DW_TAG_class_type:
21524 case DW_TAG_interface_type:
21525 case DW_TAG_structure_type:
21526 case DW_TAG_union_type:
21527 case DW_TAG_module:
21528 parent_type = read_type_die (parent, cu);
21529 if (TYPE_NAME (parent_type) != NULL)
21530 return TYPE_NAME (parent_type);
21531 else
21532 /* An anonymous structure is only allowed non-static data
21533 members; no typedefs, no member functions, et cetera.
21534 So it does not need a prefix. */
21535 return "";
21536 case DW_TAG_compile_unit:
21537 case DW_TAG_partial_unit:
21538 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21539 if (cu->language == language_cplus
21540 && !dwarf2_per_objfile->types.empty ()
21541 && die->child != NULL
21542 && (die->tag == DW_TAG_class_type
21543 || die->tag == DW_TAG_structure_type
21544 || die->tag == DW_TAG_union_type))
21545 {
21546 const char *name = guess_full_die_structure_name (die, cu);
21547 if (name != NULL)
21548 return name;
21549 }
21550 return "";
21551 case DW_TAG_subprogram:
21552 /* Nested subroutines in Fortran get a prefix with the name
21553 of the parent's subroutine. */
21554 if (cu->language == language_fortran)
21555 {
21556 if ((die->tag == DW_TAG_subprogram)
21557 && (dwarf2_name (parent, cu) != NULL))
21558 return dwarf2_name (parent, cu);
21559 }
21560 return determine_prefix (parent, cu);
21561 case DW_TAG_enumeration_type:
21562 parent_type = read_type_die (parent, cu);
21563 if (TYPE_DECLARED_CLASS (parent_type))
21564 {
21565 if (TYPE_NAME (parent_type) != NULL)
21566 return TYPE_NAME (parent_type);
21567 return "";
21568 }
21569 /* Fall through. */
21570 default:
21571 return determine_prefix (parent, cu);
21572 }
21573 }
21574
21575 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21576 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21577 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21578 an obconcat, otherwise allocate storage for the result. The CU argument is
21579 used to determine the language and hence, the appropriate separator. */
21580
21581 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21582
21583 static char *
21584 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21585 int physname, struct dwarf2_cu *cu)
21586 {
21587 const char *lead = "";
21588 const char *sep;
21589
21590 if (suffix == NULL || suffix[0] == '\0'
21591 || prefix == NULL || prefix[0] == '\0')
21592 sep = "";
21593 else if (cu->language == language_d)
21594 {
21595 /* For D, the 'main' function could be defined in any module, but it
21596 should never be prefixed. */
21597 if (strcmp (suffix, "D main") == 0)
21598 {
21599 prefix = "";
21600 sep = "";
21601 }
21602 else
21603 sep = ".";
21604 }
21605 else if (cu->language == language_fortran && physname)
21606 {
21607 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21608 DW_AT_MIPS_linkage_name is preferred and used instead. */
21609
21610 lead = "__";
21611 sep = "_MOD_";
21612 }
21613 else
21614 sep = "::";
21615
21616 if (prefix == NULL)
21617 prefix = "";
21618 if (suffix == NULL)
21619 suffix = "";
21620
21621 if (obs == NULL)
21622 {
21623 char *retval
21624 = ((char *)
21625 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21626
21627 strcpy (retval, lead);
21628 strcat (retval, prefix);
21629 strcat (retval, sep);
21630 strcat (retval, suffix);
21631 return retval;
21632 }
21633 else
21634 {
21635 /* We have an obstack. */
21636 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21637 }
21638 }
21639
21640 /* Return sibling of die, NULL if no sibling. */
21641
21642 static struct die_info *
21643 sibling_die (struct die_info *die)
21644 {
21645 return die->sibling;
21646 }
21647
21648 /* Get name of a die, return NULL if not found. */
21649
21650 static const char *
21651 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21652 struct objfile *objfile)
21653 {
21654 if (name && cu->language == language_cplus)
21655 {
21656 std::string canon_name = cp_canonicalize_string (name);
21657
21658 if (!canon_name.empty ())
21659 {
21660 if (canon_name != name)
21661 name = objfile->intern (canon_name);
21662 }
21663 }
21664
21665 return name;
21666 }
21667
21668 /* Get name of a die, return NULL if not found.
21669 Anonymous namespaces are converted to their magic string. */
21670
21671 static const char *
21672 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21673 {
21674 struct attribute *attr;
21675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21676
21677 attr = dwarf2_attr (die, DW_AT_name, cu);
21678 if ((!attr || !DW_STRING (attr))
21679 && die->tag != DW_TAG_namespace
21680 && die->tag != DW_TAG_class_type
21681 && die->tag != DW_TAG_interface_type
21682 && die->tag != DW_TAG_structure_type
21683 && die->tag != DW_TAG_union_type)
21684 return NULL;
21685
21686 switch (die->tag)
21687 {
21688 case DW_TAG_compile_unit:
21689 case DW_TAG_partial_unit:
21690 /* Compilation units have a DW_AT_name that is a filename, not
21691 a source language identifier. */
21692 case DW_TAG_enumeration_type:
21693 case DW_TAG_enumerator:
21694 /* These tags always have simple identifiers already; no need
21695 to canonicalize them. */
21696 return DW_STRING (attr);
21697
21698 case DW_TAG_namespace:
21699 if (attr != NULL && DW_STRING (attr) != NULL)
21700 return DW_STRING (attr);
21701 return CP_ANONYMOUS_NAMESPACE_STR;
21702
21703 case DW_TAG_class_type:
21704 case DW_TAG_interface_type:
21705 case DW_TAG_structure_type:
21706 case DW_TAG_union_type:
21707 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21708 structures or unions. These were of the form "._%d" in GCC 4.1,
21709 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21710 and GCC 4.4. We work around this problem by ignoring these. */
21711 if (attr && DW_STRING (attr)
21712 && (startswith (DW_STRING (attr), "._")
21713 || startswith (DW_STRING (attr), "<anonymous")))
21714 return NULL;
21715
21716 /* GCC might emit a nameless typedef that has a linkage name. See
21717 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21718 if (!attr || DW_STRING (attr) == NULL)
21719 {
21720 attr = dw2_linkage_name_attr (die, cu);
21721 if (attr == NULL || DW_STRING (attr) == NULL)
21722 return NULL;
21723
21724 /* Avoid demangling DW_STRING (attr) the second time on a second
21725 call for the same DIE. */
21726 if (!DW_STRING_IS_CANONICAL (attr))
21727 {
21728 gdb::unique_xmalloc_ptr<char> demangled
21729 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21730 if (demangled == nullptr)
21731 return nullptr;
21732
21733 DW_STRING (attr) = objfile->intern (demangled.get ());
21734 DW_STRING_IS_CANONICAL (attr) = 1;
21735 }
21736
21737 /* Strip any leading namespaces/classes, keep only the base name.
21738 DW_AT_name for named DIEs does not contain the prefixes. */
21739 const char *base = strrchr (DW_STRING (attr), ':');
21740 if (base && base > DW_STRING (attr) && base[-1] == ':')
21741 return &base[1];
21742 else
21743 return DW_STRING (attr);
21744 }
21745 break;
21746
21747 default:
21748 break;
21749 }
21750
21751 if (!DW_STRING_IS_CANONICAL (attr))
21752 {
21753 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21754 objfile);
21755 DW_STRING_IS_CANONICAL (attr) = 1;
21756 }
21757 return DW_STRING (attr);
21758 }
21759
21760 /* Return the die that this die in an extension of, or NULL if there
21761 is none. *EXT_CU is the CU containing DIE on input, and the CU
21762 containing the return value on output. */
21763
21764 static struct die_info *
21765 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21766 {
21767 struct attribute *attr;
21768
21769 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21770 if (attr == NULL)
21771 return NULL;
21772
21773 return follow_die_ref (die, attr, ext_cu);
21774 }
21775
21776 /* A convenience function that returns an "unknown" DWARF name,
21777 including the value of V. STR is the name of the entity being
21778 printed, e.g., "TAG". */
21779
21780 static const char *
21781 dwarf_unknown (const char *str, unsigned v)
21782 {
21783 char *cell = get_print_cell ();
21784 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21785 return cell;
21786 }
21787
21788 /* Convert a DIE tag into its string name. */
21789
21790 static const char *
21791 dwarf_tag_name (unsigned tag)
21792 {
21793 const char *name = get_DW_TAG_name (tag);
21794
21795 if (name == NULL)
21796 return dwarf_unknown ("TAG", tag);
21797
21798 return name;
21799 }
21800
21801 /* Convert a DWARF attribute code into its string name. */
21802
21803 static const char *
21804 dwarf_attr_name (unsigned attr)
21805 {
21806 const char *name;
21807
21808 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21809 if (attr == DW_AT_MIPS_fde)
21810 return "DW_AT_MIPS_fde";
21811 #else
21812 if (attr == DW_AT_HP_block_index)
21813 return "DW_AT_HP_block_index";
21814 #endif
21815
21816 name = get_DW_AT_name (attr);
21817
21818 if (name == NULL)
21819 return dwarf_unknown ("AT", attr);
21820
21821 return name;
21822 }
21823
21824 /* Convert a DWARF value form code into its string name. */
21825
21826 static const char *
21827 dwarf_form_name (unsigned form)
21828 {
21829 const char *name = get_DW_FORM_name (form);
21830
21831 if (name == NULL)
21832 return dwarf_unknown ("FORM", form);
21833
21834 return name;
21835 }
21836
21837 static const char *
21838 dwarf_bool_name (unsigned mybool)
21839 {
21840 if (mybool)
21841 return "TRUE";
21842 else
21843 return "FALSE";
21844 }
21845
21846 /* Convert a DWARF type code into its string name. */
21847
21848 static const char *
21849 dwarf_type_encoding_name (unsigned enc)
21850 {
21851 const char *name = get_DW_ATE_name (enc);
21852
21853 if (name == NULL)
21854 return dwarf_unknown ("ATE", enc);
21855
21856 return name;
21857 }
21858
21859 static void
21860 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21861 {
21862 unsigned int i;
21863
21864 print_spaces (indent, f);
21865 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21866 dwarf_tag_name (die->tag), die->abbrev,
21867 sect_offset_str (die->sect_off));
21868
21869 if (die->parent != NULL)
21870 {
21871 print_spaces (indent, f);
21872 fprintf_unfiltered (f, " parent at offset: %s\n",
21873 sect_offset_str (die->parent->sect_off));
21874 }
21875
21876 print_spaces (indent, f);
21877 fprintf_unfiltered (f, " has children: %s\n",
21878 dwarf_bool_name (die->child != NULL));
21879
21880 print_spaces (indent, f);
21881 fprintf_unfiltered (f, " attributes:\n");
21882
21883 for (i = 0; i < die->num_attrs; ++i)
21884 {
21885 print_spaces (indent, f);
21886 fprintf_unfiltered (f, " %s (%s) ",
21887 dwarf_attr_name (die->attrs[i].name),
21888 dwarf_form_name (die->attrs[i].form));
21889
21890 switch (die->attrs[i].form)
21891 {
21892 case DW_FORM_addr:
21893 case DW_FORM_addrx:
21894 case DW_FORM_GNU_addr_index:
21895 fprintf_unfiltered (f, "address: ");
21896 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21897 break;
21898 case DW_FORM_block2:
21899 case DW_FORM_block4:
21900 case DW_FORM_block:
21901 case DW_FORM_block1:
21902 fprintf_unfiltered (f, "block: size %s",
21903 pulongest (DW_BLOCK (&die->attrs[i])->size));
21904 break;
21905 case DW_FORM_exprloc:
21906 fprintf_unfiltered (f, "expression: size %s",
21907 pulongest (DW_BLOCK (&die->attrs[i])->size));
21908 break;
21909 case DW_FORM_data16:
21910 fprintf_unfiltered (f, "constant of 16 bytes");
21911 break;
21912 case DW_FORM_ref_addr:
21913 fprintf_unfiltered (f, "ref address: ");
21914 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21915 break;
21916 case DW_FORM_GNU_ref_alt:
21917 fprintf_unfiltered (f, "alt ref address: ");
21918 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21919 break;
21920 case DW_FORM_ref1:
21921 case DW_FORM_ref2:
21922 case DW_FORM_ref4:
21923 case DW_FORM_ref8:
21924 case DW_FORM_ref_udata:
21925 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21926 (long) (DW_UNSND (&die->attrs[i])));
21927 break;
21928 case DW_FORM_data1:
21929 case DW_FORM_data2:
21930 case DW_FORM_data4:
21931 case DW_FORM_data8:
21932 case DW_FORM_udata:
21933 case DW_FORM_sdata:
21934 fprintf_unfiltered (f, "constant: %s",
21935 pulongest (DW_UNSND (&die->attrs[i])));
21936 break;
21937 case DW_FORM_sec_offset:
21938 fprintf_unfiltered (f, "section offset: %s",
21939 pulongest (DW_UNSND (&die->attrs[i])));
21940 break;
21941 case DW_FORM_ref_sig8:
21942 fprintf_unfiltered (f, "signature: %s",
21943 hex_string (DW_SIGNATURE (&die->attrs[i])));
21944 break;
21945 case DW_FORM_string:
21946 case DW_FORM_strp:
21947 case DW_FORM_line_strp:
21948 case DW_FORM_strx:
21949 case DW_FORM_GNU_str_index:
21950 case DW_FORM_GNU_strp_alt:
21951 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21952 DW_STRING (&die->attrs[i])
21953 ? DW_STRING (&die->attrs[i]) : "",
21954 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21955 break;
21956 case DW_FORM_flag:
21957 if (DW_UNSND (&die->attrs[i]))
21958 fprintf_unfiltered (f, "flag: TRUE");
21959 else
21960 fprintf_unfiltered (f, "flag: FALSE");
21961 break;
21962 case DW_FORM_flag_present:
21963 fprintf_unfiltered (f, "flag: TRUE");
21964 break;
21965 case DW_FORM_indirect:
21966 /* The reader will have reduced the indirect form to
21967 the "base form" so this form should not occur. */
21968 fprintf_unfiltered (f,
21969 "unexpected attribute form: DW_FORM_indirect");
21970 break;
21971 case DW_FORM_implicit_const:
21972 fprintf_unfiltered (f, "constant: %s",
21973 plongest (DW_SND (&die->attrs[i])));
21974 break;
21975 default:
21976 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21977 die->attrs[i].form);
21978 break;
21979 }
21980 fprintf_unfiltered (f, "\n");
21981 }
21982 }
21983
21984 static void
21985 dump_die_for_error (struct die_info *die)
21986 {
21987 dump_die_shallow (gdb_stderr, 0, die);
21988 }
21989
21990 static void
21991 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21992 {
21993 int indent = level * 4;
21994
21995 gdb_assert (die != NULL);
21996
21997 if (level >= max_level)
21998 return;
21999
22000 dump_die_shallow (f, indent, die);
22001
22002 if (die->child != NULL)
22003 {
22004 print_spaces (indent, f);
22005 fprintf_unfiltered (f, " Children:");
22006 if (level + 1 < max_level)
22007 {
22008 fprintf_unfiltered (f, "\n");
22009 dump_die_1 (f, level + 1, max_level, die->child);
22010 }
22011 else
22012 {
22013 fprintf_unfiltered (f,
22014 " [not printed, max nesting level reached]\n");
22015 }
22016 }
22017
22018 if (die->sibling != NULL && level > 0)
22019 {
22020 dump_die_1 (f, level, max_level, die->sibling);
22021 }
22022 }
22023
22024 /* This is called from the pdie macro in gdbinit.in.
22025 It's not static so gcc will keep a copy callable from gdb. */
22026
22027 void
22028 dump_die (struct die_info *die, int max_level)
22029 {
22030 dump_die_1 (gdb_stdlog, 0, max_level, die);
22031 }
22032
22033 static void
22034 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22035 {
22036 void **slot;
22037
22038 slot = htab_find_slot_with_hash (cu->die_hash, die,
22039 to_underlying (die->sect_off),
22040 INSERT);
22041
22042 *slot = die;
22043 }
22044
22045 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22046 required kind. */
22047
22048 static sect_offset
22049 dwarf2_get_ref_die_offset (const struct attribute *attr)
22050 {
22051 if (attr->form_is_ref ())
22052 return (sect_offset) DW_UNSND (attr);
22053
22054 complaint (_("unsupported die ref attribute form: '%s'"),
22055 dwarf_form_name (attr->form));
22056 return {};
22057 }
22058
22059 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22060 * the value held by the attribute is not constant. */
22061
22062 static LONGEST
22063 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22064 {
22065 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22066 return DW_SND (attr);
22067 else if (attr->form == DW_FORM_udata
22068 || attr->form == DW_FORM_data1
22069 || attr->form == DW_FORM_data2
22070 || attr->form == DW_FORM_data4
22071 || attr->form == DW_FORM_data8)
22072 return DW_UNSND (attr);
22073 else
22074 {
22075 /* For DW_FORM_data16 see attribute::form_is_constant. */
22076 complaint (_("Attribute value is not a constant (%s)"),
22077 dwarf_form_name (attr->form));
22078 return default_value;
22079 }
22080 }
22081
22082 /* Follow reference or signature attribute ATTR of SRC_DIE.
22083 On entry *REF_CU is the CU of SRC_DIE.
22084 On exit *REF_CU is the CU of the result. */
22085
22086 static struct die_info *
22087 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22088 struct dwarf2_cu **ref_cu)
22089 {
22090 struct die_info *die;
22091
22092 if (attr->form_is_ref ())
22093 die = follow_die_ref (src_die, attr, ref_cu);
22094 else if (attr->form == DW_FORM_ref_sig8)
22095 die = follow_die_sig (src_die, attr, ref_cu);
22096 else
22097 {
22098 dump_die_for_error (src_die);
22099 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22100 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22101 }
22102
22103 return die;
22104 }
22105
22106 /* Follow reference OFFSET.
22107 On entry *REF_CU is the CU of the source die referencing OFFSET.
22108 On exit *REF_CU is the CU of the result.
22109 Returns NULL if OFFSET is invalid. */
22110
22111 static struct die_info *
22112 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22113 struct dwarf2_cu **ref_cu)
22114 {
22115 struct die_info temp_die;
22116 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22117 struct dwarf2_per_objfile *dwarf2_per_objfile
22118 = cu->per_cu->dwarf2_per_objfile;
22119
22120 gdb_assert (cu->per_cu != NULL);
22121
22122 target_cu = cu;
22123
22124 if (cu->per_cu->is_debug_types)
22125 {
22126 /* .debug_types CUs cannot reference anything outside their CU.
22127 If they need to, they have to reference a signatured type via
22128 DW_FORM_ref_sig8. */
22129 if (!cu->header.offset_in_cu_p (sect_off))
22130 return NULL;
22131 }
22132 else if (offset_in_dwz != cu->per_cu->is_dwz
22133 || !cu->header.offset_in_cu_p (sect_off))
22134 {
22135 struct dwarf2_per_cu_data *per_cu;
22136
22137 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22138 dwarf2_per_objfile);
22139
22140 /* If necessary, add it to the queue and load its DIEs. */
22141 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22142 load_full_comp_unit (per_cu, false, cu->language);
22143
22144 target_cu = per_cu->cu;
22145 }
22146 else if (cu->dies == NULL)
22147 {
22148 /* We're loading full DIEs during partial symbol reading. */
22149 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22150 load_full_comp_unit (cu->per_cu, false, language_minimal);
22151 }
22152
22153 *ref_cu = target_cu;
22154 temp_die.sect_off = sect_off;
22155
22156 if (target_cu != cu)
22157 target_cu->ancestor = cu;
22158
22159 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22160 &temp_die,
22161 to_underlying (sect_off));
22162 }
22163
22164 /* Follow reference attribute ATTR of SRC_DIE.
22165 On entry *REF_CU is the CU of SRC_DIE.
22166 On exit *REF_CU is the CU of the result. */
22167
22168 static struct die_info *
22169 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22170 struct dwarf2_cu **ref_cu)
22171 {
22172 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22173 struct dwarf2_cu *cu = *ref_cu;
22174 struct die_info *die;
22175
22176 die = follow_die_offset (sect_off,
22177 (attr->form == DW_FORM_GNU_ref_alt
22178 || cu->per_cu->is_dwz),
22179 ref_cu);
22180 if (!die)
22181 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22182 "at %s [in module %s]"),
22183 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22184 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22185
22186 return die;
22187 }
22188
22189 /* See read.h. */
22190
22191 struct dwarf2_locexpr_baton
22192 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22193 dwarf2_per_cu_data *per_cu,
22194 CORE_ADDR (*get_frame_pc) (void *baton),
22195 void *baton, bool resolve_abstract_p)
22196 {
22197 struct dwarf2_cu *cu;
22198 struct die_info *die;
22199 struct attribute *attr;
22200 struct dwarf2_locexpr_baton retval;
22201 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22202 struct objfile *objfile = dwarf2_per_objfile->objfile;
22203
22204 if (per_cu->cu == NULL)
22205 load_cu (per_cu, false);
22206 cu = per_cu->cu;
22207 if (cu == NULL)
22208 {
22209 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22210 Instead just throw an error, not much else we can do. */
22211 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22212 sect_offset_str (sect_off), objfile_name (objfile));
22213 }
22214
22215 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22216 if (!die)
22217 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22218 sect_offset_str (sect_off), objfile_name (objfile));
22219
22220 attr = dwarf2_attr (die, DW_AT_location, cu);
22221 if (!attr && resolve_abstract_p
22222 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22223 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22224 {
22225 CORE_ADDR pc = (*get_frame_pc) (baton);
22226 CORE_ADDR baseaddr = objfile->text_section_offset ();
22227 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22228
22229 for (const auto &cand_off
22230 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22231 {
22232 struct dwarf2_cu *cand_cu = cu;
22233 struct die_info *cand
22234 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22235 if (!cand
22236 || !cand->parent
22237 || cand->parent->tag != DW_TAG_subprogram)
22238 continue;
22239
22240 CORE_ADDR pc_low, pc_high;
22241 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22242 if (pc_low == ((CORE_ADDR) -1))
22243 continue;
22244 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22245 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22246 if (!(pc_low <= pc && pc < pc_high))
22247 continue;
22248
22249 die = cand;
22250 attr = dwarf2_attr (die, DW_AT_location, cu);
22251 break;
22252 }
22253 }
22254
22255 if (!attr)
22256 {
22257 /* DWARF: "If there is no such attribute, then there is no effect.".
22258 DATA is ignored if SIZE is 0. */
22259
22260 retval.data = NULL;
22261 retval.size = 0;
22262 }
22263 else if (attr->form_is_section_offset ())
22264 {
22265 struct dwarf2_loclist_baton loclist_baton;
22266 CORE_ADDR pc = (*get_frame_pc) (baton);
22267 size_t size;
22268
22269 fill_in_loclist_baton (cu, &loclist_baton, attr);
22270
22271 retval.data = dwarf2_find_location_expression (&loclist_baton,
22272 &size, pc);
22273 retval.size = size;
22274 }
22275 else
22276 {
22277 if (!attr->form_is_block ())
22278 error (_("Dwarf Error: DIE at %s referenced in module %s "
22279 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22280 sect_offset_str (sect_off), objfile_name (objfile));
22281
22282 retval.data = DW_BLOCK (attr)->data;
22283 retval.size = DW_BLOCK (attr)->size;
22284 }
22285 retval.per_cu = cu->per_cu;
22286
22287 age_cached_comp_units (dwarf2_per_objfile);
22288
22289 return retval;
22290 }
22291
22292 /* See read.h. */
22293
22294 struct dwarf2_locexpr_baton
22295 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22296 dwarf2_per_cu_data *per_cu,
22297 CORE_ADDR (*get_frame_pc) (void *baton),
22298 void *baton)
22299 {
22300 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22301
22302 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22303 }
22304
22305 /* Write a constant of a given type as target-ordered bytes into
22306 OBSTACK. */
22307
22308 static const gdb_byte *
22309 write_constant_as_bytes (struct obstack *obstack,
22310 enum bfd_endian byte_order,
22311 struct type *type,
22312 ULONGEST value,
22313 LONGEST *len)
22314 {
22315 gdb_byte *result;
22316
22317 *len = TYPE_LENGTH (type);
22318 result = (gdb_byte *) obstack_alloc (obstack, *len);
22319 store_unsigned_integer (result, *len, byte_order, value);
22320
22321 return result;
22322 }
22323
22324 /* See read.h. */
22325
22326 const gdb_byte *
22327 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22328 dwarf2_per_cu_data *per_cu,
22329 obstack *obstack,
22330 LONGEST *len)
22331 {
22332 struct dwarf2_cu *cu;
22333 struct die_info *die;
22334 struct attribute *attr;
22335 const gdb_byte *result = NULL;
22336 struct type *type;
22337 LONGEST value;
22338 enum bfd_endian byte_order;
22339 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22340
22341 if (per_cu->cu == NULL)
22342 load_cu (per_cu, false);
22343 cu = per_cu->cu;
22344 if (cu == NULL)
22345 {
22346 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22347 Instead just throw an error, not much else we can do. */
22348 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22349 sect_offset_str (sect_off), objfile_name (objfile));
22350 }
22351
22352 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22353 if (!die)
22354 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22355 sect_offset_str (sect_off), objfile_name (objfile));
22356
22357 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22358 if (attr == NULL)
22359 return NULL;
22360
22361 byte_order = (bfd_big_endian (objfile->obfd)
22362 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22363
22364 switch (attr->form)
22365 {
22366 case DW_FORM_addr:
22367 case DW_FORM_addrx:
22368 case DW_FORM_GNU_addr_index:
22369 {
22370 gdb_byte *tem;
22371
22372 *len = cu->header.addr_size;
22373 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22374 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22375 result = tem;
22376 }
22377 break;
22378 case DW_FORM_string:
22379 case DW_FORM_strp:
22380 case DW_FORM_strx:
22381 case DW_FORM_GNU_str_index:
22382 case DW_FORM_GNU_strp_alt:
22383 /* DW_STRING is already allocated on the objfile obstack, point
22384 directly to it. */
22385 result = (const gdb_byte *) DW_STRING (attr);
22386 *len = strlen (DW_STRING (attr));
22387 break;
22388 case DW_FORM_block1:
22389 case DW_FORM_block2:
22390 case DW_FORM_block4:
22391 case DW_FORM_block:
22392 case DW_FORM_exprloc:
22393 case DW_FORM_data16:
22394 result = DW_BLOCK (attr)->data;
22395 *len = DW_BLOCK (attr)->size;
22396 break;
22397
22398 /* The DW_AT_const_value attributes are supposed to carry the
22399 symbol's value "represented as it would be on the target
22400 architecture." By the time we get here, it's already been
22401 converted to host endianness, so we just need to sign- or
22402 zero-extend it as appropriate. */
22403 case DW_FORM_data1:
22404 type = die_type (die, cu);
22405 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22406 if (result == NULL)
22407 result = write_constant_as_bytes (obstack, byte_order,
22408 type, value, len);
22409 break;
22410 case DW_FORM_data2:
22411 type = die_type (die, cu);
22412 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22413 if (result == NULL)
22414 result = write_constant_as_bytes (obstack, byte_order,
22415 type, value, len);
22416 break;
22417 case DW_FORM_data4:
22418 type = die_type (die, cu);
22419 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22420 if (result == NULL)
22421 result = write_constant_as_bytes (obstack, byte_order,
22422 type, value, len);
22423 break;
22424 case DW_FORM_data8:
22425 type = die_type (die, cu);
22426 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22427 if (result == NULL)
22428 result = write_constant_as_bytes (obstack, byte_order,
22429 type, value, len);
22430 break;
22431
22432 case DW_FORM_sdata:
22433 case DW_FORM_implicit_const:
22434 type = die_type (die, cu);
22435 result = write_constant_as_bytes (obstack, byte_order,
22436 type, DW_SND (attr), len);
22437 break;
22438
22439 case DW_FORM_udata:
22440 type = die_type (die, cu);
22441 result = write_constant_as_bytes (obstack, byte_order,
22442 type, DW_UNSND (attr), len);
22443 break;
22444
22445 default:
22446 complaint (_("unsupported const value attribute form: '%s'"),
22447 dwarf_form_name (attr->form));
22448 break;
22449 }
22450
22451 return result;
22452 }
22453
22454 /* See read.h. */
22455
22456 struct type *
22457 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22458 dwarf2_per_cu_data *per_cu)
22459 {
22460 struct dwarf2_cu *cu;
22461 struct die_info *die;
22462
22463 if (per_cu->cu == NULL)
22464 load_cu (per_cu, false);
22465 cu = per_cu->cu;
22466 if (!cu)
22467 return NULL;
22468
22469 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22470 if (!die)
22471 return NULL;
22472
22473 return die_type (die, cu);
22474 }
22475
22476 /* See read.h. */
22477
22478 struct type *
22479 dwarf2_get_die_type (cu_offset die_offset,
22480 struct dwarf2_per_cu_data *per_cu)
22481 {
22482 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22483 return get_die_type_at_offset (die_offset_sect, per_cu);
22484 }
22485
22486 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22487 On entry *REF_CU is the CU of SRC_DIE.
22488 On exit *REF_CU is the CU of the result.
22489 Returns NULL if the referenced DIE isn't found. */
22490
22491 static struct die_info *
22492 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22493 struct dwarf2_cu **ref_cu)
22494 {
22495 struct die_info temp_die;
22496 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22497 struct die_info *die;
22498
22499 /* While it might be nice to assert sig_type->type == NULL here,
22500 we can get here for DW_AT_imported_declaration where we need
22501 the DIE not the type. */
22502
22503 /* If necessary, add it to the queue and load its DIEs. */
22504
22505 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22506 read_signatured_type (sig_type);
22507
22508 sig_cu = sig_type->per_cu.cu;
22509 gdb_assert (sig_cu != NULL);
22510 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22511 temp_die.sect_off = sig_type->type_offset_in_section;
22512 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22513 to_underlying (temp_die.sect_off));
22514 if (die)
22515 {
22516 struct dwarf2_per_objfile *dwarf2_per_objfile
22517 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22518
22519 /* For .gdb_index version 7 keep track of included TUs.
22520 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22521 if (dwarf2_per_objfile->index_table != NULL
22522 && dwarf2_per_objfile->index_table->version <= 7)
22523 {
22524 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22525 }
22526
22527 *ref_cu = sig_cu;
22528 if (sig_cu != cu)
22529 sig_cu->ancestor = cu;
22530
22531 return die;
22532 }
22533
22534 return NULL;
22535 }
22536
22537 /* Follow signatured type referenced by ATTR in SRC_DIE.
22538 On entry *REF_CU is the CU of SRC_DIE.
22539 On exit *REF_CU is the CU of the result.
22540 The result is the DIE of the type.
22541 If the referenced type cannot be found an error is thrown. */
22542
22543 static struct die_info *
22544 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22545 struct dwarf2_cu **ref_cu)
22546 {
22547 ULONGEST signature = DW_SIGNATURE (attr);
22548 struct signatured_type *sig_type;
22549 struct die_info *die;
22550
22551 gdb_assert (attr->form == DW_FORM_ref_sig8);
22552
22553 sig_type = lookup_signatured_type (*ref_cu, signature);
22554 /* sig_type will be NULL if the signatured type is missing from
22555 the debug info. */
22556 if (sig_type == NULL)
22557 {
22558 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22559 " from DIE at %s [in module %s]"),
22560 hex_string (signature), sect_offset_str (src_die->sect_off),
22561 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22562 }
22563
22564 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22565 if (die == NULL)
22566 {
22567 dump_die_for_error (src_die);
22568 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22569 " from DIE at %s [in module %s]"),
22570 hex_string (signature), sect_offset_str (src_die->sect_off),
22571 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22572 }
22573
22574 return die;
22575 }
22576
22577 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22578 reading in and processing the type unit if necessary. */
22579
22580 static struct type *
22581 get_signatured_type (struct die_info *die, ULONGEST signature,
22582 struct dwarf2_cu *cu)
22583 {
22584 struct dwarf2_per_objfile *dwarf2_per_objfile
22585 = cu->per_cu->dwarf2_per_objfile;
22586 struct signatured_type *sig_type;
22587 struct dwarf2_cu *type_cu;
22588 struct die_info *type_die;
22589 struct type *type;
22590
22591 sig_type = lookup_signatured_type (cu, signature);
22592 /* sig_type will be NULL if the signatured type is missing from
22593 the debug info. */
22594 if (sig_type == NULL)
22595 {
22596 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22597 " from DIE at %s [in module %s]"),
22598 hex_string (signature), sect_offset_str (die->sect_off),
22599 objfile_name (dwarf2_per_objfile->objfile));
22600 return build_error_marker_type (cu, die);
22601 }
22602
22603 /* If we already know the type we're done. */
22604 if (sig_type->type != NULL)
22605 return sig_type->type;
22606
22607 type_cu = cu;
22608 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22609 if (type_die != NULL)
22610 {
22611 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22612 is created. This is important, for example, because for c++ classes
22613 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22614 type = read_type_die (type_die, type_cu);
22615 if (type == NULL)
22616 {
22617 complaint (_("Dwarf Error: Cannot build signatured type %s"
22618 " referenced from DIE at %s [in module %s]"),
22619 hex_string (signature), sect_offset_str (die->sect_off),
22620 objfile_name (dwarf2_per_objfile->objfile));
22621 type = build_error_marker_type (cu, die);
22622 }
22623 }
22624 else
22625 {
22626 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22627 " from DIE at %s [in module %s]"),
22628 hex_string (signature), sect_offset_str (die->sect_off),
22629 objfile_name (dwarf2_per_objfile->objfile));
22630 type = build_error_marker_type (cu, die);
22631 }
22632 sig_type->type = type;
22633
22634 return type;
22635 }
22636
22637 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22638 reading in and processing the type unit if necessary. */
22639
22640 static struct type *
22641 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22642 struct dwarf2_cu *cu) /* ARI: editCase function */
22643 {
22644 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22645 if (attr->form_is_ref ())
22646 {
22647 struct dwarf2_cu *type_cu = cu;
22648 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22649
22650 return read_type_die (type_die, type_cu);
22651 }
22652 else if (attr->form == DW_FORM_ref_sig8)
22653 {
22654 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22655 }
22656 else
22657 {
22658 struct dwarf2_per_objfile *dwarf2_per_objfile
22659 = cu->per_cu->dwarf2_per_objfile;
22660
22661 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22662 " at %s [in module %s]"),
22663 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22664 objfile_name (dwarf2_per_objfile->objfile));
22665 return build_error_marker_type (cu, die);
22666 }
22667 }
22668
22669 /* Load the DIEs associated with type unit PER_CU into memory. */
22670
22671 static void
22672 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22673 {
22674 struct signatured_type *sig_type;
22675
22676 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22677 gdb_assert (! per_cu->type_unit_group_p ());
22678
22679 /* We have the per_cu, but we need the signatured_type.
22680 Fortunately this is an easy translation. */
22681 gdb_assert (per_cu->is_debug_types);
22682 sig_type = (struct signatured_type *) per_cu;
22683
22684 gdb_assert (per_cu->cu == NULL);
22685
22686 read_signatured_type (sig_type);
22687
22688 gdb_assert (per_cu->cu != NULL);
22689 }
22690
22691 /* Read in a signatured type and build its CU and DIEs.
22692 If the type is a stub for the real type in a DWO file,
22693 read in the real type from the DWO file as well. */
22694
22695 static void
22696 read_signatured_type (struct signatured_type *sig_type)
22697 {
22698 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22699
22700 gdb_assert (per_cu->is_debug_types);
22701 gdb_assert (per_cu->cu == NULL);
22702
22703 cutu_reader reader (per_cu, NULL, 0, false);
22704
22705 if (!reader.dummy_p)
22706 {
22707 struct dwarf2_cu *cu = reader.cu;
22708 const gdb_byte *info_ptr = reader.info_ptr;
22709
22710 gdb_assert (cu->die_hash == NULL);
22711 cu->die_hash =
22712 htab_create_alloc_ex (cu->header.length / 12,
22713 die_hash,
22714 die_eq,
22715 NULL,
22716 &cu->comp_unit_obstack,
22717 hashtab_obstack_allocate,
22718 dummy_obstack_deallocate);
22719
22720 if (reader.comp_unit_die->has_children)
22721 reader.comp_unit_die->child
22722 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22723 reader.comp_unit_die);
22724 cu->dies = reader.comp_unit_die;
22725 /* comp_unit_die is not stored in die_hash, no need. */
22726
22727 /* We try not to read any attributes in this function, because
22728 not all CUs needed for references have been loaded yet, and
22729 symbol table processing isn't initialized. But we have to
22730 set the CU language, or we won't be able to build types
22731 correctly. Similarly, if we do not read the producer, we can
22732 not apply producer-specific interpretation. */
22733 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22734
22735 reader.keep ();
22736 }
22737
22738 sig_type->per_cu.tu_read = 1;
22739 }
22740
22741 /* Decode simple location descriptions.
22742 Given a pointer to a dwarf block that defines a location, compute
22743 the location and return the value.
22744
22745 NOTE drow/2003-11-18: This function is called in two situations
22746 now: for the address of static or global variables (partial symbols
22747 only) and for offsets into structures which are expected to be
22748 (more or less) constant. The partial symbol case should go away,
22749 and only the constant case should remain. That will let this
22750 function complain more accurately. A few special modes are allowed
22751 without complaint for global variables (for instance, global
22752 register values and thread-local values).
22753
22754 A location description containing no operations indicates that the
22755 object is optimized out. The return value is 0 for that case.
22756 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22757 callers will only want a very basic result and this can become a
22758 complaint.
22759
22760 Note that stack[0] is unused except as a default error return. */
22761
22762 static CORE_ADDR
22763 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22764 {
22765 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22766 size_t i;
22767 size_t size = blk->size;
22768 const gdb_byte *data = blk->data;
22769 CORE_ADDR stack[64];
22770 int stacki;
22771 unsigned int bytes_read, unsnd;
22772 gdb_byte op;
22773
22774 i = 0;
22775 stacki = 0;
22776 stack[stacki] = 0;
22777 stack[++stacki] = 0;
22778
22779 while (i < size)
22780 {
22781 op = data[i++];
22782 switch (op)
22783 {
22784 case DW_OP_lit0:
22785 case DW_OP_lit1:
22786 case DW_OP_lit2:
22787 case DW_OP_lit3:
22788 case DW_OP_lit4:
22789 case DW_OP_lit5:
22790 case DW_OP_lit6:
22791 case DW_OP_lit7:
22792 case DW_OP_lit8:
22793 case DW_OP_lit9:
22794 case DW_OP_lit10:
22795 case DW_OP_lit11:
22796 case DW_OP_lit12:
22797 case DW_OP_lit13:
22798 case DW_OP_lit14:
22799 case DW_OP_lit15:
22800 case DW_OP_lit16:
22801 case DW_OP_lit17:
22802 case DW_OP_lit18:
22803 case DW_OP_lit19:
22804 case DW_OP_lit20:
22805 case DW_OP_lit21:
22806 case DW_OP_lit22:
22807 case DW_OP_lit23:
22808 case DW_OP_lit24:
22809 case DW_OP_lit25:
22810 case DW_OP_lit26:
22811 case DW_OP_lit27:
22812 case DW_OP_lit28:
22813 case DW_OP_lit29:
22814 case DW_OP_lit30:
22815 case DW_OP_lit31:
22816 stack[++stacki] = op - DW_OP_lit0;
22817 break;
22818
22819 case DW_OP_reg0:
22820 case DW_OP_reg1:
22821 case DW_OP_reg2:
22822 case DW_OP_reg3:
22823 case DW_OP_reg4:
22824 case DW_OP_reg5:
22825 case DW_OP_reg6:
22826 case DW_OP_reg7:
22827 case DW_OP_reg8:
22828 case DW_OP_reg9:
22829 case DW_OP_reg10:
22830 case DW_OP_reg11:
22831 case DW_OP_reg12:
22832 case DW_OP_reg13:
22833 case DW_OP_reg14:
22834 case DW_OP_reg15:
22835 case DW_OP_reg16:
22836 case DW_OP_reg17:
22837 case DW_OP_reg18:
22838 case DW_OP_reg19:
22839 case DW_OP_reg20:
22840 case DW_OP_reg21:
22841 case DW_OP_reg22:
22842 case DW_OP_reg23:
22843 case DW_OP_reg24:
22844 case DW_OP_reg25:
22845 case DW_OP_reg26:
22846 case DW_OP_reg27:
22847 case DW_OP_reg28:
22848 case DW_OP_reg29:
22849 case DW_OP_reg30:
22850 case DW_OP_reg31:
22851 stack[++stacki] = op - DW_OP_reg0;
22852 if (i < size)
22853 dwarf2_complex_location_expr_complaint ();
22854 break;
22855
22856 case DW_OP_regx:
22857 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22858 i += bytes_read;
22859 stack[++stacki] = unsnd;
22860 if (i < size)
22861 dwarf2_complex_location_expr_complaint ();
22862 break;
22863
22864 case DW_OP_addr:
22865 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22866 &bytes_read);
22867 i += bytes_read;
22868 break;
22869
22870 case DW_OP_const1u:
22871 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22872 i += 1;
22873 break;
22874
22875 case DW_OP_const1s:
22876 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22877 i += 1;
22878 break;
22879
22880 case DW_OP_const2u:
22881 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22882 i += 2;
22883 break;
22884
22885 case DW_OP_const2s:
22886 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22887 i += 2;
22888 break;
22889
22890 case DW_OP_const4u:
22891 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22892 i += 4;
22893 break;
22894
22895 case DW_OP_const4s:
22896 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22897 i += 4;
22898 break;
22899
22900 case DW_OP_const8u:
22901 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22902 i += 8;
22903 break;
22904
22905 case DW_OP_constu:
22906 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22907 &bytes_read);
22908 i += bytes_read;
22909 break;
22910
22911 case DW_OP_consts:
22912 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22913 i += bytes_read;
22914 break;
22915
22916 case DW_OP_dup:
22917 stack[stacki + 1] = stack[stacki];
22918 stacki++;
22919 break;
22920
22921 case DW_OP_plus:
22922 stack[stacki - 1] += stack[stacki];
22923 stacki--;
22924 break;
22925
22926 case DW_OP_plus_uconst:
22927 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22928 &bytes_read);
22929 i += bytes_read;
22930 break;
22931
22932 case DW_OP_minus:
22933 stack[stacki - 1] -= stack[stacki];
22934 stacki--;
22935 break;
22936
22937 case DW_OP_deref:
22938 /* If we're not the last op, then we definitely can't encode
22939 this using GDB's address_class enum. This is valid for partial
22940 global symbols, although the variable's address will be bogus
22941 in the psymtab. */
22942 if (i < size)
22943 dwarf2_complex_location_expr_complaint ();
22944 break;
22945
22946 case DW_OP_GNU_push_tls_address:
22947 case DW_OP_form_tls_address:
22948 /* The top of the stack has the offset from the beginning
22949 of the thread control block at which the variable is located. */
22950 /* Nothing should follow this operator, so the top of stack would
22951 be returned. */
22952 /* This is valid for partial global symbols, but the variable's
22953 address will be bogus in the psymtab. Make it always at least
22954 non-zero to not look as a variable garbage collected by linker
22955 which have DW_OP_addr 0. */
22956 if (i < size)
22957 dwarf2_complex_location_expr_complaint ();
22958 stack[stacki]++;
22959 break;
22960
22961 case DW_OP_GNU_uninit:
22962 break;
22963
22964 case DW_OP_addrx:
22965 case DW_OP_GNU_addr_index:
22966 case DW_OP_GNU_const_index:
22967 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22968 &bytes_read);
22969 i += bytes_read;
22970 break;
22971
22972 default:
22973 {
22974 const char *name = get_DW_OP_name (op);
22975
22976 if (name)
22977 complaint (_("unsupported stack op: '%s'"),
22978 name);
22979 else
22980 complaint (_("unsupported stack op: '%02x'"),
22981 op);
22982 }
22983
22984 return (stack[stacki]);
22985 }
22986
22987 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22988 outside of the allocated space. Also enforce minimum>0. */
22989 if (stacki >= ARRAY_SIZE (stack) - 1)
22990 {
22991 complaint (_("location description stack overflow"));
22992 return 0;
22993 }
22994
22995 if (stacki <= 0)
22996 {
22997 complaint (_("location description stack underflow"));
22998 return 0;
22999 }
23000 }
23001 return (stack[stacki]);
23002 }
23003
23004 /* memory allocation interface */
23005
23006 static struct dwarf_block *
23007 dwarf_alloc_block (struct dwarf2_cu *cu)
23008 {
23009 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23010 }
23011
23012 static struct die_info *
23013 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23014 {
23015 struct die_info *die;
23016 size_t size = sizeof (struct die_info);
23017
23018 if (num_attrs > 1)
23019 size += (num_attrs - 1) * sizeof (struct attribute);
23020
23021 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23022 memset (die, 0, sizeof (struct die_info));
23023 return (die);
23024 }
23025
23026 \f
23027
23028 /* Macro support. */
23029
23030 /* An overload of dwarf_decode_macros that finds the correct section
23031 and ensures it is read in before calling the other overload. */
23032
23033 static void
23034 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23035 int section_is_gnu)
23036 {
23037 struct dwarf2_per_objfile *dwarf2_per_objfile
23038 = cu->per_cu->dwarf2_per_objfile;
23039 struct objfile *objfile = dwarf2_per_objfile->objfile;
23040 const struct line_header *lh = cu->line_header;
23041 unsigned int offset_size = cu->header.offset_size;
23042 struct dwarf2_section_info *section;
23043 const char *section_name;
23044
23045 if (cu->dwo_unit != nullptr)
23046 {
23047 if (section_is_gnu)
23048 {
23049 section = &cu->dwo_unit->dwo_file->sections.macro;
23050 section_name = ".debug_macro.dwo";
23051 }
23052 else
23053 {
23054 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23055 section_name = ".debug_macinfo.dwo";
23056 }
23057 }
23058 else
23059 {
23060 if (section_is_gnu)
23061 {
23062 section = &dwarf2_per_objfile->macro;
23063 section_name = ".debug_macro";
23064 }
23065 else
23066 {
23067 section = &dwarf2_per_objfile->macinfo;
23068 section_name = ".debug_macinfo";
23069 }
23070 }
23071
23072 section->read (objfile);
23073 if (section->buffer == nullptr)
23074 {
23075 complaint (_("missing %s section"), section_name);
23076 return;
23077 }
23078
23079 buildsym_compunit *builder = cu->get_builder ();
23080
23081 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23082 offset_size, offset, section_is_gnu);
23083 }
23084
23085 /* Return the .debug_loc section to use for CU.
23086 For DWO files use .debug_loc.dwo. */
23087
23088 static struct dwarf2_section_info *
23089 cu_debug_loc_section (struct dwarf2_cu *cu)
23090 {
23091 struct dwarf2_per_objfile *dwarf2_per_objfile
23092 = cu->per_cu->dwarf2_per_objfile;
23093
23094 if (cu->dwo_unit)
23095 {
23096 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23097
23098 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23099 }
23100 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23101 : &dwarf2_per_objfile->loc);
23102 }
23103
23104 /* A helper function that fills in a dwarf2_loclist_baton. */
23105
23106 static void
23107 fill_in_loclist_baton (struct dwarf2_cu *cu,
23108 struct dwarf2_loclist_baton *baton,
23109 const struct attribute *attr)
23110 {
23111 struct dwarf2_per_objfile *dwarf2_per_objfile
23112 = cu->per_cu->dwarf2_per_objfile;
23113 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23114
23115 section->read (dwarf2_per_objfile->objfile);
23116
23117 baton->per_cu = cu->per_cu;
23118 gdb_assert (baton->per_cu);
23119 /* We don't know how long the location list is, but make sure we
23120 don't run off the edge of the section. */
23121 baton->size = section->size - DW_UNSND (attr);
23122 baton->data = section->buffer + DW_UNSND (attr);
23123 baton->base_address = cu->base_address;
23124 baton->from_dwo = cu->dwo_unit != NULL;
23125 }
23126
23127 static void
23128 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23129 struct dwarf2_cu *cu, int is_block)
23130 {
23131 struct dwarf2_per_objfile *dwarf2_per_objfile
23132 = cu->per_cu->dwarf2_per_objfile;
23133 struct objfile *objfile = dwarf2_per_objfile->objfile;
23134 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23135
23136 if (attr->form_is_section_offset ()
23137 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23138 the section. If so, fall through to the complaint in the
23139 other branch. */
23140 && DW_UNSND (attr) < section->get_size (objfile))
23141 {
23142 struct dwarf2_loclist_baton *baton;
23143
23144 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23145
23146 fill_in_loclist_baton (cu, baton, attr);
23147
23148 if (cu->base_known == 0)
23149 complaint (_("Location list used without "
23150 "specifying the CU base address."));
23151
23152 SYMBOL_ACLASS_INDEX (sym) = (is_block
23153 ? dwarf2_loclist_block_index
23154 : dwarf2_loclist_index);
23155 SYMBOL_LOCATION_BATON (sym) = baton;
23156 }
23157 else
23158 {
23159 struct dwarf2_locexpr_baton *baton;
23160
23161 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23162 baton->per_cu = cu->per_cu;
23163 gdb_assert (baton->per_cu);
23164
23165 if (attr->form_is_block ())
23166 {
23167 /* Note that we're just copying the block's data pointer
23168 here, not the actual data. We're still pointing into the
23169 info_buffer for SYM's objfile; right now we never release
23170 that buffer, but when we do clean up properly this may
23171 need to change. */
23172 baton->size = DW_BLOCK (attr)->size;
23173 baton->data = DW_BLOCK (attr)->data;
23174 }
23175 else
23176 {
23177 dwarf2_invalid_attrib_class_complaint ("location description",
23178 sym->natural_name ());
23179 baton->size = 0;
23180 }
23181
23182 SYMBOL_ACLASS_INDEX (sym) = (is_block
23183 ? dwarf2_locexpr_block_index
23184 : dwarf2_locexpr_index);
23185 SYMBOL_LOCATION_BATON (sym) = baton;
23186 }
23187 }
23188
23189 /* See read.h. */
23190
23191 struct objfile *
23192 dwarf2_per_cu_data::objfile () const
23193 {
23194 struct objfile *objfile = dwarf2_per_objfile->objfile;
23195
23196 /* Return the master objfile, so that we can report and look up the
23197 correct file containing this variable. */
23198 if (objfile->separate_debug_objfile_backlink)
23199 objfile = objfile->separate_debug_objfile_backlink;
23200
23201 return objfile;
23202 }
23203
23204 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23205 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23206 CU_HEADERP first. */
23207
23208 static const struct comp_unit_head *
23209 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23210 const struct dwarf2_per_cu_data *per_cu)
23211 {
23212 const gdb_byte *info_ptr;
23213
23214 if (per_cu->cu)
23215 return &per_cu->cu->header;
23216
23217 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23218
23219 memset (cu_headerp, 0, sizeof (*cu_headerp));
23220 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23221 rcuh_kind::COMPILE);
23222
23223 return cu_headerp;
23224 }
23225
23226 /* See read.h. */
23227
23228 int
23229 dwarf2_per_cu_data::addr_size () const
23230 {
23231 struct comp_unit_head cu_header_local;
23232 const struct comp_unit_head *cu_headerp;
23233
23234 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23235
23236 return cu_headerp->addr_size;
23237 }
23238
23239 /* See read.h. */
23240
23241 int
23242 dwarf2_per_cu_data::offset_size () const
23243 {
23244 struct comp_unit_head cu_header_local;
23245 const struct comp_unit_head *cu_headerp;
23246
23247 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23248
23249 return cu_headerp->offset_size;
23250 }
23251
23252 /* See read.h. */
23253
23254 int
23255 dwarf2_per_cu_data::ref_addr_size () const
23256 {
23257 struct comp_unit_head cu_header_local;
23258 const struct comp_unit_head *cu_headerp;
23259
23260 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23261
23262 if (cu_headerp->version == 2)
23263 return cu_headerp->addr_size;
23264 else
23265 return cu_headerp->offset_size;
23266 }
23267
23268 /* See read.h. */
23269
23270 CORE_ADDR
23271 dwarf2_per_cu_data::text_offset () const
23272 {
23273 struct objfile *objfile = dwarf2_per_objfile->objfile;
23274
23275 return objfile->text_section_offset ();
23276 }
23277
23278 /* See read.h. */
23279
23280 struct type *
23281 dwarf2_per_cu_data::addr_type () const
23282 {
23283 struct objfile *objfile = dwarf2_per_objfile->objfile;
23284 struct type *void_type = objfile_type (objfile)->builtin_void;
23285 struct type *addr_type = lookup_pointer_type (void_type);
23286 int addr_size = this->addr_size ();
23287
23288 if (TYPE_LENGTH (addr_type) == addr_size)
23289 return addr_type;
23290
23291 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23292 return addr_type;
23293 }
23294
23295 /* A helper function for dwarf2_find_containing_comp_unit that returns
23296 the index of the result, and that searches a vector. It will
23297 return a result even if the offset in question does not actually
23298 occur in any CU. This is separate so that it can be unit
23299 tested. */
23300
23301 static int
23302 dwarf2_find_containing_comp_unit
23303 (sect_offset sect_off,
23304 unsigned int offset_in_dwz,
23305 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23306 {
23307 int low, high;
23308
23309 low = 0;
23310 high = all_comp_units.size () - 1;
23311 while (high > low)
23312 {
23313 struct dwarf2_per_cu_data *mid_cu;
23314 int mid = low + (high - low) / 2;
23315
23316 mid_cu = all_comp_units[mid];
23317 if (mid_cu->is_dwz > offset_in_dwz
23318 || (mid_cu->is_dwz == offset_in_dwz
23319 && mid_cu->sect_off + mid_cu->length > sect_off))
23320 high = mid;
23321 else
23322 low = mid + 1;
23323 }
23324 gdb_assert (low == high);
23325 return low;
23326 }
23327
23328 /* Locate the .debug_info compilation unit from CU's objfile which contains
23329 the DIE at OFFSET. Raises an error on failure. */
23330
23331 static struct dwarf2_per_cu_data *
23332 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23333 unsigned int offset_in_dwz,
23334 struct dwarf2_per_objfile *dwarf2_per_objfile)
23335 {
23336 int low
23337 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23338 dwarf2_per_objfile->all_comp_units);
23339 struct dwarf2_per_cu_data *this_cu
23340 = dwarf2_per_objfile->all_comp_units[low];
23341
23342 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23343 {
23344 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23345 error (_("Dwarf Error: could not find partial DIE containing "
23346 "offset %s [in module %s]"),
23347 sect_offset_str (sect_off),
23348 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23349
23350 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23351 <= sect_off);
23352 return dwarf2_per_objfile->all_comp_units[low-1];
23353 }
23354 else
23355 {
23356 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
23357 && sect_off >= this_cu->sect_off + this_cu->length)
23358 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23359 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23360 return this_cu;
23361 }
23362 }
23363
23364 #if GDB_SELF_TEST
23365
23366 namespace selftests {
23367 namespace find_containing_comp_unit {
23368
23369 static void
23370 run_test ()
23371 {
23372 struct dwarf2_per_cu_data one {};
23373 struct dwarf2_per_cu_data two {};
23374 struct dwarf2_per_cu_data three {};
23375 struct dwarf2_per_cu_data four {};
23376
23377 one.length = 5;
23378 two.sect_off = sect_offset (one.length);
23379 two.length = 7;
23380
23381 three.length = 5;
23382 three.is_dwz = 1;
23383 four.sect_off = sect_offset (three.length);
23384 four.length = 7;
23385 four.is_dwz = 1;
23386
23387 std::vector<dwarf2_per_cu_data *> units;
23388 units.push_back (&one);
23389 units.push_back (&two);
23390 units.push_back (&three);
23391 units.push_back (&four);
23392
23393 int result;
23394
23395 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23396 SELF_CHECK (units[result] == &one);
23397 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23398 SELF_CHECK (units[result] == &one);
23399 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23400 SELF_CHECK (units[result] == &two);
23401
23402 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23403 SELF_CHECK (units[result] == &three);
23404 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23405 SELF_CHECK (units[result] == &three);
23406 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23407 SELF_CHECK (units[result] == &four);
23408 }
23409
23410 }
23411 }
23412
23413 #endif /* GDB_SELF_TEST */
23414
23415 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23416
23417 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23418 : per_cu (per_cu_),
23419 mark (false),
23420 has_loclist (false),
23421 checked_producer (false),
23422 producer_is_gxx_lt_4_6 (false),
23423 producer_is_gcc_lt_4_3 (false),
23424 producer_is_icc (false),
23425 producer_is_icc_lt_14 (false),
23426 producer_is_codewarrior (false),
23427 processing_has_namespace_info (false)
23428 {
23429 per_cu->cu = this;
23430 }
23431
23432 /* Destroy a dwarf2_cu. */
23433
23434 dwarf2_cu::~dwarf2_cu ()
23435 {
23436 per_cu->cu = NULL;
23437 }
23438
23439 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23440
23441 static void
23442 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23443 enum language pretend_language)
23444 {
23445 struct attribute *attr;
23446
23447 /* Set the language we're debugging. */
23448 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23449 if (attr != nullptr)
23450 set_cu_language (DW_UNSND (attr), cu);
23451 else
23452 {
23453 cu->language = pretend_language;
23454 cu->language_defn = language_def (cu->language);
23455 }
23456
23457 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23458 }
23459
23460 /* Increase the age counter on each cached compilation unit, and free
23461 any that are too old. */
23462
23463 static void
23464 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23465 {
23466 struct dwarf2_per_cu_data *per_cu, **last_chain;
23467
23468 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23469 per_cu = dwarf2_per_objfile->read_in_chain;
23470 while (per_cu != NULL)
23471 {
23472 per_cu->cu->last_used ++;
23473 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23474 dwarf2_mark (per_cu->cu);
23475 per_cu = per_cu->cu->read_in_chain;
23476 }
23477
23478 per_cu = dwarf2_per_objfile->read_in_chain;
23479 last_chain = &dwarf2_per_objfile->read_in_chain;
23480 while (per_cu != NULL)
23481 {
23482 struct dwarf2_per_cu_data *next_cu;
23483
23484 next_cu = per_cu->cu->read_in_chain;
23485
23486 if (!per_cu->cu->mark)
23487 {
23488 delete per_cu->cu;
23489 *last_chain = next_cu;
23490 }
23491 else
23492 last_chain = &per_cu->cu->read_in_chain;
23493
23494 per_cu = next_cu;
23495 }
23496 }
23497
23498 /* Remove a single compilation unit from the cache. */
23499
23500 static void
23501 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23502 {
23503 struct dwarf2_per_cu_data *per_cu, **last_chain;
23504 struct dwarf2_per_objfile *dwarf2_per_objfile
23505 = target_per_cu->dwarf2_per_objfile;
23506
23507 per_cu = dwarf2_per_objfile->read_in_chain;
23508 last_chain = &dwarf2_per_objfile->read_in_chain;
23509 while (per_cu != NULL)
23510 {
23511 struct dwarf2_per_cu_data *next_cu;
23512
23513 next_cu = per_cu->cu->read_in_chain;
23514
23515 if (per_cu == target_per_cu)
23516 {
23517 delete per_cu->cu;
23518 per_cu->cu = NULL;
23519 *last_chain = next_cu;
23520 break;
23521 }
23522 else
23523 last_chain = &per_cu->cu->read_in_chain;
23524
23525 per_cu = next_cu;
23526 }
23527 }
23528
23529 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23530 We store these in a hash table separate from the DIEs, and preserve them
23531 when the DIEs are flushed out of cache.
23532
23533 The CU "per_cu" pointer is needed because offset alone is not enough to
23534 uniquely identify the type. A file may have multiple .debug_types sections,
23535 or the type may come from a DWO file. Furthermore, while it's more logical
23536 to use per_cu->section+offset, with Fission the section with the data is in
23537 the DWO file but we don't know that section at the point we need it.
23538 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23539 because we can enter the lookup routine, get_die_type_at_offset, from
23540 outside this file, and thus won't necessarily have PER_CU->cu.
23541 Fortunately, PER_CU is stable for the life of the objfile. */
23542
23543 struct dwarf2_per_cu_offset_and_type
23544 {
23545 const struct dwarf2_per_cu_data *per_cu;
23546 sect_offset sect_off;
23547 struct type *type;
23548 };
23549
23550 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23551
23552 static hashval_t
23553 per_cu_offset_and_type_hash (const void *item)
23554 {
23555 const struct dwarf2_per_cu_offset_and_type *ofs
23556 = (const struct dwarf2_per_cu_offset_and_type *) item;
23557
23558 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23559 }
23560
23561 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23562
23563 static int
23564 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23565 {
23566 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23567 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23568 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23569 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23570
23571 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23572 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23573 }
23574
23575 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23576 table if necessary. For convenience, return TYPE.
23577
23578 The DIEs reading must have careful ordering to:
23579 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23580 reading current DIE.
23581 * Not trying to dereference contents of still incompletely read in types
23582 while reading in other DIEs.
23583 * Enable referencing still incompletely read in types just by a pointer to
23584 the type without accessing its fields.
23585
23586 Therefore caller should follow these rules:
23587 * Try to fetch any prerequisite types we may need to build this DIE type
23588 before building the type and calling set_die_type.
23589 * After building type call set_die_type for current DIE as soon as
23590 possible before fetching more types to complete the current type.
23591 * Make the type as complete as possible before fetching more types. */
23592
23593 static struct type *
23594 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23595 {
23596 struct dwarf2_per_objfile *dwarf2_per_objfile
23597 = cu->per_cu->dwarf2_per_objfile;
23598 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23599 struct objfile *objfile = dwarf2_per_objfile->objfile;
23600 struct attribute *attr;
23601 struct dynamic_prop prop;
23602
23603 /* For Ada types, make sure that the gnat-specific data is always
23604 initialized (if not already set). There are a few types where
23605 we should not be doing so, because the type-specific area is
23606 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23607 where the type-specific area is used to store the floatformat).
23608 But this is not a problem, because the gnat-specific information
23609 is actually not needed for these types. */
23610 if (need_gnat_info (cu)
23611 && TYPE_CODE (type) != TYPE_CODE_FUNC
23612 && TYPE_CODE (type) != TYPE_CODE_FLT
23613 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23614 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23615 && TYPE_CODE (type) != TYPE_CODE_METHOD
23616 && !HAVE_GNAT_AUX_INFO (type))
23617 INIT_GNAT_SPECIFIC (type);
23618
23619 /* Read DW_AT_allocated and set in type. */
23620 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23621 if (attr != NULL && attr->form_is_block ())
23622 {
23623 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23624 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23625 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23626 }
23627 else if (attr != NULL)
23628 {
23629 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23630 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23631 sect_offset_str (die->sect_off));
23632 }
23633
23634 /* Read DW_AT_associated and set in type. */
23635 attr = dwarf2_attr (die, DW_AT_associated, cu);
23636 if (attr != NULL && attr->form_is_block ())
23637 {
23638 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23639 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23640 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23641 }
23642 else if (attr != NULL)
23643 {
23644 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23645 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23646 sect_offset_str (die->sect_off));
23647 }
23648
23649 /* Read DW_AT_data_location and set in type. */
23650 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23651 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23652 cu->per_cu->addr_type ()))
23653 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23654
23655 if (dwarf2_per_objfile->die_type_hash == NULL)
23656 dwarf2_per_objfile->die_type_hash
23657 = htab_up (htab_create_alloc (127,
23658 per_cu_offset_and_type_hash,
23659 per_cu_offset_and_type_eq,
23660 NULL, xcalloc, xfree));
23661
23662 ofs.per_cu = cu->per_cu;
23663 ofs.sect_off = die->sect_off;
23664 ofs.type = type;
23665 slot = (struct dwarf2_per_cu_offset_and_type **)
23666 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23667 if (*slot)
23668 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23669 sect_offset_str (die->sect_off));
23670 *slot = XOBNEW (&objfile->objfile_obstack,
23671 struct dwarf2_per_cu_offset_and_type);
23672 **slot = ofs;
23673 return type;
23674 }
23675
23676 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23677 or return NULL if the die does not have a saved type. */
23678
23679 static struct type *
23680 get_die_type_at_offset (sect_offset sect_off,
23681 struct dwarf2_per_cu_data *per_cu)
23682 {
23683 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23684 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23685
23686 if (dwarf2_per_objfile->die_type_hash == NULL)
23687 return NULL;
23688
23689 ofs.per_cu = per_cu;
23690 ofs.sect_off = sect_off;
23691 slot = ((struct dwarf2_per_cu_offset_and_type *)
23692 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23693 if (slot)
23694 return slot->type;
23695 else
23696 return NULL;
23697 }
23698
23699 /* Look up the type for DIE in CU in die_type_hash,
23700 or return NULL if DIE does not have a saved type. */
23701
23702 static struct type *
23703 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23704 {
23705 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23706 }
23707
23708 /* Add a dependence relationship from CU to REF_PER_CU. */
23709
23710 static void
23711 dwarf2_add_dependence (struct dwarf2_cu *cu,
23712 struct dwarf2_per_cu_data *ref_per_cu)
23713 {
23714 void **slot;
23715
23716 if (cu->dependencies == NULL)
23717 cu->dependencies
23718 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23719 NULL, &cu->comp_unit_obstack,
23720 hashtab_obstack_allocate,
23721 dummy_obstack_deallocate);
23722
23723 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23724 if (*slot == NULL)
23725 *slot = ref_per_cu;
23726 }
23727
23728 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23729 Set the mark field in every compilation unit in the
23730 cache that we must keep because we are keeping CU. */
23731
23732 static int
23733 dwarf2_mark_helper (void **slot, void *data)
23734 {
23735 struct dwarf2_per_cu_data *per_cu;
23736
23737 per_cu = (struct dwarf2_per_cu_data *) *slot;
23738
23739 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23740 reading of the chain. As such dependencies remain valid it is not much
23741 useful to track and undo them during QUIT cleanups. */
23742 if (per_cu->cu == NULL)
23743 return 1;
23744
23745 if (per_cu->cu->mark)
23746 return 1;
23747 per_cu->cu->mark = true;
23748
23749 if (per_cu->cu->dependencies != NULL)
23750 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23751
23752 return 1;
23753 }
23754
23755 /* Set the mark field in CU and in every other compilation unit in the
23756 cache that we must keep because we are keeping CU. */
23757
23758 static void
23759 dwarf2_mark (struct dwarf2_cu *cu)
23760 {
23761 if (cu->mark)
23762 return;
23763 cu->mark = true;
23764 if (cu->dependencies != NULL)
23765 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23766 }
23767
23768 static void
23769 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23770 {
23771 while (per_cu)
23772 {
23773 per_cu->cu->mark = false;
23774 per_cu = per_cu->cu->read_in_chain;
23775 }
23776 }
23777
23778 /* Trivial hash function for partial_die_info: the hash value of a DIE
23779 is its offset in .debug_info for this objfile. */
23780
23781 static hashval_t
23782 partial_die_hash (const void *item)
23783 {
23784 const struct partial_die_info *part_die
23785 = (const struct partial_die_info *) item;
23786
23787 return to_underlying (part_die->sect_off);
23788 }
23789
23790 /* Trivial comparison function for partial_die_info structures: two DIEs
23791 are equal if they have the same offset. */
23792
23793 static int
23794 partial_die_eq (const void *item_lhs, const void *item_rhs)
23795 {
23796 const struct partial_die_info *part_die_lhs
23797 = (const struct partial_die_info *) item_lhs;
23798 const struct partial_die_info *part_die_rhs
23799 = (const struct partial_die_info *) item_rhs;
23800
23801 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23802 }
23803
23804 struct cmd_list_element *set_dwarf_cmdlist;
23805 struct cmd_list_element *show_dwarf_cmdlist;
23806
23807 static void
23808 set_dwarf_cmd (const char *args, int from_tty)
23809 {
23810 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23811 gdb_stdout);
23812 }
23813
23814 static void
23815 show_dwarf_cmd (const char *args, int from_tty)
23816 {
23817 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23818 }
23819
23820 static void
23821 show_check_physname (struct ui_file *file, int from_tty,
23822 struct cmd_list_element *c, const char *value)
23823 {
23824 fprintf_filtered (file,
23825 _("Whether to check \"physname\" is %s.\n"),
23826 value);
23827 }
23828
23829 void _initialize_dwarf2_read ();
23830 void
23831 _initialize_dwarf2_read ()
23832 {
23833 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23834 Set DWARF specific variables.\n\
23835 Configure DWARF variables such as the cache size."),
23836 &set_dwarf_cmdlist, "maintenance set dwarf ",
23837 0/*allow-unknown*/, &maintenance_set_cmdlist);
23838
23839 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23840 Show DWARF specific variables.\n\
23841 Show DWARF variables such as the cache size."),
23842 &show_dwarf_cmdlist, "maintenance show dwarf ",
23843 0/*allow-unknown*/, &maintenance_show_cmdlist);
23844
23845 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23846 &dwarf_max_cache_age, _("\
23847 Set the upper bound on the age of cached DWARF compilation units."), _("\
23848 Show the upper bound on the age of cached DWARF compilation units."), _("\
23849 A higher limit means that cached compilation units will be stored\n\
23850 in memory longer, and more total memory will be used. Zero disables\n\
23851 caching, which can slow down startup."),
23852 NULL,
23853 show_dwarf_max_cache_age,
23854 &set_dwarf_cmdlist,
23855 &show_dwarf_cmdlist);
23856
23857 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23858 Set debugging of the DWARF reader."), _("\
23859 Show debugging of the DWARF reader."), _("\
23860 When enabled (non-zero), debugging messages are printed during DWARF\n\
23861 reading and symtab expansion. A value of 1 (one) provides basic\n\
23862 information. A value greater than 1 provides more verbose information."),
23863 NULL,
23864 NULL,
23865 &setdebuglist, &showdebuglist);
23866
23867 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23868 Set debugging of the DWARF DIE reader."), _("\
23869 Show debugging of the DWARF DIE reader."), _("\
23870 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23871 The value is the maximum depth to print."),
23872 NULL,
23873 NULL,
23874 &setdebuglist, &showdebuglist);
23875
23876 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23877 Set debugging of the dwarf line reader."), _("\
23878 Show debugging of the dwarf line reader."), _("\
23879 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23880 A value of 1 (one) provides basic information.\n\
23881 A value greater than 1 provides more verbose information."),
23882 NULL,
23883 NULL,
23884 &setdebuglist, &showdebuglist);
23885
23886 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23887 Set cross-checking of \"physname\" code against demangler."), _("\
23888 Show cross-checking of \"physname\" code against demangler."), _("\
23889 When enabled, GDB's internal \"physname\" code is checked against\n\
23890 the demangler."),
23891 NULL, show_check_physname,
23892 &setdebuglist, &showdebuglist);
23893
23894 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23895 no_class, &use_deprecated_index_sections, _("\
23896 Set whether to use deprecated gdb_index sections."), _("\
23897 Show whether to use deprecated gdb_index sections."), _("\
23898 When enabled, deprecated .gdb_index sections are used anyway.\n\
23899 Normally they are ignored either because of a missing feature or\n\
23900 performance issue.\n\
23901 Warning: This option must be enabled before gdb reads the file."),
23902 NULL,
23903 NULL,
23904 &setlist, &showlist);
23905
23906 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23907 &dwarf2_locexpr_funcs);
23908 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23909 &dwarf2_loclist_funcs);
23910
23911 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23912 &dwarf2_block_frame_base_locexpr_funcs);
23913 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23914 &dwarf2_block_frame_base_loclist_funcs);
23915
23916 #if GDB_SELF_TEST
23917 selftests::register_test ("dw2_expand_symtabs_matching",
23918 selftests::dw2_expand_symtabs_matching::run_test);
23919 selftests::register_test ("dwarf2_find_containing_comp_unit",
23920 selftests::find_containing_comp_unit::run_test);
23921 #endif
23922 }
This page took 0.507876 seconds and 5 git commands to generate.