Remove "keep" parameter from cutu_reader constructor
[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 "bfd.h"
41 #include "elf-bfd.h"
42 #include "symtab.h"
43 #include "gdbtypes.h"
44 #include "objfiles.h"
45 #include "dwarf2.h"
46 #include "buildsym.h"
47 #include "demangle.h"
48 #include "gdb-demangle.h"
49 #include "filenames.h" /* for DOSish file names */
50 #include "macrotab.h"
51 #include "language.h"
52 #include "complaints.h"
53 #include "dwarf2/expr.h"
54 #include "dwarf2/loc.h"
55 #include "cp-support.h"
56 #include "hashtab.h"
57 #include "command.h"
58 #include "gdbcmd.h"
59 #include "block.h"
60 #include "addrmap.h"
61 #include "typeprint.h"
62 #include "psympriv.h"
63 #include "c-lang.h"
64 #include "go-lang.h"
65 #include "valprint.h"
66 #include "gdbcore.h" /* for gnutarget */
67 #include "gdb/gdb-index.h"
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "build-id.h"
72 #include "namespace.h"
73 #include "gdbsupport/function-view.h"
74 #include "gdbsupport/gdb_optional.h"
75 #include "gdbsupport/underlying.h"
76 #include "gdbsupport/hash_enum.h"
77 #include "filename-seen-cache.h"
78 #include "producer.h"
79 #include <fcntl.h>
80 #include <algorithm>
81 #include <unordered_map>
82 #include "gdbsupport/selftest.h"
83 #include "rust-lang.h"
84 #include "gdbsupport/pathstuff.h"
85
86 /* When == 1, print basic high level tracing messages.
87 When > 1, be more verbose.
88 This is in contrast to the low level DIE reading of dwarf_die_debug. */
89 static unsigned int dwarf_read_debug = 0;
90
91 /* When non-zero, dump DIEs after they are read in. */
92 static unsigned int dwarf_die_debug = 0;
93
94 /* When non-zero, dump line number entries as they are read in. */
95 unsigned int dwarf_line_debug = 0;
96
97 /* When true, cross-check physname against demangler. */
98 static bool check_physname = false;
99
100 /* When true, do not reject deprecated .gdb_index sections. */
101 static bool use_deprecated_index_sections = false;
102
103 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
104
105 /* The "aclass" indices for various kinds of computed DWARF symbols. */
106
107 static int dwarf2_locexpr_index;
108 static int dwarf2_loclist_index;
109 static int dwarf2_locexpr_block_index;
110 static int dwarf2_loclist_block_index;
111
112 /* An index into a (C++) symbol name component in a symbol name as
113 recorded in the mapped_index's symbol table. For each C++ symbol
114 in the symbol table, we record one entry for the start of each
115 component in the symbol in a table of name components, and then
116 sort the table, in order to be able to binary search symbol names,
117 ignoring leading namespaces, both completion and regular look up.
118 For example, for symbol "A::B::C", we'll have an entry that points
119 to "A::B::C", another that points to "B::C", and another for "C".
120 Note that function symbols in GDB index have no parameter
121 information, just the function/method names. You can convert a
122 name_component to a "const char *" using the
123 'mapped_index::symbol_name_at(offset_type)' method. */
124
125 struct name_component
126 {
127 /* Offset in the symbol name where the component starts. Stored as
128 a (32-bit) offset instead of a pointer to save memory and improve
129 locality on 64-bit architectures. */
130 offset_type name_offset;
131
132 /* The symbol's index in the symbol and constant pool tables of a
133 mapped_index. */
134 offset_type idx;
135 };
136
137 /* Base class containing bits shared by both .gdb_index and
138 .debug_name indexes. */
139
140 struct mapped_index_base
141 {
142 mapped_index_base () = default;
143 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
144
145 /* The name_component table (a sorted vector). See name_component's
146 description above. */
147 std::vector<name_component> name_components;
148
149 /* How NAME_COMPONENTS is sorted. */
150 enum case_sensitivity name_components_casing;
151
152 /* Return the number of names in the symbol table. */
153 virtual size_t symbol_name_count () const = 0;
154
155 /* Get the name of the symbol at IDX in the symbol table. */
156 virtual const char *symbol_name_at (offset_type idx) const = 0;
157
158 /* Return whether the name at IDX in the symbol table should be
159 ignored. */
160 virtual bool symbol_name_slot_invalid (offset_type idx) const
161 {
162 return false;
163 }
164
165 /* Build the symbol name component sorted vector, if we haven't
166 yet. */
167 void build_name_components ();
168
169 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
170 possible matches for LN_NO_PARAMS in the name component
171 vector. */
172 std::pair<std::vector<name_component>::const_iterator,
173 std::vector<name_component>::const_iterator>
174 find_name_components_bounds (const lookup_name_info &ln_no_params,
175 enum language lang) const;
176
177 /* Prevent deleting/destroying via a base class pointer. */
178 protected:
179 ~mapped_index_base() = default;
180 };
181
182 /* A description of the mapped index. The file format is described in
183 a comment by the code that writes the index. */
184 struct mapped_index final : public mapped_index_base
185 {
186 /* A slot/bucket in the symbol table hash. */
187 struct symbol_table_slot
188 {
189 const offset_type name;
190 const offset_type vec;
191 };
192
193 /* Index data format version. */
194 int version = 0;
195
196 /* The address table data. */
197 gdb::array_view<const gdb_byte> address_table;
198
199 /* The symbol table, implemented as a hash table. */
200 gdb::array_view<symbol_table_slot> symbol_table;
201
202 /* A pointer to the constant pool. */
203 const char *constant_pool = nullptr;
204
205 bool symbol_name_slot_invalid (offset_type idx) const override
206 {
207 const auto &bucket = this->symbol_table[idx];
208 return bucket.name == 0 && bucket.vec == 0;
209 }
210
211 /* Convenience method to get at the name of the symbol at IDX in the
212 symbol table. */
213 const char *symbol_name_at (offset_type idx) const override
214 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
215
216 size_t symbol_name_count () const override
217 { return this->symbol_table.size (); }
218 };
219
220 /* A description of the mapped .debug_names.
221 Uninitialized map has CU_COUNT 0. */
222 struct mapped_debug_names final : public mapped_index_base
223 {
224 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
225 : dwarf2_per_objfile (dwarf2_per_objfile_)
226 {}
227
228 struct dwarf2_per_objfile *dwarf2_per_objfile;
229 bfd_endian dwarf5_byte_order;
230 bool dwarf5_is_dwarf64;
231 bool augmentation_is_gdb;
232 uint8_t offset_size;
233 uint32_t cu_count = 0;
234 uint32_t tu_count, bucket_count, name_count;
235 const gdb_byte *cu_table_reordered, *tu_table_reordered;
236 const uint32_t *bucket_table_reordered, *hash_table_reordered;
237 const gdb_byte *name_table_string_offs_reordered;
238 const gdb_byte *name_table_entry_offs_reordered;
239 const gdb_byte *entry_pool;
240
241 struct index_val
242 {
243 ULONGEST dwarf_tag;
244 struct attr
245 {
246 /* Attribute name DW_IDX_*. */
247 ULONGEST dw_idx;
248
249 /* Attribute form DW_FORM_*. */
250 ULONGEST form;
251
252 /* Value if FORM is DW_FORM_implicit_const. */
253 LONGEST implicit_const;
254 };
255 std::vector<attr> attr_vec;
256 };
257
258 std::unordered_map<ULONGEST, index_val> abbrev_map;
259
260 const char *namei_to_name (uint32_t namei) const;
261
262 /* Implementation of the mapped_index_base virtual interface, for
263 the name_components cache. */
264
265 const char *symbol_name_at (offset_type idx) const override
266 { return namei_to_name (idx); }
267
268 size_t symbol_name_count () const override
269 { return this->name_count; }
270 };
271
272 /* See dwarf2read.h. */
273
274 dwarf2_per_objfile *
275 get_dwarf2_per_objfile (struct objfile *objfile)
276 {
277 return dwarf2_objfile_data_key.get (objfile);
278 }
279
280 /* Default names of the debugging sections. */
281
282 /* Note that if the debugging section has been compressed, it might
283 have a name like .zdebug_info. */
284
285 static const struct dwarf2_debug_sections dwarf2_elf_names =
286 {
287 { ".debug_info", ".zdebug_info" },
288 { ".debug_abbrev", ".zdebug_abbrev" },
289 { ".debug_line", ".zdebug_line" },
290 { ".debug_loc", ".zdebug_loc" },
291 { ".debug_loclists", ".zdebug_loclists" },
292 { ".debug_macinfo", ".zdebug_macinfo" },
293 { ".debug_macro", ".zdebug_macro" },
294 { ".debug_str", ".zdebug_str" },
295 { ".debug_str_offsets", ".zdebug_str_offsets" },
296 { ".debug_line_str", ".zdebug_line_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_rnglists", ".zdebug_rnglists" },
299 { ".debug_types", ".zdebug_types" },
300 { ".debug_addr", ".zdebug_addr" },
301 { ".debug_frame", ".zdebug_frame" },
302 { ".eh_frame", NULL },
303 { ".gdb_index", ".zgdb_index" },
304 { ".debug_names", ".zdebug_names" },
305 { ".debug_aranges", ".zdebug_aranges" },
306 23
307 };
308
309 /* List of DWO/DWP sections. */
310
311 static const struct dwop_section_names
312 {
313 struct dwarf2_section_names abbrev_dwo;
314 struct dwarf2_section_names info_dwo;
315 struct dwarf2_section_names line_dwo;
316 struct dwarf2_section_names loc_dwo;
317 struct dwarf2_section_names loclists_dwo;
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
325 }
326 dwop_section_names =
327 {
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
332 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
333 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
334 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
335 { ".debug_str.dwo", ".zdebug_str.dwo" },
336 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
337 { ".debug_types.dwo", ".zdebug_types.dwo" },
338 { ".debug_cu_index", ".zdebug_cu_index" },
339 { ".debug_tu_index", ".zdebug_tu_index" },
340 };
341
342 /* local data types */
343
344 /* Type used for delaying computation of method physnames.
345 See comments for compute_delayed_physnames. */
346 struct delayed_method_info
347 {
348 /* The type to which the method is attached, i.e., its parent class. */
349 struct type *type;
350
351 /* The index of the method in the type's function fieldlists. */
352 int fnfield_index;
353
354 /* The index of the method in the fieldlist. */
355 int index;
356
357 /* The name of the DIE. */
358 const char *name;
359
360 /* The DIE associated with this method. */
361 struct die_info *die;
362 };
363
364 /* Internal state when decoding a particular compilation unit. */
365 struct dwarf2_cu
366 {
367 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
368 ~dwarf2_cu ();
369
370 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
371
372 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
373 Create the set of symtabs used by this TU, or if this TU is sharing
374 symtabs with another TU and the symtabs have already been created
375 then restore those symtabs in the line header.
376 We don't need the pc/line-number mapping for type units. */
377 void setup_type_unit_groups (struct die_info *die);
378
379 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
380 buildsym_compunit constructor. */
381 struct compunit_symtab *start_symtab (const char *name,
382 const char *comp_dir,
383 CORE_ADDR low_pc);
384
385 /* Reset the builder. */
386 void reset_builder () { m_builder.reset (); }
387
388 /* The header of the compilation unit. */
389 struct comp_unit_head header {};
390
391 /* Base address of this compilation unit. */
392 CORE_ADDR base_address = 0;
393
394 /* Non-zero if base_address has been set. */
395 int base_known = 0;
396
397 /* The language we are debugging. */
398 enum language language = language_unknown;
399 const struct language_defn *language_defn = nullptr;
400
401 const char *producer = nullptr;
402
403 private:
404 /* The symtab builder for this CU. This is only non-NULL when full
405 symbols are being read. */
406 std::unique_ptr<buildsym_compunit> m_builder;
407
408 public:
409 /* The generic symbol table building routines have separate lists for
410 file scope symbols and all all other scopes (local scopes). So
411 we need to select the right one to pass to add_symbol_to_list().
412 We do it by keeping a pointer to the correct list in list_in_scope.
413
414 FIXME: The original dwarf code just treated the file scope as the
415 first local scope, and all other local scopes as nested local
416 scopes, and worked fine. Check to see if we really need to
417 distinguish these in buildsym.c. */
418 struct pending **list_in_scope = nullptr;
419
420 /* Hash table holding all the loaded partial DIEs
421 with partial_die->offset.SECT_OFF as hash. */
422 htab_t partial_dies = nullptr;
423
424 /* Storage for things with the same lifetime as this read-in compilation
425 unit, including partial DIEs. */
426 auto_obstack comp_unit_obstack;
427
428 /* When multiple dwarf2_cu structures are living in memory, this field
429 chains them all together, so that they can be released efficiently.
430 We will probably also want a generation counter so that most-recently-used
431 compilation units are cached... */
432 struct dwarf2_per_cu_data *read_in_chain = nullptr;
433
434 /* Backlink to our per_cu entry. */
435 struct dwarf2_per_cu_data *per_cu;
436
437 /* How many compilation units ago was this CU last referenced? */
438 int last_used = 0;
439
440 /* A hash table of DIE cu_offset for following references with
441 die_info->offset.sect_off as hash. */
442 htab_t die_hash = nullptr;
443
444 /* Full DIEs if read in. */
445 struct die_info *dies = nullptr;
446
447 /* A set of pointers to dwarf2_per_cu_data objects for compilation
448 units referenced by this one. Only set during full symbol processing;
449 partial symbol tables do not have dependencies. */
450 htab_t dependencies = nullptr;
451
452 /* Header data from the line table, during full symbol processing. */
453 struct line_header *line_header = nullptr;
454 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
455 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
456 this is the DW_TAG_compile_unit die for this CU. We'll hold on
457 to the line header as long as this DIE is being processed. See
458 process_die_scope. */
459 die_info *line_header_die_owner = nullptr;
460
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 std::vector<delayed_method_info> method_list;
464
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab = nullptr;
467
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
477 struct dwo_unit *dwo_unit = nullptr;
478
479 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
480 Note this value comes from the Fission stub CU/TU's DIE. */
481 gdb::optional<ULONGEST> addr_base;
482
483 /* The DW_AT_rnglists_base attribute if present.
484 Note this value comes from the Fission stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
486 be used without needing to know whether DWO files are in use or not.
487 N.B. This does not apply to DW_AT_ranges appearing in
488 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
489 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
490 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
491 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
492 ULONGEST ranges_base = 0;
493
494 /* When reading debug info generated by older versions of rustc, we
495 have to rewrite some union types to be struct types with a
496 variant part. This rewriting must be done after the CU is fully
497 read in, because otherwise at the point of rewriting some struct
498 type might not have been fully processed. So, we keep a list of
499 all such types here and process them after expansion. */
500 std::vector<struct type *> rust_unions;
501
502 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
503 files, the value is implicitly zero. For DWARF 5 version DWO files, the
504 value is often implicit and is the size of the header of
505 .debug_str_offsets section (8 or 4, depending on the address size). */
506 gdb::optional<ULONGEST> str_offsets_base;
507
508 /* Mark used when releasing cached dies. */
509 bool mark : 1;
510
511 /* This CU references .debug_loc. See the symtab->locations_valid field.
512 This test is imperfect as there may exist optimized debug code not using
513 any location list and still facing inlining issues if handled as
514 unoptimized code. For a future better test see GCC PR other/32998. */
515 bool has_loclist : 1;
516
517 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
518 if all the producer_is_* fields are valid. This information is cached
519 because profiling CU expansion showed excessive time spent in
520 producer_is_gxx_lt_4_6. */
521 bool checked_producer : 1;
522 bool producer_is_gxx_lt_4_6 : 1;
523 bool producer_is_gcc_lt_4_3 : 1;
524 bool producer_is_icc : 1;
525 bool producer_is_icc_lt_14 : 1;
526 bool producer_is_codewarrior : 1;
527
528 /* When true, the file that we're processing is known to have
529 debugging info for C++ namespaces. GCC 3.3.x did not produce
530 this information, but later versions do. */
531
532 bool processing_has_namespace_info : 1;
533
534 struct partial_die_info *find_partial_die (sect_offset sect_off);
535
536 /* If this CU was inherited by another CU (via specification,
537 abstract_origin, etc), this is the ancestor CU. */
538 dwarf2_cu *ancestor;
539
540 /* Get the buildsym_compunit for this CU. */
541 buildsym_compunit *get_builder ()
542 {
543 /* If this CU has a builder associated with it, use that. */
544 if (m_builder != nullptr)
545 return m_builder.get ();
546
547 /* Otherwise, search ancestors for a valid builder. */
548 if (ancestor != nullptr)
549 return ancestor->get_builder ();
550
551 return nullptr;
552 }
553 };
554
555 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
556 This includes type_unit_group and quick_file_names. */
557
558 struct stmt_list_hash
559 {
560 /* The DWO unit this table is from or NULL if there is none. */
561 struct dwo_unit *dwo_unit;
562
563 /* Offset in .debug_line or .debug_line.dwo. */
564 sect_offset line_sect_off;
565 };
566
567 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
568 an object of this type. */
569
570 struct type_unit_group
571 {
572 /* dwarf2read.c's main "handle" on a TU symtab.
573 To simplify things we create an artificial CU that "includes" all the
574 type units using this stmt_list so that the rest of the code still has
575 a "per_cu" handle on the symtab.
576 This PER_CU is recognized by having no section. */
577 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
578 struct dwarf2_per_cu_data per_cu;
579
580 /* The TUs that share this DW_AT_stmt_list entry.
581 This is added to while parsing type units to build partial symtabs,
582 and is deleted afterwards and not used again. */
583 std::vector<signatured_type *> *tus;
584
585 /* The compunit symtab.
586 Type units in a group needn't all be defined in the same source file,
587 so we create an essentially anonymous symtab as the compunit symtab. */
588 struct compunit_symtab *compunit_symtab;
589
590 /* The data used to construct the hash key. */
591 struct stmt_list_hash hash;
592
593 /* The number of symtabs from the line header.
594 The value here must match line_header.num_file_names. */
595 unsigned int num_symtabs;
596
597 /* The symbol tables for this TU (obtained from the files listed in
598 DW_AT_stmt_list).
599 WARNING: The order of entries here must match the order of entries
600 in the line header. After the first TU using this type_unit_group, the
601 line header for the subsequent TUs is recreated from this. This is done
602 because we need to use the same symtabs for each TU using the same
603 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
604 there's no guarantee the line header doesn't have duplicate entries. */
605 struct symtab **symtabs;
606 };
607
608 /* These sections are what may appear in a (real or virtual) DWO file. */
609
610 struct dwo_sections
611 {
612 struct dwarf2_section_info abbrev;
613 struct dwarf2_section_info line;
614 struct dwarf2_section_info loc;
615 struct dwarf2_section_info loclists;
616 struct dwarf2_section_info macinfo;
617 struct dwarf2_section_info macro;
618 struct dwarf2_section_info str;
619 struct dwarf2_section_info str_offsets;
620 /* In the case of a virtual DWO file, these two are unused. */
621 struct dwarf2_section_info info;
622 std::vector<dwarf2_section_info> types;
623 };
624
625 /* CUs/TUs in DWP/DWO files. */
626
627 struct dwo_unit
628 {
629 /* Backlink to the containing struct dwo_file. */
630 struct dwo_file *dwo_file;
631
632 /* The "id" that distinguishes this CU/TU.
633 .debug_info calls this "dwo_id", .debug_types calls this "signature".
634 Since signatures came first, we stick with it for consistency. */
635 ULONGEST signature;
636
637 /* The section this CU/TU lives in, in the DWO file. */
638 struct dwarf2_section_info *section;
639
640 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
641 sect_offset sect_off;
642 unsigned int length;
643
644 /* For types, offset in the type's DIE of the type defined by this TU. */
645 cu_offset type_offset_in_tu;
646 };
647
648 /* include/dwarf2.h defines the DWP section codes.
649 It defines a max value but it doesn't define a min value, which we
650 use for error checking, so provide one. */
651
652 enum dwp_v2_section_ids
653 {
654 DW_SECT_MIN = 1
655 };
656
657 /* Data for one DWO file.
658
659 This includes virtual DWO files (a virtual DWO file is a DWO file as it
660 appears in a DWP file). DWP files don't really have DWO files per se -
661 comdat folding of types "loses" the DWO file they came from, and from
662 a high level view DWP files appear to contain a mass of random types.
663 However, to maintain consistency with the non-DWP case we pretend DWP
664 files contain virtual DWO files, and we assign each TU with one virtual
665 DWO file (generally based on the line and abbrev section offsets -
666 a heuristic that seems to work in practice). */
667
668 struct dwo_file
669 {
670 dwo_file () = default;
671 DISABLE_COPY_AND_ASSIGN (dwo_file);
672
673 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
674 For virtual DWO files the name is constructed from the section offsets
675 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
676 from related CU+TUs. */
677 const char *dwo_name = nullptr;
678
679 /* The DW_AT_comp_dir attribute. */
680 const char *comp_dir = nullptr;
681
682 /* The bfd, when the file is open. Otherwise this is NULL.
683 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
684 gdb_bfd_ref_ptr dbfd;
685
686 /* The sections that make up this DWO file.
687 Remember that for virtual DWO files in DWP V2, these are virtual
688 sections (for lack of a better name). */
689 struct dwo_sections sections {};
690
691 /* The CUs in the file.
692 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
693 an extension to handle LLVM's Link Time Optimization output (where
694 multiple source files may be compiled into a single object/dwo pair). */
695 htab_up cus;
696
697 /* Table of TUs in the file.
698 Each element is a struct dwo_unit. */
699 htab_up tus;
700 };
701
702 /* These sections are what may appear in a DWP file. */
703
704 struct dwp_sections
705 {
706 /* These are used by both DWP version 1 and 2. */
707 struct dwarf2_section_info str;
708 struct dwarf2_section_info cu_index;
709 struct dwarf2_section_info tu_index;
710
711 /* These are only used by DWP version 2 files.
712 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
713 sections are referenced by section number, and are not recorded here.
714 In DWP version 2 there is at most one copy of all these sections, each
715 section being (effectively) comprised of the concatenation of all of the
716 individual sections that exist in the version 1 format.
717 To keep the code simple we treat each of these concatenated pieces as a
718 section itself (a virtual section?). */
719 struct dwarf2_section_info abbrev;
720 struct dwarf2_section_info info;
721 struct dwarf2_section_info line;
722 struct dwarf2_section_info loc;
723 struct dwarf2_section_info macinfo;
724 struct dwarf2_section_info macro;
725 struct dwarf2_section_info str_offsets;
726 struct dwarf2_section_info types;
727 };
728
729 /* These sections are what may appear in a virtual DWO file in DWP version 1.
730 A virtual DWO file is a DWO file as it appears in a DWP file. */
731
732 struct virtual_v1_dwo_sections
733 {
734 struct dwarf2_section_info abbrev;
735 struct dwarf2_section_info line;
736 struct dwarf2_section_info loc;
737 struct dwarf2_section_info macinfo;
738 struct dwarf2_section_info macro;
739 struct dwarf2_section_info str_offsets;
740 /* Each DWP hash table entry records one CU or one TU.
741 That is recorded here, and copied to dwo_unit.section. */
742 struct dwarf2_section_info info_or_types;
743 };
744
745 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
746 In version 2, the sections of the DWO files are concatenated together
747 and stored in one section of that name. Thus each ELF section contains
748 several "virtual" sections. */
749
750 struct virtual_v2_dwo_sections
751 {
752 bfd_size_type abbrev_offset;
753 bfd_size_type abbrev_size;
754
755 bfd_size_type line_offset;
756 bfd_size_type line_size;
757
758 bfd_size_type loc_offset;
759 bfd_size_type loc_size;
760
761 bfd_size_type macinfo_offset;
762 bfd_size_type macinfo_size;
763
764 bfd_size_type macro_offset;
765 bfd_size_type macro_size;
766
767 bfd_size_type str_offsets_offset;
768 bfd_size_type str_offsets_size;
769
770 /* Each DWP hash table entry records one CU or one TU.
771 That is recorded here, and copied to dwo_unit.section. */
772 bfd_size_type info_or_types_offset;
773 bfd_size_type info_or_types_size;
774 };
775
776 /* Contents of DWP hash tables. */
777
778 struct dwp_hash_table
779 {
780 uint32_t version, nr_columns;
781 uint32_t nr_units, nr_slots;
782 const gdb_byte *hash_table, *unit_table;
783 union
784 {
785 struct
786 {
787 const gdb_byte *indices;
788 } v1;
789 struct
790 {
791 /* This is indexed by column number and gives the id of the section
792 in that column. */
793 #define MAX_NR_V2_DWO_SECTIONS \
794 (1 /* .debug_info or .debug_types */ \
795 + 1 /* .debug_abbrev */ \
796 + 1 /* .debug_line */ \
797 + 1 /* .debug_loc */ \
798 + 1 /* .debug_str_offsets */ \
799 + 1 /* .debug_macro or .debug_macinfo */)
800 int section_ids[MAX_NR_V2_DWO_SECTIONS];
801 const gdb_byte *offsets;
802 const gdb_byte *sizes;
803 } v2;
804 } section_pool;
805 };
806
807 /* Data for one DWP file. */
808
809 struct dwp_file
810 {
811 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
812 : name (name_),
813 dbfd (std::move (abfd))
814 {
815 }
816
817 /* Name of the file. */
818 const char *name;
819
820 /* File format version. */
821 int version = 0;
822
823 /* The bfd. */
824 gdb_bfd_ref_ptr dbfd;
825
826 /* Section info for this file. */
827 struct dwp_sections sections {};
828
829 /* Table of CUs in the file. */
830 const struct dwp_hash_table *cus = nullptr;
831
832 /* Table of TUs in the file. */
833 const struct dwp_hash_table *tus = nullptr;
834
835 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
836 htab_up loaded_cus;
837 htab_up loaded_tus;
838
839 /* Table to map ELF section numbers to their sections.
840 This is only needed for the DWP V1 file format. */
841 unsigned int num_sections = 0;
842 asection **elf_sections = nullptr;
843 };
844
845 /* Struct used to pass misc. parameters to read_die_and_children, et
846 al. which are used for both .debug_info and .debug_types dies.
847 All parameters here are unchanging for the life of the call. This
848 struct exists to abstract away the constant parameters of die reading. */
849
850 struct die_reader_specs
851 {
852 /* The bfd of die_section. */
853 bfd* abfd;
854
855 /* The CU of the DIE we are parsing. */
856 struct dwarf2_cu *cu;
857
858 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
859 struct dwo_file *dwo_file;
860
861 /* The section the die comes from.
862 This is either .debug_info or .debug_types, or the .dwo variants. */
863 struct dwarf2_section_info *die_section;
864
865 /* die_section->buffer. */
866 const gdb_byte *buffer;
867
868 /* The end of the buffer. */
869 const gdb_byte *buffer_end;
870
871 /* The abbreviation table to use when reading the DIEs. */
872 struct abbrev_table *abbrev_table;
873 };
874
875 /* A subclass of die_reader_specs that holds storage and has complex
876 constructor and destructor behavior. */
877
878 class cutu_reader : public die_reader_specs
879 {
880 public:
881
882 cutu_reader (struct dwarf2_per_cu_data *this_cu,
883 struct abbrev_table *abbrev_table,
884 int use_existing_cu,
885 bool skip_partial);
886
887 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
888 struct dwarf2_cu *parent_cu = nullptr,
889 struct dwo_file *dwo_file = nullptr);
890
891 DISABLE_COPY_AND_ASSIGN (cutu_reader);
892
893 const gdb_byte *info_ptr = nullptr;
894 struct die_info *comp_unit_die = nullptr;
895 bool dummy_p = false;
896
897 /* Release the new CU, putting it on the chain. This cannot be done
898 for dummy CUs. */
899 void keep ();
900
901 private:
902 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
903 int use_existing_cu);
904
905 struct dwarf2_per_cu_data *m_this_cu;
906 std::unique_ptr<dwarf2_cu> m_new_cu;
907
908 /* The ordinary abbreviation table. */
909 abbrev_table_up m_abbrev_table_holder;
910
911 /* The DWO abbreviation table. */
912 abbrev_table_up m_dwo_abbrev_table;
913 };
914
915 /* When we construct a partial symbol table entry we only
916 need this much information. */
917 struct partial_die_info : public allocate_on_obstack
918 {
919 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
920
921 /* Disable assign but still keep copy ctor, which is needed
922 load_partial_dies. */
923 partial_die_info& operator=(const partial_die_info& rhs) = delete;
924
925 /* Adjust the partial die before generating a symbol for it. This
926 function may set the is_external flag or change the DIE's
927 name. */
928 void fixup (struct dwarf2_cu *cu);
929
930 /* Read a minimal amount of information into the minimal die
931 structure. */
932 const gdb_byte *read (const struct die_reader_specs *reader,
933 const struct abbrev_info &abbrev,
934 const gdb_byte *info_ptr);
935
936 /* Offset of this DIE. */
937 const sect_offset sect_off;
938
939 /* DWARF-2 tag for this DIE. */
940 const ENUM_BITFIELD(dwarf_tag) tag : 16;
941
942 /* Assorted flags describing the data found in this DIE. */
943 const unsigned int has_children : 1;
944
945 unsigned int is_external : 1;
946 unsigned int is_declaration : 1;
947 unsigned int has_type : 1;
948 unsigned int has_specification : 1;
949 unsigned int has_pc_info : 1;
950 unsigned int may_be_inlined : 1;
951
952 /* This DIE has been marked DW_AT_main_subprogram. */
953 unsigned int main_subprogram : 1;
954
955 /* Flag set if the SCOPE field of this structure has been
956 computed. */
957 unsigned int scope_set : 1;
958
959 /* Flag set if the DIE has a byte_size attribute. */
960 unsigned int has_byte_size : 1;
961
962 /* Flag set if the DIE has a DW_AT_const_value attribute. */
963 unsigned int has_const_value : 1;
964
965 /* Flag set if any of the DIE's children are template arguments. */
966 unsigned int has_template_arguments : 1;
967
968 /* Flag set if fixup has been called on this die. */
969 unsigned int fixup_called : 1;
970
971 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
972 unsigned int is_dwz : 1;
973
974 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
975 unsigned int spec_is_dwz : 1;
976
977 /* The name of this DIE. Normally the value of DW_AT_name, but
978 sometimes a default name for unnamed DIEs. */
979 const char *name = nullptr;
980
981 /* The linkage name, if present. */
982 const char *linkage_name = nullptr;
983
984 /* The scope to prepend to our children. This is generally
985 allocated on the comp_unit_obstack, so will disappear
986 when this compilation unit leaves the cache. */
987 const char *scope = nullptr;
988
989 /* Some data associated with the partial DIE. The tag determines
990 which field is live. */
991 union
992 {
993 /* The location description associated with this DIE, if any. */
994 struct dwarf_block *locdesc;
995 /* The offset of an import, for DW_TAG_imported_unit. */
996 sect_offset sect_off;
997 } d {};
998
999 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1000 CORE_ADDR lowpc = 0;
1001 CORE_ADDR highpc = 0;
1002
1003 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1004 DW_AT_sibling, if any. */
1005 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1006 could return DW_AT_sibling values to its caller load_partial_dies. */
1007 const gdb_byte *sibling = nullptr;
1008
1009 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1010 DW_AT_specification (or DW_AT_abstract_origin or
1011 DW_AT_extension). */
1012 sect_offset spec_offset {};
1013
1014 /* Pointers to this DIE's parent, first child, and next sibling,
1015 if any. */
1016 struct partial_die_info *die_parent = nullptr;
1017 struct partial_die_info *die_child = nullptr;
1018 struct partial_die_info *die_sibling = nullptr;
1019
1020 friend struct partial_die_info *
1021 dwarf2_cu::find_partial_die (sect_offset sect_off);
1022
1023 private:
1024 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1025 partial_die_info (sect_offset sect_off)
1026 : partial_die_info (sect_off, DW_TAG_padding, 0)
1027 {
1028 }
1029
1030 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1031 int has_children_)
1032 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1033 {
1034 is_external = 0;
1035 is_declaration = 0;
1036 has_type = 0;
1037 has_specification = 0;
1038 has_pc_info = 0;
1039 may_be_inlined = 0;
1040 main_subprogram = 0;
1041 scope_set = 0;
1042 has_byte_size = 0;
1043 has_const_value = 0;
1044 has_template_arguments = 0;
1045 fixup_called = 0;
1046 is_dwz = 0;
1047 spec_is_dwz = 0;
1048 }
1049 };
1050
1051 /* This data structure holds a complete die structure. */
1052 struct die_info
1053 {
1054 /* DWARF-2 tag for this DIE. */
1055 ENUM_BITFIELD(dwarf_tag) tag : 16;
1056
1057 /* Number of attributes */
1058 unsigned char num_attrs;
1059
1060 /* True if we're presently building the full type name for the
1061 type derived from this DIE. */
1062 unsigned char building_fullname : 1;
1063
1064 /* True if this die is in process. PR 16581. */
1065 unsigned char in_process : 1;
1066
1067 /* True if this DIE has children. */
1068 unsigned char has_children : 1;
1069
1070 /* Abbrev number */
1071 unsigned int abbrev;
1072
1073 /* Offset in .debug_info or .debug_types section. */
1074 sect_offset sect_off;
1075
1076 /* The dies in a compilation unit form an n-ary tree. PARENT
1077 points to this die's parent; CHILD points to the first child of
1078 this node; and all the children of a given node are chained
1079 together via their SIBLING fields. */
1080 struct die_info *child; /* Its first child, if any. */
1081 struct die_info *sibling; /* Its next sibling, if any. */
1082 struct die_info *parent; /* Its parent, if any. */
1083
1084 /* An array of attributes, with NUM_ATTRS elements. There may be
1085 zero, but it's not common and zero-sized arrays are not
1086 sufficiently portable C. */
1087 struct attribute attrs[1];
1088 };
1089
1090 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1091 but this would require a corresponding change in unpack_field_as_long
1092 and friends. */
1093 static int bits_per_byte = 8;
1094
1095 /* When reading a variant or variant part, we track a bit more
1096 information about the field, and store it in an object of this
1097 type. */
1098
1099 struct variant_field
1100 {
1101 /* If we see a DW_TAG_variant, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value;
1104 /* If we see a DW_TAG_variant, then this will be set if this is the
1105 default branch. */
1106 bool default_branch;
1107 /* While reading a DW_TAG_variant_part, this will be set if this
1108 field is the discriminant. */
1109 bool is_discriminant;
1110 };
1111
1112 struct nextfield
1113 {
1114 int accessibility = 0;
1115 int virtuality = 0;
1116 /* Extra information to describe a variant or variant part. */
1117 struct variant_field variant {};
1118 struct field field {};
1119 };
1120
1121 struct fnfieldlist
1122 {
1123 const char *name = nullptr;
1124 std::vector<struct fn_field> fnfields;
1125 };
1126
1127 /* The routines that read and process dies for a C struct or C++ class
1128 pass lists of data member fields and lists of member function fields
1129 in an instance of a field_info structure, as defined below. */
1130 struct field_info
1131 {
1132 /* List of data member and baseclasses fields. */
1133 std::vector<struct nextfield> fields;
1134 std::vector<struct nextfield> baseclasses;
1135
1136 /* Number of fields (including baseclasses). */
1137 int nfields = 0;
1138
1139 /* Set if the accessibility of one of the fields is not public. */
1140 int non_public_fields = 0;
1141
1142 /* Member function fieldlist array, contains name of possibly overloaded
1143 member function, number of overloaded member functions and a pointer
1144 to the head of the member function field chain. */
1145 std::vector<struct fnfieldlist> fnfieldlists;
1146
1147 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1148 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1149 std::vector<struct decl_field> typedef_field_list;
1150
1151 /* Nested types defined by this class and the number of elements in this
1152 list. */
1153 std::vector<struct decl_field> nested_types_list;
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_line_string
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1256 const struct comp_unit_head *, unsigned int *);
1257
1258 static const char *read_indirect_string_at_offset
1259 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1260 LONGEST str_offset);
1261
1262 static const char *read_indirect_string_from_dwz
1263 (struct objfile *objfile, struct dwz_file *, LONGEST);
1264
1265 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1266 const gdb_byte *,
1267 unsigned int *);
1268
1269 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1270 ULONGEST str_index);
1271
1272 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1273 ULONGEST str_index);
1274
1275 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1276
1277 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1278 struct dwarf2_cu *);
1279
1280 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1281 unsigned int);
1282
1283 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1284 struct dwarf2_cu *cu);
1285
1286 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1287
1288 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1289 struct dwarf2_cu *cu);
1290
1291 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1292
1293 static struct die_info *die_specification (struct die_info *die,
1294 struct dwarf2_cu **);
1295
1296 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1297 struct dwarf2_cu *cu);
1298
1299 static void dwarf_decode_lines (struct line_header *, const char *,
1300 struct dwarf2_cu *, dwarf2_psymtab *,
1301 CORE_ADDR, int decode_mapping);
1302
1303 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1304 const char *);
1305
1306 static struct symbol *new_symbol (struct die_info *, struct type *,
1307 struct dwarf2_cu *, struct symbol * = NULL);
1308
1309 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1310 struct dwarf2_cu *);
1311
1312 static void dwarf2_const_value_attr (const struct attribute *attr,
1313 struct type *type,
1314 const char *name,
1315 struct obstack *obstack,
1316 struct dwarf2_cu *cu, LONGEST *value,
1317 const gdb_byte **bytes,
1318 struct dwarf2_locexpr_baton **baton);
1319
1320 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1321
1322 static int need_gnat_info (struct dwarf2_cu *);
1323
1324 static struct type *die_descriptive_type (struct die_info *,
1325 struct dwarf2_cu *);
1326
1327 static void set_descriptive_type (struct type *, struct die_info *,
1328 struct dwarf2_cu *);
1329
1330 static struct type *die_containing_type (struct die_info *,
1331 struct dwarf2_cu *);
1332
1333 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1334 struct dwarf2_cu *);
1335
1336 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1337
1338 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1339
1340 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1341
1342 static char *typename_concat (struct obstack *obs, const char *prefix,
1343 const char *suffix, int physname,
1344 struct dwarf2_cu *cu);
1345
1346 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1347
1348 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1349
1350 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1351
1352 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1355
1356 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1357
1358 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1359 struct dwarf2_cu *, dwarf2_psymtab *);
1360
1361 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1362 values. Keep the items ordered with increasing constraints compliance. */
1363 enum pc_bounds_kind
1364 {
1365 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1366 PC_BOUNDS_NOT_PRESENT,
1367
1368 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1369 were present but they do not form a valid range of PC addresses. */
1370 PC_BOUNDS_INVALID,
1371
1372 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1373 PC_BOUNDS_RANGES,
1374
1375 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1376 PC_BOUNDS_HIGH_LOW,
1377 };
1378
1379 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1380 CORE_ADDR *, CORE_ADDR *,
1381 struct dwarf2_cu *,
1382 dwarf2_psymtab *);
1383
1384 static void get_scope_pc_bounds (struct die_info *,
1385 CORE_ADDR *, CORE_ADDR *,
1386 struct dwarf2_cu *);
1387
1388 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1389 CORE_ADDR, struct dwarf2_cu *);
1390
1391 static void dwarf2_add_field (struct field_info *, struct die_info *,
1392 struct dwarf2_cu *);
1393
1394 static void dwarf2_attach_fields_to_type (struct field_info *,
1395 struct type *, struct dwarf2_cu *);
1396
1397 static void dwarf2_add_member_fn (struct field_info *,
1398 struct die_info *, struct type *,
1399 struct dwarf2_cu *);
1400
1401 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1402 struct type *,
1403 struct dwarf2_cu *);
1404
1405 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1406
1407 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1408
1409 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1410
1411 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1412
1413 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1414
1415 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1416
1417 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1418
1419 static struct type *read_module_type (struct die_info *die,
1420 struct dwarf2_cu *cu);
1421
1422 static const char *namespace_name (struct die_info *die,
1423 int *is_anonymous, struct dwarf2_cu *);
1424
1425 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1426
1427 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1428
1429 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1430 struct dwarf2_cu *);
1431
1432 static struct die_info *read_die_and_siblings_1
1433 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1434 struct die_info *);
1435
1436 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1437 const gdb_byte *info_ptr,
1438 const gdb_byte **new_info_ptr,
1439 struct die_info *parent);
1440
1441 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1442 struct die_info **, const gdb_byte *,
1443 int);
1444
1445 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1446 struct die_info **, const gdb_byte *);
1447
1448 static void process_die (struct die_info *, struct dwarf2_cu *);
1449
1450 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1451 struct obstack *);
1452
1453 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1454
1455 static const char *dwarf2_full_name (const char *name,
1456 struct die_info *die,
1457 struct dwarf2_cu *cu);
1458
1459 static const char *dwarf2_physname (const char *name, struct die_info *die,
1460 struct dwarf2_cu *cu);
1461
1462 static struct die_info *dwarf2_extension (struct die_info *die,
1463 struct dwarf2_cu **);
1464
1465 static const char *dwarf_tag_name (unsigned int);
1466
1467 static const char *dwarf_attr_name (unsigned int);
1468
1469 static const char *dwarf_form_name (unsigned int);
1470
1471 static const char *dwarf_bool_name (unsigned int);
1472
1473 static const char *dwarf_type_encoding_name (unsigned int);
1474
1475 static struct die_info *sibling_die (struct die_info *);
1476
1477 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479 static void dump_die_for_error (struct die_info *);
1480
1481 static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
1483
1484 /*static*/ void dump_die (struct die_info *, int max_level);
1485
1486 static void store_in_ref_table (struct die_info *,
1487 struct dwarf2_cu *);
1488
1489 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1490
1491 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1492
1493 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1494 const struct attribute *,
1495 struct dwarf2_cu **);
1496
1497 static struct die_info *follow_die_ref (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct die_info *follow_die_sig (struct die_info *,
1502 const struct attribute *,
1503 struct dwarf2_cu **);
1504
1505 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1506 struct dwarf2_cu *);
1507
1508 static struct type *get_DW_AT_signature_type (struct die_info *,
1509 const struct attribute *,
1510 struct dwarf2_cu *);
1511
1512 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1513
1514 static void read_signatured_type (struct signatured_type *);
1515
1516 static int attr_to_dynamic_prop (const struct attribute *attr,
1517 struct die_info *die, struct dwarf2_cu *cu,
1518 struct dynamic_prop *prop, struct type *type);
1519
1520 /* memory allocation interface */
1521
1522 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1523
1524 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1525
1526 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1527
1528 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1529 struct dwarf2_loclist_baton *baton,
1530 const struct attribute *attr);
1531
1532 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1533 struct symbol *sym,
1534 struct dwarf2_cu *cu,
1535 int is_block);
1536
1537 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1538 const gdb_byte *info_ptr,
1539 struct abbrev_info *abbrev);
1540
1541 static hashval_t partial_die_hash (const void *item);
1542
1543 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1544
1545 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1546 (sect_offset sect_off, unsigned int offset_in_dwz,
1547 struct dwarf2_per_objfile *dwarf2_per_objfile);
1548
1549 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1550 struct die_info *comp_unit_die,
1551 enum language pretend_language);
1552
1553 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1556
1557 static struct type *set_die_type (struct die_info *, struct type *,
1558 struct dwarf2_cu *);
1559
1560 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1561
1562 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1563
1564 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1565 enum language);
1566
1567 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1568 enum language);
1569
1570 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1571 enum language);
1572
1573 static void dwarf2_add_dependence (struct dwarf2_cu *,
1574 struct dwarf2_per_cu_data *);
1575
1576 static void dwarf2_mark (struct dwarf2_cu *);
1577
1578 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1579
1580 static struct type *get_die_type_at_offset (sect_offset,
1581 struct dwarf2_per_cu_data *);
1582
1583 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1584
1585 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1586 enum language pretend_language);
1587
1588 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1589
1590 /* Class, the destructor of which frees all allocated queue entries. This
1591 will only have work to do if an error was thrown while processing the
1592 dwarf. If no error was thrown then the queue entries should have all
1593 been processed, and freed, as we went along. */
1594
1595 class dwarf2_queue_guard
1596 {
1597 public:
1598 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1599 : m_per_objfile (per_objfile)
1600 {
1601 }
1602
1603 /* Free any entries remaining on the queue. There should only be
1604 entries left if we hit an error while processing the dwarf. */
1605 ~dwarf2_queue_guard ()
1606 {
1607 /* Ensure that no memory is allocated by the queue. */
1608 std::queue<dwarf2_queue_item> empty;
1609 std::swap (m_per_objfile->queue, empty);
1610 }
1611
1612 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1613
1614 private:
1615 dwarf2_per_objfile *m_per_objfile;
1616 };
1617
1618 dwarf2_queue_item::~dwarf2_queue_item ()
1619 {
1620 /* Anything still marked queued is likely to be in an
1621 inconsistent state, so discard it. */
1622 if (per_cu->queued)
1623 {
1624 if (per_cu->cu != NULL)
1625 free_one_cached_comp_unit (per_cu);
1626 per_cu->queued = 0;
1627 }
1628 }
1629
1630 /* The return type of find_file_and_directory. Note, the enclosed
1631 string pointers are only valid while this object is valid. */
1632
1633 struct file_and_directory
1634 {
1635 /* The filename. This is never NULL. */
1636 const char *name;
1637
1638 /* The compilation directory. NULL if not known. If we needed to
1639 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1640 points directly to the DW_AT_comp_dir string attribute owned by
1641 the obstack that owns the DIE. */
1642 const char *comp_dir;
1643
1644 /* If we needed to build a new string for comp_dir, this is what
1645 owns the storage. */
1646 std::string comp_dir_storage;
1647 };
1648
1649 static file_and_directory find_file_and_directory (struct die_info *die,
1650 struct dwarf2_cu *cu);
1651
1652 static htab_up allocate_signatured_type_table (struct objfile *objfile);
1653
1654 static htab_up allocate_dwo_unit_table (struct objfile *objfile);
1655
1656 static struct dwo_unit *lookup_dwo_unit_in_dwp
1657 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1658 struct dwp_file *dwp_file, const char *comp_dir,
1659 ULONGEST signature, int is_debug_types);
1660
1661 static struct dwp_file *get_dwp_file
1662 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1663
1664 static struct dwo_unit *lookup_dwo_comp_unit
1665 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1666
1667 static struct dwo_unit *lookup_dwo_type_unit
1668 (struct signatured_type *, const char *, const char *);
1669
1670 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1671
1672 /* A unique pointer to a dwo_file. */
1673
1674 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1675
1676 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1677
1678 static void check_producer (struct dwarf2_cu *cu);
1679
1680 static void free_line_header_voidp (void *arg);
1681 \f
1682 /* Various complaints about symbol reading that don't abort the process. */
1683
1684 static void
1685 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1686 {
1687 complaint (_("statement list doesn't fit in .debug_line section"));
1688 }
1689
1690 static void
1691 dwarf2_debug_line_missing_file_complaint (void)
1692 {
1693 complaint (_(".debug_line section has line data without a file"));
1694 }
1695
1696 static void
1697 dwarf2_debug_line_missing_end_sequence_complaint (void)
1698 {
1699 complaint (_(".debug_line section has line "
1700 "program sequence without an end"));
1701 }
1702
1703 static void
1704 dwarf2_complex_location_expr_complaint (void)
1705 {
1706 complaint (_("location expression too complex"));
1707 }
1708
1709 static void
1710 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1711 int arg3)
1712 {
1713 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1714 arg1, arg2, arg3);
1715 }
1716
1717 static void
1718 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1719 {
1720 complaint (_("debug info runs off end of %s section"
1721 " [in module %s]"),
1722 section->get_name (),
1723 section->get_file_name ());
1724 }
1725
1726 static void
1727 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1728 {
1729 complaint (_("macro debug info contains a "
1730 "malformed macro definition:\n`%s'"),
1731 arg1);
1732 }
1733
1734 static void
1735 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1736 {
1737 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1738 arg1, arg2);
1739 }
1740
1741 /* Hash function for line_header_hash. */
1742
1743 static hashval_t
1744 line_header_hash (const struct line_header *ofs)
1745 {
1746 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1747 }
1748
1749 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1750
1751 static hashval_t
1752 line_header_hash_voidp (const void *item)
1753 {
1754 const struct line_header *ofs = (const struct line_header *) item;
1755
1756 return line_header_hash (ofs);
1757 }
1758
1759 /* Equality function for line_header_hash. */
1760
1761 static int
1762 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1763 {
1764 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1765 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1766
1767 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1768 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1769 }
1770
1771 \f
1772
1773 /* See declaration. */
1774
1775 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1776 const dwarf2_debug_sections *names,
1777 bool can_copy_)
1778 : objfile (objfile_),
1779 can_copy (can_copy_)
1780 {
1781 if (names == NULL)
1782 names = &dwarf2_elf_names;
1783
1784 bfd *obfd = objfile->obfd;
1785
1786 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1787 locate_sections (obfd, sec, *names);
1788 }
1789
1790 dwarf2_per_objfile::~dwarf2_per_objfile ()
1791 {
1792 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1793 free_cached_comp_units ();
1794
1795 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1796 per_cu->imported_symtabs_free ();
1797
1798 for (signatured_type *sig_type : all_type_units)
1799 sig_type->per_cu.imported_symtabs_free ();
1800
1801 /* Everything else should be on the objfile obstack. */
1802 }
1803
1804 /* See declaration. */
1805
1806 void
1807 dwarf2_per_objfile::free_cached_comp_units ()
1808 {
1809 dwarf2_per_cu_data *per_cu = read_in_chain;
1810 dwarf2_per_cu_data **last_chain = &read_in_chain;
1811 while (per_cu != NULL)
1812 {
1813 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1814
1815 delete per_cu->cu;
1816 *last_chain = next_cu;
1817 per_cu = next_cu;
1818 }
1819 }
1820
1821 /* A helper class that calls free_cached_comp_units on
1822 destruction. */
1823
1824 class free_cached_comp_units
1825 {
1826 public:
1827
1828 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1829 : m_per_objfile (per_objfile)
1830 {
1831 }
1832
1833 ~free_cached_comp_units ()
1834 {
1835 m_per_objfile->free_cached_comp_units ();
1836 }
1837
1838 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1839
1840 private:
1841
1842 dwarf2_per_objfile *m_per_objfile;
1843 };
1844
1845 /* Try to locate the sections we need for DWARF 2 debugging
1846 information and return true if we have enough to do something.
1847 NAMES points to the dwarf2 section names, or is NULL if the standard
1848 ELF names are used. CAN_COPY is true for formats where symbol
1849 interposition is possible and so symbol values must follow copy
1850 relocation rules. */
1851
1852 int
1853 dwarf2_has_info (struct objfile *objfile,
1854 const struct dwarf2_debug_sections *names,
1855 bool can_copy)
1856 {
1857 if (objfile->flags & OBJF_READNEVER)
1858 return 0;
1859
1860 struct dwarf2_per_objfile *dwarf2_per_objfile
1861 = get_dwarf2_per_objfile (objfile);
1862
1863 if (dwarf2_per_objfile == NULL)
1864 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1865 names,
1866 can_copy);
1867
1868 return (!dwarf2_per_objfile->info.is_virtual
1869 && dwarf2_per_objfile->info.s.section != NULL
1870 && !dwarf2_per_objfile->abbrev.is_virtual
1871 && dwarf2_per_objfile->abbrev.s.section != NULL);
1872 }
1873
1874 /* When loading sections, we look either for uncompressed section or for
1875 compressed section names. */
1876
1877 static int
1878 section_is_p (const char *section_name,
1879 const struct dwarf2_section_names *names)
1880 {
1881 if (names->normal != NULL
1882 && strcmp (section_name, names->normal) == 0)
1883 return 1;
1884 if (names->compressed != NULL
1885 && strcmp (section_name, names->compressed) == 0)
1886 return 1;
1887 return 0;
1888 }
1889
1890 /* See declaration. */
1891
1892 void
1893 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1894 const dwarf2_debug_sections &names)
1895 {
1896 flagword aflag = bfd_section_flags (sectp);
1897
1898 if ((aflag & SEC_HAS_CONTENTS) == 0)
1899 {
1900 }
1901 else if (elf_section_data (sectp)->this_hdr.sh_size
1902 > bfd_get_file_size (abfd))
1903 {
1904 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1905 warning (_("Discarding section %s which has a section size (%s"
1906 ") larger than the file size [in module %s]"),
1907 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1908 bfd_get_filename (abfd));
1909 }
1910 else if (section_is_p (sectp->name, &names.info))
1911 {
1912 this->info.s.section = sectp;
1913 this->info.size = bfd_section_size (sectp);
1914 }
1915 else if (section_is_p (sectp->name, &names.abbrev))
1916 {
1917 this->abbrev.s.section = sectp;
1918 this->abbrev.size = bfd_section_size (sectp);
1919 }
1920 else if (section_is_p (sectp->name, &names.line))
1921 {
1922 this->line.s.section = sectp;
1923 this->line.size = bfd_section_size (sectp);
1924 }
1925 else if (section_is_p (sectp->name, &names.loc))
1926 {
1927 this->loc.s.section = sectp;
1928 this->loc.size = bfd_section_size (sectp);
1929 }
1930 else if (section_is_p (sectp->name, &names.loclists))
1931 {
1932 this->loclists.s.section = sectp;
1933 this->loclists.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.macinfo))
1936 {
1937 this->macinfo.s.section = sectp;
1938 this->macinfo.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.macro))
1941 {
1942 this->macro.s.section = sectp;
1943 this->macro.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.str))
1946 {
1947 this->str.s.section = sectp;
1948 this->str.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.str_offsets))
1951 {
1952 this->str_offsets.s.section = sectp;
1953 this->str_offsets.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.line_str))
1956 {
1957 this->line_str.s.section = sectp;
1958 this->line_str.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.addr))
1961 {
1962 this->addr.s.section = sectp;
1963 this->addr.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.frame))
1966 {
1967 this->frame.s.section = sectp;
1968 this->frame.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.eh_frame))
1971 {
1972 this->eh_frame.s.section = sectp;
1973 this->eh_frame.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.ranges))
1976 {
1977 this->ranges.s.section = sectp;
1978 this->ranges.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.rnglists))
1981 {
1982 this->rnglists.s.section = sectp;
1983 this->rnglists.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.types))
1986 {
1987 struct dwarf2_section_info type_section;
1988
1989 memset (&type_section, 0, sizeof (type_section));
1990 type_section.s.section = sectp;
1991 type_section.size = bfd_section_size (sectp);
1992
1993 this->types.push_back (type_section);
1994 }
1995 else if (section_is_p (sectp->name, &names.gdb_index))
1996 {
1997 this->gdb_index.s.section = sectp;
1998 this->gdb_index.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.debug_names))
2001 {
2002 this->debug_names.s.section = sectp;
2003 this->debug_names.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.debug_aranges))
2006 {
2007 this->debug_aranges.s.section = sectp;
2008 this->debug_aranges.size = bfd_section_size (sectp);
2009 }
2010
2011 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2012 && bfd_section_vma (sectp) == 0)
2013 this->has_section_at_zero = true;
2014 }
2015
2016 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2017 SECTION_NAME. */
2018
2019 void
2020 dwarf2_get_section_info (struct objfile *objfile,
2021 enum dwarf2_section_enum sect,
2022 asection **sectp, const gdb_byte **bufp,
2023 bfd_size_type *sizep)
2024 {
2025 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2026 struct dwarf2_section_info *info;
2027
2028 /* We may see an objfile without any DWARF, in which case we just
2029 return nothing. */
2030 if (data == NULL)
2031 {
2032 *sectp = NULL;
2033 *bufp = NULL;
2034 *sizep = 0;
2035 return;
2036 }
2037 switch (sect)
2038 {
2039 case DWARF2_DEBUG_FRAME:
2040 info = &data->frame;
2041 break;
2042 case DWARF2_EH_FRAME:
2043 info = &data->eh_frame;
2044 break;
2045 default:
2046 gdb_assert_not_reached ("unexpected section");
2047 }
2048
2049 info->read (objfile);
2050
2051 *sectp = info->get_bfd_section ();
2052 *bufp = info->buffer;
2053 *sizep = info->size;
2054 }
2055
2056 /* A helper function to find the sections for a .dwz file. */
2057
2058 static void
2059 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2060 {
2061 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2062
2063 /* Note that we only support the standard ELF names, because .dwz
2064 is ELF-only (at the time of writing). */
2065 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2066 {
2067 dwz_file->abbrev.s.section = sectp;
2068 dwz_file->abbrev.size = bfd_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2071 {
2072 dwz_file->info.s.section = sectp;
2073 dwz_file->info.size = bfd_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2076 {
2077 dwz_file->str.s.section = sectp;
2078 dwz_file->str.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2081 {
2082 dwz_file->line.s.section = sectp;
2083 dwz_file->line.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2086 {
2087 dwz_file->macro.s.section = sectp;
2088 dwz_file->macro.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2091 {
2092 dwz_file->gdb_index.s.section = sectp;
2093 dwz_file->gdb_index.size = bfd_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2096 {
2097 dwz_file->debug_names.s.section = sectp;
2098 dwz_file->debug_names.size = bfd_section_size (sectp);
2099 }
2100 }
2101
2102 /* See dwarf2read.h. */
2103
2104 struct dwz_file *
2105 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2106 {
2107 const char *filename;
2108 bfd_size_type buildid_len_arg;
2109 size_t buildid_len;
2110 bfd_byte *buildid;
2111
2112 if (dwarf2_per_objfile->dwz_file != NULL)
2113 return dwarf2_per_objfile->dwz_file.get ();
2114
2115 bfd_set_error (bfd_error_no_error);
2116 gdb::unique_xmalloc_ptr<char> data
2117 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2118 &buildid_len_arg, &buildid));
2119 if (data == NULL)
2120 {
2121 if (bfd_get_error () == bfd_error_no_error)
2122 return NULL;
2123 error (_("could not read '.gnu_debugaltlink' section: %s"),
2124 bfd_errmsg (bfd_get_error ()));
2125 }
2126
2127 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2128
2129 buildid_len = (size_t) buildid_len_arg;
2130
2131 filename = data.get ();
2132
2133 std::string abs_storage;
2134 if (!IS_ABSOLUTE_PATH (filename))
2135 {
2136 gdb::unique_xmalloc_ptr<char> abs
2137 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2138
2139 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2140 filename = abs_storage.c_str ();
2141 }
2142
2143 /* First try the file name given in the section. If that doesn't
2144 work, try to use the build-id instead. */
2145 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2146 if (dwz_bfd != NULL)
2147 {
2148 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2149 dwz_bfd.reset (nullptr);
2150 }
2151
2152 if (dwz_bfd == NULL)
2153 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2154
2155 if (dwz_bfd == NULL)
2156 error (_("could not find '.gnu_debugaltlink' file for %s"),
2157 objfile_name (dwarf2_per_objfile->objfile));
2158
2159 std::unique_ptr<struct dwz_file> result
2160 (new struct dwz_file (std::move (dwz_bfd)));
2161
2162 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2163 result.get ());
2164
2165 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2166 result->dwz_bfd.get ());
2167 dwarf2_per_objfile->dwz_file = std::move (result);
2168 return dwarf2_per_objfile->dwz_file.get ();
2169 }
2170 \f
2171 /* DWARF quick_symbols_functions support. */
2172
2173 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2174 unique line tables, so we maintain a separate table of all .debug_line
2175 derived entries to support the sharing.
2176 All the quick functions need is the list of file names. We discard the
2177 line_header when we're done and don't need to record it here. */
2178 struct quick_file_names
2179 {
2180 /* The data used to construct the hash key. */
2181 struct stmt_list_hash hash;
2182
2183 /* The number of entries in file_names, real_names. */
2184 unsigned int num_file_names;
2185
2186 /* The file names from the line table, after being run through
2187 file_full_name. */
2188 const char **file_names;
2189
2190 /* The file names from the line table after being run through
2191 gdb_realpath. These are computed lazily. */
2192 const char **real_names;
2193 };
2194
2195 /* When using the index (and thus not using psymtabs), each CU has an
2196 object of this type. This is used to hold information needed by
2197 the various "quick" methods. */
2198 struct dwarf2_per_cu_quick_data
2199 {
2200 /* The file table. This can be NULL if there was no file table
2201 or it's currently not read in.
2202 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2203 struct quick_file_names *file_names;
2204
2205 /* The corresponding symbol table. This is NULL if symbols for this
2206 CU have not yet been read. */
2207 struct compunit_symtab *compunit_symtab;
2208
2209 /* A temporary mark bit used when iterating over all CUs in
2210 expand_symtabs_matching. */
2211 unsigned int mark : 1;
2212
2213 /* True if we've tried to read the file table and found there isn't one.
2214 There will be no point in trying to read it again next time. */
2215 unsigned int no_file_data : 1;
2216 };
2217
2218 /* Utility hash function for a stmt_list_hash. */
2219
2220 static hashval_t
2221 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2222 {
2223 hashval_t v = 0;
2224
2225 if (stmt_list_hash->dwo_unit != NULL)
2226 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2227 v += to_underlying (stmt_list_hash->line_sect_off);
2228 return v;
2229 }
2230
2231 /* Utility equality function for a stmt_list_hash. */
2232
2233 static int
2234 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2235 const struct stmt_list_hash *rhs)
2236 {
2237 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2238 return 0;
2239 if (lhs->dwo_unit != NULL
2240 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2241 return 0;
2242
2243 return lhs->line_sect_off == rhs->line_sect_off;
2244 }
2245
2246 /* Hash function for a quick_file_names. */
2247
2248 static hashval_t
2249 hash_file_name_entry (const void *e)
2250 {
2251 const struct quick_file_names *file_data
2252 = (const struct quick_file_names *) e;
2253
2254 return hash_stmt_list_entry (&file_data->hash);
2255 }
2256
2257 /* Equality function for a quick_file_names. */
2258
2259 static int
2260 eq_file_name_entry (const void *a, const void *b)
2261 {
2262 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2263 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2264
2265 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2266 }
2267
2268 /* Delete function for a quick_file_names. */
2269
2270 static void
2271 delete_file_name_entry (void *e)
2272 {
2273 struct quick_file_names *file_data = (struct quick_file_names *) e;
2274 int i;
2275
2276 for (i = 0; i < file_data->num_file_names; ++i)
2277 {
2278 xfree ((void*) file_data->file_names[i]);
2279 if (file_data->real_names)
2280 xfree ((void*) file_data->real_names[i]);
2281 }
2282
2283 /* The space for the struct itself lives on objfile_obstack,
2284 so we don't free it here. */
2285 }
2286
2287 /* Create a quick_file_names hash table. */
2288
2289 static htab_up
2290 create_quick_file_names_table (unsigned int nr_initial_entries)
2291 {
2292 return htab_up (htab_create_alloc (nr_initial_entries,
2293 hash_file_name_entry, eq_file_name_entry,
2294 delete_file_name_entry, xcalloc, xfree));
2295 }
2296
2297 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2298 have to be created afterwards. You should call age_cached_comp_units after
2299 processing PER_CU->CU. dw2_setup must have been already called. */
2300
2301 static void
2302 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2303 {
2304 if (per_cu->is_debug_types)
2305 load_full_type_unit (per_cu);
2306 else
2307 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2308
2309 if (per_cu->cu == NULL)
2310 return; /* Dummy CU. */
2311
2312 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2313 }
2314
2315 /* Read in the symbols for PER_CU. */
2316
2317 static void
2318 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2319 {
2320 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2321
2322 /* Skip type_unit_groups, reading the type units they contain
2323 is handled elsewhere. */
2324 if (IS_TYPE_UNIT_GROUP (per_cu))
2325 return;
2326
2327 /* The destructor of dwarf2_queue_guard frees any entries left on
2328 the queue. After this point we're guaranteed to leave this function
2329 with the dwarf queue empty. */
2330 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2331
2332 if (dwarf2_per_objfile->using_index
2333 ? per_cu->v.quick->compunit_symtab == NULL
2334 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2335 {
2336 queue_comp_unit (per_cu, language_minimal);
2337 load_cu (per_cu, skip_partial);
2338
2339 /* If we just loaded a CU from a DWO, and we're working with an index
2340 that may badly handle TUs, load all the TUs in that DWO as well.
2341 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2342 if (!per_cu->is_debug_types
2343 && per_cu->cu != NULL
2344 && per_cu->cu->dwo_unit != NULL
2345 && dwarf2_per_objfile->index_table != NULL
2346 && dwarf2_per_objfile->index_table->version <= 7
2347 /* DWP files aren't supported yet. */
2348 && get_dwp_file (dwarf2_per_objfile) == NULL)
2349 queue_and_load_all_dwo_tus (per_cu);
2350 }
2351
2352 process_queue (dwarf2_per_objfile);
2353
2354 /* Age the cache, releasing compilation units that have not
2355 been used recently. */
2356 age_cached_comp_units (dwarf2_per_objfile);
2357 }
2358
2359 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2360 the objfile from which this CU came. Returns the resulting symbol
2361 table. */
2362
2363 static struct compunit_symtab *
2364 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2365 {
2366 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2367
2368 gdb_assert (dwarf2_per_objfile->using_index);
2369 if (!per_cu->v.quick->compunit_symtab)
2370 {
2371 free_cached_comp_units freer (dwarf2_per_objfile);
2372 scoped_restore decrementer = increment_reading_symtab ();
2373 dw2_do_instantiate_symtab (per_cu, skip_partial);
2374 process_cu_includes (dwarf2_per_objfile);
2375 }
2376
2377 return per_cu->v.quick->compunit_symtab;
2378 }
2379
2380 /* See declaration. */
2381
2382 dwarf2_per_cu_data *
2383 dwarf2_per_objfile::get_cutu (int index)
2384 {
2385 if (index >= this->all_comp_units.size ())
2386 {
2387 index -= this->all_comp_units.size ();
2388 gdb_assert (index < this->all_type_units.size ());
2389 return &this->all_type_units[index]->per_cu;
2390 }
2391
2392 return this->all_comp_units[index];
2393 }
2394
2395 /* See declaration. */
2396
2397 dwarf2_per_cu_data *
2398 dwarf2_per_objfile::get_cu (int index)
2399 {
2400 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2401
2402 return this->all_comp_units[index];
2403 }
2404
2405 /* See declaration. */
2406
2407 signatured_type *
2408 dwarf2_per_objfile::get_tu (int index)
2409 {
2410 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2411
2412 return this->all_type_units[index];
2413 }
2414
2415 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2416 objfile_obstack, and constructed with the specified field
2417 values. */
2418
2419 static dwarf2_per_cu_data *
2420 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2421 struct dwarf2_section_info *section,
2422 int is_dwz,
2423 sect_offset sect_off, ULONGEST length)
2424 {
2425 struct objfile *objfile = dwarf2_per_objfile->objfile;
2426 dwarf2_per_cu_data *the_cu
2427 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2428 struct dwarf2_per_cu_data);
2429 the_cu->sect_off = sect_off;
2430 the_cu->length = length;
2431 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2432 the_cu->section = section;
2433 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2434 struct dwarf2_per_cu_quick_data);
2435 the_cu->is_dwz = is_dwz;
2436 return the_cu;
2437 }
2438
2439 /* A helper for create_cus_from_index that handles a given list of
2440 CUs. */
2441
2442 static void
2443 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2444 const gdb_byte *cu_list, offset_type n_elements,
2445 struct dwarf2_section_info *section,
2446 int is_dwz)
2447 {
2448 for (offset_type i = 0; i < n_elements; i += 2)
2449 {
2450 gdb_static_assert (sizeof (ULONGEST) >= 8);
2451
2452 sect_offset sect_off
2453 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2454 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2455 cu_list += 2 * 8;
2456
2457 dwarf2_per_cu_data *per_cu
2458 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2459 sect_off, length);
2460 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2461 }
2462 }
2463
2464 /* Read the CU list from the mapped index, and use it to create all
2465 the CU objects for this objfile. */
2466
2467 static void
2468 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2469 const gdb_byte *cu_list, offset_type cu_list_elements,
2470 const gdb_byte *dwz_list, offset_type dwz_elements)
2471 {
2472 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2473 dwarf2_per_objfile->all_comp_units.reserve
2474 ((cu_list_elements + dwz_elements) / 2);
2475
2476 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2477 &dwarf2_per_objfile->info, 0);
2478
2479 if (dwz_elements == 0)
2480 return;
2481
2482 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2483 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2484 &dwz->info, 1);
2485 }
2486
2487 /* Create the signatured type hash table from the index. */
2488
2489 static void
2490 create_signatured_type_table_from_index
2491 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2492 struct dwarf2_section_info *section,
2493 const gdb_byte *bytes,
2494 offset_type elements)
2495 {
2496 struct objfile *objfile = dwarf2_per_objfile->objfile;
2497
2498 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2499 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2500
2501 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2502
2503 for (offset_type i = 0; i < elements; i += 3)
2504 {
2505 struct signatured_type *sig_type;
2506 ULONGEST signature;
2507 void **slot;
2508 cu_offset type_offset_in_tu;
2509
2510 gdb_static_assert (sizeof (ULONGEST) >= 8);
2511 sect_offset sect_off
2512 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2513 type_offset_in_tu
2514 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2515 BFD_ENDIAN_LITTLE);
2516 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2517 bytes += 3 * 8;
2518
2519 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2520 struct signatured_type);
2521 sig_type->signature = signature;
2522 sig_type->type_offset_in_tu = type_offset_in_tu;
2523 sig_type->per_cu.is_debug_types = 1;
2524 sig_type->per_cu.section = section;
2525 sig_type->per_cu.sect_off = sect_off;
2526 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2527 sig_type->per_cu.v.quick
2528 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2529 struct dwarf2_per_cu_quick_data);
2530
2531 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2532 *slot = sig_type;
2533
2534 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2535 }
2536
2537 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2538 }
2539
2540 /* Create the signatured type hash table from .debug_names. */
2541
2542 static void
2543 create_signatured_type_table_from_debug_names
2544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2545 const mapped_debug_names &map,
2546 struct dwarf2_section_info *section,
2547 struct dwarf2_section_info *abbrev_section)
2548 {
2549 struct objfile *objfile = dwarf2_per_objfile->objfile;
2550
2551 section->read (objfile);
2552 abbrev_section->read (objfile);
2553
2554 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2555 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2556
2557 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2558
2559 for (uint32_t i = 0; i < map.tu_count; ++i)
2560 {
2561 struct signatured_type *sig_type;
2562 void **slot;
2563
2564 sect_offset sect_off
2565 = (sect_offset) (extract_unsigned_integer
2566 (map.tu_table_reordered + i * map.offset_size,
2567 map.offset_size,
2568 map.dwarf5_byte_order));
2569
2570 comp_unit_head cu_header;
2571 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2572 abbrev_section,
2573 section->buffer + to_underlying (sect_off),
2574 rcuh_kind::TYPE);
2575
2576 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2577 struct signatured_type);
2578 sig_type->signature = cu_header.signature;
2579 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2580 sig_type->per_cu.is_debug_types = 1;
2581 sig_type->per_cu.section = section;
2582 sig_type->per_cu.sect_off = sect_off;
2583 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2584 sig_type->per_cu.v.quick
2585 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2586 struct dwarf2_per_cu_quick_data);
2587
2588 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2589 *slot = sig_type;
2590
2591 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2592 }
2593
2594 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2595 }
2596
2597 /* Read the address map data from the mapped index, and use it to
2598 populate the objfile's psymtabs_addrmap. */
2599
2600 static void
2601 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2602 struct mapped_index *index)
2603 {
2604 struct objfile *objfile = dwarf2_per_objfile->objfile;
2605 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2606 const gdb_byte *iter, *end;
2607 struct addrmap *mutable_map;
2608 CORE_ADDR baseaddr;
2609
2610 auto_obstack temp_obstack;
2611
2612 mutable_map = addrmap_create_mutable (&temp_obstack);
2613
2614 iter = index->address_table.data ();
2615 end = iter + index->address_table.size ();
2616
2617 baseaddr = objfile->text_section_offset ();
2618
2619 while (iter < end)
2620 {
2621 ULONGEST hi, lo, cu_index;
2622 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2623 iter += 8;
2624 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2625 iter += 8;
2626 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2627 iter += 4;
2628
2629 if (lo > hi)
2630 {
2631 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2632 hex_string (lo), hex_string (hi));
2633 continue;
2634 }
2635
2636 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2637 {
2638 complaint (_(".gdb_index address table has invalid CU number %u"),
2639 (unsigned) cu_index);
2640 continue;
2641 }
2642
2643 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2644 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2645 addrmap_set_empty (mutable_map, lo, hi - 1,
2646 dwarf2_per_objfile->get_cu (cu_index));
2647 }
2648
2649 objfile->partial_symtabs->psymtabs_addrmap
2650 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2651 }
2652
2653 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2654 populate the objfile's psymtabs_addrmap. */
2655
2656 static void
2657 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2658 struct dwarf2_section_info *section)
2659 {
2660 struct objfile *objfile = dwarf2_per_objfile->objfile;
2661 bfd *abfd = objfile->obfd;
2662 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2663 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2664
2665 auto_obstack temp_obstack;
2666 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2667
2668 std::unordered_map<sect_offset,
2669 dwarf2_per_cu_data *,
2670 gdb::hash_enum<sect_offset>>
2671 debug_info_offset_to_per_cu;
2672 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2673 {
2674 const auto insertpair
2675 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2676 if (!insertpair.second)
2677 {
2678 warning (_("Section .debug_aranges in %s has duplicate "
2679 "debug_info_offset %s, ignoring .debug_aranges."),
2680 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2681 return;
2682 }
2683 }
2684
2685 section->read (objfile);
2686
2687 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2688
2689 const gdb_byte *addr = section->buffer;
2690
2691 while (addr < section->buffer + section->size)
2692 {
2693 const gdb_byte *const entry_addr = addr;
2694 unsigned int bytes_read;
2695
2696 const LONGEST entry_length = read_initial_length (abfd, addr,
2697 &bytes_read);
2698 addr += bytes_read;
2699
2700 const gdb_byte *const entry_end = addr + entry_length;
2701 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2702 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2703 if (addr + entry_length > section->buffer + section->size)
2704 {
2705 warning (_("Section .debug_aranges in %s entry at offset %s "
2706 "length %s exceeds section length %s, "
2707 "ignoring .debug_aranges."),
2708 objfile_name (objfile),
2709 plongest (entry_addr - section->buffer),
2710 plongest (bytes_read + entry_length),
2711 pulongest (section->size));
2712 return;
2713 }
2714
2715 /* The version number. */
2716 const uint16_t version = read_2_bytes (abfd, addr);
2717 addr += 2;
2718 if (version != 2)
2719 {
2720 warning (_("Section .debug_aranges in %s entry at offset %s "
2721 "has unsupported version %d, ignoring .debug_aranges."),
2722 objfile_name (objfile),
2723 plongest (entry_addr - section->buffer), version);
2724 return;
2725 }
2726
2727 const uint64_t debug_info_offset
2728 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2729 addr += offset_size;
2730 const auto per_cu_it
2731 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2732 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2733 {
2734 warning (_("Section .debug_aranges in %s entry at offset %s "
2735 "debug_info_offset %s does not exists, "
2736 "ignoring .debug_aranges."),
2737 objfile_name (objfile),
2738 plongest (entry_addr - section->buffer),
2739 pulongest (debug_info_offset));
2740 return;
2741 }
2742 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2743
2744 const uint8_t address_size = *addr++;
2745 if (address_size < 1 || address_size > 8)
2746 {
2747 warning (_("Section .debug_aranges in %s entry at offset %s "
2748 "address_size %u is invalid, ignoring .debug_aranges."),
2749 objfile_name (objfile),
2750 plongest (entry_addr - section->buffer), address_size);
2751 return;
2752 }
2753
2754 const uint8_t segment_selector_size = *addr++;
2755 if (segment_selector_size != 0)
2756 {
2757 warning (_("Section .debug_aranges in %s entry at offset %s "
2758 "segment_selector_size %u is not supported, "
2759 "ignoring .debug_aranges."),
2760 objfile_name (objfile),
2761 plongest (entry_addr - section->buffer),
2762 segment_selector_size);
2763 return;
2764 }
2765
2766 /* Must pad to an alignment boundary that is twice the address
2767 size. It is undocumented by the DWARF standard but GCC does
2768 use it. */
2769 for (size_t padding = ((-(addr - section->buffer))
2770 & (2 * address_size - 1));
2771 padding > 0; padding--)
2772 if (*addr++ != 0)
2773 {
2774 warning (_("Section .debug_aranges in %s entry at offset %s "
2775 "padding is not zero, ignoring .debug_aranges."),
2776 objfile_name (objfile),
2777 plongest (entry_addr - section->buffer));
2778 return;
2779 }
2780
2781 for (;;)
2782 {
2783 if (addr + 2 * address_size > entry_end)
2784 {
2785 warning (_("Section .debug_aranges in %s entry at offset %s "
2786 "address list is not properly terminated, "
2787 "ignoring .debug_aranges."),
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer));
2790 return;
2791 }
2792 ULONGEST start = extract_unsigned_integer (addr, address_size,
2793 dwarf5_byte_order);
2794 addr += address_size;
2795 ULONGEST length = extract_unsigned_integer (addr, address_size,
2796 dwarf5_byte_order);
2797 addr += address_size;
2798 if (start == 0 && length == 0)
2799 break;
2800 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2801 {
2802 /* Symbol was eliminated due to a COMDAT group. */
2803 continue;
2804 }
2805 ULONGEST end = start + length;
2806 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2807 - baseaddr);
2808 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2809 - baseaddr);
2810 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2811 }
2812 }
2813
2814 objfile->partial_symtabs->psymtabs_addrmap
2815 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2816 }
2817
2818 /* Find a slot in the mapped index INDEX for the object named NAME.
2819 If NAME is found, set *VEC_OUT to point to the CU vector in the
2820 constant pool and return true. If NAME cannot be found, return
2821 false. */
2822
2823 static bool
2824 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2825 offset_type **vec_out)
2826 {
2827 offset_type hash;
2828 offset_type slot, step;
2829 int (*cmp) (const char *, const char *);
2830
2831 gdb::unique_xmalloc_ptr<char> without_params;
2832 if (current_language->la_language == language_cplus
2833 || current_language->la_language == language_fortran
2834 || current_language->la_language == language_d)
2835 {
2836 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2837 not contain any. */
2838
2839 if (strchr (name, '(') != NULL)
2840 {
2841 without_params = cp_remove_params (name);
2842
2843 if (without_params != NULL)
2844 name = without_params.get ();
2845 }
2846 }
2847
2848 /* Index version 4 did not support case insensitive searches. But the
2849 indices for case insensitive languages are built in lowercase, therefore
2850 simulate our NAME being searched is also lowercased. */
2851 hash = mapped_index_string_hash ((index->version == 4
2852 && case_sensitivity == case_sensitive_off
2853 ? 5 : index->version),
2854 name);
2855
2856 slot = hash & (index->symbol_table.size () - 1);
2857 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2858 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2859
2860 for (;;)
2861 {
2862 const char *str;
2863
2864 const auto &bucket = index->symbol_table[slot];
2865 if (bucket.name == 0 && bucket.vec == 0)
2866 return false;
2867
2868 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2869 if (!cmp (name, str))
2870 {
2871 *vec_out = (offset_type *) (index->constant_pool
2872 + MAYBE_SWAP (bucket.vec));
2873 return true;
2874 }
2875
2876 slot = (slot + step) & (index->symbol_table.size () - 1);
2877 }
2878 }
2879
2880 /* A helper function that reads the .gdb_index from BUFFER and fills
2881 in MAP. FILENAME is the name of the file containing the data;
2882 it is used for error reporting. DEPRECATED_OK is true if it is
2883 ok to use deprecated sections.
2884
2885 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2886 out parameters that are filled in with information about the CU and
2887 TU lists in the section.
2888
2889 Returns true if all went well, false otherwise. */
2890
2891 static bool
2892 read_gdb_index_from_buffer (struct objfile *objfile,
2893 const char *filename,
2894 bool deprecated_ok,
2895 gdb::array_view<const gdb_byte> buffer,
2896 struct mapped_index *map,
2897 const gdb_byte **cu_list,
2898 offset_type *cu_list_elements,
2899 const gdb_byte **types_list,
2900 offset_type *types_list_elements)
2901 {
2902 const gdb_byte *addr = &buffer[0];
2903
2904 /* Version check. */
2905 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2906 /* Versions earlier than 3 emitted every copy of a psymbol. This
2907 causes the index to behave very poorly for certain requests. Version 3
2908 contained incomplete addrmap. So, it seems better to just ignore such
2909 indices. */
2910 if (version < 4)
2911 {
2912 static int warning_printed = 0;
2913 if (!warning_printed)
2914 {
2915 warning (_("Skipping obsolete .gdb_index section in %s."),
2916 filename);
2917 warning_printed = 1;
2918 }
2919 return 0;
2920 }
2921 /* Index version 4 uses a different hash function than index version
2922 5 and later.
2923
2924 Versions earlier than 6 did not emit psymbols for inlined
2925 functions. Using these files will cause GDB not to be able to
2926 set breakpoints on inlined functions by name, so we ignore these
2927 indices unless the user has done
2928 "set use-deprecated-index-sections on". */
2929 if (version < 6 && !deprecated_ok)
2930 {
2931 static int warning_printed = 0;
2932 if (!warning_printed)
2933 {
2934 warning (_("\
2935 Skipping deprecated .gdb_index section in %s.\n\
2936 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2937 to use the section anyway."),
2938 filename);
2939 warning_printed = 1;
2940 }
2941 return 0;
2942 }
2943 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2944 of the TU (for symbols coming from TUs),
2945 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2946 Plus gold-generated indices can have duplicate entries for global symbols,
2947 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2948 These are just performance bugs, and we can't distinguish gdb-generated
2949 indices from gold-generated ones, so issue no warning here. */
2950
2951 /* Indexes with higher version than the one supported by GDB may be no
2952 longer backward compatible. */
2953 if (version > 8)
2954 return 0;
2955
2956 map->version = version;
2957
2958 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2959
2960 int i = 0;
2961 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2962 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2963 / 8);
2964 ++i;
2965
2966 *types_list = addr + MAYBE_SWAP (metadata[i]);
2967 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2968 - MAYBE_SWAP (metadata[i]))
2969 / 8);
2970 ++i;
2971
2972 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2973 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2974 map->address_table
2975 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2976 ++i;
2977
2978 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2979 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2980 map->symbol_table
2981 = gdb::array_view<mapped_index::symbol_table_slot>
2982 ((mapped_index::symbol_table_slot *) symbol_table,
2983 (mapped_index::symbol_table_slot *) symbol_table_end);
2984
2985 ++i;
2986 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2987
2988 return 1;
2989 }
2990
2991 /* Callback types for dwarf2_read_gdb_index. */
2992
2993 typedef gdb::function_view
2994 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2995 get_gdb_index_contents_ftype;
2996 typedef gdb::function_view
2997 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2998 get_gdb_index_contents_dwz_ftype;
2999
3000 /* Read .gdb_index. If everything went ok, initialize the "quick"
3001 elements of all the CUs and return 1. Otherwise, return 0. */
3002
3003 static int
3004 dwarf2_read_gdb_index
3005 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3006 get_gdb_index_contents_ftype get_gdb_index_contents,
3007 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3008 {
3009 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3010 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3011 struct dwz_file *dwz;
3012 struct objfile *objfile = dwarf2_per_objfile->objfile;
3013
3014 gdb::array_view<const gdb_byte> main_index_contents
3015 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3016
3017 if (main_index_contents.empty ())
3018 return 0;
3019
3020 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3021 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3022 use_deprecated_index_sections,
3023 main_index_contents, map.get (), &cu_list,
3024 &cu_list_elements, &types_list,
3025 &types_list_elements))
3026 return 0;
3027
3028 /* Don't use the index if it's empty. */
3029 if (map->symbol_table.empty ())
3030 return 0;
3031
3032 /* If there is a .dwz file, read it so we can get its CU list as
3033 well. */
3034 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3035 if (dwz != NULL)
3036 {
3037 struct mapped_index dwz_map;
3038 const gdb_byte *dwz_types_ignore;
3039 offset_type dwz_types_elements_ignore;
3040
3041 gdb::array_view<const gdb_byte> dwz_index_content
3042 = get_gdb_index_contents_dwz (objfile, dwz);
3043
3044 if (dwz_index_content.empty ())
3045 return 0;
3046
3047 if (!read_gdb_index_from_buffer (objfile,
3048 bfd_get_filename (dwz->dwz_bfd.get ()),
3049 1, dwz_index_content, &dwz_map,
3050 &dwz_list, &dwz_list_elements,
3051 &dwz_types_ignore,
3052 &dwz_types_elements_ignore))
3053 {
3054 warning (_("could not read '.gdb_index' section from %s; skipping"),
3055 bfd_get_filename (dwz->dwz_bfd.get ()));
3056 return 0;
3057 }
3058 }
3059
3060 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3061 dwz_list, dwz_list_elements);
3062
3063 if (types_list_elements)
3064 {
3065 /* We can only handle a single .debug_types when we have an
3066 index. */
3067 if (dwarf2_per_objfile->types.size () != 1)
3068 return 0;
3069
3070 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3071
3072 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3073 types_list, types_list_elements);
3074 }
3075
3076 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3077
3078 dwarf2_per_objfile->index_table = std::move (map);
3079 dwarf2_per_objfile->using_index = 1;
3080 dwarf2_per_objfile->quick_file_names_table =
3081 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3082
3083 return 1;
3084 }
3085
3086 /* die_reader_func for dw2_get_file_names. */
3087
3088 static void
3089 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3090 const gdb_byte *info_ptr,
3091 struct die_info *comp_unit_die)
3092 {
3093 struct dwarf2_cu *cu = reader->cu;
3094 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3095 struct dwarf2_per_objfile *dwarf2_per_objfile
3096 = cu->per_cu->dwarf2_per_objfile;
3097 struct objfile *objfile = dwarf2_per_objfile->objfile;
3098 struct dwarf2_per_cu_data *lh_cu;
3099 struct attribute *attr;
3100 void **slot;
3101 struct quick_file_names *qfn;
3102
3103 gdb_assert (! this_cu->is_debug_types);
3104
3105 /* Our callers never want to match partial units -- instead they
3106 will match the enclosing full CU. */
3107 if (comp_unit_die->tag == DW_TAG_partial_unit)
3108 {
3109 this_cu->v.quick->no_file_data = 1;
3110 return;
3111 }
3112
3113 lh_cu = this_cu;
3114 slot = NULL;
3115
3116 line_header_up lh;
3117 sect_offset line_offset {};
3118
3119 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3120 if (attr != nullptr)
3121 {
3122 struct quick_file_names find_entry;
3123
3124 line_offset = (sect_offset) DW_UNSND (attr);
3125
3126 /* We may have already read in this line header (TU line header sharing).
3127 If we have we're done. */
3128 find_entry.hash.dwo_unit = cu->dwo_unit;
3129 find_entry.hash.line_sect_off = line_offset;
3130 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3131 &find_entry, INSERT);
3132 if (*slot != NULL)
3133 {
3134 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3135 return;
3136 }
3137
3138 lh = dwarf_decode_line_header (line_offset, cu);
3139 }
3140 if (lh == NULL)
3141 {
3142 lh_cu->v.quick->no_file_data = 1;
3143 return;
3144 }
3145
3146 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3147 qfn->hash.dwo_unit = cu->dwo_unit;
3148 qfn->hash.line_sect_off = line_offset;
3149 gdb_assert (slot != NULL);
3150 *slot = qfn;
3151
3152 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3153
3154 int offset = 0;
3155 if (strcmp (fnd.name, "<unknown>") != 0)
3156 ++offset;
3157
3158 qfn->num_file_names = offset + lh->file_names_size ();
3159 qfn->file_names =
3160 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3161 if (offset != 0)
3162 qfn->file_names[0] = xstrdup (fnd.name);
3163 for (int i = 0; i < lh->file_names_size (); ++i)
3164 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3165 fnd.comp_dir).release ();
3166 qfn->real_names = NULL;
3167
3168 lh_cu->v.quick->file_names = qfn;
3169 }
3170
3171 /* A helper for the "quick" functions which attempts to read the line
3172 table for THIS_CU. */
3173
3174 static struct quick_file_names *
3175 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3176 {
3177 /* This should never be called for TUs. */
3178 gdb_assert (! this_cu->is_debug_types);
3179 /* Nor type unit groups. */
3180 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3181
3182 if (this_cu->v.quick->file_names != NULL)
3183 return this_cu->v.quick->file_names;
3184 /* If we know there is no line data, no point in looking again. */
3185 if (this_cu->v.quick->no_file_data)
3186 return NULL;
3187
3188 cutu_reader reader (this_cu);
3189 if (!reader.dummy_p)
3190 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3191
3192 if (this_cu->v.quick->no_file_data)
3193 return NULL;
3194 return this_cu->v.quick->file_names;
3195 }
3196
3197 /* A helper for the "quick" functions which computes and caches the
3198 real path for a given file name from the line table. */
3199
3200 static const char *
3201 dw2_get_real_path (struct objfile *objfile,
3202 struct quick_file_names *qfn, int index)
3203 {
3204 if (qfn->real_names == NULL)
3205 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3206 qfn->num_file_names, const char *);
3207
3208 if (qfn->real_names[index] == NULL)
3209 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3210
3211 return qfn->real_names[index];
3212 }
3213
3214 static struct symtab *
3215 dw2_find_last_source_symtab (struct objfile *objfile)
3216 {
3217 struct dwarf2_per_objfile *dwarf2_per_objfile
3218 = get_dwarf2_per_objfile (objfile);
3219 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3220 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3221
3222 if (cust == NULL)
3223 return NULL;
3224
3225 return compunit_primary_filetab (cust);
3226 }
3227
3228 /* Traversal function for dw2_forget_cached_source_info. */
3229
3230 static int
3231 dw2_free_cached_file_names (void **slot, void *info)
3232 {
3233 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3234
3235 if (file_data->real_names)
3236 {
3237 int i;
3238
3239 for (i = 0; i < file_data->num_file_names; ++i)
3240 {
3241 xfree ((void*) file_data->real_names[i]);
3242 file_data->real_names[i] = NULL;
3243 }
3244 }
3245
3246 return 1;
3247 }
3248
3249 static void
3250 dw2_forget_cached_source_info (struct objfile *objfile)
3251 {
3252 struct dwarf2_per_objfile *dwarf2_per_objfile
3253 = get_dwarf2_per_objfile (objfile);
3254
3255 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3256 dw2_free_cached_file_names, NULL);
3257 }
3258
3259 /* Helper function for dw2_map_symtabs_matching_filename that expands
3260 the symtabs and calls the iterator. */
3261
3262 static int
3263 dw2_map_expand_apply (struct objfile *objfile,
3264 struct dwarf2_per_cu_data *per_cu,
3265 const char *name, const char *real_path,
3266 gdb::function_view<bool (symtab *)> callback)
3267 {
3268 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3269
3270 /* Don't visit already-expanded CUs. */
3271 if (per_cu->v.quick->compunit_symtab)
3272 return 0;
3273
3274 /* This may expand more than one symtab, and we want to iterate over
3275 all of them. */
3276 dw2_instantiate_symtab (per_cu, false);
3277
3278 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3279 last_made, callback);
3280 }
3281
3282 /* Implementation of the map_symtabs_matching_filename method. */
3283
3284 static bool
3285 dw2_map_symtabs_matching_filename
3286 (struct objfile *objfile, const char *name, const char *real_path,
3287 gdb::function_view<bool (symtab *)> callback)
3288 {
3289 const char *name_basename = lbasename (name);
3290 struct dwarf2_per_objfile *dwarf2_per_objfile
3291 = get_dwarf2_per_objfile (objfile);
3292
3293 /* The rule is CUs specify all the files, including those used by
3294 any TU, so there's no need to scan TUs here. */
3295
3296 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3297 {
3298 /* We only need to look at symtabs not already expanded. */
3299 if (per_cu->v.quick->compunit_symtab)
3300 continue;
3301
3302 quick_file_names *file_data = dw2_get_file_names (per_cu);
3303 if (file_data == NULL)
3304 continue;
3305
3306 for (int j = 0; j < file_data->num_file_names; ++j)
3307 {
3308 const char *this_name = file_data->file_names[j];
3309 const char *this_real_name;
3310
3311 if (compare_filenames_for_search (this_name, name))
3312 {
3313 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3314 callback))
3315 return true;
3316 continue;
3317 }
3318
3319 /* Before we invoke realpath, which can get expensive when many
3320 files are involved, do a quick comparison of the basenames. */
3321 if (! basenames_may_differ
3322 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3323 continue;
3324
3325 this_real_name = dw2_get_real_path (objfile, file_data, j);
3326 if (compare_filenames_for_search (this_real_name, name))
3327 {
3328 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3329 callback))
3330 return true;
3331 continue;
3332 }
3333
3334 if (real_path != NULL)
3335 {
3336 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3337 gdb_assert (IS_ABSOLUTE_PATH (name));
3338 if (this_real_name != NULL
3339 && FILENAME_CMP (real_path, this_real_name) == 0)
3340 {
3341 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3342 callback))
3343 return true;
3344 continue;
3345 }
3346 }
3347 }
3348 }
3349
3350 return false;
3351 }
3352
3353 /* Struct used to manage iterating over all CUs looking for a symbol. */
3354
3355 struct dw2_symtab_iterator
3356 {
3357 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3358 struct dwarf2_per_objfile *dwarf2_per_objfile;
3359 /* If set, only look for symbols that match that block. Valid values are
3360 GLOBAL_BLOCK and STATIC_BLOCK. */
3361 gdb::optional<block_enum> block_index;
3362 /* The kind of symbol we're looking for. */
3363 domain_enum domain;
3364 /* The list of CUs from the index entry of the symbol,
3365 or NULL if not found. */
3366 offset_type *vec;
3367 /* The next element in VEC to look at. */
3368 int next;
3369 /* The number of elements in VEC, or zero if there is no match. */
3370 int length;
3371 /* Have we seen a global version of the symbol?
3372 If so we can ignore all further global instances.
3373 This is to work around gold/15646, inefficient gold-generated
3374 indices. */
3375 int global_seen;
3376 };
3377
3378 /* Initialize the index symtab iterator ITER. */
3379
3380 static void
3381 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3382 struct dwarf2_per_objfile *dwarf2_per_objfile,
3383 gdb::optional<block_enum> block_index,
3384 domain_enum domain,
3385 const char *name)
3386 {
3387 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3388 iter->block_index = block_index;
3389 iter->domain = domain;
3390 iter->next = 0;
3391 iter->global_seen = 0;
3392
3393 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3394
3395 /* index is NULL if OBJF_READNOW. */
3396 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3397 iter->length = MAYBE_SWAP (*iter->vec);
3398 else
3399 {
3400 iter->vec = NULL;
3401 iter->length = 0;
3402 }
3403 }
3404
3405 /* Return the next matching CU or NULL if there are no more. */
3406
3407 static struct dwarf2_per_cu_data *
3408 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3409 {
3410 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3411
3412 for ( ; iter->next < iter->length; ++iter->next)
3413 {
3414 offset_type cu_index_and_attrs =
3415 MAYBE_SWAP (iter->vec[iter->next + 1]);
3416 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3417 gdb_index_symbol_kind symbol_kind =
3418 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3419 /* Only check the symbol attributes if they're present.
3420 Indices prior to version 7 don't record them,
3421 and indices >= 7 may elide them for certain symbols
3422 (gold does this). */
3423 int attrs_valid =
3424 (dwarf2_per_objfile->index_table->version >= 7
3425 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3426
3427 /* Don't crash on bad data. */
3428 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3429 + dwarf2_per_objfile->all_type_units.size ()))
3430 {
3431 complaint (_(".gdb_index entry has bad CU index"
3432 " [in module %s]"),
3433 objfile_name (dwarf2_per_objfile->objfile));
3434 continue;
3435 }
3436
3437 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3438
3439 /* Skip if already read in. */
3440 if (per_cu->v.quick->compunit_symtab)
3441 continue;
3442
3443 /* Check static vs global. */
3444 if (attrs_valid)
3445 {
3446 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3447
3448 if (iter->block_index.has_value ())
3449 {
3450 bool want_static = *iter->block_index == STATIC_BLOCK;
3451
3452 if (is_static != want_static)
3453 continue;
3454 }
3455
3456 /* Work around gold/15646. */
3457 if (!is_static && iter->global_seen)
3458 continue;
3459 if (!is_static)
3460 iter->global_seen = 1;
3461 }
3462
3463 /* Only check the symbol's kind if it has one. */
3464 if (attrs_valid)
3465 {
3466 switch (iter->domain)
3467 {
3468 case VAR_DOMAIN:
3469 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3471 /* Some types are also in VAR_DOMAIN. */
3472 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3473 continue;
3474 break;
3475 case STRUCT_DOMAIN:
3476 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3477 continue;
3478 break;
3479 case LABEL_DOMAIN:
3480 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3481 continue;
3482 break;
3483 case MODULE_DOMAIN:
3484 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3485 continue;
3486 break;
3487 default:
3488 break;
3489 }
3490 }
3491
3492 ++iter->next;
3493 return per_cu;
3494 }
3495
3496 return NULL;
3497 }
3498
3499 static struct compunit_symtab *
3500 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3501 const char *name, domain_enum domain)
3502 {
3503 struct compunit_symtab *stab_best = NULL;
3504 struct dwarf2_per_objfile *dwarf2_per_objfile
3505 = get_dwarf2_per_objfile (objfile);
3506
3507 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3508
3509 struct dw2_symtab_iterator iter;
3510 struct dwarf2_per_cu_data *per_cu;
3511
3512 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3513
3514 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3515 {
3516 struct symbol *sym, *with_opaque = NULL;
3517 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3518 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3519 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3520
3521 sym = block_find_symbol (block, name, domain,
3522 block_find_non_opaque_type_preferred,
3523 &with_opaque);
3524
3525 /* Some caution must be observed with overloaded functions
3526 and methods, since the index will not contain any overload
3527 information (but NAME might contain it). */
3528
3529 if (sym != NULL
3530 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3531 return stab;
3532 if (with_opaque != NULL
3533 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3534 stab_best = stab;
3535
3536 /* Keep looking through other CUs. */
3537 }
3538
3539 return stab_best;
3540 }
3541
3542 static void
3543 dw2_print_stats (struct objfile *objfile)
3544 {
3545 struct dwarf2_per_objfile *dwarf2_per_objfile
3546 = get_dwarf2_per_objfile (objfile);
3547 int total = (dwarf2_per_objfile->all_comp_units.size ()
3548 + dwarf2_per_objfile->all_type_units.size ());
3549 int count = 0;
3550
3551 for (int i = 0; i < total; ++i)
3552 {
3553 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3554
3555 if (!per_cu->v.quick->compunit_symtab)
3556 ++count;
3557 }
3558 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3559 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3560 }
3561
3562 /* This dumps minimal information about the index.
3563 It is called via "mt print objfiles".
3564 One use is to verify .gdb_index has been loaded by the
3565 gdb.dwarf2/gdb-index.exp testcase. */
3566
3567 static void
3568 dw2_dump (struct objfile *objfile)
3569 {
3570 struct dwarf2_per_objfile *dwarf2_per_objfile
3571 = get_dwarf2_per_objfile (objfile);
3572
3573 gdb_assert (dwarf2_per_objfile->using_index);
3574 printf_filtered (".gdb_index:");
3575 if (dwarf2_per_objfile->index_table != NULL)
3576 {
3577 printf_filtered (" version %d\n",
3578 dwarf2_per_objfile->index_table->version);
3579 }
3580 else
3581 printf_filtered (" faked for \"readnow\"\n");
3582 printf_filtered ("\n");
3583 }
3584
3585 static void
3586 dw2_expand_symtabs_for_function (struct objfile *objfile,
3587 const char *func_name)
3588 {
3589 struct dwarf2_per_objfile *dwarf2_per_objfile
3590 = get_dwarf2_per_objfile (objfile);
3591
3592 struct dw2_symtab_iterator iter;
3593 struct dwarf2_per_cu_data *per_cu;
3594
3595 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3596
3597 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3598 dw2_instantiate_symtab (per_cu, false);
3599
3600 }
3601
3602 static void
3603 dw2_expand_all_symtabs (struct objfile *objfile)
3604 {
3605 struct dwarf2_per_objfile *dwarf2_per_objfile
3606 = get_dwarf2_per_objfile (objfile);
3607 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3608 + dwarf2_per_objfile->all_type_units.size ());
3609
3610 for (int i = 0; i < total_units; ++i)
3611 {
3612 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3613
3614 /* We don't want to directly expand a partial CU, because if we
3615 read it with the wrong language, then assertion failures can
3616 be triggered later on. See PR symtab/23010. So, tell
3617 dw2_instantiate_symtab to skip partial CUs -- any important
3618 partial CU will be read via DW_TAG_imported_unit anyway. */
3619 dw2_instantiate_symtab (per_cu, true);
3620 }
3621 }
3622
3623 static void
3624 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3625 const char *fullname)
3626 {
3627 struct dwarf2_per_objfile *dwarf2_per_objfile
3628 = get_dwarf2_per_objfile (objfile);
3629
3630 /* We don't need to consider type units here.
3631 This is only called for examining code, e.g. expand_line_sal.
3632 There can be an order of magnitude (or more) more type units
3633 than comp units, and we avoid them if we can. */
3634
3635 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3636 {
3637 /* We only need to look at symtabs not already expanded. */
3638 if (per_cu->v.quick->compunit_symtab)
3639 continue;
3640
3641 quick_file_names *file_data = dw2_get_file_names (per_cu);
3642 if (file_data == NULL)
3643 continue;
3644
3645 for (int j = 0; j < file_data->num_file_names; ++j)
3646 {
3647 const char *this_fullname = file_data->file_names[j];
3648
3649 if (filename_cmp (this_fullname, fullname) == 0)
3650 {
3651 dw2_instantiate_symtab (per_cu, false);
3652 break;
3653 }
3654 }
3655 }
3656 }
3657
3658 static void
3659 dw2_map_matching_symbols
3660 (struct objfile *objfile,
3661 const lookup_name_info &name, domain_enum domain,
3662 int global,
3663 gdb::function_view<symbol_found_callback_ftype> callback,
3664 symbol_compare_ftype *ordered_compare)
3665 {
3666 /* Currently unimplemented; used for Ada. The function can be called if the
3667 current language is Ada for a non-Ada objfile using GNU index. As Ada
3668 does not look for non-Ada symbols this function should just return. */
3669 }
3670
3671 /* Starting from a search name, return the string that finds the upper
3672 bound of all strings that start with SEARCH_NAME in a sorted name
3673 list. Returns the empty string to indicate that the upper bound is
3674 the end of the list. */
3675
3676 static std::string
3677 make_sort_after_prefix_name (const char *search_name)
3678 {
3679 /* When looking to complete "func", we find the upper bound of all
3680 symbols that start with "func" by looking for where we'd insert
3681 the closest string that would follow "func" in lexicographical
3682 order. Usually, that's "func"-with-last-character-incremented,
3683 i.e. "fund". Mind non-ASCII characters, though. Usually those
3684 will be UTF-8 multi-byte sequences, but we can't be certain.
3685 Especially mind the 0xff character, which is a valid character in
3686 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3687 rule out compilers allowing it in identifiers. Note that
3688 conveniently, strcmp/strcasecmp are specified to compare
3689 characters interpreted as unsigned char. So what we do is treat
3690 the whole string as a base 256 number composed of a sequence of
3691 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3692 to 0, and carries 1 to the following more-significant position.
3693 If the very first character in SEARCH_NAME ends up incremented
3694 and carries/overflows, then the upper bound is the end of the
3695 list. The string after the empty string is also the empty
3696 string.
3697
3698 Some examples of this operation:
3699
3700 SEARCH_NAME => "+1" RESULT
3701
3702 "abc" => "abd"
3703 "ab\xff" => "ac"
3704 "\xff" "a" "\xff" => "\xff" "b"
3705 "\xff" => ""
3706 "\xff\xff" => ""
3707 "" => ""
3708
3709 Then, with these symbols for example:
3710
3711 func
3712 func1
3713 fund
3714
3715 completing "func" looks for symbols between "func" and
3716 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3717 which finds "func" and "func1", but not "fund".
3718
3719 And with:
3720
3721 funcÿ (Latin1 'ÿ' [0xff])
3722 funcÿ1
3723 fund
3724
3725 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3726 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3727
3728 And with:
3729
3730 ÿÿ (Latin1 'ÿ' [0xff])
3731 ÿÿ1
3732
3733 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3734 the end of the list.
3735 */
3736 std::string after = search_name;
3737 while (!after.empty () && (unsigned char) after.back () == 0xff)
3738 after.pop_back ();
3739 if (!after.empty ())
3740 after.back () = (unsigned char) after.back () + 1;
3741 return after;
3742 }
3743
3744 /* See declaration. */
3745
3746 std::pair<std::vector<name_component>::const_iterator,
3747 std::vector<name_component>::const_iterator>
3748 mapped_index_base::find_name_components_bounds
3749 (const lookup_name_info &lookup_name_without_params, language lang) const
3750 {
3751 auto *name_cmp
3752 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3753
3754 const char *lang_name
3755 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3756
3757 /* Comparison function object for lower_bound that matches against a
3758 given symbol name. */
3759 auto lookup_compare_lower = [&] (const name_component &elem,
3760 const char *name)
3761 {
3762 const char *elem_qualified = this->symbol_name_at (elem.idx);
3763 const char *elem_name = elem_qualified + elem.name_offset;
3764 return name_cmp (elem_name, name) < 0;
3765 };
3766
3767 /* Comparison function object for upper_bound that matches against a
3768 given symbol name. */
3769 auto lookup_compare_upper = [&] (const char *name,
3770 const name_component &elem)
3771 {
3772 const char *elem_qualified = this->symbol_name_at (elem.idx);
3773 const char *elem_name = elem_qualified + elem.name_offset;
3774 return name_cmp (name, elem_name) < 0;
3775 };
3776
3777 auto begin = this->name_components.begin ();
3778 auto end = this->name_components.end ();
3779
3780 /* Find the lower bound. */
3781 auto lower = [&] ()
3782 {
3783 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3784 return begin;
3785 else
3786 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3787 } ();
3788
3789 /* Find the upper bound. */
3790 auto upper = [&] ()
3791 {
3792 if (lookup_name_without_params.completion_mode ())
3793 {
3794 /* In completion mode, we want UPPER to point past all
3795 symbols names that have the same prefix. I.e., with
3796 these symbols, and completing "func":
3797
3798 function << lower bound
3799 function1
3800 other_function << upper bound
3801
3802 We find the upper bound by looking for the insertion
3803 point of "func"-with-last-character-incremented,
3804 i.e. "fund". */
3805 std::string after = make_sort_after_prefix_name (lang_name);
3806 if (after.empty ())
3807 return end;
3808 return std::lower_bound (lower, end, after.c_str (),
3809 lookup_compare_lower);
3810 }
3811 else
3812 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3813 } ();
3814
3815 return {lower, upper};
3816 }
3817
3818 /* See declaration. */
3819
3820 void
3821 mapped_index_base::build_name_components ()
3822 {
3823 if (!this->name_components.empty ())
3824 return;
3825
3826 this->name_components_casing = case_sensitivity;
3827 auto *name_cmp
3828 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3829
3830 /* The code below only knows how to break apart components of C++
3831 symbol names (and other languages that use '::' as
3832 namespace/module separator) and Ada symbol names. */
3833 auto count = this->symbol_name_count ();
3834 for (offset_type idx = 0; idx < count; idx++)
3835 {
3836 if (this->symbol_name_slot_invalid (idx))
3837 continue;
3838
3839 const char *name = this->symbol_name_at (idx);
3840
3841 /* Add each name component to the name component table. */
3842 unsigned int previous_len = 0;
3843
3844 if (strstr (name, "::") != nullptr)
3845 {
3846 for (unsigned int current_len = cp_find_first_component (name);
3847 name[current_len] != '\0';
3848 current_len += cp_find_first_component (name + current_len))
3849 {
3850 gdb_assert (name[current_len] == ':');
3851 this->name_components.push_back ({previous_len, idx});
3852 /* Skip the '::'. */
3853 current_len += 2;
3854 previous_len = current_len;
3855 }
3856 }
3857 else
3858 {
3859 /* Handle the Ada encoded (aka mangled) form here. */
3860 for (const char *iter = strstr (name, "__");
3861 iter != nullptr;
3862 iter = strstr (iter, "__"))
3863 {
3864 this->name_components.push_back ({previous_len, idx});
3865 iter += 2;
3866 previous_len = iter - name;
3867 }
3868 }
3869
3870 this->name_components.push_back ({previous_len, idx});
3871 }
3872
3873 /* Sort name_components elements by name. */
3874 auto name_comp_compare = [&] (const name_component &left,
3875 const name_component &right)
3876 {
3877 const char *left_qualified = this->symbol_name_at (left.idx);
3878 const char *right_qualified = this->symbol_name_at (right.idx);
3879
3880 const char *left_name = left_qualified + left.name_offset;
3881 const char *right_name = right_qualified + right.name_offset;
3882
3883 return name_cmp (left_name, right_name) < 0;
3884 };
3885
3886 std::sort (this->name_components.begin (),
3887 this->name_components.end (),
3888 name_comp_compare);
3889 }
3890
3891 /* Helper for dw2_expand_symtabs_matching that works with a
3892 mapped_index_base instead of the containing objfile. This is split
3893 to a separate function in order to be able to unit test the
3894 name_components matching using a mock mapped_index_base. For each
3895 symbol name that matches, calls MATCH_CALLBACK, passing it the
3896 symbol's index in the mapped_index_base symbol table. */
3897
3898 static void
3899 dw2_expand_symtabs_matching_symbol
3900 (mapped_index_base &index,
3901 const lookup_name_info &lookup_name_in,
3902 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3903 enum search_domain kind,
3904 gdb::function_view<bool (offset_type)> match_callback)
3905 {
3906 lookup_name_info lookup_name_without_params
3907 = lookup_name_in.make_ignore_params ();
3908
3909 /* Build the symbol name component sorted vector, if we haven't
3910 yet. */
3911 index.build_name_components ();
3912
3913 /* The same symbol may appear more than once in the range though.
3914 E.g., if we're looking for symbols that complete "w", and we have
3915 a symbol named "w1::w2", we'll find the two name components for
3916 that same symbol in the range. To be sure we only call the
3917 callback once per symbol, we first collect the symbol name
3918 indexes that matched in a temporary vector and ignore
3919 duplicates. */
3920 std::vector<offset_type> matches;
3921
3922 struct name_and_matcher
3923 {
3924 symbol_name_matcher_ftype *matcher;
3925 const std::string &name;
3926
3927 bool operator== (const name_and_matcher &other) const
3928 {
3929 return matcher == other.matcher && name == other.name;
3930 }
3931 };
3932
3933 /* A vector holding all the different symbol name matchers, for all
3934 languages. */
3935 std::vector<name_and_matcher> matchers;
3936
3937 for (int i = 0; i < nr_languages; i++)
3938 {
3939 enum language lang_e = (enum language) i;
3940
3941 const language_defn *lang = language_def (lang_e);
3942 symbol_name_matcher_ftype *name_matcher
3943 = get_symbol_name_matcher (lang, lookup_name_without_params);
3944
3945 name_and_matcher key {
3946 name_matcher,
3947 lookup_name_without_params.language_lookup_name (lang_e)
3948 };
3949
3950 /* Don't insert the same comparison routine more than once.
3951 Note that we do this linear walk. This is not a problem in
3952 practice because the number of supported languages is
3953 low. */
3954 if (std::find (matchers.begin (), matchers.end (), key)
3955 != matchers.end ())
3956 continue;
3957 matchers.push_back (std::move (key));
3958
3959 auto bounds
3960 = index.find_name_components_bounds (lookup_name_without_params,
3961 lang_e);
3962
3963 /* Now for each symbol name in range, check to see if we have a name
3964 match, and if so, call the MATCH_CALLBACK callback. */
3965
3966 for (; bounds.first != bounds.second; ++bounds.first)
3967 {
3968 const char *qualified = index.symbol_name_at (bounds.first->idx);
3969
3970 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3971 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3972 continue;
3973
3974 matches.push_back (bounds.first->idx);
3975 }
3976 }
3977
3978 std::sort (matches.begin (), matches.end ());
3979
3980 /* Finally call the callback, once per match. */
3981 ULONGEST prev = -1;
3982 for (offset_type idx : matches)
3983 {
3984 if (prev != idx)
3985 {
3986 if (!match_callback (idx))
3987 break;
3988 prev = idx;
3989 }
3990 }
3991
3992 /* Above we use a type wider than idx's for 'prev', since 0 and
3993 (offset_type)-1 are both possible values. */
3994 static_assert (sizeof (prev) > sizeof (offset_type), "");
3995 }
3996
3997 #if GDB_SELF_TEST
3998
3999 namespace selftests { namespace dw2_expand_symtabs_matching {
4000
4001 /* A mock .gdb_index/.debug_names-like name index table, enough to
4002 exercise dw2_expand_symtabs_matching_symbol, which works with the
4003 mapped_index_base interface. Builds an index from the symbol list
4004 passed as parameter to the constructor. */
4005 class mock_mapped_index : public mapped_index_base
4006 {
4007 public:
4008 mock_mapped_index (gdb::array_view<const char *> symbols)
4009 : m_symbol_table (symbols)
4010 {}
4011
4012 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4013
4014 /* Return the number of names in the symbol table. */
4015 size_t symbol_name_count () const override
4016 {
4017 return m_symbol_table.size ();
4018 }
4019
4020 /* Get the name of the symbol at IDX in the symbol table. */
4021 const char *symbol_name_at (offset_type idx) const override
4022 {
4023 return m_symbol_table[idx];
4024 }
4025
4026 private:
4027 gdb::array_view<const char *> m_symbol_table;
4028 };
4029
4030 /* Convenience function that converts a NULL pointer to a "<null>"
4031 string, to pass to print routines. */
4032
4033 static const char *
4034 string_or_null (const char *str)
4035 {
4036 return str != NULL ? str : "<null>";
4037 }
4038
4039 /* Check if a lookup_name_info built from
4040 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4041 index. EXPECTED_LIST is the list of expected matches, in expected
4042 matching order. If no match expected, then an empty list is
4043 specified. Returns true on success. On failure prints a warning
4044 indicating the file:line that failed, and returns false. */
4045
4046 static bool
4047 check_match (const char *file, int line,
4048 mock_mapped_index &mock_index,
4049 const char *name, symbol_name_match_type match_type,
4050 bool completion_mode,
4051 std::initializer_list<const char *> expected_list)
4052 {
4053 lookup_name_info lookup_name (name, match_type, completion_mode);
4054
4055 bool matched = true;
4056
4057 auto mismatch = [&] (const char *expected_str,
4058 const char *got)
4059 {
4060 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4061 "expected=\"%s\", got=\"%s\"\n"),
4062 file, line,
4063 (match_type == symbol_name_match_type::FULL
4064 ? "FULL" : "WILD"),
4065 name, string_or_null (expected_str), string_or_null (got));
4066 matched = false;
4067 };
4068
4069 auto expected_it = expected_list.begin ();
4070 auto expected_end = expected_list.end ();
4071
4072 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4073 NULL, ALL_DOMAIN,
4074 [&] (offset_type idx)
4075 {
4076 const char *matched_name = mock_index.symbol_name_at (idx);
4077 const char *expected_str
4078 = expected_it == expected_end ? NULL : *expected_it++;
4079
4080 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4081 mismatch (expected_str, matched_name);
4082 return true;
4083 });
4084
4085 const char *expected_str
4086 = expected_it == expected_end ? NULL : *expected_it++;
4087 if (expected_str != NULL)
4088 mismatch (expected_str, NULL);
4089
4090 return matched;
4091 }
4092
4093 /* The symbols added to the mock mapped_index for testing (in
4094 canonical form). */
4095 static const char *test_symbols[] = {
4096 "function",
4097 "std::bar",
4098 "std::zfunction",
4099 "std::zfunction2",
4100 "w1::w2",
4101 "ns::foo<char*>",
4102 "ns::foo<int>",
4103 "ns::foo<long>",
4104 "ns2::tmpl<int>::foo2",
4105 "(anonymous namespace)::A::B::C",
4106
4107 /* These are used to check that the increment-last-char in the
4108 matching algorithm for completion doesn't match "t1_fund" when
4109 completing "t1_func". */
4110 "t1_func",
4111 "t1_func1",
4112 "t1_fund",
4113 "t1_fund1",
4114
4115 /* A UTF-8 name with multi-byte sequences to make sure that
4116 cp-name-parser understands this as a single identifier ("função"
4117 is "function" in PT). */
4118 u8"u8função",
4119
4120 /* \377 (0xff) is Latin1 'ÿ'. */
4121 "yfunc\377",
4122
4123 /* \377 (0xff) is Latin1 'ÿ'. */
4124 "\377",
4125 "\377\377123",
4126
4127 /* A name with all sorts of complications. Starts with "z" to make
4128 it easier for the completion tests below. */
4129 #define Z_SYM_NAME \
4130 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4131 "::tuple<(anonymous namespace)::ui*, " \
4132 "std::default_delete<(anonymous namespace)::ui>, void>"
4133
4134 Z_SYM_NAME
4135 };
4136
4137 /* Returns true if the mapped_index_base::find_name_component_bounds
4138 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4139 in completion mode. */
4140
4141 static bool
4142 check_find_bounds_finds (mapped_index_base &index,
4143 const char *search_name,
4144 gdb::array_view<const char *> expected_syms)
4145 {
4146 lookup_name_info lookup_name (search_name,
4147 symbol_name_match_type::FULL, true);
4148
4149 auto bounds = index.find_name_components_bounds (lookup_name,
4150 language_cplus);
4151
4152 size_t distance = std::distance (bounds.first, bounds.second);
4153 if (distance != expected_syms.size ())
4154 return false;
4155
4156 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4157 {
4158 auto nc_elem = bounds.first + exp_elem;
4159 const char *qualified = index.symbol_name_at (nc_elem->idx);
4160 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4161 return false;
4162 }
4163
4164 return true;
4165 }
4166
4167 /* Test the lower-level mapped_index::find_name_component_bounds
4168 method. */
4169
4170 static void
4171 test_mapped_index_find_name_component_bounds ()
4172 {
4173 mock_mapped_index mock_index (test_symbols);
4174
4175 mock_index.build_name_components ();
4176
4177 /* Test the lower-level mapped_index::find_name_component_bounds
4178 method in completion mode. */
4179 {
4180 static const char *expected_syms[] = {
4181 "t1_func",
4182 "t1_func1",
4183 };
4184
4185 SELF_CHECK (check_find_bounds_finds (mock_index,
4186 "t1_func", expected_syms));
4187 }
4188
4189 /* Check that the increment-last-char in the name matching algorithm
4190 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4191 {
4192 static const char *expected_syms1[] = {
4193 "\377",
4194 "\377\377123",
4195 };
4196 SELF_CHECK (check_find_bounds_finds (mock_index,
4197 "\377", expected_syms1));
4198
4199 static const char *expected_syms2[] = {
4200 "\377\377123",
4201 };
4202 SELF_CHECK (check_find_bounds_finds (mock_index,
4203 "\377\377", expected_syms2));
4204 }
4205 }
4206
4207 /* Test dw2_expand_symtabs_matching_symbol. */
4208
4209 static void
4210 test_dw2_expand_symtabs_matching_symbol ()
4211 {
4212 mock_mapped_index mock_index (test_symbols);
4213
4214 /* We let all tests run until the end even if some fails, for debug
4215 convenience. */
4216 bool any_mismatch = false;
4217
4218 /* Create the expected symbols list (an initializer_list). Needed
4219 because lists have commas, and we need to pass them to CHECK,
4220 which is a macro. */
4221 #define EXPECT(...) { __VA_ARGS__ }
4222
4223 /* Wrapper for check_match that passes down the current
4224 __FILE__/__LINE__. */
4225 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4226 any_mismatch |= !check_match (__FILE__, __LINE__, \
4227 mock_index, \
4228 NAME, MATCH_TYPE, COMPLETION_MODE, \
4229 EXPECTED_LIST)
4230
4231 /* Identity checks. */
4232 for (const char *sym : test_symbols)
4233 {
4234 /* Should be able to match all existing symbols. */
4235 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4236 EXPECT (sym));
4237
4238 /* Should be able to match all existing symbols with
4239 parameters. */
4240 std::string with_params = std::string (sym) + "(int)";
4241 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4242 EXPECT (sym));
4243
4244 /* Should be able to match all existing symbols with
4245 parameters and qualifiers. */
4246 with_params = std::string (sym) + " ( int ) const";
4247 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4248 EXPECT (sym));
4249
4250 /* This should really find sym, but cp-name-parser.y doesn't
4251 know about lvalue/rvalue qualifiers yet. */
4252 with_params = std::string (sym) + " ( int ) &&";
4253 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4254 {});
4255 }
4256
4257 /* Check that the name matching algorithm for completion doesn't get
4258 confused with Latin1 'ÿ' / 0xff. */
4259 {
4260 static const char str[] = "\377";
4261 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4262 EXPECT ("\377", "\377\377123"));
4263 }
4264
4265 /* Check that the increment-last-char in the matching algorithm for
4266 completion doesn't match "t1_fund" when completing "t1_func". */
4267 {
4268 static const char str[] = "t1_func";
4269 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4270 EXPECT ("t1_func", "t1_func1"));
4271 }
4272
4273 /* Check that completion mode works at each prefix of the expected
4274 symbol name. */
4275 {
4276 static const char str[] = "function(int)";
4277 size_t len = strlen (str);
4278 std::string lookup;
4279
4280 for (size_t i = 1; i < len; i++)
4281 {
4282 lookup.assign (str, i);
4283 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4284 EXPECT ("function"));
4285 }
4286 }
4287
4288 /* While "w" is a prefix of both components, the match function
4289 should still only be called once. */
4290 {
4291 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4292 EXPECT ("w1::w2"));
4293 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4294 EXPECT ("w1::w2"));
4295 }
4296
4297 /* Same, with a "complicated" symbol. */
4298 {
4299 static const char str[] = Z_SYM_NAME;
4300 size_t len = strlen (str);
4301 std::string lookup;
4302
4303 for (size_t i = 1; i < len; i++)
4304 {
4305 lookup.assign (str, i);
4306 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4307 EXPECT (Z_SYM_NAME));
4308 }
4309 }
4310
4311 /* In FULL mode, an incomplete symbol doesn't match. */
4312 {
4313 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4314 {});
4315 }
4316
4317 /* A complete symbol with parameters matches any overload, since the
4318 index has no overload info. */
4319 {
4320 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4321 EXPECT ("std::zfunction", "std::zfunction2"));
4322 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4323 EXPECT ("std::zfunction", "std::zfunction2"));
4324 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4325 EXPECT ("std::zfunction", "std::zfunction2"));
4326 }
4327
4328 /* Check that whitespace is ignored appropriately. A symbol with a
4329 template argument list. */
4330 {
4331 static const char expected[] = "ns::foo<int>";
4332 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4333 EXPECT (expected));
4334 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4335 EXPECT (expected));
4336 }
4337
4338 /* Check that whitespace is ignored appropriately. A symbol with a
4339 template argument list that includes a pointer. */
4340 {
4341 static const char expected[] = "ns::foo<char*>";
4342 /* Try both completion and non-completion modes. */
4343 static const bool completion_mode[2] = {false, true};
4344 for (size_t i = 0; i < 2; i++)
4345 {
4346 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4347 completion_mode[i], EXPECT (expected));
4348 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4349 completion_mode[i], EXPECT (expected));
4350
4351 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4352 completion_mode[i], EXPECT (expected));
4353 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4354 completion_mode[i], EXPECT (expected));
4355 }
4356 }
4357
4358 {
4359 /* Check method qualifiers are ignored. */
4360 static const char expected[] = "ns::foo<char*>";
4361 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4362 symbol_name_match_type::FULL, true, EXPECT (expected));
4363 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4364 symbol_name_match_type::FULL, true, EXPECT (expected));
4365 CHECK_MATCH ("foo < char * > ( int ) const",
4366 symbol_name_match_type::WILD, true, EXPECT (expected));
4367 CHECK_MATCH ("foo < char * > ( int ) &&",
4368 symbol_name_match_type::WILD, true, EXPECT (expected));
4369 }
4370
4371 /* Test lookup names that don't match anything. */
4372 {
4373 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4374 {});
4375
4376 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4377 {});
4378 }
4379
4380 /* Some wild matching tests, exercising "(anonymous namespace)",
4381 which should not be confused with a parameter list. */
4382 {
4383 static const char *syms[] = {
4384 "A::B::C",
4385 "B::C",
4386 "C",
4387 "A :: B :: C ( int )",
4388 "B :: C ( int )",
4389 "C ( int )",
4390 };
4391
4392 for (const char *s : syms)
4393 {
4394 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4395 EXPECT ("(anonymous namespace)::A::B::C"));
4396 }
4397 }
4398
4399 {
4400 static const char expected[] = "ns2::tmpl<int>::foo2";
4401 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4402 EXPECT (expected));
4403 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4404 EXPECT (expected));
4405 }
4406
4407 SELF_CHECK (!any_mismatch);
4408
4409 #undef EXPECT
4410 #undef CHECK_MATCH
4411 }
4412
4413 static void
4414 run_test ()
4415 {
4416 test_mapped_index_find_name_component_bounds ();
4417 test_dw2_expand_symtabs_matching_symbol ();
4418 }
4419
4420 }} // namespace selftests::dw2_expand_symtabs_matching
4421
4422 #endif /* GDB_SELF_TEST */
4423
4424 /* If FILE_MATCHER is NULL or if PER_CU has
4425 dwarf2_per_cu_quick_data::MARK set (see
4426 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4427 EXPANSION_NOTIFY on it. */
4428
4429 static void
4430 dw2_expand_symtabs_matching_one
4431 (struct dwarf2_per_cu_data *per_cu,
4432 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4433 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4434 {
4435 if (file_matcher == NULL || per_cu->v.quick->mark)
4436 {
4437 bool symtab_was_null
4438 = (per_cu->v.quick->compunit_symtab == NULL);
4439
4440 dw2_instantiate_symtab (per_cu, false);
4441
4442 if (expansion_notify != NULL
4443 && symtab_was_null
4444 && per_cu->v.quick->compunit_symtab != NULL)
4445 expansion_notify (per_cu->v.quick->compunit_symtab);
4446 }
4447 }
4448
4449 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4450 matched, to expand corresponding CUs that were marked. IDX is the
4451 index of the symbol name that matched. */
4452
4453 static void
4454 dw2_expand_marked_cus
4455 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4456 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4457 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4458 search_domain kind)
4459 {
4460 offset_type *vec, vec_len, vec_idx;
4461 bool global_seen = false;
4462 mapped_index &index = *dwarf2_per_objfile->index_table;
4463
4464 vec = (offset_type *) (index.constant_pool
4465 + MAYBE_SWAP (index.symbol_table[idx].vec));
4466 vec_len = MAYBE_SWAP (vec[0]);
4467 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4468 {
4469 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4470 /* This value is only valid for index versions >= 7. */
4471 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4472 gdb_index_symbol_kind symbol_kind =
4473 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4474 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4475 /* Only check the symbol attributes if they're present.
4476 Indices prior to version 7 don't record them,
4477 and indices >= 7 may elide them for certain symbols
4478 (gold does this). */
4479 int attrs_valid =
4480 (index.version >= 7
4481 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4482
4483 /* Work around gold/15646. */
4484 if (attrs_valid)
4485 {
4486 if (!is_static && global_seen)
4487 continue;
4488 if (!is_static)
4489 global_seen = true;
4490 }
4491
4492 /* Only check the symbol's kind if it has one. */
4493 if (attrs_valid)
4494 {
4495 switch (kind)
4496 {
4497 case VARIABLES_DOMAIN:
4498 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4499 continue;
4500 break;
4501 case FUNCTIONS_DOMAIN:
4502 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4503 continue;
4504 break;
4505 case TYPES_DOMAIN:
4506 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4507 continue;
4508 break;
4509 case MODULES_DOMAIN:
4510 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4511 continue;
4512 break;
4513 default:
4514 break;
4515 }
4516 }
4517
4518 /* Don't crash on bad data. */
4519 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4520 + dwarf2_per_objfile->all_type_units.size ()))
4521 {
4522 complaint (_(".gdb_index entry has bad CU index"
4523 " [in module %s]"),
4524 objfile_name (dwarf2_per_objfile->objfile));
4525 continue;
4526 }
4527
4528 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4529 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4530 expansion_notify);
4531 }
4532 }
4533
4534 /* If FILE_MATCHER is non-NULL, set all the
4535 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4536 that match FILE_MATCHER. */
4537
4538 static void
4539 dw_expand_symtabs_matching_file_matcher
4540 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4541 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4542 {
4543 if (file_matcher == NULL)
4544 return;
4545
4546 objfile *const objfile = dwarf2_per_objfile->objfile;
4547
4548 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4549 htab_eq_pointer,
4550 NULL, xcalloc, xfree));
4551 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4552 htab_eq_pointer,
4553 NULL, xcalloc, xfree));
4554
4555 /* The rule is CUs specify all the files, including those used by
4556 any TU, so there's no need to scan TUs here. */
4557
4558 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4559 {
4560 QUIT;
4561
4562 per_cu->v.quick->mark = 0;
4563
4564 /* We only need to look at symtabs not already expanded. */
4565 if (per_cu->v.quick->compunit_symtab)
4566 continue;
4567
4568 quick_file_names *file_data = dw2_get_file_names (per_cu);
4569 if (file_data == NULL)
4570 continue;
4571
4572 if (htab_find (visited_not_found.get (), file_data) != NULL)
4573 continue;
4574 else if (htab_find (visited_found.get (), file_data) != NULL)
4575 {
4576 per_cu->v.quick->mark = 1;
4577 continue;
4578 }
4579
4580 for (int j = 0; j < file_data->num_file_names; ++j)
4581 {
4582 const char *this_real_name;
4583
4584 if (file_matcher (file_data->file_names[j], false))
4585 {
4586 per_cu->v.quick->mark = 1;
4587 break;
4588 }
4589
4590 /* Before we invoke realpath, which can get expensive when many
4591 files are involved, do a quick comparison of the basenames. */
4592 if (!basenames_may_differ
4593 && !file_matcher (lbasename (file_data->file_names[j]),
4594 true))
4595 continue;
4596
4597 this_real_name = dw2_get_real_path (objfile, file_data, j);
4598 if (file_matcher (this_real_name, false))
4599 {
4600 per_cu->v.quick->mark = 1;
4601 break;
4602 }
4603 }
4604
4605 void **slot = htab_find_slot (per_cu->v.quick->mark
4606 ? visited_found.get ()
4607 : visited_not_found.get (),
4608 file_data, INSERT);
4609 *slot = file_data;
4610 }
4611 }
4612
4613 static void
4614 dw2_expand_symtabs_matching
4615 (struct objfile *objfile,
4616 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4617 const lookup_name_info &lookup_name,
4618 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4619 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4620 enum search_domain kind)
4621 {
4622 struct dwarf2_per_objfile *dwarf2_per_objfile
4623 = get_dwarf2_per_objfile (objfile);
4624
4625 /* index_table is NULL if OBJF_READNOW. */
4626 if (!dwarf2_per_objfile->index_table)
4627 return;
4628
4629 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4630
4631 mapped_index &index = *dwarf2_per_objfile->index_table;
4632
4633 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4634 symbol_matcher,
4635 kind, [&] (offset_type idx)
4636 {
4637 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4638 expansion_notify, kind);
4639 return true;
4640 });
4641 }
4642
4643 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4644 symtab. */
4645
4646 static struct compunit_symtab *
4647 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4648 CORE_ADDR pc)
4649 {
4650 int i;
4651
4652 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4653 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4654 return cust;
4655
4656 if (cust->includes == NULL)
4657 return NULL;
4658
4659 for (i = 0; cust->includes[i]; ++i)
4660 {
4661 struct compunit_symtab *s = cust->includes[i];
4662
4663 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4664 if (s != NULL)
4665 return s;
4666 }
4667
4668 return NULL;
4669 }
4670
4671 static struct compunit_symtab *
4672 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4673 struct bound_minimal_symbol msymbol,
4674 CORE_ADDR pc,
4675 struct obj_section *section,
4676 int warn_if_readin)
4677 {
4678 struct dwarf2_per_cu_data *data;
4679 struct compunit_symtab *result;
4680
4681 if (!objfile->partial_symtabs->psymtabs_addrmap)
4682 return NULL;
4683
4684 CORE_ADDR baseaddr = objfile->text_section_offset ();
4685 data = (struct dwarf2_per_cu_data *) addrmap_find
4686 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4687 if (!data)
4688 return NULL;
4689
4690 if (warn_if_readin && data->v.quick->compunit_symtab)
4691 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4692 paddress (get_objfile_arch (objfile), pc));
4693
4694 result
4695 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4696 false),
4697 pc);
4698 gdb_assert (result != NULL);
4699 return result;
4700 }
4701
4702 static void
4703 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4704 void *data, int need_fullname)
4705 {
4706 struct dwarf2_per_objfile *dwarf2_per_objfile
4707 = get_dwarf2_per_objfile (objfile);
4708
4709 if (!dwarf2_per_objfile->filenames_cache)
4710 {
4711 dwarf2_per_objfile->filenames_cache.emplace ();
4712
4713 htab_up visited (htab_create_alloc (10,
4714 htab_hash_pointer, htab_eq_pointer,
4715 NULL, xcalloc, xfree));
4716
4717 /* The rule is CUs specify all the files, including those used
4718 by any TU, so there's no need to scan TUs here. We can
4719 ignore file names coming from already-expanded CUs. */
4720
4721 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4722 {
4723 if (per_cu->v.quick->compunit_symtab)
4724 {
4725 void **slot = htab_find_slot (visited.get (),
4726 per_cu->v.quick->file_names,
4727 INSERT);
4728
4729 *slot = per_cu->v.quick->file_names;
4730 }
4731 }
4732
4733 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4734 {
4735 /* We only need to look at symtabs not already expanded. */
4736 if (per_cu->v.quick->compunit_symtab)
4737 continue;
4738
4739 quick_file_names *file_data = dw2_get_file_names (per_cu);
4740 if (file_data == NULL)
4741 continue;
4742
4743 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4744 if (*slot)
4745 {
4746 /* Already visited. */
4747 continue;
4748 }
4749 *slot = file_data;
4750
4751 for (int j = 0; j < file_data->num_file_names; ++j)
4752 {
4753 const char *filename = file_data->file_names[j];
4754 dwarf2_per_objfile->filenames_cache->seen (filename);
4755 }
4756 }
4757 }
4758
4759 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4760 {
4761 gdb::unique_xmalloc_ptr<char> this_real_name;
4762
4763 if (need_fullname)
4764 this_real_name = gdb_realpath (filename);
4765 (*fun) (filename, this_real_name.get (), data);
4766 });
4767 }
4768
4769 static int
4770 dw2_has_symbols (struct objfile *objfile)
4771 {
4772 return 1;
4773 }
4774
4775 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4776 {
4777 dw2_has_symbols,
4778 dw2_find_last_source_symtab,
4779 dw2_forget_cached_source_info,
4780 dw2_map_symtabs_matching_filename,
4781 dw2_lookup_symbol,
4782 dw2_print_stats,
4783 dw2_dump,
4784 dw2_expand_symtabs_for_function,
4785 dw2_expand_all_symtabs,
4786 dw2_expand_symtabs_with_fullname,
4787 dw2_map_matching_symbols,
4788 dw2_expand_symtabs_matching,
4789 dw2_find_pc_sect_compunit_symtab,
4790 NULL,
4791 dw2_map_symbol_filenames
4792 };
4793
4794 /* DWARF-5 debug_names reader. */
4795
4796 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4797 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4798
4799 /* A helper function that reads the .debug_names section in SECTION
4800 and fills in MAP. FILENAME is the name of the file containing the
4801 section; it is used for error reporting.
4802
4803 Returns true if all went well, false otherwise. */
4804
4805 static bool
4806 read_debug_names_from_section (struct objfile *objfile,
4807 const char *filename,
4808 struct dwarf2_section_info *section,
4809 mapped_debug_names &map)
4810 {
4811 if (section->empty ())
4812 return false;
4813
4814 /* Older elfutils strip versions could keep the section in the main
4815 executable while splitting it for the separate debug info file. */
4816 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4817 return false;
4818
4819 section->read (objfile);
4820
4821 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4822
4823 const gdb_byte *addr = section->buffer;
4824
4825 bfd *const abfd = section->get_bfd_owner ();
4826
4827 unsigned int bytes_read;
4828 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4829 addr += bytes_read;
4830
4831 map.dwarf5_is_dwarf64 = bytes_read != 4;
4832 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4833 if (bytes_read + length != section->size)
4834 {
4835 /* There may be multiple per-CU indices. */
4836 warning (_("Section .debug_names in %s length %s does not match "
4837 "section length %s, ignoring .debug_names."),
4838 filename, plongest (bytes_read + length),
4839 pulongest (section->size));
4840 return false;
4841 }
4842
4843 /* The version number. */
4844 uint16_t version = read_2_bytes (abfd, addr);
4845 addr += 2;
4846 if (version != 5)
4847 {
4848 warning (_("Section .debug_names in %s has unsupported version %d, "
4849 "ignoring .debug_names."),
4850 filename, version);
4851 return false;
4852 }
4853
4854 /* Padding. */
4855 uint16_t padding = read_2_bytes (abfd, addr);
4856 addr += 2;
4857 if (padding != 0)
4858 {
4859 warning (_("Section .debug_names in %s has unsupported padding %d, "
4860 "ignoring .debug_names."),
4861 filename, padding);
4862 return false;
4863 }
4864
4865 /* comp_unit_count - The number of CUs in the CU list. */
4866 map.cu_count = read_4_bytes (abfd, addr);
4867 addr += 4;
4868
4869 /* local_type_unit_count - The number of TUs in the local TU
4870 list. */
4871 map.tu_count = read_4_bytes (abfd, addr);
4872 addr += 4;
4873
4874 /* foreign_type_unit_count - The number of TUs in the foreign TU
4875 list. */
4876 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4877 addr += 4;
4878 if (foreign_tu_count != 0)
4879 {
4880 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4881 "ignoring .debug_names."),
4882 filename, static_cast<unsigned long> (foreign_tu_count));
4883 return false;
4884 }
4885
4886 /* bucket_count - The number of hash buckets in the hash lookup
4887 table. */
4888 map.bucket_count = read_4_bytes (abfd, addr);
4889 addr += 4;
4890
4891 /* name_count - The number of unique names in the index. */
4892 map.name_count = read_4_bytes (abfd, addr);
4893 addr += 4;
4894
4895 /* abbrev_table_size - The size in bytes of the abbreviations
4896 table. */
4897 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4898 addr += 4;
4899
4900 /* augmentation_string_size - The size in bytes of the augmentation
4901 string. This value is rounded up to a multiple of 4. */
4902 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4903 addr += 4;
4904 map.augmentation_is_gdb = ((augmentation_string_size
4905 == sizeof (dwarf5_augmentation))
4906 && memcmp (addr, dwarf5_augmentation,
4907 sizeof (dwarf5_augmentation)) == 0);
4908 augmentation_string_size += (-augmentation_string_size) & 3;
4909 addr += augmentation_string_size;
4910
4911 /* List of CUs */
4912 map.cu_table_reordered = addr;
4913 addr += map.cu_count * map.offset_size;
4914
4915 /* List of Local TUs */
4916 map.tu_table_reordered = addr;
4917 addr += map.tu_count * map.offset_size;
4918
4919 /* Hash Lookup Table */
4920 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4921 addr += map.bucket_count * 4;
4922 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4923 addr += map.name_count * 4;
4924
4925 /* Name Table */
4926 map.name_table_string_offs_reordered = addr;
4927 addr += map.name_count * map.offset_size;
4928 map.name_table_entry_offs_reordered = addr;
4929 addr += map.name_count * map.offset_size;
4930
4931 const gdb_byte *abbrev_table_start = addr;
4932 for (;;)
4933 {
4934 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4935 addr += bytes_read;
4936 if (index_num == 0)
4937 break;
4938
4939 const auto insertpair
4940 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4941 if (!insertpair.second)
4942 {
4943 warning (_("Section .debug_names in %s has duplicate index %s, "
4944 "ignoring .debug_names."),
4945 filename, pulongest (index_num));
4946 return false;
4947 }
4948 mapped_debug_names::index_val &indexval = insertpair.first->second;
4949 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4950 addr += bytes_read;
4951
4952 for (;;)
4953 {
4954 mapped_debug_names::index_val::attr attr;
4955 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4956 addr += bytes_read;
4957 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4958 addr += bytes_read;
4959 if (attr.form == DW_FORM_implicit_const)
4960 {
4961 attr.implicit_const = read_signed_leb128 (abfd, addr,
4962 &bytes_read);
4963 addr += bytes_read;
4964 }
4965 if (attr.dw_idx == 0 && attr.form == 0)
4966 break;
4967 indexval.attr_vec.push_back (std::move (attr));
4968 }
4969 }
4970 if (addr != abbrev_table_start + abbrev_table_size)
4971 {
4972 warning (_("Section .debug_names in %s has abbreviation_table "
4973 "of size %s vs. written as %u, ignoring .debug_names."),
4974 filename, plongest (addr - abbrev_table_start),
4975 abbrev_table_size);
4976 return false;
4977 }
4978 map.entry_pool = addr;
4979
4980 return true;
4981 }
4982
4983 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4984 list. */
4985
4986 static void
4987 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4988 const mapped_debug_names &map,
4989 dwarf2_section_info &section,
4990 bool is_dwz)
4991 {
4992 sect_offset sect_off_prev;
4993 for (uint32_t i = 0; i <= map.cu_count; ++i)
4994 {
4995 sect_offset sect_off_next;
4996 if (i < map.cu_count)
4997 {
4998 sect_off_next
4999 = (sect_offset) (extract_unsigned_integer
5000 (map.cu_table_reordered + i * map.offset_size,
5001 map.offset_size,
5002 map.dwarf5_byte_order));
5003 }
5004 else
5005 sect_off_next = (sect_offset) section.size;
5006 if (i >= 1)
5007 {
5008 const ULONGEST length = sect_off_next - sect_off_prev;
5009 dwarf2_per_cu_data *per_cu
5010 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5011 sect_off_prev, length);
5012 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5013 }
5014 sect_off_prev = sect_off_next;
5015 }
5016 }
5017
5018 /* Read the CU list from the mapped index, and use it to create all
5019 the CU objects for this dwarf2_per_objfile. */
5020
5021 static void
5022 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5023 const mapped_debug_names &map,
5024 const mapped_debug_names &dwz_map)
5025 {
5026 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5027 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5028
5029 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5030 dwarf2_per_objfile->info,
5031 false /* is_dwz */);
5032
5033 if (dwz_map.cu_count == 0)
5034 return;
5035
5036 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5037 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5038 true /* is_dwz */);
5039 }
5040
5041 /* Read .debug_names. If everything went ok, initialize the "quick"
5042 elements of all the CUs and return true. Otherwise, return false. */
5043
5044 static bool
5045 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5046 {
5047 std::unique_ptr<mapped_debug_names> map
5048 (new mapped_debug_names (dwarf2_per_objfile));
5049 mapped_debug_names dwz_map (dwarf2_per_objfile);
5050 struct objfile *objfile = dwarf2_per_objfile->objfile;
5051
5052 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5053 &dwarf2_per_objfile->debug_names,
5054 *map))
5055 return false;
5056
5057 /* Don't use the index if it's empty. */
5058 if (map->name_count == 0)
5059 return false;
5060
5061 /* If there is a .dwz file, read it so we can get its CU list as
5062 well. */
5063 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5064 if (dwz != NULL)
5065 {
5066 if (!read_debug_names_from_section (objfile,
5067 bfd_get_filename (dwz->dwz_bfd.get ()),
5068 &dwz->debug_names, dwz_map))
5069 {
5070 warning (_("could not read '.debug_names' section from %s; skipping"),
5071 bfd_get_filename (dwz->dwz_bfd.get ()));
5072 return false;
5073 }
5074 }
5075
5076 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5077
5078 if (map->tu_count != 0)
5079 {
5080 /* We can only handle a single .debug_types when we have an
5081 index. */
5082 if (dwarf2_per_objfile->types.size () != 1)
5083 return false;
5084
5085 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5086
5087 create_signatured_type_table_from_debug_names
5088 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5089 }
5090
5091 create_addrmap_from_aranges (dwarf2_per_objfile,
5092 &dwarf2_per_objfile->debug_aranges);
5093
5094 dwarf2_per_objfile->debug_names_table = std::move (map);
5095 dwarf2_per_objfile->using_index = 1;
5096 dwarf2_per_objfile->quick_file_names_table =
5097 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5098
5099 return true;
5100 }
5101
5102 /* Type used to manage iterating over all CUs looking for a symbol for
5103 .debug_names. */
5104
5105 class dw2_debug_names_iterator
5106 {
5107 public:
5108 dw2_debug_names_iterator (const mapped_debug_names &map,
5109 gdb::optional<block_enum> block_index,
5110 domain_enum domain,
5111 const char *name)
5112 : m_map (map), m_block_index (block_index), m_domain (domain),
5113 m_addr (find_vec_in_debug_names (map, name))
5114 {}
5115
5116 dw2_debug_names_iterator (const mapped_debug_names &map,
5117 search_domain search, uint32_t namei)
5118 : m_map (map),
5119 m_search (search),
5120 m_addr (find_vec_in_debug_names (map, namei))
5121 {}
5122
5123 dw2_debug_names_iterator (const mapped_debug_names &map,
5124 block_enum block_index, domain_enum domain,
5125 uint32_t namei)
5126 : m_map (map), m_block_index (block_index), m_domain (domain),
5127 m_addr (find_vec_in_debug_names (map, namei))
5128 {}
5129
5130 /* Return the next matching CU or NULL if there are no more. */
5131 dwarf2_per_cu_data *next ();
5132
5133 private:
5134 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5135 const char *name);
5136 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5137 uint32_t namei);
5138
5139 /* The internalized form of .debug_names. */
5140 const mapped_debug_names &m_map;
5141
5142 /* If set, only look for symbols that match that block. Valid values are
5143 GLOBAL_BLOCK and STATIC_BLOCK. */
5144 const gdb::optional<block_enum> m_block_index;
5145
5146 /* The kind of symbol we're looking for. */
5147 const domain_enum m_domain = UNDEF_DOMAIN;
5148 const search_domain m_search = ALL_DOMAIN;
5149
5150 /* The list of CUs from the index entry of the symbol, or NULL if
5151 not found. */
5152 const gdb_byte *m_addr;
5153 };
5154
5155 const char *
5156 mapped_debug_names::namei_to_name (uint32_t namei) const
5157 {
5158 const ULONGEST namei_string_offs
5159 = extract_unsigned_integer ((name_table_string_offs_reordered
5160 + namei * offset_size),
5161 offset_size,
5162 dwarf5_byte_order);
5163 return read_indirect_string_at_offset
5164 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5165 }
5166
5167 /* Find a slot in .debug_names for the object named NAME. If NAME is
5168 found, return pointer to its pool data. If NAME cannot be found,
5169 return NULL. */
5170
5171 const gdb_byte *
5172 dw2_debug_names_iterator::find_vec_in_debug_names
5173 (const mapped_debug_names &map, const char *name)
5174 {
5175 int (*cmp) (const char *, const char *);
5176
5177 gdb::unique_xmalloc_ptr<char> without_params;
5178 if (current_language->la_language == language_cplus
5179 || current_language->la_language == language_fortran
5180 || current_language->la_language == language_d)
5181 {
5182 /* NAME is already canonical. Drop any qualifiers as
5183 .debug_names does not contain any. */
5184
5185 if (strchr (name, '(') != NULL)
5186 {
5187 without_params = cp_remove_params (name);
5188 if (without_params != NULL)
5189 name = without_params.get ();
5190 }
5191 }
5192
5193 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5194
5195 const uint32_t full_hash = dwarf5_djb_hash (name);
5196 uint32_t namei
5197 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5198 (map.bucket_table_reordered
5199 + (full_hash % map.bucket_count)), 4,
5200 map.dwarf5_byte_order);
5201 if (namei == 0)
5202 return NULL;
5203 --namei;
5204 if (namei >= map.name_count)
5205 {
5206 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5207 "[in module %s]"),
5208 namei, map.name_count,
5209 objfile_name (map.dwarf2_per_objfile->objfile));
5210 return NULL;
5211 }
5212
5213 for (;;)
5214 {
5215 const uint32_t namei_full_hash
5216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5217 (map.hash_table_reordered + namei), 4,
5218 map.dwarf5_byte_order);
5219 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5220 return NULL;
5221
5222 if (full_hash == namei_full_hash)
5223 {
5224 const char *const namei_string = map.namei_to_name (namei);
5225
5226 #if 0 /* An expensive sanity check. */
5227 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5228 {
5229 complaint (_("Wrong .debug_names hash for string at index %u "
5230 "[in module %s]"),
5231 namei, objfile_name (dwarf2_per_objfile->objfile));
5232 return NULL;
5233 }
5234 #endif
5235
5236 if (cmp (namei_string, name) == 0)
5237 {
5238 const ULONGEST namei_entry_offs
5239 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5240 + namei * map.offset_size),
5241 map.offset_size, map.dwarf5_byte_order);
5242 return map.entry_pool + namei_entry_offs;
5243 }
5244 }
5245
5246 ++namei;
5247 if (namei >= map.name_count)
5248 return NULL;
5249 }
5250 }
5251
5252 const gdb_byte *
5253 dw2_debug_names_iterator::find_vec_in_debug_names
5254 (const mapped_debug_names &map, uint32_t namei)
5255 {
5256 if (namei >= map.name_count)
5257 {
5258 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5259 "[in module %s]"),
5260 namei, map.name_count,
5261 objfile_name (map.dwarf2_per_objfile->objfile));
5262 return NULL;
5263 }
5264
5265 const ULONGEST namei_entry_offs
5266 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5267 + namei * map.offset_size),
5268 map.offset_size, map.dwarf5_byte_order);
5269 return map.entry_pool + namei_entry_offs;
5270 }
5271
5272 /* See dw2_debug_names_iterator. */
5273
5274 dwarf2_per_cu_data *
5275 dw2_debug_names_iterator::next ()
5276 {
5277 if (m_addr == NULL)
5278 return NULL;
5279
5280 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5281 struct objfile *objfile = dwarf2_per_objfile->objfile;
5282 bfd *const abfd = objfile->obfd;
5283
5284 again:
5285
5286 unsigned int bytes_read;
5287 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5288 m_addr += bytes_read;
5289 if (abbrev == 0)
5290 return NULL;
5291
5292 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5293 if (indexval_it == m_map.abbrev_map.cend ())
5294 {
5295 complaint (_("Wrong .debug_names undefined abbrev code %s "
5296 "[in module %s]"),
5297 pulongest (abbrev), objfile_name (objfile));
5298 return NULL;
5299 }
5300 const mapped_debug_names::index_val &indexval = indexval_it->second;
5301 enum class symbol_linkage {
5302 unknown,
5303 static_,
5304 extern_,
5305 } symbol_linkage_ = symbol_linkage::unknown;
5306 dwarf2_per_cu_data *per_cu = NULL;
5307 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5308 {
5309 ULONGEST ull;
5310 switch (attr.form)
5311 {
5312 case DW_FORM_implicit_const:
5313 ull = attr.implicit_const;
5314 break;
5315 case DW_FORM_flag_present:
5316 ull = 1;
5317 break;
5318 case DW_FORM_udata:
5319 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5320 m_addr += bytes_read;
5321 break;
5322 default:
5323 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5324 dwarf_form_name (attr.form),
5325 objfile_name (objfile));
5326 return NULL;
5327 }
5328 switch (attr.dw_idx)
5329 {
5330 case DW_IDX_compile_unit:
5331 /* Don't crash on bad data. */
5332 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5333 {
5334 complaint (_(".debug_names entry has bad CU index %s"
5335 " [in module %s]"),
5336 pulongest (ull),
5337 objfile_name (dwarf2_per_objfile->objfile));
5338 continue;
5339 }
5340 per_cu = dwarf2_per_objfile->get_cutu (ull);
5341 break;
5342 case DW_IDX_type_unit:
5343 /* Don't crash on bad data. */
5344 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5345 {
5346 complaint (_(".debug_names entry has bad TU index %s"
5347 " [in module %s]"),
5348 pulongest (ull),
5349 objfile_name (dwarf2_per_objfile->objfile));
5350 continue;
5351 }
5352 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5353 break;
5354 case DW_IDX_GNU_internal:
5355 if (!m_map.augmentation_is_gdb)
5356 break;
5357 symbol_linkage_ = symbol_linkage::static_;
5358 break;
5359 case DW_IDX_GNU_external:
5360 if (!m_map.augmentation_is_gdb)
5361 break;
5362 symbol_linkage_ = symbol_linkage::extern_;
5363 break;
5364 }
5365 }
5366
5367 /* Skip if already read in. */
5368 if (per_cu->v.quick->compunit_symtab)
5369 goto again;
5370
5371 /* Check static vs global. */
5372 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5373 {
5374 const bool want_static = *m_block_index == STATIC_BLOCK;
5375 const bool symbol_is_static =
5376 symbol_linkage_ == symbol_linkage::static_;
5377 if (want_static != symbol_is_static)
5378 goto again;
5379 }
5380
5381 /* Match dw2_symtab_iter_next, symbol_kind
5382 and debug_names::psymbol_tag. */
5383 switch (m_domain)
5384 {
5385 case VAR_DOMAIN:
5386 switch (indexval.dwarf_tag)
5387 {
5388 case DW_TAG_variable:
5389 case DW_TAG_subprogram:
5390 /* Some types are also in VAR_DOMAIN. */
5391 case DW_TAG_typedef:
5392 case DW_TAG_structure_type:
5393 break;
5394 default:
5395 goto again;
5396 }
5397 break;
5398 case STRUCT_DOMAIN:
5399 switch (indexval.dwarf_tag)
5400 {
5401 case DW_TAG_typedef:
5402 case DW_TAG_structure_type:
5403 break;
5404 default:
5405 goto again;
5406 }
5407 break;
5408 case LABEL_DOMAIN:
5409 switch (indexval.dwarf_tag)
5410 {
5411 case 0:
5412 case DW_TAG_variable:
5413 break;
5414 default:
5415 goto again;
5416 }
5417 break;
5418 case MODULE_DOMAIN:
5419 switch (indexval.dwarf_tag)
5420 {
5421 case DW_TAG_module:
5422 break;
5423 default:
5424 goto again;
5425 }
5426 break;
5427 default:
5428 break;
5429 }
5430
5431 /* Match dw2_expand_symtabs_matching, symbol_kind and
5432 debug_names::psymbol_tag. */
5433 switch (m_search)
5434 {
5435 case VARIABLES_DOMAIN:
5436 switch (indexval.dwarf_tag)
5437 {
5438 case DW_TAG_variable:
5439 break;
5440 default:
5441 goto again;
5442 }
5443 break;
5444 case FUNCTIONS_DOMAIN:
5445 switch (indexval.dwarf_tag)
5446 {
5447 case DW_TAG_subprogram:
5448 break;
5449 default:
5450 goto again;
5451 }
5452 break;
5453 case TYPES_DOMAIN:
5454 switch (indexval.dwarf_tag)
5455 {
5456 case DW_TAG_typedef:
5457 case DW_TAG_structure_type:
5458 break;
5459 default:
5460 goto again;
5461 }
5462 break;
5463 case MODULES_DOMAIN:
5464 switch (indexval.dwarf_tag)
5465 {
5466 case DW_TAG_module:
5467 break;
5468 default:
5469 goto again;
5470 }
5471 default:
5472 break;
5473 }
5474
5475 return per_cu;
5476 }
5477
5478 static struct compunit_symtab *
5479 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5480 const char *name, domain_enum domain)
5481 {
5482 struct dwarf2_per_objfile *dwarf2_per_objfile
5483 = get_dwarf2_per_objfile (objfile);
5484
5485 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5486 if (!mapp)
5487 {
5488 /* index is NULL if OBJF_READNOW. */
5489 return NULL;
5490 }
5491 const auto &map = *mapp;
5492
5493 dw2_debug_names_iterator iter (map, block_index, domain, name);
5494
5495 struct compunit_symtab *stab_best = NULL;
5496 struct dwarf2_per_cu_data *per_cu;
5497 while ((per_cu = iter.next ()) != NULL)
5498 {
5499 struct symbol *sym, *with_opaque = NULL;
5500 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5501 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5502 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5503
5504 sym = block_find_symbol (block, name, domain,
5505 block_find_non_opaque_type_preferred,
5506 &with_opaque);
5507
5508 /* Some caution must be observed with overloaded functions and
5509 methods, since the index will not contain any overload
5510 information (but NAME might contain it). */
5511
5512 if (sym != NULL
5513 && strcmp_iw (sym->search_name (), name) == 0)
5514 return stab;
5515 if (with_opaque != NULL
5516 && strcmp_iw (with_opaque->search_name (), name) == 0)
5517 stab_best = stab;
5518
5519 /* Keep looking through other CUs. */
5520 }
5521
5522 return stab_best;
5523 }
5524
5525 /* This dumps minimal information about .debug_names. It is called
5526 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5527 uses this to verify that .debug_names has been loaded. */
5528
5529 static void
5530 dw2_debug_names_dump (struct objfile *objfile)
5531 {
5532 struct dwarf2_per_objfile *dwarf2_per_objfile
5533 = get_dwarf2_per_objfile (objfile);
5534
5535 gdb_assert (dwarf2_per_objfile->using_index);
5536 printf_filtered (".debug_names:");
5537 if (dwarf2_per_objfile->debug_names_table)
5538 printf_filtered (" exists\n");
5539 else
5540 printf_filtered (" faked for \"readnow\"\n");
5541 printf_filtered ("\n");
5542 }
5543
5544 static void
5545 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5546 const char *func_name)
5547 {
5548 struct dwarf2_per_objfile *dwarf2_per_objfile
5549 = get_dwarf2_per_objfile (objfile);
5550
5551 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5552 if (dwarf2_per_objfile->debug_names_table)
5553 {
5554 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5555
5556 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5557
5558 struct dwarf2_per_cu_data *per_cu;
5559 while ((per_cu = iter.next ()) != NULL)
5560 dw2_instantiate_symtab (per_cu, false);
5561 }
5562 }
5563
5564 static void
5565 dw2_debug_names_map_matching_symbols
5566 (struct objfile *objfile,
5567 const lookup_name_info &name, domain_enum domain,
5568 int global,
5569 gdb::function_view<symbol_found_callback_ftype> callback,
5570 symbol_compare_ftype *ordered_compare)
5571 {
5572 struct dwarf2_per_objfile *dwarf2_per_objfile
5573 = get_dwarf2_per_objfile (objfile);
5574
5575 /* debug_names_table is NULL if OBJF_READNOW. */
5576 if (!dwarf2_per_objfile->debug_names_table)
5577 return;
5578
5579 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5580 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5581
5582 const char *match_name = name.ada ().lookup_name ().c_str ();
5583 auto matcher = [&] (const char *symname)
5584 {
5585 if (ordered_compare == nullptr)
5586 return true;
5587 return ordered_compare (symname, match_name) == 0;
5588 };
5589
5590 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5591 [&] (offset_type namei)
5592 {
5593 /* The name was matched, now expand corresponding CUs that were
5594 marked. */
5595 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5596
5597 struct dwarf2_per_cu_data *per_cu;
5598 while ((per_cu = iter.next ()) != NULL)
5599 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5600 return true;
5601 });
5602
5603 /* It's a shame we couldn't do this inside the
5604 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5605 that have already been expanded. Instead, this loop matches what
5606 the psymtab code does. */
5607 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5608 {
5609 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5610 if (cust != nullptr)
5611 {
5612 const struct block *block
5613 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5614 if (!iterate_over_symbols_terminated (block, name,
5615 domain, callback))
5616 break;
5617 }
5618 }
5619 }
5620
5621 static void
5622 dw2_debug_names_expand_symtabs_matching
5623 (struct objfile *objfile,
5624 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5625 const lookup_name_info &lookup_name,
5626 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5627 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5628 enum search_domain kind)
5629 {
5630 struct dwarf2_per_objfile *dwarf2_per_objfile
5631 = get_dwarf2_per_objfile (objfile);
5632
5633 /* debug_names_table is NULL if OBJF_READNOW. */
5634 if (!dwarf2_per_objfile->debug_names_table)
5635 return;
5636
5637 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5638
5639 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5640
5641 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5642 symbol_matcher,
5643 kind, [&] (offset_type namei)
5644 {
5645 /* The name was matched, now expand corresponding CUs that were
5646 marked. */
5647 dw2_debug_names_iterator iter (map, kind, namei);
5648
5649 struct dwarf2_per_cu_data *per_cu;
5650 while ((per_cu = iter.next ()) != NULL)
5651 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5652 expansion_notify);
5653 return true;
5654 });
5655 }
5656
5657 const struct quick_symbol_functions dwarf2_debug_names_functions =
5658 {
5659 dw2_has_symbols,
5660 dw2_find_last_source_symtab,
5661 dw2_forget_cached_source_info,
5662 dw2_map_symtabs_matching_filename,
5663 dw2_debug_names_lookup_symbol,
5664 dw2_print_stats,
5665 dw2_debug_names_dump,
5666 dw2_debug_names_expand_symtabs_for_function,
5667 dw2_expand_all_symtabs,
5668 dw2_expand_symtabs_with_fullname,
5669 dw2_debug_names_map_matching_symbols,
5670 dw2_debug_names_expand_symtabs_matching,
5671 dw2_find_pc_sect_compunit_symtab,
5672 NULL,
5673 dw2_map_symbol_filenames
5674 };
5675
5676 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5677 to either a dwarf2_per_objfile or dwz_file object. */
5678
5679 template <typename T>
5680 static gdb::array_view<const gdb_byte>
5681 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5682 {
5683 dwarf2_section_info *section = &section_owner->gdb_index;
5684
5685 if (section->empty ())
5686 return {};
5687
5688 /* Older elfutils strip versions could keep the section in the main
5689 executable while splitting it for the separate debug info file. */
5690 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5691 return {};
5692
5693 section->read (obj);
5694
5695 /* dwarf2_section_info::size is a bfd_size_type, while
5696 gdb::array_view works with size_t. On 32-bit hosts, with
5697 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5698 is 32-bit. So we need an explicit narrowing conversion here.
5699 This is fine, because it's impossible to allocate or mmap an
5700 array/buffer larger than what size_t can represent. */
5701 return gdb::make_array_view (section->buffer, section->size);
5702 }
5703
5704 /* Lookup the index cache for the contents of the index associated to
5705 DWARF2_OBJ. */
5706
5707 static gdb::array_view<const gdb_byte>
5708 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5709 {
5710 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5711 if (build_id == nullptr)
5712 return {};
5713
5714 return global_index_cache.lookup_gdb_index (build_id,
5715 &dwarf2_obj->index_cache_res);
5716 }
5717
5718 /* Same as the above, but for DWZ. */
5719
5720 static gdb::array_view<const gdb_byte>
5721 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5722 {
5723 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5724 if (build_id == nullptr)
5725 return {};
5726
5727 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5728 }
5729
5730 /* See symfile.h. */
5731
5732 bool
5733 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5734 {
5735 struct dwarf2_per_objfile *dwarf2_per_objfile
5736 = get_dwarf2_per_objfile (objfile);
5737
5738 /* If we're about to read full symbols, don't bother with the
5739 indices. In this case we also don't care if some other debug
5740 format is making psymtabs, because they are all about to be
5741 expanded anyway. */
5742 if ((objfile->flags & OBJF_READNOW))
5743 {
5744 dwarf2_per_objfile->using_index = 1;
5745 create_all_comp_units (dwarf2_per_objfile);
5746 create_all_type_units (dwarf2_per_objfile);
5747 dwarf2_per_objfile->quick_file_names_table
5748 = create_quick_file_names_table
5749 (dwarf2_per_objfile->all_comp_units.size ());
5750
5751 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5752 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5753 {
5754 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5755
5756 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5757 struct dwarf2_per_cu_quick_data);
5758 }
5759
5760 /* Return 1 so that gdb sees the "quick" functions. However,
5761 these functions will be no-ops because we will have expanded
5762 all symtabs. */
5763 *index_kind = dw_index_kind::GDB_INDEX;
5764 return true;
5765 }
5766
5767 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5768 {
5769 *index_kind = dw_index_kind::DEBUG_NAMES;
5770 return true;
5771 }
5772
5773 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5774 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5775 get_gdb_index_contents_from_section<dwz_file>))
5776 {
5777 *index_kind = dw_index_kind::GDB_INDEX;
5778 return true;
5779 }
5780
5781 /* ... otherwise, try to find the index in the index cache. */
5782 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5783 get_gdb_index_contents_from_cache,
5784 get_gdb_index_contents_from_cache_dwz))
5785 {
5786 global_index_cache.hit ();
5787 *index_kind = dw_index_kind::GDB_INDEX;
5788 return true;
5789 }
5790
5791 global_index_cache.miss ();
5792 return false;
5793 }
5794
5795 \f
5796
5797 /* Build a partial symbol table. */
5798
5799 void
5800 dwarf2_build_psymtabs (struct objfile *objfile)
5801 {
5802 struct dwarf2_per_objfile *dwarf2_per_objfile
5803 = get_dwarf2_per_objfile (objfile);
5804
5805 init_psymbol_list (objfile, 1024);
5806
5807 try
5808 {
5809 /* This isn't really ideal: all the data we allocate on the
5810 objfile's obstack is still uselessly kept around. However,
5811 freeing it seems unsafe. */
5812 psymtab_discarder psymtabs (objfile);
5813 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5814 psymtabs.keep ();
5815
5816 /* (maybe) store an index in the cache. */
5817 global_index_cache.store (dwarf2_per_objfile);
5818 }
5819 catch (const gdb_exception_error &except)
5820 {
5821 exception_print (gdb_stderr, except);
5822 }
5823 }
5824
5825 /* Find the base address of the compilation unit for range lists and
5826 location lists. It will normally be specified by DW_AT_low_pc.
5827 In DWARF-3 draft 4, the base address could be overridden by
5828 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5829 compilation units with discontinuous ranges. */
5830
5831 static void
5832 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5833 {
5834 struct attribute *attr;
5835
5836 cu->base_known = 0;
5837 cu->base_address = 0;
5838
5839 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5840 if (attr != nullptr)
5841 {
5842 cu->base_address = attr->value_as_address ();
5843 cu->base_known = 1;
5844 }
5845 else
5846 {
5847 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5848 if (attr != nullptr)
5849 {
5850 cu->base_address = attr->value_as_address ();
5851 cu->base_known = 1;
5852 }
5853 }
5854 }
5855
5856 /* Helper function that returns the proper abbrev section for
5857 THIS_CU. */
5858
5859 static struct dwarf2_section_info *
5860 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5861 {
5862 struct dwarf2_section_info *abbrev;
5863 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5864
5865 if (this_cu->is_dwz)
5866 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5867 else
5868 abbrev = &dwarf2_per_objfile->abbrev;
5869
5870 return abbrev;
5871 }
5872
5873 /* Fetch the abbreviation table offset from a comp or type unit header. */
5874
5875 static sect_offset
5876 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5877 struct dwarf2_section_info *section,
5878 sect_offset sect_off)
5879 {
5880 bfd *abfd = section->get_bfd_owner ();
5881 const gdb_byte *info_ptr;
5882 unsigned int initial_length_size, offset_size;
5883 uint16_t version;
5884
5885 section->read (dwarf2_per_objfile->objfile);
5886 info_ptr = section->buffer + to_underlying (sect_off);
5887 read_initial_length (abfd, info_ptr, &initial_length_size);
5888 offset_size = initial_length_size == 4 ? 4 : 8;
5889 info_ptr += initial_length_size;
5890
5891 version = read_2_bytes (abfd, info_ptr);
5892 info_ptr += 2;
5893 if (version >= 5)
5894 {
5895 /* Skip unit type and address size. */
5896 info_ptr += 2;
5897 }
5898
5899 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5900 }
5901
5902 /* Allocate a new partial symtab for file named NAME and mark this new
5903 partial symtab as being an include of PST. */
5904
5905 static void
5906 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5907 struct objfile *objfile)
5908 {
5909 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
5910
5911 if (!IS_ABSOLUTE_PATH (subpst->filename))
5912 {
5913 /* It shares objfile->objfile_obstack. */
5914 subpst->dirname = pst->dirname;
5915 }
5916
5917 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5918 subpst->dependencies[0] = pst;
5919 subpst->number_of_dependencies = 1;
5920
5921 /* No private part is necessary for include psymtabs. This property
5922 can be used to differentiate between such include psymtabs and
5923 the regular ones. */
5924 subpst->per_cu_data = nullptr;
5925 }
5926
5927 /* Read the Line Number Program data and extract the list of files
5928 included by the source file represented by PST. Build an include
5929 partial symtab for each of these included files. */
5930
5931 static void
5932 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5933 struct die_info *die,
5934 dwarf2_psymtab *pst)
5935 {
5936 line_header_up lh;
5937 struct attribute *attr;
5938
5939 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5940 if (attr != nullptr)
5941 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5942 if (lh == NULL)
5943 return; /* No linetable, so no includes. */
5944
5945 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5946 that we pass in the raw text_low here; that is ok because we're
5947 only decoding the line table to make include partial symtabs, and
5948 so the addresses aren't really used. */
5949 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5950 pst->raw_text_low (), 1);
5951 }
5952
5953 static hashval_t
5954 hash_signatured_type (const void *item)
5955 {
5956 const struct signatured_type *sig_type
5957 = (const struct signatured_type *) item;
5958
5959 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5960 return sig_type->signature;
5961 }
5962
5963 static int
5964 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5965 {
5966 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5967 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5968
5969 return lhs->signature == rhs->signature;
5970 }
5971
5972 /* Allocate a hash table for signatured types. */
5973
5974 static htab_up
5975 allocate_signatured_type_table (struct objfile *objfile)
5976 {
5977 return htab_up (htab_create_alloc (41,
5978 hash_signatured_type,
5979 eq_signatured_type,
5980 NULL, xcalloc, xfree));
5981 }
5982
5983 /* A helper function to add a signatured type CU to a table. */
5984
5985 static int
5986 add_signatured_type_cu_to_table (void **slot, void *datum)
5987 {
5988 struct signatured_type *sigt = (struct signatured_type *) *slot;
5989 std::vector<signatured_type *> *all_type_units
5990 = (std::vector<signatured_type *> *) datum;
5991
5992 all_type_units->push_back (sigt);
5993
5994 return 1;
5995 }
5996
5997 /* A helper for create_debug_types_hash_table. Read types from SECTION
5998 and fill them into TYPES_HTAB. It will process only type units,
5999 therefore DW_UT_type. */
6000
6001 static void
6002 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6003 struct dwo_file *dwo_file,
6004 dwarf2_section_info *section, htab_up &types_htab,
6005 rcuh_kind section_kind)
6006 {
6007 struct objfile *objfile = dwarf2_per_objfile->objfile;
6008 struct dwarf2_section_info *abbrev_section;
6009 bfd *abfd;
6010 const gdb_byte *info_ptr, *end_ptr;
6011
6012 abbrev_section = (dwo_file != NULL
6013 ? &dwo_file->sections.abbrev
6014 : &dwarf2_per_objfile->abbrev);
6015
6016 if (dwarf_read_debug)
6017 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6018 section->get_name (),
6019 abbrev_section->get_file_name ());
6020
6021 section->read (objfile);
6022 info_ptr = section->buffer;
6023
6024 if (info_ptr == NULL)
6025 return;
6026
6027 /* We can't set abfd until now because the section may be empty or
6028 not present, in which case the bfd is unknown. */
6029 abfd = section->get_bfd_owner ();
6030
6031 /* We don't use cutu_reader here because we don't need to read
6032 any dies: the signature is in the header. */
6033
6034 end_ptr = info_ptr + section->size;
6035 while (info_ptr < end_ptr)
6036 {
6037 struct signatured_type *sig_type;
6038 struct dwo_unit *dwo_tu;
6039 void **slot;
6040 const gdb_byte *ptr = info_ptr;
6041 struct comp_unit_head header;
6042 unsigned int length;
6043
6044 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6045
6046 /* Initialize it due to a false compiler warning. */
6047 header.signature = -1;
6048 header.type_cu_offset_in_tu = (cu_offset) -1;
6049
6050 /* We need to read the type's signature in order to build the hash
6051 table, but we don't need anything else just yet. */
6052
6053 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6054 abbrev_section, ptr, section_kind);
6055
6056 length = header.get_length ();
6057
6058 /* Skip dummy type units. */
6059 if (ptr >= info_ptr + length
6060 || peek_abbrev_code (abfd, ptr) == 0
6061 || header.unit_type != DW_UT_type)
6062 {
6063 info_ptr += length;
6064 continue;
6065 }
6066
6067 if (types_htab == NULL)
6068 {
6069 if (dwo_file)
6070 types_htab = allocate_dwo_unit_table (objfile);
6071 else
6072 types_htab = allocate_signatured_type_table (objfile);
6073 }
6074
6075 if (dwo_file)
6076 {
6077 sig_type = NULL;
6078 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6079 struct dwo_unit);
6080 dwo_tu->dwo_file = dwo_file;
6081 dwo_tu->signature = header.signature;
6082 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6083 dwo_tu->section = section;
6084 dwo_tu->sect_off = sect_off;
6085 dwo_tu->length = length;
6086 }
6087 else
6088 {
6089 /* N.B.: type_offset is not usable if this type uses a DWO file.
6090 The real type_offset is in the DWO file. */
6091 dwo_tu = NULL;
6092 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6093 struct signatured_type);
6094 sig_type->signature = header.signature;
6095 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6096 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6097 sig_type->per_cu.is_debug_types = 1;
6098 sig_type->per_cu.section = section;
6099 sig_type->per_cu.sect_off = sect_off;
6100 sig_type->per_cu.length = length;
6101 }
6102
6103 slot = htab_find_slot (types_htab.get (),
6104 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6105 INSERT);
6106 gdb_assert (slot != NULL);
6107 if (*slot != NULL)
6108 {
6109 sect_offset dup_sect_off;
6110
6111 if (dwo_file)
6112 {
6113 const struct dwo_unit *dup_tu
6114 = (const struct dwo_unit *) *slot;
6115
6116 dup_sect_off = dup_tu->sect_off;
6117 }
6118 else
6119 {
6120 const struct signatured_type *dup_tu
6121 = (const struct signatured_type *) *slot;
6122
6123 dup_sect_off = dup_tu->per_cu.sect_off;
6124 }
6125
6126 complaint (_("debug type entry at offset %s is duplicate to"
6127 " the entry at offset %s, signature %s"),
6128 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6129 hex_string (header.signature));
6130 }
6131 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6132
6133 if (dwarf_read_debug > 1)
6134 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6135 sect_offset_str (sect_off),
6136 hex_string (header.signature));
6137
6138 info_ptr += length;
6139 }
6140 }
6141
6142 /* Create the hash table of all entries in the .debug_types
6143 (or .debug_types.dwo) section(s).
6144 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6145 otherwise it is NULL.
6146
6147 The result is a pointer to the hash table or NULL if there are no types.
6148
6149 Note: This function processes DWO files only, not DWP files. */
6150
6151 static void
6152 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6153 struct dwo_file *dwo_file,
6154 gdb::array_view<dwarf2_section_info> type_sections,
6155 htab_up &types_htab)
6156 {
6157 for (dwarf2_section_info &section : type_sections)
6158 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6159 types_htab, rcuh_kind::TYPE);
6160 }
6161
6162 /* Create the hash table of all entries in the .debug_types section,
6163 and initialize all_type_units.
6164 The result is zero if there is an error (e.g. missing .debug_types section),
6165 otherwise non-zero. */
6166
6167 static int
6168 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6169 {
6170 htab_up types_htab;
6171
6172 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6173 &dwarf2_per_objfile->info, types_htab,
6174 rcuh_kind::COMPILE);
6175 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6176 dwarf2_per_objfile->types, types_htab);
6177 if (types_htab == NULL)
6178 {
6179 dwarf2_per_objfile->signatured_types = NULL;
6180 return 0;
6181 }
6182
6183 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6184
6185 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6186 dwarf2_per_objfile->all_type_units.reserve
6187 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6188
6189 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6190 add_signatured_type_cu_to_table,
6191 &dwarf2_per_objfile->all_type_units);
6192
6193 return 1;
6194 }
6195
6196 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6197 If SLOT is non-NULL, it is the entry to use in the hash table.
6198 Otherwise we find one. */
6199
6200 static struct signatured_type *
6201 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6202 void **slot)
6203 {
6204 struct objfile *objfile = dwarf2_per_objfile->objfile;
6205
6206 if (dwarf2_per_objfile->all_type_units.size ()
6207 == dwarf2_per_objfile->all_type_units.capacity ())
6208 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6209
6210 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6211 struct signatured_type);
6212
6213 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6214 sig_type->signature = sig;
6215 sig_type->per_cu.is_debug_types = 1;
6216 if (dwarf2_per_objfile->using_index)
6217 {
6218 sig_type->per_cu.v.quick =
6219 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6220 struct dwarf2_per_cu_quick_data);
6221 }
6222
6223 if (slot == NULL)
6224 {
6225 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6226 sig_type, INSERT);
6227 }
6228 gdb_assert (*slot == NULL);
6229 *slot = sig_type;
6230 /* The rest of sig_type must be filled in by the caller. */
6231 return sig_type;
6232 }
6233
6234 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6235 Fill in SIG_ENTRY with DWO_ENTRY. */
6236
6237 static void
6238 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6239 struct signatured_type *sig_entry,
6240 struct dwo_unit *dwo_entry)
6241 {
6242 /* Make sure we're not clobbering something we don't expect to. */
6243 gdb_assert (! sig_entry->per_cu.queued);
6244 gdb_assert (sig_entry->per_cu.cu == NULL);
6245 if (dwarf2_per_objfile->using_index)
6246 {
6247 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6248 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6249 }
6250 else
6251 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6252 gdb_assert (sig_entry->signature == dwo_entry->signature);
6253 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6254 gdb_assert (sig_entry->type_unit_group == NULL);
6255 gdb_assert (sig_entry->dwo_unit == NULL);
6256
6257 sig_entry->per_cu.section = dwo_entry->section;
6258 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6259 sig_entry->per_cu.length = dwo_entry->length;
6260 sig_entry->per_cu.reading_dwo_directly = 1;
6261 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6262 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6263 sig_entry->dwo_unit = dwo_entry;
6264 }
6265
6266 /* Subroutine of lookup_signatured_type.
6267 If we haven't read the TU yet, create the signatured_type data structure
6268 for a TU to be read in directly from a DWO file, bypassing the stub.
6269 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6270 using .gdb_index, then when reading a CU we want to stay in the DWO file
6271 containing that CU. Otherwise we could end up reading several other DWO
6272 files (due to comdat folding) to process the transitive closure of all the
6273 mentioned TUs, and that can be slow. The current DWO file will have every
6274 type signature that it needs.
6275 We only do this for .gdb_index because in the psymtab case we already have
6276 to read all the DWOs to build the type unit groups. */
6277
6278 static struct signatured_type *
6279 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6280 {
6281 struct dwarf2_per_objfile *dwarf2_per_objfile
6282 = cu->per_cu->dwarf2_per_objfile;
6283 struct objfile *objfile = dwarf2_per_objfile->objfile;
6284 struct dwo_file *dwo_file;
6285 struct dwo_unit find_dwo_entry, *dwo_entry;
6286 struct signatured_type find_sig_entry, *sig_entry;
6287 void **slot;
6288
6289 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6290
6291 /* If TU skeletons have been removed then we may not have read in any
6292 TUs yet. */
6293 if (dwarf2_per_objfile->signatured_types == NULL)
6294 {
6295 dwarf2_per_objfile->signatured_types
6296 = allocate_signatured_type_table (objfile);
6297 }
6298
6299 /* We only ever need to read in one copy of a signatured type.
6300 Use the global signatured_types array to do our own comdat-folding
6301 of types. If this is the first time we're reading this TU, and
6302 the TU has an entry in .gdb_index, replace the recorded data from
6303 .gdb_index with this TU. */
6304
6305 find_sig_entry.signature = sig;
6306 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6307 &find_sig_entry, INSERT);
6308 sig_entry = (struct signatured_type *) *slot;
6309
6310 /* We can get here with the TU already read, *or* in the process of being
6311 read. Don't reassign the global entry to point to this DWO if that's
6312 the case. Also note that if the TU is already being read, it may not
6313 have come from a DWO, the program may be a mix of Fission-compiled
6314 code and non-Fission-compiled code. */
6315
6316 /* Have we already tried to read this TU?
6317 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6318 needn't exist in the global table yet). */
6319 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6320 return sig_entry;
6321
6322 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6323 dwo_unit of the TU itself. */
6324 dwo_file = cu->dwo_unit->dwo_file;
6325
6326 /* Ok, this is the first time we're reading this TU. */
6327 if (dwo_file->tus == NULL)
6328 return NULL;
6329 find_dwo_entry.signature = sig;
6330 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6331 &find_dwo_entry);
6332 if (dwo_entry == NULL)
6333 return NULL;
6334
6335 /* If the global table doesn't have an entry for this TU, add one. */
6336 if (sig_entry == NULL)
6337 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6338
6339 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6340 sig_entry->per_cu.tu_read = 1;
6341 return sig_entry;
6342 }
6343
6344 /* Subroutine of lookup_signatured_type.
6345 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6346 then try the DWP file. If the TU stub (skeleton) has been removed then
6347 it won't be in .gdb_index. */
6348
6349 static struct signatured_type *
6350 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6351 {
6352 struct dwarf2_per_objfile *dwarf2_per_objfile
6353 = cu->per_cu->dwarf2_per_objfile;
6354 struct objfile *objfile = dwarf2_per_objfile->objfile;
6355 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6356 struct dwo_unit *dwo_entry;
6357 struct signatured_type find_sig_entry, *sig_entry;
6358 void **slot;
6359
6360 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6361 gdb_assert (dwp_file != NULL);
6362
6363 /* If TU skeletons have been removed then we may not have read in any
6364 TUs yet. */
6365 if (dwarf2_per_objfile->signatured_types == NULL)
6366 {
6367 dwarf2_per_objfile->signatured_types
6368 = allocate_signatured_type_table (objfile);
6369 }
6370
6371 find_sig_entry.signature = sig;
6372 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6373 &find_sig_entry, INSERT);
6374 sig_entry = (struct signatured_type *) *slot;
6375
6376 /* Have we already tried to read this TU?
6377 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6378 needn't exist in the global table yet). */
6379 if (sig_entry != NULL)
6380 return sig_entry;
6381
6382 if (dwp_file->tus == NULL)
6383 return NULL;
6384 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6385 sig, 1 /* is_debug_types */);
6386 if (dwo_entry == NULL)
6387 return NULL;
6388
6389 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6390 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6391
6392 return sig_entry;
6393 }
6394
6395 /* Lookup a signature based type for DW_FORM_ref_sig8.
6396 Returns NULL if signature SIG is not present in the table.
6397 It is up to the caller to complain about this. */
6398
6399 static struct signatured_type *
6400 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6401 {
6402 struct dwarf2_per_objfile *dwarf2_per_objfile
6403 = cu->per_cu->dwarf2_per_objfile;
6404
6405 if (cu->dwo_unit
6406 && dwarf2_per_objfile->using_index)
6407 {
6408 /* We're in a DWO/DWP file, and we're using .gdb_index.
6409 These cases require special processing. */
6410 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6411 return lookup_dwo_signatured_type (cu, sig);
6412 else
6413 return lookup_dwp_signatured_type (cu, sig);
6414 }
6415 else
6416 {
6417 struct signatured_type find_entry, *entry;
6418
6419 if (dwarf2_per_objfile->signatured_types == NULL)
6420 return NULL;
6421 find_entry.signature = sig;
6422 entry = ((struct signatured_type *)
6423 htab_find (dwarf2_per_objfile->signatured_types.get (),
6424 &find_entry));
6425 return entry;
6426 }
6427 }
6428
6429 /* Return the address base of the compile unit, which, if exists, is stored
6430 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6431 static gdb::optional<ULONGEST>
6432 lookup_addr_base (struct die_info *comp_unit_die)
6433 {
6434 struct attribute *attr;
6435 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6436 if (attr == nullptr)
6437 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6438 if (attr == nullptr)
6439 return gdb::optional<ULONGEST> ();
6440 return DW_UNSND (attr);
6441 }
6442
6443 /* Return range lists base of the compile unit, which, if exists, is stored
6444 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6445 static ULONGEST
6446 lookup_ranges_base (struct die_info *comp_unit_die)
6447 {
6448 struct attribute *attr;
6449 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6450 if (attr == nullptr)
6451 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6452 if (attr == nullptr)
6453 return 0;
6454 return DW_UNSND (attr);
6455 }
6456
6457 /* Low level DIE reading support. */
6458
6459 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6460
6461 static void
6462 init_cu_die_reader (struct die_reader_specs *reader,
6463 struct dwarf2_cu *cu,
6464 struct dwarf2_section_info *section,
6465 struct dwo_file *dwo_file,
6466 struct abbrev_table *abbrev_table)
6467 {
6468 gdb_assert (section->readin && section->buffer != NULL);
6469 reader->abfd = section->get_bfd_owner ();
6470 reader->cu = cu;
6471 reader->dwo_file = dwo_file;
6472 reader->die_section = section;
6473 reader->buffer = section->buffer;
6474 reader->buffer_end = section->buffer + section->size;
6475 reader->abbrev_table = abbrev_table;
6476 }
6477
6478 /* Subroutine of cutu_reader to simplify it.
6479 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6480 There's just a lot of work to do, and cutu_reader is big enough
6481 already.
6482
6483 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6484 from it to the DIE in the DWO. If NULL we are skipping the stub.
6485 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6486 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6487 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6488 STUB_COMP_DIR may be non-NULL.
6489 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6490 are filled in with the info of the DIE from the DWO file.
6491 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6492 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6493 kept around for at least as long as *RESULT_READER.
6494
6495 The result is non-zero if a valid (non-dummy) DIE was found. */
6496
6497 static int
6498 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6499 struct dwo_unit *dwo_unit,
6500 struct die_info *stub_comp_unit_die,
6501 const char *stub_comp_dir,
6502 struct die_reader_specs *result_reader,
6503 const gdb_byte **result_info_ptr,
6504 struct die_info **result_comp_unit_die,
6505 abbrev_table_up *result_dwo_abbrev_table)
6506 {
6507 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6508 struct objfile *objfile = dwarf2_per_objfile->objfile;
6509 struct dwarf2_cu *cu = this_cu->cu;
6510 bfd *abfd;
6511 const gdb_byte *begin_info_ptr, *info_ptr;
6512 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6513 int i,num_extra_attrs;
6514 struct dwarf2_section_info *dwo_abbrev_section;
6515 struct die_info *comp_unit_die;
6516
6517 /* At most one of these may be provided. */
6518 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6519
6520 /* These attributes aren't processed until later:
6521 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6522 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6523 referenced later. However, these attributes are found in the stub
6524 which we won't have later. In order to not impose this complication
6525 on the rest of the code, we read them here and copy them to the
6526 DWO CU/TU die. */
6527
6528 stmt_list = NULL;
6529 low_pc = NULL;
6530 high_pc = NULL;
6531 ranges = NULL;
6532 comp_dir = NULL;
6533
6534 if (stub_comp_unit_die != NULL)
6535 {
6536 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6537 DWO file. */
6538 if (! this_cu->is_debug_types)
6539 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6540 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6541 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6542 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6543 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6544
6545 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6546
6547 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6548 here (if needed). We need the value before we can process
6549 DW_AT_ranges. */
6550 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6551 }
6552 else if (stub_comp_dir != NULL)
6553 {
6554 /* Reconstruct the comp_dir attribute to simplify the code below. */
6555 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6556 comp_dir->name = DW_AT_comp_dir;
6557 comp_dir->form = DW_FORM_string;
6558 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6559 DW_STRING (comp_dir) = stub_comp_dir;
6560 }
6561
6562 /* Set up for reading the DWO CU/TU. */
6563 cu->dwo_unit = dwo_unit;
6564 dwarf2_section_info *section = dwo_unit->section;
6565 section->read (objfile);
6566 abfd = section->get_bfd_owner ();
6567 begin_info_ptr = info_ptr = (section->buffer
6568 + to_underlying (dwo_unit->sect_off));
6569 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6570
6571 if (this_cu->is_debug_types)
6572 {
6573 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6574
6575 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6576 &cu->header, section,
6577 dwo_abbrev_section,
6578 info_ptr, rcuh_kind::TYPE);
6579 /* This is not an assert because it can be caused by bad debug info. */
6580 if (sig_type->signature != cu->header.signature)
6581 {
6582 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6583 " TU at offset %s [in module %s]"),
6584 hex_string (sig_type->signature),
6585 hex_string (cu->header.signature),
6586 sect_offset_str (dwo_unit->sect_off),
6587 bfd_get_filename (abfd));
6588 }
6589 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6590 /* For DWOs coming from DWP files, we don't know the CU length
6591 nor the type's offset in the TU until now. */
6592 dwo_unit->length = cu->header.get_length ();
6593 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6594
6595 /* Establish the type offset that can be used to lookup the type.
6596 For DWO files, we don't know it until now. */
6597 sig_type->type_offset_in_section
6598 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6599 }
6600 else
6601 {
6602 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6603 &cu->header, section,
6604 dwo_abbrev_section,
6605 info_ptr, rcuh_kind::COMPILE);
6606 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6607 /* For DWOs coming from DWP files, we don't know the CU length
6608 until now. */
6609 dwo_unit->length = cu->header.get_length ();
6610 }
6611
6612 *result_dwo_abbrev_table
6613 = abbrev_table::read (objfile, dwo_abbrev_section,
6614 cu->header.abbrev_sect_off);
6615 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6616 result_dwo_abbrev_table->get ());
6617
6618 /* Read in the die, but leave space to copy over the attributes
6619 from the stub. This has the benefit of simplifying the rest of
6620 the code - all the work to maintain the illusion of a single
6621 DW_TAG_{compile,type}_unit DIE is done here. */
6622 num_extra_attrs = ((stmt_list != NULL)
6623 + (low_pc != NULL)
6624 + (high_pc != NULL)
6625 + (ranges != NULL)
6626 + (comp_dir != NULL));
6627 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6628 num_extra_attrs);
6629
6630 /* Copy over the attributes from the stub to the DIE we just read in. */
6631 comp_unit_die = *result_comp_unit_die;
6632 i = comp_unit_die->num_attrs;
6633 if (stmt_list != NULL)
6634 comp_unit_die->attrs[i++] = *stmt_list;
6635 if (low_pc != NULL)
6636 comp_unit_die->attrs[i++] = *low_pc;
6637 if (high_pc != NULL)
6638 comp_unit_die->attrs[i++] = *high_pc;
6639 if (ranges != NULL)
6640 comp_unit_die->attrs[i++] = *ranges;
6641 if (comp_dir != NULL)
6642 comp_unit_die->attrs[i++] = *comp_dir;
6643 comp_unit_die->num_attrs += num_extra_attrs;
6644
6645 if (dwarf_die_debug)
6646 {
6647 fprintf_unfiltered (gdb_stdlog,
6648 "Read die from %s@0x%x of %s:\n",
6649 section->get_name (),
6650 (unsigned) (begin_info_ptr - section->buffer),
6651 bfd_get_filename (abfd));
6652 dump_die (comp_unit_die, dwarf_die_debug);
6653 }
6654
6655 /* Skip dummy compilation units. */
6656 if (info_ptr >= begin_info_ptr + dwo_unit->length
6657 || peek_abbrev_code (abfd, info_ptr) == 0)
6658 return 0;
6659
6660 *result_info_ptr = info_ptr;
6661 return 1;
6662 }
6663
6664 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6665 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6666 signature is part of the header. */
6667 static gdb::optional<ULONGEST>
6668 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6669 {
6670 if (cu->header.version >= 5)
6671 return cu->header.signature;
6672 struct attribute *attr;
6673 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6674 if (attr == nullptr)
6675 return gdb::optional<ULONGEST> ();
6676 return DW_UNSND (attr);
6677 }
6678
6679 /* Subroutine of cutu_reader to simplify it.
6680 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6681 Returns NULL if the specified DWO unit cannot be found. */
6682
6683 static struct dwo_unit *
6684 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6685 struct die_info *comp_unit_die,
6686 const char *dwo_name)
6687 {
6688 struct dwarf2_cu *cu = this_cu->cu;
6689 struct dwo_unit *dwo_unit;
6690 const char *comp_dir;
6691
6692 gdb_assert (cu != NULL);
6693
6694 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6695 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6696 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6697
6698 if (this_cu->is_debug_types)
6699 {
6700 struct signatured_type *sig_type;
6701
6702 /* Since this_cu is the first member of struct signatured_type,
6703 we can go from a pointer to one to a pointer to the other. */
6704 sig_type = (struct signatured_type *) this_cu;
6705 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6706 }
6707 else
6708 {
6709 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6710 if (!signature.has_value ())
6711 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6712 " [in module %s]"),
6713 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6714 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6715 *signature);
6716 }
6717
6718 return dwo_unit;
6719 }
6720
6721 /* Subroutine of cutu_reader to simplify it.
6722 See it for a description of the parameters.
6723 Read a TU directly from a DWO file, bypassing the stub. */
6724
6725 void
6726 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6727 int use_existing_cu)
6728 {
6729 struct signatured_type *sig_type;
6730 struct die_reader_specs reader;
6731
6732 /* Verify we can do the following downcast, and that we have the
6733 data we need. */
6734 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6735 sig_type = (struct signatured_type *) this_cu;
6736 gdb_assert (sig_type->dwo_unit != NULL);
6737
6738 if (use_existing_cu && this_cu->cu != NULL)
6739 {
6740 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6741 /* There's no need to do the rereading_dwo_cu handling that
6742 cutu_reader does since we don't read the stub. */
6743 }
6744 else
6745 {
6746 /* If !use_existing_cu, this_cu->cu must be NULL. */
6747 gdb_assert (this_cu->cu == NULL);
6748 m_new_cu.reset (new dwarf2_cu (this_cu));
6749 }
6750
6751 /* A future optimization, if needed, would be to use an existing
6752 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6753 could share abbrev tables. */
6754
6755 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6756 NULL /* stub_comp_unit_die */,
6757 sig_type->dwo_unit->dwo_file->comp_dir,
6758 &reader, &info_ptr,
6759 &comp_unit_die,
6760 &m_dwo_abbrev_table) == 0)
6761 {
6762 /* Dummy die. */
6763 dummy_p = true;
6764 }
6765 }
6766
6767 /* Initialize a CU (or TU) and read its DIEs.
6768 If the CU defers to a DWO file, read the DWO file as well.
6769
6770 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6771 Otherwise the table specified in the comp unit header is read in and used.
6772 This is an optimization for when we already have the abbrev table.
6773
6774 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6775 Otherwise, a new CU is allocated with xmalloc. */
6776
6777 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6778 struct abbrev_table *abbrev_table,
6779 int use_existing_cu,
6780 bool skip_partial)
6781 : die_reader_specs {},
6782 m_this_cu (this_cu)
6783 {
6784 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6785 struct objfile *objfile = dwarf2_per_objfile->objfile;
6786 struct dwarf2_section_info *section = this_cu->section;
6787 bfd *abfd = section->get_bfd_owner ();
6788 struct dwarf2_cu *cu;
6789 const gdb_byte *begin_info_ptr;
6790 struct signatured_type *sig_type = NULL;
6791 struct dwarf2_section_info *abbrev_section;
6792 /* Non-zero if CU currently points to a DWO file and we need to
6793 reread it. When this happens we need to reread the skeleton die
6794 before we can reread the DWO file (this only applies to CUs, not TUs). */
6795 int rereading_dwo_cu = 0;
6796
6797 if (dwarf_die_debug)
6798 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6799 this_cu->is_debug_types ? "type" : "comp",
6800 sect_offset_str (this_cu->sect_off));
6801
6802 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6803 file (instead of going through the stub), short-circuit all of this. */
6804 if (this_cu->reading_dwo_directly)
6805 {
6806 /* Narrow down the scope of possibilities to have to understand. */
6807 gdb_assert (this_cu->is_debug_types);
6808 gdb_assert (abbrev_table == NULL);
6809 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6810 return;
6811 }
6812
6813 /* This is cheap if the section is already read in. */
6814 section->read (objfile);
6815
6816 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6817
6818 abbrev_section = get_abbrev_section_for_cu (this_cu);
6819
6820 if (use_existing_cu && this_cu->cu != NULL)
6821 {
6822 cu = this_cu->cu;
6823 /* If this CU is from a DWO file we need to start over, we need to
6824 refetch the attributes from the skeleton CU.
6825 This could be optimized by retrieving those attributes from when we
6826 were here the first time: the previous comp_unit_die was stored in
6827 comp_unit_obstack. But there's no data yet that we need this
6828 optimization. */
6829 if (cu->dwo_unit != NULL)
6830 rereading_dwo_cu = 1;
6831 }
6832 else
6833 {
6834 /* If !use_existing_cu, this_cu->cu must be NULL. */
6835 gdb_assert (this_cu->cu == NULL);
6836 m_new_cu.reset (new dwarf2_cu (this_cu));
6837 cu = m_new_cu.get ();
6838 }
6839
6840 /* Get the header. */
6841 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6842 {
6843 /* We already have the header, there's no need to read it in again. */
6844 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6845 }
6846 else
6847 {
6848 if (this_cu->is_debug_types)
6849 {
6850 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6851 &cu->header, section,
6852 abbrev_section, info_ptr,
6853 rcuh_kind::TYPE);
6854
6855 /* Since per_cu is the first member of struct signatured_type,
6856 we can go from a pointer to one to a pointer to the other. */
6857 sig_type = (struct signatured_type *) this_cu;
6858 gdb_assert (sig_type->signature == cu->header.signature);
6859 gdb_assert (sig_type->type_offset_in_tu
6860 == cu->header.type_cu_offset_in_tu);
6861 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6862
6863 /* LENGTH has not been set yet for type units if we're
6864 using .gdb_index. */
6865 this_cu->length = cu->header.get_length ();
6866
6867 /* Establish the type offset that can be used to lookup the type. */
6868 sig_type->type_offset_in_section =
6869 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6870
6871 this_cu->dwarf_version = cu->header.version;
6872 }
6873 else
6874 {
6875 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6876 &cu->header, section,
6877 abbrev_section,
6878 info_ptr,
6879 rcuh_kind::COMPILE);
6880
6881 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6882 gdb_assert (this_cu->length == cu->header.get_length ());
6883 this_cu->dwarf_version = cu->header.version;
6884 }
6885 }
6886
6887 /* Skip dummy compilation units. */
6888 if (info_ptr >= begin_info_ptr + this_cu->length
6889 || peek_abbrev_code (abfd, info_ptr) == 0)
6890 {
6891 dummy_p = true;
6892 return;
6893 }
6894
6895 /* If we don't have them yet, read the abbrevs for this compilation unit.
6896 And if we need to read them now, make sure they're freed when we're
6897 done. */
6898 if (abbrev_table != NULL)
6899 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6900 else
6901 {
6902 m_abbrev_table_holder
6903 = abbrev_table::read (objfile, abbrev_section,
6904 cu->header.abbrev_sect_off);
6905 abbrev_table = m_abbrev_table_holder.get ();
6906 }
6907
6908 /* Read the top level CU/TU die. */
6909 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6910 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6911
6912 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6913 {
6914 dummy_p = true;
6915 return;
6916 }
6917
6918 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6919 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6920 table from the DWO file and pass the ownership over to us. It will be
6921 referenced from READER, so we must make sure to free it after we're done
6922 with READER.
6923
6924 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6925 DWO CU, that this test will fail (the attribute will not be present). */
6926 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6927 if (dwo_name != nullptr)
6928 {
6929 struct dwo_unit *dwo_unit;
6930 struct die_info *dwo_comp_unit_die;
6931
6932 if (comp_unit_die->has_children)
6933 {
6934 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6935 " has children (offset %s) [in module %s]"),
6936 sect_offset_str (this_cu->sect_off),
6937 bfd_get_filename (abfd));
6938 }
6939 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6940 if (dwo_unit != NULL)
6941 {
6942 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6943 comp_unit_die, NULL,
6944 this, &info_ptr,
6945 &dwo_comp_unit_die,
6946 &m_dwo_abbrev_table) == 0)
6947 {
6948 /* Dummy die. */
6949 dummy_p = true;
6950 return;
6951 }
6952 comp_unit_die = dwo_comp_unit_die;
6953 }
6954 else
6955 {
6956 /* Yikes, we couldn't find the rest of the DIE, we only have
6957 the stub. A complaint has already been logged. There's
6958 not much more we can do except pass on the stub DIE to
6959 die_reader_func. We don't want to throw an error on bad
6960 debug info. */
6961 }
6962 }
6963 }
6964
6965 void
6966 cutu_reader::keep ()
6967 {
6968 /* Done, clean up. */
6969 gdb_assert (!dummy_p);
6970 if (m_new_cu != NULL)
6971 {
6972 struct dwarf2_per_objfile *dwarf2_per_objfile
6973 = m_this_cu->dwarf2_per_objfile;
6974 /* Link this CU into read_in_chain. */
6975 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6976 dwarf2_per_objfile->read_in_chain = m_this_cu;
6977 /* The chain owns it now. */
6978 m_new_cu.release ();
6979 }
6980 }
6981
6982 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6983 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6984 assumed to have already done the lookup to find the DWO file).
6985
6986 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6987 THIS_CU->is_debug_types, but nothing else.
6988
6989 We fill in THIS_CU->length.
6990
6991 THIS_CU->cu is always freed when done.
6992 This is done in order to not leave THIS_CU->cu in a state where we have
6993 to care whether it refers to the "main" CU or the DWO CU.
6994
6995 When parent_cu is passed, it is used to provide a default value for
6996 str_offsets_base and addr_base from the parent. */
6997
6998 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6999 struct dwarf2_cu *parent_cu,
7000 struct dwo_file *dwo_file)
7001 : die_reader_specs {},
7002 m_this_cu (this_cu)
7003 {
7004 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7005 struct objfile *objfile = dwarf2_per_objfile->objfile;
7006 struct dwarf2_section_info *section = this_cu->section;
7007 bfd *abfd = section->get_bfd_owner ();
7008 struct dwarf2_section_info *abbrev_section;
7009 const gdb_byte *begin_info_ptr, *info_ptr;
7010
7011 if (dwarf_die_debug)
7012 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7013 this_cu->is_debug_types ? "type" : "comp",
7014 sect_offset_str (this_cu->sect_off));
7015
7016 gdb_assert (this_cu->cu == NULL);
7017
7018 abbrev_section = (dwo_file != NULL
7019 ? &dwo_file->sections.abbrev
7020 : get_abbrev_section_for_cu (this_cu));
7021
7022 /* This is cheap if the section is already read in. */
7023 section->read (objfile);
7024
7025 m_new_cu.reset (new dwarf2_cu (this_cu));
7026
7027 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7028 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7029 &m_new_cu->header, section,
7030 abbrev_section, info_ptr,
7031 (this_cu->is_debug_types
7032 ? rcuh_kind::TYPE
7033 : rcuh_kind::COMPILE));
7034
7035 if (parent_cu != nullptr)
7036 {
7037 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7038 m_new_cu->addr_base = parent_cu->addr_base;
7039 }
7040 this_cu->length = m_new_cu->header.get_length ();
7041
7042 /* Skip dummy compilation units. */
7043 if (info_ptr >= begin_info_ptr + this_cu->length
7044 || peek_abbrev_code (abfd, info_ptr) == 0)
7045 {
7046 dummy_p = true;
7047 return;
7048 }
7049
7050 m_abbrev_table_holder
7051 = abbrev_table::read (objfile, abbrev_section,
7052 m_new_cu->header.abbrev_sect_off);
7053
7054 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7055 m_abbrev_table_holder.get ());
7056 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7057 }
7058
7059 \f
7060 /* Type Unit Groups.
7061
7062 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7063 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7064 so that all types coming from the same compilation (.o file) are grouped
7065 together. A future step could be to put the types in the same symtab as
7066 the CU the types ultimately came from. */
7067
7068 static hashval_t
7069 hash_type_unit_group (const void *item)
7070 {
7071 const struct type_unit_group *tu_group
7072 = (const struct type_unit_group *) item;
7073
7074 return hash_stmt_list_entry (&tu_group->hash);
7075 }
7076
7077 static int
7078 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7079 {
7080 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7081 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7082
7083 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7084 }
7085
7086 /* Allocate a hash table for type unit groups. */
7087
7088 static htab_up
7089 allocate_type_unit_groups_table (struct objfile *objfile)
7090 {
7091 return htab_up (htab_create_alloc (3,
7092 hash_type_unit_group,
7093 eq_type_unit_group,
7094 NULL, xcalloc, xfree));
7095 }
7096
7097 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7098 partial symtabs. We combine several TUs per psymtab to not let the size
7099 of any one psymtab grow too big. */
7100 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7101 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7102
7103 /* Helper routine for get_type_unit_group.
7104 Create the type_unit_group object used to hold one or more TUs. */
7105
7106 static struct type_unit_group *
7107 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7108 {
7109 struct dwarf2_per_objfile *dwarf2_per_objfile
7110 = cu->per_cu->dwarf2_per_objfile;
7111 struct objfile *objfile = dwarf2_per_objfile->objfile;
7112 struct dwarf2_per_cu_data *per_cu;
7113 struct type_unit_group *tu_group;
7114
7115 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7116 struct type_unit_group);
7117 per_cu = &tu_group->per_cu;
7118 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7119
7120 if (dwarf2_per_objfile->using_index)
7121 {
7122 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7123 struct dwarf2_per_cu_quick_data);
7124 }
7125 else
7126 {
7127 unsigned int line_offset = to_underlying (line_offset_struct);
7128 dwarf2_psymtab *pst;
7129 std::string name;
7130
7131 /* Give the symtab a useful name for debug purposes. */
7132 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7133 name = string_printf ("<type_units_%d>",
7134 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7135 else
7136 name = string_printf ("<type_units_at_0x%x>", line_offset);
7137
7138 pst = create_partial_symtab (per_cu, name.c_str ());
7139 pst->anonymous = true;
7140 }
7141
7142 tu_group->hash.dwo_unit = cu->dwo_unit;
7143 tu_group->hash.line_sect_off = line_offset_struct;
7144
7145 return tu_group;
7146 }
7147
7148 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7149 STMT_LIST is a DW_AT_stmt_list attribute. */
7150
7151 static struct type_unit_group *
7152 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7153 {
7154 struct dwarf2_per_objfile *dwarf2_per_objfile
7155 = cu->per_cu->dwarf2_per_objfile;
7156 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7157 struct type_unit_group *tu_group;
7158 void **slot;
7159 unsigned int line_offset;
7160 struct type_unit_group type_unit_group_for_lookup;
7161
7162 if (dwarf2_per_objfile->type_unit_groups == NULL)
7163 {
7164 dwarf2_per_objfile->type_unit_groups =
7165 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7166 }
7167
7168 /* Do we need to create a new group, or can we use an existing one? */
7169
7170 if (stmt_list)
7171 {
7172 line_offset = DW_UNSND (stmt_list);
7173 ++tu_stats->nr_symtab_sharers;
7174 }
7175 else
7176 {
7177 /* Ugh, no stmt_list. Rare, but we have to handle it.
7178 We can do various things here like create one group per TU or
7179 spread them over multiple groups to split up the expansion work.
7180 To avoid worst case scenarios (too many groups or too large groups)
7181 we, umm, group them in bunches. */
7182 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7183 | (tu_stats->nr_stmt_less_type_units
7184 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7185 ++tu_stats->nr_stmt_less_type_units;
7186 }
7187
7188 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7189 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7190 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7191 &type_unit_group_for_lookup, INSERT);
7192 if (*slot != NULL)
7193 {
7194 tu_group = (struct type_unit_group *) *slot;
7195 gdb_assert (tu_group != NULL);
7196 }
7197 else
7198 {
7199 sect_offset line_offset_struct = (sect_offset) line_offset;
7200 tu_group = create_type_unit_group (cu, line_offset_struct);
7201 *slot = tu_group;
7202 ++tu_stats->nr_symtabs;
7203 }
7204
7205 return tu_group;
7206 }
7207 \f
7208 /* Partial symbol tables. */
7209
7210 /* Create a psymtab named NAME and assign it to PER_CU.
7211
7212 The caller must fill in the following details:
7213 dirname, textlow, texthigh. */
7214
7215 static dwarf2_psymtab *
7216 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7217 {
7218 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7219 dwarf2_psymtab *pst;
7220
7221 pst = new dwarf2_psymtab (name, objfile, 0);
7222
7223 pst->psymtabs_addrmap_supported = true;
7224
7225 /* This is the glue that links PST into GDB's symbol API. */
7226 pst->per_cu_data = per_cu;
7227 per_cu->v.psymtab = pst;
7228
7229 return pst;
7230 }
7231
7232 /* DIE reader function for process_psymtab_comp_unit. */
7233
7234 static void
7235 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7236 const gdb_byte *info_ptr,
7237 struct die_info *comp_unit_die,
7238 enum language pretend_language)
7239 {
7240 struct dwarf2_cu *cu = reader->cu;
7241 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7243 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7244 CORE_ADDR baseaddr;
7245 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7246 dwarf2_psymtab *pst;
7247 enum pc_bounds_kind cu_bounds_kind;
7248 const char *filename;
7249
7250 gdb_assert (! per_cu->is_debug_types);
7251
7252 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7253
7254 /* Allocate a new partial symbol table structure. */
7255 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7256 if (filename == NULL)
7257 filename = "";
7258
7259 pst = create_partial_symtab (per_cu, filename);
7260
7261 /* This must be done before calling dwarf2_build_include_psymtabs. */
7262 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7263
7264 baseaddr = objfile->text_section_offset ();
7265
7266 dwarf2_find_base_address (comp_unit_die, cu);
7267
7268 /* Possibly set the default values of LOWPC and HIGHPC from
7269 `DW_AT_ranges'. */
7270 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7271 &best_highpc, cu, pst);
7272 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7273 {
7274 CORE_ADDR low
7275 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7276 - baseaddr);
7277 CORE_ADDR high
7278 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7279 - baseaddr - 1);
7280 /* Store the contiguous range if it is not empty; it can be
7281 empty for CUs with no code. */
7282 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7283 low, high, pst);
7284 }
7285
7286 /* Check if comp unit has_children.
7287 If so, read the rest of the partial symbols from this comp unit.
7288 If not, there's no more debug_info for this comp unit. */
7289 if (comp_unit_die->has_children)
7290 {
7291 struct partial_die_info *first_die;
7292 CORE_ADDR lowpc, highpc;
7293
7294 lowpc = ((CORE_ADDR) -1);
7295 highpc = ((CORE_ADDR) 0);
7296
7297 first_die = load_partial_dies (reader, info_ptr, 1);
7298
7299 scan_partial_symbols (first_die, &lowpc, &highpc,
7300 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7301
7302 /* If we didn't find a lowpc, set it to highpc to avoid
7303 complaints from `maint check'. */
7304 if (lowpc == ((CORE_ADDR) -1))
7305 lowpc = highpc;
7306
7307 /* If the compilation unit didn't have an explicit address range,
7308 then use the information extracted from its child dies. */
7309 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7310 {
7311 best_lowpc = lowpc;
7312 best_highpc = highpc;
7313 }
7314 }
7315 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7316 best_lowpc + baseaddr)
7317 - baseaddr);
7318 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7319 best_highpc + baseaddr)
7320 - baseaddr);
7321
7322 end_psymtab_common (objfile, pst);
7323
7324 if (!cu->per_cu->imported_symtabs_empty ())
7325 {
7326 int i;
7327 int len = cu->per_cu->imported_symtabs_size ();
7328
7329 /* Fill in 'dependencies' here; we fill in 'users' in a
7330 post-pass. */
7331 pst->number_of_dependencies = len;
7332 pst->dependencies
7333 = objfile->partial_symtabs->allocate_dependencies (len);
7334 for (i = 0; i < len; ++i)
7335 {
7336 pst->dependencies[i]
7337 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7338 }
7339
7340 cu->per_cu->imported_symtabs_free ();
7341 }
7342
7343 /* Get the list of files included in the current compilation unit,
7344 and build a psymtab for each of them. */
7345 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7346
7347 if (dwarf_read_debug)
7348 fprintf_unfiltered (gdb_stdlog,
7349 "Psymtab for %s unit @%s: %s - %s"
7350 ", %d global, %d static syms\n",
7351 per_cu->is_debug_types ? "type" : "comp",
7352 sect_offset_str (per_cu->sect_off),
7353 paddress (gdbarch, pst->text_low (objfile)),
7354 paddress (gdbarch, pst->text_high (objfile)),
7355 pst->n_global_syms, pst->n_static_syms);
7356 }
7357
7358 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7359 Process compilation unit THIS_CU for a psymtab. */
7360
7361 static void
7362 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7363 bool want_partial_unit,
7364 enum language pretend_language)
7365 {
7366 /* If this compilation unit was already read in, free the
7367 cached copy in order to read it in again. This is
7368 necessary because we skipped some symbols when we first
7369 read in the compilation unit (see load_partial_dies).
7370 This problem could be avoided, but the benefit is unclear. */
7371 if (this_cu->cu != NULL)
7372 free_one_cached_comp_unit (this_cu);
7373
7374 cutu_reader reader (this_cu, NULL, 0, false);
7375
7376 if (reader.dummy_p)
7377 {
7378 /* Nothing. */
7379 }
7380 else if (this_cu->is_debug_types)
7381 build_type_psymtabs_reader (&reader, reader.info_ptr,
7382 reader.comp_unit_die);
7383 else if (want_partial_unit
7384 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7385 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7386 reader.comp_unit_die,
7387 pretend_language);
7388
7389 /* Age out any secondary CUs. */
7390 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7391 }
7392
7393 /* Reader function for build_type_psymtabs. */
7394
7395 static void
7396 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7397 const gdb_byte *info_ptr,
7398 struct die_info *type_unit_die)
7399 {
7400 struct dwarf2_per_objfile *dwarf2_per_objfile
7401 = reader->cu->per_cu->dwarf2_per_objfile;
7402 struct objfile *objfile = dwarf2_per_objfile->objfile;
7403 struct dwarf2_cu *cu = reader->cu;
7404 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7405 struct signatured_type *sig_type;
7406 struct type_unit_group *tu_group;
7407 struct attribute *attr;
7408 struct partial_die_info *first_die;
7409 CORE_ADDR lowpc, highpc;
7410 dwarf2_psymtab *pst;
7411
7412 gdb_assert (per_cu->is_debug_types);
7413 sig_type = (struct signatured_type *) per_cu;
7414
7415 if (! type_unit_die->has_children)
7416 return;
7417
7418 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7419 tu_group = get_type_unit_group (cu, attr);
7420
7421 if (tu_group->tus == nullptr)
7422 tu_group->tus = new std::vector<signatured_type *>;
7423 tu_group->tus->push_back (sig_type);
7424
7425 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7426 pst = create_partial_symtab (per_cu, "");
7427 pst->anonymous = true;
7428
7429 first_die = load_partial_dies (reader, info_ptr, 1);
7430
7431 lowpc = (CORE_ADDR) -1;
7432 highpc = (CORE_ADDR) 0;
7433 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7434
7435 end_psymtab_common (objfile, pst);
7436 }
7437
7438 /* Struct used to sort TUs by their abbreviation table offset. */
7439
7440 struct tu_abbrev_offset
7441 {
7442 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7443 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7444 {}
7445
7446 signatured_type *sig_type;
7447 sect_offset abbrev_offset;
7448 };
7449
7450 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7451
7452 static bool
7453 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7454 const struct tu_abbrev_offset &b)
7455 {
7456 return a.abbrev_offset < b.abbrev_offset;
7457 }
7458
7459 /* Efficiently read all the type units.
7460 This does the bulk of the work for build_type_psymtabs.
7461
7462 The efficiency is because we sort TUs by the abbrev table they use and
7463 only read each abbrev table once. In one program there are 200K TUs
7464 sharing 8K abbrev tables.
7465
7466 The main purpose of this function is to support building the
7467 dwarf2_per_objfile->type_unit_groups table.
7468 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7469 can collapse the search space by grouping them by stmt_list.
7470 The savings can be significant, in the same program from above the 200K TUs
7471 share 8K stmt_list tables.
7472
7473 FUNC is expected to call get_type_unit_group, which will create the
7474 struct type_unit_group if necessary and add it to
7475 dwarf2_per_objfile->type_unit_groups. */
7476
7477 static void
7478 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7479 {
7480 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7481 abbrev_table_up abbrev_table;
7482 sect_offset abbrev_offset;
7483
7484 /* It's up to the caller to not call us multiple times. */
7485 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7486
7487 if (dwarf2_per_objfile->all_type_units.empty ())
7488 return;
7489
7490 /* TUs typically share abbrev tables, and there can be way more TUs than
7491 abbrev tables. Sort by abbrev table to reduce the number of times we
7492 read each abbrev table in.
7493 Alternatives are to punt or to maintain a cache of abbrev tables.
7494 This is simpler and efficient enough for now.
7495
7496 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7497 symtab to use). Typically TUs with the same abbrev offset have the same
7498 stmt_list value too so in practice this should work well.
7499
7500 The basic algorithm here is:
7501
7502 sort TUs by abbrev table
7503 for each TU with same abbrev table:
7504 read abbrev table if first user
7505 read TU top level DIE
7506 [IWBN if DWO skeletons had DW_AT_stmt_list]
7507 call FUNC */
7508
7509 if (dwarf_read_debug)
7510 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7511
7512 /* Sort in a separate table to maintain the order of all_type_units
7513 for .gdb_index: TU indices directly index all_type_units. */
7514 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7515 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7516
7517 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7518 sorted_by_abbrev.emplace_back
7519 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7520 sig_type->per_cu.section,
7521 sig_type->per_cu.sect_off));
7522
7523 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7524 sort_tu_by_abbrev_offset);
7525
7526 abbrev_offset = (sect_offset) ~(unsigned) 0;
7527
7528 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7529 {
7530 /* Switch to the next abbrev table if necessary. */
7531 if (abbrev_table == NULL
7532 || tu.abbrev_offset != abbrev_offset)
7533 {
7534 abbrev_offset = tu.abbrev_offset;
7535 abbrev_table =
7536 abbrev_table::read (dwarf2_per_objfile->objfile,
7537 &dwarf2_per_objfile->abbrev,
7538 abbrev_offset);
7539 ++tu_stats->nr_uniq_abbrev_tables;
7540 }
7541
7542 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7543 0, false);
7544 if (!reader.dummy_p)
7545 build_type_psymtabs_reader (&reader, reader.info_ptr,
7546 reader.comp_unit_die);
7547 }
7548 }
7549
7550 /* Print collected type unit statistics. */
7551
7552 static void
7553 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7554 {
7555 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7556
7557 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7558 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7559 dwarf2_per_objfile->all_type_units.size ());
7560 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7561 tu_stats->nr_uniq_abbrev_tables);
7562 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7563 tu_stats->nr_symtabs);
7564 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7565 tu_stats->nr_symtab_sharers);
7566 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7567 tu_stats->nr_stmt_less_type_units);
7568 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7569 tu_stats->nr_all_type_units_reallocs);
7570 }
7571
7572 /* Traversal function for build_type_psymtabs. */
7573
7574 static int
7575 build_type_psymtab_dependencies (void **slot, void *info)
7576 {
7577 struct dwarf2_per_objfile *dwarf2_per_objfile
7578 = (struct dwarf2_per_objfile *) info;
7579 struct objfile *objfile = dwarf2_per_objfile->objfile;
7580 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7581 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7582 dwarf2_psymtab *pst = per_cu->v.psymtab;
7583 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7584 int i;
7585
7586 gdb_assert (len > 0);
7587 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7588
7589 pst->number_of_dependencies = len;
7590 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7591 for (i = 0; i < len; ++i)
7592 {
7593 struct signatured_type *iter = tu_group->tus->at (i);
7594 gdb_assert (iter->per_cu.is_debug_types);
7595 pst->dependencies[i] = iter->per_cu.v.psymtab;
7596 iter->type_unit_group = tu_group;
7597 }
7598
7599 delete tu_group->tus;
7600 tu_group->tus = nullptr;
7601
7602 return 1;
7603 }
7604
7605 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7606 Build partial symbol tables for the .debug_types comp-units. */
7607
7608 static void
7609 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7610 {
7611 if (! create_all_type_units (dwarf2_per_objfile))
7612 return;
7613
7614 build_type_psymtabs_1 (dwarf2_per_objfile);
7615 }
7616
7617 /* Traversal function for process_skeletonless_type_unit.
7618 Read a TU in a DWO file and build partial symbols for it. */
7619
7620 static int
7621 process_skeletonless_type_unit (void **slot, void *info)
7622 {
7623 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7624 struct dwarf2_per_objfile *dwarf2_per_objfile
7625 = (struct dwarf2_per_objfile *) info;
7626 struct signatured_type find_entry, *entry;
7627
7628 /* If this TU doesn't exist in the global table, add it and read it in. */
7629
7630 if (dwarf2_per_objfile->signatured_types == NULL)
7631 {
7632 dwarf2_per_objfile->signatured_types
7633 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
7634 }
7635
7636 find_entry.signature = dwo_unit->signature;
7637 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7638 &find_entry, INSERT);
7639 /* If we've already seen this type there's nothing to do. What's happening
7640 is we're doing our own version of comdat-folding here. */
7641 if (*slot != NULL)
7642 return 1;
7643
7644 /* This does the job that create_all_type_units would have done for
7645 this TU. */
7646 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7647 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7648 *slot = entry;
7649
7650 /* This does the job that build_type_psymtabs_1 would have done. */
7651 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7652 if (!reader.dummy_p)
7653 build_type_psymtabs_reader (&reader, reader.info_ptr,
7654 reader.comp_unit_die);
7655
7656 return 1;
7657 }
7658
7659 /* Traversal function for process_skeletonless_type_units. */
7660
7661 static int
7662 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7663 {
7664 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7665
7666 if (dwo_file->tus != NULL)
7667 htab_traverse_noresize (dwo_file->tus.get (),
7668 process_skeletonless_type_unit, info);
7669
7670 return 1;
7671 }
7672
7673 /* Scan all TUs of DWO files, verifying we've processed them.
7674 This is needed in case a TU was emitted without its skeleton.
7675 Note: This can't be done until we know what all the DWO files are. */
7676
7677 static void
7678 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7679 {
7680 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7681 if (get_dwp_file (dwarf2_per_objfile) == NULL
7682 && dwarf2_per_objfile->dwo_files != NULL)
7683 {
7684 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7685 process_dwo_file_for_skeletonless_type_units,
7686 dwarf2_per_objfile);
7687 }
7688 }
7689
7690 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7691
7692 static void
7693 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7694 {
7695 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7696 {
7697 dwarf2_psymtab *pst = per_cu->v.psymtab;
7698
7699 if (pst == NULL)
7700 continue;
7701
7702 for (int j = 0; j < pst->number_of_dependencies; ++j)
7703 {
7704 /* Set the 'user' field only if it is not already set. */
7705 if (pst->dependencies[j]->user == NULL)
7706 pst->dependencies[j]->user = pst;
7707 }
7708 }
7709 }
7710
7711 /* Build the partial symbol table by doing a quick pass through the
7712 .debug_info and .debug_abbrev sections. */
7713
7714 static void
7715 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7716 {
7717 struct objfile *objfile = dwarf2_per_objfile->objfile;
7718
7719 if (dwarf_read_debug)
7720 {
7721 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7722 objfile_name (objfile));
7723 }
7724
7725 dwarf2_per_objfile->reading_partial_symbols = 1;
7726
7727 dwarf2_per_objfile->info.read (objfile);
7728
7729 /* Any cached compilation units will be linked by the per-objfile
7730 read_in_chain. Make sure to free them when we're done. */
7731 free_cached_comp_units freer (dwarf2_per_objfile);
7732
7733 build_type_psymtabs (dwarf2_per_objfile);
7734
7735 create_all_comp_units (dwarf2_per_objfile);
7736
7737 /* Create a temporary address map on a temporary obstack. We later
7738 copy this to the final obstack. */
7739 auto_obstack temp_obstack;
7740
7741 scoped_restore save_psymtabs_addrmap
7742 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7743 addrmap_create_mutable (&temp_obstack));
7744
7745 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7746 process_psymtab_comp_unit (per_cu, false, language_minimal);
7747
7748 /* This has to wait until we read the CUs, we need the list of DWOs. */
7749 process_skeletonless_type_units (dwarf2_per_objfile);
7750
7751 /* Now that all TUs have been processed we can fill in the dependencies. */
7752 if (dwarf2_per_objfile->type_unit_groups != NULL)
7753 {
7754 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7755 build_type_psymtab_dependencies, dwarf2_per_objfile);
7756 }
7757
7758 if (dwarf_read_debug)
7759 print_tu_stats (dwarf2_per_objfile);
7760
7761 set_partial_user (dwarf2_per_objfile);
7762
7763 objfile->partial_symtabs->psymtabs_addrmap
7764 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7765 objfile->partial_symtabs->obstack ());
7766 /* At this point we want to keep the address map. */
7767 save_psymtabs_addrmap.release ();
7768
7769 if (dwarf_read_debug)
7770 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7771 objfile_name (objfile));
7772 }
7773
7774 /* Load the partial DIEs for a secondary CU into memory.
7775 This is also used when rereading a primary CU with load_all_dies. */
7776
7777 static void
7778 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7779 {
7780 cutu_reader reader (this_cu, NULL, 1, false);
7781
7782 if (!reader.dummy_p)
7783 {
7784 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7785 language_minimal);
7786
7787 /* Check if comp unit has_children.
7788 If so, read the rest of the partial symbols from this comp unit.
7789 If not, there's no more debug_info for this comp unit. */
7790 if (reader.comp_unit_die->has_children)
7791 load_partial_dies (&reader, reader.info_ptr, 0);
7792
7793 reader.keep ();
7794 }
7795 }
7796
7797 static void
7798 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7799 struct dwarf2_section_info *section,
7800 struct dwarf2_section_info *abbrev_section,
7801 unsigned int is_dwz)
7802 {
7803 const gdb_byte *info_ptr;
7804 struct objfile *objfile = dwarf2_per_objfile->objfile;
7805
7806 if (dwarf_read_debug)
7807 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7808 section->get_name (),
7809 section->get_file_name ());
7810
7811 section->read (objfile);
7812
7813 info_ptr = section->buffer;
7814
7815 while (info_ptr < section->buffer + section->size)
7816 {
7817 struct dwarf2_per_cu_data *this_cu;
7818
7819 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7820
7821 comp_unit_head cu_header;
7822 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7823 abbrev_section, info_ptr,
7824 rcuh_kind::COMPILE);
7825
7826 /* Save the compilation unit for later lookup. */
7827 if (cu_header.unit_type != DW_UT_type)
7828 {
7829 this_cu = XOBNEW (&objfile->objfile_obstack,
7830 struct dwarf2_per_cu_data);
7831 memset (this_cu, 0, sizeof (*this_cu));
7832 }
7833 else
7834 {
7835 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7836 struct signatured_type);
7837 memset (sig_type, 0, sizeof (*sig_type));
7838 sig_type->signature = cu_header.signature;
7839 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7840 this_cu = &sig_type->per_cu;
7841 }
7842 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7843 this_cu->sect_off = sect_off;
7844 this_cu->length = cu_header.length + cu_header.initial_length_size;
7845 this_cu->is_dwz = is_dwz;
7846 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7847 this_cu->section = section;
7848
7849 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7850
7851 info_ptr = info_ptr + this_cu->length;
7852 }
7853 }
7854
7855 /* Create a list of all compilation units in OBJFILE.
7856 This is only done for -readnow and building partial symtabs. */
7857
7858 static void
7859 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7860 {
7861 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7862 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7863 &dwarf2_per_objfile->abbrev, 0);
7864
7865 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7866 if (dwz != NULL)
7867 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7868 1);
7869 }
7870
7871 /* Process all loaded DIEs for compilation unit CU, starting at
7872 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7873 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7874 DW_AT_ranges). See the comments of add_partial_subprogram on how
7875 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7876
7877 static void
7878 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7879 CORE_ADDR *highpc, int set_addrmap,
7880 struct dwarf2_cu *cu)
7881 {
7882 struct partial_die_info *pdi;
7883
7884 /* Now, march along the PDI's, descending into ones which have
7885 interesting children but skipping the children of the other ones,
7886 until we reach the end of the compilation unit. */
7887
7888 pdi = first_die;
7889
7890 while (pdi != NULL)
7891 {
7892 pdi->fixup (cu);
7893
7894 /* Anonymous namespaces or modules have no name but have interesting
7895 children, so we need to look at them. Ditto for anonymous
7896 enums. */
7897
7898 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7899 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7900 || pdi->tag == DW_TAG_imported_unit
7901 || pdi->tag == DW_TAG_inlined_subroutine)
7902 {
7903 switch (pdi->tag)
7904 {
7905 case DW_TAG_subprogram:
7906 case DW_TAG_inlined_subroutine:
7907 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7908 break;
7909 case DW_TAG_constant:
7910 case DW_TAG_variable:
7911 case DW_TAG_typedef:
7912 case DW_TAG_union_type:
7913 if (!pdi->is_declaration)
7914 {
7915 add_partial_symbol (pdi, cu);
7916 }
7917 break;
7918 case DW_TAG_class_type:
7919 case DW_TAG_interface_type:
7920 case DW_TAG_structure_type:
7921 if (!pdi->is_declaration)
7922 {
7923 add_partial_symbol (pdi, cu);
7924 }
7925 if ((cu->language == language_rust
7926 || cu->language == language_cplus) && pdi->has_children)
7927 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7928 set_addrmap, cu);
7929 break;
7930 case DW_TAG_enumeration_type:
7931 if (!pdi->is_declaration)
7932 add_partial_enumeration (pdi, cu);
7933 break;
7934 case DW_TAG_base_type:
7935 case DW_TAG_subrange_type:
7936 /* File scope base type definitions are added to the partial
7937 symbol table. */
7938 add_partial_symbol (pdi, cu);
7939 break;
7940 case DW_TAG_namespace:
7941 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7942 break;
7943 case DW_TAG_module:
7944 if (!pdi->is_declaration)
7945 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7946 break;
7947 case DW_TAG_imported_unit:
7948 {
7949 struct dwarf2_per_cu_data *per_cu;
7950
7951 /* For now we don't handle imported units in type units. */
7952 if (cu->per_cu->is_debug_types)
7953 {
7954 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7955 " supported in type units [in module %s]"),
7956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7957 }
7958
7959 per_cu = dwarf2_find_containing_comp_unit
7960 (pdi->d.sect_off, pdi->is_dwz,
7961 cu->per_cu->dwarf2_per_objfile);
7962
7963 /* Go read the partial unit, if needed. */
7964 if (per_cu->v.psymtab == NULL)
7965 process_psymtab_comp_unit (per_cu, true, cu->language);
7966
7967 cu->per_cu->imported_symtabs_push (per_cu);
7968 }
7969 break;
7970 case DW_TAG_imported_declaration:
7971 add_partial_symbol (pdi, cu);
7972 break;
7973 default:
7974 break;
7975 }
7976 }
7977
7978 /* If the die has a sibling, skip to the sibling. */
7979
7980 pdi = pdi->die_sibling;
7981 }
7982 }
7983
7984 /* Functions used to compute the fully scoped name of a partial DIE.
7985
7986 Normally, this is simple. For C++, the parent DIE's fully scoped
7987 name is concatenated with "::" and the partial DIE's name.
7988 Enumerators are an exception; they use the scope of their parent
7989 enumeration type, i.e. the name of the enumeration type is not
7990 prepended to the enumerator.
7991
7992 There are two complexities. One is DW_AT_specification; in this
7993 case "parent" means the parent of the target of the specification,
7994 instead of the direct parent of the DIE. The other is compilers
7995 which do not emit DW_TAG_namespace; in this case we try to guess
7996 the fully qualified name of structure types from their members'
7997 linkage names. This must be done using the DIE's children rather
7998 than the children of any DW_AT_specification target. We only need
7999 to do this for structures at the top level, i.e. if the target of
8000 any DW_AT_specification (if any; otherwise the DIE itself) does not
8001 have a parent. */
8002
8003 /* Compute the scope prefix associated with PDI's parent, in
8004 compilation unit CU. The result will be allocated on CU's
8005 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8006 field. NULL is returned if no prefix is necessary. */
8007 static const char *
8008 partial_die_parent_scope (struct partial_die_info *pdi,
8009 struct dwarf2_cu *cu)
8010 {
8011 const char *grandparent_scope;
8012 struct partial_die_info *parent, *real_pdi;
8013
8014 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8015 then this means the parent of the specification DIE. */
8016
8017 real_pdi = pdi;
8018 while (real_pdi->has_specification)
8019 {
8020 auto res = find_partial_die (real_pdi->spec_offset,
8021 real_pdi->spec_is_dwz, cu);
8022 real_pdi = res.pdi;
8023 cu = res.cu;
8024 }
8025
8026 parent = real_pdi->die_parent;
8027 if (parent == NULL)
8028 return NULL;
8029
8030 if (parent->scope_set)
8031 return parent->scope;
8032
8033 parent->fixup (cu);
8034
8035 grandparent_scope = partial_die_parent_scope (parent, cu);
8036
8037 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8038 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8039 Work around this problem here. */
8040 if (cu->language == language_cplus
8041 && parent->tag == DW_TAG_namespace
8042 && strcmp (parent->name, "::") == 0
8043 && grandparent_scope == NULL)
8044 {
8045 parent->scope = NULL;
8046 parent->scope_set = 1;
8047 return NULL;
8048 }
8049
8050 /* Nested subroutines in Fortran get a prefix. */
8051 if (pdi->tag == DW_TAG_enumerator)
8052 /* Enumerators should not get the name of the enumeration as a prefix. */
8053 parent->scope = grandparent_scope;
8054 else if (parent->tag == DW_TAG_namespace
8055 || parent->tag == DW_TAG_module
8056 || parent->tag == DW_TAG_structure_type
8057 || parent->tag == DW_TAG_class_type
8058 || parent->tag == DW_TAG_interface_type
8059 || parent->tag == DW_TAG_union_type
8060 || parent->tag == DW_TAG_enumeration_type
8061 || (cu->language == language_fortran
8062 && parent->tag == DW_TAG_subprogram
8063 && pdi->tag == DW_TAG_subprogram))
8064 {
8065 if (grandparent_scope == NULL)
8066 parent->scope = parent->name;
8067 else
8068 parent->scope = typename_concat (&cu->comp_unit_obstack,
8069 grandparent_scope,
8070 parent->name, 0, cu);
8071 }
8072 else
8073 {
8074 /* FIXME drow/2004-04-01: What should we be doing with
8075 function-local names? For partial symbols, we should probably be
8076 ignoring them. */
8077 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8078 dwarf_tag_name (parent->tag),
8079 sect_offset_str (pdi->sect_off));
8080 parent->scope = grandparent_scope;
8081 }
8082
8083 parent->scope_set = 1;
8084 return parent->scope;
8085 }
8086
8087 /* Return the fully scoped name associated with PDI, from compilation unit
8088 CU. The result will be allocated with malloc. */
8089
8090 static gdb::unique_xmalloc_ptr<char>
8091 partial_die_full_name (struct partial_die_info *pdi,
8092 struct dwarf2_cu *cu)
8093 {
8094 const char *parent_scope;
8095
8096 /* If this is a template instantiation, we can not work out the
8097 template arguments from partial DIEs. So, unfortunately, we have
8098 to go through the full DIEs. At least any work we do building
8099 types here will be reused if full symbols are loaded later. */
8100 if (pdi->has_template_arguments)
8101 {
8102 pdi->fixup (cu);
8103
8104 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8105 {
8106 struct die_info *die;
8107 struct attribute attr;
8108 struct dwarf2_cu *ref_cu = cu;
8109
8110 /* DW_FORM_ref_addr is using section offset. */
8111 attr.name = (enum dwarf_attribute) 0;
8112 attr.form = DW_FORM_ref_addr;
8113 attr.u.unsnd = to_underlying (pdi->sect_off);
8114 die = follow_die_ref (NULL, &attr, &ref_cu);
8115
8116 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8117 }
8118 }
8119
8120 parent_scope = partial_die_parent_scope (pdi, cu);
8121 if (parent_scope == NULL)
8122 return NULL;
8123 else
8124 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8125 pdi->name, 0, cu));
8126 }
8127
8128 static void
8129 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8130 {
8131 struct dwarf2_per_objfile *dwarf2_per_objfile
8132 = cu->per_cu->dwarf2_per_objfile;
8133 struct objfile *objfile = dwarf2_per_objfile->objfile;
8134 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8135 CORE_ADDR addr = 0;
8136 const char *actual_name = NULL;
8137 CORE_ADDR baseaddr;
8138
8139 baseaddr = objfile->text_section_offset ();
8140
8141 gdb::unique_xmalloc_ptr<char> built_actual_name
8142 = partial_die_full_name (pdi, cu);
8143 if (built_actual_name != NULL)
8144 actual_name = built_actual_name.get ();
8145
8146 if (actual_name == NULL)
8147 actual_name = pdi->name;
8148
8149 switch (pdi->tag)
8150 {
8151 case DW_TAG_inlined_subroutine:
8152 case DW_TAG_subprogram:
8153 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8154 - baseaddr);
8155 if (pdi->is_external
8156 || cu->language == language_ada
8157 || (cu->language == language_fortran
8158 && pdi->die_parent != NULL
8159 && pdi->die_parent->tag == DW_TAG_subprogram))
8160 {
8161 /* Normally, only "external" DIEs are part of the global scope.
8162 But in Ada and Fortran, we want to be able to access nested
8163 procedures globally. So all Ada and Fortran subprograms are
8164 stored in the global scope. */
8165 add_psymbol_to_list (actual_name,
8166 built_actual_name != NULL,
8167 VAR_DOMAIN, LOC_BLOCK,
8168 SECT_OFF_TEXT (objfile),
8169 psymbol_placement::GLOBAL,
8170 addr,
8171 cu->language, objfile);
8172 }
8173 else
8174 {
8175 add_psymbol_to_list (actual_name,
8176 built_actual_name != NULL,
8177 VAR_DOMAIN, LOC_BLOCK,
8178 SECT_OFF_TEXT (objfile),
8179 psymbol_placement::STATIC,
8180 addr, cu->language, objfile);
8181 }
8182
8183 if (pdi->main_subprogram && actual_name != NULL)
8184 set_objfile_main_name (objfile, actual_name, cu->language);
8185 break;
8186 case DW_TAG_constant:
8187 add_psymbol_to_list (actual_name,
8188 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8189 -1, (pdi->is_external
8190 ? psymbol_placement::GLOBAL
8191 : psymbol_placement::STATIC),
8192 0, cu->language, objfile);
8193 break;
8194 case DW_TAG_variable:
8195 if (pdi->d.locdesc)
8196 addr = decode_locdesc (pdi->d.locdesc, cu);
8197
8198 if (pdi->d.locdesc
8199 && addr == 0
8200 && !dwarf2_per_objfile->has_section_at_zero)
8201 {
8202 /* A global or static variable may also have been stripped
8203 out by the linker if unused, in which case its address
8204 will be nullified; do not add such variables into partial
8205 symbol table then. */
8206 }
8207 else if (pdi->is_external)
8208 {
8209 /* Global Variable.
8210 Don't enter into the minimal symbol tables as there is
8211 a minimal symbol table entry from the ELF symbols already.
8212 Enter into partial symbol table if it has a location
8213 descriptor or a type.
8214 If the location descriptor is missing, new_symbol will create
8215 a LOC_UNRESOLVED symbol, the address of the variable will then
8216 be determined from the minimal symbol table whenever the variable
8217 is referenced.
8218 The address for the partial symbol table entry is not
8219 used by GDB, but it comes in handy for debugging partial symbol
8220 table building. */
8221
8222 if (pdi->d.locdesc || pdi->has_type)
8223 add_psymbol_to_list (actual_name,
8224 built_actual_name != NULL,
8225 VAR_DOMAIN, LOC_STATIC,
8226 SECT_OFF_TEXT (objfile),
8227 psymbol_placement::GLOBAL,
8228 addr, cu->language, objfile);
8229 }
8230 else
8231 {
8232 int has_loc = pdi->d.locdesc != NULL;
8233
8234 /* Static Variable. Skip symbols whose value we cannot know (those
8235 without location descriptors or constant values). */
8236 if (!has_loc && !pdi->has_const_value)
8237 return;
8238
8239 add_psymbol_to_list (actual_name,
8240 built_actual_name != NULL,
8241 VAR_DOMAIN, LOC_STATIC,
8242 SECT_OFF_TEXT (objfile),
8243 psymbol_placement::STATIC,
8244 has_loc ? addr : 0,
8245 cu->language, objfile);
8246 }
8247 break;
8248 case DW_TAG_typedef:
8249 case DW_TAG_base_type:
8250 case DW_TAG_subrange_type:
8251 add_psymbol_to_list (actual_name,
8252 built_actual_name != NULL,
8253 VAR_DOMAIN, LOC_TYPEDEF, -1,
8254 psymbol_placement::STATIC,
8255 0, cu->language, objfile);
8256 break;
8257 case DW_TAG_imported_declaration:
8258 case DW_TAG_namespace:
8259 add_psymbol_to_list (actual_name,
8260 built_actual_name != NULL,
8261 VAR_DOMAIN, LOC_TYPEDEF, -1,
8262 psymbol_placement::GLOBAL,
8263 0, cu->language, objfile);
8264 break;
8265 case DW_TAG_module:
8266 /* With Fortran 77 there might be a "BLOCK DATA" module
8267 available without any name. If so, we skip the module as it
8268 doesn't bring any value. */
8269 if (actual_name != nullptr)
8270 add_psymbol_to_list (actual_name,
8271 built_actual_name != NULL,
8272 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8273 psymbol_placement::GLOBAL,
8274 0, cu->language, objfile);
8275 break;
8276 case DW_TAG_class_type:
8277 case DW_TAG_interface_type:
8278 case DW_TAG_structure_type:
8279 case DW_TAG_union_type:
8280 case DW_TAG_enumeration_type:
8281 /* Skip external references. The DWARF standard says in the section
8282 about "Structure, Union, and Class Type Entries": "An incomplete
8283 structure, union or class type is represented by a structure,
8284 union or class entry that does not have a byte size attribute
8285 and that has a DW_AT_declaration attribute." */
8286 if (!pdi->has_byte_size && pdi->is_declaration)
8287 return;
8288
8289 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8290 static vs. global. */
8291 add_psymbol_to_list (actual_name,
8292 built_actual_name != NULL,
8293 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8294 cu->language == language_cplus
8295 ? psymbol_placement::GLOBAL
8296 : psymbol_placement::STATIC,
8297 0, cu->language, objfile);
8298
8299 break;
8300 case DW_TAG_enumerator:
8301 add_psymbol_to_list (actual_name,
8302 built_actual_name != NULL,
8303 VAR_DOMAIN, LOC_CONST, -1,
8304 cu->language == language_cplus
8305 ? psymbol_placement::GLOBAL
8306 : psymbol_placement::STATIC,
8307 0, cu->language, objfile);
8308 break;
8309 default:
8310 break;
8311 }
8312 }
8313
8314 /* Read a partial die corresponding to a namespace; also, add a symbol
8315 corresponding to that namespace to the symbol table. NAMESPACE is
8316 the name of the enclosing namespace. */
8317
8318 static void
8319 add_partial_namespace (struct partial_die_info *pdi,
8320 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8321 int set_addrmap, struct dwarf2_cu *cu)
8322 {
8323 /* Add a symbol for the namespace. */
8324
8325 add_partial_symbol (pdi, cu);
8326
8327 /* Now scan partial symbols in that namespace. */
8328
8329 if (pdi->has_children)
8330 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8331 }
8332
8333 /* Read a partial die corresponding to a Fortran module. */
8334
8335 static void
8336 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8337 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8338 {
8339 /* Add a symbol for the namespace. */
8340
8341 add_partial_symbol (pdi, cu);
8342
8343 /* Now scan partial symbols in that module. */
8344
8345 if (pdi->has_children)
8346 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8347 }
8348
8349 /* Read a partial die corresponding to a subprogram or an inlined
8350 subprogram and create a partial symbol for that subprogram.
8351 When the CU language allows it, this routine also defines a partial
8352 symbol for each nested subprogram that this subprogram contains.
8353 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8354 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8355
8356 PDI may also be a lexical block, in which case we simply search
8357 recursively for subprograms defined inside that lexical block.
8358 Again, this is only performed when the CU language allows this
8359 type of definitions. */
8360
8361 static void
8362 add_partial_subprogram (struct partial_die_info *pdi,
8363 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8364 int set_addrmap, struct dwarf2_cu *cu)
8365 {
8366 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8367 {
8368 if (pdi->has_pc_info)
8369 {
8370 if (pdi->lowpc < *lowpc)
8371 *lowpc = pdi->lowpc;
8372 if (pdi->highpc > *highpc)
8373 *highpc = pdi->highpc;
8374 if (set_addrmap)
8375 {
8376 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8377 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8378 CORE_ADDR baseaddr;
8379 CORE_ADDR this_highpc;
8380 CORE_ADDR this_lowpc;
8381
8382 baseaddr = objfile->text_section_offset ();
8383 this_lowpc
8384 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8385 pdi->lowpc + baseaddr)
8386 - baseaddr);
8387 this_highpc
8388 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8389 pdi->highpc + baseaddr)
8390 - baseaddr);
8391 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8392 this_lowpc, this_highpc - 1,
8393 cu->per_cu->v.psymtab);
8394 }
8395 }
8396
8397 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8398 {
8399 if (!pdi->is_declaration)
8400 /* Ignore subprogram DIEs that do not have a name, they are
8401 illegal. Do not emit a complaint at this point, we will
8402 do so when we convert this psymtab into a symtab. */
8403 if (pdi->name)
8404 add_partial_symbol (pdi, cu);
8405 }
8406 }
8407
8408 if (! pdi->has_children)
8409 return;
8410
8411 if (cu->language == language_ada || cu->language == language_fortran)
8412 {
8413 pdi = pdi->die_child;
8414 while (pdi != NULL)
8415 {
8416 pdi->fixup (cu);
8417 if (pdi->tag == DW_TAG_subprogram
8418 || pdi->tag == DW_TAG_inlined_subroutine
8419 || pdi->tag == DW_TAG_lexical_block)
8420 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8421 pdi = pdi->die_sibling;
8422 }
8423 }
8424 }
8425
8426 /* Read a partial die corresponding to an enumeration type. */
8427
8428 static void
8429 add_partial_enumeration (struct partial_die_info *enum_pdi,
8430 struct dwarf2_cu *cu)
8431 {
8432 struct partial_die_info *pdi;
8433
8434 if (enum_pdi->name != NULL)
8435 add_partial_symbol (enum_pdi, cu);
8436
8437 pdi = enum_pdi->die_child;
8438 while (pdi)
8439 {
8440 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8441 complaint (_("malformed enumerator DIE ignored"));
8442 else
8443 add_partial_symbol (pdi, cu);
8444 pdi = pdi->die_sibling;
8445 }
8446 }
8447
8448 /* Return the initial uleb128 in the die at INFO_PTR. */
8449
8450 static unsigned int
8451 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8452 {
8453 unsigned int bytes_read;
8454
8455 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8456 }
8457
8458 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8459 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8460
8461 Return the corresponding abbrev, or NULL if the number is zero (indicating
8462 an empty DIE). In either case *BYTES_READ will be set to the length of
8463 the initial number. */
8464
8465 static struct abbrev_info *
8466 peek_die_abbrev (const die_reader_specs &reader,
8467 const gdb_byte *info_ptr, unsigned int *bytes_read)
8468 {
8469 dwarf2_cu *cu = reader.cu;
8470 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8471 unsigned int abbrev_number
8472 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8473
8474 if (abbrev_number == 0)
8475 return NULL;
8476
8477 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8478 if (!abbrev)
8479 {
8480 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8481 " at offset %s [in module %s]"),
8482 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8483 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8484 }
8485
8486 return abbrev;
8487 }
8488
8489 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8490 Returns a pointer to the end of a series of DIEs, terminated by an empty
8491 DIE. Any children of the skipped DIEs will also be skipped. */
8492
8493 static const gdb_byte *
8494 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8495 {
8496 while (1)
8497 {
8498 unsigned int bytes_read;
8499 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8500
8501 if (abbrev == NULL)
8502 return info_ptr + bytes_read;
8503 else
8504 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8505 }
8506 }
8507
8508 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8509 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8510 abbrev corresponding to that skipped uleb128 should be passed in
8511 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8512 children. */
8513
8514 static const gdb_byte *
8515 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8516 struct abbrev_info *abbrev)
8517 {
8518 unsigned int bytes_read;
8519 struct attribute attr;
8520 bfd *abfd = reader->abfd;
8521 struct dwarf2_cu *cu = reader->cu;
8522 const gdb_byte *buffer = reader->buffer;
8523 const gdb_byte *buffer_end = reader->buffer_end;
8524 unsigned int form, i;
8525
8526 for (i = 0; i < abbrev->num_attrs; i++)
8527 {
8528 /* The only abbrev we care about is DW_AT_sibling. */
8529 if (abbrev->attrs[i].name == DW_AT_sibling)
8530 {
8531 bool ignored;
8532 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8533 &ignored);
8534 if (attr.form == DW_FORM_ref_addr)
8535 complaint (_("ignoring absolute DW_AT_sibling"));
8536 else
8537 {
8538 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8539 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8540
8541 if (sibling_ptr < info_ptr)
8542 complaint (_("DW_AT_sibling points backwards"));
8543 else if (sibling_ptr > reader->buffer_end)
8544 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8545 else
8546 return sibling_ptr;
8547 }
8548 }
8549
8550 /* If it isn't DW_AT_sibling, skip this attribute. */
8551 form = abbrev->attrs[i].form;
8552 skip_attribute:
8553 switch (form)
8554 {
8555 case DW_FORM_ref_addr:
8556 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8557 and later it is offset sized. */
8558 if (cu->header.version == 2)
8559 info_ptr += cu->header.addr_size;
8560 else
8561 info_ptr += cu->header.offset_size;
8562 break;
8563 case DW_FORM_GNU_ref_alt:
8564 info_ptr += cu->header.offset_size;
8565 break;
8566 case DW_FORM_addr:
8567 info_ptr += cu->header.addr_size;
8568 break;
8569 case DW_FORM_data1:
8570 case DW_FORM_ref1:
8571 case DW_FORM_flag:
8572 case DW_FORM_strx1:
8573 info_ptr += 1;
8574 break;
8575 case DW_FORM_flag_present:
8576 case DW_FORM_implicit_const:
8577 break;
8578 case DW_FORM_data2:
8579 case DW_FORM_ref2:
8580 case DW_FORM_strx2:
8581 info_ptr += 2;
8582 break;
8583 case DW_FORM_strx3:
8584 info_ptr += 3;
8585 break;
8586 case DW_FORM_data4:
8587 case DW_FORM_ref4:
8588 case DW_FORM_strx4:
8589 info_ptr += 4;
8590 break;
8591 case DW_FORM_data8:
8592 case DW_FORM_ref8:
8593 case DW_FORM_ref_sig8:
8594 info_ptr += 8;
8595 break;
8596 case DW_FORM_data16:
8597 info_ptr += 16;
8598 break;
8599 case DW_FORM_string:
8600 read_direct_string (abfd, info_ptr, &bytes_read);
8601 info_ptr += bytes_read;
8602 break;
8603 case DW_FORM_sec_offset:
8604 case DW_FORM_strp:
8605 case DW_FORM_GNU_strp_alt:
8606 info_ptr += cu->header.offset_size;
8607 break;
8608 case DW_FORM_exprloc:
8609 case DW_FORM_block:
8610 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8611 info_ptr += bytes_read;
8612 break;
8613 case DW_FORM_block1:
8614 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8615 break;
8616 case DW_FORM_block2:
8617 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8618 break;
8619 case DW_FORM_block4:
8620 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8621 break;
8622 case DW_FORM_addrx:
8623 case DW_FORM_strx:
8624 case DW_FORM_sdata:
8625 case DW_FORM_udata:
8626 case DW_FORM_ref_udata:
8627 case DW_FORM_GNU_addr_index:
8628 case DW_FORM_GNU_str_index:
8629 case DW_FORM_rnglistx:
8630 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8631 break;
8632 case DW_FORM_indirect:
8633 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8634 info_ptr += bytes_read;
8635 /* We need to continue parsing from here, so just go back to
8636 the top. */
8637 goto skip_attribute;
8638
8639 default:
8640 error (_("Dwarf Error: Cannot handle %s "
8641 "in DWARF reader [in module %s]"),
8642 dwarf_form_name (form),
8643 bfd_get_filename (abfd));
8644 }
8645 }
8646
8647 if (abbrev->has_children)
8648 return skip_children (reader, info_ptr);
8649 else
8650 return info_ptr;
8651 }
8652
8653 /* Locate ORIG_PDI's sibling.
8654 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8655
8656 static const gdb_byte *
8657 locate_pdi_sibling (const struct die_reader_specs *reader,
8658 struct partial_die_info *orig_pdi,
8659 const gdb_byte *info_ptr)
8660 {
8661 /* Do we know the sibling already? */
8662
8663 if (orig_pdi->sibling)
8664 return orig_pdi->sibling;
8665
8666 /* Are there any children to deal with? */
8667
8668 if (!orig_pdi->has_children)
8669 return info_ptr;
8670
8671 /* Skip the children the long way. */
8672
8673 return skip_children (reader, info_ptr);
8674 }
8675
8676 /* Expand this partial symbol table into a full symbol table. SELF is
8677 not NULL. */
8678
8679 void
8680 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8681 {
8682 struct dwarf2_per_objfile *dwarf2_per_objfile
8683 = get_dwarf2_per_objfile (objfile);
8684
8685 gdb_assert (!readin);
8686 /* If this psymtab is constructed from a debug-only objfile, the
8687 has_section_at_zero flag will not necessarily be correct. We
8688 can get the correct value for this flag by looking at the data
8689 associated with the (presumably stripped) associated objfile. */
8690 if (objfile->separate_debug_objfile_backlink)
8691 {
8692 struct dwarf2_per_objfile *dpo_backlink
8693 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8694
8695 dwarf2_per_objfile->has_section_at_zero
8696 = dpo_backlink->has_section_at_zero;
8697 }
8698
8699 dwarf2_per_objfile->reading_partial_symbols = 0;
8700
8701 expand_psymtab (objfile);
8702
8703 process_cu_includes (dwarf2_per_objfile);
8704 }
8705 \f
8706 /* Reading in full CUs. */
8707
8708 /* Add PER_CU to the queue. */
8709
8710 static void
8711 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8712 enum language pretend_language)
8713 {
8714 per_cu->queued = 1;
8715 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8716 }
8717
8718 /* If PER_CU is not yet queued, add it to the queue.
8719 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8720 dependency.
8721 The result is non-zero if PER_CU was queued, otherwise the result is zero
8722 meaning either PER_CU is already queued or it is already loaded.
8723
8724 N.B. There is an invariant here that if a CU is queued then it is loaded.
8725 The caller is required to load PER_CU if we return non-zero. */
8726
8727 static int
8728 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8729 struct dwarf2_per_cu_data *per_cu,
8730 enum language pretend_language)
8731 {
8732 /* We may arrive here during partial symbol reading, if we need full
8733 DIEs to process an unusual case (e.g. template arguments). Do
8734 not queue PER_CU, just tell our caller to load its DIEs. */
8735 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8736 {
8737 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8738 return 1;
8739 return 0;
8740 }
8741
8742 /* Mark the dependence relation so that we don't flush PER_CU
8743 too early. */
8744 if (dependent_cu != NULL)
8745 dwarf2_add_dependence (dependent_cu, per_cu);
8746
8747 /* If it's already on the queue, we have nothing to do. */
8748 if (per_cu->queued)
8749 return 0;
8750
8751 /* If the compilation unit is already loaded, just mark it as
8752 used. */
8753 if (per_cu->cu != NULL)
8754 {
8755 per_cu->cu->last_used = 0;
8756 return 0;
8757 }
8758
8759 /* Add it to the queue. */
8760 queue_comp_unit (per_cu, pretend_language);
8761
8762 return 1;
8763 }
8764
8765 /* Process the queue. */
8766
8767 static void
8768 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8769 {
8770 if (dwarf_read_debug)
8771 {
8772 fprintf_unfiltered (gdb_stdlog,
8773 "Expanding one or more symtabs of objfile %s ...\n",
8774 objfile_name (dwarf2_per_objfile->objfile));
8775 }
8776
8777 /* The queue starts out with one item, but following a DIE reference
8778 may load a new CU, adding it to the end of the queue. */
8779 while (!dwarf2_per_objfile->queue.empty ())
8780 {
8781 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8782
8783 if ((dwarf2_per_objfile->using_index
8784 ? !item.per_cu->v.quick->compunit_symtab
8785 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8786 /* Skip dummy CUs. */
8787 && item.per_cu->cu != NULL)
8788 {
8789 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8790 unsigned int debug_print_threshold;
8791 char buf[100];
8792
8793 if (per_cu->is_debug_types)
8794 {
8795 struct signatured_type *sig_type =
8796 (struct signatured_type *) per_cu;
8797
8798 sprintf (buf, "TU %s at offset %s",
8799 hex_string (sig_type->signature),
8800 sect_offset_str (per_cu->sect_off));
8801 /* There can be 100s of TUs.
8802 Only print them in verbose mode. */
8803 debug_print_threshold = 2;
8804 }
8805 else
8806 {
8807 sprintf (buf, "CU at offset %s",
8808 sect_offset_str (per_cu->sect_off));
8809 debug_print_threshold = 1;
8810 }
8811
8812 if (dwarf_read_debug >= debug_print_threshold)
8813 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8814
8815 if (per_cu->is_debug_types)
8816 process_full_type_unit (per_cu, item.pretend_language);
8817 else
8818 process_full_comp_unit (per_cu, item.pretend_language);
8819
8820 if (dwarf_read_debug >= debug_print_threshold)
8821 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8822 }
8823
8824 item.per_cu->queued = 0;
8825 dwarf2_per_objfile->queue.pop ();
8826 }
8827
8828 if (dwarf_read_debug)
8829 {
8830 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8831 objfile_name (dwarf2_per_objfile->objfile));
8832 }
8833 }
8834
8835 /* Read in full symbols for PST, and anything it depends on. */
8836
8837 void
8838 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8839 {
8840 struct dwarf2_per_cu_data *per_cu;
8841
8842 if (readin)
8843 return;
8844
8845 read_dependencies (objfile);
8846
8847 per_cu = per_cu_data;
8848
8849 if (per_cu == NULL)
8850 {
8851 /* It's an include file, no symbols to read for it.
8852 Everything is in the parent symtab. */
8853 readin = true;
8854 return;
8855 }
8856
8857 dw2_do_instantiate_symtab (per_cu, false);
8858 }
8859
8860 /* Trivial hash function for die_info: the hash value of a DIE
8861 is its offset in .debug_info for this objfile. */
8862
8863 static hashval_t
8864 die_hash (const void *item)
8865 {
8866 const struct die_info *die = (const struct die_info *) item;
8867
8868 return to_underlying (die->sect_off);
8869 }
8870
8871 /* Trivial comparison function for die_info structures: two DIEs
8872 are equal if they have the same offset. */
8873
8874 static int
8875 die_eq (const void *item_lhs, const void *item_rhs)
8876 {
8877 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8878 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8879
8880 return die_lhs->sect_off == die_rhs->sect_off;
8881 }
8882
8883 /* Load the DIEs associated with PER_CU into memory. */
8884
8885 static void
8886 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8887 bool skip_partial,
8888 enum language pretend_language)
8889 {
8890 gdb_assert (! this_cu->is_debug_types);
8891
8892 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8893 if (reader.dummy_p)
8894 return;
8895
8896 struct dwarf2_cu *cu = reader.cu;
8897 const gdb_byte *info_ptr = reader.info_ptr;
8898
8899 gdb_assert (cu->die_hash == NULL);
8900 cu->die_hash =
8901 htab_create_alloc_ex (cu->header.length / 12,
8902 die_hash,
8903 die_eq,
8904 NULL,
8905 &cu->comp_unit_obstack,
8906 hashtab_obstack_allocate,
8907 dummy_obstack_deallocate);
8908
8909 if (reader.comp_unit_die->has_children)
8910 reader.comp_unit_die->child
8911 = read_die_and_siblings (&reader, reader.info_ptr,
8912 &info_ptr, reader.comp_unit_die);
8913 cu->dies = reader.comp_unit_die;
8914 /* comp_unit_die is not stored in die_hash, no need. */
8915
8916 /* We try not to read any attributes in this function, because not
8917 all CUs needed for references have been loaded yet, and symbol
8918 table processing isn't initialized. But we have to set the CU language,
8919 or we won't be able to build types correctly.
8920 Similarly, if we do not read the producer, we can not apply
8921 producer-specific interpretation. */
8922 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8923
8924 reader.keep ();
8925 }
8926
8927 /* Add a DIE to the delayed physname list. */
8928
8929 static void
8930 add_to_method_list (struct type *type, int fnfield_index, int index,
8931 const char *name, struct die_info *die,
8932 struct dwarf2_cu *cu)
8933 {
8934 struct delayed_method_info mi;
8935 mi.type = type;
8936 mi.fnfield_index = fnfield_index;
8937 mi.index = index;
8938 mi.name = name;
8939 mi.die = die;
8940 cu->method_list.push_back (mi);
8941 }
8942
8943 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8944 "const" / "volatile". If so, decrements LEN by the length of the
8945 modifier and return true. Otherwise return false. */
8946
8947 template<size_t N>
8948 static bool
8949 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8950 {
8951 size_t mod_len = sizeof (mod) - 1;
8952 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8953 {
8954 len -= mod_len;
8955 return true;
8956 }
8957 return false;
8958 }
8959
8960 /* Compute the physnames of any methods on the CU's method list.
8961
8962 The computation of method physnames is delayed in order to avoid the
8963 (bad) condition that one of the method's formal parameters is of an as yet
8964 incomplete type. */
8965
8966 static void
8967 compute_delayed_physnames (struct dwarf2_cu *cu)
8968 {
8969 /* Only C++ delays computing physnames. */
8970 if (cu->method_list.empty ())
8971 return;
8972 gdb_assert (cu->language == language_cplus);
8973
8974 for (const delayed_method_info &mi : cu->method_list)
8975 {
8976 const char *physname;
8977 struct fn_fieldlist *fn_flp
8978 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8979 physname = dwarf2_physname (mi.name, mi.die, cu);
8980 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8981 = physname ? physname : "";
8982
8983 /* Since there's no tag to indicate whether a method is a
8984 const/volatile overload, extract that information out of the
8985 demangled name. */
8986 if (physname != NULL)
8987 {
8988 size_t len = strlen (physname);
8989
8990 while (1)
8991 {
8992 if (physname[len] == ')') /* shortcut */
8993 break;
8994 else if (check_modifier (physname, len, " const"))
8995 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8996 else if (check_modifier (physname, len, " volatile"))
8997 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8998 else
8999 break;
9000 }
9001 }
9002 }
9003
9004 /* The list is no longer needed. */
9005 cu->method_list.clear ();
9006 }
9007
9008 /* Go objects should be embedded in a DW_TAG_module DIE,
9009 and it's not clear if/how imported objects will appear.
9010 To keep Go support simple until that's worked out,
9011 go back through what we've read and create something usable.
9012 We could do this while processing each DIE, and feels kinda cleaner,
9013 but that way is more invasive.
9014 This is to, for example, allow the user to type "p var" or "b main"
9015 without having to specify the package name, and allow lookups
9016 of module.object to work in contexts that use the expression
9017 parser. */
9018
9019 static void
9020 fixup_go_packaging (struct dwarf2_cu *cu)
9021 {
9022 gdb::unique_xmalloc_ptr<char> package_name;
9023 struct pending *list;
9024 int i;
9025
9026 for (list = *cu->get_builder ()->get_global_symbols ();
9027 list != NULL;
9028 list = list->next)
9029 {
9030 for (i = 0; i < list->nsyms; ++i)
9031 {
9032 struct symbol *sym = list->symbol[i];
9033
9034 if (sym->language () == language_go
9035 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9036 {
9037 gdb::unique_xmalloc_ptr<char> this_package_name
9038 (go_symbol_package_name (sym));
9039
9040 if (this_package_name == NULL)
9041 continue;
9042 if (package_name == NULL)
9043 package_name = std::move (this_package_name);
9044 else
9045 {
9046 struct objfile *objfile
9047 = cu->per_cu->dwarf2_per_objfile->objfile;
9048 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9049 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9050 (symbol_symtab (sym) != NULL
9051 ? symtab_to_filename_for_display
9052 (symbol_symtab (sym))
9053 : objfile_name (objfile)),
9054 this_package_name.get (), package_name.get ());
9055 }
9056 }
9057 }
9058 }
9059
9060 if (package_name != NULL)
9061 {
9062 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9063 const char *saved_package_name
9064 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9065 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9066 saved_package_name);
9067 struct symbol *sym;
9068
9069 sym = allocate_symbol (objfile);
9070 sym->set_language (language_go, &objfile->objfile_obstack);
9071 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9072 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9073 e.g., "main" finds the "main" module and not C's main(). */
9074 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9075 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9076 SYMBOL_TYPE (sym) = type;
9077
9078 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9079 }
9080 }
9081
9082 /* Allocate a fully-qualified name consisting of the two parts on the
9083 obstack. */
9084
9085 static const char *
9086 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9087 {
9088 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9089 }
9090
9091 /* A helper that allocates a struct discriminant_info to attach to a
9092 union type. */
9093
9094 static struct discriminant_info *
9095 alloc_discriminant_info (struct type *type, int discriminant_index,
9096 int default_index)
9097 {
9098 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9099 gdb_assert (discriminant_index == -1
9100 || (discriminant_index >= 0
9101 && discriminant_index < TYPE_NFIELDS (type)));
9102 gdb_assert (default_index == -1
9103 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9104
9105 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9106
9107 struct discriminant_info *disc
9108 = ((struct discriminant_info *)
9109 TYPE_ZALLOC (type,
9110 offsetof (struct discriminant_info, discriminants)
9111 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9112 disc->default_index = default_index;
9113 disc->discriminant_index = discriminant_index;
9114
9115 struct dynamic_prop prop;
9116 prop.kind = PROP_UNDEFINED;
9117 prop.data.baton = disc;
9118
9119 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9120
9121 return disc;
9122 }
9123
9124 /* Some versions of rustc emitted enums in an unusual way.
9125
9126 Ordinary enums were emitted as unions. The first element of each
9127 structure in the union was named "RUST$ENUM$DISR". This element
9128 held the discriminant.
9129
9130 These versions of Rust also implemented the "non-zero"
9131 optimization. When the enum had two values, and one is empty and
9132 the other holds a pointer that cannot be zero, the pointer is used
9133 as the discriminant, with a zero value meaning the empty variant.
9134 Here, the union's first member is of the form
9135 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9136 where the fieldnos are the indices of the fields that should be
9137 traversed in order to find the field (which may be several fields deep)
9138 and the variantname is the name of the variant of the case when the
9139 field is zero.
9140
9141 This function recognizes whether TYPE is of one of these forms,
9142 and, if so, smashes it to be a variant type. */
9143
9144 static void
9145 quirk_rust_enum (struct type *type, struct objfile *objfile)
9146 {
9147 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9148
9149 /* We don't need to deal with empty enums. */
9150 if (TYPE_NFIELDS (type) == 0)
9151 return;
9152
9153 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9154 if (TYPE_NFIELDS (type) == 1
9155 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9156 {
9157 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9158
9159 /* Decode the field name to find the offset of the
9160 discriminant. */
9161 ULONGEST bit_offset = 0;
9162 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9163 while (name[0] >= '0' && name[0] <= '9')
9164 {
9165 char *tail;
9166 unsigned long index = strtoul (name, &tail, 10);
9167 name = tail;
9168 if (*name != '$'
9169 || index >= TYPE_NFIELDS (field_type)
9170 || (TYPE_FIELD_LOC_KIND (field_type, index)
9171 != FIELD_LOC_KIND_BITPOS))
9172 {
9173 complaint (_("Could not parse Rust enum encoding string \"%s\""
9174 "[in module %s]"),
9175 TYPE_FIELD_NAME (type, 0),
9176 objfile_name (objfile));
9177 return;
9178 }
9179 ++name;
9180
9181 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9182 field_type = TYPE_FIELD_TYPE (field_type, index);
9183 }
9184
9185 /* Make a union to hold the variants. */
9186 struct type *union_type = alloc_type (objfile);
9187 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9188 TYPE_NFIELDS (union_type) = 3;
9189 TYPE_FIELDS (union_type)
9190 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9191 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9192 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9193
9194 /* Put the discriminant must at index 0. */
9195 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9196 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9197 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9198 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9199
9200 /* The order of fields doesn't really matter, so put the real
9201 field at index 1 and the data-less field at index 2. */
9202 struct discriminant_info *disc
9203 = alloc_discriminant_info (union_type, 0, 1);
9204 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9205 TYPE_FIELD_NAME (union_type, 1)
9206 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9207 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9208 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9209 TYPE_FIELD_NAME (union_type, 1));
9210
9211 const char *dataless_name
9212 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9213 name);
9214 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9215 dataless_name);
9216 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9217 /* NAME points into the original discriminant name, which
9218 already has the correct lifetime. */
9219 TYPE_FIELD_NAME (union_type, 2) = name;
9220 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9221 disc->discriminants[2] = 0;
9222
9223 /* Smash this type to be a structure type. We have to do this
9224 because the type has already been recorded. */
9225 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9226 TYPE_NFIELDS (type) = 1;
9227 TYPE_FIELDS (type)
9228 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9229
9230 /* Install the variant part. */
9231 TYPE_FIELD_TYPE (type, 0) = union_type;
9232 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9233 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9234 }
9235 /* A union with a single anonymous field is probably an old-style
9236 univariant enum. */
9237 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9238 {
9239 /* Smash this type to be a structure type. We have to do this
9240 because the type has already been recorded. */
9241 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9242
9243 /* Make a union to hold the variants. */
9244 struct type *union_type = alloc_type (objfile);
9245 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9246 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9247 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9248 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9249 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9250
9251 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9252 const char *variant_name
9253 = rust_last_path_segment (TYPE_NAME (field_type));
9254 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9255 TYPE_NAME (field_type)
9256 = rust_fully_qualify (&objfile->objfile_obstack,
9257 TYPE_NAME (type), variant_name);
9258
9259 /* Install the union in the outer struct type. */
9260 TYPE_NFIELDS (type) = 1;
9261 TYPE_FIELDS (type)
9262 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9263 TYPE_FIELD_TYPE (type, 0) = union_type;
9264 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9265 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9266
9267 alloc_discriminant_info (union_type, -1, 0);
9268 }
9269 else
9270 {
9271 struct type *disr_type = nullptr;
9272 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9273 {
9274 disr_type = TYPE_FIELD_TYPE (type, i);
9275
9276 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9277 {
9278 /* All fields of a true enum will be structs. */
9279 return;
9280 }
9281 else if (TYPE_NFIELDS (disr_type) == 0)
9282 {
9283 /* Could be data-less variant, so keep going. */
9284 disr_type = nullptr;
9285 }
9286 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9287 "RUST$ENUM$DISR") != 0)
9288 {
9289 /* Not a Rust enum. */
9290 return;
9291 }
9292 else
9293 {
9294 /* Found one. */
9295 break;
9296 }
9297 }
9298
9299 /* If we got here without a discriminant, then it's probably
9300 just a union. */
9301 if (disr_type == nullptr)
9302 return;
9303
9304 /* Smash this type to be a structure type. We have to do this
9305 because the type has already been recorded. */
9306 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9307
9308 /* Make a union to hold the variants. */
9309 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9310 struct type *union_type = alloc_type (objfile);
9311 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9312 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9313 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9314 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9315 TYPE_FIELDS (union_type)
9316 = (struct field *) TYPE_ZALLOC (union_type,
9317 (TYPE_NFIELDS (union_type)
9318 * sizeof (struct field)));
9319
9320 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9321 TYPE_NFIELDS (type) * sizeof (struct field));
9322
9323 /* Install the discriminant at index 0 in the union. */
9324 TYPE_FIELD (union_type, 0) = *disr_field;
9325 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9326 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9327
9328 /* Install the union in the outer struct type. */
9329 TYPE_FIELD_TYPE (type, 0) = union_type;
9330 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9331 TYPE_NFIELDS (type) = 1;
9332
9333 /* Set the size and offset of the union type. */
9334 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9335
9336 /* We need a way to find the correct discriminant given a
9337 variant name. For convenience we build a map here. */
9338 struct type *enum_type = FIELD_TYPE (*disr_field);
9339 std::unordered_map<std::string, ULONGEST> discriminant_map;
9340 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9341 {
9342 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9343 {
9344 const char *name
9345 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9346 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9347 }
9348 }
9349
9350 int n_fields = TYPE_NFIELDS (union_type);
9351 struct discriminant_info *disc
9352 = alloc_discriminant_info (union_type, 0, -1);
9353 /* Skip the discriminant here. */
9354 for (int i = 1; i < n_fields; ++i)
9355 {
9356 /* Find the final word in the name of this variant's type.
9357 That name can be used to look up the correct
9358 discriminant. */
9359 const char *variant_name
9360 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9361 i)));
9362
9363 auto iter = discriminant_map.find (variant_name);
9364 if (iter != discriminant_map.end ())
9365 disc->discriminants[i] = iter->second;
9366
9367 /* Remove the discriminant field, if it exists. */
9368 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9369 if (TYPE_NFIELDS (sub_type) > 0)
9370 {
9371 --TYPE_NFIELDS (sub_type);
9372 ++TYPE_FIELDS (sub_type);
9373 }
9374 TYPE_FIELD_NAME (union_type, i) = variant_name;
9375 TYPE_NAME (sub_type)
9376 = rust_fully_qualify (&objfile->objfile_obstack,
9377 TYPE_NAME (type), variant_name);
9378 }
9379 }
9380 }
9381
9382 /* Rewrite some Rust unions to be structures with variants parts. */
9383
9384 static void
9385 rust_union_quirks (struct dwarf2_cu *cu)
9386 {
9387 gdb_assert (cu->language == language_rust);
9388 for (type *type_ : cu->rust_unions)
9389 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9390 /* We don't need this any more. */
9391 cu->rust_unions.clear ();
9392 }
9393
9394 /* Return the symtab for PER_CU. This works properly regardless of
9395 whether we're using the index or psymtabs. */
9396
9397 static struct compunit_symtab *
9398 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9399 {
9400 return (per_cu->dwarf2_per_objfile->using_index
9401 ? per_cu->v.quick->compunit_symtab
9402 : per_cu->v.psymtab->compunit_symtab);
9403 }
9404
9405 /* A helper function for computing the list of all symbol tables
9406 included by PER_CU. */
9407
9408 static void
9409 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9410 htab_t all_children, htab_t all_type_symtabs,
9411 struct dwarf2_per_cu_data *per_cu,
9412 struct compunit_symtab *immediate_parent)
9413 {
9414 void **slot;
9415 struct compunit_symtab *cust;
9416
9417 slot = htab_find_slot (all_children, per_cu, INSERT);
9418 if (*slot != NULL)
9419 {
9420 /* This inclusion and its children have been processed. */
9421 return;
9422 }
9423
9424 *slot = per_cu;
9425 /* Only add a CU if it has a symbol table. */
9426 cust = get_compunit_symtab (per_cu);
9427 if (cust != NULL)
9428 {
9429 /* If this is a type unit only add its symbol table if we haven't
9430 seen it yet (type unit per_cu's can share symtabs). */
9431 if (per_cu->is_debug_types)
9432 {
9433 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9434 if (*slot == NULL)
9435 {
9436 *slot = cust;
9437 result->push_back (cust);
9438 if (cust->user == NULL)
9439 cust->user = immediate_parent;
9440 }
9441 }
9442 else
9443 {
9444 result->push_back (cust);
9445 if (cust->user == NULL)
9446 cust->user = immediate_parent;
9447 }
9448 }
9449
9450 if (!per_cu->imported_symtabs_empty ())
9451 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9452 {
9453 recursively_compute_inclusions (result, all_children,
9454 all_type_symtabs, ptr, cust);
9455 }
9456 }
9457
9458 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9459 PER_CU. */
9460
9461 static void
9462 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9463 {
9464 gdb_assert (! per_cu->is_debug_types);
9465
9466 if (!per_cu->imported_symtabs_empty ())
9467 {
9468 int len;
9469 std::vector<compunit_symtab *> result_symtabs;
9470 htab_t all_children, all_type_symtabs;
9471 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9472
9473 /* If we don't have a symtab, we can just skip this case. */
9474 if (cust == NULL)
9475 return;
9476
9477 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9478 NULL, xcalloc, xfree);
9479 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9480 NULL, xcalloc, xfree);
9481
9482 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9483 {
9484 recursively_compute_inclusions (&result_symtabs, all_children,
9485 all_type_symtabs, ptr, cust);
9486 }
9487
9488 /* Now we have a transitive closure of all the included symtabs. */
9489 len = result_symtabs.size ();
9490 cust->includes
9491 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9492 struct compunit_symtab *, len + 1);
9493 memcpy (cust->includes, result_symtabs.data (),
9494 len * sizeof (compunit_symtab *));
9495 cust->includes[len] = NULL;
9496
9497 htab_delete (all_children);
9498 htab_delete (all_type_symtabs);
9499 }
9500 }
9501
9502 /* Compute the 'includes' field for the symtabs of all the CUs we just
9503 read. */
9504
9505 static void
9506 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9507 {
9508 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9509 {
9510 if (! iter->is_debug_types)
9511 compute_compunit_symtab_includes (iter);
9512 }
9513
9514 dwarf2_per_objfile->just_read_cus.clear ();
9515 }
9516
9517 /* Generate full symbol information for PER_CU, whose DIEs have
9518 already been loaded into memory. */
9519
9520 static void
9521 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9522 enum language pretend_language)
9523 {
9524 struct dwarf2_cu *cu = per_cu->cu;
9525 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9526 struct objfile *objfile = dwarf2_per_objfile->objfile;
9527 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9528 CORE_ADDR lowpc, highpc;
9529 struct compunit_symtab *cust;
9530 CORE_ADDR baseaddr;
9531 struct block *static_block;
9532 CORE_ADDR addr;
9533
9534 baseaddr = objfile->text_section_offset ();
9535
9536 /* Clear the list here in case something was left over. */
9537 cu->method_list.clear ();
9538
9539 cu->language = pretend_language;
9540 cu->language_defn = language_def (cu->language);
9541
9542 /* Do line number decoding in read_file_scope () */
9543 process_die (cu->dies, cu);
9544
9545 /* For now fudge the Go package. */
9546 if (cu->language == language_go)
9547 fixup_go_packaging (cu);
9548
9549 /* Now that we have processed all the DIEs in the CU, all the types
9550 should be complete, and it should now be safe to compute all of the
9551 physnames. */
9552 compute_delayed_physnames (cu);
9553
9554 if (cu->language == language_rust)
9555 rust_union_quirks (cu);
9556
9557 /* Some compilers don't define a DW_AT_high_pc attribute for the
9558 compilation unit. If the DW_AT_high_pc is missing, synthesize
9559 it, by scanning the DIE's below the compilation unit. */
9560 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9561
9562 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9563 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9564
9565 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9566 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9567 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9568 addrmap to help ensure it has an accurate map of pc values belonging to
9569 this comp unit. */
9570 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9571
9572 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9573 SECT_OFF_TEXT (objfile),
9574 0);
9575
9576 if (cust != NULL)
9577 {
9578 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9579
9580 /* Set symtab language to language from DW_AT_language. If the
9581 compilation is from a C file generated by language preprocessors, do
9582 not set the language if it was already deduced by start_subfile. */
9583 if (!(cu->language == language_c
9584 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9585 COMPUNIT_FILETABS (cust)->language = cu->language;
9586
9587 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9588 produce DW_AT_location with location lists but it can be possibly
9589 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9590 there were bugs in prologue debug info, fixed later in GCC-4.5
9591 by "unwind info for epilogues" patch (which is not directly related).
9592
9593 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9594 needed, it would be wrong due to missing DW_AT_producer there.
9595
9596 Still one can confuse GDB by using non-standard GCC compilation
9597 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9598 */
9599 if (cu->has_loclist && gcc_4_minor >= 5)
9600 cust->locations_valid = 1;
9601
9602 if (gcc_4_minor >= 5)
9603 cust->epilogue_unwind_valid = 1;
9604
9605 cust->call_site_htab = cu->call_site_htab;
9606 }
9607
9608 if (dwarf2_per_objfile->using_index)
9609 per_cu->v.quick->compunit_symtab = cust;
9610 else
9611 {
9612 dwarf2_psymtab *pst = per_cu->v.psymtab;
9613 pst->compunit_symtab = cust;
9614 pst->readin = true;
9615 }
9616
9617 /* Push it for inclusion processing later. */
9618 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9619
9620 /* Not needed any more. */
9621 cu->reset_builder ();
9622 }
9623
9624 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9625 already been loaded into memory. */
9626
9627 static void
9628 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9629 enum language pretend_language)
9630 {
9631 struct dwarf2_cu *cu = per_cu->cu;
9632 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9633 struct objfile *objfile = dwarf2_per_objfile->objfile;
9634 struct compunit_symtab *cust;
9635 struct signatured_type *sig_type;
9636
9637 gdb_assert (per_cu->is_debug_types);
9638 sig_type = (struct signatured_type *) per_cu;
9639
9640 /* Clear the list here in case something was left over. */
9641 cu->method_list.clear ();
9642
9643 cu->language = pretend_language;
9644 cu->language_defn = language_def (cu->language);
9645
9646 /* The symbol tables are set up in read_type_unit_scope. */
9647 process_die (cu->dies, cu);
9648
9649 /* For now fudge the Go package. */
9650 if (cu->language == language_go)
9651 fixup_go_packaging (cu);
9652
9653 /* Now that we have processed all the DIEs in the CU, all the types
9654 should be complete, and it should now be safe to compute all of the
9655 physnames. */
9656 compute_delayed_physnames (cu);
9657
9658 if (cu->language == language_rust)
9659 rust_union_quirks (cu);
9660
9661 /* TUs share symbol tables.
9662 If this is the first TU to use this symtab, complete the construction
9663 of it with end_expandable_symtab. Otherwise, complete the addition of
9664 this TU's symbols to the existing symtab. */
9665 if (sig_type->type_unit_group->compunit_symtab == NULL)
9666 {
9667 buildsym_compunit *builder = cu->get_builder ();
9668 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9669 sig_type->type_unit_group->compunit_symtab = cust;
9670
9671 if (cust != NULL)
9672 {
9673 /* Set symtab language to language from DW_AT_language. If the
9674 compilation is from a C file generated by language preprocessors,
9675 do not set the language if it was already deduced by
9676 start_subfile. */
9677 if (!(cu->language == language_c
9678 && COMPUNIT_FILETABS (cust)->language != language_c))
9679 COMPUNIT_FILETABS (cust)->language = cu->language;
9680 }
9681 }
9682 else
9683 {
9684 cu->get_builder ()->augment_type_symtab ();
9685 cust = sig_type->type_unit_group->compunit_symtab;
9686 }
9687
9688 if (dwarf2_per_objfile->using_index)
9689 per_cu->v.quick->compunit_symtab = cust;
9690 else
9691 {
9692 dwarf2_psymtab *pst = per_cu->v.psymtab;
9693 pst->compunit_symtab = cust;
9694 pst->readin = true;
9695 }
9696
9697 /* Not needed any more. */
9698 cu->reset_builder ();
9699 }
9700
9701 /* Process an imported unit DIE. */
9702
9703 static void
9704 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9705 {
9706 struct attribute *attr;
9707
9708 /* For now we don't handle imported units in type units. */
9709 if (cu->per_cu->is_debug_types)
9710 {
9711 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9712 " supported in type units [in module %s]"),
9713 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9714 }
9715
9716 attr = dwarf2_attr (die, DW_AT_import, cu);
9717 if (attr != NULL)
9718 {
9719 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9720 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9721 dwarf2_per_cu_data *per_cu
9722 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9723 cu->per_cu->dwarf2_per_objfile);
9724
9725 /* If necessary, add it to the queue and load its DIEs. */
9726 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9727 load_full_comp_unit (per_cu, false, cu->language);
9728
9729 cu->per_cu->imported_symtabs_push (per_cu);
9730 }
9731 }
9732
9733 /* RAII object that represents a process_die scope: i.e.,
9734 starts/finishes processing a DIE. */
9735 class process_die_scope
9736 {
9737 public:
9738 process_die_scope (die_info *die, dwarf2_cu *cu)
9739 : m_die (die), m_cu (cu)
9740 {
9741 /* We should only be processing DIEs not already in process. */
9742 gdb_assert (!m_die->in_process);
9743 m_die->in_process = true;
9744 }
9745
9746 ~process_die_scope ()
9747 {
9748 m_die->in_process = false;
9749
9750 /* If we're done processing the DIE for the CU that owns the line
9751 header, we don't need the line header anymore. */
9752 if (m_cu->line_header_die_owner == m_die)
9753 {
9754 delete m_cu->line_header;
9755 m_cu->line_header = NULL;
9756 m_cu->line_header_die_owner = NULL;
9757 }
9758 }
9759
9760 private:
9761 die_info *m_die;
9762 dwarf2_cu *m_cu;
9763 };
9764
9765 /* Process a die and its children. */
9766
9767 static void
9768 process_die (struct die_info *die, struct dwarf2_cu *cu)
9769 {
9770 process_die_scope scope (die, cu);
9771
9772 switch (die->tag)
9773 {
9774 case DW_TAG_padding:
9775 break;
9776 case DW_TAG_compile_unit:
9777 case DW_TAG_partial_unit:
9778 read_file_scope (die, cu);
9779 break;
9780 case DW_TAG_type_unit:
9781 read_type_unit_scope (die, cu);
9782 break;
9783 case DW_TAG_subprogram:
9784 /* Nested subprograms in Fortran get a prefix. */
9785 if (cu->language == language_fortran
9786 && die->parent != NULL
9787 && die->parent->tag == DW_TAG_subprogram)
9788 cu->processing_has_namespace_info = true;
9789 /* Fall through. */
9790 case DW_TAG_inlined_subroutine:
9791 read_func_scope (die, cu);
9792 break;
9793 case DW_TAG_lexical_block:
9794 case DW_TAG_try_block:
9795 case DW_TAG_catch_block:
9796 read_lexical_block_scope (die, cu);
9797 break;
9798 case DW_TAG_call_site:
9799 case DW_TAG_GNU_call_site:
9800 read_call_site_scope (die, cu);
9801 break;
9802 case DW_TAG_class_type:
9803 case DW_TAG_interface_type:
9804 case DW_TAG_structure_type:
9805 case DW_TAG_union_type:
9806 process_structure_scope (die, cu);
9807 break;
9808 case DW_TAG_enumeration_type:
9809 process_enumeration_scope (die, cu);
9810 break;
9811
9812 /* These dies have a type, but processing them does not create
9813 a symbol or recurse to process the children. Therefore we can
9814 read them on-demand through read_type_die. */
9815 case DW_TAG_subroutine_type:
9816 case DW_TAG_set_type:
9817 case DW_TAG_array_type:
9818 case DW_TAG_pointer_type:
9819 case DW_TAG_ptr_to_member_type:
9820 case DW_TAG_reference_type:
9821 case DW_TAG_rvalue_reference_type:
9822 case DW_TAG_string_type:
9823 break;
9824
9825 case DW_TAG_base_type:
9826 case DW_TAG_subrange_type:
9827 case DW_TAG_typedef:
9828 /* Add a typedef symbol for the type definition, if it has a
9829 DW_AT_name. */
9830 new_symbol (die, read_type_die (die, cu), cu);
9831 break;
9832 case DW_TAG_common_block:
9833 read_common_block (die, cu);
9834 break;
9835 case DW_TAG_common_inclusion:
9836 break;
9837 case DW_TAG_namespace:
9838 cu->processing_has_namespace_info = true;
9839 read_namespace (die, cu);
9840 break;
9841 case DW_TAG_module:
9842 cu->processing_has_namespace_info = true;
9843 read_module (die, cu);
9844 break;
9845 case DW_TAG_imported_declaration:
9846 cu->processing_has_namespace_info = true;
9847 if (read_namespace_alias (die, cu))
9848 break;
9849 /* The declaration is not a global namespace alias. */
9850 /* Fall through. */
9851 case DW_TAG_imported_module:
9852 cu->processing_has_namespace_info = true;
9853 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9854 || cu->language != language_fortran))
9855 complaint (_("Tag '%s' has unexpected children"),
9856 dwarf_tag_name (die->tag));
9857 read_import_statement (die, cu);
9858 break;
9859
9860 case DW_TAG_imported_unit:
9861 process_imported_unit_die (die, cu);
9862 break;
9863
9864 case DW_TAG_variable:
9865 read_variable (die, cu);
9866 break;
9867
9868 default:
9869 new_symbol (die, NULL, cu);
9870 break;
9871 }
9872 }
9873 \f
9874 /* DWARF name computation. */
9875
9876 /* A helper function for dwarf2_compute_name which determines whether DIE
9877 needs to have the name of the scope prepended to the name listed in the
9878 die. */
9879
9880 static int
9881 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9882 {
9883 struct attribute *attr;
9884
9885 switch (die->tag)
9886 {
9887 case DW_TAG_namespace:
9888 case DW_TAG_typedef:
9889 case DW_TAG_class_type:
9890 case DW_TAG_interface_type:
9891 case DW_TAG_structure_type:
9892 case DW_TAG_union_type:
9893 case DW_TAG_enumeration_type:
9894 case DW_TAG_enumerator:
9895 case DW_TAG_subprogram:
9896 case DW_TAG_inlined_subroutine:
9897 case DW_TAG_member:
9898 case DW_TAG_imported_declaration:
9899 return 1;
9900
9901 case DW_TAG_variable:
9902 case DW_TAG_constant:
9903 /* We only need to prefix "globally" visible variables. These include
9904 any variable marked with DW_AT_external or any variable that
9905 lives in a namespace. [Variables in anonymous namespaces
9906 require prefixing, but they are not DW_AT_external.] */
9907
9908 if (dwarf2_attr (die, DW_AT_specification, cu))
9909 {
9910 struct dwarf2_cu *spec_cu = cu;
9911
9912 return die_needs_namespace (die_specification (die, &spec_cu),
9913 spec_cu);
9914 }
9915
9916 attr = dwarf2_attr (die, DW_AT_external, cu);
9917 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9918 && die->parent->tag != DW_TAG_module)
9919 return 0;
9920 /* A variable in a lexical block of some kind does not need a
9921 namespace, even though in C++ such variables may be external
9922 and have a mangled name. */
9923 if (die->parent->tag == DW_TAG_lexical_block
9924 || die->parent->tag == DW_TAG_try_block
9925 || die->parent->tag == DW_TAG_catch_block
9926 || die->parent->tag == DW_TAG_subprogram)
9927 return 0;
9928 return 1;
9929
9930 default:
9931 return 0;
9932 }
9933 }
9934
9935 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9936 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9937 defined for the given DIE. */
9938
9939 static struct attribute *
9940 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9941 {
9942 struct attribute *attr;
9943
9944 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9945 if (attr == NULL)
9946 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9947
9948 return attr;
9949 }
9950
9951 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9952 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9953 defined for the given DIE. */
9954
9955 static const char *
9956 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9957 {
9958 const char *linkage_name;
9959
9960 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9961 if (linkage_name == NULL)
9962 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9963
9964 return linkage_name;
9965 }
9966
9967 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9968 compute the physname for the object, which include a method's:
9969 - formal parameters (C++),
9970 - receiver type (Go),
9971
9972 The term "physname" is a bit confusing.
9973 For C++, for example, it is the demangled name.
9974 For Go, for example, it's the mangled name.
9975
9976 For Ada, return the DIE's linkage name rather than the fully qualified
9977 name. PHYSNAME is ignored..
9978
9979 The result is allocated on the objfile_obstack and canonicalized. */
9980
9981 static const char *
9982 dwarf2_compute_name (const char *name,
9983 struct die_info *die, struct dwarf2_cu *cu,
9984 int physname)
9985 {
9986 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9987
9988 if (name == NULL)
9989 name = dwarf2_name (die, cu);
9990
9991 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9992 but otherwise compute it by typename_concat inside GDB.
9993 FIXME: Actually this is not really true, or at least not always true.
9994 It's all very confusing. compute_and_set_names doesn't try to demangle
9995 Fortran names because there is no mangling standard. So new_symbol
9996 will set the demangled name to the result of dwarf2_full_name, and it is
9997 the demangled name that GDB uses if it exists. */
9998 if (cu->language == language_ada
9999 || (cu->language == language_fortran && physname))
10000 {
10001 /* For Ada unit, we prefer the linkage name over the name, as
10002 the former contains the exported name, which the user expects
10003 to be able to reference. Ideally, we want the user to be able
10004 to reference this entity using either natural or linkage name,
10005 but we haven't started looking at this enhancement yet. */
10006 const char *linkage_name = dw2_linkage_name (die, cu);
10007
10008 if (linkage_name != NULL)
10009 return linkage_name;
10010 }
10011
10012 /* These are the only languages we know how to qualify names in. */
10013 if (name != NULL
10014 && (cu->language == language_cplus
10015 || cu->language == language_fortran || cu->language == language_d
10016 || cu->language == language_rust))
10017 {
10018 if (die_needs_namespace (die, cu))
10019 {
10020 const char *prefix;
10021 const char *canonical_name = NULL;
10022
10023 string_file buf;
10024
10025 prefix = determine_prefix (die, cu);
10026 if (*prefix != '\0')
10027 {
10028 gdb::unique_xmalloc_ptr<char> prefixed_name
10029 (typename_concat (NULL, prefix, name, physname, cu));
10030
10031 buf.puts (prefixed_name.get ());
10032 }
10033 else
10034 buf.puts (name);
10035
10036 /* Template parameters may be specified in the DIE's DW_AT_name, or
10037 as children with DW_TAG_template_type_param or
10038 DW_TAG_value_type_param. If the latter, add them to the name
10039 here. If the name already has template parameters, then
10040 skip this step; some versions of GCC emit both, and
10041 it is more efficient to use the pre-computed name.
10042
10043 Something to keep in mind about this process: it is very
10044 unlikely, or in some cases downright impossible, to produce
10045 something that will match the mangled name of a function.
10046 If the definition of the function has the same debug info,
10047 we should be able to match up with it anyway. But fallbacks
10048 using the minimal symbol, for instance to find a method
10049 implemented in a stripped copy of libstdc++, will not work.
10050 If we do not have debug info for the definition, we will have to
10051 match them up some other way.
10052
10053 When we do name matching there is a related problem with function
10054 templates; two instantiated function templates are allowed to
10055 differ only by their return types, which we do not add here. */
10056
10057 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10058 {
10059 struct attribute *attr;
10060 struct die_info *child;
10061 int first = 1;
10062
10063 die->building_fullname = 1;
10064
10065 for (child = die->child; child != NULL; child = child->sibling)
10066 {
10067 struct type *type;
10068 LONGEST value;
10069 const gdb_byte *bytes;
10070 struct dwarf2_locexpr_baton *baton;
10071 struct value *v;
10072
10073 if (child->tag != DW_TAG_template_type_param
10074 && child->tag != DW_TAG_template_value_param)
10075 continue;
10076
10077 if (first)
10078 {
10079 buf.puts ("<");
10080 first = 0;
10081 }
10082 else
10083 buf.puts (", ");
10084
10085 attr = dwarf2_attr (child, DW_AT_type, cu);
10086 if (attr == NULL)
10087 {
10088 complaint (_("template parameter missing DW_AT_type"));
10089 buf.puts ("UNKNOWN_TYPE");
10090 continue;
10091 }
10092 type = die_type (child, cu);
10093
10094 if (child->tag == DW_TAG_template_type_param)
10095 {
10096 c_print_type (type, "", &buf, -1, 0, cu->language,
10097 &type_print_raw_options);
10098 continue;
10099 }
10100
10101 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10102 if (attr == NULL)
10103 {
10104 complaint (_("template parameter missing "
10105 "DW_AT_const_value"));
10106 buf.puts ("UNKNOWN_VALUE");
10107 continue;
10108 }
10109
10110 dwarf2_const_value_attr (attr, type, name,
10111 &cu->comp_unit_obstack, cu,
10112 &value, &bytes, &baton);
10113
10114 if (TYPE_NOSIGN (type))
10115 /* GDB prints characters as NUMBER 'CHAR'. If that's
10116 changed, this can use value_print instead. */
10117 c_printchar (value, type, &buf);
10118 else
10119 {
10120 struct value_print_options opts;
10121
10122 if (baton != NULL)
10123 v = dwarf2_evaluate_loc_desc (type, NULL,
10124 baton->data,
10125 baton->size,
10126 baton->per_cu);
10127 else if (bytes != NULL)
10128 {
10129 v = allocate_value (type);
10130 memcpy (value_contents_writeable (v), bytes,
10131 TYPE_LENGTH (type));
10132 }
10133 else
10134 v = value_from_longest (type, value);
10135
10136 /* Specify decimal so that we do not depend on
10137 the radix. */
10138 get_formatted_print_options (&opts, 'd');
10139 opts.raw = 1;
10140 value_print (v, &buf, &opts);
10141 release_value (v);
10142 }
10143 }
10144
10145 die->building_fullname = 0;
10146
10147 if (!first)
10148 {
10149 /* Close the argument list, with a space if necessary
10150 (nested templates). */
10151 if (!buf.empty () && buf.string ().back () == '>')
10152 buf.puts (" >");
10153 else
10154 buf.puts (">");
10155 }
10156 }
10157
10158 /* For C++ methods, append formal parameter type
10159 information, if PHYSNAME. */
10160
10161 if (physname && die->tag == DW_TAG_subprogram
10162 && cu->language == language_cplus)
10163 {
10164 struct type *type = read_type_die (die, cu);
10165
10166 c_type_print_args (type, &buf, 1, cu->language,
10167 &type_print_raw_options);
10168
10169 if (cu->language == language_cplus)
10170 {
10171 /* Assume that an artificial first parameter is
10172 "this", but do not crash if it is not. RealView
10173 marks unnamed (and thus unused) parameters as
10174 artificial; there is no way to differentiate
10175 the two cases. */
10176 if (TYPE_NFIELDS (type) > 0
10177 && TYPE_FIELD_ARTIFICIAL (type, 0)
10178 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10179 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10180 0))))
10181 buf.puts (" const");
10182 }
10183 }
10184
10185 const std::string &intermediate_name = buf.string ();
10186
10187 if (cu->language == language_cplus)
10188 canonical_name
10189 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10190 &objfile->per_bfd->storage_obstack);
10191
10192 /* If we only computed INTERMEDIATE_NAME, or if
10193 INTERMEDIATE_NAME is already canonical, then we need to
10194 copy it to the appropriate obstack. */
10195 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10196 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10197 intermediate_name);
10198 else
10199 name = canonical_name;
10200 }
10201 }
10202
10203 return name;
10204 }
10205
10206 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10207 If scope qualifiers are appropriate they will be added. The result
10208 will be allocated on the storage_obstack, or NULL if the DIE does
10209 not have a name. NAME may either be from a previous call to
10210 dwarf2_name or NULL.
10211
10212 The output string will be canonicalized (if C++). */
10213
10214 static const char *
10215 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10216 {
10217 return dwarf2_compute_name (name, die, cu, 0);
10218 }
10219
10220 /* Construct a physname for the given DIE in CU. NAME may either be
10221 from a previous call to dwarf2_name or NULL. The result will be
10222 allocated on the objfile_objstack or NULL if the DIE does not have a
10223 name.
10224
10225 The output string will be canonicalized (if C++). */
10226
10227 static const char *
10228 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10229 {
10230 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10231 const char *retval, *mangled = NULL, *canon = NULL;
10232 int need_copy = 1;
10233
10234 /* In this case dwarf2_compute_name is just a shortcut not building anything
10235 on its own. */
10236 if (!die_needs_namespace (die, cu))
10237 return dwarf2_compute_name (name, die, cu, 1);
10238
10239 mangled = dw2_linkage_name (die, cu);
10240
10241 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10242 See https://github.com/rust-lang/rust/issues/32925. */
10243 if (cu->language == language_rust && mangled != NULL
10244 && strchr (mangled, '{') != NULL)
10245 mangled = NULL;
10246
10247 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10248 has computed. */
10249 gdb::unique_xmalloc_ptr<char> demangled;
10250 if (mangled != NULL)
10251 {
10252
10253 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10254 {
10255 /* Do nothing (do not demangle the symbol name). */
10256 }
10257 else if (cu->language == language_go)
10258 {
10259 /* This is a lie, but we already lie to the caller new_symbol.
10260 new_symbol assumes we return the mangled name.
10261 This just undoes that lie until things are cleaned up. */
10262 }
10263 else
10264 {
10265 /* Use DMGL_RET_DROP for C++ template functions to suppress
10266 their return type. It is easier for GDB users to search
10267 for such functions as `name(params)' than `long name(params)'.
10268 In such case the minimal symbol names do not match the full
10269 symbol names but for template functions there is never a need
10270 to look up their definition from their declaration so
10271 the only disadvantage remains the minimal symbol variant
10272 `long name(params)' does not have the proper inferior type. */
10273 demangled.reset (gdb_demangle (mangled,
10274 (DMGL_PARAMS | DMGL_ANSI
10275 | DMGL_RET_DROP)));
10276 }
10277 if (demangled)
10278 canon = demangled.get ();
10279 else
10280 {
10281 canon = mangled;
10282 need_copy = 0;
10283 }
10284 }
10285
10286 if (canon == NULL || check_physname)
10287 {
10288 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10289
10290 if (canon != NULL && strcmp (physname, canon) != 0)
10291 {
10292 /* It may not mean a bug in GDB. The compiler could also
10293 compute DW_AT_linkage_name incorrectly. But in such case
10294 GDB would need to be bug-to-bug compatible. */
10295
10296 complaint (_("Computed physname <%s> does not match demangled <%s> "
10297 "(from linkage <%s>) - DIE at %s [in module %s]"),
10298 physname, canon, mangled, sect_offset_str (die->sect_off),
10299 objfile_name (objfile));
10300
10301 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10302 is available here - over computed PHYSNAME. It is safer
10303 against both buggy GDB and buggy compilers. */
10304
10305 retval = canon;
10306 }
10307 else
10308 {
10309 retval = physname;
10310 need_copy = 0;
10311 }
10312 }
10313 else
10314 retval = canon;
10315
10316 if (need_copy)
10317 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10318
10319 return retval;
10320 }
10321
10322 /* Inspect DIE in CU for a namespace alias. If one exists, record
10323 a new symbol for it.
10324
10325 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10326
10327 static int
10328 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10329 {
10330 struct attribute *attr;
10331
10332 /* If the die does not have a name, this is not a namespace
10333 alias. */
10334 attr = dwarf2_attr (die, DW_AT_name, cu);
10335 if (attr != NULL)
10336 {
10337 int num;
10338 struct die_info *d = die;
10339 struct dwarf2_cu *imported_cu = cu;
10340
10341 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10342 keep inspecting DIEs until we hit the underlying import. */
10343 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10344 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10345 {
10346 attr = dwarf2_attr (d, DW_AT_import, cu);
10347 if (attr == NULL)
10348 break;
10349
10350 d = follow_die_ref (d, attr, &imported_cu);
10351 if (d->tag != DW_TAG_imported_declaration)
10352 break;
10353 }
10354
10355 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10356 {
10357 complaint (_("DIE at %s has too many recursively imported "
10358 "declarations"), sect_offset_str (d->sect_off));
10359 return 0;
10360 }
10361
10362 if (attr != NULL)
10363 {
10364 struct type *type;
10365 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10366
10367 type = get_die_type_at_offset (sect_off, cu->per_cu);
10368 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10369 {
10370 /* This declaration is a global namespace alias. Add
10371 a symbol for it whose type is the aliased namespace. */
10372 new_symbol (die, type, cu);
10373 return 1;
10374 }
10375 }
10376 }
10377
10378 return 0;
10379 }
10380
10381 /* Return the using directives repository (global or local?) to use in the
10382 current context for CU.
10383
10384 For Ada, imported declarations can materialize renamings, which *may* be
10385 global. However it is impossible (for now?) in DWARF to distinguish
10386 "external" imported declarations and "static" ones. As all imported
10387 declarations seem to be static in all other languages, make them all CU-wide
10388 global only in Ada. */
10389
10390 static struct using_direct **
10391 using_directives (struct dwarf2_cu *cu)
10392 {
10393 if (cu->language == language_ada
10394 && cu->get_builder ()->outermost_context_p ())
10395 return cu->get_builder ()->get_global_using_directives ();
10396 else
10397 return cu->get_builder ()->get_local_using_directives ();
10398 }
10399
10400 /* Read the import statement specified by the given die and record it. */
10401
10402 static void
10403 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10404 {
10405 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10406 struct attribute *import_attr;
10407 struct die_info *imported_die, *child_die;
10408 struct dwarf2_cu *imported_cu;
10409 const char *imported_name;
10410 const char *imported_name_prefix;
10411 const char *canonical_name;
10412 const char *import_alias;
10413 const char *imported_declaration = NULL;
10414 const char *import_prefix;
10415 std::vector<const char *> excludes;
10416
10417 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10418 if (import_attr == NULL)
10419 {
10420 complaint (_("Tag '%s' has no DW_AT_import"),
10421 dwarf_tag_name (die->tag));
10422 return;
10423 }
10424
10425 imported_cu = cu;
10426 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10427 imported_name = dwarf2_name (imported_die, imported_cu);
10428 if (imported_name == NULL)
10429 {
10430 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10431
10432 The import in the following code:
10433 namespace A
10434 {
10435 typedef int B;
10436 }
10437
10438 int main ()
10439 {
10440 using A::B;
10441 B b;
10442 return b;
10443 }
10444
10445 ...
10446 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10447 <52> DW_AT_decl_file : 1
10448 <53> DW_AT_decl_line : 6
10449 <54> DW_AT_import : <0x75>
10450 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10451 <59> DW_AT_name : B
10452 <5b> DW_AT_decl_file : 1
10453 <5c> DW_AT_decl_line : 2
10454 <5d> DW_AT_type : <0x6e>
10455 ...
10456 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10457 <76> DW_AT_byte_size : 4
10458 <77> DW_AT_encoding : 5 (signed)
10459
10460 imports the wrong die ( 0x75 instead of 0x58 ).
10461 This case will be ignored until the gcc bug is fixed. */
10462 return;
10463 }
10464
10465 /* Figure out the local name after import. */
10466 import_alias = dwarf2_name (die, cu);
10467
10468 /* Figure out where the statement is being imported to. */
10469 import_prefix = determine_prefix (die, cu);
10470
10471 /* Figure out what the scope of the imported die is and prepend it
10472 to the name of the imported die. */
10473 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10474
10475 if (imported_die->tag != DW_TAG_namespace
10476 && imported_die->tag != DW_TAG_module)
10477 {
10478 imported_declaration = imported_name;
10479 canonical_name = imported_name_prefix;
10480 }
10481 else if (strlen (imported_name_prefix) > 0)
10482 canonical_name = obconcat (&objfile->objfile_obstack,
10483 imported_name_prefix,
10484 (cu->language == language_d ? "." : "::"),
10485 imported_name, (char *) NULL);
10486 else
10487 canonical_name = imported_name;
10488
10489 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10490 for (child_die = die->child; child_die && child_die->tag;
10491 child_die = sibling_die (child_die))
10492 {
10493 /* DWARF-4: A Fortran use statement with a “rename list” may be
10494 represented by an imported module entry with an import attribute
10495 referring to the module and owned entries corresponding to those
10496 entities that are renamed as part of being imported. */
10497
10498 if (child_die->tag != DW_TAG_imported_declaration)
10499 {
10500 complaint (_("child DW_TAG_imported_declaration expected "
10501 "- DIE at %s [in module %s]"),
10502 sect_offset_str (child_die->sect_off),
10503 objfile_name (objfile));
10504 continue;
10505 }
10506
10507 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10508 if (import_attr == NULL)
10509 {
10510 complaint (_("Tag '%s' has no DW_AT_import"),
10511 dwarf_tag_name (child_die->tag));
10512 continue;
10513 }
10514
10515 imported_cu = cu;
10516 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10517 &imported_cu);
10518 imported_name = dwarf2_name (imported_die, imported_cu);
10519 if (imported_name == NULL)
10520 {
10521 complaint (_("child DW_TAG_imported_declaration has unknown "
10522 "imported name - DIE at %s [in module %s]"),
10523 sect_offset_str (child_die->sect_off),
10524 objfile_name (objfile));
10525 continue;
10526 }
10527
10528 excludes.push_back (imported_name);
10529
10530 process_die (child_die, cu);
10531 }
10532
10533 add_using_directive (using_directives (cu),
10534 import_prefix,
10535 canonical_name,
10536 import_alias,
10537 imported_declaration,
10538 excludes,
10539 0,
10540 &objfile->objfile_obstack);
10541 }
10542
10543 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10544 types, but gives them a size of zero. Starting with version 14,
10545 ICC is compatible with GCC. */
10546
10547 static bool
10548 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10549 {
10550 if (!cu->checked_producer)
10551 check_producer (cu);
10552
10553 return cu->producer_is_icc_lt_14;
10554 }
10555
10556 /* ICC generates a DW_AT_type for C void functions. This was observed on
10557 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10558 which says that void functions should not have a DW_AT_type. */
10559
10560 static bool
10561 producer_is_icc (struct dwarf2_cu *cu)
10562 {
10563 if (!cu->checked_producer)
10564 check_producer (cu);
10565
10566 return cu->producer_is_icc;
10567 }
10568
10569 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10570 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10571 this, it was first present in GCC release 4.3.0. */
10572
10573 static bool
10574 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10575 {
10576 if (!cu->checked_producer)
10577 check_producer (cu);
10578
10579 return cu->producer_is_gcc_lt_4_3;
10580 }
10581
10582 static file_and_directory
10583 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10584 {
10585 file_and_directory res;
10586
10587 /* Find the filename. Do not use dwarf2_name here, since the filename
10588 is not a source language identifier. */
10589 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10590 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10591
10592 if (res.comp_dir == NULL
10593 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10594 && IS_ABSOLUTE_PATH (res.name))
10595 {
10596 res.comp_dir_storage = ldirname (res.name);
10597 if (!res.comp_dir_storage.empty ())
10598 res.comp_dir = res.comp_dir_storage.c_str ();
10599 }
10600 if (res.comp_dir != NULL)
10601 {
10602 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10603 directory, get rid of it. */
10604 const char *cp = strchr (res.comp_dir, ':');
10605
10606 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10607 res.comp_dir = cp + 1;
10608 }
10609
10610 if (res.name == NULL)
10611 res.name = "<unknown>";
10612
10613 return res;
10614 }
10615
10616 /* Handle DW_AT_stmt_list for a compilation unit.
10617 DIE is the DW_TAG_compile_unit die for CU.
10618 COMP_DIR is the compilation directory. LOWPC is passed to
10619 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10620
10621 static void
10622 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10623 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10624 {
10625 struct dwarf2_per_objfile *dwarf2_per_objfile
10626 = cu->per_cu->dwarf2_per_objfile;
10627 struct attribute *attr;
10628 struct line_header line_header_local;
10629 hashval_t line_header_local_hash;
10630 void **slot;
10631 int decode_mapping;
10632
10633 gdb_assert (! cu->per_cu->is_debug_types);
10634
10635 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10636 if (attr == NULL)
10637 return;
10638
10639 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10640
10641 /* The line header hash table is only created if needed (it exists to
10642 prevent redundant reading of the line table for partial_units).
10643 If we're given a partial_unit, we'll need it. If we're given a
10644 compile_unit, then use the line header hash table if it's already
10645 created, but don't create one just yet. */
10646
10647 if (dwarf2_per_objfile->line_header_hash == NULL
10648 && die->tag == DW_TAG_partial_unit)
10649 {
10650 dwarf2_per_objfile->line_header_hash
10651 .reset (htab_create_alloc (127, line_header_hash_voidp,
10652 line_header_eq_voidp,
10653 free_line_header_voidp,
10654 xcalloc, xfree));
10655 }
10656
10657 line_header_local.sect_off = line_offset;
10658 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10659 line_header_local_hash = line_header_hash (&line_header_local);
10660 if (dwarf2_per_objfile->line_header_hash != NULL)
10661 {
10662 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10663 &line_header_local,
10664 line_header_local_hash, NO_INSERT);
10665
10666 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10667 is not present in *SLOT (since if there is something in *SLOT then
10668 it will be for a partial_unit). */
10669 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10670 {
10671 gdb_assert (*slot != NULL);
10672 cu->line_header = (struct line_header *) *slot;
10673 return;
10674 }
10675 }
10676
10677 /* dwarf_decode_line_header does not yet provide sufficient information.
10678 We always have to call also dwarf_decode_lines for it. */
10679 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10680 if (lh == NULL)
10681 return;
10682
10683 cu->line_header = lh.release ();
10684 cu->line_header_die_owner = die;
10685
10686 if (dwarf2_per_objfile->line_header_hash == NULL)
10687 slot = NULL;
10688 else
10689 {
10690 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10691 &line_header_local,
10692 line_header_local_hash, INSERT);
10693 gdb_assert (slot != NULL);
10694 }
10695 if (slot != NULL && *slot == NULL)
10696 {
10697 /* This newly decoded line number information unit will be owned
10698 by line_header_hash hash table. */
10699 *slot = cu->line_header;
10700 cu->line_header_die_owner = NULL;
10701 }
10702 else
10703 {
10704 /* We cannot free any current entry in (*slot) as that struct line_header
10705 may be already used by multiple CUs. Create only temporary decoded
10706 line_header for this CU - it may happen at most once for each line
10707 number information unit. And if we're not using line_header_hash
10708 then this is what we want as well. */
10709 gdb_assert (die->tag != DW_TAG_partial_unit);
10710 }
10711 decode_mapping = (die->tag != DW_TAG_partial_unit);
10712 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10713 decode_mapping);
10714
10715 }
10716
10717 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10718
10719 static void
10720 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10721 {
10722 struct dwarf2_per_objfile *dwarf2_per_objfile
10723 = cu->per_cu->dwarf2_per_objfile;
10724 struct objfile *objfile = dwarf2_per_objfile->objfile;
10725 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10726 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10727 CORE_ADDR highpc = ((CORE_ADDR) 0);
10728 struct attribute *attr;
10729 struct die_info *child_die;
10730 CORE_ADDR baseaddr;
10731
10732 prepare_one_comp_unit (cu, die, cu->language);
10733 baseaddr = objfile->text_section_offset ();
10734
10735 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10736
10737 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10738 from finish_block. */
10739 if (lowpc == ((CORE_ADDR) -1))
10740 lowpc = highpc;
10741 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10742
10743 file_and_directory fnd = find_file_and_directory (die, cu);
10744
10745 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10746 standardised yet. As a workaround for the language detection we fall
10747 back to the DW_AT_producer string. */
10748 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10749 cu->language = language_opencl;
10750
10751 /* Similar hack for Go. */
10752 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10753 set_cu_language (DW_LANG_Go, cu);
10754
10755 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10756
10757 /* Decode line number information if present. We do this before
10758 processing child DIEs, so that the line header table is available
10759 for DW_AT_decl_file. */
10760 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10761
10762 /* Process all dies in compilation unit. */
10763 if (die->child != NULL)
10764 {
10765 child_die = die->child;
10766 while (child_die && child_die->tag)
10767 {
10768 process_die (child_die, cu);
10769 child_die = sibling_die (child_die);
10770 }
10771 }
10772
10773 /* Decode macro information, if present. Dwarf 2 macro information
10774 refers to information in the line number info statement program
10775 header, so we can only read it if we've read the header
10776 successfully. */
10777 attr = dwarf2_attr (die, DW_AT_macros, cu);
10778 if (attr == NULL)
10779 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10780 if (attr && cu->line_header)
10781 {
10782 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10783 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10784
10785 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10786 }
10787 else
10788 {
10789 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10790 if (attr && cu->line_header)
10791 {
10792 unsigned int macro_offset = DW_UNSND (attr);
10793
10794 dwarf_decode_macros (cu, macro_offset, 0);
10795 }
10796 }
10797 }
10798
10799 void
10800 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10801 {
10802 struct type_unit_group *tu_group;
10803 int first_time;
10804 struct attribute *attr;
10805 unsigned int i;
10806 struct signatured_type *sig_type;
10807
10808 gdb_assert (per_cu->is_debug_types);
10809 sig_type = (struct signatured_type *) per_cu;
10810
10811 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10812
10813 /* If we're using .gdb_index (includes -readnow) then
10814 per_cu->type_unit_group may not have been set up yet. */
10815 if (sig_type->type_unit_group == NULL)
10816 sig_type->type_unit_group = get_type_unit_group (this, attr);
10817 tu_group = sig_type->type_unit_group;
10818
10819 /* If we've already processed this stmt_list there's no real need to
10820 do it again, we could fake it and just recreate the part we need
10821 (file name,index -> symtab mapping). If data shows this optimization
10822 is useful we can do it then. */
10823 first_time = tu_group->compunit_symtab == NULL;
10824
10825 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10826 debug info. */
10827 line_header_up lh;
10828 if (attr != NULL)
10829 {
10830 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10831 lh = dwarf_decode_line_header (line_offset, this);
10832 }
10833 if (lh == NULL)
10834 {
10835 if (first_time)
10836 start_symtab ("", NULL, 0);
10837 else
10838 {
10839 gdb_assert (tu_group->symtabs == NULL);
10840 gdb_assert (m_builder == nullptr);
10841 struct compunit_symtab *cust = tu_group->compunit_symtab;
10842 m_builder.reset (new struct buildsym_compunit
10843 (COMPUNIT_OBJFILE (cust), "",
10844 COMPUNIT_DIRNAME (cust),
10845 compunit_language (cust),
10846 0, cust));
10847 }
10848 return;
10849 }
10850
10851 line_header = lh.release ();
10852 line_header_die_owner = die;
10853
10854 if (first_time)
10855 {
10856 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10857
10858 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10859 still initializing it, and our caller (a few levels up)
10860 process_full_type_unit still needs to know if this is the first
10861 time. */
10862
10863 tu_group->num_symtabs = line_header->file_names_size ();
10864 tu_group->symtabs = XNEWVEC (struct symtab *,
10865 line_header->file_names_size ());
10866
10867 auto &file_names = line_header->file_names ();
10868 for (i = 0; i < file_names.size (); ++i)
10869 {
10870 file_entry &fe = file_names[i];
10871 dwarf2_start_subfile (this, fe.name,
10872 fe.include_dir (line_header));
10873 buildsym_compunit *b = get_builder ();
10874 if (b->get_current_subfile ()->symtab == NULL)
10875 {
10876 /* NOTE: start_subfile will recognize when it's been
10877 passed a file it has already seen. So we can't
10878 assume there's a simple mapping from
10879 cu->line_header->file_names to subfiles, plus
10880 cu->line_header->file_names may contain dups. */
10881 b->get_current_subfile ()->symtab
10882 = allocate_symtab (cust, b->get_current_subfile ()->name);
10883 }
10884
10885 fe.symtab = b->get_current_subfile ()->symtab;
10886 tu_group->symtabs[i] = fe.symtab;
10887 }
10888 }
10889 else
10890 {
10891 gdb_assert (m_builder == nullptr);
10892 struct compunit_symtab *cust = tu_group->compunit_symtab;
10893 m_builder.reset (new struct buildsym_compunit
10894 (COMPUNIT_OBJFILE (cust), "",
10895 COMPUNIT_DIRNAME (cust),
10896 compunit_language (cust),
10897 0, cust));
10898
10899 auto &file_names = line_header->file_names ();
10900 for (i = 0; i < file_names.size (); ++i)
10901 {
10902 file_entry &fe = file_names[i];
10903 fe.symtab = tu_group->symtabs[i];
10904 }
10905 }
10906
10907 /* The main symtab is allocated last. Type units don't have DW_AT_name
10908 so they don't have a "real" (so to speak) symtab anyway.
10909 There is later code that will assign the main symtab to all symbols
10910 that don't have one. We need to handle the case of a symbol with a
10911 missing symtab (DW_AT_decl_file) anyway. */
10912 }
10913
10914 /* Process DW_TAG_type_unit.
10915 For TUs we want to skip the first top level sibling if it's not the
10916 actual type being defined by this TU. In this case the first top
10917 level sibling is there to provide context only. */
10918
10919 static void
10920 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10921 {
10922 struct die_info *child_die;
10923
10924 prepare_one_comp_unit (cu, die, language_minimal);
10925
10926 /* Initialize (or reinitialize) the machinery for building symtabs.
10927 We do this before processing child DIEs, so that the line header table
10928 is available for DW_AT_decl_file. */
10929 cu->setup_type_unit_groups (die);
10930
10931 if (die->child != NULL)
10932 {
10933 child_die = die->child;
10934 while (child_die && child_die->tag)
10935 {
10936 process_die (child_die, cu);
10937 child_die = sibling_die (child_die);
10938 }
10939 }
10940 }
10941 \f
10942 /* DWO/DWP files.
10943
10944 http://gcc.gnu.org/wiki/DebugFission
10945 http://gcc.gnu.org/wiki/DebugFissionDWP
10946
10947 To simplify handling of both DWO files ("object" files with the DWARF info)
10948 and DWP files (a file with the DWOs packaged up into one file), we treat
10949 DWP files as having a collection of virtual DWO files. */
10950
10951 static hashval_t
10952 hash_dwo_file (const void *item)
10953 {
10954 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10955 hashval_t hash;
10956
10957 hash = htab_hash_string (dwo_file->dwo_name);
10958 if (dwo_file->comp_dir != NULL)
10959 hash += htab_hash_string (dwo_file->comp_dir);
10960 return hash;
10961 }
10962
10963 static int
10964 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10965 {
10966 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10967 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10968
10969 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10970 return 0;
10971 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10972 return lhs->comp_dir == rhs->comp_dir;
10973 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10974 }
10975
10976 /* Allocate a hash table for DWO files. */
10977
10978 static htab_up
10979 allocate_dwo_file_hash_table (struct objfile *objfile)
10980 {
10981 auto delete_dwo_file = [] (void *item)
10982 {
10983 struct dwo_file *dwo_file = (struct dwo_file *) item;
10984
10985 delete dwo_file;
10986 };
10987
10988 return htab_up (htab_create_alloc (41,
10989 hash_dwo_file,
10990 eq_dwo_file,
10991 delete_dwo_file,
10992 xcalloc, xfree));
10993 }
10994
10995 /* Lookup DWO file DWO_NAME. */
10996
10997 static void **
10998 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10999 const char *dwo_name,
11000 const char *comp_dir)
11001 {
11002 struct dwo_file find_entry;
11003 void **slot;
11004
11005 if (dwarf2_per_objfile->dwo_files == NULL)
11006 dwarf2_per_objfile->dwo_files
11007 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11008
11009 find_entry.dwo_name = dwo_name;
11010 find_entry.comp_dir = comp_dir;
11011 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11012 INSERT);
11013
11014 return slot;
11015 }
11016
11017 static hashval_t
11018 hash_dwo_unit (const void *item)
11019 {
11020 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11021
11022 /* This drops the top 32 bits of the id, but is ok for a hash. */
11023 return dwo_unit->signature;
11024 }
11025
11026 static int
11027 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11028 {
11029 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11030 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11031
11032 /* The signature is assumed to be unique within the DWO file.
11033 So while object file CU dwo_id's always have the value zero,
11034 that's OK, assuming each object file DWO file has only one CU,
11035 and that's the rule for now. */
11036 return lhs->signature == rhs->signature;
11037 }
11038
11039 /* Allocate a hash table for DWO CUs,TUs.
11040 There is one of these tables for each of CUs,TUs for each DWO file. */
11041
11042 static htab_up
11043 allocate_dwo_unit_table (struct objfile *objfile)
11044 {
11045 /* Start out with a pretty small number.
11046 Generally DWO files contain only one CU and maybe some TUs. */
11047 return htab_up (htab_create_alloc (3,
11048 hash_dwo_unit,
11049 eq_dwo_unit,
11050 NULL, xcalloc, xfree));
11051 }
11052
11053 /* die_reader_func for create_dwo_cu. */
11054
11055 static void
11056 create_dwo_cu_reader (const struct die_reader_specs *reader,
11057 const gdb_byte *info_ptr,
11058 struct die_info *comp_unit_die,
11059 struct dwo_file *dwo_file,
11060 struct dwo_unit *dwo_unit)
11061 {
11062 struct dwarf2_cu *cu = reader->cu;
11063 sect_offset sect_off = cu->per_cu->sect_off;
11064 struct dwarf2_section_info *section = cu->per_cu->section;
11065
11066 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11067 if (!signature.has_value ())
11068 {
11069 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11070 " its dwo_id [in module %s]"),
11071 sect_offset_str (sect_off), dwo_file->dwo_name);
11072 return;
11073 }
11074
11075 dwo_unit->dwo_file = dwo_file;
11076 dwo_unit->signature = *signature;
11077 dwo_unit->section = section;
11078 dwo_unit->sect_off = sect_off;
11079 dwo_unit->length = cu->per_cu->length;
11080
11081 if (dwarf_read_debug)
11082 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11083 sect_offset_str (sect_off),
11084 hex_string (dwo_unit->signature));
11085 }
11086
11087 /* Create the dwo_units for the CUs in a DWO_FILE.
11088 Note: This function processes DWO files only, not DWP files. */
11089
11090 static void
11091 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11092 dwarf2_cu *cu, struct dwo_file &dwo_file,
11093 dwarf2_section_info &section, htab_up &cus_htab)
11094 {
11095 struct objfile *objfile = dwarf2_per_objfile->objfile;
11096 const gdb_byte *info_ptr, *end_ptr;
11097
11098 section.read (objfile);
11099 info_ptr = section.buffer;
11100
11101 if (info_ptr == NULL)
11102 return;
11103
11104 if (dwarf_read_debug)
11105 {
11106 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11107 section.get_name (),
11108 section.get_file_name ());
11109 }
11110
11111 end_ptr = info_ptr + section.size;
11112 while (info_ptr < end_ptr)
11113 {
11114 struct dwarf2_per_cu_data per_cu;
11115 struct dwo_unit read_unit {};
11116 struct dwo_unit *dwo_unit;
11117 void **slot;
11118 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11119
11120 memset (&per_cu, 0, sizeof (per_cu));
11121 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11122 per_cu.is_debug_types = 0;
11123 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11124 per_cu.section = &section;
11125
11126 cutu_reader reader (&per_cu, cu, &dwo_file);
11127 if (!reader.dummy_p)
11128 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11129 &dwo_file, &read_unit);
11130 info_ptr += per_cu.length;
11131
11132 // If the unit could not be parsed, skip it.
11133 if (read_unit.dwo_file == NULL)
11134 continue;
11135
11136 if (cus_htab == NULL)
11137 cus_htab = allocate_dwo_unit_table (objfile);
11138
11139 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11140 *dwo_unit = read_unit;
11141 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11142 gdb_assert (slot != NULL);
11143 if (*slot != NULL)
11144 {
11145 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11146 sect_offset dup_sect_off = dup_cu->sect_off;
11147
11148 complaint (_("debug cu entry at offset %s is duplicate to"
11149 " the entry at offset %s, signature %s"),
11150 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11151 hex_string (dwo_unit->signature));
11152 }
11153 *slot = (void *)dwo_unit;
11154 }
11155 }
11156
11157 /* DWP file .debug_{cu,tu}_index section format:
11158 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11159
11160 DWP Version 1:
11161
11162 Both index sections have the same format, and serve to map a 64-bit
11163 signature to a set of section numbers. Each section begins with a header,
11164 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11165 indexes, and a pool of 32-bit section numbers. The index sections will be
11166 aligned at 8-byte boundaries in the file.
11167
11168 The index section header consists of:
11169
11170 V, 32 bit version number
11171 -, 32 bits unused
11172 N, 32 bit number of compilation units or type units in the index
11173 M, 32 bit number of slots in the hash table
11174
11175 Numbers are recorded using the byte order of the application binary.
11176
11177 The hash table begins at offset 16 in the section, and consists of an array
11178 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11179 order of the application binary). Unused slots in the hash table are 0.
11180 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11181
11182 The parallel table begins immediately after the hash table
11183 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11184 array of 32-bit indexes (using the byte order of the application binary),
11185 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11186 table contains a 32-bit index into the pool of section numbers. For unused
11187 hash table slots, the corresponding entry in the parallel table will be 0.
11188
11189 The pool of section numbers begins immediately following the hash table
11190 (at offset 16 + 12 * M from the beginning of the section). The pool of
11191 section numbers consists of an array of 32-bit words (using the byte order
11192 of the application binary). Each item in the array is indexed starting
11193 from 0. The hash table entry provides the index of the first section
11194 number in the set. Additional section numbers in the set follow, and the
11195 set is terminated by a 0 entry (section number 0 is not used in ELF).
11196
11197 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11198 section must be the first entry in the set, and the .debug_abbrev.dwo must
11199 be the second entry. Other members of the set may follow in any order.
11200
11201 ---
11202
11203 DWP Version 2:
11204
11205 DWP Version 2 combines all the .debug_info, etc. sections into one,
11206 and the entries in the index tables are now offsets into these sections.
11207 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11208 section.
11209
11210 Index Section Contents:
11211 Header
11212 Hash Table of Signatures dwp_hash_table.hash_table
11213 Parallel Table of Indices dwp_hash_table.unit_table
11214 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11215 Table of Section Sizes dwp_hash_table.v2.sizes
11216
11217 The index section header consists of:
11218
11219 V, 32 bit version number
11220 L, 32 bit number of columns in the table of section offsets
11221 N, 32 bit number of compilation units or type units in the index
11222 M, 32 bit number of slots in the hash table
11223
11224 Numbers are recorded using the byte order of the application binary.
11225
11226 The hash table has the same format as version 1.
11227 The parallel table of indices has the same format as version 1,
11228 except that the entries are origin-1 indices into the table of sections
11229 offsets and the table of section sizes.
11230
11231 The table of offsets begins immediately following the parallel table
11232 (at offset 16 + 12 * M from the beginning of the section). The table is
11233 a two-dimensional array of 32-bit words (using the byte order of the
11234 application binary), with L columns and N+1 rows, in row-major order.
11235 Each row in the array is indexed starting from 0. The first row provides
11236 a key to the remaining rows: each column in this row provides an identifier
11237 for a debug section, and the offsets in the same column of subsequent rows
11238 refer to that section. The section identifiers are:
11239
11240 DW_SECT_INFO 1 .debug_info.dwo
11241 DW_SECT_TYPES 2 .debug_types.dwo
11242 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11243 DW_SECT_LINE 4 .debug_line.dwo
11244 DW_SECT_LOC 5 .debug_loc.dwo
11245 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11246 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11247 DW_SECT_MACRO 8 .debug_macro.dwo
11248
11249 The offsets provided by the CU and TU index sections are the base offsets
11250 for the contributions made by each CU or TU to the corresponding section
11251 in the package file. Each CU and TU header contains an abbrev_offset
11252 field, used to find the abbreviations table for that CU or TU within the
11253 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11254 be interpreted as relative to the base offset given in the index section.
11255 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11256 should be interpreted as relative to the base offset for .debug_line.dwo,
11257 and offsets into other debug sections obtained from DWARF attributes should
11258 also be interpreted as relative to the corresponding base offset.
11259
11260 The table of sizes begins immediately following the table of offsets.
11261 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11262 with L columns and N rows, in row-major order. Each row in the array is
11263 indexed starting from 1 (row 0 is shared by the two tables).
11264
11265 ---
11266
11267 Hash table lookup is handled the same in version 1 and 2:
11268
11269 We assume that N and M will not exceed 2^32 - 1.
11270 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11271
11272 Given a 64-bit compilation unit signature or a type signature S, an entry
11273 in the hash table is located as follows:
11274
11275 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11276 the low-order k bits all set to 1.
11277
11278 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11279
11280 3) If the hash table entry at index H matches the signature, use that
11281 entry. If the hash table entry at index H is unused (all zeroes),
11282 terminate the search: the signature is not present in the table.
11283
11284 4) Let H = (H + H') modulo M. Repeat at Step 3.
11285
11286 Because M > N and H' and M are relatively prime, the search is guaranteed
11287 to stop at an unused slot or find the match. */
11288
11289 /* Create a hash table to map DWO IDs to their CU/TU entry in
11290 .debug_{info,types}.dwo in DWP_FILE.
11291 Returns NULL if there isn't one.
11292 Note: This function processes DWP files only, not DWO files. */
11293
11294 static struct dwp_hash_table *
11295 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11296 struct dwp_file *dwp_file, int is_debug_types)
11297 {
11298 struct objfile *objfile = dwarf2_per_objfile->objfile;
11299 bfd *dbfd = dwp_file->dbfd.get ();
11300 const gdb_byte *index_ptr, *index_end;
11301 struct dwarf2_section_info *index;
11302 uint32_t version, nr_columns, nr_units, nr_slots;
11303 struct dwp_hash_table *htab;
11304
11305 if (is_debug_types)
11306 index = &dwp_file->sections.tu_index;
11307 else
11308 index = &dwp_file->sections.cu_index;
11309
11310 if (index->empty ())
11311 return NULL;
11312 index->read (objfile);
11313
11314 index_ptr = index->buffer;
11315 index_end = index_ptr + index->size;
11316
11317 version = read_4_bytes (dbfd, index_ptr);
11318 index_ptr += 4;
11319 if (version == 2)
11320 nr_columns = read_4_bytes (dbfd, index_ptr);
11321 else
11322 nr_columns = 0;
11323 index_ptr += 4;
11324 nr_units = read_4_bytes (dbfd, index_ptr);
11325 index_ptr += 4;
11326 nr_slots = read_4_bytes (dbfd, index_ptr);
11327 index_ptr += 4;
11328
11329 if (version != 1 && version != 2)
11330 {
11331 error (_("Dwarf Error: unsupported DWP file version (%s)"
11332 " [in module %s]"),
11333 pulongest (version), dwp_file->name);
11334 }
11335 if (nr_slots != (nr_slots & -nr_slots))
11336 {
11337 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11338 " is not power of 2 [in module %s]"),
11339 pulongest (nr_slots), dwp_file->name);
11340 }
11341
11342 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11343 htab->version = version;
11344 htab->nr_columns = nr_columns;
11345 htab->nr_units = nr_units;
11346 htab->nr_slots = nr_slots;
11347 htab->hash_table = index_ptr;
11348 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11349
11350 /* Exit early if the table is empty. */
11351 if (nr_slots == 0 || nr_units == 0
11352 || (version == 2 && nr_columns == 0))
11353 {
11354 /* All must be zero. */
11355 if (nr_slots != 0 || nr_units != 0
11356 || (version == 2 && nr_columns != 0))
11357 {
11358 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11359 " all zero [in modules %s]"),
11360 dwp_file->name);
11361 }
11362 return htab;
11363 }
11364
11365 if (version == 1)
11366 {
11367 htab->section_pool.v1.indices =
11368 htab->unit_table + sizeof (uint32_t) * nr_slots;
11369 /* It's harder to decide whether the section is too small in v1.
11370 V1 is deprecated anyway so we punt. */
11371 }
11372 else
11373 {
11374 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11375 int *ids = htab->section_pool.v2.section_ids;
11376 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11377 /* Reverse map for error checking. */
11378 int ids_seen[DW_SECT_MAX + 1];
11379 int i;
11380
11381 if (nr_columns < 2)
11382 {
11383 error (_("Dwarf Error: bad DWP hash table, too few columns"
11384 " in section table [in module %s]"),
11385 dwp_file->name);
11386 }
11387 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11388 {
11389 error (_("Dwarf Error: bad DWP hash table, too many columns"
11390 " in section table [in module %s]"),
11391 dwp_file->name);
11392 }
11393 memset (ids, 255, sizeof_ids);
11394 memset (ids_seen, 255, sizeof (ids_seen));
11395 for (i = 0; i < nr_columns; ++i)
11396 {
11397 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11398
11399 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11400 {
11401 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11402 " in section table [in module %s]"),
11403 id, dwp_file->name);
11404 }
11405 if (ids_seen[id] != -1)
11406 {
11407 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11408 " id %d in section table [in module %s]"),
11409 id, dwp_file->name);
11410 }
11411 ids_seen[id] = i;
11412 ids[i] = id;
11413 }
11414 /* Must have exactly one info or types section. */
11415 if (((ids_seen[DW_SECT_INFO] != -1)
11416 + (ids_seen[DW_SECT_TYPES] != -1))
11417 != 1)
11418 {
11419 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11420 " DWO info/types section [in module %s]"),
11421 dwp_file->name);
11422 }
11423 /* Must have an abbrev section. */
11424 if (ids_seen[DW_SECT_ABBREV] == -1)
11425 {
11426 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11427 " section [in module %s]"),
11428 dwp_file->name);
11429 }
11430 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11431 htab->section_pool.v2.sizes =
11432 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11433 * nr_units * nr_columns);
11434 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11435 * nr_units * nr_columns))
11436 > index_end)
11437 {
11438 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11439 " [in module %s]"),
11440 dwp_file->name);
11441 }
11442 }
11443
11444 return htab;
11445 }
11446
11447 /* Update SECTIONS with the data from SECTP.
11448
11449 This function is like the other "locate" section routines that are
11450 passed to bfd_map_over_sections, but in this context the sections to
11451 read comes from the DWP V1 hash table, not the full ELF section table.
11452
11453 The result is non-zero for success, or zero if an error was found. */
11454
11455 static int
11456 locate_v1_virtual_dwo_sections (asection *sectp,
11457 struct virtual_v1_dwo_sections *sections)
11458 {
11459 const struct dwop_section_names *names = &dwop_section_names;
11460
11461 if (section_is_p (sectp->name, &names->abbrev_dwo))
11462 {
11463 /* There can be only one. */
11464 if (sections->abbrev.s.section != NULL)
11465 return 0;
11466 sections->abbrev.s.section = sectp;
11467 sections->abbrev.size = bfd_section_size (sectp);
11468 }
11469 else if (section_is_p (sectp->name, &names->info_dwo)
11470 || section_is_p (sectp->name, &names->types_dwo))
11471 {
11472 /* There can be only one. */
11473 if (sections->info_or_types.s.section != NULL)
11474 return 0;
11475 sections->info_or_types.s.section = sectp;
11476 sections->info_or_types.size = bfd_section_size (sectp);
11477 }
11478 else if (section_is_p (sectp->name, &names->line_dwo))
11479 {
11480 /* There can be only one. */
11481 if (sections->line.s.section != NULL)
11482 return 0;
11483 sections->line.s.section = sectp;
11484 sections->line.size = bfd_section_size (sectp);
11485 }
11486 else if (section_is_p (sectp->name, &names->loc_dwo))
11487 {
11488 /* There can be only one. */
11489 if (sections->loc.s.section != NULL)
11490 return 0;
11491 sections->loc.s.section = sectp;
11492 sections->loc.size = bfd_section_size (sectp);
11493 }
11494 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11495 {
11496 /* There can be only one. */
11497 if (sections->macinfo.s.section != NULL)
11498 return 0;
11499 sections->macinfo.s.section = sectp;
11500 sections->macinfo.size = bfd_section_size (sectp);
11501 }
11502 else if (section_is_p (sectp->name, &names->macro_dwo))
11503 {
11504 /* There can be only one. */
11505 if (sections->macro.s.section != NULL)
11506 return 0;
11507 sections->macro.s.section = sectp;
11508 sections->macro.size = bfd_section_size (sectp);
11509 }
11510 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11511 {
11512 /* There can be only one. */
11513 if (sections->str_offsets.s.section != NULL)
11514 return 0;
11515 sections->str_offsets.s.section = sectp;
11516 sections->str_offsets.size = bfd_section_size (sectp);
11517 }
11518 else
11519 {
11520 /* No other kind of section is valid. */
11521 return 0;
11522 }
11523
11524 return 1;
11525 }
11526
11527 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11528 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11529 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11530 This is for DWP version 1 files. */
11531
11532 static struct dwo_unit *
11533 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11534 struct dwp_file *dwp_file,
11535 uint32_t unit_index,
11536 const char *comp_dir,
11537 ULONGEST signature, int is_debug_types)
11538 {
11539 struct objfile *objfile = dwarf2_per_objfile->objfile;
11540 const struct dwp_hash_table *dwp_htab =
11541 is_debug_types ? dwp_file->tus : dwp_file->cus;
11542 bfd *dbfd = dwp_file->dbfd.get ();
11543 const char *kind = is_debug_types ? "TU" : "CU";
11544 struct dwo_file *dwo_file;
11545 struct dwo_unit *dwo_unit;
11546 struct virtual_v1_dwo_sections sections;
11547 void **dwo_file_slot;
11548 int i;
11549
11550 gdb_assert (dwp_file->version == 1);
11551
11552 if (dwarf_read_debug)
11553 {
11554 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11555 kind,
11556 pulongest (unit_index), hex_string (signature),
11557 dwp_file->name);
11558 }
11559
11560 /* Fetch the sections of this DWO unit.
11561 Put a limit on the number of sections we look for so that bad data
11562 doesn't cause us to loop forever. */
11563
11564 #define MAX_NR_V1_DWO_SECTIONS \
11565 (1 /* .debug_info or .debug_types */ \
11566 + 1 /* .debug_abbrev */ \
11567 + 1 /* .debug_line */ \
11568 + 1 /* .debug_loc */ \
11569 + 1 /* .debug_str_offsets */ \
11570 + 1 /* .debug_macro or .debug_macinfo */ \
11571 + 1 /* trailing zero */)
11572
11573 memset (&sections, 0, sizeof (sections));
11574
11575 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11576 {
11577 asection *sectp;
11578 uint32_t section_nr =
11579 read_4_bytes (dbfd,
11580 dwp_htab->section_pool.v1.indices
11581 + (unit_index + i) * sizeof (uint32_t));
11582
11583 if (section_nr == 0)
11584 break;
11585 if (section_nr >= dwp_file->num_sections)
11586 {
11587 error (_("Dwarf Error: bad DWP hash table, section number too large"
11588 " [in module %s]"),
11589 dwp_file->name);
11590 }
11591
11592 sectp = dwp_file->elf_sections[section_nr];
11593 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11594 {
11595 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11596 " [in module %s]"),
11597 dwp_file->name);
11598 }
11599 }
11600
11601 if (i < 2
11602 || sections.info_or_types.empty ()
11603 || sections.abbrev.empty ())
11604 {
11605 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11606 " [in module %s]"),
11607 dwp_file->name);
11608 }
11609 if (i == MAX_NR_V1_DWO_SECTIONS)
11610 {
11611 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11612 " [in module %s]"),
11613 dwp_file->name);
11614 }
11615
11616 /* It's easier for the rest of the code if we fake a struct dwo_file and
11617 have dwo_unit "live" in that. At least for now.
11618
11619 The DWP file can be made up of a random collection of CUs and TUs.
11620 However, for each CU + set of TUs that came from the same original DWO
11621 file, we can combine them back into a virtual DWO file to save space
11622 (fewer struct dwo_file objects to allocate). Remember that for really
11623 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11624
11625 std::string virtual_dwo_name =
11626 string_printf ("virtual-dwo/%d-%d-%d-%d",
11627 sections.abbrev.get_id (),
11628 sections.line.get_id (),
11629 sections.loc.get_id (),
11630 sections.str_offsets.get_id ());
11631 /* Can we use an existing virtual DWO file? */
11632 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11633 virtual_dwo_name.c_str (),
11634 comp_dir);
11635 /* Create one if necessary. */
11636 if (*dwo_file_slot == NULL)
11637 {
11638 if (dwarf_read_debug)
11639 {
11640 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11641 virtual_dwo_name.c_str ());
11642 }
11643 dwo_file = new struct dwo_file;
11644 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11645 virtual_dwo_name);
11646 dwo_file->comp_dir = comp_dir;
11647 dwo_file->sections.abbrev = sections.abbrev;
11648 dwo_file->sections.line = sections.line;
11649 dwo_file->sections.loc = sections.loc;
11650 dwo_file->sections.macinfo = sections.macinfo;
11651 dwo_file->sections.macro = sections.macro;
11652 dwo_file->sections.str_offsets = sections.str_offsets;
11653 /* The "str" section is global to the entire DWP file. */
11654 dwo_file->sections.str = dwp_file->sections.str;
11655 /* The info or types section is assigned below to dwo_unit,
11656 there's no need to record it in dwo_file.
11657 Also, we can't simply record type sections in dwo_file because
11658 we record a pointer into the vector in dwo_unit. As we collect more
11659 types we'll grow the vector and eventually have to reallocate space
11660 for it, invalidating all copies of pointers into the previous
11661 contents. */
11662 *dwo_file_slot = dwo_file;
11663 }
11664 else
11665 {
11666 if (dwarf_read_debug)
11667 {
11668 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11669 virtual_dwo_name.c_str ());
11670 }
11671 dwo_file = (struct dwo_file *) *dwo_file_slot;
11672 }
11673
11674 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11675 dwo_unit->dwo_file = dwo_file;
11676 dwo_unit->signature = signature;
11677 dwo_unit->section =
11678 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11679 *dwo_unit->section = sections.info_or_types;
11680 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11681
11682 return dwo_unit;
11683 }
11684
11685 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11686 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11687 piece within that section used by a TU/CU, return a virtual section
11688 of just that piece. */
11689
11690 static struct dwarf2_section_info
11691 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11692 struct dwarf2_section_info *section,
11693 bfd_size_type offset, bfd_size_type size)
11694 {
11695 struct dwarf2_section_info result;
11696 asection *sectp;
11697
11698 gdb_assert (section != NULL);
11699 gdb_assert (!section->is_virtual);
11700
11701 memset (&result, 0, sizeof (result));
11702 result.s.containing_section = section;
11703 result.is_virtual = true;
11704
11705 if (size == 0)
11706 return result;
11707
11708 sectp = section->get_bfd_section ();
11709
11710 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11711 bounds of the real section. This is a pretty-rare event, so just
11712 flag an error (easier) instead of a warning and trying to cope. */
11713 if (sectp == NULL
11714 || offset + size > bfd_section_size (sectp))
11715 {
11716 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11717 " in section %s [in module %s]"),
11718 sectp ? bfd_section_name (sectp) : "<unknown>",
11719 objfile_name (dwarf2_per_objfile->objfile));
11720 }
11721
11722 result.virtual_offset = offset;
11723 result.size = size;
11724 return result;
11725 }
11726
11727 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11728 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11729 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11730 This is for DWP version 2 files. */
11731
11732 static struct dwo_unit *
11733 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11734 struct dwp_file *dwp_file,
11735 uint32_t unit_index,
11736 const char *comp_dir,
11737 ULONGEST signature, int is_debug_types)
11738 {
11739 struct objfile *objfile = dwarf2_per_objfile->objfile;
11740 const struct dwp_hash_table *dwp_htab =
11741 is_debug_types ? dwp_file->tus : dwp_file->cus;
11742 bfd *dbfd = dwp_file->dbfd.get ();
11743 const char *kind = is_debug_types ? "TU" : "CU";
11744 struct dwo_file *dwo_file;
11745 struct dwo_unit *dwo_unit;
11746 struct virtual_v2_dwo_sections sections;
11747 void **dwo_file_slot;
11748 int i;
11749
11750 gdb_assert (dwp_file->version == 2);
11751
11752 if (dwarf_read_debug)
11753 {
11754 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11755 kind,
11756 pulongest (unit_index), hex_string (signature),
11757 dwp_file->name);
11758 }
11759
11760 /* Fetch the section offsets of this DWO unit. */
11761
11762 memset (&sections, 0, sizeof (sections));
11763
11764 for (i = 0; i < dwp_htab->nr_columns; ++i)
11765 {
11766 uint32_t offset = read_4_bytes (dbfd,
11767 dwp_htab->section_pool.v2.offsets
11768 + (((unit_index - 1) * dwp_htab->nr_columns
11769 + i)
11770 * sizeof (uint32_t)));
11771 uint32_t size = read_4_bytes (dbfd,
11772 dwp_htab->section_pool.v2.sizes
11773 + (((unit_index - 1) * dwp_htab->nr_columns
11774 + i)
11775 * sizeof (uint32_t)));
11776
11777 switch (dwp_htab->section_pool.v2.section_ids[i])
11778 {
11779 case DW_SECT_INFO:
11780 case DW_SECT_TYPES:
11781 sections.info_or_types_offset = offset;
11782 sections.info_or_types_size = size;
11783 break;
11784 case DW_SECT_ABBREV:
11785 sections.abbrev_offset = offset;
11786 sections.abbrev_size = size;
11787 break;
11788 case DW_SECT_LINE:
11789 sections.line_offset = offset;
11790 sections.line_size = size;
11791 break;
11792 case DW_SECT_LOC:
11793 sections.loc_offset = offset;
11794 sections.loc_size = size;
11795 break;
11796 case DW_SECT_STR_OFFSETS:
11797 sections.str_offsets_offset = offset;
11798 sections.str_offsets_size = size;
11799 break;
11800 case DW_SECT_MACINFO:
11801 sections.macinfo_offset = offset;
11802 sections.macinfo_size = size;
11803 break;
11804 case DW_SECT_MACRO:
11805 sections.macro_offset = offset;
11806 sections.macro_size = size;
11807 break;
11808 }
11809 }
11810
11811 /* It's easier for the rest of the code if we fake a struct dwo_file and
11812 have dwo_unit "live" in that. At least for now.
11813
11814 The DWP file can be made up of a random collection of CUs and TUs.
11815 However, for each CU + set of TUs that came from the same original DWO
11816 file, we can combine them back into a virtual DWO file to save space
11817 (fewer struct dwo_file objects to allocate). Remember that for really
11818 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11819
11820 std::string virtual_dwo_name =
11821 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11822 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11823 (long) (sections.line_size ? sections.line_offset : 0),
11824 (long) (sections.loc_size ? sections.loc_offset : 0),
11825 (long) (sections.str_offsets_size
11826 ? sections.str_offsets_offset : 0));
11827 /* Can we use an existing virtual DWO file? */
11828 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11829 virtual_dwo_name.c_str (),
11830 comp_dir);
11831 /* Create one if necessary. */
11832 if (*dwo_file_slot == NULL)
11833 {
11834 if (dwarf_read_debug)
11835 {
11836 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11837 virtual_dwo_name.c_str ());
11838 }
11839 dwo_file = new struct dwo_file;
11840 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11841 virtual_dwo_name);
11842 dwo_file->comp_dir = comp_dir;
11843 dwo_file->sections.abbrev =
11844 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11845 sections.abbrev_offset, sections.abbrev_size);
11846 dwo_file->sections.line =
11847 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11848 sections.line_offset, sections.line_size);
11849 dwo_file->sections.loc =
11850 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11851 sections.loc_offset, sections.loc_size);
11852 dwo_file->sections.macinfo =
11853 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11854 sections.macinfo_offset, sections.macinfo_size);
11855 dwo_file->sections.macro =
11856 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11857 sections.macro_offset, sections.macro_size);
11858 dwo_file->sections.str_offsets =
11859 create_dwp_v2_section (dwarf2_per_objfile,
11860 &dwp_file->sections.str_offsets,
11861 sections.str_offsets_offset,
11862 sections.str_offsets_size);
11863 /* The "str" section is global to the entire DWP file. */
11864 dwo_file->sections.str = dwp_file->sections.str;
11865 /* The info or types section is assigned below to dwo_unit,
11866 there's no need to record it in dwo_file.
11867 Also, we can't simply record type sections in dwo_file because
11868 we record a pointer into the vector in dwo_unit. As we collect more
11869 types we'll grow the vector and eventually have to reallocate space
11870 for it, invalidating all copies of pointers into the previous
11871 contents. */
11872 *dwo_file_slot = dwo_file;
11873 }
11874 else
11875 {
11876 if (dwarf_read_debug)
11877 {
11878 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11879 virtual_dwo_name.c_str ());
11880 }
11881 dwo_file = (struct dwo_file *) *dwo_file_slot;
11882 }
11883
11884 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11885 dwo_unit->dwo_file = dwo_file;
11886 dwo_unit->signature = signature;
11887 dwo_unit->section =
11888 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11889 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11890 is_debug_types
11891 ? &dwp_file->sections.types
11892 : &dwp_file->sections.info,
11893 sections.info_or_types_offset,
11894 sections.info_or_types_size);
11895 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11896
11897 return dwo_unit;
11898 }
11899
11900 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11901 Returns NULL if the signature isn't found. */
11902
11903 static struct dwo_unit *
11904 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11905 struct dwp_file *dwp_file, const char *comp_dir,
11906 ULONGEST signature, int is_debug_types)
11907 {
11908 const struct dwp_hash_table *dwp_htab =
11909 is_debug_types ? dwp_file->tus : dwp_file->cus;
11910 bfd *dbfd = dwp_file->dbfd.get ();
11911 uint32_t mask = dwp_htab->nr_slots - 1;
11912 uint32_t hash = signature & mask;
11913 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11914 unsigned int i;
11915 void **slot;
11916 struct dwo_unit find_dwo_cu;
11917
11918 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11919 find_dwo_cu.signature = signature;
11920 slot = htab_find_slot (is_debug_types
11921 ? dwp_file->loaded_tus.get ()
11922 : dwp_file->loaded_cus.get (),
11923 &find_dwo_cu, INSERT);
11924
11925 if (*slot != NULL)
11926 return (struct dwo_unit *) *slot;
11927
11928 /* Use a for loop so that we don't loop forever on bad debug info. */
11929 for (i = 0; i < dwp_htab->nr_slots; ++i)
11930 {
11931 ULONGEST signature_in_table;
11932
11933 signature_in_table =
11934 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11935 if (signature_in_table == signature)
11936 {
11937 uint32_t unit_index =
11938 read_4_bytes (dbfd,
11939 dwp_htab->unit_table + hash * sizeof (uint32_t));
11940
11941 if (dwp_file->version == 1)
11942 {
11943 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11944 dwp_file, unit_index,
11945 comp_dir, signature,
11946 is_debug_types);
11947 }
11948 else
11949 {
11950 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11951 dwp_file, unit_index,
11952 comp_dir, signature,
11953 is_debug_types);
11954 }
11955 return (struct dwo_unit *) *slot;
11956 }
11957 if (signature_in_table == 0)
11958 return NULL;
11959 hash = (hash + hash2) & mask;
11960 }
11961
11962 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11963 " [in module %s]"),
11964 dwp_file->name);
11965 }
11966
11967 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11968 Open the file specified by FILE_NAME and hand it off to BFD for
11969 preliminary analysis. Return a newly initialized bfd *, which
11970 includes a canonicalized copy of FILE_NAME.
11971 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11972 SEARCH_CWD is true if the current directory is to be searched.
11973 It will be searched before debug-file-directory.
11974 If successful, the file is added to the bfd include table of the
11975 objfile's bfd (see gdb_bfd_record_inclusion).
11976 If unable to find/open the file, return NULL.
11977 NOTE: This function is derived from symfile_bfd_open. */
11978
11979 static gdb_bfd_ref_ptr
11980 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11981 const char *file_name, int is_dwp, int search_cwd)
11982 {
11983 int desc;
11984 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11985 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11986 to debug_file_directory. */
11987 const char *search_path;
11988 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11989
11990 gdb::unique_xmalloc_ptr<char> search_path_holder;
11991 if (search_cwd)
11992 {
11993 if (*debug_file_directory != '\0')
11994 {
11995 search_path_holder.reset (concat (".", dirname_separator_string,
11996 debug_file_directory,
11997 (char *) NULL));
11998 search_path = search_path_holder.get ();
11999 }
12000 else
12001 search_path = ".";
12002 }
12003 else
12004 search_path = debug_file_directory;
12005
12006 openp_flags flags = OPF_RETURN_REALPATH;
12007 if (is_dwp)
12008 flags |= OPF_SEARCH_IN_PATH;
12009
12010 gdb::unique_xmalloc_ptr<char> absolute_name;
12011 desc = openp (search_path, flags, file_name,
12012 O_RDONLY | O_BINARY, &absolute_name);
12013 if (desc < 0)
12014 return NULL;
12015
12016 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12017 gnutarget, desc));
12018 if (sym_bfd == NULL)
12019 return NULL;
12020 bfd_set_cacheable (sym_bfd.get (), 1);
12021
12022 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12023 return NULL;
12024
12025 /* Success. Record the bfd as having been included by the objfile's bfd.
12026 This is important because things like demangled_names_hash lives in the
12027 objfile's per_bfd space and may have references to things like symbol
12028 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12029 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12030
12031 return sym_bfd;
12032 }
12033
12034 /* Try to open DWO file FILE_NAME.
12035 COMP_DIR is the DW_AT_comp_dir attribute.
12036 The result is the bfd handle of the file.
12037 If there is a problem finding or opening the file, return NULL.
12038 Upon success, the canonicalized path of the file is stored in the bfd,
12039 same as symfile_bfd_open. */
12040
12041 static gdb_bfd_ref_ptr
12042 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12043 const char *file_name, const char *comp_dir)
12044 {
12045 if (IS_ABSOLUTE_PATH (file_name))
12046 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12047 0 /*is_dwp*/, 0 /*search_cwd*/);
12048
12049 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12050
12051 if (comp_dir != NULL)
12052 {
12053 gdb::unique_xmalloc_ptr<char> path_to_try
12054 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12055
12056 /* NOTE: If comp_dir is a relative path, this will also try the
12057 search path, which seems useful. */
12058 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12059 path_to_try.get (),
12060 0 /*is_dwp*/,
12061 1 /*search_cwd*/));
12062 if (abfd != NULL)
12063 return abfd;
12064 }
12065
12066 /* That didn't work, try debug-file-directory, which, despite its name,
12067 is a list of paths. */
12068
12069 if (*debug_file_directory == '\0')
12070 return NULL;
12071
12072 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12073 0 /*is_dwp*/, 1 /*search_cwd*/);
12074 }
12075
12076 /* This function is mapped across the sections and remembers the offset and
12077 size of each of the DWO debugging sections we are interested in. */
12078
12079 static void
12080 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12081 {
12082 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12083 const struct dwop_section_names *names = &dwop_section_names;
12084
12085 if (section_is_p (sectp->name, &names->abbrev_dwo))
12086 {
12087 dwo_sections->abbrev.s.section = sectp;
12088 dwo_sections->abbrev.size = bfd_section_size (sectp);
12089 }
12090 else if (section_is_p (sectp->name, &names->info_dwo))
12091 {
12092 dwo_sections->info.s.section = sectp;
12093 dwo_sections->info.size = bfd_section_size (sectp);
12094 }
12095 else if (section_is_p (sectp->name, &names->line_dwo))
12096 {
12097 dwo_sections->line.s.section = sectp;
12098 dwo_sections->line.size = bfd_section_size (sectp);
12099 }
12100 else if (section_is_p (sectp->name, &names->loc_dwo))
12101 {
12102 dwo_sections->loc.s.section = sectp;
12103 dwo_sections->loc.size = bfd_section_size (sectp);
12104 }
12105 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12106 {
12107 dwo_sections->macinfo.s.section = sectp;
12108 dwo_sections->macinfo.size = bfd_section_size (sectp);
12109 }
12110 else if (section_is_p (sectp->name, &names->macro_dwo))
12111 {
12112 dwo_sections->macro.s.section = sectp;
12113 dwo_sections->macro.size = bfd_section_size (sectp);
12114 }
12115 else if (section_is_p (sectp->name, &names->str_dwo))
12116 {
12117 dwo_sections->str.s.section = sectp;
12118 dwo_sections->str.size = bfd_section_size (sectp);
12119 }
12120 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12121 {
12122 dwo_sections->str_offsets.s.section = sectp;
12123 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12124 }
12125 else if (section_is_p (sectp->name, &names->types_dwo))
12126 {
12127 struct dwarf2_section_info type_section;
12128
12129 memset (&type_section, 0, sizeof (type_section));
12130 type_section.s.section = sectp;
12131 type_section.size = bfd_section_size (sectp);
12132 dwo_sections->types.push_back (type_section);
12133 }
12134 }
12135
12136 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12137 by PER_CU. This is for the non-DWP case.
12138 The result is NULL if DWO_NAME can't be found. */
12139
12140 static struct dwo_file *
12141 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12142 const char *dwo_name, const char *comp_dir)
12143 {
12144 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12145
12146 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12147 if (dbfd == NULL)
12148 {
12149 if (dwarf_read_debug)
12150 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12151 return NULL;
12152 }
12153
12154 dwo_file_up dwo_file (new struct dwo_file);
12155 dwo_file->dwo_name = dwo_name;
12156 dwo_file->comp_dir = comp_dir;
12157 dwo_file->dbfd = std::move (dbfd);
12158
12159 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12160 &dwo_file->sections);
12161
12162 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12163 dwo_file->sections.info, dwo_file->cus);
12164
12165 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12166 dwo_file->sections.types, dwo_file->tus);
12167
12168 if (dwarf_read_debug)
12169 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12170
12171 return dwo_file.release ();
12172 }
12173
12174 /* This function is mapped across the sections and remembers the offset and
12175 size of each of the DWP debugging sections common to version 1 and 2 that
12176 we are interested in. */
12177
12178 static void
12179 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12180 void *dwp_file_ptr)
12181 {
12182 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12183 const struct dwop_section_names *names = &dwop_section_names;
12184 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12185
12186 /* Record the ELF section number for later lookup: this is what the
12187 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12188 gdb_assert (elf_section_nr < dwp_file->num_sections);
12189 dwp_file->elf_sections[elf_section_nr] = sectp;
12190
12191 /* Look for specific sections that we need. */
12192 if (section_is_p (sectp->name, &names->str_dwo))
12193 {
12194 dwp_file->sections.str.s.section = sectp;
12195 dwp_file->sections.str.size = bfd_section_size (sectp);
12196 }
12197 else if (section_is_p (sectp->name, &names->cu_index))
12198 {
12199 dwp_file->sections.cu_index.s.section = sectp;
12200 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12201 }
12202 else if (section_is_p (sectp->name, &names->tu_index))
12203 {
12204 dwp_file->sections.tu_index.s.section = sectp;
12205 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12206 }
12207 }
12208
12209 /* This function is mapped across the sections and remembers the offset and
12210 size of each of the DWP version 2 debugging sections that we are interested
12211 in. This is split into a separate function because we don't know if we
12212 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12213
12214 static void
12215 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12216 {
12217 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12218 const struct dwop_section_names *names = &dwop_section_names;
12219 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12220
12221 /* Record the ELF section number for later lookup: this is what the
12222 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12223 gdb_assert (elf_section_nr < dwp_file->num_sections);
12224 dwp_file->elf_sections[elf_section_nr] = sectp;
12225
12226 /* Look for specific sections that we need. */
12227 if (section_is_p (sectp->name, &names->abbrev_dwo))
12228 {
12229 dwp_file->sections.abbrev.s.section = sectp;
12230 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12231 }
12232 else if (section_is_p (sectp->name, &names->info_dwo))
12233 {
12234 dwp_file->sections.info.s.section = sectp;
12235 dwp_file->sections.info.size = bfd_section_size (sectp);
12236 }
12237 else if (section_is_p (sectp->name, &names->line_dwo))
12238 {
12239 dwp_file->sections.line.s.section = sectp;
12240 dwp_file->sections.line.size = bfd_section_size (sectp);
12241 }
12242 else if (section_is_p (sectp->name, &names->loc_dwo))
12243 {
12244 dwp_file->sections.loc.s.section = sectp;
12245 dwp_file->sections.loc.size = bfd_section_size (sectp);
12246 }
12247 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12248 {
12249 dwp_file->sections.macinfo.s.section = sectp;
12250 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12251 }
12252 else if (section_is_p (sectp->name, &names->macro_dwo))
12253 {
12254 dwp_file->sections.macro.s.section = sectp;
12255 dwp_file->sections.macro.size = bfd_section_size (sectp);
12256 }
12257 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12258 {
12259 dwp_file->sections.str_offsets.s.section = sectp;
12260 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12261 }
12262 else if (section_is_p (sectp->name, &names->types_dwo))
12263 {
12264 dwp_file->sections.types.s.section = sectp;
12265 dwp_file->sections.types.size = bfd_section_size (sectp);
12266 }
12267 }
12268
12269 /* Hash function for dwp_file loaded CUs/TUs. */
12270
12271 static hashval_t
12272 hash_dwp_loaded_cutus (const void *item)
12273 {
12274 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12275
12276 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12277 return dwo_unit->signature;
12278 }
12279
12280 /* Equality function for dwp_file loaded CUs/TUs. */
12281
12282 static int
12283 eq_dwp_loaded_cutus (const void *a, const void *b)
12284 {
12285 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12286 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12287
12288 return dua->signature == dub->signature;
12289 }
12290
12291 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12292
12293 static htab_up
12294 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12295 {
12296 return htab_up (htab_create_alloc (3,
12297 hash_dwp_loaded_cutus,
12298 eq_dwp_loaded_cutus,
12299 NULL, xcalloc, xfree));
12300 }
12301
12302 /* Try to open DWP file FILE_NAME.
12303 The result is the bfd handle of the file.
12304 If there is a problem finding or opening the file, return NULL.
12305 Upon success, the canonicalized path of the file is stored in the bfd,
12306 same as symfile_bfd_open. */
12307
12308 static gdb_bfd_ref_ptr
12309 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12310 const char *file_name)
12311 {
12312 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12313 1 /*is_dwp*/,
12314 1 /*search_cwd*/));
12315 if (abfd != NULL)
12316 return abfd;
12317
12318 /* Work around upstream bug 15652.
12319 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12320 [Whether that's a "bug" is debatable, but it is getting in our way.]
12321 We have no real idea where the dwp file is, because gdb's realpath-ing
12322 of the executable's path may have discarded the needed info.
12323 [IWBN if the dwp file name was recorded in the executable, akin to
12324 .gnu_debuglink, but that doesn't exist yet.]
12325 Strip the directory from FILE_NAME and search again. */
12326 if (*debug_file_directory != '\0')
12327 {
12328 /* Don't implicitly search the current directory here.
12329 If the user wants to search "." to handle this case,
12330 it must be added to debug-file-directory. */
12331 return try_open_dwop_file (dwarf2_per_objfile,
12332 lbasename (file_name), 1 /*is_dwp*/,
12333 0 /*search_cwd*/);
12334 }
12335
12336 return NULL;
12337 }
12338
12339 /* Initialize the use of the DWP file for the current objfile.
12340 By convention the name of the DWP file is ${objfile}.dwp.
12341 The result is NULL if it can't be found. */
12342
12343 static std::unique_ptr<struct dwp_file>
12344 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12345 {
12346 struct objfile *objfile = dwarf2_per_objfile->objfile;
12347
12348 /* Try to find first .dwp for the binary file before any symbolic links
12349 resolving. */
12350
12351 /* If the objfile is a debug file, find the name of the real binary
12352 file and get the name of dwp file from there. */
12353 std::string dwp_name;
12354 if (objfile->separate_debug_objfile_backlink != NULL)
12355 {
12356 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12357 const char *backlink_basename = lbasename (backlink->original_name);
12358
12359 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12360 }
12361 else
12362 dwp_name = objfile->original_name;
12363
12364 dwp_name += ".dwp";
12365
12366 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12367 if (dbfd == NULL
12368 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12369 {
12370 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12371 dwp_name = objfile_name (objfile);
12372 dwp_name += ".dwp";
12373 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12374 }
12375
12376 if (dbfd == NULL)
12377 {
12378 if (dwarf_read_debug)
12379 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12380 return std::unique_ptr<dwp_file> ();
12381 }
12382
12383 const char *name = bfd_get_filename (dbfd.get ());
12384 std::unique_ptr<struct dwp_file> dwp_file
12385 (new struct dwp_file (name, std::move (dbfd)));
12386
12387 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12388 dwp_file->elf_sections =
12389 OBSTACK_CALLOC (&objfile->objfile_obstack,
12390 dwp_file->num_sections, asection *);
12391
12392 bfd_map_over_sections (dwp_file->dbfd.get (),
12393 dwarf2_locate_common_dwp_sections,
12394 dwp_file.get ());
12395
12396 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12397 0);
12398
12399 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12400 1);
12401
12402 /* The DWP file version is stored in the hash table. Oh well. */
12403 if (dwp_file->cus && dwp_file->tus
12404 && dwp_file->cus->version != dwp_file->tus->version)
12405 {
12406 /* Technically speaking, we should try to limp along, but this is
12407 pretty bizarre. We use pulongest here because that's the established
12408 portability solution (e.g, we cannot use %u for uint32_t). */
12409 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12410 " TU version %s [in DWP file %s]"),
12411 pulongest (dwp_file->cus->version),
12412 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12413 }
12414
12415 if (dwp_file->cus)
12416 dwp_file->version = dwp_file->cus->version;
12417 else if (dwp_file->tus)
12418 dwp_file->version = dwp_file->tus->version;
12419 else
12420 dwp_file->version = 2;
12421
12422 if (dwp_file->version == 2)
12423 bfd_map_over_sections (dwp_file->dbfd.get (),
12424 dwarf2_locate_v2_dwp_sections,
12425 dwp_file.get ());
12426
12427 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12428 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12429
12430 if (dwarf_read_debug)
12431 {
12432 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12433 fprintf_unfiltered (gdb_stdlog,
12434 " %s CUs, %s TUs\n",
12435 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12436 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12437 }
12438
12439 return dwp_file;
12440 }
12441
12442 /* Wrapper around open_and_init_dwp_file, only open it once. */
12443
12444 static struct dwp_file *
12445 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12446 {
12447 if (! dwarf2_per_objfile->dwp_checked)
12448 {
12449 dwarf2_per_objfile->dwp_file
12450 = open_and_init_dwp_file (dwarf2_per_objfile);
12451 dwarf2_per_objfile->dwp_checked = 1;
12452 }
12453 return dwarf2_per_objfile->dwp_file.get ();
12454 }
12455
12456 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12457 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12458 or in the DWP file for the objfile, referenced by THIS_UNIT.
12459 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12460 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12461
12462 This is called, for example, when wanting to read a variable with a
12463 complex location. Therefore we don't want to do file i/o for every call.
12464 Therefore we don't want to look for a DWO file on every call.
12465 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12466 then we check if we've already seen DWO_NAME, and only THEN do we check
12467 for a DWO file.
12468
12469 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12470 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12471
12472 static struct dwo_unit *
12473 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12474 const char *dwo_name, const char *comp_dir,
12475 ULONGEST signature, int is_debug_types)
12476 {
12477 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12478 struct objfile *objfile = dwarf2_per_objfile->objfile;
12479 const char *kind = is_debug_types ? "TU" : "CU";
12480 void **dwo_file_slot;
12481 struct dwo_file *dwo_file;
12482 struct dwp_file *dwp_file;
12483
12484 /* First see if there's a DWP file.
12485 If we have a DWP file but didn't find the DWO inside it, don't
12486 look for the original DWO file. It makes gdb behave differently
12487 depending on whether one is debugging in the build tree. */
12488
12489 dwp_file = get_dwp_file (dwarf2_per_objfile);
12490 if (dwp_file != NULL)
12491 {
12492 const struct dwp_hash_table *dwp_htab =
12493 is_debug_types ? dwp_file->tus : dwp_file->cus;
12494
12495 if (dwp_htab != NULL)
12496 {
12497 struct dwo_unit *dwo_cutu =
12498 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12499 signature, is_debug_types);
12500
12501 if (dwo_cutu != NULL)
12502 {
12503 if (dwarf_read_debug)
12504 {
12505 fprintf_unfiltered (gdb_stdlog,
12506 "Virtual DWO %s %s found: @%s\n",
12507 kind, hex_string (signature),
12508 host_address_to_string (dwo_cutu));
12509 }
12510 return dwo_cutu;
12511 }
12512 }
12513 }
12514 else
12515 {
12516 /* No DWP file, look for the DWO file. */
12517
12518 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12519 dwo_name, comp_dir);
12520 if (*dwo_file_slot == NULL)
12521 {
12522 /* Read in the file and build a table of the CUs/TUs it contains. */
12523 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12524 }
12525 /* NOTE: This will be NULL if unable to open the file. */
12526 dwo_file = (struct dwo_file *) *dwo_file_slot;
12527
12528 if (dwo_file != NULL)
12529 {
12530 struct dwo_unit *dwo_cutu = NULL;
12531
12532 if (is_debug_types && dwo_file->tus)
12533 {
12534 struct dwo_unit find_dwo_cutu;
12535
12536 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12537 find_dwo_cutu.signature = signature;
12538 dwo_cutu
12539 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12540 &find_dwo_cutu);
12541 }
12542 else if (!is_debug_types && dwo_file->cus)
12543 {
12544 struct dwo_unit find_dwo_cutu;
12545
12546 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12547 find_dwo_cutu.signature = signature;
12548 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12549 &find_dwo_cutu);
12550 }
12551
12552 if (dwo_cutu != NULL)
12553 {
12554 if (dwarf_read_debug)
12555 {
12556 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12557 kind, dwo_name, hex_string (signature),
12558 host_address_to_string (dwo_cutu));
12559 }
12560 return dwo_cutu;
12561 }
12562 }
12563 }
12564
12565 /* We didn't find it. This could mean a dwo_id mismatch, or
12566 someone deleted the DWO/DWP file, or the search path isn't set up
12567 correctly to find the file. */
12568
12569 if (dwarf_read_debug)
12570 {
12571 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12572 kind, dwo_name, hex_string (signature));
12573 }
12574
12575 /* This is a warning and not a complaint because it can be caused by
12576 pilot error (e.g., user accidentally deleting the DWO). */
12577 {
12578 /* Print the name of the DWP file if we looked there, helps the user
12579 better diagnose the problem. */
12580 std::string dwp_text;
12581
12582 if (dwp_file != NULL)
12583 dwp_text = string_printf (" [in DWP file %s]",
12584 lbasename (dwp_file->name));
12585
12586 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12587 " [in module %s]"),
12588 kind, dwo_name, hex_string (signature),
12589 dwp_text.c_str (),
12590 this_unit->is_debug_types ? "TU" : "CU",
12591 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12592 }
12593 return NULL;
12594 }
12595
12596 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12597 See lookup_dwo_cutu_unit for details. */
12598
12599 static struct dwo_unit *
12600 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12601 const char *dwo_name, const char *comp_dir,
12602 ULONGEST signature)
12603 {
12604 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12605 }
12606
12607 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12608 See lookup_dwo_cutu_unit for details. */
12609
12610 static struct dwo_unit *
12611 lookup_dwo_type_unit (struct signatured_type *this_tu,
12612 const char *dwo_name, const char *comp_dir)
12613 {
12614 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12615 }
12616
12617 /* Traversal function for queue_and_load_all_dwo_tus. */
12618
12619 static int
12620 queue_and_load_dwo_tu (void **slot, void *info)
12621 {
12622 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12623 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12624 ULONGEST signature = dwo_unit->signature;
12625 struct signatured_type *sig_type =
12626 lookup_dwo_signatured_type (per_cu->cu, signature);
12627
12628 if (sig_type != NULL)
12629 {
12630 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12631
12632 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12633 a real dependency of PER_CU on SIG_TYPE. That is detected later
12634 while processing PER_CU. */
12635 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12636 load_full_type_unit (sig_cu);
12637 per_cu->imported_symtabs_push (sig_cu);
12638 }
12639
12640 return 1;
12641 }
12642
12643 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12644 The DWO may have the only definition of the type, though it may not be
12645 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12646 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12647
12648 static void
12649 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12650 {
12651 struct dwo_unit *dwo_unit;
12652 struct dwo_file *dwo_file;
12653
12654 gdb_assert (!per_cu->is_debug_types);
12655 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12656 gdb_assert (per_cu->cu != NULL);
12657
12658 dwo_unit = per_cu->cu->dwo_unit;
12659 gdb_assert (dwo_unit != NULL);
12660
12661 dwo_file = dwo_unit->dwo_file;
12662 if (dwo_file->tus != NULL)
12663 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12664 per_cu);
12665 }
12666
12667 /* Read in various DIEs. */
12668
12669 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12670 Inherit only the children of the DW_AT_abstract_origin DIE not being
12671 already referenced by DW_AT_abstract_origin from the children of the
12672 current DIE. */
12673
12674 static void
12675 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12676 {
12677 struct die_info *child_die;
12678 sect_offset *offsetp;
12679 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12680 struct die_info *origin_die;
12681 /* Iterator of the ORIGIN_DIE children. */
12682 struct die_info *origin_child_die;
12683 struct attribute *attr;
12684 struct dwarf2_cu *origin_cu;
12685 struct pending **origin_previous_list_in_scope;
12686
12687 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12688 if (!attr)
12689 return;
12690
12691 /* Note that following die references may follow to a die in a
12692 different cu. */
12693
12694 origin_cu = cu;
12695 origin_die = follow_die_ref (die, attr, &origin_cu);
12696
12697 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12698 symbols in. */
12699 origin_previous_list_in_scope = origin_cu->list_in_scope;
12700 origin_cu->list_in_scope = cu->list_in_scope;
12701
12702 if (die->tag != origin_die->tag
12703 && !(die->tag == DW_TAG_inlined_subroutine
12704 && origin_die->tag == DW_TAG_subprogram))
12705 complaint (_("DIE %s and its abstract origin %s have different tags"),
12706 sect_offset_str (die->sect_off),
12707 sect_offset_str (origin_die->sect_off));
12708
12709 std::vector<sect_offset> offsets;
12710
12711 for (child_die = die->child;
12712 child_die && child_die->tag;
12713 child_die = sibling_die (child_die))
12714 {
12715 struct die_info *child_origin_die;
12716 struct dwarf2_cu *child_origin_cu;
12717
12718 /* We are trying to process concrete instance entries:
12719 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12720 it's not relevant to our analysis here. i.e. detecting DIEs that are
12721 present in the abstract instance but not referenced in the concrete
12722 one. */
12723 if (child_die->tag == DW_TAG_call_site
12724 || child_die->tag == DW_TAG_GNU_call_site)
12725 continue;
12726
12727 /* For each CHILD_DIE, find the corresponding child of
12728 ORIGIN_DIE. If there is more than one layer of
12729 DW_AT_abstract_origin, follow them all; there shouldn't be,
12730 but GCC versions at least through 4.4 generate this (GCC PR
12731 40573). */
12732 child_origin_die = child_die;
12733 child_origin_cu = cu;
12734 while (1)
12735 {
12736 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12737 child_origin_cu);
12738 if (attr == NULL)
12739 break;
12740 child_origin_die = follow_die_ref (child_origin_die, attr,
12741 &child_origin_cu);
12742 }
12743
12744 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12745 counterpart may exist. */
12746 if (child_origin_die != child_die)
12747 {
12748 if (child_die->tag != child_origin_die->tag
12749 && !(child_die->tag == DW_TAG_inlined_subroutine
12750 && child_origin_die->tag == DW_TAG_subprogram))
12751 complaint (_("Child DIE %s and its abstract origin %s have "
12752 "different tags"),
12753 sect_offset_str (child_die->sect_off),
12754 sect_offset_str (child_origin_die->sect_off));
12755 if (child_origin_die->parent != origin_die)
12756 complaint (_("Child DIE %s and its abstract origin %s have "
12757 "different parents"),
12758 sect_offset_str (child_die->sect_off),
12759 sect_offset_str (child_origin_die->sect_off));
12760 else
12761 offsets.push_back (child_origin_die->sect_off);
12762 }
12763 }
12764 std::sort (offsets.begin (), offsets.end ());
12765 sect_offset *offsets_end = offsets.data () + offsets.size ();
12766 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12767 if (offsetp[-1] == *offsetp)
12768 complaint (_("Multiple children of DIE %s refer "
12769 "to DIE %s as their abstract origin"),
12770 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12771
12772 offsetp = offsets.data ();
12773 origin_child_die = origin_die->child;
12774 while (origin_child_die && origin_child_die->tag)
12775 {
12776 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12777 while (offsetp < offsets_end
12778 && *offsetp < origin_child_die->sect_off)
12779 offsetp++;
12780 if (offsetp >= offsets_end
12781 || *offsetp > origin_child_die->sect_off)
12782 {
12783 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12784 Check whether we're already processing ORIGIN_CHILD_DIE.
12785 This can happen with mutually referenced abstract_origins.
12786 PR 16581. */
12787 if (!origin_child_die->in_process)
12788 process_die (origin_child_die, origin_cu);
12789 }
12790 origin_child_die = sibling_die (origin_child_die);
12791 }
12792 origin_cu->list_in_scope = origin_previous_list_in_scope;
12793
12794 if (cu != origin_cu)
12795 compute_delayed_physnames (origin_cu);
12796 }
12797
12798 static void
12799 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12800 {
12801 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12802 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12803 struct context_stack *newobj;
12804 CORE_ADDR lowpc;
12805 CORE_ADDR highpc;
12806 struct die_info *child_die;
12807 struct attribute *attr, *call_line, *call_file;
12808 const char *name;
12809 CORE_ADDR baseaddr;
12810 struct block *block;
12811 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12812 std::vector<struct symbol *> template_args;
12813 struct template_symbol *templ_func = NULL;
12814
12815 if (inlined_func)
12816 {
12817 /* If we do not have call site information, we can't show the
12818 caller of this inlined function. That's too confusing, so
12819 only use the scope for local variables. */
12820 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12821 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12822 if (call_line == NULL || call_file == NULL)
12823 {
12824 read_lexical_block_scope (die, cu);
12825 return;
12826 }
12827 }
12828
12829 baseaddr = objfile->text_section_offset ();
12830
12831 name = dwarf2_name (die, cu);
12832
12833 /* Ignore functions with missing or empty names. These are actually
12834 illegal according to the DWARF standard. */
12835 if (name == NULL)
12836 {
12837 complaint (_("missing name for subprogram DIE at %s"),
12838 sect_offset_str (die->sect_off));
12839 return;
12840 }
12841
12842 /* Ignore functions with missing or invalid low and high pc attributes. */
12843 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12844 <= PC_BOUNDS_INVALID)
12845 {
12846 attr = dwarf2_attr (die, DW_AT_external, cu);
12847 if (!attr || !DW_UNSND (attr))
12848 complaint (_("cannot get low and high bounds "
12849 "for subprogram DIE at %s"),
12850 sect_offset_str (die->sect_off));
12851 return;
12852 }
12853
12854 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12855 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12856
12857 /* If we have any template arguments, then we must allocate a
12858 different sort of symbol. */
12859 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12860 {
12861 if (child_die->tag == DW_TAG_template_type_param
12862 || child_die->tag == DW_TAG_template_value_param)
12863 {
12864 templ_func = allocate_template_symbol (objfile);
12865 templ_func->subclass = SYMBOL_TEMPLATE;
12866 break;
12867 }
12868 }
12869
12870 newobj = cu->get_builder ()->push_context (0, lowpc);
12871 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12872 (struct symbol *) templ_func);
12873
12874 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12875 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12876 cu->language);
12877
12878 /* If there is a location expression for DW_AT_frame_base, record
12879 it. */
12880 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12881 if (attr != nullptr)
12882 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12883
12884 /* If there is a location for the static link, record it. */
12885 newobj->static_link = NULL;
12886 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12887 if (attr != nullptr)
12888 {
12889 newobj->static_link
12890 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12891 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12892 cu->per_cu->addr_type ());
12893 }
12894
12895 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12896
12897 if (die->child != NULL)
12898 {
12899 child_die = die->child;
12900 while (child_die && child_die->tag)
12901 {
12902 if (child_die->tag == DW_TAG_template_type_param
12903 || child_die->tag == DW_TAG_template_value_param)
12904 {
12905 struct symbol *arg = new_symbol (child_die, NULL, cu);
12906
12907 if (arg != NULL)
12908 template_args.push_back (arg);
12909 }
12910 else
12911 process_die (child_die, cu);
12912 child_die = sibling_die (child_die);
12913 }
12914 }
12915
12916 inherit_abstract_dies (die, cu);
12917
12918 /* If we have a DW_AT_specification, we might need to import using
12919 directives from the context of the specification DIE. See the
12920 comment in determine_prefix. */
12921 if (cu->language == language_cplus
12922 && dwarf2_attr (die, DW_AT_specification, cu))
12923 {
12924 struct dwarf2_cu *spec_cu = cu;
12925 struct die_info *spec_die = die_specification (die, &spec_cu);
12926
12927 while (spec_die)
12928 {
12929 child_die = spec_die->child;
12930 while (child_die && child_die->tag)
12931 {
12932 if (child_die->tag == DW_TAG_imported_module)
12933 process_die (child_die, spec_cu);
12934 child_die = sibling_die (child_die);
12935 }
12936
12937 /* In some cases, GCC generates specification DIEs that
12938 themselves contain DW_AT_specification attributes. */
12939 spec_die = die_specification (spec_die, &spec_cu);
12940 }
12941 }
12942
12943 struct context_stack cstk = cu->get_builder ()->pop_context ();
12944 /* Make a block for the local symbols within. */
12945 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12946 cstk.static_link, lowpc, highpc);
12947
12948 /* For C++, set the block's scope. */
12949 if ((cu->language == language_cplus
12950 || cu->language == language_fortran
12951 || cu->language == language_d
12952 || cu->language == language_rust)
12953 && cu->processing_has_namespace_info)
12954 block_set_scope (block, determine_prefix (die, cu),
12955 &objfile->objfile_obstack);
12956
12957 /* If we have address ranges, record them. */
12958 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12959
12960 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12961
12962 /* Attach template arguments to function. */
12963 if (!template_args.empty ())
12964 {
12965 gdb_assert (templ_func != NULL);
12966
12967 templ_func->n_template_arguments = template_args.size ();
12968 templ_func->template_arguments
12969 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12970 templ_func->n_template_arguments);
12971 memcpy (templ_func->template_arguments,
12972 template_args.data (),
12973 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12974
12975 /* Make sure that the symtab is set on the new symbols. Even
12976 though they don't appear in this symtab directly, other parts
12977 of gdb assume that symbols do, and this is reasonably
12978 true. */
12979 for (symbol *sym : template_args)
12980 symbol_set_symtab (sym, symbol_symtab (templ_func));
12981 }
12982
12983 /* In C++, we can have functions nested inside functions (e.g., when
12984 a function declares a class that has methods). This means that
12985 when we finish processing a function scope, we may need to go
12986 back to building a containing block's symbol lists. */
12987 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12988 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12989
12990 /* If we've finished processing a top-level function, subsequent
12991 symbols go in the file symbol list. */
12992 if (cu->get_builder ()->outermost_context_p ())
12993 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12994 }
12995
12996 /* Process all the DIES contained within a lexical block scope. Start
12997 a new scope, process the dies, and then close the scope. */
12998
12999 static void
13000 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13001 {
13002 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13003 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13004 CORE_ADDR lowpc, highpc;
13005 struct die_info *child_die;
13006 CORE_ADDR baseaddr;
13007
13008 baseaddr = objfile->text_section_offset ();
13009
13010 /* Ignore blocks with missing or invalid low and high pc attributes. */
13011 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13012 as multiple lexical blocks? Handling children in a sane way would
13013 be nasty. Might be easier to properly extend generic blocks to
13014 describe ranges. */
13015 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13016 {
13017 case PC_BOUNDS_NOT_PRESENT:
13018 /* DW_TAG_lexical_block has no attributes, process its children as if
13019 there was no wrapping by that DW_TAG_lexical_block.
13020 GCC does no longer produces such DWARF since GCC r224161. */
13021 for (child_die = die->child;
13022 child_die != NULL && child_die->tag;
13023 child_die = sibling_die (child_die))
13024 process_die (child_die, cu);
13025 return;
13026 case PC_BOUNDS_INVALID:
13027 return;
13028 }
13029 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13030 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13031
13032 cu->get_builder ()->push_context (0, lowpc);
13033 if (die->child != NULL)
13034 {
13035 child_die = die->child;
13036 while (child_die && child_die->tag)
13037 {
13038 process_die (child_die, cu);
13039 child_die = sibling_die (child_die);
13040 }
13041 }
13042 inherit_abstract_dies (die, cu);
13043 struct context_stack cstk = cu->get_builder ()->pop_context ();
13044
13045 if (*cu->get_builder ()->get_local_symbols () != NULL
13046 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13047 {
13048 struct block *block
13049 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13050 cstk.start_addr, highpc);
13051
13052 /* Note that recording ranges after traversing children, as we
13053 do here, means that recording a parent's ranges entails
13054 walking across all its children's ranges as they appear in
13055 the address map, which is quadratic behavior.
13056
13057 It would be nicer to record the parent's ranges before
13058 traversing its children, simply overriding whatever you find
13059 there. But since we don't even decide whether to create a
13060 block until after we've traversed its children, that's hard
13061 to do. */
13062 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13063 }
13064 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13065 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13066 }
13067
13068 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13069
13070 static void
13071 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13072 {
13073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13074 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13075 CORE_ADDR pc, baseaddr;
13076 struct attribute *attr;
13077 struct call_site *call_site, call_site_local;
13078 void **slot;
13079 int nparams;
13080 struct die_info *child_die;
13081
13082 baseaddr = objfile->text_section_offset ();
13083
13084 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13085 if (attr == NULL)
13086 {
13087 /* This was a pre-DWARF-5 GNU extension alias
13088 for DW_AT_call_return_pc. */
13089 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13090 }
13091 if (!attr)
13092 {
13093 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13094 "DIE %s [in module %s]"),
13095 sect_offset_str (die->sect_off), objfile_name (objfile));
13096 return;
13097 }
13098 pc = attr->value_as_address () + baseaddr;
13099 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13100
13101 if (cu->call_site_htab == NULL)
13102 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13103 NULL, &objfile->objfile_obstack,
13104 hashtab_obstack_allocate, NULL);
13105 call_site_local.pc = pc;
13106 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13107 if (*slot != NULL)
13108 {
13109 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13110 "DIE %s [in module %s]"),
13111 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13112 objfile_name (objfile));
13113 return;
13114 }
13115
13116 /* Count parameters at the caller. */
13117
13118 nparams = 0;
13119 for (child_die = die->child; child_die && child_die->tag;
13120 child_die = sibling_die (child_die))
13121 {
13122 if (child_die->tag != DW_TAG_call_site_parameter
13123 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13124 {
13125 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13126 "DW_TAG_call_site child DIE %s [in module %s]"),
13127 child_die->tag, sect_offset_str (child_die->sect_off),
13128 objfile_name (objfile));
13129 continue;
13130 }
13131
13132 nparams++;
13133 }
13134
13135 call_site
13136 = ((struct call_site *)
13137 obstack_alloc (&objfile->objfile_obstack,
13138 sizeof (*call_site)
13139 + (sizeof (*call_site->parameter) * (nparams - 1))));
13140 *slot = call_site;
13141 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13142 call_site->pc = pc;
13143
13144 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13145 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13146 {
13147 struct die_info *func_die;
13148
13149 /* Skip also over DW_TAG_inlined_subroutine. */
13150 for (func_die = die->parent;
13151 func_die && func_die->tag != DW_TAG_subprogram
13152 && func_die->tag != DW_TAG_subroutine_type;
13153 func_die = func_die->parent);
13154
13155 /* DW_AT_call_all_calls is a superset
13156 of DW_AT_call_all_tail_calls. */
13157 if (func_die
13158 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13159 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13160 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13161 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13162 {
13163 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13164 not complete. But keep CALL_SITE for look ups via call_site_htab,
13165 both the initial caller containing the real return address PC and
13166 the final callee containing the current PC of a chain of tail
13167 calls do not need to have the tail call list complete. But any
13168 function candidate for a virtual tail call frame searched via
13169 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13170 determined unambiguously. */
13171 }
13172 else
13173 {
13174 struct type *func_type = NULL;
13175
13176 if (func_die)
13177 func_type = get_die_type (func_die, cu);
13178 if (func_type != NULL)
13179 {
13180 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13181
13182 /* Enlist this call site to the function. */
13183 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13184 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13185 }
13186 else
13187 complaint (_("Cannot find function owning DW_TAG_call_site "
13188 "DIE %s [in module %s]"),
13189 sect_offset_str (die->sect_off), objfile_name (objfile));
13190 }
13191 }
13192
13193 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13194 if (attr == NULL)
13195 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13196 if (attr == NULL)
13197 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13198 if (attr == NULL)
13199 {
13200 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13201 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13202 }
13203 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13204 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13205 /* Keep NULL DWARF_BLOCK. */;
13206 else if (attr->form_is_block ())
13207 {
13208 struct dwarf2_locexpr_baton *dlbaton;
13209
13210 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13211 dlbaton->data = DW_BLOCK (attr)->data;
13212 dlbaton->size = DW_BLOCK (attr)->size;
13213 dlbaton->per_cu = cu->per_cu;
13214
13215 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13216 }
13217 else if (attr->form_is_ref ())
13218 {
13219 struct dwarf2_cu *target_cu = cu;
13220 struct die_info *target_die;
13221
13222 target_die = follow_die_ref (die, attr, &target_cu);
13223 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13224 if (die_is_declaration (target_die, target_cu))
13225 {
13226 const char *target_physname;
13227
13228 /* Prefer the mangled name; otherwise compute the demangled one. */
13229 target_physname = dw2_linkage_name (target_die, target_cu);
13230 if (target_physname == NULL)
13231 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13232 if (target_physname == NULL)
13233 complaint (_("DW_AT_call_target target DIE has invalid "
13234 "physname, for referencing DIE %s [in module %s]"),
13235 sect_offset_str (die->sect_off), objfile_name (objfile));
13236 else
13237 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13238 }
13239 else
13240 {
13241 CORE_ADDR lowpc;
13242
13243 /* DW_AT_entry_pc should be preferred. */
13244 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13245 <= PC_BOUNDS_INVALID)
13246 complaint (_("DW_AT_call_target target DIE has invalid "
13247 "low pc, for referencing DIE %s [in module %s]"),
13248 sect_offset_str (die->sect_off), objfile_name (objfile));
13249 else
13250 {
13251 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13252 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13253 }
13254 }
13255 }
13256 else
13257 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13258 "block nor reference, for DIE %s [in module %s]"),
13259 sect_offset_str (die->sect_off), objfile_name (objfile));
13260
13261 call_site->per_cu = cu->per_cu;
13262
13263 for (child_die = die->child;
13264 child_die && child_die->tag;
13265 child_die = sibling_die (child_die))
13266 {
13267 struct call_site_parameter *parameter;
13268 struct attribute *loc, *origin;
13269
13270 if (child_die->tag != DW_TAG_call_site_parameter
13271 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13272 {
13273 /* Already printed the complaint above. */
13274 continue;
13275 }
13276
13277 gdb_assert (call_site->parameter_count < nparams);
13278 parameter = &call_site->parameter[call_site->parameter_count];
13279
13280 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13281 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13282 register is contained in DW_AT_call_value. */
13283
13284 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13285 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13286 if (origin == NULL)
13287 {
13288 /* This was a pre-DWARF-5 GNU extension alias
13289 for DW_AT_call_parameter. */
13290 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13291 }
13292 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13293 {
13294 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13295
13296 sect_offset sect_off
13297 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13298 if (!cu->header.offset_in_cu_p (sect_off))
13299 {
13300 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13301 binding can be done only inside one CU. Such referenced DIE
13302 therefore cannot be even moved to DW_TAG_partial_unit. */
13303 complaint (_("DW_AT_call_parameter offset is not in CU for "
13304 "DW_TAG_call_site child DIE %s [in module %s]"),
13305 sect_offset_str (child_die->sect_off),
13306 objfile_name (objfile));
13307 continue;
13308 }
13309 parameter->u.param_cu_off
13310 = (cu_offset) (sect_off - cu->header.sect_off);
13311 }
13312 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13313 {
13314 complaint (_("No DW_FORM_block* DW_AT_location for "
13315 "DW_TAG_call_site child DIE %s [in module %s]"),
13316 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13317 continue;
13318 }
13319 else
13320 {
13321 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13322 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13323 if (parameter->u.dwarf_reg != -1)
13324 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13325 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13326 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13327 &parameter->u.fb_offset))
13328 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13329 else
13330 {
13331 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13332 "for DW_FORM_block* DW_AT_location is supported for "
13333 "DW_TAG_call_site child DIE %s "
13334 "[in module %s]"),
13335 sect_offset_str (child_die->sect_off),
13336 objfile_name (objfile));
13337 continue;
13338 }
13339 }
13340
13341 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13342 if (attr == NULL)
13343 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13344 if (attr == NULL || !attr->form_is_block ())
13345 {
13346 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13347 "DW_TAG_call_site child DIE %s [in module %s]"),
13348 sect_offset_str (child_die->sect_off),
13349 objfile_name (objfile));
13350 continue;
13351 }
13352 parameter->value = DW_BLOCK (attr)->data;
13353 parameter->value_size = DW_BLOCK (attr)->size;
13354
13355 /* Parameters are not pre-cleared by memset above. */
13356 parameter->data_value = NULL;
13357 parameter->data_value_size = 0;
13358 call_site->parameter_count++;
13359
13360 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13361 if (attr == NULL)
13362 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13363 if (attr != nullptr)
13364 {
13365 if (!attr->form_is_block ())
13366 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13367 "DW_TAG_call_site child DIE %s [in module %s]"),
13368 sect_offset_str (child_die->sect_off),
13369 objfile_name (objfile));
13370 else
13371 {
13372 parameter->data_value = DW_BLOCK (attr)->data;
13373 parameter->data_value_size = DW_BLOCK (attr)->size;
13374 }
13375 }
13376 }
13377 }
13378
13379 /* Helper function for read_variable. If DIE represents a virtual
13380 table, then return the type of the concrete object that is
13381 associated with the virtual table. Otherwise, return NULL. */
13382
13383 static struct type *
13384 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13385 {
13386 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13387 if (attr == NULL)
13388 return NULL;
13389
13390 /* Find the type DIE. */
13391 struct die_info *type_die = NULL;
13392 struct dwarf2_cu *type_cu = cu;
13393
13394 if (attr->form_is_ref ())
13395 type_die = follow_die_ref (die, attr, &type_cu);
13396 if (type_die == NULL)
13397 return NULL;
13398
13399 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13400 return NULL;
13401 return die_containing_type (type_die, type_cu);
13402 }
13403
13404 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13405
13406 static void
13407 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13408 {
13409 struct rust_vtable_symbol *storage = NULL;
13410
13411 if (cu->language == language_rust)
13412 {
13413 struct type *containing_type = rust_containing_type (die, cu);
13414
13415 if (containing_type != NULL)
13416 {
13417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13418
13419 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13420 initialize_objfile_symbol (storage);
13421 storage->concrete_type = containing_type;
13422 storage->subclass = SYMBOL_RUST_VTABLE;
13423 }
13424 }
13425
13426 struct symbol *res = new_symbol (die, NULL, cu, storage);
13427 struct attribute *abstract_origin
13428 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13429 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13430 if (res == NULL && loc && abstract_origin)
13431 {
13432 /* We have a variable without a name, but with a location and an abstract
13433 origin. This may be a concrete instance of an abstract variable
13434 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13435 later. */
13436 struct dwarf2_cu *origin_cu = cu;
13437 struct die_info *origin_die
13438 = follow_die_ref (die, abstract_origin, &origin_cu);
13439 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13440 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13441 }
13442 }
13443
13444 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13445 reading .debug_rnglists.
13446 Callback's type should be:
13447 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13448 Return true if the attributes are present and valid, otherwise,
13449 return false. */
13450
13451 template <typename Callback>
13452 static bool
13453 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13454 Callback &&callback)
13455 {
13456 struct dwarf2_per_objfile *dwarf2_per_objfile
13457 = cu->per_cu->dwarf2_per_objfile;
13458 struct objfile *objfile = dwarf2_per_objfile->objfile;
13459 bfd *obfd = objfile->obfd;
13460 /* Base address selection entry. */
13461 CORE_ADDR base;
13462 int found_base;
13463 const gdb_byte *buffer;
13464 CORE_ADDR baseaddr;
13465 bool overflow = false;
13466
13467 found_base = cu->base_known;
13468 base = cu->base_address;
13469
13470 dwarf2_per_objfile->rnglists.read (objfile);
13471 if (offset >= dwarf2_per_objfile->rnglists.size)
13472 {
13473 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13474 offset);
13475 return false;
13476 }
13477 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13478
13479 baseaddr = objfile->text_section_offset ();
13480
13481 while (1)
13482 {
13483 /* Initialize it due to a false compiler warning. */
13484 CORE_ADDR range_beginning = 0, range_end = 0;
13485 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13486 + dwarf2_per_objfile->rnglists.size);
13487 unsigned int bytes_read;
13488
13489 if (buffer == buf_end)
13490 {
13491 overflow = true;
13492 break;
13493 }
13494 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13495 switch (rlet)
13496 {
13497 case DW_RLE_end_of_list:
13498 break;
13499 case DW_RLE_base_address:
13500 if (buffer + cu->header.addr_size > buf_end)
13501 {
13502 overflow = true;
13503 break;
13504 }
13505 base = cu->header.read_address (obfd, buffer, &bytes_read);
13506 found_base = 1;
13507 buffer += bytes_read;
13508 break;
13509 case DW_RLE_start_length:
13510 if (buffer + cu->header.addr_size > buf_end)
13511 {
13512 overflow = true;
13513 break;
13514 }
13515 range_beginning = cu->header.read_address (obfd, buffer,
13516 &bytes_read);
13517 buffer += bytes_read;
13518 range_end = (range_beginning
13519 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13520 buffer += bytes_read;
13521 if (buffer > buf_end)
13522 {
13523 overflow = true;
13524 break;
13525 }
13526 break;
13527 case DW_RLE_offset_pair:
13528 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13529 buffer += bytes_read;
13530 if (buffer > buf_end)
13531 {
13532 overflow = true;
13533 break;
13534 }
13535 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13536 buffer += bytes_read;
13537 if (buffer > buf_end)
13538 {
13539 overflow = true;
13540 break;
13541 }
13542 break;
13543 case DW_RLE_start_end:
13544 if (buffer + 2 * cu->header.addr_size > buf_end)
13545 {
13546 overflow = true;
13547 break;
13548 }
13549 range_beginning = cu->header.read_address (obfd, buffer,
13550 &bytes_read);
13551 buffer += bytes_read;
13552 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13553 buffer += bytes_read;
13554 break;
13555 default:
13556 complaint (_("Invalid .debug_rnglists data (no base address)"));
13557 return false;
13558 }
13559 if (rlet == DW_RLE_end_of_list || overflow)
13560 break;
13561 if (rlet == DW_RLE_base_address)
13562 continue;
13563
13564 if (!found_base)
13565 {
13566 /* We have no valid base address for the ranges
13567 data. */
13568 complaint (_("Invalid .debug_rnglists data (no base address)"));
13569 return false;
13570 }
13571
13572 if (range_beginning > range_end)
13573 {
13574 /* Inverted range entries are invalid. */
13575 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13576 return false;
13577 }
13578
13579 /* Empty range entries have no effect. */
13580 if (range_beginning == range_end)
13581 continue;
13582
13583 range_beginning += base;
13584 range_end += base;
13585
13586 /* A not-uncommon case of bad debug info.
13587 Don't pollute the addrmap with bad data. */
13588 if (range_beginning + baseaddr == 0
13589 && !dwarf2_per_objfile->has_section_at_zero)
13590 {
13591 complaint (_(".debug_rnglists entry has start address of zero"
13592 " [in module %s]"), objfile_name (objfile));
13593 continue;
13594 }
13595
13596 callback (range_beginning, range_end);
13597 }
13598
13599 if (overflow)
13600 {
13601 complaint (_("Offset %d is not terminated "
13602 "for DW_AT_ranges attribute"),
13603 offset);
13604 return false;
13605 }
13606
13607 return true;
13608 }
13609
13610 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13611 Callback's type should be:
13612 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13613 Return 1 if the attributes are present and valid, otherwise, return 0. */
13614
13615 template <typename Callback>
13616 static int
13617 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13618 Callback &&callback)
13619 {
13620 struct dwarf2_per_objfile *dwarf2_per_objfile
13621 = cu->per_cu->dwarf2_per_objfile;
13622 struct objfile *objfile = dwarf2_per_objfile->objfile;
13623 struct comp_unit_head *cu_header = &cu->header;
13624 bfd *obfd = objfile->obfd;
13625 unsigned int addr_size = cu_header->addr_size;
13626 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13627 /* Base address selection entry. */
13628 CORE_ADDR base;
13629 int found_base;
13630 unsigned int dummy;
13631 const gdb_byte *buffer;
13632 CORE_ADDR baseaddr;
13633
13634 if (cu_header->version >= 5)
13635 return dwarf2_rnglists_process (offset, cu, callback);
13636
13637 found_base = cu->base_known;
13638 base = cu->base_address;
13639
13640 dwarf2_per_objfile->ranges.read (objfile);
13641 if (offset >= dwarf2_per_objfile->ranges.size)
13642 {
13643 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13644 offset);
13645 return 0;
13646 }
13647 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13648
13649 baseaddr = objfile->text_section_offset ();
13650
13651 while (1)
13652 {
13653 CORE_ADDR range_beginning, range_end;
13654
13655 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13656 buffer += addr_size;
13657 range_end = cu->header.read_address (obfd, buffer, &dummy);
13658 buffer += addr_size;
13659 offset += 2 * addr_size;
13660
13661 /* An end of list marker is a pair of zero addresses. */
13662 if (range_beginning == 0 && range_end == 0)
13663 /* Found the end of list entry. */
13664 break;
13665
13666 /* Each base address selection entry is a pair of 2 values.
13667 The first is the largest possible address, the second is
13668 the base address. Check for a base address here. */
13669 if ((range_beginning & mask) == mask)
13670 {
13671 /* If we found the largest possible address, then we already
13672 have the base address in range_end. */
13673 base = range_end;
13674 found_base = 1;
13675 continue;
13676 }
13677
13678 if (!found_base)
13679 {
13680 /* We have no valid base address for the ranges
13681 data. */
13682 complaint (_("Invalid .debug_ranges data (no base address)"));
13683 return 0;
13684 }
13685
13686 if (range_beginning > range_end)
13687 {
13688 /* Inverted range entries are invalid. */
13689 complaint (_("Invalid .debug_ranges data (inverted range)"));
13690 return 0;
13691 }
13692
13693 /* Empty range entries have no effect. */
13694 if (range_beginning == range_end)
13695 continue;
13696
13697 range_beginning += base;
13698 range_end += base;
13699
13700 /* A not-uncommon case of bad debug info.
13701 Don't pollute the addrmap with bad data. */
13702 if (range_beginning + baseaddr == 0
13703 && !dwarf2_per_objfile->has_section_at_zero)
13704 {
13705 complaint (_(".debug_ranges entry has start address of zero"
13706 " [in module %s]"), objfile_name (objfile));
13707 continue;
13708 }
13709
13710 callback (range_beginning, range_end);
13711 }
13712
13713 return 1;
13714 }
13715
13716 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13717 Return 1 if the attributes are present and valid, otherwise, return 0.
13718 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13719
13720 static int
13721 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13722 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13723 dwarf2_psymtab *ranges_pst)
13724 {
13725 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13726 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13727 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13728 int low_set = 0;
13729 CORE_ADDR low = 0;
13730 CORE_ADDR high = 0;
13731 int retval;
13732
13733 retval = dwarf2_ranges_process (offset, cu,
13734 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13735 {
13736 if (ranges_pst != NULL)
13737 {
13738 CORE_ADDR lowpc;
13739 CORE_ADDR highpc;
13740
13741 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13742 range_beginning + baseaddr)
13743 - baseaddr);
13744 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13745 range_end + baseaddr)
13746 - baseaddr);
13747 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13748 lowpc, highpc - 1, ranges_pst);
13749 }
13750
13751 /* FIXME: This is recording everything as a low-high
13752 segment of consecutive addresses. We should have a
13753 data structure for discontiguous block ranges
13754 instead. */
13755 if (! low_set)
13756 {
13757 low = range_beginning;
13758 high = range_end;
13759 low_set = 1;
13760 }
13761 else
13762 {
13763 if (range_beginning < low)
13764 low = range_beginning;
13765 if (range_end > high)
13766 high = range_end;
13767 }
13768 });
13769 if (!retval)
13770 return 0;
13771
13772 if (! low_set)
13773 /* If the first entry is an end-of-list marker, the range
13774 describes an empty scope, i.e. no instructions. */
13775 return 0;
13776
13777 if (low_return)
13778 *low_return = low;
13779 if (high_return)
13780 *high_return = high;
13781 return 1;
13782 }
13783
13784 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13785 definition for the return value. *LOWPC and *HIGHPC are set iff
13786 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13787
13788 static enum pc_bounds_kind
13789 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13790 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13791 dwarf2_psymtab *pst)
13792 {
13793 struct dwarf2_per_objfile *dwarf2_per_objfile
13794 = cu->per_cu->dwarf2_per_objfile;
13795 struct attribute *attr;
13796 struct attribute *attr_high;
13797 CORE_ADDR low = 0;
13798 CORE_ADDR high = 0;
13799 enum pc_bounds_kind ret;
13800
13801 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13802 if (attr_high)
13803 {
13804 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13805 if (attr != nullptr)
13806 {
13807 low = attr->value_as_address ();
13808 high = attr_high->value_as_address ();
13809 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13810 high += low;
13811 }
13812 else
13813 /* Found high w/o low attribute. */
13814 return PC_BOUNDS_INVALID;
13815
13816 /* Found consecutive range of addresses. */
13817 ret = PC_BOUNDS_HIGH_LOW;
13818 }
13819 else
13820 {
13821 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13822 if (attr != NULL)
13823 {
13824 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13825 We take advantage of the fact that DW_AT_ranges does not appear
13826 in DW_TAG_compile_unit of DWO files. */
13827 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13828 unsigned int ranges_offset = (DW_UNSND (attr)
13829 + (need_ranges_base
13830 ? cu->ranges_base
13831 : 0));
13832
13833 /* Value of the DW_AT_ranges attribute is the offset in the
13834 .debug_ranges section. */
13835 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13836 return PC_BOUNDS_INVALID;
13837 /* Found discontinuous range of addresses. */
13838 ret = PC_BOUNDS_RANGES;
13839 }
13840 else
13841 return PC_BOUNDS_NOT_PRESENT;
13842 }
13843
13844 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13845 if (high <= low)
13846 return PC_BOUNDS_INVALID;
13847
13848 /* When using the GNU linker, .gnu.linkonce. sections are used to
13849 eliminate duplicate copies of functions and vtables and such.
13850 The linker will arbitrarily choose one and discard the others.
13851 The AT_*_pc values for such functions refer to local labels in
13852 these sections. If the section from that file was discarded, the
13853 labels are not in the output, so the relocs get a value of 0.
13854 If this is a discarded function, mark the pc bounds as invalid,
13855 so that GDB will ignore it. */
13856 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13857 return PC_BOUNDS_INVALID;
13858
13859 *lowpc = low;
13860 if (highpc)
13861 *highpc = high;
13862 return ret;
13863 }
13864
13865 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13866 its low and high PC addresses. Do nothing if these addresses could not
13867 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13868 and HIGHPC to the high address if greater than HIGHPC. */
13869
13870 static void
13871 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13872 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13873 struct dwarf2_cu *cu)
13874 {
13875 CORE_ADDR low, high;
13876 struct die_info *child = die->child;
13877
13878 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13879 {
13880 *lowpc = std::min (*lowpc, low);
13881 *highpc = std::max (*highpc, high);
13882 }
13883
13884 /* If the language does not allow nested subprograms (either inside
13885 subprograms or lexical blocks), we're done. */
13886 if (cu->language != language_ada)
13887 return;
13888
13889 /* Check all the children of the given DIE. If it contains nested
13890 subprograms, then check their pc bounds. Likewise, we need to
13891 check lexical blocks as well, as they may also contain subprogram
13892 definitions. */
13893 while (child && child->tag)
13894 {
13895 if (child->tag == DW_TAG_subprogram
13896 || child->tag == DW_TAG_lexical_block)
13897 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13898 child = sibling_die (child);
13899 }
13900 }
13901
13902 /* Get the low and high pc's represented by the scope DIE, and store
13903 them in *LOWPC and *HIGHPC. If the correct values can't be
13904 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13905
13906 static void
13907 get_scope_pc_bounds (struct die_info *die,
13908 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13909 struct dwarf2_cu *cu)
13910 {
13911 CORE_ADDR best_low = (CORE_ADDR) -1;
13912 CORE_ADDR best_high = (CORE_ADDR) 0;
13913 CORE_ADDR current_low, current_high;
13914
13915 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13916 >= PC_BOUNDS_RANGES)
13917 {
13918 best_low = current_low;
13919 best_high = current_high;
13920 }
13921 else
13922 {
13923 struct die_info *child = die->child;
13924
13925 while (child && child->tag)
13926 {
13927 switch (child->tag) {
13928 case DW_TAG_subprogram:
13929 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13930 break;
13931 case DW_TAG_namespace:
13932 case DW_TAG_module:
13933 /* FIXME: carlton/2004-01-16: Should we do this for
13934 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13935 that current GCC's always emit the DIEs corresponding
13936 to definitions of methods of classes as children of a
13937 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13938 the DIEs giving the declarations, which could be
13939 anywhere). But I don't see any reason why the
13940 standards says that they have to be there. */
13941 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13942
13943 if (current_low != ((CORE_ADDR) -1))
13944 {
13945 best_low = std::min (best_low, current_low);
13946 best_high = std::max (best_high, current_high);
13947 }
13948 break;
13949 default:
13950 /* Ignore. */
13951 break;
13952 }
13953
13954 child = sibling_die (child);
13955 }
13956 }
13957
13958 *lowpc = best_low;
13959 *highpc = best_high;
13960 }
13961
13962 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13963 in DIE. */
13964
13965 static void
13966 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13967 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13968 {
13969 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13970 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13971 struct attribute *attr;
13972 struct attribute *attr_high;
13973
13974 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13975 if (attr_high)
13976 {
13977 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13978 if (attr != nullptr)
13979 {
13980 CORE_ADDR low = attr->value_as_address ();
13981 CORE_ADDR high = attr_high->value_as_address ();
13982
13983 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13984 high += low;
13985
13986 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13987 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13988 cu->get_builder ()->record_block_range (block, low, high - 1);
13989 }
13990 }
13991
13992 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13993 if (attr != nullptr)
13994 {
13995 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13996 We take advantage of the fact that DW_AT_ranges does not appear
13997 in DW_TAG_compile_unit of DWO files. */
13998 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13999
14000 /* The value of the DW_AT_ranges attribute is the offset of the
14001 address range list in the .debug_ranges section. */
14002 unsigned long offset = (DW_UNSND (attr)
14003 + (need_ranges_base ? cu->ranges_base : 0));
14004
14005 std::vector<blockrange> blockvec;
14006 dwarf2_ranges_process (offset, cu,
14007 [&] (CORE_ADDR start, CORE_ADDR end)
14008 {
14009 start += baseaddr;
14010 end += baseaddr;
14011 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14012 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14013 cu->get_builder ()->record_block_range (block, start, end - 1);
14014 blockvec.emplace_back (start, end);
14015 });
14016
14017 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14018 }
14019 }
14020
14021 /* Check whether the producer field indicates either of GCC < 4.6, or the
14022 Intel C/C++ compiler, and cache the result in CU. */
14023
14024 static void
14025 check_producer (struct dwarf2_cu *cu)
14026 {
14027 int major, minor;
14028
14029 if (cu->producer == NULL)
14030 {
14031 /* For unknown compilers expect their behavior is DWARF version
14032 compliant.
14033
14034 GCC started to support .debug_types sections by -gdwarf-4 since
14035 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14036 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14037 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14038 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14039 }
14040 else if (producer_is_gcc (cu->producer, &major, &minor))
14041 {
14042 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14043 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14044 }
14045 else if (producer_is_icc (cu->producer, &major, &minor))
14046 {
14047 cu->producer_is_icc = true;
14048 cu->producer_is_icc_lt_14 = major < 14;
14049 }
14050 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14051 cu->producer_is_codewarrior = true;
14052 else
14053 {
14054 /* For other non-GCC compilers, expect their behavior is DWARF version
14055 compliant. */
14056 }
14057
14058 cu->checked_producer = true;
14059 }
14060
14061 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14062 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14063 during 4.6.0 experimental. */
14064
14065 static bool
14066 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14067 {
14068 if (!cu->checked_producer)
14069 check_producer (cu);
14070
14071 return cu->producer_is_gxx_lt_4_6;
14072 }
14073
14074
14075 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14076 with incorrect is_stmt attributes. */
14077
14078 static bool
14079 producer_is_codewarrior (struct dwarf2_cu *cu)
14080 {
14081 if (!cu->checked_producer)
14082 check_producer (cu);
14083
14084 return cu->producer_is_codewarrior;
14085 }
14086
14087 /* Return the default accessibility type if it is not overridden by
14088 DW_AT_accessibility. */
14089
14090 static enum dwarf_access_attribute
14091 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14092 {
14093 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14094 {
14095 /* The default DWARF 2 accessibility for members is public, the default
14096 accessibility for inheritance is private. */
14097
14098 if (die->tag != DW_TAG_inheritance)
14099 return DW_ACCESS_public;
14100 else
14101 return DW_ACCESS_private;
14102 }
14103 else
14104 {
14105 /* DWARF 3+ defines the default accessibility a different way. The same
14106 rules apply now for DW_TAG_inheritance as for the members and it only
14107 depends on the container kind. */
14108
14109 if (die->parent->tag == DW_TAG_class_type)
14110 return DW_ACCESS_private;
14111 else
14112 return DW_ACCESS_public;
14113 }
14114 }
14115
14116 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14117 offset. If the attribute was not found return 0, otherwise return
14118 1. If it was found but could not properly be handled, set *OFFSET
14119 to 0. */
14120
14121 static int
14122 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14123 LONGEST *offset)
14124 {
14125 struct attribute *attr;
14126
14127 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14128 if (attr != NULL)
14129 {
14130 *offset = 0;
14131
14132 /* Note that we do not check for a section offset first here.
14133 This is because DW_AT_data_member_location is new in DWARF 4,
14134 so if we see it, we can assume that a constant form is really
14135 a constant and not a section offset. */
14136 if (attr->form_is_constant ())
14137 *offset = dwarf2_get_attr_constant_value (attr, 0);
14138 else if (attr->form_is_section_offset ())
14139 dwarf2_complex_location_expr_complaint ();
14140 else if (attr->form_is_block ())
14141 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14142 else
14143 dwarf2_complex_location_expr_complaint ();
14144
14145 return 1;
14146 }
14147
14148 return 0;
14149 }
14150
14151 /* Add an aggregate field to the field list. */
14152
14153 static void
14154 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14155 struct dwarf2_cu *cu)
14156 {
14157 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14158 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14159 struct nextfield *new_field;
14160 struct attribute *attr;
14161 struct field *fp;
14162 const char *fieldname = "";
14163
14164 if (die->tag == DW_TAG_inheritance)
14165 {
14166 fip->baseclasses.emplace_back ();
14167 new_field = &fip->baseclasses.back ();
14168 }
14169 else
14170 {
14171 fip->fields.emplace_back ();
14172 new_field = &fip->fields.back ();
14173 }
14174
14175 fip->nfields++;
14176
14177 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14178 if (attr != nullptr)
14179 new_field->accessibility = DW_UNSND (attr);
14180 else
14181 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14182 if (new_field->accessibility != DW_ACCESS_public)
14183 fip->non_public_fields = 1;
14184
14185 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14186 if (attr != nullptr)
14187 new_field->virtuality = DW_UNSND (attr);
14188 else
14189 new_field->virtuality = DW_VIRTUALITY_none;
14190
14191 fp = &new_field->field;
14192
14193 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14194 {
14195 LONGEST offset;
14196
14197 /* Data member other than a C++ static data member. */
14198
14199 /* Get type of field. */
14200 fp->type = die_type (die, cu);
14201
14202 SET_FIELD_BITPOS (*fp, 0);
14203
14204 /* Get bit size of field (zero if none). */
14205 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14206 if (attr != nullptr)
14207 {
14208 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14209 }
14210 else
14211 {
14212 FIELD_BITSIZE (*fp) = 0;
14213 }
14214
14215 /* Get bit offset of field. */
14216 if (handle_data_member_location (die, cu, &offset))
14217 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14218 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14219 if (attr != nullptr)
14220 {
14221 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14222 {
14223 /* For big endian bits, the DW_AT_bit_offset gives the
14224 additional bit offset from the MSB of the containing
14225 anonymous object to the MSB of the field. We don't
14226 have to do anything special since we don't need to
14227 know the size of the anonymous object. */
14228 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14229 }
14230 else
14231 {
14232 /* For little endian bits, compute the bit offset to the
14233 MSB of the anonymous object, subtract off the number of
14234 bits from the MSB of the field to the MSB of the
14235 object, and then subtract off the number of bits of
14236 the field itself. The result is the bit offset of
14237 the LSB of the field. */
14238 int anonymous_size;
14239 int bit_offset = DW_UNSND (attr);
14240
14241 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14242 if (attr != nullptr)
14243 {
14244 /* The size of the anonymous object containing
14245 the bit field is explicit, so use the
14246 indicated size (in bytes). */
14247 anonymous_size = DW_UNSND (attr);
14248 }
14249 else
14250 {
14251 /* The size of the anonymous object containing
14252 the bit field must be inferred from the type
14253 attribute of the data member containing the
14254 bit field. */
14255 anonymous_size = TYPE_LENGTH (fp->type);
14256 }
14257 SET_FIELD_BITPOS (*fp,
14258 (FIELD_BITPOS (*fp)
14259 + anonymous_size * bits_per_byte
14260 - bit_offset - FIELD_BITSIZE (*fp)));
14261 }
14262 }
14263 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14264 if (attr != NULL)
14265 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14266 + dwarf2_get_attr_constant_value (attr, 0)));
14267
14268 /* Get name of field. */
14269 fieldname = dwarf2_name (die, cu);
14270 if (fieldname == NULL)
14271 fieldname = "";
14272
14273 /* The name is already allocated along with this objfile, so we don't
14274 need to duplicate it for the type. */
14275 fp->name = fieldname;
14276
14277 /* Change accessibility for artificial fields (e.g. virtual table
14278 pointer or virtual base class pointer) to private. */
14279 if (dwarf2_attr (die, DW_AT_artificial, cu))
14280 {
14281 FIELD_ARTIFICIAL (*fp) = 1;
14282 new_field->accessibility = DW_ACCESS_private;
14283 fip->non_public_fields = 1;
14284 }
14285 }
14286 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14287 {
14288 /* C++ static member. */
14289
14290 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14291 is a declaration, but all versions of G++ as of this writing
14292 (so through at least 3.2.1) incorrectly generate
14293 DW_TAG_variable tags. */
14294
14295 const char *physname;
14296
14297 /* Get name of field. */
14298 fieldname = dwarf2_name (die, cu);
14299 if (fieldname == NULL)
14300 return;
14301
14302 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14303 if (attr
14304 /* Only create a symbol if this is an external value.
14305 new_symbol checks this and puts the value in the global symbol
14306 table, which we want. If it is not external, new_symbol
14307 will try to put the value in cu->list_in_scope which is wrong. */
14308 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14309 {
14310 /* A static const member, not much different than an enum as far as
14311 we're concerned, except that we can support more types. */
14312 new_symbol (die, NULL, cu);
14313 }
14314
14315 /* Get physical name. */
14316 physname = dwarf2_physname (fieldname, die, cu);
14317
14318 /* The name is already allocated along with this objfile, so we don't
14319 need to duplicate it for the type. */
14320 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14321 FIELD_TYPE (*fp) = die_type (die, cu);
14322 FIELD_NAME (*fp) = fieldname;
14323 }
14324 else if (die->tag == DW_TAG_inheritance)
14325 {
14326 LONGEST offset;
14327
14328 /* C++ base class field. */
14329 if (handle_data_member_location (die, cu, &offset))
14330 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14331 FIELD_BITSIZE (*fp) = 0;
14332 FIELD_TYPE (*fp) = die_type (die, cu);
14333 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14334 }
14335 else if (die->tag == DW_TAG_variant_part)
14336 {
14337 /* process_structure_scope will treat this DIE as a union. */
14338 process_structure_scope (die, cu);
14339
14340 /* The variant part is relative to the start of the enclosing
14341 structure. */
14342 SET_FIELD_BITPOS (*fp, 0);
14343 fp->type = get_die_type (die, cu);
14344 fp->artificial = 1;
14345 fp->name = "<<variant>>";
14346
14347 /* Normally a DW_TAG_variant_part won't have a size, but our
14348 representation requires one, so set it to the maximum of the
14349 child sizes, being sure to account for the offset at which
14350 each child is seen. */
14351 if (TYPE_LENGTH (fp->type) == 0)
14352 {
14353 unsigned max = 0;
14354 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14355 {
14356 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14357 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14358 if (len > max)
14359 max = len;
14360 }
14361 TYPE_LENGTH (fp->type) = max;
14362 }
14363 }
14364 else
14365 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14366 }
14367
14368 /* Can the type given by DIE define another type? */
14369
14370 static bool
14371 type_can_define_types (const struct die_info *die)
14372 {
14373 switch (die->tag)
14374 {
14375 case DW_TAG_typedef:
14376 case DW_TAG_class_type:
14377 case DW_TAG_structure_type:
14378 case DW_TAG_union_type:
14379 case DW_TAG_enumeration_type:
14380 return true;
14381
14382 default:
14383 return false;
14384 }
14385 }
14386
14387 /* Add a type definition defined in the scope of the FIP's class. */
14388
14389 static void
14390 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14391 struct dwarf2_cu *cu)
14392 {
14393 struct decl_field fp;
14394 memset (&fp, 0, sizeof (fp));
14395
14396 gdb_assert (type_can_define_types (die));
14397
14398 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14399 fp.name = dwarf2_name (die, cu);
14400 fp.type = read_type_die (die, cu);
14401
14402 /* Save accessibility. */
14403 enum dwarf_access_attribute accessibility;
14404 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14405 if (attr != NULL)
14406 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14407 else
14408 accessibility = dwarf2_default_access_attribute (die, cu);
14409 switch (accessibility)
14410 {
14411 case DW_ACCESS_public:
14412 /* The assumed value if neither private nor protected. */
14413 break;
14414 case DW_ACCESS_private:
14415 fp.is_private = 1;
14416 break;
14417 case DW_ACCESS_protected:
14418 fp.is_protected = 1;
14419 break;
14420 default:
14421 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14422 }
14423
14424 if (die->tag == DW_TAG_typedef)
14425 fip->typedef_field_list.push_back (fp);
14426 else
14427 fip->nested_types_list.push_back (fp);
14428 }
14429
14430 /* Create the vector of fields, and attach it to the type. */
14431
14432 static void
14433 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14434 struct dwarf2_cu *cu)
14435 {
14436 int nfields = fip->nfields;
14437
14438 /* Record the field count, allocate space for the array of fields,
14439 and create blank accessibility bitfields if necessary. */
14440 TYPE_NFIELDS (type) = nfields;
14441 TYPE_FIELDS (type) = (struct field *)
14442 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14443
14444 if (fip->non_public_fields && cu->language != language_ada)
14445 {
14446 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14447
14448 TYPE_FIELD_PRIVATE_BITS (type) =
14449 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14450 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14451
14452 TYPE_FIELD_PROTECTED_BITS (type) =
14453 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14454 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14455
14456 TYPE_FIELD_IGNORE_BITS (type) =
14457 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14458 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14459 }
14460
14461 /* If the type has baseclasses, allocate and clear a bit vector for
14462 TYPE_FIELD_VIRTUAL_BITS. */
14463 if (!fip->baseclasses.empty () && cu->language != language_ada)
14464 {
14465 int num_bytes = B_BYTES (fip->baseclasses.size ());
14466 unsigned char *pointer;
14467
14468 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14469 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14470 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14471 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14472 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14473 }
14474
14475 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14476 {
14477 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14478
14479 for (int index = 0; index < nfields; ++index)
14480 {
14481 struct nextfield &field = fip->fields[index];
14482
14483 if (field.variant.is_discriminant)
14484 di->discriminant_index = index;
14485 else if (field.variant.default_branch)
14486 di->default_index = index;
14487 else
14488 di->discriminants[index] = field.variant.discriminant_value;
14489 }
14490 }
14491
14492 /* Copy the saved-up fields into the field vector. */
14493 for (int i = 0; i < nfields; ++i)
14494 {
14495 struct nextfield &field
14496 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14497 : fip->fields[i - fip->baseclasses.size ()]);
14498
14499 TYPE_FIELD (type, i) = field.field;
14500 switch (field.accessibility)
14501 {
14502 case DW_ACCESS_private:
14503 if (cu->language != language_ada)
14504 SET_TYPE_FIELD_PRIVATE (type, i);
14505 break;
14506
14507 case DW_ACCESS_protected:
14508 if (cu->language != language_ada)
14509 SET_TYPE_FIELD_PROTECTED (type, i);
14510 break;
14511
14512 case DW_ACCESS_public:
14513 break;
14514
14515 default:
14516 /* Unknown accessibility. Complain and treat it as public. */
14517 {
14518 complaint (_("unsupported accessibility %d"),
14519 field.accessibility);
14520 }
14521 break;
14522 }
14523 if (i < fip->baseclasses.size ())
14524 {
14525 switch (field.virtuality)
14526 {
14527 case DW_VIRTUALITY_virtual:
14528 case DW_VIRTUALITY_pure_virtual:
14529 if (cu->language == language_ada)
14530 error (_("unexpected virtuality in component of Ada type"));
14531 SET_TYPE_FIELD_VIRTUAL (type, i);
14532 break;
14533 }
14534 }
14535 }
14536 }
14537
14538 /* Return true if this member function is a constructor, false
14539 otherwise. */
14540
14541 static int
14542 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14543 {
14544 const char *fieldname;
14545 const char *type_name;
14546 int len;
14547
14548 if (die->parent == NULL)
14549 return 0;
14550
14551 if (die->parent->tag != DW_TAG_structure_type
14552 && die->parent->tag != DW_TAG_union_type
14553 && die->parent->tag != DW_TAG_class_type)
14554 return 0;
14555
14556 fieldname = dwarf2_name (die, cu);
14557 type_name = dwarf2_name (die->parent, cu);
14558 if (fieldname == NULL || type_name == NULL)
14559 return 0;
14560
14561 len = strlen (fieldname);
14562 return (strncmp (fieldname, type_name, len) == 0
14563 && (type_name[len] == '\0' || type_name[len] == '<'));
14564 }
14565
14566 /* Check if the given VALUE is a recognized enum
14567 dwarf_defaulted_attribute constant according to DWARF5 spec,
14568 Table 7.24. */
14569
14570 static bool
14571 is_valid_DW_AT_defaulted (ULONGEST value)
14572 {
14573 switch (value)
14574 {
14575 case DW_DEFAULTED_no:
14576 case DW_DEFAULTED_in_class:
14577 case DW_DEFAULTED_out_of_class:
14578 return true;
14579 }
14580
14581 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14582 return false;
14583 }
14584
14585 /* Add a member function to the proper fieldlist. */
14586
14587 static void
14588 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14589 struct type *type, struct dwarf2_cu *cu)
14590 {
14591 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14592 struct attribute *attr;
14593 int i;
14594 struct fnfieldlist *flp = nullptr;
14595 struct fn_field *fnp;
14596 const char *fieldname;
14597 struct type *this_type;
14598 enum dwarf_access_attribute accessibility;
14599
14600 if (cu->language == language_ada)
14601 error (_("unexpected member function in Ada type"));
14602
14603 /* Get name of member function. */
14604 fieldname = dwarf2_name (die, cu);
14605 if (fieldname == NULL)
14606 return;
14607
14608 /* Look up member function name in fieldlist. */
14609 for (i = 0; i < fip->fnfieldlists.size (); i++)
14610 {
14611 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14612 {
14613 flp = &fip->fnfieldlists[i];
14614 break;
14615 }
14616 }
14617
14618 /* Create a new fnfieldlist if necessary. */
14619 if (flp == nullptr)
14620 {
14621 fip->fnfieldlists.emplace_back ();
14622 flp = &fip->fnfieldlists.back ();
14623 flp->name = fieldname;
14624 i = fip->fnfieldlists.size () - 1;
14625 }
14626
14627 /* Create a new member function field and add it to the vector of
14628 fnfieldlists. */
14629 flp->fnfields.emplace_back ();
14630 fnp = &flp->fnfields.back ();
14631
14632 /* Delay processing of the physname until later. */
14633 if (cu->language == language_cplus)
14634 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14635 die, cu);
14636 else
14637 {
14638 const char *physname = dwarf2_physname (fieldname, die, cu);
14639 fnp->physname = physname ? physname : "";
14640 }
14641
14642 fnp->type = alloc_type (objfile);
14643 this_type = read_type_die (die, cu);
14644 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14645 {
14646 int nparams = TYPE_NFIELDS (this_type);
14647
14648 /* TYPE is the domain of this method, and THIS_TYPE is the type
14649 of the method itself (TYPE_CODE_METHOD). */
14650 smash_to_method_type (fnp->type, type,
14651 TYPE_TARGET_TYPE (this_type),
14652 TYPE_FIELDS (this_type),
14653 TYPE_NFIELDS (this_type),
14654 TYPE_VARARGS (this_type));
14655
14656 /* Handle static member functions.
14657 Dwarf2 has no clean way to discern C++ static and non-static
14658 member functions. G++ helps GDB by marking the first
14659 parameter for non-static member functions (which is the this
14660 pointer) as artificial. We obtain this information from
14661 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14662 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14663 fnp->voffset = VOFFSET_STATIC;
14664 }
14665 else
14666 complaint (_("member function type missing for '%s'"),
14667 dwarf2_full_name (fieldname, die, cu));
14668
14669 /* Get fcontext from DW_AT_containing_type if present. */
14670 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14671 fnp->fcontext = die_containing_type (die, cu);
14672
14673 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14674 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14675
14676 /* Get accessibility. */
14677 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14678 if (attr != nullptr)
14679 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14680 else
14681 accessibility = dwarf2_default_access_attribute (die, cu);
14682 switch (accessibility)
14683 {
14684 case DW_ACCESS_private:
14685 fnp->is_private = 1;
14686 break;
14687 case DW_ACCESS_protected:
14688 fnp->is_protected = 1;
14689 break;
14690 }
14691
14692 /* Check for artificial methods. */
14693 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14694 if (attr && DW_UNSND (attr) != 0)
14695 fnp->is_artificial = 1;
14696
14697 /* Check for defaulted methods. */
14698 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14699 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14700 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14701
14702 /* Check for deleted methods. */
14703 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14704 if (attr != nullptr && DW_UNSND (attr) != 0)
14705 fnp->is_deleted = 1;
14706
14707 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14708
14709 /* Get index in virtual function table if it is a virtual member
14710 function. For older versions of GCC, this is an offset in the
14711 appropriate virtual table, as specified by DW_AT_containing_type.
14712 For everyone else, it is an expression to be evaluated relative
14713 to the object address. */
14714
14715 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14716 if (attr != nullptr)
14717 {
14718 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14719 {
14720 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14721 {
14722 /* Old-style GCC. */
14723 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14724 }
14725 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14726 || (DW_BLOCK (attr)->size > 1
14727 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14728 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14729 {
14730 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14731 if ((fnp->voffset % cu->header.addr_size) != 0)
14732 dwarf2_complex_location_expr_complaint ();
14733 else
14734 fnp->voffset /= cu->header.addr_size;
14735 fnp->voffset += 2;
14736 }
14737 else
14738 dwarf2_complex_location_expr_complaint ();
14739
14740 if (!fnp->fcontext)
14741 {
14742 /* If there is no `this' field and no DW_AT_containing_type,
14743 we cannot actually find a base class context for the
14744 vtable! */
14745 if (TYPE_NFIELDS (this_type) == 0
14746 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14747 {
14748 complaint (_("cannot determine context for virtual member "
14749 "function \"%s\" (offset %s)"),
14750 fieldname, sect_offset_str (die->sect_off));
14751 }
14752 else
14753 {
14754 fnp->fcontext
14755 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14756 }
14757 }
14758 }
14759 else if (attr->form_is_section_offset ())
14760 {
14761 dwarf2_complex_location_expr_complaint ();
14762 }
14763 else
14764 {
14765 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14766 fieldname);
14767 }
14768 }
14769 else
14770 {
14771 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14772 if (attr && DW_UNSND (attr))
14773 {
14774 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14775 complaint (_("Member function \"%s\" (offset %s) is virtual "
14776 "but the vtable offset is not specified"),
14777 fieldname, sect_offset_str (die->sect_off));
14778 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14779 TYPE_CPLUS_DYNAMIC (type) = 1;
14780 }
14781 }
14782 }
14783
14784 /* Create the vector of member function fields, and attach it to the type. */
14785
14786 static void
14787 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14788 struct dwarf2_cu *cu)
14789 {
14790 if (cu->language == language_ada)
14791 error (_("unexpected member functions in Ada type"));
14792
14793 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14794 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14795 TYPE_ALLOC (type,
14796 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14797
14798 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14799 {
14800 struct fnfieldlist &nf = fip->fnfieldlists[i];
14801 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14802
14803 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14804 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14805 fn_flp->fn_fields = (struct fn_field *)
14806 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14807
14808 for (int k = 0; k < nf.fnfields.size (); ++k)
14809 fn_flp->fn_fields[k] = nf.fnfields[k];
14810 }
14811
14812 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14813 }
14814
14815 /* Returns non-zero if NAME is the name of a vtable member in CU's
14816 language, zero otherwise. */
14817 static int
14818 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14819 {
14820 static const char vptr[] = "_vptr";
14821
14822 /* Look for the C++ form of the vtable. */
14823 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14824 return 1;
14825
14826 return 0;
14827 }
14828
14829 /* GCC outputs unnamed structures that are really pointers to member
14830 functions, with the ABI-specified layout. If TYPE describes
14831 such a structure, smash it into a member function type.
14832
14833 GCC shouldn't do this; it should just output pointer to member DIEs.
14834 This is GCC PR debug/28767. */
14835
14836 static void
14837 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14838 {
14839 struct type *pfn_type, *self_type, *new_type;
14840
14841 /* Check for a structure with no name and two children. */
14842 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14843 return;
14844
14845 /* Check for __pfn and __delta members. */
14846 if (TYPE_FIELD_NAME (type, 0) == NULL
14847 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14848 || TYPE_FIELD_NAME (type, 1) == NULL
14849 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14850 return;
14851
14852 /* Find the type of the method. */
14853 pfn_type = TYPE_FIELD_TYPE (type, 0);
14854 if (pfn_type == NULL
14855 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14856 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14857 return;
14858
14859 /* Look for the "this" argument. */
14860 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14861 if (TYPE_NFIELDS (pfn_type) == 0
14862 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14863 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14864 return;
14865
14866 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14867 new_type = alloc_type (objfile);
14868 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14869 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14870 TYPE_VARARGS (pfn_type));
14871 smash_to_methodptr_type (type, new_type);
14872 }
14873
14874 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14875 appropriate error checking and issuing complaints if there is a
14876 problem. */
14877
14878 static ULONGEST
14879 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14880 {
14881 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14882
14883 if (attr == nullptr)
14884 return 0;
14885
14886 if (!attr->form_is_constant ())
14887 {
14888 complaint (_("DW_AT_alignment must have constant form"
14889 " - DIE at %s [in module %s]"),
14890 sect_offset_str (die->sect_off),
14891 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14892 return 0;
14893 }
14894
14895 ULONGEST align;
14896 if (attr->form == DW_FORM_sdata)
14897 {
14898 LONGEST val = DW_SND (attr);
14899 if (val < 0)
14900 {
14901 complaint (_("DW_AT_alignment value must not be negative"
14902 " - DIE at %s [in module %s]"),
14903 sect_offset_str (die->sect_off),
14904 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14905 return 0;
14906 }
14907 align = val;
14908 }
14909 else
14910 align = DW_UNSND (attr);
14911
14912 if (align == 0)
14913 {
14914 complaint (_("DW_AT_alignment value must not be zero"
14915 " - DIE at %s [in module %s]"),
14916 sect_offset_str (die->sect_off),
14917 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14918 return 0;
14919 }
14920 if ((align & (align - 1)) != 0)
14921 {
14922 complaint (_("DW_AT_alignment value must be a power of 2"
14923 " - DIE at %s [in module %s]"),
14924 sect_offset_str (die->sect_off),
14925 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14926 return 0;
14927 }
14928
14929 return align;
14930 }
14931
14932 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14933 the alignment for TYPE. */
14934
14935 static void
14936 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14937 struct type *type)
14938 {
14939 if (!set_type_align (type, get_alignment (cu, die)))
14940 complaint (_("DW_AT_alignment value too large"
14941 " - DIE at %s [in module %s]"),
14942 sect_offset_str (die->sect_off),
14943 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14944 }
14945
14946 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14947 constant for a type, according to DWARF5 spec, Table 5.5. */
14948
14949 static bool
14950 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14951 {
14952 switch (value)
14953 {
14954 case DW_CC_normal:
14955 case DW_CC_pass_by_reference:
14956 case DW_CC_pass_by_value:
14957 return true;
14958
14959 default:
14960 complaint (_("unrecognized DW_AT_calling_convention value "
14961 "(%s) for a type"), pulongest (value));
14962 return false;
14963 }
14964 }
14965
14966 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14967 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14968 also according to GNU-specific values (see include/dwarf2.h). */
14969
14970 static bool
14971 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14972 {
14973 switch (value)
14974 {
14975 case DW_CC_normal:
14976 case DW_CC_program:
14977 case DW_CC_nocall:
14978 return true;
14979
14980 case DW_CC_GNU_renesas_sh:
14981 case DW_CC_GNU_borland_fastcall_i386:
14982 case DW_CC_GDB_IBM_OpenCL:
14983 return true;
14984
14985 default:
14986 complaint (_("unrecognized DW_AT_calling_convention value "
14987 "(%s) for a subroutine"), pulongest (value));
14988 return false;
14989 }
14990 }
14991
14992 /* Called when we find the DIE that starts a structure or union scope
14993 (definition) to create a type for the structure or union. Fill in
14994 the type's name and general properties; the members will not be
14995 processed until process_structure_scope. A symbol table entry for
14996 the type will also not be done until process_structure_scope (assuming
14997 the type has a name).
14998
14999 NOTE: we need to call these functions regardless of whether or not the
15000 DIE has a DW_AT_name attribute, since it might be an anonymous
15001 structure or union. This gets the type entered into our set of
15002 user defined types. */
15003
15004 static struct type *
15005 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15006 {
15007 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15008 struct type *type;
15009 struct attribute *attr;
15010 const char *name;
15011
15012 /* If the definition of this type lives in .debug_types, read that type.
15013 Don't follow DW_AT_specification though, that will take us back up
15014 the chain and we want to go down. */
15015 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15016 if (attr != nullptr)
15017 {
15018 type = get_DW_AT_signature_type (die, attr, cu);
15019
15020 /* The type's CU may not be the same as CU.
15021 Ensure TYPE is recorded with CU in die_type_hash. */
15022 return set_die_type (die, type, cu);
15023 }
15024
15025 type = alloc_type (objfile);
15026 INIT_CPLUS_SPECIFIC (type);
15027
15028 name = dwarf2_name (die, cu);
15029 if (name != NULL)
15030 {
15031 if (cu->language == language_cplus
15032 || cu->language == language_d
15033 || cu->language == language_rust)
15034 {
15035 const char *full_name = dwarf2_full_name (name, die, cu);
15036
15037 /* dwarf2_full_name might have already finished building the DIE's
15038 type. If so, there is no need to continue. */
15039 if (get_die_type (die, cu) != NULL)
15040 return get_die_type (die, cu);
15041
15042 TYPE_NAME (type) = full_name;
15043 }
15044 else
15045 {
15046 /* The name is already allocated along with this objfile, so
15047 we don't need to duplicate it for the type. */
15048 TYPE_NAME (type) = name;
15049 }
15050 }
15051
15052 if (die->tag == DW_TAG_structure_type)
15053 {
15054 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15055 }
15056 else if (die->tag == DW_TAG_union_type)
15057 {
15058 TYPE_CODE (type) = TYPE_CODE_UNION;
15059 }
15060 else if (die->tag == DW_TAG_variant_part)
15061 {
15062 TYPE_CODE (type) = TYPE_CODE_UNION;
15063 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15064 }
15065 else
15066 {
15067 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15068 }
15069
15070 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15071 TYPE_DECLARED_CLASS (type) = 1;
15072
15073 /* Store the calling convention in the type if it's available in
15074 the die. Otherwise the calling convention remains set to
15075 the default value DW_CC_normal. */
15076 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15077 if (attr != nullptr
15078 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15079 {
15080 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15081 TYPE_CPLUS_CALLING_CONVENTION (type)
15082 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15083 }
15084
15085 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15086 if (attr != nullptr)
15087 {
15088 if (attr->form_is_constant ())
15089 TYPE_LENGTH (type) = DW_UNSND (attr);
15090 else
15091 {
15092 /* For the moment, dynamic type sizes are not supported
15093 by GDB's struct type. The actual size is determined
15094 on-demand when resolving the type of a given object,
15095 so set the type's length to zero for now. Otherwise,
15096 we record an expression as the length, and that expression
15097 could lead to a very large value, which could eventually
15098 lead to us trying to allocate that much memory when creating
15099 a value of that type. */
15100 TYPE_LENGTH (type) = 0;
15101 }
15102 }
15103 else
15104 {
15105 TYPE_LENGTH (type) = 0;
15106 }
15107
15108 maybe_set_alignment (cu, die, type);
15109
15110 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15111 {
15112 /* ICC<14 does not output the required DW_AT_declaration on
15113 incomplete types, but gives them a size of zero. */
15114 TYPE_STUB (type) = 1;
15115 }
15116 else
15117 TYPE_STUB_SUPPORTED (type) = 1;
15118
15119 if (die_is_declaration (die, cu))
15120 TYPE_STUB (type) = 1;
15121 else if (attr == NULL && die->child == NULL
15122 && producer_is_realview (cu->producer))
15123 /* RealView does not output the required DW_AT_declaration
15124 on incomplete types. */
15125 TYPE_STUB (type) = 1;
15126
15127 /* We need to add the type field to the die immediately so we don't
15128 infinitely recurse when dealing with pointers to the structure
15129 type within the structure itself. */
15130 set_die_type (die, type, cu);
15131
15132 /* set_die_type should be already done. */
15133 set_descriptive_type (type, die, cu);
15134
15135 return type;
15136 }
15137
15138 /* A helper for process_structure_scope that handles a single member
15139 DIE. */
15140
15141 static void
15142 handle_struct_member_die (struct die_info *child_die, struct type *type,
15143 struct field_info *fi,
15144 std::vector<struct symbol *> *template_args,
15145 struct dwarf2_cu *cu)
15146 {
15147 if (child_die->tag == DW_TAG_member
15148 || child_die->tag == DW_TAG_variable
15149 || child_die->tag == DW_TAG_variant_part)
15150 {
15151 /* NOTE: carlton/2002-11-05: A C++ static data member
15152 should be a DW_TAG_member that is a declaration, but
15153 all versions of G++ as of this writing (so through at
15154 least 3.2.1) incorrectly generate DW_TAG_variable
15155 tags for them instead. */
15156 dwarf2_add_field (fi, child_die, cu);
15157 }
15158 else if (child_die->tag == DW_TAG_subprogram)
15159 {
15160 /* Rust doesn't have member functions in the C++ sense.
15161 However, it does emit ordinary functions as children
15162 of a struct DIE. */
15163 if (cu->language == language_rust)
15164 read_func_scope (child_die, cu);
15165 else
15166 {
15167 /* C++ member function. */
15168 dwarf2_add_member_fn (fi, child_die, type, cu);
15169 }
15170 }
15171 else if (child_die->tag == DW_TAG_inheritance)
15172 {
15173 /* C++ base class field. */
15174 dwarf2_add_field (fi, child_die, cu);
15175 }
15176 else if (type_can_define_types (child_die))
15177 dwarf2_add_type_defn (fi, child_die, cu);
15178 else if (child_die->tag == DW_TAG_template_type_param
15179 || child_die->tag == DW_TAG_template_value_param)
15180 {
15181 struct symbol *arg = new_symbol (child_die, NULL, cu);
15182
15183 if (arg != NULL)
15184 template_args->push_back (arg);
15185 }
15186 else if (child_die->tag == DW_TAG_variant)
15187 {
15188 /* In a variant we want to get the discriminant and also add a
15189 field for our sole member child. */
15190 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15191
15192 for (die_info *variant_child = child_die->child;
15193 variant_child != NULL;
15194 variant_child = sibling_die (variant_child))
15195 {
15196 if (variant_child->tag == DW_TAG_member)
15197 {
15198 handle_struct_member_die (variant_child, type, fi,
15199 template_args, cu);
15200 /* Only handle the one. */
15201 break;
15202 }
15203 }
15204
15205 /* We don't handle this but we might as well report it if we see
15206 it. */
15207 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15208 complaint (_("DW_AT_discr_list is not supported yet"
15209 " - DIE at %s [in module %s]"),
15210 sect_offset_str (child_die->sect_off),
15211 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15212
15213 /* The first field was just added, so we can stash the
15214 discriminant there. */
15215 gdb_assert (!fi->fields.empty ());
15216 if (discr == NULL)
15217 fi->fields.back ().variant.default_branch = true;
15218 else
15219 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15220 }
15221 }
15222
15223 /* Finish creating a structure or union type, including filling in
15224 its members and creating a symbol for it. */
15225
15226 static void
15227 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15228 {
15229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15230 struct die_info *child_die;
15231 struct type *type;
15232
15233 type = get_die_type (die, cu);
15234 if (type == NULL)
15235 type = read_structure_type (die, cu);
15236
15237 /* When reading a DW_TAG_variant_part, we need to notice when we
15238 read the discriminant member, so we can record it later in the
15239 discriminant_info. */
15240 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15241 sect_offset discr_offset {};
15242 bool has_template_parameters = false;
15243
15244 if (is_variant_part)
15245 {
15246 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15247 if (discr == NULL)
15248 {
15249 /* Maybe it's a univariant form, an extension we support.
15250 In this case arrange not to check the offset. */
15251 is_variant_part = false;
15252 }
15253 else if (discr->form_is_ref ())
15254 {
15255 struct dwarf2_cu *target_cu = cu;
15256 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15257
15258 discr_offset = target_die->sect_off;
15259 }
15260 else
15261 {
15262 complaint (_("DW_AT_discr does not have DIE reference form"
15263 " - DIE at %s [in module %s]"),
15264 sect_offset_str (die->sect_off),
15265 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15266 is_variant_part = false;
15267 }
15268 }
15269
15270 if (die->child != NULL && ! die_is_declaration (die, cu))
15271 {
15272 struct field_info fi;
15273 std::vector<struct symbol *> template_args;
15274
15275 child_die = die->child;
15276
15277 while (child_die && child_die->tag)
15278 {
15279 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15280
15281 if (is_variant_part && discr_offset == child_die->sect_off)
15282 fi.fields.back ().variant.is_discriminant = true;
15283
15284 child_die = sibling_die (child_die);
15285 }
15286
15287 /* Attach template arguments to type. */
15288 if (!template_args.empty ())
15289 {
15290 has_template_parameters = true;
15291 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15292 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15293 TYPE_TEMPLATE_ARGUMENTS (type)
15294 = XOBNEWVEC (&objfile->objfile_obstack,
15295 struct symbol *,
15296 TYPE_N_TEMPLATE_ARGUMENTS (type));
15297 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15298 template_args.data (),
15299 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15300 * sizeof (struct symbol *)));
15301 }
15302
15303 /* Attach fields and member functions to the type. */
15304 if (fi.nfields)
15305 dwarf2_attach_fields_to_type (&fi, type, cu);
15306 if (!fi.fnfieldlists.empty ())
15307 {
15308 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15309
15310 /* Get the type which refers to the base class (possibly this
15311 class itself) which contains the vtable pointer for the current
15312 class from the DW_AT_containing_type attribute. This use of
15313 DW_AT_containing_type is a GNU extension. */
15314
15315 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15316 {
15317 struct type *t = die_containing_type (die, cu);
15318
15319 set_type_vptr_basetype (type, t);
15320 if (type == t)
15321 {
15322 int i;
15323
15324 /* Our own class provides vtbl ptr. */
15325 for (i = TYPE_NFIELDS (t) - 1;
15326 i >= TYPE_N_BASECLASSES (t);
15327 --i)
15328 {
15329 const char *fieldname = TYPE_FIELD_NAME (t, i);
15330
15331 if (is_vtable_name (fieldname, cu))
15332 {
15333 set_type_vptr_fieldno (type, i);
15334 break;
15335 }
15336 }
15337
15338 /* Complain if virtual function table field not found. */
15339 if (i < TYPE_N_BASECLASSES (t))
15340 complaint (_("virtual function table pointer "
15341 "not found when defining class '%s'"),
15342 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15343 }
15344 else
15345 {
15346 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15347 }
15348 }
15349 else if (cu->producer
15350 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15351 {
15352 /* The IBM XLC compiler does not provide direct indication
15353 of the containing type, but the vtable pointer is
15354 always named __vfp. */
15355
15356 int i;
15357
15358 for (i = TYPE_NFIELDS (type) - 1;
15359 i >= TYPE_N_BASECLASSES (type);
15360 --i)
15361 {
15362 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15363 {
15364 set_type_vptr_fieldno (type, i);
15365 set_type_vptr_basetype (type, type);
15366 break;
15367 }
15368 }
15369 }
15370 }
15371
15372 /* Copy fi.typedef_field_list linked list elements content into the
15373 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15374 if (!fi.typedef_field_list.empty ())
15375 {
15376 int count = fi.typedef_field_list.size ();
15377
15378 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15379 TYPE_TYPEDEF_FIELD_ARRAY (type)
15380 = ((struct decl_field *)
15381 TYPE_ALLOC (type,
15382 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15383 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15384
15385 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15386 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15387 }
15388
15389 /* Copy fi.nested_types_list linked list elements content into the
15390 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15391 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15392 {
15393 int count = fi.nested_types_list.size ();
15394
15395 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15396 TYPE_NESTED_TYPES_ARRAY (type)
15397 = ((struct decl_field *)
15398 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15399 TYPE_NESTED_TYPES_COUNT (type) = count;
15400
15401 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15402 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15403 }
15404 }
15405
15406 quirk_gcc_member_function_pointer (type, objfile);
15407 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15408 cu->rust_unions.push_back (type);
15409
15410 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15411 snapshots) has been known to create a die giving a declaration
15412 for a class that has, as a child, a die giving a definition for a
15413 nested class. So we have to process our children even if the
15414 current die is a declaration. Normally, of course, a declaration
15415 won't have any children at all. */
15416
15417 child_die = die->child;
15418
15419 while (child_die != NULL && child_die->tag)
15420 {
15421 if (child_die->tag == DW_TAG_member
15422 || child_die->tag == DW_TAG_variable
15423 || child_die->tag == DW_TAG_inheritance
15424 || child_die->tag == DW_TAG_template_value_param
15425 || child_die->tag == DW_TAG_template_type_param)
15426 {
15427 /* Do nothing. */
15428 }
15429 else
15430 process_die (child_die, cu);
15431
15432 child_die = sibling_die (child_die);
15433 }
15434
15435 /* Do not consider external references. According to the DWARF standard,
15436 these DIEs are identified by the fact that they have no byte_size
15437 attribute, and a declaration attribute. */
15438 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15439 || !die_is_declaration (die, cu))
15440 {
15441 struct symbol *sym = new_symbol (die, type, cu);
15442
15443 if (has_template_parameters)
15444 {
15445 struct symtab *symtab;
15446 if (sym != nullptr)
15447 symtab = symbol_symtab (sym);
15448 else if (cu->line_header != nullptr)
15449 {
15450 /* Any related symtab will do. */
15451 symtab
15452 = cu->line_header->file_names ()[0].symtab;
15453 }
15454 else
15455 {
15456 symtab = nullptr;
15457 complaint (_("could not find suitable "
15458 "symtab for template parameter"
15459 " - DIE at %s [in module %s]"),
15460 sect_offset_str (die->sect_off),
15461 objfile_name (objfile));
15462 }
15463
15464 if (symtab != nullptr)
15465 {
15466 /* Make sure that the symtab is set on the new symbols.
15467 Even though they don't appear in this symtab directly,
15468 other parts of gdb assume that symbols do, and this is
15469 reasonably true. */
15470 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15471 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15472 }
15473 }
15474 }
15475 }
15476
15477 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15478 update TYPE using some information only available in DIE's children. */
15479
15480 static void
15481 update_enumeration_type_from_children (struct die_info *die,
15482 struct type *type,
15483 struct dwarf2_cu *cu)
15484 {
15485 struct die_info *child_die;
15486 int unsigned_enum = 1;
15487 int flag_enum = 1;
15488 ULONGEST mask = 0;
15489
15490 auto_obstack obstack;
15491
15492 for (child_die = die->child;
15493 child_die != NULL && child_die->tag;
15494 child_die = sibling_die (child_die))
15495 {
15496 struct attribute *attr;
15497 LONGEST value;
15498 const gdb_byte *bytes;
15499 struct dwarf2_locexpr_baton *baton;
15500 const char *name;
15501
15502 if (child_die->tag != DW_TAG_enumerator)
15503 continue;
15504
15505 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15506 if (attr == NULL)
15507 continue;
15508
15509 name = dwarf2_name (child_die, cu);
15510 if (name == NULL)
15511 name = "<anonymous enumerator>";
15512
15513 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15514 &value, &bytes, &baton);
15515 if (value < 0)
15516 {
15517 unsigned_enum = 0;
15518 flag_enum = 0;
15519 }
15520 else if ((mask & value) != 0)
15521 flag_enum = 0;
15522 else
15523 mask |= value;
15524
15525 /* If we already know that the enum type is neither unsigned, nor
15526 a flag type, no need to look at the rest of the enumerates. */
15527 if (!unsigned_enum && !flag_enum)
15528 break;
15529 }
15530
15531 if (unsigned_enum)
15532 TYPE_UNSIGNED (type) = 1;
15533 if (flag_enum)
15534 TYPE_FLAG_ENUM (type) = 1;
15535 }
15536
15537 /* Given a DW_AT_enumeration_type die, set its type. We do not
15538 complete the type's fields yet, or create any symbols. */
15539
15540 static struct type *
15541 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15542 {
15543 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15544 struct type *type;
15545 struct attribute *attr;
15546 const char *name;
15547
15548 /* If the definition of this type lives in .debug_types, read that type.
15549 Don't follow DW_AT_specification though, that will take us back up
15550 the chain and we want to go down. */
15551 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15552 if (attr != nullptr)
15553 {
15554 type = get_DW_AT_signature_type (die, attr, cu);
15555
15556 /* The type's CU may not be the same as CU.
15557 Ensure TYPE is recorded with CU in die_type_hash. */
15558 return set_die_type (die, type, cu);
15559 }
15560
15561 type = alloc_type (objfile);
15562
15563 TYPE_CODE (type) = TYPE_CODE_ENUM;
15564 name = dwarf2_full_name (NULL, die, cu);
15565 if (name != NULL)
15566 TYPE_NAME (type) = name;
15567
15568 attr = dwarf2_attr (die, DW_AT_type, cu);
15569 if (attr != NULL)
15570 {
15571 struct type *underlying_type = die_type (die, cu);
15572
15573 TYPE_TARGET_TYPE (type) = underlying_type;
15574 }
15575
15576 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15577 if (attr != nullptr)
15578 {
15579 TYPE_LENGTH (type) = DW_UNSND (attr);
15580 }
15581 else
15582 {
15583 TYPE_LENGTH (type) = 0;
15584 }
15585
15586 maybe_set_alignment (cu, die, type);
15587
15588 /* The enumeration DIE can be incomplete. In Ada, any type can be
15589 declared as private in the package spec, and then defined only
15590 inside the package body. Such types are known as Taft Amendment
15591 Types. When another package uses such a type, an incomplete DIE
15592 may be generated by the compiler. */
15593 if (die_is_declaration (die, cu))
15594 TYPE_STUB (type) = 1;
15595
15596 /* Finish the creation of this type by using the enum's children.
15597 We must call this even when the underlying type has been provided
15598 so that we can determine if we're looking at a "flag" enum. */
15599 update_enumeration_type_from_children (die, type, cu);
15600
15601 /* If this type has an underlying type that is not a stub, then we
15602 may use its attributes. We always use the "unsigned" attribute
15603 in this situation, because ordinarily we guess whether the type
15604 is unsigned -- but the guess can be wrong and the underlying type
15605 can tell us the reality. However, we defer to a local size
15606 attribute if one exists, because this lets the compiler override
15607 the underlying type if needed. */
15608 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15609 {
15610 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15611 if (TYPE_LENGTH (type) == 0)
15612 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15613 if (TYPE_RAW_ALIGN (type) == 0
15614 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15615 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15616 }
15617
15618 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15619
15620 return set_die_type (die, type, cu);
15621 }
15622
15623 /* Given a pointer to a die which begins an enumeration, process all
15624 the dies that define the members of the enumeration, and create the
15625 symbol for the enumeration type.
15626
15627 NOTE: We reverse the order of the element list. */
15628
15629 static void
15630 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15631 {
15632 struct type *this_type;
15633
15634 this_type = get_die_type (die, cu);
15635 if (this_type == NULL)
15636 this_type = read_enumeration_type (die, cu);
15637
15638 if (die->child != NULL)
15639 {
15640 struct die_info *child_die;
15641 struct symbol *sym;
15642 std::vector<struct field> fields;
15643 const char *name;
15644
15645 child_die = die->child;
15646 while (child_die && child_die->tag)
15647 {
15648 if (child_die->tag != DW_TAG_enumerator)
15649 {
15650 process_die (child_die, cu);
15651 }
15652 else
15653 {
15654 name = dwarf2_name (child_die, cu);
15655 if (name)
15656 {
15657 sym = new_symbol (child_die, this_type, cu);
15658
15659 fields.emplace_back ();
15660 struct field &field = fields.back ();
15661
15662 FIELD_NAME (field) = sym->linkage_name ();
15663 FIELD_TYPE (field) = NULL;
15664 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15665 FIELD_BITSIZE (field) = 0;
15666 }
15667 }
15668
15669 child_die = sibling_die (child_die);
15670 }
15671
15672 if (!fields.empty ())
15673 {
15674 TYPE_NFIELDS (this_type) = fields.size ();
15675 TYPE_FIELDS (this_type) = (struct field *)
15676 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15677 memcpy (TYPE_FIELDS (this_type), fields.data (),
15678 sizeof (struct field) * fields.size ());
15679 }
15680 }
15681
15682 /* If we are reading an enum from a .debug_types unit, and the enum
15683 is a declaration, and the enum is not the signatured type in the
15684 unit, then we do not want to add a symbol for it. Adding a
15685 symbol would in some cases obscure the true definition of the
15686 enum, giving users an incomplete type when the definition is
15687 actually available. Note that we do not want to do this for all
15688 enums which are just declarations, because C++0x allows forward
15689 enum declarations. */
15690 if (cu->per_cu->is_debug_types
15691 && die_is_declaration (die, cu))
15692 {
15693 struct signatured_type *sig_type;
15694
15695 sig_type = (struct signatured_type *) cu->per_cu;
15696 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15697 if (sig_type->type_offset_in_section != die->sect_off)
15698 return;
15699 }
15700
15701 new_symbol (die, this_type, cu);
15702 }
15703
15704 /* Extract all information from a DW_TAG_array_type DIE and put it in
15705 the DIE's type field. For now, this only handles one dimensional
15706 arrays. */
15707
15708 static struct type *
15709 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15710 {
15711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15712 struct die_info *child_die;
15713 struct type *type;
15714 struct type *element_type, *range_type, *index_type;
15715 struct attribute *attr;
15716 const char *name;
15717 struct dynamic_prop *byte_stride_prop = NULL;
15718 unsigned int bit_stride = 0;
15719
15720 element_type = die_type (die, cu);
15721
15722 /* The die_type call above may have already set the type for this DIE. */
15723 type = get_die_type (die, cu);
15724 if (type)
15725 return type;
15726
15727 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15728 if (attr != NULL)
15729 {
15730 int stride_ok;
15731 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15732
15733 byte_stride_prop
15734 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15735 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15736 prop_type);
15737 if (!stride_ok)
15738 {
15739 complaint (_("unable to read array DW_AT_byte_stride "
15740 " - DIE at %s [in module %s]"),
15741 sect_offset_str (die->sect_off),
15742 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15743 /* Ignore this attribute. We will likely not be able to print
15744 arrays of this type correctly, but there is little we can do
15745 to help if we cannot read the attribute's value. */
15746 byte_stride_prop = NULL;
15747 }
15748 }
15749
15750 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15751 if (attr != NULL)
15752 bit_stride = DW_UNSND (attr);
15753
15754 /* Irix 6.2 native cc creates array types without children for
15755 arrays with unspecified length. */
15756 if (die->child == NULL)
15757 {
15758 index_type = objfile_type (objfile)->builtin_int;
15759 range_type = create_static_range_type (NULL, index_type, 0, -1);
15760 type = create_array_type_with_stride (NULL, element_type, range_type,
15761 byte_stride_prop, bit_stride);
15762 return set_die_type (die, type, cu);
15763 }
15764
15765 std::vector<struct type *> range_types;
15766 child_die = die->child;
15767 while (child_die && child_die->tag)
15768 {
15769 if (child_die->tag == DW_TAG_subrange_type)
15770 {
15771 struct type *child_type = read_type_die (child_die, cu);
15772
15773 if (child_type != NULL)
15774 {
15775 /* The range type was succesfully read. Save it for the
15776 array type creation. */
15777 range_types.push_back (child_type);
15778 }
15779 }
15780 child_die = sibling_die (child_die);
15781 }
15782
15783 /* Dwarf2 dimensions are output from left to right, create the
15784 necessary array types in backwards order. */
15785
15786 type = element_type;
15787
15788 if (read_array_order (die, cu) == DW_ORD_col_major)
15789 {
15790 int i = 0;
15791
15792 while (i < range_types.size ())
15793 type = create_array_type_with_stride (NULL, type, range_types[i++],
15794 byte_stride_prop, bit_stride);
15795 }
15796 else
15797 {
15798 size_t ndim = range_types.size ();
15799 while (ndim-- > 0)
15800 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15801 byte_stride_prop, bit_stride);
15802 }
15803
15804 /* Understand Dwarf2 support for vector types (like they occur on
15805 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15806 array type. This is not part of the Dwarf2/3 standard yet, but a
15807 custom vendor extension. The main difference between a regular
15808 array and the vector variant is that vectors are passed by value
15809 to functions. */
15810 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15811 if (attr != nullptr)
15812 make_vector_type (type);
15813
15814 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15815 implementation may choose to implement triple vectors using this
15816 attribute. */
15817 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15818 if (attr != nullptr)
15819 {
15820 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15821 TYPE_LENGTH (type) = DW_UNSND (attr);
15822 else
15823 complaint (_("DW_AT_byte_size for array type smaller "
15824 "than the total size of elements"));
15825 }
15826
15827 name = dwarf2_name (die, cu);
15828 if (name)
15829 TYPE_NAME (type) = name;
15830
15831 maybe_set_alignment (cu, die, type);
15832
15833 /* Install the type in the die. */
15834 set_die_type (die, type, cu);
15835
15836 /* set_die_type should be already done. */
15837 set_descriptive_type (type, die, cu);
15838
15839 return type;
15840 }
15841
15842 static enum dwarf_array_dim_ordering
15843 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15844 {
15845 struct attribute *attr;
15846
15847 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15848
15849 if (attr != nullptr)
15850 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15851
15852 /* GNU F77 is a special case, as at 08/2004 array type info is the
15853 opposite order to the dwarf2 specification, but data is still
15854 laid out as per normal fortran.
15855
15856 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15857 version checking. */
15858
15859 if (cu->language == language_fortran
15860 && cu->producer && strstr (cu->producer, "GNU F77"))
15861 {
15862 return DW_ORD_row_major;
15863 }
15864
15865 switch (cu->language_defn->la_array_ordering)
15866 {
15867 case array_column_major:
15868 return DW_ORD_col_major;
15869 case array_row_major:
15870 default:
15871 return DW_ORD_row_major;
15872 };
15873 }
15874
15875 /* Extract all information from a DW_TAG_set_type DIE and put it in
15876 the DIE's type field. */
15877
15878 static struct type *
15879 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15880 {
15881 struct type *domain_type, *set_type;
15882 struct attribute *attr;
15883
15884 domain_type = die_type (die, cu);
15885
15886 /* The die_type call above may have already set the type for this DIE. */
15887 set_type = get_die_type (die, cu);
15888 if (set_type)
15889 return set_type;
15890
15891 set_type = create_set_type (NULL, domain_type);
15892
15893 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15894 if (attr != nullptr)
15895 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15896
15897 maybe_set_alignment (cu, die, set_type);
15898
15899 return set_die_type (die, set_type, cu);
15900 }
15901
15902 /* A helper for read_common_block that creates a locexpr baton.
15903 SYM is the symbol which we are marking as computed.
15904 COMMON_DIE is the DIE for the common block.
15905 COMMON_LOC is the location expression attribute for the common
15906 block itself.
15907 MEMBER_LOC is the location expression attribute for the particular
15908 member of the common block that we are processing.
15909 CU is the CU from which the above come. */
15910
15911 static void
15912 mark_common_block_symbol_computed (struct symbol *sym,
15913 struct die_info *common_die,
15914 struct attribute *common_loc,
15915 struct attribute *member_loc,
15916 struct dwarf2_cu *cu)
15917 {
15918 struct dwarf2_per_objfile *dwarf2_per_objfile
15919 = cu->per_cu->dwarf2_per_objfile;
15920 struct objfile *objfile = dwarf2_per_objfile->objfile;
15921 struct dwarf2_locexpr_baton *baton;
15922 gdb_byte *ptr;
15923 unsigned int cu_off;
15924 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15925 LONGEST offset = 0;
15926
15927 gdb_assert (common_loc && member_loc);
15928 gdb_assert (common_loc->form_is_block ());
15929 gdb_assert (member_loc->form_is_block ()
15930 || member_loc->form_is_constant ());
15931
15932 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15933 baton->per_cu = cu->per_cu;
15934 gdb_assert (baton->per_cu);
15935
15936 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15937
15938 if (member_loc->form_is_constant ())
15939 {
15940 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15941 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15942 }
15943 else
15944 baton->size += DW_BLOCK (member_loc)->size;
15945
15946 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15947 baton->data = ptr;
15948
15949 *ptr++ = DW_OP_call4;
15950 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15951 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15952 ptr += 4;
15953
15954 if (member_loc->form_is_constant ())
15955 {
15956 *ptr++ = DW_OP_addr;
15957 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15958 ptr += cu->header.addr_size;
15959 }
15960 else
15961 {
15962 /* We have to copy the data here, because DW_OP_call4 will only
15963 use a DW_AT_location attribute. */
15964 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15965 ptr += DW_BLOCK (member_loc)->size;
15966 }
15967
15968 *ptr++ = DW_OP_plus;
15969 gdb_assert (ptr - baton->data == baton->size);
15970
15971 SYMBOL_LOCATION_BATON (sym) = baton;
15972 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15973 }
15974
15975 /* Create appropriate locally-scoped variables for all the
15976 DW_TAG_common_block entries. Also create a struct common_block
15977 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15978 is used to separate the common blocks name namespace from regular
15979 variable names. */
15980
15981 static void
15982 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15983 {
15984 struct attribute *attr;
15985
15986 attr = dwarf2_attr (die, DW_AT_location, cu);
15987 if (attr != nullptr)
15988 {
15989 /* Support the .debug_loc offsets. */
15990 if (attr->form_is_block ())
15991 {
15992 /* Ok. */
15993 }
15994 else if (attr->form_is_section_offset ())
15995 {
15996 dwarf2_complex_location_expr_complaint ();
15997 attr = NULL;
15998 }
15999 else
16000 {
16001 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16002 "common block member");
16003 attr = NULL;
16004 }
16005 }
16006
16007 if (die->child != NULL)
16008 {
16009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16010 struct die_info *child_die;
16011 size_t n_entries = 0, size;
16012 struct common_block *common_block;
16013 struct symbol *sym;
16014
16015 for (child_die = die->child;
16016 child_die && child_die->tag;
16017 child_die = sibling_die (child_die))
16018 ++n_entries;
16019
16020 size = (sizeof (struct common_block)
16021 + (n_entries - 1) * sizeof (struct symbol *));
16022 common_block
16023 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16024 size);
16025 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16026 common_block->n_entries = 0;
16027
16028 for (child_die = die->child;
16029 child_die && child_die->tag;
16030 child_die = sibling_die (child_die))
16031 {
16032 /* Create the symbol in the DW_TAG_common_block block in the current
16033 symbol scope. */
16034 sym = new_symbol (child_die, NULL, cu);
16035 if (sym != NULL)
16036 {
16037 struct attribute *member_loc;
16038
16039 common_block->contents[common_block->n_entries++] = sym;
16040
16041 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16042 cu);
16043 if (member_loc)
16044 {
16045 /* GDB has handled this for a long time, but it is
16046 not specified by DWARF. It seems to have been
16047 emitted by gfortran at least as recently as:
16048 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16049 complaint (_("Variable in common block has "
16050 "DW_AT_data_member_location "
16051 "- DIE at %s [in module %s]"),
16052 sect_offset_str (child_die->sect_off),
16053 objfile_name (objfile));
16054
16055 if (member_loc->form_is_section_offset ())
16056 dwarf2_complex_location_expr_complaint ();
16057 else if (member_loc->form_is_constant ()
16058 || member_loc->form_is_block ())
16059 {
16060 if (attr != nullptr)
16061 mark_common_block_symbol_computed (sym, die, attr,
16062 member_loc, cu);
16063 }
16064 else
16065 dwarf2_complex_location_expr_complaint ();
16066 }
16067 }
16068 }
16069
16070 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16071 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16072 }
16073 }
16074
16075 /* Create a type for a C++ namespace. */
16076
16077 static struct type *
16078 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16079 {
16080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16081 const char *previous_prefix, *name;
16082 int is_anonymous;
16083 struct type *type;
16084
16085 /* For extensions, reuse the type of the original namespace. */
16086 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16087 {
16088 struct die_info *ext_die;
16089 struct dwarf2_cu *ext_cu = cu;
16090
16091 ext_die = dwarf2_extension (die, &ext_cu);
16092 type = read_type_die (ext_die, ext_cu);
16093
16094 /* EXT_CU may not be the same as CU.
16095 Ensure TYPE is recorded with CU in die_type_hash. */
16096 return set_die_type (die, type, cu);
16097 }
16098
16099 name = namespace_name (die, &is_anonymous, cu);
16100
16101 /* Now build the name of the current namespace. */
16102
16103 previous_prefix = determine_prefix (die, cu);
16104 if (previous_prefix[0] != '\0')
16105 name = typename_concat (&objfile->objfile_obstack,
16106 previous_prefix, name, 0, cu);
16107
16108 /* Create the type. */
16109 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16110
16111 return set_die_type (die, type, cu);
16112 }
16113
16114 /* Read a namespace scope. */
16115
16116 static void
16117 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16118 {
16119 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16120 int is_anonymous;
16121
16122 /* Add a symbol associated to this if we haven't seen the namespace
16123 before. Also, add a using directive if it's an anonymous
16124 namespace. */
16125
16126 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16127 {
16128 struct type *type;
16129
16130 type = read_type_die (die, cu);
16131 new_symbol (die, type, cu);
16132
16133 namespace_name (die, &is_anonymous, cu);
16134 if (is_anonymous)
16135 {
16136 const char *previous_prefix = determine_prefix (die, cu);
16137
16138 std::vector<const char *> excludes;
16139 add_using_directive (using_directives (cu),
16140 previous_prefix, TYPE_NAME (type), NULL,
16141 NULL, excludes, 0, &objfile->objfile_obstack);
16142 }
16143 }
16144
16145 if (die->child != NULL)
16146 {
16147 struct die_info *child_die = die->child;
16148
16149 while (child_die && child_die->tag)
16150 {
16151 process_die (child_die, cu);
16152 child_die = sibling_die (child_die);
16153 }
16154 }
16155 }
16156
16157 /* Read a Fortran module as type. This DIE can be only a declaration used for
16158 imported module. Still we need that type as local Fortran "use ... only"
16159 declaration imports depend on the created type in determine_prefix. */
16160
16161 static struct type *
16162 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16163 {
16164 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16165 const char *module_name;
16166 struct type *type;
16167
16168 module_name = dwarf2_name (die, cu);
16169 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16170
16171 return set_die_type (die, type, cu);
16172 }
16173
16174 /* Read a Fortran module. */
16175
16176 static void
16177 read_module (struct die_info *die, struct dwarf2_cu *cu)
16178 {
16179 struct die_info *child_die = die->child;
16180 struct type *type;
16181
16182 type = read_type_die (die, cu);
16183 new_symbol (die, type, cu);
16184
16185 while (child_die && child_die->tag)
16186 {
16187 process_die (child_die, cu);
16188 child_die = sibling_die (child_die);
16189 }
16190 }
16191
16192 /* Return the name of the namespace represented by DIE. Set
16193 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16194 namespace. */
16195
16196 static const char *
16197 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16198 {
16199 struct die_info *current_die;
16200 const char *name = NULL;
16201
16202 /* Loop through the extensions until we find a name. */
16203
16204 for (current_die = die;
16205 current_die != NULL;
16206 current_die = dwarf2_extension (die, &cu))
16207 {
16208 /* We don't use dwarf2_name here so that we can detect the absence
16209 of a name -> anonymous namespace. */
16210 name = dwarf2_string_attr (die, DW_AT_name, cu);
16211
16212 if (name != NULL)
16213 break;
16214 }
16215
16216 /* Is it an anonymous namespace? */
16217
16218 *is_anonymous = (name == NULL);
16219 if (*is_anonymous)
16220 name = CP_ANONYMOUS_NAMESPACE_STR;
16221
16222 return name;
16223 }
16224
16225 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16226 the user defined type vector. */
16227
16228 static struct type *
16229 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16230 {
16231 struct gdbarch *gdbarch
16232 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16233 struct comp_unit_head *cu_header = &cu->header;
16234 struct type *type;
16235 struct attribute *attr_byte_size;
16236 struct attribute *attr_address_class;
16237 int byte_size, addr_class;
16238 struct type *target_type;
16239
16240 target_type = die_type (die, cu);
16241
16242 /* The die_type call above may have already set the type for this DIE. */
16243 type = get_die_type (die, cu);
16244 if (type)
16245 return type;
16246
16247 type = lookup_pointer_type (target_type);
16248
16249 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16250 if (attr_byte_size)
16251 byte_size = DW_UNSND (attr_byte_size);
16252 else
16253 byte_size = cu_header->addr_size;
16254
16255 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16256 if (attr_address_class)
16257 addr_class = DW_UNSND (attr_address_class);
16258 else
16259 addr_class = DW_ADDR_none;
16260
16261 ULONGEST alignment = get_alignment (cu, die);
16262
16263 /* If the pointer size, alignment, or address class is different
16264 than the default, create a type variant marked as such and set
16265 the length accordingly. */
16266 if (TYPE_LENGTH (type) != byte_size
16267 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16268 && alignment != TYPE_RAW_ALIGN (type))
16269 || addr_class != DW_ADDR_none)
16270 {
16271 if (gdbarch_address_class_type_flags_p (gdbarch))
16272 {
16273 int type_flags;
16274
16275 type_flags = gdbarch_address_class_type_flags
16276 (gdbarch, byte_size, addr_class);
16277 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16278 == 0);
16279 type = make_type_with_address_space (type, type_flags);
16280 }
16281 else if (TYPE_LENGTH (type) != byte_size)
16282 {
16283 complaint (_("invalid pointer size %d"), byte_size);
16284 }
16285 else if (TYPE_RAW_ALIGN (type) != alignment)
16286 {
16287 complaint (_("Invalid DW_AT_alignment"
16288 " - DIE at %s [in module %s]"),
16289 sect_offset_str (die->sect_off),
16290 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16291 }
16292 else
16293 {
16294 /* Should we also complain about unhandled address classes? */
16295 }
16296 }
16297
16298 TYPE_LENGTH (type) = byte_size;
16299 set_type_align (type, alignment);
16300 return set_die_type (die, type, cu);
16301 }
16302
16303 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16304 the user defined type vector. */
16305
16306 static struct type *
16307 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16308 {
16309 struct type *type;
16310 struct type *to_type;
16311 struct type *domain;
16312
16313 to_type = die_type (die, cu);
16314 domain = die_containing_type (die, cu);
16315
16316 /* The calls above may have already set the type for this DIE. */
16317 type = get_die_type (die, cu);
16318 if (type)
16319 return type;
16320
16321 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16322 type = lookup_methodptr_type (to_type);
16323 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16324 {
16325 struct type *new_type
16326 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16327
16328 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16329 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16330 TYPE_VARARGS (to_type));
16331 type = lookup_methodptr_type (new_type);
16332 }
16333 else
16334 type = lookup_memberptr_type (to_type, domain);
16335
16336 return set_die_type (die, type, cu);
16337 }
16338
16339 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16340 the user defined type vector. */
16341
16342 static struct type *
16343 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16344 enum type_code refcode)
16345 {
16346 struct comp_unit_head *cu_header = &cu->header;
16347 struct type *type, *target_type;
16348 struct attribute *attr;
16349
16350 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16351
16352 target_type = die_type (die, cu);
16353
16354 /* The die_type call above may have already set the type for this DIE. */
16355 type = get_die_type (die, cu);
16356 if (type)
16357 return type;
16358
16359 type = lookup_reference_type (target_type, refcode);
16360 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16361 if (attr != nullptr)
16362 {
16363 TYPE_LENGTH (type) = DW_UNSND (attr);
16364 }
16365 else
16366 {
16367 TYPE_LENGTH (type) = cu_header->addr_size;
16368 }
16369 maybe_set_alignment (cu, die, type);
16370 return set_die_type (die, type, cu);
16371 }
16372
16373 /* Add the given cv-qualifiers to the element type of the array. GCC
16374 outputs DWARF type qualifiers that apply to an array, not the
16375 element type. But GDB relies on the array element type to carry
16376 the cv-qualifiers. This mimics section 6.7.3 of the C99
16377 specification. */
16378
16379 static struct type *
16380 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16381 struct type *base_type, int cnst, int voltl)
16382 {
16383 struct type *el_type, *inner_array;
16384
16385 base_type = copy_type (base_type);
16386 inner_array = base_type;
16387
16388 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16389 {
16390 TYPE_TARGET_TYPE (inner_array) =
16391 copy_type (TYPE_TARGET_TYPE (inner_array));
16392 inner_array = TYPE_TARGET_TYPE (inner_array);
16393 }
16394
16395 el_type = TYPE_TARGET_TYPE (inner_array);
16396 cnst |= TYPE_CONST (el_type);
16397 voltl |= TYPE_VOLATILE (el_type);
16398 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16399
16400 return set_die_type (die, base_type, cu);
16401 }
16402
16403 static struct type *
16404 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16405 {
16406 struct type *base_type, *cv_type;
16407
16408 base_type = die_type (die, cu);
16409
16410 /* The die_type call above may have already set the type for this DIE. */
16411 cv_type = get_die_type (die, cu);
16412 if (cv_type)
16413 return cv_type;
16414
16415 /* In case the const qualifier is applied to an array type, the element type
16416 is so qualified, not the array type (section 6.7.3 of C99). */
16417 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16418 return add_array_cv_type (die, cu, base_type, 1, 0);
16419
16420 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16421 return set_die_type (die, cv_type, cu);
16422 }
16423
16424 static struct type *
16425 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16426 {
16427 struct type *base_type, *cv_type;
16428
16429 base_type = die_type (die, cu);
16430
16431 /* The die_type call above may have already set the type for this DIE. */
16432 cv_type = get_die_type (die, cu);
16433 if (cv_type)
16434 return cv_type;
16435
16436 /* In case the volatile qualifier is applied to an array type, the
16437 element type is so qualified, not the array type (section 6.7.3
16438 of C99). */
16439 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16440 return add_array_cv_type (die, cu, base_type, 0, 1);
16441
16442 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16443 return set_die_type (die, cv_type, cu);
16444 }
16445
16446 /* Handle DW_TAG_restrict_type. */
16447
16448 static struct type *
16449 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16450 {
16451 struct type *base_type, *cv_type;
16452
16453 base_type = die_type (die, cu);
16454
16455 /* The die_type call above may have already set the type for this DIE. */
16456 cv_type = get_die_type (die, cu);
16457 if (cv_type)
16458 return cv_type;
16459
16460 cv_type = make_restrict_type (base_type);
16461 return set_die_type (die, cv_type, cu);
16462 }
16463
16464 /* Handle DW_TAG_atomic_type. */
16465
16466 static struct type *
16467 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16468 {
16469 struct type *base_type, *cv_type;
16470
16471 base_type = die_type (die, cu);
16472
16473 /* The die_type call above may have already set the type for this DIE. */
16474 cv_type = get_die_type (die, cu);
16475 if (cv_type)
16476 return cv_type;
16477
16478 cv_type = make_atomic_type (base_type);
16479 return set_die_type (die, cv_type, cu);
16480 }
16481
16482 /* Extract all information from a DW_TAG_string_type DIE and add to
16483 the user defined type vector. It isn't really a user defined type,
16484 but it behaves like one, with other DIE's using an AT_user_def_type
16485 attribute to reference it. */
16486
16487 static struct type *
16488 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16489 {
16490 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16491 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16492 struct type *type, *range_type, *index_type, *char_type;
16493 struct attribute *attr;
16494 struct dynamic_prop prop;
16495 bool length_is_constant = true;
16496 LONGEST length;
16497
16498 /* There are a couple of places where bit sizes might be made use of
16499 when parsing a DW_TAG_string_type, however, no producer that we know
16500 of make use of these. Handling bit sizes that are a multiple of the
16501 byte size is easy enough, but what about other bit sizes? Lets deal
16502 with that problem when we have to. Warn about these attributes being
16503 unsupported, then parse the type and ignore them like we always
16504 have. */
16505 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16506 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16507 {
16508 static bool warning_printed = false;
16509 if (!warning_printed)
16510 {
16511 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16512 "currently supported on DW_TAG_string_type."));
16513 warning_printed = true;
16514 }
16515 }
16516
16517 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16518 if (attr != nullptr && !attr->form_is_constant ())
16519 {
16520 /* The string length describes the location at which the length of
16521 the string can be found. The size of the length field can be
16522 specified with one of the attributes below. */
16523 struct type *prop_type;
16524 struct attribute *len
16525 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16526 if (len == nullptr)
16527 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16528 if (len != nullptr && len->form_is_constant ())
16529 {
16530 /* Pass 0 as the default as we know this attribute is constant
16531 and the default value will not be returned. */
16532 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16533 prop_type = cu->per_cu->int_type (sz, true);
16534 }
16535 else
16536 {
16537 /* If the size is not specified then we assume it is the size of
16538 an address on this target. */
16539 prop_type = cu->per_cu->addr_sized_int_type (true);
16540 }
16541
16542 /* Convert the attribute into a dynamic property. */
16543 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16544 length = 1;
16545 else
16546 length_is_constant = false;
16547 }
16548 else if (attr != nullptr)
16549 {
16550 /* This DW_AT_string_length just contains the length with no
16551 indirection. There's no need to create a dynamic property in this
16552 case. Pass 0 for the default value as we know it will not be
16553 returned in this case. */
16554 length = dwarf2_get_attr_constant_value (attr, 0);
16555 }
16556 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16557 {
16558 /* We don't currently support non-constant byte sizes for strings. */
16559 length = dwarf2_get_attr_constant_value (attr, 1);
16560 }
16561 else
16562 {
16563 /* Use 1 as a fallback length if we have nothing else. */
16564 length = 1;
16565 }
16566
16567 index_type = objfile_type (objfile)->builtin_int;
16568 if (length_is_constant)
16569 range_type = create_static_range_type (NULL, index_type, 1, length);
16570 else
16571 {
16572 struct dynamic_prop low_bound;
16573
16574 low_bound.kind = PROP_CONST;
16575 low_bound.data.const_val = 1;
16576 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16577 }
16578 char_type = language_string_char_type (cu->language_defn, gdbarch);
16579 type = create_string_type (NULL, char_type, range_type);
16580
16581 return set_die_type (die, type, cu);
16582 }
16583
16584 /* Assuming that DIE corresponds to a function, returns nonzero
16585 if the function is prototyped. */
16586
16587 static int
16588 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16589 {
16590 struct attribute *attr;
16591
16592 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16593 if (attr && (DW_UNSND (attr) != 0))
16594 return 1;
16595
16596 /* The DWARF standard implies that the DW_AT_prototyped attribute
16597 is only meaningful for C, but the concept also extends to other
16598 languages that allow unprototyped functions (Eg: Objective C).
16599 For all other languages, assume that functions are always
16600 prototyped. */
16601 if (cu->language != language_c
16602 && cu->language != language_objc
16603 && cu->language != language_opencl)
16604 return 1;
16605
16606 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16607 prototyped and unprototyped functions; default to prototyped,
16608 since that is more common in modern code (and RealView warns
16609 about unprototyped functions). */
16610 if (producer_is_realview (cu->producer))
16611 return 1;
16612
16613 return 0;
16614 }
16615
16616 /* Handle DIES due to C code like:
16617
16618 struct foo
16619 {
16620 int (*funcp)(int a, long l);
16621 int b;
16622 };
16623
16624 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16625
16626 static struct type *
16627 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16628 {
16629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16630 struct type *type; /* Type that this function returns. */
16631 struct type *ftype; /* Function that returns above type. */
16632 struct attribute *attr;
16633
16634 type = die_type (die, cu);
16635
16636 /* The die_type call above may have already set the type for this DIE. */
16637 ftype = get_die_type (die, cu);
16638 if (ftype)
16639 return ftype;
16640
16641 ftype = lookup_function_type (type);
16642
16643 if (prototyped_function_p (die, cu))
16644 TYPE_PROTOTYPED (ftype) = 1;
16645
16646 /* Store the calling convention in the type if it's available in
16647 the subroutine die. Otherwise set the calling convention to
16648 the default value DW_CC_normal. */
16649 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16650 if (attr != nullptr
16651 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16652 TYPE_CALLING_CONVENTION (ftype)
16653 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16654 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16655 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16656 else
16657 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16658
16659 /* Record whether the function returns normally to its caller or not
16660 if the DWARF producer set that information. */
16661 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16662 if (attr && (DW_UNSND (attr) != 0))
16663 TYPE_NO_RETURN (ftype) = 1;
16664
16665 /* We need to add the subroutine type to the die immediately so
16666 we don't infinitely recurse when dealing with parameters
16667 declared as the same subroutine type. */
16668 set_die_type (die, ftype, cu);
16669
16670 if (die->child != NULL)
16671 {
16672 struct type *void_type = objfile_type (objfile)->builtin_void;
16673 struct die_info *child_die;
16674 int nparams, iparams;
16675
16676 /* Count the number of parameters.
16677 FIXME: GDB currently ignores vararg functions, but knows about
16678 vararg member functions. */
16679 nparams = 0;
16680 child_die = die->child;
16681 while (child_die && child_die->tag)
16682 {
16683 if (child_die->tag == DW_TAG_formal_parameter)
16684 nparams++;
16685 else if (child_die->tag == DW_TAG_unspecified_parameters)
16686 TYPE_VARARGS (ftype) = 1;
16687 child_die = sibling_die (child_die);
16688 }
16689
16690 /* Allocate storage for parameters and fill them in. */
16691 TYPE_NFIELDS (ftype) = nparams;
16692 TYPE_FIELDS (ftype) = (struct field *)
16693 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16694
16695 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16696 even if we error out during the parameters reading below. */
16697 for (iparams = 0; iparams < nparams; iparams++)
16698 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16699
16700 iparams = 0;
16701 child_die = die->child;
16702 while (child_die && child_die->tag)
16703 {
16704 if (child_die->tag == DW_TAG_formal_parameter)
16705 {
16706 struct type *arg_type;
16707
16708 /* DWARF version 2 has no clean way to discern C++
16709 static and non-static member functions. G++ helps
16710 GDB by marking the first parameter for non-static
16711 member functions (which is the this pointer) as
16712 artificial. We pass this information to
16713 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16714
16715 DWARF version 3 added DW_AT_object_pointer, which GCC
16716 4.5 does not yet generate. */
16717 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16718 if (attr != nullptr)
16719 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16720 else
16721 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16722 arg_type = die_type (child_die, cu);
16723
16724 /* RealView does not mark THIS as const, which the testsuite
16725 expects. GCC marks THIS as const in method definitions,
16726 but not in the class specifications (GCC PR 43053). */
16727 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16728 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16729 {
16730 int is_this = 0;
16731 struct dwarf2_cu *arg_cu = cu;
16732 const char *name = dwarf2_name (child_die, cu);
16733
16734 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16735 if (attr != nullptr)
16736 {
16737 /* If the compiler emits this, use it. */
16738 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16739 is_this = 1;
16740 }
16741 else if (name && strcmp (name, "this") == 0)
16742 /* Function definitions will have the argument names. */
16743 is_this = 1;
16744 else if (name == NULL && iparams == 0)
16745 /* Declarations may not have the names, so like
16746 elsewhere in GDB, assume an artificial first
16747 argument is "this". */
16748 is_this = 1;
16749
16750 if (is_this)
16751 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16752 arg_type, 0);
16753 }
16754
16755 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16756 iparams++;
16757 }
16758 child_die = sibling_die (child_die);
16759 }
16760 }
16761
16762 return ftype;
16763 }
16764
16765 static struct type *
16766 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16767 {
16768 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16769 const char *name = NULL;
16770 struct type *this_type, *target_type;
16771
16772 name = dwarf2_full_name (NULL, die, cu);
16773 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16774 TYPE_TARGET_STUB (this_type) = 1;
16775 set_die_type (die, this_type, cu);
16776 target_type = die_type (die, cu);
16777 if (target_type != this_type)
16778 TYPE_TARGET_TYPE (this_type) = target_type;
16779 else
16780 {
16781 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16782 spec and cause infinite loops in GDB. */
16783 complaint (_("Self-referential DW_TAG_typedef "
16784 "- DIE at %s [in module %s]"),
16785 sect_offset_str (die->sect_off), objfile_name (objfile));
16786 TYPE_TARGET_TYPE (this_type) = NULL;
16787 }
16788 return this_type;
16789 }
16790
16791 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16792 (which may be different from NAME) to the architecture back-end to allow
16793 it to guess the correct format if necessary. */
16794
16795 static struct type *
16796 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16797 const char *name_hint, enum bfd_endian byte_order)
16798 {
16799 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16800 const struct floatformat **format;
16801 struct type *type;
16802
16803 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16804 if (format)
16805 type = init_float_type (objfile, bits, name, format, byte_order);
16806 else
16807 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16808
16809 return type;
16810 }
16811
16812 /* Allocate an integer type of size BITS and name NAME. */
16813
16814 static struct type *
16815 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16816 int bits, int unsigned_p, const char *name)
16817 {
16818 struct type *type;
16819
16820 /* Versions of Intel's C Compiler generate an integer type called "void"
16821 instead of using DW_TAG_unspecified_type. This has been seen on
16822 at least versions 14, 17, and 18. */
16823 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16824 && strcmp (name, "void") == 0)
16825 type = objfile_type (objfile)->builtin_void;
16826 else
16827 type = init_integer_type (objfile, bits, unsigned_p, name);
16828
16829 return type;
16830 }
16831
16832 /* Initialise and return a floating point type of size BITS suitable for
16833 use as a component of a complex number. The NAME_HINT is passed through
16834 when initialising the floating point type and is the name of the complex
16835 type.
16836
16837 As DWARF doesn't currently provide an explicit name for the components
16838 of a complex number, but it can be helpful to have these components
16839 named, we try to select a suitable name based on the size of the
16840 component. */
16841 static struct type *
16842 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16843 struct objfile *objfile,
16844 int bits, const char *name_hint,
16845 enum bfd_endian byte_order)
16846 {
16847 gdbarch *gdbarch = get_objfile_arch (objfile);
16848 struct type *tt = nullptr;
16849
16850 /* Try to find a suitable floating point builtin type of size BITS.
16851 We're going to use the name of this type as the name for the complex
16852 target type that we are about to create. */
16853 switch (cu->language)
16854 {
16855 case language_fortran:
16856 switch (bits)
16857 {
16858 case 32:
16859 tt = builtin_f_type (gdbarch)->builtin_real;
16860 break;
16861 case 64:
16862 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16863 break;
16864 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16865 case 128:
16866 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16867 break;
16868 }
16869 break;
16870 default:
16871 switch (bits)
16872 {
16873 case 32:
16874 tt = builtin_type (gdbarch)->builtin_float;
16875 break;
16876 case 64:
16877 tt = builtin_type (gdbarch)->builtin_double;
16878 break;
16879 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16880 case 128:
16881 tt = builtin_type (gdbarch)->builtin_long_double;
16882 break;
16883 }
16884 break;
16885 }
16886
16887 /* If the type we found doesn't match the size we were looking for, then
16888 pretend we didn't find a type at all, the complex target type we
16889 create will then be nameless. */
16890 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16891 tt = nullptr;
16892
16893 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16894 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16895 }
16896
16897 /* Find a representation of a given base type and install
16898 it in the TYPE field of the die. */
16899
16900 static struct type *
16901 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16902 {
16903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16904 struct type *type;
16905 struct attribute *attr;
16906 int encoding = 0, bits = 0;
16907 const char *name;
16908 gdbarch *arch;
16909
16910 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16911 if (attr != nullptr)
16912 encoding = DW_UNSND (attr);
16913 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16914 if (attr != nullptr)
16915 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16916 name = dwarf2_name (die, cu);
16917 if (!name)
16918 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16919
16920 arch = get_objfile_arch (objfile);
16921 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16922
16923 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16924 if (attr)
16925 {
16926 int endianity = DW_UNSND (attr);
16927
16928 switch (endianity)
16929 {
16930 case DW_END_big:
16931 byte_order = BFD_ENDIAN_BIG;
16932 break;
16933 case DW_END_little:
16934 byte_order = BFD_ENDIAN_LITTLE;
16935 break;
16936 default:
16937 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16938 break;
16939 }
16940 }
16941
16942 switch (encoding)
16943 {
16944 case DW_ATE_address:
16945 /* Turn DW_ATE_address into a void * pointer. */
16946 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16947 type = init_pointer_type (objfile, bits, name, type);
16948 break;
16949 case DW_ATE_boolean:
16950 type = init_boolean_type (objfile, bits, 1, name);
16951 break;
16952 case DW_ATE_complex_float:
16953 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16954 byte_order);
16955 type = init_complex_type (objfile, name, type);
16956 break;
16957 case DW_ATE_decimal_float:
16958 type = init_decfloat_type (objfile, bits, name);
16959 break;
16960 case DW_ATE_float:
16961 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16962 break;
16963 case DW_ATE_signed:
16964 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16965 break;
16966 case DW_ATE_unsigned:
16967 if (cu->language == language_fortran
16968 && name
16969 && startswith (name, "character("))
16970 type = init_character_type (objfile, bits, 1, name);
16971 else
16972 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16973 break;
16974 case DW_ATE_signed_char:
16975 if (cu->language == language_ada || cu->language == language_m2
16976 || cu->language == language_pascal
16977 || cu->language == language_fortran)
16978 type = init_character_type (objfile, bits, 0, name);
16979 else
16980 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16981 break;
16982 case DW_ATE_unsigned_char:
16983 if (cu->language == language_ada || cu->language == language_m2
16984 || cu->language == language_pascal
16985 || cu->language == language_fortran
16986 || cu->language == language_rust)
16987 type = init_character_type (objfile, bits, 1, name);
16988 else
16989 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16990 break;
16991 case DW_ATE_UTF:
16992 {
16993 if (bits == 16)
16994 type = builtin_type (arch)->builtin_char16;
16995 else if (bits == 32)
16996 type = builtin_type (arch)->builtin_char32;
16997 else
16998 {
16999 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17000 bits);
17001 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17002 }
17003 return set_die_type (die, type, cu);
17004 }
17005 break;
17006
17007 default:
17008 complaint (_("unsupported DW_AT_encoding: '%s'"),
17009 dwarf_type_encoding_name (encoding));
17010 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17011 break;
17012 }
17013
17014 if (name && strcmp (name, "char") == 0)
17015 TYPE_NOSIGN (type) = 1;
17016
17017 maybe_set_alignment (cu, die, type);
17018
17019 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17020
17021 return set_die_type (die, type, cu);
17022 }
17023
17024 /* Parse dwarf attribute if it's a block, reference or constant and put the
17025 resulting value of the attribute into struct bound_prop.
17026 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17027
17028 static int
17029 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17030 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17031 struct type *default_type)
17032 {
17033 struct dwarf2_property_baton *baton;
17034 struct obstack *obstack
17035 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17036
17037 gdb_assert (default_type != NULL);
17038
17039 if (attr == NULL || prop == NULL)
17040 return 0;
17041
17042 if (attr->form_is_block ())
17043 {
17044 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17045 baton->property_type = default_type;
17046 baton->locexpr.per_cu = cu->per_cu;
17047 baton->locexpr.size = DW_BLOCK (attr)->size;
17048 baton->locexpr.data = DW_BLOCK (attr)->data;
17049 switch (attr->name)
17050 {
17051 case DW_AT_string_length:
17052 baton->locexpr.is_reference = true;
17053 break;
17054 default:
17055 baton->locexpr.is_reference = false;
17056 break;
17057 }
17058 prop->data.baton = baton;
17059 prop->kind = PROP_LOCEXPR;
17060 gdb_assert (prop->data.baton != NULL);
17061 }
17062 else if (attr->form_is_ref ())
17063 {
17064 struct dwarf2_cu *target_cu = cu;
17065 struct die_info *target_die;
17066 struct attribute *target_attr;
17067
17068 target_die = follow_die_ref (die, attr, &target_cu);
17069 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17070 if (target_attr == NULL)
17071 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17072 target_cu);
17073 if (target_attr == NULL)
17074 return 0;
17075
17076 switch (target_attr->name)
17077 {
17078 case DW_AT_location:
17079 if (target_attr->form_is_section_offset ())
17080 {
17081 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17082 baton->property_type = die_type (target_die, target_cu);
17083 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17084 prop->data.baton = baton;
17085 prop->kind = PROP_LOCLIST;
17086 gdb_assert (prop->data.baton != NULL);
17087 }
17088 else if (target_attr->form_is_block ())
17089 {
17090 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17091 baton->property_type = die_type (target_die, target_cu);
17092 baton->locexpr.per_cu = cu->per_cu;
17093 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17094 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17095 baton->locexpr.is_reference = true;
17096 prop->data.baton = baton;
17097 prop->kind = PROP_LOCEXPR;
17098 gdb_assert (prop->data.baton != NULL);
17099 }
17100 else
17101 {
17102 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17103 "dynamic property");
17104 return 0;
17105 }
17106 break;
17107 case DW_AT_data_member_location:
17108 {
17109 LONGEST offset;
17110
17111 if (!handle_data_member_location (target_die, target_cu,
17112 &offset))
17113 return 0;
17114
17115 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17116 baton->property_type = read_type_die (target_die->parent,
17117 target_cu);
17118 baton->offset_info.offset = offset;
17119 baton->offset_info.type = die_type (target_die, target_cu);
17120 prop->data.baton = baton;
17121 prop->kind = PROP_ADDR_OFFSET;
17122 break;
17123 }
17124 }
17125 }
17126 else if (attr->form_is_constant ())
17127 {
17128 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17129 prop->kind = PROP_CONST;
17130 }
17131 else
17132 {
17133 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17134 dwarf2_name (die, cu));
17135 return 0;
17136 }
17137
17138 return 1;
17139 }
17140
17141 /* See read.h. */
17142
17143 struct type *
17144 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17145 {
17146 struct objfile *objfile = dwarf2_per_objfile->objfile;
17147 struct type *int_type;
17148
17149 /* Helper macro to examine the various builtin types. */
17150 #define TRY_TYPE(F) \
17151 int_type = (unsigned_p \
17152 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17153 : objfile_type (objfile)->builtin_ ## F); \
17154 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17155 return int_type
17156
17157 TRY_TYPE (char);
17158 TRY_TYPE (short);
17159 TRY_TYPE (int);
17160 TRY_TYPE (long);
17161 TRY_TYPE (long_long);
17162
17163 #undef TRY_TYPE
17164
17165 gdb_assert_not_reached ("unable to find suitable integer type");
17166 }
17167
17168 /* See read.h. */
17169
17170 struct type *
17171 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17172 {
17173 int addr_size = this->addr_size ();
17174 return int_type (addr_size, unsigned_p);
17175 }
17176
17177 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17178 present (which is valid) then compute the default type based on the
17179 compilation units address size. */
17180
17181 static struct type *
17182 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17183 {
17184 struct type *index_type = die_type (die, cu);
17185
17186 /* Dwarf-2 specifications explicitly allows to create subrange types
17187 without specifying a base type.
17188 In that case, the base type must be set to the type of
17189 the lower bound, upper bound or count, in that order, if any of these
17190 three attributes references an object that has a type.
17191 If no base type is found, the Dwarf-2 specifications say that
17192 a signed integer type of size equal to the size of an address should
17193 be used.
17194 For the following C code: `extern char gdb_int [];'
17195 GCC produces an empty range DIE.
17196 FIXME: muller/2010-05-28: Possible references to object for low bound,
17197 high bound or count are not yet handled by this code. */
17198 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17199 index_type = cu->per_cu->addr_sized_int_type (false);
17200
17201 return index_type;
17202 }
17203
17204 /* Read the given DW_AT_subrange DIE. */
17205
17206 static struct type *
17207 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17208 {
17209 struct type *base_type, *orig_base_type;
17210 struct type *range_type;
17211 struct attribute *attr;
17212 struct dynamic_prop low, high;
17213 int low_default_is_valid;
17214 int high_bound_is_count = 0;
17215 const char *name;
17216 ULONGEST negative_mask;
17217
17218 orig_base_type = read_subrange_index_type (die, cu);
17219
17220 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17221 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17222 creating the range type, but we use the result of check_typedef
17223 when examining properties of the type. */
17224 base_type = check_typedef (orig_base_type);
17225
17226 /* The die_type call above may have already set the type for this DIE. */
17227 range_type = get_die_type (die, cu);
17228 if (range_type)
17229 return range_type;
17230
17231 low.kind = PROP_CONST;
17232 high.kind = PROP_CONST;
17233 high.data.const_val = 0;
17234
17235 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17236 omitting DW_AT_lower_bound. */
17237 switch (cu->language)
17238 {
17239 case language_c:
17240 case language_cplus:
17241 low.data.const_val = 0;
17242 low_default_is_valid = 1;
17243 break;
17244 case language_fortran:
17245 low.data.const_val = 1;
17246 low_default_is_valid = 1;
17247 break;
17248 case language_d:
17249 case language_objc:
17250 case language_rust:
17251 low.data.const_val = 0;
17252 low_default_is_valid = (cu->header.version >= 4);
17253 break;
17254 case language_ada:
17255 case language_m2:
17256 case language_pascal:
17257 low.data.const_val = 1;
17258 low_default_is_valid = (cu->header.version >= 4);
17259 break;
17260 default:
17261 low.data.const_val = 0;
17262 low_default_is_valid = 0;
17263 break;
17264 }
17265
17266 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17267 if (attr != nullptr)
17268 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17269 else if (!low_default_is_valid)
17270 complaint (_("Missing DW_AT_lower_bound "
17271 "- DIE at %s [in module %s]"),
17272 sect_offset_str (die->sect_off),
17273 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17274
17275 struct attribute *attr_ub, *attr_count;
17276 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17277 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17278 {
17279 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17280 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17281 {
17282 /* If bounds are constant do the final calculation here. */
17283 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17284 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17285 else
17286 high_bound_is_count = 1;
17287 }
17288 else
17289 {
17290 if (attr_ub != NULL)
17291 complaint (_("Unresolved DW_AT_upper_bound "
17292 "- DIE at %s [in module %s]"),
17293 sect_offset_str (die->sect_off),
17294 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17295 if (attr_count != NULL)
17296 complaint (_("Unresolved DW_AT_count "
17297 "- DIE at %s [in module %s]"),
17298 sect_offset_str (die->sect_off),
17299 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17300 }
17301 }
17302
17303 LONGEST bias = 0;
17304 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17305 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17306 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17307
17308 /* Normally, the DWARF producers are expected to use a signed
17309 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17310 But this is unfortunately not always the case, as witnessed
17311 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17312 is used instead. To work around that ambiguity, we treat
17313 the bounds as signed, and thus sign-extend their values, when
17314 the base type is signed. */
17315 negative_mask =
17316 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17317 if (low.kind == PROP_CONST
17318 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17319 low.data.const_val |= negative_mask;
17320 if (high.kind == PROP_CONST
17321 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17322 high.data.const_val |= negative_mask;
17323
17324 /* Check for bit and byte strides. */
17325 struct dynamic_prop byte_stride_prop;
17326 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17327 if (attr_byte_stride != nullptr)
17328 {
17329 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17330 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17331 prop_type);
17332 }
17333
17334 struct dynamic_prop bit_stride_prop;
17335 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17336 if (attr_bit_stride != nullptr)
17337 {
17338 /* It only makes sense to have either a bit or byte stride. */
17339 if (attr_byte_stride != nullptr)
17340 {
17341 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17342 "- DIE at %s [in module %s]"),
17343 sect_offset_str (die->sect_off),
17344 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17345 attr_bit_stride = nullptr;
17346 }
17347 else
17348 {
17349 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17350 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17351 prop_type);
17352 }
17353 }
17354
17355 if (attr_byte_stride != nullptr
17356 || attr_bit_stride != nullptr)
17357 {
17358 bool byte_stride_p = (attr_byte_stride != nullptr);
17359 struct dynamic_prop *stride
17360 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17361
17362 range_type
17363 = create_range_type_with_stride (NULL, orig_base_type, &low,
17364 &high, bias, stride, byte_stride_p);
17365 }
17366 else
17367 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17368
17369 if (high_bound_is_count)
17370 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17371
17372 /* Ada expects an empty array on no boundary attributes. */
17373 if (attr == NULL && cu->language != language_ada)
17374 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17375
17376 name = dwarf2_name (die, cu);
17377 if (name)
17378 TYPE_NAME (range_type) = name;
17379
17380 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17381 if (attr != nullptr)
17382 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17383
17384 maybe_set_alignment (cu, die, range_type);
17385
17386 set_die_type (die, range_type, cu);
17387
17388 /* set_die_type should be already done. */
17389 set_descriptive_type (range_type, die, cu);
17390
17391 return range_type;
17392 }
17393
17394 static struct type *
17395 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17396 {
17397 struct type *type;
17398
17399 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17400 NULL);
17401 TYPE_NAME (type) = dwarf2_name (die, cu);
17402
17403 /* In Ada, an unspecified type is typically used when the description
17404 of the type is deferred to a different unit. When encountering
17405 such a type, we treat it as a stub, and try to resolve it later on,
17406 when needed. */
17407 if (cu->language == language_ada)
17408 TYPE_STUB (type) = 1;
17409
17410 return set_die_type (die, type, cu);
17411 }
17412
17413 /* Read a single die and all its descendents. Set the die's sibling
17414 field to NULL; set other fields in the die correctly, and set all
17415 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17416 location of the info_ptr after reading all of those dies. PARENT
17417 is the parent of the die in question. */
17418
17419 static struct die_info *
17420 read_die_and_children (const struct die_reader_specs *reader,
17421 const gdb_byte *info_ptr,
17422 const gdb_byte **new_info_ptr,
17423 struct die_info *parent)
17424 {
17425 struct die_info *die;
17426 const gdb_byte *cur_ptr;
17427
17428 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17429 if (die == NULL)
17430 {
17431 *new_info_ptr = cur_ptr;
17432 return NULL;
17433 }
17434 store_in_ref_table (die, reader->cu);
17435
17436 if (die->has_children)
17437 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17438 else
17439 {
17440 die->child = NULL;
17441 *new_info_ptr = cur_ptr;
17442 }
17443
17444 die->sibling = NULL;
17445 die->parent = parent;
17446 return die;
17447 }
17448
17449 /* Read a die, all of its descendents, and all of its siblings; set
17450 all of the fields of all of the dies correctly. Arguments are as
17451 in read_die_and_children. */
17452
17453 static struct die_info *
17454 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17455 const gdb_byte *info_ptr,
17456 const gdb_byte **new_info_ptr,
17457 struct die_info *parent)
17458 {
17459 struct die_info *first_die, *last_sibling;
17460 const gdb_byte *cur_ptr;
17461
17462 cur_ptr = info_ptr;
17463 first_die = last_sibling = NULL;
17464
17465 while (1)
17466 {
17467 struct die_info *die
17468 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17469
17470 if (die == NULL)
17471 {
17472 *new_info_ptr = cur_ptr;
17473 return first_die;
17474 }
17475
17476 if (!first_die)
17477 first_die = die;
17478 else
17479 last_sibling->sibling = die;
17480
17481 last_sibling = die;
17482 }
17483 }
17484
17485 /* Read a die, all of its descendents, and all of its siblings; set
17486 all of the fields of all of the dies correctly. Arguments are as
17487 in read_die_and_children.
17488 This the main entry point for reading a DIE and all its children. */
17489
17490 static struct die_info *
17491 read_die_and_siblings (const struct die_reader_specs *reader,
17492 const gdb_byte *info_ptr,
17493 const gdb_byte **new_info_ptr,
17494 struct die_info *parent)
17495 {
17496 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17497 new_info_ptr, parent);
17498
17499 if (dwarf_die_debug)
17500 {
17501 fprintf_unfiltered (gdb_stdlog,
17502 "Read die from %s@0x%x of %s:\n",
17503 reader->die_section->get_name (),
17504 (unsigned) (info_ptr - reader->die_section->buffer),
17505 bfd_get_filename (reader->abfd));
17506 dump_die (die, dwarf_die_debug);
17507 }
17508
17509 return die;
17510 }
17511
17512 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17513 attributes.
17514 The caller is responsible for filling in the extra attributes
17515 and updating (*DIEP)->num_attrs.
17516 Set DIEP to point to a newly allocated die with its information,
17517 except for its child, sibling, and parent fields. */
17518
17519 static const gdb_byte *
17520 read_full_die_1 (const struct die_reader_specs *reader,
17521 struct die_info **diep, const gdb_byte *info_ptr,
17522 int num_extra_attrs)
17523 {
17524 unsigned int abbrev_number, bytes_read, i;
17525 struct abbrev_info *abbrev;
17526 struct die_info *die;
17527 struct dwarf2_cu *cu = reader->cu;
17528 bfd *abfd = reader->abfd;
17529
17530 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17531 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17532 info_ptr += bytes_read;
17533 if (!abbrev_number)
17534 {
17535 *diep = NULL;
17536 return info_ptr;
17537 }
17538
17539 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17540 if (!abbrev)
17541 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17542 abbrev_number,
17543 bfd_get_filename (abfd));
17544
17545 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17546 die->sect_off = sect_off;
17547 die->tag = abbrev->tag;
17548 die->abbrev = abbrev_number;
17549 die->has_children = abbrev->has_children;
17550
17551 /* Make the result usable.
17552 The caller needs to update num_attrs after adding the extra
17553 attributes. */
17554 die->num_attrs = abbrev->num_attrs;
17555
17556 std::vector<int> indexes_that_need_reprocess;
17557 for (i = 0; i < abbrev->num_attrs; ++i)
17558 {
17559 bool need_reprocess;
17560 info_ptr =
17561 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17562 info_ptr, &need_reprocess);
17563 if (need_reprocess)
17564 indexes_that_need_reprocess.push_back (i);
17565 }
17566
17567 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17568 if (attr != nullptr)
17569 cu->str_offsets_base = DW_UNSND (attr);
17570
17571 auto maybe_addr_base = lookup_addr_base(die);
17572 if (maybe_addr_base.has_value ())
17573 cu->addr_base = *maybe_addr_base;
17574 for (int index : indexes_that_need_reprocess)
17575 read_attribute_reprocess (reader, &die->attrs[index]);
17576 *diep = die;
17577 return info_ptr;
17578 }
17579
17580 /* Read a die and all its attributes.
17581 Set DIEP to point to a newly allocated die with its information,
17582 except for its child, sibling, and parent fields. */
17583
17584 static const gdb_byte *
17585 read_full_die (const struct die_reader_specs *reader,
17586 struct die_info **diep, const gdb_byte *info_ptr)
17587 {
17588 const gdb_byte *result;
17589
17590 result = read_full_die_1 (reader, diep, info_ptr, 0);
17591
17592 if (dwarf_die_debug)
17593 {
17594 fprintf_unfiltered (gdb_stdlog,
17595 "Read die from %s@0x%x of %s:\n",
17596 reader->die_section->get_name (),
17597 (unsigned) (info_ptr - reader->die_section->buffer),
17598 bfd_get_filename (reader->abfd));
17599 dump_die (*diep, dwarf_die_debug);
17600 }
17601
17602 return result;
17603 }
17604 \f
17605
17606 /* Returns nonzero if TAG represents a type that we might generate a partial
17607 symbol for. */
17608
17609 static int
17610 is_type_tag_for_partial (int tag)
17611 {
17612 switch (tag)
17613 {
17614 #if 0
17615 /* Some types that would be reasonable to generate partial symbols for,
17616 that we don't at present. */
17617 case DW_TAG_array_type:
17618 case DW_TAG_file_type:
17619 case DW_TAG_ptr_to_member_type:
17620 case DW_TAG_set_type:
17621 case DW_TAG_string_type:
17622 case DW_TAG_subroutine_type:
17623 #endif
17624 case DW_TAG_base_type:
17625 case DW_TAG_class_type:
17626 case DW_TAG_interface_type:
17627 case DW_TAG_enumeration_type:
17628 case DW_TAG_structure_type:
17629 case DW_TAG_subrange_type:
17630 case DW_TAG_typedef:
17631 case DW_TAG_union_type:
17632 return 1;
17633 default:
17634 return 0;
17635 }
17636 }
17637
17638 /* Load all DIEs that are interesting for partial symbols into memory. */
17639
17640 static struct partial_die_info *
17641 load_partial_dies (const struct die_reader_specs *reader,
17642 const gdb_byte *info_ptr, int building_psymtab)
17643 {
17644 struct dwarf2_cu *cu = reader->cu;
17645 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17646 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17647 unsigned int bytes_read;
17648 unsigned int load_all = 0;
17649 int nesting_level = 1;
17650
17651 parent_die = NULL;
17652 last_die = NULL;
17653
17654 gdb_assert (cu->per_cu != NULL);
17655 if (cu->per_cu->load_all_dies)
17656 load_all = 1;
17657
17658 cu->partial_dies
17659 = htab_create_alloc_ex (cu->header.length / 12,
17660 partial_die_hash,
17661 partial_die_eq,
17662 NULL,
17663 &cu->comp_unit_obstack,
17664 hashtab_obstack_allocate,
17665 dummy_obstack_deallocate);
17666
17667 while (1)
17668 {
17669 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17670
17671 /* A NULL abbrev means the end of a series of children. */
17672 if (abbrev == NULL)
17673 {
17674 if (--nesting_level == 0)
17675 return first_die;
17676
17677 info_ptr += bytes_read;
17678 last_die = parent_die;
17679 parent_die = parent_die->die_parent;
17680 continue;
17681 }
17682
17683 /* Check for template arguments. We never save these; if
17684 they're seen, we just mark the parent, and go on our way. */
17685 if (parent_die != NULL
17686 && cu->language == language_cplus
17687 && (abbrev->tag == DW_TAG_template_type_param
17688 || abbrev->tag == DW_TAG_template_value_param))
17689 {
17690 parent_die->has_template_arguments = 1;
17691
17692 if (!load_all)
17693 {
17694 /* We don't need a partial DIE for the template argument. */
17695 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17696 continue;
17697 }
17698 }
17699
17700 /* We only recurse into c++ subprograms looking for template arguments.
17701 Skip their other children. */
17702 if (!load_all
17703 && cu->language == language_cplus
17704 && parent_die != NULL
17705 && parent_die->tag == DW_TAG_subprogram)
17706 {
17707 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17708 continue;
17709 }
17710
17711 /* Check whether this DIE is interesting enough to save. Normally
17712 we would not be interested in members here, but there may be
17713 later variables referencing them via DW_AT_specification (for
17714 static members). */
17715 if (!load_all
17716 && !is_type_tag_for_partial (abbrev->tag)
17717 && abbrev->tag != DW_TAG_constant
17718 && abbrev->tag != DW_TAG_enumerator
17719 && abbrev->tag != DW_TAG_subprogram
17720 && abbrev->tag != DW_TAG_inlined_subroutine
17721 && abbrev->tag != DW_TAG_lexical_block
17722 && abbrev->tag != DW_TAG_variable
17723 && abbrev->tag != DW_TAG_namespace
17724 && abbrev->tag != DW_TAG_module
17725 && abbrev->tag != DW_TAG_member
17726 && abbrev->tag != DW_TAG_imported_unit
17727 && abbrev->tag != DW_TAG_imported_declaration)
17728 {
17729 /* Otherwise we skip to the next sibling, if any. */
17730 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17731 continue;
17732 }
17733
17734 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17735 abbrev);
17736
17737 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17738
17739 /* This two-pass algorithm for processing partial symbols has a
17740 high cost in cache pressure. Thus, handle some simple cases
17741 here which cover the majority of C partial symbols. DIEs
17742 which neither have specification tags in them, nor could have
17743 specification tags elsewhere pointing at them, can simply be
17744 processed and discarded.
17745
17746 This segment is also optional; scan_partial_symbols and
17747 add_partial_symbol will handle these DIEs if we chain
17748 them in normally. When compilers which do not emit large
17749 quantities of duplicate debug information are more common,
17750 this code can probably be removed. */
17751
17752 /* Any complete simple types at the top level (pretty much all
17753 of them, for a language without namespaces), can be processed
17754 directly. */
17755 if (parent_die == NULL
17756 && pdi.has_specification == 0
17757 && pdi.is_declaration == 0
17758 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17759 || pdi.tag == DW_TAG_base_type
17760 || pdi.tag == DW_TAG_subrange_type))
17761 {
17762 if (building_psymtab && pdi.name != NULL)
17763 add_psymbol_to_list (pdi.name, false,
17764 VAR_DOMAIN, LOC_TYPEDEF, -1,
17765 psymbol_placement::STATIC,
17766 0, cu->language, objfile);
17767 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17768 continue;
17769 }
17770
17771 /* The exception for DW_TAG_typedef with has_children above is
17772 a workaround of GCC PR debug/47510. In the case of this complaint
17773 type_name_or_error will error on such types later.
17774
17775 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17776 it could not find the child DIEs referenced later, this is checked
17777 above. In correct DWARF DW_TAG_typedef should have no children. */
17778
17779 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17780 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17781 "- DIE at %s [in module %s]"),
17782 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17783
17784 /* If we're at the second level, and we're an enumerator, and
17785 our parent has no specification (meaning possibly lives in a
17786 namespace elsewhere), then we can add the partial symbol now
17787 instead of queueing it. */
17788 if (pdi.tag == DW_TAG_enumerator
17789 && parent_die != NULL
17790 && parent_die->die_parent == NULL
17791 && parent_die->tag == DW_TAG_enumeration_type
17792 && parent_die->has_specification == 0)
17793 {
17794 if (pdi.name == NULL)
17795 complaint (_("malformed enumerator DIE ignored"));
17796 else if (building_psymtab)
17797 add_psymbol_to_list (pdi.name, false,
17798 VAR_DOMAIN, LOC_CONST, -1,
17799 cu->language == language_cplus
17800 ? psymbol_placement::GLOBAL
17801 : psymbol_placement::STATIC,
17802 0, cu->language, objfile);
17803
17804 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17805 continue;
17806 }
17807
17808 struct partial_die_info *part_die
17809 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17810
17811 /* We'll save this DIE so link it in. */
17812 part_die->die_parent = parent_die;
17813 part_die->die_sibling = NULL;
17814 part_die->die_child = NULL;
17815
17816 if (last_die && last_die == parent_die)
17817 last_die->die_child = part_die;
17818 else if (last_die)
17819 last_die->die_sibling = part_die;
17820
17821 last_die = part_die;
17822
17823 if (first_die == NULL)
17824 first_die = part_die;
17825
17826 /* Maybe add the DIE to the hash table. Not all DIEs that we
17827 find interesting need to be in the hash table, because we
17828 also have the parent/sibling/child chains; only those that we
17829 might refer to by offset later during partial symbol reading.
17830
17831 For now this means things that might have be the target of a
17832 DW_AT_specification, DW_AT_abstract_origin, or
17833 DW_AT_extension. DW_AT_extension will refer only to
17834 namespaces; DW_AT_abstract_origin refers to functions (and
17835 many things under the function DIE, but we do not recurse
17836 into function DIEs during partial symbol reading) and
17837 possibly variables as well; DW_AT_specification refers to
17838 declarations. Declarations ought to have the DW_AT_declaration
17839 flag. It happens that GCC forgets to put it in sometimes, but
17840 only for functions, not for types.
17841
17842 Adding more things than necessary to the hash table is harmless
17843 except for the performance cost. Adding too few will result in
17844 wasted time in find_partial_die, when we reread the compilation
17845 unit with load_all_dies set. */
17846
17847 if (load_all
17848 || abbrev->tag == DW_TAG_constant
17849 || abbrev->tag == DW_TAG_subprogram
17850 || abbrev->tag == DW_TAG_variable
17851 || abbrev->tag == DW_TAG_namespace
17852 || part_die->is_declaration)
17853 {
17854 void **slot;
17855
17856 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17857 to_underlying (part_die->sect_off),
17858 INSERT);
17859 *slot = part_die;
17860 }
17861
17862 /* For some DIEs we want to follow their children (if any). For C
17863 we have no reason to follow the children of structures; for other
17864 languages we have to, so that we can get at method physnames
17865 to infer fully qualified class names, for DW_AT_specification,
17866 and for C++ template arguments. For C++, we also look one level
17867 inside functions to find template arguments (if the name of the
17868 function does not already contain the template arguments).
17869
17870 For Ada and Fortran, we need to scan the children of subprograms
17871 and lexical blocks as well because these languages allow the
17872 definition of nested entities that could be interesting for the
17873 debugger, such as nested subprograms for instance. */
17874 if (last_die->has_children
17875 && (load_all
17876 || last_die->tag == DW_TAG_namespace
17877 || last_die->tag == DW_TAG_module
17878 || last_die->tag == DW_TAG_enumeration_type
17879 || (cu->language == language_cplus
17880 && last_die->tag == DW_TAG_subprogram
17881 && (last_die->name == NULL
17882 || strchr (last_die->name, '<') == NULL))
17883 || (cu->language != language_c
17884 && (last_die->tag == DW_TAG_class_type
17885 || last_die->tag == DW_TAG_interface_type
17886 || last_die->tag == DW_TAG_structure_type
17887 || last_die->tag == DW_TAG_union_type))
17888 || ((cu->language == language_ada
17889 || cu->language == language_fortran)
17890 && (last_die->tag == DW_TAG_subprogram
17891 || last_die->tag == DW_TAG_lexical_block))))
17892 {
17893 nesting_level++;
17894 parent_die = last_die;
17895 continue;
17896 }
17897
17898 /* Otherwise we skip to the next sibling, if any. */
17899 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17900
17901 /* Back to the top, do it again. */
17902 }
17903 }
17904
17905 partial_die_info::partial_die_info (sect_offset sect_off_,
17906 struct abbrev_info *abbrev)
17907 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17908 {
17909 }
17910
17911 /* Read a minimal amount of information into the minimal die structure.
17912 INFO_PTR should point just after the initial uleb128 of a DIE. */
17913
17914 const gdb_byte *
17915 partial_die_info::read (const struct die_reader_specs *reader,
17916 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17917 {
17918 struct dwarf2_cu *cu = reader->cu;
17919 struct dwarf2_per_objfile *dwarf2_per_objfile
17920 = cu->per_cu->dwarf2_per_objfile;
17921 unsigned int i;
17922 int has_low_pc_attr = 0;
17923 int has_high_pc_attr = 0;
17924 int high_pc_relative = 0;
17925
17926 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17927 for (i = 0; i < abbrev.num_attrs; ++i)
17928 {
17929 bool need_reprocess;
17930 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17931 info_ptr, &need_reprocess);
17932 /* String and address offsets that need to do the reprocessing have
17933 already been read at this point, so there is no need to wait until
17934 the loop terminates to do the reprocessing. */
17935 if (need_reprocess)
17936 read_attribute_reprocess (reader, &attr_vec[i]);
17937 attribute &attr = attr_vec[i];
17938 /* Store the data if it is of an attribute we want to keep in a
17939 partial symbol table. */
17940 switch (attr.name)
17941 {
17942 case DW_AT_name:
17943 switch (tag)
17944 {
17945 case DW_TAG_compile_unit:
17946 case DW_TAG_partial_unit:
17947 case DW_TAG_type_unit:
17948 /* Compilation units have a DW_AT_name that is a filename, not
17949 a source language identifier. */
17950 case DW_TAG_enumeration_type:
17951 case DW_TAG_enumerator:
17952 /* These tags always have simple identifiers already; no need
17953 to canonicalize them. */
17954 name = DW_STRING (&attr);
17955 break;
17956 default:
17957 {
17958 struct objfile *objfile = dwarf2_per_objfile->objfile;
17959
17960 name
17961 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17962 &objfile->per_bfd->storage_obstack);
17963 }
17964 break;
17965 }
17966 break;
17967 case DW_AT_linkage_name:
17968 case DW_AT_MIPS_linkage_name:
17969 /* Note that both forms of linkage name might appear. We
17970 assume they will be the same, and we only store the last
17971 one we see. */
17972 linkage_name = DW_STRING (&attr);
17973 break;
17974 case DW_AT_low_pc:
17975 has_low_pc_attr = 1;
17976 lowpc = attr.value_as_address ();
17977 break;
17978 case DW_AT_high_pc:
17979 has_high_pc_attr = 1;
17980 highpc = attr.value_as_address ();
17981 if (cu->header.version >= 4 && attr.form_is_constant ())
17982 high_pc_relative = 1;
17983 break;
17984 case DW_AT_location:
17985 /* Support the .debug_loc offsets. */
17986 if (attr.form_is_block ())
17987 {
17988 d.locdesc = DW_BLOCK (&attr);
17989 }
17990 else if (attr.form_is_section_offset ())
17991 {
17992 dwarf2_complex_location_expr_complaint ();
17993 }
17994 else
17995 {
17996 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17997 "partial symbol information");
17998 }
17999 break;
18000 case DW_AT_external:
18001 is_external = DW_UNSND (&attr);
18002 break;
18003 case DW_AT_declaration:
18004 is_declaration = DW_UNSND (&attr);
18005 break;
18006 case DW_AT_type:
18007 has_type = 1;
18008 break;
18009 case DW_AT_abstract_origin:
18010 case DW_AT_specification:
18011 case DW_AT_extension:
18012 has_specification = 1;
18013 spec_offset = dwarf2_get_ref_die_offset (&attr);
18014 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18015 || cu->per_cu->is_dwz);
18016 break;
18017 case DW_AT_sibling:
18018 /* Ignore absolute siblings, they might point outside of
18019 the current compile unit. */
18020 if (attr.form == DW_FORM_ref_addr)
18021 complaint (_("ignoring absolute DW_AT_sibling"));
18022 else
18023 {
18024 const gdb_byte *buffer = reader->buffer;
18025 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18026 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18027
18028 if (sibling_ptr < info_ptr)
18029 complaint (_("DW_AT_sibling points backwards"));
18030 else if (sibling_ptr > reader->buffer_end)
18031 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18032 else
18033 sibling = sibling_ptr;
18034 }
18035 break;
18036 case DW_AT_byte_size:
18037 has_byte_size = 1;
18038 break;
18039 case DW_AT_const_value:
18040 has_const_value = 1;
18041 break;
18042 case DW_AT_calling_convention:
18043 /* DWARF doesn't provide a way to identify a program's source-level
18044 entry point. DW_AT_calling_convention attributes are only meant
18045 to describe functions' calling conventions.
18046
18047 However, because it's a necessary piece of information in
18048 Fortran, and before DWARF 4 DW_CC_program was the only
18049 piece of debugging information whose definition refers to
18050 a 'main program' at all, several compilers marked Fortran
18051 main programs with DW_CC_program --- even when those
18052 functions use the standard calling conventions.
18053
18054 Although DWARF now specifies a way to provide this
18055 information, we support this practice for backward
18056 compatibility. */
18057 if (DW_UNSND (&attr) == DW_CC_program
18058 && cu->language == language_fortran)
18059 main_subprogram = 1;
18060 break;
18061 case DW_AT_inline:
18062 if (DW_UNSND (&attr) == DW_INL_inlined
18063 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18064 may_be_inlined = 1;
18065 break;
18066
18067 case DW_AT_import:
18068 if (tag == DW_TAG_imported_unit)
18069 {
18070 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18071 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18072 || cu->per_cu->is_dwz);
18073 }
18074 break;
18075
18076 case DW_AT_main_subprogram:
18077 main_subprogram = DW_UNSND (&attr);
18078 break;
18079
18080 case DW_AT_ranges:
18081 {
18082 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18083 but that requires a full DIE, so instead we just
18084 reimplement it. */
18085 int need_ranges_base = tag != DW_TAG_compile_unit;
18086 unsigned int ranges_offset = (DW_UNSND (&attr)
18087 + (need_ranges_base
18088 ? cu->ranges_base
18089 : 0));
18090
18091 /* Value of the DW_AT_ranges attribute is the offset in the
18092 .debug_ranges section. */
18093 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18094 nullptr))
18095 has_pc_info = 1;
18096 }
18097 break;
18098
18099 default:
18100 break;
18101 }
18102 }
18103
18104 /* For Ada, if both the name and the linkage name appear, we prefer
18105 the latter. This lets "catch exception" work better, regardless
18106 of the order in which the name and linkage name were emitted.
18107 Really, though, this is just a workaround for the fact that gdb
18108 doesn't store both the name and the linkage name. */
18109 if (cu->language == language_ada && linkage_name != nullptr)
18110 name = linkage_name;
18111
18112 if (high_pc_relative)
18113 highpc += lowpc;
18114
18115 if (has_low_pc_attr && has_high_pc_attr)
18116 {
18117 /* When using the GNU linker, .gnu.linkonce. sections are used to
18118 eliminate duplicate copies of functions and vtables and such.
18119 The linker will arbitrarily choose one and discard the others.
18120 The AT_*_pc values for such functions refer to local labels in
18121 these sections. If the section from that file was discarded, the
18122 labels are not in the output, so the relocs get a value of 0.
18123 If this is a discarded function, mark the pc bounds as invalid,
18124 so that GDB will ignore it. */
18125 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18126 {
18127 struct objfile *objfile = dwarf2_per_objfile->objfile;
18128 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18129
18130 complaint (_("DW_AT_low_pc %s is zero "
18131 "for DIE at %s [in module %s]"),
18132 paddress (gdbarch, lowpc),
18133 sect_offset_str (sect_off),
18134 objfile_name (objfile));
18135 }
18136 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18137 else if (lowpc >= highpc)
18138 {
18139 struct objfile *objfile = dwarf2_per_objfile->objfile;
18140 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18141
18142 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18143 "for DIE at %s [in module %s]"),
18144 paddress (gdbarch, lowpc),
18145 paddress (gdbarch, highpc),
18146 sect_offset_str (sect_off),
18147 objfile_name (objfile));
18148 }
18149 else
18150 has_pc_info = 1;
18151 }
18152
18153 return info_ptr;
18154 }
18155
18156 /* Find a cached partial DIE at OFFSET in CU. */
18157
18158 struct partial_die_info *
18159 dwarf2_cu::find_partial_die (sect_offset sect_off)
18160 {
18161 struct partial_die_info *lookup_die = NULL;
18162 struct partial_die_info part_die (sect_off);
18163
18164 lookup_die = ((struct partial_die_info *)
18165 htab_find_with_hash (partial_dies, &part_die,
18166 to_underlying (sect_off)));
18167
18168 return lookup_die;
18169 }
18170
18171 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18172 except in the case of .debug_types DIEs which do not reference
18173 outside their CU (they do however referencing other types via
18174 DW_FORM_ref_sig8). */
18175
18176 static const struct cu_partial_die_info
18177 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18178 {
18179 struct dwarf2_per_objfile *dwarf2_per_objfile
18180 = cu->per_cu->dwarf2_per_objfile;
18181 struct objfile *objfile = dwarf2_per_objfile->objfile;
18182 struct dwarf2_per_cu_data *per_cu = NULL;
18183 struct partial_die_info *pd = NULL;
18184
18185 if (offset_in_dwz == cu->per_cu->is_dwz
18186 && cu->header.offset_in_cu_p (sect_off))
18187 {
18188 pd = cu->find_partial_die (sect_off);
18189 if (pd != NULL)
18190 return { cu, pd };
18191 /* We missed recording what we needed.
18192 Load all dies and try again. */
18193 per_cu = cu->per_cu;
18194 }
18195 else
18196 {
18197 /* TUs don't reference other CUs/TUs (except via type signatures). */
18198 if (cu->per_cu->is_debug_types)
18199 {
18200 error (_("Dwarf Error: Type Unit at offset %s contains"
18201 " external reference to offset %s [in module %s].\n"),
18202 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18203 bfd_get_filename (objfile->obfd));
18204 }
18205 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18206 dwarf2_per_objfile);
18207
18208 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18209 load_partial_comp_unit (per_cu);
18210
18211 per_cu->cu->last_used = 0;
18212 pd = per_cu->cu->find_partial_die (sect_off);
18213 }
18214
18215 /* If we didn't find it, and not all dies have been loaded,
18216 load them all and try again. */
18217
18218 if (pd == NULL && per_cu->load_all_dies == 0)
18219 {
18220 per_cu->load_all_dies = 1;
18221
18222 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18223 THIS_CU->cu may already be in use. So we can't just free it and
18224 replace its DIEs with the ones we read in. Instead, we leave those
18225 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18226 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18227 set. */
18228 load_partial_comp_unit (per_cu);
18229
18230 pd = per_cu->cu->find_partial_die (sect_off);
18231 }
18232
18233 if (pd == NULL)
18234 internal_error (__FILE__, __LINE__,
18235 _("could not find partial DIE %s "
18236 "in cache [from module %s]\n"),
18237 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18238 return { per_cu->cu, pd };
18239 }
18240
18241 /* See if we can figure out if the class lives in a namespace. We do
18242 this by looking for a member function; its demangled name will
18243 contain namespace info, if there is any. */
18244
18245 static void
18246 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18247 struct dwarf2_cu *cu)
18248 {
18249 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18250 what template types look like, because the demangler
18251 frequently doesn't give the same name as the debug info. We
18252 could fix this by only using the demangled name to get the
18253 prefix (but see comment in read_structure_type). */
18254
18255 struct partial_die_info *real_pdi;
18256 struct partial_die_info *child_pdi;
18257
18258 /* If this DIE (this DIE's specification, if any) has a parent, then
18259 we should not do this. We'll prepend the parent's fully qualified
18260 name when we create the partial symbol. */
18261
18262 real_pdi = struct_pdi;
18263 while (real_pdi->has_specification)
18264 {
18265 auto res = find_partial_die (real_pdi->spec_offset,
18266 real_pdi->spec_is_dwz, cu);
18267 real_pdi = res.pdi;
18268 cu = res.cu;
18269 }
18270
18271 if (real_pdi->die_parent != NULL)
18272 return;
18273
18274 for (child_pdi = struct_pdi->die_child;
18275 child_pdi != NULL;
18276 child_pdi = child_pdi->die_sibling)
18277 {
18278 if (child_pdi->tag == DW_TAG_subprogram
18279 && child_pdi->linkage_name != NULL)
18280 {
18281 gdb::unique_xmalloc_ptr<char> actual_class_name
18282 (language_class_name_from_physname (cu->language_defn,
18283 child_pdi->linkage_name));
18284 if (actual_class_name != NULL)
18285 {
18286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18287 struct_pdi->name
18288 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18289 actual_class_name.get ());
18290 }
18291 break;
18292 }
18293 }
18294 }
18295
18296 void
18297 partial_die_info::fixup (struct dwarf2_cu *cu)
18298 {
18299 /* Once we've fixed up a die, there's no point in doing so again.
18300 This also avoids a memory leak if we were to call
18301 guess_partial_die_structure_name multiple times. */
18302 if (fixup_called)
18303 return;
18304
18305 /* If we found a reference attribute and the DIE has no name, try
18306 to find a name in the referred to DIE. */
18307
18308 if (name == NULL && has_specification)
18309 {
18310 struct partial_die_info *spec_die;
18311
18312 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18313 spec_die = res.pdi;
18314 cu = res.cu;
18315
18316 spec_die->fixup (cu);
18317
18318 if (spec_die->name)
18319 {
18320 name = spec_die->name;
18321
18322 /* Copy DW_AT_external attribute if it is set. */
18323 if (spec_die->is_external)
18324 is_external = spec_die->is_external;
18325 }
18326 }
18327
18328 /* Set default names for some unnamed DIEs. */
18329
18330 if (name == NULL && tag == DW_TAG_namespace)
18331 name = CP_ANONYMOUS_NAMESPACE_STR;
18332
18333 /* If there is no parent die to provide a namespace, and there are
18334 children, see if we can determine the namespace from their linkage
18335 name. */
18336 if (cu->language == language_cplus
18337 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18338 && die_parent == NULL
18339 && has_children
18340 && (tag == DW_TAG_class_type
18341 || tag == DW_TAG_structure_type
18342 || tag == DW_TAG_union_type))
18343 guess_partial_die_structure_name (this, cu);
18344
18345 /* GCC might emit a nameless struct or union that has a linkage
18346 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18347 if (name == NULL
18348 && (tag == DW_TAG_class_type
18349 || tag == DW_TAG_interface_type
18350 || tag == DW_TAG_structure_type
18351 || tag == DW_TAG_union_type)
18352 && linkage_name != NULL)
18353 {
18354 gdb::unique_xmalloc_ptr<char> demangled
18355 (gdb_demangle (linkage_name, DMGL_TYPES));
18356 if (demangled != nullptr)
18357 {
18358 const char *base;
18359
18360 /* Strip any leading namespaces/classes, keep only the base name.
18361 DW_AT_name for named DIEs does not contain the prefixes. */
18362 base = strrchr (demangled.get (), ':');
18363 if (base && base > demangled.get () && base[-1] == ':')
18364 base++;
18365 else
18366 base = demangled.get ();
18367
18368 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18369 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18370 }
18371 }
18372
18373 fixup_called = 1;
18374 }
18375
18376 /* Process the attributes that had to be skipped in the first round. These
18377 attributes are the ones that need str_offsets_base or addr_base attributes.
18378 They could not have been processed in the first round, because at the time
18379 the values of str_offsets_base or addr_base may not have been known. */
18380 void read_attribute_reprocess (const struct die_reader_specs *reader,
18381 struct attribute *attr)
18382 {
18383 struct dwarf2_cu *cu = reader->cu;
18384 switch (attr->form)
18385 {
18386 case DW_FORM_addrx:
18387 case DW_FORM_GNU_addr_index:
18388 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18389 break;
18390 case DW_FORM_strx:
18391 case DW_FORM_strx1:
18392 case DW_FORM_strx2:
18393 case DW_FORM_strx3:
18394 case DW_FORM_strx4:
18395 case DW_FORM_GNU_str_index:
18396 {
18397 unsigned int str_index = DW_UNSND (attr);
18398 if (reader->dwo_file != NULL)
18399 {
18400 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18401 DW_STRING_IS_CANONICAL (attr) = 0;
18402 }
18403 else
18404 {
18405 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18406 DW_STRING_IS_CANONICAL (attr) = 0;
18407 }
18408 break;
18409 }
18410 default:
18411 gdb_assert_not_reached (_("Unexpected DWARF form."));
18412 }
18413 }
18414
18415 /* Read an attribute value described by an attribute form. */
18416
18417 static const gdb_byte *
18418 read_attribute_value (const struct die_reader_specs *reader,
18419 struct attribute *attr, unsigned form,
18420 LONGEST implicit_const, const gdb_byte *info_ptr,
18421 bool *need_reprocess)
18422 {
18423 struct dwarf2_cu *cu = reader->cu;
18424 struct dwarf2_per_objfile *dwarf2_per_objfile
18425 = cu->per_cu->dwarf2_per_objfile;
18426 struct objfile *objfile = dwarf2_per_objfile->objfile;
18427 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18428 bfd *abfd = reader->abfd;
18429 struct comp_unit_head *cu_header = &cu->header;
18430 unsigned int bytes_read;
18431 struct dwarf_block *blk;
18432 *need_reprocess = false;
18433
18434 attr->form = (enum dwarf_form) form;
18435 switch (form)
18436 {
18437 case DW_FORM_ref_addr:
18438 if (cu->header.version == 2)
18439 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18440 &bytes_read);
18441 else
18442 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18443 &bytes_read);
18444 info_ptr += bytes_read;
18445 break;
18446 case DW_FORM_GNU_ref_alt:
18447 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18448 info_ptr += bytes_read;
18449 break;
18450 case DW_FORM_addr:
18451 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18452 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18453 info_ptr += bytes_read;
18454 break;
18455 case DW_FORM_block2:
18456 blk = dwarf_alloc_block (cu);
18457 blk->size = read_2_bytes (abfd, info_ptr);
18458 info_ptr += 2;
18459 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18460 info_ptr += blk->size;
18461 DW_BLOCK (attr) = blk;
18462 break;
18463 case DW_FORM_block4:
18464 blk = dwarf_alloc_block (cu);
18465 blk->size = read_4_bytes (abfd, info_ptr);
18466 info_ptr += 4;
18467 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18468 info_ptr += blk->size;
18469 DW_BLOCK (attr) = blk;
18470 break;
18471 case DW_FORM_data2:
18472 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18473 info_ptr += 2;
18474 break;
18475 case DW_FORM_data4:
18476 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18477 info_ptr += 4;
18478 break;
18479 case DW_FORM_data8:
18480 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18481 info_ptr += 8;
18482 break;
18483 case DW_FORM_data16:
18484 blk = dwarf_alloc_block (cu);
18485 blk->size = 16;
18486 blk->data = read_n_bytes (abfd, info_ptr, 16);
18487 info_ptr += 16;
18488 DW_BLOCK (attr) = blk;
18489 break;
18490 case DW_FORM_sec_offset:
18491 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18492 info_ptr += bytes_read;
18493 break;
18494 case DW_FORM_string:
18495 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18496 DW_STRING_IS_CANONICAL (attr) = 0;
18497 info_ptr += bytes_read;
18498 break;
18499 case DW_FORM_strp:
18500 if (!cu->per_cu->is_dwz)
18501 {
18502 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18503 abfd, info_ptr, cu_header,
18504 &bytes_read);
18505 DW_STRING_IS_CANONICAL (attr) = 0;
18506 info_ptr += bytes_read;
18507 break;
18508 }
18509 /* FALLTHROUGH */
18510 case DW_FORM_line_strp:
18511 if (!cu->per_cu->is_dwz)
18512 {
18513 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18514 abfd, info_ptr,
18515 cu_header, &bytes_read);
18516 DW_STRING_IS_CANONICAL (attr) = 0;
18517 info_ptr += bytes_read;
18518 break;
18519 }
18520 /* FALLTHROUGH */
18521 case DW_FORM_GNU_strp_alt:
18522 {
18523 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18524 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18525 &bytes_read);
18526
18527 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18528 dwz, str_offset);
18529 DW_STRING_IS_CANONICAL (attr) = 0;
18530 info_ptr += bytes_read;
18531 }
18532 break;
18533 case DW_FORM_exprloc:
18534 case DW_FORM_block:
18535 blk = dwarf_alloc_block (cu);
18536 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18537 info_ptr += bytes_read;
18538 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18539 info_ptr += blk->size;
18540 DW_BLOCK (attr) = blk;
18541 break;
18542 case DW_FORM_block1:
18543 blk = dwarf_alloc_block (cu);
18544 blk->size = read_1_byte (abfd, info_ptr);
18545 info_ptr += 1;
18546 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18547 info_ptr += blk->size;
18548 DW_BLOCK (attr) = blk;
18549 break;
18550 case DW_FORM_data1:
18551 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18552 info_ptr += 1;
18553 break;
18554 case DW_FORM_flag:
18555 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18556 info_ptr += 1;
18557 break;
18558 case DW_FORM_flag_present:
18559 DW_UNSND (attr) = 1;
18560 break;
18561 case DW_FORM_sdata:
18562 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18563 info_ptr += bytes_read;
18564 break;
18565 case DW_FORM_udata:
18566 case DW_FORM_rnglistx:
18567 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18568 info_ptr += bytes_read;
18569 break;
18570 case DW_FORM_ref1:
18571 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18572 + read_1_byte (abfd, info_ptr));
18573 info_ptr += 1;
18574 break;
18575 case DW_FORM_ref2:
18576 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18577 + read_2_bytes (abfd, info_ptr));
18578 info_ptr += 2;
18579 break;
18580 case DW_FORM_ref4:
18581 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18582 + read_4_bytes (abfd, info_ptr));
18583 info_ptr += 4;
18584 break;
18585 case DW_FORM_ref8:
18586 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18587 + read_8_bytes (abfd, info_ptr));
18588 info_ptr += 8;
18589 break;
18590 case DW_FORM_ref_sig8:
18591 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18592 info_ptr += 8;
18593 break;
18594 case DW_FORM_ref_udata:
18595 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18596 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18597 info_ptr += bytes_read;
18598 break;
18599 case DW_FORM_indirect:
18600 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18601 info_ptr += bytes_read;
18602 if (form == DW_FORM_implicit_const)
18603 {
18604 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18605 info_ptr += bytes_read;
18606 }
18607 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18608 info_ptr, need_reprocess);
18609 break;
18610 case DW_FORM_implicit_const:
18611 DW_SND (attr) = implicit_const;
18612 break;
18613 case DW_FORM_addrx:
18614 case DW_FORM_GNU_addr_index:
18615 *need_reprocess = true;
18616 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18617 info_ptr += bytes_read;
18618 break;
18619 case DW_FORM_strx:
18620 case DW_FORM_strx1:
18621 case DW_FORM_strx2:
18622 case DW_FORM_strx3:
18623 case DW_FORM_strx4:
18624 case DW_FORM_GNU_str_index:
18625 {
18626 ULONGEST str_index;
18627 if (form == DW_FORM_strx1)
18628 {
18629 str_index = read_1_byte (abfd, info_ptr);
18630 info_ptr += 1;
18631 }
18632 else if (form == DW_FORM_strx2)
18633 {
18634 str_index = read_2_bytes (abfd, info_ptr);
18635 info_ptr += 2;
18636 }
18637 else if (form == DW_FORM_strx3)
18638 {
18639 str_index = read_3_bytes (abfd, info_ptr);
18640 info_ptr += 3;
18641 }
18642 else if (form == DW_FORM_strx4)
18643 {
18644 str_index = read_4_bytes (abfd, info_ptr);
18645 info_ptr += 4;
18646 }
18647 else
18648 {
18649 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18650 info_ptr += bytes_read;
18651 }
18652 *need_reprocess = true;
18653 DW_UNSND (attr) = str_index;
18654 }
18655 break;
18656 default:
18657 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18658 dwarf_form_name (form),
18659 bfd_get_filename (abfd));
18660 }
18661
18662 /* Super hack. */
18663 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18664 attr->form = DW_FORM_GNU_ref_alt;
18665
18666 /* We have seen instances where the compiler tried to emit a byte
18667 size attribute of -1 which ended up being encoded as an unsigned
18668 0xffffffff. Although 0xffffffff is technically a valid size value,
18669 an object of this size seems pretty unlikely so we can relatively
18670 safely treat these cases as if the size attribute was invalid and
18671 treat them as zero by default. */
18672 if (attr->name == DW_AT_byte_size
18673 && form == DW_FORM_data4
18674 && DW_UNSND (attr) >= 0xffffffff)
18675 {
18676 complaint
18677 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18678 hex_string (DW_UNSND (attr)));
18679 DW_UNSND (attr) = 0;
18680 }
18681
18682 return info_ptr;
18683 }
18684
18685 /* Read an attribute described by an abbreviated attribute. */
18686
18687 static const gdb_byte *
18688 read_attribute (const struct die_reader_specs *reader,
18689 struct attribute *attr, struct attr_abbrev *abbrev,
18690 const gdb_byte *info_ptr, bool *need_reprocess)
18691 {
18692 attr->name = abbrev->name;
18693 return read_attribute_value (reader, attr, abbrev->form,
18694 abbrev->implicit_const, info_ptr,
18695 need_reprocess);
18696 }
18697
18698 /* Cover function for read_initial_length.
18699 Returns the length of the object at BUF, and stores the size of the
18700 initial length in *BYTES_READ and stores the size that offsets will be in
18701 *OFFSET_SIZE.
18702 If the initial length size is not equivalent to that specified in
18703 CU_HEADER then issue a complaint.
18704 This is useful when reading non-comp-unit headers. */
18705
18706 static LONGEST
18707 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18708 const struct comp_unit_head *cu_header,
18709 unsigned int *bytes_read,
18710 unsigned int *offset_size)
18711 {
18712 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18713
18714 gdb_assert (cu_header->initial_length_size == 4
18715 || cu_header->initial_length_size == 8
18716 || cu_header->initial_length_size == 12);
18717
18718 if (cu_header->initial_length_size != *bytes_read)
18719 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18720
18721 *offset_size = (*bytes_read == 4) ? 4 : 8;
18722 return length;
18723 }
18724
18725 /* Return pointer to string at section SECT offset STR_OFFSET with error
18726 reporting strings FORM_NAME and SECT_NAME. */
18727
18728 static const char *
18729 read_indirect_string_at_offset_from (struct objfile *objfile,
18730 bfd *abfd, LONGEST str_offset,
18731 struct dwarf2_section_info *sect,
18732 const char *form_name,
18733 const char *sect_name)
18734 {
18735 sect->read (objfile);
18736 if (sect->buffer == NULL)
18737 error (_("%s used without %s section [in module %s]"),
18738 form_name, sect_name, bfd_get_filename (abfd));
18739 if (str_offset >= sect->size)
18740 error (_("%s pointing outside of %s section [in module %s]"),
18741 form_name, sect_name, bfd_get_filename (abfd));
18742 gdb_assert (HOST_CHAR_BIT == 8);
18743 if (sect->buffer[str_offset] == '\0')
18744 return NULL;
18745 return (const char *) (sect->buffer + str_offset);
18746 }
18747
18748 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18749
18750 static const char *
18751 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18752 bfd *abfd, LONGEST str_offset)
18753 {
18754 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18755 abfd, str_offset,
18756 &dwarf2_per_objfile->str,
18757 "DW_FORM_strp", ".debug_str");
18758 }
18759
18760 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18761
18762 static const char *
18763 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18764 bfd *abfd, LONGEST str_offset)
18765 {
18766 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18767 abfd, str_offset,
18768 &dwarf2_per_objfile->line_str,
18769 "DW_FORM_line_strp",
18770 ".debug_line_str");
18771 }
18772
18773 /* Read a string at offset STR_OFFSET in the .debug_str section from
18774 the .dwz file DWZ. Throw an error if the offset is too large. If
18775 the string consists of a single NUL byte, return NULL; otherwise
18776 return a pointer to the string. */
18777
18778 static const char *
18779 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18780 LONGEST str_offset)
18781 {
18782 dwz->str.read (objfile);
18783
18784 if (dwz->str.buffer == NULL)
18785 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18786 "section [in module %s]"),
18787 bfd_get_filename (dwz->dwz_bfd.get ()));
18788 if (str_offset >= dwz->str.size)
18789 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18790 ".debug_str section [in module %s]"),
18791 bfd_get_filename (dwz->dwz_bfd.get ()));
18792 gdb_assert (HOST_CHAR_BIT == 8);
18793 if (dwz->str.buffer[str_offset] == '\0')
18794 return NULL;
18795 return (const char *) (dwz->str.buffer + str_offset);
18796 }
18797
18798 /* Return pointer to string at .debug_str offset as read from BUF.
18799 BUF is assumed to be in a compilation unit described by CU_HEADER.
18800 Return *BYTES_READ_PTR count of bytes read from BUF. */
18801
18802 static const char *
18803 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18804 const gdb_byte *buf,
18805 const struct comp_unit_head *cu_header,
18806 unsigned int *bytes_read_ptr)
18807 {
18808 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18809
18810 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18811 }
18812
18813 /* Return pointer to string at .debug_line_str offset as read from BUF.
18814 BUF is assumed to be in a compilation unit described by CU_HEADER.
18815 Return *BYTES_READ_PTR count of bytes read from BUF. */
18816
18817 static const char *
18818 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18819 bfd *abfd, const gdb_byte *buf,
18820 const struct comp_unit_head *cu_header,
18821 unsigned int *bytes_read_ptr)
18822 {
18823 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18824
18825 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18826 str_offset);
18827 }
18828
18829 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18830 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18831 ADDR_SIZE is the size of addresses from the CU header. */
18832
18833 static CORE_ADDR
18834 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18835 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18836 int addr_size)
18837 {
18838 struct objfile *objfile = dwarf2_per_objfile->objfile;
18839 bfd *abfd = objfile->obfd;
18840 const gdb_byte *info_ptr;
18841 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18842
18843 dwarf2_per_objfile->addr.read (objfile);
18844 if (dwarf2_per_objfile->addr.buffer == NULL)
18845 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18846 objfile_name (objfile));
18847 if (addr_base_or_zero + addr_index * addr_size
18848 >= dwarf2_per_objfile->addr.size)
18849 error (_("DW_FORM_addr_index pointing outside of "
18850 ".debug_addr section [in module %s]"),
18851 objfile_name (objfile));
18852 info_ptr = (dwarf2_per_objfile->addr.buffer
18853 + addr_base_or_zero + addr_index * addr_size);
18854 if (addr_size == 4)
18855 return bfd_get_32 (abfd, info_ptr);
18856 else
18857 return bfd_get_64 (abfd, info_ptr);
18858 }
18859
18860 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18861
18862 static CORE_ADDR
18863 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18864 {
18865 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18866 cu->addr_base, cu->header.addr_size);
18867 }
18868
18869 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18870
18871 static CORE_ADDR
18872 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18873 unsigned int *bytes_read)
18874 {
18875 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18876 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18877
18878 return read_addr_index (cu, addr_index);
18879 }
18880
18881 /* Given an index in .debug_addr, fetch the value.
18882 NOTE: This can be called during dwarf expression evaluation,
18883 long after the debug information has been read, and thus per_cu->cu
18884 may no longer exist. */
18885
18886 CORE_ADDR
18887 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18888 unsigned int addr_index)
18889 {
18890 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18891 struct dwarf2_cu *cu = per_cu->cu;
18892 gdb::optional<ULONGEST> addr_base;
18893 int addr_size;
18894
18895 /* We need addr_base and addr_size.
18896 If we don't have PER_CU->cu, we have to get it.
18897 Nasty, but the alternative is storing the needed info in PER_CU,
18898 which at this point doesn't seem justified: it's not clear how frequently
18899 it would get used and it would increase the size of every PER_CU.
18900 Entry points like dwarf2_per_cu_addr_size do a similar thing
18901 so we're not in uncharted territory here.
18902 Alas we need to be a bit more complicated as addr_base is contained
18903 in the DIE.
18904
18905 We don't need to read the entire CU(/TU).
18906 We just need the header and top level die.
18907
18908 IWBN to use the aging mechanism to let us lazily later discard the CU.
18909 For now we skip this optimization. */
18910
18911 if (cu != NULL)
18912 {
18913 addr_base = cu->addr_base;
18914 addr_size = cu->header.addr_size;
18915 }
18916 else
18917 {
18918 cutu_reader reader (per_cu, NULL, 0, false);
18919 addr_base = reader.cu->addr_base;
18920 addr_size = reader.cu->header.addr_size;
18921 }
18922
18923 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18924 addr_size);
18925 }
18926
18927 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18928 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18929 DWO file. */
18930
18931 static const char *
18932 read_str_index (struct dwarf2_cu *cu,
18933 struct dwarf2_section_info *str_section,
18934 struct dwarf2_section_info *str_offsets_section,
18935 ULONGEST str_offsets_base, ULONGEST str_index)
18936 {
18937 struct dwarf2_per_objfile *dwarf2_per_objfile
18938 = cu->per_cu->dwarf2_per_objfile;
18939 struct objfile *objfile = dwarf2_per_objfile->objfile;
18940 const char *objf_name = objfile_name (objfile);
18941 bfd *abfd = objfile->obfd;
18942 const gdb_byte *info_ptr;
18943 ULONGEST str_offset;
18944 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18945
18946 str_section->read (objfile);
18947 str_offsets_section->read (objfile);
18948 if (str_section->buffer == NULL)
18949 error (_("%s used without %s section"
18950 " in CU at offset %s [in module %s]"),
18951 form_name, str_section->get_name (),
18952 sect_offset_str (cu->header.sect_off), objf_name);
18953 if (str_offsets_section->buffer == NULL)
18954 error (_("%s used without %s section"
18955 " in CU at offset %s [in module %s]"),
18956 form_name, str_section->get_name (),
18957 sect_offset_str (cu->header.sect_off), objf_name);
18958 info_ptr = (str_offsets_section->buffer
18959 + str_offsets_base
18960 + str_index * cu->header.offset_size);
18961 if (cu->header.offset_size == 4)
18962 str_offset = bfd_get_32 (abfd, info_ptr);
18963 else
18964 str_offset = bfd_get_64 (abfd, info_ptr);
18965 if (str_offset >= str_section->size)
18966 error (_("Offset from %s pointing outside of"
18967 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18968 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18969 return (const char *) (str_section->buffer + str_offset);
18970 }
18971
18972 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18973
18974 static const char *
18975 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18976 {
18977 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18978 ? reader->cu->header.addr_size : 0;
18979 return read_str_index (reader->cu,
18980 &reader->dwo_file->sections.str,
18981 &reader->dwo_file->sections.str_offsets,
18982 str_offsets_base, str_index);
18983 }
18984
18985 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18986
18987 static const char *
18988 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18989 {
18990 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18991 const char *objf_name = objfile_name (objfile);
18992 static const char form_name[] = "DW_FORM_GNU_str_index";
18993 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18994
18995 if (!cu->str_offsets_base.has_value ())
18996 error (_("%s used in Fission stub without %s"
18997 " in CU at offset 0x%lx [in module %s]"),
18998 form_name, str_offsets_attr_name,
18999 (long) cu->header.offset_size, objf_name);
19000
19001 return read_str_index (cu,
19002 &cu->per_cu->dwarf2_per_objfile->str,
19003 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19004 *cu->str_offsets_base, str_index);
19005 }
19006
19007 /* Return the length of an LEB128 number in BUF. */
19008
19009 static int
19010 leb128_size (const gdb_byte *buf)
19011 {
19012 const gdb_byte *begin = buf;
19013 gdb_byte byte;
19014
19015 while (1)
19016 {
19017 byte = *buf++;
19018 if ((byte & 128) == 0)
19019 return buf - begin;
19020 }
19021 }
19022
19023 static void
19024 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19025 {
19026 switch (lang)
19027 {
19028 case DW_LANG_C89:
19029 case DW_LANG_C99:
19030 case DW_LANG_C11:
19031 case DW_LANG_C:
19032 case DW_LANG_UPC:
19033 cu->language = language_c;
19034 break;
19035 case DW_LANG_Java:
19036 case DW_LANG_C_plus_plus:
19037 case DW_LANG_C_plus_plus_11:
19038 case DW_LANG_C_plus_plus_14:
19039 cu->language = language_cplus;
19040 break;
19041 case DW_LANG_D:
19042 cu->language = language_d;
19043 break;
19044 case DW_LANG_Fortran77:
19045 case DW_LANG_Fortran90:
19046 case DW_LANG_Fortran95:
19047 case DW_LANG_Fortran03:
19048 case DW_LANG_Fortran08:
19049 cu->language = language_fortran;
19050 break;
19051 case DW_LANG_Go:
19052 cu->language = language_go;
19053 break;
19054 case DW_LANG_Mips_Assembler:
19055 cu->language = language_asm;
19056 break;
19057 case DW_LANG_Ada83:
19058 case DW_LANG_Ada95:
19059 cu->language = language_ada;
19060 break;
19061 case DW_LANG_Modula2:
19062 cu->language = language_m2;
19063 break;
19064 case DW_LANG_Pascal83:
19065 cu->language = language_pascal;
19066 break;
19067 case DW_LANG_ObjC:
19068 cu->language = language_objc;
19069 break;
19070 case DW_LANG_Rust:
19071 case DW_LANG_Rust_old:
19072 cu->language = language_rust;
19073 break;
19074 case DW_LANG_Cobol74:
19075 case DW_LANG_Cobol85:
19076 default:
19077 cu->language = language_minimal;
19078 break;
19079 }
19080 cu->language_defn = language_def (cu->language);
19081 }
19082
19083 /* Return the named attribute or NULL if not there. */
19084
19085 static struct attribute *
19086 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19087 {
19088 for (;;)
19089 {
19090 unsigned int i;
19091 struct attribute *spec = NULL;
19092
19093 for (i = 0; i < die->num_attrs; ++i)
19094 {
19095 if (die->attrs[i].name == name)
19096 return &die->attrs[i];
19097 if (die->attrs[i].name == DW_AT_specification
19098 || die->attrs[i].name == DW_AT_abstract_origin)
19099 spec = &die->attrs[i];
19100 }
19101
19102 if (!spec)
19103 break;
19104
19105 die = follow_die_ref (die, spec, &cu);
19106 }
19107
19108 return NULL;
19109 }
19110
19111 /* Return the named attribute or NULL if not there,
19112 but do not follow DW_AT_specification, etc.
19113 This is for use in contexts where we're reading .debug_types dies.
19114 Following DW_AT_specification, DW_AT_abstract_origin will take us
19115 back up the chain, and we want to go down. */
19116
19117 static struct attribute *
19118 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19119 {
19120 unsigned int i;
19121
19122 for (i = 0; i < die->num_attrs; ++i)
19123 if (die->attrs[i].name == name)
19124 return &die->attrs[i];
19125
19126 return NULL;
19127 }
19128
19129 /* Return the string associated with a string-typed attribute, or NULL if it
19130 is either not found or is of an incorrect type. */
19131
19132 static const char *
19133 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19134 {
19135 struct attribute *attr;
19136 const char *str = NULL;
19137
19138 attr = dwarf2_attr (die, name, cu);
19139
19140 if (attr != NULL)
19141 {
19142 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19143 || attr->form == DW_FORM_string
19144 || attr->form == DW_FORM_strx
19145 || attr->form == DW_FORM_strx1
19146 || attr->form == DW_FORM_strx2
19147 || attr->form == DW_FORM_strx3
19148 || attr->form == DW_FORM_strx4
19149 || attr->form == DW_FORM_GNU_str_index
19150 || attr->form == DW_FORM_GNU_strp_alt)
19151 str = DW_STRING (attr);
19152 else
19153 complaint (_("string type expected for attribute %s for "
19154 "DIE at %s in module %s"),
19155 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19156 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19157 }
19158
19159 return str;
19160 }
19161
19162 /* Return the dwo name or NULL if not present. If present, it is in either
19163 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19164 static const char *
19165 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19166 {
19167 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19168 if (dwo_name == nullptr)
19169 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19170 return dwo_name;
19171 }
19172
19173 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19174 and holds a non-zero value. This function should only be used for
19175 DW_FORM_flag or DW_FORM_flag_present attributes. */
19176
19177 static int
19178 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19179 {
19180 struct attribute *attr = dwarf2_attr (die, name, cu);
19181
19182 return (attr && DW_UNSND (attr));
19183 }
19184
19185 static int
19186 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19187 {
19188 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19189 which value is non-zero. However, we have to be careful with
19190 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19191 (via dwarf2_flag_true_p) follows this attribute. So we may
19192 end up accidently finding a declaration attribute that belongs
19193 to a different DIE referenced by the specification attribute,
19194 even though the given DIE does not have a declaration attribute. */
19195 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19196 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19197 }
19198
19199 /* Return the die giving the specification for DIE, if there is
19200 one. *SPEC_CU is the CU containing DIE on input, and the CU
19201 containing the return value on output. If there is no
19202 specification, but there is an abstract origin, that is
19203 returned. */
19204
19205 static struct die_info *
19206 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19207 {
19208 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19209 *spec_cu);
19210
19211 if (spec_attr == NULL)
19212 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19213
19214 if (spec_attr == NULL)
19215 return NULL;
19216 else
19217 return follow_die_ref (die, spec_attr, spec_cu);
19218 }
19219
19220 /* Stub for free_line_header to match void * callback types. */
19221
19222 static void
19223 free_line_header_voidp (void *arg)
19224 {
19225 struct line_header *lh = (struct line_header *) arg;
19226
19227 delete lh;
19228 }
19229
19230 /* A convenience function to find the proper .debug_line section for a CU. */
19231
19232 static struct dwarf2_section_info *
19233 get_debug_line_section (struct dwarf2_cu *cu)
19234 {
19235 struct dwarf2_section_info *section;
19236 struct dwarf2_per_objfile *dwarf2_per_objfile
19237 = cu->per_cu->dwarf2_per_objfile;
19238
19239 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19240 DWO file. */
19241 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19242 section = &cu->dwo_unit->dwo_file->sections.line;
19243 else if (cu->per_cu->is_dwz)
19244 {
19245 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19246
19247 section = &dwz->line;
19248 }
19249 else
19250 section = &dwarf2_per_objfile->line;
19251
19252 return section;
19253 }
19254
19255 /* Read directory or file name entry format, starting with byte of
19256 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19257 entries count and the entries themselves in the described entry
19258 format. */
19259
19260 static void
19261 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19262 bfd *abfd, const gdb_byte **bufp,
19263 struct line_header *lh,
19264 const struct comp_unit_head *cu_header,
19265 void (*callback) (struct line_header *lh,
19266 const char *name,
19267 dir_index d_index,
19268 unsigned int mod_time,
19269 unsigned int length))
19270 {
19271 gdb_byte format_count, formati;
19272 ULONGEST data_count, datai;
19273 const gdb_byte *buf = *bufp;
19274 const gdb_byte *format_header_data;
19275 unsigned int bytes_read;
19276
19277 format_count = read_1_byte (abfd, buf);
19278 buf += 1;
19279 format_header_data = buf;
19280 for (formati = 0; formati < format_count; formati++)
19281 {
19282 read_unsigned_leb128 (abfd, buf, &bytes_read);
19283 buf += bytes_read;
19284 read_unsigned_leb128 (abfd, buf, &bytes_read);
19285 buf += bytes_read;
19286 }
19287
19288 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19289 buf += bytes_read;
19290 for (datai = 0; datai < data_count; datai++)
19291 {
19292 const gdb_byte *format = format_header_data;
19293 struct file_entry fe;
19294
19295 for (formati = 0; formati < format_count; formati++)
19296 {
19297 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19298 format += bytes_read;
19299
19300 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19301 format += bytes_read;
19302
19303 gdb::optional<const char *> string;
19304 gdb::optional<unsigned int> uint;
19305
19306 switch (form)
19307 {
19308 case DW_FORM_string:
19309 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19310 buf += bytes_read;
19311 break;
19312
19313 case DW_FORM_line_strp:
19314 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19315 abfd, buf,
19316 cu_header,
19317 &bytes_read));
19318 buf += bytes_read;
19319 break;
19320
19321 case DW_FORM_data1:
19322 uint.emplace (read_1_byte (abfd, buf));
19323 buf += 1;
19324 break;
19325
19326 case DW_FORM_data2:
19327 uint.emplace (read_2_bytes (abfd, buf));
19328 buf += 2;
19329 break;
19330
19331 case DW_FORM_data4:
19332 uint.emplace (read_4_bytes (abfd, buf));
19333 buf += 4;
19334 break;
19335
19336 case DW_FORM_data8:
19337 uint.emplace (read_8_bytes (abfd, buf));
19338 buf += 8;
19339 break;
19340
19341 case DW_FORM_data16:
19342 /* This is used for MD5, but file_entry does not record MD5s. */
19343 buf += 16;
19344 break;
19345
19346 case DW_FORM_udata:
19347 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19348 buf += bytes_read;
19349 break;
19350
19351 case DW_FORM_block:
19352 /* It is valid only for DW_LNCT_timestamp which is ignored by
19353 current GDB. */
19354 break;
19355 }
19356
19357 switch (content_type)
19358 {
19359 case DW_LNCT_path:
19360 if (string.has_value ())
19361 fe.name = *string;
19362 break;
19363 case DW_LNCT_directory_index:
19364 if (uint.has_value ())
19365 fe.d_index = (dir_index) *uint;
19366 break;
19367 case DW_LNCT_timestamp:
19368 if (uint.has_value ())
19369 fe.mod_time = *uint;
19370 break;
19371 case DW_LNCT_size:
19372 if (uint.has_value ())
19373 fe.length = *uint;
19374 break;
19375 case DW_LNCT_MD5:
19376 break;
19377 default:
19378 complaint (_("Unknown format content type %s"),
19379 pulongest (content_type));
19380 }
19381 }
19382
19383 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19384 }
19385
19386 *bufp = buf;
19387 }
19388
19389 /* Read the statement program header starting at OFFSET in
19390 .debug_line, or .debug_line.dwo. Return a pointer
19391 to a struct line_header, allocated using xmalloc.
19392 Returns NULL if there is a problem reading the header, e.g., if it
19393 has a version we don't understand.
19394
19395 NOTE: the strings in the include directory and file name tables of
19396 the returned object point into the dwarf line section buffer,
19397 and must not be freed. */
19398
19399 static line_header_up
19400 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19401 {
19402 const gdb_byte *line_ptr;
19403 unsigned int bytes_read, offset_size;
19404 int i;
19405 const char *cur_dir, *cur_file;
19406 struct dwarf2_section_info *section;
19407 bfd *abfd;
19408 struct dwarf2_per_objfile *dwarf2_per_objfile
19409 = cu->per_cu->dwarf2_per_objfile;
19410
19411 section = get_debug_line_section (cu);
19412 section->read (dwarf2_per_objfile->objfile);
19413 if (section->buffer == NULL)
19414 {
19415 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19416 complaint (_("missing .debug_line.dwo section"));
19417 else
19418 complaint (_("missing .debug_line section"));
19419 return 0;
19420 }
19421
19422 /* We can't do this until we know the section is non-empty.
19423 Only then do we know we have such a section. */
19424 abfd = section->get_bfd_owner ();
19425
19426 /* Make sure that at least there's room for the total_length field.
19427 That could be 12 bytes long, but we're just going to fudge that. */
19428 if (to_underlying (sect_off) + 4 >= section->size)
19429 {
19430 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19431 return 0;
19432 }
19433
19434 line_header_up lh (new line_header ());
19435
19436 lh->sect_off = sect_off;
19437 lh->offset_in_dwz = cu->per_cu->is_dwz;
19438
19439 line_ptr = section->buffer + to_underlying (sect_off);
19440
19441 /* Read in the header. */
19442 lh->total_length =
19443 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19444 &bytes_read, &offset_size);
19445 line_ptr += bytes_read;
19446
19447 const gdb_byte *start_here = line_ptr;
19448
19449 if (line_ptr + lh->total_length > (section->buffer + section->size))
19450 {
19451 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19452 return 0;
19453 }
19454 lh->statement_program_end = start_here + lh->total_length;
19455 lh->version = read_2_bytes (abfd, line_ptr);
19456 line_ptr += 2;
19457 if (lh->version > 5)
19458 {
19459 /* This is a version we don't understand. The format could have
19460 changed in ways we don't handle properly so just punt. */
19461 complaint (_("unsupported version in .debug_line section"));
19462 return NULL;
19463 }
19464 if (lh->version >= 5)
19465 {
19466 gdb_byte segment_selector_size;
19467
19468 /* Skip address size. */
19469 read_1_byte (abfd, line_ptr);
19470 line_ptr += 1;
19471
19472 segment_selector_size = read_1_byte (abfd, line_ptr);
19473 line_ptr += 1;
19474 if (segment_selector_size != 0)
19475 {
19476 complaint (_("unsupported segment selector size %u "
19477 "in .debug_line section"),
19478 segment_selector_size);
19479 return NULL;
19480 }
19481 }
19482 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19483 line_ptr += offset_size;
19484 lh->statement_program_start = line_ptr + lh->header_length;
19485 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19486 line_ptr += 1;
19487 if (lh->version >= 4)
19488 {
19489 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19490 line_ptr += 1;
19491 }
19492 else
19493 lh->maximum_ops_per_instruction = 1;
19494
19495 if (lh->maximum_ops_per_instruction == 0)
19496 {
19497 lh->maximum_ops_per_instruction = 1;
19498 complaint (_("invalid maximum_ops_per_instruction "
19499 "in `.debug_line' section"));
19500 }
19501
19502 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19503 line_ptr += 1;
19504 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19505 line_ptr += 1;
19506 lh->line_range = read_1_byte (abfd, line_ptr);
19507 line_ptr += 1;
19508 lh->opcode_base = read_1_byte (abfd, line_ptr);
19509 line_ptr += 1;
19510 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19511
19512 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19513 for (i = 1; i < lh->opcode_base; ++i)
19514 {
19515 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19516 line_ptr += 1;
19517 }
19518
19519 if (lh->version >= 5)
19520 {
19521 /* Read directory table. */
19522 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19523 &cu->header,
19524 [] (struct line_header *header, const char *name,
19525 dir_index d_index, unsigned int mod_time,
19526 unsigned int length)
19527 {
19528 header->add_include_dir (name);
19529 });
19530
19531 /* Read file name table. */
19532 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19533 &cu->header,
19534 [] (struct line_header *header, const char *name,
19535 dir_index d_index, unsigned int mod_time,
19536 unsigned int length)
19537 {
19538 header->add_file_name (name, d_index, mod_time, length);
19539 });
19540 }
19541 else
19542 {
19543 /* Read directory table. */
19544 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19545 {
19546 line_ptr += bytes_read;
19547 lh->add_include_dir (cur_dir);
19548 }
19549 line_ptr += bytes_read;
19550
19551 /* Read file name table. */
19552 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19553 {
19554 unsigned int mod_time, length;
19555 dir_index d_index;
19556
19557 line_ptr += bytes_read;
19558 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19559 line_ptr += bytes_read;
19560 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19561 line_ptr += bytes_read;
19562 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19563 line_ptr += bytes_read;
19564
19565 lh->add_file_name (cur_file, d_index, mod_time, length);
19566 }
19567 line_ptr += bytes_read;
19568 }
19569
19570 if (line_ptr > (section->buffer + section->size))
19571 complaint (_("line number info header doesn't "
19572 "fit in `.debug_line' section"));
19573
19574 return lh;
19575 }
19576
19577 /* Subroutine of dwarf_decode_lines to simplify it.
19578 Return the file name of the psymtab for the given file_entry.
19579 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19580 If space for the result is malloc'd, *NAME_HOLDER will be set.
19581 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19582
19583 static const char *
19584 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19585 const dwarf2_psymtab *pst,
19586 const char *comp_dir,
19587 gdb::unique_xmalloc_ptr<char> *name_holder)
19588 {
19589 const char *include_name = fe.name;
19590 const char *include_name_to_compare = include_name;
19591 const char *pst_filename;
19592 int file_is_pst;
19593
19594 const char *dir_name = fe.include_dir (lh);
19595
19596 gdb::unique_xmalloc_ptr<char> hold_compare;
19597 if (!IS_ABSOLUTE_PATH (include_name)
19598 && (dir_name != NULL || comp_dir != NULL))
19599 {
19600 /* Avoid creating a duplicate psymtab for PST.
19601 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19602 Before we do the comparison, however, we need to account
19603 for DIR_NAME and COMP_DIR.
19604 First prepend dir_name (if non-NULL). If we still don't
19605 have an absolute path prepend comp_dir (if non-NULL).
19606 However, the directory we record in the include-file's
19607 psymtab does not contain COMP_DIR (to match the
19608 corresponding symtab(s)).
19609
19610 Example:
19611
19612 bash$ cd /tmp
19613 bash$ gcc -g ./hello.c
19614 include_name = "hello.c"
19615 dir_name = "."
19616 DW_AT_comp_dir = comp_dir = "/tmp"
19617 DW_AT_name = "./hello.c"
19618
19619 */
19620
19621 if (dir_name != NULL)
19622 {
19623 name_holder->reset (concat (dir_name, SLASH_STRING,
19624 include_name, (char *) NULL));
19625 include_name = name_holder->get ();
19626 include_name_to_compare = include_name;
19627 }
19628 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19629 {
19630 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19631 include_name, (char *) NULL));
19632 include_name_to_compare = hold_compare.get ();
19633 }
19634 }
19635
19636 pst_filename = pst->filename;
19637 gdb::unique_xmalloc_ptr<char> copied_name;
19638 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19639 {
19640 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19641 pst_filename, (char *) NULL));
19642 pst_filename = copied_name.get ();
19643 }
19644
19645 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19646
19647 if (file_is_pst)
19648 return NULL;
19649 return include_name;
19650 }
19651
19652 /* State machine to track the state of the line number program. */
19653
19654 class lnp_state_machine
19655 {
19656 public:
19657 /* Initialize a machine state for the start of a line number
19658 program. */
19659 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19660 bool record_lines_p);
19661
19662 file_entry *current_file ()
19663 {
19664 /* lh->file_names is 0-based, but the file name numbers in the
19665 statement program are 1-based. */
19666 return m_line_header->file_name_at (m_file);
19667 }
19668
19669 /* Record the line in the state machine. END_SEQUENCE is true if
19670 we're processing the end of a sequence. */
19671 void record_line (bool end_sequence);
19672
19673 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19674 nop-out rest of the lines in this sequence. */
19675 void check_line_address (struct dwarf2_cu *cu,
19676 const gdb_byte *line_ptr,
19677 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19678
19679 void handle_set_discriminator (unsigned int discriminator)
19680 {
19681 m_discriminator = discriminator;
19682 m_line_has_non_zero_discriminator |= discriminator != 0;
19683 }
19684
19685 /* Handle DW_LNE_set_address. */
19686 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19687 {
19688 m_op_index = 0;
19689 address += baseaddr;
19690 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19691 }
19692
19693 /* Handle DW_LNS_advance_pc. */
19694 void handle_advance_pc (CORE_ADDR adjust);
19695
19696 /* Handle a special opcode. */
19697 void handle_special_opcode (unsigned char op_code);
19698
19699 /* Handle DW_LNS_advance_line. */
19700 void handle_advance_line (int line_delta)
19701 {
19702 advance_line (line_delta);
19703 }
19704
19705 /* Handle DW_LNS_set_file. */
19706 void handle_set_file (file_name_index file);
19707
19708 /* Handle DW_LNS_negate_stmt. */
19709 void handle_negate_stmt ()
19710 {
19711 m_is_stmt = !m_is_stmt;
19712 }
19713
19714 /* Handle DW_LNS_const_add_pc. */
19715 void handle_const_add_pc ();
19716
19717 /* Handle DW_LNS_fixed_advance_pc. */
19718 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19719 {
19720 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19721 m_op_index = 0;
19722 }
19723
19724 /* Handle DW_LNS_copy. */
19725 void handle_copy ()
19726 {
19727 record_line (false);
19728 m_discriminator = 0;
19729 }
19730
19731 /* Handle DW_LNE_end_sequence. */
19732 void handle_end_sequence ()
19733 {
19734 m_currently_recording_lines = true;
19735 }
19736
19737 private:
19738 /* Advance the line by LINE_DELTA. */
19739 void advance_line (int line_delta)
19740 {
19741 m_line += line_delta;
19742
19743 if (line_delta != 0)
19744 m_line_has_non_zero_discriminator = m_discriminator != 0;
19745 }
19746
19747 struct dwarf2_cu *m_cu;
19748
19749 gdbarch *m_gdbarch;
19750
19751 /* True if we're recording lines.
19752 Otherwise we're building partial symtabs and are just interested in
19753 finding include files mentioned by the line number program. */
19754 bool m_record_lines_p;
19755
19756 /* The line number header. */
19757 line_header *m_line_header;
19758
19759 /* These are part of the standard DWARF line number state machine,
19760 and initialized according to the DWARF spec. */
19761
19762 unsigned char m_op_index = 0;
19763 /* The line table index of the current file. */
19764 file_name_index m_file = 1;
19765 unsigned int m_line = 1;
19766
19767 /* These are initialized in the constructor. */
19768
19769 CORE_ADDR m_address;
19770 bool m_is_stmt;
19771 unsigned int m_discriminator;
19772
19773 /* Additional bits of state we need to track. */
19774
19775 /* The last file that we called dwarf2_start_subfile for.
19776 This is only used for TLLs. */
19777 unsigned int m_last_file = 0;
19778 /* The last file a line number was recorded for. */
19779 struct subfile *m_last_subfile = NULL;
19780
19781 /* When true, record the lines we decode. */
19782 bool m_currently_recording_lines = false;
19783
19784 /* The last line number that was recorded, used to coalesce
19785 consecutive entries for the same line. This can happen, for
19786 example, when discriminators are present. PR 17276. */
19787 unsigned int m_last_line = 0;
19788 bool m_line_has_non_zero_discriminator = false;
19789 };
19790
19791 void
19792 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19793 {
19794 CORE_ADDR addr_adj = (((m_op_index + adjust)
19795 / m_line_header->maximum_ops_per_instruction)
19796 * m_line_header->minimum_instruction_length);
19797 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19798 m_op_index = ((m_op_index + adjust)
19799 % m_line_header->maximum_ops_per_instruction);
19800 }
19801
19802 void
19803 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19804 {
19805 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19806 CORE_ADDR addr_adj = (((m_op_index
19807 + (adj_opcode / m_line_header->line_range))
19808 / m_line_header->maximum_ops_per_instruction)
19809 * m_line_header->minimum_instruction_length);
19810 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19811 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19812 % m_line_header->maximum_ops_per_instruction);
19813
19814 int line_delta = (m_line_header->line_base
19815 + (adj_opcode % m_line_header->line_range));
19816 advance_line (line_delta);
19817 record_line (false);
19818 m_discriminator = 0;
19819 }
19820
19821 void
19822 lnp_state_machine::handle_set_file (file_name_index file)
19823 {
19824 m_file = file;
19825
19826 const file_entry *fe = current_file ();
19827 if (fe == NULL)
19828 dwarf2_debug_line_missing_file_complaint ();
19829 else if (m_record_lines_p)
19830 {
19831 const char *dir = fe->include_dir (m_line_header);
19832
19833 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19834 m_line_has_non_zero_discriminator = m_discriminator != 0;
19835 dwarf2_start_subfile (m_cu, fe->name, dir);
19836 }
19837 }
19838
19839 void
19840 lnp_state_machine::handle_const_add_pc ()
19841 {
19842 CORE_ADDR adjust
19843 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19844
19845 CORE_ADDR addr_adj
19846 = (((m_op_index + adjust)
19847 / m_line_header->maximum_ops_per_instruction)
19848 * m_line_header->minimum_instruction_length);
19849
19850 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19851 m_op_index = ((m_op_index + adjust)
19852 % m_line_header->maximum_ops_per_instruction);
19853 }
19854
19855 /* Return non-zero if we should add LINE to the line number table.
19856 LINE is the line to add, LAST_LINE is the last line that was added,
19857 LAST_SUBFILE is the subfile for LAST_LINE.
19858 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19859 had a non-zero discriminator.
19860
19861 We have to be careful in the presence of discriminators.
19862 E.g., for this line:
19863
19864 for (i = 0; i < 100000; i++);
19865
19866 clang can emit four line number entries for that one line,
19867 each with a different discriminator.
19868 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19869
19870 However, we want gdb to coalesce all four entries into one.
19871 Otherwise the user could stepi into the middle of the line and
19872 gdb would get confused about whether the pc really was in the
19873 middle of the line.
19874
19875 Things are further complicated by the fact that two consecutive
19876 line number entries for the same line is a heuristic used by gcc
19877 to denote the end of the prologue. So we can't just discard duplicate
19878 entries, we have to be selective about it. The heuristic we use is
19879 that we only collapse consecutive entries for the same line if at least
19880 one of those entries has a non-zero discriminator. PR 17276.
19881
19882 Note: Addresses in the line number state machine can never go backwards
19883 within one sequence, thus this coalescing is ok. */
19884
19885 static int
19886 dwarf_record_line_p (struct dwarf2_cu *cu,
19887 unsigned int line, unsigned int last_line,
19888 int line_has_non_zero_discriminator,
19889 struct subfile *last_subfile)
19890 {
19891 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19892 return 1;
19893 if (line != last_line)
19894 return 1;
19895 /* Same line for the same file that we've seen already.
19896 As a last check, for pr 17276, only record the line if the line
19897 has never had a non-zero discriminator. */
19898 if (!line_has_non_zero_discriminator)
19899 return 1;
19900 return 0;
19901 }
19902
19903 /* Use the CU's builder to record line number LINE beginning at
19904 address ADDRESS in the line table of subfile SUBFILE. */
19905
19906 static void
19907 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19908 unsigned int line, CORE_ADDR address,
19909 struct dwarf2_cu *cu)
19910 {
19911 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19912
19913 if (dwarf_line_debug)
19914 {
19915 fprintf_unfiltered (gdb_stdlog,
19916 "Recording line %u, file %s, address %s\n",
19917 line, lbasename (subfile->name),
19918 paddress (gdbarch, address));
19919 }
19920
19921 if (cu != nullptr)
19922 cu->get_builder ()->record_line (subfile, line, addr);
19923 }
19924
19925 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19926 Mark the end of a set of line number records.
19927 The arguments are the same as for dwarf_record_line_1.
19928 If SUBFILE is NULL the request is ignored. */
19929
19930 static void
19931 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19932 CORE_ADDR address, struct dwarf2_cu *cu)
19933 {
19934 if (subfile == NULL)
19935 return;
19936
19937 if (dwarf_line_debug)
19938 {
19939 fprintf_unfiltered (gdb_stdlog,
19940 "Finishing current line, file %s, address %s\n",
19941 lbasename (subfile->name),
19942 paddress (gdbarch, address));
19943 }
19944
19945 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
19946 }
19947
19948 void
19949 lnp_state_machine::record_line (bool end_sequence)
19950 {
19951 if (dwarf_line_debug)
19952 {
19953 fprintf_unfiltered (gdb_stdlog,
19954 "Processing actual line %u: file %u,"
19955 " address %s, is_stmt %u, discrim %u%s\n",
19956 m_line, m_file,
19957 paddress (m_gdbarch, m_address),
19958 m_is_stmt, m_discriminator,
19959 (end_sequence ? "\t(end sequence)" : ""));
19960 }
19961
19962 file_entry *fe = current_file ();
19963
19964 if (fe == NULL)
19965 dwarf2_debug_line_missing_file_complaint ();
19966 /* For now we ignore lines not starting on an instruction boundary.
19967 But not when processing end_sequence for compatibility with the
19968 previous version of the code. */
19969 else if (m_op_index == 0 || end_sequence)
19970 {
19971 fe->included_p = 1;
19972 if (m_record_lines_p
19973 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
19974 {
19975 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19976 || end_sequence)
19977 {
19978 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19979 m_currently_recording_lines ? m_cu : nullptr);
19980 }
19981
19982 if (!end_sequence)
19983 {
19984 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19985 m_line_has_non_zero_discriminator,
19986 m_last_subfile))
19987 {
19988 buildsym_compunit *builder = m_cu->get_builder ();
19989 dwarf_record_line_1 (m_gdbarch,
19990 builder->get_current_subfile (),
19991 m_line, m_address,
19992 m_currently_recording_lines ? m_cu : nullptr);
19993 }
19994 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19995 m_last_line = m_line;
19996 }
19997 }
19998 }
19999 }
20000
20001 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20002 line_header *lh, bool record_lines_p)
20003 {
20004 m_cu = cu;
20005 m_gdbarch = arch;
20006 m_record_lines_p = record_lines_p;
20007 m_line_header = lh;
20008
20009 m_currently_recording_lines = true;
20010
20011 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20012 was a line entry for it so that the backend has a chance to adjust it
20013 and also record it in case it needs it. This is currently used by MIPS
20014 code, cf. `mips_adjust_dwarf2_line'. */
20015 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20016 m_is_stmt = lh->default_is_stmt;
20017 m_discriminator = 0;
20018 }
20019
20020 void
20021 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20022 const gdb_byte *line_ptr,
20023 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20024 {
20025 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20026 the pc range of the CU. However, we restrict the test to only ADDRESS
20027 values of zero to preserve GDB's previous behaviour which is to handle
20028 the specific case of a function being GC'd by the linker. */
20029
20030 if (address == 0 && address < unrelocated_lowpc)
20031 {
20032 /* This line table is for a function which has been
20033 GCd by the linker. Ignore it. PR gdb/12528 */
20034
20035 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20036 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20037
20038 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20039 line_offset, objfile_name (objfile));
20040 m_currently_recording_lines = false;
20041 /* Note: m_currently_recording_lines is left as false until we see
20042 DW_LNE_end_sequence. */
20043 }
20044 }
20045
20046 /* Subroutine of dwarf_decode_lines to simplify it.
20047 Process the line number information in LH.
20048 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20049 program in order to set included_p for every referenced header. */
20050
20051 static void
20052 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20053 const int decode_for_pst_p, CORE_ADDR lowpc)
20054 {
20055 const gdb_byte *line_ptr, *extended_end;
20056 const gdb_byte *line_end;
20057 unsigned int bytes_read, extended_len;
20058 unsigned char op_code, extended_op;
20059 CORE_ADDR baseaddr;
20060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20061 bfd *abfd = objfile->obfd;
20062 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20063 /* True if we're recording line info (as opposed to building partial
20064 symtabs and just interested in finding include files mentioned by
20065 the line number program). */
20066 bool record_lines_p = !decode_for_pst_p;
20067
20068 baseaddr = objfile->text_section_offset ();
20069
20070 line_ptr = lh->statement_program_start;
20071 line_end = lh->statement_program_end;
20072
20073 /* Read the statement sequences until there's nothing left. */
20074 while (line_ptr < line_end)
20075 {
20076 /* The DWARF line number program state machine. Reset the state
20077 machine at the start of each sequence. */
20078 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20079 bool end_sequence = false;
20080
20081 if (record_lines_p)
20082 {
20083 /* Start a subfile for the current file of the state
20084 machine. */
20085 const file_entry *fe = state_machine.current_file ();
20086
20087 if (fe != NULL)
20088 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20089 }
20090
20091 /* Decode the table. */
20092 while (line_ptr < line_end && !end_sequence)
20093 {
20094 op_code = read_1_byte (abfd, line_ptr);
20095 line_ptr += 1;
20096
20097 if (op_code >= lh->opcode_base)
20098 {
20099 /* Special opcode. */
20100 state_machine.handle_special_opcode (op_code);
20101 }
20102 else switch (op_code)
20103 {
20104 case DW_LNS_extended_op:
20105 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20106 &bytes_read);
20107 line_ptr += bytes_read;
20108 extended_end = line_ptr + extended_len;
20109 extended_op = read_1_byte (abfd, line_ptr);
20110 line_ptr += 1;
20111 switch (extended_op)
20112 {
20113 case DW_LNE_end_sequence:
20114 state_machine.handle_end_sequence ();
20115 end_sequence = true;
20116 break;
20117 case DW_LNE_set_address:
20118 {
20119 CORE_ADDR address
20120 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20121 line_ptr += bytes_read;
20122
20123 state_machine.check_line_address (cu, line_ptr,
20124 lowpc - baseaddr, address);
20125 state_machine.handle_set_address (baseaddr, address);
20126 }
20127 break;
20128 case DW_LNE_define_file:
20129 {
20130 const char *cur_file;
20131 unsigned int mod_time, length;
20132 dir_index dindex;
20133
20134 cur_file = read_direct_string (abfd, line_ptr,
20135 &bytes_read);
20136 line_ptr += bytes_read;
20137 dindex = (dir_index)
20138 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20139 line_ptr += bytes_read;
20140 mod_time =
20141 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20142 line_ptr += bytes_read;
20143 length =
20144 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20145 line_ptr += bytes_read;
20146 lh->add_file_name (cur_file, dindex, mod_time, length);
20147 }
20148 break;
20149 case DW_LNE_set_discriminator:
20150 {
20151 /* The discriminator is not interesting to the
20152 debugger; just ignore it. We still need to
20153 check its value though:
20154 if there are consecutive entries for the same
20155 (non-prologue) line we want to coalesce them.
20156 PR 17276. */
20157 unsigned int discr
20158 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20159 line_ptr += bytes_read;
20160
20161 state_machine.handle_set_discriminator (discr);
20162 }
20163 break;
20164 default:
20165 complaint (_("mangled .debug_line section"));
20166 return;
20167 }
20168 /* Make sure that we parsed the extended op correctly. If e.g.
20169 we expected a different address size than the producer used,
20170 we may have read the wrong number of bytes. */
20171 if (line_ptr != extended_end)
20172 {
20173 complaint (_("mangled .debug_line section"));
20174 return;
20175 }
20176 break;
20177 case DW_LNS_copy:
20178 state_machine.handle_copy ();
20179 break;
20180 case DW_LNS_advance_pc:
20181 {
20182 CORE_ADDR adjust
20183 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20184 line_ptr += bytes_read;
20185
20186 state_machine.handle_advance_pc (adjust);
20187 }
20188 break;
20189 case DW_LNS_advance_line:
20190 {
20191 int line_delta
20192 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20193 line_ptr += bytes_read;
20194
20195 state_machine.handle_advance_line (line_delta);
20196 }
20197 break;
20198 case DW_LNS_set_file:
20199 {
20200 file_name_index file
20201 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20202 &bytes_read);
20203 line_ptr += bytes_read;
20204
20205 state_machine.handle_set_file (file);
20206 }
20207 break;
20208 case DW_LNS_set_column:
20209 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20210 line_ptr += bytes_read;
20211 break;
20212 case DW_LNS_negate_stmt:
20213 state_machine.handle_negate_stmt ();
20214 break;
20215 case DW_LNS_set_basic_block:
20216 break;
20217 /* Add to the address register of the state machine the
20218 address increment value corresponding to special opcode
20219 255. I.e., this value is scaled by the minimum
20220 instruction length since special opcode 255 would have
20221 scaled the increment. */
20222 case DW_LNS_const_add_pc:
20223 state_machine.handle_const_add_pc ();
20224 break;
20225 case DW_LNS_fixed_advance_pc:
20226 {
20227 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20228 line_ptr += 2;
20229
20230 state_machine.handle_fixed_advance_pc (addr_adj);
20231 }
20232 break;
20233 default:
20234 {
20235 /* Unknown standard opcode, ignore it. */
20236 int i;
20237
20238 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20239 {
20240 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20241 line_ptr += bytes_read;
20242 }
20243 }
20244 }
20245 }
20246
20247 if (!end_sequence)
20248 dwarf2_debug_line_missing_end_sequence_complaint ();
20249
20250 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20251 in which case we still finish recording the last line). */
20252 state_machine.record_line (true);
20253 }
20254 }
20255
20256 /* Decode the Line Number Program (LNP) for the given line_header
20257 structure and CU. The actual information extracted and the type
20258 of structures created from the LNP depends on the value of PST.
20259
20260 1. If PST is NULL, then this procedure uses the data from the program
20261 to create all necessary symbol tables, and their linetables.
20262
20263 2. If PST is not NULL, this procedure reads the program to determine
20264 the list of files included by the unit represented by PST, and
20265 builds all the associated partial symbol tables.
20266
20267 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20268 It is used for relative paths in the line table.
20269 NOTE: When processing partial symtabs (pst != NULL),
20270 comp_dir == pst->dirname.
20271
20272 NOTE: It is important that psymtabs have the same file name (via strcmp)
20273 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20274 symtab we don't use it in the name of the psymtabs we create.
20275 E.g. expand_line_sal requires this when finding psymtabs to expand.
20276 A good testcase for this is mb-inline.exp.
20277
20278 LOWPC is the lowest address in CU (or 0 if not known).
20279
20280 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20281 for its PC<->lines mapping information. Otherwise only the filename
20282 table is read in. */
20283
20284 static void
20285 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20286 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20287 CORE_ADDR lowpc, int decode_mapping)
20288 {
20289 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20290 const int decode_for_pst_p = (pst != NULL);
20291
20292 if (decode_mapping)
20293 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20294
20295 if (decode_for_pst_p)
20296 {
20297 /* Now that we're done scanning the Line Header Program, we can
20298 create the psymtab of each included file. */
20299 for (auto &file_entry : lh->file_names ())
20300 if (file_entry.included_p == 1)
20301 {
20302 gdb::unique_xmalloc_ptr<char> name_holder;
20303 const char *include_name =
20304 psymtab_include_file_name (lh, file_entry, pst,
20305 comp_dir, &name_holder);
20306 if (include_name != NULL)
20307 dwarf2_create_include_psymtab (include_name, pst, objfile);
20308 }
20309 }
20310 else
20311 {
20312 /* Make sure a symtab is created for every file, even files
20313 which contain only variables (i.e. no code with associated
20314 line numbers). */
20315 buildsym_compunit *builder = cu->get_builder ();
20316 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20317
20318 for (auto &fe : lh->file_names ())
20319 {
20320 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20321 if (builder->get_current_subfile ()->symtab == NULL)
20322 {
20323 builder->get_current_subfile ()->symtab
20324 = allocate_symtab (cust,
20325 builder->get_current_subfile ()->name);
20326 }
20327 fe.symtab = builder->get_current_subfile ()->symtab;
20328 }
20329 }
20330 }
20331
20332 /* Start a subfile for DWARF. FILENAME is the name of the file and
20333 DIRNAME the name of the source directory which contains FILENAME
20334 or NULL if not known.
20335 This routine tries to keep line numbers from identical absolute and
20336 relative file names in a common subfile.
20337
20338 Using the `list' example from the GDB testsuite, which resides in
20339 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20340 of /srcdir/list0.c yields the following debugging information for list0.c:
20341
20342 DW_AT_name: /srcdir/list0.c
20343 DW_AT_comp_dir: /compdir
20344 files.files[0].name: list0.h
20345 files.files[0].dir: /srcdir
20346 files.files[1].name: list0.c
20347 files.files[1].dir: /srcdir
20348
20349 The line number information for list0.c has to end up in a single
20350 subfile, so that `break /srcdir/list0.c:1' works as expected.
20351 start_subfile will ensure that this happens provided that we pass the
20352 concatenation of files.files[1].dir and files.files[1].name as the
20353 subfile's name. */
20354
20355 static void
20356 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20357 const char *dirname)
20358 {
20359 gdb::unique_xmalloc_ptr<char> copy;
20360
20361 /* In order not to lose the line information directory,
20362 we concatenate it to the filename when it makes sense.
20363 Note that the Dwarf3 standard says (speaking of filenames in line
20364 information): ``The directory index is ignored for file names
20365 that represent full path names''. Thus ignoring dirname in the
20366 `else' branch below isn't an issue. */
20367
20368 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20369 {
20370 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20371 filename = copy.get ();
20372 }
20373
20374 cu->get_builder ()->start_subfile (filename);
20375 }
20376
20377 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20378 buildsym_compunit constructor. */
20379
20380 struct compunit_symtab *
20381 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20382 CORE_ADDR low_pc)
20383 {
20384 gdb_assert (m_builder == nullptr);
20385
20386 m_builder.reset (new struct buildsym_compunit
20387 (per_cu->dwarf2_per_objfile->objfile,
20388 name, comp_dir, language, low_pc));
20389
20390 list_in_scope = get_builder ()->get_file_symbols ();
20391
20392 get_builder ()->record_debugformat ("DWARF 2");
20393 get_builder ()->record_producer (producer);
20394
20395 processing_has_namespace_info = false;
20396
20397 return get_builder ()->get_compunit_symtab ();
20398 }
20399
20400 static void
20401 var_decode_location (struct attribute *attr, struct symbol *sym,
20402 struct dwarf2_cu *cu)
20403 {
20404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20405 struct comp_unit_head *cu_header = &cu->header;
20406
20407 /* NOTE drow/2003-01-30: There used to be a comment and some special
20408 code here to turn a symbol with DW_AT_external and a
20409 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20410 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20411 with some versions of binutils) where shared libraries could have
20412 relocations against symbols in their debug information - the
20413 minimal symbol would have the right address, but the debug info
20414 would not. It's no longer necessary, because we will explicitly
20415 apply relocations when we read in the debug information now. */
20416
20417 /* A DW_AT_location attribute with no contents indicates that a
20418 variable has been optimized away. */
20419 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20420 {
20421 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20422 return;
20423 }
20424
20425 /* Handle one degenerate form of location expression specially, to
20426 preserve GDB's previous behavior when section offsets are
20427 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20428 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20429
20430 if (attr->form_is_block ()
20431 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20432 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20433 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20434 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20435 && (DW_BLOCK (attr)->size
20436 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20437 {
20438 unsigned int dummy;
20439
20440 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20441 SET_SYMBOL_VALUE_ADDRESS
20442 (sym, cu->header.read_address (objfile->obfd,
20443 DW_BLOCK (attr)->data + 1,
20444 &dummy));
20445 else
20446 SET_SYMBOL_VALUE_ADDRESS
20447 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20448 &dummy));
20449 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20450 fixup_symbol_section (sym, objfile);
20451 SET_SYMBOL_VALUE_ADDRESS
20452 (sym,
20453 SYMBOL_VALUE_ADDRESS (sym)
20454 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20455 return;
20456 }
20457
20458 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20459 expression evaluator, and use LOC_COMPUTED only when necessary
20460 (i.e. when the value of a register or memory location is
20461 referenced, or a thread-local block, etc.). Then again, it might
20462 not be worthwhile. I'm assuming that it isn't unless performance
20463 or memory numbers show me otherwise. */
20464
20465 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20466
20467 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20468 cu->has_loclist = true;
20469 }
20470
20471 /* Given a pointer to a DWARF information entry, figure out if we need
20472 to make a symbol table entry for it, and if so, create a new entry
20473 and return a pointer to it.
20474 If TYPE is NULL, determine symbol type from the die, otherwise
20475 used the passed type.
20476 If SPACE is not NULL, use it to hold the new symbol. If it is
20477 NULL, allocate a new symbol on the objfile's obstack. */
20478
20479 static struct symbol *
20480 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20481 struct symbol *space)
20482 {
20483 struct dwarf2_per_objfile *dwarf2_per_objfile
20484 = cu->per_cu->dwarf2_per_objfile;
20485 struct objfile *objfile = dwarf2_per_objfile->objfile;
20486 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20487 struct symbol *sym = NULL;
20488 const char *name;
20489 struct attribute *attr = NULL;
20490 struct attribute *attr2 = NULL;
20491 CORE_ADDR baseaddr;
20492 struct pending **list_to_add = NULL;
20493
20494 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20495
20496 baseaddr = objfile->text_section_offset ();
20497
20498 name = dwarf2_name (die, cu);
20499 if (name)
20500 {
20501 const char *linkagename;
20502 int suppress_add = 0;
20503
20504 if (space)
20505 sym = space;
20506 else
20507 sym = allocate_symbol (objfile);
20508 OBJSTAT (objfile, n_syms++);
20509
20510 /* Cache this symbol's name and the name's demangled form (if any). */
20511 sym->set_language (cu->language, &objfile->objfile_obstack);
20512 linkagename = dwarf2_physname (name, die, cu);
20513 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20514
20515 /* Fortran does not have mangling standard and the mangling does differ
20516 between gfortran, iFort etc. */
20517 if (cu->language == language_fortran
20518 && symbol_get_demangled_name (sym) == NULL)
20519 symbol_set_demangled_name (sym,
20520 dwarf2_full_name (name, die, cu),
20521 NULL);
20522
20523 /* Default assumptions.
20524 Use the passed type or decode it from the die. */
20525 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20526 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20527 if (type != NULL)
20528 SYMBOL_TYPE (sym) = type;
20529 else
20530 SYMBOL_TYPE (sym) = die_type (die, cu);
20531 attr = dwarf2_attr (die,
20532 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20533 cu);
20534 if (attr != nullptr)
20535 {
20536 SYMBOL_LINE (sym) = DW_UNSND (attr);
20537 }
20538
20539 attr = dwarf2_attr (die,
20540 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20541 cu);
20542 if (attr != nullptr)
20543 {
20544 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20545 struct file_entry *fe;
20546
20547 if (cu->line_header != NULL)
20548 fe = cu->line_header->file_name_at (file_index);
20549 else
20550 fe = NULL;
20551
20552 if (fe == NULL)
20553 complaint (_("file index out of range"));
20554 else
20555 symbol_set_symtab (sym, fe->symtab);
20556 }
20557
20558 switch (die->tag)
20559 {
20560 case DW_TAG_label:
20561 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20562 if (attr != nullptr)
20563 {
20564 CORE_ADDR addr;
20565
20566 addr = attr->value_as_address ();
20567 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20568 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20569 }
20570 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20571 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20572 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20573 add_symbol_to_list (sym, cu->list_in_scope);
20574 break;
20575 case DW_TAG_subprogram:
20576 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20577 finish_block. */
20578 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20579 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20580 if ((attr2 && (DW_UNSND (attr2) != 0))
20581 || cu->language == language_ada
20582 || cu->language == language_fortran)
20583 {
20584 /* Subprograms marked external are stored as a global symbol.
20585 Ada and Fortran subprograms, whether marked external or
20586 not, are always stored as a global symbol, because we want
20587 to be able to access them globally. For instance, we want
20588 to be able to break on a nested subprogram without having
20589 to specify the context. */
20590 list_to_add = cu->get_builder ()->get_global_symbols ();
20591 }
20592 else
20593 {
20594 list_to_add = cu->list_in_scope;
20595 }
20596 break;
20597 case DW_TAG_inlined_subroutine:
20598 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20599 finish_block. */
20600 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20601 SYMBOL_INLINED (sym) = 1;
20602 list_to_add = cu->list_in_scope;
20603 break;
20604 case DW_TAG_template_value_param:
20605 suppress_add = 1;
20606 /* Fall through. */
20607 case DW_TAG_constant:
20608 case DW_TAG_variable:
20609 case DW_TAG_member:
20610 /* Compilation with minimal debug info may result in
20611 variables with missing type entries. Change the
20612 misleading `void' type to something sensible. */
20613 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20614 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20615
20616 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20617 /* In the case of DW_TAG_member, we should only be called for
20618 static const members. */
20619 if (die->tag == DW_TAG_member)
20620 {
20621 /* dwarf2_add_field uses die_is_declaration,
20622 so we do the same. */
20623 gdb_assert (die_is_declaration (die, cu));
20624 gdb_assert (attr);
20625 }
20626 if (attr != nullptr)
20627 {
20628 dwarf2_const_value (attr, sym, cu);
20629 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20630 if (!suppress_add)
20631 {
20632 if (attr2 && (DW_UNSND (attr2) != 0))
20633 list_to_add = cu->get_builder ()->get_global_symbols ();
20634 else
20635 list_to_add = cu->list_in_scope;
20636 }
20637 break;
20638 }
20639 attr = dwarf2_attr (die, DW_AT_location, cu);
20640 if (attr != nullptr)
20641 {
20642 var_decode_location (attr, sym, cu);
20643 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20644
20645 /* Fortran explicitly imports any global symbols to the local
20646 scope by DW_TAG_common_block. */
20647 if (cu->language == language_fortran && die->parent
20648 && die->parent->tag == DW_TAG_common_block)
20649 attr2 = NULL;
20650
20651 if (SYMBOL_CLASS (sym) == LOC_STATIC
20652 && SYMBOL_VALUE_ADDRESS (sym) == 0
20653 && !dwarf2_per_objfile->has_section_at_zero)
20654 {
20655 /* When a static variable is eliminated by the linker,
20656 the corresponding debug information is not stripped
20657 out, but the variable address is set to null;
20658 do not add such variables into symbol table. */
20659 }
20660 else if (attr2 && (DW_UNSND (attr2) != 0))
20661 {
20662 if (SYMBOL_CLASS (sym) == LOC_STATIC
20663 && (objfile->flags & OBJF_MAINLINE) == 0
20664 && dwarf2_per_objfile->can_copy)
20665 {
20666 /* A global static variable might be subject to
20667 copy relocation. We first check for a local
20668 minsym, though, because maybe the symbol was
20669 marked hidden, in which case this would not
20670 apply. */
20671 bound_minimal_symbol found
20672 = (lookup_minimal_symbol_linkage
20673 (sym->linkage_name (), objfile));
20674 if (found.minsym != nullptr)
20675 sym->maybe_copied = 1;
20676 }
20677
20678 /* A variable with DW_AT_external is never static,
20679 but it 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 else
20687 list_to_add = cu->list_in_scope;
20688 }
20689 else
20690 {
20691 /* We do not know the address of this symbol.
20692 If it is an external symbol and we have type information
20693 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20694 The address of the variable will then be determined from
20695 the minimal symbol table whenever the variable is
20696 referenced. */
20697 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20698
20699 /* Fortran explicitly imports any global symbols to the local
20700 scope by DW_TAG_common_block. */
20701 if (cu->language == language_fortran && die->parent
20702 && die->parent->tag == DW_TAG_common_block)
20703 {
20704 /* SYMBOL_CLASS doesn't matter here because
20705 read_common_block is going to reset it. */
20706 if (!suppress_add)
20707 list_to_add = cu->list_in_scope;
20708 }
20709 else if (attr2 && (DW_UNSND (attr2) != 0)
20710 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20711 {
20712 /* A variable with DW_AT_external is never static, but it
20713 may be block-scoped. */
20714 list_to_add
20715 = ((cu->list_in_scope
20716 == cu->get_builder ()->get_file_symbols ())
20717 ? cu->get_builder ()->get_global_symbols ()
20718 : cu->list_in_scope);
20719
20720 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20721 }
20722 else if (!die_is_declaration (die, cu))
20723 {
20724 /* Use the default LOC_OPTIMIZED_OUT class. */
20725 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20726 if (!suppress_add)
20727 list_to_add = cu->list_in_scope;
20728 }
20729 }
20730 break;
20731 case DW_TAG_formal_parameter:
20732 {
20733 /* If we are inside a function, mark this as an argument. If
20734 not, we might be looking at an argument to an inlined function
20735 when we do not have enough information to show inlined frames;
20736 pretend it's a local variable in that case so that the user can
20737 still see it. */
20738 struct context_stack *curr
20739 = cu->get_builder ()->get_current_context_stack ();
20740 if (curr != nullptr && curr->name != nullptr)
20741 SYMBOL_IS_ARGUMENT (sym) = 1;
20742 attr = dwarf2_attr (die, DW_AT_location, cu);
20743 if (attr != nullptr)
20744 {
20745 var_decode_location (attr, sym, cu);
20746 }
20747 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20748 if (attr != nullptr)
20749 {
20750 dwarf2_const_value (attr, sym, cu);
20751 }
20752
20753 list_to_add = cu->list_in_scope;
20754 }
20755 break;
20756 case DW_TAG_unspecified_parameters:
20757 /* From varargs functions; gdb doesn't seem to have any
20758 interest in this information, so just ignore it for now.
20759 (FIXME?) */
20760 break;
20761 case DW_TAG_template_type_param:
20762 suppress_add = 1;
20763 /* Fall through. */
20764 case DW_TAG_class_type:
20765 case DW_TAG_interface_type:
20766 case DW_TAG_structure_type:
20767 case DW_TAG_union_type:
20768 case DW_TAG_set_type:
20769 case DW_TAG_enumeration_type:
20770 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20771 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20772
20773 {
20774 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20775 really ever be static objects: otherwise, if you try
20776 to, say, break of a class's method and you're in a file
20777 which doesn't mention that class, it won't work unless
20778 the check for all static symbols in lookup_symbol_aux
20779 saves you. See the OtherFileClass tests in
20780 gdb.c++/namespace.exp. */
20781
20782 if (!suppress_add)
20783 {
20784 buildsym_compunit *builder = cu->get_builder ();
20785 list_to_add
20786 = (cu->list_in_scope == builder->get_file_symbols ()
20787 && cu->language == language_cplus
20788 ? builder->get_global_symbols ()
20789 : cu->list_in_scope);
20790
20791 /* The semantics of C++ state that "struct foo {
20792 ... }" also defines a typedef for "foo". */
20793 if (cu->language == language_cplus
20794 || cu->language == language_ada
20795 || cu->language == language_d
20796 || cu->language == language_rust)
20797 {
20798 /* The symbol's name is already allocated along
20799 with this objfile, so we don't need to
20800 duplicate it for the type. */
20801 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20802 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20803 }
20804 }
20805 }
20806 break;
20807 case DW_TAG_typedef:
20808 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20809 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20810 list_to_add = cu->list_in_scope;
20811 break;
20812 case DW_TAG_base_type:
20813 case DW_TAG_subrange_type:
20814 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20815 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20816 list_to_add = cu->list_in_scope;
20817 break;
20818 case DW_TAG_enumerator:
20819 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20820 if (attr != nullptr)
20821 {
20822 dwarf2_const_value (attr, sym, cu);
20823 }
20824 {
20825 /* NOTE: carlton/2003-11-10: See comment above in the
20826 DW_TAG_class_type, etc. block. */
20827
20828 list_to_add
20829 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20830 && cu->language == language_cplus
20831 ? cu->get_builder ()->get_global_symbols ()
20832 : cu->list_in_scope);
20833 }
20834 break;
20835 case DW_TAG_imported_declaration:
20836 case DW_TAG_namespace:
20837 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20838 list_to_add = cu->get_builder ()->get_global_symbols ();
20839 break;
20840 case DW_TAG_module:
20841 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20842 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20843 list_to_add = cu->get_builder ()->get_global_symbols ();
20844 break;
20845 case DW_TAG_common_block:
20846 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20847 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20848 add_symbol_to_list (sym, cu->list_in_scope);
20849 break;
20850 default:
20851 /* Not a tag we recognize. Hopefully we aren't processing
20852 trash data, but since we must specifically ignore things
20853 we don't recognize, there is nothing else we should do at
20854 this point. */
20855 complaint (_("unsupported tag: '%s'"),
20856 dwarf_tag_name (die->tag));
20857 break;
20858 }
20859
20860 if (suppress_add)
20861 {
20862 sym->hash_next = objfile->template_symbols;
20863 objfile->template_symbols = sym;
20864 list_to_add = NULL;
20865 }
20866
20867 if (list_to_add != NULL)
20868 add_symbol_to_list (sym, list_to_add);
20869
20870 /* For the benefit of old versions of GCC, check for anonymous
20871 namespaces based on the demangled name. */
20872 if (!cu->processing_has_namespace_info
20873 && cu->language == language_cplus)
20874 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20875 }
20876 return (sym);
20877 }
20878
20879 /* Given an attr with a DW_FORM_dataN value in host byte order,
20880 zero-extend it as appropriate for the symbol's type. The DWARF
20881 standard (v4) is not entirely clear about the meaning of using
20882 DW_FORM_dataN for a constant with a signed type, where the type is
20883 wider than the data. The conclusion of a discussion on the DWARF
20884 list was that this is unspecified. We choose to always zero-extend
20885 because that is the interpretation long in use by GCC. */
20886
20887 static gdb_byte *
20888 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20889 struct dwarf2_cu *cu, LONGEST *value, int bits)
20890 {
20891 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20892 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20893 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20894 LONGEST l = DW_UNSND (attr);
20895
20896 if (bits < sizeof (*value) * 8)
20897 {
20898 l &= ((LONGEST) 1 << bits) - 1;
20899 *value = l;
20900 }
20901 else if (bits == sizeof (*value) * 8)
20902 *value = l;
20903 else
20904 {
20905 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20906 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20907 return bytes;
20908 }
20909
20910 return NULL;
20911 }
20912
20913 /* Read a constant value from an attribute. Either set *VALUE, or if
20914 the value does not fit in *VALUE, set *BYTES - either already
20915 allocated on the objfile obstack, or newly allocated on OBSTACK,
20916 or, set *BATON, if we translated the constant to a location
20917 expression. */
20918
20919 static void
20920 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20921 const char *name, struct obstack *obstack,
20922 struct dwarf2_cu *cu,
20923 LONGEST *value, const gdb_byte **bytes,
20924 struct dwarf2_locexpr_baton **baton)
20925 {
20926 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20927 struct comp_unit_head *cu_header = &cu->header;
20928 struct dwarf_block *blk;
20929 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20930 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20931
20932 *value = 0;
20933 *bytes = NULL;
20934 *baton = NULL;
20935
20936 switch (attr->form)
20937 {
20938 case DW_FORM_addr:
20939 case DW_FORM_addrx:
20940 case DW_FORM_GNU_addr_index:
20941 {
20942 gdb_byte *data;
20943
20944 if (TYPE_LENGTH (type) != cu_header->addr_size)
20945 dwarf2_const_value_length_mismatch_complaint (name,
20946 cu_header->addr_size,
20947 TYPE_LENGTH (type));
20948 /* Symbols of this form are reasonably rare, so we just
20949 piggyback on the existing location code rather than writing
20950 a new implementation of symbol_computed_ops. */
20951 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20952 (*baton)->per_cu = cu->per_cu;
20953 gdb_assert ((*baton)->per_cu);
20954
20955 (*baton)->size = 2 + cu_header->addr_size;
20956 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20957 (*baton)->data = data;
20958
20959 data[0] = DW_OP_addr;
20960 store_unsigned_integer (&data[1], cu_header->addr_size,
20961 byte_order, DW_ADDR (attr));
20962 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20963 }
20964 break;
20965 case DW_FORM_string:
20966 case DW_FORM_strp:
20967 case DW_FORM_strx:
20968 case DW_FORM_GNU_str_index:
20969 case DW_FORM_GNU_strp_alt:
20970 /* DW_STRING is already allocated on the objfile obstack, point
20971 directly to it. */
20972 *bytes = (const gdb_byte *) DW_STRING (attr);
20973 break;
20974 case DW_FORM_block1:
20975 case DW_FORM_block2:
20976 case DW_FORM_block4:
20977 case DW_FORM_block:
20978 case DW_FORM_exprloc:
20979 case DW_FORM_data16:
20980 blk = DW_BLOCK (attr);
20981 if (TYPE_LENGTH (type) != blk->size)
20982 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20983 TYPE_LENGTH (type));
20984 *bytes = blk->data;
20985 break;
20986
20987 /* The DW_AT_const_value attributes are supposed to carry the
20988 symbol's value "represented as it would be on the target
20989 architecture." By the time we get here, it's already been
20990 converted to host endianness, so we just need to sign- or
20991 zero-extend it as appropriate. */
20992 case DW_FORM_data1:
20993 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20994 break;
20995 case DW_FORM_data2:
20996 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20997 break;
20998 case DW_FORM_data4:
20999 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21000 break;
21001 case DW_FORM_data8:
21002 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21003 break;
21004
21005 case DW_FORM_sdata:
21006 case DW_FORM_implicit_const:
21007 *value = DW_SND (attr);
21008 break;
21009
21010 case DW_FORM_udata:
21011 *value = DW_UNSND (attr);
21012 break;
21013
21014 default:
21015 complaint (_("unsupported const value attribute form: '%s'"),
21016 dwarf_form_name (attr->form));
21017 *value = 0;
21018 break;
21019 }
21020 }
21021
21022
21023 /* Copy constant value from an attribute to a symbol. */
21024
21025 static void
21026 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21027 struct dwarf2_cu *cu)
21028 {
21029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21030 LONGEST value;
21031 const gdb_byte *bytes;
21032 struct dwarf2_locexpr_baton *baton;
21033
21034 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21035 sym->print_name (),
21036 &objfile->objfile_obstack, cu,
21037 &value, &bytes, &baton);
21038
21039 if (baton != NULL)
21040 {
21041 SYMBOL_LOCATION_BATON (sym) = baton;
21042 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21043 }
21044 else if (bytes != NULL)
21045 {
21046 SYMBOL_VALUE_BYTES (sym) = bytes;
21047 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21048 }
21049 else
21050 {
21051 SYMBOL_VALUE (sym) = value;
21052 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21053 }
21054 }
21055
21056 /* Return the type of the die in question using its DW_AT_type attribute. */
21057
21058 static struct type *
21059 die_type (struct die_info *die, struct dwarf2_cu *cu)
21060 {
21061 struct attribute *type_attr;
21062
21063 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21064 if (!type_attr)
21065 {
21066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21067 /* A missing DW_AT_type represents a void type. */
21068 return objfile_type (objfile)->builtin_void;
21069 }
21070
21071 return lookup_die_type (die, type_attr, cu);
21072 }
21073
21074 /* True iff CU's producer generates GNAT Ada auxiliary information
21075 that allows to find parallel types through that information instead
21076 of having to do expensive parallel lookups by type name. */
21077
21078 static int
21079 need_gnat_info (struct dwarf2_cu *cu)
21080 {
21081 /* Assume that the Ada compiler was GNAT, which always produces
21082 the auxiliary information. */
21083 return (cu->language == language_ada);
21084 }
21085
21086 /* Return the auxiliary type of the die in question using its
21087 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21088 attribute is not present. */
21089
21090 static struct type *
21091 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21092 {
21093 struct attribute *type_attr;
21094
21095 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21096 if (!type_attr)
21097 return NULL;
21098
21099 return lookup_die_type (die, type_attr, cu);
21100 }
21101
21102 /* If DIE has a descriptive_type attribute, then set the TYPE's
21103 descriptive type accordingly. */
21104
21105 static void
21106 set_descriptive_type (struct type *type, struct die_info *die,
21107 struct dwarf2_cu *cu)
21108 {
21109 struct type *descriptive_type = die_descriptive_type (die, cu);
21110
21111 if (descriptive_type)
21112 {
21113 ALLOCATE_GNAT_AUX_TYPE (type);
21114 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21115 }
21116 }
21117
21118 /* Return the containing type of the die in question using its
21119 DW_AT_containing_type attribute. */
21120
21121 static struct type *
21122 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21123 {
21124 struct attribute *type_attr;
21125 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21126
21127 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21128 if (!type_attr)
21129 error (_("Dwarf Error: Problem turning containing type into gdb type "
21130 "[in module %s]"), objfile_name (objfile));
21131
21132 return lookup_die_type (die, type_attr, cu);
21133 }
21134
21135 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21136
21137 static struct type *
21138 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21139 {
21140 struct dwarf2_per_objfile *dwarf2_per_objfile
21141 = cu->per_cu->dwarf2_per_objfile;
21142 struct objfile *objfile = dwarf2_per_objfile->objfile;
21143 char *saved;
21144
21145 std::string message
21146 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21147 objfile_name (objfile),
21148 sect_offset_str (cu->header.sect_off),
21149 sect_offset_str (die->sect_off));
21150 saved = obstack_strdup (&objfile->objfile_obstack, message);
21151
21152 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21153 }
21154
21155 /* Look up the type of DIE in CU using its type attribute ATTR.
21156 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21157 DW_AT_containing_type.
21158 If there is no type substitute an error marker. */
21159
21160 static struct type *
21161 lookup_die_type (struct die_info *die, const struct attribute *attr,
21162 struct dwarf2_cu *cu)
21163 {
21164 struct dwarf2_per_objfile *dwarf2_per_objfile
21165 = cu->per_cu->dwarf2_per_objfile;
21166 struct objfile *objfile = dwarf2_per_objfile->objfile;
21167 struct type *this_type;
21168
21169 gdb_assert (attr->name == DW_AT_type
21170 || attr->name == DW_AT_GNAT_descriptive_type
21171 || attr->name == DW_AT_containing_type);
21172
21173 /* First see if we have it cached. */
21174
21175 if (attr->form == DW_FORM_GNU_ref_alt)
21176 {
21177 struct dwarf2_per_cu_data *per_cu;
21178 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21179
21180 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21181 dwarf2_per_objfile);
21182 this_type = get_die_type_at_offset (sect_off, per_cu);
21183 }
21184 else if (attr->form_is_ref ())
21185 {
21186 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21187
21188 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21189 }
21190 else if (attr->form == DW_FORM_ref_sig8)
21191 {
21192 ULONGEST signature = DW_SIGNATURE (attr);
21193
21194 return get_signatured_type (die, signature, cu);
21195 }
21196 else
21197 {
21198 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21199 " at %s [in module %s]"),
21200 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21201 objfile_name (objfile));
21202 return build_error_marker_type (cu, die);
21203 }
21204
21205 /* If not cached we need to read it in. */
21206
21207 if (this_type == NULL)
21208 {
21209 struct die_info *type_die = NULL;
21210 struct dwarf2_cu *type_cu = cu;
21211
21212 if (attr->form_is_ref ())
21213 type_die = follow_die_ref (die, attr, &type_cu);
21214 if (type_die == NULL)
21215 return build_error_marker_type (cu, die);
21216 /* If we find the type now, it's probably because the type came
21217 from an inter-CU reference and the type's CU got expanded before
21218 ours. */
21219 this_type = read_type_die (type_die, type_cu);
21220 }
21221
21222 /* If we still don't have a type use an error marker. */
21223
21224 if (this_type == NULL)
21225 return build_error_marker_type (cu, die);
21226
21227 return this_type;
21228 }
21229
21230 /* Return the type in DIE, CU.
21231 Returns NULL for invalid types.
21232
21233 This first does a lookup in die_type_hash,
21234 and only reads the die in if necessary.
21235
21236 NOTE: This can be called when reading in partial or full symbols. */
21237
21238 static struct type *
21239 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21240 {
21241 struct type *this_type;
21242
21243 this_type = get_die_type (die, cu);
21244 if (this_type)
21245 return this_type;
21246
21247 return read_type_die_1 (die, cu);
21248 }
21249
21250 /* Read the type in DIE, CU.
21251 Returns NULL for invalid types. */
21252
21253 static struct type *
21254 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21255 {
21256 struct type *this_type = NULL;
21257
21258 switch (die->tag)
21259 {
21260 case DW_TAG_class_type:
21261 case DW_TAG_interface_type:
21262 case DW_TAG_structure_type:
21263 case DW_TAG_union_type:
21264 this_type = read_structure_type (die, cu);
21265 break;
21266 case DW_TAG_enumeration_type:
21267 this_type = read_enumeration_type (die, cu);
21268 break;
21269 case DW_TAG_subprogram:
21270 case DW_TAG_subroutine_type:
21271 case DW_TAG_inlined_subroutine:
21272 this_type = read_subroutine_type (die, cu);
21273 break;
21274 case DW_TAG_array_type:
21275 this_type = read_array_type (die, cu);
21276 break;
21277 case DW_TAG_set_type:
21278 this_type = read_set_type (die, cu);
21279 break;
21280 case DW_TAG_pointer_type:
21281 this_type = read_tag_pointer_type (die, cu);
21282 break;
21283 case DW_TAG_ptr_to_member_type:
21284 this_type = read_tag_ptr_to_member_type (die, cu);
21285 break;
21286 case DW_TAG_reference_type:
21287 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21288 break;
21289 case DW_TAG_rvalue_reference_type:
21290 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21291 break;
21292 case DW_TAG_const_type:
21293 this_type = read_tag_const_type (die, cu);
21294 break;
21295 case DW_TAG_volatile_type:
21296 this_type = read_tag_volatile_type (die, cu);
21297 break;
21298 case DW_TAG_restrict_type:
21299 this_type = read_tag_restrict_type (die, cu);
21300 break;
21301 case DW_TAG_string_type:
21302 this_type = read_tag_string_type (die, cu);
21303 break;
21304 case DW_TAG_typedef:
21305 this_type = read_typedef (die, cu);
21306 break;
21307 case DW_TAG_subrange_type:
21308 this_type = read_subrange_type (die, cu);
21309 break;
21310 case DW_TAG_base_type:
21311 this_type = read_base_type (die, cu);
21312 break;
21313 case DW_TAG_unspecified_type:
21314 this_type = read_unspecified_type (die, cu);
21315 break;
21316 case DW_TAG_namespace:
21317 this_type = read_namespace_type (die, cu);
21318 break;
21319 case DW_TAG_module:
21320 this_type = read_module_type (die, cu);
21321 break;
21322 case DW_TAG_atomic_type:
21323 this_type = read_tag_atomic_type (die, cu);
21324 break;
21325 default:
21326 complaint (_("unexpected tag in read_type_die: '%s'"),
21327 dwarf_tag_name (die->tag));
21328 break;
21329 }
21330
21331 return this_type;
21332 }
21333
21334 /* See if we can figure out if the class lives in a namespace. We do
21335 this by looking for a member function; its demangled name will
21336 contain namespace info, if there is any.
21337 Return the computed name or NULL.
21338 Space for the result is allocated on the objfile's obstack.
21339 This is the full-die version of guess_partial_die_structure_name.
21340 In this case we know DIE has no useful parent. */
21341
21342 static const char *
21343 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21344 {
21345 struct die_info *spec_die;
21346 struct dwarf2_cu *spec_cu;
21347 struct die_info *child;
21348 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21349
21350 spec_cu = cu;
21351 spec_die = die_specification (die, &spec_cu);
21352 if (spec_die != NULL)
21353 {
21354 die = spec_die;
21355 cu = spec_cu;
21356 }
21357
21358 for (child = die->child;
21359 child != NULL;
21360 child = child->sibling)
21361 {
21362 if (child->tag == DW_TAG_subprogram)
21363 {
21364 const char *linkage_name = dw2_linkage_name (child, cu);
21365
21366 if (linkage_name != NULL)
21367 {
21368 gdb::unique_xmalloc_ptr<char> actual_name
21369 (language_class_name_from_physname (cu->language_defn,
21370 linkage_name));
21371 const char *name = NULL;
21372
21373 if (actual_name != NULL)
21374 {
21375 const char *die_name = dwarf2_name (die, cu);
21376
21377 if (die_name != NULL
21378 && strcmp (die_name, actual_name.get ()) != 0)
21379 {
21380 /* Strip off the class name from the full name.
21381 We want the prefix. */
21382 int die_name_len = strlen (die_name);
21383 int actual_name_len = strlen (actual_name.get ());
21384 const char *ptr = actual_name.get ();
21385
21386 /* Test for '::' as a sanity check. */
21387 if (actual_name_len > die_name_len + 2
21388 && ptr[actual_name_len - die_name_len - 1] == ':')
21389 name = obstack_strndup (
21390 &objfile->per_bfd->storage_obstack,
21391 ptr, actual_name_len - die_name_len - 2);
21392 }
21393 }
21394 return name;
21395 }
21396 }
21397 }
21398
21399 return NULL;
21400 }
21401
21402 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21403 prefix part in such case. See
21404 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21405
21406 static const char *
21407 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21408 {
21409 struct attribute *attr;
21410 const char *base;
21411
21412 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21413 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21414 return NULL;
21415
21416 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21417 return NULL;
21418
21419 attr = dw2_linkage_name_attr (die, cu);
21420 if (attr == NULL || DW_STRING (attr) == NULL)
21421 return NULL;
21422
21423 /* dwarf2_name had to be already called. */
21424 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21425
21426 /* Strip the base name, keep any leading namespaces/classes. */
21427 base = strrchr (DW_STRING (attr), ':');
21428 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21429 return "";
21430
21431 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21432 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21433 DW_STRING (attr),
21434 &base[-1] - DW_STRING (attr));
21435 }
21436
21437 /* Return the name of the namespace/class that DIE is defined within,
21438 or "" if we can't tell. The caller should not xfree the result.
21439
21440 For example, if we're within the method foo() in the following
21441 code:
21442
21443 namespace N {
21444 class C {
21445 void foo () {
21446 }
21447 };
21448 }
21449
21450 then determine_prefix on foo's die will return "N::C". */
21451
21452 static const char *
21453 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21454 {
21455 struct dwarf2_per_objfile *dwarf2_per_objfile
21456 = cu->per_cu->dwarf2_per_objfile;
21457 struct die_info *parent, *spec_die;
21458 struct dwarf2_cu *spec_cu;
21459 struct type *parent_type;
21460 const char *retval;
21461
21462 if (cu->language != language_cplus
21463 && cu->language != language_fortran && cu->language != language_d
21464 && cu->language != language_rust)
21465 return "";
21466
21467 retval = anonymous_struct_prefix (die, cu);
21468 if (retval)
21469 return retval;
21470
21471 /* We have to be careful in the presence of DW_AT_specification.
21472 For example, with GCC 3.4, given the code
21473
21474 namespace N {
21475 void foo() {
21476 // Definition of N::foo.
21477 }
21478 }
21479
21480 then we'll have a tree of DIEs like this:
21481
21482 1: DW_TAG_compile_unit
21483 2: DW_TAG_namespace // N
21484 3: DW_TAG_subprogram // declaration of N::foo
21485 4: DW_TAG_subprogram // definition of N::foo
21486 DW_AT_specification // refers to die #3
21487
21488 Thus, when processing die #4, we have to pretend that we're in
21489 the context of its DW_AT_specification, namely the contex of die
21490 #3. */
21491 spec_cu = cu;
21492 spec_die = die_specification (die, &spec_cu);
21493 if (spec_die == NULL)
21494 parent = die->parent;
21495 else
21496 {
21497 parent = spec_die->parent;
21498 cu = spec_cu;
21499 }
21500
21501 if (parent == NULL)
21502 return "";
21503 else if (parent->building_fullname)
21504 {
21505 const char *name;
21506 const char *parent_name;
21507
21508 /* It has been seen on RealView 2.2 built binaries,
21509 DW_TAG_template_type_param types actually _defined_ as
21510 children of the parent class:
21511
21512 enum E {};
21513 template class <class Enum> Class{};
21514 Class<enum E> class_e;
21515
21516 1: DW_TAG_class_type (Class)
21517 2: DW_TAG_enumeration_type (E)
21518 3: DW_TAG_enumerator (enum1:0)
21519 3: DW_TAG_enumerator (enum2:1)
21520 ...
21521 2: DW_TAG_template_type_param
21522 DW_AT_type DW_FORM_ref_udata (E)
21523
21524 Besides being broken debug info, it can put GDB into an
21525 infinite loop. Consider:
21526
21527 When we're building the full name for Class<E>, we'll start
21528 at Class, and go look over its template type parameters,
21529 finding E. We'll then try to build the full name of E, and
21530 reach here. We're now trying to build the full name of E,
21531 and look over the parent DIE for containing scope. In the
21532 broken case, if we followed the parent DIE of E, we'd again
21533 find Class, and once again go look at its template type
21534 arguments, etc., etc. Simply don't consider such parent die
21535 as source-level parent of this die (it can't be, the language
21536 doesn't allow it), and break the loop here. */
21537 name = dwarf2_name (die, cu);
21538 parent_name = dwarf2_name (parent, cu);
21539 complaint (_("template param type '%s' defined within parent '%s'"),
21540 name ? name : "<unknown>",
21541 parent_name ? parent_name : "<unknown>");
21542 return "";
21543 }
21544 else
21545 switch (parent->tag)
21546 {
21547 case DW_TAG_namespace:
21548 parent_type = read_type_die (parent, cu);
21549 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21550 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21551 Work around this problem here. */
21552 if (cu->language == language_cplus
21553 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21554 return "";
21555 /* We give a name to even anonymous namespaces. */
21556 return TYPE_NAME (parent_type);
21557 case DW_TAG_class_type:
21558 case DW_TAG_interface_type:
21559 case DW_TAG_structure_type:
21560 case DW_TAG_union_type:
21561 case DW_TAG_module:
21562 parent_type = read_type_die (parent, cu);
21563 if (TYPE_NAME (parent_type) != NULL)
21564 return TYPE_NAME (parent_type);
21565 else
21566 /* An anonymous structure is only allowed non-static data
21567 members; no typedefs, no member functions, et cetera.
21568 So it does not need a prefix. */
21569 return "";
21570 case DW_TAG_compile_unit:
21571 case DW_TAG_partial_unit:
21572 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21573 if (cu->language == language_cplus
21574 && !dwarf2_per_objfile->types.empty ()
21575 && die->child != NULL
21576 && (die->tag == DW_TAG_class_type
21577 || die->tag == DW_TAG_structure_type
21578 || die->tag == DW_TAG_union_type))
21579 {
21580 const char *name = guess_full_die_structure_name (die, cu);
21581 if (name != NULL)
21582 return name;
21583 }
21584 return "";
21585 case DW_TAG_subprogram:
21586 /* Nested subroutines in Fortran get a prefix with the name
21587 of the parent's subroutine. */
21588 if (cu->language == language_fortran)
21589 {
21590 if ((die->tag == DW_TAG_subprogram)
21591 && (dwarf2_name (parent, cu) != NULL))
21592 return dwarf2_name (parent, cu);
21593 }
21594 return determine_prefix (parent, cu);
21595 case DW_TAG_enumeration_type:
21596 parent_type = read_type_die (parent, cu);
21597 if (TYPE_DECLARED_CLASS (parent_type))
21598 {
21599 if (TYPE_NAME (parent_type) != NULL)
21600 return TYPE_NAME (parent_type);
21601 return "";
21602 }
21603 /* Fall through. */
21604 default:
21605 return determine_prefix (parent, cu);
21606 }
21607 }
21608
21609 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21610 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21611 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21612 an obconcat, otherwise allocate storage for the result. The CU argument is
21613 used to determine the language and hence, the appropriate separator. */
21614
21615 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21616
21617 static char *
21618 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21619 int physname, struct dwarf2_cu *cu)
21620 {
21621 const char *lead = "";
21622 const char *sep;
21623
21624 if (suffix == NULL || suffix[0] == '\0'
21625 || prefix == NULL || prefix[0] == '\0')
21626 sep = "";
21627 else if (cu->language == language_d)
21628 {
21629 /* For D, the 'main' function could be defined in any module, but it
21630 should never be prefixed. */
21631 if (strcmp (suffix, "D main") == 0)
21632 {
21633 prefix = "";
21634 sep = "";
21635 }
21636 else
21637 sep = ".";
21638 }
21639 else if (cu->language == language_fortran && physname)
21640 {
21641 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21642 DW_AT_MIPS_linkage_name is preferred and used instead. */
21643
21644 lead = "__";
21645 sep = "_MOD_";
21646 }
21647 else
21648 sep = "::";
21649
21650 if (prefix == NULL)
21651 prefix = "";
21652 if (suffix == NULL)
21653 suffix = "";
21654
21655 if (obs == NULL)
21656 {
21657 char *retval
21658 = ((char *)
21659 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21660
21661 strcpy (retval, lead);
21662 strcat (retval, prefix);
21663 strcat (retval, sep);
21664 strcat (retval, suffix);
21665 return retval;
21666 }
21667 else
21668 {
21669 /* We have an obstack. */
21670 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21671 }
21672 }
21673
21674 /* Return sibling of die, NULL if no sibling. */
21675
21676 static struct die_info *
21677 sibling_die (struct die_info *die)
21678 {
21679 return die->sibling;
21680 }
21681
21682 /* Get name of a die, return NULL if not found. */
21683
21684 static const char *
21685 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21686 struct obstack *obstack)
21687 {
21688 if (name && cu->language == language_cplus)
21689 {
21690 std::string canon_name = cp_canonicalize_string (name);
21691
21692 if (!canon_name.empty ())
21693 {
21694 if (canon_name != name)
21695 name = obstack_strdup (obstack, canon_name);
21696 }
21697 }
21698
21699 return name;
21700 }
21701
21702 /* Get name of a die, return NULL if not found.
21703 Anonymous namespaces are converted to their magic string. */
21704
21705 static const char *
21706 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21707 {
21708 struct attribute *attr;
21709 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21710
21711 attr = dwarf2_attr (die, DW_AT_name, cu);
21712 if ((!attr || !DW_STRING (attr))
21713 && die->tag != DW_TAG_namespace
21714 && die->tag != DW_TAG_class_type
21715 && die->tag != DW_TAG_interface_type
21716 && die->tag != DW_TAG_structure_type
21717 && die->tag != DW_TAG_union_type)
21718 return NULL;
21719
21720 switch (die->tag)
21721 {
21722 case DW_TAG_compile_unit:
21723 case DW_TAG_partial_unit:
21724 /* Compilation units have a DW_AT_name that is a filename, not
21725 a source language identifier. */
21726 case DW_TAG_enumeration_type:
21727 case DW_TAG_enumerator:
21728 /* These tags always have simple identifiers already; no need
21729 to canonicalize them. */
21730 return DW_STRING (attr);
21731
21732 case DW_TAG_namespace:
21733 if (attr != NULL && DW_STRING (attr) != NULL)
21734 return DW_STRING (attr);
21735 return CP_ANONYMOUS_NAMESPACE_STR;
21736
21737 case DW_TAG_class_type:
21738 case DW_TAG_interface_type:
21739 case DW_TAG_structure_type:
21740 case DW_TAG_union_type:
21741 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21742 structures or unions. These were of the form "._%d" in GCC 4.1,
21743 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21744 and GCC 4.4. We work around this problem by ignoring these. */
21745 if (attr && DW_STRING (attr)
21746 && (startswith (DW_STRING (attr), "._")
21747 || startswith (DW_STRING (attr), "<anonymous")))
21748 return NULL;
21749
21750 /* GCC might emit a nameless typedef that has a linkage name. See
21751 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21752 if (!attr || DW_STRING (attr) == NULL)
21753 {
21754 attr = dw2_linkage_name_attr (die, cu);
21755 if (attr == NULL || DW_STRING (attr) == NULL)
21756 return NULL;
21757
21758 /* Avoid demangling DW_STRING (attr) the second time on a second
21759 call for the same DIE. */
21760 if (!DW_STRING_IS_CANONICAL (attr))
21761 {
21762 gdb::unique_xmalloc_ptr<char> demangled
21763 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21764
21765 const char *base;
21766
21767 /* FIXME: we already did this for the partial symbol... */
21768 DW_STRING (attr)
21769 = obstack_strdup (&objfile->per_bfd->storage_obstack,
21770 demangled.get ());
21771 DW_STRING_IS_CANONICAL (attr) = 1;
21772
21773 /* Strip any leading namespaces/classes, keep only the base name.
21774 DW_AT_name for named DIEs does not contain the prefixes. */
21775 base = strrchr (DW_STRING (attr), ':');
21776 if (base && base > DW_STRING (attr) && base[-1] == ':')
21777 return &base[1];
21778 else
21779 return DW_STRING (attr);
21780 }
21781 }
21782 break;
21783
21784 default:
21785 break;
21786 }
21787
21788 if (!DW_STRING_IS_CANONICAL (attr))
21789 {
21790 DW_STRING (attr)
21791 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21792 &objfile->per_bfd->storage_obstack);
21793 DW_STRING_IS_CANONICAL (attr) = 1;
21794 }
21795 return DW_STRING (attr);
21796 }
21797
21798 /* Return the die that this die in an extension of, or NULL if there
21799 is none. *EXT_CU is the CU containing DIE on input, and the CU
21800 containing the return value on output. */
21801
21802 static struct die_info *
21803 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21804 {
21805 struct attribute *attr;
21806
21807 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21808 if (attr == NULL)
21809 return NULL;
21810
21811 return follow_die_ref (die, attr, ext_cu);
21812 }
21813
21814 /* A convenience function that returns an "unknown" DWARF name,
21815 including the value of V. STR is the name of the entity being
21816 printed, e.g., "TAG". */
21817
21818 static const char *
21819 dwarf_unknown (const char *str, unsigned v)
21820 {
21821 char *cell = get_print_cell ();
21822 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21823 return cell;
21824 }
21825
21826 /* Convert a DIE tag into its string name. */
21827
21828 static const char *
21829 dwarf_tag_name (unsigned tag)
21830 {
21831 const char *name = get_DW_TAG_name (tag);
21832
21833 if (name == NULL)
21834 return dwarf_unknown ("TAG", tag);
21835
21836 return name;
21837 }
21838
21839 /* Convert a DWARF attribute code into its string name. */
21840
21841 static const char *
21842 dwarf_attr_name (unsigned attr)
21843 {
21844 const char *name;
21845
21846 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21847 if (attr == DW_AT_MIPS_fde)
21848 return "DW_AT_MIPS_fde";
21849 #else
21850 if (attr == DW_AT_HP_block_index)
21851 return "DW_AT_HP_block_index";
21852 #endif
21853
21854 name = get_DW_AT_name (attr);
21855
21856 if (name == NULL)
21857 return dwarf_unknown ("AT", attr);
21858
21859 return name;
21860 }
21861
21862 /* Convert a DWARF value form code into its string name. */
21863
21864 static const char *
21865 dwarf_form_name (unsigned form)
21866 {
21867 const char *name = get_DW_FORM_name (form);
21868
21869 if (name == NULL)
21870 return dwarf_unknown ("FORM", form);
21871
21872 return name;
21873 }
21874
21875 static const char *
21876 dwarf_bool_name (unsigned mybool)
21877 {
21878 if (mybool)
21879 return "TRUE";
21880 else
21881 return "FALSE";
21882 }
21883
21884 /* Convert a DWARF type code into its string name. */
21885
21886 static const char *
21887 dwarf_type_encoding_name (unsigned enc)
21888 {
21889 const char *name = get_DW_ATE_name (enc);
21890
21891 if (name == NULL)
21892 return dwarf_unknown ("ATE", enc);
21893
21894 return name;
21895 }
21896
21897 static void
21898 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21899 {
21900 unsigned int i;
21901
21902 print_spaces (indent, f);
21903 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21904 dwarf_tag_name (die->tag), die->abbrev,
21905 sect_offset_str (die->sect_off));
21906
21907 if (die->parent != NULL)
21908 {
21909 print_spaces (indent, f);
21910 fprintf_unfiltered (f, " parent at offset: %s\n",
21911 sect_offset_str (die->parent->sect_off));
21912 }
21913
21914 print_spaces (indent, f);
21915 fprintf_unfiltered (f, " has children: %s\n",
21916 dwarf_bool_name (die->child != NULL));
21917
21918 print_spaces (indent, f);
21919 fprintf_unfiltered (f, " attributes:\n");
21920
21921 for (i = 0; i < die->num_attrs; ++i)
21922 {
21923 print_spaces (indent, f);
21924 fprintf_unfiltered (f, " %s (%s) ",
21925 dwarf_attr_name (die->attrs[i].name),
21926 dwarf_form_name (die->attrs[i].form));
21927
21928 switch (die->attrs[i].form)
21929 {
21930 case DW_FORM_addr:
21931 case DW_FORM_addrx:
21932 case DW_FORM_GNU_addr_index:
21933 fprintf_unfiltered (f, "address: ");
21934 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21935 break;
21936 case DW_FORM_block2:
21937 case DW_FORM_block4:
21938 case DW_FORM_block:
21939 case DW_FORM_block1:
21940 fprintf_unfiltered (f, "block: size %s",
21941 pulongest (DW_BLOCK (&die->attrs[i])->size));
21942 break;
21943 case DW_FORM_exprloc:
21944 fprintf_unfiltered (f, "expression: size %s",
21945 pulongest (DW_BLOCK (&die->attrs[i])->size));
21946 break;
21947 case DW_FORM_data16:
21948 fprintf_unfiltered (f, "constant of 16 bytes");
21949 break;
21950 case DW_FORM_ref_addr:
21951 fprintf_unfiltered (f, "ref address: ");
21952 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21953 break;
21954 case DW_FORM_GNU_ref_alt:
21955 fprintf_unfiltered (f, "alt ref address: ");
21956 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21957 break;
21958 case DW_FORM_ref1:
21959 case DW_FORM_ref2:
21960 case DW_FORM_ref4:
21961 case DW_FORM_ref8:
21962 case DW_FORM_ref_udata:
21963 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21964 (long) (DW_UNSND (&die->attrs[i])));
21965 break;
21966 case DW_FORM_data1:
21967 case DW_FORM_data2:
21968 case DW_FORM_data4:
21969 case DW_FORM_data8:
21970 case DW_FORM_udata:
21971 case DW_FORM_sdata:
21972 fprintf_unfiltered (f, "constant: %s",
21973 pulongest (DW_UNSND (&die->attrs[i])));
21974 break;
21975 case DW_FORM_sec_offset:
21976 fprintf_unfiltered (f, "section offset: %s",
21977 pulongest (DW_UNSND (&die->attrs[i])));
21978 break;
21979 case DW_FORM_ref_sig8:
21980 fprintf_unfiltered (f, "signature: %s",
21981 hex_string (DW_SIGNATURE (&die->attrs[i])));
21982 break;
21983 case DW_FORM_string:
21984 case DW_FORM_strp:
21985 case DW_FORM_line_strp:
21986 case DW_FORM_strx:
21987 case DW_FORM_GNU_str_index:
21988 case DW_FORM_GNU_strp_alt:
21989 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21990 DW_STRING (&die->attrs[i])
21991 ? DW_STRING (&die->attrs[i]) : "",
21992 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21993 break;
21994 case DW_FORM_flag:
21995 if (DW_UNSND (&die->attrs[i]))
21996 fprintf_unfiltered (f, "flag: TRUE");
21997 else
21998 fprintf_unfiltered (f, "flag: FALSE");
21999 break;
22000 case DW_FORM_flag_present:
22001 fprintf_unfiltered (f, "flag: TRUE");
22002 break;
22003 case DW_FORM_indirect:
22004 /* The reader will have reduced the indirect form to
22005 the "base form" so this form should not occur. */
22006 fprintf_unfiltered (f,
22007 "unexpected attribute form: DW_FORM_indirect");
22008 break;
22009 case DW_FORM_implicit_const:
22010 fprintf_unfiltered (f, "constant: %s",
22011 plongest (DW_SND (&die->attrs[i])));
22012 break;
22013 default:
22014 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22015 die->attrs[i].form);
22016 break;
22017 }
22018 fprintf_unfiltered (f, "\n");
22019 }
22020 }
22021
22022 static void
22023 dump_die_for_error (struct die_info *die)
22024 {
22025 dump_die_shallow (gdb_stderr, 0, die);
22026 }
22027
22028 static void
22029 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22030 {
22031 int indent = level * 4;
22032
22033 gdb_assert (die != NULL);
22034
22035 if (level >= max_level)
22036 return;
22037
22038 dump_die_shallow (f, indent, die);
22039
22040 if (die->child != NULL)
22041 {
22042 print_spaces (indent, f);
22043 fprintf_unfiltered (f, " Children:");
22044 if (level + 1 < max_level)
22045 {
22046 fprintf_unfiltered (f, "\n");
22047 dump_die_1 (f, level + 1, max_level, die->child);
22048 }
22049 else
22050 {
22051 fprintf_unfiltered (f,
22052 " [not printed, max nesting level reached]\n");
22053 }
22054 }
22055
22056 if (die->sibling != NULL && level > 0)
22057 {
22058 dump_die_1 (f, level, max_level, die->sibling);
22059 }
22060 }
22061
22062 /* This is called from the pdie macro in gdbinit.in.
22063 It's not static so gcc will keep a copy callable from gdb. */
22064
22065 void
22066 dump_die (struct die_info *die, int max_level)
22067 {
22068 dump_die_1 (gdb_stdlog, 0, max_level, die);
22069 }
22070
22071 static void
22072 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22073 {
22074 void **slot;
22075
22076 slot = htab_find_slot_with_hash (cu->die_hash, die,
22077 to_underlying (die->sect_off),
22078 INSERT);
22079
22080 *slot = die;
22081 }
22082
22083 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22084 required kind. */
22085
22086 static sect_offset
22087 dwarf2_get_ref_die_offset (const struct attribute *attr)
22088 {
22089 if (attr->form_is_ref ())
22090 return (sect_offset) DW_UNSND (attr);
22091
22092 complaint (_("unsupported die ref attribute form: '%s'"),
22093 dwarf_form_name (attr->form));
22094 return {};
22095 }
22096
22097 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22098 * the value held by the attribute is not constant. */
22099
22100 static LONGEST
22101 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22102 {
22103 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22104 return DW_SND (attr);
22105 else if (attr->form == DW_FORM_udata
22106 || attr->form == DW_FORM_data1
22107 || attr->form == DW_FORM_data2
22108 || attr->form == DW_FORM_data4
22109 || attr->form == DW_FORM_data8)
22110 return DW_UNSND (attr);
22111 else
22112 {
22113 /* For DW_FORM_data16 see attribute::form_is_constant. */
22114 complaint (_("Attribute value is not a constant (%s)"),
22115 dwarf_form_name (attr->form));
22116 return default_value;
22117 }
22118 }
22119
22120 /* Follow reference or signature attribute ATTR of SRC_DIE.
22121 On entry *REF_CU is the CU of SRC_DIE.
22122 On exit *REF_CU is the CU of the result. */
22123
22124 static struct die_info *
22125 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22126 struct dwarf2_cu **ref_cu)
22127 {
22128 struct die_info *die;
22129
22130 if (attr->form_is_ref ())
22131 die = follow_die_ref (src_die, attr, ref_cu);
22132 else if (attr->form == DW_FORM_ref_sig8)
22133 die = follow_die_sig (src_die, attr, ref_cu);
22134 else
22135 {
22136 dump_die_for_error (src_die);
22137 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22138 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22139 }
22140
22141 return die;
22142 }
22143
22144 /* Follow reference OFFSET.
22145 On entry *REF_CU is the CU of the source die referencing OFFSET.
22146 On exit *REF_CU is the CU of the result.
22147 Returns NULL if OFFSET is invalid. */
22148
22149 static struct die_info *
22150 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22151 struct dwarf2_cu **ref_cu)
22152 {
22153 struct die_info temp_die;
22154 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22155 struct dwarf2_per_objfile *dwarf2_per_objfile
22156 = cu->per_cu->dwarf2_per_objfile;
22157
22158 gdb_assert (cu->per_cu != NULL);
22159
22160 target_cu = cu;
22161
22162 if (cu->per_cu->is_debug_types)
22163 {
22164 /* .debug_types CUs cannot reference anything outside their CU.
22165 If they need to, they have to reference a signatured type via
22166 DW_FORM_ref_sig8. */
22167 if (!cu->header.offset_in_cu_p (sect_off))
22168 return NULL;
22169 }
22170 else if (offset_in_dwz != cu->per_cu->is_dwz
22171 || !cu->header.offset_in_cu_p (sect_off))
22172 {
22173 struct dwarf2_per_cu_data *per_cu;
22174
22175 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22176 dwarf2_per_objfile);
22177
22178 /* If necessary, add it to the queue and load its DIEs. */
22179 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22180 load_full_comp_unit (per_cu, false, cu->language);
22181
22182 target_cu = per_cu->cu;
22183 }
22184 else if (cu->dies == NULL)
22185 {
22186 /* We're loading full DIEs during partial symbol reading. */
22187 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22188 load_full_comp_unit (cu->per_cu, false, language_minimal);
22189 }
22190
22191 *ref_cu = target_cu;
22192 temp_die.sect_off = sect_off;
22193
22194 if (target_cu != cu)
22195 target_cu->ancestor = cu;
22196
22197 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22198 &temp_die,
22199 to_underlying (sect_off));
22200 }
22201
22202 /* Follow reference attribute ATTR of SRC_DIE.
22203 On entry *REF_CU is the CU of SRC_DIE.
22204 On exit *REF_CU is the CU of the result. */
22205
22206 static struct die_info *
22207 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22208 struct dwarf2_cu **ref_cu)
22209 {
22210 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22211 struct dwarf2_cu *cu = *ref_cu;
22212 struct die_info *die;
22213
22214 die = follow_die_offset (sect_off,
22215 (attr->form == DW_FORM_GNU_ref_alt
22216 || cu->per_cu->is_dwz),
22217 ref_cu);
22218 if (!die)
22219 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22220 "at %s [in module %s]"),
22221 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22222 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22223
22224 return die;
22225 }
22226
22227 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22228 Returned value is intended for DW_OP_call*. Returned
22229 dwarf2_locexpr_baton->data has lifetime of
22230 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22231
22232 struct dwarf2_locexpr_baton
22233 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22234 struct dwarf2_per_cu_data *per_cu,
22235 CORE_ADDR (*get_frame_pc) (void *baton),
22236 void *baton, bool resolve_abstract_p)
22237 {
22238 struct dwarf2_cu *cu;
22239 struct die_info *die;
22240 struct attribute *attr;
22241 struct dwarf2_locexpr_baton retval;
22242 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22243 struct objfile *objfile = dwarf2_per_objfile->objfile;
22244
22245 if (per_cu->cu == NULL)
22246 load_cu (per_cu, false);
22247 cu = per_cu->cu;
22248 if (cu == NULL)
22249 {
22250 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22251 Instead just throw an error, not much else we can do. */
22252 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22253 sect_offset_str (sect_off), objfile_name (objfile));
22254 }
22255
22256 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22257 if (!die)
22258 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22259 sect_offset_str (sect_off), objfile_name (objfile));
22260
22261 attr = dwarf2_attr (die, DW_AT_location, cu);
22262 if (!attr && resolve_abstract_p
22263 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22264 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22265 {
22266 CORE_ADDR pc = (*get_frame_pc) (baton);
22267 CORE_ADDR baseaddr = objfile->text_section_offset ();
22268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22269
22270 for (const auto &cand_off
22271 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22272 {
22273 struct dwarf2_cu *cand_cu = cu;
22274 struct die_info *cand
22275 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22276 if (!cand
22277 || !cand->parent
22278 || cand->parent->tag != DW_TAG_subprogram)
22279 continue;
22280
22281 CORE_ADDR pc_low, pc_high;
22282 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22283 if (pc_low == ((CORE_ADDR) -1))
22284 continue;
22285 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22286 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22287 if (!(pc_low <= pc && pc < pc_high))
22288 continue;
22289
22290 die = cand;
22291 attr = dwarf2_attr (die, DW_AT_location, cu);
22292 break;
22293 }
22294 }
22295
22296 if (!attr)
22297 {
22298 /* DWARF: "If there is no such attribute, then there is no effect.".
22299 DATA is ignored if SIZE is 0. */
22300
22301 retval.data = NULL;
22302 retval.size = 0;
22303 }
22304 else if (attr->form_is_section_offset ())
22305 {
22306 struct dwarf2_loclist_baton loclist_baton;
22307 CORE_ADDR pc = (*get_frame_pc) (baton);
22308 size_t size;
22309
22310 fill_in_loclist_baton (cu, &loclist_baton, attr);
22311
22312 retval.data = dwarf2_find_location_expression (&loclist_baton,
22313 &size, pc);
22314 retval.size = size;
22315 }
22316 else
22317 {
22318 if (!attr->form_is_block ())
22319 error (_("Dwarf Error: DIE at %s referenced in module %s "
22320 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22321 sect_offset_str (sect_off), objfile_name (objfile));
22322
22323 retval.data = DW_BLOCK (attr)->data;
22324 retval.size = DW_BLOCK (attr)->size;
22325 }
22326 retval.per_cu = cu->per_cu;
22327
22328 age_cached_comp_units (dwarf2_per_objfile);
22329
22330 return retval;
22331 }
22332
22333 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22334 offset. */
22335
22336 struct dwarf2_locexpr_baton
22337 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22338 struct dwarf2_per_cu_data *per_cu,
22339 CORE_ADDR (*get_frame_pc) (void *baton),
22340 void *baton)
22341 {
22342 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22343
22344 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22345 }
22346
22347 /* Write a constant of a given type as target-ordered bytes into
22348 OBSTACK. */
22349
22350 static const gdb_byte *
22351 write_constant_as_bytes (struct obstack *obstack,
22352 enum bfd_endian byte_order,
22353 struct type *type,
22354 ULONGEST value,
22355 LONGEST *len)
22356 {
22357 gdb_byte *result;
22358
22359 *len = TYPE_LENGTH (type);
22360 result = (gdb_byte *) obstack_alloc (obstack, *len);
22361 store_unsigned_integer (result, *len, byte_order, value);
22362
22363 return result;
22364 }
22365
22366 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22367 pointer to the constant bytes and set LEN to the length of the
22368 data. If memory is needed, allocate it on OBSTACK. If the DIE
22369 does not have a DW_AT_const_value, return NULL. */
22370
22371 const gdb_byte *
22372 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22373 struct dwarf2_per_cu_data *per_cu,
22374 struct obstack *obstack,
22375 LONGEST *len)
22376 {
22377 struct dwarf2_cu *cu;
22378 struct die_info *die;
22379 struct attribute *attr;
22380 const gdb_byte *result = NULL;
22381 struct type *type;
22382 LONGEST value;
22383 enum bfd_endian byte_order;
22384 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22385
22386 if (per_cu->cu == NULL)
22387 load_cu (per_cu, false);
22388 cu = per_cu->cu;
22389 if (cu == NULL)
22390 {
22391 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22392 Instead just throw an error, not much else we can do. */
22393 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22394 sect_offset_str (sect_off), objfile_name (objfile));
22395 }
22396
22397 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22398 if (!die)
22399 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22400 sect_offset_str (sect_off), objfile_name (objfile));
22401
22402 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22403 if (attr == NULL)
22404 return NULL;
22405
22406 byte_order = (bfd_big_endian (objfile->obfd)
22407 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22408
22409 switch (attr->form)
22410 {
22411 case DW_FORM_addr:
22412 case DW_FORM_addrx:
22413 case DW_FORM_GNU_addr_index:
22414 {
22415 gdb_byte *tem;
22416
22417 *len = cu->header.addr_size;
22418 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22419 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22420 result = tem;
22421 }
22422 break;
22423 case DW_FORM_string:
22424 case DW_FORM_strp:
22425 case DW_FORM_strx:
22426 case DW_FORM_GNU_str_index:
22427 case DW_FORM_GNU_strp_alt:
22428 /* DW_STRING is already allocated on the objfile obstack, point
22429 directly to it. */
22430 result = (const gdb_byte *) DW_STRING (attr);
22431 *len = strlen (DW_STRING (attr));
22432 break;
22433 case DW_FORM_block1:
22434 case DW_FORM_block2:
22435 case DW_FORM_block4:
22436 case DW_FORM_block:
22437 case DW_FORM_exprloc:
22438 case DW_FORM_data16:
22439 result = DW_BLOCK (attr)->data;
22440 *len = DW_BLOCK (attr)->size;
22441 break;
22442
22443 /* The DW_AT_const_value attributes are supposed to carry the
22444 symbol's value "represented as it would be on the target
22445 architecture." By the time we get here, it's already been
22446 converted to host endianness, so we just need to sign- or
22447 zero-extend it as appropriate. */
22448 case DW_FORM_data1:
22449 type = die_type (die, cu);
22450 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22451 if (result == NULL)
22452 result = write_constant_as_bytes (obstack, byte_order,
22453 type, value, len);
22454 break;
22455 case DW_FORM_data2:
22456 type = die_type (die, cu);
22457 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22458 if (result == NULL)
22459 result = write_constant_as_bytes (obstack, byte_order,
22460 type, value, len);
22461 break;
22462 case DW_FORM_data4:
22463 type = die_type (die, cu);
22464 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22465 if (result == NULL)
22466 result = write_constant_as_bytes (obstack, byte_order,
22467 type, value, len);
22468 break;
22469 case DW_FORM_data8:
22470 type = die_type (die, cu);
22471 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22472 if (result == NULL)
22473 result = write_constant_as_bytes (obstack, byte_order,
22474 type, value, len);
22475 break;
22476
22477 case DW_FORM_sdata:
22478 case DW_FORM_implicit_const:
22479 type = die_type (die, cu);
22480 result = write_constant_as_bytes (obstack, byte_order,
22481 type, DW_SND (attr), len);
22482 break;
22483
22484 case DW_FORM_udata:
22485 type = die_type (die, cu);
22486 result = write_constant_as_bytes (obstack, byte_order,
22487 type, DW_UNSND (attr), len);
22488 break;
22489
22490 default:
22491 complaint (_("unsupported const value attribute form: '%s'"),
22492 dwarf_form_name (attr->form));
22493 break;
22494 }
22495
22496 return result;
22497 }
22498
22499 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22500 valid type for this die is found. */
22501
22502 struct type *
22503 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22504 struct dwarf2_per_cu_data *per_cu)
22505 {
22506 struct dwarf2_cu *cu;
22507 struct die_info *die;
22508
22509 if (per_cu->cu == NULL)
22510 load_cu (per_cu, false);
22511 cu = per_cu->cu;
22512 if (!cu)
22513 return NULL;
22514
22515 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22516 if (!die)
22517 return NULL;
22518
22519 return die_type (die, cu);
22520 }
22521
22522 /* Return the type of the DIE at DIE_OFFSET in the CU named by
22523 PER_CU. */
22524
22525 struct type *
22526 dwarf2_get_die_type (cu_offset die_offset,
22527 struct dwarf2_per_cu_data *per_cu)
22528 {
22529 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22530 return get_die_type_at_offset (die_offset_sect, per_cu);
22531 }
22532
22533 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22534 On entry *REF_CU is the CU of SRC_DIE.
22535 On exit *REF_CU is the CU of the result.
22536 Returns NULL if the referenced DIE isn't found. */
22537
22538 static struct die_info *
22539 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22540 struct dwarf2_cu **ref_cu)
22541 {
22542 struct die_info temp_die;
22543 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22544 struct die_info *die;
22545
22546 /* While it might be nice to assert sig_type->type == NULL here,
22547 we can get here for DW_AT_imported_declaration where we need
22548 the DIE not the type. */
22549
22550 /* If necessary, add it to the queue and load its DIEs. */
22551
22552 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22553 read_signatured_type (sig_type);
22554
22555 sig_cu = sig_type->per_cu.cu;
22556 gdb_assert (sig_cu != NULL);
22557 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22558 temp_die.sect_off = sig_type->type_offset_in_section;
22559 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22560 to_underlying (temp_die.sect_off));
22561 if (die)
22562 {
22563 struct dwarf2_per_objfile *dwarf2_per_objfile
22564 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22565
22566 /* For .gdb_index version 7 keep track of included TUs.
22567 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22568 if (dwarf2_per_objfile->index_table != NULL
22569 && dwarf2_per_objfile->index_table->version <= 7)
22570 {
22571 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22572 }
22573
22574 *ref_cu = sig_cu;
22575 if (sig_cu != cu)
22576 sig_cu->ancestor = cu;
22577
22578 return die;
22579 }
22580
22581 return NULL;
22582 }
22583
22584 /* Follow signatured type referenced by ATTR in SRC_DIE.
22585 On entry *REF_CU is the CU of SRC_DIE.
22586 On exit *REF_CU is the CU of the result.
22587 The result is the DIE of the type.
22588 If the referenced type cannot be found an error is thrown. */
22589
22590 static struct die_info *
22591 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22592 struct dwarf2_cu **ref_cu)
22593 {
22594 ULONGEST signature = DW_SIGNATURE (attr);
22595 struct signatured_type *sig_type;
22596 struct die_info *die;
22597
22598 gdb_assert (attr->form == DW_FORM_ref_sig8);
22599
22600 sig_type = lookup_signatured_type (*ref_cu, signature);
22601 /* sig_type will be NULL if the signatured type is missing from
22602 the debug info. */
22603 if (sig_type == NULL)
22604 {
22605 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22606 " from DIE at %s [in module %s]"),
22607 hex_string (signature), sect_offset_str (src_die->sect_off),
22608 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22609 }
22610
22611 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22612 if (die == NULL)
22613 {
22614 dump_die_for_error (src_die);
22615 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22616 " from DIE at %s [in module %s]"),
22617 hex_string (signature), sect_offset_str (src_die->sect_off),
22618 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22619 }
22620
22621 return die;
22622 }
22623
22624 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22625 reading in and processing the type unit if necessary. */
22626
22627 static struct type *
22628 get_signatured_type (struct die_info *die, ULONGEST signature,
22629 struct dwarf2_cu *cu)
22630 {
22631 struct dwarf2_per_objfile *dwarf2_per_objfile
22632 = cu->per_cu->dwarf2_per_objfile;
22633 struct signatured_type *sig_type;
22634 struct dwarf2_cu *type_cu;
22635 struct die_info *type_die;
22636 struct type *type;
22637
22638 sig_type = lookup_signatured_type (cu, signature);
22639 /* sig_type will be NULL if the signatured type is missing from
22640 the debug info. */
22641 if (sig_type == NULL)
22642 {
22643 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22644 " from DIE at %s [in module %s]"),
22645 hex_string (signature), sect_offset_str (die->sect_off),
22646 objfile_name (dwarf2_per_objfile->objfile));
22647 return build_error_marker_type (cu, die);
22648 }
22649
22650 /* If we already know the type we're done. */
22651 if (sig_type->type != NULL)
22652 return sig_type->type;
22653
22654 type_cu = cu;
22655 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22656 if (type_die != NULL)
22657 {
22658 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22659 is created. This is important, for example, because for c++ classes
22660 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22661 type = read_type_die (type_die, type_cu);
22662 if (type == NULL)
22663 {
22664 complaint (_("Dwarf Error: Cannot build signatured type %s"
22665 " referenced from DIE at %s [in module %s]"),
22666 hex_string (signature), sect_offset_str (die->sect_off),
22667 objfile_name (dwarf2_per_objfile->objfile));
22668 type = build_error_marker_type (cu, die);
22669 }
22670 }
22671 else
22672 {
22673 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22674 " from DIE at %s [in module %s]"),
22675 hex_string (signature), sect_offset_str (die->sect_off),
22676 objfile_name (dwarf2_per_objfile->objfile));
22677 type = build_error_marker_type (cu, die);
22678 }
22679 sig_type->type = type;
22680
22681 return type;
22682 }
22683
22684 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22685 reading in and processing the type unit if necessary. */
22686
22687 static struct type *
22688 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22689 struct dwarf2_cu *cu) /* ARI: editCase function */
22690 {
22691 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22692 if (attr->form_is_ref ())
22693 {
22694 struct dwarf2_cu *type_cu = cu;
22695 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22696
22697 return read_type_die (type_die, type_cu);
22698 }
22699 else if (attr->form == DW_FORM_ref_sig8)
22700 {
22701 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22702 }
22703 else
22704 {
22705 struct dwarf2_per_objfile *dwarf2_per_objfile
22706 = cu->per_cu->dwarf2_per_objfile;
22707
22708 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22709 " at %s [in module %s]"),
22710 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22711 objfile_name (dwarf2_per_objfile->objfile));
22712 return build_error_marker_type (cu, die);
22713 }
22714 }
22715
22716 /* Load the DIEs associated with type unit PER_CU into memory. */
22717
22718 static void
22719 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22720 {
22721 struct signatured_type *sig_type;
22722
22723 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22724 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22725
22726 /* We have the per_cu, but we need the signatured_type.
22727 Fortunately this is an easy translation. */
22728 gdb_assert (per_cu->is_debug_types);
22729 sig_type = (struct signatured_type *) per_cu;
22730
22731 gdb_assert (per_cu->cu == NULL);
22732
22733 read_signatured_type (sig_type);
22734
22735 gdb_assert (per_cu->cu != NULL);
22736 }
22737
22738 /* Read in a signatured type and build its CU and DIEs.
22739 If the type is a stub for the real type in a DWO file,
22740 read in the real type from the DWO file as well. */
22741
22742 static void
22743 read_signatured_type (struct signatured_type *sig_type)
22744 {
22745 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22746
22747 gdb_assert (per_cu->is_debug_types);
22748 gdb_assert (per_cu->cu == NULL);
22749
22750 cutu_reader reader (per_cu, NULL, 0, false);
22751
22752 if (!reader.dummy_p)
22753 {
22754 struct dwarf2_cu *cu = reader.cu;
22755 const gdb_byte *info_ptr = reader.info_ptr;
22756
22757 gdb_assert (cu->die_hash == NULL);
22758 cu->die_hash =
22759 htab_create_alloc_ex (cu->header.length / 12,
22760 die_hash,
22761 die_eq,
22762 NULL,
22763 &cu->comp_unit_obstack,
22764 hashtab_obstack_allocate,
22765 dummy_obstack_deallocate);
22766
22767 if (reader.comp_unit_die->has_children)
22768 reader.comp_unit_die->child
22769 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22770 reader.comp_unit_die);
22771 cu->dies = reader.comp_unit_die;
22772 /* comp_unit_die is not stored in die_hash, no need. */
22773
22774 /* We try not to read any attributes in this function, because
22775 not all CUs needed for references have been loaded yet, and
22776 symbol table processing isn't initialized. But we have to
22777 set the CU language, or we won't be able to build types
22778 correctly. Similarly, if we do not read the producer, we can
22779 not apply producer-specific interpretation. */
22780 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22781
22782 reader.keep ();
22783 }
22784
22785 sig_type->per_cu.tu_read = 1;
22786 }
22787
22788 /* Decode simple location descriptions.
22789 Given a pointer to a dwarf block that defines a location, compute
22790 the location and return the value.
22791
22792 NOTE drow/2003-11-18: This function is called in two situations
22793 now: for the address of static or global variables (partial symbols
22794 only) and for offsets into structures which are expected to be
22795 (more or less) constant. The partial symbol case should go away,
22796 and only the constant case should remain. That will let this
22797 function complain more accurately. A few special modes are allowed
22798 without complaint for global variables (for instance, global
22799 register values and thread-local values).
22800
22801 A location description containing no operations indicates that the
22802 object is optimized out. The return value is 0 for that case.
22803 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22804 callers will only want a very basic result and this can become a
22805 complaint.
22806
22807 Note that stack[0] is unused except as a default error return. */
22808
22809 static CORE_ADDR
22810 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22811 {
22812 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22813 size_t i;
22814 size_t size = blk->size;
22815 const gdb_byte *data = blk->data;
22816 CORE_ADDR stack[64];
22817 int stacki;
22818 unsigned int bytes_read, unsnd;
22819 gdb_byte op;
22820
22821 i = 0;
22822 stacki = 0;
22823 stack[stacki] = 0;
22824 stack[++stacki] = 0;
22825
22826 while (i < size)
22827 {
22828 op = data[i++];
22829 switch (op)
22830 {
22831 case DW_OP_lit0:
22832 case DW_OP_lit1:
22833 case DW_OP_lit2:
22834 case DW_OP_lit3:
22835 case DW_OP_lit4:
22836 case DW_OP_lit5:
22837 case DW_OP_lit6:
22838 case DW_OP_lit7:
22839 case DW_OP_lit8:
22840 case DW_OP_lit9:
22841 case DW_OP_lit10:
22842 case DW_OP_lit11:
22843 case DW_OP_lit12:
22844 case DW_OP_lit13:
22845 case DW_OP_lit14:
22846 case DW_OP_lit15:
22847 case DW_OP_lit16:
22848 case DW_OP_lit17:
22849 case DW_OP_lit18:
22850 case DW_OP_lit19:
22851 case DW_OP_lit20:
22852 case DW_OP_lit21:
22853 case DW_OP_lit22:
22854 case DW_OP_lit23:
22855 case DW_OP_lit24:
22856 case DW_OP_lit25:
22857 case DW_OP_lit26:
22858 case DW_OP_lit27:
22859 case DW_OP_lit28:
22860 case DW_OP_lit29:
22861 case DW_OP_lit30:
22862 case DW_OP_lit31:
22863 stack[++stacki] = op - DW_OP_lit0;
22864 break;
22865
22866 case DW_OP_reg0:
22867 case DW_OP_reg1:
22868 case DW_OP_reg2:
22869 case DW_OP_reg3:
22870 case DW_OP_reg4:
22871 case DW_OP_reg5:
22872 case DW_OP_reg6:
22873 case DW_OP_reg7:
22874 case DW_OP_reg8:
22875 case DW_OP_reg9:
22876 case DW_OP_reg10:
22877 case DW_OP_reg11:
22878 case DW_OP_reg12:
22879 case DW_OP_reg13:
22880 case DW_OP_reg14:
22881 case DW_OP_reg15:
22882 case DW_OP_reg16:
22883 case DW_OP_reg17:
22884 case DW_OP_reg18:
22885 case DW_OP_reg19:
22886 case DW_OP_reg20:
22887 case DW_OP_reg21:
22888 case DW_OP_reg22:
22889 case DW_OP_reg23:
22890 case DW_OP_reg24:
22891 case DW_OP_reg25:
22892 case DW_OP_reg26:
22893 case DW_OP_reg27:
22894 case DW_OP_reg28:
22895 case DW_OP_reg29:
22896 case DW_OP_reg30:
22897 case DW_OP_reg31:
22898 stack[++stacki] = op - DW_OP_reg0;
22899 if (i < size)
22900 dwarf2_complex_location_expr_complaint ();
22901 break;
22902
22903 case DW_OP_regx:
22904 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22905 i += bytes_read;
22906 stack[++stacki] = unsnd;
22907 if (i < size)
22908 dwarf2_complex_location_expr_complaint ();
22909 break;
22910
22911 case DW_OP_addr:
22912 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22913 &bytes_read);
22914 i += bytes_read;
22915 break;
22916
22917 case DW_OP_const1u:
22918 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22919 i += 1;
22920 break;
22921
22922 case DW_OP_const1s:
22923 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22924 i += 1;
22925 break;
22926
22927 case DW_OP_const2u:
22928 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22929 i += 2;
22930 break;
22931
22932 case DW_OP_const2s:
22933 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22934 i += 2;
22935 break;
22936
22937 case DW_OP_const4u:
22938 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22939 i += 4;
22940 break;
22941
22942 case DW_OP_const4s:
22943 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22944 i += 4;
22945 break;
22946
22947 case DW_OP_const8u:
22948 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22949 i += 8;
22950 break;
22951
22952 case DW_OP_constu:
22953 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22954 &bytes_read);
22955 i += bytes_read;
22956 break;
22957
22958 case DW_OP_consts:
22959 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22960 i += bytes_read;
22961 break;
22962
22963 case DW_OP_dup:
22964 stack[stacki + 1] = stack[stacki];
22965 stacki++;
22966 break;
22967
22968 case DW_OP_plus:
22969 stack[stacki - 1] += stack[stacki];
22970 stacki--;
22971 break;
22972
22973 case DW_OP_plus_uconst:
22974 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22975 &bytes_read);
22976 i += bytes_read;
22977 break;
22978
22979 case DW_OP_minus:
22980 stack[stacki - 1] -= stack[stacki];
22981 stacki--;
22982 break;
22983
22984 case DW_OP_deref:
22985 /* If we're not the last op, then we definitely can't encode
22986 this using GDB's address_class enum. This is valid for partial
22987 global symbols, although the variable's address will be bogus
22988 in the psymtab. */
22989 if (i < size)
22990 dwarf2_complex_location_expr_complaint ();
22991 break;
22992
22993 case DW_OP_GNU_push_tls_address:
22994 case DW_OP_form_tls_address:
22995 /* The top of the stack has the offset from the beginning
22996 of the thread control block at which the variable is located. */
22997 /* Nothing should follow this operator, so the top of stack would
22998 be returned. */
22999 /* This is valid for partial global symbols, but the variable's
23000 address will be bogus in the psymtab. Make it always at least
23001 non-zero to not look as a variable garbage collected by linker
23002 which have DW_OP_addr 0. */
23003 if (i < size)
23004 dwarf2_complex_location_expr_complaint ();
23005 stack[stacki]++;
23006 break;
23007
23008 case DW_OP_GNU_uninit:
23009 break;
23010
23011 case DW_OP_addrx:
23012 case DW_OP_GNU_addr_index:
23013 case DW_OP_GNU_const_index:
23014 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23015 &bytes_read);
23016 i += bytes_read;
23017 break;
23018
23019 default:
23020 {
23021 const char *name = get_DW_OP_name (op);
23022
23023 if (name)
23024 complaint (_("unsupported stack op: '%s'"),
23025 name);
23026 else
23027 complaint (_("unsupported stack op: '%02x'"),
23028 op);
23029 }
23030
23031 return (stack[stacki]);
23032 }
23033
23034 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23035 outside of the allocated space. Also enforce minimum>0. */
23036 if (stacki >= ARRAY_SIZE (stack) - 1)
23037 {
23038 complaint (_("location description stack overflow"));
23039 return 0;
23040 }
23041
23042 if (stacki <= 0)
23043 {
23044 complaint (_("location description stack underflow"));
23045 return 0;
23046 }
23047 }
23048 return (stack[stacki]);
23049 }
23050
23051 /* memory allocation interface */
23052
23053 static struct dwarf_block *
23054 dwarf_alloc_block (struct dwarf2_cu *cu)
23055 {
23056 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23057 }
23058
23059 static struct die_info *
23060 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23061 {
23062 struct die_info *die;
23063 size_t size = sizeof (struct die_info);
23064
23065 if (num_attrs > 1)
23066 size += (num_attrs - 1) * sizeof (struct attribute);
23067
23068 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23069 memset (die, 0, sizeof (struct die_info));
23070 return (die);
23071 }
23072
23073 \f
23074 /* Macro support. */
23075
23076 static struct macro_source_file *
23077 macro_start_file (struct dwarf2_cu *cu,
23078 int file, int line,
23079 struct macro_source_file *current_file,
23080 struct line_header *lh)
23081 {
23082 /* File name relative to the compilation directory of this source file. */
23083 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23084
23085 if (! current_file)
23086 {
23087 /* Note: We don't create a macro table for this compilation unit
23088 at all until we actually get a filename. */
23089 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23090
23091 /* If we have no current file, then this must be the start_file
23092 directive for the compilation unit's main source file. */
23093 current_file = macro_set_main (macro_table, file_name.get ());
23094 macro_define_special (macro_table);
23095 }
23096 else
23097 current_file = macro_include (current_file, line, file_name.get ());
23098
23099 return current_file;
23100 }
23101
23102 static const char *
23103 consume_improper_spaces (const char *p, const char *body)
23104 {
23105 if (*p == ' ')
23106 {
23107 complaint (_("macro definition contains spaces "
23108 "in formal argument list:\n`%s'"),
23109 body);
23110
23111 while (*p == ' ')
23112 p++;
23113 }
23114
23115 return p;
23116 }
23117
23118
23119 static void
23120 parse_macro_definition (struct macro_source_file *file, int line,
23121 const char *body)
23122 {
23123 const char *p;
23124
23125 /* The body string takes one of two forms. For object-like macro
23126 definitions, it should be:
23127
23128 <macro name> " " <definition>
23129
23130 For function-like macro definitions, it should be:
23131
23132 <macro name> "() " <definition>
23133 or
23134 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23135
23136 Spaces may appear only where explicitly indicated, and in the
23137 <definition>.
23138
23139 The Dwarf 2 spec says that an object-like macro's name is always
23140 followed by a space, but versions of GCC around March 2002 omit
23141 the space when the macro's definition is the empty string.
23142
23143 The Dwarf 2 spec says that there should be no spaces between the
23144 formal arguments in a function-like macro's formal argument list,
23145 but versions of GCC around March 2002 include spaces after the
23146 commas. */
23147
23148
23149 /* Find the extent of the macro name. The macro name is terminated
23150 by either a space or null character (for an object-like macro) or
23151 an opening paren (for a function-like macro). */
23152 for (p = body; *p; p++)
23153 if (*p == ' ' || *p == '(')
23154 break;
23155
23156 if (*p == ' ' || *p == '\0')
23157 {
23158 /* It's an object-like macro. */
23159 int name_len = p - body;
23160 std::string name (body, name_len);
23161 const char *replacement;
23162
23163 if (*p == ' ')
23164 replacement = body + name_len + 1;
23165 else
23166 {
23167 dwarf2_macro_malformed_definition_complaint (body);
23168 replacement = body + name_len;
23169 }
23170
23171 macro_define_object (file, line, name.c_str (), replacement);
23172 }
23173 else if (*p == '(')
23174 {
23175 /* It's a function-like macro. */
23176 std::string name (body, p - body);
23177 int argc = 0;
23178 int argv_size = 1;
23179 char **argv = XNEWVEC (char *, argv_size);
23180
23181 p++;
23182
23183 p = consume_improper_spaces (p, body);
23184
23185 /* Parse the formal argument list. */
23186 while (*p && *p != ')')
23187 {
23188 /* Find the extent of the current argument name. */
23189 const char *arg_start = p;
23190
23191 while (*p && *p != ',' && *p != ')' && *p != ' ')
23192 p++;
23193
23194 if (! *p || p == arg_start)
23195 dwarf2_macro_malformed_definition_complaint (body);
23196 else
23197 {
23198 /* Make sure argv has room for the new argument. */
23199 if (argc >= argv_size)
23200 {
23201 argv_size *= 2;
23202 argv = XRESIZEVEC (char *, argv, argv_size);
23203 }
23204
23205 argv[argc++] = savestring (arg_start, p - arg_start);
23206 }
23207
23208 p = consume_improper_spaces (p, body);
23209
23210 /* Consume the comma, if present. */
23211 if (*p == ',')
23212 {
23213 p++;
23214
23215 p = consume_improper_spaces (p, body);
23216 }
23217 }
23218
23219 if (*p == ')')
23220 {
23221 p++;
23222
23223 if (*p == ' ')
23224 /* Perfectly formed definition, no complaints. */
23225 macro_define_function (file, line, name.c_str (),
23226 argc, (const char **) argv,
23227 p + 1);
23228 else if (*p == '\0')
23229 {
23230 /* Complain, but do define it. */
23231 dwarf2_macro_malformed_definition_complaint (body);
23232 macro_define_function (file, line, name.c_str (),
23233 argc, (const char **) argv,
23234 p);
23235 }
23236 else
23237 /* Just complain. */
23238 dwarf2_macro_malformed_definition_complaint (body);
23239 }
23240 else
23241 /* Just complain. */
23242 dwarf2_macro_malformed_definition_complaint (body);
23243
23244 {
23245 int i;
23246
23247 for (i = 0; i < argc; i++)
23248 xfree (argv[i]);
23249 }
23250 xfree (argv);
23251 }
23252 else
23253 dwarf2_macro_malformed_definition_complaint (body);
23254 }
23255
23256 /* Skip some bytes from BYTES according to the form given in FORM.
23257 Returns the new pointer. */
23258
23259 static const gdb_byte *
23260 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23261 enum dwarf_form form,
23262 unsigned int offset_size,
23263 struct dwarf2_section_info *section)
23264 {
23265 unsigned int bytes_read;
23266
23267 switch (form)
23268 {
23269 case DW_FORM_data1:
23270 case DW_FORM_flag:
23271 ++bytes;
23272 break;
23273
23274 case DW_FORM_data2:
23275 bytes += 2;
23276 break;
23277
23278 case DW_FORM_data4:
23279 bytes += 4;
23280 break;
23281
23282 case DW_FORM_data8:
23283 bytes += 8;
23284 break;
23285
23286 case DW_FORM_data16:
23287 bytes += 16;
23288 break;
23289
23290 case DW_FORM_string:
23291 read_direct_string (abfd, bytes, &bytes_read);
23292 bytes += bytes_read;
23293 break;
23294
23295 case DW_FORM_sec_offset:
23296 case DW_FORM_strp:
23297 case DW_FORM_GNU_strp_alt:
23298 bytes += offset_size;
23299 break;
23300
23301 case DW_FORM_block:
23302 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23303 bytes += bytes_read;
23304 break;
23305
23306 case DW_FORM_block1:
23307 bytes += 1 + read_1_byte (abfd, bytes);
23308 break;
23309 case DW_FORM_block2:
23310 bytes += 2 + read_2_bytes (abfd, bytes);
23311 break;
23312 case DW_FORM_block4:
23313 bytes += 4 + read_4_bytes (abfd, bytes);
23314 break;
23315
23316 case DW_FORM_addrx:
23317 case DW_FORM_sdata:
23318 case DW_FORM_strx:
23319 case DW_FORM_udata:
23320 case DW_FORM_GNU_addr_index:
23321 case DW_FORM_GNU_str_index:
23322 bytes = gdb_skip_leb128 (bytes, buffer_end);
23323 if (bytes == NULL)
23324 {
23325 dwarf2_section_buffer_overflow_complaint (section);
23326 return NULL;
23327 }
23328 break;
23329
23330 case DW_FORM_implicit_const:
23331 break;
23332
23333 default:
23334 {
23335 complaint (_("invalid form 0x%x in `%s'"),
23336 form, section->get_name ());
23337 return NULL;
23338 }
23339 }
23340
23341 return bytes;
23342 }
23343
23344 /* A helper for dwarf_decode_macros that handles skipping an unknown
23345 opcode. Returns an updated pointer to the macro data buffer; or,
23346 on error, issues a complaint and returns NULL. */
23347
23348 static const gdb_byte *
23349 skip_unknown_opcode (unsigned int opcode,
23350 const gdb_byte **opcode_definitions,
23351 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23352 bfd *abfd,
23353 unsigned int offset_size,
23354 struct dwarf2_section_info *section)
23355 {
23356 unsigned int bytes_read, i;
23357 unsigned long arg;
23358 const gdb_byte *defn;
23359
23360 if (opcode_definitions[opcode] == NULL)
23361 {
23362 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23363 opcode);
23364 return NULL;
23365 }
23366
23367 defn = opcode_definitions[opcode];
23368 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23369 defn += bytes_read;
23370
23371 for (i = 0; i < arg; ++i)
23372 {
23373 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23374 (enum dwarf_form) defn[i], offset_size,
23375 section);
23376 if (mac_ptr == NULL)
23377 {
23378 /* skip_form_bytes already issued the complaint. */
23379 return NULL;
23380 }
23381 }
23382
23383 return mac_ptr;
23384 }
23385
23386 /* A helper function which parses the header of a macro section.
23387 If the macro section is the extended (for now called "GNU") type,
23388 then this updates *OFFSET_SIZE. Returns a pointer to just after
23389 the header, or issues a complaint and returns NULL on error. */
23390
23391 static const gdb_byte *
23392 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23393 bfd *abfd,
23394 const gdb_byte *mac_ptr,
23395 unsigned int *offset_size,
23396 int section_is_gnu)
23397 {
23398 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23399
23400 if (section_is_gnu)
23401 {
23402 unsigned int version, flags;
23403
23404 version = read_2_bytes (abfd, mac_ptr);
23405 if (version != 4 && version != 5)
23406 {
23407 complaint (_("unrecognized version `%d' in .debug_macro section"),
23408 version);
23409 return NULL;
23410 }
23411 mac_ptr += 2;
23412
23413 flags = read_1_byte (abfd, mac_ptr);
23414 ++mac_ptr;
23415 *offset_size = (flags & 1) ? 8 : 4;
23416
23417 if ((flags & 2) != 0)
23418 /* We don't need the line table offset. */
23419 mac_ptr += *offset_size;
23420
23421 /* Vendor opcode descriptions. */
23422 if ((flags & 4) != 0)
23423 {
23424 unsigned int i, count;
23425
23426 count = read_1_byte (abfd, mac_ptr);
23427 ++mac_ptr;
23428 for (i = 0; i < count; ++i)
23429 {
23430 unsigned int opcode, bytes_read;
23431 unsigned long arg;
23432
23433 opcode = read_1_byte (abfd, mac_ptr);
23434 ++mac_ptr;
23435 opcode_definitions[opcode] = mac_ptr;
23436 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23437 mac_ptr += bytes_read;
23438 mac_ptr += arg;
23439 }
23440 }
23441 }
23442
23443 return mac_ptr;
23444 }
23445
23446 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23447 including DW_MACRO_import. */
23448
23449 static void
23450 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23451 bfd *abfd,
23452 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23453 struct macro_source_file *current_file,
23454 struct line_header *lh,
23455 struct dwarf2_section_info *section,
23456 int section_is_gnu, int section_is_dwz,
23457 unsigned int offset_size,
23458 htab_t include_hash)
23459 {
23460 struct dwarf2_per_objfile *dwarf2_per_objfile
23461 = cu->per_cu->dwarf2_per_objfile;
23462 struct objfile *objfile = dwarf2_per_objfile->objfile;
23463 enum dwarf_macro_record_type macinfo_type;
23464 int at_commandline;
23465 const gdb_byte *opcode_definitions[256];
23466
23467 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23468 &offset_size, section_is_gnu);
23469 if (mac_ptr == NULL)
23470 {
23471 /* We already issued a complaint. */
23472 return;
23473 }
23474
23475 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23476 GDB is still reading the definitions from command line. First
23477 DW_MACINFO_start_file will need to be ignored as it was already executed
23478 to create CURRENT_FILE for the main source holding also the command line
23479 definitions. On first met DW_MACINFO_start_file this flag is reset to
23480 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23481
23482 at_commandline = 1;
23483
23484 do
23485 {
23486 /* Do we at least have room for a macinfo type byte? */
23487 if (mac_ptr >= mac_end)
23488 {
23489 dwarf2_section_buffer_overflow_complaint (section);
23490 break;
23491 }
23492
23493 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23494 mac_ptr++;
23495
23496 /* Note that we rely on the fact that the corresponding GNU and
23497 DWARF constants are the same. */
23498 DIAGNOSTIC_PUSH
23499 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23500 switch (macinfo_type)
23501 {
23502 /* A zero macinfo type indicates the end of the macro
23503 information. */
23504 case 0:
23505 break;
23506
23507 case DW_MACRO_define:
23508 case DW_MACRO_undef:
23509 case DW_MACRO_define_strp:
23510 case DW_MACRO_undef_strp:
23511 case DW_MACRO_define_sup:
23512 case DW_MACRO_undef_sup:
23513 {
23514 unsigned int bytes_read;
23515 int line;
23516 const char *body;
23517 int is_define;
23518
23519 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23520 mac_ptr += bytes_read;
23521
23522 if (macinfo_type == DW_MACRO_define
23523 || macinfo_type == DW_MACRO_undef)
23524 {
23525 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23526 mac_ptr += bytes_read;
23527 }
23528 else
23529 {
23530 LONGEST str_offset;
23531
23532 str_offset = read_offset (abfd, mac_ptr, offset_size);
23533 mac_ptr += offset_size;
23534
23535 if (macinfo_type == DW_MACRO_define_sup
23536 || macinfo_type == DW_MACRO_undef_sup
23537 || section_is_dwz)
23538 {
23539 struct dwz_file *dwz
23540 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23541
23542 body = read_indirect_string_from_dwz (objfile,
23543 dwz, str_offset);
23544 }
23545 else
23546 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23547 abfd, str_offset);
23548 }
23549
23550 is_define = (macinfo_type == DW_MACRO_define
23551 || macinfo_type == DW_MACRO_define_strp
23552 || macinfo_type == DW_MACRO_define_sup);
23553 if (! current_file)
23554 {
23555 /* DWARF violation as no main source is present. */
23556 complaint (_("debug info with no main source gives macro %s "
23557 "on line %d: %s"),
23558 is_define ? _("definition") : _("undefinition"),
23559 line, body);
23560 break;
23561 }
23562 if ((line == 0 && !at_commandline)
23563 || (line != 0 && at_commandline))
23564 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23565 at_commandline ? _("command-line") : _("in-file"),
23566 is_define ? _("definition") : _("undefinition"),
23567 line == 0 ? _("zero") : _("non-zero"), line, body);
23568
23569 if (body == NULL)
23570 {
23571 /* Fedora's rpm-build's "debugedit" binary
23572 corrupted .debug_macro sections.
23573
23574 For more info, see
23575 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23576 complaint (_("debug info gives %s invalid macro %s "
23577 "without body (corrupted?) at line %d "
23578 "on file %s"),
23579 at_commandline ? _("command-line") : _("in-file"),
23580 is_define ? _("definition") : _("undefinition"),
23581 line, current_file->filename);
23582 }
23583 else if (is_define)
23584 parse_macro_definition (current_file, line, body);
23585 else
23586 {
23587 gdb_assert (macinfo_type == DW_MACRO_undef
23588 || macinfo_type == DW_MACRO_undef_strp
23589 || macinfo_type == DW_MACRO_undef_sup);
23590 macro_undef (current_file, line, body);
23591 }
23592 }
23593 break;
23594
23595 case DW_MACRO_start_file:
23596 {
23597 unsigned int bytes_read;
23598 int line, file;
23599
23600 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23601 mac_ptr += bytes_read;
23602 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23603 mac_ptr += bytes_read;
23604
23605 if ((line == 0 && !at_commandline)
23606 || (line != 0 && at_commandline))
23607 complaint (_("debug info gives source %d included "
23608 "from %s at %s line %d"),
23609 file, at_commandline ? _("command-line") : _("file"),
23610 line == 0 ? _("zero") : _("non-zero"), line);
23611
23612 if (at_commandline)
23613 {
23614 /* This DW_MACRO_start_file was executed in the
23615 pass one. */
23616 at_commandline = 0;
23617 }
23618 else
23619 current_file = macro_start_file (cu, file, line, current_file,
23620 lh);
23621 }
23622 break;
23623
23624 case DW_MACRO_end_file:
23625 if (! current_file)
23626 complaint (_("macro debug info has an unmatched "
23627 "`close_file' directive"));
23628 else
23629 {
23630 current_file = current_file->included_by;
23631 if (! current_file)
23632 {
23633 enum dwarf_macro_record_type next_type;
23634
23635 /* GCC circa March 2002 doesn't produce the zero
23636 type byte marking the end of the compilation
23637 unit. Complain if it's not there, but exit no
23638 matter what. */
23639
23640 /* Do we at least have room for a macinfo type byte? */
23641 if (mac_ptr >= mac_end)
23642 {
23643 dwarf2_section_buffer_overflow_complaint (section);
23644 return;
23645 }
23646
23647 /* We don't increment mac_ptr here, so this is just
23648 a look-ahead. */
23649 next_type
23650 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23651 mac_ptr);
23652 if (next_type != 0)
23653 complaint (_("no terminating 0-type entry for "
23654 "macros in `.debug_macinfo' section"));
23655
23656 return;
23657 }
23658 }
23659 break;
23660
23661 case DW_MACRO_import:
23662 case DW_MACRO_import_sup:
23663 {
23664 LONGEST offset;
23665 void **slot;
23666 bfd *include_bfd = abfd;
23667 struct dwarf2_section_info *include_section = section;
23668 const gdb_byte *include_mac_end = mac_end;
23669 int is_dwz = section_is_dwz;
23670 const gdb_byte *new_mac_ptr;
23671
23672 offset = read_offset (abfd, mac_ptr, offset_size);
23673 mac_ptr += offset_size;
23674
23675 if (macinfo_type == DW_MACRO_import_sup)
23676 {
23677 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23678
23679 dwz->macro.read (objfile);
23680
23681 include_section = &dwz->macro;
23682 include_bfd = include_section->get_bfd_owner ();
23683 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23684 is_dwz = 1;
23685 }
23686
23687 new_mac_ptr = include_section->buffer + offset;
23688 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23689
23690 if (*slot != NULL)
23691 {
23692 /* This has actually happened; see
23693 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23694 complaint (_("recursive DW_MACRO_import in "
23695 ".debug_macro section"));
23696 }
23697 else
23698 {
23699 *slot = (void *) new_mac_ptr;
23700
23701 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23702 include_mac_end, current_file, lh,
23703 section, section_is_gnu, is_dwz,
23704 offset_size, include_hash);
23705
23706 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23707 }
23708 }
23709 break;
23710
23711 case DW_MACINFO_vendor_ext:
23712 if (!section_is_gnu)
23713 {
23714 unsigned int bytes_read;
23715
23716 /* This reads the constant, but since we don't recognize
23717 any vendor extensions, we ignore it. */
23718 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23719 mac_ptr += bytes_read;
23720 read_direct_string (abfd, mac_ptr, &bytes_read);
23721 mac_ptr += bytes_read;
23722
23723 /* We don't recognize any vendor extensions. */
23724 break;
23725 }
23726 /* FALLTHROUGH */
23727
23728 default:
23729 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23730 mac_ptr, mac_end, abfd, offset_size,
23731 section);
23732 if (mac_ptr == NULL)
23733 return;
23734 break;
23735 }
23736 DIAGNOSTIC_POP
23737 } while (macinfo_type != 0);
23738 }
23739
23740 static void
23741 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23742 int section_is_gnu)
23743 {
23744 struct dwarf2_per_objfile *dwarf2_per_objfile
23745 = cu->per_cu->dwarf2_per_objfile;
23746 struct objfile *objfile = dwarf2_per_objfile->objfile;
23747 struct line_header *lh = cu->line_header;
23748 bfd *abfd;
23749 const gdb_byte *mac_ptr, *mac_end;
23750 struct macro_source_file *current_file = 0;
23751 enum dwarf_macro_record_type macinfo_type;
23752 unsigned int offset_size = cu->header.offset_size;
23753 const gdb_byte *opcode_definitions[256];
23754 void **slot;
23755 struct dwarf2_section_info *section;
23756 const char *section_name;
23757
23758 if (cu->dwo_unit != NULL)
23759 {
23760 if (section_is_gnu)
23761 {
23762 section = &cu->dwo_unit->dwo_file->sections.macro;
23763 section_name = ".debug_macro.dwo";
23764 }
23765 else
23766 {
23767 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23768 section_name = ".debug_macinfo.dwo";
23769 }
23770 }
23771 else
23772 {
23773 if (section_is_gnu)
23774 {
23775 section = &dwarf2_per_objfile->macro;
23776 section_name = ".debug_macro";
23777 }
23778 else
23779 {
23780 section = &dwarf2_per_objfile->macinfo;
23781 section_name = ".debug_macinfo";
23782 }
23783 }
23784
23785 section->read (objfile);
23786 if (section->buffer == NULL)
23787 {
23788 complaint (_("missing %s section"), section_name);
23789 return;
23790 }
23791 abfd = section->get_bfd_owner ();
23792
23793 /* First pass: Find the name of the base filename.
23794 This filename is needed in order to process all macros whose definition
23795 (or undefinition) comes from the command line. These macros are defined
23796 before the first DW_MACINFO_start_file entry, and yet still need to be
23797 associated to the base file.
23798
23799 To determine the base file name, we scan the macro definitions until we
23800 reach the first DW_MACINFO_start_file entry. We then initialize
23801 CURRENT_FILE accordingly so that any macro definition found before the
23802 first DW_MACINFO_start_file can still be associated to the base file. */
23803
23804 mac_ptr = section->buffer + offset;
23805 mac_end = section->buffer + section->size;
23806
23807 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23808 &offset_size, section_is_gnu);
23809 if (mac_ptr == NULL)
23810 {
23811 /* We already issued a complaint. */
23812 return;
23813 }
23814
23815 do
23816 {
23817 /* Do we at least have room for a macinfo type byte? */
23818 if (mac_ptr >= mac_end)
23819 {
23820 /* Complaint is printed during the second pass as GDB will probably
23821 stop the first pass earlier upon finding
23822 DW_MACINFO_start_file. */
23823 break;
23824 }
23825
23826 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23827 mac_ptr++;
23828
23829 /* Note that we rely on the fact that the corresponding GNU and
23830 DWARF constants are the same. */
23831 DIAGNOSTIC_PUSH
23832 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23833 switch (macinfo_type)
23834 {
23835 /* A zero macinfo type indicates the end of the macro
23836 information. */
23837 case 0:
23838 break;
23839
23840 case DW_MACRO_define:
23841 case DW_MACRO_undef:
23842 /* Only skip the data by MAC_PTR. */
23843 {
23844 unsigned int bytes_read;
23845
23846 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23847 mac_ptr += bytes_read;
23848 read_direct_string (abfd, mac_ptr, &bytes_read);
23849 mac_ptr += bytes_read;
23850 }
23851 break;
23852
23853 case DW_MACRO_start_file:
23854 {
23855 unsigned int bytes_read;
23856 int line, file;
23857
23858 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23859 mac_ptr += bytes_read;
23860 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23861 mac_ptr += bytes_read;
23862
23863 current_file = macro_start_file (cu, file, line, current_file, lh);
23864 }
23865 break;
23866
23867 case DW_MACRO_end_file:
23868 /* No data to skip by MAC_PTR. */
23869 break;
23870
23871 case DW_MACRO_define_strp:
23872 case DW_MACRO_undef_strp:
23873 case DW_MACRO_define_sup:
23874 case DW_MACRO_undef_sup:
23875 {
23876 unsigned int bytes_read;
23877
23878 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23879 mac_ptr += bytes_read;
23880 mac_ptr += offset_size;
23881 }
23882 break;
23883
23884 case DW_MACRO_import:
23885 case DW_MACRO_import_sup:
23886 /* Note that, according to the spec, a transparent include
23887 chain cannot call DW_MACRO_start_file. So, we can just
23888 skip this opcode. */
23889 mac_ptr += offset_size;
23890 break;
23891
23892 case DW_MACINFO_vendor_ext:
23893 /* Only skip the data by MAC_PTR. */
23894 if (!section_is_gnu)
23895 {
23896 unsigned int bytes_read;
23897
23898 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23899 mac_ptr += bytes_read;
23900 read_direct_string (abfd, mac_ptr, &bytes_read);
23901 mac_ptr += bytes_read;
23902 }
23903 /* FALLTHROUGH */
23904
23905 default:
23906 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23907 mac_ptr, mac_end, abfd, offset_size,
23908 section);
23909 if (mac_ptr == NULL)
23910 return;
23911 break;
23912 }
23913 DIAGNOSTIC_POP
23914 } while (macinfo_type != 0 && current_file == NULL);
23915
23916 /* Second pass: Process all entries.
23917
23918 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23919 command-line macro definitions/undefinitions. This flag is unset when we
23920 reach the first DW_MACINFO_start_file entry. */
23921
23922 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23923 htab_eq_pointer,
23924 NULL, xcalloc, xfree));
23925 mac_ptr = section->buffer + offset;
23926 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23927 *slot = (void *) mac_ptr;
23928 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
23929 current_file, lh, section,
23930 section_is_gnu, 0, offset_size,
23931 include_hash.get ());
23932 }
23933
23934 /* Return the .debug_loc section to use for CU.
23935 For DWO files use .debug_loc.dwo. */
23936
23937 static struct dwarf2_section_info *
23938 cu_debug_loc_section (struct dwarf2_cu *cu)
23939 {
23940 struct dwarf2_per_objfile *dwarf2_per_objfile
23941 = cu->per_cu->dwarf2_per_objfile;
23942
23943 if (cu->dwo_unit)
23944 {
23945 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23946
23947 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23948 }
23949 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23950 : &dwarf2_per_objfile->loc);
23951 }
23952
23953 /* A helper function that fills in a dwarf2_loclist_baton. */
23954
23955 static void
23956 fill_in_loclist_baton (struct dwarf2_cu *cu,
23957 struct dwarf2_loclist_baton *baton,
23958 const struct attribute *attr)
23959 {
23960 struct dwarf2_per_objfile *dwarf2_per_objfile
23961 = cu->per_cu->dwarf2_per_objfile;
23962 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23963
23964 section->read (dwarf2_per_objfile->objfile);
23965
23966 baton->per_cu = cu->per_cu;
23967 gdb_assert (baton->per_cu);
23968 /* We don't know how long the location list is, but make sure we
23969 don't run off the edge of the section. */
23970 baton->size = section->size - DW_UNSND (attr);
23971 baton->data = section->buffer + DW_UNSND (attr);
23972 baton->base_address = cu->base_address;
23973 baton->from_dwo = cu->dwo_unit != NULL;
23974 }
23975
23976 static void
23977 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23978 struct dwarf2_cu *cu, int is_block)
23979 {
23980 struct dwarf2_per_objfile *dwarf2_per_objfile
23981 = cu->per_cu->dwarf2_per_objfile;
23982 struct objfile *objfile = dwarf2_per_objfile->objfile;
23983 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23984
23985 if (attr->form_is_section_offset ()
23986 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23987 the section. If so, fall through to the complaint in the
23988 other branch. */
23989 && DW_UNSND (attr) < section->get_size (objfile))
23990 {
23991 struct dwarf2_loclist_baton *baton;
23992
23993 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23994
23995 fill_in_loclist_baton (cu, baton, attr);
23996
23997 if (cu->base_known == 0)
23998 complaint (_("Location list used without "
23999 "specifying the CU base address."));
24000
24001 SYMBOL_ACLASS_INDEX (sym) = (is_block
24002 ? dwarf2_loclist_block_index
24003 : dwarf2_loclist_index);
24004 SYMBOL_LOCATION_BATON (sym) = baton;
24005 }
24006 else
24007 {
24008 struct dwarf2_locexpr_baton *baton;
24009
24010 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24011 baton->per_cu = cu->per_cu;
24012 gdb_assert (baton->per_cu);
24013
24014 if (attr->form_is_block ())
24015 {
24016 /* Note that we're just copying the block's data pointer
24017 here, not the actual data. We're still pointing into the
24018 info_buffer for SYM's objfile; right now we never release
24019 that buffer, but when we do clean up properly this may
24020 need to change. */
24021 baton->size = DW_BLOCK (attr)->size;
24022 baton->data = DW_BLOCK (attr)->data;
24023 }
24024 else
24025 {
24026 dwarf2_invalid_attrib_class_complaint ("location description",
24027 sym->natural_name ());
24028 baton->size = 0;
24029 }
24030
24031 SYMBOL_ACLASS_INDEX (sym) = (is_block
24032 ? dwarf2_locexpr_block_index
24033 : dwarf2_locexpr_index);
24034 SYMBOL_LOCATION_BATON (sym) = baton;
24035 }
24036 }
24037
24038 /* See read.h. */
24039
24040 struct objfile *
24041 dwarf2_per_cu_data::objfile () const
24042 {
24043 struct objfile *objfile = dwarf2_per_objfile->objfile;
24044
24045 /* Return the master objfile, so that we can report and look up the
24046 correct file containing this variable. */
24047 if (objfile->separate_debug_objfile_backlink)
24048 objfile = objfile->separate_debug_objfile_backlink;
24049
24050 return objfile;
24051 }
24052
24053 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24054 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24055 CU_HEADERP first. */
24056
24057 static const struct comp_unit_head *
24058 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24059 const struct dwarf2_per_cu_data *per_cu)
24060 {
24061 const gdb_byte *info_ptr;
24062
24063 if (per_cu->cu)
24064 return &per_cu->cu->header;
24065
24066 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24067
24068 memset (cu_headerp, 0, sizeof (*cu_headerp));
24069 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24070 rcuh_kind::COMPILE);
24071
24072 return cu_headerp;
24073 }
24074
24075 /* See read.h. */
24076
24077 int
24078 dwarf2_per_cu_data::addr_size () const
24079 {
24080 struct comp_unit_head cu_header_local;
24081 const struct comp_unit_head *cu_headerp;
24082
24083 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24084
24085 return cu_headerp->addr_size;
24086 }
24087
24088 /* See read.h. */
24089
24090 int
24091 dwarf2_per_cu_data::offset_size () const
24092 {
24093 struct comp_unit_head cu_header_local;
24094 const struct comp_unit_head *cu_headerp;
24095
24096 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24097
24098 return cu_headerp->offset_size;
24099 }
24100
24101 /* See read.h. */
24102
24103 int
24104 dwarf2_per_cu_data::ref_addr_size () const
24105 {
24106 struct comp_unit_head cu_header_local;
24107 const struct comp_unit_head *cu_headerp;
24108
24109 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24110
24111 if (cu_headerp->version == 2)
24112 return cu_headerp->addr_size;
24113 else
24114 return cu_headerp->offset_size;
24115 }
24116
24117 /* See read.h. */
24118
24119 CORE_ADDR
24120 dwarf2_per_cu_data::text_offset () const
24121 {
24122 struct objfile *objfile = dwarf2_per_objfile->objfile;
24123
24124 return objfile->text_section_offset ();
24125 }
24126
24127 /* See read.h. */
24128
24129 struct type *
24130 dwarf2_per_cu_data::addr_type () const
24131 {
24132 struct objfile *objfile = dwarf2_per_objfile->objfile;
24133 struct type *void_type = objfile_type (objfile)->builtin_void;
24134 struct type *addr_type = lookup_pointer_type (void_type);
24135 int addr_size = this->addr_size ();
24136
24137 if (TYPE_LENGTH (addr_type) == addr_size)
24138 return addr_type;
24139
24140 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24141 return addr_type;
24142 }
24143
24144 /* Locate the .debug_info compilation unit from CU's objfile which contains
24145 the DIE at OFFSET. Raises an error on failure. */
24146
24147 static struct dwarf2_per_cu_data *
24148 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24149 unsigned int offset_in_dwz,
24150 struct dwarf2_per_objfile *dwarf2_per_objfile)
24151 {
24152 struct dwarf2_per_cu_data *this_cu;
24153 int low, high;
24154
24155 low = 0;
24156 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24157 while (high > low)
24158 {
24159 struct dwarf2_per_cu_data *mid_cu;
24160 int mid = low + (high - low) / 2;
24161
24162 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24163 if (mid_cu->is_dwz > offset_in_dwz
24164 || (mid_cu->is_dwz == offset_in_dwz
24165 && mid_cu->sect_off + mid_cu->length >= sect_off))
24166 high = mid;
24167 else
24168 low = mid + 1;
24169 }
24170 gdb_assert (low == high);
24171 this_cu = dwarf2_per_objfile->all_comp_units[low];
24172 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24173 {
24174 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24175 error (_("Dwarf Error: could not find partial DIE containing "
24176 "offset %s [in module %s]"),
24177 sect_offset_str (sect_off),
24178 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24179
24180 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24181 <= sect_off);
24182 return dwarf2_per_objfile->all_comp_units[low-1];
24183 }
24184 else
24185 {
24186 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24187 && sect_off >= this_cu->sect_off + this_cu->length)
24188 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24189 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24190 return this_cu;
24191 }
24192 }
24193
24194 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24195
24196 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24197 : per_cu (per_cu_),
24198 mark (false),
24199 has_loclist (false),
24200 checked_producer (false),
24201 producer_is_gxx_lt_4_6 (false),
24202 producer_is_gcc_lt_4_3 (false),
24203 producer_is_icc (false),
24204 producer_is_icc_lt_14 (false),
24205 producer_is_codewarrior (false),
24206 processing_has_namespace_info (false)
24207 {
24208 per_cu->cu = this;
24209 }
24210
24211 /* Destroy a dwarf2_cu. */
24212
24213 dwarf2_cu::~dwarf2_cu ()
24214 {
24215 per_cu->cu = NULL;
24216 }
24217
24218 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24219
24220 static void
24221 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24222 enum language pretend_language)
24223 {
24224 struct attribute *attr;
24225
24226 /* Set the language we're debugging. */
24227 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24228 if (attr != nullptr)
24229 set_cu_language (DW_UNSND (attr), cu);
24230 else
24231 {
24232 cu->language = pretend_language;
24233 cu->language_defn = language_def (cu->language);
24234 }
24235
24236 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24237 }
24238
24239 /* Increase the age counter on each cached compilation unit, and free
24240 any that are too old. */
24241
24242 static void
24243 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24244 {
24245 struct dwarf2_per_cu_data *per_cu, **last_chain;
24246
24247 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24248 per_cu = dwarf2_per_objfile->read_in_chain;
24249 while (per_cu != NULL)
24250 {
24251 per_cu->cu->last_used ++;
24252 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24253 dwarf2_mark (per_cu->cu);
24254 per_cu = per_cu->cu->read_in_chain;
24255 }
24256
24257 per_cu = dwarf2_per_objfile->read_in_chain;
24258 last_chain = &dwarf2_per_objfile->read_in_chain;
24259 while (per_cu != NULL)
24260 {
24261 struct dwarf2_per_cu_data *next_cu;
24262
24263 next_cu = per_cu->cu->read_in_chain;
24264
24265 if (!per_cu->cu->mark)
24266 {
24267 delete per_cu->cu;
24268 *last_chain = next_cu;
24269 }
24270 else
24271 last_chain = &per_cu->cu->read_in_chain;
24272
24273 per_cu = next_cu;
24274 }
24275 }
24276
24277 /* Remove a single compilation unit from the cache. */
24278
24279 static void
24280 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24281 {
24282 struct dwarf2_per_cu_data *per_cu, **last_chain;
24283 struct dwarf2_per_objfile *dwarf2_per_objfile
24284 = target_per_cu->dwarf2_per_objfile;
24285
24286 per_cu = dwarf2_per_objfile->read_in_chain;
24287 last_chain = &dwarf2_per_objfile->read_in_chain;
24288 while (per_cu != NULL)
24289 {
24290 struct dwarf2_per_cu_data *next_cu;
24291
24292 next_cu = per_cu->cu->read_in_chain;
24293
24294 if (per_cu == target_per_cu)
24295 {
24296 delete per_cu->cu;
24297 per_cu->cu = NULL;
24298 *last_chain = next_cu;
24299 break;
24300 }
24301 else
24302 last_chain = &per_cu->cu->read_in_chain;
24303
24304 per_cu = next_cu;
24305 }
24306 }
24307
24308 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24309 We store these in a hash table separate from the DIEs, and preserve them
24310 when the DIEs are flushed out of cache.
24311
24312 The CU "per_cu" pointer is needed because offset alone is not enough to
24313 uniquely identify the type. A file may have multiple .debug_types sections,
24314 or the type may come from a DWO file. Furthermore, while it's more logical
24315 to use per_cu->section+offset, with Fission the section with the data is in
24316 the DWO file but we don't know that section at the point we need it.
24317 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24318 because we can enter the lookup routine, get_die_type_at_offset, from
24319 outside this file, and thus won't necessarily have PER_CU->cu.
24320 Fortunately, PER_CU is stable for the life of the objfile. */
24321
24322 struct dwarf2_per_cu_offset_and_type
24323 {
24324 const struct dwarf2_per_cu_data *per_cu;
24325 sect_offset sect_off;
24326 struct type *type;
24327 };
24328
24329 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24330
24331 static hashval_t
24332 per_cu_offset_and_type_hash (const void *item)
24333 {
24334 const struct dwarf2_per_cu_offset_and_type *ofs
24335 = (const struct dwarf2_per_cu_offset_and_type *) item;
24336
24337 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24338 }
24339
24340 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24341
24342 static int
24343 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24344 {
24345 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24346 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24347 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24348 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24349
24350 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24351 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24352 }
24353
24354 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24355 table if necessary. For convenience, return TYPE.
24356
24357 The DIEs reading must have careful ordering to:
24358 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24359 reading current DIE.
24360 * Not trying to dereference contents of still incompletely read in types
24361 while reading in other DIEs.
24362 * Enable referencing still incompletely read in types just by a pointer to
24363 the type without accessing its fields.
24364
24365 Therefore caller should follow these rules:
24366 * Try to fetch any prerequisite types we may need to build this DIE type
24367 before building the type and calling set_die_type.
24368 * After building type call set_die_type for current DIE as soon as
24369 possible before fetching more types to complete the current type.
24370 * Make the type as complete as possible before fetching more types. */
24371
24372 static struct type *
24373 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24374 {
24375 struct dwarf2_per_objfile *dwarf2_per_objfile
24376 = cu->per_cu->dwarf2_per_objfile;
24377 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24378 struct objfile *objfile = dwarf2_per_objfile->objfile;
24379 struct attribute *attr;
24380 struct dynamic_prop prop;
24381
24382 /* For Ada types, make sure that the gnat-specific data is always
24383 initialized (if not already set). There are a few types where
24384 we should not be doing so, because the type-specific area is
24385 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24386 where the type-specific area is used to store the floatformat).
24387 But this is not a problem, because the gnat-specific information
24388 is actually not needed for these types. */
24389 if (need_gnat_info (cu)
24390 && TYPE_CODE (type) != TYPE_CODE_FUNC
24391 && TYPE_CODE (type) != TYPE_CODE_FLT
24392 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24393 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24394 && TYPE_CODE (type) != TYPE_CODE_METHOD
24395 && !HAVE_GNAT_AUX_INFO (type))
24396 INIT_GNAT_SPECIFIC (type);
24397
24398 /* Read DW_AT_allocated and set in type. */
24399 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24400 if (attr != NULL && attr->form_is_block ())
24401 {
24402 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24403 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24404 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24405 }
24406 else if (attr != NULL)
24407 {
24408 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24409 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24410 sect_offset_str (die->sect_off));
24411 }
24412
24413 /* Read DW_AT_associated and set in type. */
24414 attr = dwarf2_attr (die, DW_AT_associated, cu);
24415 if (attr != NULL && attr->form_is_block ())
24416 {
24417 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24418 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24419 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24420 }
24421 else if (attr != NULL)
24422 {
24423 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24424 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24425 sect_offset_str (die->sect_off));
24426 }
24427
24428 /* Read DW_AT_data_location and set in type. */
24429 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24430 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24431 cu->per_cu->addr_type ()))
24432 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24433
24434 if (dwarf2_per_objfile->die_type_hash == NULL)
24435 dwarf2_per_objfile->die_type_hash
24436 = htab_up (htab_create_alloc (127,
24437 per_cu_offset_and_type_hash,
24438 per_cu_offset_and_type_eq,
24439 NULL, xcalloc, xfree));
24440
24441 ofs.per_cu = cu->per_cu;
24442 ofs.sect_off = die->sect_off;
24443 ofs.type = type;
24444 slot = (struct dwarf2_per_cu_offset_and_type **)
24445 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24446 if (*slot)
24447 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24448 sect_offset_str (die->sect_off));
24449 *slot = XOBNEW (&objfile->objfile_obstack,
24450 struct dwarf2_per_cu_offset_and_type);
24451 **slot = ofs;
24452 return type;
24453 }
24454
24455 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24456 or return NULL if the die does not have a saved type. */
24457
24458 static struct type *
24459 get_die_type_at_offset (sect_offset sect_off,
24460 struct dwarf2_per_cu_data *per_cu)
24461 {
24462 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24463 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24464
24465 if (dwarf2_per_objfile->die_type_hash == NULL)
24466 return NULL;
24467
24468 ofs.per_cu = per_cu;
24469 ofs.sect_off = sect_off;
24470 slot = ((struct dwarf2_per_cu_offset_and_type *)
24471 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24472 if (slot)
24473 return slot->type;
24474 else
24475 return NULL;
24476 }
24477
24478 /* Look up the type for DIE in CU in die_type_hash,
24479 or return NULL if DIE does not have a saved type. */
24480
24481 static struct type *
24482 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24483 {
24484 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24485 }
24486
24487 /* Add a dependence relationship from CU to REF_PER_CU. */
24488
24489 static void
24490 dwarf2_add_dependence (struct dwarf2_cu *cu,
24491 struct dwarf2_per_cu_data *ref_per_cu)
24492 {
24493 void **slot;
24494
24495 if (cu->dependencies == NULL)
24496 cu->dependencies
24497 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24498 NULL, &cu->comp_unit_obstack,
24499 hashtab_obstack_allocate,
24500 dummy_obstack_deallocate);
24501
24502 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24503 if (*slot == NULL)
24504 *slot = ref_per_cu;
24505 }
24506
24507 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24508 Set the mark field in every compilation unit in the
24509 cache that we must keep because we are keeping CU. */
24510
24511 static int
24512 dwarf2_mark_helper (void **slot, void *data)
24513 {
24514 struct dwarf2_per_cu_data *per_cu;
24515
24516 per_cu = (struct dwarf2_per_cu_data *) *slot;
24517
24518 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24519 reading of the chain. As such dependencies remain valid it is not much
24520 useful to track and undo them during QUIT cleanups. */
24521 if (per_cu->cu == NULL)
24522 return 1;
24523
24524 if (per_cu->cu->mark)
24525 return 1;
24526 per_cu->cu->mark = true;
24527
24528 if (per_cu->cu->dependencies != NULL)
24529 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24530
24531 return 1;
24532 }
24533
24534 /* Set the mark field in CU and in every other compilation unit in the
24535 cache that we must keep because we are keeping CU. */
24536
24537 static void
24538 dwarf2_mark (struct dwarf2_cu *cu)
24539 {
24540 if (cu->mark)
24541 return;
24542 cu->mark = true;
24543 if (cu->dependencies != NULL)
24544 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24545 }
24546
24547 static void
24548 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24549 {
24550 while (per_cu)
24551 {
24552 per_cu->cu->mark = false;
24553 per_cu = per_cu->cu->read_in_chain;
24554 }
24555 }
24556
24557 /* Trivial hash function for partial_die_info: the hash value of a DIE
24558 is its offset in .debug_info for this objfile. */
24559
24560 static hashval_t
24561 partial_die_hash (const void *item)
24562 {
24563 const struct partial_die_info *part_die
24564 = (const struct partial_die_info *) item;
24565
24566 return to_underlying (part_die->sect_off);
24567 }
24568
24569 /* Trivial comparison function for partial_die_info structures: two DIEs
24570 are equal if they have the same offset. */
24571
24572 static int
24573 partial_die_eq (const void *item_lhs, const void *item_rhs)
24574 {
24575 const struct partial_die_info *part_die_lhs
24576 = (const struct partial_die_info *) item_lhs;
24577 const struct partial_die_info *part_die_rhs
24578 = (const struct partial_die_info *) item_rhs;
24579
24580 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24581 }
24582
24583 struct cmd_list_element *set_dwarf_cmdlist;
24584 struct cmd_list_element *show_dwarf_cmdlist;
24585
24586 static void
24587 set_dwarf_cmd (const char *args, int from_tty)
24588 {
24589 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24590 gdb_stdout);
24591 }
24592
24593 static void
24594 show_dwarf_cmd (const char *args, int from_tty)
24595 {
24596 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24597 }
24598
24599 static void
24600 show_check_physname (struct ui_file *file, int from_tty,
24601 struct cmd_list_element *c, const char *value)
24602 {
24603 fprintf_filtered (file,
24604 _("Whether to check \"physname\" is %s.\n"),
24605 value);
24606 }
24607
24608 void _initialize_dwarf2_read ();
24609 void
24610 _initialize_dwarf2_read ()
24611 {
24612 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24613 Set DWARF specific variables.\n\
24614 Configure DWARF variables such as the cache size."),
24615 &set_dwarf_cmdlist, "maintenance set dwarf ",
24616 0/*allow-unknown*/, &maintenance_set_cmdlist);
24617
24618 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24619 Show DWARF specific variables.\n\
24620 Show DWARF variables such as the cache size."),
24621 &show_dwarf_cmdlist, "maintenance show dwarf ",
24622 0/*allow-unknown*/, &maintenance_show_cmdlist);
24623
24624 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24625 &dwarf_max_cache_age, _("\
24626 Set the upper bound on the age of cached DWARF compilation units."), _("\
24627 Show the upper bound on the age of cached DWARF compilation units."), _("\
24628 A higher limit means that cached compilation units will be stored\n\
24629 in memory longer, and more total memory will be used. Zero disables\n\
24630 caching, which can slow down startup."),
24631 NULL,
24632 show_dwarf_max_cache_age,
24633 &set_dwarf_cmdlist,
24634 &show_dwarf_cmdlist);
24635
24636 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24637 Set debugging of the DWARF reader."), _("\
24638 Show debugging of the DWARF reader."), _("\
24639 When enabled (non-zero), debugging messages are printed during DWARF\n\
24640 reading and symtab expansion. A value of 1 (one) provides basic\n\
24641 information. A value greater than 1 provides more verbose information."),
24642 NULL,
24643 NULL,
24644 &setdebuglist, &showdebuglist);
24645
24646 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24647 Set debugging of the DWARF DIE reader."), _("\
24648 Show debugging of the DWARF DIE reader."), _("\
24649 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24650 The value is the maximum depth to print."),
24651 NULL,
24652 NULL,
24653 &setdebuglist, &showdebuglist);
24654
24655 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24656 Set debugging of the dwarf line reader."), _("\
24657 Show debugging of the dwarf line reader."), _("\
24658 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24659 A value of 1 (one) provides basic information.\n\
24660 A value greater than 1 provides more verbose information."),
24661 NULL,
24662 NULL,
24663 &setdebuglist, &showdebuglist);
24664
24665 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24666 Set cross-checking of \"physname\" code against demangler."), _("\
24667 Show cross-checking of \"physname\" code against demangler."), _("\
24668 When enabled, GDB's internal \"physname\" code is checked against\n\
24669 the demangler."),
24670 NULL, show_check_physname,
24671 &setdebuglist, &showdebuglist);
24672
24673 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24674 no_class, &use_deprecated_index_sections, _("\
24675 Set whether to use deprecated gdb_index sections."), _("\
24676 Show whether to use deprecated gdb_index sections."), _("\
24677 When enabled, deprecated .gdb_index sections are used anyway.\n\
24678 Normally they are ignored either because of a missing feature or\n\
24679 performance issue.\n\
24680 Warning: This option must be enabled before gdb reads the file."),
24681 NULL,
24682 NULL,
24683 &setlist, &showlist);
24684
24685 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24686 &dwarf2_locexpr_funcs);
24687 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24688 &dwarf2_loclist_funcs);
24689
24690 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24691 &dwarf2_block_frame_base_locexpr_funcs);
24692 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24693 &dwarf2_block_frame_base_loclist_funcs);
24694
24695 #if GDB_SELF_TEST
24696 selftests::register_test ("dw2_expand_symtabs_matching",
24697 selftests::dw2_expand_symtabs_matching::run_test);
24698 #endif
24699 }