Convert read_offset to method on comp_unit_head
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "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, int keep,
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 ~cutu_reader ();
892
893 DISABLE_COPY_AND_ASSIGN (cutu_reader);
894
895 const gdb_byte *info_ptr = nullptr;
896 struct die_info *comp_unit_die = nullptr;
897 bool dummy_p = false;
898
899 private:
900 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
901 int use_existing_cu, int keep);
902
903 struct dwarf2_per_cu_data *m_this_cu;
904 int m_keep = 0;
905 std::unique_ptr<dwarf2_cu> m_new_cu;
906
907 /* The ordinary abbreviation table. */
908 abbrev_table_up m_abbrev_table_holder;
909
910 /* The DWO abbreviation table. */
911 abbrev_table_up m_dwo_abbrev_table;
912 };
913
914 /* When we construct a partial symbol table entry we only
915 need this much information. */
916 struct partial_die_info : public allocate_on_obstack
917 {
918 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
919
920 /* Disable assign but still keep copy ctor, which is needed
921 load_partial_dies. */
922 partial_die_info& operator=(const partial_die_info& rhs) = delete;
923
924 /* Adjust the partial die before generating a symbol for it. This
925 function may set the is_external flag or change the DIE's
926 name. */
927 void fixup (struct dwarf2_cu *cu);
928
929 /* Read a minimal amount of information into the minimal die
930 structure. */
931 const gdb_byte *read (const struct die_reader_specs *reader,
932 const struct abbrev_info &abbrev,
933 const gdb_byte *info_ptr);
934
935 /* Offset of this DIE. */
936 const sect_offset sect_off;
937
938 /* DWARF-2 tag for this DIE. */
939 const ENUM_BITFIELD(dwarf_tag) tag : 16;
940
941 /* Assorted flags describing the data found in this DIE. */
942 const unsigned int has_children : 1;
943
944 unsigned int is_external : 1;
945 unsigned int is_declaration : 1;
946 unsigned int has_type : 1;
947 unsigned int has_specification : 1;
948 unsigned int has_pc_info : 1;
949 unsigned int may_be_inlined : 1;
950
951 /* This DIE has been marked DW_AT_main_subprogram. */
952 unsigned int main_subprogram : 1;
953
954 /* Flag set if the SCOPE field of this structure has been
955 computed. */
956 unsigned int scope_set : 1;
957
958 /* Flag set if the DIE has a byte_size attribute. */
959 unsigned int has_byte_size : 1;
960
961 /* Flag set if the DIE has a DW_AT_const_value attribute. */
962 unsigned int has_const_value : 1;
963
964 /* Flag set if any of the DIE's children are template arguments. */
965 unsigned int has_template_arguments : 1;
966
967 /* Flag set if fixup has been called on this die. */
968 unsigned int fixup_called : 1;
969
970 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
971 unsigned int is_dwz : 1;
972
973 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
974 unsigned int spec_is_dwz : 1;
975
976 /* The name of this DIE. Normally the value of DW_AT_name, but
977 sometimes a default name for unnamed DIEs. */
978 const char *name = nullptr;
979
980 /* The linkage name, if present. */
981 const char *linkage_name = nullptr;
982
983 /* The scope to prepend to our children. This is generally
984 allocated on the comp_unit_obstack, so will disappear
985 when this compilation unit leaves the cache. */
986 const char *scope = nullptr;
987
988 /* Some data associated with the partial DIE. The tag determines
989 which field is live. */
990 union
991 {
992 /* The location description associated with this DIE, if any. */
993 struct dwarf_block *locdesc;
994 /* The offset of an import, for DW_TAG_imported_unit. */
995 sect_offset sect_off;
996 } d {};
997
998 /* If HAS_PC_INFO, the PC range associated with this DIE. */
999 CORE_ADDR lowpc = 0;
1000 CORE_ADDR highpc = 0;
1001
1002 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1003 DW_AT_sibling, if any. */
1004 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1005 could return DW_AT_sibling values to its caller load_partial_dies. */
1006 const gdb_byte *sibling = nullptr;
1007
1008 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1009 DW_AT_specification (or DW_AT_abstract_origin or
1010 DW_AT_extension). */
1011 sect_offset spec_offset {};
1012
1013 /* Pointers to this DIE's parent, first child, and next sibling,
1014 if any. */
1015 struct partial_die_info *die_parent = nullptr;
1016 struct partial_die_info *die_child = nullptr;
1017 struct partial_die_info *die_sibling = nullptr;
1018
1019 friend struct partial_die_info *
1020 dwarf2_cu::find_partial_die (sect_offset sect_off);
1021
1022 private:
1023 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1024 partial_die_info (sect_offset sect_off)
1025 : partial_die_info (sect_off, DW_TAG_padding, 0)
1026 {
1027 }
1028
1029 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1030 int has_children_)
1031 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1032 {
1033 is_external = 0;
1034 is_declaration = 0;
1035 has_type = 0;
1036 has_specification = 0;
1037 has_pc_info = 0;
1038 may_be_inlined = 0;
1039 main_subprogram = 0;
1040 scope_set = 0;
1041 has_byte_size = 0;
1042 has_const_value = 0;
1043 has_template_arguments = 0;
1044 fixup_called = 0;
1045 is_dwz = 0;
1046 spec_is_dwz = 0;
1047 }
1048 };
1049
1050 /* This data structure holds a complete die structure. */
1051 struct die_info
1052 {
1053 /* DWARF-2 tag for this DIE. */
1054 ENUM_BITFIELD(dwarf_tag) tag : 16;
1055
1056 /* Number of attributes */
1057 unsigned char num_attrs;
1058
1059 /* True if we're presently building the full type name for the
1060 type derived from this DIE. */
1061 unsigned char building_fullname : 1;
1062
1063 /* True if this die is in process. PR 16581. */
1064 unsigned char in_process : 1;
1065
1066 /* True if this DIE has children. */
1067 unsigned char has_children : 1;
1068
1069 /* Abbrev number */
1070 unsigned int abbrev;
1071
1072 /* Offset in .debug_info or .debug_types section. */
1073 sect_offset sect_off;
1074
1075 /* The dies in a compilation unit form an n-ary tree. PARENT
1076 points to this die's parent; CHILD points to the first child of
1077 this node; and all the children of a given node are chained
1078 together via their SIBLING fields. */
1079 struct die_info *child; /* Its first child, if any. */
1080 struct die_info *sibling; /* Its next sibling, if any. */
1081 struct die_info *parent; /* Its parent, if any. */
1082
1083 /* An array of attributes, with NUM_ATTRS elements. There may be
1084 zero, but it's not common and zero-sized arrays are not
1085 sufficiently portable C. */
1086 struct attribute attrs[1];
1087 };
1088
1089 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1090 but this would require a corresponding change in unpack_field_as_long
1091 and friends. */
1092 static int bits_per_byte = 8;
1093
1094 /* When reading a variant or variant part, we track a bit more
1095 information about the field, and store it in an object of this
1096 type. */
1097
1098 struct variant_field
1099 {
1100 /* If we see a DW_TAG_variant, then this will be the discriminant
1101 value. */
1102 ULONGEST discriminant_value;
1103 /* If we see a DW_TAG_variant, then this will be set if this is the
1104 default branch. */
1105 bool default_branch;
1106 /* While reading a DW_TAG_variant_part, this will be set if this
1107 field is the discriminant. */
1108 bool is_discriminant;
1109 };
1110
1111 struct nextfield
1112 {
1113 int accessibility = 0;
1114 int virtuality = 0;
1115 /* Extra information to describe a variant or variant part. */
1116 struct variant_field variant {};
1117 struct field field {};
1118 };
1119
1120 struct fnfieldlist
1121 {
1122 const char *name = nullptr;
1123 std::vector<struct fn_field> fnfields;
1124 };
1125
1126 /* The routines that read and process dies for a C struct or C++ class
1127 pass lists of data member fields and lists of member function fields
1128 in an instance of a field_info structure, as defined below. */
1129 struct field_info
1130 {
1131 /* List of data member and baseclasses fields. */
1132 std::vector<struct nextfield> fields;
1133 std::vector<struct nextfield> baseclasses;
1134
1135 /* Number of fields (including baseclasses). */
1136 int nfields = 0;
1137
1138 /* Set if the accessibility of one of the fields is not public. */
1139 int non_public_fields = 0;
1140
1141 /* Member function fieldlist array, contains name of possibly overloaded
1142 member function, number of overloaded member functions and a pointer
1143 to the head of the member function field chain. */
1144 std::vector<struct fnfieldlist> fnfieldlists;
1145
1146 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1147 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1148 std::vector<struct decl_field> typedef_field_list;
1149
1150 /* Nested types defined by this class and the number of elements in this
1151 list. */
1152 std::vector<struct decl_field> nested_types_list;
1153 };
1154
1155 /* Loaded secondary compilation units are kept in memory until they
1156 have not been referenced for the processing of this many
1157 compilation units. Set this to zero to disable caching. Cache
1158 sizes of up to at least twenty will improve startup time for
1159 typical inter-CU-reference binaries, at an obvious memory cost. */
1160 static int dwarf_max_cache_age = 5;
1161 static void
1162 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1163 struct cmd_list_element *c, const char *value)
1164 {
1165 fprintf_filtered (file, _("The upper bound on the age of cached "
1166 "DWARF compilation units is %s.\n"),
1167 value);
1168 }
1169 \f
1170 /* local function prototypes */
1171
1172 static void dwarf2_find_base_address (struct die_info *die,
1173 struct dwarf2_cu *cu);
1174
1175 static dwarf2_psymtab *create_partial_symtab
1176 (struct dwarf2_per_cu_data *per_cu, const char *name);
1177
1178 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1179 const gdb_byte *info_ptr,
1180 struct die_info *type_unit_die);
1181
1182 static void dwarf2_build_psymtabs_hard
1183 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1184
1185 static void scan_partial_symbols (struct partial_die_info *,
1186 CORE_ADDR *, CORE_ADDR *,
1187 int, struct dwarf2_cu *);
1188
1189 static void add_partial_symbol (struct partial_die_info *,
1190 struct dwarf2_cu *);
1191
1192 static void add_partial_namespace (struct partial_die_info *pdi,
1193 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1194 int set_addrmap, struct dwarf2_cu *cu);
1195
1196 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1197 CORE_ADDR *highpc, int set_addrmap,
1198 struct dwarf2_cu *cu);
1199
1200 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1201 struct dwarf2_cu *cu);
1202
1203 static void add_partial_subprogram (struct partial_die_info *pdi,
1204 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1205 int need_pc, struct dwarf2_cu *cu);
1206
1207 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1208
1209 static struct partial_die_info *load_partial_dies
1210 (const struct die_reader_specs *, const gdb_byte *, int);
1211
1212 /* A pair of partial_die_info and compilation unit. */
1213 struct cu_partial_die_info
1214 {
1215 /* The compilation unit of the partial_die_info. */
1216 struct dwarf2_cu *cu;
1217 /* A partial_die_info. */
1218 struct partial_die_info *pdi;
1219
1220 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1221 : cu (cu),
1222 pdi (pdi)
1223 { /* Nothing. */ }
1224
1225 private:
1226 cu_partial_die_info () = delete;
1227 };
1228
1229 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1230 struct dwarf2_cu *);
1231
1232 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1233 struct attribute *, struct attr_abbrev *,
1234 const gdb_byte *, bool *need_reprocess);
1235
1236 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1237 struct attribute *attr);
1238
1239 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1240
1241 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1242 unsigned int *);
1243
1244 static LONGEST read_checked_initial_length_and_offset
1245 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1246 unsigned int *, unsigned int *);
1247
1248 static sect_offset read_abbrev_offset
1249 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1250 struct dwarf2_section_info *, sect_offset);
1251
1252 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1253
1254 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1255
1256 static const char *read_indirect_string
1257 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1258 const struct comp_unit_head *, unsigned int *);
1259
1260 static const char *read_indirect_line_string
1261 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1262 const struct comp_unit_head *, unsigned int *);
1263
1264 static const char *read_indirect_string_at_offset
1265 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1266 LONGEST str_offset);
1267
1268 static const char *read_indirect_string_from_dwz
1269 (struct objfile *objfile, struct dwz_file *, LONGEST);
1270
1271 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1272 const gdb_byte *,
1273 unsigned int *);
1274
1275 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1276 ULONGEST str_index);
1277
1278 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1279 ULONGEST str_index);
1280
1281 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1282
1283 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1284 struct dwarf2_cu *);
1285
1286 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1287 unsigned int);
1288
1289 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1290 struct dwarf2_cu *cu);
1291
1292 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1293
1294 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1295 struct dwarf2_cu *cu);
1296
1297 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1298
1299 static struct die_info *die_specification (struct die_info *die,
1300 struct dwarf2_cu **);
1301
1302 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1303 struct dwarf2_cu *cu);
1304
1305 static void dwarf_decode_lines (struct line_header *, const char *,
1306 struct dwarf2_cu *, dwarf2_psymtab *,
1307 CORE_ADDR, int decode_mapping);
1308
1309 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1310 const char *);
1311
1312 static struct symbol *new_symbol (struct die_info *, struct type *,
1313 struct dwarf2_cu *, struct symbol * = NULL);
1314
1315 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1316 struct dwarf2_cu *);
1317
1318 static void dwarf2_const_value_attr (const struct attribute *attr,
1319 struct type *type,
1320 const char *name,
1321 struct obstack *obstack,
1322 struct dwarf2_cu *cu, LONGEST *value,
1323 const gdb_byte **bytes,
1324 struct dwarf2_locexpr_baton **baton);
1325
1326 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1327
1328 static int need_gnat_info (struct dwarf2_cu *);
1329
1330 static struct type *die_descriptive_type (struct die_info *,
1331 struct dwarf2_cu *);
1332
1333 static void set_descriptive_type (struct type *, struct die_info *,
1334 struct dwarf2_cu *);
1335
1336 static struct type *die_containing_type (struct die_info *,
1337 struct dwarf2_cu *);
1338
1339 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1340 struct dwarf2_cu *);
1341
1342 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1343
1344 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1345
1346 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1347
1348 static char *typename_concat (struct obstack *obs, const char *prefix,
1349 const char *suffix, int physname,
1350 struct dwarf2_cu *cu);
1351
1352 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1355
1356 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1357
1358 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1359
1360 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1361
1362 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1365 struct dwarf2_cu *, dwarf2_psymtab *);
1366
1367 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1368 values. Keep the items ordered with increasing constraints compliance. */
1369 enum pc_bounds_kind
1370 {
1371 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1372 PC_BOUNDS_NOT_PRESENT,
1373
1374 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1375 were present but they do not form a valid range of PC addresses. */
1376 PC_BOUNDS_INVALID,
1377
1378 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1379 PC_BOUNDS_RANGES,
1380
1381 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1382 PC_BOUNDS_HIGH_LOW,
1383 };
1384
1385 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1386 CORE_ADDR *, CORE_ADDR *,
1387 struct dwarf2_cu *,
1388 dwarf2_psymtab *);
1389
1390 static void get_scope_pc_bounds (struct die_info *,
1391 CORE_ADDR *, CORE_ADDR *,
1392 struct dwarf2_cu *);
1393
1394 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1395 CORE_ADDR, struct dwarf2_cu *);
1396
1397 static void dwarf2_add_field (struct field_info *, struct die_info *,
1398 struct dwarf2_cu *);
1399
1400 static void dwarf2_attach_fields_to_type (struct field_info *,
1401 struct type *, struct dwarf2_cu *);
1402
1403 static void dwarf2_add_member_fn (struct field_info *,
1404 struct die_info *, struct type *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1408 struct type *,
1409 struct dwarf2_cu *);
1410
1411 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1412
1413 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1414
1415 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1416
1417 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1418
1419 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1420
1421 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1422
1423 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1424
1425 static struct type *read_module_type (struct die_info *die,
1426 struct dwarf2_cu *cu);
1427
1428 static const char *namespace_name (struct die_info *die,
1429 int *is_anonymous, struct dwarf2_cu *);
1430
1431 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1432
1433 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1434
1435 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1436 struct dwarf2_cu *);
1437
1438 static struct die_info *read_die_and_siblings_1
1439 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1440 struct die_info *);
1441
1442 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1443 const gdb_byte *info_ptr,
1444 const gdb_byte **new_info_ptr,
1445 struct die_info *parent);
1446
1447 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1448 struct die_info **, const gdb_byte *,
1449 int);
1450
1451 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1452 struct die_info **, const gdb_byte *);
1453
1454 static void process_die (struct die_info *, struct dwarf2_cu *);
1455
1456 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1457 struct obstack *);
1458
1459 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1460
1461 static const char *dwarf2_full_name (const char *name,
1462 struct die_info *die,
1463 struct dwarf2_cu *cu);
1464
1465 static const char *dwarf2_physname (const char *name, struct die_info *die,
1466 struct dwarf2_cu *cu);
1467
1468 static struct die_info *dwarf2_extension (struct die_info *die,
1469 struct dwarf2_cu **);
1470
1471 static const char *dwarf_tag_name (unsigned int);
1472
1473 static const char *dwarf_attr_name (unsigned int);
1474
1475 static const char *dwarf_form_name (unsigned int);
1476
1477 static const char *dwarf_bool_name (unsigned int);
1478
1479 static const char *dwarf_type_encoding_name (unsigned int);
1480
1481 static struct die_info *sibling_die (struct die_info *);
1482
1483 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1484
1485 static void dump_die_for_error (struct die_info *);
1486
1487 static void dump_die_1 (struct ui_file *, int level, int max_level,
1488 struct die_info *);
1489
1490 /*static*/ void dump_die (struct die_info *, int max_level);
1491
1492 static void store_in_ref_table (struct die_info *,
1493 struct dwarf2_cu *);
1494
1495 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1496
1497 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1498
1499 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1500 const struct attribute *,
1501 struct dwarf2_cu **);
1502
1503 static struct die_info *follow_die_ref (struct die_info *,
1504 const struct attribute *,
1505 struct dwarf2_cu **);
1506
1507 static struct die_info *follow_die_sig (struct die_info *,
1508 const struct attribute *,
1509 struct dwarf2_cu **);
1510
1511 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1512 struct dwarf2_cu *);
1513
1514 static struct type *get_DW_AT_signature_type (struct die_info *,
1515 const struct attribute *,
1516 struct dwarf2_cu *);
1517
1518 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1519
1520 static void read_signatured_type (struct signatured_type *);
1521
1522 static int attr_to_dynamic_prop (const struct attribute *attr,
1523 struct die_info *die, struct dwarf2_cu *cu,
1524 struct dynamic_prop *prop, struct type *type);
1525
1526 /* memory allocation interface */
1527
1528 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1529
1530 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1531
1532 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1533
1534 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1535 struct dwarf2_loclist_baton *baton,
1536 const struct attribute *attr);
1537
1538 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1539 struct symbol *sym,
1540 struct dwarf2_cu *cu,
1541 int is_block);
1542
1543 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1544 const gdb_byte *info_ptr,
1545 struct abbrev_info *abbrev);
1546
1547 static hashval_t partial_die_hash (const void *item);
1548
1549 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1550
1551 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1552 (sect_offset sect_off, unsigned int offset_in_dwz,
1553 struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1556 struct die_info *comp_unit_die,
1557 enum language pretend_language);
1558
1559 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1562
1563 static struct type *set_die_type (struct die_info *, struct type *,
1564 struct dwarf2_cu *);
1565
1566 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1567
1568 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1569
1570 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1571 enum language);
1572
1573 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1574 enum language);
1575
1576 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1577 enum language);
1578
1579 static void dwarf2_add_dependence (struct dwarf2_cu *,
1580 struct dwarf2_per_cu_data *);
1581
1582 static void dwarf2_mark (struct dwarf2_cu *);
1583
1584 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1585
1586 static struct type *get_die_type_at_offset (sect_offset,
1587 struct dwarf2_per_cu_data *);
1588
1589 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1590
1591 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1592 enum language pretend_language);
1593
1594 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1595
1596 /* Class, the destructor of which frees all allocated queue entries. This
1597 will only have work to do if an error was thrown while processing the
1598 dwarf. If no error was thrown then the queue entries should have all
1599 been processed, and freed, as we went along. */
1600
1601 class dwarf2_queue_guard
1602 {
1603 public:
1604 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1605 : m_per_objfile (per_objfile)
1606 {
1607 }
1608
1609 /* Free any entries remaining on the queue. There should only be
1610 entries left if we hit an error while processing the dwarf. */
1611 ~dwarf2_queue_guard ()
1612 {
1613 /* Ensure that no memory is allocated by the queue. */
1614 std::queue<dwarf2_queue_item> empty;
1615 std::swap (m_per_objfile->queue, empty);
1616 }
1617
1618 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1619
1620 private:
1621 dwarf2_per_objfile *m_per_objfile;
1622 };
1623
1624 dwarf2_queue_item::~dwarf2_queue_item ()
1625 {
1626 /* Anything still marked queued is likely to be in an
1627 inconsistent state, so discard it. */
1628 if (per_cu->queued)
1629 {
1630 if (per_cu->cu != NULL)
1631 free_one_cached_comp_unit (per_cu);
1632 per_cu->queued = 0;
1633 }
1634 }
1635
1636 /* The return type of find_file_and_directory. Note, the enclosed
1637 string pointers are only valid while this object is valid. */
1638
1639 struct file_and_directory
1640 {
1641 /* The filename. This is never NULL. */
1642 const char *name;
1643
1644 /* The compilation directory. NULL if not known. If we needed to
1645 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1646 points directly to the DW_AT_comp_dir string attribute owned by
1647 the obstack that owns the DIE. */
1648 const char *comp_dir;
1649
1650 /* If we needed to build a new string for comp_dir, this is what
1651 owns the storage. */
1652 std::string comp_dir_storage;
1653 };
1654
1655 static file_and_directory find_file_and_directory (struct die_info *die,
1656 struct dwarf2_cu *cu);
1657
1658 static htab_up allocate_signatured_type_table (struct objfile *objfile);
1659
1660 static htab_up allocate_dwo_unit_table (struct objfile *objfile);
1661
1662 static struct dwo_unit *lookup_dwo_unit_in_dwp
1663 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1664 struct dwp_file *dwp_file, const char *comp_dir,
1665 ULONGEST signature, int is_debug_types);
1666
1667 static struct dwp_file *get_dwp_file
1668 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1669
1670 static struct dwo_unit *lookup_dwo_comp_unit
1671 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1672
1673 static struct dwo_unit *lookup_dwo_type_unit
1674 (struct signatured_type *, const char *, const char *);
1675
1676 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1677
1678 /* A unique pointer to a dwo_file. */
1679
1680 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1681
1682 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1683
1684 static void check_producer (struct dwarf2_cu *cu);
1685
1686 static void free_line_header_voidp (void *arg);
1687 \f
1688 /* Various complaints about symbol reading that don't abort the process. */
1689
1690 static void
1691 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1692 {
1693 complaint (_("statement list doesn't fit in .debug_line section"));
1694 }
1695
1696 static void
1697 dwarf2_debug_line_missing_file_complaint (void)
1698 {
1699 complaint (_(".debug_line section has line data without a file"));
1700 }
1701
1702 static void
1703 dwarf2_debug_line_missing_end_sequence_complaint (void)
1704 {
1705 complaint (_(".debug_line section has line "
1706 "program sequence without an end"));
1707 }
1708
1709 static void
1710 dwarf2_complex_location_expr_complaint (void)
1711 {
1712 complaint (_("location expression too complex"));
1713 }
1714
1715 static void
1716 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1717 int arg3)
1718 {
1719 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1720 arg1, arg2, arg3);
1721 }
1722
1723 static void
1724 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1725 {
1726 complaint (_("debug info runs off end of %s section"
1727 " [in module %s]"),
1728 section->get_name (),
1729 section->get_file_name ());
1730 }
1731
1732 static void
1733 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1734 {
1735 complaint (_("macro debug info contains a "
1736 "malformed macro definition:\n`%s'"),
1737 arg1);
1738 }
1739
1740 static void
1741 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1742 {
1743 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1744 arg1, arg2);
1745 }
1746
1747 /* Hash function for line_header_hash. */
1748
1749 static hashval_t
1750 line_header_hash (const struct line_header *ofs)
1751 {
1752 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1753 }
1754
1755 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1756
1757 static hashval_t
1758 line_header_hash_voidp (const void *item)
1759 {
1760 const struct line_header *ofs = (const struct line_header *) item;
1761
1762 return line_header_hash (ofs);
1763 }
1764
1765 /* Equality function for line_header_hash. */
1766
1767 static int
1768 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1769 {
1770 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1771 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1772
1773 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1774 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1775 }
1776
1777 \f
1778
1779 /* See declaration. */
1780
1781 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1782 const dwarf2_debug_sections *names,
1783 bool can_copy_)
1784 : objfile (objfile_),
1785 can_copy (can_copy_)
1786 {
1787 if (names == NULL)
1788 names = &dwarf2_elf_names;
1789
1790 bfd *obfd = objfile->obfd;
1791
1792 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1793 locate_sections (obfd, sec, *names);
1794 }
1795
1796 dwarf2_per_objfile::~dwarf2_per_objfile ()
1797 {
1798 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1799 free_cached_comp_units ();
1800
1801 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1802 per_cu->imported_symtabs_free ();
1803
1804 for (signatured_type *sig_type : all_type_units)
1805 sig_type->per_cu.imported_symtabs_free ();
1806
1807 /* Everything else should be on the objfile obstack. */
1808 }
1809
1810 /* See declaration. */
1811
1812 void
1813 dwarf2_per_objfile::free_cached_comp_units ()
1814 {
1815 dwarf2_per_cu_data *per_cu = read_in_chain;
1816 dwarf2_per_cu_data **last_chain = &read_in_chain;
1817 while (per_cu != NULL)
1818 {
1819 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1820
1821 delete per_cu->cu;
1822 *last_chain = next_cu;
1823 per_cu = next_cu;
1824 }
1825 }
1826
1827 /* A helper class that calls free_cached_comp_units on
1828 destruction. */
1829
1830 class free_cached_comp_units
1831 {
1832 public:
1833
1834 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1835 : m_per_objfile (per_objfile)
1836 {
1837 }
1838
1839 ~free_cached_comp_units ()
1840 {
1841 m_per_objfile->free_cached_comp_units ();
1842 }
1843
1844 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1845
1846 private:
1847
1848 dwarf2_per_objfile *m_per_objfile;
1849 };
1850
1851 /* Try to locate the sections we need for DWARF 2 debugging
1852 information and return true if we have enough to do something.
1853 NAMES points to the dwarf2 section names, or is NULL if the standard
1854 ELF names are used. CAN_COPY is true for formats where symbol
1855 interposition is possible and so symbol values must follow copy
1856 relocation rules. */
1857
1858 int
1859 dwarf2_has_info (struct objfile *objfile,
1860 const struct dwarf2_debug_sections *names,
1861 bool can_copy)
1862 {
1863 if (objfile->flags & OBJF_READNEVER)
1864 return 0;
1865
1866 struct dwarf2_per_objfile *dwarf2_per_objfile
1867 = get_dwarf2_per_objfile (objfile);
1868
1869 if (dwarf2_per_objfile == NULL)
1870 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1871 names,
1872 can_copy);
1873
1874 return (!dwarf2_per_objfile->info.is_virtual
1875 && dwarf2_per_objfile->info.s.section != NULL
1876 && !dwarf2_per_objfile->abbrev.is_virtual
1877 && dwarf2_per_objfile->abbrev.s.section != NULL);
1878 }
1879
1880 /* When loading sections, we look either for uncompressed section or for
1881 compressed section names. */
1882
1883 static int
1884 section_is_p (const char *section_name,
1885 const struct dwarf2_section_names *names)
1886 {
1887 if (names->normal != NULL
1888 && strcmp (section_name, names->normal) == 0)
1889 return 1;
1890 if (names->compressed != NULL
1891 && strcmp (section_name, names->compressed) == 0)
1892 return 1;
1893 return 0;
1894 }
1895
1896 /* See declaration. */
1897
1898 void
1899 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1900 const dwarf2_debug_sections &names)
1901 {
1902 flagword aflag = bfd_section_flags (sectp);
1903
1904 if ((aflag & SEC_HAS_CONTENTS) == 0)
1905 {
1906 }
1907 else if (elf_section_data (sectp)->this_hdr.sh_size
1908 > bfd_get_file_size (abfd))
1909 {
1910 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1911 warning (_("Discarding section %s which has a section size (%s"
1912 ") larger than the file size [in module %s]"),
1913 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1914 bfd_get_filename (abfd));
1915 }
1916 else if (section_is_p (sectp->name, &names.info))
1917 {
1918 this->info.s.section = sectp;
1919 this->info.size = bfd_section_size (sectp);
1920 }
1921 else if (section_is_p (sectp->name, &names.abbrev))
1922 {
1923 this->abbrev.s.section = sectp;
1924 this->abbrev.size = bfd_section_size (sectp);
1925 }
1926 else if (section_is_p (sectp->name, &names.line))
1927 {
1928 this->line.s.section = sectp;
1929 this->line.size = bfd_section_size (sectp);
1930 }
1931 else if (section_is_p (sectp->name, &names.loc))
1932 {
1933 this->loc.s.section = sectp;
1934 this->loc.size = bfd_section_size (sectp);
1935 }
1936 else if (section_is_p (sectp->name, &names.loclists))
1937 {
1938 this->loclists.s.section = sectp;
1939 this->loclists.size = bfd_section_size (sectp);
1940 }
1941 else if (section_is_p (sectp->name, &names.macinfo))
1942 {
1943 this->macinfo.s.section = sectp;
1944 this->macinfo.size = bfd_section_size (sectp);
1945 }
1946 else if (section_is_p (sectp->name, &names.macro))
1947 {
1948 this->macro.s.section = sectp;
1949 this->macro.size = bfd_section_size (sectp);
1950 }
1951 else if (section_is_p (sectp->name, &names.str))
1952 {
1953 this->str.s.section = sectp;
1954 this->str.size = bfd_section_size (sectp);
1955 }
1956 else if (section_is_p (sectp->name, &names.str_offsets))
1957 {
1958 this->str_offsets.s.section = sectp;
1959 this->str_offsets.size = bfd_section_size (sectp);
1960 }
1961 else if (section_is_p (sectp->name, &names.line_str))
1962 {
1963 this->line_str.s.section = sectp;
1964 this->line_str.size = bfd_section_size (sectp);
1965 }
1966 else if (section_is_p (sectp->name, &names.addr))
1967 {
1968 this->addr.s.section = sectp;
1969 this->addr.size = bfd_section_size (sectp);
1970 }
1971 else if (section_is_p (sectp->name, &names.frame))
1972 {
1973 this->frame.s.section = sectp;
1974 this->frame.size = bfd_section_size (sectp);
1975 }
1976 else if (section_is_p (sectp->name, &names.eh_frame))
1977 {
1978 this->eh_frame.s.section = sectp;
1979 this->eh_frame.size = bfd_section_size (sectp);
1980 }
1981 else if (section_is_p (sectp->name, &names.ranges))
1982 {
1983 this->ranges.s.section = sectp;
1984 this->ranges.size = bfd_section_size (sectp);
1985 }
1986 else if (section_is_p (sectp->name, &names.rnglists))
1987 {
1988 this->rnglists.s.section = sectp;
1989 this->rnglists.size = bfd_section_size (sectp);
1990 }
1991 else if (section_is_p (sectp->name, &names.types))
1992 {
1993 struct dwarf2_section_info type_section;
1994
1995 memset (&type_section, 0, sizeof (type_section));
1996 type_section.s.section = sectp;
1997 type_section.size = bfd_section_size (sectp);
1998
1999 this->types.push_back (type_section);
2000 }
2001 else if (section_is_p (sectp->name, &names.gdb_index))
2002 {
2003 this->gdb_index.s.section = sectp;
2004 this->gdb_index.size = bfd_section_size (sectp);
2005 }
2006 else if (section_is_p (sectp->name, &names.debug_names))
2007 {
2008 this->debug_names.s.section = sectp;
2009 this->debug_names.size = bfd_section_size (sectp);
2010 }
2011 else if (section_is_p (sectp->name, &names.debug_aranges))
2012 {
2013 this->debug_aranges.s.section = sectp;
2014 this->debug_aranges.size = bfd_section_size (sectp);
2015 }
2016
2017 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2018 && bfd_section_vma (sectp) == 0)
2019 this->has_section_at_zero = true;
2020 }
2021
2022 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2023 SECTION_NAME. */
2024
2025 void
2026 dwarf2_get_section_info (struct objfile *objfile,
2027 enum dwarf2_section_enum sect,
2028 asection **sectp, const gdb_byte **bufp,
2029 bfd_size_type *sizep)
2030 {
2031 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2032 struct dwarf2_section_info *info;
2033
2034 /* We may see an objfile without any DWARF, in which case we just
2035 return nothing. */
2036 if (data == NULL)
2037 {
2038 *sectp = NULL;
2039 *bufp = NULL;
2040 *sizep = 0;
2041 return;
2042 }
2043 switch (sect)
2044 {
2045 case DWARF2_DEBUG_FRAME:
2046 info = &data->frame;
2047 break;
2048 case DWARF2_EH_FRAME:
2049 info = &data->eh_frame;
2050 break;
2051 default:
2052 gdb_assert_not_reached ("unexpected section");
2053 }
2054
2055 info->read (objfile);
2056
2057 *sectp = info->get_bfd_section ();
2058 *bufp = info->buffer;
2059 *sizep = info->size;
2060 }
2061
2062 /* A helper function to find the sections for a .dwz file. */
2063
2064 static void
2065 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2066 {
2067 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2068
2069 /* Note that we only support the standard ELF names, because .dwz
2070 is ELF-only (at the time of writing). */
2071 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2072 {
2073 dwz_file->abbrev.s.section = sectp;
2074 dwz_file->abbrev.size = bfd_section_size (sectp);
2075 }
2076 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2077 {
2078 dwz_file->info.s.section = sectp;
2079 dwz_file->info.size = bfd_section_size (sectp);
2080 }
2081 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2082 {
2083 dwz_file->str.s.section = sectp;
2084 dwz_file->str.size = bfd_section_size (sectp);
2085 }
2086 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2087 {
2088 dwz_file->line.s.section = sectp;
2089 dwz_file->line.size = bfd_section_size (sectp);
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2092 {
2093 dwz_file->macro.s.section = sectp;
2094 dwz_file->macro.size = bfd_section_size (sectp);
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2097 {
2098 dwz_file->gdb_index.s.section = sectp;
2099 dwz_file->gdb_index.size = bfd_section_size (sectp);
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2102 {
2103 dwz_file->debug_names.s.section = sectp;
2104 dwz_file->debug_names.size = bfd_section_size (sectp);
2105 }
2106 }
2107
2108 /* See dwarf2read.h. */
2109
2110 struct dwz_file *
2111 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2112 {
2113 const char *filename;
2114 bfd_size_type buildid_len_arg;
2115 size_t buildid_len;
2116 bfd_byte *buildid;
2117
2118 if (dwarf2_per_objfile->dwz_file != NULL)
2119 return dwarf2_per_objfile->dwz_file.get ();
2120
2121 bfd_set_error (bfd_error_no_error);
2122 gdb::unique_xmalloc_ptr<char> data
2123 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2124 &buildid_len_arg, &buildid));
2125 if (data == NULL)
2126 {
2127 if (bfd_get_error () == bfd_error_no_error)
2128 return NULL;
2129 error (_("could not read '.gnu_debugaltlink' section: %s"),
2130 bfd_errmsg (bfd_get_error ()));
2131 }
2132
2133 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2134
2135 buildid_len = (size_t) buildid_len_arg;
2136
2137 filename = data.get ();
2138
2139 std::string abs_storage;
2140 if (!IS_ABSOLUTE_PATH (filename))
2141 {
2142 gdb::unique_xmalloc_ptr<char> abs
2143 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2144
2145 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2146 filename = abs_storage.c_str ();
2147 }
2148
2149 /* First try the file name given in the section. If that doesn't
2150 work, try to use the build-id instead. */
2151 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2152 if (dwz_bfd != NULL)
2153 {
2154 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2155 dwz_bfd.reset (nullptr);
2156 }
2157
2158 if (dwz_bfd == NULL)
2159 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2160
2161 if (dwz_bfd == NULL)
2162 error (_("could not find '.gnu_debugaltlink' file for %s"),
2163 objfile_name (dwarf2_per_objfile->objfile));
2164
2165 std::unique_ptr<struct dwz_file> result
2166 (new struct dwz_file (std::move (dwz_bfd)));
2167
2168 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2169 result.get ());
2170
2171 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2172 result->dwz_bfd.get ());
2173 dwarf2_per_objfile->dwz_file = std::move (result);
2174 return dwarf2_per_objfile->dwz_file.get ();
2175 }
2176 \f
2177 /* DWARF quick_symbols_functions support. */
2178
2179 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2180 unique line tables, so we maintain a separate table of all .debug_line
2181 derived entries to support the sharing.
2182 All the quick functions need is the list of file names. We discard the
2183 line_header when we're done and don't need to record it here. */
2184 struct quick_file_names
2185 {
2186 /* The data used to construct the hash key. */
2187 struct stmt_list_hash hash;
2188
2189 /* The number of entries in file_names, real_names. */
2190 unsigned int num_file_names;
2191
2192 /* The file names from the line table, after being run through
2193 file_full_name. */
2194 const char **file_names;
2195
2196 /* The file names from the line table after being run through
2197 gdb_realpath. These are computed lazily. */
2198 const char **real_names;
2199 };
2200
2201 /* When using the index (and thus not using psymtabs), each CU has an
2202 object of this type. This is used to hold information needed by
2203 the various "quick" methods. */
2204 struct dwarf2_per_cu_quick_data
2205 {
2206 /* The file table. This can be NULL if there was no file table
2207 or it's currently not read in.
2208 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2209 struct quick_file_names *file_names;
2210
2211 /* The corresponding symbol table. This is NULL if symbols for this
2212 CU have not yet been read. */
2213 struct compunit_symtab *compunit_symtab;
2214
2215 /* A temporary mark bit used when iterating over all CUs in
2216 expand_symtabs_matching. */
2217 unsigned int mark : 1;
2218
2219 /* True if we've tried to read the file table and found there isn't one.
2220 There will be no point in trying to read it again next time. */
2221 unsigned int no_file_data : 1;
2222 };
2223
2224 /* Utility hash function for a stmt_list_hash. */
2225
2226 static hashval_t
2227 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2228 {
2229 hashval_t v = 0;
2230
2231 if (stmt_list_hash->dwo_unit != NULL)
2232 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2233 v += to_underlying (stmt_list_hash->line_sect_off);
2234 return v;
2235 }
2236
2237 /* Utility equality function for a stmt_list_hash. */
2238
2239 static int
2240 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2241 const struct stmt_list_hash *rhs)
2242 {
2243 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2244 return 0;
2245 if (lhs->dwo_unit != NULL
2246 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2247 return 0;
2248
2249 return lhs->line_sect_off == rhs->line_sect_off;
2250 }
2251
2252 /* Hash function for a quick_file_names. */
2253
2254 static hashval_t
2255 hash_file_name_entry (const void *e)
2256 {
2257 const struct quick_file_names *file_data
2258 = (const struct quick_file_names *) e;
2259
2260 return hash_stmt_list_entry (&file_data->hash);
2261 }
2262
2263 /* Equality function for a quick_file_names. */
2264
2265 static int
2266 eq_file_name_entry (const void *a, const void *b)
2267 {
2268 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2269 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2270
2271 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2272 }
2273
2274 /* Delete function for a quick_file_names. */
2275
2276 static void
2277 delete_file_name_entry (void *e)
2278 {
2279 struct quick_file_names *file_data = (struct quick_file_names *) e;
2280 int i;
2281
2282 for (i = 0; i < file_data->num_file_names; ++i)
2283 {
2284 xfree ((void*) file_data->file_names[i]);
2285 if (file_data->real_names)
2286 xfree ((void*) file_data->real_names[i]);
2287 }
2288
2289 /* The space for the struct itself lives on objfile_obstack,
2290 so we don't free it here. */
2291 }
2292
2293 /* Create a quick_file_names hash table. */
2294
2295 static htab_up
2296 create_quick_file_names_table (unsigned int nr_initial_entries)
2297 {
2298 return htab_up (htab_create_alloc (nr_initial_entries,
2299 hash_file_name_entry, eq_file_name_entry,
2300 delete_file_name_entry, xcalloc, xfree));
2301 }
2302
2303 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2304 have to be created afterwards. You should call age_cached_comp_units after
2305 processing PER_CU->CU. dw2_setup must have been already called. */
2306
2307 static void
2308 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2309 {
2310 if (per_cu->is_debug_types)
2311 load_full_type_unit (per_cu);
2312 else
2313 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2314
2315 if (per_cu->cu == NULL)
2316 return; /* Dummy CU. */
2317
2318 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2319 }
2320
2321 /* Read in the symbols for PER_CU. */
2322
2323 static void
2324 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2325 {
2326 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2327
2328 /* Skip type_unit_groups, reading the type units they contain
2329 is handled elsewhere. */
2330 if (IS_TYPE_UNIT_GROUP (per_cu))
2331 return;
2332
2333 /* The destructor of dwarf2_queue_guard frees any entries left on
2334 the queue. After this point we're guaranteed to leave this function
2335 with the dwarf queue empty. */
2336 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2337
2338 if (dwarf2_per_objfile->using_index
2339 ? per_cu->v.quick->compunit_symtab == NULL
2340 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2341 {
2342 queue_comp_unit (per_cu, language_minimal);
2343 load_cu (per_cu, skip_partial);
2344
2345 /* If we just loaded a CU from a DWO, and we're working with an index
2346 that may badly handle TUs, load all the TUs in that DWO as well.
2347 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2348 if (!per_cu->is_debug_types
2349 && per_cu->cu != NULL
2350 && per_cu->cu->dwo_unit != NULL
2351 && dwarf2_per_objfile->index_table != NULL
2352 && dwarf2_per_objfile->index_table->version <= 7
2353 /* DWP files aren't supported yet. */
2354 && get_dwp_file (dwarf2_per_objfile) == NULL)
2355 queue_and_load_all_dwo_tus (per_cu);
2356 }
2357
2358 process_queue (dwarf2_per_objfile);
2359
2360 /* Age the cache, releasing compilation units that have not
2361 been used recently. */
2362 age_cached_comp_units (dwarf2_per_objfile);
2363 }
2364
2365 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2366 the objfile from which this CU came. Returns the resulting symbol
2367 table. */
2368
2369 static struct compunit_symtab *
2370 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2371 {
2372 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2373
2374 gdb_assert (dwarf2_per_objfile->using_index);
2375 if (!per_cu->v.quick->compunit_symtab)
2376 {
2377 free_cached_comp_units freer (dwarf2_per_objfile);
2378 scoped_restore decrementer = increment_reading_symtab ();
2379 dw2_do_instantiate_symtab (per_cu, skip_partial);
2380 process_cu_includes (dwarf2_per_objfile);
2381 }
2382
2383 return per_cu->v.quick->compunit_symtab;
2384 }
2385
2386 /* See declaration. */
2387
2388 dwarf2_per_cu_data *
2389 dwarf2_per_objfile::get_cutu (int index)
2390 {
2391 if (index >= this->all_comp_units.size ())
2392 {
2393 index -= this->all_comp_units.size ();
2394 gdb_assert (index < this->all_type_units.size ());
2395 return &this->all_type_units[index]->per_cu;
2396 }
2397
2398 return this->all_comp_units[index];
2399 }
2400
2401 /* See declaration. */
2402
2403 dwarf2_per_cu_data *
2404 dwarf2_per_objfile::get_cu (int index)
2405 {
2406 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2407
2408 return this->all_comp_units[index];
2409 }
2410
2411 /* See declaration. */
2412
2413 signatured_type *
2414 dwarf2_per_objfile::get_tu (int index)
2415 {
2416 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2417
2418 return this->all_type_units[index];
2419 }
2420
2421 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2422 objfile_obstack, and constructed with the specified field
2423 values. */
2424
2425 static dwarf2_per_cu_data *
2426 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2427 struct dwarf2_section_info *section,
2428 int is_dwz,
2429 sect_offset sect_off, ULONGEST length)
2430 {
2431 struct objfile *objfile = dwarf2_per_objfile->objfile;
2432 dwarf2_per_cu_data *the_cu
2433 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2434 struct dwarf2_per_cu_data);
2435 the_cu->sect_off = sect_off;
2436 the_cu->length = length;
2437 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2438 the_cu->section = section;
2439 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2440 struct dwarf2_per_cu_quick_data);
2441 the_cu->is_dwz = is_dwz;
2442 return the_cu;
2443 }
2444
2445 /* A helper for create_cus_from_index that handles a given list of
2446 CUs. */
2447
2448 static void
2449 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2450 const gdb_byte *cu_list, offset_type n_elements,
2451 struct dwarf2_section_info *section,
2452 int is_dwz)
2453 {
2454 for (offset_type i = 0; i < n_elements; i += 2)
2455 {
2456 gdb_static_assert (sizeof (ULONGEST) >= 8);
2457
2458 sect_offset sect_off
2459 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2460 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2461 cu_list += 2 * 8;
2462
2463 dwarf2_per_cu_data *per_cu
2464 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2465 sect_off, length);
2466 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2467 }
2468 }
2469
2470 /* Read the CU list from the mapped index, and use it to create all
2471 the CU objects for this objfile. */
2472
2473 static void
2474 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2475 const gdb_byte *cu_list, offset_type cu_list_elements,
2476 const gdb_byte *dwz_list, offset_type dwz_elements)
2477 {
2478 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2479 dwarf2_per_objfile->all_comp_units.reserve
2480 ((cu_list_elements + dwz_elements) / 2);
2481
2482 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2483 &dwarf2_per_objfile->info, 0);
2484
2485 if (dwz_elements == 0)
2486 return;
2487
2488 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2489 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2490 &dwz->info, 1);
2491 }
2492
2493 /* Create the signatured type hash table from the index. */
2494
2495 static void
2496 create_signatured_type_table_from_index
2497 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2498 struct dwarf2_section_info *section,
2499 const gdb_byte *bytes,
2500 offset_type elements)
2501 {
2502 struct objfile *objfile = dwarf2_per_objfile->objfile;
2503
2504 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2505 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2506
2507 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2508
2509 for (offset_type i = 0; i < elements; i += 3)
2510 {
2511 struct signatured_type *sig_type;
2512 ULONGEST signature;
2513 void **slot;
2514 cu_offset type_offset_in_tu;
2515
2516 gdb_static_assert (sizeof (ULONGEST) >= 8);
2517 sect_offset sect_off
2518 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2519 type_offset_in_tu
2520 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2521 BFD_ENDIAN_LITTLE);
2522 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2523 bytes += 3 * 8;
2524
2525 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2526 struct signatured_type);
2527 sig_type->signature = signature;
2528 sig_type->type_offset_in_tu = type_offset_in_tu;
2529 sig_type->per_cu.is_debug_types = 1;
2530 sig_type->per_cu.section = section;
2531 sig_type->per_cu.sect_off = sect_off;
2532 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2533 sig_type->per_cu.v.quick
2534 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2535 struct dwarf2_per_cu_quick_data);
2536
2537 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2538 *slot = sig_type;
2539
2540 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2541 }
2542
2543 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2544 }
2545
2546 /* Create the signatured type hash table from .debug_names. */
2547
2548 static void
2549 create_signatured_type_table_from_debug_names
2550 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2551 const mapped_debug_names &map,
2552 struct dwarf2_section_info *section,
2553 struct dwarf2_section_info *abbrev_section)
2554 {
2555 struct objfile *objfile = dwarf2_per_objfile->objfile;
2556
2557 section->read (objfile);
2558 abbrev_section->read (objfile);
2559
2560 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2561 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2562
2563 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2564
2565 for (uint32_t i = 0; i < map.tu_count; ++i)
2566 {
2567 struct signatured_type *sig_type;
2568 void **slot;
2569
2570 sect_offset sect_off
2571 = (sect_offset) (extract_unsigned_integer
2572 (map.tu_table_reordered + i * map.offset_size,
2573 map.offset_size,
2574 map.dwarf5_byte_order));
2575
2576 comp_unit_head cu_header;
2577 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2578 abbrev_section,
2579 section->buffer + to_underlying (sect_off),
2580 rcuh_kind::TYPE);
2581
2582 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2583 struct signatured_type);
2584 sig_type->signature = cu_header.signature;
2585 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2586 sig_type->per_cu.is_debug_types = 1;
2587 sig_type->per_cu.section = section;
2588 sig_type->per_cu.sect_off = sect_off;
2589 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2590 sig_type->per_cu.v.quick
2591 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2592 struct dwarf2_per_cu_quick_data);
2593
2594 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2595 *slot = sig_type;
2596
2597 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2598 }
2599
2600 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2601 }
2602
2603 /* Read the address map data from the mapped index, and use it to
2604 populate the objfile's psymtabs_addrmap. */
2605
2606 static void
2607 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2608 struct mapped_index *index)
2609 {
2610 struct objfile *objfile = dwarf2_per_objfile->objfile;
2611 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2612 const gdb_byte *iter, *end;
2613 struct addrmap *mutable_map;
2614 CORE_ADDR baseaddr;
2615
2616 auto_obstack temp_obstack;
2617
2618 mutable_map = addrmap_create_mutable (&temp_obstack);
2619
2620 iter = index->address_table.data ();
2621 end = iter + index->address_table.size ();
2622
2623 baseaddr = objfile->text_section_offset ();
2624
2625 while (iter < end)
2626 {
2627 ULONGEST hi, lo, cu_index;
2628 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2629 iter += 8;
2630 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2631 iter += 8;
2632 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2633 iter += 4;
2634
2635 if (lo > hi)
2636 {
2637 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2638 hex_string (lo), hex_string (hi));
2639 continue;
2640 }
2641
2642 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2643 {
2644 complaint (_(".gdb_index address table has invalid CU number %u"),
2645 (unsigned) cu_index);
2646 continue;
2647 }
2648
2649 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2650 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2651 addrmap_set_empty (mutable_map, lo, hi - 1,
2652 dwarf2_per_objfile->get_cu (cu_index));
2653 }
2654
2655 objfile->partial_symtabs->psymtabs_addrmap
2656 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2657 }
2658
2659 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2660 populate the objfile's psymtabs_addrmap. */
2661
2662 static void
2663 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2664 struct dwarf2_section_info *section)
2665 {
2666 struct objfile *objfile = dwarf2_per_objfile->objfile;
2667 bfd *abfd = objfile->obfd;
2668 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2669 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2670
2671 auto_obstack temp_obstack;
2672 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2673
2674 std::unordered_map<sect_offset,
2675 dwarf2_per_cu_data *,
2676 gdb::hash_enum<sect_offset>>
2677 debug_info_offset_to_per_cu;
2678 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2679 {
2680 const auto insertpair
2681 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2682 if (!insertpair.second)
2683 {
2684 warning (_("Section .debug_aranges in %s has duplicate "
2685 "debug_info_offset %s, ignoring .debug_aranges."),
2686 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2687 return;
2688 }
2689 }
2690
2691 section->read (objfile);
2692
2693 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2694
2695 const gdb_byte *addr = section->buffer;
2696
2697 while (addr < section->buffer + section->size)
2698 {
2699 const gdb_byte *const entry_addr = addr;
2700 unsigned int bytes_read;
2701
2702 const LONGEST entry_length = read_initial_length (abfd, addr,
2703 &bytes_read);
2704 addr += bytes_read;
2705
2706 const gdb_byte *const entry_end = addr + entry_length;
2707 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2708 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2709 if (addr + entry_length > section->buffer + section->size)
2710 {
2711 warning (_("Section .debug_aranges in %s entry at offset %s "
2712 "length %s exceeds section length %s, "
2713 "ignoring .debug_aranges."),
2714 objfile_name (objfile),
2715 plongest (entry_addr - section->buffer),
2716 plongest (bytes_read + entry_length),
2717 pulongest (section->size));
2718 return;
2719 }
2720
2721 /* The version number. */
2722 const uint16_t version = read_2_bytes (abfd, addr);
2723 addr += 2;
2724 if (version != 2)
2725 {
2726 warning (_("Section .debug_aranges in %s entry at offset %s "
2727 "has unsupported version %d, ignoring .debug_aranges."),
2728 objfile_name (objfile),
2729 plongest (entry_addr - section->buffer), version);
2730 return;
2731 }
2732
2733 const uint64_t debug_info_offset
2734 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2735 addr += offset_size;
2736 const auto per_cu_it
2737 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2738 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2739 {
2740 warning (_("Section .debug_aranges in %s entry at offset %s "
2741 "debug_info_offset %s does not exists, "
2742 "ignoring .debug_aranges."),
2743 objfile_name (objfile),
2744 plongest (entry_addr - section->buffer),
2745 pulongest (debug_info_offset));
2746 return;
2747 }
2748 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2749
2750 const uint8_t address_size = *addr++;
2751 if (address_size < 1 || address_size > 8)
2752 {
2753 warning (_("Section .debug_aranges in %s entry at offset %s "
2754 "address_size %u is invalid, ignoring .debug_aranges."),
2755 objfile_name (objfile),
2756 plongest (entry_addr - section->buffer), address_size);
2757 return;
2758 }
2759
2760 const uint8_t segment_selector_size = *addr++;
2761 if (segment_selector_size != 0)
2762 {
2763 warning (_("Section .debug_aranges in %s entry at offset %s "
2764 "segment_selector_size %u is not supported, "
2765 "ignoring .debug_aranges."),
2766 objfile_name (objfile),
2767 plongest (entry_addr - section->buffer),
2768 segment_selector_size);
2769 return;
2770 }
2771
2772 /* Must pad to an alignment boundary that is twice the address
2773 size. It is undocumented by the DWARF standard but GCC does
2774 use it. */
2775 for (size_t padding = ((-(addr - section->buffer))
2776 & (2 * address_size - 1));
2777 padding > 0; padding--)
2778 if (*addr++ != 0)
2779 {
2780 warning (_("Section .debug_aranges in %s entry at offset %s "
2781 "padding is not zero, ignoring .debug_aranges."),
2782 objfile_name (objfile),
2783 plongest (entry_addr - section->buffer));
2784 return;
2785 }
2786
2787 for (;;)
2788 {
2789 if (addr + 2 * address_size > entry_end)
2790 {
2791 warning (_("Section .debug_aranges in %s entry at offset %s "
2792 "address list is not properly terminated, "
2793 "ignoring .debug_aranges."),
2794 objfile_name (objfile),
2795 plongest (entry_addr - section->buffer));
2796 return;
2797 }
2798 ULONGEST start = extract_unsigned_integer (addr, address_size,
2799 dwarf5_byte_order);
2800 addr += address_size;
2801 ULONGEST length = extract_unsigned_integer (addr, address_size,
2802 dwarf5_byte_order);
2803 addr += address_size;
2804 if (start == 0 && length == 0)
2805 break;
2806 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2807 {
2808 /* Symbol was eliminated due to a COMDAT group. */
2809 continue;
2810 }
2811 ULONGEST end = start + length;
2812 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2813 - baseaddr);
2814 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2815 - baseaddr);
2816 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2817 }
2818 }
2819
2820 objfile->partial_symtabs->psymtabs_addrmap
2821 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2822 }
2823
2824 /* Find a slot in the mapped index INDEX for the object named NAME.
2825 If NAME is found, set *VEC_OUT to point to the CU vector in the
2826 constant pool and return true. If NAME cannot be found, return
2827 false. */
2828
2829 static bool
2830 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2831 offset_type **vec_out)
2832 {
2833 offset_type hash;
2834 offset_type slot, step;
2835 int (*cmp) (const char *, const char *);
2836
2837 gdb::unique_xmalloc_ptr<char> without_params;
2838 if (current_language->la_language == language_cplus
2839 || current_language->la_language == language_fortran
2840 || current_language->la_language == language_d)
2841 {
2842 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2843 not contain any. */
2844
2845 if (strchr (name, '(') != NULL)
2846 {
2847 without_params = cp_remove_params (name);
2848
2849 if (without_params != NULL)
2850 name = without_params.get ();
2851 }
2852 }
2853
2854 /* Index version 4 did not support case insensitive searches. But the
2855 indices for case insensitive languages are built in lowercase, therefore
2856 simulate our NAME being searched is also lowercased. */
2857 hash = mapped_index_string_hash ((index->version == 4
2858 && case_sensitivity == case_sensitive_off
2859 ? 5 : index->version),
2860 name);
2861
2862 slot = hash & (index->symbol_table.size () - 1);
2863 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2864 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2865
2866 for (;;)
2867 {
2868 const char *str;
2869
2870 const auto &bucket = index->symbol_table[slot];
2871 if (bucket.name == 0 && bucket.vec == 0)
2872 return false;
2873
2874 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2875 if (!cmp (name, str))
2876 {
2877 *vec_out = (offset_type *) (index->constant_pool
2878 + MAYBE_SWAP (bucket.vec));
2879 return true;
2880 }
2881
2882 slot = (slot + step) & (index->symbol_table.size () - 1);
2883 }
2884 }
2885
2886 /* A helper function that reads the .gdb_index from BUFFER and fills
2887 in MAP. FILENAME is the name of the file containing the data;
2888 it is used for error reporting. DEPRECATED_OK is true if it is
2889 ok to use deprecated sections.
2890
2891 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2892 out parameters that are filled in with information about the CU and
2893 TU lists in the section.
2894
2895 Returns true if all went well, false otherwise. */
2896
2897 static bool
2898 read_gdb_index_from_buffer (struct objfile *objfile,
2899 const char *filename,
2900 bool deprecated_ok,
2901 gdb::array_view<const gdb_byte> buffer,
2902 struct mapped_index *map,
2903 const gdb_byte **cu_list,
2904 offset_type *cu_list_elements,
2905 const gdb_byte **types_list,
2906 offset_type *types_list_elements)
2907 {
2908 const gdb_byte *addr = &buffer[0];
2909
2910 /* Version check. */
2911 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2912 /* Versions earlier than 3 emitted every copy of a psymbol. This
2913 causes the index to behave very poorly for certain requests. Version 3
2914 contained incomplete addrmap. So, it seems better to just ignore such
2915 indices. */
2916 if (version < 4)
2917 {
2918 static int warning_printed = 0;
2919 if (!warning_printed)
2920 {
2921 warning (_("Skipping obsolete .gdb_index section in %s."),
2922 filename);
2923 warning_printed = 1;
2924 }
2925 return 0;
2926 }
2927 /* Index version 4 uses a different hash function than index version
2928 5 and later.
2929
2930 Versions earlier than 6 did not emit psymbols for inlined
2931 functions. Using these files will cause GDB not to be able to
2932 set breakpoints on inlined functions by name, so we ignore these
2933 indices unless the user has done
2934 "set use-deprecated-index-sections on". */
2935 if (version < 6 && !deprecated_ok)
2936 {
2937 static int warning_printed = 0;
2938 if (!warning_printed)
2939 {
2940 warning (_("\
2941 Skipping deprecated .gdb_index section in %s.\n\
2942 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2943 to use the section anyway."),
2944 filename);
2945 warning_printed = 1;
2946 }
2947 return 0;
2948 }
2949 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2950 of the TU (for symbols coming from TUs),
2951 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2952 Plus gold-generated indices can have duplicate entries for global symbols,
2953 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2954 These are just performance bugs, and we can't distinguish gdb-generated
2955 indices from gold-generated ones, so issue no warning here. */
2956
2957 /* Indexes with higher version than the one supported by GDB may be no
2958 longer backward compatible. */
2959 if (version > 8)
2960 return 0;
2961
2962 map->version = version;
2963
2964 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2965
2966 int i = 0;
2967 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2968 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2969 / 8);
2970 ++i;
2971
2972 *types_list = addr + MAYBE_SWAP (metadata[i]);
2973 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2974 - MAYBE_SWAP (metadata[i]))
2975 / 8);
2976 ++i;
2977
2978 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2979 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2980 map->address_table
2981 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2982 ++i;
2983
2984 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2985 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2986 map->symbol_table
2987 = gdb::array_view<mapped_index::symbol_table_slot>
2988 ((mapped_index::symbol_table_slot *) symbol_table,
2989 (mapped_index::symbol_table_slot *) symbol_table_end);
2990
2991 ++i;
2992 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2993
2994 return 1;
2995 }
2996
2997 /* Callback types for dwarf2_read_gdb_index. */
2998
2999 typedef gdb::function_view
3000 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3001 get_gdb_index_contents_ftype;
3002 typedef gdb::function_view
3003 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3004 get_gdb_index_contents_dwz_ftype;
3005
3006 /* Read .gdb_index. If everything went ok, initialize the "quick"
3007 elements of all the CUs and return 1. Otherwise, return 0. */
3008
3009 static int
3010 dwarf2_read_gdb_index
3011 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3012 get_gdb_index_contents_ftype get_gdb_index_contents,
3013 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3014 {
3015 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3016 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3017 struct dwz_file *dwz;
3018 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019
3020 gdb::array_view<const gdb_byte> main_index_contents
3021 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3022
3023 if (main_index_contents.empty ())
3024 return 0;
3025
3026 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3027 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3028 use_deprecated_index_sections,
3029 main_index_contents, map.get (), &cu_list,
3030 &cu_list_elements, &types_list,
3031 &types_list_elements))
3032 return 0;
3033
3034 /* Don't use the index if it's empty. */
3035 if (map->symbol_table.empty ())
3036 return 0;
3037
3038 /* If there is a .dwz file, read it so we can get its CU list as
3039 well. */
3040 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3041 if (dwz != NULL)
3042 {
3043 struct mapped_index dwz_map;
3044 const gdb_byte *dwz_types_ignore;
3045 offset_type dwz_types_elements_ignore;
3046
3047 gdb::array_view<const gdb_byte> dwz_index_content
3048 = get_gdb_index_contents_dwz (objfile, dwz);
3049
3050 if (dwz_index_content.empty ())
3051 return 0;
3052
3053 if (!read_gdb_index_from_buffer (objfile,
3054 bfd_get_filename (dwz->dwz_bfd.get ()),
3055 1, dwz_index_content, &dwz_map,
3056 &dwz_list, &dwz_list_elements,
3057 &dwz_types_ignore,
3058 &dwz_types_elements_ignore))
3059 {
3060 warning (_("could not read '.gdb_index' section from %s; skipping"),
3061 bfd_get_filename (dwz->dwz_bfd.get ()));
3062 return 0;
3063 }
3064 }
3065
3066 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3067 dwz_list, dwz_list_elements);
3068
3069 if (types_list_elements)
3070 {
3071 /* We can only handle a single .debug_types when we have an
3072 index. */
3073 if (dwarf2_per_objfile->types.size () != 1)
3074 return 0;
3075
3076 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3077
3078 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3079 types_list, types_list_elements);
3080 }
3081
3082 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3083
3084 dwarf2_per_objfile->index_table = std::move (map);
3085 dwarf2_per_objfile->using_index = 1;
3086 dwarf2_per_objfile->quick_file_names_table =
3087 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3088
3089 return 1;
3090 }
3091
3092 /* die_reader_func for dw2_get_file_names. */
3093
3094 static void
3095 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3096 const gdb_byte *info_ptr,
3097 struct die_info *comp_unit_die)
3098 {
3099 struct dwarf2_cu *cu = reader->cu;
3100 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3101 struct dwarf2_per_objfile *dwarf2_per_objfile
3102 = cu->per_cu->dwarf2_per_objfile;
3103 struct objfile *objfile = dwarf2_per_objfile->objfile;
3104 struct dwarf2_per_cu_data *lh_cu;
3105 struct attribute *attr;
3106 void **slot;
3107 struct quick_file_names *qfn;
3108
3109 gdb_assert (! this_cu->is_debug_types);
3110
3111 /* Our callers never want to match partial units -- instead they
3112 will match the enclosing full CU. */
3113 if (comp_unit_die->tag == DW_TAG_partial_unit)
3114 {
3115 this_cu->v.quick->no_file_data = 1;
3116 return;
3117 }
3118
3119 lh_cu = this_cu;
3120 slot = NULL;
3121
3122 line_header_up lh;
3123 sect_offset line_offset {};
3124
3125 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3126 if (attr != nullptr)
3127 {
3128 struct quick_file_names find_entry;
3129
3130 line_offset = (sect_offset) DW_UNSND (attr);
3131
3132 /* We may have already read in this line header (TU line header sharing).
3133 If we have we're done. */
3134 find_entry.hash.dwo_unit = cu->dwo_unit;
3135 find_entry.hash.line_sect_off = line_offset;
3136 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3137 &find_entry, INSERT);
3138 if (*slot != NULL)
3139 {
3140 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3141 return;
3142 }
3143
3144 lh = dwarf_decode_line_header (line_offset, cu);
3145 }
3146 if (lh == NULL)
3147 {
3148 lh_cu->v.quick->no_file_data = 1;
3149 return;
3150 }
3151
3152 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3153 qfn->hash.dwo_unit = cu->dwo_unit;
3154 qfn->hash.line_sect_off = line_offset;
3155 gdb_assert (slot != NULL);
3156 *slot = qfn;
3157
3158 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3159
3160 int offset = 0;
3161 if (strcmp (fnd.name, "<unknown>") != 0)
3162 ++offset;
3163
3164 qfn->num_file_names = offset + lh->file_names_size ();
3165 qfn->file_names =
3166 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3167 if (offset != 0)
3168 qfn->file_names[0] = xstrdup (fnd.name);
3169 for (int i = 0; i < lh->file_names_size (); ++i)
3170 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3171 fnd.comp_dir).release ();
3172 qfn->real_names = NULL;
3173
3174 lh_cu->v.quick->file_names = qfn;
3175 }
3176
3177 /* A helper for the "quick" functions which attempts to read the line
3178 table for THIS_CU. */
3179
3180 static struct quick_file_names *
3181 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3182 {
3183 /* This should never be called for TUs. */
3184 gdb_assert (! this_cu->is_debug_types);
3185 /* Nor type unit groups. */
3186 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3187
3188 if (this_cu->v.quick->file_names != NULL)
3189 return this_cu->v.quick->file_names;
3190 /* If we know there is no line data, no point in looking again. */
3191 if (this_cu->v.quick->no_file_data)
3192 return NULL;
3193
3194 cutu_reader reader (this_cu);
3195 if (!reader.dummy_p)
3196 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3197
3198 if (this_cu->v.quick->no_file_data)
3199 return NULL;
3200 return this_cu->v.quick->file_names;
3201 }
3202
3203 /* A helper for the "quick" functions which computes and caches the
3204 real path for a given file name from the line table. */
3205
3206 static const char *
3207 dw2_get_real_path (struct objfile *objfile,
3208 struct quick_file_names *qfn, int index)
3209 {
3210 if (qfn->real_names == NULL)
3211 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3212 qfn->num_file_names, const char *);
3213
3214 if (qfn->real_names[index] == NULL)
3215 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3216
3217 return qfn->real_names[index];
3218 }
3219
3220 static struct symtab *
3221 dw2_find_last_source_symtab (struct objfile *objfile)
3222 {
3223 struct dwarf2_per_objfile *dwarf2_per_objfile
3224 = get_dwarf2_per_objfile (objfile);
3225 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3226 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3227
3228 if (cust == NULL)
3229 return NULL;
3230
3231 return compunit_primary_filetab (cust);
3232 }
3233
3234 /* Traversal function for dw2_forget_cached_source_info. */
3235
3236 static int
3237 dw2_free_cached_file_names (void **slot, void *info)
3238 {
3239 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3240
3241 if (file_data->real_names)
3242 {
3243 int i;
3244
3245 for (i = 0; i < file_data->num_file_names; ++i)
3246 {
3247 xfree ((void*) file_data->real_names[i]);
3248 file_data->real_names[i] = NULL;
3249 }
3250 }
3251
3252 return 1;
3253 }
3254
3255 static void
3256 dw2_forget_cached_source_info (struct objfile *objfile)
3257 {
3258 struct dwarf2_per_objfile *dwarf2_per_objfile
3259 = get_dwarf2_per_objfile (objfile);
3260
3261 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3262 dw2_free_cached_file_names, NULL);
3263 }
3264
3265 /* Helper function for dw2_map_symtabs_matching_filename that expands
3266 the symtabs and calls the iterator. */
3267
3268 static int
3269 dw2_map_expand_apply (struct objfile *objfile,
3270 struct dwarf2_per_cu_data *per_cu,
3271 const char *name, const char *real_path,
3272 gdb::function_view<bool (symtab *)> callback)
3273 {
3274 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3275
3276 /* Don't visit already-expanded CUs. */
3277 if (per_cu->v.quick->compunit_symtab)
3278 return 0;
3279
3280 /* This may expand more than one symtab, and we want to iterate over
3281 all of them. */
3282 dw2_instantiate_symtab (per_cu, false);
3283
3284 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3285 last_made, callback);
3286 }
3287
3288 /* Implementation of the map_symtabs_matching_filename method. */
3289
3290 static bool
3291 dw2_map_symtabs_matching_filename
3292 (struct objfile *objfile, const char *name, const char *real_path,
3293 gdb::function_view<bool (symtab *)> callback)
3294 {
3295 const char *name_basename = lbasename (name);
3296 struct dwarf2_per_objfile *dwarf2_per_objfile
3297 = get_dwarf2_per_objfile (objfile);
3298
3299 /* The rule is CUs specify all the files, including those used by
3300 any TU, so there's no need to scan TUs here. */
3301
3302 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3303 {
3304 /* We only need to look at symtabs not already expanded. */
3305 if (per_cu->v.quick->compunit_symtab)
3306 continue;
3307
3308 quick_file_names *file_data = dw2_get_file_names (per_cu);
3309 if (file_data == NULL)
3310 continue;
3311
3312 for (int j = 0; j < file_data->num_file_names; ++j)
3313 {
3314 const char *this_name = file_data->file_names[j];
3315 const char *this_real_name;
3316
3317 if (compare_filenames_for_search (this_name, name))
3318 {
3319 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3320 callback))
3321 return true;
3322 continue;
3323 }
3324
3325 /* Before we invoke realpath, which can get expensive when many
3326 files are involved, do a quick comparison of the basenames. */
3327 if (! basenames_may_differ
3328 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3329 continue;
3330
3331 this_real_name = dw2_get_real_path (objfile, file_data, j);
3332 if (compare_filenames_for_search (this_real_name, name))
3333 {
3334 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3335 callback))
3336 return true;
3337 continue;
3338 }
3339
3340 if (real_path != NULL)
3341 {
3342 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3343 gdb_assert (IS_ABSOLUTE_PATH (name));
3344 if (this_real_name != NULL
3345 && FILENAME_CMP (real_path, this_real_name) == 0)
3346 {
3347 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3348 callback))
3349 return true;
3350 continue;
3351 }
3352 }
3353 }
3354 }
3355
3356 return false;
3357 }
3358
3359 /* Struct used to manage iterating over all CUs looking for a symbol. */
3360
3361 struct dw2_symtab_iterator
3362 {
3363 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3364 struct dwarf2_per_objfile *dwarf2_per_objfile;
3365 /* If set, only look for symbols that match that block. Valid values are
3366 GLOBAL_BLOCK and STATIC_BLOCK. */
3367 gdb::optional<block_enum> block_index;
3368 /* The kind of symbol we're looking for. */
3369 domain_enum domain;
3370 /* The list of CUs from the index entry of the symbol,
3371 or NULL if not found. */
3372 offset_type *vec;
3373 /* The next element in VEC to look at. */
3374 int next;
3375 /* The number of elements in VEC, or zero if there is no match. */
3376 int length;
3377 /* Have we seen a global version of the symbol?
3378 If so we can ignore all further global instances.
3379 This is to work around gold/15646, inefficient gold-generated
3380 indices. */
3381 int global_seen;
3382 };
3383
3384 /* Initialize the index symtab iterator ITER. */
3385
3386 static void
3387 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3388 struct dwarf2_per_objfile *dwarf2_per_objfile,
3389 gdb::optional<block_enum> block_index,
3390 domain_enum domain,
3391 const char *name)
3392 {
3393 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3394 iter->block_index = block_index;
3395 iter->domain = domain;
3396 iter->next = 0;
3397 iter->global_seen = 0;
3398
3399 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3400
3401 /* index is NULL if OBJF_READNOW. */
3402 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3403 iter->length = MAYBE_SWAP (*iter->vec);
3404 else
3405 {
3406 iter->vec = NULL;
3407 iter->length = 0;
3408 }
3409 }
3410
3411 /* Return the next matching CU or NULL if there are no more. */
3412
3413 static struct dwarf2_per_cu_data *
3414 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3415 {
3416 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3417
3418 for ( ; iter->next < iter->length; ++iter->next)
3419 {
3420 offset_type cu_index_and_attrs =
3421 MAYBE_SWAP (iter->vec[iter->next + 1]);
3422 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3423 gdb_index_symbol_kind symbol_kind =
3424 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3425 /* Only check the symbol attributes if they're present.
3426 Indices prior to version 7 don't record them,
3427 and indices >= 7 may elide them for certain symbols
3428 (gold does this). */
3429 int attrs_valid =
3430 (dwarf2_per_objfile->index_table->version >= 7
3431 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3432
3433 /* Don't crash on bad data. */
3434 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3435 + dwarf2_per_objfile->all_type_units.size ()))
3436 {
3437 complaint (_(".gdb_index entry has bad CU index"
3438 " [in module %s]"),
3439 objfile_name (dwarf2_per_objfile->objfile));
3440 continue;
3441 }
3442
3443 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3444
3445 /* Skip if already read in. */
3446 if (per_cu->v.quick->compunit_symtab)
3447 continue;
3448
3449 /* Check static vs global. */
3450 if (attrs_valid)
3451 {
3452 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3453
3454 if (iter->block_index.has_value ())
3455 {
3456 bool want_static = *iter->block_index == STATIC_BLOCK;
3457
3458 if (is_static != want_static)
3459 continue;
3460 }
3461
3462 /* Work around gold/15646. */
3463 if (!is_static && iter->global_seen)
3464 continue;
3465 if (!is_static)
3466 iter->global_seen = 1;
3467 }
3468
3469 /* Only check the symbol's kind if it has one. */
3470 if (attrs_valid)
3471 {
3472 switch (iter->domain)
3473 {
3474 case VAR_DOMAIN:
3475 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3476 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3477 /* Some types are also in VAR_DOMAIN. */
3478 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3479 continue;
3480 break;
3481 case STRUCT_DOMAIN:
3482 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3483 continue;
3484 break;
3485 case LABEL_DOMAIN:
3486 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3487 continue;
3488 break;
3489 case MODULE_DOMAIN:
3490 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3491 continue;
3492 break;
3493 default:
3494 break;
3495 }
3496 }
3497
3498 ++iter->next;
3499 return per_cu;
3500 }
3501
3502 return NULL;
3503 }
3504
3505 static struct compunit_symtab *
3506 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3507 const char *name, domain_enum domain)
3508 {
3509 struct compunit_symtab *stab_best = NULL;
3510 struct dwarf2_per_objfile *dwarf2_per_objfile
3511 = get_dwarf2_per_objfile (objfile);
3512
3513 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3514
3515 struct dw2_symtab_iterator iter;
3516 struct dwarf2_per_cu_data *per_cu;
3517
3518 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3519
3520 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3521 {
3522 struct symbol *sym, *with_opaque = NULL;
3523 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3524 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3525 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3526
3527 sym = block_find_symbol (block, name, domain,
3528 block_find_non_opaque_type_preferred,
3529 &with_opaque);
3530
3531 /* Some caution must be observed with overloaded functions
3532 and methods, since the index will not contain any overload
3533 information (but NAME might contain it). */
3534
3535 if (sym != NULL
3536 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3537 return stab;
3538 if (with_opaque != NULL
3539 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3540 stab_best = stab;
3541
3542 /* Keep looking through other CUs. */
3543 }
3544
3545 return stab_best;
3546 }
3547
3548 static void
3549 dw2_print_stats (struct objfile *objfile)
3550 {
3551 struct dwarf2_per_objfile *dwarf2_per_objfile
3552 = get_dwarf2_per_objfile (objfile);
3553 int total = (dwarf2_per_objfile->all_comp_units.size ()
3554 + dwarf2_per_objfile->all_type_units.size ());
3555 int count = 0;
3556
3557 for (int i = 0; i < total; ++i)
3558 {
3559 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3560
3561 if (!per_cu->v.quick->compunit_symtab)
3562 ++count;
3563 }
3564 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3565 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3566 }
3567
3568 /* This dumps minimal information about the index.
3569 It is called via "mt print objfiles".
3570 One use is to verify .gdb_index has been loaded by the
3571 gdb.dwarf2/gdb-index.exp testcase. */
3572
3573 static void
3574 dw2_dump (struct objfile *objfile)
3575 {
3576 struct dwarf2_per_objfile *dwarf2_per_objfile
3577 = get_dwarf2_per_objfile (objfile);
3578
3579 gdb_assert (dwarf2_per_objfile->using_index);
3580 printf_filtered (".gdb_index:");
3581 if (dwarf2_per_objfile->index_table != NULL)
3582 {
3583 printf_filtered (" version %d\n",
3584 dwarf2_per_objfile->index_table->version);
3585 }
3586 else
3587 printf_filtered (" faked for \"readnow\"\n");
3588 printf_filtered ("\n");
3589 }
3590
3591 static void
3592 dw2_expand_symtabs_for_function (struct objfile *objfile,
3593 const char *func_name)
3594 {
3595 struct dwarf2_per_objfile *dwarf2_per_objfile
3596 = get_dwarf2_per_objfile (objfile);
3597
3598 struct dw2_symtab_iterator iter;
3599 struct dwarf2_per_cu_data *per_cu;
3600
3601 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3602
3603 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3604 dw2_instantiate_symtab (per_cu, false);
3605
3606 }
3607
3608 static void
3609 dw2_expand_all_symtabs (struct objfile *objfile)
3610 {
3611 struct dwarf2_per_objfile *dwarf2_per_objfile
3612 = get_dwarf2_per_objfile (objfile);
3613 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3614 + dwarf2_per_objfile->all_type_units.size ());
3615
3616 for (int i = 0; i < total_units; ++i)
3617 {
3618 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3619
3620 /* We don't want to directly expand a partial CU, because if we
3621 read it with the wrong language, then assertion failures can
3622 be triggered later on. See PR symtab/23010. So, tell
3623 dw2_instantiate_symtab to skip partial CUs -- any important
3624 partial CU will be read via DW_TAG_imported_unit anyway. */
3625 dw2_instantiate_symtab (per_cu, true);
3626 }
3627 }
3628
3629 static void
3630 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3631 const char *fullname)
3632 {
3633 struct dwarf2_per_objfile *dwarf2_per_objfile
3634 = get_dwarf2_per_objfile (objfile);
3635
3636 /* We don't need to consider type units here.
3637 This is only called for examining code, e.g. expand_line_sal.
3638 There can be an order of magnitude (or more) more type units
3639 than comp units, and we avoid them if we can. */
3640
3641 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3642 {
3643 /* We only need to look at symtabs not already expanded. */
3644 if (per_cu->v.quick->compunit_symtab)
3645 continue;
3646
3647 quick_file_names *file_data = dw2_get_file_names (per_cu);
3648 if (file_data == NULL)
3649 continue;
3650
3651 for (int j = 0; j < file_data->num_file_names; ++j)
3652 {
3653 const char *this_fullname = file_data->file_names[j];
3654
3655 if (filename_cmp (this_fullname, fullname) == 0)
3656 {
3657 dw2_instantiate_symtab (per_cu, false);
3658 break;
3659 }
3660 }
3661 }
3662 }
3663
3664 static void
3665 dw2_map_matching_symbols
3666 (struct objfile *objfile,
3667 const lookup_name_info &name, domain_enum domain,
3668 int global,
3669 gdb::function_view<symbol_found_callback_ftype> callback,
3670 symbol_compare_ftype *ordered_compare)
3671 {
3672 /* Currently unimplemented; used for Ada. The function can be called if the
3673 current language is Ada for a non-Ada objfile using GNU index. As Ada
3674 does not look for non-Ada symbols this function should just return. */
3675 }
3676
3677 /* Starting from a search name, return the string that finds the upper
3678 bound of all strings that start with SEARCH_NAME in a sorted name
3679 list. Returns the empty string to indicate that the upper bound is
3680 the end of the list. */
3681
3682 static std::string
3683 make_sort_after_prefix_name (const char *search_name)
3684 {
3685 /* When looking to complete "func", we find the upper bound of all
3686 symbols that start with "func" by looking for where we'd insert
3687 the closest string that would follow "func" in lexicographical
3688 order. Usually, that's "func"-with-last-character-incremented,
3689 i.e. "fund". Mind non-ASCII characters, though. Usually those
3690 will be UTF-8 multi-byte sequences, but we can't be certain.
3691 Especially mind the 0xff character, which is a valid character in
3692 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3693 rule out compilers allowing it in identifiers. Note that
3694 conveniently, strcmp/strcasecmp are specified to compare
3695 characters interpreted as unsigned char. So what we do is treat
3696 the whole string as a base 256 number composed of a sequence of
3697 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3698 to 0, and carries 1 to the following more-significant position.
3699 If the very first character in SEARCH_NAME ends up incremented
3700 and carries/overflows, then the upper bound is the end of the
3701 list. The string after the empty string is also the empty
3702 string.
3703
3704 Some examples of this operation:
3705
3706 SEARCH_NAME => "+1" RESULT
3707
3708 "abc" => "abd"
3709 "ab\xff" => "ac"
3710 "\xff" "a" "\xff" => "\xff" "b"
3711 "\xff" => ""
3712 "\xff\xff" => ""
3713 "" => ""
3714
3715 Then, with these symbols for example:
3716
3717 func
3718 func1
3719 fund
3720
3721 completing "func" looks for symbols between "func" and
3722 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3723 which finds "func" and "func1", but not "fund".
3724
3725 And with:
3726
3727 funcÿ (Latin1 'ÿ' [0xff])
3728 funcÿ1
3729 fund
3730
3731 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3732 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3733
3734 And with:
3735
3736 ÿÿ (Latin1 'ÿ' [0xff])
3737 ÿÿ1
3738
3739 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3740 the end of the list.
3741 */
3742 std::string after = search_name;
3743 while (!after.empty () && (unsigned char) after.back () == 0xff)
3744 after.pop_back ();
3745 if (!after.empty ())
3746 after.back () = (unsigned char) after.back () + 1;
3747 return after;
3748 }
3749
3750 /* See declaration. */
3751
3752 std::pair<std::vector<name_component>::const_iterator,
3753 std::vector<name_component>::const_iterator>
3754 mapped_index_base::find_name_components_bounds
3755 (const lookup_name_info &lookup_name_without_params, language lang) const
3756 {
3757 auto *name_cmp
3758 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3759
3760 const char *lang_name
3761 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3762
3763 /* Comparison function object for lower_bound that matches against a
3764 given symbol name. */
3765 auto lookup_compare_lower = [&] (const name_component &elem,
3766 const char *name)
3767 {
3768 const char *elem_qualified = this->symbol_name_at (elem.idx);
3769 const char *elem_name = elem_qualified + elem.name_offset;
3770 return name_cmp (elem_name, name) < 0;
3771 };
3772
3773 /* Comparison function object for upper_bound that matches against a
3774 given symbol name. */
3775 auto lookup_compare_upper = [&] (const char *name,
3776 const name_component &elem)
3777 {
3778 const char *elem_qualified = this->symbol_name_at (elem.idx);
3779 const char *elem_name = elem_qualified + elem.name_offset;
3780 return name_cmp (name, elem_name) < 0;
3781 };
3782
3783 auto begin = this->name_components.begin ();
3784 auto end = this->name_components.end ();
3785
3786 /* Find the lower bound. */
3787 auto lower = [&] ()
3788 {
3789 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3790 return begin;
3791 else
3792 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3793 } ();
3794
3795 /* Find the upper bound. */
3796 auto upper = [&] ()
3797 {
3798 if (lookup_name_without_params.completion_mode ())
3799 {
3800 /* In completion mode, we want UPPER to point past all
3801 symbols names that have the same prefix. I.e., with
3802 these symbols, and completing "func":
3803
3804 function << lower bound
3805 function1
3806 other_function << upper bound
3807
3808 We find the upper bound by looking for the insertion
3809 point of "func"-with-last-character-incremented,
3810 i.e. "fund". */
3811 std::string after = make_sort_after_prefix_name (lang_name);
3812 if (after.empty ())
3813 return end;
3814 return std::lower_bound (lower, end, after.c_str (),
3815 lookup_compare_lower);
3816 }
3817 else
3818 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3819 } ();
3820
3821 return {lower, upper};
3822 }
3823
3824 /* See declaration. */
3825
3826 void
3827 mapped_index_base::build_name_components ()
3828 {
3829 if (!this->name_components.empty ())
3830 return;
3831
3832 this->name_components_casing = case_sensitivity;
3833 auto *name_cmp
3834 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3835
3836 /* The code below only knows how to break apart components of C++
3837 symbol names (and other languages that use '::' as
3838 namespace/module separator) and Ada symbol names. */
3839 auto count = this->symbol_name_count ();
3840 for (offset_type idx = 0; idx < count; idx++)
3841 {
3842 if (this->symbol_name_slot_invalid (idx))
3843 continue;
3844
3845 const char *name = this->symbol_name_at (idx);
3846
3847 /* Add each name component to the name component table. */
3848 unsigned int previous_len = 0;
3849
3850 if (strstr (name, "::") != nullptr)
3851 {
3852 for (unsigned int current_len = cp_find_first_component (name);
3853 name[current_len] != '\0';
3854 current_len += cp_find_first_component (name + current_len))
3855 {
3856 gdb_assert (name[current_len] == ':');
3857 this->name_components.push_back ({previous_len, idx});
3858 /* Skip the '::'. */
3859 current_len += 2;
3860 previous_len = current_len;
3861 }
3862 }
3863 else
3864 {
3865 /* Handle the Ada encoded (aka mangled) form here. */
3866 for (const char *iter = strstr (name, "__");
3867 iter != nullptr;
3868 iter = strstr (iter, "__"))
3869 {
3870 this->name_components.push_back ({previous_len, idx});
3871 iter += 2;
3872 previous_len = iter - name;
3873 }
3874 }
3875
3876 this->name_components.push_back ({previous_len, idx});
3877 }
3878
3879 /* Sort name_components elements by name. */
3880 auto name_comp_compare = [&] (const name_component &left,
3881 const name_component &right)
3882 {
3883 const char *left_qualified = this->symbol_name_at (left.idx);
3884 const char *right_qualified = this->symbol_name_at (right.idx);
3885
3886 const char *left_name = left_qualified + left.name_offset;
3887 const char *right_name = right_qualified + right.name_offset;
3888
3889 return name_cmp (left_name, right_name) < 0;
3890 };
3891
3892 std::sort (this->name_components.begin (),
3893 this->name_components.end (),
3894 name_comp_compare);
3895 }
3896
3897 /* Helper for dw2_expand_symtabs_matching that works with a
3898 mapped_index_base instead of the containing objfile. This is split
3899 to a separate function in order to be able to unit test the
3900 name_components matching using a mock mapped_index_base. For each
3901 symbol name that matches, calls MATCH_CALLBACK, passing it the
3902 symbol's index in the mapped_index_base symbol table. */
3903
3904 static void
3905 dw2_expand_symtabs_matching_symbol
3906 (mapped_index_base &index,
3907 const lookup_name_info &lookup_name_in,
3908 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3909 enum search_domain kind,
3910 gdb::function_view<bool (offset_type)> match_callback)
3911 {
3912 lookup_name_info lookup_name_without_params
3913 = lookup_name_in.make_ignore_params ();
3914
3915 /* Build the symbol name component sorted vector, if we haven't
3916 yet. */
3917 index.build_name_components ();
3918
3919 /* The same symbol may appear more than once in the range though.
3920 E.g., if we're looking for symbols that complete "w", and we have
3921 a symbol named "w1::w2", we'll find the two name components for
3922 that same symbol in the range. To be sure we only call the
3923 callback once per symbol, we first collect the symbol name
3924 indexes that matched in a temporary vector and ignore
3925 duplicates. */
3926 std::vector<offset_type> matches;
3927
3928 struct name_and_matcher
3929 {
3930 symbol_name_matcher_ftype *matcher;
3931 const std::string &name;
3932
3933 bool operator== (const name_and_matcher &other) const
3934 {
3935 return matcher == other.matcher && name == other.name;
3936 }
3937 };
3938
3939 /* A vector holding all the different symbol name matchers, for all
3940 languages. */
3941 std::vector<name_and_matcher> matchers;
3942
3943 for (int i = 0; i < nr_languages; i++)
3944 {
3945 enum language lang_e = (enum language) i;
3946
3947 const language_defn *lang = language_def (lang_e);
3948 symbol_name_matcher_ftype *name_matcher
3949 = get_symbol_name_matcher (lang, lookup_name_without_params);
3950
3951 name_and_matcher key {
3952 name_matcher,
3953 lookup_name_without_params.language_lookup_name (lang_e)
3954 };
3955
3956 /* Don't insert the same comparison routine more than once.
3957 Note that we do this linear walk. This is not a problem in
3958 practice because the number of supported languages is
3959 low. */
3960 if (std::find (matchers.begin (), matchers.end (), key)
3961 != matchers.end ())
3962 continue;
3963 matchers.push_back (std::move (key));
3964
3965 auto bounds
3966 = index.find_name_components_bounds (lookup_name_without_params,
3967 lang_e);
3968
3969 /* Now for each symbol name in range, check to see if we have a name
3970 match, and if so, call the MATCH_CALLBACK callback. */
3971
3972 for (; bounds.first != bounds.second; ++bounds.first)
3973 {
3974 const char *qualified = index.symbol_name_at (bounds.first->idx);
3975
3976 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3977 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3978 continue;
3979
3980 matches.push_back (bounds.first->idx);
3981 }
3982 }
3983
3984 std::sort (matches.begin (), matches.end ());
3985
3986 /* Finally call the callback, once per match. */
3987 ULONGEST prev = -1;
3988 for (offset_type idx : matches)
3989 {
3990 if (prev != idx)
3991 {
3992 if (!match_callback (idx))
3993 break;
3994 prev = idx;
3995 }
3996 }
3997
3998 /* Above we use a type wider than idx's for 'prev', since 0 and
3999 (offset_type)-1 are both possible values. */
4000 static_assert (sizeof (prev) > sizeof (offset_type), "");
4001 }
4002
4003 #if GDB_SELF_TEST
4004
4005 namespace selftests { namespace dw2_expand_symtabs_matching {
4006
4007 /* A mock .gdb_index/.debug_names-like name index table, enough to
4008 exercise dw2_expand_symtabs_matching_symbol, which works with the
4009 mapped_index_base interface. Builds an index from the symbol list
4010 passed as parameter to the constructor. */
4011 class mock_mapped_index : public mapped_index_base
4012 {
4013 public:
4014 mock_mapped_index (gdb::array_view<const char *> symbols)
4015 : m_symbol_table (symbols)
4016 {}
4017
4018 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4019
4020 /* Return the number of names in the symbol table. */
4021 size_t symbol_name_count () const override
4022 {
4023 return m_symbol_table.size ();
4024 }
4025
4026 /* Get the name of the symbol at IDX in the symbol table. */
4027 const char *symbol_name_at (offset_type idx) const override
4028 {
4029 return m_symbol_table[idx];
4030 }
4031
4032 private:
4033 gdb::array_view<const char *> m_symbol_table;
4034 };
4035
4036 /* Convenience function that converts a NULL pointer to a "<null>"
4037 string, to pass to print routines. */
4038
4039 static const char *
4040 string_or_null (const char *str)
4041 {
4042 return str != NULL ? str : "<null>";
4043 }
4044
4045 /* Check if a lookup_name_info built from
4046 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4047 index. EXPECTED_LIST is the list of expected matches, in expected
4048 matching order. If no match expected, then an empty list is
4049 specified. Returns true on success. On failure prints a warning
4050 indicating the file:line that failed, and returns false. */
4051
4052 static bool
4053 check_match (const char *file, int line,
4054 mock_mapped_index &mock_index,
4055 const char *name, symbol_name_match_type match_type,
4056 bool completion_mode,
4057 std::initializer_list<const char *> expected_list)
4058 {
4059 lookup_name_info lookup_name (name, match_type, completion_mode);
4060
4061 bool matched = true;
4062
4063 auto mismatch = [&] (const char *expected_str,
4064 const char *got)
4065 {
4066 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4067 "expected=\"%s\", got=\"%s\"\n"),
4068 file, line,
4069 (match_type == symbol_name_match_type::FULL
4070 ? "FULL" : "WILD"),
4071 name, string_or_null (expected_str), string_or_null (got));
4072 matched = false;
4073 };
4074
4075 auto expected_it = expected_list.begin ();
4076 auto expected_end = expected_list.end ();
4077
4078 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4079 NULL, ALL_DOMAIN,
4080 [&] (offset_type idx)
4081 {
4082 const char *matched_name = mock_index.symbol_name_at (idx);
4083 const char *expected_str
4084 = expected_it == expected_end ? NULL : *expected_it++;
4085
4086 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4087 mismatch (expected_str, matched_name);
4088 return true;
4089 });
4090
4091 const char *expected_str
4092 = expected_it == expected_end ? NULL : *expected_it++;
4093 if (expected_str != NULL)
4094 mismatch (expected_str, NULL);
4095
4096 return matched;
4097 }
4098
4099 /* The symbols added to the mock mapped_index for testing (in
4100 canonical form). */
4101 static const char *test_symbols[] = {
4102 "function",
4103 "std::bar",
4104 "std::zfunction",
4105 "std::zfunction2",
4106 "w1::w2",
4107 "ns::foo<char*>",
4108 "ns::foo<int>",
4109 "ns::foo<long>",
4110 "ns2::tmpl<int>::foo2",
4111 "(anonymous namespace)::A::B::C",
4112
4113 /* These are used to check that the increment-last-char in the
4114 matching algorithm for completion doesn't match "t1_fund" when
4115 completing "t1_func". */
4116 "t1_func",
4117 "t1_func1",
4118 "t1_fund",
4119 "t1_fund1",
4120
4121 /* A UTF-8 name with multi-byte sequences to make sure that
4122 cp-name-parser understands this as a single identifier ("função"
4123 is "function" in PT). */
4124 u8"u8função",
4125
4126 /* \377 (0xff) is Latin1 'ÿ'. */
4127 "yfunc\377",
4128
4129 /* \377 (0xff) is Latin1 'ÿ'. */
4130 "\377",
4131 "\377\377123",
4132
4133 /* A name with all sorts of complications. Starts with "z" to make
4134 it easier for the completion tests below. */
4135 #define Z_SYM_NAME \
4136 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4137 "::tuple<(anonymous namespace)::ui*, " \
4138 "std::default_delete<(anonymous namespace)::ui>, void>"
4139
4140 Z_SYM_NAME
4141 };
4142
4143 /* Returns true if the mapped_index_base::find_name_component_bounds
4144 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4145 in completion mode. */
4146
4147 static bool
4148 check_find_bounds_finds (mapped_index_base &index,
4149 const char *search_name,
4150 gdb::array_view<const char *> expected_syms)
4151 {
4152 lookup_name_info lookup_name (search_name,
4153 symbol_name_match_type::FULL, true);
4154
4155 auto bounds = index.find_name_components_bounds (lookup_name,
4156 language_cplus);
4157
4158 size_t distance = std::distance (bounds.first, bounds.second);
4159 if (distance != expected_syms.size ())
4160 return false;
4161
4162 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4163 {
4164 auto nc_elem = bounds.first + exp_elem;
4165 const char *qualified = index.symbol_name_at (nc_elem->idx);
4166 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4167 return false;
4168 }
4169
4170 return true;
4171 }
4172
4173 /* Test the lower-level mapped_index::find_name_component_bounds
4174 method. */
4175
4176 static void
4177 test_mapped_index_find_name_component_bounds ()
4178 {
4179 mock_mapped_index mock_index (test_symbols);
4180
4181 mock_index.build_name_components ();
4182
4183 /* Test the lower-level mapped_index::find_name_component_bounds
4184 method in completion mode. */
4185 {
4186 static const char *expected_syms[] = {
4187 "t1_func",
4188 "t1_func1",
4189 };
4190
4191 SELF_CHECK (check_find_bounds_finds (mock_index,
4192 "t1_func", expected_syms));
4193 }
4194
4195 /* Check that the increment-last-char in the name matching algorithm
4196 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4197 {
4198 static const char *expected_syms1[] = {
4199 "\377",
4200 "\377\377123",
4201 };
4202 SELF_CHECK (check_find_bounds_finds (mock_index,
4203 "\377", expected_syms1));
4204
4205 static const char *expected_syms2[] = {
4206 "\377\377123",
4207 };
4208 SELF_CHECK (check_find_bounds_finds (mock_index,
4209 "\377\377", expected_syms2));
4210 }
4211 }
4212
4213 /* Test dw2_expand_symtabs_matching_symbol. */
4214
4215 static void
4216 test_dw2_expand_symtabs_matching_symbol ()
4217 {
4218 mock_mapped_index mock_index (test_symbols);
4219
4220 /* We let all tests run until the end even if some fails, for debug
4221 convenience. */
4222 bool any_mismatch = false;
4223
4224 /* Create the expected symbols list (an initializer_list). Needed
4225 because lists have commas, and we need to pass them to CHECK,
4226 which is a macro. */
4227 #define EXPECT(...) { __VA_ARGS__ }
4228
4229 /* Wrapper for check_match that passes down the current
4230 __FILE__/__LINE__. */
4231 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4232 any_mismatch |= !check_match (__FILE__, __LINE__, \
4233 mock_index, \
4234 NAME, MATCH_TYPE, COMPLETION_MODE, \
4235 EXPECTED_LIST)
4236
4237 /* Identity checks. */
4238 for (const char *sym : test_symbols)
4239 {
4240 /* Should be able to match all existing symbols. */
4241 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4242 EXPECT (sym));
4243
4244 /* Should be able to match all existing symbols with
4245 parameters. */
4246 std::string with_params = std::string (sym) + "(int)";
4247 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4248 EXPECT (sym));
4249
4250 /* Should be able to match all existing symbols with
4251 parameters and qualifiers. */
4252 with_params = std::string (sym) + " ( int ) const";
4253 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4254 EXPECT (sym));
4255
4256 /* This should really find sym, but cp-name-parser.y doesn't
4257 know about lvalue/rvalue qualifiers yet. */
4258 with_params = std::string (sym) + " ( int ) &&";
4259 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4260 {});
4261 }
4262
4263 /* Check that the name matching algorithm for completion doesn't get
4264 confused with Latin1 'ÿ' / 0xff. */
4265 {
4266 static const char str[] = "\377";
4267 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4268 EXPECT ("\377", "\377\377123"));
4269 }
4270
4271 /* Check that the increment-last-char in the matching algorithm for
4272 completion doesn't match "t1_fund" when completing "t1_func". */
4273 {
4274 static const char str[] = "t1_func";
4275 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4276 EXPECT ("t1_func", "t1_func1"));
4277 }
4278
4279 /* Check that completion mode works at each prefix of the expected
4280 symbol name. */
4281 {
4282 static const char str[] = "function(int)";
4283 size_t len = strlen (str);
4284 std::string lookup;
4285
4286 for (size_t i = 1; i < len; i++)
4287 {
4288 lookup.assign (str, i);
4289 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4290 EXPECT ("function"));
4291 }
4292 }
4293
4294 /* While "w" is a prefix of both components, the match function
4295 should still only be called once. */
4296 {
4297 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4298 EXPECT ("w1::w2"));
4299 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4300 EXPECT ("w1::w2"));
4301 }
4302
4303 /* Same, with a "complicated" symbol. */
4304 {
4305 static const char str[] = Z_SYM_NAME;
4306 size_t len = strlen (str);
4307 std::string lookup;
4308
4309 for (size_t i = 1; i < len; i++)
4310 {
4311 lookup.assign (str, i);
4312 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4313 EXPECT (Z_SYM_NAME));
4314 }
4315 }
4316
4317 /* In FULL mode, an incomplete symbol doesn't match. */
4318 {
4319 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4320 {});
4321 }
4322
4323 /* A complete symbol with parameters matches any overload, since the
4324 index has no overload info. */
4325 {
4326 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4327 EXPECT ("std::zfunction", "std::zfunction2"));
4328 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4329 EXPECT ("std::zfunction", "std::zfunction2"));
4330 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4331 EXPECT ("std::zfunction", "std::zfunction2"));
4332 }
4333
4334 /* Check that whitespace is ignored appropriately. A symbol with a
4335 template argument list. */
4336 {
4337 static const char expected[] = "ns::foo<int>";
4338 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4339 EXPECT (expected));
4340 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4341 EXPECT (expected));
4342 }
4343
4344 /* Check that whitespace is ignored appropriately. A symbol with a
4345 template argument list that includes a pointer. */
4346 {
4347 static const char expected[] = "ns::foo<char*>";
4348 /* Try both completion and non-completion modes. */
4349 static const bool completion_mode[2] = {false, true};
4350 for (size_t i = 0; i < 2; i++)
4351 {
4352 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4353 completion_mode[i], EXPECT (expected));
4354 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4355 completion_mode[i], EXPECT (expected));
4356
4357 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4358 completion_mode[i], EXPECT (expected));
4359 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4360 completion_mode[i], EXPECT (expected));
4361 }
4362 }
4363
4364 {
4365 /* Check method qualifiers are ignored. */
4366 static const char expected[] = "ns::foo<char*>";
4367 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4368 symbol_name_match_type::FULL, true, EXPECT (expected));
4369 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4370 symbol_name_match_type::FULL, true, EXPECT (expected));
4371 CHECK_MATCH ("foo < char * > ( int ) const",
4372 symbol_name_match_type::WILD, true, EXPECT (expected));
4373 CHECK_MATCH ("foo < char * > ( int ) &&",
4374 symbol_name_match_type::WILD, true, EXPECT (expected));
4375 }
4376
4377 /* Test lookup names that don't match anything. */
4378 {
4379 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4380 {});
4381
4382 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4383 {});
4384 }
4385
4386 /* Some wild matching tests, exercising "(anonymous namespace)",
4387 which should not be confused with a parameter list. */
4388 {
4389 static const char *syms[] = {
4390 "A::B::C",
4391 "B::C",
4392 "C",
4393 "A :: B :: C ( int )",
4394 "B :: C ( int )",
4395 "C ( int )",
4396 };
4397
4398 for (const char *s : syms)
4399 {
4400 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4401 EXPECT ("(anonymous namespace)::A::B::C"));
4402 }
4403 }
4404
4405 {
4406 static const char expected[] = "ns2::tmpl<int>::foo2";
4407 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4408 EXPECT (expected));
4409 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4410 EXPECT (expected));
4411 }
4412
4413 SELF_CHECK (!any_mismatch);
4414
4415 #undef EXPECT
4416 #undef CHECK_MATCH
4417 }
4418
4419 static void
4420 run_test ()
4421 {
4422 test_mapped_index_find_name_component_bounds ();
4423 test_dw2_expand_symtabs_matching_symbol ();
4424 }
4425
4426 }} // namespace selftests::dw2_expand_symtabs_matching
4427
4428 #endif /* GDB_SELF_TEST */
4429
4430 /* If FILE_MATCHER is NULL or if PER_CU has
4431 dwarf2_per_cu_quick_data::MARK set (see
4432 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4433 EXPANSION_NOTIFY on it. */
4434
4435 static void
4436 dw2_expand_symtabs_matching_one
4437 (struct dwarf2_per_cu_data *per_cu,
4438 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4439 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4440 {
4441 if (file_matcher == NULL || per_cu->v.quick->mark)
4442 {
4443 bool symtab_was_null
4444 = (per_cu->v.quick->compunit_symtab == NULL);
4445
4446 dw2_instantiate_symtab (per_cu, false);
4447
4448 if (expansion_notify != NULL
4449 && symtab_was_null
4450 && per_cu->v.quick->compunit_symtab != NULL)
4451 expansion_notify (per_cu->v.quick->compunit_symtab);
4452 }
4453 }
4454
4455 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4456 matched, to expand corresponding CUs that were marked. IDX is the
4457 index of the symbol name that matched. */
4458
4459 static void
4460 dw2_expand_marked_cus
4461 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4462 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4463 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4464 search_domain kind)
4465 {
4466 offset_type *vec, vec_len, vec_idx;
4467 bool global_seen = false;
4468 mapped_index &index = *dwarf2_per_objfile->index_table;
4469
4470 vec = (offset_type *) (index.constant_pool
4471 + MAYBE_SWAP (index.symbol_table[idx].vec));
4472 vec_len = MAYBE_SWAP (vec[0]);
4473 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4474 {
4475 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4476 /* This value is only valid for index versions >= 7. */
4477 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4478 gdb_index_symbol_kind symbol_kind =
4479 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4480 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4481 /* Only check the symbol attributes if they're present.
4482 Indices prior to version 7 don't record them,
4483 and indices >= 7 may elide them for certain symbols
4484 (gold does this). */
4485 int attrs_valid =
4486 (index.version >= 7
4487 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4488
4489 /* Work around gold/15646. */
4490 if (attrs_valid)
4491 {
4492 if (!is_static && global_seen)
4493 continue;
4494 if (!is_static)
4495 global_seen = true;
4496 }
4497
4498 /* Only check the symbol's kind if it has one. */
4499 if (attrs_valid)
4500 {
4501 switch (kind)
4502 {
4503 case VARIABLES_DOMAIN:
4504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4505 continue;
4506 break;
4507 case FUNCTIONS_DOMAIN:
4508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4509 continue;
4510 break;
4511 case TYPES_DOMAIN:
4512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4513 continue;
4514 break;
4515 case MODULES_DOMAIN:
4516 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4517 continue;
4518 break;
4519 default:
4520 break;
4521 }
4522 }
4523
4524 /* Don't crash on bad data. */
4525 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4526 + dwarf2_per_objfile->all_type_units.size ()))
4527 {
4528 complaint (_(".gdb_index entry has bad CU index"
4529 " [in module %s]"),
4530 objfile_name (dwarf2_per_objfile->objfile));
4531 continue;
4532 }
4533
4534 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4535 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4536 expansion_notify);
4537 }
4538 }
4539
4540 /* If FILE_MATCHER is non-NULL, set all the
4541 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4542 that match FILE_MATCHER. */
4543
4544 static void
4545 dw_expand_symtabs_matching_file_matcher
4546 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4547 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4548 {
4549 if (file_matcher == NULL)
4550 return;
4551
4552 objfile *const objfile = dwarf2_per_objfile->objfile;
4553
4554 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4555 htab_eq_pointer,
4556 NULL, xcalloc, xfree));
4557 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4558 htab_eq_pointer,
4559 NULL, xcalloc, xfree));
4560
4561 /* The rule is CUs specify all the files, including those used by
4562 any TU, so there's no need to scan TUs here. */
4563
4564 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4565 {
4566 QUIT;
4567
4568 per_cu->v.quick->mark = 0;
4569
4570 /* We only need to look at symtabs not already expanded. */
4571 if (per_cu->v.quick->compunit_symtab)
4572 continue;
4573
4574 quick_file_names *file_data = dw2_get_file_names (per_cu);
4575 if (file_data == NULL)
4576 continue;
4577
4578 if (htab_find (visited_not_found.get (), file_data) != NULL)
4579 continue;
4580 else if (htab_find (visited_found.get (), file_data) != NULL)
4581 {
4582 per_cu->v.quick->mark = 1;
4583 continue;
4584 }
4585
4586 for (int j = 0; j < file_data->num_file_names; ++j)
4587 {
4588 const char *this_real_name;
4589
4590 if (file_matcher (file_data->file_names[j], false))
4591 {
4592 per_cu->v.quick->mark = 1;
4593 break;
4594 }
4595
4596 /* Before we invoke realpath, which can get expensive when many
4597 files are involved, do a quick comparison of the basenames. */
4598 if (!basenames_may_differ
4599 && !file_matcher (lbasename (file_data->file_names[j]),
4600 true))
4601 continue;
4602
4603 this_real_name = dw2_get_real_path (objfile, file_data, j);
4604 if (file_matcher (this_real_name, false))
4605 {
4606 per_cu->v.quick->mark = 1;
4607 break;
4608 }
4609 }
4610
4611 void **slot = htab_find_slot (per_cu->v.quick->mark
4612 ? visited_found.get ()
4613 : visited_not_found.get (),
4614 file_data, INSERT);
4615 *slot = file_data;
4616 }
4617 }
4618
4619 static void
4620 dw2_expand_symtabs_matching
4621 (struct objfile *objfile,
4622 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4623 const lookup_name_info &lookup_name,
4624 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4625 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4626 enum search_domain kind)
4627 {
4628 struct dwarf2_per_objfile *dwarf2_per_objfile
4629 = get_dwarf2_per_objfile (objfile);
4630
4631 /* index_table is NULL if OBJF_READNOW. */
4632 if (!dwarf2_per_objfile->index_table)
4633 return;
4634
4635 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4636
4637 mapped_index &index = *dwarf2_per_objfile->index_table;
4638
4639 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4640 symbol_matcher,
4641 kind, [&] (offset_type idx)
4642 {
4643 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4644 expansion_notify, kind);
4645 return true;
4646 });
4647 }
4648
4649 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4650 symtab. */
4651
4652 static struct compunit_symtab *
4653 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4654 CORE_ADDR pc)
4655 {
4656 int i;
4657
4658 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4659 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4660 return cust;
4661
4662 if (cust->includes == NULL)
4663 return NULL;
4664
4665 for (i = 0; cust->includes[i]; ++i)
4666 {
4667 struct compunit_symtab *s = cust->includes[i];
4668
4669 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4670 if (s != NULL)
4671 return s;
4672 }
4673
4674 return NULL;
4675 }
4676
4677 static struct compunit_symtab *
4678 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4679 struct bound_minimal_symbol msymbol,
4680 CORE_ADDR pc,
4681 struct obj_section *section,
4682 int warn_if_readin)
4683 {
4684 struct dwarf2_per_cu_data *data;
4685 struct compunit_symtab *result;
4686
4687 if (!objfile->partial_symtabs->psymtabs_addrmap)
4688 return NULL;
4689
4690 CORE_ADDR baseaddr = objfile->text_section_offset ();
4691 data = (struct dwarf2_per_cu_data *) addrmap_find
4692 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4693 if (!data)
4694 return NULL;
4695
4696 if (warn_if_readin && data->v.quick->compunit_symtab)
4697 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4698 paddress (get_objfile_arch (objfile), pc));
4699
4700 result
4701 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4702 false),
4703 pc);
4704 gdb_assert (result != NULL);
4705 return result;
4706 }
4707
4708 static void
4709 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4710 void *data, int need_fullname)
4711 {
4712 struct dwarf2_per_objfile *dwarf2_per_objfile
4713 = get_dwarf2_per_objfile (objfile);
4714
4715 if (!dwarf2_per_objfile->filenames_cache)
4716 {
4717 dwarf2_per_objfile->filenames_cache.emplace ();
4718
4719 htab_up visited (htab_create_alloc (10,
4720 htab_hash_pointer, htab_eq_pointer,
4721 NULL, xcalloc, xfree));
4722
4723 /* The rule is CUs specify all the files, including those used
4724 by any TU, so there's no need to scan TUs here. We can
4725 ignore file names coming from already-expanded CUs. */
4726
4727 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4728 {
4729 if (per_cu->v.quick->compunit_symtab)
4730 {
4731 void **slot = htab_find_slot (visited.get (),
4732 per_cu->v.quick->file_names,
4733 INSERT);
4734
4735 *slot = per_cu->v.quick->file_names;
4736 }
4737 }
4738
4739 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4740 {
4741 /* We only need to look at symtabs not already expanded. */
4742 if (per_cu->v.quick->compunit_symtab)
4743 continue;
4744
4745 quick_file_names *file_data = dw2_get_file_names (per_cu);
4746 if (file_data == NULL)
4747 continue;
4748
4749 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4750 if (*slot)
4751 {
4752 /* Already visited. */
4753 continue;
4754 }
4755 *slot = file_data;
4756
4757 for (int j = 0; j < file_data->num_file_names; ++j)
4758 {
4759 const char *filename = file_data->file_names[j];
4760 dwarf2_per_objfile->filenames_cache->seen (filename);
4761 }
4762 }
4763 }
4764
4765 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4766 {
4767 gdb::unique_xmalloc_ptr<char> this_real_name;
4768
4769 if (need_fullname)
4770 this_real_name = gdb_realpath (filename);
4771 (*fun) (filename, this_real_name.get (), data);
4772 });
4773 }
4774
4775 static int
4776 dw2_has_symbols (struct objfile *objfile)
4777 {
4778 return 1;
4779 }
4780
4781 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4782 {
4783 dw2_has_symbols,
4784 dw2_find_last_source_symtab,
4785 dw2_forget_cached_source_info,
4786 dw2_map_symtabs_matching_filename,
4787 dw2_lookup_symbol,
4788 dw2_print_stats,
4789 dw2_dump,
4790 dw2_expand_symtabs_for_function,
4791 dw2_expand_all_symtabs,
4792 dw2_expand_symtabs_with_fullname,
4793 dw2_map_matching_symbols,
4794 dw2_expand_symtabs_matching,
4795 dw2_find_pc_sect_compunit_symtab,
4796 NULL,
4797 dw2_map_symbol_filenames
4798 };
4799
4800 /* DWARF-5 debug_names reader. */
4801
4802 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4803 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4804
4805 /* A helper function that reads the .debug_names section in SECTION
4806 and fills in MAP. FILENAME is the name of the file containing the
4807 section; it is used for error reporting.
4808
4809 Returns true if all went well, false otherwise. */
4810
4811 static bool
4812 read_debug_names_from_section (struct objfile *objfile,
4813 const char *filename,
4814 struct dwarf2_section_info *section,
4815 mapped_debug_names &map)
4816 {
4817 if (section->empty ())
4818 return false;
4819
4820 /* Older elfutils strip versions could keep the section in the main
4821 executable while splitting it for the separate debug info file. */
4822 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4823 return false;
4824
4825 section->read (objfile);
4826
4827 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4828
4829 const gdb_byte *addr = section->buffer;
4830
4831 bfd *const abfd = section->get_bfd_owner ();
4832
4833 unsigned int bytes_read;
4834 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4835 addr += bytes_read;
4836
4837 map.dwarf5_is_dwarf64 = bytes_read != 4;
4838 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4839 if (bytes_read + length != section->size)
4840 {
4841 /* There may be multiple per-CU indices. */
4842 warning (_("Section .debug_names in %s length %s does not match "
4843 "section length %s, ignoring .debug_names."),
4844 filename, plongest (bytes_read + length),
4845 pulongest (section->size));
4846 return false;
4847 }
4848
4849 /* The version number. */
4850 uint16_t version = read_2_bytes (abfd, addr);
4851 addr += 2;
4852 if (version != 5)
4853 {
4854 warning (_("Section .debug_names in %s has unsupported version %d, "
4855 "ignoring .debug_names."),
4856 filename, version);
4857 return false;
4858 }
4859
4860 /* Padding. */
4861 uint16_t padding = read_2_bytes (abfd, addr);
4862 addr += 2;
4863 if (padding != 0)
4864 {
4865 warning (_("Section .debug_names in %s has unsupported padding %d, "
4866 "ignoring .debug_names."),
4867 filename, padding);
4868 return false;
4869 }
4870
4871 /* comp_unit_count - The number of CUs in the CU list. */
4872 map.cu_count = read_4_bytes (abfd, addr);
4873 addr += 4;
4874
4875 /* local_type_unit_count - The number of TUs in the local TU
4876 list. */
4877 map.tu_count = read_4_bytes (abfd, addr);
4878 addr += 4;
4879
4880 /* foreign_type_unit_count - The number of TUs in the foreign TU
4881 list. */
4882 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4883 addr += 4;
4884 if (foreign_tu_count != 0)
4885 {
4886 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4887 "ignoring .debug_names."),
4888 filename, static_cast<unsigned long> (foreign_tu_count));
4889 return false;
4890 }
4891
4892 /* bucket_count - The number of hash buckets in the hash lookup
4893 table. */
4894 map.bucket_count = read_4_bytes (abfd, addr);
4895 addr += 4;
4896
4897 /* name_count - The number of unique names in the index. */
4898 map.name_count = read_4_bytes (abfd, addr);
4899 addr += 4;
4900
4901 /* abbrev_table_size - The size in bytes of the abbreviations
4902 table. */
4903 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4904 addr += 4;
4905
4906 /* augmentation_string_size - The size in bytes of the augmentation
4907 string. This value is rounded up to a multiple of 4. */
4908 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4909 addr += 4;
4910 map.augmentation_is_gdb = ((augmentation_string_size
4911 == sizeof (dwarf5_augmentation))
4912 && memcmp (addr, dwarf5_augmentation,
4913 sizeof (dwarf5_augmentation)) == 0);
4914 augmentation_string_size += (-augmentation_string_size) & 3;
4915 addr += augmentation_string_size;
4916
4917 /* List of CUs */
4918 map.cu_table_reordered = addr;
4919 addr += map.cu_count * map.offset_size;
4920
4921 /* List of Local TUs */
4922 map.tu_table_reordered = addr;
4923 addr += map.tu_count * map.offset_size;
4924
4925 /* Hash Lookup Table */
4926 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4927 addr += map.bucket_count * 4;
4928 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4929 addr += map.name_count * 4;
4930
4931 /* Name Table */
4932 map.name_table_string_offs_reordered = addr;
4933 addr += map.name_count * map.offset_size;
4934 map.name_table_entry_offs_reordered = addr;
4935 addr += map.name_count * map.offset_size;
4936
4937 const gdb_byte *abbrev_table_start = addr;
4938 for (;;)
4939 {
4940 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4941 addr += bytes_read;
4942 if (index_num == 0)
4943 break;
4944
4945 const auto insertpair
4946 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4947 if (!insertpair.second)
4948 {
4949 warning (_("Section .debug_names in %s has duplicate index %s, "
4950 "ignoring .debug_names."),
4951 filename, pulongest (index_num));
4952 return false;
4953 }
4954 mapped_debug_names::index_val &indexval = insertpair.first->second;
4955 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4956 addr += bytes_read;
4957
4958 for (;;)
4959 {
4960 mapped_debug_names::index_val::attr attr;
4961 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4962 addr += bytes_read;
4963 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4964 addr += bytes_read;
4965 if (attr.form == DW_FORM_implicit_const)
4966 {
4967 attr.implicit_const = read_signed_leb128 (abfd, addr,
4968 &bytes_read);
4969 addr += bytes_read;
4970 }
4971 if (attr.dw_idx == 0 && attr.form == 0)
4972 break;
4973 indexval.attr_vec.push_back (std::move (attr));
4974 }
4975 }
4976 if (addr != abbrev_table_start + abbrev_table_size)
4977 {
4978 warning (_("Section .debug_names in %s has abbreviation_table "
4979 "of size %s vs. written as %u, ignoring .debug_names."),
4980 filename, plongest (addr - abbrev_table_start),
4981 abbrev_table_size);
4982 return false;
4983 }
4984 map.entry_pool = addr;
4985
4986 return true;
4987 }
4988
4989 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4990 list. */
4991
4992 static void
4993 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4994 const mapped_debug_names &map,
4995 dwarf2_section_info &section,
4996 bool is_dwz)
4997 {
4998 sect_offset sect_off_prev;
4999 for (uint32_t i = 0; i <= map.cu_count; ++i)
5000 {
5001 sect_offset sect_off_next;
5002 if (i < map.cu_count)
5003 {
5004 sect_off_next
5005 = (sect_offset) (extract_unsigned_integer
5006 (map.cu_table_reordered + i * map.offset_size,
5007 map.offset_size,
5008 map.dwarf5_byte_order));
5009 }
5010 else
5011 sect_off_next = (sect_offset) section.size;
5012 if (i >= 1)
5013 {
5014 const ULONGEST length = sect_off_next - sect_off_prev;
5015 dwarf2_per_cu_data *per_cu
5016 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5017 sect_off_prev, length);
5018 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5019 }
5020 sect_off_prev = sect_off_next;
5021 }
5022 }
5023
5024 /* Read the CU list from the mapped index, and use it to create all
5025 the CU objects for this dwarf2_per_objfile. */
5026
5027 static void
5028 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5029 const mapped_debug_names &map,
5030 const mapped_debug_names &dwz_map)
5031 {
5032 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5033 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5034
5035 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5036 dwarf2_per_objfile->info,
5037 false /* is_dwz */);
5038
5039 if (dwz_map.cu_count == 0)
5040 return;
5041
5042 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5043 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5044 true /* is_dwz */);
5045 }
5046
5047 /* Read .debug_names. If everything went ok, initialize the "quick"
5048 elements of all the CUs and return true. Otherwise, return false. */
5049
5050 static bool
5051 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5052 {
5053 std::unique_ptr<mapped_debug_names> map
5054 (new mapped_debug_names (dwarf2_per_objfile));
5055 mapped_debug_names dwz_map (dwarf2_per_objfile);
5056 struct objfile *objfile = dwarf2_per_objfile->objfile;
5057
5058 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5059 &dwarf2_per_objfile->debug_names,
5060 *map))
5061 return false;
5062
5063 /* Don't use the index if it's empty. */
5064 if (map->name_count == 0)
5065 return false;
5066
5067 /* If there is a .dwz file, read it so we can get its CU list as
5068 well. */
5069 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5070 if (dwz != NULL)
5071 {
5072 if (!read_debug_names_from_section (objfile,
5073 bfd_get_filename (dwz->dwz_bfd.get ()),
5074 &dwz->debug_names, dwz_map))
5075 {
5076 warning (_("could not read '.debug_names' section from %s; skipping"),
5077 bfd_get_filename (dwz->dwz_bfd.get ()));
5078 return false;
5079 }
5080 }
5081
5082 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5083
5084 if (map->tu_count != 0)
5085 {
5086 /* We can only handle a single .debug_types when we have an
5087 index. */
5088 if (dwarf2_per_objfile->types.size () != 1)
5089 return false;
5090
5091 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5092
5093 create_signatured_type_table_from_debug_names
5094 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5095 }
5096
5097 create_addrmap_from_aranges (dwarf2_per_objfile,
5098 &dwarf2_per_objfile->debug_aranges);
5099
5100 dwarf2_per_objfile->debug_names_table = std::move (map);
5101 dwarf2_per_objfile->using_index = 1;
5102 dwarf2_per_objfile->quick_file_names_table =
5103 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5104
5105 return true;
5106 }
5107
5108 /* Type used to manage iterating over all CUs looking for a symbol for
5109 .debug_names. */
5110
5111 class dw2_debug_names_iterator
5112 {
5113 public:
5114 dw2_debug_names_iterator (const mapped_debug_names &map,
5115 gdb::optional<block_enum> block_index,
5116 domain_enum domain,
5117 const char *name)
5118 : m_map (map), m_block_index (block_index), m_domain (domain),
5119 m_addr (find_vec_in_debug_names (map, name))
5120 {}
5121
5122 dw2_debug_names_iterator (const mapped_debug_names &map,
5123 search_domain search, uint32_t namei)
5124 : m_map (map),
5125 m_search (search),
5126 m_addr (find_vec_in_debug_names (map, namei))
5127 {}
5128
5129 dw2_debug_names_iterator (const mapped_debug_names &map,
5130 block_enum block_index, domain_enum domain,
5131 uint32_t namei)
5132 : m_map (map), m_block_index (block_index), m_domain (domain),
5133 m_addr (find_vec_in_debug_names (map, namei))
5134 {}
5135
5136 /* Return the next matching CU or NULL if there are no more. */
5137 dwarf2_per_cu_data *next ();
5138
5139 private:
5140 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5141 const char *name);
5142 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5143 uint32_t namei);
5144
5145 /* The internalized form of .debug_names. */
5146 const mapped_debug_names &m_map;
5147
5148 /* If set, only look for symbols that match that block. Valid values are
5149 GLOBAL_BLOCK and STATIC_BLOCK. */
5150 const gdb::optional<block_enum> m_block_index;
5151
5152 /* The kind of symbol we're looking for. */
5153 const domain_enum m_domain = UNDEF_DOMAIN;
5154 const search_domain m_search = ALL_DOMAIN;
5155
5156 /* The list of CUs from the index entry of the symbol, or NULL if
5157 not found. */
5158 const gdb_byte *m_addr;
5159 };
5160
5161 const char *
5162 mapped_debug_names::namei_to_name (uint32_t namei) const
5163 {
5164 const ULONGEST namei_string_offs
5165 = extract_unsigned_integer ((name_table_string_offs_reordered
5166 + namei * offset_size),
5167 offset_size,
5168 dwarf5_byte_order);
5169 return read_indirect_string_at_offset
5170 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5171 }
5172
5173 /* Find a slot in .debug_names for the object named NAME. If NAME is
5174 found, return pointer to its pool data. If NAME cannot be found,
5175 return NULL. */
5176
5177 const gdb_byte *
5178 dw2_debug_names_iterator::find_vec_in_debug_names
5179 (const mapped_debug_names &map, const char *name)
5180 {
5181 int (*cmp) (const char *, const char *);
5182
5183 gdb::unique_xmalloc_ptr<char> without_params;
5184 if (current_language->la_language == language_cplus
5185 || current_language->la_language == language_fortran
5186 || current_language->la_language == language_d)
5187 {
5188 /* NAME is already canonical. Drop any qualifiers as
5189 .debug_names does not contain any. */
5190
5191 if (strchr (name, '(') != NULL)
5192 {
5193 without_params = cp_remove_params (name);
5194 if (without_params != NULL)
5195 name = without_params.get ();
5196 }
5197 }
5198
5199 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5200
5201 const uint32_t full_hash = dwarf5_djb_hash (name);
5202 uint32_t namei
5203 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5204 (map.bucket_table_reordered
5205 + (full_hash % map.bucket_count)), 4,
5206 map.dwarf5_byte_order);
5207 if (namei == 0)
5208 return NULL;
5209 --namei;
5210 if (namei >= map.name_count)
5211 {
5212 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5213 "[in module %s]"),
5214 namei, map.name_count,
5215 objfile_name (map.dwarf2_per_objfile->objfile));
5216 return NULL;
5217 }
5218
5219 for (;;)
5220 {
5221 const uint32_t namei_full_hash
5222 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5223 (map.hash_table_reordered + namei), 4,
5224 map.dwarf5_byte_order);
5225 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5226 return NULL;
5227
5228 if (full_hash == namei_full_hash)
5229 {
5230 const char *const namei_string = map.namei_to_name (namei);
5231
5232 #if 0 /* An expensive sanity check. */
5233 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5234 {
5235 complaint (_("Wrong .debug_names hash for string at index %u "
5236 "[in module %s]"),
5237 namei, objfile_name (dwarf2_per_objfile->objfile));
5238 return NULL;
5239 }
5240 #endif
5241
5242 if (cmp (namei_string, name) == 0)
5243 {
5244 const ULONGEST namei_entry_offs
5245 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5246 + namei * map.offset_size),
5247 map.offset_size, map.dwarf5_byte_order);
5248 return map.entry_pool + namei_entry_offs;
5249 }
5250 }
5251
5252 ++namei;
5253 if (namei >= map.name_count)
5254 return NULL;
5255 }
5256 }
5257
5258 const gdb_byte *
5259 dw2_debug_names_iterator::find_vec_in_debug_names
5260 (const mapped_debug_names &map, uint32_t namei)
5261 {
5262 if (namei >= map.name_count)
5263 {
5264 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5265 "[in module %s]"),
5266 namei, map.name_count,
5267 objfile_name (map.dwarf2_per_objfile->objfile));
5268 return NULL;
5269 }
5270
5271 const ULONGEST namei_entry_offs
5272 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5273 + namei * map.offset_size),
5274 map.offset_size, map.dwarf5_byte_order);
5275 return map.entry_pool + namei_entry_offs;
5276 }
5277
5278 /* See dw2_debug_names_iterator. */
5279
5280 dwarf2_per_cu_data *
5281 dw2_debug_names_iterator::next ()
5282 {
5283 if (m_addr == NULL)
5284 return NULL;
5285
5286 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5287 struct objfile *objfile = dwarf2_per_objfile->objfile;
5288 bfd *const abfd = objfile->obfd;
5289
5290 again:
5291
5292 unsigned int bytes_read;
5293 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5294 m_addr += bytes_read;
5295 if (abbrev == 0)
5296 return NULL;
5297
5298 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5299 if (indexval_it == m_map.abbrev_map.cend ())
5300 {
5301 complaint (_("Wrong .debug_names undefined abbrev code %s "
5302 "[in module %s]"),
5303 pulongest (abbrev), objfile_name (objfile));
5304 return NULL;
5305 }
5306 const mapped_debug_names::index_val &indexval = indexval_it->second;
5307 enum class symbol_linkage {
5308 unknown,
5309 static_,
5310 extern_,
5311 } symbol_linkage_ = symbol_linkage::unknown;
5312 dwarf2_per_cu_data *per_cu = NULL;
5313 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5314 {
5315 ULONGEST ull;
5316 switch (attr.form)
5317 {
5318 case DW_FORM_implicit_const:
5319 ull = attr.implicit_const;
5320 break;
5321 case DW_FORM_flag_present:
5322 ull = 1;
5323 break;
5324 case DW_FORM_udata:
5325 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5326 m_addr += bytes_read;
5327 break;
5328 default:
5329 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5330 dwarf_form_name (attr.form),
5331 objfile_name (objfile));
5332 return NULL;
5333 }
5334 switch (attr.dw_idx)
5335 {
5336 case DW_IDX_compile_unit:
5337 /* Don't crash on bad data. */
5338 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5339 {
5340 complaint (_(".debug_names entry has bad CU index %s"
5341 " [in module %s]"),
5342 pulongest (ull),
5343 objfile_name (dwarf2_per_objfile->objfile));
5344 continue;
5345 }
5346 per_cu = dwarf2_per_objfile->get_cutu (ull);
5347 break;
5348 case DW_IDX_type_unit:
5349 /* Don't crash on bad data. */
5350 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5351 {
5352 complaint (_(".debug_names entry has bad TU index %s"
5353 " [in module %s]"),
5354 pulongest (ull),
5355 objfile_name (dwarf2_per_objfile->objfile));
5356 continue;
5357 }
5358 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5359 break;
5360 case DW_IDX_GNU_internal:
5361 if (!m_map.augmentation_is_gdb)
5362 break;
5363 symbol_linkage_ = symbol_linkage::static_;
5364 break;
5365 case DW_IDX_GNU_external:
5366 if (!m_map.augmentation_is_gdb)
5367 break;
5368 symbol_linkage_ = symbol_linkage::extern_;
5369 break;
5370 }
5371 }
5372
5373 /* Skip if already read in. */
5374 if (per_cu->v.quick->compunit_symtab)
5375 goto again;
5376
5377 /* Check static vs global. */
5378 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5379 {
5380 const bool want_static = *m_block_index == STATIC_BLOCK;
5381 const bool symbol_is_static =
5382 symbol_linkage_ == symbol_linkage::static_;
5383 if (want_static != symbol_is_static)
5384 goto again;
5385 }
5386
5387 /* Match dw2_symtab_iter_next, symbol_kind
5388 and debug_names::psymbol_tag. */
5389 switch (m_domain)
5390 {
5391 case VAR_DOMAIN:
5392 switch (indexval.dwarf_tag)
5393 {
5394 case DW_TAG_variable:
5395 case DW_TAG_subprogram:
5396 /* Some types are also in VAR_DOMAIN. */
5397 case DW_TAG_typedef:
5398 case DW_TAG_structure_type:
5399 break;
5400 default:
5401 goto again;
5402 }
5403 break;
5404 case STRUCT_DOMAIN:
5405 switch (indexval.dwarf_tag)
5406 {
5407 case DW_TAG_typedef:
5408 case DW_TAG_structure_type:
5409 break;
5410 default:
5411 goto again;
5412 }
5413 break;
5414 case LABEL_DOMAIN:
5415 switch (indexval.dwarf_tag)
5416 {
5417 case 0:
5418 case DW_TAG_variable:
5419 break;
5420 default:
5421 goto again;
5422 }
5423 break;
5424 case MODULE_DOMAIN:
5425 switch (indexval.dwarf_tag)
5426 {
5427 case DW_TAG_module:
5428 break;
5429 default:
5430 goto again;
5431 }
5432 break;
5433 default:
5434 break;
5435 }
5436
5437 /* Match dw2_expand_symtabs_matching, symbol_kind and
5438 debug_names::psymbol_tag. */
5439 switch (m_search)
5440 {
5441 case VARIABLES_DOMAIN:
5442 switch (indexval.dwarf_tag)
5443 {
5444 case DW_TAG_variable:
5445 break;
5446 default:
5447 goto again;
5448 }
5449 break;
5450 case FUNCTIONS_DOMAIN:
5451 switch (indexval.dwarf_tag)
5452 {
5453 case DW_TAG_subprogram:
5454 break;
5455 default:
5456 goto again;
5457 }
5458 break;
5459 case TYPES_DOMAIN:
5460 switch (indexval.dwarf_tag)
5461 {
5462 case DW_TAG_typedef:
5463 case DW_TAG_structure_type:
5464 break;
5465 default:
5466 goto again;
5467 }
5468 break;
5469 case MODULES_DOMAIN:
5470 switch (indexval.dwarf_tag)
5471 {
5472 case DW_TAG_module:
5473 break;
5474 default:
5475 goto again;
5476 }
5477 default:
5478 break;
5479 }
5480
5481 return per_cu;
5482 }
5483
5484 static struct compunit_symtab *
5485 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5486 const char *name, domain_enum domain)
5487 {
5488 struct dwarf2_per_objfile *dwarf2_per_objfile
5489 = get_dwarf2_per_objfile (objfile);
5490
5491 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5492 if (!mapp)
5493 {
5494 /* index is NULL if OBJF_READNOW. */
5495 return NULL;
5496 }
5497 const auto &map = *mapp;
5498
5499 dw2_debug_names_iterator iter (map, block_index, domain, name);
5500
5501 struct compunit_symtab *stab_best = NULL;
5502 struct dwarf2_per_cu_data *per_cu;
5503 while ((per_cu = iter.next ()) != NULL)
5504 {
5505 struct symbol *sym, *with_opaque = NULL;
5506 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5507 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5508 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5509
5510 sym = block_find_symbol (block, name, domain,
5511 block_find_non_opaque_type_preferred,
5512 &with_opaque);
5513
5514 /* Some caution must be observed with overloaded functions and
5515 methods, since the index will not contain any overload
5516 information (but NAME might contain it). */
5517
5518 if (sym != NULL
5519 && strcmp_iw (sym->search_name (), name) == 0)
5520 return stab;
5521 if (with_opaque != NULL
5522 && strcmp_iw (with_opaque->search_name (), name) == 0)
5523 stab_best = stab;
5524
5525 /* Keep looking through other CUs. */
5526 }
5527
5528 return stab_best;
5529 }
5530
5531 /* This dumps minimal information about .debug_names. It is called
5532 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5533 uses this to verify that .debug_names has been loaded. */
5534
5535 static void
5536 dw2_debug_names_dump (struct objfile *objfile)
5537 {
5538 struct dwarf2_per_objfile *dwarf2_per_objfile
5539 = get_dwarf2_per_objfile (objfile);
5540
5541 gdb_assert (dwarf2_per_objfile->using_index);
5542 printf_filtered (".debug_names:");
5543 if (dwarf2_per_objfile->debug_names_table)
5544 printf_filtered (" exists\n");
5545 else
5546 printf_filtered (" faked for \"readnow\"\n");
5547 printf_filtered ("\n");
5548 }
5549
5550 static void
5551 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5552 const char *func_name)
5553 {
5554 struct dwarf2_per_objfile *dwarf2_per_objfile
5555 = get_dwarf2_per_objfile (objfile);
5556
5557 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5558 if (dwarf2_per_objfile->debug_names_table)
5559 {
5560 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5561
5562 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5563
5564 struct dwarf2_per_cu_data *per_cu;
5565 while ((per_cu = iter.next ()) != NULL)
5566 dw2_instantiate_symtab (per_cu, false);
5567 }
5568 }
5569
5570 static void
5571 dw2_debug_names_map_matching_symbols
5572 (struct objfile *objfile,
5573 const lookup_name_info &name, domain_enum domain,
5574 int global,
5575 gdb::function_view<symbol_found_callback_ftype> callback,
5576 symbol_compare_ftype *ordered_compare)
5577 {
5578 struct dwarf2_per_objfile *dwarf2_per_objfile
5579 = get_dwarf2_per_objfile (objfile);
5580
5581 /* debug_names_table is NULL if OBJF_READNOW. */
5582 if (!dwarf2_per_objfile->debug_names_table)
5583 return;
5584
5585 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5586 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5587
5588 const char *match_name = name.ada ().lookup_name ().c_str ();
5589 auto matcher = [&] (const char *symname)
5590 {
5591 if (ordered_compare == nullptr)
5592 return true;
5593 return ordered_compare (symname, match_name) == 0;
5594 };
5595
5596 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5597 [&] (offset_type namei)
5598 {
5599 /* The name was matched, now expand corresponding CUs that were
5600 marked. */
5601 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5602
5603 struct dwarf2_per_cu_data *per_cu;
5604 while ((per_cu = iter.next ()) != NULL)
5605 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5606 return true;
5607 });
5608
5609 /* It's a shame we couldn't do this inside the
5610 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5611 that have already been expanded. Instead, this loop matches what
5612 the psymtab code does. */
5613 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5614 {
5615 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5616 if (cust != nullptr)
5617 {
5618 const struct block *block
5619 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5620 if (!iterate_over_symbols_terminated (block, name,
5621 domain, callback))
5622 break;
5623 }
5624 }
5625 }
5626
5627 static void
5628 dw2_debug_names_expand_symtabs_matching
5629 (struct objfile *objfile,
5630 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5631 const lookup_name_info &lookup_name,
5632 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5633 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5634 enum search_domain kind)
5635 {
5636 struct dwarf2_per_objfile *dwarf2_per_objfile
5637 = get_dwarf2_per_objfile (objfile);
5638
5639 /* debug_names_table is NULL if OBJF_READNOW. */
5640 if (!dwarf2_per_objfile->debug_names_table)
5641 return;
5642
5643 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5644
5645 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5646
5647 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5648 symbol_matcher,
5649 kind, [&] (offset_type namei)
5650 {
5651 /* The name was matched, now expand corresponding CUs that were
5652 marked. */
5653 dw2_debug_names_iterator iter (map, kind, namei);
5654
5655 struct dwarf2_per_cu_data *per_cu;
5656 while ((per_cu = iter.next ()) != NULL)
5657 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5658 expansion_notify);
5659 return true;
5660 });
5661 }
5662
5663 const struct quick_symbol_functions dwarf2_debug_names_functions =
5664 {
5665 dw2_has_symbols,
5666 dw2_find_last_source_symtab,
5667 dw2_forget_cached_source_info,
5668 dw2_map_symtabs_matching_filename,
5669 dw2_debug_names_lookup_symbol,
5670 dw2_print_stats,
5671 dw2_debug_names_dump,
5672 dw2_debug_names_expand_symtabs_for_function,
5673 dw2_expand_all_symtabs,
5674 dw2_expand_symtabs_with_fullname,
5675 dw2_debug_names_map_matching_symbols,
5676 dw2_debug_names_expand_symtabs_matching,
5677 dw2_find_pc_sect_compunit_symtab,
5678 NULL,
5679 dw2_map_symbol_filenames
5680 };
5681
5682 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5683 to either a dwarf2_per_objfile or dwz_file object. */
5684
5685 template <typename T>
5686 static gdb::array_view<const gdb_byte>
5687 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5688 {
5689 dwarf2_section_info *section = &section_owner->gdb_index;
5690
5691 if (section->empty ())
5692 return {};
5693
5694 /* Older elfutils strip versions could keep the section in the main
5695 executable while splitting it for the separate debug info file. */
5696 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5697 return {};
5698
5699 section->read (obj);
5700
5701 /* dwarf2_section_info::size is a bfd_size_type, while
5702 gdb::array_view works with size_t. On 32-bit hosts, with
5703 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5704 is 32-bit. So we need an explicit narrowing conversion here.
5705 This is fine, because it's impossible to allocate or mmap an
5706 array/buffer larger than what size_t can represent. */
5707 return gdb::make_array_view (section->buffer, section->size);
5708 }
5709
5710 /* Lookup the index cache for the contents of the index associated to
5711 DWARF2_OBJ. */
5712
5713 static gdb::array_view<const gdb_byte>
5714 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5715 {
5716 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5717 if (build_id == nullptr)
5718 return {};
5719
5720 return global_index_cache.lookup_gdb_index (build_id,
5721 &dwarf2_obj->index_cache_res);
5722 }
5723
5724 /* Same as the above, but for DWZ. */
5725
5726 static gdb::array_view<const gdb_byte>
5727 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5728 {
5729 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5730 if (build_id == nullptr)
5731 return {};
5732
5733 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5734 }
5735
5736 /* See symfile.h. */
5737
5738 bool
5739 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5740 {
5741 struct dwarf2_per_objfile *dwarf2_per_objfile
5742 = get_dwarf2_per_objfile (objfile);
5743
5744 /* If we're about to read full symbols, don't bother with the
5745 indices. In this case we also don't care if some other debug
5746 format is making psymtabs, because they are all about to be
5747 expanded anyway. */
5748 if ((objfile->flags & OBJF_READNOW))
5749 {
5750 dwarf2_per_objfile->using_index = 1;
5751 create_all_comp_units (dwarf2_per_objfile);
5752 create_all_type_units (dwarf2_per_objfile);
5753 dwarf2_per_objfile->quick_file_names_table
5754 = create_quick_file_names_table
5755 (dwarf2_per_objfile->all_comp_units.size ());
5756
5757 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5758 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5759 {
5760 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5761
5762 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5763 struct dwarf2_per_cu_quick_data);
5764 }
5765
5766 /* Return 1 so that gdb sees the "quick" functions. However,
5767 these functions will be no-ops because we will have expanded
5768 all symtabs. */
5769 *index_kind = dw_index_kind::GDB_INDEX;
5770 return true;
5771 }
5772
5773 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5774 {
5775 *index_kind = dw_index_kind::DEBUG_NAMES;
5776 return true;
5777 }
5778
5779 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5780 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5781 get_gdb_index_contents_from_section<dwz_file>))
5782 {
5783 *index_kind = dw_index_kind::GDB_INDEX;
5784 return true;
5785 }
5786
5787 /* ... otherwise, try to find the index in the index cache. */
5788 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5789 get_gdb_index_contents_from_cache,
5790 get_gdb_index_contents_from_cache_dwz))
5791 {
5792 global_index_cache.hit ();
5793 *index_kind = dw_index_kind::GDB_INDEX;
5794 return true;
5795 }
5796
5797 global_index_cache.miss ();
5798 return false;
5799 }
5800
5801 \f
5802
5803 /* Build a partial symbol table. */
5804
5805 void
5806 dwarf2_build_psymtabs (struct objfile *objfile)
5807 {
5808 struct dwarf2_per_objfile *dwarf2_per_objfile
5809 = get_dwarf2_per_objfile (objfile);
5810
5811 init_psymbol_list (objfile, 1024);
5812
5813 try
5814 {
5815 /* This isn't really ideal: all the data we allocate on the
5816 objfile's obstack is still uselessly kept around. However,
5817 freeing it seems unsafe. */
5818 psymtab_discarder psymtabs (objfile);
5819 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5820 psymtabs.keep ();
5821
5822 /* (maybe) store an index in the cache. */
5823 global_index_cache.store (dwarf2_per_objfile);
5824 }
5825 catch (const gdb_exception_error &except)
5826 {
5827 exception_print (gdb_stderr, except);
5828 }
5829 }
5830
5831 /* Find the base address of the compilation unit for range lists and
5832 location lists. It will normally be specified by DW_AT_low_pc.
5833 In DWARF-3 draft 4, the base address could be overridden by
5834 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5835 compilation units with discontinuous ranges. */
5836
5837 static void
5838 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5839 {
5840 struct attribute *attr;
5841
5842 cu->base_known = 0;
5843 cu->base_address = 0;
5844
5845 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5846 if (attr != nullptr)
5847 {
5848 cu->base_address = attr->value_as_address ();
5849 cu->base_known = 1;
5850 }
5851 else
5852 {
5853 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5854 if (attr != nullptr)
5855 {
5856 cu->base_address = attr->value_as_address ();
5857 cu->base_known = 1;
5858 }
5859 }
5860 }
5861
5862 /* Helper function that returns the proper abbrev section for
5863 THIS_CU. */
5864
5865 static struct dwarf2_section_info *
5866 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5867 {
5868 struct dwarf2_section_info *abbrev;
5869 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5870
5871 if (this_cu->is_dwz)
5872 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5873 else
5874 abbrev = &dwarf2_per_objfile->abbrev;
5875
5876 return abbrev;
5877 }
5878
5879 /* Fetch the abbreviation table offset from a comp or type unit header. */
5880
5881 static sect_offset
5882 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5883 struct dwarf2_section_info *section,
5884 sect_offset sect_off)
5885 {
5886 bfd *abfd = section->get_bfd_owner ();
5887 const gdb_byte *info_ptr;
5888 unsigned int initial_length_size, offset_size;
5889 uint16_t version;
5890
5891 section->read (dwarf2_per_objfile->objfile);
5892 info_ptr = section->buffer + to_underlying (sect_off);
5893 read_initial_length (abfd, info_ptr, &initial_length_size);
5894 offset_size = initial_length_size == 4 ? 4 : 8;
5895 info_ptr += initial_length_size;
5896
5897 version = read_2_bytes (abfd, info_ptr);
5898 info_ptr += 2;
5899 if (version >= 5)
5900 {
5901 /* Skip unit type and address size. */
5902 info_ptr += 2;
5903 }
5904
5905 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5906 }
5907
5908 /* Allocate a new partial symtab for file named NAME and mark this new
5909 partial symtab as being an include of PST. */
5910
5911 static void
5912 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5913 struct objfile *objfile)
5914 {
5915 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
5916
5917 if (!IS_ABSOLUTE_PATH (subpst->filename))
5918 {
5919 /* It shares objfile->objfile_obstack. */
5920 subpst->dirname = pst->dirname;
5921 }
5922
5923 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5924 subpst->dependencies[0] = pst;
5925 subpst->number_of_dependencies = 1;
5926
5927 /* No private part is necessary for include psymtabs. This property
5928 can be used to differentiate between such include psymtabs and
5929 the regular ones. */
5930 subpst->per_cu_data = nullptr;
5931 }
5932
5933 /* Read the Line Number Program data and extract the list of files
5934 included by the source file represented by PST. Build an include
5935 partial symtab for each of these included files. */
5936
5937 static void
5938 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5939 struct die_info *die,
5940 dwarf2_psymtab *pst)
5941 {
5942 line_header_up lh;
5943 struct attribute *attr;
5944
5945 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5946 if (attr != nullptr)
5947 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5948 if (lh == NULL)
5949 return; /* No linetable, so no includes. */
5950
5951 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5952 that we pass in the raw text_low here; that is ok because we're
5953 only decoding the line table to make include partial symtabs, and
5954 so the addresses aren't really used. */
5955 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5956 pst->raw_text_low (), 1);
5957 }
5958
5959 static hashval_t
5960 hash_signatured_type (const void *item)
5961 {
5962 const struct signatured_type *sig_type
5963 = (const struct signatured_type *) item;
5964
5965 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5966 return sig_type->signature;
5967 }
5968
5969 static int
5970 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5971 {
5972 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5973 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5974
5975 return lhs->signature == rhs->signature;
5976 }
5977
5978 /* Allocate a hash table for signatured types. */
5979
5980 static htab_up
5981 allocate_signatured_type_table (struct objfile *objfile)
5982 {
5983 return htab_up (htab_create_alloc (41,
5984 hash_signatured_type,
5985 eq_signatured_type,
5986 NULL, xcalloc, xfree));
5987 }
5988
5989 /* A helper function to add a signatured type CU to a table. */
5990
5991 static int
5992 add_signatured_type_cu_to_table (void **slot, void *datum)
5993 {
5994 struct signatured_type *sigt = (struct signatured_type *) *slot;
5995 std::vector<signatured_type *> *all_type_units
5996 = (std::vector<signatured_type *> *) datum;
5997
5998 all_type_units->push_back (sigt);
5999
6000 return 1;
6001 }
6002
6003 /* A helper for create_debug_types_hash_table. Read types from SECTION
6004 and fill them into TYPES_HTAB. It will process only type units,
6005 therefore DW_UT_type. */
6006
6007 static void
6008 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6009 struct dwo_file *dwo_file,
6010 dwarf2_section_info *section, htab_up &types_htab,
6011 rcuh_kind section_kind)
6012 {
6013 struct objfile *objfile = dwarf2_per_objfile->objfile;
6014 struct dwarf2_section_info *abbrev_section;
6015 bfd *abfd;
6016 const gdb_byte *info_ptr, *end_ptr;
6017
6018 abbrev_section = (dwo_file != NULL
6019 ? &dwo_file->sections.abbrev
6020 : &dwarf2_per_objfile->abbrev);
6021
6022 if (dwarf_read_debug)
6023 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6024 section->get_name (),
6025 abbrev_section->get_file_name ());
6026
6027 section->read (objfile);
6028 info_ptr = section->buffer;
6029
6030 if (info_ptr == NULL)
6031 return;
6032
6033 /* We can't set abfd until now because the section may be empty or
6034 not present, in which case the bfd is unknown. */
6035 abfd = section->get_bfd_owner ();
6036
6037 /* We don't use cutu_reader here because we don't need to read
6038 any dies: the signature is in the header. */
6039
6040 end_ptr = info_ptr + section->size;
6041 while (info_ptr < end_ptr)
6042 {
6043 struct signatured_type *sig_type;
6044 struct dwo_unit *dwo_tu;
6045 void **slot;
6046 const gdb_byte *ptr = info_ptr;
6047 struct comp_unit_head header;
6048 unsigned int length;
6049
6050 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6051
6052 /* Initialize it due to a false compiler warning. */
6053 header.signature = -1;
6054 header.type_cu_offset_in_tu = (cu_offset) -1;
6055
6056 /* We need to read the type's signature in order to build the hash
6057 table, but we don't need anything else just yet. */
6058
6059 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6060 abbrev_section, ptr, section_kind);
6061
6062 length = header.get_length ();
6063
6064 /* Skip dummy type units. */
6065 if (ptr >= info_ptr + length
6066 || peek_abbrev_code (abfd, ptr) == 0
6067 || header.unit_type != DW_UT_type)
6068 {
6069 info_ptr += length;
6070 continue;
6071 }
6072
6073 if (types_htab == NULL)
6074 {
6075 if (dwo_file)
6076 types_htab = allocate_dwo_unit_table (objfile);
6077 else
6078 types_htab = allocate_signatured_type_table (objfile);
6079 }
6080
6081 if (dwo_file)
6082 {
6083 sig_type = NULL;
6084 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6085 struct dwo_unit);
6086 dwo_tu->dwo_file = dwo_file;
6087 dwo_tu->signature = header.signature;
6088 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6089 dwo_tu->section = section;
6090 dwo_tu->sect_off = sect_off;
6091 dwo_tu->length = length;
6092 }
6093 else
6094 {
6095 /* N.B.: type_offset is not usable if this type uses a DWO file.
6096 The real type_offset is in the DWO file. */
6097 dwo_tu = NULL;
6098 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6099 struct signatured_type);
6100 sig_type->signature = header.signature;
6101 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6102 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6103 sig_type->per_cu.is_debug_types = 1;
6104 sig_type->per_cu.section = section;
6105 sig_type->per_cu.sect_off = sect_off;
6106 sig_type->per_cu.length = length;
6107 }
6108
6109 slot = htab_find_slot (types_htab.get (),
6110 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6111 INSERT);
6112 gdb_assert (slot != NULL);
6113 if (*slot != NULL)
6114 {
6115 sect_offset dup_sect_off;
6116
6117 if (dwo_file)
6118 {
6119 const struct dwo_unit *dup_tu
6120 = (const struct dwo_unit *) *slot;
6121
6122 dup_sect_off = dup_tu->sect_off;
6123 }
6124 else
6125 {
6126 const struct signatured_type *dup_tu
6127 = (const struct signatured_type *) *slot;
6128
6129 dup_sect_off = dup_tu->per_cu.sect_off;
6130 }
6131
6132 complaint (_("debug type entry at offset %s is duplicate to"
6133 " the entry at offset %s, signature %s"),
6134 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6135 hex_string (header.signature));
6136 }
6137 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6138
6139 if (dwarf_read_debug > 1)
6140 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6141 sect_offset_str (sect_off),
6142 hex_string (header.signature));
6143
6144 info_ptr += length;
6145 }
6146 }
6147
6148 /* Create the hash table of all entries in the .debug_types
6149 (or .debug_types.dwo) section(s).
6150 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6151 otherwise it is NULL.
6152
6153 The result is a pointer to the hash table or NULL if there are no types.
6154
6155 Note: This function processes DWO files only, not DWP files. */
6156
6157 static void
6158 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6159 struct dwo_file *dwo_file,
6160 gdb::array_view<dwarf2_section_info> type_sections,
6161 htab_up &types_htab)
6162 {
6163 for (dwarf2_section_info &section : type_sections)
6164 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6165 types_htab, rcuh_kind::TYPE);
6166 }
6167
6168 /* Create the hash table of all entries in the .debug_types section,
6169 and initialize all_type_units.
6170 The result is zero if there is an error (e.g. missing .debug_types section),
6171 otherwise non-zero. */
6172
6173 static int
6174 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6175 {
6176 htab_up types_htab;
6177
6178 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6179 &dwarf2_per_objfile->info, types_htab,
6180 rcuh_kind::COMPILE);
6181 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6182 dwarf2_per_objfile->types, types_htab);
6183 if (types_htab == NULL)
6184 {
6185 dwarf2_per_objfile->signatured_types = NULL;
6186 return 0;
6187 }
6188
6189 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6190
6191 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6192 dwarf2_per_objfile->all_type_units.reserve
6193 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6194
6195 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6196 add_signatured_type_cu_to_table,
6197 &dwarf2_per_objfile->all_type_units);
6198
6199 return 1;
6200 }
6201
6202 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6203 If SLOT is non-NULL, it is the entry to use in the hash table.
6204 Otherwise we find one. */
6205
6206 static struct signatured_type *
6207 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6208 void **slot)
6209 {
6210 struct objfile *objfile = dwarf2_per_objfile->objfile;
6211
6212 if (dwarf2_per_objfile->all_type_units.size ()
6213 == dwarf2_per_objfile->all_type_units.capacity ())
6214 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6215
6216 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6217 struct signatured_type);
6218
6219 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6220 sig_type->signature = sig;
6221 sig_type->per_cu.is_debug_types = 1;
6222 if (dwarf2_per_objfile->using_index)
6223 {
6224 sig_type->per_cu.v.quick =
6225 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6226 struct dwarf2_per_cu_quick_data);
6227 }
6228
6229 if (slot == NULL)
6230 {
6231 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6232 sig_type, INSERT);
6233 }
6234 gdb_assert (*slot == NULL);
6235 *slot = sig_type;
6236 /* The rest of sig_type must be filled in by the caller. */
6237 return sig_type;
6238 }
6239
6240 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6241 Fill in SIG_ENTRY with DWO_ENTRY. */
6242
6243 static void
6244 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6245 struct signatured_type *sig_entry,
6246 struct dwo_unit *dwo_entry)
6247 {
6248 /* Make sure we're not clobbering something we don't expect to. */
6249 gdb_assert (! sig_entry->per_cu.queued);
6250 gdb_assert (sig_entry->per_cu.cu == NULL);
6251 if (dwarf2_per_objfile->using_index)
6252 {
6253 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6254 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6255 }
6256 else
6257 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6258 gdb_assert (sig_entry->signature == dwo_entry->signature);
6259 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6260 gdb_assert (sig_entry->type_unit_group == NULL);
6261 gdb_assert (sig_entry->dwo_unit == NULL);
6262
6263 sig_entry->per_cu.section = dwo_entry->section;
6264 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6265 sig_entry->per_cu.length = dwo_entry->length;
6266 sig_entry->per_cu.reading_dwo_directly = 1;
6267 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6268 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6269 sig_entry->dwo_unit = dwo_entry;
6270 }
6271
6272 /* Subroutine of lookup_signatured_type.
6273 If we haven't read the TU yet, create the signatured_type data structure
6274 for a TU to be read in directly from a DWO file, bypassing the stub.
6275 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6276 using .gdb_index, then when reading a CU we want to stay in the DWO file
6277 containing that CU. Otherwise we could end up reading several other DWO
6278 files (due to comdat folding) to process the transitive closure of all the
6279 mentioned TUs, and that can be slow. The current DWO file will have every
6280 type signature that it needs.
6281 We only do this for .gdb_index because in the psymtab case we already have
6282 to read all the DWOs to build the type unit groups. */
6283
6284 static struct signatured_type *
6285 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6286 {
6287 struct dwarf2_per_objfile *dwarf2_per_objfile
6288 = cu->per_cu->dwarf2_per_objfile;
6289 struct objfile *objfile = dwarf2_per_objfile->objfile;
6290 struct dwo_file *dwo_file;
6291 struct dwo_unit find_dwo_entry, *dwo_entry;
6292 struct signatured_type find_sig_entry, *sig_entry;
6293 void **slot;
6294
6295 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6296
6297 /* If TU skeletons have been removed then we may not have read in any
6298 TUs yet. */
6299 if (dwarf2_per_objfile->signatured_types == NULL)
6300 {
6301 dwarf2_per_objfile->signatured_types
6302 = allocate_signatured_type_table (objfile);
6303 }
6304
6305 /* We only ever need to read in one copy of a signatured type.
6306 Use the global signatured_types array to do our own comdat-folding
6307 of types. If this is the first time we're reading this TU, and
6308 the TU has an entry in .gdb_index, replace the recorded data from
6309 .gdb_index with this TU. */
6310
6311 find_sig_entry.signature = sig;
6312 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6313 &find_sig_entry, INSERT);
6314 sig_entry = (struct signatured_type *) *slot;
6315
6316 /* We can get here with the TU already read, *or* in the process of being
6317 read. Don't reassign the global entry to point to this DWO if that's
6318 the case. Also note that if the TU is already being read, it may not
6319 have come from a DWO, the program may be a mix of Fission-compiled
6320 code and non-Fission-compiled code. */
6321
6322 /* Have we already tried to read this TU?
6323 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6324 needn't exist in the global table yet). */
6325 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6326 return sig_entry;
6327
6328 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6329 dwo_unit of the TU itself. */
6330 dwo_file = cu->dwo_unit->dwo_file;
6331
6332 /* Ok, this is the first time we're reading this TU. */
6333 if (dwo_file->tus == NULL)
6334 return NULL;
6335 find_dwo_entry.signature = sig;
6336 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6337 &find_dwo_entry);
6338 if (dwo_entry == NULL)
6339 return NULL;
6340
6341 /* If the global table doesn't have an entry for this TU, add one. */
6342 if (sig_entry == NULL)
6343 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6344
6345 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6346 sig_entry->per_cu.tu_read = 1;
6347 return sig_entry;
6348 }
6349
6350 /* Subroutine of lookup_signatured_type.
6351 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6352 then try the DWP file. If the TU stub (skeleton) has been removed then
6353 it won't be in .gdb_index. */
6354
6355 static struct signatured_type *
6356 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6357 {
6358 struct dwarf2_per_objfile *dwarf2_per_objfile
6359 = cu->per_cu->dwarf2_per_objfile;
6360 struct objfile *objfile = dwarf2_per_objfile->objfile;
6361 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6362 struct dwo_unit *dwo_entry;
6363 struct signatured_type find_sig_entry, *sig_entry;
6364 void **slot;
6365
6366 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6367 gdb_assert (dwp_file != NULL);
6368
6369 /* If TU skeletons have been removed then we may not have read in any
6370 TUs yet. */
6371 if (dwarf2_per_objfile->signatured_types == NULL)
6372 {
6373 dwarf2_per_objfile->signatured_types
6374 = allocate_signatured_type_table (objfile);
6375 }
6376
6377 find_sig_entry.signature = sig;
6378 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6379 &find_sig_entry, INSERT);
6380 sig_entry = (struct signatured_type *) *slot;
6381
6382 /* Have we already tried to read this TU?
6383 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6384 needn't exist in the global table yet). */
6385 if (sig_entry != NULL)
6386 return sig_entry;
6387
6388 if (dwp_file->tus == NULL)
6389 return NULL;
6390 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6391 sig, 1 /* is_debug_types */);
6392 if (dwo_entry == NULL)
6393 return NULL;
6394
6395 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6396 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6397
6398 return sig_entry;
6399 }
6400
6401 /* Lookup a signature based type for DW_FORM_ref_sig8.
6402 Returns NULL if signature SIG is not present in the table.
6403 It is up to the caller to complain about this. */
6404
6405 static struct signatured_type *
6406 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6407 {
6408 struct dwarf2_per_objfile *dwarf2_per_objfile
6409 = cu->per_cu->dwarf2_per_objfile;
6410
6411 if (cu->dwo_unit
6412 && dwarf2_per_objfile->using_index)
6413 {
6414 /* We're in a DWO/DWP file, and we're using .gdb_index.
6415 These cases require special processing. */
6416 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6417 return lookup_dwo_signatured_type (cu, sig);
6418 else
6419 return lookup_dwp_signatured_type (cu, sig);
6420 }
6421 else
6422 {
6423 struct signatured_type find_entry, *entry;
6424
6425 if (dwarf2_per_objfile->signatured_types == NULL)
6426 return NULL;
6427 find_entry.signature = sig;
6428 entry = ((struct signatured_type *)
6429 htab_find (dwarf2_per_objfile->signatured_types.get (),
6430 &find_entry));
6431 return entry;
6432 }
6433 }
6434
6435 /* Return the address base of the compile unit, which, if exists, is stored
6436 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6437 static gdb::optional<ULONGEST>
6438 lookup_addr_base (struct die_info *comp_unit_die)
6439 {
6440 struct attribute *attr;
6441 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6442 if (attr == nullptr)
6443 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6444 if (attr == nullptr)
6445 return gdb::optional<ULONGEST> ();
6446 return DW_UNSND (attr);
6447 }
6448
6449 /* Return range lists base of the compile unit, which, if exists, is stored
6450 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6451 static ULONGEST
6452 lookup_ranges_base (struct die_info *comp_unit_die)
6453 {
6454 struct attribute *attr;
6455 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6456 if (attr == nullptr)
6457 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6458 if (attr == nullptr)
6459 return 0;
6460 return DW_UNSND (attr);
6461 }
6462
6463 /* Low level DIE reading support. */
6464
6465 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6466
6467 static void
6468 init_cu_die_reader (struct die_reader_specs *reader,
6469 struct dwarf2_cu *cu,
6470 struct dwarf2_section_info *section,
6471 struct dwo_file *dwo_file,
6472 struct abbrev_table *abbrev_table)
6473 {
6474 gdb_assert (section->readin && section->buffer != NULL);
6475 reader->abfd = section->get_bfd_owner ();
6476 reader->cu = cu;
6477 reader->dwo_file = dwo_file;
6478 reader->die_section = section;
6479 reader->buffer = section->buffer;
6480 reader->buffer_end = section->buffer + section->size;
6481 reader->abbrev_table = abbrev_table;
6482 }
6483
6484 /* Subroutine of cutu_reader to simplify it.
6485 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6486 There's just a lot of work to do, and cutu_reader is big enough
6487 already.
6488
6489 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6490 from it to the DIE in the DWO. If NULL we are skipping the stub.
6491 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6492 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6493 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6494 STUB_COMP_DIR may be non-NULL.
6495 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6496 are filled in with the info of the DIE from the DWO file.
6497 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6498 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6499 kept around for at least as long as *RESULT_READER.
6500
6501 The result is non-zero if a valid (non-dummy) DIE was found. */
6502
6503 static int
6504 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6505 struct dwo_unit *dwo_unit,
6506 struct die_info *stub_comp_unit_die,
6507 const char *stub_comp_dir,
6508 struct die_reader_specs *result_reader,
6509 const gdb_byte **result_info_ptr,
6510 struct die_info **result_comp_unit_die,
6511 abbrev_table_up *result_dwo_abbrev_table)
6512 {
6513 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6514 struct objfile *objfile = dwarf2_per_objfile->objfile;
6515 struct dwarf2_cu *cu = this_cu->cu;
6516 bfd *abfd;
6517 const gdb_byte *begin_info_ptr, *info_ptr;
6518 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6519 int i,num_extra_attrs;
6520 struct dwarf2_section_info *dwo_abbrev_section;
6521 struct die_info *comp_unit_die;
6522
6523 /* At most one of these may be provided. */
6524 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6525
6526 /* These attributes aren't processed until later:
6527 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6528 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6529 referenced later. However, these attributes are found in the stub
6530 which we won't have later. In order to not impose this complication
6531 on the rest of the code, we read them here and copy them to the
6532 DWO CU/TU die. */
6533
6534 stmt_list = NULL;
6535 low_pc = NULL;
6536 high_pc = NULL;
6537 ranges = NULL;
6538 comp_dir = NULL;
6539
6540 if (stub_comp_unit_die != NULL)
6541 {
6542 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6543 DWO file. */
6544 if (! this_cu->is_debug_types)
6545 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6546 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6547 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6548 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6549 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6550
6551 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6552
6553 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6554 here (if needed). We need the value before we can process
6555 DW_AT_ranges. */
6556 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6557 }
6558 else if (stub_comp_dir != NULL)
6559 {
6560 /* Reconstruct the comp_dir attribute to simplify the code below. */
6561 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6562 comp_dir->name = DW_AT_comp_dir;
6563 comp_dir->form = DW_FORM_string;
6564 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6565 DW_STRING (comp_dir) = stub_comp_dir;
6566 }
6567
6568 /* Set up for reading the DWO CU/TU. */
6569 cu->dwo_unit = dwo_unit;
6570 dwarf2_section_info *section = dwo_unit->section;
6571 section->read (objfile);
6572 abfd = section->get_bfd_owner ();
6573 begin_info_ptr = info_ptr = (section->buffer
6574 + to_underlying (dwo_unit->sect_off));
6575 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6576
6577 if (this_cu->is_debug_types)
6578 {
6579 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6580
6581 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6582 &cu->header, section,
6583 dwo_abbrev_section,
6584 info_ptr, rcuh_kind::TYPE);
6585 /* This is not an assert because it can be caused by bad debug info. */
6586 if (sig_type->signature != cu->header.signature)
6587 {
6588 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6589 " TU at offset %s [in module %s]"),
6590 hex_string (sig_type->signature),
6591 hex_string (cu->header.signature),
6592 sect_offset_str (dwo_unit->sect_off),
6593 bfd_get_filename (abfd));
6594 }
6595 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6596 /* For DWOs coming from DWP files, we don't know the CU length
6597 nor the type's offset in the TU until now. */
6598 dwo_unit->length = cu->header.get_length ();
6599 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6600
6601 /* Establish the type offset that can be used to lookup the type.
6602 For DWO files, we don't know it until now. */
6603 sig_type->type_offset_in_section
6604 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6605 }
6606 else
6607 {
6608 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6609 &cu->header, section,
6610 dwo_abbrev_section,
6611 info_ptr, rcuh_kind::COMPILE);
6612 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6613 /* For DWOs coming from DWP files, we don't know the CU length
6614 until now. */
6615 dwo_unit->length = cu->header.get_length ();
6616 }
6617
6618 *result_dwo_abbrev_table
6619 = abbrev_table::read (objfile, dwo_abbrev_section,
6620 cu->header.abbrev_sect_off);
6621 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6622 result_dwo_abbrev_table->get ());
6623
6624 /* Read in the die, but leave space to copy over the attributes
6625 from the stub. This has the benefit of simplifying the rest of
6626 the code - all the work to maintain the illusion of a single
6627 DW_TAG_{compile,type}_unit DIE is done here. */
6628 num_extra_attrs = ((stmt_list != NULL)
6629 + (low_pc != NULL)
6630 + (high_pc != NULL)
6631 + (ranges != NULL)
6632 + (comp_dir != NULL));
6633 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6634 num_extra_attrs);
6635
6636 /* Copy over the attributes from the stub to the DIE we just read in. */
6637 comp_unit_die = *result_comp_unit_die;
6638 i = comp_unit_die->num_attrs;
6639 if (stmt_list != NULL)
6640 comp_unit_die->attrs[i++] = *stmt_list;
6641 if (low_pc != NULL)
6642 comp_unit_die->attrs[i++] = *low_pc;
6643 if (high_pc != NULL)
6644 comp_unit_die->attrs[i++] = *high_pc;
6645 if (ranges != NULL)
6646 comp_unit_die->attrs[i++] = *ranges;
6647 if (comp_dir != NULL)
6648 comp_unit_die->attrs[i++] = *comp_dir;
6649 comp_unit_die->num_attrs += num_extra_attrs;
6650
6651 if (dwarf_die_debug)
6652 {
6653 fprintf_unfiltered (gdb_stdlog,
6654 "Read die from %s@0x%x of %s:\n",
6655 section->get_name (),
6656 (unsigned) (begin_info_ptr - section->buffer),
6657 bfd_get_filename (abfd));
6658 dump_die (comp_unit_die, dwarf_die_debug);
6659 }
6660
6661 /* Skip dummy compilation units. */
6662 if (info_ptr >= begin_info_ptr + dwo_unit->length
6663 || peek_abbrev_code (abfd, info_ptr) == 0)
6664 return 0;
6665
6666 *result_info_ptr = info_ptr;
6667 return 1;
6668 }
6669
6670 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6671 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6672 signature is part of the header. */
6673 static gdb::optional<ULONGEST>
6674 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6675 {
6676 if (cu->header.version >= 5)
6677 return cu->header.signature;
6678 struct attribute *attr;
6679 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6680 if (attr == nullptr)
6681 return gdb::optional<ULONGEST> ();
6682 return DW_UNSND (attr);
6683 }
6684
6685 /* Subroutine of cutu_reader to simplify it.
6686 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6687 Returns NULL if the specified DWO unit cannot be found. */
6688
6689 static struct dwo_unit *
6690 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6691 struct die_info *comp_unit_die,
6692 const char *dwo_name)
6693 {
6694 struct dwarf2_cu *cu = this_cu->cu;
6695 struct dwo_unit *dwo_unit;
6696 const char *comp_dir;
6697
6698 gdb_assert (cu != NULL);
6699
6700 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6701 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6702 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6703
6704 if (this_cu->is_debug_types)
6705 {
6706 struct signatured_type *sig_type;
6707
6708 /* Since this_cu is the first member of struct signatured_type,
6709 we can go from a pointer to one to a pointer to the other. */
6710 sig_type = (struct signatured_type *) this_cu;
6711 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6712 }
6713 else
6714 {
6715 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6716 if (!signature.has_value ())
6717 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6718 " [in module %s]"),
6719 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6720 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6721 *signature);
6722 }
6723
6724 return dwo_unit;
6725 }
6726
6727 /* Subroutine of cutu_reader to simplify it.
6728 See it for a description of the parameters.
6729 Read a TU directly from a DWO file, bypassing the stub. */
6730
6731 void
6732 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6733 int use_existing_cu, int keep)
6734 {
6735 struct signatured_type *sig_type;
6736 struct die_reader_specs reader;
6737
6738 /* Verify we can do the following downcast, and that we have the
6739 data we need. */
6740 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6741 sig_type = (struct signatured_type *) this_cu;
6742 gdb_assert (sig_type->dwo_unit != NULL);
6743
6744 if (use_existing_cu && this_cu->cu != NULL)
6745 {
6746 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6747 /* There's no need to do the rereading_dwo_cu handling that
6748 cutu_reader does since we don't read the stub. */
6749 }
6750 else
6751 {
6752 /* If !use_existing_cu, this_cu->cu must be NULL. */
6753 gdb_assert (this_cu->cu == NULL);
6754 m_new_cu.reset (new dwarf2_cu (this_cu));
6755 }
6756
6757 /* A future optimization, if needed, would be to use an existing
6758 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6759 could share abbrev tables. */
6760
6761 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6762 NULL /* stub_comp_unit_die */,
6763 sig_type->dwo_unit->dwo_file->comp_dir,
6764 &reader, &info_ptr,
6765 &comp_unit_die,
6766 &m_dwo_abbrev_table) == 0)
6767 {
6768 /* Dummy die. */
6769 dummy_p = true;
6770 }
6771 }
6772
6773 /* Initialize a CU (or TU) and read its DIEs.
6774 If the CU defers to a DWO file, read the DWO file as well.
6775
6776 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6777 Otherwise the table specified in the comp unit header is read in and used.
6778 This is an optimization for when we already have the abbrev table.
6779
6780 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6781 Otherwise, a new CU is allocated with xmalloc.
6782
6783 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6784 read_in_chain. Otherwise the dwarf2_cu data is freed at the
6785 end. */
6786
6787 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6788 struct abbrev_table *abbrev_table,
6789 int use_existing_cu, int keep,
6790 bool skip_partial)
6791 : die_reader_specs {},
6792 m_this_cu (this_cu),
6793 m_keep (keep)
6794 {
6795 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6796 struct objfile *objfile = dwarf2_per_objfile->objfile;
6797 struct dwarf2_section_info *section = this_cu->section;
6798 bfd *abfd = section->get_bfd_owner ();
6799 struct dwarf2_cu *cu;
6800 const gdb_byte *begin_info_ptr;
6801 struct signatured_type *sig_type = NULL;
6802 struct dwarf2_section_info *abbrev_section;
6803 /* Non-zero if CU currently points to a DWO file and we need to
6804 reread it. When this happens we need to reread the skeleton die
6805 before we can reread the DWO file (this only applies to CUs, not TUs). */
6806 int rereading_dwo_cu = 0;
6807
6808 if (dwarf_die_debug)
6809 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6810 this_cu->is_debug_types ? "type" : "comp",
6811 sect_offset_str (this_cu->sect_off));
6812
6813 if (use_existing_cu)
6814 gdb_assert (keep);
6815
6816 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6817 file (instead of going through the stub), short-circuit all of this. */
6818 if (this_cu->reading_dwo_directly)
6819 {
6820 /* Narrow down the scope of possibilities to have to understand. */
6821 gdb_assert (this_cu->is_debug_types);
6822 gdb_assert (abbrev_table == NULL);
6823 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
6824 return;
6825 }
6826
6827 /* This is cheap if the section is already read in. */
6828 section->read (objfile);
6829
6830 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6831
6832 abbrev_section = get_abbrev_section_for_cu (this_cu);
6833
6834 if (use_existing_cu && this_cu->cu != NULL)
6835 {
6836 cu = this_cu->cu;
6837 /* If this CU is from a DWO file we need to start over, we need to
6838 refetch the attributes from the skeleton CU.
6839 This could be optimized by retrieving those attributes from when we
6840 were here the first time: the previous comp_unit_die was stored in
6841 comp_unit_obstack. But there's no data yet that we need this
6842 optimization. */
6843 if (cu->dwo_unit != NULL)
6844 rereading_dwo_cu = 1;
6845 }
6846 else
6847 {
6848 /* If !use_existing_cu, this_cu->cu must be NULL. */
6849 gdb_assert (this_cu->cu == NULL);
6850 m_new_cu.reset (new dwarf2_cu (this_cu));
6851 cu = m_new_cu.get ();
6852 }
6853
6854 /* Get the header. */
6855 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6856 {
6857 /* We already have the header, there's no need to read it in again. */
6858 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6859 }
6860 else
6861 {
6862 if (this_cu->is_debug_types)
6863 {
6864 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6865 &cu->header, section,
6866 abbrev_section, info_ptr,
6867 rcuh_kind::TYPE);
6868
6869 /* Since per_cu is the first member of struct signatured_type,
6870 we can go from a pointer to one to a pointer to the other. */
6871 sig_type = (struct signatured_type *) this_cu;
6872 gdb_assert (sig_type->signature == cu->header.signature);
6873 gdb_assert (sig_type->type_offset_in_tu
6874 == cu->header.type_cu_offset_in_tu);
6875 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6876
6877 /* LENGTH has not been set yet for type units if we're
6878 using .gdb_index. */
6879 this_cu->length = cu->header.get_length ();
6880
6881 /* Establish the type offset that can be used to lookup the type. */
6882 sig_type->type_offset_in_section =
6883 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6884
6885 this_cu->dwarf_version = cu->header.version;
6886 }
6887 else
6888 {
6889 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6890 &cu->header, section,
6891 abbrev_section,
6892 info_ptr,
6893 rcuh_kind::COMPILE);
6894
6895 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6896 gdb_assert (this_cu->length == cu->header.get_length ());
6897 this_cu->dwarf_version = cu->header.version;
6898 }
6899 }
6900
6901 /* Skip dummy compilation units. */
6902 if (info_ptr >= begin_info_ptr + this_cu->length
6903 || peek_abbrev_code (abfd, info_ptr) == 0)
6904 {
6905 dummy_p = true;
6906 return;
6907 }
6908
6909 /* If we don't have them yet, read the abbrevs for this compilation unit.
6910 And if we need to read them now, make sure they're freed when we're
6911 done. */
6912 if (abbrev_table != NULL)
6913 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6914 else
6915 {
6916 m_abbrev_table_holder
6917 = abbrev_table::read (objfile, abbrev_section,
6918 cu->header.abbrev_sect_off);
6919 abbrev_table = m_abbrev_table_holder.get ();
6920 }
6921
6922 /* Read the top level CU/TU die. */
6923 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6924 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6925
6926 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6927 {
6928 dummy_p = true;
6929 return;
6930 }
6931
6932 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6933 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6934 table from the DWO file and pass the ownership over to us. It will be
6935 referenced from READER, so we must make sure to free it after we're done
6936 with READER.
6937
6938 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6939 DWO CU, that this test will fail (the attribute will not be present). */
6940 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6941 if (dwo_name != nullptr)
6942 {
6943 struct dwo_unit *dwo_unit;
6944 struct die_info *dwo_comp_unit_die;
6945
6946 if (comp_unit_die->has_children)
6947 {
6948 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6949 " has children (offset %s) [in module %s]"),
6950 sect_offset_str (this_cu->sect_off),
6951 bfd_get_filename (abfd));
6952 }
6953 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6954 if (dwo_unit != NULL)
6955 {
6956 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6957 comp_unit_die, NULL,
6958 this, &info_ptr,
6959 &dwo_comp_unit_die,
6960 &m_dwo_abbrev_table) == 0)
6961 {
6962 /* Dummy die. */
6963 dummy_p = true;
6964 return;
6965 }
6966 comp_unit_die = dwo_comp_unit_die;
6967 }
6968 else
6969 {
6970 /* Yikes, we couldn't find the rest of the DIE, we only have
6971 the stub. A complaint has already been logged. There's
6972 not much more we can do except pass on the stub DIE to
6973 die_reader_func. We don't want to throw an error on bad
6974 debug info. */
6975 }
6976 }
6977 }
6978
6979 cutu_reader::~cutu_reader ()
6980 {
6981 /* Done, clean up. */
6982 if (m_new_cu != NULL && m_keep && !dummy_p)
6983 {
6984 struct dwarf2_per_objfile *dwarf2_per_objfile
6985 = m_this_cu->dwarf2_per_objfile;
6986 /* Link this CU into read_in_chain. */
6987 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6988 dwarf2_per_objfile->read_in_chain = m_this_cu;
6989 /* The chain owns it now. */
6990 m_new_cu.release ();
6991 }
6992 }
6993
6994 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6995 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6996 assumed to have already done the lookup to find the DWO file).
6997
6998 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6999 THIS_CU->is_debug_types, but nothing else.
7000
7001 We fill in THIS_CU->length.
7002
7003 THIS_CU->cu is always freed when done.
7004 This is done in order to not leave THIS_CU->cu in a state where we have
7005 to care whether it refers to the "main" CU or the DWO CU.
7006
7007 When parent_cu is passed, it is used to provide a default value for
7008 str_offsets_base and addr_base from the parent. */
7009
7010 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7011 struct dwarf2_cu *parent_cu,
7012 struct dwo_file *dwo_file)
7013 : die_reader_specs {},
7014 m_this_cu (this_cu)
7015 {
7016 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7017 struct objfile *objfile = dwarf2_per_objfile->objfile;
7018 struct dwarf2_section_info *section = this_cu->section;
7019 bfd *abfd = section->get_bfd_owner ();
7020 struct dwarf2_section_info *abbrev_section;
7021 const gdb_byte *begin_info_ptr, *info_ptr;
7022
7023 if (dwarf_die_debug)
7024 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7025 this_cu->is_debug_types ? "type" : "comp",
7026 sect_offset_str (this_cu->sect_off));
7027
7028 gdb_assert (this_cu->cu == NULL);
7029
7030 abbrev_section = (dwo_file != NULL
7031 ? &dwo_file->sections.abbrev
7032 : get_abbrev_section_for_cu (this_cu));
7033
7034 /* This is cheap if the section is already read in. */
7035 section->read (objfile);
7036
7037 m_new_cu.reset (new dwarf2_cu (this_cu));
7038
7039 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7040 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7041 &m_new_cu->header, section,
7042 abbrev_section, info_ptr,
7043 (this_cu->is_debug_types
7044 ? rcuh_kind::TYPE
7045 : rcuh_kind::COMPILE));
7046
7047 if (parent_cu != nullptr)
7048 {
7049 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7050 m_new_cu->addr_base = parent_cu->addr_base;
7051 }
7052 this_cu->length = m_new_cu->header.get_length ();
7053
7054 /* Skip dummy compilation units. */
7055 if (info_ptr >= begin_info_ptr + this_cu->length
7056 || peek_abbrev_code (abfd, info_ptr) == 0)
7057 {
7058 dummy_p = true;
7059 return;
7060 }
7061
7062 m_abbrev_table_holder
7063 = abbrev_table::read (objfile, abbrev_section,
7064 m_new_cu->header.abbrev_sect_off);
7065
7066 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7067 m_abbrev_table_holder.get ());
7068 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7069 }
7070
7071 \f
7072 /* Type Unit Groups.
7073
7074 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7075 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7076 so that all types coming from the same compilation (.o file) are grouped
7077 together. A future step could be to put the types in the same symtab as
7078 the CU the types ultimately came from. */
7079
7080 static hashval_t
7081 hash_type_unit_group (const void *item)
7082 {
7083 const struct type_unit_group *tu_group
7084 = (const struct type_unit_group *) item;
7085
7086 return hash_stmt_list_entry (&tu_group->hash);
7087 }
7088
7089 static int
7090 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7091 {
7092 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7093 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7094
7095 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7096 }
7097
7098 /* Allocate a hash table for type unit groups. */
7099
7100 static htab_up
7101 allocate_type_unit_groups_table (struct objfile *objfile)
7102 {
7103 return htab_up (htab_create_alloc (3,
7104 hash_type_unit_group,
7105 eq_type_unit_group,
7106 NULL, xcalloc, xfree));
7107 }
7108
7109 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7110 partial symtabs. We combine several TUs per psymtab to not let the size
7111 of any one psymtab grow too big. */
7112 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7113 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7114
7115 /* Helper routine for get_type_unit_group.
7116 Create the type_unit_group object used to hold one or more TUs. */
7117
7118 static struct type_unit_group *
7119 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7120 {
7121 struct dwarf2_per_objfile *dwarf2_per_objfile
7122 = cu->per_cu->dwarf2_per_objfile;
7123 struct objfile *objfile = dwarf2_per_objfile->objfile;
7124 struct dwarf2_per_cu_data *per_cu;
7125 struct type_unit_group *tu_group;
7126
7127 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7128 struct type_unit_group);
7129 per_cu = &tu_group->per_cu;
7130 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7131
7132 if (dwarf2_per_objfile->using_index)
7133 {
7134 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7135 struct dwarf2_per_cu_quick_data);
7136 }
7137 else
7138 {
7139 unsigned int line_offset = to_underlying (line_offset_struct);
7140 dwarf2_psymtab *pst;
7141 std::string name;
7142
7143 /* Give the symtab a useful name for debug purposes. */
7144 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7145 name = string_printf ("<type_units_%d>",
7146 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7147 else
7148 name = string_printf ("<type_units_at_0x%x>", line_offset);
7149
7150 pst = create_partial_symtab (per_cu, name.c_str ());
7151 pst->anonymous = true;
7152 }
7153
7154 tu_group->hash.dwo_unit = cu->dwo_unit;
7155 tu_group->hash.line_sect_off = line_offset_struct;
7156
7157 return tu_group;
7158 }
7159
7160 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7161 STMT_LIST is a DW_AT_stmt_list attribute. */
7162
7163 static struct type_unit_group *
7164 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7165 {
7166 struct dwarf2_per_objfile *dwarf2_per_objfile
7167 = cu->per_cu->dwarf2_per_objfile;
7168 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7169 struct type_unit_group *tu_group;
7170 void **slot;
7171 unsigned int line_offset;
7172 struct type_unit_group type_unit_group_for_lookup;
7173
7174 if (dwarf2_per_objfile->type_unit_groups == NULL)
7175 {
7176 dwarf2_per_objfile->type_unit_groups =
7177 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7178 }
7179
7180 /* Do we need to create a new group, or can we use an existing one? */
7181
7182 if (stmt_list)
7183 {
7184 line_offset = DW_UNSND (stmt_list);
7185 ++tu_stats->nr_symtab_sharers;
7186 }
7187 else
7188 {
7189 /* Ugh, no stmt_list. Rare, but we have to handle it.
7190 We can do various things here like create one group per TU or
7191 spread them over multiple groups to split up the expansion work.
7192 To avoid worst case scenarios (too many groups or too large groups)
7193 we, umm, group them in bunches. */
7194 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7195 | (tu_stats->nr_stmt_less_type_units
7196 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7197 ++tu_stats->nr_stmt_less_type_units;
7198 }
7199
7200 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7201 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7202 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7203 &type_unit_group_for_lookup, INSERT);
7204 if (*slot != NULL)
7205 {
7206 tu_group = (struct type_unit_group *) *slot;
7207 gdb_assert (tu_group != NULL);
7208 }
7209 else
7210 {
7211 sect_offset line_offset_struct = (sect_offset) line_offset;
7212 tu_group = create_type_unit_group (cu, line_offset_struct);
7213 *slot = tu_group;
7214 ++tu_stats->nr_symtabs;
7215 }
7216
7217 return tu_group;
7218 }
7219 \f
7220 /* Partial symbol tables. */
7221
7222 /* Create a psymtab named NAME and assign it to PER_CU.
7223
7224 The caller must fill in the following details:
7225 dirname, textlow, texthigh. */
7226
7227 static dwarf2_psymtab *
7228 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7229 {
7230 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7231 dwarf2_psymtab *pst;
7232
7233 pst = new dwarf2_psymtab (name, objfile, 0);
7234
7235 pst->psymtabs_addrmap_supported = true;
7236
7237 /* This is the glue that links PST into GDB's symbol API. */
7238 pst->per_cu_data = per_cu;
7239 per_cu->v.psymtab = pst;
7240
7241 return pst;
7242 }
7243
7244 /* DIE reader function for process_psymtab_comp_unit. */
7245
7246 static void
7247 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7248 const gdb_byte *info_ptr,
7249 struct die_info *comp_unit_die,
7250 int want_partial_unit,
7251 enum language pretend_language)
7252 {
7253 struct dwarf2_cu *cu = reader->cu;
7254 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7255 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7256 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7257 CORE_ADDR baseaddr;
7258 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7259 dwarf2_psymtab *pst;
7260 enum pc_bounds_kind cu_bounds_kind;
7261 const char *filename;
7262
7263 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
7264 return;
7265
7266 gdb_assert (! per_cu->is_debug_types);
7267
7268 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7269
7270 /* Allocate a new partial symbol table structure. */
7271 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7272 if (filename == NULL)
7273 filename = "";
7274
7275 pst = create_partial_symtab (per_cu, filename);
7276
7277 /* This must be done before calling dwarf2_build_include_psymtabs. */
7278 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7279
7280 baseaddr = objfile->text_section_offset ();
7281
7282 dwarf2_find_base_address (comp_unit_die, cu);
7283
7284 /* Possibly set the default values of LOWPC and HIGHPC from
7285 `DW_AT_ranges'. */
7286 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7287 &best_highpc, cu, pst);
7288 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7289 {
7290 CORE_ADDR low
7291 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7292 - baseaddr);
7293 CORE_ADDR high
7294 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7295 - baseaddr - 1);
7296 /* Store the contiguous range if it is not empty; it can be
7297 empty for CUs with no code. */
7298 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7299 low, high, pst);
7300 }
7301
7302 /* Check if comp unit has_children.
7303 If so, read the rest of the partial symbols from this comp unit.
7304 If not, there's no more debug_info for this comp unit. */
7305 if (comp_unit_die->has_children)
7306 {
7307 struct partial_die_info *first_die;
7308 CORE_ADDR lowpc, highpc;
7309
7310 lowpc = ((CORE_ADDR) -1);
7311 highpc = ((CORE_ADDR) 0);
7312
7313 first_die = load_partial_dies (reader, info_ptr, 1);
7314
7315 scan_partial_symbols (first_die, &lowpc, &highpc,
7316 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7317
7318 /* If we didn't find a lowpc, set it to highpc to avoid
7319 complaints from `maint check'. */
7320 if (lowpc == ((CORE_ADDR) -1))
7321 lowpc = highpc;
7322
7323 /* If the compilation unit didn't have an explicit address range,
7324 then use the information extracted from its child dies. */
7325 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7326 {
7327 best_lowpc = lowpc;
7328 best_highpc = highpc;
7329 }
7330 }
7331 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7332 best_lowpc + baseaddr)
7333 - baseaddr);
7334 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7335 best_highpc + baseaddr)
7336 - baseaddr);
7337
7338 end_psymtab_common (objfile, pst);
7339
7340 if (!cu->per_cu->imported_symtabs_empty ())
7341 {
7342 int i;
7343 int len = cu->per_cu->imported_symtabs_size ();
7344
7345 /* Fill in 'dependencies' here; we fill in 'users' in a
7346 post-pass. */
7347 pst->number_of_dependencies = len;
7348 pst->dependencies
7349 = objfile->partial_symtabs->allocate_dependencies (len);
7350 for (i = 0; i < len; ++i)
7351 {
7352 pst->dependencies[i]
7353 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7354 }
7355
7356 cu->per_cu->imported_symtabs_free ();
7357 }
7358
7359 /* Get the list of files included in the current compilation unit,
7360 and build a psymtab for each of them. */
7361 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7362
7363 if (dwarf_read_debug)
7364 fprintf_unfiltered (gdb_stdlog,
7365 "Psymtab for %s unit @%s: %s - %s"
7366 ", %d global, %d static syms\n",
7367 per_cu->is_debug_types ? "type" : "comp",
7368 sect_offset_str (per_cu->sect_off),
7369 paddress (gdbarch, pst->text_low (objfile)),
7370 paddress (gdbarch, pst->text_high (objfile)),
7371 pst->n_global_syms, pst->n_static_syms);
7372 }
7373
7374 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7375 Process compilation unit THIS_CU for a psymtab. */
7376
7377 static void
7378 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7379 int want_partial_unit,
7380 enum language pretend_language)
7381 {
7382 /* If this compilation unit was already read in, free the
7383 cached copy in order to read it in again. This is
7384 necessary because we skipped some symbols when we first
7385 read in the compilation unit (see load_partial_dies).
7386 This problem could be avoided, but the benefit is unclear. */
7387 if (this_cu->cu != NULL)
7388 free_one_cached_comp_unit (this_cu);
7389
7390 cutu_reader reader (this_cu, NULL, 0, 0, false);
7391
7392 if (reader.dummy_p)
7393 {
7394 /* Nothing. */
7395 }
7396 else if (this_cu->is_debug_types)
7397 build_type_psymtabs_reader (&reader, reader.info_ptr,
7398 reader.comp_unit_die);
7399 else
7400 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7401 reader.comp_unit_die,
7402 want_partial_unit,
7403 pretend_language);
7404
7405 /* Age out any secondary CUs. */
7406 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7407 }
7408
7409 /* Reader function for build_type_psymtabs. */
7410
7411 static void
7412 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7413 const gdb_byte *info_ptr,
7414 struct die_info *type_unit_die)
7415 {
7416 struct dwarf2_per_objfile *dwarf2_per_objfile
7417 = reader->cu->per_cu->dwarf2_per_objfile;
7418 struct objfile *objfile = dwarf2_per_objfile->objfile;
7419 struct dwarf2_cu *cu = reader->cu;
7420 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7421 struct signatured_type *sig_type;
7422 struct type_unit_group *tu_group;
7423 struct attribute *attr;
7424 struct partial_die_info *first_die;
7425 CORE_ADDR lowpc, highpc;
7426 dwarf2_psymtab *pst;
7427
7428 gdb_assert (per_cu->is_debug_types);
7429 sig_type = (struct signatured_type *) per_cu;
7430
7431 if (! type_unit_die->has_children)
7432 return;
7433
7434 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7435 tu_group = get_type_unit_group (cu, attr);
7436
7437 if (tu_group->tus == nullptr)
7438 tu_group->tus = new std::vector<signatured_type *>;
7439 tu_group->tus->push_back (sig_type);
7440
7441 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7442 pst = create_partial_symtab (per_cu, "");
7443 pst->anonymous = true;
7444
7445 first_die = load_partial_dies (reader, info_ptr, 1);
7446
7447 lowpc = (CORE_ADDR) -1;
7448 highpc = (CORE_ADDR) 0;
7449 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7450
7451 end_psymtab_common (objfile, pst);
7452 }
7453
7454 /* Struct used to sort TUs by their abbreviation table offset. */
7455
7456 struct tu_abbrev_offset
7457 {
7458 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7459 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7460 {}
7461
7462 signatured_type *sig_type;
7463 sect_offset abbrev_offset;
7464 };
7465
7466 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7467
7468 static bool
7469 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7470 const struct tu_abbrev_offset &b)
7471 {
7472 return a.abbrev_offset < b.abbrev_offset;
7473 }
7474
7475 /* Efficiently read all the type units.
7476 This does the bulk of the work for build_type_psymtabs.
7477
7478 The efficiency is because we sort TUs by the abbrev table they use and
7479 only read each abbrev table once. In one program there are 200K TUs
7480 sharing 8K abbrev tables.
7481
7482 The main purpose of this function is to support building the
7483 dwarf2_per_objfile->type_unit_groups table.
7484 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7485 can collapse the search space by grouping them by stmt_list.
7486 The savings can be significant, in the same program from above the 200K TUs
7487 share 8K stmt_list tables.
7488
7489 FUNC is expected to call get_type_unit_group, which will create the
7490 struct type_unit_group if necessary and add it to
7491 dwarf2_per_objfile->type_unit_groups. */
7492
7493 static void
7494 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7495 {
7496 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7497 abbrev_table_up abbrev_table;
7498 sect_offset abbrev_offset;
7499
7500 /* It's up to the caller to not call us multiple times. */
7501 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7502
7503 if (dwarf2_per_objfile->all_type_units.empty ())
7504 return;
7505
7506 /* TUs typically share abbrev tables, and there can be way more TUs than
7507 abbrev tables. Sort by abbrev table to reduce the number of times we
7508 read each abbrev table in.
7509 Alternatives are to punt or to maintain a cache of abbrev tables.
7510 This is simpler and efficient enough for now.
7511
7512 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7513 symtab to use). Typically TUs with the same abbrev offset have the same
7514 stmt_list value too so in practice this should work well.
7515
7516 The basic algorithm here is:
7517
7518 sort TUs by abbrev table
7519 for each TU with same abbrev table:
7520 read abbrev table if first user
7521 read TU top level DIE
7522 [IWBN if DWO skeletons had DW_AT_stmt_list]
7523 call FUNC */
7524
7525 if (dwarf_read_debug)
7526 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7527
7528 /* Sort in a separate table to maintain the order of all_type_units
7529 for .gdb_index: TU indices directly index all_type_units. */
7530 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7531 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7532
7533 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7534 sorted_by_abbrev.emplace_back
7535 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7536 sig_type->per_cu.section,
7537 sig_type->per_cu.sect_off));
7538
7539 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7540 sort_tu_by_abbrev_offset);
7541
7542 abbrev_offset = (sect_offset) ~(unsigned) 0;
7543
7544 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7545 {
7546 /* Switch to the next abbrev table if necessary. */
7547 if (abbrev_table == NULL
7548 || tu.abbrev_offset != abbrev_offset)
7549 {
7550 abbrev_offset = tu.abbrev_offset;
7551 abbrev_table =
7552 abbrev_table::read (dwarf2_per_objfile->objfile,
7553 &dwarf2_per_objfile->abbrev,
7554 abbrev_offset);
7555 ++tu_stats->nr_uniq_abbrev_tables;
7556 }
7557
7558 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7559 0, 0, false);
7560 if (!reader.dummy_p)
7561 build_type_psymtabs_reader (&reader, reader.info_ptr,
7562 reader.comp_unit_die);
7563 }
7564 }
7565
7566 /* Print collected type unit statistics. */
7567
7568 static void
7569 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7570 {
7571 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7572
7573 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7574 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7575 dwarf2_per_objfile->all_type_units.size ());
7576 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7577 tu_stats->nr_uniq_abbrev_tables);
7578 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7579 tu_stats->nr_symtabs);
7580 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7581 tu_stats->nr_symtab_sharers);
7582 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7583 tu_stats->nr_stmt_less_type_units);
7584 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7585 tu_stats->nr_all_type_units_reallocs);
7586 }
7587
7588 /* Traversal function for build_type_psymtabs. */
7589
7590 static int
7591 build_type_psymtab_dependencies (void **slot, void *info)
7592 {
7593 struct dwarf2_per_objfile *dwarf2_per_objfile
7594 = (struct dwarf2_per_objfile *) info;
7595 struct objfile *objfile = dwarf2_per_objfile->objfile;
7596 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7597 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7598 dwarf2_psymtab *pst = per_cu->v.psymtab;
7599 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7600 int i;
7601
7602 gdb_assert (len > 0);
7603 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7604
7605 pst->number_of_dependencies = len;
7606 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7607 for (i = 0; i < len; ++i)
7608 {
7609 struct signatured_type *iter = tu_group->tus->at (i);
7610 gdb_assert (iter->per_cu.is_debug_types);
7611 pst->dependencies[i] = iter->per_cu.v.psymtab;
7612 iter->type_unit_group = tu_group;
7613 }
7614
7615 delete tu_group->tus;
7616 tu_group->tus = nullptr;
7617
7618 return 1;
7619 }
7620
7621 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7622 Build partial symbol tables for the .debug_types comp-units. */
7623
7624 static void
7625 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7626 {
7627 if (! create_all_type_units (dwarf2_per_objfile))
7628 return;
7629
7630 build_type_psymtabs_1 (dwarf2_per_objfile);
7631 }
7632
7633 /* Traversal function for process_skeletonless_type_unit.
7634 Read a TU in a DWO file and build partial symbols for it. */
7635
7636 static int
7637 process_skeletonless_type_unit (void **slot, void *info)
7638 {
7639 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7640 struct dwarf2_per_objfile *dwarf2_per_objfile
7641 = (struct dwarf2_per_objfile *) info;
7642 struct signatured_type find_entry, *entry;
7643
7644 /* If this TU doesn't exist in the global table, add it and read it in. */
7645
7646 if (dwarf2_per_objfile->signatured_types == NULL)
7647 {
7648 dwarf2_per_objfile->signatured_types
7649 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
7650 }
7651
7652 find_entry.signature = dwo_unit->signature;
7653 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7654 &find_entry, INSERT);
7655 /* If we've already seen this type there's nothing to do. What's happening
7656 is we're doing our own version of comdat-folding here. */
7657 if (*slot != NULL)
7658 return 1;
7659
7660 /* This does the job that create_all_type_units would have done for
7661 this TU. */
7662 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7663 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7664 *slot = entry;
7665
7666 /* This does the job that build_type_psymtabs_1 would have done. */
7667 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
7668 if (!reader.dummy_p)
7669 build_type_psymtabs_reader (&reader, reader.info_ptr,
7670 reader.comp_unit_die);
7671
7672 return 1;
7673 }
7674
7675 /* Traversal function for process_skeletonless_type_units. */
7676
7677 static int
7678 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7679 {
7680 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7681
7682 if (dwo_file->tus != NULL)
7683 htab_traverse_noresize (dwo_file->tus.get (),
7684 process_skeletonless_type_unit, info);
7685
7686 return 1;
7687 }
7688
7689 /* Scan all TUs of DWO files, verifying we've processed them.
7690 This is needed in case a TU was emitted without its skeleton.
7691 Note: This can't be done until we know what all the DWO files are. */
7692
7693 static void
7694 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7695 {
7696 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7697 if (get_dwp_file (dwarf2_per_objfile) == NULL
7698 && dwarf2_per_objfile->dwo_files != NULL)
7699 {
7700 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7701 process_dwo_file_for_skeletonless_type_units,
7702 dwarf2_per_objfile);
7703 }
7704 }
7705
7706 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7707
7708 static void
7709 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7710 {
7711 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7712 {
7713 dwarf2_psymtab *pst = per_cu->v.psymtab;
7714
7715 if (pst == NULL)
7716 continue;
7717
7718 for (int j = 0; j < pst->number_of_dependencies; ++j)
7719 {
7720 /* Set the 'user' field only if it is not already set. */
7721 if (pst->dependencies[j]->user == NULL)
7722 pst->dependencies[j]->user = pst;
7723 }
7724 }
7725 }
7726
7727 /* Build the partial symbol table by doing a quick pass through the
7728 .debug_info and .debug_abbrev sections. */
7729
7730 static void
7731 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7732 {
7733 struct objfile *objfile = dwarf2_per_objfile->objfile;
7734
7735 if (dwarf_read_debug)
7736 {
7737 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7738 objfile_name (objfile));
7739 }
7740
7741 dwarf2_per_objfile->reading_partial_symbols = 1;
7742
7743 dwarf2_per_objfile->info.read (objfile);
7744
7745 /* Any cached compilation units will be linked by the per-objfile
7746 read_in_chain. Make sure to free them when we're done. */
7747 free_cached_comp_units freer (dwarf2_per_objfile);
7748
7749 build_type_psymtabs (dwarf2_per_objfile);
7750
7751 create_all_comp_units (dwarf2_per_objfile);
7752
7753 /* Create a temporary address map on a temporary obstack. We later
7754 copy this to the final obstack. */
7755 auto_obstack temp_obstack;
7756
7757 scoped_restore save_psymtabs_addrmap
7758 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7759 addrmap_create_mutable (&temp_obstack));
7760
7761 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7762 process_psymtab_comp_unit (per_cu, 0, language_minimal);
7763
7764 /* This has to wait until we read the CUs, we need the list of DWOs. */
7765 process_skeletonless_type_units (dwarf2_per_objfile);
7766
7767 /* Now that all TUs have been processed we can fill in the dependencies. */
7768 if (dwarf2_per_objfile->type_unit_groups != NULL)
7769 {
7770 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7771 build_type_psymtab_dependencies, dwarf2_per_objfile);
7772 }
7773
7774 if (dwarf_read_debug)
7775 print_tu_stats (dwarf2_per_objfile);
7776
7777 set_partial_user (dwarf2_per_objfile);
7778
7779 objfile->partial_symtabs->psymtabs_addrmap
7780 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7781 objfile->partial_symtabs->obstack ());
7782 /* At this point we want to keep the address map. */
7783 save_psymtabs_addrmap.release ();
7784
7785 if (dwarf_read_debug)
7786 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7787 objfile_name (objfile));
7788 }
7789
7790 /* Load the partial DIEs for a secondary CU into memory.
7791 This is also used when rereading a primary CU with load_all_dies. */
7792
7793 static void
7794 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7795 {
7796 cutu_reader reader (this_cu, NULL, 1, 1, false);
7797
7798 if (!reader.dummy_p)
7799 {
7800 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7801 language_minimal);
7802
7803 /* Check if comp unit has_children.
7804 If so, read the rest of the partial symbols from this comp unit.
7805 If not, there's no more debug_info for this comp unit. */
7806 if (reader.comp_unit_die->has_children)
7807 load_partial_dies (&reader, reader.info_ptr, 0);
7808 }
7809 }
7810
7811 static void
7812 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7813 struct dwarf2_section_info *section,
7814 struct dwarf2_section_info *abbrev_section,
7815 unsigned int is_dwz)
7816 {
7817 const gdb_byte *info_ptr;
7818 struct objfile *objfile = dwarf2_per_objfile->objfile;
7819
7820 if (dwarf_read_debug)
7821 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7822 section->get_name (),
7823 section->get_file_name ());
7824
7825 section->read (objfile);
7826
7827 info_ptr = section->buffer;
7828
7829 while (info_ptr < section->buffer + section->size)
7830 {
7831 struct dwarf2_per_cu_data *this_cu;
7832
7833 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7834
7835 comp_unit_head cu_header;
7836 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7837 abbrev_section, info_ptr,
7838 rcuh_kind::COMPILE);
7839
7840 /* Save the compilation unit for later lookup. */
7841 if (cu_header.unit_type != DW_UT_type)
7842 {
7843 this_cu = XOBNEW (&objfile->objfile_obstack,
7844 struct dwarf2_per_cu_data);
7845 memset (this_cu, 0, sizeof (*this_cu));
7846 }
7847 else
7848 {
7849 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7850 struct signatured_type);
7851 memset (sig_type, 0, sizeof (*sig_type));
7852 sig_type->signature = cu_header.signature;
7853 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7854 this_cu = &sig_type->per_cu;
7855 }
7856 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7857 this_cu->sect_off = sect_off;
7858 this_cu->length = cu_header.length + cu_header.initial_length_size;
7859 this_cu->is_dwz = is_dwz;
7860 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7861 this_cu->section = section;
7862
7863 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7864
7865 info_ptr = info_ptr + this_cu->length;
7866 }
7867 }
7868
7869 /* Create a list of all compilation units in OBJFILE.
7870 This is only done for -readnow and building partial symtabs. */
7871
7872 static void
7873 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7874 {
7875 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7876 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7877 &dwarf2_per_objfile->abbrev, 0);
7878
7879 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7880 if (dwz != NULL)
7881 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7882 1);
7883 }
7884
7885 /* Process all loaded DIEs for compilation unit CU, starting at
7886 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7887 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7888 DW_AT_ranges). See the comments of add_partial_subprogram on how
7889 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7890
7891 static void
7892 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7893 CORE_ADDR *highpc, int set_addrmap,
7894 struct dwarf2_cu *cu)
7895 {
7896 struct partial_die_info *pdi;
7897
7898 /* Now, march along the PDI's, descending into ones which have
7899 interesting children but skipping the children of the other ones,
7900 until we reach the end of the compilation unit. */
7901
7902 pdi = first_die;
7903
7904 while (pdi != NULL)
7905 {
7906 pdi->fixup (cu);
7907
7908 /* Anonymous namespaces or modules have no name but have interesting
7909 children, so we need to look at them. Ditto for anonymous
7910 enums. */
7911
7912 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7913 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7914 || pdi->tag == DW_TAG_imported_unit
7915 || pdi->tag == DW_TAG_inlined_subroutine)
7916 {
7917 switch (pdi->tag)
7918 {
7919 case DW_TAG_subprogram:
7920 case DW_TAG_inlined_subroutine:
7921 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7922 break;
7923 case DW_TAG_constant:
7924 case DW_TAG_variable:
7925 case DW_TAG_typedef:
7926 case DW_TAG_union_type:
7927 if (!pdi->is_declaration)
7928 {
7929 add_partial_symbol (pdi, cu);
7930 }
7931 break;
7932 case DW_TAG_class_type:
7933 case DW_TAG_interface_type:
7934 case DW_TAG_structure_type:
7935 if (!pdi->is_declaration)
7936 {
7937 add_partial_symbol (pdi, cu);
7938 }
7939 if ((cu->language == language_rust
7940 || cu->language == language_cplus) && pdi->has_children)
7941 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7942 set_addrmap, cu);
7943 break;
7944 case DW_TAG_enumeration_type:
7945 if (!pdi->is_declaration)
7946 add_partial_enumeration (pdi, cu);
7947 break;
7948 case DW_TAG_base_type:
7949 case DW_TAG_subrange_type:
7950 /* File scope base type definitions are added to the partial
7951 symbol table. */
7952 add_partial_symbol (pdi, cu);
7953 break;
7954 case DW_TAG_namespace:
7955 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7956 break;
7957 case DW_TAG_module:
7958 if (!pdi->is_declaration)
7959 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7960 break;
7961 case DW_TAG_imported_unit:
7962 {
7963 struct dwarf2_per_cu_data *per_cu;
7964
7965 /* For now we don't handle imported units in type units. */
7966 if (cu->per_cu->is_debug_types)
7967 {
7968 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7969 " supported in type units [in module %s]"),
7970 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7971 }
7972
7973 per_cu = dwarf2_find_containing_comp_unit
7974 (pdi->d.sect_off, pdi->is_dwz,
7975 cu->per_cu->dwarf2_per_objfile);
7976
7977 /* Go read the partial unit, if needed. */
7978 if (per_cu->v.psymtab == NULL)
7979 process_psymtab_comp_unit (per_cu, 1, cu->language);
7980
7981 cu->per_cu->imported_symtabs_push (per_cu);
7982 }
7983 break;
7984 case DW_TAG_imported_declaration:
7985 add_partial_symbol (pdi, cu);
7986 break;
7987 default:
7988 break;
7989 }
7990 }
7991
7992 /* If the die has a sibling, skip to the sibling. */
7993
7994 pdi = pdi->die_sibling;
7995 }
7996 }
7997
7998 /* Functions used to compute the fully scoped name of a partial DIE.
7999
8000 Normally, this is simple. For C++, the parent DIE's fully scoped
8001 name is concatenated with "::" and the partial DIE's name.
8002 Enumerators are an exception; they use the scope of their parent
8003 enumeration type, i.e. the name of the enumeration type is not
8004 prepended to the enumerator.
8005
8006 There are two complexities. One is DW_AT_specification; in this
8007 case "parent" means the parent of the target of the specification,
8008 instead of the direct parent of the DIE. The other is compilers
8009 which do not emit DW_TAG_namespace; in this case we try to guess
8010 the fully qualified name of structure types from their members'
8011 linkage names. This must be done using the DIE's children rather
8012 than the children of any DW_AT_specification target. We only need
8013 to do this for structures at the top level, i.e. if the target of
8014 any DW_AT_specification (if any; otherwise the DIE itself) does not
8015 have a parent. */
8016
8017 /* Compute the scope prefix associated with PDI's parent, in
8018 compilation unit CU. The result will be allocated on CU's
8019 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8020 field. NULL is returned if no prefix is necessary. */
8021 static const char *
8022 partial_die_parent_scope (struct partial_die_info *pdi,
8023 struct dwarf2_cu *cu)
8024 {
8025 const char *grandparent_scope;
8026 struct partial_die_info *parent, *real_pdi;
8027
8028 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8029 then this means the parent of the specification DIE. */
8030
8031 real_pdi = pdi;
8032 while (real_pdi->has_specification)
8033 {
8034 auto res = find_partial_die (real_pdi->spec_offset,
8035 real_pdi->spec_is_dwz, cu);
8036 real_pdi = res.pdi;
8037 cu = res.cu;
8038 }
8039
8040 parent = real_pdi->die_parent;
8041 if (parent == NULL)
8042 return NULL;
8043
8044 if (parent->scope_set)
8045 return parent->scope;
8046
8047 parent->fixup (cu);
8048
8049 grandparent_scope = partial_die_parent_scope (parent, cu);
8050
8051 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8052 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8053 Work around this problem here. */
8054 if (cu->language == language_cplus
8055 && parent->tag == DW_TAG_namespace
8056 && strcmp (parent->name, "::") == 0
8057 && grandparent_scope == NULL)
8058 {
8059 parent->scope = NULL;
8060 parent->scope_set = 1;
8061 return NULL;
8062 }
8063
8064 /* Nested subroutines in Fortran get a prefix. */
8065 if (pdi->tag == DW_TAG_enumerator)
8066 /* Enumerators should not get the name of the enumeration as a prefix. */
8067 parent->scope = grandparent_scope;
8068 else if (parent->tag == DW_TAG_namespace
8069 || parent->tag == DW_TAG_module
8070 || parent->tag == DW_TAG_structure_type
8071 || parent->tag == DW_TAG_class_type
8072 || parent->tag == DW_TAG_interface_type
8073 || parent->tag == DW_TAG_union_type
8074 || parent->tag == DW_TAG_enumeration_type
8075 || (cu->language == language_fortran
8076 && parent->tag == DW_TAG_subprogram
8077 && pdi->tag == DW_TAG_subprogram))
8078 {
8079 if (grandparent_scope == NULL)
8080 parent->scope = parent->name;
8081 else
8082 parent->scope = typename_concat (&cu->comp_unit_obstack,
8083 grandparent_scope,
8084 parent->name, 0, cu);
8085 }
8086 else
8087 {
8088 /* FIXME drow/2004-04-01: What should we be doing with
8089 function-local names? For partial symbols, we should probably be
8090 ignoring them. */
8091 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8092 dwarf_tag_name (parent->tag),
8093 sect_offset_str (pdi->sect_off));
8094 parent->scope = grandparent_scope;
8095 }
8096
8097 parent->scope_set = 1;
8098 return parent->scope;
8099 }
8100
8101 /* Return the fully scoped name associated with PDI, from compilation unit
8102 CU. The result will be allocated with malloc. */
8103
8104 static gdb::unique_xmalloc_ptr<char>
8105 partial_die_full_name (struct partial_die_info *pdi,
8106 struct dwarf2_cu *cu)
8107 {
8108 const char *parent_scope;
8109
8110 /* If this is a template instantiation, we can not work out the
8111 template arguments from partial DIEs. So, unfortunately, we have
8112 to go through the full DIEs. At least any work we do building
8113 types here will be reused if full symbols are loaded later. */
8114 if (pdi->has_template_arguments)
8115 {
8116 pdi->fixup (cu);
8117
8118 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8119 {
8120 struct die_info *die;
8121 struct attribute attr;
8122 struct dwarf2_cu *ref_cu = cu;
8123
8124 /* DW_FORM_ref_addr is using section offset. */
8125 attr.name = (enum dwarf_attribute) 0;
8126 attr.form = DW_FORM_ref_addr;
8127 attr.u.unsnd = to_underlying (pdi->sect_off);
8128 die = follow_die_ref (NULL, &attr, &ref_cu);
8129
8130 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8131 }
8132 }
8133
8134 parent_scope = partial_die_parent_scope (pdi, cu);
8135 if (parent_scope == NULL)
8136 return NULL;
8137 else
8138 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8139 pdi->name, 0, cu));
8140 }
8141
8142 static void
8143 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8144 {
8145 struct dwarf2_per_objfile *dwarf2_per_objfile
8146 = cu->per_cu->dwarf2_per_objfile;
8147 struct objfile *objfile = dwarf2_per_objfile->objfile;
8148 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8149 CORE_ADDR addr = 0;
8150 const char *actual_name = NULL;
8151 CORE_ADDR baseaddr;
8152
8153 baseaddr = objfile->text_section_offset ();
8154
8155 gdb::unique_xmalloc_ptr<char> built_actual_name
8156 = partial_die_full_name (pdi, cu);
8157 if (built_actual_name != NULL)
8158 actual_name = built_actual_name.get ();
8159
8160 if (actual_name == NULL)
8161 actual_name = pdi->name;
8162
8163 switch (pdi->tag)
8164 {
8165 case DW_TAG_inlined_subroutine:
8166 case DW_TAG_subprogram:
8167 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8168 - baseaddr);
8169 if (pdi->is_external
8170 || cu->language == language_ada
8171 || (cu->language == language_fortran
8172 && pdi->die_parent != NULL
8173 && pdi->die_parent->tag == DW_TAG_subprogram))
8174 {
8175 /* Normally, only "external" DIEs are part of the global scope.
8176 But in Ada and Fortran, we want to be able to access nested
8177 procedures globally. So all Ada and Fortran subprograms are
8178 stored in the global scope. */
8179 add_psymbol_to_list (actual_name,
8180 built_actual_name != NULL,
8181 VAR_DOMAIN, LOC_BLOCK,
8182 SECT_OFF_TEXT (objfile),
8183 psymbol_placement::GLOBAL,
8184 addr,
8185 cu->language, objfile);
8186 }
8187 else
8188 {
8189 add_psymbol_to_list (actual_name,
8190 built_actual_name != NULL,
8191 VAR_DOMAIN, LOC_BLOCK,
8192 SECT_OFF_TEXT (objfile),
8193 psymbol_placement::STATIC,
8194 addr, cu->language, objfile);
8195 }
8196
8197 if (pdi->main_subprogram && actual_name != NULL)
8198 set_objfile_main_name (objfile, actual_name, cu->language);
8199 break;
8200 case DW_TAG_constant:
8201 add_psymbol_to_list (actual_name,
8202 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8203 -1, (pdi->is_external
8204 ? psymbol_placement::GLOBAL
8205 : psymbol_placement::STATIC),
8206 0, cu->language, objfile);
8207 break;
8208 case DW_TAG_variable:
8209 if (pdi->d.locdesc)
8210 addr = decode_locdesc (pdi->d.locdesc, cu);
8211
8212 if (pdi->d.locdesc
8213 && addr == 0
8214 && !dwarf2_per_objfile->has_section_at_zero)
8215 {
8216 /* A global or static variable may also have been stripped
8217 out by the linker if unused, in which case its address
8218 will be nullified; do not add such variables into partial
8219 symbol table then. */
8220 }
8221 else if (pdi->is_external)
8222 {
8223 /* Global Variable.
8224 Don't enter into the minimal symbol tables as there is
8225 a minimal symbol table entry from the ELF symbols already.
8226 Enter into partial symbol table if it has a location
8227 descriptor or a type.
8228 If the location descriptor is missing, new_symbol will create
8229 a LOC_UNRESOLVED symbol, the address of the variable will then
8230 be determined from the minimal symbol table whenever the variable
8231 is referenced.
8232 The address for the partial symbol table entry is not
8233 used by GDB, but it comes in handy for debugging partial symbol
8234 table building. */
8235
8236 if (pdi->d.locdesc || pdi->has_type)
8237 add_psymbol_to_list (actual_name,
8238 built_actual_name != NULL,
8239 VAR_DOMAIN, LOC_STATIC,
8240 SECT_OFF_TEXT (objfile),
8241 psymbol_placement::GLOBAL,
8242 addr, cu->language, objfile);
8243 }
8244 else
8245 {
8246 int has_loc = pdi->d.locdesc != NULL;
8247
8248 /* Static Variable. Skip symbols whose value we cannot know (those
8249 without location descriptors or constant values). */
8250 if (!has_loc && !pdi->has_const_value)
8251 return;
8252
8253 add_psymbol_to_list (actual_name,
8254 built_actual_name != NULL,
8255 VAR_DOMAIN, LOC_STATIC,
8256 SECT_OFF_TEXT (objfile),
8257 psymbol_placement::STATIC,
8258 has_loc ? addr : 0,
8259 cu->language, objfile);
8260 }
8261 break;
8262 case DW_TAG_typedef:
8263 case DW_TAG_base_type:
8264 case DW_TAG_subrange_type:
8265 add_psymbol_to_list (actual_name,
8266 built_actual_name != NULL,
8267 VAR_DOMAIN, LOC_TYPEDEF, -1,
8268 psymbol_placement::STATIC,
8269 0, cu->language, objfile);
8270 break;
8271 case DW_TAG_imported_declaration:
8272 case DW_TAG_namespace:
8273 add_psymbol_to_list (actual_name,
8274 built_actual_name != NULL,
8275 VAR_DOMAIN, LOC_TYPEDEF, -1,
8276 psymbol_placement::GLOBAL,
8277 0, cu->language, objfile);
8278 break;
8279 case DW_TAG_module:
8280 /* With Fortran 77 there might be a "BLOCK DATA" module
8281 available without any name. If so, we skip the module as it
8282 doesn't bring any value. */
8283 if (actual_name != nullptr)
8284 add_psymbol_to_list (actual_name,
8285 built_actual_name != NULL,
8286 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8287 psymbol_placement::GLOBAL,
8288 0, cu->language, objfile);
8289 break;
8290 case DW_TAG_class_type:
8291 case DW_TAG_interface_type:
8292 case DW_TAG_structure_type:
8293 case DW_TAG_union_type:
8294 case DW_TAG_enumeration_type:
8295 /* Skip external references. The DWARF standard says in the section
8296 about "Structure, Union, and Class Type Entries": "An incomplete
8297 structure, union or class type is represented by a structure,
8298 union or class entry that does not have a byte size attribute
8299 and that has a DW_AT_declaration attribute." */
8300 if (!pdi->has_byte_size && pdi->is_declaration)
8301 return;
8302
8303 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8304 static vs. global. */
8305 add_psymbol_to_list (actual_name,
8306 built_actual_name != NULL,
8307 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8308 cu->language == language_cplus
8309 ? psymbol_placement::GLOBAL
8310 : psymbol_placement::STATIC,
8311 0, cu->language, objfile);
8312
8313 break;
8314 case DW_TAG_enumerator:
8315 add_psymbol_to_list (actual_name,
8316 built_actual_name != NULL,
8317 VAR_DOMAIN, LOC_CONST, -1,
8318 cu->language == language_cplus
8319 ? psymbol_placement::GLOBAL
8320 : psymbol_placement::STATIC,
8321 0, cu->language, objfile);
8322 break;
8323 default:
8324 break;
8325 }
8326 }
8327
8328 /* Read a partial die corresponding to a namespace; also, add a symbol
8329 corresponding to that namespace to the symbol table. NAMESPACE is
8330 the name of the enclosing namespace. */
8331
8332 static void
8333 add_partial_namespace (struct partial_die_info *pdi,
8334 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8335 int set_addrmap, struct dwarf2_cu *cu)
8336 {
8337 /* Add a symbol for the namespace. */
8338
8339 add_partial_symbol (pdi, cu);
8340
8341 /* Now scan partial symbols in that namespace. */
8342
8343 if (pdi->has_children)
8344 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8345 }
8346
8347 /* Read a partial die corresponding to a Fortran module. */
8348
8349 static void
8350 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8351 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8352 {
8353 /* Add a symbol for the namespace. */
8354
8355 add_partial_symbol (pdi, cu);
8356
8357 /* Now scan partial symbols in that module. */
8358
8359 if (pdi->has_children)
8360 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8361 }
8362
8363 /* Read a partial die corresponding to a subprogram or an inlined
8364 subprogram and create a partial symbol for that subprogram.
8365 When the CU language allows it, this routine also defines a partial
8366 symbol for each nested subprogram that this subprogram contains.
8367 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8368 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8369
8370 PDI may also be a lexical block, in which case we simply search
8371 recursively for subprograms defined inside that lexical block.
8372 Again, this is only performed when the CU language allows this
8373 type of definitions. */
8374
8375 static void
8376 add_partial_subprogram (struct partial_die_info *pdi,
8377 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8378 int set_addrmap, struct dwarf2_cu *cu)
8379 {
8380 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8381 {
8382 if (pdi->has_pc_info)
8383 {
8384 if (pdi->lowpc < *lowpc)
8385 *lowpc = pdi->lowpc;
8386 if (pdi->highpc > *highpc)
8387 *highpc = pdi->highpc;
8388 if (set_addrmap)
8389 {
8390 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8391 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8392 CORE_ADDR baseaddr;
8393 CORE_ADDR this_highpc;
8394 CORE_ADDR this_lowpc;
8395
8396 baseaddr = objfile->text_section_offset ();
8397 this_lowpc
8398 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8399 pdi->lowpc + baseaddr)
8400 - baseaddr);
8401 this_highpc
8402 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8403 pdi->highpc + baseaddr)
8404 - baseaddr);
8405 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8406 this_lowpc, this_highpc - 1,
8407 cu->per_cu->v.psymtab);
8408 }
8409 }
8410
8411 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8412 {
8413 if (!pdi->is_declaration)
8414 /* Ignore subprogram DIEs that do not have a name, they are
8415 illegal. Do not emit a complaint at this point, we will
8416 do so when we convert this psymtab into a symtab. */
8417 if (pdi->name)
8418 add_partial_symbol (pdi, cu);
8419 }
8420 }
8421
8422 if (! pdi->has_children)
8423 return;
8424
8425 if (cu->language == language_ada || cu->language == language_fortran)
8426 {
8427 pdi = pdi->die_child;
8428 while (pdi != NULL)
8429 {
8430 pdi->fixup (cu);
8431 if (pdi->tag == DW_TAG_subprogram
8432 || pdi->tag == DW_TAG_inlined_subroutine
8433 || pdi->tag == DW_TAG_lexical_block)
8434 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8435 pdi = pdi->die_sibling;
8436 }
8437 }
8438 }
8439
8440 /* Read a partial die corresponding to an enumeration type. */
8441
8442 static void
8443 add_partial_enumeration (struct partial_die_info *enum_pdi,
8444 struct dwarf2_cu *cu)
8445 {
8446 struct partial_die_info *pdi;
8447
8448 if (enum_pdi->name != NULL)
8449 add_partial_symbol (enum_pdi, cu);
8450
8451 pdi = enum_pdi->die_child;
8452 while (pdi)
8453 {
8454 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8455 complaint (_("malformed enumerator DIE ignored"));
8456 else
8457 add_partial_symbol (pdi, cu);
8458 pdi = pdi->die_sibling;
8459 }
8460 }
8461
8462 /* Return the initial uleb128 in the die at INFO_PTR. */
8463
8464 static unsigned int
8465 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8466 {
8467 unsigned int bytes_read;
8468
8469 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8470 }
8471
8472 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8473 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8474
8475 Return the corresponding abbrev, or NULL if the number is zero (indicating
8476 an empty DIE). In either case *BYTES_READ will be set to the length of
8477 the initial number. */
8478
8479 static struct abbrev_info *
8480 peek_die_abbrev (const die_reader_specs &reader,
8481 const gdb_byte *info_ptr, unsigned int *bytes_read)
8482 {
8483 dwarf2_cu *cu = reader.cu;
8484 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8485 unsigned int abbrev_number
8486 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8487
8488 if (abbrev_number == 0)
8489 return NULL;
8490
8491 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8492 if (!abbrev)
8493 {
8494 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8495 " at offset %s [in module %s]"),
8496 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8497 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8498 }
8499
8500 return abbrev;
8501 }
8502
8503 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8504 Returns a pointer to the end of a series of DIEs, terminated by an empty
8505 DIE. Any children of the skipped DIEs will also be skipped. */
8506
8507 static const gdb_byte *
8508 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8509 {
8510 while (1)
8511 {
8512 unsigned int bytes_read;
8513 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8514
8515 if (abbrev == NULL)
8516 return info_ptr + bytes_read;
8517 else
8518 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8519 }
8520 }
8521
8522 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8523 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8524 abbrev corresponding to that skipped uleb128 should be passed in
8525 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8526 children. */
8527
8528 static const gdb_byte *
8529 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8530 struct abbrev_info *abbrev)
8531 {
8532 unsigned int bytes_read;
8533 struct attribute attr;
8534 bfd *abfd = reader->abfd;
8535 struct dwarf2_cu *cu = reader->cu;
8536 const gdb_byte *buffer = reader->buffer;
8537 const gdb_byte *buffer_end = reader->buffer_end;
8538 unsigned int form, i;
8539
8540 for (i = 0; i < abbrev->num_attrs; i++)
8541 {
8542 /* The only abbrev we care about is DW_AT_sibling. */
8543 if (abbrev->attrs[i].name == DW_AT_sibling)
8544 {
8545 bool ignored;
8546 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8547 &ignored);
8548 if (attr.form == DW_FORM_ref_addr)
8549 complaint (_("ignoring absolute DW_AT_sibling"));
8550 else
8551 {
8552 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8553 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8554
8555 if (sibling_ptr < info_ptr)
8556 complaint (_("DW_AT_sibling points backwards"));
8557 else if (sibling_ptr > reader->buffer_end)
8558 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8559 else
8560 return sibling_ptr;
8561 }
8562 }
8563
8564 /* If it isn't DW_AT_sibling, skip this attribute. */
8565 form = abbrev->attrs[i].form;
8566 skip_attribute:
8567 switch (form)
8568 {
8569 case DW_FORM_ref_addr:
8570 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8571 and later it is offset sized. */
8572 if (cu->header.version == 2)
8573 info_ptr += cu->header.addr_size;
8574 else
8575 info_ptr += cu->header.offset_size;
8576 break;
8577 case DW_FORM_GNU_ref_alt:
8578 info_ptr += cu->header.offset_size;
8579 break;
8580 case DW_FORM_addr:
8581 info_ptr += cu->header.addr_size;
8582 break;
8583 case DW_FORM_data1:
8584 case DW_FORM_ref1:
8585 case DW_FORM_flag:
8586 case DW_FORM_strx1:
8587 info_ptr += 1;
8588 break;
8589 case DW_FORM_flag_present:
8590 case DW_FORM_implicit_const:
8591 break;
8592 case DW_FORM_data2:
8593 case DW_FORM_ref2:
8594 case DW_FORM_strx2:
8595 info_ptr += 2;
8596 break;
8597 case DW_FORM_strx3:
8598 info_ptr += 3;
8599 break;
8600 case DW_FORM_data4:
8601 case DW_FORM_ref4:
8602 case DW_FORM_strx4:
8603 info_ptr += 4;
8604 break;
8605 case DW_FORM_data8:
8606 case DW_FORM_ref8:
8607 case DW_FORM_ref_sig8:
8608 info_ptr += 8;
8609 break;
8610 case DW_FORM_data16:
8611 info_ptr += 16;
8612 break;
8613 case DW_FORM_string:
8614 read_direct_string (abfd, info_ptr, &bytes_read);
8615 info_ptr += bytes_read;
8616 break;
8617 case DW_FORM_sec_offset:
8618 case DW_FORM_strp:
8619 case DW_FORM_GNU_strp_alt:
8620 info_ptr += cu->header.offset_size;
8621 break;
8622 case DW_FORM_exprloc:
8623 case DW_FORM_block:
8624 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8625 info_ptr += bytes_read;
8626 break;
8627 case DW_FORM_block1:
8628 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8629 break;
8630 case DW_FORM_block2:
8631 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8632 break;
8633 case DW_FORM_block4:
8634 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8635 break;
8636 case DW_FORM_addrx:
8637 case DW_FORM_strx:
8638 case DW_FORM_sdata:
8639 case DW_FORM_udata:
8640 case DW_FORM_ref_udata:
8641 case DW_FORM_GNU_addr_index:
8642 case DW_FORM_GNU_str_index:
8643 case DW_FORM_rnglistx:
8644 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8645 break;
8646 case DW_FORM_indirect:
8647 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8648 info_ptr += bytes_read;
8649 /* We need to continue parsing from here, so just go back to
8650 the top. */
8651 goto skip_attribute;
8652
8653 default:
8654 error (_("Dwarf Error: Cannot handle %s "
8655 "in DWARF reader [in module %s]"),
8656 dwarf_form_name (form),
8657 bfd_get_filename (abfd));
8658 }
8659 }
8660
8661 if (abbrev->has_children)
8662 return skip_children (reader, info_ptr);
8663 else
8664 return info_ptr;
8665 }
8666
8667 /* Locate ORIG_PDI's sibling.
8668 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8669
8670 static const gdb_byte *
8671 locate_pdi_sibling (const struct die_reader_specs *reader,
8672 struct partial_die_info *orig_pdi,
8673 const gdb_byte *info_ptr)
8674 {
8675 /* Do we know the sibling already? */
8676
8677 if (orig_pdi->sibling)
8678 return orig_pdi->sibling;
8679
8680 /* Are there any children to deal with? */
8681
8682 if (!orig_pdi->has_children)
8683 return info_ptr;
8684
8685 /* Skip the children the long way. */
8686
8687 return skip_children (reader, info_ptr);
8688 }
8689
8690 /* Expand this partial symbol table into a full symbol table. SELF is
8691 not NULL. */
8692
8693 void
8694 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8695 {
8696 struct dwarf2_per_objfile *dwarf2_per_objfile
8697 = get_dwarf2_per_objfile (objfile);
8698
8699 gdb_assert (!readin);
8700 /* If this psymtab is constructed from a debug-only objfile, the
8701 has_section_at_zero flag will not necessarily be correct. We
8702 can get the correct value for this flag by looking at the data
8703 associated with the (presumably stripped) associated objfile. */
8704 if (objfile->separate_debug_objfile_backlink)
8705 {
8706 struct dwarf2_per_objfile *dpo_backlink
8707 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8708
8709 dwarf2_per_objfile->has_section_at_zero
8710 = dpo_backlink->has_section_at_zero;
8711 }
8712
8713 dwarf2_per_objfile->reading_partial_symbols = 0;
8714
8715 expand_psymtab (objfile);
8716
8717 process_cu_includes (dwarf2_per_objfile);
8718 }
8719 \f
8720 /* Reading in full CUs. */
8721
8722 /* Add PER_CU to the queue. */
8723
8724 static void
8725 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8726 enum language pretend_language)
8727 {
8728 per_cu->queued = 1;
8729 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8730 }
8731
8732 /* If PER_CU is not yet queued, add it to the queue.
8733 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8734 dependency.
8735 The result is non-zero if PER_CU was queued, otherwise the result is zero
8736 meaning either PER_CU is already queued or it is already loaded.
8737
8738 N.B. There is an invariant here that if a CU is queued then it is loaded.
8739 The caller is required to load PER_CU if we return non-zero. */
8740
8741 static int
8742 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8743 struct dwarf2_per_cu_data *per_cu,
8744 enum language pretend_language)
8745 {
8746 /* We may arrive here during partial symbol reading, if we need full
8747 DIEs to process an unusual case (e.g. template arguments). Do
8748 not queue PER_CU, just tell our caller to load its DIEs. */
8749 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8750 {
8751 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8752 return 1;
8753 return 0;
8754 }
8755
8756 /* Mark the dependence relation so that we don't flush PER_CU
8757 too early. */
8758 if (dependent_cu != NULL)
8759 dwarf2_add_dependence (dependent_cu, per_cu);
8760
8761 /* If it's already on the queue, we have nothing to do. */
8762 if (per_cu->queued)
8763 return 0;
8764
8765 /* If the compilation unit is already loaded, just mark it as
8766 used. */
8767 if (per_cu->cu != NULL)
8768 {
8769 per_cu->cu->last_used = 0;
8770 return 0;
8771 }
8772
8773 /* Add it to the queue. */
8774 queue_comp_unit (per_cu, pretend_language);
8775
8776 return 1;
8777 }
8778
8779 /* Process the queue. */
8780
8781 static void
8782 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8783 {
8784 if (dwarf_read_debug)
8785 {
8786 fprintf_unfiltered (gdb_stdlog,
8787 "Expanding one or more symtabs of objfile %s ...\n",
8788 objfile_name (dwarf2_per_objfile->objfile));
8789 }
8790
8791 /* The queue starts out with one item, but following a DIE reference
8792 may load a new CU, adding it to the end of the queue. */
8793 while (!dwarf2_per_objfile->queue.empty ())
8794 {
8795 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8796
8797 if ((dwarf2_per_objfile->using_index
8798 ? !item.per_cu->v.quick->compunit_symtab
8799 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8800 /* Skip dummy CUs. */
8801 && item.per_cu->cu != NULL)
8802 {
8803 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8804 unsigned int debug_print_threshold;
8805 char buf[100];
8806
8807 if (per_cu->is_debug_types)
8808 {
8809 struct signatured_type *sig_type =
8810 (struct signatured_type *) per_cu;
8811
8812 sprintf (buf, "TU %s at offset %s",
8813 hex_string (sig_type->signature),
8814 sect_offset_str (per_cu->sect_off));
8815 /* There can be 100s of TUs.
8816 Only print them in verbose mode. */
8817 debug_print_threshold = 2;
8818 }
8819 else
8820 {
8821 sprintf (buf, "CU at offset %s",
8822 sect_offset_str (per_cu->sect_off));
8823 debug_print_threshold = 1;
8824 }
8825
8826 if (dwarf_read_debug >= debug_print_threshold)
8827 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8828
8829 if (per_cu->is_debug_types)
8830 process_full_type_unit (per_cu, item.pretend_language);
8831 else
8832 process_full_comp_unit (per_cu, item.pretend_language);
8833
8834 if (dwarf_read_debug >= debug_print_threshold)
8835 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8836 }
8837
8838 item.per_cu->queued = 0;
8839 dwarf2_per_objfile->queue.pop ();
8840 }
8841
8842 if (dwarf_read_debug)
8843 {
8844 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8845 objfile_name (dwarf2_per_objfile->objfile));
8846 }
8847 }
8848
8849 /* Read in full symbols for PST, and anything it depends on. */
8850
8851 void
8852 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8853 {
8854 struct dwarf2_per_cu_data *per_cu;
8855
8856 if (readin)
8857 return;
8858
8859 read_dependencies (objfile);
8860
8861 per_cu = per_cu_data;
8862
8863 if (per_cu == NULL)
8864 {
8865 /* It's an include file, no symbols to read for it.
8866 Everything is in the parent symtab. */
8867 readin = true;
8868 return;
8869 }
8870
8871 dw2_do_instantiate_symtab (per_cu, false);
8872 }
8873
8874 /* Trivial hash function for die_info: the hash value of a DIE
8875 is its offset in .debug_info for this objfile. */
8876
8877 static hashval_t
8878 die_hash (const void *item)
8879 {
8880 const struct die_info *die = (const struct die_info *) item;
8881
8882 return to_underlying (die->sect_off);
8883 }
8884
8885 /* Trivial comparison function for die_info structures: two DIEs
8886 are equal if they have the same offset. */
8887
8888 static int
8889 die_eq (const void *item_lhs, const void *item_rhs)
8890 {
8891 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8892 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8893
8894 return die_lhs->sect_off == die_rhs->sect_off;
8895 }
8896
8897 /* Load the DIEs associated with PER_CU into memory. */
8898
8899 static void
8900 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8901 bool skip_partial,
8902 enum language pretend_language)
8903 {
8904 gdb_assert (! this_cu->is_debug_types);
8905
8906 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
8907 if (reader.dummy_p)
8908 return;
8909
8910 struct dwarf2_cu *cu = reader.cu;
8911 const gdb_byte *info_ptr = reader.info_ptr;
8912
8913 gdb_assert (cu->die_hash == NULL);
8914 cu->die_hash =
8915 htab_create_alloc_ex (cu->header.length / 12,
8916 die_hash,
8917 die_eq,
8918 NULL,
8919 &cu->comp_unit_obstack,
8920 hashtab_obstack_allocate,
8921 dummy_obstack_deallocate);
8922
8923 if (reader.comp_unit_die->has_children)
8924 reader.comp_unit_die->child
8925 = read_die_and_siblings (&reader, reader.info_ptr,
8926 &info_ptr, reader.comp_unit_die);
8927 cu->dies = reader.comp_unit_die;
8928 /* comp_unit_die is not stored in die_hash, no need. */
8929
8930 /* We try not to read any attributes in this function, because not
8931 all CUs needed for references have been loaded yet, and symbol
8932 table processing isn't initialized. But we have to set the CU language,
8933 or we won't be able to build types correctly.
8934 Similarly, if we do not read the producer, we can not apply
8935 producer-specific interpretation. */
8936 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8937 }
8938
8939 /* Add a DIE to the delayed physname list. */
8940
8941 static void
8942 add_to_method_list (struct type *type, int fnfield_index, int index,
8943 const char *name, struct die_info *die,
8944 struct dwarf2_cu *cu)
8945 {
8946 struct delayed_method_info mi;
8947 mi.type = type;
8948 mi.fnfield_index = fnfield_index;
8949 mi.index = index;
8950 mi.name = name;
8951 mi.die = die;
8952 cu->method_list.push_back (mi);
8953 }
8954
8955 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8956 "const" / "volatile". If so, decrements LEN by the length of the
8957 modifier and return true. Otherwise return false. */
8958
8959 template<size_t N>
8960 static bool
8961 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8962 {
8963 size_t mod_len = sizeof (mod) - 1;
8964 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8965 {
8966 len -= mod_len;
8967 return true;
8968 }
8969 return false;
8970 }
8971
8972 /* Compute the physnames of any methods on the CU's method list.
8973
8974 The computation of method physnames is delayed in order to avoid the
8975 (bad) condition that one of the method's formal parameters is of an as yet
8976 incomplete type. */
8977
8978 static void
8979 compute_delayed_physnames (struct dwarf2_cu *cu)
8980 {
8981 /* Only C++ delays computing physnames. */
8982 if (cu->method_list.empty ())
8983 return;
8984 gdb_assert (cu->language == language_cplus);
8985
8986 for (const delayed_method_info &mi : cu->method_list)
8987 {
8988 const char *physname;
8989 struct fn_fieldlist *fn_flp
8990 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8991 physname = dwarf2_physname (mi.name, mi.die, cu);
8992 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8993 = physname ? physname : "";
8994
8995 /* Since there's no tag to indicate whether a method is a
8996 const/volatile overload, extract that information out of the
8997 demangled name. */
8998 if (physname != NULL)
8999 {
9000 size_t len = strlen (physname);
9001
9002 while (1)
9003 {
9004 if (physname[len] == ')') /* shortcut */
9005 break;
9006 else if (check_modifier (physname, len, " const"))
9007 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9008 else if (check_modifier (physname, len, " volatile"))
9009 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9010 else
9011 break;
9012 }
9013 }
9014 }
9015
9016 /* The list is no longer needed. */
9017 cu->method_list.clear ();
9018 }
9019
9020 /* Go objects should be embedded in a DW_TAG_module DIE,
9021 and it's not clear if/how imported objects will appear.
9022 To keep Go support simple until that's worked out,
9023 go back through what we've read and create something usable.
9024 We could do this while processing each DIE, and feels kinda cleaner,
9025 but that way is more invasive.
9026 This is to, for example, allow the user to type "p var" or "b main"
9027 without having to specify the package name, and allow lookups
9028 of module.object to work in contexts that use the expression
9029 parser. */
9030
9031 static void
9032 fixup_go_packaging (struct dwarf2_cu *cu)
9033 {
9034 gdb::unique_xmalloc_ptr<char> package_name;
9035 struct pending *list;
9036 int i;
9037
9038 for (list = *cu->get_builder ()->get_global_symbols ();
9039 list != NULL;
9040 list = list->next)
9041 {
9042 for (i = 0; i < list->nsyms; ++i)
9043 {
9044 struct symbol *sym = list->symbol[i];
9045
9046 if (sym->language () == language_go
9047 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9048 {
9049 gdb::unique_xmalloc_ptr<char> this_package_name
9050 (go_symbol_package_name (sym));
9051
9052 if (this_package_name == NULL)
9053 continue;
9054 if (package_name == NULL)
9055 package_name = std::move (this_package_name);
9056 else
9057 {
9058 struct objfile *objfile
9059 = cu->per_cu->dwarf2_per_objfile->objfile;
9060 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9061 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9062 (symbol_symtab (sym) != NULL
9063 ? symtab_to_filename_for_display
9064 (symbol_symtab (sym))
9065 : objfile_name (objfile)),
9066 this_package_name.get (), package_name.get ());
9067 }
9068 }
9069 }
9070 }
9071
9072 if (package_name != NULL)
9073 {
9074 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9075 const char *saved_package_name
9076 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9077 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9078 saved_package_name);
9079 struct symbol *sym;
9080
9081 sym = allocate_symbol (objfile);
9082 sym->set_language (language_go, &objfile->objfile_obstack);
9083 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9084 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9085 e.g., "main" finds the "main" module and not C's main(). */
9086 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9087 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9088 SYMBOL_TYPE (sym) = type;
9089
9090 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9091 }
9092 }
9093
9094 /* Allocate a fully-qualified name consisting of the two parts on the
9095 obstack. */
9096
9097 static const char *
9098 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9099 {
9100 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9101 }
9102
9103 /* A helper that allocates a struct discriminant_info to attach to a
9104 union type. */
9105
9106 static struct discriminant_info *
9107 alloc_discriminant_info (struct type *type, int discriminant_index,
9108 int default_index)
9109 {
9110 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9111 gdb_assert (discriminant_index == -1
9112 || (discriminant_index >= 0
9113 && discriminant_index < TYPE_NFIELDS (type)));
9114 gdb_assert (default_index == -1
9115 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9116
9117 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9118
9119 struct discriminant_info *disc
9120 = ((struct discriminant_info *)
9121 TYPE_ZALLOC (type,
9122 offsetof (struct discriminant_info, discriminants)
9123 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9124 disc->default_index = default_index;
9125 disc->discriminant_index = discriminant_index;
9126
9127 struct dynamic_prop prop;
9128 prop.kind = PROP_UNDEFINED;
9129 prop.data.baton = disc;
9130
9131 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9132
9133 return disc;
9134 }
9135
9136 /* Some versions of rustc emitted enums in an unusual way.
9137
9138 Ordinary enums were emitted as unions. The first element of each
9139 structure in the union was named "RUST$ENUM$DISR". This element
9140 held the discriminant.
9141
9142 These versions of Rust also implemented the "non-zero"
9143 optimization. When the enum had two values, and one is empty and
9144 the other holds a pointer that cannot be zero, the pointer is used
9145 as the discriminant, with a zero value meaning the empty variant.
9146 Here, the union's first member is of the form
9147 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9148 where the fieldnos are the indices of the fields that should be
9149 traversed in order to find the field (which may be several fields deep)
9150 and the variantname is the name of the variant of the case when the
9151 field is zero.
9152
9153 This function recognizes whether TYPE is of one of these forms,
9154 and, if so, smashes it to be a variant type. */
9155
9156 static void
9157 quirk_rust_enum (struct type *type, struct objfile *objfile)
9158 {
9159 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9160
9161 /* We don't need to deal with empty enums. */
9162 if (TYPE_NFIELDS (type) == 0)
9163 return;
9164
9165 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9166 if (TYPE_NFIELDS (type) == 1
9167 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9168 {
9169 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9170
9171 /* Decode the field name to find the offset of the
9172 discriminant. */
9173 ULONGEST bit_offset = 0;
9174 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9175 while (name[0] >= '0' && name[0] <= '9')
9176 {
9177 char *tail;
9178 unsigned long index = strtoul (name, &tail, 10);
9179 name = tail;
9180 if (*name != '$'
9181 || index >= TYPE_NFIELDS (field_type)
9182 || (TYPE_FIELD_LOC_KIND (field_type, index)
9183 != FIELD_LOC_KIND_BITPOS))
9184 {
9185 complaint (_("Could not parse Rust enum encoding string \"%s\""
9186 "[in module %s]"),
9187 TYPE_FIELD_NAME (type, 0),
9188 objfile_name (objfile));
9189 return;
9190 }
9191 ++name;
9192
9193 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9194 field_type = TYPE_FIELD_TYPE (field_type, index);
9195 }
9196
9197 /* Make a union to hold the variants. */
9198 struct type *union_type = alloc_type (objfile);
9199 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9200 TYPE_NFIELDS (union_type) = 3;
9201 TYPE_FIELDS (union_type)
9202 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9203 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9204 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9205
9206 /* Put the discriminant must at index 0. */
9207 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9208 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9209 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9210 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9211
9212 /* The order of fields doesn't really matter, so put the real
9213 field at index 1 and the data-less field at index 2. */
9214 struct discriminant_info *disc
9215 = alloc_discriminant_info (union_type, 0, 1);
9216 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9217 TYPE_FIELD_NAME (union_type, 1)
9218 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9219 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9220 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9221 TYPE_FIELD_NAME (union_type, 1));
9222
9223 const char *dataless_name
9224 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9225 name);
9226 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9227 dataless_name);
9228 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9229 /* NAME points into the original discriminant name, which
9230 already has the correct lifetime. */
9231 TYPE_FIELD_NAME (union_type, 2) = name;
9232 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9233 disc->discriminants[2] = 0;
9234
9235 /* Smash this type to be a structure type. We have to do this
9236 because the type has already been recorded. */
9237 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9238 TYPE_NFIELDS (type) = 1;
9239 TYPE_FIELDS (type)
9240 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9241
9242 /* Install the variant part. */
9243 TYPE_FIELD_TYPE (type, 0) = union_type;
9244 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9245 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9246 }
9247 /* A union with a single anonymous field is probably an old-style
9248 univariant enum. */
9249 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9250 {
9251 /* Smash this type to be a structure type. We have to do this
9252 because the type has already been recorded. */
9253 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9254
9255 /* Make a union to hold the variants. */
9256 struct type *union_type = alloc_type (objfile);
9257 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9258 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9259 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9260 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9261 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9262
9263 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9264 const char *variant_name
9265 = rust_last_path_segment (TYPE_NAME (field_type));
9266 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9267 TYPE_NAME (field_type)
9268 = rust_fully_qualify (&objfile->objfile_obstack,
9269 TYPE_NAME (type), variant_name);
9270
9271 /* Install the union in the outer struct type. */
9272 TYPE_NFIELDS (type) = 1;
9273 TYPE_FIELDS (type)
9274 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9275 TYPE_FIELD_TYPE (type, 0) = union_type;
9276 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9277 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9278
9279 alloc_discriminant_info (union_type, -1, 0);
9280 }
9281 else
9282 {
9283 struct type *disr_type = nullptr;
9284 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9285 {
9286 disr_type = TYPE_FIELD_TYPE (type, i);
9287
9288 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9289 {
9290 /* All fields of a true enum will be structs. */
9291 return;
9292 }
9293 else if (TYPE_NFIELDS (disr_type) == 0)
9294 {
9295 /* Could be data-less variant, so keep going. */
9296 disr_type = nullptr;
9297 }
9298 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9299 "RUST$ENUM$DISR") != 0)
9300 {
9301 /* Not a Rust enum. */
9302 return;
9303 }
9304 else
9305 {
9306 /* Found one. */
9307 break;
9308 }
9309 }
9310
9311 /* If we got here without a discriminant, then it's probably
9312 just a union. */
9313 if (disr_type == nullptr)
9314 return;
9315
9316 /* Smash this type to be a structure type. We have to do this
9317 because the type has already been recorded. */
9318 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9319
9320 /* Make a union to hold the variants. */
9321 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9322 struct type *union_type = alloc_type (objfile);
9323 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9324 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9325 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9326 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9327 TYPE_FIELDS (union_type)
9328 = (struct field *) TYPE_ZALLOC (union_type,
9329 (TYPE_NFIELDS (union_type)
9330 * sizeof (struct field)));
9331
9332 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9333 TYPE_NFIELDS (type) * sizeof (struct field));
9334
9335 /* Install the discriminant at index 0 in the union. */
9336 TYPE_FIELD (union_type, 0) = *disr_field;
9337 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9338 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9339
9340 /* Install the union in the outer struct type. */
9341 TYPE_FIELD_TYPE (type, 0) = union_type;
9342 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9343 TYPE_NFIELDS (type) = 1;
9344
9345 /* Set the size and offset of the union type. */
9346 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9347
9348 /* We need a way to find the correct discriminant given a
9349 variant name. For convenience we build a map here. */
9350 struct type *enum_type = FIELD_TYPE (*disr_field);
9351 std::unordered_map<std::string, ULONGEST> discriminant_map;
9352 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9353 {
9354 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9355 {
9356 const char *name
9357 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9358 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9359 }
9360 }
9361
9362 int n_fields = TYPE_NFIELDS (union_type);
9363 struct discriminant_info *disc
9364 = alloc_discriminant_info (union_type, 0, -1);
9365 /* Skip the discriminant here. */
9366 for (int i = 1; i < n_fields; ++i)
9367 {
9368 /* Find the final word in the name of this variant's type.
9369 That name can be used to look up the correct
9370 discriminant. */
9371 const char *variant_name
9372 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9373 i)));
9374
9375 auto iter = discriminant_map.find (variant_name);
9376 if (iter != discriminant_map.end ())
9377 disc->discriminants[i] = iter->second;
9378
9379 /* Remove the discriminant field, if it exists. */
9380 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9381 if (TYPE_NFIELDS (sub_type) > 0)
9382 {
9383 --TYPE_NFIELDS (sub_type);
9384 ++TYPE_FIELDS (sub_type);
9385 }
9386 TYPE_FIELD_NAME (union_type, i) = variant_name;
9387 TYPE_NAME (sub_type)
9388 = rust_fully_qualify (&objfile->objfile_obstack,
9389 TYPE_NAME (type), variant_name);
9390 }
9391 }
9392 }
9393
9394 /* Rewrite some Rust unions to be structures with variants parts. */
9395
9396 static void
9397 rust_union_quirks (struct dwarf2_cu *cu)
9398 {
9399 gdb_assert (cu->language == language_rust);
9400 for (type *type_ : cu->rust_unions)
9401 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9402 /* We don't need this any more. */
9403 cu->rust_unions.clear ();
9404 }
9405
9406 /* Return the symtab for PER_CU. This works properly regardless of
9407 whether we're using the index or psymtabs. */
9408
9409 static struct compunit_symtab *
9410 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9411 {
9412 return (per_cu->dwarf2_per_objfile->using_index
9413 ? per_cu->v.quick->compunit_symtab
9414 : per_cu->v.psymtab->compunit_symtab);
9415 }
9416
9417 /* A helper function for computing the list of all symbol tables
9418 included by PER_CU. */
9419
9420 static void
9421 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9422 htab_t all_children, htab_t all_type_symtabs,
9423 struct dwarf2_per_cu_data *per_cu,
9424 struct compunit_symtab *immediate_parent)
9425 {
9426 void **slot;
9427 struct compunit_symtab *cust;
9428
9429 slot = htab_find_slot (all_children, per_cu, INSERT);
9430 if (*slot != NULL)
9431 {
9432 /* This inclusion and its children have been processed. */
9433 return;
9434 }
9435
9436 *slot = per_cu;
9437 /* Only add a CU if it has a symbol table. */
9438 cust = get_compunit_symtab (per_cu);
9439 if (cust != NULL)
9440 {
9441 /* If this is a type unit only add its symbol table if we haven't
9442 seen it yet (type unit per_cu's can share symtabs). */
9443 if (per_cu->is_debug_types)
9444 {
9445 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9446 if (*slot == NULL)
9447 {
9448 *slot = cust;
9449 result->push_back (cust);
9450 if (cust->user == NULL)
9451 cust->user = immediate_parent;
9452 }
9453 }
9454 else
9455 {
9456 result->push_back (cust);
9457 if (cust->user == NULL)
9458 cust->user = immediate_parent;
9459 }
9460 }
9461
9462 if (!per_cu->imported_symtabs_empty ())
9463 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9464 {
9465 recursively_compute_inclusions (result, all_children,
9466 all_type_symtabs, ptr, cust);
9467 }
9468 }
9469
9470 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9471 PER_CU. */
9472
9473 static void
9474 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9475 {
9476 gdb_assert (! per_cu->is_debug_types);
9477
9478 if (!per_cu->imported_symtabs_empty ())
9479 {
9480 int len;
9481 std::vector<compunit_symtab *> result_symtabs;
9482 htab_t all_children, all_type_symtabs;
9483 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9484
9485 /* If we don't have a symtab, we can just skip this case. */
9486 if (cust == NULL)
9487 return;
9488
9489 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9490 NULL, xcalloc, xfree);
9491 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9492 NULL, xcalloc, xfree);
9493
9494 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9495 {
9496 recursively_compute_inclusions (&result_symtabs, all_children,
9497 all_type_symtabs, ptr, cust);
9498 }
9499
9500 /* Now we have a transitive closure of all the included symtabs. */
9501 len = result_symtabs.size ();
9502 cust->includes
9503 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9504 struct compunit_symtab *, len + 1);
9505 memcpy (cust->includes, result_symtabs.data (),
9506 len * sizeof (compunit_symtab *));
9507 cust->includes[len] = NULL;
9508
9509 htab_delete (all_children);
9510 htab_delete (all_type_symtabs);
9511 }
9512 }
9513
9514 /* Compute the 'includes' field for the symtabs of all the CUs we just
9515 read. */
9516
9517 static void
9518 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9519 {
9520 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9521 {
9522 if (! iter->is_debug_types)
9523 compute_compunit_symtab_includes (iter);
9524 }
9525
9526 dwarf2_per_objfile->just_read_cus.clear ();
9527 }
9528
9529 /* Generate full symbol information for PER_CU, whose DIEs have
9530 already been loaded into memory. */
9531
9532 static void
9533 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9534 enum language pretend_language)
9535 {
9536 struct dwarf2_cu *cu = per_cu->cu;
9537 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9538 struct objfile *objfile = dwarf2_per_objfile->objfile;
9539 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9540 CORE_ADDR lowpc, highpc;
9541 struct compunit_symtab *cust;
9542 CORE_ADDR baseaddr;
9543 struct block *static_block;
9544 CORE_ADDR addr;
9545
9546 baseaddr = objfile->text_section_offset ();
9547
9548 /* Clear the list here in case something was left over. */
9549 cu->method_list.clear ();
9550
9551 cu->language = pretend_language;
9552 cu->language_defn = language_def (cu->language);
9553
9554 /* Do line number decoding in read_file_scope () */
9555 process_die (cu->dies, cu);
9556
9557 /* For now fudge the Go package. */
9558 if (cu->language == language_go)
9559 fixup_go_packaging (cu);
9560
9561 /* Now that we have processed all the DIEs in the CU, all the types
9562 should be complete, and it should now be safe to compute all of the
9563 physnames. */
9564 compute_delayed_physnames (cu);
9565
9566 if (cu->language == language_rust)
9567 rust_union_quirks (cu);
9568
9569 /* Some compilers don't define a DW_AT_high_pc attribute for the
9570 compilation unit. If the DW_AT_high_pc is missing, synthesize
9571 it, by scanning the DIE's below the compilation unit. */
9572 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9573
9574 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9575 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9576
9577 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9578 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9579 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9580 addrmap to help ensure it has an accurate map of pc values belonging to
9581 this comp unit. */
9582 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9583
9584 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9585 SECT_OFF_TEXT (objfile),
9586 0);
9587
9588 if (cust != NULL)
9589 {
9590 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9591
9592 /* Set symtab language to language from DW_AT_language. If the
9593 compilation is from a C file generated by language preprocessors, do
9594 not set the language if it was already deduced by start_subfile. */
9595 if (!(cu->language == language_c
9596 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9597 COMPUNIT_FILETABS (cust)->language = cu->language;
9598
9599 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9600 produce DW_AT_location with location lists but it can be possibly
9601 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9602 there were bugs in prologue debug info, fixed later in GCC-4.5
9603 by "unwind info for epilogues" patch (which is not directly related).
9604
9605 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9606 needed, it would be wrong due to missing DW_AT_producer there.
9607
9608 Still one can confuse GDB by using non-standard GCC compilation
9609 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9610 */
9611 if (cu->has_loclist && gcc_4_minor >= 5)
9612 cust->locations_valid = 1;
9613
9614 if (gcc_4_minor >= 5)
9615 cust->epilogue_unwind_valid = 1;
9616
9617 cust->call_site_htab = cu->call_site_htab;
9618 }
9619
9620 if (dwarf2_per_objfile->using_index)
9621 per_cu->v.quick->compunit_symtab = cust;
9622 else
9623 {
9624 dwarf2_psymtab *pst = per_cu->v.psymtab;
9625 pst->compunit_symtab = cust;
9626 pst->readin = true;
9627 }
9628
9629 /* Push it for inclusion processing later. */
9630 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9631
9632 /* Not needed any more. */
9633 cu->reset_builder ();
9634 }
9635
9636 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9637 already been loaded into memory. */
9638
9639 static void
9640 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9641 enum language pretend_language)
9642 {
9643 struct dwarf2_cu *cu = per_cu->cu;
9644 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9645 struct objfile *objfile = dwarf2_per_objfile->objfile;
9646 struct compunit_symtab *cust;
9647 struct signatured_type *sig_type;
9648
9649 gdb_assert (per_cu->is_debug_types);
9650 sig_type = (struct signatured_type *) per_cu;
9651
9652 /* Clear the list here in case something was left over. */
9653 cu->method_list.clear ();
9654
9655 cu->language = pretend_language;
9656 cu->language_defn = language_def (cu->language);
9657
9658 /* The symbol tables are set up in read_type_unit_scope. */
9659 process_die (cu->dies, cu);
9660
9661 /* For now fudge the Go package. */
9662 if (cu->language == language_go)
9663 fixup_go_packaging (cu);
9664
9665 /* Now that we have processed all the DIEs in the CU, all the types
9666 should be complete, and it should now be safe to compute all of the
9667 physnames. */
9668 compute_delayed_physnames (cu);
9669
9670 if (cu->language == language_rust)
9671 rust_union_quirks (cu);
9672
9673 /* TUs share symbol tables.
9674 If this is the first TU to use this symtab, complete the construction
9675 of it with end_expandable_symtab. Otherwise, complete the addition of
9676 this TU's symbols to the existing symtab. */
9677 if (sig_type->type_unit_group->compunit_symtab == NULL)
9678 {
9679 buildsym_compunit *builder = cu->get_builder ();
9680 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9681 sig_type->type_unit_group->compunit_symtab = cust;
9682
9683 if (cust != NULL)
9684 {
9685 /* Set symtab language to language from DW_AT_language. If the
9686 compilation is from a C file generated by language preprocessors,
9687 do not set the language if it was already deduced by
9688 start_subfile. */
9689 if (!(cu->language == language_c
9690 && COMPUNIT_FILETABS (cust)->language != language_c))
9691 COMPUNIT_FILETABS (cust)->language = cu->language;
9692 }
9693 }
9694 else
9695 {
9696 cu->get_builder ()->augment_type_symtab ();
9697 cust = sig_type->type_unit_group->compunit_symtab;
9698 }
9699
9700 if (dwarf2_per_objfile->using_index)
9701 per_cu->v.quick->compunit_symtab = cust;
9702 else
9703 {
9704 dwarf2_psymtab *pst = per_cu->v.psymtab;
9705 pst->compunit_symtab = cust;
9706 pst->readin = true;
9707 }
9708
9709 /* Not needed any more. */
9710 cu->reset_builder ();
9711 }
9712
9713 /* Process an imported unit DIE. */
9714
9715 static void
9716 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9717 {
9718 struct attribute *attr;
9719
9720 /* For now we don't handle imported units in type units. */
9721 if (cu->per_cu->is_debug_types)
9722 {
9723 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9724 " supported in type units [in module %s]"),
9725 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9726 }
9727
9728 attr = dwarf2_attr (die, DW_AT_import, cu);
9729 if (attr != NULL)
9730 {
9731 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9732 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9733 dwarf2_per_cu_data *per_cu
9734 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9735 cu->per_cu->dwarf2_per_objfile);
9736
9737 /* If necessary, add it to the queue and load its DIEs. */
9738 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9739 load_full_comp_unit (per_cu, false, cu->language);
9740
9741 cu->per_cu->imported_symtabs_push (per_cu);
9742 }
9743 }
9744
9745 /* RAII object that represents a process_die scope: i.e.,
9746 starts/finishes processing a DIE. */
9747 class process_die_scope
9748 {
9749 public:
9750 process_die_scope (die_info *die, dwarf2_cu *cu)
9751 : m_die (die), m_cu (cu)
9752 {
9753 /* We should only be processing DIEs not already in process. */
9754 gdb_assert (!m_die->in_process);
9755 m_die->in_process = true;
9756 }
9757
9758 ~process_die_scope ()
9759 {
9760 m_die->in_process = false;
9761
9762 /* If we're done processing the DIE for the CU that owns the line
9763 header, we don't need the line header anymore. */
9764 if (m_cu->line_header_die_owner == m_die)
9765 {
9766 delete m_cu->line_header;
9767 m_cu->line_header = NULL;
9768 m_cu->line_header_die_owner = NULL;
9769 }
9770 }
9771
9772 private:
9773 die_info *m_die;
9774 dwarf2_cu *m_cu;
9775 };
9776
9777 /* Process a die and its children. */
9778
9779 static void
9780 process_die (struct die_info *die, struct dwarf2_cu *cu)
9781 {
9782 process_die_scope scope (die, cu);
9783
9784 switch (die->tag)
9785 {
9786 case DW_TAG_padding:
9787 break;
9788 case DW_TAG_compile_unit:
9789 case DW_TAG_partial_unit:
9790 read_file_scope (die, cu);
9791 break;
9792 case DW_TAG_type_unit:
9793 read_type_unit_scope (die, cu);
9794 break;
9795 case DW_TAG_subprogram:
9796 /* Nested subprograms in Fortran get a prefix. */
9797 if (cu->language == language_fortran
9798 && die->parent != NULL
9799 && die->parent->tag == DW_TAG_subprogram)
9800 cu->processing_has_namespace_info = true;
9801 /* Fall through. */
9802 case DW_TAG_inlined_subroutine:
9803 read_func_scope (die, cu);
9804 break;
9805 case DW_TAG_lexical_block:
9806 case DW_TAG_try_block:
9807 case DW_TAG_catch_block:
9808 read_lexical_block_scope (die, cu);
9809 break;
9810 case DW_TAG_call_site:
9811 case DW_TAG_GNU_call_site:
9812 read_call_site_scope (die, cu);
9813 break;
9814 case DW_TAG_class_type:
9815 case DW_TAG_interface_type:
9816 case DW_TAG_structure_type:
9817 case DW_TAG_union_type:
9818 process_structure_scope (die, cu);
9819 break;
9820 case DW_TAG_enumeration_type:
9821 process_enumeration_scope (die, cu);
9822 break;
9823
9824 /* These dies have a type, but processing them does not create
9825 a symbol or recurse to process the children. Therefore we can
9826 read them on-demand through read_type_die. */
9827 case DW_TAG_subroutine_type:
9828 case DW_TAG_set_type:
9829 case DW_TAG_array_type:
9830 case DW_TAG_pointer_type:
9831 case DW_TAG_ptr_to_member_type:
9832 case DW_TAG_reference_type:
9833 case DW_TAG_rvalue_reference_type:
9834 case DW_TAG_string_type:
9835 break;
9836
9837 case DW_TAG_base_type:
9838 case DW_TAG_subrange_type:
9839 case DW_TAG_typedef:
9840 /* Add a typedef symbol for the type definition, if it has a
9841 DW_AT_name. */
9842 new_symbol (die, read_type_die (die, cu), cu);
9843 break;
9844 case DW_TAG_common_block:
9845 read_common_block (die, cu);
9846 break;
9847 case DW_TAG_common_inclusion:
9848 break;
9849 case DW_TAG_namespace:
9850 cu->processing_has_namespace_info = true;
9851 read_namespace (die, cu);
9852 break;
9853 case DW_TAG_module:
9854 cu->processing_has_namespace_info = true;
9855 read_module (die, cu);
9856 break;
9857 case DW_TAG_imported_declaration:
9858 cu->processing_has_namespace_info = true;
9859 if (read_namespace_alias (die, cu))
9860 break;
9861 /* The declaration is not a global namespace alias. */
9862 /* Fall through. */
9863 case DW_TAG_imported_module:
9864 cu->processing_has_namespace_info = true;
9865 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9866 || cu->language != language_fortran))
9867 complaint (_("Tag '%s' has unexpected children"),
9868 dwarf_tag_name (die->tag));
9869 read_import_statement (die, cu);
9870 break;
9871
9872 case DW_TAG_imported_unit:
9873 process_imported_unit_die (die, cu);
9874 break;
9875
9876 case DW_TAG_variable:
9877 read_variable (die, cu);
9878 break;
9879
9880 default:
9881 new_symbol (die, NULL, cu);
9882 break;
9883 }
9884 }
9885 \f
9886 /* DWARF name computation. */
9887
9888 /* A helper function for dwarf2_compute_name which determines whether DIE
9889 needs to have the name of the scope prepended to the name listed in the
9890 die. */
9891
9892 static int
9893 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9894 {
9895 struct attribute *attr;
9896
9897 switch (die->tag)
9898 {
9899 case DW_TAG_namespace:
9900 case DW_TAG_typedef:
9901 case DW_TAG_class_type:
9902 case DW_TAG_interface_type:
9903 case DW_TAG_structure_type:
9904 case DW_TAG_union_type:
9905 case DW_TAG_enumeration_type:
9906 case DW_TAG_enumerator:
9907 case DW_TAG_subprogram:
9908 case DW_TAG_inlined_subroutine:
9909 case DW_TAG_member:
9910 case DW_TAG_imported_declaration:
9911 return 1;
9912
9913 case DW_TAG_variable:
9914 case DW_TAG_constant:
9915 /* We only need to prefix "globally" visible variables. These include
9916 any variable marked with DW_AT_external or any variable that
9917 lives in a namespace. [Variables in anonymous namespaces
9918 require prefixing, but they are not DW_AT_external.] */
9919
9920 if (dwarf2_attr (die, DW_AT_specification, cu))
9921 {
9922 struct dwarf2_cu *spec_cu = cu;
9923
9924 return die_needs_namespace (die_specification (die, &spec_cu),
9925 spec_cu);
9926 }
9927
9928 attr = dwarf2_attr (die, DW_AT_external, cu);
9929 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9930 && die->parent->tag != DW_TAG_module)
9931 return 0;
9932 /* A variable in a lexical block of some kind does not need a
9933 namespace, even though in C++ such variables may be external
9934 and have a mangled name. */
9935 if (die->parent->tag == DW_TAG_lexical_block
9936 || die->parent->tag == DW_TAG_try_block
9937 || die->parent->tag == DW_TAG_catch_block
9938 || die->parent->tag == DW_TAG_subprogram)
9939 return 0;
9940 return 1;
9941
9942 default:
9943 return 0;
9944 }
9945 }
9946
9947 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9948 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9949 defined for the given DIE. */
9950
9951 static struct attribute *
9952 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9953 {
9954 struct attribute *attr;
9955
9956 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9957 if (attr == NULL)
9958 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9959
9960 return attr;
9961 }
9962
9963 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9964 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9965 defined for the given DIE. */
9966
9967 static const char *
9968 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9969 {
9970 const char *linkage_name;
9971
9972 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9973 if (linkage_name == NULL)
9974 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9975
9976 return linkage_name;
9977 }
9978
9979 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9980 compute the physname for the object, which include a method's:
9981 - formal parameters (C++),
9982 - receiver type (Go),
9983
9984 The term "physname" is a bit confusing.
9985 For C++, for example, it is the demangled name.
9986 For Go, for example, it's the mangled name.
9987
9988 For Ada, return the DIE's linkage name rather than the fully qualified
9989 name. PHYSNAME is ignored..
9990
9991 The result is allocated on the objfile_obstack and canonicalized. */
9992
9993 static const char *
9994 dwarf2_compute_name (const char *name,
9995 struct die_info *die, struct dwarf2_cu *cu,
9996 int physname)
9997 {
9998 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9999
10000 if (name == NULL)
10001 name = dwarf2_name (die, cu);
10002
10003 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10004 but otherwise compute it by typename_concat inside GDB.
10005 FIXME: Actually this is not really true, or at least not always true.
10006 It's all very confusing. compute_and_set_names doesn't try to demangle
10007 Fortran names because there is no mangling standard. So new_symbol
10008 will set the demangled name to the result of dwarf2_full_name, and it is
10009 the demangled name that GDB uses if it exists. */
10010 if (cu->language == language_ada
10011 || (cu->language == language_fortran && physname))
10012 {
10013 /* For Ada unit, we prefer the linkage name over the name, as
10014 the former contains the exported name, which the user expects
10015 to be able to reference. Ideally, we want the user to be able
10016 to reference this entity using either natural or linkage name,
10017 but we haven't started looking at this enhancement yet. */
10018 const char *linkage_name = dw2_linkage_name (die, cu);
10019
10020 if (linkage_name != NULL)
10021 return linkage_name;
10022 }
10023
10024 /* These are the only languages we know how to qualify names in. */
10025 if (name != NULL
10026 && (cu->language == language_cplus
10027 || cu->language == language_fortran || cu->language == language_d
10028 || cu->language == language_rust))
10029 {
10030 if (die_needs_namespace (die, cu))
10031 {
10032 const char *prefix;
10033 const char *canonical_name = NULL;
10034
10035 string_file buf;
10036
10037 prefix = determine_prefix (die, cu);
10038 if (*prefix != '\0')
10039 {
10040 gdb::unique_xmalloc_ptr<char> prefixed_name
10041 (typename_concat (NULL, prefix, name, physname, cu));
10042
10043 buf.puts (prefixed_name.get ());
10044 }
10045 else
10046 buf.puts (name);
10047
10048 /* Template parameters may be specified in the DIE's DW_AT_name, or
10049 as children with DW_TAG_template_type_param or
10050 DW_TAG_value_type_param. If the latter, add them to the name
10051 here. If the name already has template parameters, then
10052 skip this step; some versions of GCC emit both, and
10053 it is more efficient to use the pre-computed name.
10054
10055 Something to keep in mind about this process: it is very
10056 unlikely, or in some cases downright impossible, to produce
10057 something that will match the mangled name of a function.
10058 If the definition of the function has the same debug info,
10059 we should be able to match up with it anyway. But fallbacks
10060 using the minimal symbol, for instance to find a method
10061 implemented in a stripped copy of libstdc++, will not work.
10062 If we do not have debug info for the definition, we will have to
10063 match them up some other way.
10064
10065 When we do name matching there is a related problem with function
10066 templates; two instantiated function templates are allowed to
10067 differ only by their return types, which we do not add here. */
10068
10069 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10070 {
10071 struct attribute *attr;
10072 struct die_info *child;
10073 int first = 1;
10074
10075 die->building_fullname = 1;
10076
10077 for (child = die->child; child != NULL; child = child->sibling)
10078 {
10079 struct type *type;
10080 LONGEST value;
10081 const gdb_byte *bytes;
10082 struct dwarf2_locexpr_baton *baton;
10083 struct value *v;
10084
10085 if (child->tag != DW_TAG_template_type_param
10086 && child->tag != DW_TAG_template_value_param)
10087 continue;
10088
10089 if (first)
10090 {
10091 buf.puts ("<");
10092 first = 0;
10093 }
10094 else
10095 buf.puts (", ");
10096
10097 attr = dwarf2_attr (child, DW_AT_type, cu);
10098 if (attr == NULL)
10099 {
10100 complaint (_("template parameter missing DW_AT_type"));
10101 buf.puts ("UNKNOWN_TYPE");
10102 continue;
10103 }
10104 type = die_type (child, cu);
10105
10106 if (child->tag == DW_TAG_template_type_param)
10107 {
10108 c_print_type (type, "", &buf, -1, 0, cu->language,
10109 &type_print_raw_options);
10110 continue;
10111 }
10112
10113 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10114 if (attr == NULL)
10115 {
10116 complaint (_("template parameter missing "
10117 "DW_AT_const_value"));
10118 buf.puts ("UNKNOWN_VALUE");
10119 continue;
10120 }
10121
10122 dwarf2_const_value_attr (attr, type, name,
10123 &cu->comp_unit_obstack, cu,
10124 &value, &bytes, &baton);
10125
10126 if (TYPE_NOSIGN (type))
10127 /* GDB prints characters as NUMBER 'CHAR'. If that's
10128 changed, this can use value_print instead. */
10129 c_printchar (value, type, &buf);
10130 else
10131 {
10132 struct value_print_options opts;
10133
10134 if (baton != NULL)
10135 v = dwarf2_evaluate_loc_desc (type, NULL,
10136 baton->data,
10137 baton->size,
10138 baton->per_cu);
10139 else if (bytes != NULL)
10140 {
10141 v = allocate_value (type);
10142 memcpy (value_contents_writeable (v), bytes,
10143 TYPE_LENGTH (type));
10144 }
10145 else
10146 v = value_from_longest (type, value);
10147
10148 /* Specify decimal so that we do not depend on
10149 the radix. */
10150 get_formatted_print_options (&opts, 'd');
10151 opts.raw = 1;
10152 value_print (v, &buf, &opts);
10153 release_value (v);
10154 }
10155 }
10156
10157 die->building_fullname = 0;
10158
10159 if (!first)
10160 {
10161 /* Close the argument list, with a space if necessary
10162 (nested templates). */
10163 if (!buf.empty () && buf.string ().back () == '>')
10164 buf.puts (" >");
10165 else
10166 buf.puts (">");
10167 }
10168 }
10169
10170 /* For C++ methods, append formal parameter type
10171 information, if PHYSNAME. */
10172
10173 if (physname && die->tag == DW_TAG_subprogram
10174 && cu->language == language_cplus)
10175 {
10176 struct type *type = read_type_die (die, cu);
10177
10178 c_type_print_args (type, &buf, 1, cu->language,
10179 &type_print_raw_options);
10180
10181 if (cu->language == language_cplus)
10182 {
10183 /* Assume that an artificial first parameter is
10184 "this", but do not crash if it is not. RealView
10185 marks unnamed (and thus unused) parameters as
10186 artificial; there is no way to differentiate
10187 the two cases. */
10188 if (TYPE_NFIELDS (type) > 0
10189 && TYPE_FIELD_ARTIFICIAL (type, 0)
10190 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10191 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10192 0))))
10193 buf.puts (" const");
10194 }
10195 }
10196
10197 const std::string &intermediate_name = buf.string ();
10198
10199 if (cu->language == language_cplus)
10200 canonical_name
10201 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10202 &objfile->per_bfd->storage_obstack);
10203
10204 /* If we only computed INTERMEDIATE_NAME, or if
10205 INTERMEDIATE_NAME is already canonical, then we need to
10206 copy it to the appropriate obstack. */
10207 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10208 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10209 intermediate_name);
10210 else
10211 name = canonical_name;
10212 }
10213 }
10214
10215 return name;
10216 }
10217
10218 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10219 If scope qualifiers are appropriate they will be added. The result
10220 will be allocated on the storage_obstack, or NULL if the DIE does
10221 not have a name. NAME may either be from a previous call to
10222 dwarf2_name or NULL.
10223
10224 The output string will be canonicalized (if C++). */
10225
10226 static const char *
10227 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10228 {
10229 return dwarf2_compute_name (name, die, cu, 0);
10230 }
10231
10232 /* Construct a physname for the given DIE in CU. NAME may either be
10233 from a previous call to dwarf2_name or NULL. The result will be
10234 allocated on the objfile_objstack or NULL if the DIE does not have a
10235 name.
10236
10237 The output string will be canonicalized (if C++). */
10238
10239 static const char *
10240 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10241 {
10242 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10243 const char *retval, *mangled = NULL, *canon = NULL;
10244 int need_copy = 1;
10245
10246 /* In this case dwarf2_compute_name is just a shortcut not building anything
10247 on its own. */
10248 if (!die_needs_namespace (die, cu))
10249 return dwarf2_compute_name (name, die, cu, 1);
10250
10251 mangled = dw2_linkage_name (die, cu);
10252
10253 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10254 See https://github.com/rust-lang/rust/issues/32925. */
10255 if (cu->language == language_rust && mangled != NULL
10256 && strchr (mangled, '{') != NULL)
10257 mangled = NULL;
10258
10259 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10260 has computed. */
10261 gdb::unique_xmalloc_ptr<char> demangled;
10262 if (mangled != NULL)
10263 {
10264
10265 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10266 {
10267 /* Do nothing (do not demangle the symbol name). */
10268 }
10269 else if (cu->language == language_go)
10270 {
10271 /* This is a lie, but we already lie to the caller new_symbol.
10272 new_symbol assumes we return the mangled name.
10273 This just undoes that lie until things are cleaned up. */
10274 }
10275 else
10276 {
10277 /* Use DMGL_RET_DROP for C++ template functions to suppress
10278 their return type. It is easier for GDB users to search
10279 for such functions as `name(params)' than `long name(params)'.
10280 In such case the minimal symbol names do not match the full
10281 symbol names but for template functions there is never a need
10282 to look up their definition from their declaration so
10283 the only disadvantage remains the minimal symbol variant
10284 `long name(params)' does not have the proper inferior type. */
10285 demangled.reset (gdb_demangle (mangled,
10286 (DMGL_PARAMS | DMGL_ANSI
10287 | DMGL_RET_DROP)));
10288 }
10289 if (demangled)
10290 canon = demangled.get ();
10291 else
10292 {
10293 canon = mangled;
10294 need_copy = 0;
10295 }
10296 }
10297
10298 if (canon == NULL || check_physname)
10299 {
10300 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10301
10302 if (canon != NULL && strcmp (physname, canon) != 0)
10303 {
10304 /* It may not mean a bug in GDB. The compiler could also
10305 compute DW_AT_linkage_name incorrectly. But in such case
10306 GDB would need to be bug-to-bug compatible. */
10307
10308 complaint (_("Computed physname <%s> does not match demangled <%s> "
10309 "(from linkage <%s>) - DIE at %s [in module %s]"),
10310 physname, canon, mangled, sect_offset_str (die->sect_off),
10311 objfile_name (objfile));
10312
10313 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10314 is available here - over computed PHYSNAME. It is safer
10315 against both buggy GDB and buggy compilers. */
10316
10317 retval = canon;
10318 }
10319 else
10320 {
10321 retval = physname;
10322 need_copy = 0;
10323 }
10324 }
10325 else
10326 retval = canon;
10327
10328 if (need_copy)
10329 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10330
10331 return retval;
10332 }
10333
10334 /* Inspect DIE in CU for a namespace alias. If one exists, record
10335 a new symbol for it.
10336
10337 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10338
10339 static int
10340 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10341 {
10342 struct attribute *attr;
10343
10344 /* If the die does not have a name, this is not a namespace
10345 alias. */
10346 attr = dwarf2_attr (die, DW_AT_name, cu);
10347 if (attr != NULL)
10348 {
10349 int num;
10350 struct die_info *d = die;
10351 struct dwarf2_cu *imported_cu = cu;
10352
10353 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10354 keep inspecting DIEs until we hit the underlying import. */
10355 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10356 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10357 {
10358 attr = dwarf2_attr (d, DW_AT_import, cu);
10359 if (attr == NULL)
10360 break;
10361
10362 d = follow_die_ref (d, attr, &imported_cu);
10363 if (d->tag != DW_TAG_imported_declaration)
10364 break;
10365 }
10366
10367 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10368 {
10369 complaint (_("DIE at %s has too many recursively imported "
10370 "declarations"), sect_offset_str (d->sect_off));
10371 return 0;
10372 }
10373
10374 if (attr != NULL)
10375 {
10376 struct type *type;
10377 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10378
10379 type = get_die_type_at_offset (sect_off, cu->per_cu);
10380 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10381 {
10382 /* This declaration is a global namespace alias. Add
10383 a symbol for it whose type is the aliased namespace. */
10384 new_symbol (die, type, cu);
10385 return 1;
10386 }
10387 }
10388 }
10389
10390 return 0;
10391 }
10392
10393 /* Return the using directives repository (global or local?) to use in the
10394 current context for CU.
10395
10396 For Ada, imported declarations can materialize renamings, which *may* be
10397 global. However it is impossible (for now?) in DWARF to distinguish
10398 "external" imported declarations and "static" ones. As all imported
10399 declarations seem to be static in all other languages, make them all CU-wide
10400 global only in Ada. */
10401
10402 static struct using_direct **
10403 using_directives (struct dwarf2_cu *cu)
10404 {
10405 if (cu->language == language_ada
10406 && cu->get_builder ()->outermost_context_p ())
10407 return cu->get_builder ()->get_global_using_directives ();
10408 else
10409 return cu->get_builder ()->get_local_using_directives ();
10410 }
10411
10412 /* Read the import statement specified by the given die and record it. */
10413
10414 static void
10415 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10416 {
10417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10418 struct attribute *import_attr;
10419 struct die_info *imported_die, *child_die;
10420 struct dwarf2_cu *imported_cu;
10421 const char *imported_name;
10422 const char *imported_name_prefix;
10423 const char *canonical_name;
10424 const char *import_alias;
10425 const char *imported_declaration = NULL;
10426 const char *import_prefix;
10427 std::vector<const char *> excludes;
10428
10429 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10430 if (import_attr == NULL)
10431 {
10432 complaint (_("Tag '%s' has no DW_AT_import"),
10433 dwarf_tag_name (die->tag));
10434 return;
10435 }
10436
10437 imported_cu = cu;
10438 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10439 imported_name = dwarf2_name (imported_die, imported_cu);
10440 if (imported_name == NULL)
10441 {
10442 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10443
10444 The import in the following code:
10445 namespace A
10446 {
10447 typedef int B;
10448 }
10449
10450 int main ()
10451 {
10452 using A::B;
10453 B b;
10454 return b;
10455 }
10456
10457 ...
10458 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10459 <52> DW_AT_decl_file : 1
10460 <53> DW_AT_decl_line : 6
10461 <54> DW_AT_import : <0x75>
10462 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10463 <59> DW_AT_name : B
10464 <5b> DW_AT_decl_file : 1
10465 <5c> DW_AT_decl_line : 2
10466 <5d> DW_AT_type : <0x6e>
10467 ...
10468 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10469 <76> DW_AT_byte_size : 4
10470 <77> DW_AT_encoding : 5 (signed)
10471
10472 imports the wrong die ( 0x75 instead of 0x58 ).
10473 This case will be ignored until the gcc bug is fixed. */
10474 return;
10475 }
10476
10477 /* Figure out the local name after import. */
10478 import_alias = dwarf2_name (die, cu);
10479
10480 /* Figure out where the statement is being imported to. */
10481 import_prefix = determine_prefix (die, cu);
10482
10483 /* Figure out what the scope of the imported die is and prepend it
10484 to the name of the imported die. */
10485 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10486
10487 if (imported_die->tag != DW_TAG_namespace
10488 && imported_die->tag != DW_TAG_module)
10489 {
10490 imported_declaration = imported_name;
10491 canonical_name = imported_name_prefix;
10492 }
10493 else if (strlen (imported_name_prefix) > 0)
10494 canonical_name = obconcat (&objfile->objfile_obstack,
10495 imported_name_prefix,
10496 (cu->language == language_d ? "." : "::"),
10497 imported_name, (char *) NULL);
10498 else
10499 canonical_name = imported_name;
10500
10501 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10502 for (child_die = die->child; child_die && child_die->tag;
10503 child_die = sibling_die (child_die))
10504 {
10505 /* DWARF-4: A Fortran use statement with a “rename list” may be
10506 represented by an imported module entry with an import attribute
10507 referring to the module and owned entries corresponding to those
10508 entities that are renamed as part of being imported. */
10509
10510 if (child_die->tag != DW_TAG_imported_declaration)
10511 {
10512 complaint (_("child DW_TAG_imported_declaration expected "
10513 "- DIE at %s [in module %s]"),
10514 sect_offset_str (child_die->sect_off),
10515 objfile_name (objfile));
10516 continue;
10517 }
10518
10519 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10520 if (import_attr == NULL)
10521 {
10522 complaint (_("Tag '%s' has no DW_AT_import"),
10523 dwarf_tag_name (child_die->tag));
10524 continue;
10525 }
10526
10527 imported_cu = cu;
10528 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10529 &imported_cu);
10530 imported_name = dwarf2_name (imported_die, imported_cu);
10531 if (imported_name == NULL)
10532 {
10533 complaint (_("child DW_TAG_imported_declaration has unknown "
10534 "imported name - DIE at %s [in module %s]"),
10535 sect_offset_str (child_die->sect_off),
10536 objfile_name (objfile));
10537 continue;
10538 }
10539
10540 excludes.push_back (imported_name);
10541
10542 process_die (child_die, cu);
10543 }
10544
10545 add_using_directive (using_directives (cu),
10546 import_prefix,
10547 canonical_name,
10548 import_alias,
10549 imported_declaration,
10550 excludes,
10551 0,
10552 &objfile->objfile_obstack);
10553 }
10554
10555 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10556 types, but gives them a size of zero. Starting with version 14,
10557 ICC is compatible with GCC. */
10558
10559 static bool
10560 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10561 {
10562 if (!cu->checked_producer)
10563 check_producer (cu);
10564
10565 return cu->producer_is_icc_lt_14;
10566 }
10567
10568 /* ICC generates a DW_AT_type for C void functions. This was observed on
10569 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10570 which says that void functions should not have a DW_AT_type. */
10571
10572 static bool
10573 producer_is_icc (struct dwarf2_cu *cu)
10574 {
10575 if (!cu->checked_producer)
10576 check_producer (cu);
10577
10578 return cu->producer_is_icc;
10579 }
10580
10581 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10582 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10583 this, it was first present in GCC release 4.3.0. */
10584
10585 static bool
10586 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10587 {
10588 if (!cu->checked_producer)
10589 check_producer (cu);
10590
10591 return cu->producer_is_gcc_lt_4_3;
10592 }
10593
10594 static file_and_directory
10595 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10596 {
10597 file_and_directory res;
10598
10599 /* Find the filename. Do not use dwarf2_name here, since the filename
10600 is not a source language identifier. */
10601 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10602 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10603
10604 if (res.comp_dir == NULL
10605 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10606 && IS_ABSOLUTE_PATH (res.name))
10607 {
10608 res.comp_dir_storage = ldirname (res.name);
10609 if (!res.comp_dir_storage.empty ())
10610 res.comp_dir = res.comp_dir_storage.c_str ();
10611 }
10612 if (res.comp_dir != NULL)
10613 {
10614 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10615 directory, get rid of it. */
10616 const char *cp = strchr (res.comp_dir, ':');
10617
10618 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10619 res.comp_dir = cp + 1;
10620 }
10621
10622 if (res.name == NULL)
10623 res.name = "<unknown>";
10624
10625 return res;
10626 }
10627
10628 /* Handle DW_AT_stmt_list for a compilation unit.
10629 DIE is the DW_TAG_compile_unit die for CU.
10630 COMP_DIR is the compilation directory. LOWPC is passed to
10631 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10632
10633 static void
10634 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10635 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10636 {
10637 struct dwarf2_per_objfile *dwarf2_per_objfile
10638 = cu->per_cu->dwarf2_per_objfile;
10639 struct attribute *attr;
10640 struct line_header line_header_local;
10641 hashval_t line_header_local_hash;
10642 void **slot;
10643 int decode_mapping;
10644
10645 gdb_assert (! cu->per_cu->is_debug_types);
10646
10647 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10648 if (attr == NULL)
10649 return;
10650
10651 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10652
10653 /* The line header hash table is only created if needed (it exists to
10654 prevent redundant reading of the line table for partial_units).
10655 If we're given a partial_unit, we'll need it. If we're given a
10656 compile_unit, then use the line header hash table if it's already
10657 created, but don't create one just yet. */
10658
10659 if (dwarf2_per_objfile->line_header_hash == NULL
10660 && die->tag == DW_TAG_partial_unit)
10661 {
10662 dwarf2_per_objfile->line_header_hash
10663 .reset (htab_create_alloc (127, line_header_hash_voidp,
10664 line_header_eq_voidp,
10665 free_line_header_voidp,
10666 xcalloc, xfree));
10667 }
10668
10669 line_header_local.sect_off = line_offset;
10670 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10671 line_header_local_hash = line_header_hash (&line_header_local);
10672 if (dwarf2_per_objfile->line_header_hash != NULL)
10673 {
10674 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10675 &line_header_local,
10676 line_header_local_hash, NO_INSERT);
10677
10678 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10679 is not present in *SLOT (since if there is something in *SLOT then
10680 it will be for a partial_unit). */
10681 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10682 {
10683 gdb_assert (*slot != NULL);
10684 cu->line_header = (struct line_header *) *slot;
10685 return;
10686 }
10687 }
10688
10689 /* dwarf_decode_line_header does not yet provide sufficient information.
10690 We always have to call also dwarf_decode_lines for it. */
10691 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10692 if (lh == NULL)
10693 return;
10694
10695 cu->line_header = lh.release ();
10696 cu->line_header_die_owner = die;
10697
10698 if (dwarf2_per_objfile->line_header_hash == NULL)
10699 slot = NULL;
10700 else
10701 {
10702 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10703 &line_header_local,
10704 line_header_local_hash, INSERT);
10705 gdb_assert (slot != NULL);
10706 }
10707 if (slot != NULL && *slot == NULL)
10708 {
10709 /* This newly decoded line number information unit will be owned
10710 by line_header_hash hash table. */
10711 *slot = cu->line_header;
10712 cu->line_header_die_owner = NULL;
10713 }
10714 else
10715 {
10716 /* We cannot free any current entry in (*slot) as that struct line_header
10717 may be already used by multiple CUs. Create only temporary decoded
10718 line_header for this CU - it may happen at most once for each line
10719 number information unit. And if we're not using line_header_hash
10720 then this is what we want as well. */
10721 gdb_assert (die->tag != DW_TAG_partial_unit);
10722 }
10723 decode_mapping = (die->tag != DW_TAG_partial_unit);
10724 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10725 decode_mapping);
10726
10727 }
10728
10729 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10730
10731 static void
10732 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10733 {
10734 struct dwarf2_per_objfile *dwarf2_per_objfile
10735 = cu->per_cu->dwarf2_per_objfile;
10736 struct objfile *objfile = dwarf2_per_objfile->objfile;
10737 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10738 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10739 CORE_ADDR highpc = ((CORE_ADDR) 0);
10740 struct attribute *attr;
10741 struct die_info *child_die;
10742 CORE_ADDR baseaddr;
10743
10744 prepare_one_comp_unit (cu, die, cu->language);
10745 baseaddr = objfile->text_section_offset ();
10746
10747 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10748
10749 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10750 from finish_block. */
10751 if (lowpc == ((CORE_ADDR) -1))
10752 lowpc = highpc;
10753 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10754
10755 file_and_directory fnd = find_file_and_directory (die, cu);
10756
10757 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10758 standardised yet. As a workaround for the language detection we fall
10759 back to the DW_AT_producer string. */
10760 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10761 cu->language = language_opencl;
10762
10763 /* Similar hack for Go. */
10764 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10765 set_cu_language (DW_LANG_Go, cu);
10766
10767 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10768
10769 /* Decode line number information if present. We do this before
10770 processing child DIEs, so that the line header table is available
10771 for DW_AT_decl_file. */
10772 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10773
10774 /* Process all dies in compilation unit. */
10775 if (die->child != NULL)
10776 {
10777 child_die = die->child;
10778 while (child_die && child_die->tag)
10779 {
10780 process_die (child_die, cu);
10781 child_die = sibling_die (child_die);
10782 }
10783 }
10784
10785 /* Decode macro information, if present. Dwarf 2 macro information
10786 refers to information in the line number info statement program
10787 header, so we can only read it if we've read the header
10788 successfully. */
10789 attr = dwarf2_attr (die, DW_AT_macros, cu);
10790 if (attr == NULL)
10791 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10792 if (attr && cu->line_header)
10793 {
10794 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10795 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10796
10797 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10798 }
10799 else
10800 {
10801 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10802 if (attr && cu->line_header)
10803 {
10804 unsigned int macro_offset = DW_UNSND (attr);
10805
10806 dwarf_decode_macros (cu, macro_offset, 0);
10807 }
10808 }
10809 }
10810
10811 void
10812 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10813 {
10814 struct type_unit_group *tu_group;
10815 int first_time;
10816 struct attribute *attr;
10817 unsigned int i;
10818 struct signatured_type *sig_type;
10819
10820 gdb_assert (per_cu->is_debug_types);
10821 sig_type = (struct signatured_type *) per_cu;
10822
10823 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10824
10825 /* If we're using .gdb_index (includes -readnow) then
10826 per_cu->type_unit_group may not have been set up yet. */
10827 if (sig_type->type_unit_group == NULL)
10828 sig_type->type_unit_group = get_type_unit_group (this, attr);
10829 tu_group = sig_type->type_unit_group;
10830
10831 /* If we've already processed this stmt_list there's no real need to
10832 do it again, we could fake it and just recreate the part we need
10833 (file name,index -> symtab mapping). If data shows this optimization
10834 is useful we can do it then. */
10835 first_time = tu_group->compunit_symtab == NULL;
10836
10837 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10838 debug info. */
10839 line_header_up lh;
10840 if (attr != NULL)
10841 {
10842 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10843 lh = dwarf_decode_line_header (line_offset, this);
10844 }
10845 if (lh == NULL)
10846 {
10847 if (first_time)
10848 start_symtab ("", NULL, 0);
10849 else
10850 {
10851 gdb_assert (tu_group->symtabs == NULL);
10852 gdb_assert (m_builder == nullptr);
10853 struct compunit_symtab *cust = tu_group->compunit_symtab;
10854 m_builder.reset (new struct buildsym_compunit
10855 (COMPUNIT_OBJFILE (cust), "",
10856 COMPUNIT_DIRNAME (cust),
10857 compunit_language (cust),
10858 0, cust));
10859 }
10860 return;
10861 }
10862
10863 line_header = lh.release ();
10864 line_header_die_owner = die;
10865
10866 if (first_time)
10867 {
10868 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10869
10870 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10871 still initializing it, and our caller (a few levels up)
10872 process_full_type_unit still needs to know if this is the first
10873 time. */
10874
10875 tu_group->num_symtabs = line_header->file_names_size ();
10876 tu_group->symtabs = XNEWVEC (struct symtab *,
10877 line_header->file_names_size ());
10878
10879 auto &file_names = line_header->file_names ();
10880 for (i = 0; i < file_names.size (); ++i)
10881 {
10882 file_entry &fe = file_names[i];
10883 dwarf2_start_subfile (this, fe.name,
10884 fe.include_dir (line_header));
10885 buildsym_compunit *b = get_builder ();
10886 if (b->get_current_subfile ()->symtab == NULL)
10887 {
10888 /* NOTE: start_subfile will recognize when it's been
10889 passed a file it has already seen. So we can't
10890 assume there's a simple mapping from
10891 cu->line_header->file_names to subfiles, plus
10892 cu->line_header->file_names may contain dups. */
10893 b->get_current_subfile ()->symtab
10894 = allocate_symtab (cust, b->get_current_subfile ()->name);
10895 }
10896
10897 fe.symtab = b->get_current_subfile ()->symtab;
10898 tu_group->symtabs[i] = fe.symtab;
10899 }
10900 }
10901 else
10902 {
10903 gdb_assert (m_builder == nullptr);
10904 struct compunit_symtab *cust = tu_group->compunit_symtab;
10905 m_builder.reset (new struct buildsym_compunit
10906 (COMPUNIT_OBJFILE (cust), "",
10907 COMPUNIT_DIRNAME (cust),
10908 compunit_language (cust),
10909 0, cust));
10910
10911 auto &file_names = line_header->file_names ();
10912 for (i = 0; i < file_names.size (); ++i)
10913 {
10914 file_entry &fe = file_names[i];
10915 fe.symtab = tu_group->symtabs[i];
10916 }
10917 }
10918
10919 /* The main symtab is allocated last. Type units don't have DW_AT_name
10920 so they don't have a "real" (so to speak) symtab anyway.
10921 There is later code that will assign the main symtab to all symbols
10922 that don't have one. We need to handle the case of a symbol with a
10923 missing symtab (DW_AT_decl_file) anyway. */
10924 }
10925
10926 /* Process DW_TAG_type_unit.
10927 For TUs we want to skip the first top level sibling if it's not the
10928 actual type being defined by this TU. In this case the first top
10929 level sibling is there to provide context only. */
10930
10931 static void
10932 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10933 {
10934 struct die_info *child_die;
10935
10936 prepare_one_comp_unit (cu, die, language_minimal);
10937
10938 /* Initialize (or reinitialize) the machinery for building symtabs.
10939 We do this before processing child DIEs, so that the line header table
10940 is available for DW_AT_decl_file. */
10941 cu->setup_type_unit_groups (die);
10942
10943 if (die->child != NULL)
10944 {
10945 child_die = die->child;
10946 while (child_die && child_die->tag)
10947 {
10948 process_die (child_die, cu);
10949 child_die = sibling_die (child_die);
10950 }
10951 }
10952 }
10953 \f
10954 /* DWO/DWP files.
10955
10956 http://gcc.gnu.org/wiki/DebugFission
10957 http://gcc.gnu.org/wiki/DebugFissionDWP
10958
10959 To simplify handling of both DWO files ("object" files with the DWARF info)
10960 and DWP files (a file with the DWOs packaged up into one file), we treat
10961 DWP files as having a collection of virtual DWO files. */
10962
10963 static hashval_t
10964 hash_dwo_file (const void *item)
10965 {
10966 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10967 hashval_t hash;
10968
10969 hash = htab_hash_string (dwo_file->dwo_name);
10970 if (dwo_file->comp_dir != NULL)
10971 hash += htab_hash_string (dwo_file->comp_dir);
10972 return hash;
10973 }
10974
10975 static int
10976 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10977 {
10978 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10979 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10980
10981 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10982 return 0;
10983 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10984 return lhs->comp_dir == rhs->comp_dir;
10985 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10986 }
10987
10988 /* Allocate a hash table for DWO files. */
10989
10990 static htab_up
10991 allocate_dwo_file_hash_table (struct objfile *objfile)
10992 {
10993 auto delete_dwo_file = [] (void *item)
10994 {
10995 struct dwo_file *dwo_file = (struct dwo_file *) item;
10996
10997 delete dwo_file;
10998 };
10999
11000 return htab_up (htab_create_alloc (41,
11001 hash_dwo_file,
11002 eq_dwo_file,
11003 delete_dwo_file,
11004 xcalloc, xfree));
11005 }
11006
11007 /* Lookup DWO file DWO_NAME. */
11008
11009 static void **
11010 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11011 const char *dwo_name,
11012 const char *comp_dir)
11013 {
11014 struct dwo_file find_entry;
11015 void **slot;
11016
11017 if (dwarf2_per_objfile->dwo_files == NULL)
11018 dwarf2_per_objfile->dwo_files
11019 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11020
11021 find_entry.dwo_name = dwo_name;
11022 find_entry.comp_dir = comp_dir;
11023 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11024 INSERT);
11025
11026 return slot;
11027 }
11028
11029 static hashval_t
11030 hash_dwo_unit (const void *item)
11031 {
11032 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11033
11034 /* This drops the top 32 bits of the id, but is ok for a hash. */
11035 return dwo_unit->signature;
11036 }
11037
11038 static int
11039 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11040 {
11041 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11042 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11043
11044 /* The signature is assumed to be unique within the DWO file.
11045 So while object file CU dwo_id's always have the value zero,
11046 that's OK, assuming each object file DWO file has only one CU,
11047 and that's the rule for now. */
11048 return lhs->signature == rhs->signature;
11049 }
11050
11051 /* Allocate a hash table for DWO CUs,TUs.
11052 There is one of these tables for each of CUs,TUs for each DWO file. */
11053
11054 static htab_up
11055 allocate_dwo_unit_table (struct objfile *objfile)
11056 {
11057 /* Start out with a pretty small number.
11058 Generally DWO files contain only one CU and maybe some TUs. */
11059 return htab_up (htab_create_alloc (3,
11060 hash_dwo_unit,
11061 eq_dwo_unit,
11062 NULL, xcalloc, xfree));
11063 }
11064
11065 /* die_reader_func for create_dwo_cu. */
11066
11067 static void
11068 create_dwo_cu_reader (const struct die_reader_specs *reader,
11069 const gdb_byte *info_ptr,
11070 struct die_info *comp_unit_die,
11071 struct dwo_file *dwo_file,
11072 struct dwo_unit *dwo_unit)
11073 {
11074 struct dwarf2_cu *cu = reader->cu;
11075 sect_offset sect_off = cu->per_cu->sect_off;
11076 struct dwarf2_section_info *section = cu->per_cu->section;
11077
11078 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11079 if (!signature.has_value ())
11080 {
11081 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11082 " its dwo_id [in module %s]"),
11083 sect_offset_str (sect_off), dwo_file->dwo_name);
11084 return;
11085 }
11086
11087 dwo_unit->dwo_file = dwo_file;
11088 dwo_unit->signature = *signature;
11089 dwo_unit->section = section;
11090 dwo_unit->sect_off = sect_off;
11091 dwo_unit->length = cu->per_cu->length;
11092
11093 if (dwarf_read_debug)
11094 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11095 sect_offset_str (sect_off),
11096 hex_string (dwo_unit->signature));
11097 }
11098
11099 /* Create the dwo_units for the CUs in a DWO_FILE.
11100 Note: This function processes DWO files only, not DWP files. */
11101
11102 static void
11103 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11104 dwarf2_cu *cu, struct dwo_file &dwo_file,
11105 dwarf2_section_info &section, htab_up &cus_htab)
11106 {
11107 struct objfile *objfile = dwarf2_per_objfile->objfile;
11108 const gdb_byte *info_ptr, *end_ptr;
11109
11110 section.read (objfile);
11111 info_ptr = section.buffer;
11112
11113 if (info_ptr == NULL)
11114 return;
11115
11116 if (dwarf_read_debug)
11117 {
11118 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11119 section.get_name (),
11120 section.get_file_name ());
11121 }
11122
11123 end_ptr = info_ptr + section.size;
11124 while (info_ptr < end_ptr)
11125 {
11126 struct dwarf2_per_cu_data per_cu;
11127 struct dwo_unit read_unit {};
11128 struct dwo_unit *dwo_unit;
11129 void **slot;
11130 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11131
11132 memset (&per_cu, 0, sizeof (per_cu));
11133 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11134 per_cu.is_debug_types = 0;
11135 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11136 per_cu.section = &section;
11137
11138 cutu_reader reader (&per_cu, cu, &dwo_file);
11139 if (!reader.dummy_p)
11140 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11141 &dwo_file, &read_unit);
11142 info_ptr += per_cu.length;
11143
11144 // If the unit could not be parsed, skip it.
11145 if (read_unit.dwo_file == NULL)
11146 continue;
11147
11148 if (cus_htab == NULL)
11149 cus_htab = allocate_dwo_unit_table (objfile);
11150
11151 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11152 *dwo_unit = read_unit;
11153 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11154 gdb_assert (slot != NULL);
11155 if (*slot != NULL)
11156 {
11157 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11158 sect_offset dup_sect_off = dup_cu->sect_off;
11159
11160 complaint (_("debug cu entry at offset %s is duplicate to"
11161 " the entry at offset %s, signature %s"),
11162 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11163 hex_string (dwo_unit->signature));
11164 }
11165 *slot = (void *)dwo_unit;
11166 }
11167 }
11168
11169 /* DWP file .debug_{cu,tu}_index section format:
11170 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11171
11172 DWP Version 1:
11173
11174 Both index sections have the same format, and serve to map a 64-bit
11175 signature to a set of section numbers. Each section begins with a header,
11176 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11177 indexes, and a pool of 32-bit section numbers. The index sections will be
11178 aligned at 8-byte boundaries in the file.
11179
11180 The index section header consists of:
11181
11182 V, 32 bit version number
11183 -, 32 bits unused
11184 N, 32 bit number of compilation units or type units in the index
11185 M, 32 bit number of slots in the hash table
11186
11187 Numbers are recorded using the byte order of the application binary.
11188
11189 The hash table begins at offset 16 in the section, and consists of an array
11190 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11191 order of the application binary). Unused slots in the hash table are 0.
11192 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11193
11194 The parallel table begins immediately after the hash table
11195 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11196 array of 32-bit indexes (using the byte order of the application binary),
11197 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11198 table contains a 32-bit index into the pool of section numbers. For unused
11199 hash table slots, the corresponding entry in the parallel table will be 0.
11200
11201 The pool of section numbers begins immediately following the hash table
11202 (at offset 16 + 12 * M from the beginning of the section). The pool of
11203 section numbers consists of an array of 32-bit words (using the byte order
11204 of the application binary). Each item in the array is indexed starting
11205 from 0. The hash table entry provides the index of the first section
11206 number in the set. Additional section numbers in the set follow, and the
11207 set is terminated by a 0 entry (section number 0 is not used in ELF).
11208
11209 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11210 section must be the first entry in the set, and the .debug_abbrev.dwo must
11211 be the second entry. Other members of the set may follow in any order.
11212
11213 ---
11214
11215 DWP Version 2:
11216
11217 DWP Version 2 combines all the .debug_info, etc. sections into one,
11218 and the entries in the index tables are now offsets into these sections.
11219 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11220 section.
11221
11222 Index Section Contents:
11223 Header
11224 Hash Table of Signatures dwp_hash_table.hash_table
11225 Parallel Table of Indices dwp_hash_table.unit_table
11226 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11227 Table of Section Sizes dwp_hash_table.v2.sizes
11228
11229 The index section header consists of:
11230
11231 V, 32 bit version number
11232 L, 32 bit number of columns in the table of section offsets
11233 N, 32 bit number of compilation units or type units in the index
11234 M, 32 bit number of slots in the hash table
11235
11236 Numbers are recorded using the byte order of the application binary.
11237
11238 The hash table has the same format as version 1.
11239 The parallel table of indices has the same format as version 1,
11240 except that the entries are origin-1 indices into the table of sections
11241 offsets and the table of section sizes.
11242
11243 The table of offsets begins immediately following the parallel table
11244 (at offset 16 + 12 * M from the beginning of the section). The table is
11245 a two-dimensional array of 32-bit words (using the byte order of the
11246 application binary), with L columns and N+1 rows, in row-major order.
11247 Each row in the array is indexed starting from 0. The first row provides
11248 a key to the remaining rows: each column in this row provides an identifier
11249 for a debug section, and the offsets in the same column of subsequent rows
11250 refer to that section. The section identifiers are:
11251
11252 DW_SECT_INFO 1 .debug_info.dwo
11253 DW_SECT_TYPES 2 .debug_types.dwo
11254 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11255 DW_SECT_LINE 4 .debug_line.dwo
11256 DW_SECT_LOC 5 .debug_loc.dwo
11257 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11258 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11259 DW_SECT_MACRO 8 .debug_macro.dwo
11260
11261 The offsets provided by the CU and TU index sections are the base offsets
11262 for the contributions made by each CU or TU to the corresponding section
11263 in the package file. Each CU and TU header contains an abbrev_offset
11264 field, used to find the abbreviations table for that CU or TU within the
11265 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11266 be interpreted as relative to the base offset given in the index section.
11267 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11268 should be interpreted as relative to the base offset for .debug_line.dwo,
11269 and offsets into other debug sections obtained from DWARF attributes should
11270 also be interpreted as relative to the corresponding base offset.
11271
11272 The table of sizes begins immediately following the table of offsets.
11273 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11274 with L columns and N rows, in row-major order. Each row in the array is
11275 indexed starting from 1 (row 0 is shared by the two tables).
11276
11277 ---
11278
11279 Hash table lookup is handled the same in version 1 and 2:
11280
11281 We assume that N and M will not exceed 2^32 - 1.
11282 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11283
11284 Given a 64-bit compilation unit signature or a type signature S, an entry
11285 in the hash table is located as follows:
11286
11287 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11288 the low-order k bits all set to 1.
11289
11290 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11291
11292 3) If the hash table entry at index H matches the signature, use that
11293 entry. If the hash table entry at index H is unused (all zeroes),
11294 terminate the search: the signature is not present in the table.
11295
11296 4) Let H = (H + H') modulo M. Repeat at Step 3.
11297
11298 Because M > N and H' and M are relatively prime, the search is guaranteed
11299 to stop at an unused slot or find the match. */
11300
11301 /* Create a hash table to map DWO IDs to their CU/TU entry in
11302 .debug_{info,types}.dwo in DWP_FILE.
11303 Returns NULL if there isn't one.
11304 Note: This function processes DWP files only, not DWO files. */
11305
11306 static struct dwp_hash_table *
11307 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11308 struct dwp_file *dwp_file, int is_debug_types)
11309 {
11310 struct objfile *objfile = dwarf2_per_objfile->objfile;
11311 bfd *dbfd = dwp_file->dbfd.get ();
11312 const gdb_byte *index_ptr, *index_end;
11313 struct dwarf2_section_info *index;
11314 uint32_t version, nr_columns, nr_units, nr_slots;
11315 struct dwp_hash_table *htab;
11316
11317 if (is_debug_types)
11318 index = &dwp_file->sections.tu_index;
11319 else
11320 index = &dwp_file->sections.cu_index;
11321
11322 if (index->empty ())
11323 return NULL;
11324 index->read (objfile);
11325
11326 index_ptr = index->buffer;
11327 index_end = index_ptr + index->size;
11328
11329 version = read_4_bytes (dbfd, index_ptr);
11330 index_ptr += 4;
11331 if (version == 2)
11332 nr_columns = read_4_bytes (dbfd, index_ptr);
11333 else
11334 nr_columns = 0;
11335 index_ptr += 4;
11336 nr_units = read_4_bytes (dbfd, index_ptr);
11337 index_ptr += 4;
11338 nr_slots = read_4_bytes (dbfd, index_ptr);
11339 index_ptr += 4;
11340
11341 if (version != 1 && version != 2)
11342 {
11343 error (_("Dwarf Error: unsupported DWP file version (%s)"
11344 " [in module %s]"),
11345 pulongest (version), dwp_file->name);
11346 }
11347 if (nr_slots != (nr_slots & -nr_slots))
11348 {
11349 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11350 " is not power of 2 [in module %s]"),
11351 pulongest (nr_slots), dwp_file->name);
11352 }
11353
11354 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11355 htab->version = version;
11356 htab->nr_columns = nr_columns;
11357 htab->nr_units = nr_units;
11358 htab->nr_slots = nr_slots;
11359 htab->hash_table = index_ptr;
11360 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11361
11362 /* Exit early if the table is empty. */
11363 if (nr_slots == 0 || nr_units == 0
11364 || (version == 2 && nr_columns == 0))
11365 {
11366 /* All must be zero. */
11367 if (nr_slots != 0 || nr_units != 0
11368 || (version == 2 && nr_columns != 0))
11369 {
11370 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11371 " all zero [in modules %s]"),
11372 dwp_file->name);
11373 }
11374 return htab;
11375 }
11376
11377 if (version == 1)
11378 {
11379 htab->section_pool.v1.indices =
11380 htab->unit_table + sizeof (uint32_t) * nr_slots;
11381 /* It's harder to decide whether the section is too small in v1.
11382 V1 is deprecated anyway so we punt. */
11383 }
11384 else
11385 {
11386 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11387 int *ids = htab->section_pool.v2.section_ids;
11388 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11389 /* Reverse map for error checking. */
11390 int ids_seen[DW_SECT_MAX + 1];
11391 int i;
11392
11393 if (nr_columns < 2)
11394 {
11395 error (_("Dwarf Error: bad DWP hash table, too few columns"
11396 " in section table [in module %s]"),
11397 dwp_file->name);
11398 }
11399 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11400 {
11401 error (_("Dwarf Error: bad DWP hash table, too many columns"
11402 " in section table [in module %s]"),
11403 dwp_file->name);
11404 }
11405 memset (ids, 255, sizeof_ids);
11406 memset (ids_seen, 255, sizeof (ids_seen));
11407 for (i = 0; i < nr_columns; ++i)
11408 {
11409 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11410
11411 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11412 {
11413 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11414 " in section table [in module %s]"),
11415 id, dwp_file->name);
11416 }
11417 if (ids_seen[id] != -1)
11418 {
11419 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11420 " id %d in section table [in module %s]"),
11421 id, dwp_file->name);
11422 }
11423 ids_seen[id] = i;
11424 ids[i] = id;
11425 }
11426 /* Must have exactly one info or types section. */
11427 if (((ids_seen[DW_SECT_INFO] != -1)
11428 + (ids_seen[DW_SECT_TYPES] != -1))
11429 != 1)
11430 {
11431 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11432 " DWO info/types section [in module %s]"),
11433 dwp_file->name);
11434 }
11435 /* Must have an abbrev section. */
11436 if (ids_seen[DW_SECT_ABBREV] == -1)
11437 {
11438 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11439 " section [in module %s]"),
11440 dwp_file->name);
11441 }
11442 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11443 htab->section_pool.v2.sizes =
11444 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11445 * nr_units * nr_columns);
11446 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11447 * nr_units * nr_columns))
11448 > index_end)
11449 {
11450 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11451 " [in module %s]"),
11452 dwp_file->name);
11453 }
11454 }
11455
11456 return htab;
11457 }
11458
11459 /* Update SECTIONS with the data from SECTP.
11460
11461 This function is like the other "locate" section routines that are
11462 passed to bfd_map_over_sections, but in this context the sections to
11463 read comes from the DWP V1 hash table, not the full ELF section table.
11464
11465 The result is non-zero for success, or zero if an error was found. */
11466
11467 static int
11468 locate_v1_virtual_dwo_sections (asection *sectp,
11469 struct virtual_v1_dwo_sections *sections)
11470 {
11471 const struct dwop_section_names *names = &dwop_section_names;
11472
11473 if (section_is_p (sectp->name, &names->abbrev_dwo))
11474 {
11475 /* There can be only one. */
11476 if (sections->abbrev.s.section != NULL)
11477 return 0;
11478 sections->abbrev.s.section = sectp;
11479 sections->abbrev.size = bfd_section_size (sectp);
11480 }
11481 else if (section_is_p (sectp->name, &names->info_dwo)
11482 || section_is_p (sectp->name, &names->types_dwo))
11483 {
11484 /* There can be only one. */
11485 if (sections->info_or_types.s.section != NULL)
11486 return 0;
11487 sections->info_or_types.s.section = sectp;
11488 sections->info_or_types.size = bfd_section_size (sectp);
11489 }
11490 else if (section_is_p (sectp->name, &names->line_dwo))
11491 {
11492 /* There can be only one. */
11493 if (sections->line.s.section != NULL)
11494 return 0;
11495 sections->line.s.section = sectp;
11496 sections->line.size = bfd_section_size (sectp);
11497 }
11498 else if (section_is_p (sectp->name, &names->loc_dwo))
11499 {
11500 /* There can be only one. */
11501 if (sections->loc.s.section != NULL)
11502 return 0;
11503 sections->loc.s.section = sectp;
11504 sections->loc.size = bfd_section_size (sectp);
11505 }
11506 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11507 {
11508 /* There can be only one. */
11509 if (sections->macinfo.s.section != NULL)
11510 return 0;
11511 sections->macinfo.s.section = sectp;
11512 sections->macinfo.size = bfd_section_size (sectp);
11513 }
11514 else if (section_is_p (sectp->name, &names->macro_dwo))
11515 {
11516 /* There can be only one. */
11517 if (sections->macro.s.section != NULL)
11518 return 0;
11519 sections->macro.s.section = sectp;
11520 sections->macro.size = bfd_section_size (sectp);
11521 }
11522 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11523 {
11524 /* There can be only one. */
11525 if (sections->str_offsets.s.section != NULL)
11526 return 0;
11527 sections->str_offsets.s.section = sectp;
11528 sections->str_offsets.size = bfd_section_size (sectp);
11529 }
11530 else
11531 {
11532 /* No other kind of section is valid. */
11533 return 0;
11534 }
11535
11536 return 1;
11537 }
11538
11539 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11540 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11541 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11542 This is for DWP version 1 files. */
11543
11544 static struct dwo_unit *
11545 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11546 struct dwp_file *dwp_file,
11547 uint32_t unit_index,
11548 const char *comp_dir,
11549 ULONGEST signature, int is_debug_types)
11550 {
11551 struct objfile *objfile = dwarf2_per_objfile->objfile;
11552 const struct dwp_hash_table *dwp_htab =
11553 is_debug_types ? dwp_file->tus : dwp_file->cus;
11554 bfd *dbfd = dwp_file->dbfd.get ();
11555 const char *kind = is_debug_types ? "TU" : "CU";
11556 struct dwo_file *dwo_file;
11557 struct dwo_unit *dwo_unit;
11558 struct virtual_v1_dwo_sections sections;
11559 void **dwo_file_slot;
11560 int i;
11561
11562 gdb_assert (dwp_file->version == 1);
11563
11564 if (dwarf_read_debug)
11565 {
11566 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11567 kind,
11568 pulongest (unit_index), hex_string (signature),
11569 dwp_file->name);
11570 }
11571
11572 /* Fetch the sections of this DWO unit.
11573 Put a limit on the number of sections we look for so that bad data
11574 doesn't cause us to loop forever. */
11575
11576 #define MAX_NR_V1_DWO_SECTIONS \
11577 (1 /* .debug_info or .debug_types */ \
11578 + 1 /* .debug_abbrev */ \
11579 + 1 /* .debug_line */ \
11580 + 1 /* .debug_loc */ \
11581 + 1 /* .debug_str_offsets */ \
11582 + 1 /* .debug_macro or .debug_macinfo */ \
11583 + 1 /* trailing zero */)
11584
11585 memset (&sections, 0, sizeof (sections));
11586
11587 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11588 {
11589 asection *sectp;
11590 uint32_t section_nr =
11591 read_4_bytes (dbfd,
11592 dwp_htab->section_pool.v1.indices
11593 + (unit_index + i) * sizeof (uint32_t));
11594
11595 if (section_nr == 0)
11596 break;
11597 if (section_nr >= dwp_file->num_sections)
11598 {
11599 error (_("Dwarf Error: bad DWP hash table, section number too large"
11600 " [in module %s]"),
11601 dwp_file->name);
11602 }
11603
11604 sectp = dwp_file->elf_sections[section_nr];
11605 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11606 {
11607 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11608 " [in module %s]"),
11609 dwp_file->name);
11610 }
11611 }
11612
11613 if (i < 2
11614 || sections.info_or_types.empty ()
11615 || sections.abbrev.empty ())
11616 {
11617 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11618 " [in module %s]"),
11619 dwp_file->name);
11620 }
11621 if (i == MAX_NR_V1_DWO_SECTIONS)
11622 {
11623 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11624 " [in module %s]"),
11625 dwp_file->name);
11626 }
11627
11628 /* It's easier for the rest of the code if we fake a struct dwo_file and
11629 have dwo_unit "live" in that. At least for now.
11630
11631 The DWP file can be made up of a random collection of CUs and TUs.
11632 However, for each CU + set of TUs that came from the same original DWO
11633 file, we can combine them back into a virtual DWO file to save space
11634 (fewer struct dwo_file objects to allocate). Remember that for really
11635 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11636
11637 std::string virtual_dwo_name =
11638 string_printf ("virtual-dwo/%d-%d-%d-%d",
11639 sections.abbrev.get_id (),
11640 sections.line.get_id (),
11641 sections.loc.get_id (),
11642 sections.str_offsets.get_id ());
11643 /* Can we use an existing virtual DWO file? */
11644 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11645 virtual_dwo_name.c_str (),
11646 comp_dir);
11647 /* Create one if necessary. */
11648 if (*dwo_file_slot == NULL)
11649 {
11650 if (dwarf_read_debug)
11651 {
11652 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11653 virtual_dwo_name.c_str ());
11654 }
11655 dwo_file = new struct dwo_file;
11656 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11657 virtual_dwo_name);
11658 dwo_file->comp_dir = comp_dir;
11659 dwo_file->sections.abbrev = sections.abbrev;
11660 dwo_file->sections.line = sections.line;
11661 dwo_file->sections.loc = sections.loc;
11662 dwo_file->sections.macinfo = sections.macinfo;
11663 dwo_file->sections.macro = sections.macro;
11664 dwo_file->sections.str_offsets = sections.str_offsets;
11665 /* The "str" section is global to the entire DWP file. */
11666 dwo_file->sections.str = dwp_file->sections.str;
11667 /* The info or types section is assigned below to dwo_unit,
11668 there's no need to record it in dwo_file.
11669 Also, we can't simply record type sections in dwo_file because
11670 we record a pointer into the vector in dwo_unit. As we collect more
11671 types we'll grow the vector and eventually have to reallocate space
11672 for it, invalidating all copies of pointers into the previous
11673 contents. */
11674 *dwo_file_slot = dwo_file;
11675 }
11676 else
11677 {
11678 if (dwarf_read_debug)
11679 {
11680 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11681 virtual_dwo_name.c_str ());
11682 }
11683 dwo_file = (struct dwo_file *) *dwo_file_slot;
11684 }
11685
11686 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11687 dwo_unit->dwo_file = dwo_file;
11688 dwo_unit->signature = signature;
11689 dwo_unit->section =
11690 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11691 *dwo_unit->section = sections.info_or_types;
11692 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11693
11694 return dwo_unit;
11695 }
11696
11697 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11698 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11699 piece within that section used by a TU/CU, return a virtual section
11700 of just that piece. */
11701
11702 static struct dwarf2_section_info
11703 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11704 struct dwarf2_section_info *section,
11705 bfd_size_type offset, bfd_size_type size)
11706 {
11707 struct dwarf2_section_info result;
11708 asection *sectp;
11709
11710 gdb_assert (section != NULL);
11711 gdb_assert (!section->is_virtual);
11712
11713 memset (&result, 0, sizeof (result));
11714 result.s.containing_section = section;
11715 result.is_virtual = true;
11716
11717 if (size == 0)
11718 return result;
11719
11720 sectp = section->get_bfd_section ();
11721
11722 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11723 bounds of the real section. This is a pretty-rare event, so just
11724 flag an error (easier) instead of a warning and trying to cope. */
11725 if (sectp == NULL
11726 || offset + size > bfd_section_size (sectp))
11727 {
11728 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11729 " in section %s [in module %s]"),
11730 sectp ? bfd_section_name (sectp) : "<unknown>",
11731 objfile_name (dwarf2_per_objfile->objfile));
11732 }
11733
11734 result.virtual_offset = offset;
11735 result.size = size;
11736 return result;
11737 }
11738
11739 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11740 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11741 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11742 This is for DWP version 2 files. */
11743
11744 static struct dwo_unit *
11745 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11746 struct dwp_file *dwp_file,
11747 uint32_t unit_index,
11748 const char *comp_dir,
11749 ULONGEST signature, int is_debug_types)
11750 {
11751 struct objfile *objfile = dwarf2_per_objfile->objfile;
11752 const struct dwp_hash_table *dwp_htab =
11753 is_debug_types ? dwp_file->tus : dwp_file->cus;
11754 bfd *dbfd = dwp_file->dbfd.get ();
11755 const char *kind = is_debug_types ? "TU" : "CU";
11756 struct dwo_file *dwo_file;
11757 struct dwo_unit *dwo_unit;
11758 struct virtual_v2_dwo_sections sections;
11759 void **dwo_file_slot;
11760 int i;
11761
11762 gdb_assert (dwp_file->version == 2);
11763
11764 if (dwarf_read_debug)
11765 {
11766 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11767 kind,
11768 pulongest (unit_index), hex_string (signature),
11769 dwp_file->name);
11770 }
11771
11772 /* Fetch the section offsets of this DWO unit. */
11773
11774 memset (&sections, 0, sizeof (sections));
11775
11776 for (i = 0; i < dwp_htab->nr_columns; ++i)
11777 {
11778 uint32_t offset = read_4_bytes (dbfd,
11779 dwp_htab->section_pool.v2.offsets
11780 + (((unit_index - 1) * dwp_htab->nr_columns
11781 + i)
11782 * sizeof (uint32_t)));
11783 uint32_t size = read_4_bytes (dbfd,
11784 dwp_htab->section_pool.v2.sizes
11785 + (((unit_index - 1) * dwp_htab->nr_columns
11786 + i)
11787 * sizeof (uint32_t)));
11788
11789 switch (dwp_htab->section_pool.v2.section_ids[i])
11790 {
11791 case DW_SECT_INFO:
11792 case DW_SECT_TYPES:
11793 sections.info_or_types_offset = offset;
11794 sections.info_or_types_size = size;
11795 break;
11796 case DW_SECT_ABBREV:
11797 sections.abbrev_offset = offset;
11798 sections.abbrev_size = size;
11799 break;
11800 case DW_SECT_LINE:
11801 sections.line_offset = offset;
11802 sections.line_size = size;
11803 break;
11804 case DW_SECT_LOC:
11805 sections.loc_offset = offset;
11806 sections.loc_size = size;
11807 break;
11808 case DW_SECT_STR_OFFSETS:
11809 sections.str_offsets_offset = offset;
11810 sections.str_offsets_size = size;
11811 break;
11812 case DW_SECT_MACINFO:
11813 sections.macinfo_offset = offset;
11814 sections.macinfo_size = size;
11815 break;
11816 case DW_SECT_MACRO:
11817 sections.macro_offset = offset;
11818 sections.macro_size = size;
11819 break;
11820 }
11821 }
11822
11823 /* It's easier for the rest of the code if we fake a struct dwo_file and
11824 have dwo_unit "live" in that. At least for now.
11825
11826 The DWP file can be made up of a random collection of CUs and TUs.
11827 However, for each CU + set of TUs that came from the same original DWO
11828 file, we can combine them back into a virtual DWO file to save space
11829 (fewer struct dwo_file objects to allocate). Remember that for really
11830 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11831
11832 std::string virtual_dwo_name =
11833 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11834 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11835 (long) (sections.line_size ? sections.line_offset : 0),
11836 (long) (sections.loc_size ? sections.loc_offset : 0),
11837 (long) (sections.str_offsets_size
11838 ? sections.str_offsets_offset : 0));
11839 /* Can we use an existing virtual DWO file? */
11840 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11841 virtual_dwo_name.c_str (),
11842 comp_dir);
11843 /* Create one if necessary. */
11844 if (*dwo_file_slot == NULL)
11845 {
11846 if (dwarf_read_debug)
11847 {
11848 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11849 virtual_dwo_name.c_str ());
11850 }
11851 dwo_file = new struct dwo_file;
11852 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11853 virtual_dwo_name);
11854 dwo_file->comp_dir = comp_dir;
11855 dwo_file->sections.abbrev =
11856 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11857 sections.abbrev_offset, sections.abbrev_size);
11858 dwo_file->sections.line =
11859 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11860 sections.line_offset, sections.line_size);
11861 dwo_file->sections.loc =
11862 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11863 sections.loc_offset, sections.loc_size);
11864 dwo_file->sections.macinfo =
11865 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11866 sections.macinfo_offset, sections.macinfo_size);
11867 dwo_file->sections.macro =
11868 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11869 sections.macro_offset, sections.macro_size);
11870 dwo_file->sections.str_offsets =
11871 create_dwp_v2_section (dwarf2_per_objfile,
11872 &dwp_file->sections.str_offsets,
11873 sections.str_offsets_offset,
11874 sections.str_offsets_size);
11875 /* The "str" section is global to the entire DWP file. */
11876 dwo_file->sections.str = dwp_file->sections.str;
11877 /* The info or types section is assigned below to dwo_unit,
11878 there's no need to record it in dwo_file.
11879 Also, we can't simply record type sections in dwo_file because
11880 we record a pointer into the vector in dwo_unit. As we collect more
11881 types we'll grow the vector and eventually have to reallocate space
11882 for it, invalidating all copies of pointers into the previous
11883 contents. */
11884 *dwo_file_slot = dwo_file;
11885 }
11886 else
11887 {
11888 if (dwarf_read_debug)
11889 {
11890 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11891 virtual_dwo_name.c_str ());
11892 }
11893 dwo_file = (struct dwo_file *) *dwo_file_slot;
11894 }
11895
11896 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11897 dwo_unit->dwo_file = dwo_file;
11898 dwo_unit->signature = signature;
11899 dwo_unit->section =
11900 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11901 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11902 is_debug_types
11903 ? &dwp_file->sections.types
11904 : &dwp_file->sections.info,
11905 sections.info_or_types_offset,
11906 sections.info_or_types_size);
11907 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11908
11909 return dwo_unit;
11910 }
11911
11912 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11913 Returns NULL if the signature isn't found. */
11914
11915 static struct dwo_unit *
11916 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11917 struct dwp_file *dwp_file, const char *comp_dir,
11918 ULONGEST signature, int is_debug_types)
11919 {
11920 const struct dwp_hash_table *dwp_htab =
11921 is_debug_types ? dwp_file->tus : dwp_file->cus;
11922 bfd *dbfd = dwp_file->dbfd.get ();
11923 uint32_t mask = dwp_htab->nr_slots - 1;
11924 uint32_t hash = signature & mask;
11925 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11926 unsigned int i;
11927 void **slot;
11928 struct dwo_unit find_dwo_cu;
11929
11930 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11931 find_dwo_cu.signature = signature;
11932 slot = htab_find_slot (is_debug_types
11933 ? dwp_file->loaded_tus.get ()
11934 : dwp_file->loaded_cus.get (),
11935 &find_dwo_cu, INSERT);
11936
11937 if (*slot != NULL)
11938 return (struct dwo_unit *) *slot;
11939
11940 /* Use a for loop so that we don't loop forever on bad debug info. */
11941 for (i = 0; i < dwp_htab->nr_slots; ++i)
11942 {
11943 ULONGEST signature_in_table;
11944
11945 signature_in_table =
11946 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11947 if (signature_in_table == signature)
11948 {
11949 uint32_t unit_index =
11950 read_4_bytes (dbfd,
11951 dwp_htab->unit_table + hash * sizeof (uint32_t));
11952
11953 if (dwp_file->version == 1)
11954 {
11955 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11956 dwp_file, unit_index,
11957 comp_dir, signature,
11958 is_debug_types);
11959 }
11960 else
11961 {
11962 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11963 dwp_file, unit_index,
11964 comp_dir, signature,
11965 is_debug_types);
11966 }
11967 return (struct dwo_unit *) *slot;
11968 }
11969 if (signature_in_table == 0)
11970 return NULL;
11971 hash = (hash + hash2) & mask;
11972 }
11973
11974 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11975 " [in module %s]"),
11976 dwp_file->name);
11977 }
11978
11979 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11980 Open the file specified by FILE_NAME and hand it off to BFD for
11981 preliminary analysis. Return a newly initialized bfd *, which
11982 includes a canonicalized copy of FILE_NAME.
11983 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11984 SEARCH_CWD is true if the current directory is to be searched.
11985 It will be searched before debug-file-directory.
11986 If successful, the file is added to the bfd include table of the
11987 objfile's bfd (see gdb_bfd_record_inclusion).
11988 If unable to find/open the file, return NULL.
11989 NOTE: This function is derived from symfile_bfd_open. */
11990
11991 static gdb_bfd_ref_ptr
11992 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11993 const char *file_name, int is_dwp, int search_cwd)
11994 {
11995 int desc;
11996 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11997 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11998 to debug_file_directory. */
11999 const char *search_path;
12000 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12001
12002 gdb::unique_xmalloc_ptr<char> search_path_holder;
12003 if (search_cwd)
12004 {
12005 if (*debug_file_directory != '\0')
12006 {
12007 search_path_holder.reset (concat (".", dirname_separator_string,
12008 debug_file_directory,
12009 (char *) NULL));
12010 search_path = search_path_holder.get ();
12011 }
12012 else
12013 search_path = ".";
12014 }
12015 else
12016 search_path = debug_file_directory;
12017
12018 openp_flags flags = OPF_RETURN_REALPATH;
12019 if (is_dwp)
12020 flags |= OPF_SEARCH_IN_PATH;
12021
12022 gdb::unique_xmalloc_ptr<char> absolute_name;
12023 desc = openp (search_path, flags, file_name,
12024 O_RDONLY | O_BINARY, &absolute_name);
12025 if (desc < 0)
12026 return NULL;
12027
12028 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12029 gnutarget, desc));
12030 if (sym_bfd == NULL)
12031 return NULL;
12032 bfd_set_cacheable (sym_bfd.get (), 1);
12033
12034 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12035 return NULL;
12036
12037 /* Success. Record the bfd as having been included by the objfile's bfd.
12038 This is important because things like demangled_names_hash lives in the
12039 objfile's per_bfd space and may have references to things like symbol
12040 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12041 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12042
12043 return sym_bfd;
12044 }
12045
12046 /* Try to open DWO file FILE_NAME.
12047 COMP_DIR is the DW_AT_comp_dir attribute.
12048 The result is the bfd handle of the file.
12049 If there is a problem finding or opening the file, return NULL.
12050 Upon success, the canonicalized path of the file is stored in the bfd,
12051 same as symfile_bfd_open. */
12052
12053 static gdb_bfd_ref_ptr
12054 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12055 const char *file_name, const char *comp_dir)
12056 {
12057 if (IS_ABSOLUTE_PATH (file_name))
12058 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12059 0 /*is_dwp*/, 0 /*search_cwd*/);
12060
12061 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12062
12063 if (comp_dir != NULL)
12064 {
12065 gdb::unique_xmalloc_ptr<char> path_to_try
12066 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12067
12068 /* NOTE: If comp_dir is a relative path, this will also try the
12069 search path, which seems useful. */
12070 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12071 path_to_try.get (),
12072 0 /*is_dwp*/,
12073 1 /*search_cwd*/));
12074 if (abfd != NULL)
12075 return abfd;
12076 }
12077
12078 /* That didn't work, try debug-file-directory, which, despite its name,
12079 is a list of paths. */
12080
12081 if (*debug_file_directory == '\0')
12082 return NULL;
12083
12084 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12085 0 /*is_dwp*/, 1 /*search_cwd*/);
12086 }
12087
12088 /* This function is mapped across the sections and remembers the offset and
12089 size of each of the DWO debugging sections we are interested in. */
12090
12091 static void
12092 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12093 {
12094 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12095 const struct dwop_section_names *names = &dwop_section_names;
12096
12097 if (section_is_p (sectp->name, &names->abbrev_dwo))
12098 {
12099 dwo_sections->abbrev.s.section = sectp;
12100 dwo_sections->abbrev.size = bfd_section_size (sectp);
12101 }
12102 else if (section_is_p (sectp->name, &names->info_dwo))
12103 {
12104 dwo_sections->info.s.section = sectp;
12105 dwo_sections->info.size = bfd_section_size (sectp);
12106 }
12107 else if (section_is_p (sectp->name, &names->line_dwo))
12108 {
12109 dwo_sections->line.s.section = sectp;
12110 dwo_sections->line.size = bfd_section_size (sectp);
12111 }
12112 else if (section_is_p (sectp->name, &names->loc_dwo))
12113 {
12114 dwo_sections->loc.s.section = sectp;
12115 dwo_sections->loc.size = bfd_section_size (sectp);
12116 }
12117 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12118 {
12119 dwo_sections->macinfo.s.section = sectp;
12120 dwo_sections->macinfo.size = bfd_section_size (sectp);
12121 }
12122 else if (section_is_p (sectp->name, &names->macro_dwo))
12123 {
12124 dwo_sections->macro.s.section = sectp;
12125 dwo_sections->macro.size = bfd_section_size (sectp);
12126 }
12127 else if (section_is_p (sectp->name, &names->str_dwo))
12128 {
12129 dwo_sections->str.s.section = sectp;
12130 dwo_sections->str.size = bfd_section_size (sectp);
12131 }
12132 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12133 {
12134 dwo_sections->str_offsets.s.section = sectp;
12135 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12136 }
12137 else if (section_is_p (sectp->name, &names->types_dwo))
12138 {
12139 struct dwarf2_section_info type_section;
12140
12141 memset (&type_section, 0, sizeof (type_section));
12142 type_section.s.section = sectp;
12143 type_section.size = bfd_section_size (sectp);
12144 dwo_sections->types.push_back (type_section);
12145 }
12146 }
12147
12148 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12149 by PER_CU. This is for the non-DWP case.
12150 The result is NULL if DWO_NAME can't be found. */
12151
12152 static struct dwo_file *
12153 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12154 const char *dwo_name, const char *comp_dir)
12155 {
12156 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12157
12158 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12159 if (dbfd == NULL)
12160 {
12161 if (dwarf_read_debug)
12162 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12163 return NULL;
12164 }
12165
12166 dwo_file_up dwo_file (new struct dwo_file);
12167 dwo_file->dwo_name = dwo_name;
12168 dwo_file->comp_dir = comp_dir;
12169 dwo_file->dbfd = std::move (dbfd);
12170
12171 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12172 &dwo_file->sections);
12173
12174 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12175 dwo_file->sections.info, dwo_file->cus);
12176
12177 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12178 dwo_file->sections.types, dwo_file->tus);
12179
12180 if (dwarf_read_debug)
12181 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12182
12183 return dwo_file.release ();
12184 }
12185
12186 /* This function is mapped across the sections and remembers the offset and
12187 size of each of the DWP debugging sections common to version 1 and 2 that
12188 we are interested in. */
12189
12190 static void
12191 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12192 void *dwp_file_ptr)
12193 {
12194 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12195 const struct dwop_section_names *names = &dwop_section_names;
12196 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12197
12198 /* Record the ELF section number for later lookup: this is what the
12199 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12200 gdb_assert (elf_section_nr < dwp_file->num_sections);
12201 dwp_file->elf_sections[elf_section_nr] = sectp;
12202
12203 /* Look for specific sections that we need. */
12204 if (section_is_p (sectp->name, &names->str_dwo))
12205 {
12206 dwp_file->sections.str.s.section = sectp;
12207 dwp_file->sections.str.size = bfd_section_size (sectp);
12208 }
12209 else if (section_is_p (sectp->name, &names->cu_index))
12210 {
12211 dwp_file->sections.cu_index.s.section = sectp;
12212 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12213 }
12214 else if (section_is_p (sectp->name, &names->tu_index))
12215 {
12216 dwp_file->sections.tu_index.s.section = sectp;
12217 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12218 }
12219 }
12220
12221 /* This function is mapped across the sections and remembers the offset and
12222 size of each of the DWP version 2 debugging sections that we are interested
12223 in. This is split into a separate function because we don't know if we
12224 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12225
12226 static void
12227 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12228 {
12229 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12230 const struct dwop_section_names *names = &dwop_section_names;
12231 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12232
12233 /* Record the ELF section number for later lookup: this is what the
12234 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12235 gdb_assert (elf_section_nr < dwp_file->num_sections);
12236 dwp_file->elf_sections[elf_section_nr] = sectp;
12237
12238 /* Look for specific sections that we need. */
12239 if (section_is_p (sectp->name, &names->abbrev_dwo))
12240 {
12241 dwp_file->sections.abbrev.s.section = sectp;
12242 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12243 }
12244 else if (section_is_p (sectp->name, &names->info_dwo))
12245 {
12246 dwp_file->sections.info.s.section = sectp;
12247 dwp_file->sections.info.size = bfd_section_size (sectp);
12248 }
12249 else if (section_is_p (sectp->name, &names->line_dwo))
12250 {
12251 dwp_file->sections.line.s.section = sectp;
12252 dwp_file->sections.line.size = bfd_section_size (sectp);
12253 }
12254 else if (section_is_p (sectp->name, &names->loc_dwo))
12255 {
12256 dwp_file->sections.loc.s.section = sectp;
12257 dwp_file->sections.loc.size = bfd_section_size (sectp);
12258 }
12259 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12260 {
12261 dwp_file->sections.macinfo.s.section = sectp;
12262 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12263 }
12264 else if (section_is_p (sectp->name, &names->macro_dwo))
12265 {
12266 dwp_file->sections.macro.s.section = sectp;
12267 dwp_file->sections.macro.size = bfd_section_size (sectp);
12268 }
12269 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12270 {
12271 dwp_file->sections.str_offsets.s.section = sectp;
12272 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12273 }
12274 else if (section_is_p (sectp->name, &names->types_dwo))
12275 {
12276 dwp_file->sections.types.s.section = sectp;
12277 dwp_file->sections.types.size = bfd_section_size (sectp);
12278 }
12279 }
12280
12281 /* Hash function for dwp_file loaded CUs/TUs. */
12282
12283 static hashval_t
12284 hash_dwp_loaded_cutus (const void *item)
12285 {
12286 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12287
12288 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12289 return dwo_unit->signature;
12290 }
12291
12292 /* Equality function for dwp_file loaded CUs/TUs. */
12293
12294 static int
12295 eq_dwp_loaded_cutus (const void *a, const void *b)
12296 {
12297 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12298 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12299
12300 return dua->signature == dub->signature;
12301 }
12302
12303 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12304
12305 static htab_up
12306 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12307 {
12308 return htab_up (htab_create_alloc (3,
12309 hash_dwp_loaded_cutus,
12310 eq_dwp_loaded_cutus,
12311 NULL, xcalloc, xfree));
12312 }
12313
12314 /* Try to open DWP file FILE_NAME.
12315 The result is the bfd handle of the file.
12316 If there is a problem finding or opening the file, return NULL.
12317 Upon success, the canonicalized path of the file is stored in the bfd,
12318 same as symfile_bfd_open. */
12319
12320 static gdb_bfd_ref_ptr
12321 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12322 const char *file_name)
12323 {
12324 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12325 1 /*is_dwp*/,
12326 1 /*search_cwd*/));
12327 if (abfd != NULL)
12328 return abfd;
12329
12330 /* Work around upstream bug 15652.
12331 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12332 [Whether that's a "bug" is debatable, but it is getting in our way.]
12333 We have no real idea where the dwp file is, because gdb's realpath-ing
12334 of the executable's path may have discarded the needed info.
12335 [IWBN if the dwp file name was recorded in the executable, akin to
12336 .gnu_debuglink, but that doesn't exist yet.]
12337 Strip the directory from FILE_NAME and search again. */
12338 if (*debug_file_directory != '\0')
12339 {
12340 /* Don't implicitly search the current directory here.
12341 If the user wants to search "." to handle this case,
12342 it must be added to debug-file-directory. */
12343 return try_open_dwop_file (dwarf2_per_objfile,
12344 lbasename (file_name), 1 /*is_dwp*/,
12345 0 /*search_cwd*/);
12346 }
12347
12348 return NULL;
12349 }
12350
12351 /* Initialize the use of the DWP file for the current objfile.
12352 By convention the name of the DWP file is ${objfile}.dwp.
12353 The result is NULL if it can't be found. */
12354
12355 static std::unique_ptr<struct dwp_file>
12356 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12357 {
12358 struct objfile *objfile = dwarf2_per_objfile->objfile;
12359
12360 /* Try to find first .dwp for the binary file before any symbolic links
12361 resolving. */
12362
12363 /* If the objfile is a debug file, find the name of the real binary
12364 file and get the name of dwp file from there. */
12365 std::string dwp_name;
12366 if (objfile->separate_debug_objfile_backlink != NULL)
12367 {
12368 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12369 const char *backlink_basename = lbasename (backlink->original_name);
12370
12371 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12372 }
12373 else
12374 dwp_name = objfile->original_name;
12375
12376 dwp_name += ".dwp";
12377
12378 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12379 if (dbfd == NULL
12380 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12381 {
12382 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12383 dwp_name = objfile_name (objfile);
12384 dwp_name += ".dwp";
12385 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12386 }
12387
12388 if (dbfd == NULL)
12389 {
12390 if (dwarf_read_debug)
12391 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12392 return std::unique_ptr<dwp_file> ();
12393 }
12394
12395 const char *name = bfd_get_filename (dbfd.get ());
12396 std::unique_ptr<struct dwp_file> dwp_file
12397 (new struct dwp_file (name, std::move (dbfd)));
12398
12399 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12400 dwp_file->elf_sections =
12401 OBSTACK_CALLOC (&objfile->objfile_obstack,
12402 dwp_file->num_sections, asection *);
12403
12404 bfd_map_over_sections (dwp_file->dbfd.get (),
12405 dwarf2_locate_common_dwp_sections,
12406 dwp_file.get ());
12407
12408 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12409 0);
12410
12411 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12412 1);
12413
12414 /* The DWP file version is stored in the hash table. Oh well. */
12415 if (dwp_file->cus && dwp_file->tus
12416 && dwp_file->cus->version != dwp_file->tus->version)
12417 {
12418 /* Technically speaking, we should try to limp along, but this is
12419 pretty bizarre. We use pulongest here because that's the established
12420 portability solution (e.g, we cannot use %u for uint32_t). */
12421 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12422 " TU version %s [in DWP file %s]"),
12423 pulongest (dwp_file->cus->version),
12424 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12425 }
12426
12427 if (dwp_file->cus)
12428 dwp_file->version = dwp_file->cus->version;
12429 else if (dwp_file->tus)
12430 dwp_file->version = dwp_file->tus->version;
12431 else
12432 dwp_file->version = 2;
12433
12434 if (dwp_file->version == 2)
12435 bfd_map_over_sections (dwp_file->dbfd.get (),
12436 dwarf2_locate_v2_dwp_sections,
12437 dwp_file.get ());
12438
12439 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12440 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12441
12442 if (dwarf_read_debug)
12443 {
12444 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12445 fprintf_unfiltered (gdb_stdlog,
12446 " %s CUs, %s TUs\n",
12447 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12448 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12449 }
12450
12451 return dwp_file;
12452 }
12453
12454 /* Wrapper around open_and_init_dwp_file, only open it once. */
12455
12456 static struct dwp_file *
12457 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12458 {
12459 if (! dwarf2_per_objfile->dwp_checked)
12460 {
12461 dwarf2_per_objfile->dwp_file
12462 = open_and_init_dwp_file (dwarf2_per_objfile);
12463 dwarf2_per_objfile->dwp_checked = 1;
12464 }
12465 return dwarf2_per_objfile->dwp_file.get ();
12466 }
12467
12468 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12469 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12470 or in the DWP file for the objfile, referenced by THIS_UNIT.
12471 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12472 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12473
12474 This is called, for example, when wanting to read a variable with a
12475 complex location. Therefore we don't want to do file i/o for every call.
12476 Therefore we don't want to look for a DWO file on every call.
12477 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12478 then we check if we've already seen DWO_NAME, and only THEN do we check
12479 for a DWO file.
12480
12481 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12482 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12483
12484 static struct dwo_unit *
12485 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12486 const char *dwo_name, const char *comp_dir,
12487 ULONGEST signature, int is_debug_types)
12488 {
12489 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12490 struct objfile *objfile = dwarf2_per_objfile->objfile;
12491 const char *kind = is_debug_types ? "TU" : "CU";
12492 void **dwo_file_slot;
12493 struct dwo_file *dwo_file;
12494 struct dwp_file *dwp_file;
12495
12496 /* First see if there's a DWP file.
12497 If we have a DWP file but didn't find the DWO inside it, don't
12498 look for the original DWO file. It makes gdb behave differently
12499 depending on whether one is debugging in the build tree. */
12500
12501 dwp_file = get_dwp_file (dwarf2_per_objfile);
12502 if (dwp_file != NULL)
12503 {
12504 const struct dwp_hash_table *dwp_htab =
12505 is_debug_types ? dwp_file->tus : dwp_file->cus;
12506
12507 if (dwp_htab != NULL)
12508 {
12509 struct dwo_unit *dwo_cutu =
12510 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12511 signature, is_debug_types);
12512
12513 if (dwo_cutu != NULL)
12514 {
12515 if (dwarf_read_debug)
12516 {
12517 fprintf_unfiltered (gdb_stdlog,
12518 "Virtual DWO %s %s found: @%s\n",
12519 kind, hex_string (signature),
12520 host_address_to_string (dwo_cutu));
12521 }
12522 return dwo_cutu;
12523 }
12524 }
12525 }
12526 else
12527 {
12528 /* No DWP file, look for the DWO file. */
12529
12530 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12531 dwo_name, comp_dir);
12532 if (*dwo_file_slot == NULL)
12533 {
12534 /* Read in the file and build a table of the CUs/TUs it contains. */
12535 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12536 }
12537 /* NOTE: This will be NULL if unable to open the file. */
12538 dwo_file = (struct dwo_file *) *dwo_file_slot;
12539
12540 if (dwo_file != NULL)
12541 {
12542 struct dwo_unit *dwo_cutu = NULL;
12543
12544 if (is_debug_types && dwo_file->tus)
12545 {
12546 struct dwo_unit find_dwo_cutu;
12547
12548 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12549 find_dwo_cutu.signature = signature;
12550 dwo_cutu
12551 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12552 &find_dwo_cutu);
12553 }
12554 else if (!is_debug_types && dwo_file->cus)
12555 {
12556 struct dwo_unit find_dwo_cutu;
12557
12558 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12559 find_dwo_cutu.signature = signature;
12560 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12561 &find_dwo_cutu);
12562 }
12563
12564 if (dwo_cutu != NULL)
12565 {
12566 if (dwarf_read_debug)
12567 {
12568 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12569 kind, dwo_name, hex_string (signature),
12570 host_address_to_string (dwo_cutu));
12571 }
12572 return dwo_cutu;
12573 }
12574 }
12575 }
12576
12577 /* We didn't find it. This could mean a dwo_id mismatch, or
12578 someone deleted the DWO/DWP file, or the search path isn't set up
12579 correctly to find the file. */
12580
12581 if (dwarf_read_debug)
12582 {
12583 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12584 kind, dwo_name, hex_string (signature));
12585 }
12586
12587 /* This is a warning and not a complaint because it can be caused by
12588 pilot error (e.g., user accidentally deleting the DWO). */
12589 {
12590 /* Print the name of the DWP file if we looked there, helps the user
12591 better diagnose the problem. */
12592 std::string dwp_text;
12593
12594 if (dwp_file != NULL)
12595 dwp_text = string_printf (" [in DWP file %s]",
12596 lbasename (dwp_file->name));
12597
12598 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12599 " [in module %s]"),
12600 kind, dwo_name, hex_string (signature),
12601 dwp_text.c_str (),
12602 this_unit->is_debug_types ? "TU" : "CU",
12603 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12604 }
12605 return NULL;
12606 }
12607
12608 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12609 See lookup_dwo_cutu_unit for details. */
12610
12611 static struct dwo_unit *
12612 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12613 const char *dwo_name, const char *comp_dir,
12614 ULONGEST signature)
12615 {
12616 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12617 }
12618
12619 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12620 See lookup_dwo_cutu_unit for details. */
12621
12622 static struct dwo_unit *
12623 lookup_dwo_type_unit (struct signatured_type *this_tu,
12624 const char *dwo_name, const char *comp_dir)
12625 {
12626 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12627 }
12628
12629 /* Traversal function for queue_and_load_all_dwo_tus. */
12630
12631 static int
12632 queue_and_load_dwo_tu (void **slot, void *info)
12633 {
12634 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12635 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12636 ULONGEST signature = dwo_unit->signature;
12637 struct signatured_type *sig_type =
12638 lookup_dwo_signatured_type (per_cu->cu, signature);
12639
12640 if (sig_type != NULL)
12641 {
12642 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12643
12644 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12645 a real dependency of PER_CU on SIG_TYPE. That is detected later
12646 while processing PER_CU. */
12647 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12648 load_full_type_unit (sig_cu);
12649 per_cu->imported_symtabs_push (sig_cu);
12650 }
12651
12652 return 1;
12653 }
12654
12655 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12656 The DWO may have the only definition of the type, though it may not be
12657 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12658 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12659
12660 static void
12661 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12662 {
12663 struct dwo_unit *dwo_unit;
12664 struct dwo_file *dwo_file;
12665
12666 gdb_assert (!per_cu->is_debug_types);
12667 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12668 gdb_assert (per_cu->cu != NULL);
12669
12670 dwo_unit = per_cu->cu->dwo_unit;
12671 gdb_assert (dwo_unit != NULL);
12672
12673 dwo_file = dwo_unit->dwo_file;
12674 if (dwo_file->tus != NULL)
12675 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12676 per_cu);
12677 }
12678
12679 /* Read in various DIEs. */
12680
12681 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12682 Inherit only the children of the DW_AT_abstract_origin DIE not being
12683 already referenced by DW_AT_abstract_origin from the children of the
12684 current DIE. */
12685
12686 static void
12687 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12688 {
12689 struct die_info *child_die;
12690 sect_offset *offsetp;
12691 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12692 struct die_info *origin_die;
12693 /* Iterator of the ORIGIN_DIE children. */
12694 struct die_info *origin_child_die;
12695 struct attribute *attr;
12696 struct dwarf2_cu *origin_cu;
12697 struct pending **origin_previous_list_in_scope;
12698
12699 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12700 if (!attr)
12701 return;
12702
12703 /* Note that following die references may follow to a die in a
12704 different cu. */
12705
12706 origin_cu = cu;
12707 origin_die = follow_die_ref (die, attr, &origin_cu);
12708
12709 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12710 symbols in. */
12711 origin_previous_list_in_scope = origin_cu->list_in_scope;
12712 origin_cu->list_in_scope = cu->list_in_scope;
12713
12714 if (die->tag != origin_die->tag
12715 && !(die->tag == DW_TAG_inlined_subroutine
12716 && origin_die->tag == DW_TAG_subprogram))
12717 complaint (_("DIE %s and its abstract origin %s have different tags"),
12718 sect_offset_str (die->sect_off),
12719 sect_offset_str (origin_die->sect_off));
12720
12721 std::vector<sect_offset> offsets;
12722
12723 for (child_die = die->child;
12724 child_die && child_die->tag;
12725 child_die = sibling_die (child_die))
12726 {
12727 struct die_info *child_origin_die;
12728 struct dwarf2_cu *child_origin_cu;
12729
12730 /* We are trying to process concrete instance entries:
12731 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12732 it's not relevant to our analysis here. i.e. detecting DIEs that are
12733 present in the abstract instance but not referenced in the concrete
12734 one. */
12735 if (child_die->tag == DW_TAG_call_site
12736 || child_die->tag == DW_TAG_GNU_call_site)
12737 continue;
12738
12739 /* For each CHILD_DIE, find the corresponding child of
12740 ORIGIN_DIE. If there is more than one layer of
12741 DW_AT_abstract_origin, follow them all; there shouldn't be,
12742 but GCC versions at least through 4.4 generate this (GCC PR
12743 40573). */
12744 child_origin_die = child_die;
12745 child_origin_cu = cu;
12746 while (1)
12747 {
12748 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12749 child_origin_cu);
12750 if (attr == NULL)
12751 break;
12752 child_origin_die = follow_die_ref (child_origin_die, attr,
12753 &child_origin_cu);
12754 }
12755
12756 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12757 counterpart may exist. */
12758 if (child_origin_die != child_die)
12759 {
12760 if (child_die->tag != child_origin_die->tag
12761 && !(child_die->tag == DW_TAG_inlined_subroutine
12762 && child_origin_die->tag == DW_TAG_subprogram))
12763 complaint (_("Child DIE %s and its abstract origin %s have "
12764 "different tags"),
12765 sect_offset_str (child_die->sect_off),
12766 sect_offset_str (child_origin_die->sect_off));
12767 if (child_origin_die->parent != origin_die)
12768 complaint (_("Child DIE %s and its abstract origin %s have "
12769 "different parents"),
12770 sect_offset_str (child_die->sect_off),
12771 sect_offset_str (child_origin_die->sect_off));
12772 else
12773 offsets.push_back (child_origin_die->sect_off);
12774 }
12775 }
12776 std::sort (offsets.begin (), offsets.end ());
12777 sect_offset *offsets_end = offsets.data () + offsets.size ();
12778 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12779 if (offsetp[-1] == *offsetp)
12780 complaint (_("Multiple children of DIE %s refer "
12781 "to DIE %s as their abstract origin"),
12782 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12783
12784 offsetp = offsets.data ();
12785 origin_child_die = origin_die->child;
12786 while (origin_child_die && origin_child_die->tag)
12787 {
12788 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12789 while (offsetp < offsets_end
12790 && *offsetp < origin_child_die->sect_off)
12791 offsetp++;
12792 if (offsetp >= offsets_end
12793 || *offsetp > origin_child_die->sect_off)
12794 {
12795 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12796 Check whether we're already processing ORIGIN_CHILD_DIE.
12797 This can happen with mutually referenced abstract_origins.
12798 PR 16581. */
12799 if (!origin_child_die->in_process)
12800 process_die (origin_child_die, origin_cu);
12801 }
12802 origin_child_die = sibling_die (origin_child_die);
12803 }
12804 origin_cu->list_in_scope = origin_previous_list_in_scope;
12805
12806 if (cu != origin_cu)
12807 compute_delayed_physnames (origin_cu);
12808 }
12809
12810 static void
12811 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12812 {
12813 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12814 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12815 struct context_stack *newobj;
12816 CORE_ADDR lowpc;
12817 CORE_ADDR highpc;
12818 struct die_info *child_die;
12819 struct attribute *attr, *call_line, *call_file;
12820 const char *name;
12821 CORE_ADDR baseaddr;
12822 struct block *block;
12823 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12824 std::vector<struct symbol *> template_args;
12825 struct template_symbol *templ_func = NULL;
12826
12827 if (inlined_func)
12828 {
12829 /* If we do not have call site information, we can't show the
12830 caller of this inlined function. That's too confusing, so
12831 only use the scope for local variables. */
12832 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12833 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12834 if (call_line == NULL || call_file == NULL)
12835 {
12836 read_lexical_block_scope (die, cu);
12837 return;
12838 }
12839 }
12840
12841 baseaddr = objfile->text_section_offset ();
12842
12843 name = dwarf2_name (die, cu);
12844
12845 /* Ignore functions with missing or empty names. These are actually
12846 illegal according to the DWARF standard. */
12847 if (name == NULL)
12848 {
12849 complaint (_("missing name for subprogram DIE at %s"),
12850 sect_offset_str (die->sect_off));
12851 return;
12852 }
12853
12854 /* Ignore functions with missing or invalid low and high pc attributes. */
12855 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12856 <= PC_BOUNDS_INVALID)
12857 {
12858 attr = dwarf2_attr (die, DW_AT_external, cu);
12859 if (!attr || !DW_UNSND (attr))
12860 complaint (_("cannot get low and high bounds "
12861 "for subprogram DIE at %s"),
12862 sect_offset_str (die->sect_off));
12863 return;
12864 }
12865
12866 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12867 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12868
12869 /* If we have any template arguments, then we must allocate a
12870 different sort of symbol. */
12871 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12872 {
12873 if (child_die->tag == DW_TAG_template_type_param
12874 || child_die->tag == DW_TAG_template_value_param)
12875 {
12876 templ_func = allocate_template_symbol (objfile);
12877 templ_func->subclass = SYMBOL_TEMPLATE;
12878 break;
12879 }
12880 }
12881
12882 newobj = cu->get_builder ()->push_context (0, lowpc);
12883 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12884 (struct symbol *) templ_func);
12885
12886 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12887 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12888 cu->language);
12889
12890 /* If there is a location expression for DW_AT_frame_base, record
12891 it. */
12892 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12893 if (attr != nullptr)
12894 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12895
12896 /* If there is a location for the static link, record it. */
12897 newobj->static_link = NULL;
12898 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12899 if (attr != nullptr)
12900 {
12901 newobj->static_link
12902 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12903 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12904 cu->per_cu->addr_type ());
12905 }
12906
12907 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12908
12909 if (die->child != NULL)
12910 {
12911 child_die = die->child;
12912 while (child_die && child_die->tag)
12913 {
12914 if (child_die->tag == DW_TAG_template_type_param
12915 || child_die->tag == DW_TAG_template_value_param)
12916 {
12917 struct symbol *arg = new_symbol (child_die, NULL, cu);
12918
12919 if (arg != NULL)
12920 template_args.push_back (arg);
12921 }
12922 else
12923 process_die (child_die, cu);
12924 child_die = sibling_die (child_die);
12925 }
12926 }
12927
12928 inherit_abstract_dies (die, cu);
12929
12930 /* If we have a DW_AT_specification, we might need to import using
12931 directives from the context of the specification DIE. See the
12932 comment in determine_prefix. */
12933 if (cu->language == language_cplus
12934 && dwarf2_attr (die, DW_AT_specification, cu))
12935 {
12936 struct dwarf2_cu *spec_cu = cu;
12937 struct die_info *spec_die = die_specification (die, &spec_cu);
12938
12939 while (spec_die)
12940 {
12941 child_die = spec_die->child;
12942 while (child_die && child_die->tag)
12943 {
12944 if (child_die->tag == DW_TAG_imported_module)
12945 process_die (child_die, spec_cu);
12946 child_die = sibling_die (child_die);
12947 }
12948
12949 /* In some cases, GCC generates specification DIEs that
12950 themselves contain DW_AT_specification attributes. */
12951 spec_die = die_specification (spec_die, &spec_cu);
12952 }
12953 }
12954
12955 struct context_stack cstk = cu->get_builder ()->pop_context ();
12956 /* Make a block for the local symbols within. */
12957 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12958 cstk.static_link, lowpc, highpc);
12959
12960 /* For C++, set the block's scope. */
12961 if ((cu->language == language_cplus
12962 || cu->language == language_fortran
12963 || cu->language == language_d
12964 || cu->language == language_rust)
12965 && cu->processing_has_namespace_info)
12966 block_set_scope (block, determine_prefix (die, cu),
12967 &objfile->objfile_obstack);
12968
12969 /* If we have address ranges, record them. */
12970 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12971
12972 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12973
12974 /* Attach template arguments to function. */
12975 if (!template_args.empty ())
12976 {
12977 gdb_assert (templ_func != NULL);
12978
12979 templ_func->n_template_arguments = template_args.size ();
12980 templ_func->template_arguments
12981 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12982 templ_func->n_template_arguments);
12983 memcpy (templ_func->template_arguments,
12984 template_args.data (),
12985 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12986
12987 /* Make sure that the symtab is set on the new symbols. Even
12988 though they don't appear in this symtab directly, other parts
12989 of gdb assume that symbols do, and this is reasonably
12990 true. */
12991 for (symbol *sym : template_args)
12992 symbol_set_symtab (sym, symbol_symtab (templ_func));
12993 }
12994
12995 /* In C++, we can have functions nested inside functions (e.g., when
12996 a function declares a class that has methods). This means that
12997 when we finish processing a function scope, we may need to go
12998 back to building a containing block's symbol lists. */
12999 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13000 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13001
13002 /* If we've finished processing a top-level function, subsequent
13003 symbols go in the file symbol list. */
13004 if (cu->get_builder ()->outermost_context_p ())
13005 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13006 }
13007
13008 /* Process all the DIES contained within a lexical block scope. Start
13009 a new scope, process the dies, and then close the scope. */
13010
13011 static void
13012 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13013 {
13014 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13015 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13016 CORE_ADDR lowpc, highpc;
13017 struct die_info *child_die;
13018 CORE_ADDR baseaddr;
13019
13020 baseaddr = objfile->text_section_offset ();
13021
13022 /* Ignore blocks with missing or invalid low and high pc attributes. */
13023 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13024 as multiple lexical blocks? Handling children in a sane way would
13025 be nasty. Might be easier to properly extend generic blocks to
13026 describe ranges. */
13027 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13028 {
13029 case PC_BOUNDS_NOT_PRESENT:
13030 /* DW_TAG_lexical_block has no attributes, process its children as if
13031 there was no wrapping by that DW_TAG_lexical_block.
13032 GCC does no longer produces such DWARF since GCC r224161. */
13033 for (child_die = die->child;
13034 child_die != NULL && child_die->tag;
13035 child_die = sibling_die (child_die))
13036 process_die (child_die, cu);
13037 return;
13038 case PC_BOUNDS_INVALID:
13039 return;
13040 }
13041 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13042 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13043
13044 cu->get_builder ()->push_context (0, lowpc);
13045 if (die->child != NULL)
13046 {
13047 child_die = die->child;
13048 while (child_die && child_die->tag)
13049 {
13050 process_die (child_die, cu);
13051 child_die = sibling_die (child_die);
13052 }
13053 }
13054 inherit_abstract_dies (die, cu);
13055 struct context_stack cstk = cu->get_builder ()->pop_context ();
13056
13057 if (*cu->get_builder ()->get_local_symbols () != NULL
13058 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13059 {
13060 struct block *block
13061 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13062 cstk.start_addr, highpc);
13063
13064 /* Note that recording ranges after traversing children, as we
13065 do here, means that recording a parent's ranges entails
13066 walking across all its children's ranges as they appear in
13067 the address map, which is quadratic behavior.
13068
13069 It would be nicer to record the parent's ranges before
13070 traversing its children, simply overriding whatever you find
13071 there. But since we don't even decide whether to create a
13072 block until after we've traversed its children, that's hard
13073 to do. */
13074 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13075 }
13076 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13077 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13078 }
13079
13080 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13081
13082 static void
13083 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13084 {
13085 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13086 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13087 CORE_ADDR pc, baseaddr;
13088 struct attribute *attr;
13089 struct call_site *call_site, call_site_local;
13090 void **slot;
13091 int nparams;
13092 struct die_info *child_die;
13093
13094 baseaddr = objfile->text_section_offset ();
13095
13096 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13097 if (attr == NULL)
13098 {
13099 /* This was a pre-DWARF-5 GNU extension alias
13100 for DW_AT_call_return_pc. */
13101 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13102 }
13103 if (!attr)
13104 {
13105 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13106 "DIE %s [in module %s]"),
13107 sect_offset_str (die->sect_off), objfile_name (objfile));
13108 return;
13109 }
13110 pc = attr->value_as_address () + baseaddr;
13111 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13112
13113 if (cu->call_site_htab == NULL)
13114 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13115 NULL, &objfile->objfile_obstack,
13116 hashtab_obstack_allocate, NULL);
13117 call_site_local.pc = pc;
13118 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13119 if (*slot != NULL)
13120 {
13121 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13122 "DIE %s [in module %s]"),
13123 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13124 objfile_name (objfile));
13125 return;
13126 }
13127
13128 /* Count parameters at the caller. */
13129
13130 nparams = 0;
13131 for (child_die = die->child; child_die && child_die->tag;
13132 child_die = sibling_die (child_die))
13133 {
13134 if (child_die->tag != DW_TAG_call_site_parameter
13135 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13136 {
13137 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13138 "DW_TAG_call_site child DIE %s [in module %s]"),
13139 child_die->tag, sect_offset_str (child_die->sect_off),
13140 objfile_name (objfile));
13141 continue;
13142 }
13143
13144 nparams++;
13145 }
13146
13147 call_site
13148 = ((struct call_site *)
13149 obstack_alloc (&objfile->objfile_obstack,
13150 sizeof (*call_site)
13151 + (sizeof (*call_site->parameter) * (nparams - 1))));
13152 *slot = call_site;
13153 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13154 call_site->pc = pc;
13155
13156 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13157 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13158 {
13159 struct die_info *func_die;
13160
13161 /* Skip also over DW_TAG_inlined_subroutine. */
13162 for (func_die = die->parent;
13163 func_die && func_die->tag != DW_TAG_subprogram
13164 && func_die->tag != DW_TAG_subroutine_type;
13165 func_die = func_die->parent);
13166
13167 /* DW_AT_call_all_calls is a superset
13168 of DW_AT_call_all_tail_calls. */
13169 if (func_die
13170 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13171 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13172 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13173 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13174 {
13175 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13176 not complete. But keep CALL_SITE for look ups via call_site_htab,
13177 both the initial caller containing the real return address PC and
13178 the final callee containing the current PC of a chain of tail
13179 calls do not need to have the tail call list complete. But any
13180 function candidate for a virtual tail call frame searched via
13181 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13182 determined unambiguously. */
13183 }
13184 else
13185 {
13186 struct type *func_type = NULL;
13187
13188 if (func_die)
13189 func_type = get_die_type (func_die, cu);
13190 if (func_type != NULL)
13191 {
13192 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13193
13194 /* Enlist this call site to the function. */
13195 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13196 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13197 }
13198 else
13199 complaint (_("Cannot find function owning DW_TAG_call_site "
13200 "DIE %s [in module %s]"),
13201 sect_offset_str (die->sect_off), objfile_name (objfile));
13202 }
13203 }
13204
13205 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13206 if (attr == NULL)
13207 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13208 if (attr == NULL)
13209 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13210 if (attr == NULL)
13211 {
13212 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13213 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13214 }
13215 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13216 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13217 /* Keep NULL DWARF_BLOCK. */;
13218 else if (attr->form_is_block ())
13219 {
13220 struct dwarf2_locexpr_baton *dlbaton;
13221
13222 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13223 dlbaton->data = DW_BLOCK (attr)->data;
13224 dlbaton->size = DW_BLOCK (attr)->size;
13225 dlbaton->per_cu = cu->per_cu;
13226
13227 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13228 }
13229 else if (attr->form_is_ref ())
13230 {
13231 struct dwarf2_cu *target_cu = cu;
13232 struct die_info *target_die;
13233
13234 target_die = follow_die_ref (die, attr, &target_cu);
13235 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13236 if (die_is_declaration (target_die, target_cu))
13237 {
13238 const char *target_physname;
13239
13240 /* Prefer the mangled name; otherwise compute the demangled one. */
13241 target_physname = dw2_linkage_name (target_die, target_cu);
13242 if (target_physname == NULL)
13243 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13244 if (target_physname == NULL)
13245 complaint (_("DW_AT_call_target target DIE has invalid "
13246 "physname, for referencing DIE %s [in module %s]"),
13247 sect_offset_str (die->sect_off), objfile_name (objfile));
13248 else
13249 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13250 }
13251 else
13252 {
13253 CORE_ADDR lowpc;
13254
13255 /* DW_AT_entry_pc should be preferred. */
13256 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13257 <= PC_BOUNDS_INVALID)
13258 complaint (_("DW_AT_call_target target DIE has invalid "
13259 "low pc, for referencing DIE %s [in module %s]"),
13260 sect_offset_str (die->sect_off), objfile_name (objfile));
13261 else
13262 {
13263 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13264 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13265 }
13266 }
13267 }
13268 else
13269 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13270 "block nor reference, for DIE %s [in module %s]"),
13271 sect_offset_str (die->sect_off), objfile_name (objfile));
13272
13273 call_site->per_cu = cu->per_cu;
13274
13275 for (child_die = die->child;
13276 child_die && child_die->tag;
13277 child_die = sibling_die (child_die))
13278 {
13279 struct call_site_parameter *parameter;
13280 struct attribute *loc, *origin;
13281
13282 if (child_die->tag != DW_TAG_call_site_parameter
13283 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13284 {
13285 /* Already printed the complaint above. */
13286 continue;
13287 }
13288
13289 gdb_assert (call_site->parameter_count < nparams);
13290 parameter = &call_site->parameter[call_site->parameter_count];
13291
13292 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13293 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13294 register is contained in DW_AT_call_value. */
13295
13296 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13297 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13298 if (origin == NULL)
13299 {
13300 /* This was a pre-DWARF-5 GNU extension alias
13301 for DW_AT_call_parameter. */
13302 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13303 }
13304 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13305 {
13306 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13307
13308 sect_offset sect_off
13309 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13310 if (!cu->header.offset_in_cu_p (sect_off))
13311 {
13312 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13313 binding can be done only inside one CU. Such referenced DIE
13314 therefore cannot be even moved to DW_TAG_partial_unit. */
13315 complaint (_("DW_AT_call_parameter offset is not in CU for "
13316 "DW_TAG_call_site child DIE %s [in module %s]"),
13317 sect_offset_str (child_die->sect_off),
13318 objfile_name (objfile));
13319 continue;
13320 }
13321 parameter->u.param_cu_off
13322 = (cu_offset) (sect_off - cu->header.sect_off);
13323 }
13324 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13325 {
13326 complaint (_("No DW_FORM_block* DW_AT_location for "
13327 "DW_TAG_call_site child DIE %s [in module %s]"),
13328 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13329 continue;
13330 }
13331 else
13332 {
13333 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13334 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13335 if (parameter->u.dwarf_reg != -1)
13336 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13337 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13338 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13339 &parameter->u.fb_offset))
13340 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13341 else
13342 {
13343 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13344 "for DW_FORM_block* DW_AT_location is supported for "
13345 "DW_TAG_call_site child DIE %s "
13346 "[in module %s]"),
13347 sect_offset_str (child_die->sect_off),
13348 objfile_name (objfile));
13349 continue;
13350 }
13351 }
13352
13353 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13354 if (attr == NULL)
13355 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13356 if (attr == NULL || !attr->form_is_block ())
13357 {
13358 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13359 "DW_TAG_call_site child DIE %s [in module %s]"),
13360 sect_offset_str (child_die->sect_off),
13361 objfile_name (objfile));
13362 continue;
13363 }
13364 parameter->value = DW_BLOCK (attr)->data;
13365 parameter->value_size = DW_BLOCK (attr)->size;
13366
13367 /* Parameters are not pre-cleared by memset above. */
13368 parameter->data_value = NULL;
13369 parameter->data_value_size = 0;
13370 call_site->parameter_count++;
13371
13372 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13373 if (attr == NULL)
13374 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13375 if (attr != nullptr)
13376 {
13377 if (!attr->form_is_block ())
13378 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13379 "DW_TAG_call_site child DIE %s [in module %s]"),
13380 sect_offset_str (child_die->sect_off),
13381 objfile_name (objfile));
13382 else
13383 {
13384 parameter->data_value = DW_BLOCK (attr)->data;
13385 parameter->data_value_size = DW_BLOCK (attr)->size;
13386 }
13387 }
13388 }
13389 }
13390
13391 /* Helper function for read_variable. If DIE represents a virtual
13392 table, then return the type of the concrete object that is
13393 associated with the virtual table. Otherwise, return NULL. */
13394
13395 static struct type *
13396 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13397 {
13398 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13399 if (attr == NULL)
13400 return NULL;
13401
13402 /* Find the type DIE. */
13403 struct die_info *type_die = NULL;
13404 struct dwarf2_cu *type_cu = cu;
13405
13406 if (attr->form_is_ref ())
13407 type_die = follow_die_ref (die, attr, &type_cu);
13408 if (type_die == NULL)
13409 return NULL;
13410
13411 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13412 return NULL;
13413 return die_containing_type (type_die, type_cu);
13414 }
13415
13416 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13417
13418 static void
13419 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13420 {
13421 struct rust_vtable_symbol *storage = NULL;
13422
13423 if (cu->language == language_rust)
13424 {
13425 struct type *containing_type = rust_containing_type (die, cu);
13426
13427 if (containing_type != NULL)
13428 {
13429 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13430
13431 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13432 initialize_objfile_symbol (storage);
13433 storage->concrete_type = containing_type;
13434 storage->subclass = SYMBOL_RUST_VTABLE;
13435 }
13436 }
13437
13438 struct symbol *res = new_symbol (die, NULL, cu, storage);
13439 struct attribute *abstract_origin
13440 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13441 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13442 if (res == NULL && loc && abstract_origin)
13443 {
13444 /* We have a variable without a name, but with a location and an abstract
13445 origin. This may be a concrete instance of an abstract variable
13446 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13447 later. */
13448 struct dwarf2_cu *origin_cu = cu;
13449 struct die_info *origin_die
13450 = follow_die_ref (die, abstract_origin, &origin_cu);
13451 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13452 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13453 }
13454 }
13455
13456 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13457 reading .debug_rnglists.
13458 Callback's type should be:
13459 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13460 Return true if the attributes are present and valid, otherwise,
13461 return false. */
13462
13463 template <typename Callback>
13464 static bool
13465 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13466 Callback &&callback)
13467 {
13468 struct dwarf2_per_objfile *dwarf2_per_objfile
13469 = cu->per_cu->dwarf2_per_objfile;
13470 struct objfile *objfile = dwarf2_per_objfile->objfile;
13471 bfd *obfd = objfile->obfd;
13472 /* Base address selection entry. */
13473 CORE_ADDR base;
13474 int found_base;
13475 const gdb_byte *buffer;
13476 CORE_ADDR baseaddr;
13477 bool overflow = false;
13478
13479 found_base = cu->base_known;
13480 base = cu->base_address;
13481
13482 dwarf2_per_objfile->rnglists.read (objfile);
13483 if (offset >= dwarf2_per_objfile->rnglists.size)
13484 {
13485 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13486 offset);
13487 return false;
13488 }
13489 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13490
13491 baseaddr = objfile->text_section_offset ();
13492
13493 while (1)
13494 {
13495 /* Initialize it due to a false compiler warning. */
13496 CORE_ADDR range_beginning = 0, range_end = 0;
13497 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13498 + dwarf2_per_objfile->rnglists.size);
13499 unsigned int bytes_read;
13500
13501 if (buffer == buf_end)
13502 {
13503 overflow = true;
13504 break;
13505 }
13506 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13507 switch (rlet)
13508 {
13509 case DW_RLE_end_of_list:
13510 break;
13511 case DW_RLE_base_address:
13512 if (buffer + cu->header.addr_size > buf_end)
13513 {
13514 overflow = true;
13515 break;
13516 }
13517 base = read_address (obfd, buffer, cu, &bytes_read);
13518 found_base = 1;
13519 buffer += bytes_read;
13520 break;
13521 case DW_RLE_start_length:
13522 if (buffer + cu->header.addr_size > buf_end)
13523 {
13524 overflow = true;
13525 break;
13526 }
13527 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13528 buffer += bytes_read;
13529 range_end = (range_beginning
13530 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13531 buffer += bytes_read;
13532 if (buffer > buf_end)
13533 {
13534 overflow = true;
13535 break;
13536 }
13537 break;
13538 case DW_RLE_offset_pair:
13539 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13540 buffer += bytes_read;
13541 if (buffer > buf_end)
13542 {
13543 overflow = true;
13544 break;
13545 }
13546 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13547 buffer += bytes_read;
13548 if (buffer > buf_end)
13549 {
13550 overflow = true;
13551 break;
13552 }
13553 break;
13554 case DW_RLE_start_end:
13555 if (buffer + 2 * cu->header.addr_size > buf_end)
13556 {
13557 overflow = true;
13558 break;
13559 }
13560 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13561 buffer += bytes_read;
13562 range_end = read_address (obfd, buffer, cu, &bytes_read);
13563 buffer += bytes_read;
13564 break;
13565 default:
13566 complaint (_("Invalid .debug_rnglists data (no base address)"));
13567 return false;
13568 }
13569 if (rlet == DW_RLE_end_of_list || overflow)
13570 break;
13571 if (rlet == DW_RLE_base_address)
13572 continue;
13573
13574 if (!found_base)
13575 {
13576 /* We have no valid base address for the ranges
13577 data. */
13578 complaint (_("Invalid .debug_rnglists data (no base address)"));
13579 return false;
13580 }
13581
13582 if (range_beginning > range_end)
13583 {
13584 /* Inverted range entries are invalid. */
13585 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13586 return false;
13587 }
13588
13589 /* Empty range entries have no effect. */
13590 if (range_beginning == range_end)
13591 continue;
13592
13593 range_beginning += base;
13594 range_end += base;
13595
13596 /* A not-uncommon case of bad debug info.
13597 Don't pollute the addrmap with bad data. */
13598 if (range_beginning + baseaddr == 0
13599 && !dwarf2_per_objfile->has_section_at_zero)
13600 {
13601 complaint (_(".debug_rnglists entry has start address of zero"
13602 " [in module %s]"), objfile_name (objfile));
13603 continue;
13604 }
13605
13606 callback (range_beginning, range_end);
13607 }
13608
13609 if (overflow)
13610 {
13611 complaint (_("Offset %d is not terminated "
13612 "for DW_AT_ranges attribute"),
13613 offset);
13614 return false;
13615 }
13616
13617 return true;
13618 }
13619
13620 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13621 Callback's type should be:
13622 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13623 Return 1 if the attributes are present and valid, otherwise, return 0. */
13624
13625 template <typename Callback>
13626 static int
13627 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13628 Callback &&callback)
13629 {
13630 struct dwarf2_per_objfile *dwarf2_per_objfile
13631 = cu->per_cu->dwarf2_per_objfile;
13632 struct objfile *objfile = dwarf2_per_objfile->objfile;
13633 struct comp_unit_head *cu_header = &cu->header;
13634 bfd *obfd = objfile->obfd;
13635 unsigned int addr_size = cu_header->addr_size;
13636 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13637 /* Base address selection entry. */
13638 CORE_ADDR base;
13639 int found_base;
13640 unsigned int dummy;
13641 const gdb_byte *buffer;
13642 CORE_ADDR baseaddr;
13643
13644 if (cu_header->version >= 5)
13645 return dwarf2_rnglists_process (offset, cu, callback);
13646
13647 found_base = cu->base_known;
13648 base = cu->base_address;
13649
13650 dwarf2_per_objfile->ranges.read (objfile);
13651 if (offset >= dwarf2_per_objfile->ranges.size)
13652 {
13653 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13654 offset);
13655 return 0;
13656 }
13657 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13658
13659 baseaddr = objfile->text_section_offset ();
13660
13661 while (1)
13662 {
13663 CORE_ADDR range_beginning, range_end;
13664
13665 range_beginning = read_address (obfd, buffer, cu, &dummy);
13666 buffer += addr_size;
13667 range_end = read_address (obfd, buffer, cu, &dummy);
13668 buffer += addr_size;
13669 offset += 2 * addr_size;
13670
13671 /* An end of list marker is a pair of zero addresses. */
13672 if (range_beginning == 0 && range_end == 0)
13673 /* Found the end of list entry. */
13674 break;
13675
13676 /* Each base address selection entry is a pair of 2 values.
13677 The first is the largest possible address, the second is
13678 the base address. Check for a base address here. */
13679 if ((range_beginning & mask) == mask)
13680 {
13681 /* If we found the largest possible address, then we already
13682 have the base address in range_end. */
13683 base = range_end;
13684 found_base = 1;
13685 continue;
13686 }
13687
13688 if (!found_base)
13689 {
13690 /* We have no valid base address for the ranges
13691 data. */
13692 complaint (_("Invalid .debug_ranges data (no base address)"));
13693 return 0;
13694 }
13695
13696 if (range_beginning > range_end)
13697 {
13698 /* Inverted range entries are invalid. */
13699 complaint (_("Invalid .debug_ranges data (inverted range)"));
13700 return 0;
13701 }
13702
13703 /* Empty range entries have no effect. */
13704 if (range_beginning == range_end)
13705 continue;
13706
13707 range_beginning += base;
13708 range_end += base;
13709
13710 /* A not-uncommon case of bad debug info.
13711 Don't pollute the addrmap with bad data. */
13712 if (range_beginning + baseaddr == 0
13713 && !dwarf2_per_objfile->has_section_at_zero)
13714 {
13715 complaint (_(".debug_ranges entry has start address of zero"
13716 " [in module %s]"), objfile_name (objfile));
13717 continue;
13718 }
13719
13720 callback (range_beginning, range_end);
13721 }
13722
13723 return 1;
13724 }
13725
13726 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13727 Return 1 if the attributes are present and valid, otherwise, return 0.
13728 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13729
13730 static int
13731 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13732 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13733 dwarf2_psymtab *ranges_pst)
13734 {
13735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13736 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13737 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13738 int low_set = 0;
13739 CORE_ADDR low = 0;
13740 CORE_ADDR high = 0;
13741 int retval;
13742
13743 retval = dwarf2_ranges_process (offset, cu,
13744 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13745 {
13746 if (ranges_pst != NULL)
13747 {
13748 CORE_ADDR lowpc;
13749 CORE_ADDR highpc;
13750
13751 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13752 range_beginning + baseaddr)
13753 - baseaddr);
13754 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13755 range_end + baseaddr)
13756 - baseaddr);
13757 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13758 lowpc, highpc - 1, ranges_pst);
13759 }
13760
13761 /* FIXME: This is recording everything as a low-high
13762 segment of consecutive addresses. We should have a
13763 data structure for discontiguous block ranges
13764 instead. */
13765 if (! low_set)
13766 {
13767 low = range_beginning;
13768 high = range_end;
13769 low_set = 1;
13770 }
13771 else
13772 {
13773 if (range_beginning < low)
13774 low = range_beginning;
13775 if (range_end > high)
13776 high = range_end;
13777 }
13778 });
13779 if (!retval)
13780 return 0;
13781
13782 if (! low_set)
13783 /* If the first entry is an end-of-list marker, the range
13784 describes an empty scope, i.e. no instructions. */
13785 return 0;
13786
13787 if (low_return)
13788 *low_return = low;
13789 if (high_return)
13790 *high_return = high;
13791 return 1;
13792 }
13793
13794 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13795 definition for the return value. *LOWPC and *HIGHPC are set iff
13796 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13797
13798 static enum pc_bounds_kind
13799 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13800 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13801 dwarf2_psymtab *pst)
13802 {
13803 struct dwarf2_per_objfile *dwarf2_per_objfile
13804 = cu->per_cu->dwarf2_per_objfile;
13805 struct attribute *attr;
13806 struct attribute *attr_high;
13807 CORE_ADDR low = 0;
13808 CORE_ADDR high = 0;
13809 enum pc_bounds_kind ret;
13810
13811 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13812 if (attr_high)
13813 {
13814 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13815 if (attr != nullptr)
13816 {
13817 low = attr->value_as_address ();
13818 high = attr_high->value_as_address ();
13819 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13820 high += low;
13821 }
13822 else
13823 /* Found high w/o low attribute. */
13824 return PC_BOUNDS_INVALID;
13825
13826 /* Found consecutive range of addresses. */
13827 ret = PC_BOUNDS_HIGH_LOW;
13828 }
13829 else
13830 {
13831 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13832 if (attr != NULL)
13833 {
13834 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13835 We take advantage of the fact that DW_AT_ranges does not appear
13836 in DW_TAG_compile_unit of DWO files. */
13837 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13838 unsigned int ranges_offset = (DW_UNSND (attr)
13839 + (need_ranges_base
13840 ? cu->ranges_base
13841 : 0));
13842
13843 /* Value of the DW_AT_ranges attribute is the offset in the
13844 .debug_ranges section. */
13845 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13846 return PC_BOUNDS_INVALID;
13847 /* Found discontinuous range of addresses. */
13848 ret = PC_BOUNDS_RANGES;
13849 }
13850 else
13851 return PC_BOUNDS_NOT_PRESENT;
13852 }
13853
13854 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13855 if (high <= low)
13856 return PC_BOUNDS_INVALID;
13857
13858 /* When using the GNU linker, .gnu.linkonce. sections are used to
13859 eliminate duplicate copies of functions and vtables and such.
13860 The linker will arbitrarily choose one and discard the others.
13861 The AT_*_pc values for such functions refer to local labels in
13862 these sections. If the section from that file was discarded, the
13863 labels are not in the output, so the relocs get a value of 0.
13864 If this is a discarded function, mark the pc bounds as invalid,
13865 so that GDB will ignore it. */
13866 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13867 return PC_BOUNDS_INVALID;
13868
13869 *lowpc = low;
13870 if (highpc)
13871 *highpc = high;
13872 return ret;
13873 }
13874
13875 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13876 its low and high PC addresses. Do nothing if these addresses could not
13877 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13878 and HIGHPC to the high address if greater than HIGHPC. */
13879
13880 static void
13881 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13882 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13883 struct dwarf2_cu *cu)
13884 {
13885 CORE_ADDR low, high;
13886 struct die_info *child = die->child;
13887
13888 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13889 {
13890 *lowpc = std::min (*lowpc, low);
13891 *highpc = std::max (*highpc, high);
13892 }
13893
13894 /* If the language does not allow nested subprograms (either inside
13895 subprograms or lexical blocks), we're done. */
13896 if (cu->language != language_ada)
13897 return;
13898
13899 /* Check all the children of the given DIE. If it contains nested
13900 subprograms, then check their pc bounds. Likewise, we need to
13901 check lexical blocks as well, as they may also contain subprogram
13902 definitions. */
13903 while (child && child->tag)
13904 {
13905 if (child->tag == DW_TAG_subprogram
13906 || child->tag == DW_TAG_lexical_block)
13907 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13908 child = sibling_die (child);
13909 }
13910 }
13911
13912 /* Get the low and high pc's represented by the scope DIE, and store
13913 them in *LOWPC and *HIGHPC. If the correct values can't be
13914 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13915
13916 static void
13917 get_scope_pc_bounds (struct die_info *die,
13918 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13919 struct dwarf2_cu *cu)
13920 {
13921 CORE_ADDR best_low = (CORE_ADDR) -1;
13922 CORE_ADDR best_high = (CORE_ADDR) 0;
13923 CORE_ADDR current_low, current_high;
13924
13925 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13926 >= PC_BOUNDS_RANGES)
13927 {
13928 best_low = current_low;
13929 best_high = current_high;
13930 }
13931 else
13932 {
13933 struct die_info *child = die->child;
13934
13935 while (child && child->tag)
13936 {
13937 switch (child->tag) {
13938 case DW_TAG_subprogram:
13939 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13940 break;
13941 case DW_TAG_namespace:
13942 case DW_TAG_module:
13943 /* FIXME: carlton/2004-01-16: Should we do this for
13944 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13945 that current GCC's always emit the DIEs corresponding
13946 to definitions of methods of classes as children of a
13947 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13948 the DIEs giving the declarations, which could be
13949 anywhere). But I don't see any reason why the
13950 standards says that they have to be there. */
13951 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13952
13953 if (current_low != ((CORE_ADDR) -1))
13954 {
13955 best_low = std::min (best_low, current_low);
13956 best_high = std::max (best_high, current_high);
13957 }
13958 break;
13959 default:
13960 /* Ignore. */
13961 break;
13962 }
13963
13964 child = sibling_die (child);
13965 }
13966 }
13967
13968 *lowpc = best_low;
13969 *highpc = best_high;
13970 }
13971
13972 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13973 in DIE. */
13974
13975 static void
13976 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13977 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13978 {
13979 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13980 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13981 struct attribute *attr;
13982 struct attribute *attr_high;
13983
13984 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13985 if (attr_high)
13986 {
13987 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13988 if (attr != nullptr)
13989 {
13990 CORE_ADDR low = attr->value_as_address ();
13991 CORE_ADDR high = attr_high->value_as_address ();
13992
13993 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13994 high += low;
13995
13996 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13997 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13998 cu->get_builder ()->record_block_range (block, low, high - 1);
13999 }
14000 }
14001
14002 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14003 if (attr != nullptr)
14004 {
14005 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14006 We take advantage of the fact that DW_AT_ranges does not appear
14007 in DW_TAG_compile_unit of DWO files. */
14008 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14009
14010 /* The value of the DW_AT_ranges attribute is the offset of the
14011 address range list in the .debug_ranges section. */
14012 unsigned long offset = (DW_UNSND (attr)
14013 + (need_ranges_base ? cu->ranges_base : 0));
14014
14015 std::vector<blockrange> blockvec;
14016 dwarf2_ranges_process (offset, cu,
14017 [&] (CORE_ADDR start, CORE_ADDR end)
14018 {
14019 start += baseaddr;
14020 end += baseaddr;
14021 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14022 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14023 cu->get_builder ()->record_block_range (block, start, end - 1);
14024 blockvec.emplace_back (start, end);
14025 });
14026
14027 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14028 }
14029 }
14030
14031 /* Check whether the producer field indicates either of GCC < 4.6, or the
14032 Intel C/C++ compiler, and cache the result in CU. */
14033
14034 static void
14035 check_producer (struct dwarf2_cu *cu)
14036 {
14037 int major, minor;
14038
14039 if (cu->producer == NULL)
14040 {
14041 /* For unknown compilers expect their behavior is DWARF version
14042 compliant.
14043
14044 GCC started to support .debug_types sections by -gdwarf-4 since
14045 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14046 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14047 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14048 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14049 }
14050 else if (producer_is_gcc (cu->producer, &major, &minor))
14051 {
14052 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14053 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14054 }
14055 else if (producer_is_icc (cu->producer, &major, &minor))
14056 {
14057 cu->producer_is_icc = true;
14058 cu->producer_is_icc_lt_14 = major < 14;
14059 }
14060 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14061 cu->producer_is_codewarrior = true;
14062 else
14063 {
14064 /* For other non-GCC compilers, expect their behavior is DWARF version
14065 compliant. */
14066 }
14067
14068 cu->checked_producer = true;
14069 }
14070
14071 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14072 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14073 during 4.6.0 experimental. */
14074
14075 static bool
14076 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14077 {
14078 if (!cu->checked_producer)
14079 check_producer (cu);
14080
14081 return cu->producer_is_gxx_lt_4_6;
14082 }
14083
14084
14085 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14086 with incorrect is_stmt attributes. */
14087
14088 static bool
14089 producer_is_codewarrior (struct dwarf2_cu *cu)
14090 {
14091 if (!cu->checked_producer)
14092 check_producer (cu);
14093
14094 return cu->producer_is_codewarrior;
14095 }
14096
14097 /* Return the default accessibility type if it is not overridden by
14098 DW_AT_accessibility. */
14099
14100 static enum dwarf_access_attribute
14101 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14102 {
14103 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14104 {
14105 /* The default DWARF 2 accessibility for members is public, the default
14106 accessibility for inheritance is private. */
14107
14108 if (die->tag != DW_TAG_inheritance)
14109 return DW_ACCESS_public;
14110 else
14111 return DW_ACCESS_private;
14112 }
14113 else
14114 {
14115 /* DWARF 3+ defines the default accessibility a different way. The same
14116 rules apply now for DW_TAG_inheritance as for the members and it only
14117 depends on the container kind. */
14118
14119 if (die->parent->tag == DW_TAG_class_type)
14120 return DW_ACCESS_private;
14121 else
14122 return DW_ACCESS_public;
14123 }
14124 }
14125
14126 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14127 offset. If the attribute was not found return 0, otherwise return
14128 1. If it was found but could not properly be handled, set *OFFSET
14129 to 0. */
14130
14131 static int
14132 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14133 LONGEST *offset)
14134 {
14135 struct attribute *attr;
14136
14137 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14138 if (attr != NULL)
14139 {
14140 *offset = 0;
14141
14142 /* Note that we do not check for a section offset first here.
14143 This is because DW_AT_data_member_location is new in DWARF 4,
14144 so if we see it, we can assume that a constant form is really
14145 a constant and not a section offset. */
14146 if (attr->form_is_constant ())
14147 *offset = dwarf2_get_attr_constant_value (attr, 0);
14148 else if (attr->form_is_section_offset ())
14149 dwarf2_complex_location_expr_complaint ();
14150 else if (attr->form_is_block ())
14151 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14152 else
14153 dwarf2_complex_location_expr_complaint ();
14154
14155 return 1;
14156 }
14157
14158 return 0;
14159 }
14160
14161 /* Add an aggregate field to the field list. */
14162
14163 static void
14164 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14165 struct dwarf2_cu *cu)
14166 {
14167 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14168 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14169 struct nextfield *new_field;
14170 struct attribute *attr;
14171 struct field *fp;
14172 const char *fieldname = "";
14173
14174 if (die->tag == DW_TAG_inheritance)
14175 {
14176 fip->baseclasses.emplace_back ();
14177 new_field = &fip->baseclasses.back ();
14178 }
14179 else
14180 {
14181 fip->fields.emplace_back ();
14182 new_field = &fip->fields.back ();
14183 }
14184
14185 fip->nfields++;
14186
14187 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14188 if (attr != nullptr)
14189 new_field->accessibility = DW_UNSND (attr);
14190 else
14191 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14192 if (new_field->accessibility != DW_ACCESS_public)
14193 fip->non_public_fields = 1;
14194
14195 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14196 if (attr != nullptr)
14197 new_field->virtuality = DW_UNSND (attr);
14198 else
14199 new_field->virtuality = DW_VIRTUALITY_none;
14200
14201 fp = &new_field->field;
14202
14203 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14204 {
14205 LONGEST offset;
14206
14207 /* Data member other than a C++ static data member. */
14208
14209 /* Get type of field. */
14210 fp->type = die_type (die, cu);
14211
14212 SET_FIELD_BITPOS (*fp, 0);
14213
14214 /* Get bit size of field (zero if none). */
14215 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14216 if (attr != nullptr)
14217 {
14218 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14219 }
14220 else
14221 {
14222 FIELD_BITSIZE (*fp) = 0;
14223 }
14224
14225 /* Get bit offset of field. */
14226 if (handle_data_member_location (die, cu, &offset))
14227 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14228 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14229 if (attr != nullptr)
14230 {
14231 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14232 {
14233 /* For big endian bits, the DW_AT_bit_offset gives the
14234 additional bit offset from the MSB of the containing
14235 anonymous object to the MSB of the field. We don't
14236 have to do anything special since we don't need to
14237 know the size of the anonymous object. */
14238 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14239 }
14240 else
14241 {
14242 /* For little endian bits, compute the bit offset to the
14243 MSB of the anonymous object, subtract off the number of
14244 bits from the MSB of the field to the MSB of the
14245 object, and then subtract off the number of bits of
14246 the field itself. The result is the bit offset of
14247 the LSB of the field. */
14248 int anonymous_size;
14249 int bit_offset = DW_UNSND (attr);
14250
14251 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14252 if (attr != nullptr)
14253 {
14254 /* The size of the anonymous object containing
14255 the bit field is explicit, so use the
14256 indicated size (in bytes). */
14257 anonymous_size = DW_UNSND (attr);
14258 }
14259 else
14260 {
14261 /* The size of the anonymous object containing
14262 the bit field must be inferred from the type
14263 attribute of the data member containing the
14264 bit field. */
14265 anonymous_size = TYPE_LENGTH (fp->type);
14266 }
14267 SET_FIELD_BITPOS (*fp,
14268 (FIELD_BITPOS (*fp)
14269 + anonymous_size * bits_per_byte
14270 - bit_offset - FIELD_BITSIZE (*fp)));
14271 }
14272 }
14273 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14274 if (attr != NULL)
14275 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14276 + dwarf2_get_attr_constant_value (attr, 0)));
14277
14278 /* Get name of field. */
14279 fieldname = dwarf2_name (die, cu);
14280 if (fieldname == NULL)
14281 fieldname = "";
14282
14283 /* The name is already allocated along with this objfile, so we don't
14284 need to duplicate it for the type. */
14285 fp->name = fieldname;
14286
14287 /* Change accessibility for artificial fields (e.g. virtual table
14288 pointer or virtual base class pointer) to private. */
14289 if (dwarf2_attr (die, DW_AT_artificial, cu))
14290 {
14291 FIELD_ARTIFICIAL (*fp) = 1;
14292 new_field->accessibility = DW_ACCESS_private;
14293 fip->non_public_fields = 1;
14294 }
14295 }
14296 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14297 {
14298 /* C++ static member. */
14299
14300 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14301 is a declaration, but all versions of G++ as of this writing
14302 (so through at least 3.2.1) incorrectly generate
14303 DW_TAG_variable tags. */
14304
14305 const char *physname;
14306
14307 /* Get name of field. */
14308 fieldname = dwarf2_name (die, cu);
14309 if (fieldname == NULL)
14310 return;
14311
14312 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14313 if (attr
14314 /* Only create a symbol if this is an external value.
14315 new_symbol checks this and puts the value in the global symbol
14316 table, which we want. If it is not external, new_symbol
14317 will try to put the value in cu->list_in_scope which is wrong. */
14318 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14319 {
14320 /* A static const member, not much different than an enum as far as
14321 we're concerned, except that we can support more types. */
14322 new_symbol (die, NULL, cu);
14323 }
14324
14325 /* Get physical name. */
14326 physname = dwarf2_physname (fieldname, die, cu);
14327
14328 /* The name is already allocated along with this objfile, so we don't
14329 need to duplicate it for the type. */
14330 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14331 FIELD_TYPE (*fp) = die_type (die, cu);
14332 FIELD_NAME (*fp) = fieldname;
14333 }
14334 else if (die->tag == DW_TAG_inheritance)
14335 {
14336 LONGEST offset;
14337
14338 /* C++ base class field. */
14339 if (handle_data_member_location (die, cu, &offset))
14340 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14341 FIELD_BITSIZE (*fp) = 0;
14342 FIELD_TYPE (*fp) = die_type (die, cu);
14343 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14344 }
14345 else if (die->tag == DW_TAG_variant_part)
14346 {
14347 /* process_structure_scope will treat this DIE as a union. */
14348 process_structure_scope (die, cu);
14349
14350 /* The variant part is relative to the start of the enclosing
14351 structure. */
14352 SET_FIELD_BITPOS (*fp, 0);
14353 fp->type = get_die_type (die, cu);
14354 fp->artificial = 1;
14355 fp->name = "<<variant>>";
14356
14357 /* Normally a DW_TAG_variant_part won't have a size, but our
14358 representation requires one, so set it to the maximum of the
14359 child sizes, being sure to account for the offset at which
14360 each child is seen. */
14361 if (TYPE_LENGTH (fp->type) == 0)
14362 {
14363 unsigned max = 0;
14364 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14365 {
14366 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14367 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14368 if (len > max)
14369 max = len;
14370 }
14371 TYPE_LENGTH (fp->type) = max;
14372 }
14373 }
14374 else
14375 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14376 }
14377
14378 /* Can the type given by DIE define another type? */
14379
14380 static bool
14381 type_can_define_types (const struct die_info *die)
14382 {
14383 switch (die->tag)
14384 {
14385 case DW_TAG_typedef:
14386 case DW_TAG_class_type:
14387 case DW_TAG_structure_type:
14388 case DW_TAG_union_type:
14389 case DW_TAG_enumeration_type:
14390 return true;
14391
14392 default:
14393 return false;
14394 }
14395 }
14396
14397 /* Add a type definition defined in the scope of the FIP's class. */
14398
14399 static void
14400 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14401 struct dwarf2_cu *cu)
14402 {
14403 struct decl_field fp;
14404 memset (&fp, 0, sizeof (fp));
14405
14406 gdb_assert (type_can_define_types (die));
14407
14408 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14409 fp.name = dwarf2_name (die, cu);
14410 fp.type = read_type_die (die, cu);
14411
14412 /* Save accessibility. */
14413 enum dwarf_access_attribute accessibility;
14414 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14415 if (attr != NULL)
14416 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14417 else
14418 accessibility = dwarf2_default_access_attribute (die, cu);
14419 switch (accessibility)
14420 {
14421 case DW_ACCESS_public:
14422 /* The assumed value if neither private nor protected. */
14423 break;
14424 case DW_ACCESS_private:
14425 fp.is_private = 1;
14426 break;
14427 case DW_ACCESS_protected:
14428 fp.is_protected = 1;
14429 break;
14430 default:
14431 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14432 }
14433
14434 if (die->tag == DW_TAG_typedef)
14435 fip->typedef_field_list.push_back (fp);
14436 else
14437 fip->nested_types_list.push_back (fp);
14438 }
14439
14440 /* Create the vector of fields, and attach it to the type. */
14441
14442 static void
14443 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14444 struct dwarf2_cu *cu)
14445 {
14446 int nfields = fip->nfields;
14447
14448 /* Record the field count, allocate space for the array of fields,
14449 and create blank accessibility bitfields if necessary. */
14450 TYPE_NFIELDS (type) = nfields;
14451 TYPE_FIELDS (type) = (struct field *)
14452 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14453
14454 if (fip->non_public_fields && cu->language != language_ada)
14455 {
14456 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14457
14458 TYPE_FIELD_PRIVATE_BITS (type) =
14459 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14460 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14461
14462 TYPE_FIELD_PROTECTED_BITS (type) =
14463 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14464 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14465
14466 TYPE_FIELD_IGNORE_BITS (type) =
14467 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14468 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14469 }
14470
14471 /* If the type has baseclasses, allocate and clear a bit vector for
14472 TYPE_FIELD_VIRTUAL_BITS. */
14473 if (!fip->baseclasses.empty () && cu->language != language_ada)
14474 {
14475 int num_bytes = B_BYTES (fip->baseclasses.size ());
14476 unsigned char *pointer;
14477
14478 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14479 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14480 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14481 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14482 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14483 }
14484
14485 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14486 {
14487 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14488
14489 for (int index = 0; index < nfields; ++index)
14490 {
14491 struct nextfield &field = fip->fields[index];
14492
14493 if (field.variant.is_discriminant)
14494 di->discriminant_index = index;
14495 else if (field.variant.default_branch)
14496 di->default_index = index;
14497 else
14498 di->discriminants[index] = field.variant.discriminant_value;
14499 }
14500 }
14501
14502 /* Copy the saved-up fields into the field vector. */
14503 for (int i = 0; i < nfields; ++i)
14504 {
14505 struct nextfield &field
14506 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14507 : fip->fields[i - fip->baseclasses.size ()]);
14508
14509 TYPE_FIELD (type, i) = field.field;
14510 switch (field.accessibility)
14511 {
14512 case DW_ACCESS_private:
14513 if (cu->language != language_ada)
14514 SET_TYPE_FIELD_PRIVATE (type, i);
14515 break;
14516
14517 case DW_ACCESS_protected:
14518 if (cu->language != language_ada)
14519 SET_TYPE_FIELD_PROTECTED (type, i);
14520 break;
14521
14522 case DW_ACCESS_public:
14523 break;
14524
14525 default:
14526 /* Unknown accessibility. Complain and treat it as public. */
14527 {
14528 complaint (_("unsupported accessibility %d"),
14529 field.accessibility);
14530 }
14531 break;
14532 }
14533 if (i < fip->baseclasses.size ())
14534 {
14535 switch (field.virtuality)
14536 {
14537 case DW_VIRTUALITY_virtual:
14538 case DW_VIRTUALITY_pure_virtual:
14539 if (cu->language == language_ada)
14540 error (_("unexpected virtuality in component of Ada type"));
14541 SET_TYPE_FIELD_VIRTUAL (type, i);
14542 break;
14543 }
14544 }
14545 }
14546 }
14547
14548 /* Return true if this member function is a constructor, false
14549 otherwise. */
14550
14551 static int
14552 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14553 {
14554 const char *fieldname;
14555 const char *type_name;
14556 int len;
14557
14558 if (die->parent == NULL)
14559 return 0;
14560
14561 if (die->parent->tag != DW_TAG_structure_type
14562 && die->parent->tag != DW_TAG_union_type
14563 && die->parent->tag != DW_TAG_class_type)
14564 return 0;
14565
14566 fieldname = dwarf2_name (die, cu);
14567 type_name = dwarf2_name (die->parent, cu);
14568 if (fieldname == NULL || type_name == NULL)
14569 return 0;
14570
14571 len = strlen (fieldname);
14572 return (strncmp (fieldname, type_name, len) == 0
14573 && (type_name[len] == '\0' || type_name[len] == '<'));
14574 }
14575
14576 /* Check if the given VALUE is a recognized enum
14577 dwarf_defaulted_attribute constant according to DWARF5 spec,
14578 Table 7.24. */
14579
14580 static bool
14581 is_valid_DW_AT_defaulted (ULONGEST value)
14582 {
14583 switch (value)
14584 {
14585 case DW_DEFAULTED_no:
14586 case DW_DEFAULTED_in_class:
14587 case DW_DEFAULTED_out_of_class:
14588 return true;
14589 }
14590
14591 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14592 return false;
14593 }
14594
14595 /* Add a member function to the proper fieldlist. */
14596
14597 static void
14598 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14599 struct type *type, struct dwarf2_cu *cu)
14600 {
14601 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14602 struct attribute *attr;
14603 int i;
14604 struct fnfieldlist *flp = nullptr;
14605 struct fn_field *fnp;
14606 const char *fieldname;
14607 struct type *this_type;
14608 enum dwarf_access_attribute accessibility;
14609
14610 if (cu->language == language_ada)
14611 error (_("unexpected member function in Ada type"));
14612
14613 /* Get name of member function. */
14614 fieldname = dwarf2_name (die, cu);
14615 if (fieldname == NULL)
14616 return;
14617
14618 /* Look up member function name in fieldlist. */
14619 for (i = 0; i < fip->fnfieldlists.size (); i++)
14620 {
14621 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14622 {
14623 flp = &fip->fnfieldlists[i];
14624 break;
14625 }
14626 }
14627
14628 /* Create a new fnfieldlist if necessary. */
14629 if (flp == nullptr)
14630 {
14631 fip->fnfieldlists.emplace_back ();
14632 flp = &fip->fnfieldlists.back ();
14633 flp->name = fieldname;
14634 i = fip->fnfieldlists.size () - 1;
14635 }
14636
14637 /* Create a new member function field and add it to the vector of
14638 fnfieldlists. */
14639 flp->fnfields.emplace_back ();
14640 fnp = &flp->fnfields.back ();
14641
14642 /* Delay processing of the physname until later. */
14643 if (cu->language == language_cplus)
14644 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14645 die, cu);
14646 else
14647 {
14648 const char *physname = dwarf2_physname (fieldname, die, cu);
14649 fnp->physname = physname ? physname : "";
14650 }
14651
14652 fnp->type = alloc_type (objfile);
14653 this_type = read_type_die (die, cu);
14654 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14655 {
14656 int nparams = TYPE_NFIELDS (this_type);
14657
14658 /* TYPE is the domain of this method, and THIS_TYPE is the type
14659 of the method itself (TYPE_CODE_METHOD). */
14660 smash_to_method_type (fnp->type, type,
14661 TYPE_TARGET_TYPE (this_type),
14662 TYPE_FIELDS (this_type),
14663 TYPE_NFIELDS (this_type),
14664 TYPE_VARARGS (this_type));
14665
14666 /* Handle static member functions.
14667 Dwarf2 has no clean way to discern C++ static and non-static
14668 member functions. G++ helps GDB by marking the first
14669 parameter for non-static member functions (which is the this
14670 pointer) as artificial. We obtain this information from
14671 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14672 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14673 fnp->voffset = VOFFSET_STATIC;
14674 }
14675 else
14676 complaint (_("member function type missing for '%s'"),
14677 dwarf2_full_name (fieldname, die, cu));
14678
14679 /* Get fcontext from DW_AT_containing_type if present. */
14680 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14681 fnp->fcontext = die_containing_type (die, cu);
14682
14683 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14684 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14685
14686 /* Get accessibility. */
14687 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14688 if (attr != nullptr)
14689 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14690 else
14691 accessibility = dwarf2_default_access_attribute (die, cu);
14692 switch (accessibility)
14693 {
14694 case DW_ACCESS_private:
14695 fnp->is_private = 1;
14696 break;
14697 case DW_ACCESS_protected:
14698 fnp->is_protected = 1;
14699 break;
14700 }
14701
14702 /* Check for artificial methods. */
14703 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14704 if (attr && DW_UNSND (attr) != 0)
14705 fnp->is_artificial = 1;
14706
14707 /* Check for defaulted methods. */
14708 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14709 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14710 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14711
14712 /* Check for deleted methods. */
14713 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14714 if (attr != nullptr && DW_UNSND (attr) != 0)
14715 fnp->is_deleted = 1;
14716
14717 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14718
14719 /* Get index in virtual function table if it is a virtual member
14720 function. For older versions of GCC, this is an offset in the
14721 appropriate virtual table, as specified by DW_AT_containing_type.
14722 For everyone else, it is an expression to be evaluated relative
14723 to the object address. */
14724
14725 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14726 if (attr != nullptr)
14727 {
14728 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14729 {
14730 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14731 {
14732 /* Old-style GCC. */
14733 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14734 }
14735 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14736 || (DW_BLOCK (attr)->size > 1
14737 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14738 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14739 {
14740 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14741 if ((fnp->voffset % cu->header.addr_size) != 0)
14742 dwarf2_complex_location_expr_complaint ();
14743 else
14744 fnp->voffset /= cu->header.addr_size;
14745 fnp->voffset += 2;
14746 }
14747 else
14748 dwarf2_complex_location_expr_complaint ();
14749
14750 if (!fnp->fcontext)
14751 {
14752 /* If there is no `this' field and no DW_AT_containing_type,
14753 we cannot actually find a base class context for the
14754 vtable! */
14755 if (TYPE_NFIELDS (this_type) == 0
14756 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14757 {
14758 complaint (_("cannot determine context for virtual member "
14759 "function \"%s\" (offset %s)"),
14760 fieldname, sect_offset_str (die->sect_off));
14761 }
14762 else
14763 {
14764 fnp->fcontext
14765 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14766 }
14767 }
14768 }
14769 else if (attr->form_is_section_offset ())
14770 {
14771 dwarf2_complex_location_expr_complaint ();
14772 }
14773 else
14774 {
14775 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14776 fieldname);
14777 }
14778 }
14779 else
14780 {
14781 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14782 if (attr && DW_UNSND (attr))
14783 {
14784 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14785 complaint (_("Member function \"%s\" (offset %s) is virtual "
14786 "but the vtable offset is not specified"),
14787 fieldname, sect_offset_str (die->sect_off));
14788 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14789 TYPE_CPLUS_DYNAMIC (type) = 1;
14790 }
14791 }
14792 }
14793
14794 /* Create the vector of member function fields, and attach it to the type. */
14795
14796 static void
14797 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14798 struct dwarf2_cu *cu)
14799 {
14800 if (cu->language == language_ada)
14801 error (_("unexpected member functions in Ada type"));
14802
14803 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14804 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14805 TYPE_ALLOC (type,
14806 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14807
14808 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14809 {
14810 struct fnfieldlist &nf = fip->fnfieldlists[i];
14811 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14812
14813 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14814 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14815 fn_flp->fn_fields = (struct fn_field *)
14816 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14817
14818 for (int k = 0; k < nf.fnfields.size (); ++k)
14819 fn_flp->fn_fields[k] = nf.fnfields[k];
14820 }
14821
14822 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14823 }
14824
14825 /* Returns non-zero if NAME is the name of a vtable member in CU's
14826 language, zero otherwise. */
14827 static int
14828 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14829 {
14830 static const char vptr[] = "_vptr";
14831
14832 /* Look for the C++ form of the vtable. */
14833 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14834 return 1;
14835
14836 return 0;
14837 }
14838
14839 /* GCC outputs unnamed structures that are really pointers to member
14840 functions, with the ABI-specified layout. If TYPE describes
14841 such a structure, smash it into a member function type.
14842
14843 GCC shouldn't do this; it should just output pointer to member DIEs.
14844 This is GCC PR debug/28767. */
14845
14846 static void
14847 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14848 {
14849 struct type *pfn_type, *self_type, *new_type;
14850
14851 /* Check for a structure with no name and two children. */
14852 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14853 return;
14854
14855 /* Check for __pfn and __delta members. */
14856 if (TYPE_FIELD_NAME (type, 0) == NULL
14857 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14858 || TYPE_FIELD_NAME (type, 1) == NULL
14859 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14860 return;
14861
14862 /* Find the type of the method. */
14863 pfn_type = TYPE_FIELD_TYPE (type, 0);
14864 if (pfn_type == NULL
14865 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14866 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14867 return;
14868
14869 /* Look for the "this" argument. */
14870 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14871 if (TYPE_NFIELDS (pfn_type) == 0
14872 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14873 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14874 return;
14875
14876 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14877 new_type = alloc_type (objfile);
14878 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14879 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14880 TYPE_VARARGS (pfn_type));
14881 smash_to_methodptr_type (type, new_type);
14882 }
14883
14884 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14885 appropriate error checking and issuing complaints if there is a
14886 problem. */
14887
14888 static ULONGEST
14889 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14890 {
14891 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14892
14893 if (attr == nullptr)
14894 return 0;
14895
14896 if (!attr->form_is_constant ())
14897 {
14898 complaint (_("DW_AT_alignment must have constant form"
14899 " - DIE at %s [in module %s]"),
14900 sect_offset_str (die->sect_off),
14901 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14902 return 0;
14903 }
14904
14905 ULONGEST align;
14906 if (attr->form == DW_FORM_sdata)
14907 {
14908 LONGEST val = DW_SND (attr);
14909 if (val < 0)
14910 {
14911 complaint (_("DW_AT_alignment value must not be negative"
14912 " - DIE at %s [in module %s]"),
14913 sect_offset_str (die->sect_off),
14914 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14915 return 0;
14916 }
14917 align = val;
14918 }
14919 else
14920 align = DW_UNSND (attr);
14921
14922 if (align == 0)
14923 {
14924 complaint (_("DW_AT_alignment value must not be zero"
14925 " - DIE at %s [in module %s]"),
14926 sect_offset_str (die->sect_off),
14927 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14928 return 0;
14929 }
14930 if ((align & (align - 1)) != 0)
14931 {
14932 complaint (_("DW_AT_alignment value must be a power of 2"
14933 " - DIE at %s [in module %s]"),
14934 sect_offset_str (die->sect_off),
14935 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14936 return 0;
14937 }
14938
14939 return align;
14940 }
14941
14942 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14943 the alignment for TYPE. */
14944
14945 static void
14946 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14947 struct type *type)
14948 {
14949 if (!set_type_align (type, get_alignment (cu, die)))
14950 complaint (_("DW_AT_alignment value too large"
14951 " - DIE at %s [in module %s]"),
14952 sect_offset_str (die->sect_off),
14953 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14954 }
14955
14956 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14957 constant for a type, according to DWARF5 spec, Table 5.5. */
14958
14959 static bool
14960 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14961 {
14962 switch (value)
14963 {
14964 case DW_CC_normal:
14965 case DW_CC_pass_by_reference:
14966 case DW_CC_pass_by_value:
14967 return true;
14968
14969 default:
14970 complaint (_("unrecognized DW_AT_calling_convention value "
14971 "(%s) for a type"), pulongest (value));
14972 return false;
14973 }
14974 }
14975
14976 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14977 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14978 also according to GNU-specific values (see include/dwarf2.h). */
14979
14980 static bool
14981 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14982 {
14983 switch (value)
14984 {
14985 case DW_CC_normal:
14986 case DW_CC_program:
14987 case DW_CC_nocall:
14988 return true;
14989
14990 case DW_CC_GNU_renesas_sh:
14991 case DW_CC_GNU_borland_fastcall_i386:
14992 case DW_CC_GDB_IBM_OpenCL:
14993 return true;
14994
14995 default:
14996 complaint (_("unrecognized DW_AT_calling_convention value "
14997 "(%s) for a subroutine"), pulongest (value));
14998 return false;
14999 }
15000 }
15001
15002 /* Called when we find the DIE that starts a structure or union scope
15003 (definition) to create a type for the structure or union. Fill in
15004 the type's name and general properties; the members will not be
15005 processed until process_structure_scope. A symbol table entry for
15006 the type will also not be done until process_structure_scope (assuming
15007 the type has a name).
15008
15009 NOTE: we need to call these functions regardless of whether or not the
15010 DIE has a DW_AT_name attribute, since it might be an anonymous
15011 structure or union. This gets the type entered into our set of
15012 user defined types. */
15013
15014 static struct type *
15015 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15016 {
15017 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15018 struct type *type;
15019 struct attribute *attr;
15020 const char *name;
15021
15022 /* If the definition of this type lives in .debug_types, read that type.
15023 Don't follow DW_AT_specification though, that will take us back up
15024 the chain and we want to go down. */
15025 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15026 if (attr != nullptr)
15027 {
15028 type = get_DW_AT_signature_type (die, attr, cu);
15029
15030 /* The type's CU may not be the same as CU.
15031 Ensure TYPE is recorded with CU in die_type_hash. */
15032 return set_die_type (die, type, cu);
15033 }
15034
15035 type = alloc_type (objfile);
15036 INIT_CPLUS_SPECIFIC (type);
15037
15038 name = dwarf2_name (die, cu);
15039 if (name != NULL)
15040 {
15041 if (cu->language == language_cplus
15042 || cu->language == language_d
15043 || cu->language == language_rust)
15044 {
15045 const char *full_name = dwarf2_full_name (name, die, cu);
15046
15047 /* dwarf2_full_name might have already finished building the DIE's
15048 type. If so, there is no need to continue. */
15049 if (get_die_type (die, cu) != NULL)
15050 return get_die_type (die, cu);
15051
15052 TYPE_NAME (type) = full_name;
15053 }
15054 else
15055 {
15056 /* The name is already allocated along with this objfile, so
15057 we don't need to duplicate it for the type. */
15058 TYPE_NAME (type) = name;
15059 }
15060 }
15061
15062 if (die->tag == DW_TAG_structure_type)
15063 {
15064 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15065 }
15066 else if (die->tag == DW_TAG_union_type)
15067 {
15068 TYPE_CODE (type) = TYPE_CODE_UNION;
15069 }
15070 else if (die->tag == DW_TAG_variant_part)
15071 {
15072 TYPE_CODE (type) = TYPE_CODE_UNION;
15073 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15074 }
15075 else
15076 {
15077 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15078 }
15079
15080 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15081 TYPE_DECLARED_CLASS (type) = 1;
15082
15083 /* Store the calling convention in the type if it's available in
15084 the die. Otherwise the calling convention remains set to
15085 the default value DW_CC_normal. */
15086 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15087 if (attr != nullptr
15088 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15089 {
15090 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15091 TYPE_CPLUS_CALLING_CONVENTION (type)
15092 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15093 }
15094
15095 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15096 if (attr != nullptr)
15097 {
15098 if (attr->form_is_constant ())
15099 TYPE_LENGTH (type) = DW_UNSND (attr);
15100 else
15101 {
15102 /* For the moment, dynamic type sizes are not supported
15103 by GDB's struct type. The actual size is determined
15104 on-demand when resolving the type of a given object,
15105 so set the type's length to zero for now. Otherwise,
15106 we record an expression as the length, and that expression
15107 could lead to a very large value, which could eventually
15108 lead to us trying to allocate that much memory when creating
15109 a value of that type. */
15110 TYPE_LENGTH (type) = 0;
15111 }
15112 }
15113 else
15114 {
15115 TYPE_LENGTH (type) = 0;
15116 }
15117
15118 maybe_set_alignment (cu, die, type);
15119
15120 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15121 {
15122 /* ICC<14 does not output the required DW_AT_declaration on
15123 incomplete types, but gives them a size of zero. */
15124 TYPE_STUB (type) = 1;
15125 }
15126 else
15127 TYPE_STUB_SUPPORTED (type) = 1;
15128
15129 if (die_is_declaration (die, cu))
15130 TYPE_STUB (type) = 1;
15131 else if (attr == NULL && die->child == NULL
15132 && producer_is_realview (cu->producer))
15133 /* RealView does not output the required DW_AT_declaration
15134 on incomplete types. */
15135 TYPE_STUB (type) = 1;
15136
15137 /* We need to add the type field to the die immediately so we don't
15138 infinitely recurse when dealing with pointers to the structure
15139 type within the structure itself. */
15140 set_die_type (die, type, cu);
15141
15142 /* set_die_type should be already done. */
15143 set_descriptive_type (type, die, cu);
15144
15145 return type;
15146 }
15147
15148 /* A helper for process_structure_scope that handles a single member
15149 DIE. */
15150
15151 static void
15152 handle_struct_member_die (struct die_info *child_die, struct type *type,
15153 struct field_info *fi,
15154 std::vector<struct symbol *> *template_args,
15155 struct dwarf2_cu *cu)
15156 {
15157 if (child_die->tag == DW_TAG_member
15158 || child_die->tag == DW_TAG_variable
15159 || child_die->tag == DW_TAG_variant_part)
15160 {
15161 /* NOTE: carlton/2002-11-05: A C++ static data member
15162 should be a DW_TAG_member that is a declaration, but
15163 all versions of G++ as of this writing (so through at
15164 least 3.2.1) incorrectly generate DW_TAG_variable
15165 tags for them instead. */
15166 dwarf2_add_field (fi, child_die, cu);
15167 }
15168 else if (child_die->tag == DW_TAG_subprogram)
15169 {
15170 /* Rust doesn't have member functions in the C++ sense.
15171 However, it does emit ordinary functions as children
15172 of a struct DIE. */
15173 if (cu->language == language_rust)
15174 read_func_scope (child_die, cu);
15175 else
15176 {
15177 /* C++ member function. */
15178 dwarf2_add_member_fn (fi, child_die, type, cu);
15179 }
15180 }
15181 else if (child_die->tag == DW_TAG_inheritance)
15182 {
15183 /* C++ base class field. */
15184 dwarf2_add_field (fi, child_die, cu);
15185 }
15186 else if (type_can_define_types (child_die))
15187 dwarf2_add_type_defn (fi, child_die, cu);
15188 else if (child_die->tag == DW_TAG_template_type_param
15189 || child_die->tag == DW_TAG_template_value_param)
15190 {
15191 struct symbol *arg = new_symbol (child_die, NULL, cu);
15192
15193 if (arg != NULL)
15194 template_args->push_back (arg);
15195 }
15196 else if (child_die->tag == DW_TAG_variant)
15197 {
15198 /* In a variant we want to get the discriminant and also add a
15199 field for our sole member child. */
15200 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15201
15202 for (die_info *variant_child = child_die->child;
15203 variant_child != NULL;
15204 variant_child = sibling_die (variant_child))
15205 {
15206 if (variant_child->tag == DW_TAG_member)
15207 {
15208 handle_struct_member_die (variant_child, type, fi,
15209 template_args, cu);
15210 /* Only handle the one. */
15211 break;
15212 }
15213 }
15214
15215 /* We don't handle this but we might as well report it if we see
15216 it. */
15217 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15218 complaint (_("DW_AT_discr_list is not supported yet"
15219 " - DIE at %s [in module %s]"),
15220 sect_offset_str (child_die->sect_off),
15221 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15222
15223 /* The first field was just added, so we can stash the
15224 discriminant there. */
15225 gdb_assert (!fi->fields.empty ());
15226 if (discr == NULL)
15227 fi->fields.back ().variant.default_branch = true;
15228 else
15229 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15230 }
15231 }
15232
15233 /* Finish creating a structure or union type, including filling in
15234 its members and creating a symbol for it. */
15235
15236 static void
15237 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15238 {
15239 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15240 struct die_info *child_die;
15241 struct type *type;
15242
15243 type = get_die_type (die, cu);
15244 if (type == NULL)
15245 type = read_structure_type (die, cu);
15246
15247 /* When reading a DW_TAG_variant_part, we need to notice when we
15248 read the discriminant member, so we can record it later in the
15249 discriminant_info. */
15250 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15251 sect_offset discr_offset {};
15252 bool has_template_parameters = false;
15253
15254 if (is_variant_part)
15255 {
15256 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15257 if (discr == NULL)
15258 {
15259 /* Maybe it's a univariant form, an extension we support.
15260 In this case arrange not to check the offset. */
15261 is_variant_part = false;
15262 }
15263 else if (discr->form_is_ref ())
15264 {
15265 struct dwarf2_cu *target_cu = cu;
15266 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15267
15268 discr_offset = target_die->sect_off;
15269 }
15270 else
15271 {
15272 complaint (_("DW_AT_discr does not have DIE reference form"
15273 " - DIE at %s [in module %s]"),
15274 sect_offset_str (die->sect_off),
15275 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15276 is_variant_part = false;
15277 }
15278 }
15279
15280 if (die->child != NULL && ! die_is_declaration (die, cu))
15281 {
15282 struct field_info fi;
15283 std::vector<struct symbol *> template_args;
15284
15285 child_die = die->child;
15286
15287 while (child_die && child_die->tag)
15288 {
15289 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15290
15291 if (is_variant_part && discr_offset == child_die->sect_off)
15292 fi.fields.back ().variant.is_discriminant = true;
15293
15294 child_die = sibling_die (child_die);
15295 }
15296
15297 /* Attach template arguments to type. */
15298 if (!template_args.empty ())
15299 {
15300 has_template_parameters = true;
15301 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15302 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15303 TYPE_TEMPLATE_ARGUMENTS (type)
15304 = XOBNEWVEC (&objfile->objfile_obstack,
15305 struct symbol *,
15306 TYPE_N_TEMPLATE_ARGUMENTS (type));
15307 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15308 template_args.data (),
15309 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15310 * sizeof (struct symbol *)));
15311 }
15312
15313 /* Attach fields and member functions to the type. */
15314 if (fi.nfields)
15315 dwarf2_attach_fields_to_type (&fi, type, cu);
15316 if (!fi.fnfieldlists.empty ())
15317 {
15318 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15319
15320 /* Get the type which refers to the base class (possibly this
15321 class itself) which contains the vtable pointer for the current
15322 class from the DW_AT_containing_type attribute. This use of
15323 DW_AT_containing_type is a GNU extension. */
15324
15325 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15326 {
15327 struct type *t = die_containing_type (die, cu);
15328
15329 set_type_vptr_basetype (type, t);
15330 if (type == t)
15331 {
15332 int i;
15333
15334 /* Our own class provides vtbl ptr. */
15335 for (i = TYPE_NFIELDS (t) - 1;
15336 i >= TYPE_N_BASECLASSES (t);
15337 --i)
15338 {
15339 const char *fieldname = TYPE_FIELD_NAME (t, i);
15340
15341 if (is_vtable_name (fieldname, cu))
15342 {
15343 set_type_vptr_fieldno (type, i);
15344 break;
15345 }
15346 }
15347
15348 /* Complain if virtual function table field not found. */
15349 if (i < TYPE_N_BASECLASSES (t))
15350 complaint (_("virtual function table pointer "
15351 "not found when defining class '%s'"),
15352 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15353 }
15354 else
15355 {
15356 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15357 }
15358 }
15359 else if (cu->producer
15360 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15361 {
15362 /* The IBM XLC compiler does not provide direct indication
15363 of the containing type, but the vtable pointer is
15364 always named __vfp. */
15365
15366 int i;
15367
15368 for (i = TYPE_NFIELDS (type) - 1;
15369 i >= TYPE_N_BASECLASSES (type);
15370 --i)
15371 {
15372 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15373 {
15374 set_type_vptr_fieldno (type, i);
15375 set_type_vptr_basetype (type, type);
15376 break;
15377 }
15378 }
15379 }
15380 }
15381
15382 /* Copy fi.typedef_field_list linked list elements content into the
15383 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15384 if (!fi.typedef_field_list.empty ())
15385 {
15386 int count = fi.typedef_field_list.size ();
15387
15388 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15389 TYPE_TYPEDEF_FIELD_ARRAY (type)
15390 = ((struct decl_field *)
15391 TYPE_ALLOC (type,
15392 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15393 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15394
15395 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15396 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15397 }
15398
15399 /* Copy fi.nested_types_list linked list elements content into the
15400 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15401 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15402 {
15403 int count = fi.nested_types_list.size ();
15404
15405 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15406 TYPE_NESTED_TYPES_ARRAY (type)
15407 = ((struct decl_field *)
15408 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15409 TYPE_NESTED_TYPES_COUNT (type) = count;
15410
15411 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15412 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15413 }
15414 }
15415
15416 quirk_gcc_member_function_pointer (type, objfile);
15417 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15418 cu->rust_unions.push_back (type);
15419
15420 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15421 snapshots) has been known to create a die giving a declaration
15422 for a class that has, as a child, a die giving a definition for a
15423 nested class. So we have to process our children even if the
15424 current die is a declaration. Normally, of course, a declaration
15425 won't have any children at all. */
15426
15427 child_die = die->child;
15428
15429 while (child_die != NULL && child_die->tag)
15430 {
15431 if (child_die->tag == DW_TAG_member
15432 || child_die->tag == DW_TAG_variable
15433 || child_die->tag == DW_TAG_inheritance
15434 || child_die->tag == DW_TAG_template_value_param
15435 || child_die->tag == DW_TAG_template_type_param)
15436 {
15437 /* Do nothing. */
15438 }
15439 else
15440 process_die (child_die, cu);
15441
15442 child_die = sibling_die (child_die);
15443 }
15444
15445 /* Do not consider external references. According to the DWARF standard,
15446 these DIEs are identified by the fact that they have no byte_size
15447 attribute, and a declaration attribute. */
15448 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15449 || !die_is_declaration (die, cu))
15450 {
15451 struct symbol *sym = new_symbol (die, type, cu);
15452
15453 if (has_template_parameters)
15454 {
15455 struct symtab *symtab;
15456 if (sym != nullptr)
15457 symtab = symbol_symtab (sym);
15458 else if (cu->line_header != nullptr)
15459 {
15460 /* Any related symtab will do. */
15461 symtab
15462 = cu->line_header->file_names ()[0].symtab;
15463 }
15464 else
15465 {
15466 symtab = nullptr;
15467 complaint (_("could not find suitable "
15468 "symtab for template parameter"
15469 " - DIE at %s [in module %s]"),
15470 sect_offset_str (die->sect_off),
15471 objfile_name (objfile));
15472 }
15473
15474 if (symtab != nullptr)
15475 {
15476 /* Make sure that the symtab is set on the new symbols.
15477 Even though they don't appear in this symtab directly,
15478 other parts of gdb assume that symbols do, and this is
15479 reasonably true. */
15480 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15481 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15482 }
15483 }
15484 }
15485 }
15486
15487 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15488 update TYPE using some information only available in DIE's children. */
15489
15490 static void
15491 update_enumeration_type_from_children (struct die_info *die,
15492 struct type *type,
15493 struct dwarf2_cu *cu)
15494 {
15495 struct die_info *child_die;
15496 int unsigned_enum = 1;
15497 int flag_enum = 1;
15498 ULONGEST mask = 0;
15499
15500 auto_obstack obstack;
15501
15502 for (child_die = die->child;
15503 child_die != NULL && child_die->tag;
15504 child_die = sibling_die (child_die))
15505 {
15506 struct attribute *attr;
15507 LONGEST value;
15508 const gdb_byte *bytes;
15509 struct dwarf2_locexpr_baton *baton;
15510 const char *name;
15511
15512 if (child_die->tag != DW_TAG_enumerator)
15513 continue;
15514
15515 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15516 if (attr == NULL)
15517 continue;
15518
15519 name = dwarf2_name (child_die, cu);
15520 if (name == NULL)
15521 name = "<anonymous enumerator>";
15522
15523 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15524 &value, &bytes, &baton);
15525 if (value < 0)
15526 {
15527 unsigned_enum = 0;
15528 flag_enum = 0;
15529 }
15530 else if ((mask & value) != 0)
15531 flag_enum = 0;
15532 else
15533 mask |= value;
15534
15535 /* If we already know that the enum type is neither unsigned, nor
15536 a flag type, no need to look at the rest of the enumerates. */
15537 if (!unsigned_enum && !flag_enum)
15538 break;
15539 }
15540
15541 if (unsigned_enum)
15542 TYPE_UNSIGNED (type) = 1;
15543 if (flag_enum)
15544 TYPE_FLAG_ENUM (type) = 1;
15545 }
15546
15547 /* Given a DW_AT_enumeration_type die, set its type. We do not
15548 complete the type's fields yet, or create any symbols. */
15549
15550 static struct type *
15551 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15552 {
15553 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15554 struct type *type;
15555 struct attribute *attr;
15556 const char *name;
15557
15558 /* If the definition of this type lives in .debug_types, read that type.
15559 Don't follow DW_AT_specification though, that will take us back up
15560 the chain and we want to go down. */
15561 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15562 if (attr != nullptr)
15563 {
15564 type = get_DW_AT_signature_type (die, attr, cu);
15565
15566 /* The type's CU may not be the same as CU.
15567 Ensure TYPE is recorded with CU in die_type_hash. */
15568 return set_die_type (die, type, cu);
15569 }
15570
15571 type = alloc_type (objfile);
15572
15573 TYPE_CODE (type) = TYPE_CODE_ENUM;
15574 name = dwarf2_full_name (NULL, die, cu);
15575 if (name != NULL)
15576 TYPE_NAME (type) = name;
15577
15578 attr = dwarf2_attr (die, DW_AT_type, cu);
15579 if (attr != NULL)
15580 {
15581 struct type *underlying_type = die_type (die, cu);
15582
15583 TYPE_TARGET_TYPE (type) = underlying_type;
15584 }
15585
15586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15587 if (attr != nullptr)
15588 {
15589 TYPE_LENGTH (type) = DW_UNSND (attr);
15590 }
15591 else
15592 {
15593 TYPE_LENGTH (type) = 0;
15594 }
15595
15596 maybe_set_alignment (cu, die, type);
15597
15598 /* The enumeration DIE can be incomplete. In Ada, any type can be
15599 declared as private in the package spec, and then defined only
15600 inside the package body. Such types are known as Taft Amendment
15601 Types. When another package uses such a type, an incomplete DIE
15602 may be generated by the compiler. */
15603 if (die_is_declaration (die, cu))
15604 TYPE_STUB (type) = 1;
15605
15606 /* Finish the creation of this type by using the enum's children.
15607 We must call this even when the underlying type has been provided
15608 so that we can determine if we're looking at a "flag" enum. */
15609 update_enumeration_type_from_children (die, type, cu);
15610
15611 /* If this type has an underlying type that is not a stub, then we
15612 may use its attributes. We always use the "unsigned" attribute
15613 in this situation, because ordinarily we guess whether the type
15614 is unsigned -- but the guess can be wrong and the underlying type
15615 can tell us the reality. However, we defer to a local size
15616 attribute if one exists, because this lets the compiler override
15617 the underlying type if needed. */
15618 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15619 {
15620 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15621 if (TYPE_LENGTH (type) == 0)
15622 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15623 if (TYPE_RAW_ALIGN (type) == 0
15624 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15625 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15626 }
15627
15628 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15629
15630 return set_die_type (die, type, cu);
15631 }
15632
15633 /* Given a pointer to a die which begins an enumeration, process all
15634 the dies that define the members of the enumeration, and create the
15635 symbol for the enumeration type.
15636
15637 NOTE: We reverse the order of the element list. */
15638
15639 static void
15640 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15641 {
15642 struct type *this_type;
15643
15644 this_type = get_die_type (die, cu);
15645 if (this_type == NULL)
15646 this_type = read_enumeration_type (die, cu);
15647
15648 if (die->child != NULL)
15649 {
15650 struct die_info *child_die;
15651 struct symbol *sym;
15652 std::vector<struct field> fields;
15653 const char *name;
15654
15655 child_die = die->child;
15656 while (child_die && child_die->tag)
15657 {
15658 if (child_die->tag != DW_TAG_enumerator)
15659 {
15660 process_die (child_die, cu);
15661 }
15662 else
15663 {
15664 name = dwarf2_name (child_die, cu);
15665 if (name)
15666 {
15667 sym = new_symbol (child_die, this_type, cu);
15668
15669 fields.emplace_back ();
15670 struct field &field = fields.back ();
15671
15672 FIELD_NAME (field) = sym->linkage_name ();
15673 FIELD_TYPE (field) = NULL;
15674 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15675 FIELD_BITSIZE (field) = 0;
15676 }
15677 }
15678
15679 child_die = sibling_die (child_die);
15680 }
15681
15682 if (!fields.empty ())
15683 {
15684 TYPE_NFIELDS (this_type) = fields.size ();
15685 TYPE_FIELDS (this_type) = (struct field *)
15686 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15687 memcpy (TYPE_FIELDS (this_type), fields.data (),
15688 sizeof (struct field) * fields.size ());
15689 }
15690 }
15691
15692 /* If we are reading an enum from a .debug_types unit, and the enum
15693 is a declaration, and the enum is not the signatured type in the
15694 unit, then we do not want to add a symbol for it. Adding a
15695 symbol would in some cases obscure the true definition of the
15696 enum, giving users an incomplete type when the definition is
15697 actually available. Note that we do not want to do this for all
15698 enums which are just declarations, because C++0x allows forward
15699 enum declarations. */
15700 if (cu->per_cu->is_debug_types
15701 && die_is_declaration (die, cu))
15702 {
15703 struct signatured_type *sig_type;
15704
15705 sig_type = (struct signatured_type *) cu->per_cu;
15706 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15707 if (sig_type->type_offset_in_section != die->sect_off)
15708 return;
15709 }
15710
15711 new_symbol (die, this_type, cu);
15712 }
15713
15714 /* Extract all information from a DW_TAG_array_type DIE and put it in
15715 the DIE's type field. For now, this only handles one dimensional
15716 arrays. */
15717
15718 static struct type *
15719 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15720 {
15721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15722 struct die_info *child_die;
15723 struct type *type;
15724 struct type *element_type, *range_type, *index_type;
15725 struct attribute *attr;
15726 const char *name;
15727 struct dynamic_prop *byte_stride_prop = NULL;
15728 unsigned int bit_stride = 0;
15729
15730 element_type = die_type (die, cu);
15731
15732 /* The die_type call above may have already set the type for this DIE. */
15733 type = get_die_type (die, cu);
15734 if (type)
15735 return type;
15736
15737 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15738 if (attr != NULL)
15739 {
15740 int stride_ok;
15741 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15742
15743 byte_stride_prop
15744 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15745 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15746 prop_type);
15747 if (!stride_ok)
15748 {
15749 complaint (_("unable to read array DW_AT_byte_stride "
15750 " - DIE at %s [in module %s]"),
15751 sect_offset_str (die->sect_off),
15752 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15753 /* Ignore this attribute. We will likely not be able to print
15754 arrays of this type correctly, but there is little we can do
15755 to help if we cannot read the attribute's value. */
15756 byte_stride_prop = NULL;
15757 }
15758 }
15759
15760 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15761 if (attr != NULL)
15762 bit_stride = DW_UNSND (attr);
15763
15764 /* Irix 6.2 native cc creates array types without children for
15765 arrays with unspecified length. */
15766 if (die->child == NULL)
15767 {
15768 index_type = objfile_type (objfile)->builtin_int;
15769 range_type = create_static_range_type (NULL, index_type, 0, -1);
15770 type = create_array_type_with_stride (NULL, element_type, range_type,
15771 byte_stride_prop, bit_stride);
15772 return set_die_type (die, type, cu);
15773 }
15774
15775 std::vector<struct type *> range_types;
15776 child_die = die->child;
15777 while (child_die && child_die->tag)
15778 {
15779 if (child_die->tag == DW_TAG_subrange_type)
15780 {
15781 struct type *child_type = read_type_die (child_die, cu);
15782
15783 if (child_type != NULL)
15784 {
15785 /* The range type was succesfully read. Save it for the
15786 array type creation. */
15787 range_types.push_back (child_type);
15788 }
15789 }
15790 child_die = sibling_die (child_die);
15791 }
15792
15793 /* Dwarf2 dimensions are output from left to right, create the
15794 necessary array types in backwards order. */
15795
15796 type = element_type;
15797
15798 if (read_array_order (die, cu) == DW_ORD_col_major)
15799 {
15800 int i = 0;
15801
15802 while (i < range_types.size ())
15803 type = create_array_type_with_stride (NULL, type, range_types[i++],
15804 byte_stride_prop, bit_stride);
15805 }
15806 else
15807 {
15808 size_t ndim = range_types.size ();
15809 while (ndim-- > 0)
15810 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15811 byte_stride_prop, bit_stride);
15812 }
15813
15814 /* Understand Dwarf2 support for vector types (like they occur on
15815 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15816 array type. This is not part of the Dwarf2/3 standard yet, but a
15817 custom vendor extension. The main difference between a regular
15818 array and the vector variant is that vectors are passed by value
15819 to functions. */
15820 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15821 if (attr != nullptr)
15822 make_vector_type (type);
15823
15824 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15825 implementation may choose to implement triple vectors using this
15826 attribute. */
15827 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15828 if (attr != nullptr)
15829 {
15830 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15831 TYPE_LENGTH (type) = DW_UNSND (attr);
15832 else
15833 complaint (_("DW_AT_byte_size for array type smaller "
15834 "than the total size of elements"));
15835 }
15836
15837 name = dwarf2_name (die, cu);
15838 if (name)
15839 TYPE_NAME (type) = name;
15840
15841 maybe_set_alignment (cu, die, type);
15842
15843 /* Install the type in the die. */
15844 set_die_type (die, type, cu);
15845
15846 /* set_die_type should be already done. */
15847 set_descriptive_type (type, die, cu);
15848
15849 return type;
15850 }
15851
15852 static enum dwarf_array_dim_ordering
15853 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15854 {
15855 struct attribute *attr;
15856
15857 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15858
15859 if (attr != nullptr)
15860 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15861
15862 /* GNU F77 is a special case, as at 08/2004 array type info is the
15863 opposite order to the dwarf2 specification, but data is still
15864 laid out as per normal fortran.
15865
15866 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15867 version checking. */
15868
15869 if (cu->language == language_fortran
15870 && cu->producer && strstr (cu->producer, "GNU F77"))
15871 {
15872 return DW_ORD_row_major;
15873 }
15874
15875 switch (cu->language_defn->la_array_ordering)
15876 {
15877 case array_column_major:
15878 return DW_ORD_col_major;
15879 case array_row_major:
15880 default:
15881 return DW_ORD_row_major;
15882 };
15883 }
15884
15885 /* Extract all information from a DW_TAG_set_type DIE and put it in
15886 the DIE's type field. */
15887
15888 static struct type *
15889 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15890 {
15891 struct type *domain_type, *set_type;
15892 struct attribute *attr;
15893
15894 domain_type = die_type (die, cu);
15895
15896 /* The die_type call above may have already set the type for this DIE. */
15897 set_type = get_die_type (die, cu);
15898 if (set_type)
15899 return set_type;
15900
15901 set_type = create_set_type (NULL, domain_type);
15902
15903 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15904 if (attr != nullptr)
15905 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15906
15907 maybe_set_alignment (cu, die, set_type);
15908
15909 return set_die_type (die, set_type, cu);
15910 }
15911
15912 /* A helper for read_common_block that creates a locexpr baton.
15913 SYM is the symbol which we are marking as computed.
15914 COMMON_DIE is the DIE for the common block.
15915 COMMON_LOC is the location expression attribute for the common
15916 block itself.
15917 MEMBER_LOC is the location expression attribute for the particular
15918 member of the common block that we are processing.
15919 CU is the CU from which the above come. */
15920
15921 static void
15922 mark_common_block_symbol_computed (struct symbol *sym,
15923 struct die_info *common_die,
15924 struct attribute *common_loc,
15925 struct attribute *member_loc,
15926 struct dwarf2_cu *cu)
15927 {
15928 struct dwarf2_per_objfile *dwarf2_per_objfile
15929 = cu->per_cu->dwarf2_per_objfile;
15930 struct objfile *objfile = dwarf2_per_objfile->objfile;
15931 struct dwarf2_locexpr_baton *baton;
15932 gdb_byte *ptr;
15933 unsigned int cu_off;
15934 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15935 LONGEST offset = 0;
15936
15937 gdb_assert (common_loc && member_loc);
15938 gdb_assert (common_loc->form_is_block ());
15939 gdb_assert (member_loc->form_is_block ()
15940 || member_loc->form_is_constant ());
15941
15942 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15943 baton->per_cu = cu->per_cu;
15944 gdb_assert (baton->per_cu);
15945
15946 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15947
15948 if (member_loc->form_is_constant ())
15949 {
15950 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15951 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15952 }
15953 else
15954 baton->size += DW_BLOCK (member_loc)->size;
15955
15956 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15957 baton->data = ptr;
15958
15959 *ptr++ = DW_OP_call4;
15960 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15961 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15962 ptr += 4;
15963
15964 if (member_loc->form_is_constant ())
15965 {
15966 *ptr++ = DW_OP_addr;
15967 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15968 ptr += cu->header.addr_size;
15969 }
15970 else
15971 {
15972 /* We have to copy the data here, because DW_OP_call4 will only
15973 use a DW_AT_location attribute. */
15974 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15975 ptr += DW_BLOCK (member_loc)->size;
15976 }
15977
15978 *ptr++ = DW_OP_plus;
15979 gdb_assert (ptr - baton->data == baton->size);
15980
15981 SYMBOL_LOCATION_BATON (sym) = baton;
15982 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15983 }
15984
15985 /* Create appropriate locally-scoped variables for all the
15986 DW_TAG_common_block entries. Also create a struct common_block
15987 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15988 is used to separate the common blocks name namespace from regular
15989 variable names. */
15990
15991 static void
15992 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15993 {
15994 struct attribute *attr;
15995
15996 attr = dwarf2_attr (die, DW_AT_location, cu);
15997 if (attr != nullptr)
15998 {
15999 /* Support the .debug_loc offsets. */
16000 if (attr->form_is_block ())
16001 {
16002 /* Ok. */
16003 }
16004 else if (attr->form_is_section_offset ())
16005 {
16006 dwarf2_complex_location_expr_complaint ();
16007 attr = NULL;
16008 }
16009 else
16010 {
16011 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16012 "common block member");
16013 attr = NULL;
16014 }
16015 }
16016
16017 if (die->child != NULL)
16018 {
16019 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16020 struct die_info *child_die;
16021 size_t n_entries = 0, size;
16022 struct common_block *common_block;
16023 struct symbol *sym;
16024
16025 for (child_die = die->child;
16026 child_die && child_die->tag;
16027 child_die = sibling_die (child_die))
16028 ++n_entries;
16029
16030 size = (sizeof (struct common_block)
16031 + (n_entries - 1) * sizeof (struct symbol *));
16032 common_block
16033 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16034 size);
16035 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16036 common_block->n_entries = 0;
16037
16038 for (child_die = die->child;
16039 child_die && child_die->tag;
16040 child_die = sibling_die (child_die))
16041 {
16042 /* Create the symbol in the DW_TAG_common_block block in the current
16043 symbol scope. */
16044 sym = new_symbol (child_die, NULL, cu);
16045 if (sym != NULL)
16046 {
16047 struct attribute *member_loc;
16048
16049 common_block->contents[common_block->n_entries++] = sym;
16050
16051 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16052 cu);
16053 if (member_loc)
16054 {
16055 /* GDB has handled this for a long time, but it is
16056 not specified by DWARF. It seems to have been
16057 emitted by gfortran at least as recently as:
16058 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16059 complaint (_("Variable in common block has "
16060 "DW_AT_data_member_location "
16061 "- DIE at %s [in module %s]"),
16062 sect_offset_str (child_die->sect_off),
16063 objfile_name (objfile));
16064
16065 if (member_loc->form_is_section_offset ())
16066 dwarf2_complex_location_expr_complaint ();
16067 else if (member_loc->form_is_constant ()
16068 || member_loc->form_is_block ())
16069 {
16070 if (attr != nullptr)
16071 mark_common_block_symbol_computed (sym, die, attr,
16072 member_loc, cu);
16073 }
16074 else
16075 dwarf2_complex_location_expr_complaint ();
16076 }
16077 }
16078 }
16079
16080 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16081 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16082 }
16083 }
16084
16085 /* Create a type for a C++ namespace. */
16086
16087 static struct type *
16088 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16089 {
16090 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16091 const char *previous_prefix, *name;
16092 int is_anonymous;
16093 struct type *type;
16094
16095 /* For extensions, reuse the type of the original namespace. */
16096 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16097 {
16098 struct die_info *ext_die;
16099 struct dwarf2_cu *ext_cu = cu;
16100
16101 ext_die = dwarf2_extension (die, &ext_cu);
16102 type = read_type_die (ext_die, ext_cu);
16103
16104 /* EXT_CU may not be the same as CU.
16105 Ensure TYPE is recorded with CU in die_type_hash. */
16106 return set_die_type (die, type, cu);
16107 }
16108
16109 name = namespace_name (die, &is_anonymous, cu);
16110
16111 /* Now build the name of the current namespace. */
16112
16113 previous_prefix = determine_prefix (die, cu);
16114 if (previous_prefix[0] != '\0')
16115 name = typename_concat (&objfile->objfile_obstack,
16116 previous_prefix, name, 0, cu);
16117
16118 /* Create the type. */
16119 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16120
16121 return set_die_type (die, type, cu);
16122 }
16123
16124 /* Read a namespace scope. */
16125
16126 static void
16127 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16128 {
16129 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16130 int is_anonymous;
16131
16132 /* Add a symbol associated to this if we haven't seen the namespace
16133 before. Also, add a using directive if it's an anonymous
16134 namespace. */
16135
16136 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16137 {
16138 struct type *type;
16139
16140 type = read_type_die (die, cu);
16141 new_symbol (die, type, cu);
16142
16143 namespace_name (die, &is_anonymous, cu);
16144 if (is_anonymous)
16145 {
16146 const char *previous_prefix = determine_prefix (die, cu);
16147
16148 std::vector<const char *> excludes;
16149 add_using_directive (using_directives (cu),
16150 previous_prefix, TYPE_NAME (type), NULL,
16151 NULL, excludes, 0, &objfile->objfile_obstack);
16152 }
16153 }
16154
16155 if (die->child != NULL)
16156 {
16157 struct die_info *child_die = die->child;
16158
16159 while (child_die && child_die->tag)
16160 {
16161 process_die (child_die, cu);
16162 child_die = sibling_die (child_die);
16163 }
16164 }
16165 }
16166
16167 /* Read a Fortran module as type. This DIE can be only a declaration used for
16168 imported module. Still we need that type as local Fortran "use ... only"
16169 declaration imports depend on the created type in determine_prefix. */
16170
16171 static struct type *
16172 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16173 {
16174 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16175 const char *module_name;
16176 struct type *type;
16177
16178 module_name = dwarf2_name (die, cu);
16179 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16180
16181 return set_die_type (die, type, cu);
16182 }
16183
16184 /* Read a Fortran module. */
16185
16186 static void
16187 read_module (struct die_info *die, struct dwarf2_cu *cu)
16188 {
16189 struct die_info *child_die = die->child;
16190 struct type *type;
16191
16192 type = read_type_die (die, cu);
16193 new_symbol (die, type, cu);
16194
16195 while (child_die && child_die->tag)
16196 {
16197 process_die (child_die, cu);
16198 child_die = sibling_die (child_die);
16199 }
16200 }
16201
16202 /* Return the name of the namespace represented by DIE. Set
16203 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16204 namespace. */
16205
16206 static const char *
16207 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16208 {
16209 struct die_info *current_die;
16210 const char *name = NULL;
16211
16212 /* Loop through the extensions until we find a name. */
16213
16214 for (current_die = die;
16215 current_die != NULL;
16216 current_die = dwarf2_extension (die, &cu))
16217 {
16218 /* We don't use dwarf2_name here so that we can detect the absence
16219 of a name -> anonymous namespace. */
16220 name = dwarf2_string_attr (die, DW_AT_name, cu);
16221
16222 if (name != NULL)
16223 break;
16224 }
16225
16226 /* Is it an anonymous namespace? */
16227
16228 *is_anonymous = (name == NULL);
16229 if (*is_anonymous)
16230 name = CP_ANONYMOUS_NAMESPACE_STR;
16231
16232 return name;
16233 }
16234
16235 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16236 the user defined type vector. */
16237
16238 static struct type *
16239 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16240 {
16241 struct gdbarch *gdbarch
16242 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16243 struct comp_unit_head *cu_header = &cu->header;
16244 struct type *type;
16245 struct attribute *attr_byte_size;
16246 struct attribute *attr_address_class;
16247 int byte_size, addr_class;
16248 struct type *target_type;
16249
16250 target_type = die_type (die, cu);
16251
16252 /* The die_type call above may have already set the type for this DIE. */
16253 type = get_die_type (die, cu);
16254 if (type)
16255 return type;
16256
16257 type = lookup_pointer_type (target_type);
16258
16259 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16260 if (attr_byte_size)
16261 byte_size = DW_UNSND (attr_byte_size);
16262 else
16263 byte_size = cu_header->addr_size;
16264
16265 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16266 if (attr_address_class)
16267 addr_class = DW_UNSND (attr_address_class);
16268 else
16269 addr_class = DW_ADDR_none;
16270
16271 ULONGEST alignment = get_alignment (cu, die);
16272
16273 /* If the pointer size, alignment, or address class is different
16274 than the default, create a type variant marked as such and set
16275 the length accordingly. */
16276 if (TYPE_LENGTH (type) != byte_size
16277 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16278 && alignment != TYPE_RAW_ALIGN (type))
16279 || addr_class != DW_ADDR_none)
16280 {
16281 if (gdbarch_address_class_type_flags_p (gdbarch))
16282 {
16283 int type_flags;
16284
16285 type_flags = gdbarch_address_class_type_flags
16286 (gdbarch, byte_size, addr_class);
16287 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16288 == 0);
16289 type = make_type_with_address_space (type, type_flags);
16290 }
16291 else if (TYPE_LENGTH (type) != byte_size)
16292 {
16293 complaint (_("invalid pointer size %d"), byte_size);
16294 }
16295 else if (TYPE_RAW_ALIGN (type) != alignment)
16296 {
16297 complaint (_("Invalid DW_AT_alignment"
16298 " - DIE at %s [in module %s]"),
16299 sect_offset_str (die->sect_off),
16300 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16301 }
16302 else
16303 {
16304 /* Should we also complain about unhandled address classes? */
16305 }
16306 }
16307
16308 TYPE_LENGTH (type) = byte_size;
16309 set_type_align (type, alignment);
16310 return set_die_type (die, type, cu);
16311 }
16312
16313 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16314 the user defined type vector. */
16315
16316 static struct type *
16317 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16318 {
16319 struct type *type;
16320 struct type *to_type;
16321 struct type *domain;
16322
16323 to_type = die_type (die, cu);
16324 domain = die_containing_type (die, cu);
16325
16326 /* The calls above may have already set the type for this DIE. */
16327 type = get_die_type (die, cu);
16328 if (type)
16329 return type;
16330
16331 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16332 type = lookup_methodptr_type (to_type);
16333 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16334 {
16335 struct type *new_type
16336 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16337
16338 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16339 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16340 TYPE_VARARGS (to_type));
16341 type = lookup_methodptr_type (new_type);
16342 }
16343 else
16344 type = lookup_memberptr_type (to_type, domain);
16345
16346 return set_die_type (die, type, cu);
16347 }
16348
16349 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16350 the user defined type vector. */
16351
16352 static struct type *
16353 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16354 enum type_code refcode)
16355 {
16356 struct comp_unit_head *cu_header = &cu->header;
16357 struct type *type, *target_type;
16358 struct attribute *attr;
16359
16360 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16361
16362 target_type = die_type (die, cu);
16363
16364 /* The die_type call above may have already set the type for this DIE. */
16365 type = get_die_type (die, cu);
16366 if (type)
16367 return type;
16368
16369 type = lookup_reference_type (target_type, refcode);
16370 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16371 if (attr != nullptr)
16372 {
16373 TYPE_LENGTH (type) = DW_UNSND (attr);
16374 }
16375 else
16376 {
16377 TYPE_LENGTH (type) = cu_header->addr_size;
16378 }
16379 maybe_set_alignment (cu, die, type);
16380 return set_die_type (die, type, cu);
16381 }
16382
16383 /* Add the given cv-qualifiers to the element type of the array. GCC
16384 outputs DWARF type qualifiers that apply to an array, not the
16385 element type. But GDB relies on the array element type to carry
16386 the cv-qualifiers. This mimics section 6.7.3 of the C99
16387 specification. */
16388
16389 static struct type *
16390 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16391 struct type *base_type, int cnst, int voltl)
16392 {
16393 struct type *el_type, *inner_array;
16394
16395 base_type = copy_type (base_type);
16396 inner_array = base_type;
16397
16398 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16399 {
16400 TYPE_TARGET_TYPE (inner_array) =
16401 copy_type (TYPE_TARGET_TYPE (inner_array));
16402 inner_array = TYPE_TARGET_TYPE (inner_array);
16403 }
16404
16405 el_type = TYPE_TARGET_TYPE (inner_array);
16406 cnst |= TYPE_CONST (el_type);
16407 voltl |= TYPE_VOLATILE (el_type);
16408 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16409
16410 return set_die_type (die, base_type, cu);
16411 }
16412
16413 static struct type *
16414 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16415 {
16416 struct type *base_type, *cv_type;
16417
16418 base_type = die_type (die, cu);
16419
16420 /* The die_type call above may have already set the type for this DIE. */
16421 cv_type = get_die_type (die, cu);
16422 if (cv_type)
16423 return cv_type;
16424
16425 /* In case the const qualifier is applied to an array type, the element type
16426 is so qualified, not the array type (section 6.7.3 of C99). */
16427 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16428 return add_array_cv_type (die, cu, base_type, 1, 0);
16429
16430 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16431 return set_die_type (die, cv_type, cu);
16432 }
16433
16434 static struct type *
16435 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16436 {
16437 struct type *base_type, *cv_type;
16438
16439 base_type = die_type (die, cu);
16440
16441 /* The die_type call above may have already set the type for this DIE. */
16442 cv_type = get_die_type (die, cu);
16443 if (cv_type)
16444 return cv_type;
16445
16446 /* In case the volatile qualifier is applied to an array type, the
16447 element type is so qualified, not the array type (section 6.7.3
16448 of C99). */
16449 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16450 return add_array_cv_type (die, cu, base_type, 0, 1);
16451
16452 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16453 return set_die_type (die, cv_type, cu);
16454 }
16455
16456 /* Handle DW_TAG_restrict_type. */
16457
16458 static struct type *
16459 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16460 {
16461 struct type *base_type, *cv_type;
16462
16463 base_type = die_type (die, cu);
16464
16465 /* The die_type call above may have already set the type for this DIE. */
16466 cv_type = get_die_type (die, cu);
16467 if (cv_type)
16468 return cv_type;
16469
16470 cv_type = make_restrict_type (base_type);
16471 return set_die_type (die, cv_type, cu);
16472 }
16473
16474 /* Handle DW_TAG_atomic_type. */
16475
16476 static struct type *
16477 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16478 {
16479 struct type *base_type, *cv_type;
16480
16481 base_type = die_type (die, cu);
16482
16483 /* The die_type call above may have already set the type for this DIE. */
16484 cv_type = get_die_type (die, cu);
16485 if (cv_type)
16486 return cv_type;
16487
16488 cv_type = make_atomic_type (base_type);
16489 return set_die_type (die, cv_type, cu);
16490 }
16491
16492 /* Extract all information from a DW_TAG_string_type DIE and add to
16493 the user defined type vector. It isn't really a user defined type,
16494 but it behaves like one, with other DIE's using an AT_user_def_type
16495 attribute to reference it. */
16496
16497 static struct type *
16498 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16499 {
16500 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16501 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16502 struct type *type, *range_type, *index_type, *char_type;
16503 struct attribute *attr;
16504 struct dynamic_prop prop;
16505 bool length_is_constant = true;
16506 LONGEST length;
16507
16508 /* There are a couple of places where bit sizes might be made use of
16509 when parsing a DW_TAG_string_type, however, no producer that we know
16510 of make use of these. Handling bit sizes that are a multiple of the
16511 byte size is easy enough, but what about other bit sizes? Lets deal
16512 with that problem when we have to. Warn about these attributes being
16513 unsupported, then parse the type and ignore them like we always
16514 have. */
16515 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16516 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16517 {
16518 static bool warning_printed = false;
16519 if (!warning_printed)
16520 {
16521 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16522 "currently supported on DW_TAG_string_type."));
16523 warning_printed = true;
16524 }
16525 }
16526
16527 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16528 if (attr != nullptr && !attr->form_is_constant ())
16529 {
16530 /* The string length describes the location at which the length of
16531 the string can be found. The size of the length field can be
16532 specified with one of the attributes below. */
16533 struct type *prop_type;
16534 struct attribute *len
16535 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16536 if (len == nullptr)
16537 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16538 if (len != nullptr && len->form_is_constant ())
16539 {
16540 /* Pass 0 as the default as we know this attribute is constant
16541 and the default value will not be returned. */
16542 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16543 prop_type = cu->per_cu->int_type (sz, true);
16544 }
16545 else
16546 {
16547 /* If the size is not specified then we assume it is the size of
16548 an address on this target. */
16549 prop_type = cu->per_cu->addr_sized_int_type (true);
16550 }
16551
16552 /* Convert the attribute into a dynamic property. */
16553 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16554 length = 1;
16555 else
16556 length_is_constant = false;
16557 }
16558 else if (attr != nullptr)
16559 {
16560 /* This DW_AT_string_length just contains the length with no
16561 indirection. There's no need to create a dynamic property in this
16562 case. Pass 0 for the default value as we know it will not be
16563 returned in this case. */
16564 length = dwarf2_get_attr_constant_value (attr, 0);
16565 }
16566 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16567 {
16568 /* We don't currently support non-constant byte sizes for strings. */
16569 length = dwarf2_get_attr_constant_value (attr, 1);
16570 }
16571 else
16572 {
16573 /* Use 1 as a fallback length if we have nothing else. */
16574 length = 1;
16575 }
16576
16577 index_type = objfile_type (objfile)->builtin_int;
16578 if (length_is_constant)
16579 range_type = create_static_range_type (NULL, index_type, 1, length);
16580 else
16581 {
16582 struct dynamic_prop low_bound;
16583
16584 low_bound.kind = PROP_CONST;
16585 low_bound.data.const_val = 1;
16586 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16587 }
16588 char_type = language_string_char_type (cu->language_defn, gdbarch);
16589 type = create_string_type (NULL, char_type, range_type);
16590
16591 return set_die_type (die, type, cu);
16592 }
16593
16594 /* Assuming that DIE corresponds to a function, returns nonzero
16595 if the function is prototyped. */
16596
16597 static int
16598 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16599 {
16600 struct attribute *attr;
16601
16602 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16603 if (attr && (DW_UNSND (attr) != 0))
16604 return 1;
16605
16606 /* The DWARF standard implies that the DW_AT_prototyped attribute
16607 is only meaningful for C, but the concept also extends to other
16608 languages that allow unprototyped functions (Eg: Objective C).
16609 For all other languages, assume that functions are always
16610 prototyped. */
16611 if (cu->language != language_c
16612 && cu->language != language_objc
16613 && cu->language != language_opencl)
16614 return 1;
16615
16616 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16617 prototyped and unprototyped functions; default to prototyped,
16618 since that is more common in modern code (and RealView warns
16619 about unprototyped functions). */
16620 if (producer_is_realview (cu->producer))
16621 return 1;
16622
16623 return 0;
16624 }
16625
16626 /* Handle DIES due to C code like:
16627
16628 struct foo
16629 {
16630 int (*funcp)(int a, long l);
16631 int b;
16632 };
16633
16634 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16635
16636 static struct type *
16637 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16638 {
16639 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16640 struct type *type; /* Type that this function returns. */
16641 struct type *ftype; /* Function that returns above type. */
16642 struct attribute *attr;
16643
16644 type = die_type (die, cu);
16645
16646 /* The die_type call above may have already set the type for this DIE. */
16647 ftype = get_die_type (die, cu);
16648 if (ftype)
16649 return ftype;
16650
16651 ftype = lookup_function_type (type);
16652
16653 if (prototyped_function_p (die, cu))
16654 TYPE_PROTOTYPED (ftype) = 1;
16655
16656 /* Store the calling convention in the type if it's available in
16657 the subroutine die. Otherwise set the calling convention to
16658 the default value DW_CC_normal. */
16659 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16660 if (attr != nullptr
16661 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16662 TYPE_CALLING_CONVENTION (ftype)
16663 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16664 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16665 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16666 else
16667 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16668
16669 /* Record whether the function returns normally to its caller or not
16670 if the DWARF producer set that information. */
16671 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16672 if (attr && (DW_UNSND (attr) != 0))
16673 TYPE_NO_RETURN (ftype) = 1;
16674
16675 /* We need to add the subroutine type to the die immediately so
16676 we don't infinitely recurse when dealing with parameters
16677 declared as the same subroutine type. */
16678 set_die_type (die, ftype, cu);
16679
16680 if (die->child != NULL)
16681 {
16682 struct type *void_type = objfile_type (objfile)->builtin_void;
16683 struct die_info *child_die;
16684 int nparams, iparams;
16685
16686 /* Count the number of parameters.
16687 FIXME: GDB currently ignores vararg functions, but knows about
16688 vararg member functions. */
16689 nparams = 0;
16690 child_die = die->child;
16691 while (child_die && child_die->tag)
16692 {
16693 if (child_die->tag == DW_TAG_formal_parameter)
16694 nparams++;
16695 else if (child_die->tag == DW_TAG_unspecified_parameters)
16696 TYPE_VARARGS (ftype) = 1;
16697 child_die = sibling_die (child_die);
16698 }
16699
16700 /* Allocate storage for parameters and fill them in. */
16701 TYPE_NFIELDS (ftype) = nparams;
16702 TYPE_FIELDS (ftype) = (struct field *)
16703 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16704
16705 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16706 even if we error out during the parameters reading below. */
16707 for (iparams = 0; iparams < nparams; iparams++)
16708 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16709
16710 iparams = 0;
16711 child_die = die->child;
16712 while (child_die && child_die->tag)
16713 {
16714 if (child_die->tag == DW_TAG_formal_parameter)
16715 {
16716 struct type *arg_type;
16717
16718 /* DWARF version 2 has no clean way to discern C++
16719 static and non-static member functions. G++ helps
16720 GDB by marking the first parameter for non-static
16721 member functions (which is the this pointer) as
16722 artificial. We pass this information to
16723 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16724
16725 DWARF version 3 added DW_AT_object_pointer, which GCC
16726 4.5 does not yet generate. */
16727 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16728 if (attr != nullptr)
16729 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16730 else
16731 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16732 arg_type = die_type (child_die, cu);
16733
16734 /* RealView does not mark THIS as const, which the testsuite
16735 expects. GCC marks THIS as const in method definitions,
16736 but not in the class specifications (GCC PR 43053). */
16737 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16738 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16739 {
16740 int is_this = 0;
16741 struct dwarf2_cu *arg_cu = cu;
16742 const char *name = dwarf2_name (child_die, cu);
16743
16744 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16745 if (attr != nullptr)
16746 {
16747 /* If the compiler emits this, use it. */
16748 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16749 is_this = 1;
16750 }
16751 else if (name && strcmp (name, "this") == 0)
16752 /* Function definitions will have the argument names. */
16753 is_this = 1;
16754 else if (name == NULL && iparams == 0)
16755 /* Declarations may not have the names, so like
16756 elsewhere in GDB, assume an artificial first
16757 argument is "this". */
16758 is_this = 1;
16759
16760 if (is_this)
16761 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16762 arg_type, 0);
16763 }
16764
16765 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16766 iparams++;
16767 }
16768 child_die = sibling_die (child_die);
16769 }
16770 }
16771
16772 return ftype;
16773 }
16774
16775 static struct type *
16776 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16777 {
16778 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16779 const char *name = NULL;
16780 struct type *this_type, *target_type;
16781
16782 name = dwarf2_full_name (NULL, die, cu);
16783 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16784 TYPE_TARGET_STUB (this_type) = 1;
16785 set_die_type (die, this_type, cu);
16786 target_type = die_type (die, cu);
16787 if (target_type != this_type)
16788 TYPE_TARGET_TYPE (this_type) = target_type;
16789 else
16790 {
16791 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16792 spec and cause infinite loops in GDB. */
16793 complaint (_("Self-referential DW_TAG_typedef "
16794 "- DIE at %s [in module %s]"),
16795 sect_offset_str (die->sect_off), objfile_name (objfile));
16796 TYPE_TARGET_TYPE (this_type) = NULL;
16797 }
16798 return this_type;
16799 }
16800
16801 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16802 (which may be different from NAME) to the architecture back-end to allow
16803 it to guess the correct format if necessary. */
16804
16805 static struct type *
16806 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16807 const char *name_hint, enum bfd_endian byte_order)
16808 {
16809 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16810 const struct floatformat **format;
16811 struct type *type;
16812
16813 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16814 if (format)
16815 type = init_float_type (objfile, bits, name, format, byte_order);
16816 else
16817 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16818
16819 return type;
16820 }
16821
16822 /* Allocate an integer type of size BITS and name NAME. */
16823
16824 static struct type *
16825 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16826 int bits, int unsigned_p, const char *name)
16827 {
16828 struct type *type;
16829
16830 /* Versions of Intel's C Compiler generate an integer type called "void"
16831 instead of using DW_TAG_unspecified_type. This has been seen on
16832 at least versions 14, 17, and 18. */
16833 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16834 && strcmp (name, "void") == 0)
16835 type = objfile_type (objfile)->builtin_void;
16836 else
16837 type = init_integer_type (objfile, bits, unsigned_p, name);
16838
16839 return type;
16840 }
16841
16842 /* Initialise and return a floating point type of size BITS suitable for
16843 use as a component of a complex number. The NAME_HINT is passed through
16844 when initialising the floating point type and is the name of the complex
16845 type.
16846
16847 As DWARF doesn't currently provide an explicit name for the components
16848 of a complex number, but it can be helpful to have these components
16849 named, we try to select a suitable name based on the size of the
16850 component. */
16851 static struct type *
16852 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16853 struct objfile *objfile,
16854 int bits, const char *name_hint,
16855 enum bfd_endian byte_order)
16856 {
16857 gdbarch *gdbarch = get_objfile_arch (objfile);
16858 struct type *tt = nullptr;
16859
16860 /* Try to find a suitable floating point builtin type of size BITS.
16861 We're going to use the name of this type as the name for the complex
16862 target type that we are about to create. */
16863 switch (cu->language)
16864 {
16865 case language_fortran:
16866 switch (bits)
16867 {
16868 case 32:
16869 tt = builtin_f_type (gdbarch)->builtin_real;
16870 break;
16871 case 64:
16872 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16873 break;
16874 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16875 case 128:
16876 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16877 break;
16878 }
16879 break;
16880 default:
16881 switch (bits)
16882 {
16883 case 32:
16884 tt = builtin_type (gdbarch)->builtin_float;
16885 break;
16886 case 64:
16887 tt = builtin_type (gdbarch)->builtin_double;
16888 break;
16889 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16890 case 128:
16891 tt = builtin_type (gdbarch)->builtin_long_double;
16892 break;
16893 }
16894 break;
16895 }
16896
16897 /* If the type we found doesn't match the size we were looking for, then
16898 pretend we didn't find a type at all, the complex target type we
16899 create will then be nameless. */
16900 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16901 tt = nullptr;
16902
16903 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16904 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16905 }
16906
16907 /* Find a representation of a given base type and install
16908 it in the TYPE field of the die. */
16909
16910 static struct type *
16911 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16912 {
16913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16914 struct type *type;
16915 struct attribute *attr;
16916 int encoding = 0, bits = 0;
16917 const char *name;
16918 gdbarch *arch;
16919
16920 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16921 if (attr != nullptr)
16922 encoding = DW_UNSND (attr);
16923 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16924 if (attr != nullptr)
16925 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16926 name = dwarf2_name (die, cu);
16927 if (!name)
16928 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16929
16930 arch = get_objfile_arch (objfile);
16931 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16932
16933 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16934 if (attr)
16935 {
16936 int endianity = DW_UNSND (attr);
16937
16938 switch (endianity)
16939 {
16940 case DW_END_big:
16941 byte_order = BFD_ENDIAN_BIG;
16942 break;
16943 case DW_END_little:
16944 byte_order = BFD_ENDIAN_LITTLE;
16945 break;
16946 default:
16947 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16948 break;
16949 }
16950 }
16951
16952 switch (encoding)
16953 {
16954 case DW_ATE_address:
16955 /* Turn DW_ATE_address into a void * pointer. */
16956 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16957 type = init_pointer_type (objfile, bits, name, type);
16958 break;
16959 case DW_ATE_boolean:
16960 type = init_boolean_type (objfile, bits, 1, name);
16961 break;
16962 case DW_ATE_complex_float:
16963 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16964 byte_order);
16965 type = init_complex_type (objfile, name, type);
16966 break;
16967 case DW_ATE_decimal_float:
16968 type = init_decfloat_type (objfile, bits, name);
16969 break;
16970 case DW_ATE_float:
16971 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16972 break;
16973 case DW_ATE_signed:
16974 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16975 break;
16976 case DW_ATE_unsigned:
16977 if (cu->language == language_fortran
16978 && name
16979 && startswith (name, "character("))
16980 type = init_character_type (objfile, bits, 1, name);
16981 else
16982 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16983 break;
16984 case DW_ATE_signed_char:
16985 if (cu->language == language_ada || cu->language == language_m2
16986 || cu->language == language_pascal
16987 || cu->language == language_fortran)
16988 type = init_character_type (objfile, bits, 0, name);
16989 else
16990 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16991 break;
16992 case DW_ATE_unsigned_char:
16993 if (cu->language == language_ada || cu->language == language_m2
16994 || cu->language == language_pascal
16995 || cu->language == language_fortran
16996 || cu->language == language_rust)
16997 type = init_character_type (objfile, bits, 1, name);
16998 else
16999 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17000 break;
17001 case DW_ATE_UTF:
17002 {
17003 if (bits == 16)
17004 type = builtin_type (arch)->builtin_char16;
17005 else if (bits == 32)
17006 type = builtin_type (arch)->builtin_char32;
17007 else
17008 {
17009 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17010 bits);
17011 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17012 }
17013 return set_die_type (die, type, cu);
17014 }
17015 break;
17016
17017 default:
17018 complaint (_("unsupported DW_AT_encoding: '%s'"),
17019 dwarf_type_encoding_name (encoding));
17020 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17021 break;
17022 }
17023
17024 if (name && strcmp (name, "char") == 0)
17025 TYPE_NOSIGN (type) = 1;
17026
17027 maybe_set_alignment (cu, die, type);
17028
17029 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17030
17031 return set_die_type (die, type, cu);
17032 }
17033
17034 /* Parse dwarf attribute if it's a block, reference or constant and put the
17035 resulting value of the attribute into struct bound_prop.
17036 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17037
17038 static int
17039 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17040 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17041 struct type *default_type)
17042 {
17043 struct dwarf2_property_baton *baton;
17044 struct obstack *obstack
17045 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17046
17047 gdb_assert (default_type != NULL);
17048
17049 if (attr == NULL || prop == NULL)
17050 return 0;
17051
17052 if (attr->form_is_block ())
17053 {
17054 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17055 baton->property_type = default_type;
17056 baton->locexpr.per_cu = cu->per_cu;
17057 baton->locexpr.size = DW_BLOCK (attr)->size;
17058 baton->locexpr.data = DW_BLOCK (attr)->data;
17059 switch (attr->name)
17060 {
17061 case DW_AT_string_length:
17062 baton->locexpr.is_reference = true;
17063 break;
17064 default:
17065 baton->locexpr.is_reference = false;
17066 break;
17067 }
17068 prop->data.baton = baton;
17069 prop->kind = PROP_LOCEXPR;
17070 gdb_assert (prop->data.baton != NULL);
17071 }
17072 else if (attr->form_is_ref ())
17073 {
17074 struct dwarf2_cu *target_cu = cu;
17075 struct die_info *target_die;
17076 struct attribute *target_attr;
17077
17078 target_die = follow_die_ref (die, attr, &target_cu);
17079 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17080 if (target_attr == NULL)
17081 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17082 target_cu);
17083 if (target_attr == NULL)
17084 return 0;
17085
17086 switch (target_attr->name)
17087 {
17088 case DW_AT_location:
17089 if (target_attr->form_is_section_offset ())
17090 {
17091 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17092 baton->property_type = die_type (target_die, target_cu);
17093 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17094 prop->data.baton = baton;
17095 prop->kind = PROP_LOCLIST;
17096 gdb_assert (prop->data.baton != NULL);
17097 }
17098 else if (target_attr->form_is_block ())
17099 {
17100 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17101 baton->property_type = die_type (target_die, target_cu);
17102 baton->locexpr.per_cu = cu->per_cu;
17103 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17104 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17105 baton->locexpr.is_reference = true;
17106 prop->data.baton = baton;
17107 prop->kind = PROP_LOCEXPR;
17108 gdb_assert (prop->data.baton != NULL);
17109 }
17110 else
17111 {
17112 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17113 "dynamic property");
17114 return 0;
17115 }
17116 break;
17117 case DW_AT_data_member_location:
17118 {
17119 LONGEST offset;
17120
17121 if (!handle_data_member_location (target_die, target_cu,
17122 &offset))
17123 return 0;
17124
17125 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17126 baton->property_type = read_type_die (target_die->parent,
17127 target_cu);
17128 baton->offset_info.offset = offset;
17129 baton->offset_info.type = die_type (target_die, target_cu);
17130 prop->data.baton = baton;
17131 prop->kind = PROP_ADDR_OFFSET;
17132 break;
17133 }
17134 }
17135 }
17136 else if (attr->form_is_constant ())
17137 {
17138 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17139 prop->kind = PROP_CONST;
17140 }
17141 else
17142 {
17143 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17144 dwarf2_name (die, cu));
17145 return 0;
17146 }
17147
17148 return 1;
17149 }
17150
17151 /* See read.h. */
17152
17153 struct type *
17154 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17155 {
17156 struct objfile *objfile = dwarf2_per_objfile->objfile;
17157 struct type *int_type;
17158
17159 /* Helper macro to examine the various builtin types. */
17160 #define TRY_TYPE(F) \
17161 int_type = (unsigned_p \
17162 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17163 : objfile_type (objfile)->builtin_ ## F); \
17164 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17165 return int_type
17166
17167 TRY_TYPE (char);
17168 TRY_TYPE (short);
17169 TRY_TYPE (int);
17170 TRY_TYPE (long);
17171 TRY_TYPE (long_long);
17172
17173 #undef TRY_TYPE
17174
17175 gdb_assert_not_reached ("unable to find suitable integer type");
17176 }
17177
17178 /* See read.h. */
17179
17180 struct type *
17181 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17182 {
17183 int addr_size = this->addr_size ();
17184 return int_type (addr_size, unsigned_p);
17185 }
17186
17187 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17188 present (which is valid) then compute the default type based on the
17189 compilation units address size. */
17190
17191 static struct type *
17192 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17193 {
17194 struct type *index_type = die_type (die, cu);
17195
17196 /* Dwarf-2 specifications explicitly allows to create subrange types
17197 without specifying a base type.
17198 In that case, the base type must be set to the type of
17199 the lower bound, upper bound or count, in that order, if any of these
17200 three attributes references an object that has a type.
17201 If no base type is found, the Dwarf-2 specifications say that
17202 a signed integer type of size equal to the size of an address should
17203 be used.
17204 For the following C code: `extern char gdb_int [];'
17205 GCC produces an empty range DIE.
17206 FIXME: muller/2010-05-28: Possible references to object for low bound,
17207 high bound or count are not yet handled by this code. */
17208 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17209 index_type = cu->per_cu->addr_sized_int_type (false);
17210
17211 return index_type;
17212 }
17213
17214 /* Read the given DW_AT_subrange DIE. */
17215
17216 static struct type *
17217 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17218 {
17219 struct type *base_type, *orig_base_type;
17220 struct type *range_type;
17221 struct attribute *attr;
17222 struct dynamic_prop low, high;
17223 int low_default_is_valid;
17224 int high_bound_is_count = 0;
17225 const char *name;
17226 ULONGEST negative_mask;
17227
17228 orig_base_type = read_subrange_index_type (die, cu);
17229
17230 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17231 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17232 creating the range type, but we use the result of check_typedef
17233 when examining properties of the type. */
17234 base_type = check_typedef (orig_base_type);
17235
17236 /* The die_type call above may have already set the type for this DIE. */
17237 range_type = get_die_type (die, cu);
17238 if (range_type)
17239 return range_type;
17240
17241 low.kind = PROP_CONST;
17242 high.kind = PROP_CONST;
17243 high.data.const_val = 0;
17244
17245 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17246 omitting DW_AT_lower_bound. */
17247 switch (cu->language)
17248 {
17249 case language_c:
17250 case language_cplus:
17251 low.data.const_val = 0;
17252 low_default_is_valid = 1;
17253 break;
17254 case language_fortran:
17255 low.data.const_val = 1;
17256 low_default_is_valid = 1;
17257 break;
17258 case language_d:
17259 case language_objc:
17260 case language_rust:
17261 low.data.const_val = 0;
17262 low_default_is_valid = (cu->header.version >= 4);
17263 break;
17264 case language_ada:
17265 case language_m2:
17266 case language_pascal:
17267 low.data.const_val = 1;
17268 low_default_is_valid = (cu->header.version >= 4);
17269 break;
17270 default:
17271 low.data.const_val = 0;
17272 low_default_is_valid = 0;
17273 break;
17274 }
17275
17276 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17277 if (attr != nullptr)
17278 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17279 else if (!low_default_is_valid)
17280 complaint (_("Missing DW_AT_lower_bound "
17281 "- DIE at %s [in module %s]"),
17282 sect_offset_str (die->sect_off),
17283 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17284
17285 struct attribute *attr_ub, *attr_count;
17286 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17287 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17288 {
17289 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17290 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17291 {
17292 /* If bounds are constant do the final calculation here. */
17293 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17294 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17295 else
17296 high_bound_is_count = 1;
17297 }
17298 else
17299 {
17300 if (attr_ub != NULL)
17301 complaint (_("Unresolved DW_AT_upper_bound "
17302 "- DIE at %s [in module %s]"),
17303 sect_offset_str (die->sect_off),
17304 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17305 if (attr_count != NULL)
17306 complaint (_("Unresolved DW_AT_count "
17307 "- DIE at %s [in module %s]"),
17308 sect_offset_str (die->sect_off),
17309 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17310 }
17311 }
17312
17313 LONGEST bias = 0;
17314 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17315 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17316 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17317
17318 /* Normally, the DWARF producers are expected to use a signed
17319 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17320 But this is unfortunately not always the case, as witnessed
17321 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17322 is used instead. To work around that ambiguity, we treat
17323 the bounds as signed, and thus sign-extend their values, when
17324 the base type is signed. */
17325 negative_mask =
17326 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17327 if (low.kind == PROP_CONST
17328 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17329 low.data.const_val |= negative_mask;
17330 if (high.kind == PROP_CONST
17331 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17332 high.data.const_val |= negative_mask;
17333
17334 /* Check for bit and byte strides. */
17335 struct dynamic_prop byte_stride_prop;
17336 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17337 if (attr_byte_stride != nullptr)
17338 {
17339 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17340 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17341 prop_type);
17342 }
17343
17344 struct dynamic_prop bit_stride_prop;
17345 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17346 if (attr_bit_stride != nullptr)
17347 {
17348 /* It only makes sense to have either a bit or byte stride. */
17349 if (attr_byte_stride != nullptr)
17350 {
17351 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17352 "- DIE at %s [in module %s]"),
17353 sect_offset_str (die->sect_off),
17354 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17355 attr_bit_stride = nullptr;
17356 }
17357 else
17358 {
17359 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17360 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17361 prop_type);
17362 }
17363 }
17364
17365 if (attr_byte_stride != nullptr
17366 || attr_bit_stride != nullptr)
17367 {
17368 bool byte_stride_p = (attr_byte_stride != nullptr);
17369 struct dynamic_prop *stride
17370 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17371
17372 range_type
17373 = create_range_type_with_stride (NULL, orig_base_type, &low,
17374 &high, bias, stride, byte_stride_p);
17375 }
17376 else
17377 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17378
17379 if (high_bound_is_count)
17380 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17381
17382 /* Ada expects an empty array on no boundary attributes. */
17383 if (attr == NULL && cu->language != language_ada)
17384 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17385
17386 name = dwarf2_name (die, cu);
17387 if (name)
17388 TYPE_NAME (range_type) = name;
17389
17390 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17391 if (attr != nullptr)
17392 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17393
17394 maybe_set_alignment (cu, die, range_type);
17395
17396 set_die_type (die, range_type, cu);
17397
17398 /* set_die_type should be already done. */
17399 set_descriptive_type (range_type, die, cu);
17400
17401 return range_type;
17402 }
17403
17404 static struct type *
17405 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17406 {
17407 struct type *type;
17408
17409 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17410 NULL);
17411 TYPE_NAME (type) = dwarf2_name (die, cu);
17412
17413 /* In Ada, an unspecified type is typically used when the description
17414 of the type is deferred to a different unit. When encountering
17415 such a type, we treat it as a stub, and try to resolve it later on,
17416 when needed. */
17417 if (cu->language == language_ada)
17418 TYPE_STUB (type) = 1;
17419
17420 return set_die_type (die, type, cu);
17421 }
17422
17423 /* Read a single die and all its descendents. Set the die's sibling
17424 field to NULL; set other fields in the die correctly, and set all
17425 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17426 location of the info_ptr after reading all of those dies. PARENT
17427 is the parent of the die in question. */
17428
17429 static struct die_info *
17430 read_die_and_children (const struct die_reader_specs *reader,
17431 const gdb_byte *info_ptr,
17432 const gdb_byte **new_info_ptr,
17433 struct die_info *parent)
17434 {
17435 struct die_info *die;
17436 const gdb_byte *cur_ptr;
17437
17438 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17439 if (die == NULL)
17440 {
17441 *new_info_ptr = cur_ptr;
17442 return NULL;
17443 }
17444 store_in_ref_table (die, reader->cu);
17445
17446 if (die->has_children)
17447 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17448 else
17449 {
17450 die->child = NULL;
17451 *new_info_ptr = cur_ptr;
17452 }
17453
17454 die->sibling = NULL;
17455 die->parent = parent;
17456 return die;
17457 }
17458
17459 /* Read a die, all of its descendents, and all of its siblings; set
17460 all of the fields of all of the dies correctly. Arguments are as
17461 in read_die_and_children. */
17462
17463 static struct die_info *
17464 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17465 const gdb_byte *info_ptr,
17466 const gdb_byte **new_info_ptr,
17467 struct die_info *parent)
17468 {
17469 struct die_info *first_die, *last_sibling;
17470 const gdb_byte *cur_ptr;
17471
17472 cur_ptr = info_ptr;
17473 first_die = last_sibling = NULL;
17474
17475 while (1)
17476 {
17477 struct die_info *die
17478 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17479
17480 if (die == NULL)
17481 {
17482 *new_info_ptr = cur_ptr;
17483 return first_die;
17484 }
17485
17486 if (!first_die)
17487 first_die = die;
17488 else
17489 last_sibling->sibling = die;
17490
17491 last_sibling = die;
17492 }
17493 }
17494
17495 /* Read a die, all of its descendents, and all of its siblings; set
17496 all of the fields of all of the dies correctly. Arguments are as
17497 in read_die_and_children.
17498 This the main entry point for reading a DIE and all its children. */
17499
17500 static struct die_info *
17501 read_die_and_siblings (const struct die_reader_specs *reader,
17502 const gdb_byte *info_ptr,
17503 const gdb_byte **new_info_ptr,
17504 struct die_info *parent)
17505 {
17506 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17507 new_info_ptr, parent);
17508
17509 if (dwarf_die_debug)
17510 {
17511 fprintf_unfiltered (gdb_stdlog,
17512 "Read die from %s@0x%x of %s:\n",
17513 reader->die_section->get_name (),
17514 (unsigned) (info_ptr - reader->die_section->buffer),
17515 bfd_get_filename (reader->abfd));
17516 dump_die (die, dwarf_die_debug);
17517 }
17518
17519 return die;
17520 }
17521
17522 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17523 attributes.
17524 The caller is responsible for filling in the extra attributes
17525 and updating (*DIEP)->num_attrs.
17526 Set DIEP to point to a newly allocated die with its information,
17527 except for its child, sibling, and parent fields. */
17528
17529 static const gdb_byte *
17530 read_full_die_1 (const struct die_reader_specs *reader,
17531 struct die_info **diep, const gdb_byte *info_ptr,
17532 int num_extra_attrs)
17533 {
17534 unsigned int abbrev_number, bytes_read, i;
17535 struct abbrev_info *abbrev;
17536 struct die_info *die;
17537 struct dwarf2_cu *cu = reader->cu;
17538 bfd *abfd = reader->abfd;
17539
17540 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17541 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17542 info_ptr += bytes_read;
17543 if (!abbrev_number)
17544 {
17545 *diep = NULL;
17546 return info_ptr;
17547 }
17548
17549 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17550 if (!abbrev)
17551 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17552 abbrev_number,
17553 bfd_get_filename (abfd));
17554
17555 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17556 die->sect_off = sect_off;
17557 die->tag = abbrev->tag;
17558 die->abbrev = abbrev_number;
17559 die->has_children = abbrev->has_children;
17560
17561 /* Make the result usable.
17562 The caller needs to update num_attrs after adding the extra
17563 attributes. */
17564 die->num_attrs = abbrev->num_attrs;
17565
17566 std::vector<int> indexes_that_need_reprocess;
17567 for (i = 0; i < abbrev->num_attrs; ++i)
17568 {
17569 bool need_reprocess;
17570 info_ptr =
17571 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17572 info_ptr, &need_reprocess);
17573 if (need_reprocess)
17574 indexes_that_need_reprocess.push_back (i);
17575 }
17576
17577 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17578 if (attr != nullptr)
17579 cu->str_offsets_base = DW_UNSND (attr);
17580
17581 auto maybe_addr_base = lookup_addr_base(die);
17582 if (maybe_addr_base.has_value ())
17583 cu->addr_base = *maybe_addr_base;
17584 for (int index : indexes_that_need_reprocess)
17585 read_attribute_reprocess (reader, &die->attrs[index]);
17586 *diep = die;
17587 return info_ptr;
17588 }
17589
17590 /* Read a die and all its attributes.
17591 Set DIEP to point to a newly allocated die with its information,
17592 except for its child, sibling, and parent fields. */
17593
17594 static const gdb_byte *
17595 read_full_die (const struct die_reader_specs *reader,
17596 struct die_info **diep, const gdb_byte *info_ptr)
17597 {
17598 const gdb_byte *result;
17599
17600 result = read_full_die_1 (reader, diep, info_ptr, 0);
17601
17602 if (dwarf_die_debug)
17603 {
17604 fprintf_unfiltered (gdb_stdlog,
17605 "Read die from %s@0x%x of %s:\n",
17606 reader->die_section->get_name (),
17607 (unsigned) (info_ptr - reader->die_section->buffer),
17608 bfd_get_filename (reader->abfd));
17609 dump_die (*diep, dwarf_die_debug);
17610 }
17611
17612 return result;
17613 }
17614 \f
17615
17616 /* Returns nonzero if TAG represents a type that we might generate a partial
17617 symbol for. */
17618
17619 static int
17620 is_type_tag_for_partial (int tag)
17621 {
17622 switch (tag)
17623 {
17624 #if 0
17625 /* Some types that would be reasonable to generate partial symbols for,
17626 that we don't at present. */
17627 case DW_TAG_array_type:
17628 case DW_TAG_file_type:
17629 case DW_TAG_ptr_to_member_type:
17630 case DW_TAG_set_type:
17631 case DW_TAG_string_type:
17632 case DW_TAG_subroutine_type:
17633 #endif
17634 case DW_TAG_base_type:
17635 case DW_TAG_class_type:
17636 case DW_TAG_interface_type:
17637 case DW_TAG_enumeration_type:
17638 case DW_TAG_structure_type:
17639 case DW_TAG_subrange_type:
17640 case DW_TAG_typedef:
17641 case DW_TAG_union_type:
17642 return 1;
17643 default:
17644 return 0;
17645 }
17646 }
17647
17648 /* Load all DIEs that are interesting for partial symbols into memory. */
17649
17650 static struct partial_die_info *
17651 load_partial_dies (const struct die_reader_specs *reader,
17652 const gdb_byte *info_ptr, int building_psymtab)
17653 {
17654 struct dwarf2_cu *cu = reader->cu;
17655 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17656 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17657 unsigned int bytes_read;
17658 unsigned int load_all = 0;
17659 int nesting_level = 1;
17660
17661 parent_die = NULL;
17662 last_die = NULL;
17663
17664 gdb_assert (cu->per_cu != NULL);
17665 if (cu->per_cu->load_all_dies)
17666 load_all = 1;
17667
17668 cu->partial_dies
17669 = htab_create_alloc_ex (cu->header.length / 12,
17670 partial_die_hash,
17671 partial_die_eq,
17672 NULL,
17673 &cu->comp_unit_obstack,
17674 hashtab_obstack_allocate,
17675 dummy_obstack_deallocate);
17676
17677 while (1)
17678 {
17679 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17680
17681 /* A NULL abbrev means the end of a series of children. */
17682 if (abbrev == NULL)
17683 {
17684 if (--nesting_level == 0)
17685 return first_die;
17686
17687 info_ptr += bytes_read;
17688 last_die = parent_die;
17689 parent_die = parent_die->die_parent;
17690 continue;
17691 }
17692
17693 /* Check for template arguments. We never save these; if
17694 they're seen, we just mark the parent, and go on our way. */
17695 if (parent_die != NULL
17696 && cu->language == language_cplus
17697 && (abbrev->tag == DW_TAG_template_type_param
17698 || abbrev->tag == DW_TAG_template_value_param))
17699 {
17700 parent_die->has_template_arguments = 1;
17701
17702 if (!load_all)
17703 {
17704 /* We don't need a partial DIE for the template argument. */
17705 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17706 continue;
17707 }
17708 }
17709
17710 /* We only recurse into c++ subprograms looking for template arguments.
17711 Skip their other children. */
17712 if (!load_all
17713 && cu->language == language_cplus
17714 && parent_die != NULL
17715 && parent_die->tag == DW_TAG_subprogram)
17716 {
17717 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17718 continue;
17719 }
17720
17721 /* Check whether this DIE is interesting enough to save. Normally
17722 we would not be interested in members here, but there may be
17723 later variables referencing them via DW_AT_specification (for
17724 static members). */
17725 if (!load_all
17726 && !is_type_tag_for_partial (abbrev->tag)
17727 && abbrev->tag != DW_TAG_constant
17728 && abbrev->tag != DW_TAG_enumerator
17729 && abbrev->tag != DW_TAG_subprogram
17730 && abbrev->tag != DW_TAG_inlined_subroutine
17731 && abbrev->tag != DW_TAG_lexical_block
17732 && abbrev->tag != DW_TAG_variable
17733 && abbrev->tag != DW_TAG_namespace
17734 && abbrev->tag != DW_TAG_module
17735 && abbrev->tag != DW_TAG_member
17736 && abbrev->tag != DW_TAG_imported_unit
17737 && abbrev->tag != DW_TAG_imported_declaration)
17738 {
17739 /* Otherwise we skip to the next sibling, if any. */
17740 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17741 continue;
17742 }
17743
17744 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17745 abbrev);
17746
17747 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17748
17749 /* This two-pass algorithm for processing partial symbols has a
17750 high cost in cache pressure. Thus, handle some simple cases
17751 here which cover the majority of C partial symbols. DIEs
17752 which neither have specification tags in them, nor could have
17753 specification tags elsewhere pointing at them, can simply be
17754 processed and discarded.
17755
17756 This segment is also optional; scan_partial_symbols and
17757 add_partial_symbol will handle these DIEs if we chain
17758 them in normally. When compilers which do not emit large
17759 quantities of duplicate debug information are more common,
17760 this code can probably be removed. */
17761
17762 /* Any complete simple types at the top level (pretty much all
17763 of them, for a language without namespaces), can be processed
17764 directly. */
17765 if (parent_die == NULL
17766 && pdi.has_specification == 0
17767 && pdi.is_declaration == 0
17768 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17769 || pdi.tag == DW_TAG_base_type
17770 || pdi.tag == DW_TAG_subrange_type))
17771 {
17772 if (building_psymtab && pdi.name != NULL)
17773 add_psymbol_to_list (pdi.name, false,
17774 VAR_DOMAIN, LOC_TYPEDEF, -1,
17775 psymbol_placement::STATIC,
17776 0, cu->language, objfile);
17777 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17778 continue;
17779 }
17780
17781 /* The exception for DW_TAG_typedef with has_children above is
17782 a workaround of GCC PR debug/47510. In the case of this complaint
17783 type_name_or_error will error on such types later.
17784
17785 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17786 it could not find the child DIEs referenced later, this is checked
17787 above. In correct DWARF DW_TAG_typedef should have no children. */
17788
17789 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17790 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17791 "- DIE at %s [in module %s]"),
17792 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17793
17794 /* If we're at the second level, and we're an enumerator, and
17795 our parent has no specification (meaning possibly lives in a
17796 namespace elsewhere), then we can add the partial symbol now
17797 instead of queueing it. */
17798 if (pdi.tag == DW_TAG_enumerator
17799 && parent_die != NULL
17800 && parent_die->die_parent == NULL
17801 && parent_die->tag == DW_TAG_enumeration_type
17802 && parent_die->has_specification == 0)
17803 {
17804 if (pdi.name == NULL)
17805 complaint (_("malformed enumerator DIE ignored"));
17806 else if (building_psymtab)
17807 add_psymbol_to_list (pdi.name, false,
17808 VAR_DOMAIN, LOC_CONST, -1,
17809 cu->language == language_cplus
17810 ? psymbol_placement::GLOBAL
17811 : psymbol_placement::STATIC,
17812 0, cu->language, objfile);
17813
17814 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17815 continue;
17816 }
17817
17818 struct partial_die_info *part_die
17819 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17820
17821 /* We'll save this DIE so link it in. */
17822 part_die->die_parent = parent_die;
17823 part_die->die_sibling = NULL;
17824 part_die->die_child = NULL;
17825
17826 if (last_die && last_die == parent_die)
17827 last_die->die_child = part_die;
17828 else if (last_die)
17829 last_die->die_sibling = part_die;
17830
17831 last_die = part_die;
17832
17833 if (first_die == NULL)
17834 first_die = part_die;
17835
17836 /* Maybe add the DIE to the hash table. Not all DIEs that we
17837 find interesting need to be in the hash table, because we
17838 also have the parent/sibling/child chains; only those that we
17839 might refer to by offset later during partial symbol reading.
17840
17841 For now this means things that might have be the target of a
17842 DW_AT_specification, DW_AT_abstract_origin, or
17843 DW_AT_extension. DW_AT_extension will refer only to
17844 namespaces; DW_AT_abstract_origin refers to functions (and
17845 many things under the function DIE, but we do not recurse
17846 into function DIEs during partial symbol reading) and
17847 possibly variables as well; DW_AT_specification refers to
17848 declarations. Declarations ought to have the DW_AT_declaration
17849 flag. It happens that GCC forgets to put it in sometimes, but
17850 only for functions, not for types.
17851
17852 Adding more things than necessary to the hash table is harmless
17853 except for the performance cost. Adding too few will result in
17854 wasted time in find_partial_die, when we reread the compilation
17855 unit with load_all_dies set. */
17856
17857 if (load_all
17858 || abbrev->tag == DW_TAG_constant
17859 || abbrev->tag == DW_TAG_subprogram
17860 || abbrev->tag == DW_TAG_variable
17861 || abbrev->tag == DW_TAG_namespace
17862 || part_die->is_declaration)
17863 {
17864 void **slot;
17865
17866 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17867 to_underlying (part_die->sect_off),
17868 INSERT);
17869 *slot = part_die;
17870 }
17871
17872 /* For some DIEs we want to follow their children (if any). For C
17873 we have no reason to follow the children of structures; for other
17874 languages we have to, so that we can get at method physnames
17875 to infer fully qualified class names, for DW_AT_specification,
17876 and for C++ template arguments. For C++, we also look one level
17877 inside functions to find template arguments (if the name of the
17878 function does not already contain the template arguments).
17879
17880 For Ada and Fortran, we need to scan the children of subprograms
17881 and lexical blocks as well because these languages allow the
17882 definition of nested entities that could be interesting for the
17883 debugger, such as nested subprograms for instance. */
17884 if (last_die->has_children
17885 && (load_all
17886 || last_die->tag == DW_TAG_namespace
17887 || last_die->tag == DW_TAG_module
17888 || last_die->tag == DW_TAG_enumeration_type
17889 || (cu->language == language_cplus
17890 && last_die->tag == DW_TAG_subprogram
17891 && (last_die->name == NULL
17892 || strchr (last_die->name, '<') == NULL))
17893 || (cu->language != language_c
17894 && (last_die->tag == DW_TAG_class_type
17895 || last_die->tag == DW_TAG_interface_type
17896 || last_die->tag == DW_TAG_structure_type
17897 || last_die->tag == DW_TAG_union_type))
17898 || ((cu->language == language_ada
17899 || cu->language == language_fortran)
17900 && (last_die->tag == DW_TAG_subprogram
17901 || last_die->tag == DW_TAG_lexical_block))))
17902 {
17903 nesting_level++;
17904 parent_die = last_die;
17905 continue;
17906 }
17907
17908 /* Otherwise we skip to the next sibling, if any. */
17909 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17910
17911 /* Back to the top, do it again. */
17912 }
17913 }
17914
17915 partial_die_info::partial_die_info (sect_offset sect_off_,
17916 struct abbrev_info *abbrev)
17917 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17918 {
17919 }
17920
17921 /* Read a minimal amount of information into the minimal die structure.
17922 INFO_PTR should point just after the initial uleb128 of a DIE. */
17923
17924 const gdb_byte *
17925 partial_die_info::read (const struct die_reader_specs *reader,
17926 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17927 {
17928 struct dwarf2_cu *cu = reader->cu;
17929 struct dwarf2_per_objfile *dwarf2_per_objfile
17930 = cu->per_cu->dwarf2_per_objfile;
17931 unsigned int i;
17932 int has_low_pc_attr = 0;
17933 int has_high_pc_attr = 0;
17934 int high_pc_relative = 0;
17935
17936 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17937 for (i = 0; i < abbrev.num_attrs; ++i)
17938 {
17939 bool need_reprocess;
17940 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17941 info_ptr, &need_reprocess);
17942 /* String and address offsets that need to do the reprocessing have
17943 already been read at this point, so there is no need to wait until
17944 the loop terminates to do the reprocessing. */
17945 if (need_reprocess)
17946 read_attribute_reprocess (reader, &attr_vec[i]);
17947 attribute &attr = attr_vec[i];
17948 /* Store the data if it is of an attribute we want to keep in a
17949 partial symbol table. */
17950 switch (attr.name)
17951 {
17952 case DW_AT_name:
17953 switch (tag)
17954 {
17955 case DW_TAG_compile_unit:
17956 case DW_TAG_partial_unit:
17957 case DW_TAG_type_unit:
17958 /* Compilation units have a DW_AT_name that is a filename, not
17959 a source language identifier. */
17960 case DW_TAG_enumeration_type:
17961 case DW_TAG_enumerator:
17962 /* These tags always have simple identifiers already; no need
17963 to canonicalize them. */
17964 name = DW_STRING (&attr);
17965 break;
17966 default:
17967 {
17968 struct objfile *objfile = dwarf2_per_objfile->objfile;
17969
17970 name
17971 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17972 &objfile->per_bfd->storage_obstack);
17973 }
17974 break;
17975 }
17976 break;
17977 case DW_AT_linkage_name:
17978 case DW_AT_MIPS_linkage_name:
17979 /* Note that both forms of linkage name might appear. We
17980 assume they will be the same, and we only store the last
17981 one we see. */
17982 linkage_name = DW_STRING (&attr);
17983 break;
17984 case DW_AT_low_pc:
17985 has_low_pc_attr = 1;
17986 lowpc = attr.value_as_address ();
17987 break;
17988 case DW_AT_high_pc:
17989 has_high_pc_attr = 1;
17990 highpc = attr.value_as_address ();
17991 if (cu->header.version >= 4 && attr.form_is_constant ())
17992 high_pc_relative = 1;
17993 break;
17994 case DW_AT_location:
17995 /* Support the .debug_loc offsets. */
17996 if (attr.form_is_block ())
17997 {
17998 d.locdesc = DW_BLOCK (&attr);
17999 }
18000 else if (attr.form_is_section_offset ())
18001 {
18002 dwarf2_complex_location_expr_complaint ();
18003 }
18004 else
18005 {
18006 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18007 "partial symbol information");
18008 }
18009 break;
18010 case DW_AT_external:
18011 is_external = DW_UNSND (&attr);
18012 break;
18013 case DW_AT_declaration:
18014 is_declaration = DW_UNSND (&attr);
18015 break;
18016 case DW_AT_type:
18017 has_type = 1;
18018 break;
18019 case DW_AT_abstract_origin:
18020 case DW_AT_specification:
18021 case DW_AT_extension:
18022 has_specification = 1;
18023 spec_offset = dwarf2_get_ref_die_offset (&attr);
18024 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18025 || cu->per_cu->is_dwz);
18026 break;
18027 case DW_AT_sibling:
18028 /* Ignore absolute siblings, they might point outside of
18029 the current compile unit. */
18030 if (attr.form == DW_FORM_ref_addr)
18031 complaint (_("ignoring absolute DW_AT_sibling"));
18032 else
18033 {
18034 const gdb_byte *buffer = reader->buffer;
18035 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18036 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18037
18038 if (sibling_ptr < info_ptr)
18039 complaint (_("DW_AT_sibling points backwards"));
18040 else if (sibling_ptr > reader->buffer_end)
18041 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18042 else
18043 sibling = sibling_ptr;
18044 }
18045 break;
18046 case DW_AT_byte_size:
18047 has_byte_size = 1;
18048 break;
18049 case DW_AT_const_value:
18050 has_const_value = 1;
18051 break;
18052 case DW_AT_calling_convention:
18053 /* DWARF doesn't provide a way to identify a program's source-level
18054 entry point. DW_AT_calling_convention attributes are only meant
18055 to describe functions' calling conventions.
18056
18057 However, because it's a necessary piece of information in
18058 Fortran, and before DWARF 4 DW_CC_program was the only
18059 piece of debugging information whose definition refers to
18060 a 'main program' at all, several compilers marked Fortran
18061 main programs with DW_CC_program --- even when those
18062 functions use the standard calling conventions.
18063
18064 Although DWARF now specifies a way to provide this
18065 information, we support this practice for backward
18066 compatibility. */
18067 if (DW_UNSND (&attr) == DW_CC_program
18068 && cu->language == language_fortran)
18069 main_subprogram = 1;
18070 break;
18071 case DW_AT_inline:
18072 if (DW_UNSND (&attr) == DW_INL_inlined
18073 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18074 may_be_inlined = 1;
18075 break;
18076
18077 case DW_AT_import:
18078 if (tag == DW_TAG_imported_unit)
18079 {
18080 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18081 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18082 || cu->per_cu->is_dwz);
18083 }
18084 break;
18085
18086 case DW_AT_main_subprogram:
18087 main_subprogram = DW_UNSND (&attr);
18088 break;
18089
18090 case DW_AT_ranges:
18091 {
18092 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18093 but that requires a full DIE, so instead we just
18094 reimplement it. */
18095 int need_ranges_base = tag != DW_TAG_compile_unit;
18096 unsigned int ranges_offset = (DW_UNSND (&attr)
18097 + (need_ranges_base
18098 ? cu->ranges_base
18099 : 0));
18100
18101 /* Value of the DW_AT_ranges attribute is the offset in the
18102 .debug_ranges section. */
18103 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18104 nullptr))
18105 has_pc_info = 1;
18106 }
18107 break;
18108
18109 default:
18110 break;
18111 }
18112 }
18113
18114 /* For Ada, if both the name and the linkage name appear, we prefer
18115 the latter. This lets "catch exception" work better, regardless
18116 of the order in which the name and linkage name were emitted.
18117 Really, though, this is just a workaround for the fact that gdb
18118 doesn't store both the name and the linkage name. */
18119 if (cu->language == language_ada && linkage_name != nullptr)
18120 name = linkage_name;
18121
18122 if (high_pc_relative)
18123 highpc += lowpc;
18124
18125 if (has_low_pc_attr && has_high_pc_attr)
18126 {
18127 /* When using the GNU linker, .gnu.linkonce. sections are used to
18128 eliminate duplicate copies of functions and vtables and such.
18129 The linker will arbitrarily choose one and discard the others.
18130 The AT_*_pc values for such functions refer to local labels in
18131 these sections. If the section from that file was discarded, the
18132 labels are not in the output, so the relocs get a value of 0.
18133 If this is a discarded function, mark the pc bounds as invalid,
18134 so that GDB will ignore it. */
18135 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18136 {
18137 struct objfile *objfile = dwarf2_per_objfile->objfile;
18138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18139
18140 complaint (_("DW_AT_low_pc %s is zero "
18141 "for DIE at %s [in module %s]"),
18142 paddress (gdbarch, lowpc),
18143 sect_offset_str (sect_off),
18144 objfile_name (objfile));
18145 }
18146 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18147 else if (lowpc >= highpc)
18148 {
18149 struct objfile *objfile = dwarf2_per_objfile->objfile;
18150 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18151
18152 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18153 "for DIE at %s [in module %s]"),
18154 paddress (gdbarch, lowpc),
18155 paddress (gdbarch, highpc),
18156 sect_offset_str (sect_off),
18157 objfile_name (objfile));
18158 }
18159 else
18160 has_pc_info = 1;
18161 }
18162
18163 return info_ptr;
18164 }
18165
18166 /* Find a cached partial DIE at OFFSET in CU. */
18167
18168 struct partial_die_info *
18169 dwarf2_cu::find_partial_die (sect_offset sect_off)
18170 {
18171 struct partial_die_info *lookup_die = NULL;
18172 struct partial_die_info part_die (sect_off);
18173
18174 lookup_die = ((struct partial_die_info *)
18175 htab_find_with_hash (partial_dies, &part_die,
18176 to_underlying (sect_off)));
18177
18178 return lookup_die;
18179 }
18180
18181 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18182 except in the case of .debug_types DIEs which do not reference
18183 outside their CU (they do however referencing other types via
18184 DW_FORM_ref_sig8). */
18185
18186 static const struct cu_partial_die_info
18187 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18188 {
18189 struct dwarf2_per_objfile *dwarf2_per_objfile
18190 = cu->per_cu->dwarf2_per_objfile;
18191 struct objfile *objfile = dwarf2_per_objfile->objfile;
18192 struct dwarf2_per_cu_data *per_cu = NULL;
18193 struct partial_die_info *pd = NULL;
18194
18195 if (offset_in_dwz == cu->per_cu->is_dwz
18196 && cu->header.offset_in_cu_p (sect_off))
18197 {
18198 pd = cu->find_partial_die (sect_off);
18199 if (pd != NULL)
18200 return { cu, pd };
18201 /* We missed recording what we needed.
18202 Load all dies and try again. */
18203 per_cu = cu->per_cu;
18204 }
18205 else
18206 {
18207 /* TUs don't reference other CUs/TUs (except via type signatures). */
18208 if (cu->per_cu->is_debug_types)
18209 {
18210 error (_("Dwarf Error: Type Unit at offset %s contains"
18211 " external reference to offset %s [in module %s].\n"),
18212 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18213 bfd_get_filename (objfile->obfd));
18214 }
18215 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18216 dwarf2_per_objfile);
18217
18218 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18219 load_partial_comp_unit (per_cu);
18220
18221 per_cu->cu->last_used = 0;
18222 pd = per_cu->cu->find_partial_die (sect_off);
18223 }
18224
18225 /* If we didn't find it, and not all dies have been loaded,
18226 load them all and try again. */
18227
18228 if (pd == NULL && per_cu->load_all_dies == 0)
18229 {
18230 per_cu->load_all_dies = 1;
18231
18232 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18233 THIS_CU->cu may already be in use. So we can't just free it and
18234 replace its DIEs with the ones we read in. Instead, we leave those
18235 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18236 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18237 set. */
18238 load_partial_comp_unit (per_cu);
18239
18240 pd = per_cu->cu->find_partial_die (sect_off);
18241 }
18242
18243 if (pd == NULL)
18244 internal_error (__FILE__, __LINE__,
18245 _("could not find partial DIE %s "
18246 "in cache [from module %s]\n"),
18247 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18248 return { per_cu->cu, pd };
18249 }
18250
18251 /* See if we can figure out if the class lives in a namespace. We do
18252 this by looking for a member function; its demangled name will
18253 contain namespace info, if there is any. */
18254
18255 static void
18256 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18257 struct dwarf2_cu *cu)
18258 {
18259 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18260 what template types look like, because the demangler
18261 frequently doesn't give the same name as the debug info. We
18262 could fix this by only using the demangled name to get the
18263 prefix (but see comment in read_structure_type). */
18264
18265 struct partial_die_info *real_pdi;
18266 struct partial_die_info *child_pdi;
18267
18268 /* If this DIE (this DIE's specification, if any) has a parent, then
18269 we should not do this. We'll prepend the parent's fully qualified
18270 name when we create the partial symbol. */
18271
18272 real_pdi = struct_pdi;
18273 while (real_pdi->has_specification)
18274 {
18275 auto res = find_partial_die (real_pdi->spec_offset,
18276 real_pdi->spec_is_dwz, cu);
18277 real_pdi = res.pdi;
18278 cu = res.cu;
18279 }
18280
18281 if (real_pdi->die_parent != NULL)
18282 return;
18283
18284 for (child_pdi = struct_pdi->die_child;
18285 child_pdi != NULL;
18286 child_pdi = child_pdi->die_sibling)
18287 {
18288 if (child_pdi->tag == DW_TAG_subprogram
18289 && child_pdi->linkage_name != NULL)
18290 {
18291 gdb::unique_xmalloc_ptr<char> actual_class_name
18292 (language_class_name_from_physname (cu->language_defn,
18293 child_pdi->linkage_name));
18294 if (actual_class_name != NULL)
18295 {
18296 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18297 struct_pdi->name
18298 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18299 actual_class_name.get ());
18300 }
18301 break;
18302 }
18303 }
18304 }
18305
18306 void
18307 partial_die_info::fixup (struct dwarf2_cu *cu)
18308 {
18309 /* Once we've fixed up a die, there's no point in doing so again.
18310 This also avoids a memory leak if we were to call
18311 guess_partial_die_structure_name multiple times. */
18312 if (fixup_called)
18313 return;
18314
18315 /* If we found a reference attribute and the DIE has no name, try
18316 to find a name in the referred to DIE. */
18317
18318 if (name == NULL && has_specification)
18319 {
18320 struct partial_die_info *spec_die;
18321
18322 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18323 spec_die = res.pdi;
18324 cu = res.cu;
18325
18326 spec_die->fixup (cu);
18327
18328 if (spec_die->name)
18329 {
18330 name = spec_die->name;
18331
18332 /* Copy DW_AT_external attribute if it is set. */
18333 if (spec_die->is_external)
18334 is_external = spec_die->is_external;
18335 }
18336 }
18337
18338 /* Set default names for some unnamed DIEs. */
18339
18340 if (name == NULL && tag == DW_TAG_namespace)
18341 name = CP_ANONYMOUS_NAMESPACE_STR;
18342
18343 /* If there is no parent die to provide a namespace, and there are
18344 children, see if we can determine the namespace from their linkage
18345 name. */
18346 if (cu->language == language_cplus
18347 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18348 && die_parent == NULL
18349 && has_children
18350 && (tag == DW_TAG_class_type
18351 || tag == DW_TAG_structure_type
18352 || tag == DW_TAG_union_type))
18353 guess_partial_die_structure_name (this, cu);
18354
18355 /* GCC might emit a nameless struct or union that has a linkage
18356 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18357 if (name == NULL
18358 && (tag == DW_TAG_class_type
18359 || tag == DW_TAG_interface_type
18360 || tag == DW_TAG_structure_type
18361 || tag == DW_TAG_union_type)
18362 && linkage_name != NULL)
18363 {
18364 gdb::unique_xmalloc_ptr<char> demangled
18365 (gdb_demangle (linkage_name, DMGL_TYPES));
18366 if (demangled != nullptr)
18367 {
18368 const char *base;
18369
18370 /* Strip any leading namespaces/classes, keep only the base name.
18371 DW_AT_name for named DIEs does not contain the prefixes. */
18372 base = strrchr (demangled.get (), ':');
18373 if (base && base > demangled.get () && base[-1] == ':')
18374 base++;
18375 else
18376 base = demangled.get ();
18377
18378 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18379 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18380 }
18381 }
18382
18383 fixup_called = 1;
18384 }
18385
18386 /* Process the attributes that had to be skipped in the first round. These
18387 attributes are the ones that need str_offsets_base or addr_base attributes.
18388 They could not have been processed in the first round, because at the time
18389 the values of str_offsets_base or addr_base may not have been known. */
18390 void read_attribute_reprocess (const struct die_reader_specs *reader,
18391 struct attribute *attr)
18392 {
18393 struct dwarf2_cu *cu = reader->cu;
18394 switch (attr->form)
18395 {
18396 case DW_FORM_addrx:
18397 case DW_FORM_GNU_addr_index:
18398 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18399 break;
18400 case DW_FORM_strx:
18401 case DW_FORM_strx1:
18402 case DW_FORM_strx2:
18403 case DW_FORM_strx3:
18404 case DW_FORM_strx4:
18405 case DW_FORM_GNU_str_index:
18406 {
18407 unsigned int str_index = DW_UNSND (attr);
18408 if (reader->dwo_file != NULL)
18409 {
18410 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18411 DW_STRING_IS_CANONICAL (attr) = 0;
18412 }
18413 else
18414 {
18415 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18416 DW_STRING_IS_CANONICAL (attr) = 0;
18417 }
18418 break;
18419 }
18420 default:
18421 gdb_assert_not_reached (_("Unexpected DWARF form."));
18422 }
18423 }
18424
18425 /* Read an attribute value described by an attribute form. */
18426
18427 static const gdb_byte *
18428 read_attribute_value (const struct die_reader_specs *reader,
18429 struct attribute *attr, unsigned form,
18430 LONGEST implicit_const, const gdb_byte *info_ptr,
18431 bool *need_reprocess)
18432 {
18433 struct dwarf2_cu *cu = reader->cu;
18434 struct dwarf2_per_objfile *dwarf2_per_objfile
18435 = cu->per_cu->dwarf2_per_objfile;
18436 struct objfile *objfile = dwarf2_per_objfile->objfile;
18437 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18438 bfd *abfd = reader->abfd;
18439 struct comp_unit_head *cu_header = &cu->header;
18440 unsigned int bytes_read;
18441 struct dwarf_block *blk;
18442 *need_reprocess = false;
18443
18444 attr->form = (enum dwarf_form) form;
18445 switch (form)
18446 {
18447 case DW_FORM_ref_addr:
18448 if (cu->header.version == 2)
18449 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18450 else
18451 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18452 &bytes_read);
18453 info_ptr += bytes_read;
18454 break;
18455 case DW_FORM_GNU_ref_alt:
18456 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18457 info_ptr += bytes_read;
18458 break;
18459 case DW_FORM_addr:
18460 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18461 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18462 info_ptr += bytes_read;
18463 break;
18464 case DW_FORM_block2:
18465 blk = dwarf_alloc_block (cu);
18466 blk->size = read_2_bytes (abfd, info_ptr);
18467 info_ptr += 2;
18468 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18469 info_ptr += blk->size;
18470 DW_BLOCK (attr) = blk;
18471 break;
18472 case DW_FORM_block4:
18473 blk = dwarf_alloc_block (cu);
18474 blk->size = read_4_bytes (abfd, info_ptr);
18475 info_ptr += 4;
18476 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18477 info_ptr += blk->size;
18478 DW_BLOCK (attr) = blk;
18479 break;
18480 case DW_FORM_data2:
18481 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18482 info_ptr += 2;
18483 break;
18484 case DW_FORM_data4:
18485 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18486 info_ptr += 4;
18487 break;
18488 case DW_FORM_data8:
18489 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18490 info_ptr += 8;
18491 break;
18492 case DW_FORM_data16:
18493 blk = dwarf_alloc_block (cu);
18494 blk->size = 16;
18495 blk->data = read_n_bytes (abfd, info_ptr, 16);
18496 info_ptr += 16;
18497 DW_BLOCK (attr) = blk;
18498 break;
18499 case DW_FORM_sec_offset:
18500 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18501 info_ptr += bytes_read;
18502 break;
18503 case DW_FORM_string:
18504 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18505 DW_STRING_IS_CANONICAL (attr) = 0;
18506 info_ptr += bytes_read;
18507 break;
18508 case DW_FORM_strp:
18509 if (!cu->per_cu->is_dwz)
18510 {
18511 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18512 abfd, info_ptr, cu_header,
18513 &bytes_read);
18514 DW_STRING_IS_CANONICAL (attr) = 0;
18515 info_ptr += bytes_read;
18516 break;
18517 }
18518 /* FALLTHROUGH */
18519 case DW_FORM_line_strp:
18520 if (!cu->per_cu->is_dwz)
18521 {
18522 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18523 abfd, info_ptr,
18524 cu_header, &bytes_read);
18525 DW_STRING_IS_CANONICAL (attr) = 0;
18526 info_ptr += bytes_read;
18527 break;
18528 }
18529 /* FALLTHROUGH */
18530 case DW_FORM_GNU_strp_alt:
18531 {
18532 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18533 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18534 &bytes_read);
18535
18536 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18537 dwz, str_offset);
18538 DW_STRING_IS_CANONICAL (attr) = 0;
18539 info_ptr += bytes_read;
18540 }
18541 break;
18542 case DW_FORM_exprloc:
18543 case DW_FORM_block:
18544 blk = dwarf_alloc_block (cu);
18545 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18546 info_ptr += bytes_read;
18547 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18548 info_ptr += blk->size;
18549 DW_BLOCK (attr) = blk;
18550 break;
18551 case DW_FORM_block1:
18552 blk = dwarf_alloc_block (cu);
18553 blk->size = read_1_byte (abfd, info_ptr);
18554 info_ptr += 1;
18555 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18556 info_ptr += blk->size;
18557 DW_BLOCK (attr) = blk;
18558 break;
18559 case DW_FORM_data1:
18560 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18561 info_ptr += 1;
18562 break;
18563 case DW_FORM_flag:
18564 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18565 info_ptr += 1;
18566 break;
18567 case DW_FORM_flag_present:
18568 DW_UNSND (attr) = 1;
18569 break;
18570 case DW_FORM_sdata:
18571 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18572 info_ptr += bytes_read;
18573 break;
18574 case DW_FORM_udata:
18575 case DW_FORM_rnglistx:
18576 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18577 info_ptr += bytes_read;
18578 break;
18579 case DW_FORM_ref1:
18580 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18581 + read_1_byte (abfd, info_ptr));
18582 info_ptr += 1;
18583 break;
18584 case DW_FORM_ref2:
18585 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18586 + read_2_bytes (abfd, info_ptr));
18587 info_ptr += 2;
18588 break;
18589 case DW_FORM_ref4:
18590 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18591 + read_4_bytes (abfd, info_ptr));
18592 info_ptr += 4;
18593 break;
18594 case DW_FORM_ref8:
18595 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18596 + read_8_bytes (abfd, info_ptr));
18597 info_ptr += 8;
18598 break;
18599 case DW_FORM_ref_sig8:
18600 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18601 info_ptr += 8;
18602 break;
18603 case DW_FORM_ref_udata:
18604 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18605 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18606 info_ptr += bytes_read;
18607 break;
18608 case DW_FORM_indirect:
18609 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18610 info_ptr += bytes_read;
18611 if (form == DW_FORM_implicit_const)
18612 {
18613 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18614 info_ptr += bytes_read;
18615 }
18616 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18617 info_ptr, need_reprocess);
18618 break;
18619 case DW_FORM_implicit_const:
18620 DW_SND (attr) = implicit_const;
18621 break;
18622 case DW_FORM_addrx:
18623 case DW_FORM_GNU_addr_index:
18624 *need_reprocess = true;
18625 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18626 info_ptr += bytes_read;
18627 break;
18628 case DW_FORM_strx:
18629 case DW_FORM_strx1:
18630 case DW_FORM_strx2:
18631 case DW_FORM_strx3:
18632 case DW_FORM_strx4:
18633 case DW_FORM_GNU_str_index:
18634 {
18635 ULONGEST str_index;
18636 if (form == DW_FORM_strx1)
18637 {
18638 str_index = read_1_byte (abfd, info_ptr);
18639 info_ptr += 1;
18640 }
18641 else if (form == DW_FORM_strx2)
18642 {
18643 str_index = read_2_bytes (abfd, info_ptr);
18644 info_ptr += 2;
18645 }
18646 else if (form == DW_FORM_strx3)
18647 {
18648 str_index = read_3_bytes (abfd, info_ptr);
18649 info_ptr += 3;
18650 }
18651 else if (form == DW_FORM_strx4)
18652 {
18653 str_index = read_4_bytes (abfd, info_ptr);
18654 info_ptr += 4;
18655 }
18656 else
18657 {
18658 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18659 info_ptr += bytes_read;
18660 }
18661 *need_reprocess = true;
18662 DW_UNSND (attr) = str_index;
18663 }
18664 break;
18665 default:
18666 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18667 dwarf_form_name (form),
18668 bfd_get_filename (abfd));
18669 }
18670
18671 /* Super hack. */
18672 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18673 attr->form = DW_FORM_GNU_ref_alt;
18674
18675 /* We have seen instances where the compiler tried to emit a byte
18676 size attribute of -1 which ended up being encoded as an unsigned
18677 0xffffffff. Although 0xffffffff is technically a valid size value,
18678 an object of this size seems pretty unlikely so we can relatively
18679 safely treat these cases as if the size attribute was invalid and
18680 treat them as zero by default. */
18681 if (attr->name == DW_AT_byte_size
18682 && form == DW_FORM_data4
18683 && DW_UNSND (attr) >= 0xffffffff)
18684 {
18685 complaint
18686 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18687 hex_string (DW_UNSND (attr)));
18688 DW_UNSND (attr) = 0;
18689 }
18690
18691 return info_ptr;
18692 }
18693
18694 /* Read an attribute described by an abbreviated attribute. */
18695
18696 static const gdb_byte *
18697 read_attribute (const struct die_reader_specs *reader,
18698 struct attribute *attr, struct attr_abbrev *abbrev,
18699 const gdb_byte *info_ptr, bool *need_reprocess)
18700 {
18701 attr->name = abbrev->name;
18702 return read_attribute_value (reader, attr, abbrev->form,
18703 abbrev->implicit_const, info_ptr,
18704 need_reprocess);
18705 }
18706
18707 static CORE_ADDR
18708 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
18709 unsigned int *bytes_read)
18710 {
18711 struct comp_unit_head *cu_header = &cu->header;
18712 CORE_ADDR retval = 0;
18713
18714 if (cu_header->signed_addr_p)
18715 {
18716 switch (cu_header->addr_size)
18717 {
18718 case 2:
18719 retval = bfd_get_signed_16 (abfd, buf);
18720 break;
18721 case 4:
18722 retval = bfd_get_signed_32 (abfd, buf);
18723 break;
18724 case 8:
18725 retval = bfd_get_signed_64 (abfd, buf);
18726 break;
18727 default:
18728 internal_error (__FILE__, __LINE__,
18729 _("read_address: bad switch, signed [in module %s]"),
18730 bfd_get_filename (abfd));
18731 }
18732 }
18733 else
18734 {
18735 switch (cu_header->addr_size)
18736 {
18737 case 2:
18738 retval = bfd_get_16 (abfd, buf);
18739 break;
18740 case 4:
18741 retval = bfd_get_32 (abfd, buf);
18742 break;
18743 case 8:
18744 retval = bfd_get_64 (abfd, buf);
18745 break;
18746 default:
18747 internal_error (__FILE__, __LINE__,
18748 _("read_address: bad switch, "
18749 "unsigned [in module %s]"),
18750 bfd_get_filename (abfd));
18751 }
18752 }
18753
18754 *bytes_read = cu_header->addr_size;
18755 return retval;
18756 }
18757
18758 /* Cover function for read_initial_length.
18759 Returns the length of the object at BUF, and stores the size of the
18760 initial length in *BYTES_READ and stores the size that offsets will be in
18761 *OFFSET_SIZE.
18762 If the initial length size is not equivalent to that specified in
18763 CU_HEADER then issue a complaint.
18764 This is useful when reading non-comp-unit headers. */
18765
18766 static LONGEST
18767 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18768 const struct comp_unit_head *cu_header,
18769 unsigned int *bytes_read,
18770 unsigned int *offset_size)
18771 {
18772 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18773
18774 gdb_assert (cu_header->initial_length_size == 4
18775 || cu_header->initial_length_size == 8
18776 || cu_header->initial_length_size == 12);
18777
18778 if (cu_header->initial_length_size != *bytes_read)
18779 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18780
18781 *offset_size = (*bytes_read == 4) ? 4 : 8;
18782 return length;
18783 }
18784
18785 static const gdb_byte *
18786 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
18787 {
18788 /* If the size of a host char is 8 bits, we can return a pointer
18789 to the buffer, otherwise we have to copy the data to a buffer
18790 allocated on the temporary obstack. */
18791 gdb_assert (HOST_CHAR_BIT == 8);
18792 return buf;
18793 }
18794
18795 static const char *
18796 read_direct_string (bfd *abfd, const gdb_byte *buf,
18797 unsigned int *bytes_read_ptr)
18798 {
18799 /* If the size of a host char is 8 bits, we can return a pointer
18800 to the string, otherwise we have to copy the string to a buffer
18801 allocated on the temporary obstack. */
18802 gdb_assert (HOST_CHAR_BIT == 8);
18803 if (*buf == '\0')
18804 {
18805 *bytes_read_ptr = 1;
18806 return NULL;
18807 }
18808 *bytes_read_ptr = strlen ((const char *) buf) + 1;
18809 return (const char *) buf;
18810 }
18811
18812 /* Return pointer to string at section SECT offset STR_OFFSET with error
18813 reporting strings FORM_NAME and SECT_NAME. */
18814
18815 static const char *
18816 read_indirect_string_at_offset_from (struct objfile *objfile,
18817 bfd *abfd, LONGEST str_offset,
18818 struct dwarf2_section_info *sect,
18819 const char *form_name,
18820 const char *sect_name)
18821 {
18822 sect->read (objfile);
18823 if (sect->buffer == NULL)
18824 error (_("%s used without %s section [in module %s]"),
18825 form_name, sect_name, bfd_get_filename (abfd));
18826 if (str_offset >= sect->size)
18827 error (_("%s pointing outside of %s section [in module %s]"),
18828 form_name, sect_name, bfd_get_filename (abfd));
18829 gdb_assert (HOST_CHAR_BIT == 8);
18830 if (sect->buffer[str_offset] == '\0')
18831 return NULL;
18832 return (const char *) (sect->buffer + str_offset);
18833 }
18834
18835 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18836
18837 static const char *
18838 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18839 bfd *abfd, LONGEST str_offset)
18840 {
18841 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18842 abfd, str_offset,
18843 &dwarf2_per_objfile->str,
18844 "DW_FORM_strp", ".debug_str");
18845 }
18846
18847 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18848
18849 static const char *
18850 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18851 bfd *abfd, LONGEST str_offset)
18852 {
18853 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18854 abfd, str_offset,
18855 &dwarf2_per_objfile->line_str,
18856 "DW_FORM_line_strp",
18857 ".debug_line_str");
18858 }
18859
18860 /* Read a string at offset STR_OFFSET in the .debug_str section from
18861 the .dwz file DWZ. Throw an error if the offset is too large. If
18862 the string consists of a single NUL byte, return NULL; otherwise
18863 return a pointer to the string. */
18864
18865 static const char *
18866 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18867 LONGEST str_offset)
18868 {
18869 dwz->str.read (objfile);
18870
18871 if (dwz->str.buffer == NULL)
18872 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18873 "section [in module %s]"),
18874 bfd_get_filename (dwz->dwz_bfd.get ()));
18875 if (str_offset >= dwz->str.size)
18876 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18877 ".debug_str section [in module %s]"),
18878 bfd_get_filename (dwz->dwz_bfd.get ()));
18879 gdb_assert (HOST_CHAR_BIT == 8);
18880 if (dwz->str.buffer[str_offset] == '\0')
18881 return NULL;
18882 return (const char *) (dwz->str.buffer + str_offset);
18883 }
18884
18885 /* Return pointer to string at .debug_str offset as read from BUF.
18886 BUF is assumed to be in a compilation unit described by CU_HEADER.
18887 Return *BYTES_READ_PTR count of bytes read from BUF. */
18888
18889 static const char *
18890 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18891 const gdb_byte *buf,
18892 const struct comp_unit_head *cu_header,
18893 unsigned int *bytes_read_ptr)
18894 {
18895 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18896
18897 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18898 }
18899
18900 /* Return pointer to string at .debug_line_str offset as read from BUF.
18901 BUF is assumed to be in a compilation unit described by CU_HEADER.
18902 Return *BYTES_READ_PTR count of bytes read from BUF. */
18903
18904 static const char *
18905 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18906 bfd *abfd, const gdb_byte *buf,
18907 const struct comp_unit_head *cu_header,
18908 unsigned int *bytes_read_ptr)
18909 {
18910 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18911
18912 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18913 str_offset);
18914 }
18915
18916 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18917 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18918 ADDR_SIZE is the size of addresses from the CU header. */
18919
18920 static CORE_ADDR
18921 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18922 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18923 int addr_size)
18924 {
18925 struct objfile *objfile = dwarf2_per_objfile->objfile;
18926 bfd *abfd = objfile->obfd;
18927 const gdb_byte *info_ptr;
18928 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18929
18930 dwarf2_per_objfile->addr.read (objfile);
18931 if (dwarf2_per_objfile->addr.buffer == NULL)
18932 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18933 objfile_name (objfile));
18934 if (addr_base_or_zero + addr_index * addr_size
18935 >= dwarf2_per_objfile->addr.size)
18936 error (_("DW_FORM_addr_index pointing outside of "
18937 ".debug_addr section [in module %s]"),
18938 objfile_name (objfile));
18939 info_ptr = (dwarf2_per_objfile->addr.buffer
18940 + addr_base_or_zero + addr_index * addr_size);
18941 if (addr_size == 4)
18942 return bfd_get_32 (abfd, info_ptr);
18943 else
18944 return bfd_get_64 (abfd, info_ptr);
18945 }
18946
18947 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18948
18949 static CORE_ADDR
18950 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18951 {
18952 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18953 cu->addr_base, cu->header.addr_size);
18954 }
18955
18956 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18957
18958 static CORE_ADDR
18959 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18960 unsigned int *bytes_read)
18961 {
18962 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18963 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18964
18965 return read_addr_index (cu, addr_index);
18966 }
18967
18968 /* Given an index in .debug_addr, fetch the value.
18969 NOTE: This can be called during dwarf expression evaluation,
18970 long after the debug information has been read, and thus per_cu->cu
18971 may no longer exist. */
18972
18973 CORE_ADDR
18974 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18975 unsigned int addr_index)
18976 {
18977 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18978 struct dwarf2_cu *cu = per_cu->cu;
18979 gdb::optional<ULONGEST> addr_base;
18980 int addr_size;
18981
18982 /* We need addr_base and addr_size.
18983 If we don't have PER_CU->cu, we have to get it.
18984 Nasty, but the alternative is storing the needed info in PER_CU,
18985 which at this point doesn't seem justified: it's not clear how frequently
18986 it would get used and it would increase the size of every PER_CU.
18987 Entry points like dwarf2_per_cu_addr_size do a similar thing
18988 so we're not in uncharted territory here.
18989 Alas we need to be a bit more complicated as addr_base is contained
18990 in the DIE.
18991
18992 We don't need to read the entire CU(/TU).
18993 We just need the header and top level die.
18994
18995 IWBN to use the aging mechanism to let us lazily later discard the CU.
18996 For now we skip this optimization. */
18997
18998 if (cu != NULL)
18999 {
19000 addr_base = cu->addr_base;
19001 addr_size = cu->header.addr_size;
19002 }
19003 else
19004 {
19005 cutu_reader reader (per_cu, NULL, 0, 0, false);
19006 addr_base = reader.cu->addr_base;
19007 addr_size = reader.cu->header.addr_size;
19008 }
19009
19010 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19011 addr_size);
19012 }
19013
19014 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19015 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19016 DWO file. */
19017
19018 static const char *
19019 read_str_index (struct dwarf2_cu *cu,
19020 struct dwarf2_section_info *str_section,
19021 struct dwarf2_section_info *str_offsets_section,
19022 ULONGEST str_offsets_base, ULONGEST str_index)
19023 {
19024 struct dwarf2_per_objfile *dwarf2_per_objfile
19025 = cu->per_cu->dwarf2_per_objfile;
19026 struct objfile *objfile = dwarf2_per_objfile->objfile;
19027 const char *objf_name = objfile_name (objfile);
19028 bfd *abfd = objfile->obfd;
19029 const gdb_byte *info_ptr;
19030 ULONGEST str_offset;
19031 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19032
19033 str_section->read (objfile);
19034 str_offsets_section->read (objfile);
19035 if (str_section->buffer == NULL)
19036 error (_("%s used without %s section"
19037 " in CU at offset %s [in module %s]"),
19038 form_name, str_section->get_name (),
19039 sect_offset_str (cu->header.sect_off), objf_name);
19040 if (str_offsets_section->buffer == NULL)
19041 error (_("%s used without %s section"
19042 " in CU at offset %s [in module %s]"),
19043 form_name, str_section->get_name (),
19044 sect_offset_str (cu->header.sect_off), objf_name);
19045 info_ptr = (str_offsets_section->buffer
19046 + str_offsets_base
19047 + str_index * cu->header.offset_size);
19048 if (cu->header.offset_size == 4)
19049 str_offset = bfd_get_32 (abfd, info_ptr);
19050 else
19051 str_offset = bfd_get_64 (abfd, info_ptr);
19052 if (str_offset >= str_section->size)
19053 error (_("Offset from %s pointing outside of"
19054 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19055 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19056 return (const char *) (str_section->buffer + str_offset);
19057 }
19058
19059 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19060
19061 static const char *
19062 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19063 {
19064 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19065 ? reader->cu->header.addr_size : 0;
19066 return read_str_index (reader->cu,
19067 &reader->dwo_file->sections.str,
19068 &reader->dwo_file->sections.str_offsets,
19069 str_offsets_base, str_index);
19070 }
19071
19072 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19073
19074 static const char *
19075 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19076 {
19077 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19078 const char *objf_name = objfile_name (objfile);
19079 static const char form_name[] = "DW_FORM_GNU_str_index";
19080 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19081
19082 if (!cu->str_offsets_base.has_value ())
19083 error (_("%s used in Fission stub without %s"
19084 " in CU at offset 0x%lx [in module %s]"),
19085 form_name, str_offsets_attr_name,
19086 (long) cu->header.offset_size, objf_name);
19087
19088 return read_str_index (cu,
19089 &cu->per_cu->dwarf2_per_objfile->str,
19090 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19091 *cu->str_offsets_base, str_index);
19092 }
19093
19094 /* Return the length of an LEB128 number in BUF. */
19095
19096 static int
19097 leb128_size (const gdb_byte *buf)
19098 {
19099 const gdb_byte *begin = buf;
19100 gdb_byte byte;
19101
19102 while (1)
19103 {
19104 byte = *buf++;
19105 if ((byte & 128) == 0)
19106 return buf - begin;
19107 }
19108 }
19109
19110 static void
19111 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19112 {
19113 switch (lang)
19114 {
19115 case DW_LANG_C89:
19116 case DW_LANG_C99:
19117 case DW_LANG_C11:
19118 case DW_LANG_C:
19119 case DW_LANG_UPC:
19120 cu->language = language_c;
19121 break;
19122 case DW_LANG_Java:
19123 case DW_LANG_C_plus_plus:
19124 case DW_LANG_C_plus_plus_11:
19125 case DW_LANG_C_plus_plus_14:
19126 cu->language = language_cplus;
19127 break;
19128 case DW_LANG_D:
19129 cu->language = language_d;
19130 break;
19131 case DW_LANG_Fortran77:
19132 case DW_LANG_Fortran90:
19133 case DW_LANG_Fortran95:
19134 case DW_LANG_Fortran03:
19135 case DW_LANG_Fortran08:
19136 cu->language = language_fortran;
19137 break;
19138 case DW_LANG_Go:
19139 cu->language = language_go;
19140 break;
19141 case DW_LANG_Mips_Assembler:
19142 cu->language = language_asm;
19143 break;
19144 case DW_LANG_Ada83:
19145 case DW_LANG_Ada95:
19146 cu->language = language_ada;
19147 break;
19148 case DW_LANG_Modula2:
19149 cu->language = language_m2;
19150 break;
19151 case DW_LANG_Pascal83:
19152 cu->language = language_pascal;
19153 break;
19154 case DW_LANG_ObjC:
19155 cu->language = language_objc;
19156 break;
19157 case DW_LANG_Rust:
19158 case DW_LANG_Rust_old:
19159 cu->language = language_rust;
19160 break;
19161 case DW_LANG_Cobol74:
19162 case DW_LANG_Cobol85:
19163 default:
19164 cu->language = language_minimal;
19165 break;
19166 }
19167 cu->language_defn = language_def (cu->language);
19168 }
19169
19170 /* Return the named attribute or NULL if not there. */
19171
19172 static struct attribute *
19173 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19174 {
19175 for (;;)
19176 {
19177 unsigned int i;
19178 struct attribute *spec = NULL;
19179
19180 for (i = 0; i < die->num_attrs; ++i)
19181 {
19182 if (die->attrs[i].name == name)
19183 return &die->attrs[i];
19184 if (die->attrs[i].name == DW_AT_specification
19185 || die->attrs[i].name == DW_AT_abstract_origin)
19186 spec = &die->attrs[i];
19187 }
19188
19189 if (!spec)
19190 break;
19191
19192 die = follow_die_ref (die, spec, &cu);
19193 }
19194
19195 return NULL;
19196 }
19197
19198 /* Return the named attribute or NULL if not there,
19199 but do not follow DW_AT_specification, etc.
19200 This is for use in contexts where we're reading .debug_types dies.
19201 Following DW_AT_specification, DW_AT_abstract_origin will take us
19202 back up the chain, and we want to go down. */
19203
19204 static struct attribute *
19205 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19206 {
19207 unsigned int i;
19208
19209 for (i = 0; i < die->num_attrs; ++i)
19210 if (die->attrs[i].name == name)
19211 return &die->attrs[i];
19212
19213 return NULL;
19214 }
19215
19216 /* Return the string associated with a string-typed attribute, or NULL if it
19217 is either not found or is of an incorrect type. */
19218
19219 static const char *
19220 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19221 {
19222 struct attribute *attr;
19223 const char *str = NULL;
19224
19225 attr = dwarf2_attr (die, name, cu);
19226
19227 if (attr != NULL)
19228 {
19229 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19230 || attr->form == DW_FORM_string
19231 || attr->form == DW_FORM_strx
19232 || attr->form == DW_FORM_strx1
19233 || attr->form == DW_FORM_strx2
19234 || attr->form == DW_FORM_strx3
19235 || attr->form == DW_FORM_strx4
19236 || attr->form == DW_FORM_GNU_str_index
19237 || attr->form == DW_FORM_GNU_strp_alt)
19238 str = DW_STRING (attr);
19239 else
19240 complaint (_("string type expected for attribute %s for "
19241 "DIE at %s in module %s"),
19242 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19243 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19244 }
19245
19246 return str;
19247 }
19248
19249 /* Return the dwo name or NULL if not present. If present, it is in either
19250 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19251 static const char *
19252 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19253 {
19254 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19255 if (dwo_name == nullptr)
19256 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19257 return dwo_name;
19258 }
19259
19260 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19261 and holds a non-zero value. This function should only be used for
19262 DW_FORM_flag or DW_FORM_flag_present attributes. */
19263
19264 static int
19265 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19266 {
19267 struct attribute *attr = dwarf2_attr (die, name, cu);
19268
19269 return (attr && DW_UNSND (attr));
19270 }
19271
19272 static int
19273 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19274 {
19275 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19276 which value is non-zero. However, we have to be careful with
19277 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19278 (via dwarf2_flag_true_p) follows this attribute. So we may
19279 end up accidently finding a declaration attribute that belongs
19280 to a different DIE referenced by the specification attribute,
19281 even though the given DIE does not have a declaration attribute. */
19282 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19283 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19284 }
19285
19286 /* Return the die giving the specification for DIE, if there is
19287 one. *SPEC_CU is the CU containing DIE on input, and the CU
19288 containing the return value on output. If there is no
19289 specification, but there is an abstract origin, that is
19290 returned. */
19291
19292 static struct die_info *
19293 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19294 {
19295 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19296 *spec_cu);
19297
19298 if (spec_attr == NULL)
19299 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19300
19301 if (spec_attr == NULL)
19302 return NULL;
19303 else
19304 return follow_die_ref (die, spec_attr, spec_cu);
19305 }
19306
19307 /* Stub for free_line_header to match void * callback types. */
19308
19309 static void
19310 free_line_header_voidp (void *arg)
19311 {
19312 struct line_header *lh = (struct line_header *) arg;
19313
19314 delete lh;
19315 }
19316
19317 /* A convenience function to find the proper .debug_line section for a CU. */
19318
19319 static struct dwarf2_section_info *
19320 get_debug_line_section (struct dwarf2_cu *cu)
19321 {
19322 struct dwarf2_section_info *section;
19323 struct dwarf2_per_objfile *dwarf2_per_objfile
19324 = cu->per_cu->dwarf2_per_objfile;
19325
19326 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19327 DWO file. */
19328 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19329 section = &cu->dwo_unit->dwo_file->sections.line;
19330 else if (cu->per_cu->is_dwz)
19331 {
19332 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19333
19334 section = &dwz->line;
19335 }
19336 else
19337 section = &dwarf2_per_objfile->line;
19338
19339 return section;
19340 }
19341
19342 /* Read directory or file name entry format, starting with byte of
19343 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19344 entries count and the entries themselves in the described entry
19345 format. */
19346
19347 static void
19348 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19349 bfd *abfd, const gdb_byte **bufp,
19350 struct line_header *lh,
19351 const struct comp_unit_head *cu_header,
19352 void (*callback) (struct line_header *lh,
19353 const char *name,
19354 dir_index d_index,
19355 unsigned int mod_time,
19356 unsigned int length))
19357 {
19358 gdb_byte format_count, formati;
19359 ULONGEST data_count, datai;
19360 const gdb_byte *buf = *bufp;
19361 const gdb_byte *format_header_data;
19362 unsigned int bytes_read;
19363
19364 format_count = read_1_byte (abfd, buf);
19365 buf += 1;
19366 format_header_data = buf;
19367 for (formati = 0; formati < format_count; formati++)
19368 {
19369 read_unsigned_leb128 (abfd, buf, &bytes_read);
19370 buf += bytes_read;
19371 read_unsigned_leb128 (abfd, buf, &bytes_read);
19372 buf += bytes_read;
19373 }
19374
19375 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19376 buf += bytes_read;
19377 for (datai = 0; datai < data_count; datai++)
19378 {
19379 const gdb_byte *format = format_header_data;
19380 struct file_entry fe;
19381
19382 for (formati = 0; formati < format_count; formati++)
19383 {
19384 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19385 format += bytes_read;
19386
19387 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19388 format += bytes_read;
19389
19390 gdb::optional<const char *> string;
19391 gdb::optional<unsigned int> uint;
19392
19393 switch (form)
19394 {
19395 case DW_FORM_string:
19396 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19397 buf += bytes_read;
19398 break;
19399
19400 case DW_FORM_line_strp:
19401 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19402 abfd, buf,
19403 cu_header,
19404 &bytes_read));
19405 buf += bytes_read;
19406 break;
19407
19408 case DW_FORM_data1:
19409 uint.emplace (read_1_byte (abfd, buf));
19410 buf += 1;
19411 break;
19412
19413 case DW_FORM_data2:
19414 uint.emplace (read_2_bytes (abfd, buf));
19415 buf += 2;
19416 break;
19417
19418 case DW_FORM_data4:
19419 uint.emplace (read_4_bytes (abfd, buf));
19420 buf += 4;
19421 break;
19422
19423 case DW_FORM_data8:
19424 uint.emplace (read_8_bytes (abfd, buf));
19425 buf += 8;
19426 break;
19427
19428 case DW_FORM_data16:
19429 /* This is used for MD5, but file_entry does not record MD5s. */
19430 buf += 16;
19431 break;
19432
19433 case DW_FORM_udata:
19434 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19435 buf += bytes_read;
19436 break;
19437
19438 case DW_FORM_block:
19439 /* It is valid only for DW_LNCT_timestamp which is ignored by
19440 current GDB. */
19441 break;
19442 }
19443
19444 switch (content_type)
19445 {
19446 case DW_LNCT_path:
19447 if (string.has_value ())
19448 fe.name = *string;
19449 break;
19450 case DW_LNCT_directory_index:
19451 if (uint.has_value ())
19452 fe.d_index = (dir_index) *uint;
19453 break;
19454 case DW_LNCT_timestamp:
19455 if (uint.has_value ())
19456 fe.mod_time = *uint;
19457 break;
19458 case DW_LNCT_size:
19459 if (uint.has_value ())
19460 fe.length = *uint;
19461 break;
19462 case DW_LNCT_MD5:
19463 break;
19464 default:
19465 complaint (_("Unknown format content type %s"),
19466 pulongest (content_type));
19467 }
19468 }
19469
19470 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19471 }
19472
19473 *bufp = buf;
19474 }
19475
19476 /* Read the statement program header starting at OFFSET in
19477 .debug_line, or .debug_line.dwo. Return a pointer
19478 to a struct line_header, allocated using xmalloc.
19479 Returns NULL if there is a problem reading the header, e.g., if it
19480 has a version we don't understand.
19481
19482 NOTE: the strings in the include directory and file name tables of
19483 the returned object point into the dwarf line section buffer,
19484 and must not be freed. */
19485
19486 static line_header_up
19487 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19488 {
19489 const gdb_byte *line_ptr;
19490 unsigned int bytes_read, offset_size;
19491 int i;
19492 const char *cur_dir, *cur_file;
19493 struct dwarf2_section_info *section;
19494 bfd *abfd;
19495 struct dwarf2_per_objfile *dwarf2_per_objfile
19496 = cu->per_cu->dwarf2_per_objfile;
19497
19498 section = get_debug_line_section (cu);
19499 section->read (dwarf2_per_objfile->objfile);
19500 if (section->buffer == NULL)
19501 {
19502 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19503 complaint (_("missing .debug_line.dwo section"));
19504 else
19505 complaint (_("missing .debug_line section"));
19506 return 0;
19507 }
19508
19509 /* We can't do this until we know the section is non-empty.
19510 Only then do we know we have such a section. */
19511 abfd = section->get_bfd_owner ();
19512
19513 /* Make sure that at least there's room for the total_length field.
19514 That could be 12 bytes long, but we're just going to fudge that. */
19515 if (to_underlying (sect_off) + 4 >= section->size)
19516 {
19517 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19518 return 0;
19519 }
19520
19521 line_header_up lh (new line_header ());
19522
19523 lh->sect_off = sect_off;
19524 lh->offset_in_dwz = cu->per_cu->is_dwz;
19525
19526 line_ptr = section->buffer + to_underlying (sect_off);
19527
19528 /* Read in the header. */
19529 lh->total_length =
19530 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19531 &bytes_read, &offset_size);
19532 line_ptr += bytes_read;
19533
19534 const gdb_byte *start_here = line_ptr;
19535
19536 if (line_ptr + lh->total_length > (section->buffer + section->size))
19537 {
19538 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19539 return 0;
19540 }
19541 lh->statement_program_end = start_here + lh->total_length;
19542 lh->version = read_2_bytes (abfd, line_ptr);
19543 line_ptr += 2;
19544 if (lh->version > 5)
19545 {
19546 /* This is a version we don't understand. The format could have
19547 changed in ways we don't handle properly so just punt. */
19548 complaint (_("unsupported version in .debug_line section"));
19549 return NULL;
19550 }
19551 if (lh->version >= 5)
19552 {
19553 gdb_byte segment_selector_size;
19554
19555 /* Skip address size. */
19556 read_1_byte (abfd, line_ptr);
19557 line_ptr += 1;
19558
19559 segment_selector_size = read_1_byte (abfd, line_ptr);
19560 line_ptr += 1;
19561 if (segment_selector_size != 0)
19562 {
19563 complaint (_("unsupported segment selector size %u "
19564 "in .debug_line section"),
19565 segment_selector_size);
19566 return NULL;
19567 }
19568 }
19569 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19570 line_ptr += offset_size;
19571 lh->statement_program_start = line_ptr + lh->header_length;
19572 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19573 line_ptr += 1;
19574 if (lh->version >= 4)
19575 {
19576 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19577 line_ptr += 1;
19578 }
19579 else
19580 lh->maximum_ops_per_instruction = 1;
19581
19582 if (lh->maximum_ops_per_instruction == 0)
19583 {
19584 lh->maximum_ops_per_instruction = 1;
19585 complaint (_("invalid maximum_ops_per_instruction "
19586 "in `.debug_line' section"));
19587 }
19588
19589 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19590 line_ptr += 1;
19591 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19592 line_ptr += 1;
19593 lh->line_range = read_1_byte (abfd, line_ptr);
19594 line_ptr += 1;
19595 lh->opcode_base = read_1_byte (abfd, line_ptr);
19596 line_ptr += 1;
19597 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19598
19599 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19600 for (i = 1; i < lh->opcode_base; ++i)
19601 {
19602 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19603 line_ptr += 1;
19604 }
19605
19606 if (lh->version >= 5)
19607 {
19608 /* Read directory table. */
19609 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19610 &cu->header,
19611 [] (struct line_header *header, const char *name,
19612 dir_index d_index, unsigned int mod_time,
19613 unsigned int length)
19614 {
19615 header->add_include_dir (name);
19616 });
19617
19618 /* Read file name table. */
19619 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19620 &cu->header,
19621 [] (struct line_header *header, const char *name,
19622 dir_index d_index, unsigned int mod_time,
19623 unsigned int length)
19624 {
19625 header->add_file_name (name, d_index, mod_time, length);
19626 });
19627 }
19628 else
19629 {
19630 /* Read directory table. */
19631 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19632 {
19633 line_ptr += bytes_read;
19634 lh->add_include_dir (cur_dir);
19635 }
19636 line_ptr += bytes_read;
19637
19638 /* Read file name table. */
19639 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19640 {
19641 unsigned int mod_time, length;
19642 dir_index d_index;
19643
19644 line_ptr += bytes_read;
19645 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19646 line_ptr += bytes_read;
19647 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19648 line_ptr += bytes_read;
19649 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19650 line_ptr += bytes_read;
19651
19652 lh->add_file_name (cur_file, d_index, mod_time, length);
19653 }
19654 line_ptr += bytes_read;
19655 }
19656
19657 if (line_ptr > (section->buffer + section->size))
19658 complaint (_("line number info header doesn't "
19659 "fit in `.debug_line' section"));
19660
19661 return lh;
19662 }
19663
19664 /* Subroutine of dwarf_decode_lines to simplify it.
19665 Return the file name of the psymtab for the given file_entry.
19666 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19667 If space for the result is malloc'd, *NAME_HOLDER will be set.
19668 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19669
19670 static const char *
19671 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19672 const dwarf2_psymtab *pst,
19673 const char *comp_dir,
19674 gdb::unique_xmalloc_ptr<char> *name_holder)
19675 {
19676 const char *include_name = fe.name;
19677 const char *include_name_to_compare = include_name;
19678 const char *pst_filename;
19679 int file_is_pst;
19680
19681 const char *dir_name = fe.include_dir (lh);
19682
19683 gdb::unique_xmalloc_ptr<char> hold_compare;
19684 if (!IS_ABSOLUTE_PATH (include_name)
19685 && (dir_name != NULL || comp_dir != NULL))
19686 {
19687 /* Avoid creating a duplicate psymtab for PST.
19688 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19689 Before we do the comparison, however, we need to account
19690 for DIR_NAME and COMP_DIR.
19691 First prepend dir_name (if non-NULL). If we still don't
19692 have an absolute path prepend comp_dir (if non-NULL).
19693 However, the directory we record in the include-file's
19694 psymtab does not contain COMP_DIR (to match the
19695 corresponding symtab(s)).
19696
19697 Example:
19698
19699 bash$ cd /tmp
19700 bash$ gcc -g ./hello.c
19701 include_name = "hello.c"
19702 dir_name = "."
19703 DW_AT_comp_dir = comp_dir = "/tmp"
19704 DW_AT_name = "./hello.c"
19705
19706 */
19707
19708 if (dir_name != NULL)
19709 {
19710 name_holder->reset (concat (dir_name, SLASH_STRING,
19711 include_name, (char *) NULL));
19712 include_name = name_holder->get ();
19713 include_name_to_compare = include_name;
19714 }
19715 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19716 {
19717 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19718 include_name, (char *) NULL));
19719 include_name_to_compare = hold_compare.get ();
19720 }
19721 }
19722
19723 pst_filename = pst->filename;
19724 gdb::unique_xmalloc_ptr<char> copied_name;
19725 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19726 {
19727 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19728 pst_filename, (char *) NULL));
19729 pst_filename = copied_name.get ();
19730 }
19731
19732 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19733
19734 if (file_is_pst)
19735 return NULL;
19736 return include_name;
19737 }
19738
19739 /* State machine to track the state of the line number program. */
19740
19741 class lnp_state_machine
19742 {
19743 public:
19744 /* Initialize a machine state for the start of a line number
19745 program. */
19746 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19747 bool record_lines_p);
19748
19749 file_entry *current_file ()
19750 {
19751 /* lh->file_names is 0-based, but the file name numbers in the
19752 statement program are 1-based. */
19753 return m_line_header->file_name_at (m_file);
19754 }
19755
19756 /* Record the line in the state machine. END_SEQUENCE is true if
19757 we're processing the end of a sequence. */
19758 void record_line (bool end_sequence);
19759
19760 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19761 nop-out rest of the lines in this sequence. */
19762 void check_line_address (struct dwarf2_cu *cu,
19763 const gdb_byte *line_ptr,
19764 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19765
19766 void handle_set_discriminator (unsigned int discriminator)
19767 {
19768 m_discriminator = discriminator;
19769 m_line_has_non_zero_discriminator |= discriminator != 0;
19770 }
19771
19772 /* Handle DW_LNE_set_address. */
19773 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19774 {
19775 m_op_index = 0;
19776 address += baseaddr;
19777 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19778 }
19779
19780 /* Handle DW_LNS_advance_pc. */
19781 void handle_advance_pc (CORE_ADDR adjust);
19782
19783 /* Handle a special opcode. */
19784 void handle_special_opcode (unsigned char op_code);
19785
19786 /* Handle DW_LNS_advance_line. */
19787 void handle_advance_line (int line_delta)
19788 {
19789 advance_line (line_delta);
19790 }
19791
19792 /* Handle DW_LNS_set_file. */
19793 void handle_set_file (file_name_index file);
19794
19795 /* Handle DW_LNS_negate_stmt. */
19796 void handle_negate_stmt ()
19797 {
19798 m_is_stmt = !m_is_stmt;
19799 }
19800
19801 /* Handle DW_LNS_const_add_pc. */
19802 void handle_const_add_pc ();
19803
19804 /* Handle DW_LNS_fixed_advance_pc. */
19805 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19806 {
19807 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19808 m_op_index = 0;
19809 }
19810
19811 /* Handle DW_LNS_copy. */
19812 void handle_copy ()
19813 {
19814 record_line (false);
19815 m_discriminator = 0;
19816 }
19817
19818 /* Handle DW_LNE_end_sequence. */
19819 void handle_end_sequence ()
19820 {
19821 m_currently_recording_lines = true;
19822 }
19823
19824 private:
19825 /* Advance the line by LINE_DELTA. */
19826 void advance_line (int line_delta)
19827 {
19828 m_line += line_delta;
19829
19830 if (line_delta != 0)
19831 m_line_has_non_zero_discriminator = m_discriminator != 0;
19832 }
19833
19834 struct dwarf2_cu *m_cu;
19835
19836 gdbarch *m_gdbarch;
19837
19838 /* True if we're recording lines.
19839 Otherwise we're building partial symtabs and are just interested in
19840 finding include files mentioned by the line number program. */
19841 bool m_record_lines_p;
19842
19843 /* The line number header. */
19844 line_header *m_line_header;
19845
19846 /* These are part of the standard DWARF line number state machine,
19847 and initialized according to the DWARF spec. */
19848
19849 unsigned char m_op_index = 0;
19850 /* The line table index of the current file. */
19851 file_name_index m_file = 1;
19852 unsigned int m_line = 1;
19853
19854 /* These are initialized in the constructor. */
19855
19856 CORE_ADDR m_address;
19857 bool m_is_stmt;
19858 unsigned int m_discriminator;
19859
19860 /* Additional bits of state we need to track. */
19861
19862 /* The last file that we called dwarf2_start_subfile for.
19863 This is only used for TLLs. */
19864 unsigned int m_last_file = 0;
19865 /* The last file a line number was recorded for. */
19866 struct subfile *m_last_subfile = NULL;
19867
19868 /* When true, record the lines we decode. */
19869 bool m_currently_recording_lines = false;
19870
19871 /* The last line number that was recorded, used to coalesce
19872 consecutive entries for the same line. This can happen, for
19873 example, when discriminators are present. PR 17276. */
19874 unsigned int m_last_line = 0;
19875 bool m_line_has_non_zero_discriminator = false;
19876 };
19877
19878 void
19879 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19880 {
19881 CORE_ADDR addr_adj = (((m_op_index + adjust)
19882 / m_line_header->maximum_ops_per_instruction)
19883 * m_line_header->minimum_instruction_length);
19884 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19885 m_op_index = ((m_op_index + adjust)
19886 % m_line_header->maximum_ops_per_instruction);
19887 }
19888
19889 void
19890 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19891 {
19892 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19893 CORE_ADDR addr_adj = (((m_op_index
19894 + (adj_opcode / m_line_header->line_range))
19895 / m_line_header->maximum_ops_per_instruction)
19896 * m_line_header->minimum_instruction_length);
19897 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19898 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19899 % m_line_header->maximum_ops_per_instruction);
19900
19901 int line_delta = (m_line_header->line_base
19902 + (adj_opcode % m_line_header->line_range));
19903 advance_line (line_delta);
19904 record_line (false);
19905 m_discriminator = 0;
19906 }
19907
19908 void
19909 lnp_state_machine::handle_set_file (file_name_index file)
19910 {
19911 m_file = file;
19912
19913 const file_entry *fe = current_file ();
19914 if (fe == NULL)
19915 dwarf2_debug_line_missing_file_complaint ();
19916 else if (m_record_lines_p)
19917 {
19918 const char *dir = fe->include_dir (m_line_header);
19919
19920 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19921 m_line_has_non_zero_discriminator = m_discriminator != 0;
19922 dwarf2_start_subfile (m_cu, fe->name, dir);
19923 }
19924 }
19925
19926 void
19927 lnp_state_machine::handle_const_add_pc ()
19928 {
19929 CORE_ADDR adjust
19930 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19931
19932 CORE_ADDR addr_adj
19933 = (((m_op_index + adjust)
19934 / m_line_header->maximum_ops_per_instruction)
19935 * m_line_header->minimum_instruction_length);
19936
19937 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19938 m_op_index = ((m_op_index + adjust)
19939 % m_line_header->maximum_ops_per_instruction);
19940 }
19941
19942 /* Return non-zero if we should add LINE to the line number table.
19943 LINE is the line to add, LAST_LINE is the last line that was added,
19944 LAST_SUBFILE is the subfile for LAST_LINE.
19945 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19946 had a non-zero discriminator.
19947
19948 We have to be careful in the presence of discriminators.
19949 E.g., for this line:
19950
19951 for (i = 0; i < 100000; i++);
19952
19953 clang can emit four line number entries for that one line,
19954 each with a different discriminator.
19955 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19956
19957 However, we want gdb to coalesce all four entries into one.
19958 Otherwise the user could stepi into the middle of the line and
19959 gdb would get confused about whether the pc really was in the
19960 middle of the line.
19961
19962 Things are further complicated by the fact that two consecutive
19963 line number entries for the same line is a heuristic used by gcc
19964 to denote the end of the prologue. So we can't just discard duplicate
19965 entries, we have to be selective about it. The heuristic we use is
19966 that we only collapse consecutive entries for the same line if at least
19967 one of those entries has a non-zero discriminator. PR 17276.
19968
19969 Note: Addresses in the line number state machine can never go backwards
19970 within one sequence, thus this coalescing is ok. */
19971
19972 static int
19973 dwarf_record_line_p (struct dwarf2_cu *cu,
19974 unsigned int line, unsigned int last_line,
19975 int line_has_non_zero_discriminator,
19976 struct subfile *last_subfile)
19977 {
19978 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19979 return 1;
19980 if (line != last_line)
19981 return 1;
19982 /* Same line for the same file that we've seen already.
19983 As a last check, for pr 17276, only record the line if the line
19984 has never had a non-zero discriminator. */
19985 if (!line_has_non_zero_discriminator)
19986 return 1;
19987 return 0;
19988 }
19989
19990 /* Use the CU's builder to record line number LINE beginning at
19991 address ADDRESS in the line table of subfile SUBFILE. */
19992
19993 static void
19994 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19995 unsigned int line, CORE_ADDR address,
19996 struct dwarf2_cu *cu)
19997 {
19998 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19999
20000 if (dwarf_line_debug)
20001 {
20002 fprintf_unfiltered (gdb_stdlog,
20003 "Recording line %u, file %s, address %s\n",
20004 line, lbasename (subfile->name),
20005 paddress (gdbarch, address));
20006 }
20007
20008 if (cu != nullptr)
20009 cu->get_builder ()->record_line (subfile, line, addr);
20010 }
20011
20012 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20013 Mark the end of a set of line number records.
20014 The arguments are the same as for dwarf_record_line_1.
20015 If SUBFILE is NULL the request is ignored. */
20016
20017 static void
20018 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20019 CORE_ADDR address, struct dwarf2_cu *cu)
20020 {
20021 if (subfile == NULL)
20022 return;
20023
20024 if (dwarf_line_debug)
20025 {
20026 fprintf_unfiltered (gdb_stdlog,
20027 "Finishing current line, file %s, address %s\n",
20028 lbasename (subfile->name),
20029 paddress (gdbarch, address));
20030 }
20031
20032 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20033 }
20034
20035 void
20036 lnp_state_machine::record_line (bool end_sequence)
20037 {
20038 if (dwarf_line_debug)
20039 {
20040 fprintf_unfiltered (gdb_stdlog,
20041 "Processing actual line %u: file %u,"
20042 " address %s, is_stmt %u, discrim %u%s\n",
20043 m_line, m_file,
20044 paddress (m_gdbarch, m_address),
20045 m_is_stmt, m_discriminator,
20046 (end_sequence ? "\t(end sequence)" : ""));
20047 }
20048
20049 file_entry *fe = current_file ();
20050
20051 if (fe == NULL)
20052 dwarf2_debug_line_missing_file_complaint ();
20053 /* For now we ignore lines not starting on an instruction boundary.
20054 But not when processing end_sequence for compatibility with the
20055 previous version of the code. */
20056 else if (m_op_index == 0 || end_sequence)
20057 {
20058 fe->included_p = 1;
20059 if (m_record_lines_p
20060 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
20061 {
20062 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20063 || end_sequence)
20064 {
20065 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20066 m_currently_recording_lines ? m_cu : nullptr);
20067 }
20068
20069 if (!end_sequence)
20070 {
20071 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20072 m_line_has_non_zero_discriminator,
20073 m_last_subfile))
20074 {
20075 buildsym_compunit *builder = m_cu->get_builder ();
20076 dwarf_record_line_1 (m_gdbarch,
20077 builder->get_current_subfile (),
20078 m_line, m_address,
20079 m_currently_recording_lines ? m_cu : nullptr);
20080 }
20081 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20082 m_last_line = m_line;
20083 }
20084 }
20085 }
20086 }
20087
20088 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20089 line_header *lh, bool record_lines_p)
20090 {
20091 m_cu = cu;
20092 m_gdbarch = arch;
20093 m_record_lines_p = record_lines_p;
20094 m_line_header = lh;
20095
20096 m_currently_recording_lines = true;
20097
20098 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20099 was a line entry for it so that the backend has a chance to adjust it
20100 and also record it in case it needs it. This is currently used by MIPS
20101 code, cf. `mips_adjust_dwarf2_line'. */
20102 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20103 m_is_stmt = lh->default_is_stmt;
20104 m_discriminator = 0;
20105 }
20106
20107 void
20108 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20109 const gdb_byte *line_ptr,
20110 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20111 {
20112 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20113 the pc range of the CU. However, we restrict the test to only ADDRESS
20114 values of zero to preserve GDB's previous behaviour which is to handle
20115 the specific case of a function being GC'd by the linker. */
20116
20117 if (address == 0 && address < unrelocated_lowpc)
20118 {
20119 /* This line table is for a function which has been
20120 GCd by the linker. Ignore it. PR gdb/12528 */
20121
20122 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20123 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20124
20125 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20126 line_offset, objfile_name (objfile));
20127 m_currently_recording_lines = false;
20128 /* Note: m_currently_recording_lines is left as false until we see
20129 DW_LNE_end_sequence. */
20130 }
20131 }
20132
20133 /* Subroutine of dwarf_decode_lines to simplify it.
20134 Process the line number information in LH.
20135 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20136 program in order to set included_p for every referenced header. */
20137
20138 static void
20139 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20140 const int decode_for_pst_p, CORE_ADDR lowpc)
20141 {
20142 const gdb_byte *line_ptr, *extended_end;
20143 const gdb_byte *line_end;
20144 unsigned int bytes_read, extended_len;
20145 unsigned char op_code, extended_op;
20146 CORE_ADDR baseaddr;
20147 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20148 bfd *abfd = objfile->obfd;
20149 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20150 /* True if we're recording line info (as opposed to building partial
20151 symtabs and just interested in finding include files mentioned by
20152 the line number program). */
20153 bool record_lines_p = !decode_for_pst_p;
20154
20155 baseaddr = objfile->text_section_offset ();
20156
20157 line_ptr = lh->statement_program_start;
20158 line_end = lh->statement_program_end;
20159
20160 /* Read the statement sequences until there's nothing left. */
20161 while (line_ptr < line_end)
20162 {
20163 /* The DWARF line number program state machine. Reset the state
20164 machine at the start of each sequence. */
20165 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20166 bool end_sequence = false;
20167
20168 if (record_lines_p)
20169 {
20170 /* Start a subfile for the current file of the state
20171 machine. */
20172 const file_entry *fe = state_machine.current_file ();
20173
20174 if (fe != NULL)
20175 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20176 }
20177
20178 /* Decode the table. */
20179 while (line_ptr < line_end && !end_sequence)
20180 {
20181 op_code = read_1_byte (abfd, line_ptr);
20182 line_ptr += 1;
20183
20184 if (op_code >= lh->opcode_base)
20185 {
20186 /* Special opcode. */
20187 state_machine.handle_special_opcode (op_code);
20188 }
20189 else switch (op_code)
20190 {
20191 case DW_LNS_extended_op:
20192 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20193 &bytes_read);
20194 line_ptr += bytes_read;
20195 extended_end = line_ptr + extended_len;
20196 extended_op = read_1_byte (abfd, line_ptr);
20197 line_ptr += 1;
20198 switch (extended_op)
20199 {
20200 case DW_LNE_end_sequence:
20201 state_machine.handle_end_sequence ();
20202 end_sequence = true;
20203 break;
20204 case DW_LNE_set_address:
20205 {
20206 CORE_ADDR address
20207 = read_address (abfd, line_ptr, cu, &bytes_read);
20208 line_ptr += bytes_read;
20209
20210 state_machine.check_line_address (cu, line_ptr,
20211 lowpc - baseaddr, address);
20212 state_machine.handle_set_address (baseaddr, address);
20213 }
20214 break;
20215 case DW_LNE_define_file:
20216 {
20217 const char *cur_file;
20218 unsigned int mod_time, length;
20219 dir_index dindex;
20220
20221 cur_file = read_direct_string (abfd, line_ptr,
20222 &bytes_read);
20223 line_ptr += bytes_read;
20224 dindex = (dir_index)
20225 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20226 line_ptr += bytes_read;
20227 mod_time =
20228 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20229 line_ptr += bytes_read;
20230 length =
20231 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20232 line_ptr += bytes_read;
20233 lh->add_file_name (cur_file, dindex, mod_time, length);
20234 }
20235 break;
20236 case DW_LNE_set_discriminator:
20237 {
20238 /* The discriminator is not interesting to the
20239 debugger; just ignore it. We still need to
20240 check its value though:
20241 if there are consecutive entries for the same
20242 (non-prologue) line we want to coalesce them.
20243 PR 17276. */
20244 unsigned int discr
20245 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20246 line_ptr += bytes_read;
20247
20248 state_machine.handle_set_discriminator (discr);
20249 }
20250 break;
20251 default:
20252 complaint (_("mangled .debug_line section"));
20253 return;
20254 }
20255 /* Make sure that we parsed the extended op correctly. If e.g.
20256 we expected a different address size than the producer used,
20257 we may have read the wrong number of bytes. */
20258 if (line_ptr != extended_end)
20259 {
20260 complaint (_("mangled .debug_line section"));
20261 return;
20262 }
20263 break;
20264 case DW_LNS_copy:
20265 state_machine.handle_copy ();
20266 break;
20267 case DW_LNS_advance_pc:
20268 {
20269 CORE_ADDR adjust
20270 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20271 line_ptr += bytes_read;
20272
20273 state_machine.handle_advance_pc (adjust);
20274 }
20275 break;
20276 case DW_LNS_advance_line:
20277 {
20278 int line_delta
20279 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20280 line_ptr += bytes_read;
20281
20282 state_machine.handle_advance_line (line_delta);
20283 }
20284 break;
20285 case DW_LNS_set_file:
20286 {
20287 file_name_index file
20288 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20289 &bytes_read);
20290 line_ptr += bytes_read;
20291
20292 state_machine.handle_set_file (file);
20293 }
20294 break;
20295 case DW_LNS_set_column:
20296 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20297 line_ptr += bytes_read;
20298 break;
20299 case DW_LNS_negate_stmt:
20300 state_machine.handle_negate_stmt ();
20301 break;
20302 case DW_LNS_set_basic_block:
20303 break;
20304 /* Add to the address register of the state machine the
20305 address increment value corresponding to special opcode
20306 255. I.e., this value is scaled by the minimum
20307 instruction length since special opcode 255 would have
20308 scaled the increment. */
20309 case DW_LNS_const_add_pc:
20310 state_machine.handle_const_add_pc ();
20311 break;
20312 case DW_LNS_fixed_advance_pc:
20313 {
20314 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20315 line_ptr += 2;
20316
20317 state_machine.handle_fixed_advance_pc (addr_adj);
20318 }
20319 break;
20320 default:
20321 {
20322 /* Unknown standard opcode, ignore it. */
20323 int i;
20324
20325 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20326 {
20327 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20328 line_ptr += bytes_read;
20329 }
20330 }
20331 }
20332 }
20333
20334 if (!end_sequence)
20335 dwarf2_debug_line_missing_end_sequence_complaint ();
20336
20337 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20338 in which case we still finish recording the last line). */
20339 state_machine.record_line (true);
20340 }
20341 }
20342
20343 /* Decode the Line Number Program (LNP) for the given line_header
20344 structure and CU. The actual information extracted and the type
20345 of structures created from the LNP depends on the value of PST.
20346
20347 1. If PST is NULL, then this procedure uses the data from the program
20348 to create all necessary symbol tables, and their linetables.
20349
20350 2. If PST is not NULL, this procedure reads the program to determine
20351 the list of files included by the unit represented by PST, and
20352 builds all the associated partial symbol tables.
20353
20354 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20355 It is used for relative paths in the line table.
20356 NOTE: When processing partial symtabs (pst != NULL),
20357 comp_dir == pst->dirname.
20358
20359 NOTE: It is important that psymtabs have the same file name (via strcmp)
20360 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20361 symtab we don't use it in the name of the psymtabs we create.
20362 E.g. expand_line_sal requires this when finding psymtabs to expand.
20363 A good testcase for this is mb-inline.exp.
20364
20365 LOWPC is the lowest address in CU (or 0 if not known).
20366
20367 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20368 for its PC<->lines mapping information. Otherwise only the filename
20369 table is read in. */
20370
20371 static void
20372 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20373 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20374 CORE_ADDR lowpc, int decode_mapping)
20375 {
20376 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20377 const int decode_for_pst_p = (pst != NULL);
20378
20379 if (decode_mapping)
20380 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20381
20382 if (decode_for_pst_p)
20383 {
20384 /* Now that we're done scanning the Line Header Program, we can
20385 create the psymtab of each included file. */
20386 for (auto &file_entry : lh->file_names ())
20387 if (file_entry.included_p == 1)
20388 {
20389 gdb::unique_xmalloc_ptr<char> name_holder;
20390 const char *include_name =
20391 psymtab_include_file_name (lh, file_entry, pst,
20392 comp_dir, &name_holder);
20393 if (include_name != NULL)
20394 dwarf2_create_include_psymtab (include_name, pst, objfile);
20395 }
20396 }
20397 else
20398 {
20399 /* Make sure a symtab is created for every file, even files
20400 which contain only variables (i.e. no code with associated
20401 line numbers). */
20402 buildsym_compunit *builder = cu->get_builder ();
20403 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20404
20405 for (auto &fe : lh->file_names ())
20406 {
20407 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20408 if (builder->get_current_subfile ()->symtab == NULL)
20409 {
20410 builder->get_current_subfile ()->symtab
20411 = allocate_symtab (cust,
20412 builder->get_current_subfile ()->name);
20413 }
20414 fe.symtab = builder->get_current_subfile ()->symtab;
20415 }
20416 }
20417 }
20418
20419 /* Start a subfile for DWARF. FILENAME is the name of the file and
20420 DIRNAME the name of the source directory which contains FILENAME
20421 or NULL if not known.
20422 This routine tries to keep line numbers from identical absolute and
20423 relative file names in a common subfile.
20424
20425 Using the `list' example from the GDB testsuite, which resides in
20426 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20427 of /srcdir/list0.c yields the following debugging information for list0.c:
20428
20429 DW_AT_name: /srcdir/list0.c
20430 DW_AT_comp_dir: /compdir
20431 files.files[0].name: list0.h
20432 files.files[0].dir: /srcdir
20433 files.files[1].name: list0.c
20434 files.files[1].dir: /srcdir
20435
20436 The line number information for list0.c has to end up in a single
20437 subfile, so that `break /srcdir/list0.c:1' works as expected.
20438 start_subfile will ensure that this happens provided that we pass the
20439 concatenation of files.files[1].dir and files.files[1].name as the
20440 subfile's name. */
20441
20442 static void
20443 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20444 const char *dirname)
20445 {
20446 gdb::unique_xmalloc_ptr<char> copy;
20447
20448 /* In order not to lose the line information directory,
20449 we concatenate it to the filename when it makes sense.
20450 Note that the Dwarf3 standard says (speaking of filenames in line
20451 information): ``The directory index is ignored for file names
20452 that represent full path names''. Thus ignoring dirname in the
20453 `else' branch below isn't an issue. */
20454
20455 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20456 {
20457 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20458 filename = copy.get ();
20459 }
20460
20461 cu->get_builder ()->start_subfile (filename);
20462 }
20463
20464 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20465 buildsym_compunit constructor. */
20466
20467 struct compunit_symtab *
20468 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20469 CORE_ADDR low_pc)
20470 {
20471 gdb_assert (m_builder == nullptr);
20472
20473 m_builder.reset (new struct buildsym_compunit
20474 (per_cu->dwarf2_per_objfile->objfile,
20475 name, comp_dir, language, low_pc));
20476
20477 list_in_scope = get_builder ()->get_file_symbols ();
20478
20479 get_builder ()->record_debugformat ("DWARF 2");
20480 get_builder ()->record_producer (producer);
20481
20482 processing_has_namespace_info = false;
20483
20484 return get_builder ()->get_compunit_symtab ();
20485 }
20486
20487 static void
20488 var_decode_location (struct attribute *attr, struct symbol *sym,
20489 struct dwarf2_cu *cu)
20490 {
20491 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20492 struct comp_unit_head *cu_header = &cu->header;
20493
20494 /* NOTE drow/2003-01-30: There used to be a comment and some special
20495 code here to turn a symbol with DW_AT_external and a
20496 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20497 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20498 with some versions of binutils) where shared libraries could have
20499 relocations against symbols in their debug information - the
20500 minimal symbol would have the right address, but the debug info
20501 would not. It's no longer necessary, because we will explicitly
20502 apply relocations when we read in the debug information now. */
20503
20504 /* A DW_AT_location attribute with no contents indicates that a
20505 variable has been optimized away. */
20506 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20507 {
20508 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20509 return;
20510 }
20511
20512 /* Handle one degenerate form of location expression specially, to
20513 preserve GDB's previous behavior when section offsets are
20514 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20515 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20516
20517 if (attr->form_is_block ()
20518 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20519 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20520 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20521 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20522 && (DW_BLOCK (attr)->size
20523 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20524 {
20525 unsigned int dummy;
20526
20527 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20528 SET_SYMBOL_VALUE_ADDRESS (sym,
20529 read_address (objfile->obfd,
20530 DW_BLOCK (attr)->data + 1,
20531 cu, &dummy));
20532 else
20533 SET_SYMBOL_VALUE_ADDRESS
20534 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20535 &dummy));
20536 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20537 fixup_symbol_section (sym, objfile);
20538 SET_SYMBOL_VALUE_ADDRESS
20539 (sym,
20540 SYMBOL_VALUE_ADDRESS (sym)
20541 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20542 return;
20543 }
20544
20545 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20546 expression evaluator, and use LOC_COMPUTED only when necessary
20547 (i.e. when the value of a register or memory location is
20548 referenced, or a thread-local block, etc.). Then again, it might
20549 not be worthwhile. I'm assuming that it isn't unless performance
20550 or memory numbers show me otherwise. */
20551
20552 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20553
20554 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20555 cu->has_loclist = true;
20556 }
20557
20558 /* Given a pointer to a DWARF information entry, figure out if we need
20559 to make a symbol table entry for it, and if so, create a new entry
20560 and return a pointer to it.
20561 If TYPE is NULL, determine symbol type from the die, otherwise
20562 used the passed type.
20563 If SPACE is not NULL, use it to hold the new symbol. If it is
20564 NULL, allocate a new symbol on the objfile's obstack. */
20565
20566 static struct symbol *
20567 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20568 struct symbol *space)
20569 {
20570 struct dwarf2_per_objfile *dwarf2_per_objfile
20571 = cu->per_cu->dwarf2_per_objfile;
20572 struct objfile *objfile = dwarf2_per_objfile->objfile;
20573 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20574 struct symbol *sym = NULL;
20575 const char *name;
20576 struct attribute *attr = NULL;
20577 struct attribute *attr2 = NULL;
20578 CORE_ADDR baseaddr;
20579 struct pending **list_to_add = NULL;
20580
20581 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20582
20583 baseaddr = objfile->text_section_offset ();
20584
20585 name = dwarf2_name (die, cu);
20586 if (name)
20587 {
20588 const char *linkagename;
20589 int suppress_add = 0;
20590
20591 if (space)
20592 sym = space;
20593 else
20594 sym = allocate_symbol (objfile);
20595 OBJSTAT (objfile, n_syms++);
20596
20597 /* Cache this symbol's name and the name's demangled form (if any). */
20598 sym->set_language (cu->language, &objfile->objfile_obstack);
20599 linkagename = dwarf2_physname (name, die, cu);
20600 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20601
20602 /* Fortran does not have mangling standard and the mangling does differ
20603 between gfortran, iFort etc. */
20604 if (cu->language == language_fortran
20605 && symbol_get_demangled_name (sym) == NULL)
20606 symbol_set_demangled_name (sym,
20607 dwarf2_full_name (name, die, cu),
20608 NULL);
20609
20610 /* Default assumptions.
20611 Use the passed type or decode it from the die. */
20612 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20613 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20614 if (type != NULL)
20615 SYMBOL_TYPE (sym) = type;
20616 else
20617 SYMBOL_TYPE (sym) = die_type (die, cu);
20618 attr = dwarf2_attr (die,
20619 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20620 cu);
20621 if (attr != nullptr)
20622 {
20623 SYMBOL_LINE (sym) = DW_UNSND (attr);
20624 }
20625
20626 attr = dwarf2_attr (die,
20627 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20628 cu);
20629 if (attr != nullptr)
20630 {
20631 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20632 struct file_entry *fe;
20633
20634 if (cu->line_header != NULL)
20635 fe = cu->line_header->file_name_at (file_index);
20636 else
20637 fe = NULL;
20638
20639 if (fe == NULL)
20640 complaint (_("file index out of range"));
20641 else
20642 symbol_set_symtab (sym, fe->symtab);
20643 }
20644
20645 switch (die->tag)
20646 {
20647 case DW_TAG_label:
20648 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20649 if (attr != nullptr)
20650 {
20651 CORE_ADDR addr;
20652
20653 addr = attr->value_as_address ();
20654 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20655 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20656 }
20657 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20658 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20659 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20660 add_symbol_to_list (sym, cu->list_in_scope);
20661 break;
20662 case DW_TAG_subprogram:
20663 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20664 finish_block. */
20665 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20666 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20667 if ((attr2 && (DW_UNSND (attr2) != 0))
20668 || cu->language == language_ada
20669 || cu->language == language_fortran)
20670 {
20671 /* Subprograms marked external are stored as a global symbol.
20672 Ada and Fortran subprograms, whether marked external or
20673 not, are always stored as a global symbol, because we want
20674 to be able to access them globally. For instance, we want
20675 to be able to break on a nested subprogram without having
20676 to specify the context. */
20677 list_to_add = cu->get_builder ()->get_global_symbols ();
20678 }
20679 else
20680 {
20681 list_to_add = cu->list_in_scope;
20682 }
20683 break;
20684 case DW_TAG_inlined_subroutine:
20685 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20686 finish_block. */
20687 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20688 SYMBOL_INLINED (sym) = 1;
20689 list_to_add = cu->list_in_scope;
20690 break;
20691 case DW_TAG_template_value_param:
20692 suppress_add = 1;
20693 /* Fall through. */
20694 case DW_TAG_constant:
20695 case DW_TAG_variable:
20696 case DW_TAG_member:
20697 /* Compilation with minimal debug info may result in
20698 variables with missing type entries. Change the
20699 misleading `void' type to something sensible. */
20700 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20701 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20702
20703 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20704 /* In the case of DW_TAG_member, we should only be called for
20705 static const members. */
20706 if (die->tag == DW_TAG_member)
20707 {
20708 /* dwarf2_add_field uses die_is_declaration,
20709 so we do the same. */
20710 gdb_assert (die_is_declaration (die, cu));
20711 gdb_assert (attr);
20712 }
20713 if (attr != nullptr)
20714 {
20715 dwarf2_const_value (attr, sym, cu);
20716 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20717 if (!suppress_add)
20718 {
20719 if (attr2 && (DW_UNSND (attr2) != 0))
20720 list_to_add = cu->get_builder ()->get_global_symbols ();
20721 else
20722 list_to_add = cu->list_in_scope;
20723 }
20724 break;
20725 }
20726 attr = dwarf2_attr (die, DW_AT_location, cu);
20727 if (attr != nullptr)
20728 {
20729 var_decode_location (attr, sym, cu);
20730 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20731
20732 /* Fortran explicitly imports any global symbols to the local
20733 scope by DW_TAG_common_block. */
20734 if (cu->language == language_fortran && die->parent
20735 && die->parent->tag == DW_TAG_common_block)
20736 attr2 = NULL;
20737
20738 if (SYMBOL_CLASS (sym) == LOC_STATIC
20739 && SYMBOL_VALUE_ADDRESS (sym) == 0
20740 && !dwarf2_per_objfile->has_section_at_zero)
20741 {
20742 /* When a static variable is eliminated by the linker,
20743 the corresponding debug information is not stripped
20744 out, but the variable address is set to null;
20745 do not add such variables into symbol table. */
20746 }
20747 else if (attr2 && (DW_UNSND (attr2) != 0))
20748 {
20749 if (SYMBOL_CLASS (sym) == LOC_STATIC
20750 && (objfile->flags & OBJF_MAINLINE) == 0
20751 && dwarf2_per_objfile->can_copy)
20752 {
20753 /* A global static variable might be subject to
20754 copy relocation. We first check for a local
20755 minsym, though, because maybe the symbol was
20756 marked hidden, in which case this would not
20757 apply. */
20758 bound_minimal_symbol found
20759 = (lookup_minimal_symbol_linkage
20760 (sym->linkage_name (), objfile));
20761 if (found.minsym != nullptr)
20762 sym->maybe_copied = 1;
20763 }
20764
20765 /* A variable with DW_AT_external is never static,
20766 but it may be block-scoped. */
20767 list_to_add
20768 = ((cu->list_in_scope
20769 == cu->get_builder ()->get_file_symbols ())
20770 ? cu->get_builder ()->get_global_symbols ()
20771 : cu->list_in_scope);
20772 }
20773 else
20774 list_to_add = cu->list_in_scope;
20775 }
20776 else
20777 {
20778 /* We do not know the address of this symbol.
20779 If it is an external symbol and we have type information
20780 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20781 The address of the variable will then be determined from
20782 the minimal symbol table whenever the variable is
20783 referenced. */
20784 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20785
20786 /* Fortran explicitly imports any global symbols to the local
20787 scope by DW_TAG_common_block. */
20788 if (cu->language == language_fortran && die->parent
20789 && die->parent->tag == DW_TAG_common_block)
20790 {
20791 /* SYMBOL_CLASS doesn't matter here because
20792 read_common_block is going to reset it. */
20793 if (!suppress_add)
20794 list_to_add = cu->list_in_scope;
20795 }
20796 else if (attr2 && (DW_UNSND (attr2) != 0)
20797 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20798 {
20799 /* A variable with DW_AT_external is never static, but it
20800 may be block-scoped. */
20801 list_to_add
20802 = ((cu->list_in_scope
20803 == cu->get_builder ()->get_file_symbols ())
20804 ? cu->get_builder ()->get_global_symbols ()
20805 : cu->list_in_scope);
20806
20807 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20808 }
20809 else if (!die_is_declaration (die, cu))
20810 {
20811 /* Use the default LOC_OPTIMIZED_OUT class. */
20812 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20813 if (!suppress_add)
20814 list_to_add = cu->list_in_scope;
20815 }
20816 }
20817 break;
20818 case DW_TAG_formal_parameter:
20819 {
20820 /* If we are inside a function, mark this as an argument. If
20821 not, we might be looking at an argument to an inlined function
20822 when we do not have enough information to show inlined frames;
20823 pretend it's a local variable in that case so that the user can
20824 still see it. */
20825 struct context_stack *curr
20826 = cu->get_builder ()->get_current_context_stack ();
20827 if (curr != nullptr && curr->name != nullptr)
20828 SYMBOL_IS_ARGUMENT (sym) = 1;
20829 attr = dwarf2_attr (die, DW_AT_location, cu);
20830 if (attr != nullptr)
20831 {
20832 var_decode_location (attr, sym, cu);
20833 }
20834 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20835 if (attr != nullptr)
20836 {
20837 dwarf2_const_value (attr, sym, cu);
20838 }
20839
20840 list_to_add = cu->list_in_scope;
20841 }
20842 break;
20843 case DW_TAG_unspecified_parameters:
20844 /* From varargs functions; gdb doesn't seem to have any
20845 interest in this information, so just ignore it for now.
20846 (FIXME?) */
20847 break;
20848 case DW_TAG_template_type_param:
20849 suppress_add = 1;
20850 /* Fall through. */
20851 case DW_TAG_class_type:
20852 case DW_TAG_interface_type:
20853 case DW_TAG_structure_type:
20854 case DW_TAG_union_type:
20855 case DW_TAG_set_type:
20856 case DW_TAG_enumeration_type:
20857 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20858 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20859
20860 {
20861 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20862 really ever be static objects: otherwise, if you try
20863 to, say, break of a class's method and you're in a file
20864 which doesn't mention that class, it won't work unless
20865 the check for all static symbols in lookup_symbol_aux
20866 saves you. See the OtherFileClass tests in
20867 gdb.c++/namespace.exp. */
20868
20869 if (!suppress_add)
20870 {
20871 buildsym_compunit *builder = cu->get_builder ();
20872 list_to_add
20873 = (cu->list_in_scope == builder->get_file_symbols ()
20874 && cu->language == language_cplus
20875 ? builder->get_global_symbols ()
20876 : cu->list_in_scope);
20877
20878 /* The semantics of C++ state that "struct foo {
20879 ... }" also defines a typedef for "foo". */
20880 if (cu->language == language_cplus
20881 || cu->language == language_ada
20882 || cu->language == language_d
20883 || cu->language == language_rust)
20884 {
20885 /* The symbol's name is already allocated along
20886 with this objfile, so we don't need to
20887 duplicate it for the type. */
20888 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20889 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20890 }
20891 }
20892 }
20893 break;
20894 case DW_TAG_typedef:
20895 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20896 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20897 list_to_add = cu->list_in_scope;
20898 break;
20899 case DW_TAG_base_type:
20900 case DW_TAG_subrange_type:
20901 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20902 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20903 list_to_add = cu->list_in_scope;
20904 break;
20905 case DW_TAG_enumerator:
20906 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20907 if (attr != nullptr)
20908 {
20909 dwarf2_const_value (attr, sym, cu);
20910 }
20911 {
20912 /* NOTE: carlton/2003-11-10: See comment above in the
20913 DW_TAG_class_type, etc. block. */
20914
20915 list_to_add
20916 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20917 && cu->language == language_cplus
20918 ? cu->get_builder ()->get_global_symbols ()
20919 : cu->list_in_scope);
20920 }
20921 break;
20922 case DW_TAG_imported_declaration:
20923 case DW_TAG_namespace:
20924 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20925 list_to_add = cu->get_builder ()->get_global_symbols ();
20926 break;
20927 case DW_TAG_module:
20928 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20929 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20930 list_to_add = cu->get_builder ()->get_global_symbols ();
20931 break;
20932 case DW_TAG_common_block:
20933 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20934 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20935 add_symbol_to_list (sym, cu->list_in_scope);
20936 break;
20937 default:
20938 /* Not a tag we recognize. Hopefully we aren't processing
20939 trash data, but since we must specifically ignore things
20940 we don't recognize, there is nothing else we should do at
20941 this point. */
20942 complaint (_("unsupported tag: '%s'"),
20943 dwarf_tag_name (die->tag));
20944 break;
20945 }
20946
20947 if (suppress_add)
20948 {
20949 sym->hash_next = objfile->template_symbols;
20950 objfile->template_symbols = sym;
20951 list_to_add = NULL;
20952 }
20953
20954 if (list_to_add != NULL)
20955 add_symbol_to_list (sym, list_to_add);
20956
20957 /* For the benefit of old versions of GCC, check for anonymous
20958 namespaces based on the demangled name. */
20959 if (!cu->processing_has_namespace_info
20960 && cu->language == language_cplus)
20961 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20962 }
20963 return (sym);
20964 }
20965
20966 /* Given an attr with a DW_FORM_dataN value in host byte order,
20967 zero-extend it as appropriate for the symbol's type. The DWARF
20968 standard (v4) is not entirely clear about the meaning of using
20969 DW_FORM_dataN for a constant with a signed type, where the type is
20970 wider than the data. The conclusion of a discussion on the DWARF
20971 list was that this is unspecified. We choose to always zero-extend
20972 because that is the interpretation long in use by GCC. */
20973
20974 static gdb_byte *
20975 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20976 struct dwarf2_cu *cu, LONGEST *value, int bits)
20977 {
20978 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20979 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20980 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20981 LONGEST l = DW_UNSND (attr);
20982
20983 if (bits < sizeof (*value) * 8)
20984 {
20985 l &= ((LONGEST) 1 << bits) - 1;
20986 *value = l;
20987 }
20988 else if (bits == sizeof (*value) * 8)
20989 *value = l;
20990 else
20991 {
20992 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20993 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20994 return bytes;
20995 }
20996
20997 return NULL;
20998 }
20999
21000 /* Read a constant value from an attribute. Either set *VALUE, or if
21001 the value does not fit in *VALUE, set *BYTES - either already
21002 allocated on the objfile obstack, or newly allocated on OBSTACK,
21003 or, set *BATON, if we translated the constant to a location
21004 expression. */
21005
21006 static void
21007 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21008 const char *name, struct obstack *obstack,
21009 struct dwarf2_cu *cu,
21010 LONGEST *value, const gdb_byte **bytes,
21011 struct dwarf2_locexpr_baton **baton)
21012 {
21013 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21014 struct comp_unit_head *cu_header = &cu->header;
21015 struct dwarf_block *blk;
21016 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21017 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21018
21019 *value = 0;
21020 *bytes = NULL;
21021 *baton = NULL;
21022
21023 switch (attr->form)
21024 {
21025 case DW_FORM_addr:
21026 case DW_FORM_addrx:
21027 case DW_FORM_GNU_addr_index:
21028 {
21029 gdb_byte *data;
21030
21031 if (TYPE_LENGTH (type) != cu_header->addr_size)
21032 dwarf2_const_value_length_mismatch_complaint (name,
21033 cu_header->addr_size,
21034 TYPE_LENGTH (type));
21035 /* Symbols of this form are reasonably rare, so we just
21036 piggyback on the existing location code rather than writing
21037 a new implementation of symbol_computed_ops. */
21038 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21039 (*baton)->per_cu = cu->per_cu;
21040 gdb_assert ((*baton)->per_cu);
21041
21042 (*baton)->size = 2 + cu_header->addr_size;
21043 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21044 (*baton)->data = data;
21045
21046 data[0] = DW_OP_addr;
21047 store_unsigned_integer (&data[1], cu_header->addr_size,
21048 byte_order, DW_ADDR (attr));
21049 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21050 }
21051 break;
21052 case DW_FORM_string:
21053 case DW_FORM_strp:
21054 case DW_FORM_strx:
21055 case DW_FORM_GNU_str_index:
21056 case DW_FORM_GNU_strp_alt:
21057 /* DW_STRING is already allocated on the objfile obstack, point
21058 directly to it. */
21059 *bytes = (const gdb_byte *) DW_STRING (attr);
21060 break;
21061 case DW_FORM_block1:
21062 case DW_FORM_block2:
21063 case DW_FORM_block4:
21064 case DW_FORM_block:
21065 case DW_FORM_exprloc:
21066 case DW_FORM_data16:
21067 blk = DW_BLOCK (attr);
21068 if (TYPE_LENGTH (type) != blk->size)
21069 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21070 TYPE_LENGTH (type));
21071 *bytes = blk->data;
21072 break;
21073
21074 /* The DW_AT_const_value attributes are supposed to carry the
21075 symbol's value "represented as it would be on the target
21076 architecture." By the time we get here, it's already been
21077 converted to host endianness, so we just need to sign- or
21078 zero-extend it as appropriate. */
21079 case DW_FORM_data1:
21080 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21081 break;
21082 case DW_FORM_data2:
21083 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21084 break;
21085 case DW_FORM_data4:
21086 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21087 break;
21088 case DW_FORM_data8:
21089 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21090 break;
21091
21092 case DW_FORM_sdata:
21093 case DW_FORM_implicit_const:
21094 *value = DW_SND (attr);
21095 break;
21096
21097 case DW_FORM_udata:
21098 *value = DW_UNSND (attr);
21099 break;
21100
21101 default:
21102 complaint (_("unsupported const value attribute form: '%s'"),
21103 dwarf_form_name (attr->form));
21104 *value = 0;
21105 break;
21106 }
21107 }
21108
21109
21110 /* Copy constant value from an attribute to a symbol. */
21111
21112 static void
21113 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21114 struct dwarf2_cu *cu)
21115 {
21116 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21117 LONGEST value;
21118 const gdb_byte *bytes;
21119 struct dwarf2_locexpr_baton *baton;
21120
21121 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21122 sym->print_name (),
21123 &objfile->objfile_obstack, cu,
21124 &value, &bytes, &baton);
21125
21126 if (baton != NULL)
21127 {
21128 SYMBOL_LOCATION_BATON (sym) = baton;
21129 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21130 }
21131 else if (bytes != NULL)
21132 {
21133 SYMBOL_VALUE_BYTES (sym) = bytes;
21134 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21135 }
21136 else
21137 {
21138 SYMBOL_VALUE (sym) = value;
21139 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21140 }
21141 }
21142
21143 /* Return the type of the die in question using its DW_AT_type attribute. */
21144
21145 static struct type *
21146 die_type (struct die_info *die, struct dwarf2_cu *cu)
21147 {
21148 struct attribute *type_attr;
21149
21150 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21151 if (!type_attr)
21152 {
21153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21154 /* A missing DW_AT_type represents a void type. */
21155 return objfile_type (objfile)->builtin_void;
21156 }
21157
21158 return lookup_die_type (die, type_attr, cu);
21159 }
21160
21161 /* True iff CU's producer generates GNAT Ada auxiliary information
21162 that allows to find parallel types through that information instead
21163 of having to do expensive parallel lookups by type name. */
21164
21165 static int
21166 need_gnat_info (struct dwarf2_cu *cu)
21167 {
21168 /* Assume that the Ada compiler was GNAT, which always produces
21169 the auxiliary information. */
21170 return (cu->language == language_ada);
21171 }
21172
21173 /* Return the auxiliary type of the die in question using its
21174 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21175 attribute is not present. */
21176
21177 static struct type *
21178 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21179 {
21180 struct attribute *type_attr;
21181
21182 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21183 if (!type_attr)
21184 return NULL;
21185
21186 return lookup_die_type (die, type_attr, cu);
21187 }
21188
21189 /* If DIE has a descriptive_type attribute, then set the TYPE's
21190 descriptive type accordingly. */
21191
21192 static void
21193 set_descriptive_type (struct type *type, struct die_info *die,
21194 struct dwarf2_cu *cu)
21195 {
21196 struct type *descriptive_type = die_descriptive_type (die, cu);
21197
21198 if (descriptive_type)
21199 {
21200 ALLOCATE_GNAT_AUX_TYPE (type);
21201 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21202 }
21203 }
21204
21205 /* Return the containing type of the die in question using its
21206 DW_AT_containing_type attribute. */
21207
21208 static struct type *
21209 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21210 {
21211 struct attribute *type_attr;
21212 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21213
21214 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21215 if (!type_attr)
21216 error (_("Dwarf Error: Problem turning containing type into gdb type "
21217 "[in module %s]"), objfile_name (objfile));
21218
21219 return lookup_die_type (die, type_attr, cu);
21220 }
21221
21222 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21223
21224 static struct type *
21225 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21226 {
21227 struct dwarf2_per_objfile *dwarf2_per_objfile
21228 = cu->per_cu->dwarf2_per_objfile;
21229 struct objfile *objfile = dwarf2_per_objfile->objfile;
21230 char *saved;
21231
21232 std::string message
21233 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21234 objfile_name (objfile),
21235 sect_offset_str (cu->header.sect_off),
21236 sect_offset_str (die->sect_off));
21237 saved = obstack_strdup (&objfile->objfile_obstack, message);
21238
21239 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21240 }
21241
21242 /* Look up the type of DIE in CU using its type attribute ATTR.
21243 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21244 DW_AT_containing_type.
21245 If there is no type substitute an error marker. */
21246
21247 static struct type *
21248 lookup_die_type (struct die_info *die, const struct attribute *attr,
21249 struct dwarf2_cu *cu)
21250 {
21251 struct dwarf2_per_objfile *dwarf2_per_objfile
21252 = cu->per_cu->dwarf2_per_objfile;
21253 struct objfile *objfile = dwarf2_per_objfile->objfile;
21254 struct type *this_type;
21255
21256 gdb_assert (attr->name == DW_AT_type
21257 || attr->name == DW_AT_GNAT_descriptive_type
21258 || attr->name == DW_AT_containing_type);
21259
21260 /* First see if we have it cached. */
21261
21262 if (attr->form == DW_FORM_GNU_ref_alt)
21263 {
21264 struct dwarf2_per_cu_data *per_cu;
21265 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21266
21267 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21268 dwarf2_per_objfile);
21269 this_type = get_die_type_at_offset (sect_off, per_cu);
21270 }
21271 else if (attr->form_is_ref ())
21272 {
21273 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21274
21275 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21276 }
21277 else if (attr->form == DW_FORM_ref_sig8)
21278 {
21279 ULONGEST signature = DW_SIGNATURE (attr);
21280
21281 return get_signatured_type (die, signature, cu);
21282 }
21283 else
21284 {
21285 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21286 " at %s [in module %s]"),
21287 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21288 objfile_name (objfile));
21289 return build_error_marker_type (cu, die);
21290 }
21291
21292 /* If not cached we need to read it in. */
21293
21294 if (this_type == NULL)
21295 {
21296 struct die_info *type_die = NULL;
21297 struct dwarf2_cu *type_cu = cu;
21298
21299 if (attr->form_is_ref ())
21300 type_die = follow_die_ref (die, attr, &type_cu);
21301 if (type_die == NULL)
21302 return build_error_marker_type (cu, die);
21303 /* If we find the type now, it's probably because the type came
21304 from an inter-CU reference and the type's CU got expanded before
21305 ours. */
21306 this_type = read_type_die (type_die, type_cu);
21307 }
21308
21309 /* If we still don't have a type use an error marker. */
21310
21311 if (this_type == NULL)
21312 return build_error_marker_type (cu, die);
21313
21314 return this_type;
21315 }
21316
21317 /* Return the type in DIE, CU.
21318 Returns NULL for invalid types.
21319
21320 This first does a lookup in die_type_hash,
21321 and only reads the die in if necessary.
21322
21323 NOTE: This can be called when reading in partial or full symbols. */
21324
21325 static struct type *
21326 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21327 {
21328 struct type *this_type;
21329
21330 this_type = get_die_type (die, cu);
21331 if (this_type)
21332 return this_type;
21333
21334 return read_type_die_1 (die, cu);
21335 }
21336
21337 /* Read the type in DIE, CU.
21338 Returns NULL for invalid types. */
21339
21340 static struct type *
21341 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21342 {
21343 struct type *this_type = NULL;
21344
21345 switch (die->tag)
21346 {
21347 case DW_TAG_class_type:
21348 case DW_TAG_interface_type:
21349 case DW_TAG_structure_type:
21350 case DW_TAG_union_type:
21351 this_type = read_structure_type (die, cu);
21352 break;
21353 case DW_TAG_enumeration_type:
21354 this_type = read_enumeration_type (die, cu);
21355 break;
21356 case DW_TAG_subprogram:
21357 case DW_TAG_subroutine_type:
21358 case DW_TAG_inlined_subroutine:
21359 this_type = read_subroutine_type (die, cu);
21360 break;
21361 case DW_TAG_array_type:
21362 this_type = read_array_type (die, cu);
21363 break;
21364 case DW_TAG_set_type:
21365 this_type = read_set_type (die, cu);
21366 break;
21367 case DW_TAG_pointer_type:
21368 this_type = read_tag_pointer_type (die, cu);
21369 break;
21370 case DW_TAG_ptr_to_member_type:
21371 this_type = read_tag_ptr_to_member_type (die, cu);
21372 break;
21373 case DW_TAG_reference_type:
21374 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21375 break;
21376 case DW_TAG_rvalue_reference_type:
21377 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21378 break;
21379 case DW_TAG_const_type:
21380 this_type = read_tag_const_type (die, cu);
21381 break;
21382 case DW_TAG_volatile_type:
21383 this_type = read_tag_volatile_type (die, cu);
21384 break;
21385 case DW_TAG_restrict_type:
21386 this_type = read_tag_restrict_type (die, cu);
21387 break;
21388 case DW_TAG_string_type:
21389 this_type = read_tag_string_type (die, cu);
21390 break;
21391 case DW_TAG_typedef:
21392 this_type = read_typedef (die, cu);
21393 break;
21394 case DW_TAG_subrange_type:
21395 this_type = read_subrange_type (die, cu);
21396 break;
21397 case DW_TAG_base_type:
21398 this_type = read_base_type (die, cu);
21399 break;
21400 case DW_TAG_unspecified_type:
21401 this_type = read_unspecified_type (die, cu);
21402 break;
21403 case DW_TAG_namespace:
21404 this_type = read_namespace_type (die, cu);
21405 break;
21406 case DW_TAG_module:
21407 this_type = read_module_type (die, cu);
21408 break;
21409 case DW_TAG_atomic_type:
21410 this_type = read_tag_atomic_type (die, cu);
21411 break;
21412 default:
21413 complaint (_("unexpected tag in read_type_die: '%s'"),
21414 dwarf_tag_name (die->tag));
21415 break;
21416 }
21417
21418 return this_type;
21419 }
21420
21421 /* See if we can figure out if the class lives in a namespace. We do
21422 this by looking for a member function; its demangled name will
21423 contain namespace info, if there is any.
21424 Return the computed name or NULL.
21425 Space for the result is allocated on the objfile's obstack.
21426 This is the full-die version of guess_partial_die_structure_name.
21427 In this case we know DIE has no useful parent. */
21428
21429 static const char *
21430 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21431 {
21432 struct die_info *spec_die;
21433 struct dwarf2_cu *spec_cu;
21434 struct die_info *child;
21435 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21436
21437 spec_cu = cu;
21438 spec_die = die_specification (die, &spec_cu);
21439 if (spec_die != NULL)
21440 {
21441 die = spec_die;
21442 cu = spec_cu;
21443 }
21444
21445 for (child = die->child;
21446 child != NULL;
21447 child = child->sibling)
21448 {
21449 if (child->tag == DW_TAG_subprogram)
21450 {
21451 const char *linkage_name = dw2_linkage_name (child, cu);
21452
21453 if (linkage_name != NULL)
21454 {
21455 gdb::unique_xmalloc_ptr<char> actual_name
21456 (language_class_name_from_physname (cu->language_defn,
21457 linkage_name));
21458 const char *name = NULL;
21459
21460 if (actual_name != NULL)
21461 {
21462 const char *die_name = dwarf2_name (die, cu);
21463
21464 if (die_name != NULL
21465 && strcmp (die_name, actual_name.get ()) != 0)
21466 {
21467 /* Strip off the class name from the full name.
21468 We want the prefix. */
21469 int die_name_len = strlen (die_name);
21470 int actual_name_len = strlen (actual_name.get ());
21471 const char *ptr = actual_name.get ();
21472
21473 /* Test for '::' as a sanity check. */
21474 if (actual_name_len > die_name_len + 2
21475 && ptr[actual_name_len - die_name_len - 1] == ':')
21476 name = obstack_strndup (
21477 &objfile->per_bfd->storage_obstack,
21478 ptr, actual_name_len - die_name_len - 2);
21479 }
21480 }
21481 return name;
21482 }
21483 }
21484 }
21485
21486 return NULL;
21487 }
21488
21489 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21490 prefix part in such case. See
21491 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21492
21493 static const char *
21494 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21495 {
21496 struct attribute *attr;
21497 const char *base;
21498
21499 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21500 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21501 return NULL;
21502
21503 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21504 return NULL;
21505
21506 attr = dw2_linkage_name_attr (die, cu);
21507 if (attr == NULL || DW_STRING (attr) == NULL)
21508 return NULL;
21509
21510 /* dwarf2_name had to be already called. */
21511 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21512
21513 /* Strip the base name, keep any leading namespaces/classes. */
21514 base = strrchr (DW_STRING (attr), ':');
21515 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21516 return "";
21517
21518 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21519 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21520 DW_STRING (attr),
21521 &base[-1] - DW_STRING (attr));
21522 }
21523
21524 /* Return the name of the namespace/class that DIE is defined within,
21525 or "" if we can't tell. The caller should not xfree the result.
21526
21527 For example, if we're within the method foo() in the following
21528 code:
21529
21530 namespace N {
21531 class C {
21532 void foo () {
21533 }
21534 };
21535 }
21536
21537 then determine_prefix on foo's die will return "N::C". */
21538
21539 static const char *
21540 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21541 {
21542 struct dwarf2_per_objfile *dwarf2_per_objfile
21543 = cu->per_cu->dwarf2_per_objfile;
21544 struct die_info *parent, *spec_die;
21545 struct dwarf2_cu *spec_cu;
21546 struct type *parent_type;
21547 const char *retval;
21548
21549 if (cu->language != language_cplus
21550 && cu->language != language_fortran && cu->language != language_d
21551 && cu->language != language_rust)
21552 return "";
21553
21554 retval = anonymous_struct_prefix (die, cu);
21555 if (retval)
21556 return retval;
21557
21558 /* We have to be careful in the presence of DW_AT_specification.
21559 For example, with GCC 3.4, given the code
21560
21561 namespace N {
21562 void foo() {
21563 // Definition of N::foo.
21564 }
21565 }
21566
21567 then we'll have a tree of DIEs like this:
21568
21569 1: DW_TAG_compile_unit
21570 2: DW_TAG_namespace // N
21571 3: DW_TAG_subprogram // declaration of N::foo
21572 4: DW_TAG_subprogram // definition of N::foo
21573 DW_AT_specification // refers to die #3
21574
21575 Thus, when processing die #4, we have to pretend that we're in
21576 the context of its DW_AT_specification, namely the contex of die
21577 #3. */
21578 spec_cu = cu;
21579 spec_die = die_specification (die, &spec_cu);
21580 if (spec_die == NULL)
21581 parent = die->parent;
21582 else
21583 {
21584 parent = spec_die->parent;
21585 cu = spec_cu;
21586 }
21587
21588 if (parent == NULL)
21589 return "";
21590 else if (parent->building_fullname)
21591 {
21592 const char *name;
21593 const char *parent_name;
21594
21595 /* It has been seen on RealView 2.2 built binaries,
21596 DW_TAG_template_type_param types actually _defined_ as
21597 children of the parent class:
21598
21599 enum E {};
21600 template class <class Enum> Class{};
21601 Class<enum E> class_e;
21602
21603 1: DW_TAG_class_type (Class)
21604 2: DW_TAG_enumeration_type (E)
21605 3: DW_TAG_enumerator (enum1:0)
21606 3: DW_TAG_enumerator (enum2:1)
21607 ...
21608 2: DW_TAG_template_type_param
21609 DW_AT_type DW_FORM_ref_udata (E)
21610
21611 Besides being broken debug info, it can put GDB into an
21612 infinite loop. Consider:
21613
21614 When we're building the full name for Class<E>, we'll start
21615 at Class, and go look over its template type parameters,
21616 finding E. We'll then try to build the full name of E, and
21617 reach here. We're now trying to build the full name of E,
21618 and look over the parent DIE for containing scope. In the
21619 broken case, if we followed the parent DIE of E, we'd again
21620 find Class, and once again go look at its template type
21621 arguments, etc., etc. Simply don't consider such parent die
21622 as source-level parent of this die (it can't be, the language
21623 doesn't allow it), and break the loop here. */
21624 name = dwarf2_name (die, cu);
21625 parent_name = dwarf2_name (parent, cu);
21626 complaint (_("template param type '%s' defined within parent '%s'"),
21627 name ? name : "<unknown>",
21628 parent_name ? parent_name : "<unknown>");
21629 return "";
21630 }
21631 else
21632 switch (parent->tag)
21633 {
21634 case DW_TAG_namespace:
21635 parent_type = read_type_die (parent, cu);
21636 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21637 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21638 Work around this problem here. */
21639 if (cu->language == language_cplus
21640 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21641 return "";
21642 /* We give a name to even anonymous namespaces. */
21643 return TYPE_NAME (parent_type);
21644 case DW_TAG_class_type:
21645 case DW_TAG_interface_type:
21646 case DW_TAG_structure_type:
21647 case DW_TAG_union_type:
21648 case DW_TAG_module:
21649 parent_type = read_type_die (parent, cu);
21650 if (TYPE_NAME (parent_type) != NULL)
21651 return TYPE_NAME (parent_type);
21652 else
21653 /* An anonymous structure is only allowed non-static data
21654 members; no typedefs, no member functions, et cetera.
21655 So it does not need a prefix. */
21656 return "";
21657 case DW_TAG_compile_unit:
21658 case DW_TAG_partial_unit:
21659 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21660 if (cu->language == language_cplus
21661 && !dwarf2_per_objfile->types.empty ()
21662 && die->child != NULL
21663 && (die->tag == DW_TAG_class_type
21664 || die->tag == DW_TAG_structure_type
21665 || die->tag == DW_TAG_union_type))
21666 {
21667 const char *name = guess_full_die_structure_name (die, cu);
21668 if (name != NULL)
21669 return name;
21670 }
21671 return "";
21672 case DW_TAG_subprogram:
21673 /* Nested subroutines in Fortran get a prefix with the name
21674 of the parent's subroutine. */
21675 if (cu->language == language_fortran)
21676 {
21677 if ((die->tag == DW_TAG_subprogram)
21678 && (dwarf2_name (parent, cu) != NULL))
21679 return dwarf2_name (parent, cu);
21680 }
21681 return determine_prefix (parent, cu);
21682 case DW_TAG_enumeration_type:
21683 parent_type = read_type_die (parent, cu);
21684 if (TYPE_DECLARED_CLASS (parent_type))
21685 {
21686 if (TYPE_NAME (parent_type) != NULL)
21687 return TYPE_NAME (parent_type);
21688 return "";
21689 }
21690 /* Fall through. */
21691 default:
21692 return determine_prefix (parent, cu);
21693 }
21694 }
21695
21696 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21697 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21698 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21699 an obconcat, otherwise allocate storage for the result. The CU argument is
21700 used to determine the language and hence, the appropriate separator. */
21701
21702 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21703
21704 static char *
21705 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21706 int physname, struct dwarf2_cu *cu)
21707 {
21708 const char *lead = "";
21709 const char *sep;
21710
21711 if (suffix == NULL || suffix[0] == '\0'
21712 || prefix == NULL || prefix[0] == '\0')
21713 sep = "";
21714 else if (cu->language == language_d)
21715 {
21716 /* For D, the 'main' function could be defined in any module, but it
21717 should never be prefixed. */
21718 if (strcmp (suffix, "D main") == 0)
21719 {
21720 prefix = "";
21721 sep = "";
21722 }
21723 else
21724 sep = ".";
21725 }
21726 else if (cu->language == language_fortran && physname)
21727 {
21728 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21729 DW_AT_MIPS_linkage_name is preferred and used instead. */
21730
21731 lead = "__";
21732 sep = "_MOD_";
21733 }
21734 else
21735 sep = "::";
21736
21737 if (prefix == NULL)
21738 prefix = "";
21739 if (suffix == NULL)
21740 suffix = "";
21741
21742 if (obs == NULL)
21743 {
21744 char *retval
21745 = ((char *)
21746 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21747
21748 strcpy (retval, lead);
21749 strcat (retval, prefix);
21750 strcat (retval, sep);
21751 strcat (retval, suffix);
21752 return retval;
21753 }
21754 else
21755 {
21756 /* We have an obstack. */
21757 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21758 }
21759 }
21760
21761 /* Return sibling of die, NULL if no sibling. */
21762
21763 static struct die_info *
21764 sibling_die (struct die_info *die)
21765 {
21766 return die->sibling;
21767 }
21768
21769 /* Get name of a die, return NULL if not found. */
21770
21771 static const char *
21772 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21773 struct obstack *obstack)
21774 {
21775 if (name && cu->language == language_cplus)
21776 {
21777 std::string canon_name = cp_canonicalize_string (name);
21778
21779 if (!canon_name.empty ())
21780 {
21781 if (canon_name != name)
21782 name = obstack_strdup (obstack, canon_name);
21783 }
21784 }
21785
21786 return name;
21787 }
21788
21789 /* Get name of a die, return NULL if not found.
21790 Anonymous namespaces are converted to their magic string. */
21791
21792 static const char *
21793 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21794 {
21795 struct attribute *attr;
21796 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21797
21798 attr = dwarf2_attr (die, DW_AT_name, cu);
21799 if ((!attr || !DW_STRING (attr))
21800 && die->tag != DW_TAG_namespace
21801 && die->tag != DW_TAG_class_type
21802 && die->tag != DW_TAG_interface_type
21803 && die->tag != DW_TAG_structure_type
21804 && die->tag != DW_TAG_union_type)
21805 return NULL;
21806
21807 switch (die->tag)
21808 {
21809 case DW_TAG_compile_unit:
21810 case DW_TAG_partial_unit:
21811 /* Compilation units have a DW_AT_name that is a filename, not
21812 a source language identifier. */
21813 case DW_TAG_enumeration_type:
21814 case DW_TAG_enumerator:
21815 /* These tags always have simple identifiers already; no need
21816 to canonicalize them. */
21817 return DW_STRING (attr);
21818
21819 case DW_TAG_namespace:
21820 if (attr != NULL && DW_STRING (attr) != NULL)
21821 return DW_STRING (attr);
21822 return CP_ANONYMOUS_NAMESPACE_STR;
21823
21824 case DW_TAG_class_type:
21825 case DW_TAG_interface_type:
21826 case DW_TAG_structure_type:
21827 case DW_TAG_union_type:
21828 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21829 structures or unions. These were of the form "._%d" in GCC 4.1,
21830 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21831 and GCC 4.4. We work around this problem by ignoring these. */
21832 if (attr && DW_STRING (attr)
21833 && (startswith (DW_STRING (attr), "._")
21834 || startswith (DW_STRING (attr), "<anonymous")))
21835 return NULL;
21836
21837 /* GCC might emit a nameless typedef that has a linkage name. See
21838 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21839 if (!attr || DW_STRING (attr) == NULL)
21840 {
21841 attr = dw2_linkage_name_attr (die, cu);
21842 if (attr == NULL || DW_STRING (attr) == NULL)
21843 return NULL;
21844
21845 /* Avoid demangling DW_STRING (attr) the second time on a second
21846 call for the same DIE. */
21847 if (!DW_STRING_IS_CANONICAL (attr))
21848 {
21849 gdb::unique_xmalloc_ptr<char> demangled
21850 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21851
21852 const char *base;
21853
21854 /* FIXME: we already did this for the partial symbol... */
21855 DW_STRING (attr)
21856 = obstack_strdup (&objfile->per_bfd->storage_obstack,
21857 demangled.get ());
21858 DW_STRING_IS_CANONICAL (attr) = 1;
21859
21860 /* Strip any leading namespaces/classes, keep only the base name.
21861 DW_AT_name for named DIEs does not contain the prefixes. */
21862 base = strrchr (DW_STRING (attr), ':');
21863 if (base && base > DW_STRING (attr) && base[-1] == ':')
21864 return &base[1];
21865 else
21866 return DW_STRING (attr);
21867 }
21868 }
21869 break;
21870
21871 default:
21872 break;
21873 }
21874
21875 if (!DW_STRING_IS_CANONICAL (attr))
21876 {
21877 DW_STRING (attr)
21878 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21879 &objfile->per_bfd->storage_obstack);
21880 DW_STRING_IS_CANONICAL (attr) = 1;
21881 }
21882 return DW_STRING (attr);
21883 }
21884
21885 /* Return the die that this die in an extension of, or NULL if there
21886 is none. *EXT_CU is the CU containing DIE on input, and the CU
21887 containing the return value on output. */
21888
21889 static struct die_info *
21890 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21891 {
21892 struct attribute *attr;
21893
21894 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21895 if (attr == NULL)
21896 return NULL;
21897
21898 return follow_die_ref (die, attr, ext_cu);
21899 }
21900
21901 /* A convenience function that returns an "unknown" DWARF name,
21902 including the value of V. STR is the name of the entity being
21903 printed, e.g., "TAG". */
21904
21905 static const char *
21906 dwarf_unknown (const char *str, unsigned v)
21907 {
21908 char *cell = get_print_cell ();
21909 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21910 return cell;
21911 }
21912
21913 /* Convert a DIE tag into its string name. */
21914
21915 static const char *
21916 dwarf_tag_name (unsigned tag)
21917 {
21918 const char *name = get_DW_TAG_name (tag);
21919
21920 if (name == NULL)
21921 return dwarf_unknown ("TAG", tag);
21922
21923 return name;
21924 }
21925
21926 /* Convert a DWARF attribute code into its string name. */
21927
21928 static const char *
21929 dwarf_attr_name (unsigned attr)
21930 {
21931 const char *name;
21932
21933 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21934 if (attr == DW_AT_MIPS_fde)
21935 return "DW_AT_MIPS_fde";
21936 #else
21937 if (attr == DW_AT_HP_block_index)
21938 return "DW_AT_HP_block_index";
21939 #endif
21940
21941 name = get_DW_AT_name (attr);
21942
21943 if (name == NULL)
21944 return dwarf_unknown ("AT", attr);
21945
21946 return name;
21947 }
21948
21949 /* Convert a DWARF value form code into its string name. */
21950
21951 static const char *
21952 dwarf_form_name (unsigned form)
21953 {
21954 const char *name = get_DW_FORM_name (form);
21955
21956 if (name == NULL)
21957 return dwarf_unknown ("FORM", form);
21958
21959 return name;
21960 }
21961
21962 static const char *
21963 dwarf_bool_name (unsigned mybool)
21964 {
21965 if (mybool)
21966 return "TRUE";
21967 else
21968 return "FALSE";
21969 }
21970
21971 /* Convert a DWARF type code into its string name. */
21972
21973 static const char *
21974 dwarf_type_encoding_name (unsigned enc)
21975 {
21976 const char *name = get_DW_ATE_name (enc);
21977
21978 if (name == NULL)
21979 return dwarf_unknown ("ATE", enc);
21980
21981 return name;
21982 }
21983
21984 static void
21985 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21986 {
21987 unsigned int i;
21988
21989 print_spaces (indent, f);
21990 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21991 dwarf_tag_name (die->tag), die->abbrev,
21992 sect_offset_str (die->sect_off));
21993
21994 if (die->parent != NULL)
21995 {
21996 print_spaces (indent, f);
21997 fprintf_unfiltered (f, " parent at offset: %s\n",
21998 sect_offset_str (die->parent->sect_off));
21999 }
22000
22001 print_spaces (indent, f);
22002 fprintf_unfiltered (f, " has children: %s\n",
22003 dwarf_bool_name (die->child != NULL));
22004
22005 print_spaces (indent, f);
22006 fprintf_unfiltered (f, " attributes:\n");
22007
22008 for (i = 0; i < die->num_attrs; ++i)
22009 {
22010 print_spaces (indent, f);
22011 fprintf_unfiltered (f, " %s (%s) ",
22012 dwarf_attr_name (die->attrs[i].name),
22013 dwarf_form_name (die->attrs[i].form));
22014
22015 switch (die->attrs[i].form)
22016 {
22017 case DW_FORM_addr:
22018 case DW_FORM_addrx:
22019 case DW_FORM_GNU_addr_index:
22020 fprintf_unfiltered (f, "address: ");
22021 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22022 break;
22023 case DW_FORM_block2:
22024 case DW_FORM_block4:
22025 case DW_FORM_block:
22026 case DW_FORM_block1:
22027 fprintf_unfiltered (f, "block: size %s",
22028 pulongest (DW_BLOCK (&die->attrs[i])->size));
22029 break;
22030 case DW_FORM_exprloc:
22031 fprintf_unfiltered (f, "expression: size %s",
22032 pulongest (DW_BLOCK (&die->attrs[i])->size));
22033 break;
22034 case DW_FORM_data16:
22035 fprintf_unfiltered (f, "constant of 16 bytes");
22036 break;
22037 case DW_FORM_ref_addr:
22038 fprintf_unfiltered (f, "ref address: ");
22039 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22040 break;
22041 case DW_FORM_GNU_ref_alt:
22042 fprintf_unfiltered (f, "alt ref address: ");
22043 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22044 break;
22045 case DW_FORM_ref1:
22046 case DW_FORM_ref2:
22047 case DW_FORM_ref4:
22048 case DW_FORM_ref8:
22049 case DW_FORM_ref_udata:
22050 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22051 (long) (DW_UNSND (&die->attrs[i])));
22052 break;
22053 case DW_FORM_data1:
22054 case DW_FORM_data2:
22055 case DW_FORM_data4:
22056 case DW_FORM_data8:
22057 case DW_FORM_udata:
22058 case DW_FORM_sdata:
22059 fprintf_unfiltered (f, "constant: %s",
22060 pulongest (DW_UNSND (&die->attrs[i])));
22061 break;
22062 case DW_FORM_sec_offset:
22063 fprintf_unfiltered (f, "section offset: %s",
22064 pulongest (DW_UNSND (&die->attrs[i])));
22065 break;
22066 case DW_FORM_ref_sig8:
22067 fprintf_unfiltered (f, "signature: %s",
22068 hex_string (DW_SIGNATURE (&die->attrs[i])));
22069 break;
22070 case DW_FORM_string:
22071 case DW_FORM_strp:
22072 case DW_FORM_line_strp:
22073 case DW_FORM_strx:
22074 case DW_FORM_GNU_str_index:
22075 case DW_FORM_GNU_strp_alt:
22076 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22077 DW_STRING (&die->attrs[i])
22078 ? DW_STRING (&die->attrs[i]) : "",
22079 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22080 break;
22081 case DW_FORM_flag:
22082 if (DW_UNSND (&die->attrs[i]))
22083 fprintf_unfiltered (f, "flag: TRUE");
22084 else
22085 fprintf_unfiltered (f, "flag: FALSE");
22086 break;
22087 case DW_FORM_flag_present:
22088 fprintf_unfiltered (f, "flag: TRUE");
22089 break;
22090 case DW_FORM_indirect:
22091 /* The reader will have reduced the indirect form to
22092 the "base form" so this form should not occur. */
22093 fprintf_unfiltered (f,
22094 "unexpected attribute form: DW_FORM_indirect");
22095 break;
22096 case DW_FORM_implicit_const:
22097 fprintf_unfiltered (f, "constant: %s",
22098 plongest (DW_SND (&die->attrs[i])));
22099 break;
22100 default:
22101 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22102 die->attrs[i].form);
22103 break;
22104 }
22105 fprintf_unfiltered (f, "\n");
22106 }
22107 }
22108
22109 static void
22110 dump_die_for_error (struct die_info *die)
22111 {
22112 dump_die_shallow (gdb_stderr, 0, die);
22113 }
22114
22115 static void
22116 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22117 {
22118 int indent = level * 4;
22119
22120 gdb_assert (die != NULL);
22121
22122 if (level >= max_level)
22123 return;
22124
22125 dump_die_shallow (f, indent, die);
22126
22127 if (die->child != NULL)
22128 {
22129 print_spaces (indent, f);
22130 fprintf_unfiltered (f, " Children:");
22131 if (level + 1 < max_level)
22132 {
22133 fprintf_unfiltered (f, "\n");
22134 dump_die_1 (f, level + 1, max_level, die->child);
22135 }
22136 else
22137 {
22138 fprintf_unfiltered (f,
22139 " [not printed, max nesting level reached]\n");
22140 }
22141 }
22142
22143 if (die->sibling != NULL && level > 0)
22144 {
22145 dump_die_1 (f, level, max_level, die->sibling);
22146 }
22147 }
22148
22149 /* This is called from the pdie macro in gdbinit.in.
22150 It's not static so gcc will keep a copy callable from gdb. */
22151
22152 void
22153 dump_die (struct die_info *die, int max_level)
22154 {
22155 dump_die_1 (gdb_stdlog, 0, max_level, die);
22156 }
22157
22158 static void
22159 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22160 {
22161 void **slot;
22162
22163 slot = htab_find_slot_with_hash (cu->die_hash, die,
22164 to_underlying (die->sect_off),
22165 INSERT);
22166
22167 *slot = die;
22168 }
22169
22170 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22171 required kind. */
22172
22173 static sect_offset
22174 dwarf2_get_ref_die_offset (const struct attribute *attr)
22175 {
22176 if (attr->form_is_ref ())
22177 return (sect_offset) DW_UNSND (attr);
22178
22179 complaint (_("unsupported die ref attribute form: '%s'"),
22180 dwarf_form_name (attr->form));
22181 return {};
22182 }
22183
22184 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22185 * the value held by the attribute is not constant. */
22186
22187 static LONGEST
22188 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22189 {
22190 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22191 return DW_SND (attr);
22192 else if (attr->form == DW_FORM_udata
22193 || attr->form == DW_FORM_data1
22194 || attr->form == DW_FORM_data2
22195 || attr->form == DW_FORM_data4
22196 || attr->form == DW_FORM_data8)
22197 return DW_UNSND (attr);
22198 else
22199 {
22200 /* For DW_FORM_data16 see attribute::form_is_constant. */
22201 complaint (_("Attribute value is not a constant (%s)"),
22202 dwarf_form_name (attr->form));
22203 return default_value;
22204 }
22205 }
22206
22207 /* Follow reference or signature attribute ATTR of SRC_DIE.
22208 On entry *REF_CU is the CU of SRC_DIE.
22209 On exit *REF_CU is the CU of the result. */
22210
22211 static struct die_info *
22212 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22213 struct dwarf2_cu **ref_cu)
22214 {
22215 struct die_info *die;
22216
22217 if (attr->form_is_ref ())
22218 die = follow_die_ref (src_die, attr, ref_cu);
22219 else if (attr->form == DW_FORM_ref_sig8)
22220 die = follow_die_sig (src_die, attr, ref_cu);
22221 else
22222 {
22223 dump_die_for_error (src_die);
22224 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22225 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22226 }
22227
22228 return die;
22229 }
22230
22231 /* Follow reference OFFSET.
22232 On entry *REF_CU is the CU of the source die referencing OFFSET.
22233 On exit *REF_CU is the CU of the result.
22234 Returns NULL if OFFSET is invalid. */
22235
22236 static struct die_info *
22237 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22238 struct dwarf2_cu **ref_cu)
22239 {
22240 struct die_info temp_die;
22241 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22242 struct dwarf2_per_objfile *dwarf2_per_objfile
22243 = cu->per_cu->dwarf2_per_objfile;
22244
22245 gdb_assert (cu->per_cu != NULL);
22246
22247 target_cu = cu;
22248
22249 if (cu->per_cu->is_debug_types)
22250 {
22251 /* .debug_types CUs cannot reference anything outside their CU.
22252 If they need to, they have to reference a signatured type via
22253 DW_FORM_ref_sig8. */
22254 if (!cu->header.offset_in_cu_p (sect_off))
22255 return NULL;
22256 }
22257 else if (offset_in_dwz != cu->per_cu->is_dwz
22258 || !cu->header.offset_in_cu_p (sect_off))
22259 {
22260 struct dwarf2_per_cu_data *per_cu;
22261
22262 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22263 dwarf2_per_objfile);
22264
22265 /* If necessary, add it to the queue and load its DIEs. */
22266 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22267 load_full_comp_unit (per_cu, false, cu->language);
22268
22269 target_cu = per_cu->cu;
22270 }
22271 else if (cu->dies == NULL)
22272 {
22273 /* We're loading full DIEs during partial symbol reading. */
22274 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22275 load_full_comp_unit (cu->per_cu, false, language_minimal);
22276 }
22277
22278 *ref_cu = target_cu;
22279 temp_die.sect_off = sect_off;
22280
22281 if (target_cu != cu)
22282 target_cu->ancestor = cu;
22283
22284 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22285 &temp_die,
22286 to_underlying (sect_off));
22287 }
22288
22289 /* Follow reference attribute ATTR of SRC_DIE.
22290 On entry *REF_CU is the CU of SRC_DIE.
22291 On exit *REF_CU is the CU of the result. */
22292
22293 static struct die_info *
22294 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22295 struct dwarf2_cu **ref_cu)
22296 {
22297 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22298 struct dwarf2_cu *cu = *ref_cu;
22299 struct die_info *die;
22300
22301 die = follow_die_offset (sect_off,
22302 (attr->form == DW_FORM_GNU_ref_alt
22303 || cu->per_cu->is_dwz),
22304 ref_cu);
22305 if (!die)
22306 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22307 "at %s [in module %s]"),
22308 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22309 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22310
22311 return die;
22312 }
22313
22314 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22315 Returned value is intended for DW_OP_call*. Returned
22316 dwarf2_locexpr_baton->data has lifetime of
22317 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22318
22319 struct dwarf2_locexpr_baton
22320 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22321 struct dwarf2_per_cu_data *per_cu,
22322 CORE_ADDR (*get_frame_pc) (void *baton),
22323 void *baton, bool resolve_abstract_p)
22324 {
22325 struct dwarf2_cu *cu;
22326 struct die_info *die;
22327 struct attribute *attr;
22328 struct dwarf2_locexpr_baton retval;
22329 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22330 struct objfile *objfile = dwarf2_per_objfile->objfile;
22331
22332 if (per_cu->cu == NULL)
22333 load_cu (per_cu, false);
22334 cu = per_cu->cu;
22335 if (cu == NULL)
22336 {
22337 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22338 Instead just throw an error, not much else we can do. */
22339 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22340 sect_offset_str (sect_off), objfile_name (objfile));
22341 }
22342
22343 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22344 if (!die)
22345 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22346 sect_offset_str (sect_off), objfile_name (objfile));
22347
22348 attr = dwarf2_attr (die, DW_AT_location, cu);
22349 if (!attr && resolve_abstract_p
22350 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22351 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22352 {
22353 CORE_ADDR pc = (*get_frame_pc) (baton);
22354 CORE_ADDR baseaddr = objfile->text_section_offset ();
22355 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22356
22357 for (const auto &cand_off
22358 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22359 {
22360 struct dwarf2_cu *cand_cu = cu;
22361 struct die_info *cand
22362 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22363 if (!cand
22364 || !cand->parent
22365 || cand->parent->tag != DW_TAG_subprogram)
22366 continue;
22367
22368 CORE_ADDR pc_low, pc_high;
22369 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22370 if (pc_low == ((CORE_ADDR) -1))
22371 continue;
22372 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22373 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22374 if (!(pc_low <= pc && pc < pc_high))
22375 continue;
22376
22377 die = cand;
22378 attr = dwarf2_attr (die, DW_AT_location, cu);
22379 break;
22380 }
22381 }
22382
22383 if (!attr)
22384 {
22385 /* DWARF: "If there is no such attribute, then there is no effect.".
22386 DATA is ignored if SIZE is 0. */
22387
22388 retval.data = NULL;
22389 retval.size = 0;
22390 }
22391 else if (attr->form_is_section_offset ())
22392 {
22393 struct dwarf2_loclist_baton loclist_baton;
22394 CORE_ADDR pc = (*get_frame_pc) (baton);
22395 size_t size;
22396
22397 fill_in_loclist_baton (cu, &loclist_baton, attr);
22398
22399 retval.data = dwarf2_find_location_expression (&loclist_baton,
22400 &size, pc);
22401 retval.size = size;
22402 }
22403 else
22404 {
22405 if (!attr->form_is_block ())
22406 error (_("Dwarf Error: DIE at %s referenced in module %s "
22407 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22408 sect_offset_str (sect_off), objfile_name (objfile));
22409
22410 retval.data = DW_BLOCK (attr)->data;
22411 retval.size = DW_BLOCK (attr)->size;
22412 }
22413 retval.per_cu = cu->per_cu;
22414
22415 age_cached_comp_units (dwarf2_per_objfile);
22416
22417 return retval;
22418 }
22419
22420 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22421 offset. */
22422
22423 struct dwarf2_locexpr_baton
22424 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22425 struct dwarf2_per_cu_data *per_cu,
22426 CORE_ADDR (*get_frame_pc) (void *baton),
22427 void *baton)
22428 {
22429 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22430
22431 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22432 }
22433
22434 /* Write a constant of a given type as target-ordered bytes into
22435 OBSTACK. */
22436
22437 static const gdb_byte *
22438 write_constant_as_bytes (struct obstack *obstack,
22439 enum bfd_endian byte_order,
22440 struct type *type,
22441 ULONGEST value,
22442 LONGEST *len)
22443 {
22444 gdb_byte *result;
22445
22446 *len = TYPE_LENGTH (type);
22447 result = (gdb_byte *) obstack_alloc (obstack, *len);
22448 store_unsigned_integer (result, *len, byte_order, value);
22449
22450 return result;
22451 }
22452
22453 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22454 pointer to the constant bytes and set LEN to the length of the
22455 data. If memory is needed, allocate it on OBSTACK. If the DIE
22456 does not have a DW_AT_const_value, return NULL. */
22457
22458 const gdb_byte *
22459 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22460 struct dwarf2_per_cu_data *per_cu,
22461 struct obstack *obstack,
22462 LONGEST *len)
22463 {
22464 struct dwarf2_cu *cu;
22465 struct die_info *die;
22466 struct attribute *attr;
22467 const gdb_byte *result = NULL;
22468 struct type *type;
22469 LONGEST value;
22470 enum bfd_endian byte_order;
22471 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22472
22473 if (per_cu->cu == NULL)
22474 load_cu (per_cu, false);
22475 cu = per_cu->cu;
22476 if (cu == NULL)
22477 {
22478 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22479 Instead just throw an error, not much else we can do. */
22480 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22481 sect_offset_str (sect_off), objfile_name (objfile));
22482 }
22483
22484 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22485 if (!die)
22486 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22487 sect_offset_str (sect_off), objfile_name (objfile));
22488
22489 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22490 if (attr == NULL)
22491 return NULL;
22492
22493 byte_order = (bfd_big_endian (objfile->obfd)
22494 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22495
22496 switch (attr->form)
22497 {
22498 case DW_FORM_addr:
22499 case DW_FORM_addrx:
22500 case DW_FORM_GNU_addr_index:
22501 {
22502 gdb_byte *tem;
22503
22504 *len = cu->header.addr_size;
22505 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22506 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22507 result = tem;
22508 }
22509 break;
22510 case DW_FORM_string:
22511 case DW_FORM_strp:
22512 case DW_FORM_strx:
22513 case DW_FORM_GNU_str_index:
22514 case DW_FORM_GNU_strp_alt:
22515 /* DW_STRING is already allocated on the objfile obstack, point
22516 directly to it. */
22517 result = (const gdb_byte *) DW_STRING (attr);
22518 *len = strlen (DW_STRING (attr));
22519 break;
22520 case DW_FORM_block1:
22521 case DW_FORM_block2:
22522 case DW_FORM_block4:
22523 case DW_FORM_block:
22524 case DW_FORM_exprloc:
22525 case DW_FORM_data16:
22526 result = DW_BLOCK (attr)->data;
22527 *len = DW_BLOCK (attr)->size;
22528 break;
22529
22530 /* The DW_AT_const_value attributes are supposed to carry the
22531 symbol's value "represented as it would be on the target
22532 architecture." By the time we get here, it's already been
22533 converted to host endianness, so we just need to sign- or
22534 zero-extend it as appropriate. */
22535 case DW_FORM_data1:
22536 type = die_type (die, cu);
22537 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22538 if (result == NULL)
22539 result = write_constant_as_bytes (obstack, byte_order,
22540 type, value, len);
22541 break;
22542 case DW_FORM_data2:
22543 type = die_type (die, cu);
22544 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22545 if (result == NULL)
22546 result = write_constant_as_bytes (obstack, byte_order,
22547 type, value, len);
22548 break;
22549 case DW_FORM_data4:
22550 type = die_type (die, cu);
22551 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22552 if (result == NULL)
22553 result = write_constant_as_bytes (obstack, byte_order,
22554 type, value, len);
22555 break;
22556 case DW_FORM_data8:
22557 type = die_type (die, cu);
22558 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22559 if (result == NULL)
22560 result = write_constant_as_bytes (obstack, byte_order,
22561 type, value, len);
22562 break;
22563
22564 case DW_FORM_sdata:
22565 case DW_FORM_implicit_const:
22566 type = die_type (die, cu);
22567 result = write_constant_as_bytes (obstack, byte_order,
22568 type, DW_SND (attr), len);
22569 break;
22570
22571 case DW_FORM_udata:
22572 type = die_type (die, cu);
22573 result = write_constant_as_bytes (obstack, byte_order,
22574 type, DW_UNSND (attr), len);
22575 break;
22576
22577 default:
22578 complaint (_("unsupported const value attribute form: '%s'"),
22579 dwarf_form_name (attr->form));
22580 break;
22581 }
22582
22583 return result;
22584 }
22585
22586 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22587 valid type for this die is found. */
22588
22589 struct type *
22590 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22591 struct dwarf2_per_cu_data *per_cu)
22592 {
22593 struct dwarf2_cu *cu;
22594 struct die_info *die;
22595
22596 if (per_cu->cu == NULL)
22597 load_cu (per_cu, false);
22598 cu = per_cu->cu;
22599 if (!cu)
22600 return NULL;
22601
22602 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22603 if (!die)
22604 return NULL;
22605
22606 return die_type (die, cu);
22607 }
22608
22609 /* Return the type of the DIE at DIE_OFFSET in the CU named by
22610 PER_CU. */
22611
22612 struct type *
22613 dwarf2_get_die_type (cu_offset die_offset,
22614 struct dwarf2_per_cu_data *per_cu)
22615 {
22616 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22617 return get_die_type_at_offset (die_offset_sect, per_cu);
22618 }
22619
22620 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22621 On entry *REF_CU is the CU of SRC_DIE.
22622 On exit *REF_CU is the CU of the result.
22623 Returns NULL if the referenced DIE isn't found. */
22624
22625 static struct die_info *
22626 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22627 struct dwarf2_cu **ref_cu)
22628 {
22629 struct die_info temp_die;
22630 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22631 struct die_info *die;
22632
22633 /* While it might be nice to assert sig_type->type == NULL here,
22634 we can get here for DW_AT_imported_declaration where we need
22635 the DIE not the type. */
22636
22637 /* If necessary, add it to the queue and load its DIEs. */
22638
22639 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22640 read_signatured_type (sig_type);
22641
22642 sig_cu = sig_type->per_cu.cu;
22643 gdb_assert (sig_cu != NULL);
22644 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22645 temp_die.sect_off = sig_type->type_offset_in_section;
22646 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22647 to_underlying (temp_die.sect_off));
22648 if (die)
22649 {
22650 struct dwarf2_per_objfile *dwarf2_per_objfile
22651 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22652
22653 /* For .gdb_index version 7 keep track of included TUs.
22654 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22655 if (dwarf2_per_objfile->index_table != NULL
22656 && dwarf2_per_objfile->index_table->version <= 7)
22657 {
22658 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22659 }
22660
22661 *ref_cu = sig_cu;
22662 if (sig_cu != cu)
22663 sig_cu->ancestor = cu;
22664
22665 return die;
22666 }
22667
22668 return NULL;
22669 }
22670
22671 /* Follow signatured type referenced by ATTR in SRC_DIE.
22672 On entry *REF_CU is the CU of SRC_DIE.
22673 On exit *REF_CU is the CU of the result.
22674 The result is the DIE of the type.
22675 If the referenced type cannot be found an error is thrown. */
22676
22677 static struct die_info *
22678 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22679 struct dwarf2_cu **ref_cu)
22680 {
22681 ULONGEST signature = DW_SIGNATURE (attr);
22682 struct signatured_type *sig_type;
22683 struct die_info *die;
22684
22685 gdb_assert (attr->form == DW_FORM_ref_sig8);
22686
22687 sig_type = lookup_signatured_type (*ref_cu, signature);
22688 /* sig_type will be NULL if the signatured type is missing from
22689 the debug info. */
22690 if (sig_type == NULL)
22691 {
22692 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22693 " from DIE at %s [in module %s]"),
22694 hex_string (signature), sect_offset_str (src_die->sect_off),
22695 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22696 }
22697
22698 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22699 if (die == NULL)
22700 {
22701 dump_die_for_error (src_die);
22702 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22703 " from DIE at %s [in module %s]"),
22704 hex_string (signature), sect_offset_str (src_die->sect_off),
22705 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22706 }
22707
22708 return die;
22709 }
22710
22711 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22712 reading in and processing the type unit if necessary. */
22713
22714 static struct type *
22715 get_signatured_type (struct die_info *die, ULONGEST signature,
22716 struct dwarf2_cu *cu)
22717 {
22718 struct dwarf2_per_objfile *dwarf2_per_objfile
22719 = cu->per_cu->dwarf2_per_objfile;
22720 struct signatured_type *sig_type;
22721 struct dwarf2_cu *type_cu;
22722 struct die_info *type_die;
22723 struct type *type;
22724
22725 sig_type = lookup_signatured_type (cu, signature);
22726 /* sig_type will be NULL if the signatured type is missing from
22727 the debug info. */
22728 if (sig_type == NULL)
22729 {
22730 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22731 " from DIE at %s [in module %s]"),
22732 hex_string (signature), sect_offset_str (die->sect_off),
22733 objfile_name (dwarf2_per_objfile->objfile));
22734 return build_error_marker_type (cu, die);
22735 }
22736
22737 /* If we already know the type we're done. */
22738 if (sig_type->type != NULL)
22739 return sig_type->type;
22740
22741 type_cu = cu;
22742 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22743 if (type_die != NULL)
22744 {
22745 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22746 is created. This is important, for example, because for c++ classes
22747 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22748 type = read_type_die (type_die, type_cu);
22749 if (type == NULL)
22750 {
22751 complaint (_("Dwarf Error: Cannot build signatured type %s"
22752 " referenced from DIE at %s [in module %s]"),
22753 hex_string (signature), sect_offset_str (die->sect_off),
22754 objfile_name (dwarf2_per_objfile->objfile));
22755 type = build_error_marker_type (cu, die);
22756 }
22757 }
22758 else
22759 {
22760 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22761 " from DIE at %s [in module %s]"),
22762 hex_string (signature), sect_offset_str (die->sect_off),
22763 objfile_name (dwarf2_per_objfile->objfile));
22764 type = build_error_marker_type (cu, die);
22765 }
22766 sig_type->type = type;
22767
22768 return type;
22769 }
22770
22771 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22772 reading in and processing the type unit if necessary. */
22773
22774 static struct type *
22775 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22776 struct dwarf2_cu *cu) /* ARI: editCase function */
22777 {
22778 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22779 if (attr->form_is_ref ())
22780 {
22781 struct dwarf2_cu *type_cu = cu;
22782 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22783
22784 return read_type_die (type_die, type_cu);
22785 }
22786 else if (attr->form == DW_FORM_ref_sig8)
22787 {
22788 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22789 }
22790 else
22791 {
22792 struct dwarf2_per_objfile *dwarf2_per_objfile
22793 = cu->per_cu->dwarf2_per_objfile;
22794
22795 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22796 " at %s [in module %s]"),
22797 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22798 objfile_name (dwarf2_per_objfile->objfile));
22799 return build_error_marker_type (cu, die);
22800 }
22801 }
22802
22803 /* Load the DIEs associated with type unit PER_CU into memory. */
22804
22805 static void
22806 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22807 {
22808 struct signatured_type *sig_type;
22809
22810 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22811 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22812
22813 /* We have the per_cu, but we need the signatured_type.
22814 Fortunately this is an easy translation. */
22815 gdb_assert (per_cu->is_debug_types);
22816 sig_type = (struct signatured_type *) per_cu;
22817
22818 gdb_assert (per_cu->cu == NULL);
22819
22820 read_signatured_type (sig_type);
22821
22822 gdb_assert (per_cu->cu != NULL);
22823 }
22824
22825 /* Read in a signatured type and build its CU and DIEs.
22826 If the type is a stub for the real type in a DWO file,
22827 read in the real type from the DWO file as well. */
22828
22829 static void
22830 read_signatured_type (struct signatured_type *sig_type)
22831 {
22832 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22833
22834 gdb_assert (per_cu->is_debug_types);
22835 gdb_assert (per_cu->cu == NULL);
22836
22837 cutu_reader reader (per_cu, NULL, 0, 1, false);
22838
22839 if (!reader.dummy_p)
22840 {
22841 struct dwarf2_cu *cu = reader.cu;
22842 const gdb_byte *info_ptr = reader.info_ptr;
22843
22844 gdb_assert (cu->die_hash == NULL);
22845 cu->die_hash =
22846 htab_create_alloc_ex (cu->header.length / 12,
22847 die_hash,
22848 die_eq,
22849 NULL,
22850 &cu->comp_unit_obstack,
22851 hashtab_obstack_allocate,
22852 dummy_obstack_deallocate);
22853
22854 if (reader.comp_unit_die->has_children)
22855 reader.comp_unit_die->child
22856 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22857 reader.comp_unit_die);
22858 cu->dies = reader.comp_unit_die;
22859 /* comp_unit_die is not stored in die_hash, no need. */
22860
22861 /* We try not to read any attributes in this function, because
22862 not all CUs needed for references have been loaded yet, and
22863 symbol table processing isn't initialized. But we have to
22864 set the CU language, or we won't be able to build types
22865 correctly. Similarly, if we do not read the producer, we can
22866 not apply producer-specific interpretation. */
22867 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22868 }
22869
22870 sig_type->per_cu.tu_read = 1;
22871 }
22872
22873 /* Decode simple location descriptions.
22874 Given a pointer to a dwarf block that defines a location, compute
22875 the location and return the value.
22876
22877 NOTE drow/2003-11-18: This function is called in two situations
22878 now: for the address of static or global variables (partial symbols
22879 only) and for offsets into structures which are expected to be
22880 (more or less) constant. The partial symbol case should go away,
22881 and only the constant case should remain. That will let this
22882 function complain more accurately. A few special modes are allowed
22883 without complaint for global variables (for instance, global
22884 register values and thread-local values).
22885
22886 A location description containing no operations indicates that the
22887 object is optimized out. The return value is 0 for that case.
22888 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22889 callers will only want a very basic result and this can become a
22890 complaint.
22891
22892 Note that stack[0] is unused except as a default error return. */
22893
22894 static CORE_ADDR
22895 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22896 {
22897 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22898 size_t i;
22899 size_t size = blk->size;
22900 const gdb_byte *data = blk->data;
22901 CORE_ADDR stack[64];
22902 int stacki;
22903 unsigned int bytes_read, unsnd;
22904 gdb_byte op;
22905
22906 i = 0;
22907 stacki = 0;
22908 stack[stacki] = 0;
22909 stack[++stacki] = 0;
22910
22911 while (i < size)
22912 {
22913 op = data[i++];
22914 switch (op)
22915 {
22916 case DW_OP_lit0:
22917 case DW_OP_lit1:
22918 case DW_OP_lit2:
22919 case DW_OP_lit3:
22920 case DW_OP_lit4:
22921 case DW_OP_lit5:
22922 case DW_OP_lit6:
22923 case DW_OP_lit7:
22924 case DW_OP_lit8:
22925 case DW_OP_lit9:
22926 case DW_OP_lit10:
22927 case DW_OP_lit11:
22928 case DW_OP_lit12:
22929 case DW_OP_lit13:
22930 case DW_OP_lit14:
22931 case DW_OP_lit15:
22932 case DW_OP_lit16:
22933 case DW_OP_lit17:
22934 case DW_OP_lit18:
22935 case DW_OP_lit19:
22936 case DW_OP_lit20:
22937 case DW_OP_lit21:
22938 case DW_OP_lit22:
22939 case DW_OP_lit23:
22940 case DW_OP_lit24:
22941 case DW_OP_lit25:
22942 case DW_OP_lit26:
22943 case DW_OP_lit27:
22944 case DW_OP_lit28:
22945 case DW_OP_lit29:
22946 case DW_OP_lit30:
22947 case DW_OP_lit31:
22948 stack[++stacki] = op - DW_OP_lit0;
22949 break;
22950
22951 case DW_OP_reg0:
22952 case DW_OP_reg1:
22953 case DW_OP_reg2:
22954 case DW_OP_reg3:
22955 case DW_OP_reg4:
22956 case DW_OP_reg5:
22957 case DW_OP_reg6:
22958 case DW_OP_reg7:
22959 case DW_OP_reg8:
22960 case DW_OP_reg9:
22961 case DW_OP_reg10:
22962 case DW_OP_reg11:
22963 case DW_OP_reg12:
22964 case DW_OP_reg13:
22965 case DW_OP_reg14:
22966 case DW_OP_reg15:
22967 case DW_OP_reg16:
22968 case DW_OP_reg17:
22969 case DW_OP_reg18:
22970 case DW_OP_reg19:
22971 case DW_OP_reg20:
22972 case DW_OP_reg21:
22973 case DW_OP_reg22:
22974 case DW_OP_reg23:
22975 case DW_OP_reg24:
22976 case DW_OP_reg25:
22977 case DW_OP_reg26:
22978 case DW_OP_reg27:
22979 case DW_OP_reg28:
22980 case DW_OP_reg29:
22981 case DW_OP_reg30:
22982 case DW_OP_reg31:
22983 stack[++stacki] = op - DW_OP_reg0;
22984 if (i < size)
22985 dwarf2_complex_location_expr_complaint ();
22986 break;
22987
22988 case DW_OP_regx:
22989 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22990 i += bytes_read;
22991 stack[++stacki] = unsnd;
22992 if (i < size)
22993 dwarf2_complex_location_expr_complaint ();
22994 break;
22995
22996 case DW_OP_addr:
22997 stack[++stacki] = read_address (objfile->obfd, &data[i],
22998 cu, &bytes_read);
22999 i += bytes_read;
23000 break;
23001
23002 case DW_OP_const1u:
23003 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23004 i += 1;
23005 break;
23006
23007 case DW_OP_const1s:
23008 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23009 i += 1;
23010 break;
23011
23012 case DW_OP_const2u:
23013 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23014 i += 2;
23015 break;
23016
23017 case DW_OP_const2s:
23018 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23019 i += 2;
23020 break;
23021
23022 case DW_OP_const4u:
23023 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23024 i += 4;
23025 break;
23026
23027 case DW_OP_const4s:
23028 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23029 i += 4;
23030 break;
23031
23032 case DW_OP_const8u:
23033 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23034 i += 8;
23035 break;
23036
23037 case DW_OP_constu:
23038 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23039 &bytes_read);
23040 i += bytes_read;
23041 break;
23042
23043 case DW_OP_consts:
23044 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23045 i += bytes_read;
23046 break;
23047
23048 case DW_OP_dup:
23049 stack[stacki + 1] = stack[stacki];
23050 stacki++;
23051 break;
23052
23053 case DW_OP_plus:
23054 stack[stacki - 1] += stack[stacki];
23055 stacki--;
23056 break;
23057
23058 case DW_OP_plus_uconst:
23059 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23060 &bytes_read);
23061 i += bytes_read;
23062 break;
23063
23064 case DW_OP_minus:
23065 stack[stacki - 1] -= stack[stacki];
23066 stacki--;
23067 break;
23068
23069 case DW_OP_deref:
23070 /* If we're not the last op, then we definitely can't encode
23071 this using GDB's address_class enum. This is valid for partial
23072 global symbols, although the variable's address will be bogus
23073 in the psymtab. */
23074 if (i < size)
23075 dwarf2_complex_location_expr_complaint ();
23076 break;
23077
23078 case DW_OP_GNU_push_tls_address:
23079 case DW_OP_form_tls_address:
23080 /* The top of the stack has the offset from the beginning
23081 of the thread control block at which the variable is located. */
23082 /* Nothing should follow this operator, so the top of stack would
23083 be returned. */
23084 /* This is valid for partial global symbols, but the variable's
23085 address will be bogus in the psymtab. Make it always at least
23086 non-zero to not look as a variable garbage collected by linker
23087 which have DW_OP_addr 0. */
23088 if (i < size)
23089 dwarf2_complex_location_expr_complaint ();
23090 stack[stacki]++;
23091 break;
23092
23093 case DW_OP_GNU_uninit:
23094 break;
23095
23096 case DW_OP_addrx:
23097 case DW_OP_GNU_addr_index:
23098 case DW_OP_GNU_const_index:
23099 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23100 &bytes_read);
23101 i += bytes_read;
23102 break;
23103
23104 default:
23105 {
23106 const char *name = get_DW_OP_name (op);
23107
23108 if (name)
23109 complaint (_("unsupported stack op: '%s'"),
23110 name);
23111 else
23112 complaint (_("unsupported stack op: '%02x'"),
23113 op);
23114 }
23115
23116 return (stack[stacki]);
23117 }
23118
23119 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23120 outside of the allocated space. Also enforce minimum>0. */
23121 if (stacki >= ARRAY_SIZE (stack) - 1)
23122 {
23123 complaint (_("location description stack overflow"));
23124 return 0;
23125 }
23126
23127 if (stacki <= 0)
23128 {
23129 complaint (_("location description stack underflow"));
23130 return 0;
23131 }
23132 }
23133 return (stack[stacki]);
23134 }
23135
23136 /* memory allocation interface */
23137
23138 static struct dwarf_block *
23139 dwarf_alloc_block (struct dwarf2_cu *cu)
23140 {
23141 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23142 }
23143
23144 static struct die_info *
23145 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23146 {
23147 struct die_info *die;
23148 size_t size = sizeof (struct die_info);
23149
23150 if (num_attrs > 1)
23151 size += (num_attrs - 1) * sizeof (struct attribute);
23152
23153 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23154 memset (die, 0, sizeof (struct die_info));
23155 return (die);
23156 }
23157
23158 \f
23159 /* Macro support. */
23160
23161 static struct macro_source_file *
23162 macro_start_file (struct dwarf2_cu *cu,
23163 int file, int line,
23164 struct macro_source_file *current_file,
23165 struct line_header *lh)
23166 {
23167 /* File name relative to the compilation directory of this source file. */
23168 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23169
23170 if (! current_file)
23171 {
23172 /* Note: We don't create a macro table for this compilation unit
23173 at all until we actually get a filename. */
23174 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23175
23176 /* If we have no current file, then this must be the start_file
23177 directive for the compilation unit's main source file. */
23178 current_file = macro_set_main (macro_table, file_name.get ());
23179 macro_define_special (macro_table);
23180 }
23181 else
23182 current_file = macro_include (current_file, line, file_name.get ());
23183
23184 return current_file;
23185 }
23186
23187 static const char *
23188 consume_improper_spaces (const char *p, const char *body)
23189 {
23190 if (*p == ' ')
23191 {
23192 complaint (_("macro definition contains spaces "
23193 "in formal argument list:\n`%s'"),
23194 body);
23195
23196 while (*p == ' ')
23197 p++;
23198 }
23199
23200 return p;
23201 }
23202
23203
23204 static void
23205 parse_macro_definition (struct macro_source_file *file, int line,
23206 const char *body)
23207 {
23208 const char *p;
23209
23210 /* The body string takes one of two forms. For object-like macro
23211 definitions, it should be:
23212
23213 <macro name> " " <definition>
23214
23215 For function-like macro definitions, it should be:
23216
23217 <macro name> "() " <definition>
23218 or
23219 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23220
23221 Spaces may appear only where explicitly indicated, and in the
23222 <definition>.
23223
23224 The Dwarf 2 spec says that an object-like macro's name is always
23225 followed by a space, but versions of GCC around March 2002 omit
23226 the space when the macro's definition is the empty string.
23227
23228 The Dwarf 2 spec says that there should be no spaces between the
23229 formal arguments in a function-like macro's formal argument list,
23230 but versions of GCC around March 2002 include spaces after the
23231 commas. */
23232
23233
23234 /* Find the extent of the macro name. The macro name is terminated
23235 by either a space or null character (for an object-like macro) or
23236 an opening paren (for a function-like macro). */
23237 for (p = body; *p; p++)
23238 if (*p == ' ' || *p == '(')
23239 break;
23240
23241 if (*p == ' ' || *p == '\0')
23242 {
23243 /* It's an object-like macro. */
23244 int name_len = p - body;
23245 std::string name (body, name_len);
23246 const char *replacement;
23247
23248 if (*p == ' ')
23249 replacement = body + name_len + 1;
23250 else
23251 {
23252 dwarf2_macro_malformed_definition_complaint (body);
23253 replacement = body + name_len;
23254 }
23255
23256 macro_define_object (file, line, name.c_str (), replacement);
23257 }
23258 else if (*p == '(')
23259 {
23260 /* It's a function-like macro. */
23261 std::string name (body, p - body);
23262 int argc = 0;
23263 int argv_size = 1;
23264 char **argv = XNEWVEC (char *, argv_size);
23265
23266 p++;
23267
23268 p = consume_improper_spaces (p, body);
23269
23270 /* Parse the formal argument list. */
23271 while (*p && *p != ')')
23272 {
23273 /* Find the extent of the current argument name. */
23274 const char *arg_start = p;
23275
23276 while (*p && *p != ',' && *p != ')' && *p != ' ')
23277 p++;
23278
23279 if (! *p || p == arg_start)
23280 dwarf2_macro_malformed_definition_complaint (body);
23281 else
23282 {
23283 /* Make sure argv has room for the new argument. */
23284 if (argc >= argv_size)
23285 {
23286 argv_size *= 2;
23287 argv = XRESIZEVEC (char *, argv, argv_size);
23288 }
23289
23290 argv[argc++] = savestring (arg_start, p - arg_start);
23291 }
23292
23293 p = consume_improper_spaces (p, body);
23294
23295 /* Consume the comma, if present. */
23296 if (*p == ',')
23297 {
23298 p++;
23299
23300 p = consume_improper_spaces (p, body);
23301 }
23302 }
23303
23304 if (*p == ')')
23305 {
23306 p++;
23307
23308 if (*p == ' ')
23309 /* Perfectly formed definition, no complaints. */
23310 macro_define_function (file, line, name.c_str (),
23311 argc, (const char **) argv,
23312 p + 1);
23313 else if (*p == '\0')
23314 {
23315 /* Complain, but do define it. */
23316 dwarf2_macro_malformed_definition_complaint (body);
23317 macro_define_function (file, line, name.c_str (),
23318 argc, (const char **) argv,
23319 p);
23320 }
23321 else
23322 /* Just complain. */
23323 dwarf2_macro_malformed_definition_complaint (body);
23324 }
23325 else
23326 /* Just complain. */
23327 dwarf2_macro_malformed_definition_complaint (body);
23328
23329 {
23330 int i;
23331
23332 for (i = 0; i < argc; i++)
23333 xfree (argv[i]);
23334 }
23335 xfree (argv);
23336 }
23337 else
23338 dwarf2_macro_malformed_definition_complaint (body);
23339 }
23340
23341 /* Skip some bytes from BYTES according to the form given in FORM.
23342 Returns the new pointer. */
23343
23344 static const gdb_byte *
23345 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23346 enum dwarf_form form,
23347 unsigned int offset_size,
23348 struct dwarf2_section_info *section)
23349 {
23350 unsigned int bytes_read;
23351
23352 switch (form)
23353 {
23354 case DW_FORM_data1:
23355 case DW_FORM_flag:
23356 ++bytes;
23357 break;
23358
23359 case DW_FORM_data2:
23360 bytes += 2;
23361 break;
23362
23363 case DW_FORM_data4:
23364 bytes += 4;
23365 break;
23366
23367 case DW_FORM_data8:
23368 bytes += 8;
23369 break;
23370
23371 case DW_FORM_data16:
23372 bytes += 16;
23373 break;
23374
23375 case DW_FORM_string:
23376 read_direct_string (abfd, bytes, &bytes_read);
23377 bytes += bytes_read;
23378 break;
23379
23380 case DW_FORM_sec_offset:
23381 case DW_FORM_strp:
23382 case DW_FORM_GNU_strp_alt:
23383 bytes += offset_size;
23384 break;
23385
23386 case DW_FORM_block:
23387 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23388 bytes += bytes_read;
23389 break;
23390
23391 case DW_FORM_block1:
23392 bytes += 1 + read_1_byte (abfd, bytes);
23393 break;
23394 case DW_FORM_block2:
23395 bytes += 2 + read_2_bytes (abfd, bytes);
23396 break;
23397 case DW_FORM_block4:
23398 bytes += 4 + read_4_bytes (abfd, bytes);
23399 break;
23400
23401 case DW_FORM_addrx:
23402 case DW_FORM_sdata:
23403 case DW_FORM_strx:
23404 case DW_FORM_udata:
23405 case DW_FORM_GNU_addr_index:
23406 case DW_FORM_GNU_str_index:
23407 bytes = gdb_skip_leb128 (bytes, buffer_end);
23408 if (bytes == NULL)
23409 {
23410 dwarf2_section_buffer_overflow_complaint (section);
23411 return NULL;
23412 }
23413 break;
23414
23415 case DW_FORM_implicit_const:
23416 break;
23417
23418 default:
23419 {
23420 complaint (_("invalid form 0x%x in `%s'"),
23421 form, section->get_name ());
23422 return NULL;
23423 }
23424 }
23425
23426 return bytes;
23427 }
23428
23429 /* A helper for dwarf_decode_macros that handles skipping an unknown
23430 opcode. Returns an updated pointer to the macro data buffer; or,
23431 on error, issues a complaint and returns NULL. */
23432
23433 static const gdb_byte *
23434 skip_unknown_opcode (unsigned int opcode,
23435 const gdb_byte **opcode_definitions,
23436 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23437 bfd *abfd,
23438 unsigned int offset_size,
23439 struct dwarf2_section_info *section)
23440 {
23441 unsigned int bytes_read, i;
23442 unsigned long arg;
23443 const gdb_byte *defn;
23444
23445 if (opcode_definitions[opcode] == NULL)
23446 {
23447 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23448 opcode);
23449 return NULL;
23450 }
23451
23452 defn = opcode_definitions[opcode];
23453 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23454 defn += bytes_read;
23455
23456 for (i = 0; i < arg; ++i)
23457 {
23458 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23459 (enum dwarf_form) defn[i], offset_size,
23460 section);
23461 if (mac_ptr == NULL)
23462 {
23463 /* skip_form_bytes already issued the complaint. */
23464 return NULL;
23465 }
23466 }
23467
23468 return mac_ptr;
23469 }
23470
23471 /* A helper function which parses the header of a macro section.
23472 If the macro section is the extended (for now called "GNU") type,
23473 then this updates *OFFSET_SIZE. Returns a pointer to just after
23474 the header, or issues a complaint and returns NULL on error. */
23475
23476 static const gdb_byte *
23477 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23478 bfd *abfd,
23479 const gdb_byte *mac_ptr,
23480 unsigned int *offset_size,
23481 int section_is_gnu)
23482 {
23483 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23484
23485 if (section_is_gnu)
23486 {
23487 unsigned int version, flags;
23488
23489 version = read_2_bytes (abfd, mac_ptr);
23490 if (version != 4 && version != 5)
23491 {
23492 complaint (_("unrecognized version `%d' in .debug_macro section"),
23493 version);
23494 return NULL;
23495 }
23496 mac_ptr += 2;
23497
23498 flags = read_1_byte (abfd, mac_ptr);
23499 ++mac_ptr;
23500 *offset_size = (flags & 1) ? 8 : 4;
23501
23502 if ((flags & 2) != 0)
23503 /* We don't need the line table offset. */
23504 mac_ptr += *offset_size;
23505
23506 /* Vendor opcode descriptions. */
23507 if ((flags & 4) != 0)
23508 {
23509 unsigned int i, count;
23510
23511 count = read_1_byte (abfd, mac_ptr);
23512 ++mac_ptr;
23513 for (i = 0; i < count; ++i)
23514 {
23515 unsigned int opcode, bytes_read;
23516 unsigned long arg;
23517
23518 opcode = read_1_byte (abfd, mac_ptr);
23519 ++mac_ptr;
23520 opcode_definitions[opcode] = mac_ptr;
23521 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23522 mac_ptr += bytes_read;
23523 mac_ptr += arg;
23524 }
23525 }
23526 }
23527
23528 return mac_ptr;
23529 }
23530
23531 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23532 including DW_MACRO_import. */
23533
23534 static void
23535 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23536 bfd *abfd,
23537 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23538 struct macro_source_file *current_file,
23539 struct line_header *lh,
23540 struct dwarf2_section_info *section,
23541 int section_is_gnu, int section_is_dwz,
23542 unsigned int offset_size,
23543 htab_t include_hash)
23544 {
23545 struct dwarf2_per_objfile *dwarf2_per_objfile
23546 = cu->per_cu->dwarf2_per_objfile;
23547 struct objfile *objfile = dwarf2_per_objfile->objfile;
23548 enum dwarf_macro_record_type macinfo_type;
23549 int at_commandline;
23550 const gdb_byte *opcode_definitions[256];
23551
23552 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23553 &offset_size, section_is_gnu);
23554 if (mac_ptr == NULL)
23555 {
23556 /* We already issued a complaint. */
23557 return;
23558 }
23559
23560 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23561 GDB is still reading the definitions from command line. First
23562 DW_MACINFO_start_file will need to be ignored as it was already executed
23563 to create CURRENT_FILE for the main source holding also the command line
23564 definitions. On first met DW_MACINFO_start_file this flag is reset to
23565 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23566
23567 at_commandline = 1;
23568
23569 do
23570 {
23571 /* Do we at least have room for a macinfo type byte? */
23572 if (mac_ptr >= mac_end)
23573 {
23574 dwarf2_section_buffer_overflow_complaint (section);
23575 break;
23576 }
23577
23578 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23579 mac_ptr++;
23580
23581 /* Note that we rely on the fact that the corresponding GNU and
23582 DWARF constants are the same. */
23583 DIAGNOSTIC_PUSH
23584 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23585 switch (macinfo_type)
23586 {
23587 /* A zero macinfo type indicates the end of the macro
23588 information. */
23589 case 0:
23590 break;
23591
23592 case DW_MACRO_define:
23593 case DW_MACRO_undef:
23594 case DW_MACRO_define_strp:
23595 case DW_MACRO_undef_strp:
23596 case DW_MACRO_define_sup:
23597 case DW_MACRO_undef_sup:
23598 {
23599 unsigned int bytes_read;
23600 int line;
23601 const char *body;
23602 int is_define;
23603
23604 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23605 mac_ptr += bytes_read;
23606
23607 if (macinfo_type == DW_MACRO_define
23608 || macinfo_type == DW_MACRO_undef)
23609 {
23610 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23611 mac_ptr += bytes_read;
23612 }
23613 else
23614 {
23615 LONGEST str_offset;
23616
23617 str_offset = read_offset (abfd, mac_ptr, offset_size);
23618 mac_ptr += offset_size;
23619
23620 if (macinfo_type == DW_MACRO_define_sup
23621 || macinfo_type == DW_MACRO_undef_sup
23622 || section_is_dwz)
23623 {
23624 struct dwz_file *dwz
23625 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23626
23627 body = read_indirect_string_from_dwz (objfile,
23628 dwz, str_offset);
23629 }
23630 else
23631 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23632 abfd, str_offset);
23633 }
23634
23635 is_define = (macinfo_type == DW_MACRO_define
23636 || macinfo_type == DW_MACRO_define_strp
23637 || macinfo_type == DW_MACRO_define_sup);
23638 if (! current_file)
23639 {
23640 /* DWARF violation as no main source is present. */
23641 complaint (_("debug info with no main source gives macro %s "
23642 "on line %d: %s"),
23643 is_define ? _("definition") : _("undefinition"),
23644 line, body);
23645 break;
23646 }
23647 if ((line == 0 && !at_commandline)
23648 || (line != 0 && at_commandline))
23649 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23650 at_commandline ? _("command-line") : _("in-file"),
23651 is_define ? _("definition") : _("undefinition"),
23652 line == 0 ? _("zero") : _("non-zero"), line, body);
23653
23654 if (body == NULL)
23655 {
23656 /* Fedora's rpm-build's "debugedit" binary
23657 corrupted .debug_macro sections.
23658
23659 For more info, see
23660 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23661 complaint (_("debug info gives %s invalid macro %s "
23662 "without body (corrupted?) at line %d "
23663 "on file %s"),
23664 at_commandline ? _("command-line") : _("in-file"),
23665 is_define ? _("definition") : _("undefinition"),
23666 line, current_file->filename);
23667 }
23668 else if (is_define)
23669 parse_macro_definition (current_file, line, body);
23670 else
23671 {
23672 gdb_assert (macinfo_type == DW_MACRO_undef
23673 || macinfo_type == DW_MACRO_undef_strp
23674 || macinfo_type == DW_MACRO_undef_sup);
23675 macro_undef (current_file, line, body);
23676 }
23677 }
23678 break;
23679
23680 case DW_MACRO_start_file:
23681 {
23682 unsigned int bytes_read;
23683 int line, file;
23684
23685 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23686 mac_ptr += bytes_read;
23687 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23688 mac_ptr += bytes_read;
23689
23690 if ((line == 0 && !at_commandline)
23691 || (line != 0 && at_commandline))
23692 complaint (_("debug info gives source %d included "
23693 "from %s at %s line %d"),
23694 file, at_commandline ? _("command-line") : _("file"),
23695 line == 0 ? _("zero") : _("non-zero"), line);
23696
23697 if (at_commandline)
23698 {
23699 /* This DW_MACRO_start_file was executed in the
23700 pass one. */
23701 at_commandline = 0;
23702 }
23703 else
23704 current_file = macro_start_file (cu, file, line, current_file,
23705 lh);
23706 }
23707 break;
23708
23709 case DW_MACRO_end_file:
23710 if (! current_file)
23711 complaint (_("macro debug info has an unmatched "
23712 "`close_file' directive"));
23713 else
23714 {
23715 current_file = current_file->included_by;
23716 if (! current_file)
23717 {
23718 enum dwarf_macro_record_type next_type;
23719
23720 /* GCC circa March 2002 doesn't produce the zero
23721 type byte marking the end of the compilation
23722 unit. Complain if it's not there, but exit no
23723 matter what. */
23724
23725 /* Do we at least have room for a macinfo type byte? */
23726 if (mac_ptr >= mac_end)
23727 {
23728 dwarf2_section_buffer_overflow_complaint (section);
23729 return;
23730 }
23731
23732 /* We don't increment mac_ptr here, so this is just
23733 a look-ahead. */
23734 next_type
23735 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23736 mac_ptr);
23737 if (next_type != 0)
23738 complaint (_("no terminating 0-type entry for "
23739 "macros in `.debug_macinfo' section"));
23740
23741 return;
23742 }
23743 }
23744 break;
23745
23746 case DW_MACRO_import:
23747 case DW_MACRO_import_sup:
23748 {
23749 LONGEST offset;
23750 void **slot;
23751 bfd *include_bfd = abfd;
23752 struct dwarf2_section_info *include_section = section;
23753 const gdb_byte *include_mac_end = mac_end;
23754 int is_dwz = section_is_dwz;
23755 const gdb_byte *new_mac_ptr;
23756
23757 offset = read_offset (abfd, mac_ptr, offset_size);
23758 mac_ptr += offset_size;
23759
23760 if (macinfo_type == DW_MACRO_import_sup)
23761 {
23762 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23763
23764 dwz->macro.read (objfile);
23765
23766 include_section = &dwz->macro;
23767 include_bfd = include_section->get_bfd_owner ();
23768 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23769 is_dwz = 1;
23770 }
23771
23772 new_mac_ptr = include_section->buffer + offset;
23773 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23774
23775 if (*slot != NULL)
23776 {
23777 /* This has actually happened; see
23778 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23779 complaint (_("recursive DW_MACRO_import in "
23780 ".debug_macro section"));
23781 }
23782 else
23783 {
23784 *slot = (void *) new_mac_ptr;
23785
23786 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23787 include_mac_end, current_file, lh,
23788 section, section_is_gnu, is_dwz,
23789 offset_size, include_hash);
23790
23791 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23792 }
23793 }
23794 break;
23795
23796 case DW_MACINFO_vendor_ext:
23797 if (!section_is_gnu)
23798 {
23799 unsigned int bytes_read;
23800
23801 /* This reads the constant, but since we don't recognize
23802 any vendor extensions, we ignore it. */
23803 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23804 mac_ptr += bytes_read;
23805 read_direct_string (abfd, mac_ptr, &bytes_read);
23806 mac_ptr += bytes_read;
23807
23808 /* We don't recognize any vendor extensions. */
23809 break;
23810 }
23811 /* FALLTHROUGH */
23812
23813 default:
23814 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23815 mac_ptr, mac_end, abfd, offset_size,
23816 section);
23817 if (mac_ptr == NULL)
23818 return;
23819 break;
23820 }
23821 DIAGNOSTIC_POP
23822 } while (macinfo_type != 0);
23823 }
23824
23825 static void
23826 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23827 int section_is_gnu)
23828 {
23829 struct dwarf2_per_objfile *dwarf2_per_objfile
23830 = cu->per_cu->dwarf2_per_objfile;
23831 struct objfile *objfile = dwarf2_per_objfile->objfile;
23832 struct line_header *lh = cu->line_header;
23833 bfd *abfd;
23834 const gdb_byte *mac_ptr, *mac_end;
23835 struct macro_source_file *current_file = 0;
23836 enum dwarf_macro_record_type macinfo_type;
23837 unsigned int offset_size = cu->header.offset_size;
23838 const gdb_byte *opcode_definitions[256];
23839 void **slot;
23840 struct dwarf2_section_info *section;
23841 const char *section_name;
23842
23843 if (cu->dwo_unit != NULL)
23844 {
23845 if (section_is_gnu)
23846 {
23847 section = &cu->dwo_unit->dwo_file->sections.macro;
23848 section_name = ".debug_macro.dwo";
23849 }
23850 else
23851 {
23852 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23853 section_name = ".debug_macinfo.dwo";
23854 }
23855 }
23856 else
23857 {
23858 if (section_is_gnu)
23859 {
23860 section = &dwarf2_per_objfile->macro;
23861 section_name = ".debug_macro";
23862 }
23863 else
23864 {
23865 section = &dwarf2_per_objfile->macinfo;
23866 section_name = ".debug_macinfo";
23867 }
23868 }
23869
23870 section->read (objfile);
23871 if (section->buffer == NULL)
23872 {
23873 complaint (_("missing %s section"), section_name);
23874 return;
23875 }
23876 abfd = section->get_bfd_owner ();
23877
23878 /* First pass: Find the name of the base filename.
23879 This filename is needed in order to process all macros whose definition
23880 (or undefinition) comes from the command line. These macros are defined
23881 before the first DW_MACINFO_start_file entry, and yet still need to be
23882 associated to the base file.
23883
23884 To determine the base file name, we scan the macro definitions until we
23885 reach the first DW_MACINFO_start_file entry. We then initialize
23886 CURRENT_FILE accordingly so that any macro definition found before the
23887 first DW_MACINFO_start_file can still be associated to the base file. */
23888
23889 mac_ptr = section->buffer + offset;
23890 mac_end = section->buffer + section->size;
23891
23892 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23893 &offset_size, section_is_gnu);
23894 if (mac_ptr == NULL)
23895 {
23896 /* We already issued a complaint. */
23897 return;
23898 }
23899
23900 do
23901 {
23902 /* Do we at least have room for a macinfo type byte? */
23903 if (mac_ptr >= mac_end)
23904 {
23905 /* Complaint is printed during the second pass as GDB will probably
23906 stop the first pass earlier upon finding
23907 DW_MACINFO_start_file. */
23908 break;
23909 }
23910
23911 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23912 mac_ptr++;
23913
23914 /* Note that we rely on the fact that the corresponding GNU and
23915 DWARF constants are the same. */
23916 DIAGNOSTIC_PUSH
23917 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23918 switch (macinfo_type)
23919 {
23920 /* A zero macinfo type indicates the end of the macro
23921 information. */
23922 case 0:
23923 break;
23924
23925 case DW_MACRO_define:
23926 case DW_MACRO_undef:
23927 /* Only skip the data by MAC_PTR. */
23928 {
23929 unsigned int bytes_read;
23930
23931 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23932 mac_ptr += bytes_read;
23933 read_direct_string (abfd, mac_ptr, &bytes_read);
23934 mac_ptr += bytes_read;
23935 }
23936 break;
23937
23938 case DW_MACRO_start_file:
23939 {
23940 unsigned int bytes_read;
23941 int line, file;
23942
23943 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23944 mac_ptr += bytes_read;
23945 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23946 mac_ptr += bytes_read;
23947
23948 current_file = macro_start_file (cu, file, line, current_file, lh);
23949 }
23950 break;
23951
23952 case DW_MACRO_end_file:
23953 /* No data to skip by MAC_PTR. */
23954 break;
23955
23956 case DW_MACRO_define_strp:
23957 case DW_MACRO_undef_strp:
23958 case DW_MACRO_define_sup:
23959 case DW_MACRO_undef_sup:
23960 {
23961 unsigned int bytes_read;
23962
23963 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23964 mac_ptr += bytes_read;
23965 mac_ptr += offset_size;
23966 }
23967 break;
23968
23969 case DW_MACRO_import:
23970 case DW_MACRO_import_sup:
23971 /* Note that, according to the spec, a transparent include
23972 chain cannot call DW_MACRO_start_file. So, we can just
23973 skip this opcode. */
23974 mac_ptr += offset_size;
23975 break;
23976
23977 case DW_MACINFO_vendor_ext:
23978 /* Only skip the data by MAC_PTR. */
23979 if (!section_is_gnu)
23980 {
23981 unsigned int bytes_read;
23982
23983 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23984 mac_ptr += bytes_read;
23985 read_direct_string (abfd, mac_ptr, &bytes_read);
23986 mac_ptr += bytes_read;
23987 }
23988 /* FALLTHROUGH */
23989
23990 default:
23991 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23992 mac_ptr, mac_end, abfd, offset_size,
23993 section);
23994 if (mac_ptr == NULL)
23995 return;
23996 break;
23997 }
23998 DIAGNOSTIC_POP
23999 } while (macinfo_type != 0 && current_file == NULL);
24000
24001 /* Second pass: Process all entries.
24002
24003 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24004 command-line macro definitions/undefinitions. This flag is unset when we
24005 reach the first DW_MACINFO_start_file entry. */
24006
24007 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24008 htab_eq_pointer,
24009 NULL, xcalloc, xfree));
24010 mac_ptr = section->buffer + offset;
24011 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24012 *slot = (void *) mac_ptr;
24013 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24014 current_file, lh, section,
24015 section_is_gnu, 0, offset_size,
24016 include_hash.get ());
24017 }
24018
24019 /* Return the .debug_loc section to use for CU.
24020 For DWO files use .debug_loc.dwo. */
24021
24022 static struct dwarf2_section_info *
24023 cu_debug_loc_section (struct dwarf2_cu *cu)
24024 {
24025 struct dwarf2_per_objfile *dwarf2_per_objfile
24026 = cu->per_cu->dwarf2_per_objfile;
24027
24028 if (cu->dwo_unit)
24029 {
24030 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24031
24032 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24033 }
24034 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24035 : &dwarf2_per_objfile->loc);
24036 }
24037
24038 /* A helper function that fills in a dwarf2_loclist_baton. */
24039
24040 static void
24041 fill_in_loclist_baton (struct dwarf2_cu *cu,
24042 struct dwarf2_loclist_baton *baton,
24043 const struct attribute *attr)
24044 {
24045 struct dwarf2_per_objfile *dwarf2_per_objfile
24046 = cu->per_cu->dwarf2_per_objfile;
24047 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24048
24049 section->read (dwarf2_per_objfile->objfile);
24050
24051 baton->per_cu = cu->per_cu;
24052 gdb_assert (baton->per_cu);
24053 /* We don't know how long the location list is, but make sure we
24054 don't run off the edge of the section. */
24055 baton->size = section->size - DW_UNSND (attr);
24056 baton->data = section->buffer + DW_UNSND (attr);
24057 baton->base_address = cu->base_address;
24058 baton->from_dwo = cu->dwo_unit != NULL;
24059 }
24060
24061 static void
24062 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24063 struct dwarf2_cu *cu, int is_block)
24064 {
24065 struct dwarf2_per_objfile *dwarf2_per_objfile
24066 = cu->per_cu->dwarf2_per_objfile;
24067 struct objfile *objfile = dwarf2_per_objfile->objfile;
24068 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24069
24070 if (attr->form_is_section_offset ()
24071 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24072 the section. If so, fall through to the complaint in the
24073 other branch. */
24074 && DW_UNSND (attr) < section->get_size (objfile))
24075 {
24076 struct dwarf2_loclist_baton *baton;
24077
24078 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24079
24080 fill_in_loclist_baton (cu, baton, attr);
24081
24082 if (cu->base_known == 0)
24083 complaint (_("Location list used without "
24084 "specifying the CU base address."));
24085
24086 SYMBOL_ACLASS_INDEX (sym) = (is_block
24087 ? dwarf2_loclist_block_index
24088 : dwarf2_loclist_index);
24089 SYMBOL_LOCATION_BATON (sym) = baton;
24090 }
24091 else
24092 {
24093 struct dwarf2_locexpr_baton *baton;
24094
24095 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24096 baton->per_cu = cu->per_cu;
24097 gdb_assert (baton->per_cu);
24098
24099 if (attr->form_is_block ())
24100 {
24101 /* Note that we're just copying the block's data pointer
24102 here, not the actual data. We're still pointing into the
24103 info_buffer for SYM's objfile; right now we never release
24104 that buffer, but when we do clean up properly this may
24105 need to change. */
24106 baton->size = DW_BLOCK (attr)->size;
24107 baton->data = DW_BLOCK (attr)->data;
24108 }
24109 else
24110 {
24111 dwarf2_invalid_attrib_class_complaint ("location description",
24112 sym->natural_name ());
24113 baton->size = 0;
24114 }
24115
24116 SYMBOL_ACLASS_INDEX (sym) = (is_block
24117 ? dwarf2_locexpr_block_index
24118 : dwarf2_locexpr_index);
24119 SYMBOL_LOCATION_BATON (sym) = baton;
24120 }
24121 }
24122
24123 /* See read.h. */
24124
24125 struct objfile *
24126 dwarf2_per_cu_data::objfile () const
24127 {
24128 struct objfile *objfile = dwarf2_per_objfile->objfile;
24129
24130 /* Return the master objfile, so that we can report and look up the
24131 correct file containing this variable. */
24132 if (objfile->separate_debug_objfile_backlink)
24133 objfile = objfile->separate_debug_objfile_backlink;
24134
24135 return objfile;
24136 }
24137
24138 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24139 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24140 CU_HEADERP first. */
24141
24142 static const struct comp_unit_head *
24143 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24144 const struct dwarf2_per_cu_data *per_cu)
24145 {
24146 const gdb_byte *info_ptr;
24147
24148 if (per_cu->cu)
24149 return &per_cu->cu->header;
24150
24151 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24152
24153 memset (cu_headerp, 0, sizeof (*cu_headerp));
24154 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24155 rcuh_kind::COMPILE);
24156
24157 return cu_headerp;
24158 }
24159
24160 /* See read.h. */
24161
24162 int
24163 dwarf2_per_cu_data::addr_size () const
24164 {
24165 struct comp_unit_head cu_header_local;
24166 const struct comp_unit_head *cu_headerp;
24167
24168 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24169
24170 return cu_headerp->addr_size;
24171 }
24172
24173 /* See read.h. */
24174
24175 int
24176 dwarf2_per_cu_data::offset_size () const
24177 {
24178 struct comp_unit_head cu_header_local;
24179 const struct comp_unit_head *cu_headerp;
24180
24181 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24182
24183 return cu_headerp->offset_size;
24184 }
24185
24186 /* See read.h. */
24187
24188 int
24189 dwarf2_per_cu_data::ref_addr_size () const
24190 {
24191 struct comp_unit_head cu_header_local;
24192 const struct comp_unit_head *cu_headerp;
24193
24194 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24195
24196 if (cu_headerp->version == 2)
24197 return cu_headerp->addr_size;
24198 else
24199 return cu_headerp->offset_size;
24200 }
24201
24202 /* See read.h. */
24203
24204 CORE_ADDR
24205 dwarf2_per_cu_data::text_offset () const
24206 {
24207 struct objfile *objfile = dwarf2_per_objfile->objfile;
24208
24209 return objfile->text_section_offset ();
24210 }
24211
24212 /* See read.h. */
24213
24214 struct type *
24215 dwarf2_per_cu_data::addr_type () const
24216 {
24217 struct objfile *objfile = dwarf2_per_objfile->objfile;
24218 struct type *void_type = objfile_type (objfile)->builtin_void;
24219 struct type *addr_type = lookup_pointer_type (void_type);
24220 int addr_size = this->addr_size ();
24221
24222 if (TYPE_LENGTH (addr_type) == addr_size)
24223 return addr_type;
24224
24225 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24226 return addr_type;
24227 }
24228
24229 /* Locate the .debug_info compilation unit from CU's objfile which contains
24230 the DIE at OFFSET. Raises an error on failure. */
24231
24232 static struct dwarf2_per_cu_data *
24233 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24234 unsigned int offset_in_dwz,
24235 struct dwarf2_per_objfile *dwarf2_per_objfile)
24236 {
24237 struct dwarf2_per_cu_data *this_cu;
24238 int low, high;
24239
24240 low = 0;
24241 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24242 while (high > low)
24243 {
24244 struct dwarf2_per_cu_data *mid_cu;
24245 int mid = low + (high - low) / 2;
24246
24247 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24248 if (mid_cu->is_dwz > offset_in_dwz
24249 || (mid_cu->is_dwz == offset_in_dwz
24250 && mid_cu->sect_off + mid_cu->length >= sect_off))
24251 high = mid;
24252 else
24253 low = mid + 1;
24254 }
24255 gdb_assert (low == high);
24256 this_cu = dwarf2_per_objfile->all_comp_units[low];
24257 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24258 {
24259 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24260 error (_("Dwarf Error: could not find partial DIE containing "
24261 "offset %s [in module %s]"),
24262 sect_offset_str (sect_off),
24263 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24264
24265 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24266 <= sect_off);
24267 return dwarf2_per_objfile->all_comp_units[low-1];
24268 }
24269 else
24270 {
24271 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24272 && sect_off >= this_cu->sect_off + this_cu->length)
24273 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24274 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24275 return this_cu;
24276 }
24277 }
24278
24279 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24280
24281 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24282 : per_cu (per_cu_),
24283 mark (false),
24284 has_loclist (false),
24285 checked_producer (false),
24286 producer_is_gxx_lt_4_6 (false),
24287 producer_is_gcc_lt_4_3 (false),
24288 producer_is_icc (false),
24289 producer_is_icc_lt_14 (false),
24290 producer_is_codewarrior (false),
24291 processing_has_namespace_info (false)
24292 {
24293 per_cu->cu = this;
24294 }
24295
24296 /* Destroy a dwarf2_cu. */
24297
24298 dwarf2_cu::~dwarf2_cu ()
24299 {
24300 per_cu->cu = NULL;
24301 }
24302
24303 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24304
24305 static void
24306 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24307 enum language pretend_language)
24308 {
24309 struct attribute *attr;
24310
24311 /* Set the language we're debugging. */
24312 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24313 if (attr != nullptr)
24314 set_cu_language (DW_UNSND (attr), cu);
24315 else
24316 {
24317 cu->language = pretend_language;
24318 cu->language_defn = language_def (cu->language);
24319 }
24320
24321 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24322 }
24323
24324 /* Increase the age counter on each cached compilation unit, and free
24325 any that are too old. */
24326
24327 static void
24328 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24329 {
24330 struct dwarf2_per_cu_data *per_cu, **last_chain;
24331
24332 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24333 per_cu = dwarf2_per_objfile->read_in_chain;
24334 while (per_cu != NULL)
24335 {
24336 per_cu->cu->last_used ++;
24337 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24338 dwarf2_mark (per_cu->cu);
24339 per_cu = per_cu->cu->read_in_chain;
24340 }
24341
24342 per_cu = dwarf2_per_objfile->read_in_chain;
24343 last_chain = &dwarf2_per_objfile->read_in_chain;
24344 while (per_cu != NULL)
24345 {
24346 struct dwarf2_per_cu_data *next_cu;
24347
24348 next_cu = per_cu->cu->read_in_chain;
24349
24350 if (!per_cu->cu->mark)
24351 {
24352 delete per_cu->cu;
24353 *last_chain = next_cu;
24354 }
24355 else
24356 last_chain = &per_cu->cu->read_in_chain;
24357
24358 per_cu = next_cu;
24359 }
24360 }
24361
24362 /* Remove a single compilation unit from the cache. */
24363
24364 static void
24365 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24366 {
24367 struct dwarf2_per_cu_data *per_cu, **last_chain;
24368 struct dwarf2_per_objfile *dwarf2_per_objfile
24369 = target_per_cu->dwarf2_per_objfile;
24370
24371 per_cu = dwarf2_per_objfile->read_in_chain;
24372 last_chain = &dwarf2_per_objfile->read_in_chain;
24373 while (per_cu != NULL)
24374 {
24375 struct dwarf2_per_cu_data *next_cu;
24376
24377 next_cu = per_cu->cu->read_in_chain;
24378
24379 if (per_cu == target_per_cu)
24380 {
24381 delete per_cu->cu;
24382 per_cu->cu = NULL;
24383 *last_chain = next_cu;
24384 break;
24385 }
24386 else
24387 last_chain = &per_cu->cu->read_in_chain;
24388
24389 per_cu = next_cu;
24390 }
24391 }
24392
24393 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24394 We store these in a hash table separate from the DIEs, and preserve them
24395 when the DIEs are flushed out of cache.
24396
24397 The CU "per_cu" pointer is needed because offset alone is not enough to
24398 uniquely identify the type. A file may have multiple .debug_types sections,
24399 or the type may come from a DWO file. Furthermore, while it's more logical
24400 to use per_cu->section+offset, with Fission the section with the data is in
24401 the DWO file but we don't know that section at the point we need it.
24402 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24403 because we can enter the lookup routine, get_die_type_at_offset, from
24404 outside this file, and thus won't necessarily have PER_CU->cu.
24405 Fortunately, PER_CU is stable for the life of the objfile. */
24406
24407 struct dwarf2_per_cu_offset_and_type
24408 {
24409 const struct dwarf2_per_cu_data *per_cu;
24410 sect_offset sect_off;
24411 struct type *type;
24412 };
24413
24414 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24415
24416 static hashval_t
24417 per_cu_offset_and_type_hash (const void *item)
24418 {
24419 const struct dwarf2_per_cu_offset_and_type *ofs
24420 = (const struct dwarf2_per_cu_offset_and_type *) item;
24421
24422 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24423 }
24424
24425 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24426
24427 static int
24428 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24429 {
24430 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24431 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24432 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24433 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24434
24435 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24436 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24437 }
24438
24439 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24440 table if necessary. For convenience, return TYPE.
24441
24442 The DIEs reading must have careful ordering to:
24443 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24444 reading current DIE.
24445 * Not trying to dereference contents of still incompletely read in types
24446 while reading in other DIEs.
24447 * Enable referencing still incompletely read in types just by a pointer to
24448 the type without accessing its fields.
24449
24450 Therefore caller should follow these rules:
24451 * Try to fetch any prerequisite types we may need to build this DIE type
24452 before building the type and calling set_die_type.
24453 * After building type call set_die_type for current DIE as soon as
24454 possible before fetching more types to complete the current type.
24455 * Make the type as complete as possible before fetching more types. */
24456
24457 static struct type *
24458 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24459 {
24460 struct dwarf2_per_objfile *dwarf2_per_objfile
24461 = cu->per_cu->dwarf2_per_objfile;
24462 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24463 struct objfile *objfile = dwarf2_per_objfile->objfile;
24464 struct attribute *attr;
24465 struct dynamic_prop prop;
24466
24467 /* For Ada types, make sure that the gnat-specific data is always
24468 initialized (if not already set). There are a few types where
24469 we should not be doing so, because the type-specific area is
24470 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24471 where the type-specific area is used to store the floatformat).
24472 But this is not a problem, because the gnat-specific information
24473 is actually not needed for these types. */
24474 if (need_gnat_info (cu)
24475 && TYPE_CODE (type) != TYPE_CODE_FUNC
24476 && TYPE_CODE (type) != TYPE_CODE_FLT
24477 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24478 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24479 && TYPE_CODE (type) != TYPE_CODE_METHOD
24480 && !HAVE_GNAT_AUX_INFO (type))
24481 INIT_GNAT_SPECIFIC (type);
24482
24483 /* Read DW_AT_allocated and set in type. */
24484 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24485 if (attr != NULL && attr->form_is_block ())
24486 {
24487 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24488 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24489 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24490 }
24491 else if (attr != NULL)
24492 {
24493 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24494 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24495 sect_offset_str (die->sect_off));
24496 }
24497
24498 /* Read DW_AT_associated and set in type. */
24499 attr = dwarf2_attr (die, DW_AT_associated, cu);
24500 if (attr != NULL && attr->form_is_block ())
24501 {
24502 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24503 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24504 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24505 }
24506 else if (attr != NULL)
24507 {
24508 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24509 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24510 sect_offset_str (die->sect_off));
24511 }
24512
24513 /* Read DW_AT_data_location and set in type. */
24514 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24515 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24516 cu->per_cu->addr_type ()))
24517 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24518
24519 if (dwarf2_per_objfile->die_type_hash == NULL)
24520 dwarf2_per_objfile->die_type_hash
24521 = htab_up (htab_create_alloc (127,
24522 per_cu_offset_and_type_hash,
24523 per_cu_offset_and_type_eq,
24524 NULL, xcalloc, xfree));
24525
24526 ofs.per_cu = cu->per_cu;
24527 ofs.sect_off = die->sect_off;
24528 ofs.type = type;
24529 slot = (struct dwarf2_per_cu_offset_and_type **)
24530 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24531 if (*slot)
24532 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24533 sect_offset_str (die->sect_off));
24534 *slot = XOBNEW (&objfile->objfile_obstack,
24535 struct dwarf2_per_cu_offset_and_type);
24536 **slot = ofs;
24537 return type;
24538 }
24539
24540 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24541 or return NULL if the die does not have a saved type. */
24542
24543 static struct type *
24544 get_die_type_at_offset (sect_offset sect_off,
24545 struct dwarf2_per_cu_data *per_cu)
24546 {
24547 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24548 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24549
24550 if (dwarf2_per_objfile->die_type_hash == NULL)
24551 return NULL;
24552
24553 ofs.per_cu = per_cu;
24554 ofs.sect_off = sect_off;
24555 slot = ((struct dwarf2_per_cu_offset_and_type *)
24556 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24557 if (slot)
24558 return slot->type;
24559 else
24560 return NULL;
24561 }
24562
24563 /* Look up the type for DIE in CU in die_type_hash,
24564 or return NULL if DIE does not have a saved type. */
24565
24566 static struct type *
24567 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24568 {
24569 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24570 }
24571
24572 /* Add a dependence relationship from CU to REF_PER_CU. */
24573
24574 static void
24575 dwarf2_add_dependence (struct dwarf2_cu *cu,
24576 struct dwarf2_per_cu_data *ref_per_cu)
24577 {
24578 void **slot;
24579
24580 if (cu->dependencies == NULL)
24581 cu->dependencies
24582 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24583 NULL, &cu->comp_unit_obstack,
24584 hashtab_obstack_allocate,
24585 dummy_obstack_deallocate);
24586
24587 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24588 if (*slot == NULL)
24589 *slot = ref_per_cu;
24590 }
24591
24592 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24593 Set the mark field in every compilation unit in the
24594 cache that we must keep because we are keeping CU. */
24595
24596 static int
24597 dwarf2_mark_helper (void **slot, void *data)
24598 {
24599 struct dwarf2_per_cu_data *per_cu;
24600
24601 per_cu = (struct dwarf2_per_cu_data *) *slot;
24602
24603 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24604 reading of the chain. As such dependencies remain valid it is not much
24605 useful to track and undo them during QUIT cleanups. */
24606 if (per_cu->cu == NULL)
24607 return 1;
24608
24609 if (per_cu->cu->mark)
24610 return 1;
24611 per_cu->cu->mark = true;
24612
24613 if (per_cu->cu->dependencies != NULL)
24614 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24615
24616 return 1;
24617 }
24618
24619 /* Set the mark field in CU and in every other compilation unit in the
24620 cache that we must keep because we are keeping CU. */
24621
24622 static void
24623 dwarf2_mark (struct dwarf2_cu *cu)
24624 {
24625 if (cu->mark)
24626 return;
24627 cu->mark = true;
24628 if (cu->dependencies != NULL)
24629 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24630 }
24631
24632 static void
24633 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24634 {
24635 while (per_cu)
24636 {
24637 per_cu->cu->mark = false;
24638 per_cu = per_cu->cu->read_in_chain;
24639 }
24640 }
24641
24642 /* Trivial hash function for partial_die_info: the hash value of a DIE
24643 is its offset in .debug_info for this objfile. */
24644
24645 static hashval_t
24646 partial_die_hash (const void *item)
24647 {
24648 const struct partial_die_info *part_die
24649 = (const struct partial_die_info *) item;
24650
24651 return to_underlying (part_die->sect_off);
24652 }
24653
24654 /* Trivial comparison function for partial_die_info structures: two DIEs
24655 are equal if they have the same offset. */
24656
24657 static int
24658 partial_die_eq (const void *item_lhs, const void *item_rhs)
24659 {
24660 const struct partial_die_info *part_die_lhs
24661 = (const struct partial_die_info *) item_lhs;
24662 const struct partial_die_info *part_die_rhs
24663 = (const struct partial_die_info *) item_rhs;
24664
24665 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24666 }
24667
24668 struct cmd_list_element *set_dwarf_cmdlist;
24669 struct cmd_list_element *show_dwarf_cmdlist;
24670
24671 static void
24672 set_dwarf_cmd (const char *args, int from_tty)
24673 {
24674 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24675 gdb_stdout);
24676 }
24677
24678 static void
24679 show_dwarf_cmd (const char *args, int from_tty)
24680 {
24681 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24682 }
24683
24684 static void
24685 show_check_physname (struct ui_file *file, int from_tty,
24686 struct cmd_list_element *c, const char *value)
24687 {
24688 fprintf_filtered (file,
24689 _("Whether to check \"physname\" is %s.\n"),
24690 value);
24691 }
24692
24693 void _initialize_dwarf2_read ();
24694 void
24695 _initialize_dwarf2_read ()
24696 {
24697 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24698 Set DWARF specific variables.\n\
24699 Configure DWARF variables such as the cache size."),
24700 &set_dwarf_cmdlist, "maintenance set dwarf ",
24701 0/*allow-unknown*/, &maintenance_set_cmdlist);
24702
24703 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24704 Show DWARF specific variables.\n\
24705 Show DWARF variables such as the cache size."),
24706 &show_dwarf_cmdlist, "maintenance show dwarf ",
24707 0/*allow-unknown*/, &maintenance_show_cmdlist);
24708
24709 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24710 &dwarf_max_cache_age, _("\
24711 Set the upper bound on the age of cached DWARF compilation units."), _("\
24712 Show the upper bound on the age of cached DWARF compilation units."), _("\
24713 A higher limit means that cached compilation units will be stored\n\
24714 in memory longer, and more total memory will be used. Zero disables\n\
24715 caching, which can slow down startup."),
24716 NULL,
24717 show_dwarf_max_cache_age,
24718 &set_dwarf_cmdlist,
24719 &show_dwarf_cmdlist);
24720
24721 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24722 Set debugging of the DWARF reader."), _("\
24723 Show debugging of the DWARF reader."), _("\
24724 When enabled (non-zero), debugging messages are printed during DWARF\n\
24725 reading and symtab expansion. A value of 1 (one) provides basic\n\
24726 information. A value greater than 1 provides more verbose information."),
24727 NULL,
24728 NULL,
24729 &setdebuglist, &showdebuglist);
24730
24731 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24732 Set debugging of the DWARF DIE reader."), _("\
24733 Show debugging of the DWARF DIE reader."), _("\
24734 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24735 The value is the maximum depth to print."),
24736 NULL,
24737 NULL,
24738 &setdebuglist, &showdebuglist);
24739
24740 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24741 Set debugging of the dwarf line reader."), _("\
24742 Show debugging of the dwarf line reader."), _("\
24743 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24744 A value of 1 (one) provides basic information.\n\
24745 A value greater than 1 provides more verbose information."),
24746 NULL,
24747 NULL,
24748 &setdebuglist, &showdebuglist);
24749
24750 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24751 Set cross-checking of \"physname\" code against demangler."), _("\
24752 Show cross-checking of \"physname\" code against demangler."), _("\
24753 When enabled, GDB's internal \"physname\" code is checked against\n\
24754 the demangler."),
24755 NULL, show_check_physname,
24756 &setdebuglist, &showdebuglist);
24757
24758 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24759 no_class, &use_deprecated_index_sections, _("\
24760 Set whether to use deprecated gdb_index sections."), _("\
24761 Show whether to use deprecated gdb_index sections."), _("\
24762 When enabled, deprecated .gdb_index sections are used anyway.\n\
24763 Normally they are ignored either because of a missing feature or\n\
24764 performance issue.\n\
24765 Warning: This option must be enabled before gdb reads the file."),
24766 NULL,
24767 NULL,
24768 &setlist, &showlist);
24769
24770 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24771 &dwarf2_locexpr_funcs);
24772 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24773 &dwarf2_loclist_funcs);
24774
24775 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24776 &dwarf2_block_frame_base_locexpr_funcs);
24777 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24778 &dwarf2_block_frame_base_loclist_funcs);
24779
24780 #if GDB_SELF_TEST
24781 selftests::register_test ("dw2_expand_symtabs_matching",
24782 selftests::dw2_expand_symtabs_matching::run_test);
24783 #endif
24784 }
This page took 0.684378 seconds and 5 git commands to generate.