Simplify "want_partial_unit" handling
[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 LONGEST read_checked_initial_length_and_offset
1242 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1243 unsigned int *, unsigned int *);
1244
1245 static sect_offset read_abbrev_offset
1246 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1247 struct dwarf2_section_info *, sect_offset);
1248
1249 static const char *read_indirect_string
1250 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1251 const struct comp_unit_head *, unsigned int *);
1252
1253 static const char *read_indirect_line_string
1254 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1255 const struct comp_unit_head *, unsigned int *);
1256
1257 static const char *read_indirect_string_at_offset
1258 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1259 LONGEST str_offset);
1260
1261 static const char *read_indirect_string_from_dwz
1262 (struct objfile *objfile, struct dwz_file *, LONGEST);
1263
1264 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1265 const gdb_byte *,
1266 unsigned int *);
1267
1268 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1269 ULONGEST str_index);
1270
1271 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1272 ULONGEST str_index);
1273
1274 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1275
1276 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1277 struct dwarf2_cu *);
1278
1279 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1280 unsigned int);
1281
1282 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1283 struct dwarf2_cu *cu);
1284
1285 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1286
1287 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1288 struct dwarf2_cu *cu);
1289
1290 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1291
1292 static struct die_info *die_specification (struct die_info *die,
1293 struct dwarf2_cu **);
1294
1295 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1296 struct dwarf2_cu *cu);
1297
1298 static void dwarf_decode_lines (struct line_header *, const char *,
1299 struct dwarf2_cu *, dwarf2_psymtab *,
1300 CORE_ADDR, int decode_mapping);
1301
1302 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1303 const char *);
1304
1305 static struct symbol *new_symbol (struct die_info *, struct type *,
1306 struct dwarf2_cu *, struct symbol * = NULL);
1307
1308 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1309 struct dwarf2_cu *);
1310
1311 static void dwarf2_const_value_attr (const struct attribute *attr,
1312 struct type *type,
1313 const char *name,
1314 struct obstack *obstack,
1315 struct dwarf2_cu *cu, LONGEST *value,
1316 const gdb_byte **bytes,
1317 struct dwarf2_locexpr_baton **baton);
1318
1319 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1320
1321 static int need_gnat_info (struct dwarf2_cu *);
1322
1323 static struct type *die_descriptive_type (struct die_info *,
1324 struct dwarf2_cu *);
1325
1326 static void set_descriptive_type (struct type *, struct die_info *,
1327 struct dwarf2_cu *);
1328
1329 static struct type *die_containing_type (struct die_info *,
1330 struct dwarf2_cu *);
1331
1332 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1333 struct dwarf2_cu *);
1334
1335 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1336
1337 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1338
1339 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1340
1341 static char *typename_concat (struct obstack *obs, const char *prefix,
1342 const char *suffix, int physname,
1343 struct dwarf2_cu *cu);
1344
1345 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1346
1347 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1348
1349 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1350
1351 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1352
1353 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1354
1355 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1356
1357 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1358 struct dwarf2_cu *, dwarf2_psymtab *);
1359
1360 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1361 values. Keep the items ordered with increasing constraints compliance. */
1362 enum pc_bounds_kind
1363 {
1364 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1365 PC_BOUNDS_NOT_PRESENT,
1366
1367 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1368 were present but they do not form a valid range of PC addresses. */
1369 PC_BOUNDS_INVALID,
1370
1371 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1372 PC_BOUNDS_RANGES,
1373
1374 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1375 PC_BOUNDS_HIGH_LOW,
1376 };
1377
1378 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1379 CORE_ADDR *, CORE_ADDR *,
1380 struct dwarf2_cu *,
1381 dwarf2_psymtab *);
1382
1383 static void get_scope_pc_bounds (struct die_info *,
1384 CORE_ADDR *, CORE_ADDR *,
1385 struct dwarf2_cu *);
1386
1387 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1388 CORE_ADDR, struct dwarf2_cu *);
1389
1390 static void dwarf2_add_field (struct field_info *, struct die_info *,
1391 struct dwarf2_cu *);
1392
1393 static void dwarf2_attach_fields_to_type (struct field_info *,
1394 struct type *, struct dwarf2_cu *);
1395
1396 static void dwarf2_add_member_fn (struct field_info *,
1397 struct die_info *, struct type *,
1398 struct dwarf2_cu *);
1399
1400 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1401 struct type *,
1402 struct dwarf2_cu *);
1403
1404 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1405
1406 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1407
1408 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1409
1410 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1411
1412 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1413
1414 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1415
1416 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1417
1418 static struct type *read_module_type (struct die_info *die,
1419 struct dwarf2_cu *cu);
1420
1421 static const char *namespace_name (struct die_info *die,
1422 int *is_anonymous, struct dwarf2_cu *);
1423
1424 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1425
1426 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1427
1428 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct die_info *read_die_and_siblings_1
1432 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1433 struct die_info *);
1434
1435 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1436 const gdb_byte *info_ptr,
1437 const gdb_byte **new_info_ptr,
1438 struct die_info *parent);
1439
1440 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1441 struct die_info **, const gdb_byte *,
1442 int);
1443
1444 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1445 struct die_info **, const gdb_byte *);
1446
1447 static void process_die (struct die_info *, struct dwarf2_cu *);
1448
1449 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1450 struct obstack *);
1451
1452 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1453
1454 static const char *dwarf2_full_name (const char *name,
1455 struct die_info *die,
1456 struct dwarf2_cu *cu);
1457
1458 static const char *dwarf2_physname (const char *name, struct die_info *die,
1459 struct dwarf2_cu *cu);
1460
1461 static struct die_info *dwarf2_extension (struct die_info *die,
1462 struct dwarf2_cu **);
1463
1464 static const char *dwarf_tag_name (unsigned int);
1465
1466 static const char *dwarf_attr_name (unsigned int);
1467
1468 static const char *dwarf_form_name (unsigned int);
1469
1470 static const char *dwarf_bool_name (unsigned int);
1471
1472 static const char *dwarf_type_encoding_name (unsigned int);
1473
1474 static struct die_info *sibling_die (struct die_info *);
1475
1476 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1477
1478 static void dump_die_for_error (struct die_info *);
1479
1480 static void dump_die_1 (struct ui_file *, int level, int max_level,
1481 struct die_info *);
1482
1483 /*static*/ void dump_die (struct die_info *, int max_level);
1484
1485 static void store_in_ref_table (struct die_info *,
1486 struct dwarf2_cu *);
1487
1488 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1489
1490 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1491
1492 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1493 const struct attribute *,
1494 struct dwarf2_cu **);
1495
1496 static struct die_info *follow_die_ref (struct die_info *,
1497 const struct attribute *,
1498 struct dwarf2_cu **);
1499
1500 static struct die_info *follow_die_sig (struct die_info *,
1501 const struct attribute *,
1502 struct dwarf2_cu **);
1503
1504 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1505 struct dwarf2_cu *);
1506
1507 static struct type *get_DW_AT_signature_type (struct die_info *,
1508 const struct attribute *,
1509 struct dwarf2_cu *);
1510
1511 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1512
1513 static void read_signatured_type (struct signatured_type *);
1514
1515 static int attr_to_dynamic_prop (const struct attribute *attr,
1516 struct die_info *die, struct dwarf2_cu *cu,
1517 struct dynamic_prop *prop, struct type *type);
1518
1519 /* memory allocation interface */
1520
1521 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1522
1523 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1524
1525 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1526
1527 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1528 struct dwarf2_loclist_baton *baton,
1529 const struct attribute *attr);
1530
1531 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1532 struct symbol *sym,
1533 struct dwarf2_cu *cu,
1534 int is_block);
1535
1536 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1537 const gdb_byte *info_ptr,
1538 struct abbrev_info *abbrev);
1539
1540 static hashval_t partial_die_hash (const void *item);
1541
1542 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1543
1544 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1545 (sect_offset sect_off, unsigned int offset_in_dwz,
1546 struct dwarf2_per_objfile *dwarf2_per_objfile);
1547
1548 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1549 struct die_info *comp_unit_die,
1550 enum language pretend_language);
1551
1552 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1553
1554 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1555
1556 static struct type *set_die_type (struct die_info *, struct type *,
1557 struct dwarf2_cu *);
1558
1559 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1562
1563 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1564 enum language);
1565
1566 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
1569 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1570 enum language);
1571
1572 static void dwarf2_add_dependence (struct dwarf2_cu *,
1573 struct dwarf2_per_cu_data *);
1574
1575 static void dwarf2_mark (struct dwarf2_cu *);
1576
1577 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1578
1579 static struct type *get_die_type_at_offset (sect_offset,
1580 struct dwarf2_per_cu_data *);
1581
1582 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1583
1584 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1585 enum language pretend_language);
1586
1587 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1588
1589 /* Class, the destructor of which frees all allocated queue entries. This
1590 will only have work to do if an error was thrown while processing the
1591 dwarf. If no error was thrown then the queue entries should have all
1592 been processed, and freed, as we went along. */
1593
1594 class dwarf2_queue_guard
1595 {
1596 public:
1597 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1598 : m_per_objfile (per_objfile)
1599 {
1600 }
1601
1602 /* Free any entries remaining on the queue. There should only be
1603 entries left if we hit an error while processing the dwarf. */
1604 ~dwarf2_queue_guard ()
1605 {
1606 /* Ensure that no memory is allocated by the queue. */
1607 std::queue<dwarf2_queue_item> empty;
1608 std::swap (m_per_objfile->queue, empty);
1609 }
1610
1611 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1612
1613 private:
1614 dwarf2_per_objfile *m_per_objfile;
1615 };
1616
1617 dwarf2_queue_item::~dwarf2_queue_item ()
1618 {
1619 /* Anything still marked queued is likely to be in an
1620 inconsistent state, so discard it. */
1621 if (per_cu->queued)
1622 {
1623 if (per_cu->cu != NULL)
1624 free_one_cached_comp_unit (per_cu);
1625 per_cu->queued = 0;
1626 }
1627 }
1628
1629 /* The return type of find_file_and_directory. Note, the enclosed
1630 string pointers are only valid while this object is valid. */
1631
1632 struct file_and_directory
1633 {
1634 /* The filename. This is never NULL. */
1635 const char *name;
1636
1637 /* The compilation directory. NULL if not known. If we needed to
1638 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1639 points directly to the DW_AT_comp_dir string attribute owned by
1640 the obstack that owns the DIE. */
1641 const char *comp_dir;
1642
1643 /* If we needed to build a new string for comp_dir, this is what
1644 owns the storage. */
1645 std::string comp_dir_storage;
1646 };
1647
1648 static file_and_directory find_file_and_directory (struct die_info *die,
1649 struct dwarf2_cu *cu);
1650
1651 static htab_up allocate_signatured_type_table (struct objfile *objfile);
1652
1653 static htab_up allocate_dwo_unit_table (struct objfile *objfile);
1654
1655 static struct dwo_unit *lookup_dwo_unit_in_dwp
1656 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1657 struct dwp_file *dwp_file, const char *comp_dir,
1658 ULONGEST signature, int is_debug_types);
1659
1660 static struct dwp_file *get_dwp_file
1661 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1662
1663 static struct dwo_unit *lookup_dwo_comp_unit
1664 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1665
1666 static struct dwo_unit *lookup_dwo_type_unit
1667 (struct signatured_type *, const char *, const char *);
1668
1669 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1670
1671 /* A unique pointer to a dwo_file. */
1672
1673 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1674
1675 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1676
1677 static void check_producer (struct dwarf2_cu *cu);
1678
1679 static void free_line_header_voidp (void *arg);
1680 \f
1681 /* Various complaints about symbol reading that don't abort the process. */
1682
1683 static void
1684 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1685 {
1686 complaint (_("statement list doesn't fit in .debug_line section"));
1687 }
1688
1689 static void
1690 dwarf2_debug_line_missing_file_complaint (void)
1691 {
1692 complaint (_(".debug_line section has line data without a file"));
1693 }
1694
1695 static void
1696 dwarf2_debug_line_missing_end_sequence_complaint (void)
1697 {
1698 complaint (_(".debug_line section has line "
1699 "program sequence without an end"));
1700 }
1701
1702 static void
1703 dwarf2_complex_location_expr_complaint (void)
1704 {
1705 complaint (_("location expression too complex"));
1706 }
1707
1708 static void
1709 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1710 int arg3)
1711 {
1712 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1713 arg1, arg2, arg3);
1714 }
1715
1716 static void
1717 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1718 {
1719 complaint (_("debug info runs off end of %s section"
1720 " [in module %s]"),
1721 section->get_name (),
1722 section->get_file_name ());
1723 }
1724
1725 static void
1726 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1727 {
1728 complaint (_("macro debug info contains a "
1729 "malformed macro definition:\n`%s'"),
1730 arg1);
1731 }
1732
1733 static void
1734 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1735 {
1736 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1737 arg1, arg2);
1738 }
1739
1740 /* Hash function for line_header_hash. */
1741
1742 static hashval_t
1743 line_header_hash (const struct line_header *ofs)
1744 {
1745 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1746 }
1747
1748 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1749
1750 static hashval_t
1751 line_header_hash_voidp (const void *item)
1752 {
1753 const struct line_header *ofs = (const struct line_header *) item;
1754
1755 return line_header_hash (ofs);
1756 }
1757
1758 /* Equality function for line_header_hash. */
1759
1760 static int
1761 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1762 {
1763 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1764 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1765
1766 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1767 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1768 }
1769
1770 \f
1771
1772 /* See declaration. */
1773
1774 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1775 const dwarf2_debug_sections *names,
1776 bool can_copy_)
1777 : objfile (objfile_),
1778 can_copy (can_copy_)
1779 {
1780 if (names == NULL)
1781 names = &dwarf2_elf_names;
1782
1783 bfd *obfd = objfile->obfd;
1784
1785 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1786 locate_sections (obfd, sec, *names);
1787 }
1788
1789 dwarf2_per_objfile::~dwarf2_per_objfile ()
1790 {
1791 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1792 free_cached_comp_units ();
1793
1794 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1795 per_cu->imported_symtabs_free ();
1796
1797 for (signatured_type *sig_type : all_type_units)
1798 sig_type->per_cu.imported_symtabs_free ();
1799
1800 /* Everything else should be on the objfile obstack. */
1801 }
1802
1803 /* See declaration. */
1804
1805 void
1806 dwarf2_per_objfile::free_cached_comp_units ()
1807 {
1808 dwarf2_per_cu_data *per_cu = read_in_chain;
1809 dwarf2_per_cu_data **last_chain = &read_in_chain;
1810 while (per_cu != NULL)
1811 {
1812 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1813
1814 delete per_cu->cu;
1815 *last_chain = next_cu;
1816 per_cu = next_cu;
1817 }
1818 }
1819
1820 /* A helper class that calls free_cached_comp_units on
1821 destruction. */
1822
1823 class free_cached_comp_units
1824 {
1825 public:
1826
1827 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1828 : m_per_objfile (per_objfile)
1829 {
1830 }
1831
1832 ~free_cached_comp_units ()
1833 {
1834 m_per_objfile->free_cached_comp_units ();
1835 }
1836
1837 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1838
1839 private:
1840
1841 dwarf2_per_objfile *m_per_objfile;
1842 };
1843
1844 /* Try to locate the sections we need for DWARF 2 debugging
1845 information and return true if we have enough to do something.
1846 NAMES points to the dwarf2 section names, or is NULL if the standard
1847 ELF names are used. CAN_COPY is true for formats where symbol
1848 interposition is possible and so symbol values must follow copy
1849 relocation rules. */
1850
1851 int
1852 dwarf2_has_info (struct objfile *objfile,
1853 const struct dwarf2_debug_sections *names,
1854 bool can_copy)
1855 {
1856 if (objfile->flags & OBJF_READNEVER)
1857 return 0;
1858
1859 struct dwarf2_per_objfile *dwarf2_per_objfile
1860 = get_dwarf2_per_objfile (objfile);
1861
1862 if (dwarf2_per_objfile == NULL)
1863 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1864 names,
1865 can_copy);
1866
1867 return (!dwarf2_per_objfile->info.is_virtual
1868 && dwarf2_per_objfile->info.s.section != NULL
1869 && !dwarf2_per_objfile->abbrev.is_virtual
1870 && dwarf2_per_objfile->abbrev.s.section != NULL);
1871 }
1872
1873 /* When loading sections, we look either for uncompressed section or for
1874 compressed section names. */
1875
1876 static int
1877 section_is_p (const char *section_name,
1878 const struct dwarf2_section_names *names)
1879 {
1880 if (names->normal != NULL
1881 && strcmp (section_name, names->normal) == 0)
1882 return 1;
1883 if (names->compressed != NULL
1884 && strcmp (section_name, names->compressed) == 0)
1885 return 1;
1886 return 0;
1887 }
1888
1889 /* See declaration. */
1890
1891 void
1892 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1893 const dwarf2_debug_sections &names)
1894 {
1895 flagword aflag = bfd_section_flags (sectp);
1896
1897 if ((aflag & SEC_HAS_CONTENTS) == 0)
1898 {
1899 }
1900 else if (elf_section_data (sectp)->this_hdr.sh_size
1901 > bfd_get_file_size (abfd))
1902 {
1903 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1904 warning (_("Discarding section %s which has a section size (%s"
1905 ") larger than the file size [in module %s]"),
1906 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1907 bfd_get_filename (abfd));
1908 }
1909 else if (section_is_p (sectp->name, &names.info))
1910 {
1911 this->info.s.section = sectp;
1912 this->info.size = bfd_section_size (sectp);
1913 }
1914 else if (section_is_p (sectp->name, &names.abbrev))
1915 {
1916 this->abbrev.s.section = sectp;
1917 this->abbrev.size = bfd_section_size (sectp);
1918 }
1919 else if (section_is_p (sectp->name, &names.line))
1920 {
1921 this->line.s.section = sectp;
1922 this->line.size = bfd_section_size (sectp);
1923 }
1924 else if (section_is_p (sectp->name, &names.loc))
1925 {
1926 this->loc.s.section = sectp;
1927 this->loc.size = bfd_section_size (sectp);
1928 }
1929 else if (section_is_p (sectp->name, &names.loclists))
1930 {
1931 this->loclists.s.section = sectp;
1932 this->loclists.size = bfd_section_size (sectp);
1933 }
1934 else if (section_is_p (sectp->name, &names.macinfo))
1935 {
1936 this->macinfo.s.section = sectp;
1937 this->macinfo.size = bfd_section_size (sectp);
1938 }
1939 else if (section_is_p (sectp->name, &names.macro))
1940 {
1941 this->macro.s.section = sectp;
1942 this->macro.size = bfd_section_size (sectp);
1943 }
1944 else if (section_is_p (sectp->name, &names.str))
1945 {
1946 this->str.s.section = sectp;
1947 this->str.size = bfd_section_size (sectp);
1948 }
1949 else if (section_is_p (sectp->name, &names.str_offsets))
1950 {
1951 this->str_offsets.s.section = sectp;
1952 this->str_offsets.size = bfd_section_size (sectp);
1953 }
1954 else if (section_is_p (sectp->name, &names.line_str))
1955 {
1956 this->line_str.s.section = sectp;
1957 this->line_str.size = bfd_section_size (sectp);
1958 }
1959 else if (section_is_p (sectp->name, &names.addr))
1960 {
1961 this->addr.s.section = sectp;
1962 this->addr.size = bfd_section_size (sectp);
1963 }
1964 else if (section_is_p (sectp->name, &names.frame))
1965 {
1966 this->frame.s.section = sectp;
1967 this->frame.size = bfd_section_size (sectp);
1968 }
1969 else if (section_is_p (sectp->name, &names.eh_frame))
1970 {
1971 this->eh_frame.s.section = sectp;
1972 this->eh_frame.size = bfd_section_size (sectp);
1973 }
1974 else if (section_is_p (sectp->name, &names.ranges))
1975 {
1976 this->ranges.s.section = sectp;
1977 this->ranges.size = bfd_section_size (sectp);
1978 }
1979 else if (section_is_p (sectp->name, &names.rnglists))
1980 {
1981 this->rnglists.s.section = sectp;
1982 this->rnglists.size = bfd_section_size (sectp);
1983 }
1984 else if (section_is_p (sectp->name, &names.types))
1985 {
1986 struct dwarf2_section_info type_section;
1987
1988 memset (&type_section, 0, sizeof (type_section));
1989 type_section.s.section = sectp;
1990 type_section.size = bfd_section_size (sectp);
1991
1992 this->types.push_back (type_section);
1993 }
1994 else if (section_is_p (sectp->name, &names.gdb_index))
1995 {
1996 this->gdb_index.s.section = sectp;
1997 this->gdb_index.size = bfd_section_size (sectp);
1998 }
1999 else if (section_is_p (sectp->name, &names.debug_names))
2000 {
2001 this->debug_names.s.section = sectp;
2002 this->debug_names.size = bfd_section_size (sectp);
2003 }
2004 else if (section_is_p (sectp->name, &names.debug_aranges))
2005 {
2006 this->debug_aranges.s.section = sectp;
2007 this->debug_aranges.size = bfd_section_size (sectp);
2008 }
2009
2010 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2011 && bfd_section_vma (sectp) == 0)
2012 this->has_section_at_zero = true;
2013 }
2014
2015 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2016 SECTION_NAME. */
2017
2018 void
2019 dwarf2_get_section_info (struct objfile *objfile,
2020 enum dwarf2_section_enum sect,
2021 asection **sectp, const gdb_byte **bufp,
2022 bfd_size_type *sizep)
2023 {
2024 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2025 struct dwarf2_section_info *info;
2026
2027 /* We may see an objfile without any DWARF, in which case we just
2028 return nothing. */
2029 if (data == NULL)
2030 {
2031 *sectp = NULL;
2032 *bufp = NULL;
2033 *sizep = 0;
2034 return;
2035 }
2036 switch (sect)
2037 {
2038 case DWARF2_DEBUG_FRAME:
2039 info = &data->frame;
2040 break;
2041 case DWARF2_EH_FRAME:
2042 info = &data->eh_frame;
2043 break;
2044 default:
2045 gdb_assert_not_reached ("unexpected section");
2046 }
2047
2048 info->read (objfile);
2049
2050 *sectp = info->get_bfd_section ();
2051 *bufp = info->buffer;
2052 *sizep = info->size;
2053 }
2054
2055 /* A helper function to find the sections for a .dwz file. */
2056
2057 static void
2058 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2059 {
2060 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2061
2062 /* Note that we only support the standard ELF names, because .dwz
2063 is ELF-only (at the time of writing). */
2064 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2065 {
2066 dwz_file->abbrev.s.section = sectp;
2067 dwz_file->abbrev.size = bfd_section_size (sectp);
2068 }
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2070 {
2071 dwz_file->info.s.section = sectp;
2072 dwz_file->info.size = bfd_section_size (sectp);
2073 }
2074 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2075 {
2076 dwz_file->str.s.section = sectp;
2077 dwz_file->str.size = bfd_section_size (sectp);
2078 }
2079 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2080 {
2081 dwz_file->line.s.section = sectp;
2082 dwz_file->line.size = bfd_section_size (sectp);
2083 }
2084 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2085 {
2086 dwz_file->macro.s.section = sectp;
2087 dwz_file->macro.size = bfd_section_size (sectp);
2088 }
2089 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2090 {
2091 dwz_file->gdb_index.s.section = sectp;
2092 dwz_file->gdb_index.size = bfd_section_size (sectp);
2093 }
2094 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2095 {
2096 dwz_file->debug_names.s.section = sectp;
2097 dwz_file->debug_names.size = bfd_section_size (sectp);
2098 }
2099 }
2100
2101 /* See dwarf2read.h. */
2102
2103 struct dwz_file *
2104 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2105 {
2106 const char *filename;
2107 bfd_size_type buildid_len_arg;
2108 size_t buildid_len;
2109 bfd_byte *buildid;
2110
2111 if (dwarf2_per_objfile->dwz_file != NULL)
2112 return dwarf2_per_objfile->dwz_file.get ();
2113
2114 bfd_set_error (bfd_error_no_error);
2115 gdb::unique_xmalloc_ptr<char> data
2116 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2117 &buildid_len_arg, &buildid));
2118 if (data == NULL)
2119 {
2120 if (bfd_get_error () == bfd_error_no_error)
2121 return NULL;
2122 error (_("could not read '.gnu_debugaltlink' section: %s"),
2123 bfd_errmsg (bfd_get_error ()));
2124 }
2125
2126 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2127
2128 buildid_len = (size_t) buildid_len_arg;
2129
2130 filename = data.get ();
2131
2132 std::string abs_storage;
2133 if (!IS_ABSOLUTE_PATH (filename))
2134 {
2135 gdb::unique_xmalloc_ptr<char> abs
2136 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2137
2138 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2139 filename = abs_storage.c_str ();
2140 }
2141
2142 /* First try the file name given in the section. If that doesn't
2143 work, try to use the build-id instead. */
2144 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2145 if (dwz_bfd != NULL)
2146 {
2147 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2148 dwz_bfd.reset (nullptr);
2149 }
2150
2151 if (dwz_bfd == NULL)
2152 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2153
2154 if (dwz_bfd == NULL)
2155 error (_("could not find '.gnu_debugaltlink' file for %s"),
2156 objfile_name (dwarf2_per_objfile->objfile));
2157
2158 std::unique_ptr<struct dwz_file> result
2159 (new struct dwz_file (std::move (dwz_bfd)));
2160
2161 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2162 result.get ());
2163
2164 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2165 result->dwz_bfd.get ());
2166 dwarf2_per_objfile->dwz_file = std::move (result);
2167 return dwarf2_per_objfile->dwz_file.get ();
2168 }
2169 \f
2170 /* DWARF quick_symbols_functions support. */
2171
2172 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2173 unique line tables, so we maintain a separate table of all .debug_line
2174 derived entries to support the sharing.
2175 All the quick functions need is the list of file names. We discard the
2176 line_header when we're done and don't need to record it here. */
2177 struct quick_file_names
2178 {
2179 /* The data used to construct the hash key. */
2180 struct stmt_list_hash hash;
2181
2182 /* The number of entries in file_names, real_names. */
2183 unsigned int num_file_names;
2184
2185 /* The file names from the line table, after being run through
2186 file_full_name. */
2187 const char **file_names;
2188
2189 /* The file names from the line table after being run through
2190 gdb_realpath. These are computed lazily. */
2191 const char **real_names;
2192 };
2193
2194 /* When using the index (and thus not using psymtabs), each CU has an
2195 object of this type. This is used to hold information needed by
2196 the various "quick" methods. */
2197 struct dwarf2_per_cu_quick_data
2198 {
2199 /* The file table. This can be NULL if there was no file table
2200 or it's currently not read in.
2201 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2202 struct quick_file_names *file_names;
2203
2204 /* The corresponding symbol table. This is NULL if symbols for this
2205 CU have not yet been read. */
2206 struct compunit_symtab *compunit_symtab;
2207
2208 /* A temporary mark bit used when iterating over all CUs in
2209 expand_symtabs_matching. */
2210 unsigned int mark : 1;
2211
2212 /* True if we've tried to read the file table and found there isn't one.
2213 There will be no point in trying to read it again next time. */
2214 unsigned int no_file_data : 1;
2215 };
2216
2217 /* Utility hash function for a stmt_list_hash. */
2218
2219 static hashval_t
2220 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2221 {
2222 hashval_t v = 0;
2223
2224 if (stmt_list_hash->dwo_unit != NULL)
2225 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2226 v += to_underlying (stmt_list_hash->line_sect_off);
2227 return v;
2228 }
2229
2230 /* Utility equality function for a stmt_list_hash. */
2231
2232 static int
2233 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2234 const struct stmt_list_hash *rhs)
2235 {
2236 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2237 return 0;
2238 if (lhs->dwo_unit != NULL
2239 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2240 return 0;
2241
2242 return lhs->line_sect_off == rhs->line_sect_off;
2243 }
2244
2245 /* Hash function for a quick_file_names. */
2246
2247 static hashval_t
2248 hash_file_name_entry (const void *e)
2249 {
2250 const struct quick_file_names *file_data
2251 = (const struct quick_file_names *) e;
2252
2253 return hash_stmt_list_entry (&file_data->hash);
2254 }
2255
2256 /* Equality function for a quick_file_names. */
2257
2258 static int
2259 eq_file_name_entry (const void *a, const void *b)
2260 {
2261 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2262 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2263
2264 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2265 }
2266
2267 /* Delete function for a quick_file_names. */
2268
2269 static void
2270 delete_file_name_entry (void *e)
2271 {
2272 struct quick_file_names *file_data = (struct quick_file_names *) e;
2273 int i;
2274
2275 for (i = 0; i < file_data->num_file_names; ++i)
2276 {
2277 xfree ((void*) file_data->file_names[i]);
2278 if (file_data->real_names)
2279 xfree ((void*) file_data->real_names[i]);
2280 }
2281
2282 /* The space for the struct itself lives on objfile_obstack,
2283 so we don't free it here. */
2284 }
2285
2286 /* Create a quick_file_names hash table. */
2287
2288 static htab_up
2289 create_quick_file_names_table (unsigned int nr_initial_entries)
2290 {
2291 return htab_up (htab_create_alloc (nr_initial_entries,
2292 hash_file_name_entry, eq_file_name_entry,
2293 delete_file_name_entry, xcalloc, xfree));
2294 }
2295
2296 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2297 have to be created afterwards. You should call age_cached_comp_units after
2298 processing PER_CU->CU. dw2_setup must have been already called. */
2299
2300 static void
2301 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2302 {
2303 if (per_cu->is_debug_types)
2304 load_full_type_unit (per_cu);
2305 else
2306 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2307
2308 if (per_cu->cu == NULL)
2309 return; /* Dummy CU. */
2310
2311 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2312 }
2313
2314 /* Read in the symbols for PER_CU. */
2315
2316 static void
2317 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2318 {
2319 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2320
2321 /* Skip type_unit_groups, reading the type units they contain
2322 is handled elsewhere. */
2323 if (IS_TYPE_UNIT_GROUP (per_cu))
2324 return;
2325
2326 /* The destructor of dwarf2_queue_guard frees any entries left on
2327 the queue. After this point we're guaranteed to leave this function
2328 with the dwarf queue empty. */
2329 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2330
2331 if (dwarf2_per_objfile->using_index
2332 ? per_cu->v.quick->compunit_symtab == NULL
2333 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2334 {
2335 queue_comp_unit (per_cu, language_minimal);
2336 load_cu (per_cu, skip_partial);
2337
2338 /* If we just loaded a CU from a DWO, and we're working with an index
2339 that may badly handle TUs, load all the TUs in that DWO as well.
2340 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2341 if (!per_cu->is_debug_types
2342 && per_cu->cu != NULL
2343 && per_cu->cu->dwo_unit != NULL
2344 && dwarf2_per_objfile->index_table != NULL
2345 && dwarf2_per_objfile->index_table->version <= 7
2346 /* DWP files aren't supported yet. */
2347 && get_dwp_file (dwarf2_per_objfile) == NULL)
2348 queue_and_load_all_dwo_tus (per_cu);
2349 }
2350
2351 process_queue (dwarf2_per_objfile);
2352
2353 /* Age the cache, releasing compilation units that have not
2354 been used recently. */
2355 age_cached_comp_units (dwarf2_per_objfile);
2356 }
2357
2358 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2359 the objfile from which this CU came. Returns the resulting symbol
2360 table. */
2361
2362 static struct compunit_symtab *
2363 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2364 {
2365 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2366
2367 gdb_assert (dwarf2_per_objfile->using_index);
2368 if (!per_cu->v.quick->compunit_symtab)
2369 {
2370 free_cached_comp_units freer (dwarf2_per_objfile);
2371 scoped_restore decrementer = increment_reading_symtab ();
2372 dw2_do_instantiate_symtab (per_cu, skip_partial);
2373 process_cu_includes (dwarf2_per_objfile);
2374 }
2375
2376 return per_cu->v.quick->compunit_symtab;
2377 }
2378
2379 /* See declaration. */
2380
2381 dwarf2_per_cu_data *
2382 dwarf2_per_objfile::get_cutu (int index)
2383 {
2384 if (index >= this->all_comp_units.size ())
2385 {
2386 index -= this->all_comp_units.size ();
2387 gdb_assert (index < this->all_type_units.size ());
2388 return &this->all_type_units[index]->per_cu;
2389 }
2390
2391 return this->all_comp_units[index];
2392 }
2393
2394 /* See declaration. */
2395
2396 dwarf2_per_cu_data *
2397 dwarf2_per_objfile::get_cu (int index)
2398 {
2399 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2400
2401 return this->all_comp_units[index];
2402 }
2403
2404 /* See declaration. */
2405
2406 signatured_type *
2407 dwarf2_per_objfile::get_tu (int index)
2408 {
2409 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2410
2411 return this->all_type_units[index];
2412 }
2413
2414 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2415 objfile_obstack, and constructed with the specified field
2416 values. */
2417
2418 static dwarf2_per_cu_data *
2419 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2420 struct dwarf2_section_info *section,
2421 int is_dwz,
2422 sect_offset sect_off, ULONGEST length)
2423 {
2424 struct objfile *objfile = dwarf2_per_objfile->objfile;
2425 dwarf2_per_cu_data *the_cu
2426 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2427 struct dwarf2_per_cu_data);
2428 the_cu->sect_off = sect_off;
2429 the_cu->length = length;
2430 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2431 the_cu->section = section;
2432 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2433 struct dwarf2_per_cu_quick_data);
2434 the_cu->is_dwz = is_dwz;
2435 return the_cu;
2436 }
2437
2438 /* A helper for create_cus_from_index that handles a given list of
2439 CUs. */
2440
2441 static void
2442 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2443 const gdb_byte *cu_list, offset_type n_elements,
2444 struct dwarf2_section_info *section,
2445 int is_dwz)
2446 {
2447 for (offset_type i = 0; i < n_elements; i += 2)
2448 {
2449 gdb_static_assert (sizeof (ULONGEST) >= 8);
2450
2451 sect_offset sect_off
2452 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2453 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2454 cu_list += 2 * 8;
2455
2456 dwarf2_per_cu_data *per_cu
2457 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2458 sect_off, length);
2459 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2460 }
2461 }
2462
2463 /* Read the CU list from the mapped index, and use it to create all
2464 the CU objects for this objfile. */
2465
2466 static void
2467 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2468 const gdb_byte *cu_list, offset_type cu_list_elements,
2469 const gdb_byte *dwz_list, offset_type dwz_elements)
2470 {
2471 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2472 dwarf2_per_objfile->all_comp_units.reserve
2473 ((cu_list_elements + dwz_elements) / 2);
2474
2475 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2476 &dwarf2_per_objfile->info, 0);
2477
2478 if (dwz_elements == 0)
2479 return;
2480
2481 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2482 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2483 &dwz->info, 1);
2484 }
2485
2486 /* Create the signatured type hash table from the index. */
2487
2488 static void
2489 create_signatured_type_table_from_index
2490 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2491 struct dwarf2_section_info *section,
2492 const gdb_byte *bytes,
2493 offset_type elements)
2494 {
2495 struct objfile *objfile = dwarf2_per_objfile->objfile;
2496
2497 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2498 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2499
2500 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2501
2502 for (offset_type i = 0; i < elements; i += 3)
2503 {
2504 struct signatured_type *sig_type;
2505 ULONGEST signature;
2506 void **slot;
2507 cu_offset type_offset_in_tu;
2508
2509 gdb_static_assert (sizeof (ULONGEST) >= 8);
2510 sect_offset sect_off
2511 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2512 type_offset_in_tu
2513 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2514 BFD_ENDIAN_LITTLE);
2515 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2516 bytes += 3 * 8;
2517
2518 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2519 struct signatured_type);
2520 sig_type->signature = signature;
2521 sig_type->type_offset_in_tu = type_offset_in_tu;
2522 sig_type->per_cu.is_debug_types = 1;
2523 sig_type->per_cu.section = section;
2524 sig_type->per_cu.sect_off = sect_off;
2525 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2526 sig_type->per_cu.v.quick
2527 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2528 struct dwarf2_per_cu_quick_data);
2529
2530 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2531 *slot = sig_type;
2532
2533 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2534 }
2535
2536 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2537 }
2538
2539 /* Create the signatured type hash table from .debug_names. */
2540
2541 static void
2542 create_signatured_type_table_from_debug_names
2543 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2544 const mapped_debug_names &map,
2545 struct dwarf2_section_info *section,
2546 struct dwarf2_section_info *abbrev_section)
2547 {
2548 struct objfile *objfile = dwarf2_per_objfile->objfile;
2549
2550 section->read (objfile);
2551 abbrev_section->read (objfile);
2552
2553 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2554 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2555
2556 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2557
2558 for (uint32_t i = 0; i < map.tu_count; ++i)
2559 {
2560 struct signatured_type *sig_type;
2561 void **slot;
2562
2563 sect_offset sect_off
2564 = (sect_offset) (extract_unsigned_integer
2565 (map.tu_table_reordered + i * map.offset_size,
2566 map.offset_size,
2567 map.dwarf5_byte_order));
2568
2569 comp_unit_head cu_header;
2570 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2571 abbrev_section,
2572 section->buffer + to_underlying (sect_off),
2573 rcuh_kind::TYPE);
2574
2575 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2576 struct signatured_type);
2577 sig_type->signature = cu_header.signature;
2578 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2579 sig_type->per_cu.is_debug_types = 1;
2580 sig_type->per_cu.section = section;
2581 sig_type->per_cu.sect_off = sect_off;
2582 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2583 sig_type->per_cu.v.quick
2584 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2585 struct dwarf2_per_cu_quick_data);
2586
2587 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2588 *slot = sig_type;
2589
2590 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2591 }
2592
2593 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2594 }
2595
2596 /* Read the address map data from the mapped index, and use it to
2597 populate the objfile's psymtabs_addrmap. */
2598
2599 static void
2600 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2601 struct mapped_index *index)
2602 {
2603 struct objfile *objfile = dwarf2_per_objfile->objfile;
2604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2605 const gdb_byte *iter, *end;
2606 struct addrmap *mutable_map;
2607 CORE_ADDR baseaddr;
2608
2609 auto_obstack temp_obstack;
2610
2611 mutable_map = addrmap_create_mutable (&temp_obstack);
2612
2613 iter = index->address_table.data ();
2614 end = iter + index->address_table.size ();
2615
2616 baseaddr = objfile->text_section_offset ();
2617
2618 while (iter < end)
2619 {
2620 ULONGEST hi, lo, cu_index;
2621 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2622 iter += 8;
2623 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2624 iter += 8;
2625 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2626 iter += 4;
2627
2628 if (lo > hi)
2629 {
2630 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2631 hex_string (lo), hex_string (hi));
2632 continue;
2633 }
2634
2635 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2636 {
2637 complaint (_(".gdb_index address table has invalid CU number %u"),
2638 (unsigned) cu_index);
2639 continue;
2640 }
2641
2642 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2643 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2644 addrmap_set_empty (mutable_map, lo, hi - 1,
2645 dwarf2_per_objfile->get_cu (cu_index));
2646 }
2647
2648 objfile->partial_symtabs->psymtabs_addrmap
2649 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2650 }
2651
2652 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2653 populate the objfile's psymtabs_addrmap. */
2654
2655 static void
2656 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2657 struct dwarf2_section_info *section)
2658 {
2659 struct objfile *objfile = dwarf2_per_objfile->objfile;
2660 bfd *abfd = objfile->obfd;
2661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2662 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2663
2664 auto_obstack temp_obstack;
2665 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2666
2667 std::unordered_map<sect_offset,
2668 dwarf2_per_cu_data *,
2669 gdb::hash_enum<sect_offset>>
2670 debug_info_offset_to_per_cu;
2671 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2672 {
2673 const auto insertpair
2674 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2675 if (!insertpair.second)
2676 {
2677 warning (_("Section .debug_aranges in %s has duplicate "
2678 "debug_info_offset %s, ignoring .debug_aranges."),
2679 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2680 return;
2681 }
2682 }
2683
2684 section->read (objfile);
2685
2686 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2687
2688 const gdb_byte *addr = section->buffer;
2689
2690 while (addr < section->buffer + section->size)
2691 {
2692 const gdb_byte *const entry_addr = addr;
2693 unsigned int bytes_read;
2694
2695 const LONGEST entry_length = read_initial_length (abfd, addr,
2696 &bytes_read);
2697 addr += bytes_read;
2698
2699 const gdb_byte *const entry_end = addr + entry_length;
2700 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2701 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2702 if (addr + entry_length > section->buffer + section->size)
2703 {
2704 warning (_("Section .debug_aranges in %s entry at offset %s "
2705 "length %s exceeds section length %s, "
2706 "ignoring .debug_aranges."),
2707 objfile_name (objfile),
2708 plongest (entry_addr - section->buffer),
2709 plongest (bytes_read + entry_length),
2710 pulongest (section->size));
2711 return;
2712 }
2713
2714 /* The version number. */
2715 const uint16_t version = read_2_bytes (abfd, addr);
2716 addr += 2;
2717 if (version != 2)
2718 {
2719 warning (_("Section .debug_aranges in %s entry at offset %s "
2720 "has unsupported version %d, ignoring .debug_aranges."),
2721 objfile_name (objfile),
2722 plongest (entry_addr - section->buffer), version);
2723 return;
2724 }
2725
2726 const uint64_t debug_info_offset
2727 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2728 addr += offset_size;
2729 const auto per_cu_it
2730 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2731 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2732 {
2733 warning (_("Section .debug_aranges in %s entry at offset %s "
2734 "debug_info_offset %s does not exists, "
2735 "ignoring .debug_aranges."),
2736 objfile_name (objfile),
2737 plongest (entry_addr - section->buffer),
2738 pulongest (debug_info_offset));
2739 return;
2740 }
2741 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2742
2743 const uint8_t address_size = *addr++;
2744 if (address_size < 1 || address_size > 8)
2745 {
2746 warning (_("Section .debug_aranges in %s entry at offset %s "
2747 "address_size %u is invalid, ignoring .debug_aranges."),
2748 objfile_name (objfile),
2749 plongest (entry_addr - section->buffer), address_size);
2750 return;
2751 }
2752
2753 const uint8_t segment_selector_size = *addr++;
2754 if (segment_selector_size != 0)
2755 {
2756 warning (_("Section .debug_aranges in %s entry at offset %s "
2757 "segment_selector_size %u is not supported, "
2758 "ignoring .debug_aranges."),
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
2761 segment_selector_size);
2762 return;
2763 }
2764
2765 /* Must pad to an alignment boundary that is twice the address
2766 size. It is undocumented by the DWARF standard but GCC does
2767 use it. */
2768 for (size_t padding = ((-(addr - section->buffer))
2769 & (2 * address_size - 1));
2770 padding > 0; padding--)
2771 if (*addr++ != 0)
2772 {
2773 warning (_("Section .debug_aranges in %s entry at offset %s "
2774 "padding is not zero, ignoring .debug_aranges."),
2775 objfile_name (objfile),
2776 plongest (entry_addr - section->buffer));
2777 return;
2778 }
2779
2780 for (;;)
2781 {
2782 if (addr + 2 * address_size > entry_end)
2783 {
2784 warning (_("Section .debug_aranges in %s entry at offset %s "
2785 "address list is not properly terminated, "
2786 "ignoring .debug_aranges."),
2787 objfile_name (objfile),
2788 plongest (entry_addr - section->buffer));
2789 return;
2790 }
2791 ULONGEST start = extract_unsigned_integer (addr, address_size,
2792 dwarf5_byte_order);
2793 addr += address_size;
2794 ULONGEST length = extract_unsigned_integer (addr, address_size,
2795 dwarf5_byte_order);
2796 addr += address_size;
2797 if (start == 0 && length == 0)
2798 break;
2799 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2800 {
2801 /* Symbol was eliminated due to a COMDAT group. */
2802 continue;
2803 }
2804 ULONGEST end = start + length;
2805 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2806 - baseaddr);
2807 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2808 - baseaddr);
2809 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2810 }
2811 }
2812
2813 objfile->partial_symtabs->psymtabs_addrmap
2814 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2815 }
2816
2817 /* Find a slot in the mapped index INDEX for the object named NAME.
2818 If NAME is found, set *VEC_OUT to point to the CU vector in the
2819 constant pool and return true. If NAME cannot be found, return
2820 false. */
2821
2822 static bool
2823 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2824 offset_type **vec_out)
2825 {
2826 offset_type hash;
2827 offset_type slot, step;
2828 int (*cmp) (const char *, const char *);
2829
2830 gdb::unique_xmalloc_ptr<char> without_params;
2831 if (current_language->la_language == language_cplus
2832 || current_language->la_language == language_fortran
2833 || current_language->la_language == language_d)
2834 {
2835 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2836 not contain any. */
2837
2838 if (strchr (name, '(') != NULL)
2839 {
2840 without_params = cp_remove_params (name);
2841
2842 if (without_params != NULL)
2843 name = without_params.get ();
2844 }
2845 }
2846
2847 /* Index version 4 did not support case insensitive searches. But the
2848 indices for case insensitive languages are built in lowercase, therefore
2849 simulate our NAME being searched is also lowercased. */
2850 hash = mapped_index_string_hash ((index->version == 4
2851 && case_sensitivity == case_sensitive_off
2852 ? 5 : index->version),
2853 name);
2854
2855 slot = hash & (index->symbol_table.size () - 1);
2856 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2857 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2858
2859 for (;;)
2860 {
2861 const char *str;
2862
2863 const auto &bucket = index->symbol_table[slot];
2864 if (bucket.name == 0 && bucket.vec == 0)
2865 return false;
2866
2867 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2868 if (!cmp (name, str))
2869 {
2870 *vec_out = (offset_type *) (index->constant_pool
2871 + MAYBE_SWAP (bucket.vec));
2872 return true;
2873 }
2874
2875 slot = (slot + step) & (index->symbol_table.size () - 1);
2876 }
2877 }
2878
2879 /* A helper function that reads the .gdb_index from BUFFER and fills
2880 in MAP. FILENAME is the name of the file containing the data;
2881 it is used for error reporting. DEPRECATED_OK is true if it is
2882 ok to use deprecated sections.
2883
2884 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2885 out parameters that are filled in with information about the CU and
2886 TU lists in the section.
2887
2888 Returns true if all went well, false otherwise. */
2889
2890 static bool
2891 read_gdb_index_from_buffer (struct objfile *objfile,
2892 const char *filename,
2893 bool deprecated_ok,
2894 gdb::array_view<const gdb_byte> buffer,
2895 struct mapped_index *map,
2896 const gdb_byte **cu_list,
2897 offset_type *cu_list_elements,
2898 const gdb_byte **types_list,
2899 offset_type *types_list_elements)
2900 {
2901 const gdb_byte *addr = &buffer[0];
2902
2903 /* Version check. */
2904 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2905 /* Versions earlier than 3 emitted every copy of a psymbol. This
2906 causes the index to behave very poorly for certain requests. Version 3
2907 contained incomplete addrmap. So, it seems better to just ignore such
2908 indices. */
2909 if (version < 4)
2910 {
2911 static int warning_printed = 0;
2912 if (!warning_printed)
2913 {
2914 warning (_("Skipping obsolete .gdb_index section in %s."),
2915 filename);
2916 warning_printed = 1;
2917 }
2918 return 0;
2919 }
2920 /* Index version 4 uses a different hash function than index version
2921 5 and later.
2922
2923 Versions earlier than 6 did not emit psymbols for inlined
2924 functions. Using these files will cause GDB not to be able to
2925 set breakpoints on inlined functions by name, so we ignore these
2926 indices unless the user has done
2927 "set use-deprecated-index-sections on". */
2928 if (version < 6 && !deprecated_ok)
2929 {
2930 static int warning_printed = 0;
2931 if (!warning_printed)
2932 {
2933 warning (_("\
2934 Skipping deprecated .gdb_index section in %s.\n\
2935 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2936 to use the section anyway."),
2937 filename);
2938 warning_printed = 1;
2939 }
2940 return 0;
2941 }
2942 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2943 of the TU (for symbols coming from TUs),
2944 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2945 Plus gold-generated indices can have duplicate entries for global symbols,
2946 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2947 These are just performance bugs, and we can't distinguish gdb-generated
2948 indices from gold-generated ones, so issue no warning here. */
2949
2950 /* Indexes with higher version than the one supported by GDB may be no
2951 longer backward compatible. */
2952 if (version > 8)
2953 return 0;
2954
2955 map->version = version;
2956
2957 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2958
2959 int i = 0;
2960 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2961 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2962 / 8);
2963 ++i;
2964
2965 *types_list = addr + MAYBE_SWAP (metadata[i]);
2966 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2967 - MAYBE_SWAP (metadata[i]))
2968 / 8);
2969 ++i;
2970
2971 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2972 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2973 map->address_table
2974 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2975 ++i;
2976
2977 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2978 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2979 map->symbol_table
2980 = gdb::array_view<mapped_index::symbol_table_slot>
2981 ((mapped_index::symbol_table_slot *) symbol_table,
2982 (mapped_index::symbol_table_slot *) symbol_table_end);
2983
2984 ++i;
2985 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2986
2987 return 1;
2988 }
2989
2990 /* Callback types for dwarf2_read_gdb_index. */
2991
2992 typedef gdb::function_view
2993 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2994 get_gdb_index_contents_ftype;
2995 typedef gdb::function_view
2996 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2997 get_gdb_index_contents_dwz_ftype;
2998
2999 /* Read .gdb_index. If everything went ok, initialize the "quick"
3000 elements of all the CUs and return 1. Otherwise, return 0. */
3001
3002 static int
3003 dwarf2_read_gdb_index
3004 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3005 get_gdb_index_contents_ftype get_gdb_index_contents,
3006 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3007 {
3008 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3009 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3010 struct dwz_file *dwz;
3011 struct objfile *objfile = dwarf2_per_objfile->objfile;
3012
3013 gdb::array_view<const gdb_byte> main_index_contents
3014 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3015
3016 if (main_index_contents.empty ())
3017 return 0;
3018
3019 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3020 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3021 use_deprecated_index_sections,
3022 main_index_contents, map.get (), &cu_list,
3023 &cu_list_elements, &types_list,
3024 &types_list_elements))
3025 return 0;
3026
3027 /* Don't use the index if it's empty. */
3028 if (map->symbol_table.empty ())
3029 return 0;
3030
3031 /* If there is a .dwz file, read it so we can get its CU list as
3032 well. */
3033 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3034 if (dwz != NULL)
3035 {
3036 struct mapped_index dwz_map;
3037 const gdb_byte *dwz_types_ignore;
3038 offset_type dwz_types_elements_ignore;
3039
3040 gdb::array_view<const gdb_byte> dwz_index_content
3041 = get_gdb_index_contents_dwz (objfile, dwz);
3042
3043 if (dwz_index_content.empty ())
3044 return 0;
3045
3046 if (!read_gdb_index_from_buffer (objfile,
3047 bfd_get_filename (dwz->dwz_bfd.get ()),
3048 1, dwz_index_content, &dwz_map,
3049 &dwz_list, &dwz_list_elements,
3050 &dwz_types_ignore,
3051 &dwz_types_elements_ignore))
3052 {
3053 warning (_("could not read '.gdb_index' section from %s; skipping"),
3054 bfd_get_filename (dwz->dwz_bfd.get ()));
3055 return 0;
3056 }
3057 }
3058
3059 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3060 dwz_list, dwz_list_elements);
3061
3062 if (types_list_elements)
3063 {
3064 /* We can only handle a single .debug_types when we have an
3065 index. */
3066 if (dwarf2_per_objfile->types.size () != 1)
3067 return 0;
3068
3069 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3070
3071 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3072 types_list, types_list_elements);
3073 }
3074
3075 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3076
3077 dwarf2_per_objfile->index_table = std::move (map);
3078 dwarf2_per_objfile->using_index = 1;
3079 dwarf2_per_objfile->quick_file_names_table =
3080 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3081
3082 return 1;
3083 }
3084
3085 /* die_reader_func for dw2_get_file_names. */
3086
3087 static void
3088 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3089 const gdb_byte *info_ptr,
3090 struct die_info *comp_unit_die)
3091 {
3092 struct dwarf2_cu *cu = reader->cu;
3093 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3094 struct dwarf2_per_objfile *dwarf2_per_objfile
3095 = cu->per_cu->dwarf2_per_objfile;
3096 struct objfile *objfile = dwarf2_per_objfile->objfile;
3097 struct dwarf2_per_cu_data *lh_cu;
3098 struct attribute *attr;
3099 void **slot;
3100 struct quick_file_names *qfn;
3101
3102 gdb_assert (! this_cu->is_debug_types);
3103
3104 /* Our callers never want to match partial units -- instead they
3105 will match the enclosing full CU. */
3106 if (comp_unit_die->tag == DW_TAG_partial_unit)
3107 {
3108 this_cu->v.quick->no_file_data = 1;
3109 return;
3110 }
3111
3112 lh_cu = this_cu;
3113 slot = NULL;
3114
3115 line_header_up lh;
3116 sect_offset line_offset {};
3117
3118 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3119 if (attr != nullptr)
3120 {
3121 struct quick_file_names find_entry;
3122
3123 line_offset = (sect_offset) DW_UNSND (attr);
3124
3125 /* We may have already read in this line header (TU line header sharing).
3126 If we have we're done. */
3127 find_entry.hash.dwo_unit = cu->dwo_unit;
3128 find_entry.hash.line_sect_off = line_offset;
3129 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3130 &find_entry, INSERT);
3131 if (*slot != NULL)
3132 {
3133 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3134 return;
3135 }
3136
3137 lh = dwarf_decode_line_header (line_offset, cu);
3138 }
3139 if (lh == NULL)
3140 {
3141 lh_cu->v.quick->no_file_data = 1;
3142 return;
3143 }
3144
3145 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3146 qfn->hash.dwo_unit = cu->dwo_unit;
3147 qfn->hash.line_sect_off = line_offset;
3148 gdb_assert (slot != NULL);
3149 *slot = qfn;
3150
3151 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3152
3153 int offset = 0;
3154 if (strcmp (fnd.name, "<unknown>") != 0)
3155 ++offset;
3156
3157 qfn->num_file_names = offset + lh->file_names_size ();
3158 qfn->file_names =
3159 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3160 if (offset != 0)
3161 qfn->file_names[0] = xstrdup (fnd.name);
3162 for (int i = 0; i < lh->file_names_size (); ++i)
3163 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3164 fnd.comp_dir).release ();
3165 qfn->real_names = NULL;
3166
3167 lh_cu->v.quick->file_names = qfn;
3168 }
3169
3170 /* A helper for the "quick" functions which attempts to read the line
3171 table for THIS_CU. */
3172
3173 static struct quick_file_names *
3174 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3175 {
3176 /* This should never be called for TUs. */
3177 gdb_assert (! this_cu->is_debug_types);
3178 /* Nor type unit groups. */
3179 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3180
3181 if (this_cu->v.quick->file_names != NULL)
3182 return this_cu->v.quick->file_names;
3183 /* If we know there is no line data, no point in looking again. */
3184 if (this_cu->v.quick->no_file_data)
3185 return NULL;
3186
3187 cutu_reader reader (this_cu);
3188 if (!reader.dummy_p)
3189 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3190
3191 if (this_cu->v.quick->no_file_data)
3192 return NULL;
3193 return this_cu->v.quick->file_names;
3194 }
3195
3196 /* A helper for the "quick" functions which computes and caches the
3197 real path for a given file name from the line table. */
3198
3199 static const char *
3200 dw2_get_real_path (struct objfile *objfile,
3201 struct quick_file_names *qfn, int index)
3202 {
3203 if (qfn->real_names == NULL)
3204 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3205 qfn->num_file_names, const char *);
3206
3207 if (qfn->real_names[index] == NULL)
3208 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3209
3210 return qfn->real_names[index];
3211 }
3212
3213 static struct symtab *
3214 dw2_find_last_source_symtab (struct objfile *objfile)
3215 {
3216 struct dwarf2_per_objfile *dwarf2_per_objfile
3217 = get_dwarf2_per_objfile (objfile);
3218 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3219 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3220
3221 if (cust == NULL)
3222 return NULL;
3223
3224 return compunit_primary_filetab (cust);
3225 }
3226
3227 /* Traversal function for dw2_forget_cached_source_info. */
3228
3229 static int
3230 dw2_free_cached_file_names (void **slot, void *info)
3231 {
3232 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3233
3234 if (file_data->real_names)
3235 {
3236 int i;
3237
3238 for (i = 0; i < file_data->num_file_names; ++i)
3239 {
3240 xfree ((void*) file_data->real_names[i]);
3241 file_data->real_names[i] = NULL;
3242 }
3243 }
3244
3245 return 1;
3246 }
3247
3248 static void
3249 dw2_forget_cached_source_info (struct objfile *objfile)
3250 {
3251 struct dwarf2_per_objfile *dwarf2_per_objfile
3252 = get_dwarf2_per_objfile (objfile);
3253
3254 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3255 dw2_free_cached_file_names, NULL);
3256 }
3257
3258 /* Helper function for dw2_map_symtabs_matching_filename that expands
3259 the symtabs and calls the iterator. */
3260
3261 static int
3262 dw2_map_expand_apply (struct objfile *objfile,
3263 struct dwarf2_per_cu_data *per_cu,
3264 const char *name, const char *real_path,
3265 gdb::function_view<bool (symtab *)> callback)
3266 {
3267 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3268
3269 /* Don't visit already-expanded CUs. */
3270 if (per_cu->v.quick->compunit_symtab)
3271 return 0;
3272
3273 /* This may expand more than one symtab, and we want to iterate over
3274 all of them. */
3275 dw2_instantiate_symtab (per_cu, false);
3276
3277 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3278 last_made, callback);
3279 }
3280
3281 /* Implementation of the map_symtabs_matching_filename method. */
3282
3283 static bool
3284 dw2_map_symtabs_matching_filename
3285 (struct objfile *objfile, const char *name, const char *real_path,
3286 gdb::function_view<bool (symtab *)> callback)
3287 {
3288 const char *name_basename = lbasename (name);
3289 struct dwarf2_per_objfile *dwarf2_per_objfile
3290 = get_dwarf2_per_objfile (objfile);
3291
3292 /* The rule is CUs specify all the files, including those used by
3293 any TU, so there's no need to scan TUs here. */
3294
3295 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3296 {
3297 /* We only need to look at symtabs not already expanded. */
3298 if (per_cu->v.quick->compunit_symtab)
3299 continue;
3300
3301 quick_file_names *file_data = dw2_get_file_names (per_cu);
3302 if (file_data == NULL)
3303 continue;
3304
3305 for (int j = 0; j < file_data->num_file_names; ++j)
3306 {
3307 const char *this_name = file_data->file_names[j];
3308 const char *this_real_name;
3309
3310 if (compare_filenames_for_search (this_name, name))
3311 {
3312 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3313 callback))
3314 return true;
3315 continue;
3316 }
3317
3318 /* Before we invoke realpath, which can get expensive when many
3319 files are involved, do a quick comparison of the basenames. */
3320 if (! basenames_may_differ
3321 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3322 continue;
3323
3324 this_real_name = dw2_get_real_path (objfile, file_data, j);
3325 if (compare_filenames_for_search (this_real_name, name))
3326 {
3327 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3328 callback))
3329 return true;
3330 continue;
3331 }
3332
3333 if (real_path != NULL)
3334 {
3335 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3336 gdb_assert (IS_ABSOLUTE_PATH (name));
3337 if (this_real_name != NULL
3338 && FILENAME_CMP (real_path, this_real_name) == 0)
3339 {
3340 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3341 callback))
3342 return true;
3343 continue;
3344 }
3345 }
3346 }
3347 }
3348
3349 return false;
3350 }
3351
3352 /* Struct used to manage iterating over all CUs looking for a symbol. */
3353
3354 struct dw2_symtab_iterator
3355 {
3356 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3357 struct dwarf2_per_objfile *dwarf2_per_objfile;
3358 /* If set, only look for symbols that match that block. Valid values are
3359 GLOBAL_BLOCK and STATIC_BLOCK. */
3360 gdb::optional<block_enum> block_index;
3361 /* The kind of symbol we're looking for. */
3362 domain_enum domain;
3363 /* The list of CUs from the index entry of the symbol,
3364 or NULL if not found. */
3365 offset_type *vec;
3366 /* The next element in VEC to look at. */
3367 int next;
3368 /* The number of elements in VEC, or zero if there is no match. */
3369 int length;
3370 /* Have we seen a global version of the symbol?
3371 If so we can ignore all further global instances.
3372 This is to work around gold/15646, inefficient gold-generated
3373 indices. */
3374 int global_seen;
3375 };
3376
3377 /* Initialize the index symtab iterator ITER. */
3378
3379 static void
3380 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3381 struct dwarf2_per_objfile *dwarf2_per_objfile,
3382 gdb::optional<block_enum> block_index,
3383 domain_enum domain,
3384 const char *name)
3385 {
3386 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3387 iter->block_index = block_index;
3388 iter->domain = domain;
3389 iter->next = 0;
3390 iter->global_seen = 0;
3391
3392 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3393
3394 /* index is NULL if OBJF_READNOW. */
3395 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3396 iter->length = MAYBE_SWAP (*iter->vec);
3397 else
3398 {
3399 iter->vec = NULL;
3400 iter->length = 0;
3401 }
3402 }
3403
3404 /* Return the next matching CU or NULL if there are no more. */
3405
3406 static struct dwarf2_per_cu_data *
3407 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3408 {
3409 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3410
3411 for ( ; iter->next < iter->length; ++iter->next)
3412 {
3413 offset_type cu_index_and_attrs =
3414 MAYBE_SWAP (iter->vec[iter->next + 1]);
3415 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3416 gdb_index_symbol_kind symbol_kind =
3417 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3418 /* Only check the symbol attributes if they're present.
3419 Indices prior to version 7 don't record them,
3420 and indices >= 7 may elide them for certain symbols
3421 (gold does this). */
3422 int attrs_valid =
3423 (dwarf2_per_objfile->index_table->version >= 7
3424 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3425
3426 /* Don't crash on bad data. */
3427 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3428 + dwarf2_per_objfile->all_type_units.size ()))
3429 {
3430 complaint (_(".gdb_index entry has bad CU index"
3431 " [in module %s]"),
3432 objfile_name (dwarf2_per_objfile->objfile));
3433 continue;
3434 }
3435
3436 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3437
3438 /* Skip if already read in. */
3439 if (per_cu->v.quick->compunit_symtab)
3440 continue;
3441
3442 /* Check static vs global. */
3443 if (attrs_valid)
3444 {
3445 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3446
3447 if (iter->block_index.has_value ())
3448 {
3449 bool want_static = *iter->block_index == STATIC_BLOCK;
3450
3451 if (is_static != want_static)
3452 continue;
3453 }
3454
3455 /* Work around gold/15646. */
3456 if (!is_static && iter->global_seen)
3457 continue;
3458 if (!is_static)
3459 iter->global_seen = 1;
3460 }
3461
3462 /* Only check the symbol's kind if it has one. */
3463 if (attrs_valid)
3464 {
3465 switch (iter->domain)
3466 {
3467 case VAR_DOMAIN:
3468 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3469 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3470 /* Some types are also in VAR_DOMAIN. */
3471 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3472 continue;
3473 break;
3474 case STRUCT_DOMAIN:
3475 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3476 continue;
3477 break;
3478 case LABEL_DOMAIN:
3479 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3480 continue;
3481 break;
3482 case MODULE_DOMAIN:
3483 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3484 continue;
3485 break;
3486 default:
3487 break;
3488 }
3489 }
3490
3491 ++iter->next;
3492 return per_cu;
3493 }
3494
3495 return NULL;
3496 }
3497
3498 static struct compunit_symtab *
3499 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3500 const char *name, domain_enum domain)
3501 {
3502 struct compunit_symtab *stab_best = NULL;
3503 struct dwarf2_per_objfile *dwarf2_per_objfile
3504 = get_dwarf2_per_objfile (objfile);
3505
3506 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3507
3508 struct dw2_symtab_iterator iter;
3509 struct dwarf2_per_cu_data *per_cu;
3510
3511 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3512
3513 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3514 {
3515 struct symbol *sym, *with_opaque = NULL;
3516 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3517 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3518 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3519
3520 sym = block_find_symbol (block, name, domain,
3521 block_find_non_opaque_type_preferred,
3522 &with_opaque);
3523
3524 /* Some caution must be observed with overloaded functions
3525 and methods, since the index will not contain any overload
3526 information (but NAME might contain it). */
3527
3528 if (sym != NULL
3529 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3530 return stab;
3531 if (with_opaque != NULL
3532 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3533 stab_best = stab;
3534
3535 /* Keep looking through other CUs. */
3536 }
3537
3538 return stab_best;
3539 }
3540
3541 static void
3542 dw2_print_stats (struct objfile *objfile)
3543 {
3544 struct dwarf2_per_objfile *dwarf2_per_objfile
3545 = get_dwarf2_per_objfile (objfile);
3546 int total = (dwarf2_per_objfile->all_comp_units.size ()
3547 + dwarf2_per_objfile->all_type_units.size ());
3548 int count = 0;
3549
3550 for (int i = 0; i < total; ++i)
3551 {
3552 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3553
3554 if (!per_cu->v.quick->compunit_symtab)
3555 ++count;
3556 }
3557 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3558 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3559 }
3560
3561 /* This dumps minimal information about the index.
3562 It is called via "mt print objfiles".
3563 One use is to verify .gdb_index has been loaded by the
3564 gdb.dwarf2/gdb-index.exp testcase. */
3565
3566 static void
3567 dw2_dump (struct objfile *objfile)
3568 {
3569 struct dwarf2_per_objfile *dwarf2_per_objfile
3570 = get_dwarf2_per_objfile (objfile);
3571
3572 gdb_assert (dwarf2_per_objfile->using_index);
3573 printf_filtered (".gdb_index:");
3574 if (dwarf2_per_objfile->index_table != NULL)
3575 {
3576 printf_filtered (" version %d\n",
3577 dwarf2_per_objfile->index_table->version);
3578 }
3579 else
3580 printf_filtered (" faked for \"readnow\"\n");
3581 printf_filtered ("\n");
3582 }
3583
3584 static void
3585 dw2_expand_symtabs_for_function (struct objfile *objfile,
3586 const char *func_name)
3587 {
3588 struct dwarf2_per_objfile *dwarf2_per_objfile
3589 = get_dwarf2_per_objfile (objfile);
3590
3591 struct dw2_symtab_iterator iter;
3592 struct dwarf2_per_cu_data *per_cu;
3593
3594 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3595
3596 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3597 dw2_instantiate_symtab (per_cu, false);
3598
3599 }
3600
3601 static void
3602 dw2_expand_all_symtabs (struct objfile *objfile)
3603 {
3604 struct dwarf2_per_objfile *dwarf2_per_objfile
3605 = get_dwarf2_per_objfile (objfile);
3606 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3607 + dwarf2_per_objfile->all_type_units.size ());
3608
3609 for (int i = 0; i < total_units; ++i)
3610 {
3611 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3612
3613 /* We don't want to directly expand a partial CU, because if we
3614 read it with the wrong language, then assertion failures can
3615 be triggered later on. See PR symtab/23010. So, tell
3616 dw2_instantiate_symtab to skip partial CUs -- any important
3617 partial CU will be read via DW_TAG_imported_unit anyway. */
3618 dw2_instantiate_symtab (per_cu, true);
3619 }
3620 }
3621
3622 static void
3623 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3624 const char *fullname)
3625 {
3626 struct dwarf2_per_objfile *dwarf2_per_objfile
3627 = get_dwarf2_per_objfile (objfile);
3628
3629 /* We don't need to consider type units here.
3630 This is only called for examining code, e.g. expand_line_sal.
3631 There can be an order of magnitude (or more) more type units
3632 than comp units, and we avoid them if we can. */
3633
3634 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3635 {
3636 /* We only need to look at symtabs not already expanded. */
3637 if (per_cu->v.quick->compunit_symtab)
3638 continue;
3639
3640 quick_file_names *file_data = dw2_get_file_names (per_cu);
3641 if (file_data == NULL)
3642 continue;
3643
3644 for (int j = 0; j < file_data->num_file_names; ++j)
3645 {
3646 const char *this_fullname = file_data->file_names[j];
3647
3648 if (filename_cmp (this_fullname, fullname) == 0)
3649 {
3650 dw2_instantiate_symtab (per_cu, false);
3651 break;
3652 }
3653 }
3654 }
3655 }
3656
3657 static void
3658 dw2_map_matching_symbols
3659 (struct objfile *objfile,
3660 const lookup_name_info &name, domain_enum domain,
3661 int global,
3662 gdb::function_view<symbol_found_callback_ftype> callback,
3663 symbol_compare_ftype *ordered_compare)
3664 {
3665 /* Currently unimplemented; used for Ada. The function can be called if the
3666 current language is Ada for a non-Ada objfile using GNU index. As Ada
3667 does not look for non-Ada symbols this function should just return. */
3668 }
3669
3670 /* Starting from a search name, return the string that finds the upper
3671 bound of all strings that start with SEARCH_NAME in a sorted name
3672 list. Returns the empty string to indicate that the upper bound is
3673 the end of the list. */
3674
3675 static std::string
3676 make_sort_after_prefix_name (const char *search_name)
3677 {
3678 /* When looking to complete "func", we find the upper bound of all
3679 symbols that start with "func" by looking for where we'd insert
3680 the closest string that would follow "func" in lexicographical
3681 order. Usually, that's "func"-with-last-character-incremented,
3682 i.e. "fund". Mind non-ASCII characters, though. Usually those
3683 will be UTF-8 multi-byte sequences, but we can't be certain.
3684 Especially mind the 0xff character, which is a valid character in
3685 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3686 rule out compilers allowing it in identifiers. Note that
3687 conveniently, strcmp/strcasecmp are specified to compare
3688 characters interpreted as unsigned char. So what we do is treat
3689 the whole string as a base 256 number composed of a sequence of
3690 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3691 to 0, and carries 1 to the following more-significant position.
3692 If the very first character in SEARCH_NAME ends up incremented
3693 and carries/overflows, then the upper bound is the end of the
3694 list. The string after the empty string is also the empty
3695 string.
3696
3697 Some examples of this operation:
3698
3699 SEARCH_NAME => "+1" RESULT
3700
3701 "abc" => "abd"
3702 "ab\xff" => "ac"
3703 "\xff" "a" "\xff" => "\xff" "b"
3704 "\xff" => ""
3705 "\xff\xff" => ""
3706 "" => ""
3707
3708 Then, with these symbols for example:
3709
3710 func
3711 func1
3712 fund
3713
3714 completing "func" looks for symbols between "func" and
3715 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3716 which finds "func" and "func1", but not "fund".
3717
3718 And with:
3719
3720 funcÿ (Latin1 'ÿ' [0xff])
3721 funcÿ1
3722 fund
3723
3724 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3725 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3726
3727 And with:
3728
3729 ÿÿ (Latin1 'ÿ' [0xff])
3730 ÿÿ1
3731
3732 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3733 the end of the list.
3734 */
3735 std::string after = search_name;
3736 while (!after.empty () && (unsigned char) after.back () == 0xff)
3737 after.pop_back ();
3738 if (!after.empty ())
3739 after.back () = (unsigned char) after.back () + 1;
3740 return after;
3741 }
3742
3743 /* See declaration. */
3744
3745 std::pair<std::vector<name_component>::const_iterator,
3746 std::vector<name_component>::const_iterator>
3747 mapped_index_base::find_name_components_bounds
3748 (const lookup_name_info &lookup_name_without_params, language lang) const
3749 {
3750 auto *name_cmp
3751 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3752
3753 const char *lang_name
3754 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3755
3756 /* Comparison function object for lower_bound that matches against a
3757 given symbol name. */
3758 auto lookup_compare_lower = [&] (const name_component &elem,
3759 const char *name)
3760 {
3761 const char *elem_qualified = this->symbol_name_at (elem.idx);
3762 const char *elem_name = elem_qualified + elem.name_offset;
3763 return name_cmp (elem_name, name) < 0;
3764 };
3765
3766 /* Comparison function object for upper_bound that matches against a
3767 given symbol name. */
3768 auto lookup_compare_upper = [&] (const char *name,
3769 const name_component &elem)
3770 {
3771 const char *elem_qualified = this->symbol_name_at (elem.idx);
3772 const char *elem_name = elem_qualified + elem.name_offset;
3773 return name_cmp (name, elem_name) < 0;
3774 };
3775
3776 auto begin = this->name_components.begin ();
3777 auto end = this->name_components.end ();
3778
3779 /* Find the lower bound. */
3780 auto lower = [&] ()
3781 {
3782 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3783 return begin;
3784 else
3785 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3786 } ();
3787
3788 /* Find the upper bound. */
3789 auto upper = [&] ()
3790 {
3791 if (lookup_name_without_params.completion_mode ())
3792 {
3793 /* In completion mode, we want UPPER to point past all
3794 symbols names that have the same prefix. I.e., with
3795 these symbols, and completing "func":
3796
3797 function << lower bound
3798 function1
3799 other_function << upper bound
3800
3801 We find the upper bound by looking for the insertion
3802 point of "func"-with-last-character-incremented,
3803 i.e. "fund". */
3804 std::string after = make_sort_after_prefix_name (lang_name);
3805 if (after.empty ())
3806 return end;
3807 return std::lower_bound (lower, end, after.c_str (),
3808 lookup_compare_lower);
3809 }
3810 else
3811 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3812 } ();
3813
3814 return {lower, upper};
3815 }
3816
3817 /* See declaration. */
3818
3819 void
3820 mapped_index_base::build_name_components ()
3821 {
3822 if (!this->name_components.empty ())
3823 return;
3824
3825 this->name_components_casing = case_sensitivity;
3826 auto *name_cmp
3827 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3828
3829 /* The code below only knows how to break apart components of C++
3830 symbol names (and other languages that use '::' as
3831 namespace/module separator) and Ada symbol names. */
3832 auto count = this->symbol_name_count ();
3833 for (offset_type idx = 0; idx < count; idx++)
3834 {
3835 if (this->symbol_name_slot_invalid (idx))
3836 continue;
3837
3838 const char *name = this->symbol_name_at (idx);
3839
3840 /* Add each name component to the name component table. */
3841 unsigned int previous_len = 0;
3842
3843 if (strstr (name, "::") != nullptr)
3844 {
3845 for (unsigned int current_len = cp_find_first_component (name);
3846 name[current_len] != '\0';
3847 current_len += cp_find_first_component (name + current_len))
3848 {
3849 gdb_assert (name[current_len] == ':');
3850 this->name_components.push_back ({previous_len, idx});
3851 /* Skip the '::'. */
3852 current_len += 2;
3853 previous_len = current_len;
3854 }
3855 }
3856 else
3857 {
3858 /* Handle the Ada encoded (aka mangled) form here. */
3859 for (const char *iter = strstr (name, "__");
3860 iter != nullptr;
3861 iter = strstr (iter, "__"))
3862 {
3863 this->name_components.push_back ({previous_len, idx});
3864 iter += 2;
3865 previous_len = iter - name;
3866 }
3867 }
3868
3869 this->name_components.push_back ({previous_len, idx});
3870 }
3871
3872 /* Sort name_components elements by name. */
3873 auto name_comp_compare = [&] (const name_component &left,
3874 const name_component &right)
3875 {
3876 const char *left_qualified = this->symbol_name_at (left.idx);
3877 const char *right_qualified = this->symbol_name_at (right.idx);
3878
3879 const char *left_name = left_qualified + left.name_offset;
3880 const char *right_name = right_qualified + right.name_offset;
3881
3882 return name_cmp (left_name, right_name) < 0;
3883 };
3884
3885 std::sort (this->name_components.begin (),
3886 this->name_components.end (),
3887 name_comp_compare);
3888 }
3889
3890 /* Helper for dw2_expand_symtabs_matching that works with a
3891 mapped_index_base instead of the containing objfile. This is split
3892 to a separate function in order to be able to unit test the
3893 name_components matching using a mock mapped_index_base. For each
3894 symbol name that matches, calls MATCH_CALLBACK, passing it the
3895 symbol's index in the mapped_index_base symbol table. */
3896
3897 static void
3898 dw2_expand_symtabs_matching_symbol
3899 (mapped_index_base &index,
3900 const lookup_name_info &lookup_name_in,
3901 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3902 enum search_domain kind,
3903 gdb::function_view<bool (offset_type)> match_callback)
3904 {
3905 lookup_name_info lookup_name_without_params
3906 = lookup_name_in.make_ignore_params ();
3907
3908 /* Build the symbol name component sorted vector, if we haven't
3909 yet. */
3910 index.build_name_components ();
3911
3912 /* The same symbol may appear more than once in the range though.
3913 E.g., if we're looking for symbols that complete "w", and we have
3914 a symbol named "w1::w2", we'll find the two name components for
3915 that same symbol in the range. To be sure we only call the
3916 callback once per symbol, we first collect the symbol name
3917 indexes that matched in a temporary vector and ignore
3918 duplicates. */
3919 std::vector<offset_type> matches;
3920
3921 struct name_and_matcher
3922 {
3923 symbol_name_matcher_ftype *matcher;
3924 const std::string &name;
3925
3926 bool operator== (const name_and_matcher &other) const
3927 {
3928 return matcher == other.matcher && name == other.name;
3929 }
3930 };
3931
3932 /* A vector holding all the different symbol name matchers, for all
3933 languages. */
3934 std::vector<name_and_matcher> matchers;
3935
3936 for (int i = 0; i < nr_languages; i++)
3937 {
3938 enum language lang_e = (enum language) i;
3939
3940 const language_defn *lang = language_def (lang_e);
3941 symbol_name_matcher_ftype *name_matcher
3942 = get_symbol_name_matcher (lang, lookup_name_without_params);
3943
3944 name_and_matcher key {
3945 name_matcher,
3946 lookup_name_without_params.language_lookup_name (lang_e)
3947 };
3948
3949 /* Don't insert the same comparison routine more than once.
3950 Note that we do this linear walk. This is not a problem in
3951 practice because the number of supported languages is
3952 low. */
3953 if (std::find (matchers.begin (), matchers.end (), key)
3954 != matchers.end ())
3955 continue;
3956 matchers.push_back (std::move (key));
3957
3958 auto bounds
3959 = index.find_name_components_bounds (lookup_name_without_params,
3960 lang_e);
3961
3962 /* Now for each symbol name in range, check to see if we have a name
3963 match, and if so, call the MATCH_CALLBACK callback. */
3964
3965 for (; bounds.first != bounds.second; ++bounds.first)
3966 {
3967 const char *qualified = index.symbol_name_at (bounds.first->idx);
3968
3969 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3970 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3971 continue;
3972
3973 matches.push_back (bounds.first->idx);
3974 }
3975 }
3976
3977 std::sort (matches.begin (), matches.end ());
3978
3979 /* Finally call the callback, once per match. */
3980 ULONGEST prev = -1;
3981 for (offset_type idx : matches)
3982 {
3983 if (prev != idx)
3984 {
3985 if (!match_callback (idx))
3986 break;
3987 prev = idx;
3988 }
3989 }
3990
3991 /* Above we use a type wider than idx's for 'prev', since 0 and
3992 (offset_type)-1 are both possible values. */
3993 static_assert (sizeof (prev) > sizeof (offset_type), "");
3994 }
3995
3996 #if GDB_SELF_TEST
3997
3998 namespace selftests { namespace dw2_expand_symtabs_matching {
3999
4000 /* A mock .gdb_index/.debug_names-like name index table, enough to
4001 exercise dw2_expand_symtabs_matching_symbol, which works with the
4002 mapped_index_base interface. Builds an index from the symbol list
4003 passed as parameter to the constructor. */
4004 class mock_mapped_index : public mapped_index_base
4005 {
4006 public:
4007 mock_mapped_index (gdb::array_view<const char *> symbols)
4008 : m_symbol_table (symbols)
4009 {}
4010
4011 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4012
4013 /* Return the number of names in the symbol table. */
4014 size_t symbol_name_count () const override
4015 {
4016 return m_symbol_table.size ();
4017 }
4018
4019 /* Get the name of the symbol at IDX in the symbol table. */
4020 const char *symbol_name_at (offset_type idx) const override
4021 {
4022 return m_symbol_table[idx];
4023 }
4024
4025 private:
4026 gdb::array_view<const char *> m_symbol_table;
4027 };
4028
4029 /* Convenience function that converts a NULL pointer to a "<null>"
4030 string, to pass to print routines. */
4031
4032 static const char *
4033 string_or_null (const char *str)
4034 {
4035 return str != NULL ? str : "<null>";
4036 }
4037
4038 /* Check if a lookup_name_info built from
4039 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4040 index. EXPECTED_LIST is the list of expected matches, in expected
4041 matching order. If no match expected, then an empty list is
4042 specified. Returns true on success. On failure prints a warning
4043 indicating the file:line that failed, and returns false. */
4044
4045 static bool
4046 check_match (const char *file, int line,
4047 mock_mapped_index &mock_index,
4048 const char *name, symbol_name_match_type match_type,
4049 bool completion_mode,
4050 std::initializer_list<const char *> expected_list)
4051 {
4052 lookup_name_info lookup_name (name, match_type, completion_mode);
4053
4054 bool matched = true;
4055
4056 auto mismatch = [&] (const char *expected_str,
4057 const char *got)
4058 {
4059 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4060 "expected=\"%s\", got=\"%s\"\n"),
4061 file, line,
4062 (match_type == symbol_name_match_type::FULL
4063 ? "FULL" : "WILD"),
4064 name, string_or_null (expected_str), string_or_null (got));
4065 matched = false;
4066 };
4067
4068 auto expected_it = expected_list.begin ();
4069 auto expected_end = expected_list.end ();
4070
4071 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4072 NULL, ALL_DOMAIN,
4073 [&] (offset_type idx)
4074 {
4075 const char *matched_name = mock_index.symbol_name_at (idx);
4076 const char *expected_str
4077 = expected_it == expected_end ? NULL : *expected_it++;
4078
4079 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4080 mismatch (expected_str, matched_name);
4081 return true;
4082 });
4083
4084 const char *expected_str
4085 = expected_it == expected_end ? NULL : *expected_it++;
4086 if (expected_str != NULL)
4087 mismatch (expected_str, NULL);
4088
4089 return matched;
4090 }
4091
4092 /* The symbols added to the mock mapped_index for testing (in
4093 canonical form). */
4094 static const char *test_symbols[] = {
4095 "function",
4096 "std::bar",
4097 "std::zfunction",
4098 "std::zfunction2",
4099 "w1::w2",
4100 "ns::foo<char*>",
4101 "ns::foo<int>",
4102 "ns::foo<long>",
4103 "ns2::tmpl<int>::foo2",
4104 "(anonymous namespace)::A::B::C",
4105
4106 /* These are used to check that the increment-last-char in the
4107 matching algorithm for completion doesn't match "t1_fund" when
4108 completing "t1_func". */
4109 "t1_func",
4110 "t1_func1",
4111 "t1_fund",
4112 "t1_fund1",
4113
4114 /* A UTF-8 name with multi-byte sequences to make sure that
4115 cp-name-parser understands this as a single identifier ("função"
4116 is "function" in PT). */
4117 u8"u8função",
4118
4119 /* \377 (0xff) is Latin1 'ÿ'. */
4120 "yfunc\377",
4121
4122 /* \377 (0xff) is Latin1 'ÿ'. */
4123 "\377",
4124 "\377\377123",
4125
4126 /* A name with all sorts of complications. Starts with "z" to make
4127 it easier for the completion tests below. */
4128 #define Z_SYM_NAME \
4129 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4130 "::tuple<(anonymous namespace)::ui*, " \
4131 "std::default_delete<(anonymous namespace)::ui>, void>"
4132
4133 Z_SYM_NAME
4134 };
4135
4136 /* Returns true if the mapped_index_base::find_name_component_bounds
4137 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4138 in completion mode. */
4139
4140 static bool
4141 check_find_bounds_finds (mapped_index_base &index,
4142 const char *search_name,
4143 gdb::array_view<const char *> expected_syms)
4144 {
4145 lookup_name_info lookup_name (search_name,
4146 symbol_name_match_type::FULL, true);
4147
4148 auto bounds = index.find_name_components_bounds (lookup_name,
4149 language_cplus);
4150
4151 size_t distance = std::distance (bounds.first, bounds.second);
4152 if (distance != expected_syms.size ())
4153 return false;
4154
4155 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4156 {
4157 auto nc_elem = bounds.first + exp_elem;
4158 const char *qualified = index.symbol_name_at (nc_elem->idx);
4159 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4160 return false;
4161 }
4162
4163 return true;
4164 }
4165
4166 /* Test the lower-level mapped_index::find_name_component_bounds
4167 method. */
4168
4169 static void
4170 test_mapped_index_find_name_component_bounds ()
4171 {
4172 mock_mapped_index mock_index (test_symbols);
4173
4174 mock_index.build_name_components ();
4175
4176 /* Test the lower-level mapped_index::find_name_component_bounds
4177 method in completion mode. */
4178 {
4179 static const char *expected_syms[] = {
4180 "t1_func",
4181 "t1_func1",
4182 };
4183
4184 SELF_CHECK (check_find_bounds_finds (mock_index,
4185 "t1_func", expected_syms));
4186 }
4187
4188 /* Check that the increment-last-char in the name matching algorithm
4189 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4190 {
4191 static const char *expected_syms1[] = {
4192 "\377",
4193 "\377\377123",
4194 };
4195 SELF_CHECK (check_find_bounds_finds (mock_index,
4196 "\377", expected_syms1));
4197
4198 static const char *expected_syms2[] = {
4199 "\377\377123",
4200 };
4201 SELF_CHECK (check_find_bounds_finds (mock_index,
4202 "\377\377", expected_syms2));
4203 }
4204 }
4205
4206 /* Test dw2_expand_symtabs_matching_symbol. */
4207
4208 static void
4209 test_dw2_expand_symtabs_matching_symbol ()
4210 {
4211 mock_mapped_index mock_index (test_symbols);
4212
4213 /* We let all tests run until the end even if some fails, for debug
4214 convenience. */
4215 bool any_mismatch = false;
4216
4217 /* Create the expected symbols list (an initializer_list). Needed
4218 because lists have commas, and we need to pass them to CHECK,
4219 which is a macro. */
4220 #define EXPECT(...) { __VA_ARGS__ }
4221
4222 /* Wrapper for check_match that passes down the current
4223 __FILE__/__LINE__. */
4224 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4225 any_mismatch |= !check_match (__FILE__, __LINE__, \
4226 mock_index, \
4227 NAME, MATCH_TYPE, COMPLETION_MODE, \
4228 EXPECTED_LIST)
4229
4230 /* Identity checks. */
4231 for (const char *sym : test_symbols)
4232 {
4233 /* Should be able to match all existing symbols. */
4234 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4235 EXPECT (sym));
4236
4237 /* Should be able to match all existing symbols with
4238 parameters. */
4239 std::string with_params = std::string (sym) + "(int)";
4240 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4241 EXPECT (sym));
4242
4243 /* Should be able to match all existing symbols with
4244 parameters and qualifiers. */
4245 with_params = std::string (sym) + " ( int ) const";
4246 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4247 EXPECT (sym));
4248
4249 /* This should really find sym, but cp-name-parser.y doesn't
4250 know about lvalue/rvalue qualifiers yet. */
4251 with_params = std::string (sym) + " ( int ) &&";
4252 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4253 {});
4254 }
4255
4256 /* Check that the name matching algorithm for completion doesn't get
4257 confused with Latin1 'ÿ' / 0xff. */
4258 {
4259 static const char str[] = "\377";
4260 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4261 EXPECT ("\377", "\377\377123"));
4262 }
4263
4264 /* Check that the increment-last-char in the matching algorithm for
4265 completion doesn't match "t1_fund" when completing "t1_func". */
4266 {
4267 static const char str[] = "t1_func";
4268 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4269 EXPECT ("t1_func", "t1_func1"));
4270 }
4271
4272 /* Check that completion mode works at each prefix of the expected
4273 symbol name. */
4274 {
4275 static const char str[] = "function(int)";
4276 size_t len = strlen (str);
4277 std::string lookup;
4278
4279 for (size_t i = 1; i < len; i++)
4280 {
4281 lookup.assign (str, i);
4282 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4283 EXPECT ("function"));
4284 }
4285 }
4286
4287 /* While "w" is a prefix of both components, the match function
4288 should still only be called once. */
4289 {
4290 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4291 EXPECT ("w1::w2"));
4292 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4293 EXPECT ("w1::w2"));
4294 }
4295
4296 /* Same, with a "complicated" symbol. */
4297 {
4298 static const char str[] = Z_SYM_NAME;
4299 size_t len = strlen (str);
4300 std::string lookup;
4301
4302 for (size_t i = 1; i < len; i++)
4303 {
4304 lookup.assign (str, i);
4305 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4306 EXPECT (Z_SYM_NAME));
4307 }
4308 }
4309
4310 /* In FULL mode, an incomplete symbol doesn't match. */
4311 {
4312 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4313 {});
4314 }
4315
4316 /* A complete symbol with parameters matches any overload, since the
4317 index has no overload info. */
4318 {
4319 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4320 EXPECT ("std::zfunction", "std::zfunction2"));
4321 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4322 EXPECT ("std::zfunction", "std::zfunction2"));
4323 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4324 EXPECT ("std::zfunction", "std::zfunction2"));
4325 }
4326
4327 /* Check that whitespace is ignored appropriately. A symbol with a
4328 template argument list. */
4329 {
4330 static const char expected[] = "ns::foo<int>";
4331 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4332 EXPECT (expected));
4333 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4334 EXPECT (expected));
4335 }
4336
4337 /* Check that whitespace is ignored appropriately. A symbol with a
4338 template argument list that includes a pointer. */
4339 {
4340 static const char expected[] = "ns::foo<char*>";
4341 /* Try both completion and non-completion modes. */
4342 static const bool completion_mode[2] = {false, true};
4343 for (size_t i = 0; i < 2; i++)
4344 {
4345 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4346 completion_mode[i], EXPECT (expected));
4347 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4348 completion_mode[i], EXPECT (expected));
4349
4350 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4351 completion_mode[i], EXPECT (expected));
4352 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4353 completion_mode[i], EXPECT (expected));
4354 }
4355 }
4356
4357 {
4358 /* Check method qualifiers are ignored. */
4359 static const char expected[] = "ns::foo<char*>";
4360 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4361 symbol_name_match_type::FULL, true, EXPECT (expected));
4362 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4363 symbol_name_match_type::FULL, true, EXPECT (expected));
4364 CHECK_MATCH ("foo < char * > ( int ) const",
4365 symbol_name_match_type::WILD, true, EXPECT (expected));
4366 CHECK_MATCH ("foo < char * > ( int ) &&",
4367 symbol_name_match_type::WILD, true, EXPECT (expected));
4368 }
4369
4370 /* Test lookup names that don't match anything. */
4371 {
4372 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4373 {});
4374
4375 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4376 {});
4377 }
4378
4379 /* Some wild matching tests, exercising "(anonymous namespace)",
4380 which should not be confused with a parameter list. */
4381 {
4382 static const char *syms[] = {
4383 "A::B::C",
4384 "B::C",
4385 "C",
4386 "A :: B :: C ( int )",
4387 "B :: C ( int )",
4388 "C ( int )",
4389 };
4390
4391 for (const char *s : syms)
4392 {
4393 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4394 EXPECT ("(anonymous namespace)::A::B::C"));
4395 }
4396 }
4397
4398 {
4399 static const char expected[] = "ns2::tmpl<int>::foo2";
4400 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4401 EXPECT (expected));
4402 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4403 EXPECT (expected));
4404 }
4405
4406 SELF_CHECK (!any_mismatch);
4407
4408 #undef EXPECT
4409 #undef CHECK_MATCH
4410 }
4411
4412 static void
4413 run_test ()
4414 {
4415 test_mapped_index_find_name_component_bounds ();
4416 test_dw2_expand_symtabs_matching_symbol ();
4417 }
4418
4419 }} // namespace selftests::dw2_expand_symtabs_matching
4420
4421 #endif /* GDB_SELF_TEST */
4422
4423 /* If FILE_MATCHER is NULL or if PER_CU has
4424 dwarf2_per_cu_quick_data::MARK set (see
4425 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4426 EXPANSION_NOTIFY on it. */
4427
4428 static void
4429 dw2_expand_symtabs_matching_one
4430 (struct dwarf2_per_cu_data *per_cu,
4431 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4432 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4433 {
4434 if (file_matcher == NULL || per_cu->v.quick->mark)
4435 {
4436 bool symtab_was_null
4437 = (per_cu->v.quick->compunit_symtab == NULL);
4438
4439 dw2_instantiate_symtab (per_cu, false);
4440
4441 if (expansion_notify != NULL
4442 && symtab_was_null
4443 && per_cu->v.quick->compunit_symtab != NULL)
4444 expansion_notify (per_cu->v.quick->compunit_symtab);
4445 }
4446 }
4447
4448 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4449 matched, to expand corresponding CUs that were marked. IDX is the
4450 index of the symbol name that matched. */
4451
4452 static void
4453 dw2_expand_marked_cus
4454 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4455 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4456 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4457 search_domain kind)
4458 {
4459 offset_type *vec, vec_len, vec_idx;
4460 bool global_seen = false;
4461 mapped_index &index = *dwarf2_per_objfile->index_table;
4462
4463 vec = (offset_type *) (index.constant_pool
4464 + MAYBE_SWAP (index.symbol_table[idx].vec));
4465 vec_len = MAYBE_SWAP (vec[0]);
4466 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4467 {
4468 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4469 /* This value is only valid for index versions >= 7. */
4470 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4471 gdb_index_symbol_kind symbol_kind =
4472 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4473 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4474 /* Only check the symbol attributes if they're present.
4475 Indices prior to version 7 don't record them,
4476 and indices >= 7 may elide them for certain symbols
4477 (gold does this). */
4478 int attrs_valid =
4479 (index.version >= 7
4480 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4481
4482 /* Work around gold/15646. */
4483 if (attrs_valid)
4484 {
4485 if (!is_static && global_seen)
4486 continue;
4487 if (!is_static)
4488 global_seen = true;
4489 }
4490
4491 /* Only check the symbol's kind if it has one. */
4492 if (attrs_valid)
4493 {
4494 switch (kind)
4495 {
4496 case VARIABLES_DOMAIN:
4497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4498 continue;
4499 break;
4500 case FUNCTIONS_DOMAIN:
4501 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4502 continue;
4503 break;
4504 case TYPES_DOMAIN:
4505 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4506 continue;
4507 break;
4508 case MODULES_DOMAIN:
4509 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4510 continue;
4511 break;
4512 default:
4513 break;
4514 }
4515 }
4516
4517 /* Don't crash on bad data. */
4518 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4519 + dwarf2_per_objfile->all_type_units.size ()))
4520 {
4521 complaint (_(".gdb_index entry has bad CU index"
4522 " [in module %s]"),
4523 objfile_name (dwarf2_per_objfile->objfile));
4524 continue;
4525 }
4526
4527 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4528 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4529 expansion_notify);
4530 }
4531 }
4532
4533 /* If FILE_MATCHER is non-NULL, set all the
4534 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4535 that match FILE_MATCHER. */
4536
4537 static void
4538 dw_expand_symtabs_matching_file_matcher
4539 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4540 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4541 {
4542 if (file_matcher == NULL)
4543 return;
4544
4545 objfile *const objfile = dwarf2_per_objfile->objfile;
4546
4547 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4548 htab_eq_pointer,
4549 NULL, xcalloc, xfree));
4550 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4551 htab_eq_pointer,
4552 NULL, xcalloc, xfree));
4553
4554 /* The rule is CUs specify all the files, including those used by
4555 any TU, so there's no need to scan TUs here. */
4556
4557 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4558 {
4559 QUIT;
4560
4561 per_cu->v.quick->mark = 0;
4562
4563 /* We only need to look at symtabs not already expanded. */
4564 if (per_cu->v.quick->compunit_symtab)
4565 continue;
4566
4567 quick_file_names *file_data = dw2_get_file_names (per_cu);
4568 if (file_data == NULL)
4569 continue;
4570
4571 if (htab_find (visited_not_found.get (), file_data) != NULL)
4572 continue;
4573 else if (htab_find (visited_found.get (), file_data) != NULL)
4574 {
4575 per_cu->v.quick->mark = 1;
4576 continue;
4577 }
4578
4579 for (int j = 0; j < file_data->num_file_names; ++j)
4580 {
4581 const char *this_real_name;
4582
4583 if (file_matcher (file_data->file_names[j], false))
4584 {
4585 per_cu->v.quick->mark = 1;
4586 break;
4587 }
4588
4589 /* Before we invoke realpath, which can get expensive when many
4590 files are involved, do a quick comparison of the basenames. */
4591 if (!basenames_may_differ
4592 && !file_matcher (lbasename (file_data->file_names[j]),
4593 true))
4594 continue;
4595
4596 this_real_name = dw2_get_real_path (objfile, file_data, j);
4597 if (file_matcher (this_real_name, false))
4598 {
4599 per_cu->v.quick->mark = 1;
4600 break;
4601 }
4602 }
4603
4604 void **slot = htab_find_slot (per_cu->v.quick->mark
4605 ? visited_found.get ()
4606 : visited_not_found.get (),
4607 file_data, INSERT);
4608 *slot = file_data;
4609 }
4610 }
4611
4612 static void
4613 dw2_expand_symtabs_matching
4614 (struct objfile *objfile,
4615 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4616 const lookup_name_info &lookup_name,
4617 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4618 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4619 enum search_domain kind)
4620 {
4621 struct dwarf2_per_objfile *dwarf2_per_objfile
4622 = get_dwarf2_per_objfile (objfile);
4623
4624 /* index_table is NULL if OBJF_READNOW. */
4625 if (!dwarf2_per_objfile->index_table)
4626 return;
4627
4628 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4629
4630 mapped_index &index = *dwarf2_per_objfile->index_table;
4631
4632 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4633 symbol_matcher,
4634 kind, [&] (offset_type idx)
4635 {
4636 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4637 expansion_notify, kind);
4638 return true;
4639 });
4640 }
4641
4642 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4643 symtab. */
4644
4645 static struct compunit_symtab *
4646 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4647 CORE_ADDR pc)
4648 {
4649 int i;
4650
4651 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4652 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4653 return cust;
4654
4655 if (cust->includes == NULL)
4656 return NULL;
4657
4658 for (i = 0; cust->includes[i]; ++i)
4659 {
4660 struct compunit_symtab *s = cust->includes[i];
4661
4662 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4663 if (s != NULL)
4664 return s;
4665 }
4666
4667 return NULL;
4668 }
4669
4670 static struct compunit_symtab *
4671 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4672 struct bound_minimal_symbol msymbol,
4673 CORE_ADDR pc,
4674 struct obj_section *section,
4675 int warn_if_readin)
4676 {
4677 struct dwarf2_per_cu_data *data;
4678 struct compunit_symtab *result;
4679
4680 if (!objfile->partial_symtabs->psymtabs_addrmap)
4681 return NULL;
4682
4683 CORE_ADDR baseaddr = objfile->text_section_offset ();
4684 data = (struct dwarf2_per_cu_data *) addrmap_find
4685 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4686 if (!data)
4687 return NULL;
4688
4689 if (warn_if_readin && data->v.quick->compunit_symtab)
4690 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4691 paddress (get_objfile_arch (objfile), pc));
4692
4693 result
4694 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4695 false),
4696 pc);
4697 gdb_assert (result != NULL);
4698 return result;
4699 }
4700
4701 static void
4702 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4703 void *data, int need_fullname)
4704 {
4705 struct dwarf2_per_objfile *dwarf2_per_objfile
4706 = get_dwarf2_per_objfile (objfile);
4707
4708 if (!dwarf2_per_objfile->filenames_cache)
4709 {
4710 dwarf2_per_objfile->filenames_cache.emplace ();
4711
4712 htab_up visited (htab_create_alloc (10,
4713 htab_hash_pointer, htab_eq_pointer,
4714 NULL, xcalloc, xfree));
4715
4716 /* The rule is CUs specify all the files, including those used
4717 by any TU, so there's no need to scan TUs here. We can
4718 ignore file names coming from already-expanded CUs. */
4719
4720 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4721 {
4722 if (per_cu->v.quick->compunit_symtab)
4723 {
4724 void **slot = htab_find_slot (visited.get (),
4725 per_cu->v.quick->file_names,
4726 INSERT);
4727
4728 *slot = per_cu->v.quick->file_names;
4729 }
4730 }
4731
4732 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4733 {
4734 /* We only need to look at symtabs not already expanded. */
4735 if (per_cu->v.quick->compunit_symtab)
4736 continue;
4737
4738 quick_file_names *file_data = dw2_get_file_names (per_cu);
4739 if (file_data == NULL)
4740 continue;
4741
4742 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4743 if (*slot)
4744 {
4745 /* Already visited. */
4746 continue;
4747 }
4748 *slot = file_data;
4749
4750 for (int j = 0; j < file_data->num_file_names; ++j)
4751 {
4752 const char *filename = file_data->file_names[j];
4753 dwarf2_per_objfile->filenames_cache->seen (filename);
4754 }
4755 }
4756 }
4757
4758 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4759 {
4760 gdb::unique_xmalloc_ptr<char> this_real_name;
4761
4762 if (need_fullname)
4763 this_real_name = gdb_realpath (filename);
4764 (*fun) (filename, this_real_name.get (), data);
4765 });
4766 }
4767
4768 static int
4769 dw2_has_symbols (struct objfile *objfile)
4770 {
4771 return 1;
4772 }
4773
4774 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4775 {
4776 dw2_has_symbols,
4777 dw2_find_last_source_symtab,
4778 dw2_forget_cached_source_info,
4779 dw2_map_symtabs_matching_filename,
4780 dw2_lookup_symbol,
4781 dw2_print_stats,
4782 dw2_dump,
4783 dw2_expand_symtabs_for_function,
4784 dw2_expand_all_symtabs,
4785 dw2_expand_symtabs_with_fullname,
4786 dw2_map_matching_symbols,
4787 dw2_expand_symtabs_matching,
4788 dw2_find_pc_sect_compunit_symtab,
4789 NULL,
4790 dw2_map_symbol_filenames
4791 };
4792
4793 /* DWARF-5 debug_names reader. */
4794
4795 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4796 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4797
4798 /* A helper function that reads the .debug_names section in SECTION
4799 and fills in MAP. FILENAME is the name of the file containing the
4800 section; it is used for error reporting.
4801
4802 Returns true if all went well, false otherwise. */
4803
4804 static bool
4805 read_debug_names_from_section (struct objfile *objfile,
4806 const char *filename,
4807 struct dwarf2_section_info *section,
4808 mapped_debug_names &map)
4809 {
4810 if (section->empty ())
4811 return false;
4812
4813 /* Older elfutils strip versions could keep the section in the main
4814 executable while splitting it for the separate debug info file. */
4815 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4816 return false;
4817
4818 section->read (objfile);
4819
4820 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4821
4822 const gdb_byte *addr = section->buffer;
4823
4824 bfd *const abfd = section->get_bfd_owner ();
4825
4826 unsigned int bytes_read;
4827 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4828 addr += bytes_read;
4829
4830 map.dwarf5_is_dwarf64 = bytes_read != 4;
4831 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4832 if (bytes_read + length != section->size)
4833 {
4834 /* There may be multiple per-CU indices. */
4835 warning (_("Section .debug_names in %s length %s does not match "
4836 "section length %s, ignoring .debug_names."),
4837 filename, plongest (bytes_read + length),
4838 pulongest (section->size));
4839 return false;
4840 }
4841
4842 /* The version number. */
4843 uint16_t version = read_2_bytes (abfd, addr);
4844 addr += 2;
4845 if (version != 5)
4846 {
4847 warning (_("Section .debug_names in %s has unsupported version %d, "
4848 "ignoring .debug_names."),
4849 filename, version);
4850 return false;
4851 }
4852
4853 /* Padding. */
4854 uint16_t padding = read_2_bytes (abfd, addr);
4855 addr += 2;
4856 if (padding != 0)
4857 {
4858 warning (_("Section .debug_names in %s has unsupported padding %d, "
4859 "ignoring .debug_names."),
4860 filename, padding);
4861 return false;
4862 }
4863
4864 /* comp_unit_count - The number of CUs in the CU list. */
4865 map.cu_count = read_4_bytes (abfd, addr);
4866 addr += 4;
4867
4868 /* local_type_unit_count - The number of TUs in the local TU
4869 list. */
4870 map.tu_count = read_4_bytes (abfd, addr);
4871 addr += 4;
4872
4873 /* foreign_type_unit_count - The number of TUs in the foreign TU
4874 list. */
4875 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4876 addr += 4;
4877 if (foreign_tu_count != 0)
4878 {
4879 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4880 "ignoring .debug_names."),
4881 filename, static_cast<unsigned long> (foreign_tu_count));
4882 return false;
4883 }
4884
4885 /* bucket_count - The number of hash buckets in the hash lookup
4886 table. */
4887 map.bucket_count = read_4_bytes (abfd, addr);
4888 addr += 4;
4889
4890 /* name_count - The number of unique names in the index. */
4891 map.name_count = read_4_bytes (abfd, addr);
4892 addr += 4;
4893
4894 /* abbrev_table_size - The size in bytes of the abbreviations
4895 table. */
4896 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4897 addr += 4;
4898
4899 /* augmentation_string_size - The size in bytes of the augmentation
4900 string. This value is rounded up to a multiple of 4. */
4901 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4902 addr += 4;
4903 map.augmentation_is_gdb = ((augmentation_string_size
4904 == sizeof (dwarf5_augmentation))
4905 && memcmp (addr, dwarf5_augmentation,
4906 sizeof (dwarf5_augmentation)) == 0);
4907 augmentation_string_size += (-augmentation_string_size) & 3;
4908 addr += augmentation_string_size;
4909
4910 /* List of CUs */
4911 map.cu_table_reordered = addr;
4912 addr += map.cu_count * map.offset_size;
4913
4914 /* List of Local TUs */
4915 map.tu_table_reordered = addr;
4916 addr += map.tu_count * map.offset_size;
4917
4918 /* Hash Lookup Table */
4919 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4920 addr += map.bucket_count * 4;
4921 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4922 addr += map.name_count * 4;
4923
4924 /* Name Table */
4925 map.name_table_string_offs_reordered = addr;
4926 addr += map.name_count * map.offset_size;
4927 map.name_table_entry_offs_reordered = addr;
4928 addr += map.name_count * map.offset_size;
4929
4930 const gdb_byte *abbrev_table_start = addr;
4931 for (;;)
4932 {
4933 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4934 addr += bytes_read;
4935 if (index_num == 0)
4936 break;
4937
4938 const auto insertpair
4939 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4940 if (!insertpair.second)
4941 {
4942 warning (_("Section .debug_names in %s has duplicate index %s, "
4943 "ignoring .debug_names."),
4944 filename, pulongest (index_num));
4945 return false;
4946 }
4947 mapped_debug_names::index_val &indexval = insertpair.first->second;
4948 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4949 addr += bytes_read;
4950
4951 for (;;)
4952 {
4953 mapped_debug_names::index_val::attr attr;
4954 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4955 addr += bytes_read;
4956 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4957 addr += bytes_read;
4958 if (attr.form == DW_FORM_implicit_const)
4959 {
4960 attr.implicit_const = read_signed_leb128 (abfd, addr,
4961 &bytes_read);
4962 addr += bytes_read;
4963 }
4964 if (attr.dw_idx == 0 && attr.form == 0)
4965 break;
4966 indexval.attr_vec.push_back (std::move (attr));
4967 }
4968 }
4969 if (addr != abbrev_table_start + abbrev_table_size)
4970 {
4971 warning (_("Section .debug_names in %s has abbreviation_table "
4972 "of size %s vs. written as %u, ignoring .debug_names."),
4973 filename, plongest (addr - abbrev_table_start),
4974 abbrev_table_size);
4975 return false;
4976 }
4977 map.entry_pool = addr;
4978
4979 return true;
4980 }
4981
4982 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4983 list. */
4984
4985 static void
4986 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4987 const mapped_debug_names &map,
4988 dwarf2_section_info &section,
4989 bool is_dwz)
4990 {
4991 sect_offset sect_off_prev;
4992 for (uint32_t i = 0; i <= map.cu_count; ++i)
4993 {
4994 sect_offset sect_off_next;
4995 if (i < map.cu_count)
4996 {
4997 sect_off_next
4998 = (sect_offset) (extract_unsigned_integer
4999 (map.cu_table_reordered + i * map.offset_size,
5000 map.offset_size,
5001 map.dwarf5_byte_order));
5002 }
5003 else
5004 sect_off_next = (sect_offset) section.size;
5005 if (i >= 1)
5006 {
5007 const ULONGEST length = sect_off_next - sect_off_prev;
5008 dwarf2_per_cu_data *per_cu
5009 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5010 sect_off_prev, length);
5011 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5012 }
5013 sect_off_prev = sect_off_next;
5014 }
5015 }
5016
5017 /* Read the CU list from the mapped index, and use it to create all
5018 the CU objects for this dwarf2_per_objfile. */
5019
5020 static void
5021 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5022 const mapped_debug_names &map,
5023 const mapped_debug_names &dwz_map)
5024 {
5025 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5026 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5027
5028 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5029 dwarf2_per_objfile->info,
5030 false /* is_dwz */);
5031
5032 if (dwz_map.cu_count == 0)
5033 return;
5034
5035 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5036 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5037 true /* is_dwz */);
5038 }
5039
5040 /* Read .debug_names. If everything went ok, initialize the "quick"
5041 elements of all the CUs and return true. Otherwise, return false. */
5042
5043 static bool
5044 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5045 {
5046 std::unique_ptr<mapped_debug_names> map
5047 (new mapped_debug_names (dwarf2_per_objfile));
5048 mapped_debug_names dwz_map (dwarf2_per_objfile);
5049 struct objfile *objfile = dwarf2_per_objfile->objfile;
5050
5051 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5052 &dwarf2_per_objfile->debug_names,
5053 *map))
5054 return false;
5055
5056 /* Don't use the index if it's empty. */
5057 if (map->name_count == 0)
5058 return false;
5059
5060 /* If there is a .dwz file, read it so we can get its CU list as
5061 well. */
5062 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5063 if (dwz != NULL)
5064 {
5065 if (!read_debug_names_from_section (objfile,
5066 bfd_get_filename (dwz->dwz_bfd.get ()),
5067 &dwz->debug_names, dwz_map))
5068 {
5069 warning (_("could not read '.debug_names' section from %s; skipping"),
5070 bfd_get_filename (dwz->dwz_bfd.get ()));
5071 return false;
5072 }
5073 }
5074
5075 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5076
5077 if (map->tu_count != 0)
5078 {
5079 /* We can only handle a single .debug_types when we have an
5080 index. */
5081 if (dwarf2_per_objfile->types.size () != 1)
5082 return false;
5083
5084 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5085
5086 create_signatured_type_table_from_debug_names
5087 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5088 }
5089
5090 create_addrmap_from_aranges (dwarf2_per_objfile,
5091 &dwarf2_per_objfile->debug_aranges);
5092
5093 dwarf2_per_objfile->debug_names_table = std::move (map);
5094 dwarf2_per_objfile->using_index = 1;
5095 dwarf2_per_objfile->quick_file_names_table =
5096 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5097
5098 return true;
5099 }
5100
5101 /* Type used to manage iterating over all CUs looking for a symbol for
5102 .debug_names. */
5103
5104 class dw2_debug_names_iterator
5105 {
5106 public:
5107 dw2_debug_names_iterator (const mapped_debug_names &map,
5108 gdb::optional<block_enum> block_index,
5109 domain_enum domain,
5110 const char *name)
5111 : m_map (map), m_block_index (block_index), m_domain (domain),
5112 m_addr (find_vec_in_debug_names (map, name))
5113 {}
5114
5115 dw2_debug_names_iterator (const mapped_debug_names &map,
5116 search_domain search, uint32_t namei)
5117 : m_map (map),
5118 m_search (search),
5119 m_addr (find_vec_in_debug_names (map, namei))
5120 {}
5121
5122 dw2_debug_names_iterator (const mapped_debug_names &map,
5123 block_enum block_index, domain_enum domain,
5124 uint32_t namei)
5125 : m_map (map), m_block_index (block_index), m_domain (domain),
5126 m_addr (find_vec_in_debug_names (map, namei))
5127 {}
5128
5129 /* Return the next matching CU or NULL if there are no more. */
5130 dwarf2_per_cu_data *next ();
5131
5132 private:
5133 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5134 const char *name);
5135 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5136 uint32_t namei);
5137
5138 /* The internalized form of .debug_names. */
5139 const mapped_debug_names &m_map;
5140
5141 /* If set, only look for symbols that match that block. Valid values are
5142 GLOBAL_BLOCK and STATIC_BLOCK. */
5143 const gdb::optional<block_enum> m_block_index;
5144
5145 /* The kind of symbol we're looking for. */
5146 const domain_enum m_domain = UNDEF_DOMAIN;
5147 const search_domain m_search = ALL_DOMAIN;
5148
5149 /* The list of CUs from the index entry of the symbol, or NULL if
5150 not found. */
5151 const gdb_byte *m_addr;
5152 };
5153
5154 const char *
5155 mapped_debug_names::namei_to_name (uint32_t namei) const
5156 {
5157 const ULONGEST namei_string_offs
5158 = extract_unsigned_integer ((name_table_string_offs_reordered
5159 + namei * offset_size),
5160 offset_size,
5161 dwarf5_byte_order);
5162 return read_indirect_string_at_offset
5163 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5164 }
5165
5166 /* Find a slot in .debug_names for the object named NAME. If NAME is
5167 found, return pointer to its pool data. If NAME cannot be found,
5168 return NULL. */
5169
5170 const gdb_byte *
5171 dw2_debug_names_iterator::find_vec_in_debug_names
5172 (const mapped_debug_names &map, const char *name)
5173 {
5174 int (*cmp) (const char *, const char *);
5175
5176 gdb::unique_xmalloc_ptr<char> without_params;
5177 if (current_language->la_language == language_cplus
5178 || current_language->la_language == language_fortran
5179 || current_language->la_language == language_d)
5180 {
5181 /* NAME is already canonical. Drop any qualifiers as
5182 .debug_names does not contain any. */
5183
5184 if (strchr (name, '(') != NULL)
5185 {
5186 without_params = cp_remove_params (name);
5187 if (without_params != NULL)
5188 name = without_params.get ();
5189 }
5190 }
5191
5192 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5193
5194 const uint32_t full_hash = dwarf5_djb_hash (name);
5195 uint32_t namei
5196 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5197 (map.bucket_table_reordered
5198 + (full_hash % map.bucket_count)), 4,
5199 map.dwarf5_byte_order);
5200 if (namei == 0)
5201 return NULL;
5202 --namei;
5203 if (namei >= map.name_count)
5204 {
5205 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5206 "[in module %s]"),
5207 namei, map.name_count,
5208 objfile_name (map.dwarf2_per_objfile->objfile));
5209 return NULL;
5210 }
5211
5212 for (;;)
5213 {
5214 const uint32_t namei_full_hash
5215 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5216 (map.hash_table_reordered + namei), 4,
5217 map.dwarf5_byte_order);
5218 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5219 return NULL;
5220
5221 if (full_hash == namei_full_hash)
5222 {
5223 const char *const namei_string = map.namei_to_name (namei);
5224
5225 #if 0 /* An expensive sanity check. */
5226 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5227 {
5228 complaint (_("Wrong .debug_names hash for string at index %u "
5229 "[in module %s]"),
5230 namei, objfile_name (dwarf2_per_objfile->objfile));
5231 return NULL;
5232 }
5233 #endif
5234
5235 if (cmp (namei_string, name) == 0)
5236 {
5237 const ULONGEST namei_entry_offs
5238 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5239 + namei * map.offset_size),
5240 map.offset_size, map.dwarf5_byte_order);
5241 return map.entry_pool + namei_entry_offs;
5242 }
5243 }
5244
5245 ++namei;
5246 if (namei >= map.name_count)
5247 return NULL;
5248 }
5249 }
5250
5251 const gdb_byte *
5252 dw2_debug_names_iterator::find_vec_in_debug_names
5253 (const mapped_debug_names &map, uint32_t namei)
5254 {
5255 if (namei >= map.name_count)
5256 {
5257 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5258 "[in module %s]"),
5259 namei, map.name_count,
5260 objfile_name (map.dwarf2_per_objfile->objfile));
5261 return NULL;
5262 }
5263
5264 const ULONGEST namei_entry_offs
5265 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5266 + namei * map.offset_size),
5267 map.offset_size, map.dwarf5_byte_order);
5268 return map.entry_pool + namei_entry_offs;
5269 }
5270
5271 /* See dw2_debug_names_iterator. */
5272
5273 dwarf2_per_cu_data *
5274 dw2_debug_names_iterator::next ()
5275 {
5276 if (m_addr == NULL)
5277 return NULL;
5278
5279 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5280 struct objfile *objfile = dwarf2_per_objfile->objfile;
5281 bfd *const abfd = objfile->obfd;
5282
5283 again:
5284
5285 unsigned int bytes_read;
5286 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5287 m_addr += bytes_read;
5288 if (abbrev == 0)
5289 return NULL;
5290
5291 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5292 if (indexval_it == m_map.abbrev_map.cend ())
5293 {
5294 complaint (_("Wrong .debug_names undefined abbrev code %s "
5295 "[in module %s]"),
5296 pulongest (abbrev), objfile_name (objfile));
5297 return NULL;
5298 }
5299 const mapped_debug_names::index_val &indexval = indexval_it->second;
5300 enum class symbol_linkage {
5301 unknown,
5302 static_,
5303 extern_,
5304 } symbol_linkage_ = symbol_linkage::unknown;
5305 dwarf2_per_cu_data *per_cu = NULL;
5306 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5307 {
5308 ULONGEST ull;
5309 switch (attr.form)
5310 {
5311 case DW_FORM_implicit_const:
5312 ull = attr.implicit_const;
5313 break;
5314 case DW_FORM_flag_present:
5315 ull = 1;
5316 break;
5317 case DW_FORM_udata:
5318 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5319 m_addr += bytes_read;
5320 break;
5321 default:
5322 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5323 dwarf_form_name (attr.form),
5324 objfile_name (objfile));
5325 return NULL;
5326 }
5327 switch (attr.dw_idx)
5328 {
5329 case DW_IDX_compile_unit:
5330 /* Don't crash on bad data. */
5331 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5332 {
5333 complaint (_(".debug_names entry has bad CU index %s"
5334 " [in module %s]"),
5335 pulongest (ull),
5336 objfile_name (dwarf2_per_objfile->objfile));
5337 continue;
5338 }
5339 per_cu = dwarf2_per_objfile->get_cutu (ull);
5340 break;
5341 case DW_IDX_type_unit:
5342 /* Don't crash on bad data. */
5343 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5344 {
5345 complaint (_(".debug_names entry has bad TU index %s"
5346 " [in module %s]"),
5347 pulongest (ull),
5348 objfile_name (dwarf2_per_objfile->objfile));
5349 continue;
5350 }
5351 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5352 break;
5353 case DW_IDX_GNU_internal:
5354 if (!m_map.augmentation_is_gdb)
5355 break;
5356 symbol_linkage_ = symbol_linkage::static_;
5357 break;
5358 case DW_IDX_GNU_external:
5359 if (!m_map.augmentation_is_gdb)
5360 break;
5361 symbol_linkage_ = symbol_linkage::extern_;
5362 break;
5363 }
5364 }
5365
5366 /* Skip if already read in. */
5367 if (per_cu->v.quick->compunit_symtab)
5368 goto again;
5369
5370 /* Check static vs global. */
5371 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5372 {
5373 const bool want_static = *m_block_index == STATIC_BLOCK;
5374 const bool symbol_is_static =
5375 symbol_linkage_ == symbol_linkage::static_;
5376 if (want_static != symbol_is_static)
5377 goto again;
5378 }
5379
5380 /* Match dw2_symtab_iter_next, symbol_kind
5381 and debug_names::psymbol_tag. */
5382 switch (m_domain)
5383 {
5384 case VAR_DOMAIN:
5385 switch (indexval.dwarf_tag)
5386 {
5387 case DW_TAG_variable:
5388 case DW_TAG_subprogram:
5389 /* Some types are also in VAR_DOMAIN. */
5390 case DW_TAG_typedef:
5391 case DW_TAG_structure_type:
5392 break;
5393 default:
5394 goto again;
5395 }
5396 break;
5397 case STRUCT_DOMAIN:
5398 switch (indexval.dwarf_tag)
5399 {
5400 case DW_TAG_typedef:
5401 case DW_TAG_structure_type:
5402 break;
5403 default:
5404 goto again;
5405 }
5406 break;
5407 case LABEL_DOMAIN:
5408 switch (indexval.dwarf_tag)
5409 {
5410 case 0:
5411 case DW_TAG_variable:
5412 break;
5413 default:
5414 goto again;
5415 }
5416 break;
5417 case MODULE_DOMAIN:
5418 switch (indexval.dwarf_tag)
5419 {
5420 case DW_TAG_module:
5421 break;
5422 default:
5423 goto again;
5424 }
5425 break;
5426 default:
5427 break;
5428 }
5429
5430 /* Match dw2_expand_symtabs_matching, symbol_kind and
5431 debug_names::psymbol_tag. */
5432 switch (m_search)
5433 {
5434 case VARIABLES_DOMAIN:
5435 switch (indexval.dwarf_tag)
5436 {
5437 case DW_TAG_variable:
5438 break;
5439 default:
5440 goto again;
5441 }
5442 break;
5443 case FUNCTIONS_DOMAIN:
5444 switch (indexval.dwarf_tag)
5445 {
5446 case DW_TAG_subprogram:
5447 break;
5448 default:
5449 goto again;
5450 }
5451 break;
5452 case TYPES_DOMAIN:
5453 switch (indexval.dwarf_tag)
5454 {
5455 case DW_TAG_typedef:
5456 case DW_TAG_structure_type:
5457 break;
5458 default:
5459 goto again;
5460 }
5461 break;
5462 case MODULES_DOMAIN:
5463 switch (indexval.dwarf_tag)
5464 {
5465 case DW_TAG_module:
5466 break;
5467 default:
5468 goto again;
5469 }
5470 default:
5471 break;
5472 }
5473
5474 return per_cu;
5475 }
5476
5477 static struct compunit_symtab *
5478 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5479 const char *name, domain_enum domain)
5480 {
5481 struct dwarf2_per_objfile *dwarf2_per_objfile
5482 = get_dwarf2_per_objfile (objfile);
5483
5484 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5485 if (!mapp)
5486 {
5487 /* index is NULL if OBJF_READNOW. */
5488 return NULL;
5489 }
5490 const auto &map = *mapp;
5491
5492 dw2_debug_names_iterator iter (map, block_index, domain, name);
5493
5494 struct compunit_symtab *stab_best = NULL;
5495 struct dwarf2_per_cu_data *per_cu;
5496 while ((per_cu = iter.next ()) != NULL)
5497 {
5498 struct symbol *sym, *with_opaque = NULL;
5499 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5500 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5501 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5502
5503 sym = block_find_symbol (block, name, domain,
5504 block_find_non_opaque_type_preferred,
5505 &with_opaque);
5506
5507 /* Some caution must be observed with overloaded functions and
5508 methods, since the index will not contain any overload
5509 information (but NAME might contain it). */
5510
5511 if (sym != NULL
5512 && strcmp_iw (sym->search_name (), name) == 0)
5513 return stab;
5514 if (with_opaque != NULL
5515 && strcmp_iw (with_opaque->search_name (), name) == 0)
5516 stab_best = stab;
5517
5518 /* Keep looking through other CUs. */
5519 }
5520
5521 return stab_best;
5522 }
5523
5524 /* This dumps minimal information about .debug_names. It is called
5525 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5526 uses this to verify that .debug_names has been loaded. */
5527
5528 static void
5529 dw2_debug_names_dump (struct objfile *objfile)
5530 {
5531 struct dwarf2_per_objfile *dwarf2_per_objfile
5532 = get_dwarf2_per_objfile (objfile);
5533
5534 gdb_assert (dwarf2_per_objfile->using_index);
5535 printf_filtered (".debug_names:");
5536 if (dwarf2_per_objfile->debug_names_table)
5537 printf_filtered (" exists\n");
5538 else
5539 printf_filtered (" faked for \"readnow\"\n");
5540 printf_filtered ("\n");
5541 }
5542
5543 static void
5544 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5545 const char *func_name)
5546 {
5547 struct dwarf2_per_objfile *dwarf2_per_objfile
5548 = get_dwarf2_per_objfile (objfile);
5549
5550 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5551 if (dwarf2_per_objfile->debug_names_table)
5552 {
5553 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5554
5555 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5556
5557 struct dwarf2_per_cu_data *per_cu;
5558 while ((per_cu = iter.next ()) != NULL)
5559 dw2_instantiate_symtab (per_cu, false);
5560 }
5561 }
5562
5563 static void
5564 dw2_debug_names_map_matching_symbols
5565 (struct objfile *objfile,
5566 const lookup_name_info &name, domain_enum domain,
5567 int global,
5568 gdb::function_view<symbol_found_callback_ftype> callback,
5569 symbol_compare_ftype *ordered_compare)
5570 {
5571 struct dwarf2_per_objfile *dwarf2_per_objfile
5572 = get_dwarf2_per_objfile (objfile);
5573
5574 /* debug_names_table is NULL if OBJF_READNOW. */
5575 if (!dwarf2_per_objfile->debug_names_table)
5576 return;
5577
5578 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5579 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5580
5581 const char *match_name = name.ada ().lookup_name ().c_str ();
5582 auto matcher = [&] (const char *symname)
5583 {
5584 if (ordered_compare == nullptr)
5585 return true;
5586 return ordered_compare (symname, match_name) == 0;
5587 };
5588
5589 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5590 [&] (offset_type namei)
5591 {
5592 /* The name was matched, now expand corresponding CUs that were
5593 marked. */
5594 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5595
5596 struct dwarf2_per_cu_data *per_cu;
5597 while ((per_cu = iter.next ()) != NULL)
5598 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5599 return true;
5600 });
5601
5602 /* It's a shame we couldn't do this inside the
5603 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5604 that have already been expanded. Instead, this loop matches what
5605 the psymtab code does. */
5606 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5607 {
5608 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5609 if (cust != nullptr)
5610 {
5611 const struct block *block
5612 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5613 if (!iterate_over_symbols_terminated (block, name,
5614 domain, callback))
5615 break;
5616 }
5617 }
5618 }
5619
5620 static void
5621 dw2_debug_names_expand_symtabs_matching
5622 (struct objfile *objfile,
5623 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5624 const lookup_name_info &lookup_name,
5625 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5626 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5627 enum search_domain kind)
5628 {
5629 struct dwarf2_per_objfile *dwarf2_per_objfile
5630 = get_dwarf2_per_objfile (objfile);
5631
5632 /* debug_names_table is NULL if OBJF_READNOW. */
5633 if (!dwarf2_per_objfile->debug_names_table)
5634 return;
5635
5636 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5637
5638 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5639
5640 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5641 symbol_matcher,
5642 kind, [&] (offset_type namei)
5643 {
5644 /* The name was matched, now expand corresponding CUs that were
5645 marked. */
5646 dw2_debug_names_iterator iter (map, kind, namei);
5647
5648 struct dwarf2_per_cu_data *per_cu;
5649 while ((per_cu = iter.next ()) != NULL)
5650 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5651 expansion_notify);
5652 return true;
5653 });
5654 }
5655
5656 const struct quick_symbol_functions dwarf2_debug_names_functions =
5657 {
5658 dw2_has_symbols,
5659 dw2_find_last_source_symtab,
5660 dw2_forget_cached_source_info,
5661 dw2_map_symtabs_matching_filename,
5662 dw2_debug_names_lookup_symbol,
5663 dw2_print_stats,
5664 dw2_debug_names_dump,
5665 dw2_debug_names_expand_symtabs_for_function,
5666 dw2_expand_all_symtabs,
5667 dw2_expand_symtabs_with_fullname,
5668 dw2_debug_names_map_matching_symbols,
5669 dw2_debug_names_expand_symtabs_matching,
5670 dw2_find_pc_sect_compunit_symtab,
5671 NULL,
5672 dw2_map_symbol_filenames
5673 };
5674
5675 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5676 to either a dwarf2_per_objfile or dwz_file object. */
5677
5678 template <typename T>
5679 static gdb::array_view<const gdb_byte>
5680 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5681 {
5682 dwarf2_section_info *section = &section_owner->gdb_index;
5683
5684 if (section->empty ())
5685 return {};
5686
5687 /* Older elfutils strip versions could keep the section in the main
5688 executable while splitting it for the separate debug info file. */
5689 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5690 return {};
5691
5692 section->read (obj);
5693
5694 /* dwarf2_section_info::size is a bfd_size_type, while
5695 gdb::array_view works with size_t. On 32-bit hosts, with
5696 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5697 is 32-bit. So we need an explicit narrowing conversion here.
5698 This is fine, because it's impossible to allocate or mmap an
5699 array/buffer larger than what size_t can represent. */
5700 return gdb::make_array_view (section->buffer, section->size);
5701 }
5702
5703 /* Lookup the index cache for the contents of the index associated to
5704 DWARF2_OBJ. */
5705
5706 static gdb::array_view<const gdb_byte>
5707 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5708 {
5709 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5710 if (build_id == nullptr)
5711 return {};
5712
5713 return global_index_cache.lookup_gdb_index (build_id,
5714 &dwarf2_obj->index_cache_res);
5715 }
5716
5717 /* Same as the above, but for DWZ. */
5718
5719 static gdb::array_view<const gdb_byte>
5720 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5721 {
5722 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5723 if (build_id == nullptr)
5724 return {};
5725
5726 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5727 }
5728
5729 /* See symfile.h. */
5730
5731 bool
5732 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5733 {
5734 struct dwarf2_per_objfile *dwarf2_per_objfile
5735 = get_dwarf2_per_objfile (objfile);
5736
5737 /* If we're about to read full symbols, don't bother with the
5738 indices. In this case we also don't care if some other debug
5739 format is making psymtabs, because they are all about to be
5740 expanded anyway. */
5741 if ((objfile->flags & OBJF_READNOW))
5742 {
5743 dwarf2_per_objfile->using_index = 1;
5744 create_all_comp_units (dwarf2_per_objfile);
5745 create_all_type_units (dwarf2_per_objfile);
5746 dwarf2_per_objfile->quick_file_names_table
5747 = create_quick_file_names_table
5748 (dwarf2_per_objfile->all_comp_units.size ());
5749
5750 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5751 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5752 {
5753 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5754
5755 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5756 struct dwarf2_per_cu_quick_data);
5757 }
5758
5759 /* Return 1 so that gdb sees the "quick" functions. However,
5760 these functions will be no-ops because we will have expanded
5761 all symtabs. */
5762 *index_kind = dw_index_kind::GDB_INDEX;
5763 return true;
5764 }
5765
5766 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5767 {
5768 *index_kind = dw_index_kind::DEBUG_NAMES;
5769 return true;
5770 }
5771
5772 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5773 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5774 get_gdb_index_contents_from_section<dwz_file>))
5775 {
5776 *index_kind = dw_index_kind::GDB_INDEX;
5777 return true;
5778 }
5779
5780 /* ... otherwise, try to find the index in the index cache. */
5781 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5782 get_gdb_index_contents_from_cache,
5783 get_gdb_index_contents_from_cache_dwz))
5784 {
5785 global_index_cache.hit ();
5786 *index_kind = dw_index_kind::GDB_INDEX;
5787 return true;
5788 }
5789
5790 global_index_cache.miss ();
5791 return false;
5792 }
5793
5794 \f
5795
5796 /* Build a partial symbol table. */
5797
5798 void
5799 dwarf2_build_psymtabs (struct objfile *objfile)
5800 {
5801 struct dwarf2_per_objfile *dwarf2_per_objfile
5802 = get_dwarf2_per_objfile (objfile);
5803
5804 init_psymbol_list (objfile, 1024);
5805
5806 try
5807 {
5808 /* This isn't really ideal: all the data we allocate on the
5809 objfile's obstack is still uselessly kept around. However,
5810 freeing it seems unsafe. */
5811 psymtab_discarder psymtabs (objfile);
5812 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5813 psymtabs.keep ();
5814
5815 /* (maybe) store an index in the cache. */
5816 global_index_cache.store (dwarf2_per_objfile);
5817 }
5818 catch (const gdb_exception_error &except)
5819 {
5820 exception_print (gdb_stderr, except);
5821 }
5822 }
5823
5824 /* Find the base address of the compilation unit for range lists and
5825 location lists. It will normally be specified by DW_AT_low_pc.
5826 In DWARF-3 draft 4, the base address could be overridden by
5827 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5828 compilation units with discontinuous ranges. */
5829
5830 static void
5831 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5832 {
5833 struct attribute *attr;
5834
5835 cu->base_known = 0;
5836 cu->base_address = 0;
5837
5838 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5839 if (attr != nullptr)
5840 {
5841 cu->base_address = attr->value_as_address ();
5842 cu->base_known = 1;
5843 }
5844 else
5845 {
5846 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5847 if (attr != nullptr)
5848 {
5849 cu->base_address = attr->value_as_address ();
5850 cu->base_known = 1;
5851 }
5852 }
5853 }
5854
5855 /* Helper function that returns the proper abbrev section for
5856 THIS_CU. */
5857
5858 static struct dwarf2_section_info *
5859 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5860 {
5861 struct dwarf2_section_info *abbrev;
5862 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5863
5864 if (this_cu->is_dwz)
5865 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5866 else
5867 abbrev = &dwarf2_per_objfile->abbrev;
5868
5869 return abbrev;
5870 }
5871
5872 /* Fetch the abbreviation table offset from a comp or type unit header. */
5873
5874 static sect_offset
5875 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5876 struct dwarf2_section_info *section,
5877 sect_offset sect_off)
5878 {
5879 bfd *abfd = section->get_bfd_owner ();
5880 const gdb_byte *info_ptr;
5881 unsigned int initial_length_size, offset_size;
5882 uint16_t version;
5883
5884 section->read (dwarf2_per_objfile->objfile);
5885 info_ptr = section->buffer + to_underlying (sect_off);
5886 read_initial_length (abfd, info_ptr, &initial_length_size);
5887 offset_size = initial_length_size == 4 ? 4 : 8;
5888 info_ptr += initial_length_size;
5889
5890 version = read_2_bytes (abfd, info_ptr);
5891 info_ptr += 2;
5892 if (version >= 5)
5893 {
5894 /* Skip unit type and address size. */
5895 info_ptr += 2;
5896 }
5897
5898 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5899 }
5900
5901 /* Allocate a new partial symtab for file named NAME and mark this new
5902 partial symtab as being an include of PST. */
5903
5904 static void
5905 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5906 struct objfile *objfile)
5907 {
5908 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
5909
5910 if (!IS_ABSOLUTE_PATH (subpst->filename))
5911 {
5912 /* It shares objfile->objfile_obstack. */
5913 subpst->dirname = pst->dirname;
5914 }
5915
5916 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5917 subpst->dependencies[0] = pst;
5918 subpst->number_of_dependencies = 1;
5919
5920 /* No private part is necessary for include psymtabs. This property
5921 can be used to differentiate between such include psymtabs and
5922 the regular ones. */
5923 subpst->per_cu_data = nullptr;
5924 }
5925
5926 /* Read the Line Number Program data and extract the list of files
5927 included by the source file represented by PST. Build an include
5928 partial symtab for each of these included files. */
5929
5930 static void
5931 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5932 struct die_info *die,
5933 dwarf2_psymtab *pst)
5934 {
5935 line_header_up lh;
5936 struct attribute *attr;
5937
5938 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5939 if (attr != nullptr)
5940 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5941 if (lh == NULL)
5942 return; /* No linetable, so no includes. */
5943
5944 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5945 that we pass in the raw text_low here; that is ok because we're
5946 only decoding the line table to make include partial symtabs, and
5947 so the addresses aren't really used. */
5948 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5949 pst->raw_text_low (), 1);
5950 }
5951
5952 static hashval_t
5953 hash_signatured_type (const void *item)
5954 {
5955 const struct signatured_type *sig_type
5956 = (const struct signatured_type *) item;
5957
5958 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5959 return sig_type->signature;
5960 }
5961
5962 static int
5963 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5964 {
5965 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5966 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5967
5968 return lhs->signature == rhs->signature;
5969 }
5970
5971 /* Allocate a hash table for signatured types. */
5972
5973 static htab_up
5974 allocate_signatured_type_table (struct objfile *objfile)
5975 {
5976 return htab_up (htab_create_alloc (41,
5977 hash_signatured_type,
5978 eq_signatured_type,
5979 NULL, xcalloc, xfree));
5980 }
5981
5982 /* A helper function to add a signatured type CU to a table. */
5983
5984 static int
5985 add_signatured_type_cu_to_table (void **slot, void *datum)
5986 {
5987 struct signatured_type *sigt = (struct signatured_type *) *slot;
5988 std::vector<signatured_type *> *all_type_units
5989 = (std::vector<signatured_type *> *) datum;
5990
5991 all_type_units->push_back (sigt);
5992
5993 return 1;
5994 }
5995
5996 /* A helper for create_debug_types_hash_table. Read types from SECTION
5997 and fill them into TYPES_HTAB. It will process only type units,
5998 therefore DW_UT_type. */
5999
6000 static void
6001 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6002 struct dwo_file *dwo_file,
6003 dwarf2_section_info *section, htab_up &types_htab,
6004 rcuh_kind section_kind)
6005 {
6006 struct objfile *objfile = dwarf2_per_objfile->objfile;
6007 struct dwarf2_section_info *abbrev_section;
6008 bfd *abfd;
6009 const gdb_byte *info_ptr, *end_ptr;
6010
6011 abbrev_section = (dwo_file != NULL
6012 ? &dwo_file->sections.abbrev
6013 : &dwarf2_per_objfile->abbrev);
6014
6015 if (dwarf_read_debug)
6016 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6017 section->get_name (),
6018 abbrev_section->get_file_name ());
6019
6020 section->read (objfile);
6021 info_ptr = section->buffer;
6022
6023 if (info_ptr == NULL)
6024 return;
6025
6026 /* We can't set abfd until now because the section may be empty or
6027 not present, in which case the bfd is unknown. */
6028 abfd = section->get_bfd_owner ();
6029
6030 /* We don't use cutu_reader here because we don't need to read
6031 any dies: the signature is in the header. */
6032
6033 end_ptr = info_ptr + section->size;
6034 while (info_ptr < end_ptr)
6035 {
6036 struct signatured_type *sig_type;
6037 struct dwo_unit *dwo_tu;
6038 void **slot;
6039 const gdb_byte *ptr = info_ptr;
6040 struct comp_unit_head header;
6041 unsigned int length;
6042
6043 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6044
6045 /* Initialize it due to a false compiler warning. */
6046 header.signature = -1;
6047 header.type_cu_offset_in_tu = (cu_offset) -1;
6048
6049 /* We need to read the type's signature in order to build the hash
6050 table, but we don't need anything else just yet. */
6051
6052 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6053 abbrev_section, ptr, section_kind);
6054
6055 length = header.get_length ();
6056
6057 /* Skip dummy type units. */
6058 if (ptr >= info_ptr + length
6059 || peek_abbrev_code (abfd, ptr) == 0
6060 || header.unit_type != DW_UT_type)
6061 {
6062 info_ptr += length;
6063 continue;
6064 }
6065
6066 if (types_htab == NULL)
6067 {
6068 if (dwo_file)
6069 types_htab = allocate_dwo_unit_table (objfile);
6070 else
6071 types_htab = allocate_signatured_type_table (objfile);
6072 }
6073
6074 if (dwo_file)
6075 {
6076 sig_type = NULL;
6077 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6078 struct dwo_unit);
6079 dwo_tu->dwo_file = dwo_file;
6080 dwo_tu->signature = header.signature;
6081 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6082 dwo_tu->section = section;
6083 dwo_tu->sect_off = sect_off;
6084 dwo_tu->length = length;
6085 }
6086 else
6087 {
6088 /* N.B.: type_offset is not usable if this type uses a DWO file.
6089 The real type_offset is in the DWO file. */
6090 dwo_tu = NULL;
6091 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6092 struct signatured_type);
6093 sig_type->signature = header.signature;
6094 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6095 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6096 sig_type->per_cu.is_debug_types = 1;
6097 sig_type->per_cu.section = section;
6098 sig_type->per_cu.sect_off = sect_off;
6099 sig_type->per_cu.length = length;
6100 }
6101
6102 slot = htab_find_slot (types_htab.get (),
6103 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6104 INSERT);
6105 gdb_assert (slot != NULL);
6106 if (*slot != NULL)
6107 {
6108 sect_offset dup_sect_off;
6109
6110 if (dwo_file)
6111 {
6112 const struct dwo_unit *dup_tu
6113 = (const struct dwo_unit *) *slot;
6114
6115 dup_sect_off = dup_tu->sect_off;
6116 }
6117 else
6118 {
6119 const struct signatured_type *dup_tu
6120 = (const struct signatured_type *) *slot;
6121
6122 dup_sect_off = dup_tu->per_cu.sect_off;
6123 }
6124
6125 complaint (_("debug type entry at offset %s is duplicate to"
6126 " the entry at offset %s, signature %s"),
6127 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6128 hex_string (header.signature));
6129 }
6130 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6131
6132 if (dwarf_read_debug > 1)
6133 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6134 sect_offset_str (sect_off),
6135 hex_string (header.signature));
6136
6137 info_ptr += length;
6138 }
6139 }
6140
6141 /* Create the hash table of all entries in the .debug_types
6142 (or .debug_types.dwo) section(s).
6143 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6144 otherwise it is NULL.
6145
6146 The result is a pointer to the hash table or NULL if there are no types.
6147
6148 Note: This function processes DWO files only, not DWP files. */
6149
6150 static void
6151 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6152 struct dwo_file *dwo_file,
6153 gdb::array_view<dwarf2_section_info> type_sections,
6154 htab_up &types_htab)
6155 {
6156 for (dwarf2_section_info &section : type_sections)
6157 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6158 types_htab, rcuh_kind::TYPE);
6159 }
6160
6161 /* Create the hash table of all entries in the .debug_types section,
6162 and initialize all_type_units.
6163 The result is zero if there is an error (e.g. missing .debug_types section),
6164 otherwise non-zero. */
6165
6166 static int
6167 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6168 {
6169 htab_up types_htab;
6170
6171 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6172 &dwarf2_per_objfile->info, types_htab,
6173 rcuh_kind::COMPILE);
6174 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6175 dwarf2_per_objfile->types, types_htab);
6176 if (types_htab == NULL)
6177 {
6178 dwarf2_per_objfile->signatured_types = NULL;
6179 return 0;
6180 }
6181
6182 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6183
6184 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6185 dwarf2_per_objfile->all_type_units.reserve
6186 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6187
6188 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6189 add_signatured_type_cu_to_table,
6190 &dwarf2_per_objfile->all_type_units);
6191
6192 return 1;
6193 }
6194
6195 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6196 If SLOT is non-NULL, it is the entry to use in the hash table.
6197 Otherwise we find one. */
6198
6199 static struct signatured_type *
6200 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6201 void **slot)
6202 {
6203 struct objfile *objfile = dwarf2_per_objfile->objfile;
6204
6205 if (dwarf2_per_objfile->all_type_units.size ()
6206 == dwarf2_per_objfile->all_type_units.capacity ())
6207 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6208
6209 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6210 struct signatured_type);
6211
6212 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6213 sig_type->signature = sig;
6214 sig_type->per_cu.is_debug_types = 1;
6215 if (dwarf2_per_objfile->using_index)
6216 {
6217 sig_type->per_cu.v.quick =
6218 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6219 struct dwarf2_per_cu_quick_data);
6220 }
6221
6222 if (slot == NULL)
6223 {
6224 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6225 sig_type, INSERT);
6226 }
6227 gdb_assert (*slot == NULL);
6228 *slot = sig_type;
6229 /* The rest of sig_type must be filled in by the caller. */
6230 return sig_type;
6231 }
6232
6233 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6234 Fill in SIG_ENTRY with DWO_ENTRY. */
6235
6236 static void
6237 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6238 struct signatured_type *sig_entry,
6239 struct dwo_unit *dwo_entry)
6240 {
6241 /* Make sure we're not clobbering something we don't expect to. */
6242 gdb_assert (! sig_entry->per_cu.queued);
6243 gdb_assert (sig_entry->per_cu.cu == NULL);
6244 if (dwarf2_per_objfile->using_index)
6245 {
6246 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6247 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6248 }
6249 else
6250 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6251 gdb_assert (sig_entry->signature == dwo_entry->signature);
6252 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6253 gdb_assert (sig_entry->type_unit_group == NULL);
6254 gdb_assert (sig_entry->dwo_unit == NULL);
6255
6256 sig_entry->per_cu.section = dwo_entry->section;
6257 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6258 sig_entry->per_cu.length = dwo_entry->length;
6259 sig_entry->per_cu.reading_dwo_directly = 1;
6260 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6261 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6262 sig_entry->dwo_unit = dwo_entry;
6263 }
6264
6265 /* Subroutine of lookup_signatured_type.
6266 If we haven't read the TU yet, create the signatured_type data structure
6267 for a TU to be read in directly from a DWO file, bypassing the stub.
6268 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6269 using .gdb_index, then when reading a CU we want to stay in the DWO file
6270 containing that CU. Otherwise we could end up reading several other DWO
6271 files (due to comdat folding) to process the transitive closure of all the
6272 mentioned TUs, and that can be slow. The current DWO file will have every
6273 type signature that it needs.
6274 We only do this for .gdb_index because in the psymtab case we already have
6275 to read all the DWOs to build the type unit groups. */
6276
6277 static struct signatured_type *
6278 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6279 {
6280 struct dwarf2_per_objfile *dwarf2_per_objfile
6281 = cu->per_cu->dwarf2_per_objfile;
6282 struct objfile *objfile = dwarf2_per_objfile->objfile;
6283 struct dwo_file *dwo_file;
6284 struct dwo_unit find_dwo_entry, *dwo_entry;
6285 struct signatured_type find_sig_entry, *sig_entry;
6286 void **slot;
6287
6288 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6289
6290 /* If TU skeletons have been removed then we may not have read in any
6291 TUs yet. */
6292 if (dwarf2_per_objfile->signatured_types == NULL)
6293 {
6294 dwarf2_per_objfile->signatured_types
6295 = allocate_signatured_type_table (objfile);
6296 }
6297
6298 /* We only ever need to read in one copy of a signatured type.
6299 Use the global signatured_types array to do our own comdat-folding
6300 of types. If this is the first time we're reading this TU, and
6301 the TU has an entry in .gdb_index, replace the recorded data from
6302 .gdb_index with this TU. */
6303
6304 find_sig_entry.signature = sig;
6305 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6306 &find_sig_entry, INSERT);
6307 sig_entry = (struct signatured_type *) *slot;
6308
6309 /* We can get here with the TU already read, *or* in the process of being
6310 read. Don't reassign the global entry to point to this DWO if that's
6311 the case. Also note that if the TU is already being read, it may not
6312 have come from a DWO, the program may be a mix of Fission-compiled
6313 code and non-Fission-compiled code. */
6314
6315 /* Have we already tried to read this TU?
6316 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6317 needn't exist in the global table yet). */
6318 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6319 return sig_entry;
6320
6321 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6322 dwo_unit of the TU itself. */
6323 dwo_file = cu->dwo_unit->dwo_file;
6324
6325 /* Ok, this is the first time we're reading this TU. */
6326 if (dwo_file->tus == NULL)
6327 return NULL;
6328 find_dwo_entry.signature = sig;
6329 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6330 &find_dwo_entry);
6331 if (dwo_entry == NULL)
6332 return NULL;
6333
6334 /* If the global table doesn't have an entry for this TU, add one. */
6335 if (sig_entry == NULL)
6336 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6337
6338 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6339 sig_entry->per_cu.tu_read = 1;
6340 return sig_entry;
6341 }
6342
6343 /* Subroutine of lookup_signatured_type.
6344 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6345 then try the DWP file. If the TU stub (skeleton) has been removed then
6346 it won't be in .gdb_index. */
6347
6348 static struct signatured_type *
6349 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6350 {
6351 struct dwarf2_per_objfile *dwarf2_per_objfile
6352 = cu->per_cu->dwarf2_per_objfile;
6353 struct objfile *objfile = dwarf2_per_objfile->objfile;
6354 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6355 struct dwo_unit *dwo_entry;
6356 struct signatured_type find_sig_entry, *sig_entry;
6357 void **slot;
6358
6359 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6360 gdb_assert (dwp_file != NULL);
6361
6362 /* If TU skeletons have been removed then we may not have read in any
6363 TUs yet. */
6364 if (dwarf2_per_objfile->signatured_types == NULL)
6365 {
6366 dwarf2_per_objfile->signatured_types
6367 = allocate_signatured_type_table (objfile);
6368 }
6369
6370 find_sig_entry.signature = sig;
6371 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6372 &find_sig_entry, INSERT);
6373 sig_entry = (struct signatured_type *) *slot;
6374
6375 /* Have we already tried to read this TU?
6376 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6377 needn't exist in the global table yet). */
6378 if (sig_entry != NULL)
6379 return sig_entry;
6380
6381 if (dwp_file->tus == NULL)
6382 return NULL;
6383 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6384 sig, 1 /* is_debug_types */);
6385 if (dwo_entry == NULL)
6386 return NULL;
6387
6388 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6389 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6390
6391 return sig_entry;
6392 }
6393
6394 /* Lookup a signature based type for DW_FORM_ref_sig8.
6395 Returns NULL if signature SIG is not present in the table.
6396 It is up to the caller to complain about this. */
6397
6398 static struct signatured_type *
6399 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6400 {
6401 struct dwarf2_per_objfile *dwarf2_per_objfile
6402 = cu->per_cu->dwarf2_per_objfile;
6403
6404 if (cu->dwo_unit
6405 && dwarf2_per_objfile->using_index)
6406 {
6407 /* We're in a DWO/DWP file, and we're using .gdb_index.
6408 These cases require special processing. */
6409 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6410 return lookup_dwo_signatured_type (cu, sig);
6411 else
6412 return lookup_dwp_signatured_type (cu, sig);
6413 }
6414 else
6415 {
6416 struct signatured_type find_entry, *entry;
6417
6418 if (dwarf2_per_objfile->signatured_types == NULL)
6419 return NULL;
6420 find_entry.signature = sig;
6421 entry = ((struct signatured_type *)
6422 htab_find (dwarf2_per_objfile->signatured_types.get (),
6423 &find_entry));
6424 return entry;
6425 }
6426 }
6427
6428 /* Return the address base of the compile unit, which, if exists, is stored
6429 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6430 static gdb::optional<ULONGEST>
6431 lookup_addr_base (struct die_info *comp_unit_die)
6432 {
6433 struct attribute *attr;
6434 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6435 if (attr == nullptr)
6436 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6437 if (attr == nullptr)
6438 return gdb::optional<ULONGEST> ();
6439 return DW_UNSND (attr);
6440 }
6441
6442 /* Return range lists base of the compile unit, which, if exists, is stored
6443 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6444 static ULONGEST
6445 lookup_ranges_base (struct die_info *comp_unit_die)
6446 {
6447 struct attribute *attr;
6448 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6449 if (attr == nullptr)
6450 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6451 if (attr == nullptr)
6452 return 0;
6453 return DW_UNSND (attr);
6454 }
6455
6456 /* Low level DIE reading support. */
6457
6458 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6459
6460 static void
6461 init_cu_die_reader (struct die_reader_specs *reader,
6462 struct dwarf2_cu *cu,
6463 struct dwarf2_section_info *section,
6464 struct dwo_file *dwo_file,
6465 struct abbrev_table *abbrev_table)
6466 {
6467 gdb_assert (section->readin && section->buffer != NULL);
6468 reader->abfd = section->get_bfd_owner ();
6469 reader->cu = cu;
6470 reader->dwo_file = dwo_file;
6471 reader->die_section = section;
6472 reader->buffer = section->buffer;
6473 reader->buffer_end = section->buffer + section->size;
6474 reader->abbrev_table = abbrev_table;
6475 }
6476
6477 /* Subroutine of cutu_reader to simplify it.
6478 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6479 There's just a lot of work to do, and cutu_reader is big enough
6480 already.
6481
6482 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6483 from it to the DIE in the DWO. If NULL we are skipping the stub.
6484 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6485 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6486 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6487 STUB_COMP_DIR may be non-NULL.
6488 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6489 are filled in with the info of the DIE from the DWO file.
6490 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6491 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6492 kept around for at least as long as *RESULT_READER.
6493
6494 The result is non-zero if a valid (non-dummy) DIE was found. */
6495
6496 static int
6497 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6498 struct dwo_unit *dwo_unit,
6499 struct die_info *stub_comp_unit_die,
6500 const char *stub_comp_dir,
6501 struct die_reader_specs *result_reader,
6502 const gdb_byte **result_info_ptr,
6503 struct die_info **result_comp_unit_die,
6504 abbrev_table_up *result_dwo_abbrev_table)
6505 {
6506 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6507 struct objfile *objfile = dwarf2_per_objfile->objfile;
6508 struct dwarf2_cu *cu = this_cu->cu;
6509 bfd *abfd;
6510 const gdb_byte *begin_info_ptr, *info_ptr;
6511 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6512 int i,num_extra_attrs;
6513 struct dwarf2_section_info *dwo_abbrev_section;
6514 struct die_info *comp_unit_die;
6515
6516 /* At most one of these may be provided. */
6517 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6518
6519 /* These attributes aren't processed until later:
6520 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6521 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6522 referenced later. However, these attributes are found in the stub
6523 which we won't have later. In order to not impose this complication
6524 on the rest of the code, we read them here and copy them to the
6525 DWO CU/TU die. */
6526
6527 stmt_list = NULL;
6528 low_pc = NULL;
6529 high_pc = NULL;
6530 ranges = NULL;
6531 comp_dir = NULL;
6532
6533 if (stub_comp_unit_die != NULL)
6534 {
6535 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6536 DWO file. */
6537 if (! this_cu->is_debug_types)
6538 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6539 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6540 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6541 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6542 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6543
6544 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6545
6546 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6547 here (if needed). We need the value before we can process
6548 DW_AT_ranges. */
6549 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6550 }
6551 else if (stub_comp_dir != NULL)
6552 {
6553 /* Reconstruct the comp_dir attribute to simplify the code below. */
6554 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6555 comp_dir->name = DW_AT_comp_dir;
6556 comp_dir->form = DW_FORM_string;
6557 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6558 DW_STRING (comp_dir) = stub_comp_dir;
6559 }
6560
6561 /* Set up for reading the DWO CU/TU. */
6562 cu->dwo_unit = dwo_unit;
6563 dwarf2_section_info *section = dwo_unit->section;
6564 section->read (objfile);
6565 abfd = section->get_bfd_owner ();
6566 begin_info_ptr = info_ptr = (section->buffer
6567 + to_underlying (dwo_unit->sect_off));
6568 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6569
6570 if (this_cu->is_debug_types)
6571 {
6572 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6573
6574 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6575 &cu->header, section,
6576 dwo_abbrev_section,
6577 info_ptr, rcuh_kind::TYPE);
6578 /* This is not an assert because it can be caused by bad debug info. */
6579 if (sig_type->signature != cu->header.signature)
6580 {
6581 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6582 " TU at offset %s [in module %s]"),
6583 hex_string (sig_type->signature),
6584 hex_string (cu->header.signature),
6585 sect_offset_str (dwo_unit->sect_off),
6586 bfd_get_filename (abfd));
6587 }
6588 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6589 /* For DWOs coming from DWP files, we don't know the CU length
6590 nor the type's offset in the TU until now. */
6591 dwo_unit->length = cu->header.get_length ();
6592 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6593
6594 /* Establish the type offset that can be used to lookup the type.
6595 For DWO files, we don't know it until now. */
6596 sig_type->type_offset_in_section
6597 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6598 }
6599 else
6600 {
6601 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6602 &cu->header, section,
6603 dwo_abbrev_section,
6604 info_ptr, rcuh_kind::COMPILE);
6605 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6606 /* For DWOs coming from DWP files, we don't know the CU length
6607 until now. */
6608 dwo_unit->length = cu->header.get_length ();
6609 }
6610
6611 *result_dwo_abbrev_table
6612 = abbrev_table::read (objfile, dwo_abbrev_section,
6613 cu->header.abbrev_sect_off);
6614 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6615 result_dwo_abbrev_table->get ());
6616
6617 /* Read in the die, but leave space to copy over the attributes
6618 from the stub. This has the benefit of simplifying the rest of
6619 the code - all the work to maintain the illusion of a single
6620 DW_TAG_{compile,type}_unit DIE is done here. */
6621 num_extra_attrs = ((stmt_list != NULL)
6622 + (low_pc != NULL)
6623 + (high_pc != NULL)
6624 + (ranges != NULL)
6625 + (comp_dir != NULL));
6626 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6627 num_extra_attrs);
6628
6629 /* Copy over the attributes from the stub to the DIE we just read in. */
6630 comp_unit_die = *result_comp_unit_die;
6631 i = comp_unit_die->num_attrs;
6632 if (stmt_list != NULL)
6633 comp_unit_die->attrs[i++] = *stmt_list;
6634 if (low_pc != NULL)
6635 comp_unit_die->attrs[i++] = *low_pc;
6636 if (high_pc != NULL)
6637 comp_unit_die->attrs[i++] = *high_pc;
6638 if (ranges != NULL)
6639 comp_unit_die->attrs[i++] = *ranges;
6640 if (comp_dir != NULL)
6641 comp_unit_die->attrs[i++] = *comp_dir;
6642 comp_unit_die->num_attrs += num_extra_attrs;
6643
6644 if (dwarf_die_debug)
6645 {
6646 fprintf_unfiltered (gdb_stdlog,
6647 "Read die from %s@0x%x of %s:\n",
6648 section->get_name (),
6649 (unsigned) (begin_info_ptr - section->buffer),
6650 bfd_get_filename (abfd));
6651 dump_die (comp_unit_die, dwarf_die_debug);
6652 }
6653
6654 /* Skip dummy compilation units. */
6655 if (info_ptr >= begin_info_ptr + dwo_unit->length
6656 || peek_abbrev_code (abfd, info_ptr) == 0)
6657 return 0;
6658
6659 *result_info_ptr = info_ptr;
6660 return 1;
6661 }
6662
6663 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6664 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6665 signature is part of the header. */
6666 static gdb::optional<ULONGEST>
6667 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6668 {
6669 if (cu->header.version >= 5)
6670 return cu->header.signature;
6671 struct attribute *attr;
6672 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6673 if (attr == nullptr)
6674 return gdb::optional<ULONGEST> ();
6675 return DW_UNSND (attr);
6676 }
6677
6678 /* Subroutine of cutu_reader to simplify it.
6679 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6680 Returns NULL if the specified DWO unit cannot be found. */
6681
6682 static struct dwo_unit *
6683 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6684 struct die_info *comp_unit_die,
6685 const char *dwo_name)
6686 {
6687 struct dwarf2_cu *cu = this_cu->cu;
6688 struct dwo_unit *dwo_unit;
6689 const char *comp_dir;
6690
6691 gdb_assert (cu != NULL);
6692
6693 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6694 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6695 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6696
6697 if (this_cu->is_debug_types)
6698 {
6699 struct signatured_type *sig_type;
6700
6701 /* Since this_cu is the first member of struct signatured_type,
6702 we can go from a pointer to one to a pointer to the other. */
6703 sig_type = (struct signatured_type *) this_cu;
6704 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6705 }
6706 else
6707 {
6708 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6709 if (!signature.has_value ())
6710 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6711 " [in module %s]"),
6712 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6713 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6714 *signature);
6715 }
6716
6717 return dwo_unit;
6718 }
6719
6720 /* Subroutine of cutu_reader to simplify it.
6721 See it for a description of the parameters.
6722 Read a TU directly from a DWO file, bypassing the stub. */
6723
6724 void
6725 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6726 int use_existing_cu, int keep)
6727 {
6728 struct signatured_type *sig_type;
6729 struct die_reader_specs reader;
6730
6731 /* Verify we can do the following downcast, and that we have the
6732 data we need. */
6733 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6734 sig_type = (struct signatured_type *) this_cu;
6735 gdb_assert (sig_type->dwo_unit != NULL);
6736
6737 if (use_existing_cu && this_cu->cu != NULL)
6738 {
6739 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6740 /* There's no need to do the rereading_dwo_cu handling that
6741 cutu_reader does since we don't read the stub. */
6742 }
6743 else
6744 {
6745 /* If !use_existing_cu, this_cu->cu must be NULL. */
6746 gdb_assert (this_cu->cu == NULL);
6747 m_new_cu.reset (new dwarf2_cu (this_cu));
6748 }
6749
6750 /* A future optimization, if needed, would be to use an existing
6751 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6752 could share abbrev tables. */
6753
6754 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6755 NULL /* stub_comp_unit_die */,
6756 sig_type->dwo_unit->dwo_file->comp_dir,
6757 &reader, &info_ptr,
6758 &comp_unit_die,
6759 &m_dwo_abbrev_table) == 0)
6760 {
6761 /* Dummy die. */
6762 dummy_p = true;
6763 }
6764 }
6765
6766 /* Initialize a CU (or TU) and read its DIEs.
6767 If the CU defers to a DWO file, read the DWO file as well.
6768
6769 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6770 Otherwise the table specified in the comp unit header is read in and used.
6771 This is an optimization for when we already have the abbrev table.
6772
6773 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6774 Otherwise, a new CU is allocated with xmalloc.
6775
6776 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6777 read_in_chain. Otherwise the dwarf2_cu data is freed at the
6778 end. */
6779
6780 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6781 struct abbrev_table *abbrev_table,
6782 int use_existing_cu, int keep,
6783 bool skip_partial)
6784 : die_reader_specs {},
6785 m_this_cu (this_cu),
6786 m_keep (keep)
6787 {
6788 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6789 struct objfile *objfile = dwarf2_per_objfile->objfile;
6790 struct dwarf2_section_info *section = this_cu->section;
6791 bfd *abfd = section->get_bfd_owner ();
6792 struct dwarf2_cu *cu;
6793 const gdb_byte *begin_info_ptr;
6794 struct signatured_type *sig_type = NULL;
6795 struct dwarf2_section_info *abbrev_section;
6796 /* Non-zero if CU currently points to a DWO file and we need to
6797 reread it. When this happens we need to reread the skeleton die
6798 before we can reread the DWO file (this only applies to CUs, not TUs). */
6799 int rereading_dwo_cu = 0;
6800
6801 if (dwarf_die_debug)
6802 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6803 this_cu->is_debug_types ? "type" : "comp",
6804 sect_offset_str (this_cu->sect_off));
6805
6806 if (use_existing_cu)
6807 gdb_assert (keep);
6808
6809 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6810 file (instead of going through the stub), short-circuit all of this. */
6811 if (this_cu->reading_dwo_directly)
6812 {
6813 /* Narrow down the scope of possibilities to have to understand. */
6814 gdb_assert (this_cu->is_debug_types);
6815 gdb_assert (abbrev_table == NULL);
6816 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
6817 return;
6818 }
6819
6820 /* This is cheap if the section is already read in. */
6821 section->read (objfile);
6822
6823 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6824
6825 abbrev_section = get_abbrev_section_for_cu (this_cu);
6826
6827 if (use_existing_cu && this_cu->cu != NULL)
6828 {
6829 cu = this_cu->cu;
6830 /* If this CU is from a DWO file we need to start over, we need to
6831 refetch the attributes from the skeleton CU.
6832 This could be optimized by retrieving those attributes from when we
6833 were here the first time: the previous comp_unit_die was stored in
6834 comp_unit_obstack. But there's no data yet that we need this
6835 optimization. */
6836 if (cu->dwo_unit != NULL)
6837 rereading_dwo_cu = 1;
6838 }
6839 else
6840 {
6841 /* If !use_existing_cu, this_cu->cu must be NULL. */
6842 gdb_assert (this_cu->cu == NULL);
6843 m_new_cu.reset (new dwarf2_cu (this_cu));
6844 cu = m_new_cu.get ();
6845 }
6846
6847 /* Get the header. */
6848 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6849 {
6850 /* We already have the header, there's no need to read it in again. */
6851 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6852 }
6853 else
6854 {
6855 if (this_cu->is_debug_types)
6856 {
6857 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6858 &cu->header, section,
6859 abbrev_section, info_ptr,
6860 rcuh_kind::TYPE);
6861
6862 /* Since per_cu is the first member of struct signatured_type,
6863 we can go from a pointer to one to a pointer to the other. */
6864 sig_type = (struct signatured_type *) this_cu;
6865 gdb_assert (sig_type->signature == cu->header.signature);
6866 gdb_assert (sig_type->type_offset_in_tu
6867 == cu->header.type_cu_offset_in_tu);
6868 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6869
6870 /* LENGTH has not been set yet for type units if we're
6871 using .gdb_index. */
6872 this_cu->length = cu->header.get_length ();
6873
6874 /* Establish the type offset that can be used to lookup the type. */
6875 sig_type->type_offset_in_section =
6876 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6877
6878 this_cu->dwarf_version = cu->header.version;
6879 }
6880 else
6881 {
6882 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6883 &cu->header, section,
6884 abbrev_section,
6885 info_ptr,
6886 rcuh_kind::COMPILE);
6887
6888 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6889 gdb_assert (this_cu->length == cu->header.get_length ());
6890 this_cu->dwarf_version = cu->header.version;
6891 }
6892 }
6893
6894 /* Skip dummy compilation units. */
6895 if (info_ptr >= begin_info_ptr + this_cu->length
6896 || peek_abbrev_code (abfd, info_ptr) == 0)
6897 {
6898 dummy_p = true;
6899 return;
6900 }
6901
6902 /* If we don't have them yet, read the abbrevs for this compilation unit.
6903 And if we need to read them now, make sure they're freed when we're
6904 done. */
6905 if (abbrev_table != NULL)
6906 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6907 else
6908 {
6909 m_abbrev_table_holder
6910 = abbrev_table::read (objfile, abbrev_section,
6911 cu->header.abbrev_sect_off);
6912 abbrev_table = m_abbrev_table_holder.get ();
6913 }
6914
6915 /* Read the top level CU/TU die. */
6916 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6917 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6918
6919 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6920 {
6921 dummy_p = true;
6922 return;
6923 }
6924
6925 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6926 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6927 table from the DWO file and pass the ownership over to us. It will be
6928 referenced from READER, so we must make sure to free it after we're done
6929 with READER.
6930
6931 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6932 DWO CU, that this test will fail (the attribute will not be present). */
6933 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6934 if (dwo_name != nullptr)
6935 {
6936 struct dwo_unit *dwo_unit;
6937 struct die_info *dwo_comp_unit_die;
6938
6939 if (comp_unit_die->has_children)
6940 {
6941 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6942 " has children (offset %s) [in module %s]"),
6943 sect_offset_str (this_cu->sect_off),
6944 bfd_get_filename (abfd));
6945 }
6946 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6947 if (dwo_unit != NULL)
6948 {
6949 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6950 comp_unit_die, NULL,
6951 this, &info_ptr,
6952 &dwo_comp_unit_die,
6953 &m_dwo_abbrev_table) == 0)
6954 {
6955 /* Dummy die. */
6956 dummy_p = true;
6957 return;
6958 }
6959 comp_unit_die = dwo_comp_unit_die;
6960 }
6961 else
6962 {
6963 /* Yikes, we couldn't find the rest of the DIE, we only have
6964 the stub. A complaint has already been logged. There's
6965 not much more we can do except pass on the stub DIE to
6966 die_reader_func. We don't want to throw an error on bad
6967 debug info. */
6968 }
6969 }
6970 }
6971
6972 cutu_reader::~cutu_reader ()
6973 {
6974 /* Done, clean up. */
6975 if (m_new_cu != NULL && m_keep && !dummy_p)
6976 {
6977 struct dwarf2_per_objfile *dwarf2_per_objfile
6978 = m_this_cu->dwarf2_per_objfile;
6979 /* Link this CU into read_in_chain. */
6980 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6981 dwarf2_per_objfile->read_in_chain = m_this_cu;
6982 /* The chain owns it now. */
6983 m_new_cu.release ();
6984 }
6985 }
6986
6987 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6988 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6989 assumed to have already done the lookup to find the DWO file).
6990
6991 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6992 THIS_CU->is_debug_types, but nothing else.
6993
6994 We fill in THIS_CU->length.
6995
6996 THIS_CU->cu is always freed when done.
6997 This is done in order to not leave THIS_CU->cu in a state where we have
6998 to care whether it refers to the "main" CU or the DWO CU.
6999
7000 When parent_cu is passed, it is used to provide a default value for
7001 str_offsets_base and addr_base from the parent. */
7002
7003 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7004 struct dwarf2_cu *parent_cu,
7005 struct dwo_file *dwo_file)
7006 : die_reader_specs {},
7007 m_this_cu (this_cu)
7008 {
7009 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7010 struct objfile *objfile = dwarf2_per_objfile->objfile;
7011 struct dwarf2_section_info *section = this_cu->section;
7012 bfd *abfd = section->get_bfd_owner ();
7013 struct dwarf2_section_info *abbrev_section;
7014 const gdb_byte *begin_info_ptr, *info_ptr;
7015
7016 if (dwarf_die_debug)
7017 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7018 this_cu->is_debug_types ? "type" : "comp",
7019 sect_offset_str (this_cu->sect_off));
7020
7021 gdb_assert (this_cu->cu == NULL);
7022
7023 abbrev_section = (dwo_file != NULL
7024 ? &dwo_file->sections.abbrev
7025 : get_abbrev_section_for_cu (this_cu));
7026
7027 /* This is cheap if the section is already read in. */
7028 section->read (objfile);
7029
7030 m_new_cu.reset (new dwarf2_cu (this_cu));
7031
7032 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7033 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7034 &m_new_cu->header, section,
7035 abbrev_section, info_ptr,
7036 (this_cu->is_debug_types
7037 ? rcuh_kind::TYPE
7038 : rcuh_kind::COMPILE));
7039
7040 if (parent_cu != nullptr)
7041 {
7042 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7043 m_new_cu->addr_base = parent_cu->addr_base;
7044 }
7045 this_cu->length = m_new_cu->header.get_length ();
7046
7047 /* Skip dummy compilation units. */
7048 if (info_ptr >= begin_info_ptr + this_cu->length
7049 || peek_abbrev_code (abfd, info_ptr) == 0)
7050 {
7051 dummy_p = true;
7052 return;
7053 }
7054
7055 m_abbrev_table_holder
7056 = abbrev_table::read (objfile, abbrev_section,
7057 m_new_cu->header.abbrev_sect_off);
7058
7059 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7060 m_abbrev_table_holder.get ());
7061 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7062 }
7063
7064 \f
7065 /* Type Unit Groups.
7066
7067 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7068 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7069 so that all types coming from the same compilation (.o file) are grouped
7070 together. A future step could be to put the types in the same symtab as
7071 the CU the types ultimately came from. */
7072
7073 static hashval_t
7074 hash_type_unit_group (const void *item)
7075 {
7076 const struct type_unit_group *tu_group
7077 = (const struct type_unit_group *) item;
7078
7079 return hash_stmt_list_entry (&tu_group->hash);
7080 }
7081
7082 static int
7083 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7084 {
7085 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7086 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7087
7088 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7089 }
7090
7091 /* Allocate a hash table for type unit groups. */
7092
7093 static htab_up
7094 allocate_type_unit_groups_table (struct objfile *objfile)
7095 {
7096 return htab_up (htab_create_alloc (3,
7097 hash_type_unit_group,
7098 eq_type_unit_group,
7099 NULL, xcalloc, xfree));
7100 }
7101
7102 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7103 partial symtabs. We combine several TUs per psymtab to not let the size
7104 of any one psymtab grow too big. */
7105 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7106 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7107
7108 /* Helper routine for get_type_unit_group.
7109 Create the type_unit_group object used to hold one or more TUs. */
7110
7111 static struct type_unit_group *
7112 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7113 {
7114 struct dwarf2_per_objfile *dwarf2_per_objfile
7115 = cu->per_cu->dwarf2_per_objfile;
7116 struct objfile *objfile = dwarf2_per_objfile->objfile;
7117 struct dwarf2_per_cu_data *per_cu;
7118 struct type_unit_group *tu_group;
7119
7120 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7121 struct type_unit_group);
7122 per_cu = &tu_group->per_cu;
7123 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7124
7125 if (dwarf2_per_objfile->using_index)
7126 {
7127 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7128 struct dwarf2_per_cu_quick_data);
7129 }
7130 else
7131 {
7132 unsigned int line_offset = to_underlying (line_offset_struct);
7133 dwarf2_psymtab *pst;
7134 std::string name;
7135
7136 /* Give the symtab a useful name for debug purposes. */
7137 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7138 name = string_printf ("<type_units_%d>",
7139 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7140 else
7141 name = string_printf ("<type_units_at_0x%x>", line_offset);
7142
7143 pst = create_partial_symtab (per_cu, name.c_str ());
7144 pst->anonymous = true;
7145 }
7146
7147 tu_group->hash.dwo_unit = cu->dwo_unit;
7148 tu_group->hash.line_sect_off = line_offset_struct;
7149
7150 return tu_group;
7151 }
7152
7153 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7154 STMT_LIST is a DW_AT_stmt_list attribute. */
7155
7156 static struct type_unit_group *
7157 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7158 {
7159 struct dwarf2_per_objfile *dwarf2_per_objfile
7160 = cu->per_cu->dwarf2_per_objfile;
7161 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7162 struct type_unit_group *tu_group;
7163 void **slot;
7164 unsigned int line_offset;
7165 struct type_unit_group type_unit_group_for_lookup;
7166
7167 if (dwarf2_per_objfile->type_unit_groups == NULL)
7168 {
7169 dwarf2_per_objfile->type_unit_groups =
7170 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7171 }
7172
7173 /* Do we need to create a new group, or can we use an existing one? */
7174
7175 if (stmt_list)
7176 {
7177 line_offset = DW_UNSND (stmt_list);
7178 ++tu_stats->nr_symtab_sharers;
7179 }
7180 else
7181 {
7182 /* Ugh, no stmt_list. Rare, but we have to handle it.
7183 We can do various things here like create one group per TU or
7184 spread them over multiple groups to split up the expansion work.
7185 To avoid worst case scenarios (too many groups or too large groups)
7186 we, umm, group them in bunches. */
7187 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7188 | (tu_stats->nr_stmt_less_type_units
7189 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7190 ++tu_stats->nr_stmt_less_type_units;
7191 }
7192
7193 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7194 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7195 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7196 &type_unit_group_for_lookup, INSERT);
7197 if (*slot != NULL)
7198 {
7199 tu_group = (struct type_unit_group *) *slot;
7200 gdb_assert (tu_group != NULL);
7201 }
7202 else
7203 {
7204 sect_offset line_offset_struct = (sect_offset) line_offset;
7205 tu_group = create_type_unit_group (cu, line_offset_struct);
7206 *slot = tu_group;
7207 ++tu_stats->nr_symtabs;
7208 }
7209
7210 return tu_group;
7211 }
7212 \f
7213 /* Partial symbol tables. */
7214
7215 /* Create a psymtab named NAME and assign it to PER_CU.
7216
7217 The caller must fill in the following details:
7218 dirname, textlow, texthigh. */
7219
7220 static dwarf2_psymtab *
7221 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7222 {
7223 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7224 dwarf2_psymtab *pst;
7225
7226 pst = new dwarf2_psymtab (name, objfile, 0);
7227
7228 pst->psymtabs_addrmap_supported = true;
7229
7230 /* This is the glue that links PST into GDB's symbol API. */
7231 pst->per_cu_data = per_cu;
7232 per_cu->v.psymtab = pst;
7233
7234 return pst;
7235 }
7236
7237 /* DIE reader function for process_psymtab_comp_unit. */
7238
7239 static void
7240 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7241 const gdb_byte *info_ptr,
7242 struct die_info *comp_unit_die,
7243 enum language pretend_language)
7244 {
7245 struct dwarf2_cu *cu = reader->cu;
7246 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7247 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7248 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7249 CORE_ADDR baseaddr;
7250 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7251 dwarf2_psymtab *pst;
7252 enum pc_bounds_kind cu_bounds_kind;
7253 const char *filename;
7254
7255 gdb_assert (! per_cu->is_debug_types);
7256
7257 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7258
7259 /* Allocate a new partial symbol table structure. */
7260 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7261 if (filename == NULL)
7262 filename = "";
7263
7264 pst = create_partial_symtab (per_cu, filename);
7265
7266 /* This must be done before calling dwarf2_build_include_psymtabs. */
7267 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7268
7269 baseaddr = objfile->text_section_offset ();
7270
7271 dwarf2_find_base_address (comp_unit_die, cu);
7272
7273 /* Possibly set the default values of LOWPC and HIGHPC from
7274 `DW_AT_ranges'. */
7275 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7276 &best_highpc, cu, pst);
7277 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7278 {
7279 CORE_ADDR low
7280 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7281 - baseaddr);
7282 CORE_ADDR high
7283 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7284 - baseaddr - 1);
7285 /* Store the contiguous range if it is not empty; it can be
7286 empty for CUs with no code. */
7287 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7288 low, high, pst);
7289 }
7290
7291 /* Check if comp unit has_children.
7292 If so, read the rest of the partial symbols from this comp unit.
7293 If not, there's no more debug_info for this comp unit. */
7294 if (comp_unit_die->has_children)
7295 {
7296 struct partial_die_info *first_die;
7297 CORE_ADDR lowpc, highpc;
7298
7299 lowpc = ((CORE_ADDR) -1);
7300 highpc = ((CORE_ADDR) 0);
7301
7302 first_die = load_partial_dies (reader, info_ptr, 1);
7303
7304 scan_partial_symbols (first_die, &lowpc, &highpc,
7305 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7306
7307 /* If we didn't find a lowpc, set it to highpc to avoid
7308 complaints from `maint check'. */
7309 if (lowpc == ((CORE_ADDR) -1))
7310 lowpc = highpc;
7311
7312 /* If the compilation unit didn't have an explicit address range,
7313 then use the information extracted from its child dies. */
7314 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7315 {
7316 best_lowpc = lowpc;
7317 best_highpc = highpc;
7318 }
7319 }
7320 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7321 best_lowpc + baseaddr)
7322 - baseaddr);
7323 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7324 best_highpc + baseaddr)
7325 - baseaddr);
7326
7327 end_psymtab_common (objfile, pst);
7328
7329 if (!cu->per_cu->imported_symtabs_empty ())
7330 {
7331 int i;
7332 int len = cu->per_cu->imported_symtabs_size ();
7333
7334 /* Fill in 'dependencies' here; we fill in 'users' in a
7335 post-pass. */
7336 pst->number_of_dependencies = len;
7337 pst->dependencies
7338 = objfile->partial_symtabs->allocate_dependencies (len);
7339 for (i = 0; i < len; ++i)
7340 {
7341 pst->dependencies[i]
7342 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7343 }
7344
7345 cu->per_cu->imported_symtabs_free ();
7346 }
7347
7348 /* Get the list of files included in the current compilation unit,
7349 and build a psymtab for each of them. */
7350 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7351
7352 if (dwarf_read_debug)
7353 fprintf_unfiltered (gdb_stdlog,
7354 "Psymtab for %s unit @%s: %s - %s"
7355 ", %d global, %d static syms\n",
7356 per_cu->is_debug_types ? "type" : "comp",
7357 sect_offset_str (per_cu->sect_off),
7358 paddress (gdbarch, pst->text_low (objfile)),
7359 paddress (gdbarch, pst->text_high (objfile)),
7360 pst->n_global_syms, pst->n_static_syms);
7361 }
7362
7363 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7364 Process compilation unit THIS_CU for a psymtab. */
7365
7366 static void
7367 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7368 bool want_partial_unit,
7369 enum language pretend_language)
7370 {
7371 /* If this compilation unit was already read in, free the
7372 cached copy in order to read it in again. This is
7373 necessary because we skipped some symbols when we first
7374 read in the compilation unit (see load_partial_dies).
7375 This problem could be avoided, but the benefit is unclear. */
7376 if (this_cu->cu != NULL)
7377 free_one_cached_comp_unit (this_cu);
7378
7379 cutu_reader reader (this_cu, NULL, 0, 0, false);
7380
7381 if (reader.dummy_p)
7382 {
7383 /* Nothing. */
7384 }
7385 else if (this_cu->is_debug_types)
7386 build_type_psymtabs_reader (&reader, reader.info_ptr,
7387 reader.comp_unit_die);
7388 else if (want_partial_unit
7389 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7390 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7391 reader.comp_unit_die,
7392 pretend_language);
7393
7394 /* Age out any secondary CUs. */
7395 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7396 }
7397
7398 /* Reader function for build_type_psymtabs. */
7399
7400 static void
7401 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7402 const gdb_byte *info_ptr,
7403 struct die_info *type_unit_die)
7404 {
7405 struct dwarf2_per_objfile *dwarf2_per_objfile
7406 = reader->cu->per_cu->dwarf2_per_objfile;
7407 struct objfile *objfile = dwarf2_per_objfile->objfile;
7408 struct dwarf2_cu *cu = reader->cu;
7409 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7410 struct signatured_type *sig_type;
7411 struct type_unit_group *tu_group;
7412 struct attribute *attr;
7413 struct partial_die_info *first_die;
7414 CORE_ADDR lowpc, highpc;
7415 dwarf2_psymtab *pst;
7416
7417 gdb_assert (per_cu->is_debug_types);
7418 sig_type = (struct signatured_type *) per_cu;
7419
7420 if (! type_unit_die->has_children)
7421 return;
7422
7423 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7424 tu_group = get_type_unit_group (cu, attr);
7425
7426 if (tu_group->tus == nullptr)
7427 tu_group->tus = new std::vector<signatured_type *>;
7428 tu_group->tus->push_back (sig_type);
7429
7430 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7431 pst = create_partial_symtab (per_cu, "");
7432 pst->anonymous = true;
7433
7434 first_die = load_partial_dies (reader, info_ptr, 1);
7435
7436 lowpc = (CORE_ADDR) -1;
7437 highpc = (CORE_ADDR) 0;
7438 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7439
7440 end_psymtab_common (objfile, pst);
7441 }
7442
7443 /* Struct used to sort TUs by their abbreviation table offset. */
7444
7445 struct tu_abbrev_offset
7446 {
7447 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7448 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7449 {}
7450
7451 signatured_type *sig_type;
7452 sect_offset abbrev_offset;
7453 };
7454
7455 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7456
7457 static bool
7458 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7459 const struct tu_abbrev_offset &b)
7460 {
7461 return a.abbrev_offset < b.abbrev_offset;
7462 }
7463
7464 /* Efficiently read all the type units.
7465 This does the bulk of the work for build_type_psymtabs.
7466
7467 The efficiency is because we sort TUs by the abbrev table they use and
7468 only read each abbrev table once. In one program there are 200K TUs
7469 sharing 8K abbrev tables.
7470
7471 The main purpose of this function is to support building the
7472 dwarf2_per_objfile->type_unit_groups table.
7473 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7474 can collapse the search space by grouping them by stmt_list.
7475 The savings can be significant, in the same program from above the 200K TUs
7476 share 8K stmt_list tables.
7477
7478 FUNC is expected to call get_type_unit_group, which will create the
7479 struct type_unit_group if necessary and add it to
7480 dwarf2_per_objfile->type_unit_groups. */
7481
7482 static void
7483 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7484 {
7485 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7486 abbrev_table_up abbrev_table;
7487 sect_offset abbrev_offset;
7488
7489 /* It's up to the caller to not call us multiple times. */
7490 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7491
7492 if (dwarf2_per_objfile->all_type_units.empty ())
7493 return;
7494
7495 /* TUs typically share abbrev tables, and there can be way more TUs than
7496 abbrev tables. Sort by abbrev table to reduce the number of times we
7497 read each abbrev table in.
7498 Alternatives are to punt or to maintain a cache of abbrev tables.
7499 This is simpler and efficient enough for now.
7500
7501 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7502 symtab to use). Typically TUs with the same abbrev offset have the same
7503 stmt_list value too so in practice this should work well.
7504
7505 The basic algorithm here is:
7506
7507 sort TUs by abbrev table
7508 for each TU with same abbrev table:
7509 read abbrev table if first user
7510 read TU top level DIE
7511 [IWBN if DWO skeletons had DW_AT_stmt_list]
7512 call FUNC */
7513
7514 if (dwarf_read_debug)
7515 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7516
7517 /* Sort in a separate table to maintain the order of all_type_units
7518 for .gdb_index: TU indices directly index all_type_units. */
7519 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7520 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7521
7522 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7523 sorted_by_abbrev.emplace_back
7524 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7525 sig_type->per_cu.section,
7526 sig_type->per_cu.sect_off));
7527
7528 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7529 sort_tu_by_abbrev_offset);
7530
7531 abbrev_offset = (sect_offset) ~(unsigned) 0;
7532
7533 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7534 {
7535 /* Switch to the next abbrev table if necessary. */
7536 if (abbrev_table == NULL
7537 || tu.abbrev_offset != abbrev_offset)
7538 {
7539 abbrev_offset = tu.abbrev_offset;
7540 abbrev_table =
7541 abbrev_table::read (dwarf2_per_objfile->objfile,
7542 &dwarf2_per_objfile->abbrev,
7543 abbrev_offset);
7544 ++tu_stats->nr_uniq_abbrev_tables;
7545 }
7546
7547 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7548 0, 0, false);
7549 if (!reader.dummy_p)
7550 build_type_psymtabs_reader (&reader, reader.info_ptr,
7551 reader.comp_unit_die);
7552 }
7553 }
7554
7555 /* Print collected type unit statistics. */
7556
7557 static void
7558 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7559 {
7560 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7561
7562 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7563 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7564 dwarf2_per_objfile->all_type_units.size ());
7565 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7566 tu_stats->nr_uniq_abbrev_tables);
7567 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7568 tu_stats->nr_symtabs);
7569 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7570 tu_stats->nr_symtab_sharers);
7571 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7572 tu_stats->nr_stmt_less_type_units);
7573 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7574 tu_stats->nr_all_type_units_reallocs);
7575 }
7576
7577 /* Traversal function for build_type_psymtabs. */
7578
7579 static int
7580 build_type_psymtab_dependencies (void **slot, void *info)
7581 {
7582 struct dwarf2_per_objfile *dwarf2_per_objfile
7583 = (struct dwarf2_per_objfile *) info;
7584 struct objfile *objfile = dwarf2_per_objfile->objfile;
7585 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7586 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7587 dwarf2_psymtab *pst = per_cu->v.psymtab;
7588 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7589 int i;
7590
7591 gdb_assert (len > 0);
7592 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7593
7594 pst->number_of_dependencies = len;
7595 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7596 for (i = 0; i < len; ++i)
7597 {
7598 struct signatured_type *iter = tu_group->tus->at (i);
7599 gdb_assert (iter->per_cu.is_debug_types);
7600 pst->dependencies[i] = iter->per_cu.v.psymtab;
7601 iter->type_unit_group = tu_group;
7602 }
7603
7604 delete tu_group->tus;
7605 tu_group->tus = nullptr;
7606
7607 return 1;
7608 }
7609
7610 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7611 Build partial symbol tables for the .debug_types comp-units. */
7612
7613 static void
7614 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7615 {
7616 if (! create_all_type_units (dwarf2_per_objfile))
7617 return;
7618
7619 build_type_psymtabs_1 (dwarf2_per_objfile);
7620 }
7621
7622 /* Traversal function for process_skeletonless_type_unit.
7623 Read a TU in a DWO file and build partial symbols for it. */
7624
7625 static int
7626 process_skeletonless_type_unit (void **slot, void *info)
7627 {
7628 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7629 struct dwarf2_per_objfile *dwarf2_per_objfile
7630 = (struct dwarf2_per_objfile *) info;
7631 struct signatured_type find_entry, *entry;
7632
7633 /* If this TU doesn't exist in the global table, add it and read it in. */
7634
7635 if (dwarf2_per_objfile->signatured_types == NULL)
7636 {
7637 dwarf2_per_objfile->signatured_types
7638 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
7639 }
7640
7641 find_entry.signature = dwo_unit->signature;
7642 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7643 &find_entry, INSERT);
7644 /* If we've already seen this type there's nothing to do. What's happening
7645 is we're doing our own version of comdat-folding here. */
7646 if (*slot != NULL)
7647 return 1;
7648
7649 /* This does the job that create_all_type_units would have done for
7650 this TU. */
7651 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7652 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7653 *slot = entry;
7654
7655 /* This does the job that build_type_psymtabs_1 would have done. */
7656 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
7657 if (!reader.dummy_p)
7658 build_type_psymtabs_reader (&reader, reader.info_ptr,
7659 reader.comp_unit_die);
7660
7661 return 1;
7662 }
7663
7664 /* Traversal function for process_skeletonless_type_units. */
7665
7666 static int
7667 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7668 {
7669 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7670
7671 if (dwo_file->tus != NULL)
7672 htab_traverse_noresize (dwo_file->tus.get (),
7673 process_skeletonless_type_unit, info);
7674
7675 return 1;
7676 }
7677
7678 /* Scan all TUs of DWO files, verifying we've processed them.
7679 This is needed in case a TU was emitted without its skeleton.
7680 Note: This can't be done until we know what all the DWO files are. */
7681
7682 static void
7683 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7684 {
7685 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7686 if (get_dwp_file (dwarf2_per_objfile) == NULL
7687 && dwarf2_per_objfile->dwo_files != NULL)
7688 {
7689 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7690 process_dwo_file_for_skeletonless_type_units,
7691 dwarf2_per_objfile);
7692 }
7693 }
7694
7695 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7696
7697 static void
7698 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7699 {
7700 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7701 {
7702 dwarf2_psymtab *pst = per_cu->v.psymtab;
7703
7704 if (pst == NULL)
7705 continue;
7706
7707 for (int j = 0; j < pst->number_of_dependencies; ++j)
7708 {
7709 /* Set the 'user' field only if it is not already set. */
7710 if (pst->dependencies[j]->user == NULL)
7711 pst->dependencies[j]->user = pst;
7712 }
7713 }
7714 }
7715
7716 /* Build the partial symbol table by doing a quick pass through the
7717 .debug_info and .debug_abbrev sections. */
7718
7719 static void
7720 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7721 {
7722 struct objfile *objfile = dwarf2_per_objfile->objfile;
7723
7724 if (dwarf_read_debug)
7725 {
7726 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7727 objfile_name (objfile));
7728 }
7729
7730 dwarf2_per_objfile->reading_partial_symbols = 1;
7731
7732 dwarf2_per_objfile->info.read (objfile);
7733
7734 /* Any cached compilation units will be linked by the per-objfile
7735 read_in_chain. Make sure to free them when we're done. */
7736 free_cached_comp_units freer (dwarf2_per_objfile);
7737
7738 build_type_psymtabs (dwarf2_per_objfile);
7739
7740 create_all_comp_units (dwarf2_per_objfile);
7741
7742 /* Create a temporary address map on a temporary obstack. We later
7743 copy this to the final obstack. */
7744 auto_obstack temp_obstack;
7745
7746 scoped_restore save_psymtabs_addrmap
7747 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7748 addrmap_create_mutable (&temp_obstack));
7749
7750 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7751 process_psymtab_comp_unit (per_cu, false, language_minimal);
7752
7753 /* This has to wait until we read the CUs, we need the list of DWOs. */
7754 process_skeletonless_type_units (dwarf2_per_objfile);
7755
7756 /* Now that all TUs have been processed we can fill in the dependencies. */
7757 if (dwarf2_per_objfile->type_unit_groups != NULL)
7758 {
7759 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7760 build_type_psymtab_dependencies, dwarf2_per_objfile);
7761 }
7762
7763 if (dwarf_read_debug)
7764 print_tu_stats (dwarf2_per_objfile);
7765
7766 set_partial_user (dwarf2_per_objfile);
7767
7768 objfile->partial_symtabs->psymtabs_addrmap
7769 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7770 objfile->partial_symtabs->obstack ());
7771 /* At this point we want to keep the address map. */
7772 save_psymtabs_addrmap.release ();
7773
7774 if (dwarf_read_debug)
7775 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7776 objfile_name (objfile));
7777 }
7778
7779 /* Load the partial DIEs for a secondary CU into memory.
7780 This is also used when rereading a primary CU with load_all_dies. */
7781
7782 static void
7783 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7784 {
7785 cutu_reader reader (this_cu, NULL, 1, 1, false);
7786
7787 if (!reader.dummy_p)
7788 {
7789 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7790 language_minimal);
7791
7792 /* Check if comp unit has_children.
7793 If so, read the rest of the partial symbols from this comp unit.
7794 If not, there's no more debug_info for this comp unit. */
7795 if (reader.comp_unit_die->has_children)
7796 load_partial_dies (&reader, reader.info_ptr, 0);
7797 }
7798 }
7799
7800 static void
7801 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7802 struct dwarf2_section_info *section,
7803 struct dwarf2_section_info *abbrev_section,
7804 unsigned int is_dwz)
7805 {
7806 const gdb_byte *info_ptr;
7807 struct objfile *objfile = dwarf2_per_objfile->objfile;
7808
7809 if (dwarf_read_debug)
7810 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7811 section->get_name (),
7812 section->get_file_name ());
7813
7814 section->read (objfile);
7815
7816 info_ptr = section->buffer;
7817
7818 while (info_ptr < section->buffer + section->size)
7819 {
7820 struct dwarf2_per_cu_data *this_cu;
7821
7822 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7823
7824 comp_unit_head cu_header;
7825 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7826 abbrev_section, info_ptr,
7827 rcuh_kind::COMPILE);
7828
7829 /* Save the compilation unit for later lookup. */
7830 if (cu_header.unit_type != DW_UT_type)
7831 {
7832 this_cu = XOBNEW (&objfile->objfile_obstack,
7833 struct dwarf2_per_cu_data);
7834 memset (this_cu, 0, sizeof (*this_cu));
7835 }
7836 else
7837 {
7838 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7839 struct signatured_type);
7840 memset (sig_type, 0, sizeof (*sig_type));
7841 sig_type->signature = cu_header.signature;
7842 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7843 this_cu = &sig_type->per_cu;
7844 }
7845 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7846 this_cu->sect_off = sect_off;
7847 this_cu->length = cu_header.length + cu_header.initial_length_size;
7848 this_cu->is_dwz = is_dwz;
7849 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7850 this_cu->section = section;
7851
7852 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7853
7854 info_ptr = info_ptr + this_cu->length;
7855 }
7856 }
7857
7858 /* Create a list of all compilation units in OBJFILE.
7859 This is only done for -readnow and building partial symtabs. */
7860
7861 static void
7862 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7863 {
7864 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7865 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7866 &dwarf2_per_objfile->abbrev, 0);
7867
7868 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7869 if (dwz != NULL)
7870 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7871 1);
7872 }
7873
7874 /* Process all loaded DIEs for compilation unit CU, starting at
7875 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7876 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7877 DW_AT_ranges). See the comments of add_partial_subprogram on how
7878 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7879
7880 static void
7881 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7882 CORE_ADDR *highpc, int set_addrmap,
7883 struct dwarf2_cu *cu)
7884 {
7885 struct partial_die_info *pdi;
7886
7887 /* Now, march along the PDI's, descending into ones which have
7888 interesting children but skipping the children of the other ones,
7889 until we reach the end of the compilation unit. */
7890
7891 pdi = first_die;
7892
7893 while (pdi != NULL)
7894 {
7895 pdi->fixup (cu);
7896
7897 /* Anonymous namespaces or modules have no name but have interesting
7898 children, so we need to look at them. Ditto for anonymous
7899 enums. */
7900
7901 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7902 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7903 || pdi->tag == DW_TAG_imported_unit
7904 || pdi->tag == DW_TAG_inlined_subroutine)
7905 {
7906 switch (pdi->tag)
7907 {
7908 case DW_TAG_subprogram:
7909 case DW_TAG_inlined_subroutine:
7910 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7911 break;
7912 case DW_TAG_constant:
7913 case DW_TAG_variable:
7914 case DW_TAG_typedef:
7915 case DW_TAG_union_type:
7916 if (!pdi->is_declaration)
7917 {
7918 add_partial_symbol (pdi, cu);
7919 }
7920 break;
7921 case DW_TAG_class_type:
7922 case DW_TAG_interface_type:
7923 case DW_TAG_structure_type:
7924 if (!pdi->is_declaration)
7925 {
7926 add_partial_symbol (pdi, cu);
7927 }
7928 if ((cu->language == language_rust
7929 || cu->language == language_cplus) && pdi->has_children)
7930 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7931 set_addrmap, cu);
7932 break;
7933 case DW_TAG_enumeration_type:
7934 if (!pdi->is_declaration)
7935 add_partial_enumeration (pdi, cu);
7936 break;
7937 case DW_TAG_base_type:
7938 case DW_TAG_subrange_type:
7939 /* File scope base type definitions are added to the partial
7940 symbol table. */
7941 add_partial_symbol (pdi, cu);
7942 break;
7943 case DW_TAG_namespace:
7944 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7945 break;
7946 case DW_TAG_module:
7947 if (!pdi->is_declaration)
7948 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7949 break;
7950 case DW_TAG_imported_unit:
7951 {
7952 struct dwarf2_per_cu_data *per_cu;
7953
7954 /* For now we don't handle imported units in type units. */
7955 if (cu->per_cu->is_debug_types)
7956 {
7957 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7958 " supported in type units [in module %s]"),
7959 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7960 }
7961
7962 per_cu = dwarf2_find_containing_comp_unit
7963 (pdi->d.sect_off, pdi->is_dwz,
7964 cu->per_cu->dwarf2_per_objfile);
7965
7966 /* Go read the partial unit, if needed. */
7967 if (per_cu->v.psymtab == NULL)
7968 process_psymtab_comp_unit (per_cu, true, cu->language);
7969
7970 cu->per_cu->imported_symtabs_push (per_cu);
7971 }
7972 break;
7973 case DW_TAG_imported_declaration:
7974 add_partial_symbol (pdi, cu);
7975 break;
7976 default:
7977 break;
7978 }
7979 }
7980
7981 /* If the die has a sibling, skip to the sibling. */
7982
7983 pdi = pdi->die_sibling;
7984 }
7985 }
7986
7987 /* Functions used to compute the fully scoped name of a partial DIE.
7988
7989 Normally, this is simple. For C++, the parent DIE's fully scoped
7990 name is concatenated with "::" and the partial DIE's name.
7991 Enumerators are an exception; they use the scope of their parent
7992 enumeration type, i.e. the name of the enumeration type is not
7993 prepended to the enumerator.
7994
7995 There are two complexities. One is DW_AT_specification; in this
7996 case "parent" means the parent of the target of the specification,
7997 instead of the direct parent of the DIE. The other is compilers
7998 which do not emit DW_TAG_namespace; in this case we try to guess
7999 the fully qualified name of structure types from their members'
8000 linkage names. This must be done using the DIE's children rather
8001 than the children of any DW_AT_specification target. We only need
8002 to do this for structures at the top level, i.e. if the target of
8003 any DW_AT_specification (if any; otherwise the DIE itself) does not
8004 have a parent. */
8005
8006 /* Compute the scope prefix associated with PDI's parent, in
8007 compilation unit CU. The result will be allocated on CU's
8008 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8009 field. NULL is returned if no prefix is necessary. */
8010 static const char *
8011 partial_die_parent_scope (struct partial_die_info *pdi,
8012 struct dwarf2_cu *cu)
8013 {
8014 const char *grandparent_scope;
8015 struct partial_die_info *parent, *real_pdi;
8016
8017 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8018 then this means the parent of the specification DIE. */
8019
8020 real_pdi = pdi;
8021 while (real_pdi->has_specification)
8022 {
8023 auto res = find_partial_die (real_pdi->spec_offset,
8024 real_pdi->spec_is_dwz, cu);
8025 real_pdi = res.pdi;
8026 cu = res.cu;
8027 }
8028
8029 parent = real_pdi->die_parent;
8030 if (parent == NULL)
8031 return NULL;
8032
8033 if (parent->scope_set)
8034 return parent->scope;
8035
8036 parent->fixup (cu);
8037
8038 grandparent_scope = partial_die_parent_scope (parent, cu);
8039
8040 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8041 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8042 Work around this problem here. */
8043 if (cu->language == language_cplus
8044 && parent->tag == DW_TAG_namespace
8045 && strcmp (parent->name, "::") == 0
8046 && grandparent_scope == NULL)
8047 {
8048 parent->scope = NULL;
8049 parent->scope_set = 1;
8050 return NULL;
8051 }
8052
8053 /* Nested subroutines in Fortran get a prefix. */
8054 if (pdi->tag == DW_TAG_enumerator)
8055 /* Enumerators should not get the name of the enumeration as a prefix. */
8056 parent->scope = grandparent_scope;
8057 else if (parent->tag == DW_TAG_namespace
8058 || parent->tag == DW_TAG_module
8059 || parent->tag == DW_TAG_structure_type
8060 || parent->tag == DW_TAG_class_type
8061 || parent->tag == DW_TAG_interface_type
8062 || parent->tag == DW_TAG_union_type
8063 || parent->tag == DW_TAG_enumeration_type
8064 || (cu->language == language_fortran
8065 && parent->tag == DW_TAG_subprogram
8066 && pdi->tag == DW_TAG_subprogram))
8067 {
8068 if (grandparent_scope == NULL)
8069 parent->scope = parent->name;
8070 else
8071 parent->scope = typename_concat (&cu->comp_unit_obstack,
8072 grandparent_scope,
8073 parent->name, 0, cu);
8074 }
8075 else
8076 {
8077 /* FIXME drow/2004-04-01: What should we be doing with
8078 function-local names? For partial symbols, we should probably be
8079 ignoring them. */
8080 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8081 dwarf_tag_name (parent->tag),
8082 sect_offset_str (pdi->sect_off));
8083 parent->scope = grandparent_scope;
8084 }
8085
8086 parent->scope_set = 1;
8087 return parent->scope;
8088 }
8089
8090 /* Return the fully scoped name associated with PDI, from compilation unit
8091 CU. The result will be allocated with malloc. */
8092
8093 static gdb::unique_xmalloc_ptr<char>
8094 partial_die_full_name (struct partial_die_info *pdi,
8095 struct dwarf2_cu *cu)
8096 {
8097 const char *parent_scope;
8098
8099 /* If this is a template instantiation, we can not work out the
8100 template arguments from partial DIEs. So, unfortunately, we have
8101 to go through the full DIEs. At least any work we do building
8102 types here will be reused if full symbols are loaded later. */
8103 if (pdi->has_template_arguments)
8104 {
8105 pdi->fixup (cu);
8106
8107 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8108 {
8109 struct die_info *die;
8110 struct attribute attr;
8111 struct dwarf2_cu *ref_cu = cu;
8112
8113 /* DW_FORM_ref_addr is using section offset. */
8114 attr.name = (enum dwarf_attribute) 0;
8115 attr.form = DW_FORM_ref_addr;
8116 attr.u.unsnd = to_underlying (pdi->sect_off);
8117 die = follow_die_ref (NULL, &attr, &ref_cu);
8118
8119 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8120 }
8121 }
8122
8123 parent_scope = partial_die_parent_scope (pdi, cu);
8124 if (parent_scope == NULL)
8125 return NULL;
8126 else
8127 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8128 pdi->name, 0, cu));
8129 }
8130
8131 static void
8132 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8133 {
8134 struct dwarf2_per_objfile *dwarf2_per_objfile
8135 = cu->per_cu->dwarf2_per_objfile;
8136 struct objfile *objfile = dwarf2_per_objfile->objfile;
8137 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8138 CORE_ADDR addr = 0;
8139 const char *actual_name = NULL;
8140 CORE_ADDR baseaddr;
8141
8142 baseaddr = objfile->text_section_offset ();
8143
8144 gdb::unique_xmalloc_ptr<char> built_actual_name
8145 = partial_die_full_name (pdi, cu);
8146 if (built_actual_name != NULL)
8147 actual_name = built_actual_name.get ();
8148
8149 if (actual_name == NULL)
8150 actual_name = pdi->name;
8151
8152 switch (pdi->tag)
8153 {
8154 case DW_TAG_inlined_subroutine:
8155 case DW_TAG_subprogram:
8156 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8157 - baseaddr);
8158 if (pdi->is_external
8159 || cu->language == language_ada
8160 || (cu->language == language_fortran
8161 && pdi->die_parent != NULL
8162 && pdi->die_parent->tag == DW_TAG_subprogram))
8163 {
8164 /* Normally, only "external" DIEs are part of the global scope.
8165 But in Ada and Fortran, we want to be able to access nested
8166 procedures globally. So all Ada and Fortran subprograms are
8167 stored in the global scope. */
8168 add_psymbol_to_list (actual_name,
8169 built_actual_name != NULL,
8170 VAR_DOMAIN, LOC_BLOCK,
8171 SECT_OFF_TEXT (objfile),
8172 psymbol_placement::GLOBAL,
8173 addr,
8174 cu->language, objfile);
8175 }
8176 else
8177 {
8178 add_psymbol_to_list (actual_name,
8179 built_actual_name != NULL,
8180 VAR_DOMAIN, LOC_BLOCK,
8181 SECT_OFF_TEXT (objfile),
8182 psymbol_placement::STATIC,
8183 addr, cu->language, objfile);
8184 }
8185
8186 if (pdi->main_subprogram && actual_name != NULL)
8187 set_objfile_main_name (objfile, actual_name, cu->language);
8188 break;
8189 case DW_TAG_constant:
8190 add_psymbol_to_list (actual_name,
8191 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8192 -1, (pdi->is_external
8193 ? psymbol_placement::GLOBAL
8194 : psymbol_placement::STATIC),
8195 0, cu->language, objfile);
8196 break;
8197 case DW_TAG_variable:
8198 if (pdi->d.locdesc)
8199 addr = decode_locdesc (pdi->d.locdesc, cu);
8200
8201 if (pdi->d.locdesc
8202 && addr == 0
8203 && !dwarf2_per_objfile->has_section_at_zero)
8204 {
8205 /* A global or static variable may also have been stripped
8206 out by the linker if unused, in which case its address
8207 will be nullified; do not add such variables into partial
8208 symbol table then. */
8209 }
8210 else if (pdi->is_external)
8211 {
8212 /* Global Variable.
8213 Don't enter into the minimal symbol tables as there is
8214 a minimal symbol table entry from the ELF symbols already.
8215 Enter into partial symbol table if it has a location
8216 descriptor or a type.
8217 If the location descriptor is missing, new_symbol will create
8218 a LOC_UNRESOLVED symbol, the address of the variable will then
8219 be determined from the minimal symbol table whenever the variable
8220 is referenced.
8221 The address for the partial symbol table entry is not
8222 used by GDB, but it comes in handy for debugging partial symbol
8223 table building. */
8224
8225 if (pdi->d.locdesc || pdi->has_type)
8226 add_psymbol_to_list (actual_name,
8227 built_actual_name != NULL,
8228 VAR_DOMAIN, LOC_STATIC,
8229 SECT_OFF_TEXT (objfile),
8230 psymbol_placement::GLOBAL,
8231 addr, cu->language, objfile);
8232 }
8233 else
8234 {
8235 int has_loc = pdi->d.locdesc != NULL;
8236
8237 /* Static Variable. Skip symbols whose value we cannot know (those
8238 without location descriptors or constant values). */
8239 if (!has_loc && !pdi->has_const_value)
8240 return;
8241
8242 add_psymbol_to_list (actual_name,
8243 built_actual_name != NULL,
8244 VAR_DOMAIN, LOC_STATIC,
8245 SECT_OFF_TEXT (objfile),
8246 psymbol_placement::STATIC,
8247 has_loc ? addr : 0,
8248 cu->language, objfile);
8249 }
8250 break;
8251 case DW_TAG_typedef:
8252 case DW_TAG_base_type:
8253 case DW_TAG_subrange_type:
8254 add_psymbol_to_list (actual_name,
8255 built_actual_name != NULL,
8256 VAR_DOMAIN, LOC_TYPEDEF, -1,
8257 psymbol_placement::STATIC,
8258 0, cu->language, objfile);
8259 break;
8260 case DW_TAG_imported_declaration:
8261 case DW_TAG_namespace:
8262 add_psymbol_to_list (actual_name,
8263 built_actual_name != NULL,
8264 VAR_DOMAIN, LOC_TYPEDEF, -1,
8265 psymbol_placement::GLOBAL,
8266 0, cu->language, objfile);
8267 break;
8268 case DW_TAG_module:
8269 /* With Fortran 77 there might be a "BLOCK DATA" module
8270 available without any name. If so, we skip the module as it
8271 doesn't bring any value. */
8272 if (actual_name != nullptr)
8273 add_psymbol_to_list (actual_name,
8274 built_actual_name != NULL,
8275 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8276 psymbol_placement::GLOBAL,
8277 0, cu->language, objfile);
8278 break;
8279 case DW_TAG_class_type:
8280 case DW_TAG_interface_type:
8281 case DW_TAG_structure_type:
8282 case DW_TAG_union_type:
8283 case DW_TAG_enumeration_type:
8284 /* Skip external references. The DWARF standard says in the section
8285 about "Structure, Union, and Class Type Entries": "An incomplete
8286 structure, union or class type is represented by a structure,
8287 union or class entry that does not have a byte size attribute
8288 and that has a DW_AT_declaration attribute." */
8289 if (!pdi->has_byte_size && pdi->is_declaration)
8290 return;
8291
8292 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8293 static vs. global. */
8294 add_psymbol_to_list (actual_name,
8295 built_actual_name != NULL,
8296 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8297 cu->language == language_cplus
8298 ? psymbol_placement::GLOBAL
8299 : psymbol_placement::STATIC,
8300 0, cu->language, objfile);
8301
8302 break;
8303 case DW_TAG_enumerator:
8304 add_psymbol_to_list (actual_name,
8305 built_actual_name != NULL,
8306 VAR_DOMAIN, LOC_CONST, -1,
8307 cu->language == language_cplus
8308 ? psymbol_placement::GLOBAL
8309 : psymbol_placement::STATIC,
8310 0, cu->language, objfile);
8311 break;
8312 default:
8313 break;
8314 }
8315 }
8316
8317 /* Read a partial die corresponding to a namespace; also, add a symbol
8318 corresponding to that namespace to the symbol table. NAMESPACE is
8319 the name of the enclosing namespace. */
8320
8321 static void
8322 add_partial_namespace (struct partial_die_info *pdi,
8323 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8324 int set_addrmap, struct dwarf2_cu *cu)
8325 {
8326 /* Add a symbol for the namespace. */
8327
8328 add_partial_symbol (pdi, cu);
8329
8330 /* Now scan partial symbols in that namespace. */
8331
8332 if (pdi->has_children)
8333 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8334 }
8335
8336 /* Read a partial die corresponding to a Fortran module. */
8337
8338 static void
8339 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8340 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8341 {
8342 /* Add a symbol for the namespace. */
8343
8344 add_partial_symbol (pdi, cu);
8345
8346 /* Now scan partial symbols in that module. */
8347
8348 if (pdi->has_children)
8349 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8350 }
8351
8352 /* Read a partial die corresponding to a subprogram or an inlined
8353 subprogram and create a partial symbol for that subprogram.
8354 When the CU language allows it, this routine also defines a partial
8355 symbol for each nested subprogram that this subprogram contains.
8356 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8357 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8358
8359 PDI may also be a lexical block, in which case we simply search
8360 recursively for subprograms defined inside that lexical block.
8361 Again, this is only performed when the CU language allows this
8362 type of definitions. */
8363
8364 static void
8365 add_partial_subprogram (struct partial_die_info *pdi,
8366 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8367 int set_addrmap, struct dwarf2_cu *cu)
8368 {
8369 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8370 {
8371 if (pdi->has_pc_info)
8372 {
8373 if (pdi->lowpc < *lowpc)
8374 *lowpc = pdi->lowpc;
8375 if (pdi->highpc > *highpc)
8376 *highpc = pdi->highpc;
8377 if (set_addrmap)
8378 {
8379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8380 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8381 CORE_ADDR baseaddr;
8382 CORE_ADDR this_highpc;
8383 CORE_ADDR this_lowpc;
8384
8385 baseaddr = objfile->text_section_offset ();
8386 this_lowpc
8387 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8388 pdi->lowpc + baseaddr)
8389 - baseaddr);
8390 this_highpc
8391 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8392 pdi->highpc + baseaddr)
8393 - baseaddr);
8394 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8395 this_lowpc, this_highpc - 1,
8396 cu->per_cu->v.psymtab);
8397 }
8398 }
8399
8400 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8401 {
8402 if (!pdi->is_declaration)
8403 /* Ignore subprogram DIEs that do not have a name, they are
8404 illegal. Do not emit a complaint at this point, we will
8405 do so when we convert this psymtab into a symtab. */
8406 if (pdi->name)
8407 add_partial_symbol (pdi, cu);
8408 }
8409 }
8410
8411 if (! pdi->has_children)
8412 return;
8413
8414 if (cu->language == language_ada || cu->language == language_fortran)
8415 {
8416 pdi = pdi->die_child;
8417 while (pdi != NULL)
8418 {
8419 pdi->fixup (cu);
8420 if (pdi->tag == DW_TAG_subprogram
8421 || pdi->tag == DW_TAG_inlined_subroutine
8422 || pdi->tag == DW_TAG_lexical_block)
8423 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8424 pdi = pdi->die_sibling;
8425 }
8426 }
8427 }
8428
8429 /* Read a partial die corresponding to an enumeration type. */
8430
8431 static void
8432 add_partial_enumeration (struct partial_die_info *enum_pdi,
8433 struct dwarf2_cu *cu)
8434 {
8435 struct partial_die_info *pdi;
8436
8437 if (enum_pdi->name != NULL)
8438 add_partial_symbol (enum_pdi, cu);
8439
8440 pdi = enum_pdi->die_child;
8441 while (pdi)
8442 {
8443 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8444 complaint (_("malformed enumerator DIE ignored"));
8445 else
8446 add_partial_symbol (pdi, cu);
8447 pdi = pdi->die_sibling;
8448 }
8449 }
8450
8451 /* Return the initial uleb128 in the die at INFO_PTR. */
8452
8453 static unsigned int
8454 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8455 {
8456 unsigned int bytes_read;
8457
8458 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8459 }
8460
8461 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8462 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8463
8464 Return the corresponding abbrev, or NULL if the number is zero (indicating
8465 an empty DIE). In either case *BYTES_READ will be set to the length of
8466 the initial number. */
8467
8468 static struct abbrev_info *
8469 peek_die_abbrev (const die_reader_specs &reader,
8470 const gdb_byte *info_ptr, unsigned int *bytes_read)
8471 {
8472 dwarf2_cu *cu = reader.cu;
8473 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8474 unsigned int abbrev_number
8475 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8476
8477 if (abbrev_number == 0)
8478 return NULL;
8479
8480 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8481 if (!abbrev)
8482 {
8483 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8484 " at offset %s [in module %s]"),
8485 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8486 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8487 }
8488
8489 return abbrev;
8490 }
8491
8492 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8493 Returns a pointer to the end of a series of DIEs, terminated by an empty
8494 DIE. Any children of the skipped DIEs will also be skipped. */
8495
8496 static const gdb_byte *
8497 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8498 {
8499 while (1)
8500 {
8501 unsigned int bytes_read;
8502 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8503
8504 if (abbrev == NULL)
8505 return info_ptr + bytes_read;
8506 else
8507 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8508 }
8509 }
8510
8511 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8512 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8513 abbrev corresponding to that skipped uleb128 should be passed in
8514 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8515 children. */
8516
8517 static const gdb_byte *
8518 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8519 struct abbrev_info *abbrev)
8520 {
8521 unsigned int bytes_read;
8522 struct attribute attr;
8523 bfd *abfd = reader->abfd;
8524 struct dwarf2_cu *cu = reader->cu;
8525 const gdb_byte *buffer = reader->buffer;
8526 const gdb_byte *buffer_end = reader->buffer_end;
8527 unsigned int form, i;
8528
8529 for (i = 0; i < abbrev->num_attrs; i++)
8530 {
8531 /* The only abbrev we care about is DW_AT_sibling. */
8532 if (abbrev->attrs[i].name == DW_AT_sibling)
8533 {
8534 bool ignored;
8535 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8536 &ignored);
8537 if (attr.form == DW_FORM_ref_addr)
8538 complaint (_("ignoring absolute DW_AT_sibling"));
8539 else
8540 {
8541 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8542 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8543
8544 if (sibling_ptr < info_ptr)
8545 complaint (_("DW_AT_sibling points backwards"));
8546 else if (sibling_ptr > reader->buffer_end)
8547 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8548 else
8549 return sibling_ptr;
8550 }
8551 }
8552
8553 /* If it isn't DW_AT_sibling, skip this attribute. */
8554 form = abbrev->attrs[i].form;
8555 skip_attribute:
8556 switch (form)
8557 {
8558 case DW_FORM_ref_addr:
8559 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8560 and later it is offset sized. */
8561 if (cu->header.version == 2)
8562 info_ptr += cu->header.addr_size;
8563 else
8564 info_ptr += cu->header.offset_size;
8565 break;
8566 case DW_FORM_GNU_ref_alt:
8567 info_ptr += cu->header.offset_size;
8568 break;
8569 case DW_FORM_addr:
8570 info_ptr += cu->header.addr_size;
8571 break;
8572 case DW_FORM_data1:
8573 case DW_FORM_ref1:
8574 case DW_FORM_flag:
8575 case DW_FORM_strx1:
8576 info_ptr += 1;
8577 break;
8578 case DW_FORM_flag_present:
8579 case DW_FORM_implicit_const:
8580 break;
8581 case DW_FORM_data2:
8582 case DW_FORM_ref2:
8583 case DW_FORM_strx2:
8584 info_ptr += 2;
8585 break;
8586 case DW_FORM_strx3:
8587 info_ptr += 3;
8588 break;
8589 case DW_FORM_data4:
8590 case DW_FORM_ref4:
8591 case DW_FORM_strx4:
8592 info_ptr += 4;
8593 break;
8594 case DW_FORM_data8:
8595 case DW_FORM_ref8:
8596 case DW_FORM_ref_sig8:
8597 info_ptr += 8;
8598 break;
8599 case DW_FORM_data16:
8600 info_ptr += 16;
8601 break;
8602 case DW_FORM_string:
8603 read_direct_string (abfd, info_ptr, &bytes_read);
8604 info_ptr += bytes_read;
8605 break;
8606 case DW_FORM_sec_offset:
8607 case DW_FORM_strp:
8608 case DW_FORM_GNU_strp_alt:
8609 info_ptr += cu->header.offset_size;
8610 break;
8611 case DW_FORM_exprloc:
8612 case DW_FORM_block:
8613 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8614 info_ptr += bytes_read;
8615 break;
8616 case DW_FORM_block1:
8617 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8618 break;
8619 case DW_FORM_block2:
8620 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8621 break;
8622 case DW_FORM_block4:
8623 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8624 break;
8625 case DW_FORM_addrx:
8626 case DW_FORM_strx:
8627 case DW_FORM_sdata:
8628 case DW_FORM_udata:
8629 case DW_FORM_ref_udata:
8630 case DW_FORM_GNU_addr_index:
8631 case DW_FORM_GNU_str_index:
8632 case DW_FORM_rnglistx:
8633 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8634 break;
8635 case DW_FORM_indirect:
8636 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8637 info_ptr += bytes_read;
8638 /* We need to continue parsing from here, so just go back to
8639 the top. */
8640 goto skip_attribute;
8641
8642 default:
8643 error (_("Dwarf Error: Cannot handle %s "
8644 "in DWARF reader [in module %s]"),
8645 dwarf_form_name (form),
8646 bfd_get_filename (abfd));
8647 }
8648 }
8649
8650 if (abbrev->has_children)
8651 return skip_children (reader, info_ptr);
8652 else
8653 return info_ptr;
8654 }
8655
8656 /* Locate ORIG_PDI's sibling.
8657 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8658
8659 static const gdb_byte *
8660 locate_pdi_sibling (const struct die_reader_specs *reader,
8661 struct partial_die_info *orig_pdi,
8662 const gdb_byte *info_ptr)
8663 {
8664 /* Do we know the sibling already? */
8665
8666 if (orig_pdi->sibling)
8667 return orig_pdi->sibling;
8668
8669 /* Are there any children to deal with? */
8670
8671 if (!orig_pdi->has_children)
8672 return info_ptr;
8673
8674 /* Skip the children the long way. */
8675
8676 return skip_children (reader, info_ptr);
8677 }
8678
8679 /* Expand this partial symbol table into a full symbol table. SELF is
8680 not NULL. */
8681
8682 void
8683 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8684 {
8685 struct dwarf2_per_objfile *dwarf2_per_objfile
8686 = get_dwarf2_per_objfile (objfile);
8687
8688 gdb_assert (!readin);
8689 /* If this psymtab is constructed from a debug-only objfile, the
8690 has_section_at_zero flag will not necessarily be correct. We
8691 can get the correct value for this flag by looking at the data
8692 associated with the (presumably stripped) associated objfile. */
8693 if (objfile->separate_debug_objfile_backlink)
8694 {
8695 struct dwarf2_per_objfile *dpo_backlink
8696 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8697
8698 dwarf2_per_objfile->has_section_at_zero
8699 = dpo_backlink->has_section_at_zero;
8700 }
8701
8702 dwarf2_per_objfile->reading_partial_symbols = 0;
8703
8704 expand_psymtab (objfile);
8705
8706 process_cu_includes (dwarf2_per_objfile);
8707 }
8708 \f
8709 /* Reading in full CUs. */
8710
8711 /* Add PER_CU to the queue. */
8712
8713 static void
8714 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8715 enum language pretend_language)
8716 {
8717 per_cu->queued = 1;
8718 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8719 }
8720
8721 /* If PER_CU is not yet queued, add it to the queue.
8722 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8723 dependency.
8724 The result is non-zero if PER_CU was queued, otherwise the result is zero
8725 meaning either PER_CU is already queued or it is already loaded.
8726
8727 N.B. There is an invariant here that if a CU is queued then it is loaded.
8728 The caller is required to load PER_CU if we return non-zero. */
8729
8730 static int
8731 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8732 struct dwarf2_per_cu_data *per_cu,
8733 enum language pretend_language)
8734 {
8735 /* We may arrive here during partial symbol reading, if we need full
8736 DIEs to process an unusual case (e.g. template arguments). Do
8737 not queue PER_CU, just tell our caller to load its DIEs. */
8738 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8739 {
8740 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8741 return 1;
8742 return 0;
8743 }
8744
8745 /* Mark the dependence relation so that we don't flush PER_CU
8746 too early. */
8747 if (dependent_cu != NULL)
8748 dwarf2_add_dependence (dependent_cu, per_cu);
8749
8750 /* If it's already on the queue, we have nothing to do. */
8751 if (per_cu->queued)
8752 return 0;
8753
8754 /* If the compilation unit is already loaded, just mark it as
8755 used. */
8756 if (per_cu->cu != NULL)
8757 {
8758 per_cu->cu->last_used = 0;
8759 return 0;
8760 }
8761
8762 /* Add it to the queue. */
8763 queue_comp_unit (per_cu, pretend_language);
8764
8765 return 1;
8766 }
8767
8768 /* Process the queue. */
8769
8770 static void
8771 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8772 {
8773 if (dwarf_read_debug)
8774 {
8775 fprintf_unfiltered (gdb_stdlog,
8776 "Expanding one or more symtabs of objfile %s ...\n",
8777 objfile_name (dwarf2_per_objfile->objfile));
8778 }
8779
8780 /* The queue starts out with one item, but following a DIE reference
8781 may load a new CU, adding it to the end of the queue. */
8782 while (!dwarf2_per_objfile->queue.empty ())
8783 {
8784 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8785
8786 if ((dwarf2_per_objfile->using_index
8787 ? !item.per_cu->v.quick->compunit_symtab
8788 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8789 /* Skip dummy CUs. */
8790 && item.per_cu->cu != NULL)
8791 {
8792 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8793 unsigned int debug_print_threshold;
8794 char buf[100];
8795
8796 if (per_cu->is_debug_types)
8797 {
8798 struct signatured_type *sig_type =
8799 (struct signatured_type *) per_cu;
8800
8801 sprintf (buf, "TU %s at offset %s",
8802 hex_string (sig_type->signature),
8803 sect_offset_str (per_cu->sect_off));
8804 /* There can be 100s of TUs.
8805 Only print them in verbose mode. */
8806 debug_print_threshold = 2;
8807 }
8808 else
8809 {
8810 sprintf (buf, "CU at offset %s",
8811 sect_offset_str (per_cu->sect_off));
8812 debug_print_threshold = 1;
8813 }
8814
8815 if (dwarf_read_debug >= debug_print_threshold)
8816 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8817
8818 if (per_cu->is_debug_types)
8819 process_full_type_unit (per_cu, item.pretend_language);
8820 else
8821 process_full_comp_unit (per_cu, item.pretend_language);
8822
8823 if (dwarf_read_debug >= debug_print_threshold)
8824 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8825 }
8826
8827 item.per_cu->queued = 0;
8828 dwarf2_per_objfile->queue.pop ();
8829 }
8830
8831 if (dwarf_read_debug)
8832 {
8833 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8834 objfile_name (dwarf2_per_objfile->objfile));
8835 }
8836 }
8837
8838 /* Read in full symbols for PST, and anything it depends on. */
8839
8840 void
8841 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8842 {
8843 struct dwarf2_per_cu_data *per_cu;
8844
8845 if (readin)
8846 return;
8847
8848 read_dependencies (objfile);
8849
8850 per_cu = per_cu_data;
8851
8852 if (per_cu == NULL)
8853 {
8854 /* It's an include file, no symbols to read for it.
8855 Everything is in the parent symtab. */
8856 readin = true;
8857 return;
8858 }
8859
8860 dw2_do_instantiate_symtab (per_cu, false);
8861 }
8862
8863 /* Trivial hash function for die_info: the hash value of a DIE
8864 is its offset in .debug_info for this objfile. */
8865
8866 static hashval_t
8867 die_hash (const void *item)
8868 {
8869 const struct die_info *die = (const struct die_info *) item;
8870
8871 return to_underlying (die->sect_off);
8872 }
8873
8874 /* Trivial comparison function for die_info structures: two DIEs
8875 are equal if they have the same offset. */
8876
8877 static int
8878 die_eq (const void *item_lhs, const void *item_rhs)
8879 {
8880 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8881 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8882
8883 return die_lhs->sect_off == die_rhs->sect_off;
8884 }
8885
8886 /* Load the DIEs associated with PER_CU into memory. */
8887
8888 static void
8889 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8890 bool skip_partial,
8891 enum language pretend_language)
8892 {
8893 gdb_assert (! this_cu->is_debug_types);
8894
8895 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
8896 if (reader.dummy_p)
8897 return;
8898
8899 struct dwarf2_cu *cu = reader.cu;
8900 const gdb_byte *info_ptr = reader.info_ptr;
8901
8902 gdb_assert (cu->die_hash == NULL);
8903 cu->die_hash =
8904 htab_create_alloc_ex (cu->header.length / 12,
8905 die_hash,
8906 die_eq,
8907 NULL,
8908 &cu->comp_unit_obstack,
8909 hashtab_obstack_allocate,
8910 dummy_obstack_deallocate);
8911
8912 if (reader.comp_unit_die->has_children)
8913 reader.comp_unit_die->child
8914 = read_die_and_siblings (&reader, reader.info_ptr,
8915 &info_ptr, reader.comp_unit_die);
8916 cu->dies = reader.comp_unit_die;
8917 /* comp_unit_die is not stored in die_hash, no need. */
8918
8919 /* We try not to read any attributes in this function, because not
8920 all CUs needed for references have been loaded yet, and symbol
8921 table processing isn't initialized. But we have to set the CU language,
8922 or we won't be able to build types correctly.
8923 Similarly, if we do not read the producer, we can not apply
8924 producer-specific interpretation. */
8925 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8926 }
8927
8928 /* Add a DIE to the delayed physname list. */
8929
8930 static void
8931 add_to_method_list (struct type *type, int fnfield_index, int index,
8932 const char *name, struct die_info *die,
8933 struct dwarf2_cu *cu)
8934 {
8935 struct delayed_method_info mi;
8936 mi.type = type;
8937 mi.fnfield_index = fnfield_index;
8938 mi.index = index;
8939 mi.name = name;
8940 mi.die = die;
8941 cu->method_list.push_back (mi);
8942 }
8943
8944 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8945 "const" / "volatile". If so, decrements LEN by the length of the
8946 modifier and return true. Otherwise return false. */
8947
8948 template<size_t N>
8949 static bool
8950 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8951 {
8952 size_t mod_len = sizeof (mod) - 1;
8953 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8954 {
8955 len -= mod_len;
8956 return true;
8957 }
8958 return false;
8959 }
8960
8961 /* Compute the physnames of any methods on the CU's method list.
8962
8963 The computation of method physnames is delayed in order to avoid the
8964 (bad) condition that one of the method's formal parameters is of an as yet
8965 incomplete type. */
8966
8967 static void
8968 compute_delayed_physnames (struct dwarf2_cu *cu)
8969 {
8970 /* Only C++ delays computing physnames. */
8971 if (cu->method_list.empty ())
8972 return;
8973 gdb_assert (cu->language == language_cplus);
8974
8975 for (const delayed_method_info &mi : cu->method_list)
8976 {
8977 const char *physname;
8978 struct fn_fieldlist *fn_flp
8979 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8980 physname = dwarf2_physname (mi.name, mi.die, cu);
8981 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8982 = physname ? physname : "";
8983
8984 /* Since there's no tag to indicate whether a method is a
8985 const/volatile overload, extract that information out of the
8986 demangled name. */
8987 if (physname != NULL)
8988 {
8989 size_t len = strlen (physname);
8990
8991 while (1)
8992 {
8993 if (physname[len] == ')') /* shortcut */
8994 break;
8995 else if (check_modifier (physname, len, " const"))
8996 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8997 else if (check_modifier (physname, len, " volatile"))
8998 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8999 else
9000 break;
9001 }
9002 }
9003 }
9004
9005 /* The list is no longer needed. */
9006 cu->method_list.clear ();
9007 }
9008
9009 /* Go objects should be embedded in a DW_TAG_module DIE,
9010 and it's not clear if/how imported objects will appear.
9011 To keep Go support simple until that's worked out,
9012 go back through what we've read and create something usable.
9013 We could do this while processing each DIE, and feels kinda cleaner,
9014 but that way is more invasive.
9015 This is to, for example, allow the user to type "p var" or "b main"
9016 without having to specify the package name, and allow lookups
9017 of module.object to work in contexts that use the expression
9018 parser. */
9019
9020 static void
9021 fixup_go_packaging (struct dwarf2_cu *cu)
9022 {
9023 gdb::unique_xmalloc_ptr<char> package_name;
9024 struct pending *list;
9025 int i;
9026
9027 for (list = *cu->get_builder ()->get_global_symbols ();
9028 list != NULL;
9029 list = list->next)
9030 {
9031 for (i = 0; i < list->nsyms; ++i)
9032 {
9033 struct symbol *sym = list->symbol[i];
9034
9035 if (sym->language () == language_go
9036 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9037 {
9038 gdb::unique_xmalloc_ptr<char> this_package_name
9039 (go_symbol_package_name (sym));
9040
9041 if (this_package_name == NULL)
9042 continue;
9043 if (package_name == NULL)
9044 package_name = std::move (this_package_name);
9045 else
9046 {
9047 struct objfile *objfile
9048 = cu->per_cu->dwarf2_per_objfile->objfile;
9049 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9050 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9051 (symbol_symtab (sym) != NULL
9052 ? symtab_to_filename_for_display
9053 (symbol_symtab (sym))
9054 : objfile_name (objfile)),
9055 this_package_name.get (), package_name.get ());
9056 }
9057 }
9058 }
9059 }
9060
9061 if (package_name != NULL)
9062 {
9063 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9064 const char *saved_package_name
9065 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9066 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9067 saved_package_name);
9068 struct symbol *sym;
9069
9070 sym = allocate_symbol (objfile);
9071 sym->set_language (language_go, &objfile->objfile_obstack);
9072 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9073 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9074 e.g., "main" finds the "main" module and not C's main(). */
9075 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9076 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9077 SYMBOL_TYPE (sym) = type;
9078
9079 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9080 }
9081 }
9082
9083 /* Allocate a fully-qualified name consisting of the two parts on the
9084 obstack. */
9085
9086 static const char *
9087 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9088 {
9089 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9090 }
9091
9092 /* A helper that allocates a struct discriminant_info to attach to a
9093 union type. */
9094
9095 static struct discriminant_info *
9096 alloc_discriminant_info (struct type *type, int discriminant_index,
9097 int default_index)
9098 {
9099 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9100 gdb_assert (discriminant_index == -1
9101 || (discriminant_index >= 0
9102 && discriminant_index < TYPE_NFIELDS (type)));
9103 gdb_assert (default_index == -1
9104 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9105
9106 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9107
9108 struct discriminant_info *disc
9109 = ((struct discriminant_info *)
9110 TYPE_ZALLOC (type,
9111 offsetof (struct discriminant_info, discriminants)
9112 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9113 disc->default_index = default_index;
9114 disc->discriminant_index = discriminant_index;
9115
9116 struct dynamic_prop prop;
9117 prop.kind = PROP_UNDEFINED;
9118 prop.data.baton = disc;
9119
9120 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9121
9122 return disc;
9123 }
9124
9125 /* Some versions of rustc emitted enums in an unusual way.
9126
9127 Ordinary enums were emitted as unions. The first element of each
9128 structure in the union was named "RUST$ENUM$DISR". This element
9129 held the discriminant.
9130
9131 These versions of Rust also implemented the "non-zero"
9132 optimization. When the enum had two values, and one is empty and
9133 the other holds a pointer that cannot be zero, the pointer is used
9134 as the discriminant, with a zero value meaning the empty variant.
9135 Here, the union's first member is of the form
9136 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9137 where the fieldnos are the indices of the fields that should be
9138 traversed in order to find the field (which may be several fields deep)
9139 and the variantname is the name of the variant of the case when the
9140 field is zero.
9141
9142 This function recognizes whether TYPE is of one of these forms,
9143 and, if so, smashes it to be a variant type. */
9144
9145 static void
9146 quirk_rust_enum (struct type *type, struct objfile *objfile)
9147 {
9148 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9149
9150 /* We don't need to deal with empty enums. */
9151 if (TYPE_NFIELDS (type) == 0)
9152 return;
9153
9154 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9155 if (TYPE_NFIELDS (type) == 1
9156 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9157 {
9158 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9159
9160 /* Decode the field name to find the offset of the
9161 discriminant. */
9162 ULONGEST bit_offset = 0;
9163 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9164 while (name[0] >= '0' && name[0] <= '9')
9165 {
9166 char *tail;
9167 unsigned long index = strtoul (name, &tail, 10);
9168 name = tail;
9169 if (*name != '$'
9170 || index >= TYPE_NFIELDS (field_type)
9171 || (TYPE_FIELD_LOC_KIND (field_type, index)
9172 != FIELD_LOC_KIND_BITPOS))
9173 {
9174 complaint (_("Could not parse Rust enum encoding string \"%s\""
9175 "[in module %s]"),
9176 TYPE_FIELD_NAME (type, 0),
9177 objfile_name (objfile));
9178 return;
9179 }
9180 ++name;
9181
9182 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9183 field_type = TYPE_FIELD_TYPE (field_type, index);
9184 }
9185
9186 /* Make a union to hold the variants. */
9187 struct type *union_type = alloc_type (objfile);
9188 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9189 TYPE_NFIELDS (union_type) = 3;
9190 TYPE_FIELDS (union_type)
9191 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9192 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9193 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9194
9195 /* Put the discriminant must at index 0. */
9196 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9197 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9198 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9199 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9200
9201 /* The order of fields doesn't really matter, so put the real
9202 field at index 1 and the data-less field at index 2. */
9203 struct discriminant_info *disc
9204 = alloc_discriminant_info (union_type, 0, 1);
9205 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9206 TYPE_FIELD_NAME (union_type, 1)
9207 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9208 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9209 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9210 TYPE_FIELD_NAME (union_type, 1));
9211
9212 const char *dataless_name
9213 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9214 name);
9215 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9216 dataless_name);
9217 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9218 /* NAME points into the original discriminant name, which
9219 already has the correct lifetime. */
9220 TYPE_FIELD_NAME (union_type, 2) = name;
9221 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9222 disc->discriminants[2] = 0;
9223
9224 /* Smash this type to be a structure type. We have to do this
9225 because the type has already been recorded. */
9226 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9227 TYPE_NFIELDS (type) = 1;
9228 TYPE_FIELDS (type)
9229 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9230
9231 /* Install the variant part. */
9232 TYPE_FIELD_TYPE (type, 0) = union_type;
9233 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9234 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9235 }
9236 /* A union with a single anonymous field is probably an old-style
9237 univariant enum. */
9238 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9239 {
9240 /* Smash this type to be a structure type. We have to do this
9241 because the type has already been recorded. */
9242 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9243
9244 /* Make a union to hold the variants. */
9245 struct type *union_type = alloc_type (objfile);
9246 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9247 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9248 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9249 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9250 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9251
9252 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9253 const char *variant_name
9254 = rust_last_path_segment (TYPE_NAME (field_type));
9255 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9256 TYPE_NAME (field_type)
9257 = rust_fully_qualify (&objfile->objfile_obstack,
9258 TYPE_NAME (type), variant_name);
9259
9260 /* Install the union in the outer struct type. */
9261 TYPE_NFIELDS (type) = 1;
9262 TYPE_FIELDS (type)
9263 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9264 TYPE_FIELD_TYPE (type, 0) = union_type;
9265 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9266 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9267
9268 alloc_discriminant_info (union_type, -1, 0);
9269 }
9270 else
9271 {
9272 struct type *disr_type = nullptr;
9273 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9274 {
9275 disr_type = TYPE_FIELD_TYPE (type, i);
9276
9277 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9278 {
9279 /* All fields of a true enum will be structs. */
9280 return;
9281 }
9282 else if (TYPE_NFIELDS (disr_type) == 0)
9283 {
9284 /* Could be data-less variant, so keep going. */
9285 disr_type = nullptr;
9286 }
9287 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9288 "RUST$ENUM$DISR") != 0)
9289 {
9290 /* Not a Rust enum. */
9291 return;
9292 }
9293 else
9294 {
9295 /* Found one. */
9296 break;
9297 }
9298 }
9299
9300 /* If we got here without a discriminant, then it's probably
9301 just a union. */
9302 if (disr_type == nullptr)
9303 return;
9304
9305 /* Smash this type to be a structure type. We have to do this
9306 because the type has already been recorded. */
9307 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9308
9309 /* Make a union to hold the variants. */
9310 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9311 struct type *union_type = alloc_type (objfile);
9312 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9313 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9314 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9315 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9316 TYPE_FIELDS (union_type)
9317 = (struct field *) TYPE_ZALLOC (union_type,
9318 (TYPE_NFIELDS (union_type)
9319 * sizeof (struct field)));
9320
9321 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9322 TYPE_NFIELDS (type) * sizeof (struct field));
9323
9324 /* Install the discriminant at index 0 in the union. */
9325 TYPE_FIELD (union_type, 0) = *disr_field;
9326 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9327 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9328
9329 /* Install the union in the outer struct type. */
9330 TYPE_FIELD_TYPE (type, 0) = union_type;
9331 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9332 TYPE_NFIELDS (type) = 1;
9333
9334 /* Set the size and offset of the union type. */
9335 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9336
9337 /* We need a way to find the correct discriminant given a
9338 variant name. For convenience we build a map here. */
9339 struct type *enum_type = FIELD_TYPE (*disr_field);
9340 std::unordered_map<std::string, ULONGEST> discriminant_map;
9341 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9342 {
9343 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9344 {
9345 const char *name
9346 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9347 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9348 }
9349 }
9350
9351 int n_fields = TYPE_NFIELDS (union_type);
9352 struct discriminant_info *disc
9353 = alloc_discriminant_info (union_type, 0, -1);
9354 /* Skip the discriminant here. */
9355 for (int i = 1; i < n_fields; ++i)
9356 {
9357 /* Find the final word in the name of this variant's type.
9358 That name can be used to look up the correct
9359 discriminant. */
9360 const char *variant_name
9361 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9362 i)));
9363
9364 auto iter = discriminant_map.find (variant_name);
9365 if (iter != discriminant_map.end ())
9366 disc->discriminants[i] = iter->second;
9367
9368 /* Remove the discriminant field, if it exists. */
9369 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9370 if (TYPE_NFIELDS (sub_type) > 0)
9371 {
9372 --TYPE_NFIELDS (sub_type);
9373 ++TYPE_FIELDS (sub_type);
9374 }
9375 TYPE_FIELD_NAME (union_type, i) = variant_name;
9376 TYPE_NAME (sub_type)
9377 = rust_fully_qualify (&objfile->objfile_obstack,
9378 TYPE_NAME (type), variant_name);
9379 }
9380 }
9381 }
9382
9383 /* Rewrite some Rust unions to be structures with variants parts. */
9384
9385 static void
9386 rust_union_quirks (struct dwarf2_cu *cu)
9387 {
9388 gdb_assert (cu->language == language_rust);
9389 for (type *type_ : cu->rust_unions)
9390 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9391 /* We don't need this any more. */
9392 cu->rust_unions.clear ();
9393 }
9394
9395 /* Return the symtab for PER_CU. This works properly regardless of
9396 whether we're using the index or psymtabs. */
9397
9398 static struct compunit_symtab *
9399 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9400 {
9401 return (per_cu->dwarf2_per_objfile->using_index
9402 ? per_cu->v.quick->compunit_symtab
9403 : per_cu->v.psymtab->compunit_symtab);
9404 }
9405
9406 /* A helper function for computing the list of all symbol tables
9407 included by PER_CU. */
9408
9409 static void
9410 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9411 htab_t all_children, htab_t all_type_symtabs,
9412 struct dwarf2_per_cu_data *per_cu,
9413 struct compunit_symtab *immediate_parent)
9414 {
9415 void **slot;
9416 struct compunit_symtab *cust;
9417
9418 slot = htab_find_slot (all_children, per_cu, INSERT);
9419 if (*slot != NULL)
9420 {
9421 /* This inclusion and its children have been processed. */
9422 return;
9423 }
9424
9425 *slot = per_cu;
9426 /* Only add a CU if it has a symbol table. */
9427 cust = get_compunit_symtab (per_cu);
9428 if (cust != NULL)
9429 {
9430 /* If this is a type unit only add its symbol table if we haven't
9431 seen it yet (type unit per_cu's can share symtabs). */
9432 if (per_cu->is_debug_types)
9433 {
9434 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9435 if (*slot == NULL)
9436 {
9437 *slot = cust;
9438 result->push_back (cust);
9439 if (cust->user == NULL)
9440 cust->user = immediate_parent;
9441 }
9442 }
9443 else
9444 {
9445 result->push_back (cust);
9446 if (cust->user == NULL)
9447 cust->user = immediate_parent;
9448 }
9449 }
9450
9451 if (!per_cu->imported_symtabs_empty ())
9452 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9453 {
9454 recursively_compute_inclusions (result, all_children,
9455 all_type_symtabs, ptr, cust);
9456 }
9457 }
9458
9459 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9460 PER_CU. */
9461
9462 static void
9463 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9464 {
9465 gdb_assert (! per_cu->is_debug_types);
9466
9467 if (!per_cu->imported_symtabs_empty ())
9468 {
9469 int len;
9470 std::vector<compunit_symtab *> result_symtabs;
9471 htab_t all_children, all_type_symtabs;
9472 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9473
9474 /* If we don't have a symtab, we can just skip this case. */
9475 if (cust == NULL)
9476 return;
9477
9478 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9479 NULL, xcalloc, xfree);
9480 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9481 NULL, xcalloc, xfree);
9482
9483 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9484 {
9485 recursively_compute_inclusions (&result_symtabs, all_children,
9486 all_type_symtabs, ptr, cust);
9487 }
9488
9489 /* Now we have a transitive closure of all the included symtabs. */
9490 len = result_symtabs.size ();
9491 cust->includes
9492 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9493 struct compunit_symtab *, len + 1);
9494 memcpy (cust->includes, result_symtabs.data (),
9495 len * sizeof (compunit_symtab *));
9496 cust->includes[len] = NULL;
9497
9498 htab_delete (all_children);
9499 htab_delete (all_type_symtabs);
9500 }
9501 }
9502
9503 /* Compute the 'includes' field for the symtabs of all the CUs we just
9504 read. */
9505
9506 static void
9507 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9508 {
9509 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9510 {
9511 if (! iter->is_debug_types)
9512 compute_compunit_symtab_includes (iter);
9513 }
9514
9515 dwarf2_per_objfile->just_read_cus.clear ();
9516 }
9517
9518 /* Generate full symbol information for PER_CU, whose DIEs have
9519 already been loaded into memory. */
9520
9521 static void
9522 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9523 enum language pretend_language)
9524 {
9525 struct dwarf2_cu *cu = per_cu->cu;
9526 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9527 struct objfile *objfile = dwarf2_per_objfile->objfile;
9528 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9529 CORE_ADDR lowpc, highpc;
9530 struct compunit_symtab *cust;
9531 CORE_ADDR baseaddr;
9532 struct block *static_block;
9533 CORE_ADDR addr;
9534
9535 baseaddr = objfile->text_section_offset ();
9536
9537 /* Clear the list here in case something was left over. */
9538 cu->method_list.clear ();
9539
9540 cu->language = pretend_language;
9541 cu->language_defn = language_def (cu->language);
9542
9543 /* Do line number decoding in read_file_scope () */
9544 process_die (cu->dies, cu);
9545
9546 /* For now fudge the Go package. */
9547 if (cu->language == language_go)
9548 fixup_go_packaging (cu);
9549
9550 /* Now that we have processed all the DIEs in the CU, all the types
9551 should be complete, and it should now be safe to compute all of the
9552 physnames. */
9553 compute_delayed_physnames (cu);
9554
9555 if (cu->language == language_rust)
9556 rust_union_quirks (cu);
9557
9558 /* Some compilers don't define a DW_AT_high_pc attribute for the
9559 compilation unit. If the DW_AT_high_pc is missing, synthesize
9560 it, by scanning the DIE's below the compilation unit. */
9561 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9562
9563 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9564 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9565
9566 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9567 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9568 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9569 addrmap to help ensure it has an accurate map of pc values belonging to
9570 this comp unit. */
9571 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9572
9573 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9574 SECT_OFF_TEXT (objfile),
9575 0);
9576
9577 if (cust != NULL)
9578 {
9579 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9580
9581 /* Set symtab language to language from DW_AT_language. If the
9582 compilation is from a C file generated by language preprocessors, do
9583 not set the language if it was already deduced by start_subfile. */
9584 if (!(cu->language == language_c
9585 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9586 COMPUNIT_FILETABS (cust)->language = cu->language;
9587
9588 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9589 produce DW_AT_location with location lists but it can be possibly
9590 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9591 there were bugs in prologue debug info, fixed later in GCC-4.5
9592 by "unwind info for epilogues" patch (which is not directly related).
9593
9594 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9595 needed, it would be wrong due to missing DW_AT_producer there.
9596
9597 Still one can confuse GDB by using non-standard GCC compilation
9598 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9599 */
9600 if (cu->has_loclist && gcc_4_minor >= 5)
9601 cust->locations_valid = 1;
9602
9603 if (gcc_4_minor >= 5)
9604 cust->epilogue_unwind_valid = 1;
9605
9606 cust->call_site_htab = cu->call_site_htab;
9607 }
9608
9609 if (dwarf2_per_objfile->using_index)
9610 per_cu->v.quick->compunit_symtab = cust;
9611 else
9612 {
9613 dwarf2_psymtab *pst = per_cu->v.psymtab;
9614 pst->compunit_symtab = cust;
9615 pst->readin = true;
9616 }
9617
9618 /* Push it for inclusion processing later. */
9619 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9620
9621 /* Not needed any more. */
9622 cu->reset_builder ();
9623 }
9624
9625 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9626 already been loaded into memory. */
9627
9628 static void
9629 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9630 enum language pretend_language)
9631 {
9632 struct dwarf2_cu *cu = per_cu->cu;
9633 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9634 struct objfile *objfile = dwarf2_per_objfile->objfile;
9635 struct compunit_symtab *cust;
9636 struct signatured_type *sig_type;
9637
9638 gdb_assert (per_cu->is_debug_types);
9639 sig_type = (struct signatured_type *) per_cu;
9640
9641 /* Clear the list here in case something was left over. */
9642 cu->method_list.clear ();
9643
9644 cu->language = pretend_language;
9645 cu->language_defn = language_def (cu->language);
9646
9647 /* The symbol tables are set up in read_type_unit_scope. */
9648 process_die (cu->dies, cu);
9649
9650 /* For now fudge the Go package. */
9651 if (cu->language == language_go)
9652 fixup_go_packaging (cu);
9653
9654 /* Now that we have processed all the DIEs in the CU, all the types
9655 should be complete, and it should now be safe to compute all of the
9656 physnames. */
9657 compute_delayed_physnames (cu);
9658
9659 if (cu->language == language_rust)
9660 rust_union_quirks (cu);
9661
9662 /* TUs share symbol tables.
9663 If this is the first TU to use this symtab, complete the construction
9664 of it with end_expandable_symtab. Otherwise, complete the addition of
9665 this TU's symbols to the existing symtab. */
9666 if (sig_type->type_unit_group->compunit_symtab == NULL)
9667 {
9668 buildsym_compunit *builder = cu->get_builder ();
9669 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9670 sig_type->type_unit_group->compunit_symtab = cust;
9671
9672 if (cust != NULL)
9673 {
9674 /* Set symtab language to language from DW_AT_language. If the
9675 compilation is from a C file generated by language preprocessors,
9676 do not set the language if it was already deduced by
9677 start_subfile. */
9678 if (!(cu->language == language_c
9679 && COMPUNIT_FILETABS (cust)->language != language_c))
9680 COMPUNIT_FILETABS (cust)->language = cu->language;
9681 }
9682 }
9683 else
9684 {
9685 cu->get_builder ()->augment_type_symtab ();
9686 cust = sig_type->type_unit_group->compunit_symtab;
9687 }
9688
9689 if (dwarf2_per_objfile->using_index)
9690 per_cu->v.quick->compunit_symtab = cust;
9691 else
9692 {
9693 dwarf2_psymtab *pst = per_cu->v.psymtab;
9694 pst->compunit_symtab = cust;
9695 pst->readin = true;
9696 }
9697
9698 /* Not needed any more. */
9699 cu->reset_builder ();
9700 }
9701
9702 /* Process an imported unit DIE. */
9703
9704 static void
9705 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9706 {
9707 struct attribute *attr;
9708
9709 /* For now we don't handle imported units in type units. */
9710 if (cu->per_cu->is_debug_types)
9711 {
9712 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9713 " supported in type units [in module %s]"),
9714 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9715 }
9716
9717 attr = dwarf2_attr (die, DW_AT_import, cu);
9718 if (attr != NULL)
9719 {
9720 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9721 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9722 dwarf2_per_cu_data *per_cu
9723 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9724 cu->per_cu->dwarf2_per_objfile);
9725
9726 /* If necessary, add it to the queue and load its DIEs. */
9727 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9728 load_full_comp_unit (per_cu, false, cu->language);
9729
9730 cu->per_cu->imported_symtabs_push (per_cu);
9731 }
9732 }
9733
9734 /* RAII object that represents a process_die scope: i.e.,
9735 starts/finishes processing a DIE. */
9736 class process_die_scope
9737 {
9738 public:
9739 process_die_scope (die_info *die, dwarf2_cu *cu)
9740 : m_die (die), m_cu (cu)
9741 {
9742 /* We should only be processing DIEs not already in process. */
9743 gdb_assert (!m_die->in_process);
9744 m_die->in_process = true;
9745 }
9746
9747 ~process_die_scope ()
9748 {
9749 m_die->in_process = false;
9750
9751 /* If we're done processing the DIE for the CU that owns the line
9752 header, we don't need the line header anymore. */
9753 if (m_cu->line_header_die_owner == m_die)
9754 {
9755 delete m_cu->line_header;
9756 m_cu->line_header = NULL;
9757 m_cu->line_header_die_owner = NULL;
9758 }
9759 }
9760
9761 private:
9762 die_info *m_die;
9763 dwarf2_cu *m_cu;
9764 };
9765
9766 /* Process a die and its children. */
9767
9768 static void
9769 process_die (struct die_info *die, struct dwarf2_cu *cu)
9770 {
9771 process_die_scope scope (die, cu);
9772
9773 switch (die->tag)
9774 {
9775 case DW_TAG_padding:
9776 break;
9777 case DW_TAG_compile_unit:
9778 case DW_TAG_partial_unit:
9779 read_file_scope (die, cu);
9780 break;
9781 case DW_TAG_type_unit:
9782 read_type_unit_scope (die, cu);
9783 break;
9784 case DW_TAG_subprogram:
9785 /* Nested subprograms in Fortran get a prefix. */
9786 if (cu->language == language_fortran
9787 && die->parent != NULL
9788 && die->parent->tag == DW_TAG_subprogram)
9789 cu->processing_has_namespace_info = true;
9790 /* Fall through. */
9791 case DW_TAG_inlined_subroutine:
9792 read_func_scope (die, cu);
9793 break;
9794 case DW_TAG_lexical_block:
9795 case DW_TAG_try_block:
9796 case DW_TAG_catch_block:
9797 read_lexical_block_scope (die, cu);
9798 break;
9799 case DW_TAG_call_site:
9800 case DW_TAG_GNU_call_site:
9801 read_call_site_scope (die, cu);
9802 break;
9803 case DW_TAG_class_type:
9804 case DW_TAG_interface_type:
9805 case DW_TAG_structure_type:
9806 case DW_TAG_union_type:
9807 process_structure_scope (die, cu);
9808 break;
9809 case DW_TAG_enumeration_type:
9810 process_enumeration_scope (die, cu);
9811 break;
9812
9813 /* These dies have a type, but processing them does not create
9814 a symbol or recurse to process the children. Therefore we can
9815 read them on-demand through read_type_die. */
9816 case DW_TAG_subroutine_type:
9817 case DW_TAG_set_type:
9818 case DW_TAG_array_type:
9819 case DW_TAG_pointer_type:
9820 case DW_TAG_ptr_to_member_type:
9821 case DW_TAG_reference_type:
9822 case DW_TAG_rvalue_reference_type:
9823 case DW_TAG_string_type:
9824 break;
9825
9826 case DW_TAG_base_type:
9827 case DW_TAG_subrange_type:
9828 case DW_TAG_typedef:
9829 /* Add a typedef symbol for the type definition, if it has a
9830 DW_AT_name. */
9831 new_symbol (die, read_type_die (die, cu), cu);
9832 break;
9833 case DW_TAG_common_block:
9834 read_common_block (die, cu);
9835 break;
9836 case DW_TAG_common_inclusion:
9837 break;
9838 case DW_TAG_namespace:
9839 cu->processing_has_namespace_info = true;
9840 read_namespace (die, cu);
9841 break;
9842 case DW_TAG_module:
9843 cu->processing_has_namespace_info = true;
9844 read_module (die, cu);
9845 break;
9846 case DW_TAG_imported_declaration:
9847 cu->processing_has_namespace_info = true;
9848 if (read_namespace_alias (die, cu))
9849 break;
9850 /* The declaration is not a global namespace alias. */
9851 /* Fall through. */
9852 case DW_TAG_imported_module:
9853 cu->processing_has_namespace_info = true;
9854 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9855 || cu->language != language_fortran))
9856 complaint (_("Tag '%s' has unexpected children"),
9857 dwarf_tag_name (die->tag));
9858 read_import_statement (die, cu);
9859 break;
9860
9861 case DW_TAG_imported_unit:
9862 process_imported_unit_die (die, cu);
9863 break;
9864
9865 case DW_TAG_variable:
9866 read_variable (die, cu);
9867 break;
9868
9869 default:
9870 new_symbol (die, NULL, cu);
9871 break;
9872 }
9873 }
9874 \f
9875 /* DWARF name computation. */
9876
9877 /* A helper function for dwarf2_compute_name which determines whether DIE
9878 needs to have the name of the scope prepended to the name listed in the
9879 die. */
9880
9881 static int
9882 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9883 {
9884 struct attribute *attr;
9885
9886 switch (die->tag)
9887 {
9888 case DW_TAG_namespace:
9889 case DW_TAG_typedef:
9890 case DW_TAG_class_type:
9891 case DW_TAG_interface_type:
9892 case DW_TAG_structure_type:
9893 case DW_TAG_union_type:
9894 case DW_TAG_enumeration_type:
9895 case DW_TAG_enumerator:
9896 case DW_TAG_subprogram:
9897 case DW_TAG_inlined_subroutine:
9898 case DW_TAG_member:
9899 case DW_TAG_imported_declaration:
9900 return 1;
9901
9902 case DW_TAG_variable:
9903 case DW_TAG_constant:
9904 /* We only need to prefix "globally" visible variables. These include
9905 any variable marked with DW_AT_external or any variable that
9906 lives in a namespace. [Variables in anonymous namespaces
9907 require prefixing, but they are not DW_AT_external.] */
9908
9909 if (dwarf2_attr (die, DW_AT_specification, cu))
9910 {
9911 struct dwarf2_cu *spec_cu = cu;
9912
9913 return die_needs_namespace (die_specification (die, &spec_cu),
9914 spec_cu);
9915 }
9916
9917 attr = dwarf2_attr (die, DW_AT_external, cu);
9918 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9919 && die->parent->tag != DW_TAG_module)
9920 return 0;
9921 /* A variable in a lexical block of some kind does not need a
9922 namespace, even though in C++ such variables may be external
9923 and have a mangled name. */
9924 if (die->parent->tag == DW_TAG_lexical_block
9925 || die->parent->tag == DW_TAG_try_block
9926 || die->parent->tag == DW_TAG_catch_block
9927 || die->parent->tag == DW_TAG_subprogram)
9928 return 0;
9929 return 1;
9930
9931 default:
9932 return 0;
9933 }
9934 }
9935
9936 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9937 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9938 defined for the given DIE. */
9939
9940 static struct attribute *
9941 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9942 {
9943 struct attribute *attr;
9944
9945 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9946 if (attr == NULL)
9947 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9948
9949 return attr;
9950 }
9951
9952 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9953 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9954 defined for the given DIE. */
9955
9956 static const char *
9957 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9958 {
9959 const char *linkage_name;
9960
9961 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9962 if (linkage_name == NULL)
9963 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9964
9965 return linkage_name;
9966 }
9967
9968 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9969 compute the physname for the object, which include a method's:
9970 - formal parameters (C++),
9971 - receiver type (Go),
9972
9973 The term "physname" is a bit confusing.
9974 For C++, for example, it is the demangled name.
9975 For Go, for example, it's the mangled name.
9976
9977 For Ada, return the DIE's linkage name rather than the fully qualified
9978 name. PHYSNAME is ignored..
9979
9980 The result is allocated on the objfile_obstack and canonicalized. */
9981
9982 static const char *
9983 dwarf2_compute_name (const char *name,
9984 struct die_info *die, struct dwarf2_cu *cu,
9985 int physname)
9986 {
9987 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9988
9989 if (name == NULL)
9990 name = dwarf2_name (die, cu);
9991
9992 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9993 but otherwise compute it by typename_concat inside GDB.
9994 FIXME: Actually this is not really true, or at least not always true.
9995 It's all very confusing. compute_and_set_names doesn't try to demangle
9996 Fortran names because there is no mangling standard. So new_symbol
9997 will set the demangled name to the result of dwarf2_full_name, and it is
9998 the demangled name that GDB uses if it exists. */
9999 if (cu->language == language_ada
10000 || (cu->language == language_fortran && physname))
10001 {
10002 /* For Ada unit, we prefer the linkage name over the name, as
10003 the former contains the exported name, which the user expects
10004 to be able to reference. Ideally, we want the user to be able
10005 to reference this entity using either natural or linkage name,
10006 but we haven't started looking at this enhancement yet. */
10007 const char *linkage_name = dw2_linkage_name (die, cu);
10008
10009 if (linkage_name != NULL)
10010 return linkage_name;
10011 }
10012
10013 /* These are the only languages we know how to qualify names in. */
10014 if (name != NULL
10015 && (cu->language == language_cplus
10016 || cu->language == language_fortran || cu->language == language_d
10017 || cu->language == language_rust))
10018 {
10019 if (die_needs_namespace (die, cu))
10020 {
10021 const char *prefix;
10022 const char *canonical_name = NULL;
10023
10024 string_file buf;
10025
10026 prefix = determine_prefix (die, cu);
10027 if (*prefix != '\0')
10028 {
10029 gdb::unique_xmalloc_ptr<char> prefixed_name
10030 (typename_concat (NULL, prefix, name, physname, cu));
10031
10032 buf.puts (prefixed_name.get ());
10033 }
10034 else
10035 buf.puts (name);
10036
10037 /* Template parameters may be specified in the DIE's DW_AT_name, or
10038 as children with DW_TAG_template_type_param or
10039 DW_TAG_value_type_param. If the latter, add them to the name
10040 here. If the name already has template parameters, then
10041 skip this step; some versions of GCC emit both, and
10042 it is more efficient to use the pre-computed name.
10043
10044 Something to keep in mind about this process: it is very
10045 unlikely, or in some cases downright impossible, to produce
10046 something that will match the mangled name of a function.
10047 If the definition of the function has the same debug info,
10048 we should be able to match up with it anyway. But fallbacks
10049 using the minimal symbol, for instance to find a method
10050 implemented in a stripped copy of libstdc++, will not work.
10051 If we do not have debug info for the definition, we will have to
10052 match them up some other way.
10053
10054 When we do name matching there is a related problem with function
10055 templates; two instantiated function templates are allowed to
10056 differ only by their return types, which we do not add here. */
10057
10058 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10059 {
10060 struct attribute *attr;
10061 struct die_info *child;
10062 int first = 1;
10063
10064 die->building_fullname = 1;
10065
10066 for (child = die->child; child != NULL; child = child->sibling)
10067 {
10068 struct type *type;
10069 LONGEST value;
10070 const gdb_byte *bytes;
10071 struct dwarf2_locexpr_baton *baton;
10072 struct value *v;
10073
10074 if (child->tag != DW_TAG_template_type_param
10075 && child->tag != DW_TAG_template_value_param)
10076 continue;
10077
10078 if (first)
10079 {
10080 buf.puts ("<");
10081 first = 0;
10082 }
10083 else
10084 buf.puts (", ");
10085
10086 attr = dwarf2_attr (child, DW_AT_type, cu);
10087 if (attr == NULL)
10088 {
10089 complaint (_("template parameter missing DW_AT_type"));
10090 buf.puts ("UNKNOWN_TYPE");
10091 continue;
10092 }
10093 type = die_type (child, cu);
10094
10095 if (child->tag == DW_TAG_template_type_param)
10096 {
10097 c_print_type (type, "", &buf, -1, 0, cu->language,
10098 &type_print_raw_options);
10099 continue;
10100 }
10101
10102 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10103 if (attr == NULL)
10104 {
10105 complaint (_("template parameter missing "
10106 "DW_AT_const_value"));
10107 buf.puts ("UNKNOWN_VALUE");
10108 continue;
10109 }
10110
10111 dwarf2_const_value_attr (attr, type, name,
10112 &cu->comp_unit_obstack, cu,
10113 &value, &bytes, &baton);
10114
10115 if (TYPE_NOSIGN (type))
10116 /* GDB prints characters as NUMBER 'CHAR'. If that's
10117 changed, this can use value_print instead. */
10118 c_printchar (value, type, &buf);
10119 else
10120 {
10121 struct value_print_options opts;
10122
10123 if (baton != NULL)
10124 v = dwarf2_evaluate_loc_desc (type, NULL,
10125 baton->data,
10126 baton->size,
10127 baton->per_cu);
10128 else if (bytes != NULL)
10129 {
10130 v = allocate_value (type);
10131 memcpy (value_contents_writeable (v), bytes,
10132 TYPE_LENGTH (type));
10133 }
10134 else
10135 v = value_from_longest (type, value);
10136
10137 /* Specify decimal so that we do not depend on
10138 the radix. */
10139 get_formatted_print_options (&opts, 'd');
10140 opts.raw = 1;
10141 value_print (v, &buf, &opts);
10142 release_value (v);
10143 }
10144 }
10145
10146 die->building_fullname = 0;
10147
10148 if (!first)
10149 {
10150 /* Close the argument list, with a space if necessary
10151 (nested templates). */
10152 if (!buf.empty () && buf.string ().back () == '>')
10153 buf.puts (" >");
10154 else
10155 buf.puts (">");
10156 }
10157 }
10158
10159 /* For C++ methods, append formal parameter type
10160 information, if PHYSNAME. */
10161
10162 if (physname && die->tag == DW_TAG_subprogram
10163 && cu->language == language_cplus)
10164 {
10165 struct type *type = read_type_die (die, cu);
10166
10167 c_type_print_args (type, &buf, 1, cu->language,
10168 &type_print_raw_options);
10169
10170 if (cu->language == language_cplus)
10171 {
10172 /* Assume that an artificial first parameter is
10173 "this", but do not crash if it is not. RealView
10174 marks unnamed (and thus unused) parameters as
10175 artificial; there is no way to differentiate
10176 the two cases. */
10177 if (TYPE_NFIELDS (type) > 0
10178 && TYPE_FIELD_ARTIFICIAL (type, 0)
10179 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10180 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10181 0))))
10182 buf.puts (" const");
10183 }
10184 }
10185
10186 const std::string &intermediate_name = buf.string ();
10187
10188 if (cu->language == language_cplus)
10189 canonical_name
10190 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10191 &objfile->per_bfd->storage_obstack);
10192
10193 /* If we only computed INTERMEDIATE_NAME, or if
10194 INTERMEDIATE_NAME is already canonical, then we need to
10195 copy it to the appropriate obstack. */
10196 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10197 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10198 intermediate_name);
10199 else
10200 name = canonical_name;
10201 }
10202 }
10203
10204 return name;
10205 }
10206
10207 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10208 If scope qualifiers are appropriate they will be added. The result
10209 will be allocated on the storage_obstack, or NULL if the DIE does
10210 not have a name. NAME may either be from a previous call to
10211 dwarf2_name or NULL.
10212
10213 The output string will be canonicalized (if C++). */
10214
10215 static const char *
10216 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10217 {
10218 return dwarf2_compute_name (name, die, cu, 0);
10219 }
10220
10221 /* Construct a physname for the given DIE in CU. NAME may either be
10222 from a previous call to dwarf2_name or NULL. The result will be
10223 allocated on the objfile_objstack or NULL if the DIE does not have a
10224 name.
10225
10226 The output string will be canonicalized (if C++). */
10227
10228 static const char *
10229 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10230 {
10231 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10232 const char *retval, *mangled = NULL, *canon = NULL;
10233 int need_copy = 1;
10234
10235 /* In this case dwarf2_compute_name is just a shortcut not building anything
10236 on its own. */
10237 if (!die_needs_namespace (die, cu))
10238 return dwarf2_compute_name (name, die, cu, 1);
10239
10240 mangled = dw2_linkage_name (die, cu);
10241
10242 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10243 See https://github.com/rust-lang/rust/issues/32925. */
10244 if (cu->language == language_rust && mangled != NULL
10245 && strchr (mangled, '{') != NULL)
10246 mangled = NULL;
10247
10248 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10249 has computed. */
10250 gdb::unique_xmalloc_ptr<char> demangled;
10251 if (mangled != NULL)
10252 {
10253
10254 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10255 {
10256 /* Do nothing (do not demangle the symbol name). */
10257 }
10258 else if (cu->language == language_go)
10259 {
10260 /* This is a lie, but we already lie to the caller new_symbol.
10261 new_symbol assumes we return the mangled name.
10262 This just undoes that lie until things are cleaned up. */
10263 }
10264 else
10265 {
10266 /* Use DMGL_RET_DROP for C++ template functions to suppress
10267 their return type. It is easier for GDB users to search
10268 for such functions as `name(params)' than `long name(params)'.
10269 In such case the minimal symbol names do not match the full
10270 symbol names but for template functions there is never a need
10271 to look up their definition from their declaration so
10272 the only disadvantage remains the minimal symbol variant
10273 `long name(params)' does not have the proper inferior type. */
10274 demangled.reset (gdb_demangle (mangled,
10275 (DMGL_PARAMS | DMGL_ANSI
10276 | DMGL_RET_DROP)));
10277 }
10278 if (demangled)
10279 canon = demangled.get ();
10280 else
10281 {
10282 canon = mangled;
10283 need_copy = 0;
10284 }
10285 }
10286
10287 if (canon == NULL || check_physname)
10288 {
10289 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10290
10291 if (canon != NULL && strcmp (physname, canon) != 0)
10292 {
10293 /* It may not mean a bug in GDB. The compiler could also
10294 compute DW_AT_linkage_name incorrectly. But in such case
10295 GDB would need to be bug-to-bug compatible. */
10296
10297 complaint (_("Computed physname <%s> does not match demangled <%s> "
10298 "(from linkage <%s>) - DIE at %s [in module %s]"),
10299 physname, canon, mangled, sect_offset_str (die->sect_off),
10300 objfile_name (objfile));
10301
10302 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10303 is available here - over computed PHYSNAME. It is safer
10304 against both buggy GDB and buggy compilers. */
10305
10306 retval = canon;
10307 }
10308 else
10309 {
10310 retval = physname;
10311 need_copy = 0;
10312 }
10313 }
10314 else
10315 retval = canon;
10316
10317 if (need_copy)
10318 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10319
10320 return retval;
10321 }
10322
10323 /* Inspect DIE in CU for a namespace alias. If one exists, record
10324 a new symbol for it.
10325
10326 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10327
10328 static int
10329 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10330 {
10331 struct attribute *attr;
10332
10333 /* If the die does not have a name, this is not a namespace
10334 alias. */
10335 attr = dwarf2_attr (die, DW_AT_name, cu);
10336 if (attr != NULL)
10337 {
10338 int num;
10339 struct die_info *d = die;
10340 struct dwarf2_cu *imported_cu = cu;
10341
10342 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10343 keep inspecting DIEs until we hit the underlying import. */
10344 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10345 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10346 {
10347 attr = dwarf2_attr (d, DW_AT_import, cu);
10348 if (attr == NULL)
10349 break;
10350
10351 d = follow_die_ref (d, attr, &imported_cu);
10352 if (d->tag != DW_TAG_imported_declaration)
10353 break;
10354 }
10355
10356 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10357 {
10358 complaint (_("DIE at %s has too many recursively imported "
10359 "declarations"), sect_offset_str (d->sect_off));
10360 return 0;
10361 }
10362
10363 if (attr != NULL)
10364 {
10365 struct type *type;
10366 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10367
10368 type = get_die_type_at_offset (sect_off, cu->per_cu);
10369 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10370 {
10371 /* This declaration is a global namespace alias. Add
10372 a symbol for it whose type is the aliased namespace. */
10373 new_symbol (die, type, cu);
10374 return 1;
10375 }
10376 }
10377 }
10378
10379 return 0;
10380 }
10381
10382 /* Return the using directives repository (global or local?) to use in the
10383 current context for CU.
10384
10385 For Ada, imported declarations can materialize renamings, which *may* be
10386 global. However it is impossible (for now?) in DWARF to distinguish
10387 "external" imported declarations and "static" ones. As all imported
10388 declarations seem to be static in all other languages, make them all CU-wide
10389 global only in Ada. */
10390
10391 static struct using_direct **
10392 using_directives (struct dwarf2_cu *cu)
10393 {
10394 if (cu->language == language_ada
10395 && cu->get_builder ()->outermost_context_p ())
10396 return cu->get_builder ()->get_global_using_directives ();
10397 else
10398 return cu->get_builder ()->get_local_using_directives ();
10399 }
10400
10401 /* Read the import statement specified by the given die and record it. */
10402
10403 static void
10404 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10405 {
10406 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10407 struct attribute *import_attr;
10408 struct die_info *imported_die, *child_die;
10409 struct dwarf2_cu *imported_cu;
10410 const char *imported_name;
10411 const char *imported_name_prefix;
10412 const char *canonical_name;
10413 const char *import_alias;
10414 const char *imported_declaration = NULL;
10415 const char *import_prefix;
10416 std::vector<const char *> excludes;
10417
10418 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10419 if (import_attr == NULL)
10420 {
10421 complaint (_("Tag '%s' has no DW_AT_import"),
10422 dwarf_tag_name (die->tag));
10423 return;
10424 }
10425
10426 imported_cu = cu;
10427 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10428 imported_name = dwarf2_name (imported_die, imported_cu);
10429 if (imported_name == NULL)
10430 {
10431 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10432
10433 The import in the following code:
10434 namespace A
10435 {
10436 typedef int B;
10437 }
10438
10439 int main ()
10440 {
10441 using A::B;
10442 B b;
10443 return b;
10444 }
10445
10446 ...
10447 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10448 <52> DW_AT_decl_file : 1
10449 <53> DW_AT_decl_line : 6
10450 <54> DW_AT_import : <0x75>
10451 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10452 <59> DW_AT_name : B
10453 <5b> DW_AT_decl_file : 1
10454 <5c> DW_AT_decl_line : 2
10455 <5d> DW_AT_type : <0x6e>
10456 ...
10457 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10458 <76> DW_AT_byte_size : 4
10459 <77> DW_AT_encoding : 5 (signed)
10460
10461 imports the wrong die ( 0x75 instead of 0x58 ).
10462 This case will be ignored until the gcc bug is fixed. */
10463 return;
10464 }
10465
10466 /* Figure out the local name after import. */
10467 import_alias = dwarf2_name (die, cu);
10468
10469 /* Figure out where the statement is being imported to. */
10470 import_prefix = determine_prefix (die, cu);
10471
10472 /* Figure out what the scope of the imported die is and prepend it
10473 to the name of the imported die. */
10474 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10475
10476 if (imported_die->tag != DW_TAG_namespace
10477 && imported_die->tag != DW_TAG_module)
10478 {
10479 imported_declaration = imported_name;
10480 canonical_name = imported_name_prefix;
10481 }
10482 else if (strlen (imported_name_prefix) > 0)
10483 canonical_name = obconcat (&objfile->objfile_obstack,
10484 imported_name_prefix,
10485 (cu->language == language_d ? "." : "::"),
10486 imported_name, (char *) NULL);
10487 else
10488 canonical_name = imported_name;
10489
10490 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10491 for (child_die = die->child; child_die && child_die->tag;
10492 child_die = sibling_die (child_die))
10493 {
10494 /* DWARF-4: A Fortran use statement with a “rename list” may be
10495 represented by an imported module entry with an import attribute
10496 referring to the module and owned entries corresponding to those
10497 entities that are renamed as part of being imported. */
10498
10499 if (child_die->tag != DW_TAG_imported_declaration)
10500 {
10501 complaint (_("child DW_TAG_imported_declaration expected "
10502 "- DIE at %s [in module %s]"),
10503 sect_offset_str (child_die->sect_off),
10504 objfile_name (objfile));
10505 continue;
10506 }
10507
10508 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10509 if (import_attr == NULL)
10510 {
10511 complaint (_("Tag '%s' has no DW_AT_import"),
10512 dwarf_tag_name (child_die->tag));
10513 continue;
10514 }
10515
10516 imported_cu = cu;
10517 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10518 &imported_cu);
10519 imported_name = dwarf2_name (imported_die, imported_cu);
10520 if (imported_name == NULL)
10521 {
10522 complaint (_("child DW_TAG_imported_declaration has unknown "
10523 "imported name - DIE at %s [in module %s]"),
10524 sect_offset_str (child_die->sect_off),
10525 objfile_name (objfile));
10526 continue;
10527 }
10528
10529 excludes.push_back (imported_name);
10530
10531 process_die (child_die, cu);
10532 }
10533
10534 add_using_directive (using_directives (cu),
10535 import_prefix,
10536 canonical_name,
10537 import_alias,
10538 imported_declaration,
10539 excludes,
10540 0,
10541 &objfile->objfile_obstack);
10542 }
10543
10544 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10545 types, but gives them a size of zero. Starting with version 14,
10546 ICC is compatible with GCC. */
10547
10548 static bool
10549 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10550 {
10551 if (!cu->checked_producer)
10552 check_producer (cu);
10553
10554 return cu->producer_is_icc_lt_14;
10555 }
10556
10557 /* ICC generates a DW_AT_type for C void functions. This was observed on
10558 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10559 which says that void functions should not have a DW_AT_type. */
10560
10561 static bool
10562 producer_is_icc (struct dwarf2_cu *cu)
10563 {
10564 if (!cu->checked_producer)
10565 check_producer (cu);
10566
10567 return cu->producer_is_icc;
10568 }
10569
10570 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10571 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10572 this, it was first present in GCC release 4.3.0. */
10573
10574 static bool
10575 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10576 {
10577 if (!cu->checked_producer)
10578 check_producer (cu);
10579
10580 return cu->producer_is_gcc_lt_4_3;
10581 }
10582
10583 static file_and_directory
10584 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10585 {
10586 file_and_directory res;
10587
10588 /* Find the filename. Do not use dwarf2_name here, since the filename
10589 is not a source language identifier. */
10590 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10591 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10592
10593 if (res.comp_dir == NULL
10594 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10595 && IS_ABSOLUTE_PATH (res.name))
10596 {
10597 res.comp_dir_storage = ldirname (res.name);
10598 if (!res.comp_dir_storage.empty ())
10599 res.comp_dir = res.comp_dir_storage.c_str ();
10600 }
10601 if (res.comp_dir != NULL)
10602 {
10603 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10604 directory, get rid of it. */
10605 const char *cp = strchr (res.comp_dir, ':');
10606
10607 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10608 res.comp_dir = cp + 1;
10609 }
10610
10611 if (res.name == NULL)
10612 res.name = "<unknown>";
10613
10614 return res;
10615 }
10616
10617 /* Handle DW_AT_stmt_list for a compilation unit.
10618 DIE is the DW_TAG_compile_unit die for CU.
10619 COMP_DIR is the compilation directory. LOWPC is passed to
10620 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10621
10622 static void
10623 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10624 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10625 {
10626 struct dwarf2_per_objfile *dwarf2_per_objfile
10627 = cu->per_cu->dwarf2_per_objfile;
10628 struct attribute *attr;
10629 struct line_header line_header_local;
10630 hashval_t line_header_local_hash;
10631 void **slot;
10632 int decode_mapping;
10633
10634 gdb_assert (! cu->per_cu->is_debug_types);
10635
10636 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10637 if (attr == NULL)
10638 return;
10639
10640 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10641
10642 /* The line header hash table is only created if needed (it exists to
10643 prevent redundant reading of the line table for partial_units).
10644 If we're given a partial_unit, we'll need it. If we're given a
10645 compile_unit, then use the line header hash table if it's already
10646 created, but don't create one just yet. */
10647
10648 if (dwarf2_per_objfile->line_header_hash == NULL
10649 && die->tag == DW_TAG_partial_unit)
10650 {
10651 dwarf2_per_objfile->line_header_hash
10652 .reset (htab_create_alloc (127, line_header_hash_voidp,
10653 line_header_eq_voidp,
10654 free_line_header_voidp,
10655 xcalloc, xfree));
10656 }
10657
10658 line_header_local.sect_off = line_offset;
10659 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10660 line_header_local_hash = line_header_hash (&line_header_local);
10661 if (dwarf2_per_objfile->line_header_hash != NULL)
10662 {
10663 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10664 &line_header_local,
10665 line_header_local_hash, NO_INSERT);
10666
10667 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10668 is not present in *SLOT (since if there is something in *SLOT then
10669 it will be for a partial_unit). */
10670 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10671 {
10672 gdb_assert (*slot != NULL);
10673 cu->line_header = (struct line_header *) *slot;
10674 return;
10675 }
10676 }
10677
10678 /* dwarf_decode_line_header does not yet provide sufficient information.
10679 We always have to call also dwarf_decode_lines for it. */
10680 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10681 if (lh == NULL)
10682 return;
10683
10684 cu->line_header = lh.release ();
10685 cu->line_header_die_owner = die;
10686
10687 if (dwarf2_per_objfile->line_header_hash == NULL)
10688 slot = NULL;
10689 else
10690 {
10691 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10692 &line_header_local,
10693 line_header_local_hash, INSERT);
10694 gdb_assert (slot != NULL);
10695 }
10696 if (slot != NULL && *slot == NULL)
10697 {
10698 /* This newly decoded line number information unit will be owned
10699 by line_header_hash hash table. */
10700 *slot = cu->line_header;
10701 cu->line_header_die_owner = NULL;
10702 }
10703 else
10704 {
10705 /* We cannot free any current entry in (*slot) as that struct line_header
10706 may be already used by multiple CUs. Create only temporary decoded
10707 line_header for this CU - it may happen at most once for each line
10708 number information unit. And if we're not using line_header_hash
10709 then this is what we want as well. */
10710 gdb_assert (die->tag != DW_TAG_partial_unit);
10711 }
10712 decode_mapping = (die->tag != DW_TAG_partial_unit);
10713 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10714 decode_mapping);
10715
10716 }
10717
10718 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10719
10720 static void
10721 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10722 {
10723 struct dwarf2_per_objfile *dwarf2_per_objfile
10724 = cu->per_cu->dwarf2_per_objfile;
10725 struct objfile *objfile = dwarf2_per_objfile->objfile;
10726 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10727 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10728 CORE_ADDR highpc = ((CORE_ADDR) 0);
10729 struct attribute *attr;
10730 struct die_info *child_die;
10731 CORE_ADDR baseaddr;
10732
10733 prepare_one_comp_unit (cu, die, cu->language);
10734 baseaddr = objfile->text_section_offset ();
10735
10736 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10737
10738 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10739 from finish_block. */
10740 if (lowpc == ((CORE_ADDR) -1))
10741 lowpc = highpc;
10742 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10743
10744 file_and_directory fnd = find_file_and_directory (die, cu);
10745
10746 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10747 standardised yet. As a workaround for the language detection we fall
10748 back to the DW_AT_producer string. */
10749 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10750 cu->language = language_opencl;
10751
10752 /* Similar hack for Go. */
10753 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10754 set_cu_language (DW_LANG_Go, cu);
10755
10756 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10757
10758 /* Decode line number information if present. We do this before
10759 processing child DIEs, so that the line header table is available
10760 for DW_AT_decl_file. */
10761 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10762
10763 /* Process all dies in compilation unit. */
10764 if (die->child != NULL)
10765 {
10766 child_die = die->child;
10767 while (child_die && child_die->tag)
10768 {
10769 process_die (child_die, cu);
10770 child_die = sibling_die (child_die);
10771 }
10772 }
10773
10774 /* Decode macro information, if present. Dwarf 2 macro information
10775 refers to information in the line number info statement program
10776 header, so we can only read it if we've read the header
10777 successfully. */
10778 attr = dwarf2_attr (die, DW_AT_macros, cu);
10779 if (attr == NULL)
10780 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10781 if (attr && cu->line_header)
10782 {
10783 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10784 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10785
10786 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10787 }
10788 else
10789 {
10790 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10791 if (attr && cu->line_header)
10792 {
10793 unsigned int macro_offset = DW_UNSND (attr);
10794
10795 dwarf_decode_macros (cu, macro_offset, 0);
10796 }
10797 }
10798 }
10799
10800 void
10801 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10802 {
10803 struct type_unit_group *tu_group;
10804 int first_time;
10805 struct attribute *attr;
10806 unsigned int i;
10807 struct signatured_type *sig_type;
10808
10809 gdb_assert (per_cu->is_debug_types);
10810 sig_type = (struct signatured_type *) per_cu;
10811
10812 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10813
10814 /* If we're using .gdb_index (includes -readnow) then
10815 per_cu->type_unit_group may not have been set up yet. */
10816 if (sig_type->type_unit_group == NULL)
10817 sig_type->type_unit_group = get_type_unit_group (this, attr);
10818 tu_group = sig_type->type_unit_group;
10819
10820 /* If we've already processed this stmt_list there's no real need to
10821 do it again, we could fake it and just recreate the part we need
10822 (file name,index -> symtab mapping). If data shows this optimization
10823 is useful we can do it then. */
10824 first_time = tu_group->compunit_symtab == NULL;
10825
10826 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10827 debug info. */
10828 line_header_up lh;
10829 if (attr != NULL)
10830 {
10831 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10832 lh = dwarf_decode_line_header (line_offset, this);
10833 }
10834 if (lh == NULL)
10835 {
10836 if (first_time)
10837 start_symtab ("", NULL, 0);
10838 else
10839 {
10840 gdb_assert (tu_group->symtabs == NULL);
10841 gdb_assert (m_builder == nullptr);
10842 struct compunit_symtab *cust = tu_group->compunit_symtab;
10843 m_builder.reset (new struct buildsym_compunit
10844 (COMPUNIT_OBJFILE (cust), "",
10845 COMPUNIT_DIRNAME (cust),
10846 compunit_language (cust),
10847 0, cust));
10848 }
10849 return;
10850 }
10851
10852 line_header = lh.release ();
10853 line_header_die_owner = die;
10854
10855 if (first_time)
10856 {
10857 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10858
10859 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10860 still initializing it, and our caller (a few levels up)
10861 process_full_type_unit still needs to know if this is the first
10862 time. */
10863
10864 tu_group->num_symtabs = line_header->file_names_size ();
10865 tu_group->symtabs = XNEWVEC (struct symtab *,
10866 line_header->file_names_size ());
10867
10868 auto &file_names = line_header->file_names ();
10869 for (i = 0; i < file_names.size (); ++i)
10870 {
10871 file_entry &fe = file_names[i];
10872 dwarf2_start_subfile (this, fe.name,
10873 fe.include_dir (line_header));
10874 buildsym_compunit *b = get_builder ();
10875 if (b->get_current_subfile ()->symtab == NULL)
10876 {
10877 /* NOTE: start_subfile will recognize when it's been
10878 passed a file it has already seen. So we can't
10879 assume there's a simple mapping from
10880 cu->line_header->file_names to subfiles, plus
10881 cu->line_header->file_names may contain dups. */
10882 b->get_current_subfile ()->symtab
10883 = allocate_symtab (cust, b->get_current_subfile ()->name);
10884 }
10885
10886 fe.symtab = b->get_current_subfile ()->symtab;
10887 tu_group->symtabs[i] = fe.symtab;
10888 }
10889 }
10890 else
10891 {
10892 gdb_assert (m_builder == nullptr);
10893 struct compunit_symtab *cust = tu_group->compunit_symtab;
10894 m_builder.reset (new struct buildsym_compunit
10895 (COMPUNIT_OBJFILE (cust), "",
10896 COMPUNIT_DIRNAME (cust),
10897 compunit_language (cust),
10898 0, cust));
10899
10900 auto &file_names = line_header->file_names ();
10901 for (i = 0; i < file_names.size (); ++i)
10902 {
10903 file_entry &fe = file_names[i];
10904 fe.symtab = tu_group->symtabs[i];
10905 }
10906 }
10907
10908 /* The main symtab is allocated last. Type units don't have DW_AT_name
10909 so they don't have a "real" (so to speak) symtab anyway.
10910 There is later code that will assign the main symtab to all symbols
10911 that don't have one. We need to handle the case of a symbol with a
10912 missing symtab (DW_AT_decl_file) anyway. */
10913 }
10914
10915 /* Process DW_TAG_type_unit.
10916 For TUs we want to skip the first top level sibling if it's not the
10917 actual type being defined by this TU. In this case the first top
10918 level sibling is there to provide context only. */
10919
10920 static void
10921 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10922 {
10923 struct die_info *child_die;
10924
10925 prepare_one_comp_unit (cu, die, language_minimal);
10926
10927 /* Initialize (or reinitialize) the machinery for building symtabs.
10928 We do this before processing child DIEs, so that the line header table
10929 is available for DW_AT_decl_file. */
10930 cu->setup_type_unit_groups (die);
10931
10932 if (die->child != NULL)
10933 {
10934 child_die = die->child;
10935 while (child_die && child_die->tag)
10936 {
10937 process_die (child_die, cu);
10938 child_die = sibling_die (child_die);
10939 }
10940 }
10941 }
10942 \f
10943 /* DWO/DWP files.
10944
10945 http://gcc.gnu.org/wiki/DebugFission
10946 http://gcc.gnu.org/wiki/DebugFissionDWP
10947
10948 To simplify handling of both DWO files ("object" files with the DWARF info)
10949 and DWP files (a file with the DWOs packaged up into one file), we treat
10950 DWP files as having a collection of virtual DWO files. */
10951
10952 static hashval_t
10953 hash_dwo_file (const void *item)
10954 {
10955 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10956 hashval_t hash;
10957
10958 hash = htab_hash_string (dwo_file->dwo_name);
10959 if (dwo_file->comp_dir != NULL)
10960 hash += htab_hash_string (dwo_file->comp_dir);
10961 return hash;
10962 }
10963
10964 static int
10965 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10966 {
10967 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10968 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10969
10970 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10971 return 0;
10972 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10973 return lhs->comp_dir == rhs->comp_dir;
10974 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10975 }
10976
10977 /* Allocate a hash table for DWO files. */
10978
10979 static htab_up
10980 allocate_dwo_file_hash_table (struct objfile *objfile)
10981 {
10982 auto delete_dwo_file = [] (void *item)
10983 {
10984 struct dwo_file *dwo_file = (struct dwo_file *) item;
10985
10986 delete dwo_file;
10987 };
10988
10989 return htab_up (htab_create_alloc (41,
10990 hash_dwo_file,
10991 eq_dwo_file,
10992 delete_dwo_file,
10993 xcalloc, xfree));
10994 }
10995
10996 /* Lookup DWO file DWO_NAME. */
10997
10998 static void **
10999 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11000 const char *dwo_name,
11001 const char *comp_dir)
11002 {
11003 struct dwo_file find_entry;
11004 void **slot;
11005
11006 if (dwarf2_per_objfile->dwo_files == NULL)
11007 dwarf2_per_objfile->dwo_files
11008 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11009
11010 find_entry.dwo_name = dwo_name;
11011 find_entry.comp_dir = comp_dir;
11012 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11013 INSERT);
11014
11015 return slot;
11016 }
11017
11018 static hashval_t
11019 hash_dwo_unit (const void *item)
11020 {
11021 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11022
11023 /* This drops the top 32 bits of the id, but is ok for a hash. */
11024 return dwo_unit->signature;
11025 }
11026
11027 static int
11028 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11029 {
11030 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11031 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11032
11033 /* The signature is assumed to be unique within the DWO file.
11034 So while object file CU dwo_id's always have the value zero,
11035 that's OK, assuming each object file DWO file has only one CU,
11036 and that's the rule for now. */
11037 return lhs->signature == rhs->signature;
11038 }
11039
11040 /* Allocate a hash table for DWO CUs,TUs.
11041 There is one of these tables for each of CUs,TUs for each DWO file. */
11042
11043 static htab_up
11044 allocate_dwo_unit_table (struct objfile *objfile)
11045 {
11046 /* Start out with a pretty small number.
11047 Generally DWO files contain only one CU and maybe some TUs. */
11048 return htab_up (htab_create_alloc (3,
11049 hash_dwo_unit,
11050 eq_dwo_unit,
11051 NULL, xcalloc, xfree));
11052 }
11053
11054 /* die_reader_func for create_dwo_cu. */
11055
11056 static void
11057 create_dwo_cu_reader (const struct die_reader_specs *reader,
11058 const gdb_byte *info_ptr,
11059 struct die_info *comp_unit_die,
11060 struct dwo_file *dwo_file,
11061 struct dwo_unit *dwo_unit)
11062 {
11063 struct dwarf2_cu *cu = reader->cu;
11064 sect_offset sect_off = cu->per_cu->sect_off;
11065 struct dwarf2_section_info *section = cu->per_cu->section;
11066
11067 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11068 if (!signature.has_value ())
11069 {
11070 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11071 " its dwo_id [in module %s]"),
11072 sect_offset_str (sect_off), dwo_file->dwo_name);
11073 return;
11074 }
11075
11076 dwo_unit->dwo_file = dwo_file;
11077 dwo_unit->signature = *signature;
11078 dwo_unit->section = section;
11079 dwo_unit->sect_off = sect_off;
11080 dwo_unit->length = cu->per_cu->length;
11081
11082 if (dwarf_read_debug)
11083 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11084 sect_offset_str (sect_off),
11085 hex_string (dwo_unit->signature));
11086 }
11087
11088 /* Create the dwo_units for the CUs in a DWO_FILE.
11089 Note: This function processes DWO files only, not DWP files. */
11090
11091 static void
11092 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11093 dwarf2_cu *cu, struct dwo_file &dwo_file,
11094 dwarf2_section_info &section, htab_up &cus_htab)
11095 {
11096 struct objfile *objfile = dwarf2_per_objfile->objfile;
11097 const gdb_byte *info_ptr, *end_ptr;
11098
11099 section.read (objfile);
11100 info_ptr = section.buffer;
11101
11102 if (info_ptr == NULL)
11103 return;
11104
11105 if (dwarf_read_debug)
11106 {
11107 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11108 section.get_name (),
11109 section.get_file_name ());
11110 }
11111
11112 end_ptr = info_ptr + section.size;
11113 while (info_ptr < end_ptr)
11114 {
11115 struct dwarf2_per_cu_data per_cu;
11116 struct dwo_unit read_unit {};
11117 struct dwo_unit *dwo_unit;
11118 void **slot;
11119 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11120
11121 memset (&per_cu, 0, sizeof (per_cu));
11122 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11123 per_cu.is_debug_types = 0;
11124 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11125 per_cu.section = &section;
11126
11127 cutu_reader reader (&per_cu, cu, &dwo_file);
11128 if (!reader.dummy_p)
11129 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11130 &dwo_file, &read_unit);
11131 info_ptr += per_cu.length;
11132
11133 // If the unit could not be parsed, skip it.
11134 if (read_unit.dwo_file == NULL)
11135 continue;
11136
11137 if (cus_htab == NULL)
11138 cus_htab = allocate_dwo_unit_table (objfile);
11139
11140 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11141 *dwo_unit = read_unit;
11142 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11143 gdb_assert (slot != NULL);
11144 if (*slot != NULL)
11145 {
11146 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11147 sect_offset dup_sect_off = dup_cu->sect_off;
11148
11149 complaint (_("debug cu entry at offset %s is duplicate to"
11150 " the entry at offset %s, signature %s"),
11151 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11152 hex_string (dwo_unit->signature));
11153 }
11154 *slot = (void *)dwo_unit;
11155 }
11156 }
11157
11158 /* DWP file .debug_{cu,tu}_index section format:
11159 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11160
11161 DWP Version 1:
11162
11163 Both index sections have the same format, and serve to map a 64-bit
11164 signature to a set of section numbers. Each section begins with a header,
11165 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11166 indexes, and a pool of 32-bit section numbers. The index sections will be
11167 aligned at 8-byte boundaries in the file.
11168
11169 The index section header consists of:
11170
11171 V, 32 bit version number
11172 -, 32 bits unused
11173 N, 32 bit number of compilation units or type units in the index
11174 M, 32 bit number of slots in the hash table
11175
11176 Numbers are recorded using the byte order of the application binary.
11177
11178 The hash table begins at offset 16 in the section, and consists of an array
11179 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11180 order of the application binary). Unused slots in the hash table are 0.
11181 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11182
11183 The parallel table begins immediately after the hash table
11184 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11185 array of 32-bit indexes (using the byte order of the application binary),
11186 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11187 table contains a 32-bit index into the pool of section numbers. For unused
11188 hash table slots, the corresponding entry in the parallel table will be 0.
11189
11190 The pool of section numbers begins immediately following the hash table
11191 (at offset 16 + 12 * M from the beginning of the section). The pool of
11192 section numbers consists of an array of 32-bit words (using the byte order
11193 of the application binary). Each item in the array is indexed starting
11194 from 0. The hash table entry provides the index of the first section
11195 number in the set. Additional section numbers in the set follow, and the
11196 set is terminated by a 0 entry (section number 0 is not used in ELF).
11197
11198 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11199 section must be the first entry in the set, and the .debug_abbrev.dwo must
11200 be the second entry. Other members of the set may follow in any order.
11201
11202 ---
11203
11204 DWP Version 2:
11205
11206 DWP Version 2 combines all the .debug_info, etc. sections into one,
11207 and the entries in the index tables are now offsets into these sections.
11208 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11209 section.
11210
11211 Index Section Contents:
11212 Header
11213 Hash Table of Signatures dwp_hash_table.hash_table
11214 Parallel Table of Indices dwp_hash_table.unit_table
11215 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11216 Table of Section Sizes dwp_hash_table.v2.sizes
11217
11218 The index section header consists of:
11219
11220 V, 32 bit version number
11221 L, 32 bit number of columns in the table of section offsets
11222 N, 32 bit number of compilation units or type units in the index
11223 M, 32 bit number of slots in the hash table
11224
11225 Numbers are recorded using the byte order of the application binary.
11226
11227 The hash table has the same format as version 1.
11228 The parallel table of indices has the same format as version 1,
11229 except that the entries are origin-1 indices into the table of sections
11230 offsets and the table of section sizes.
11231
11232 The table of offsets begins immediately following the parallel table
11233 (at offset 16 + 12 * M from the beginning of the section). The table is
11234 a two-dimensional array of 32-bit words (using the byte order of the
11235 application binary), with L columns and N+1 rows, in row-major order.
11236 Each row in the array is indexed starting from 0. The first row provides
11237 a key to the remaining rows: each column in this row provides an identifier
11238 for a debug section, and the offsets in the same column of subsequent rows
11239 refer to that section. The section identifiers are:
11240
11241 DW_SECT_INFO 1 .debug_info.dwo
11242 DW_SECT_TYPES 2 .debug_types.dwo
11243 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11244 DW_SECT_LINE 4 .debug_line.dwo
11245 DW_SECT_LOC 5 .debug_loc.dwo
11246 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11247 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11248 DW_SECT_MACRO 8 .debug_macro.dwo
11249
11250 The offsets provided by the CU and TU index sections are the base offsets
11251 for the contributions made by each CU or TU to the corresponding section
11252 in the package file. Each CU and TU header contains an abbrev_offset
11253 field, used to find the abbreviations table for that CU or TU within the
11254 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11255 be interpreted as relative to the base offset given in the index section.
11256 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11257 should be interpreted as relative to the base offset for .debug_line.dwo,
11258 and offsets into other debug sections obtained from DWARF attributes should
11259 also be interpreted as relative to the corresponding base offset.
11260
11261 The table of sizes begins immediately following the table of offsets.
11262 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11263 with L columns and N rows, in row-major order. Each row in the array is
11264 indexed starting from 1 (row 0 is shared by the two tables).
11265
11266 ---
11267
11268 Hash table lookup is handled the same in version 1 and 2:
11269
11270 We assume that N and M will not exceed 2^32 - 1.
11271 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11272
11273 Given a 64-bit compilation unit signature or a type signature S, an entry
11274 in the hash table is located as follows:
11275
11276 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11277 the low-order k bits all set to 1.
11278
11279 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11280
11281 3) If the hash table entry at index H matches the signature, use that
11282 entry. If the hash table entry at index H is unused (all zeroes),
11283 terminate the search: the signature is not present in the table.
11284
11285 4) Let H = (H + H') modulo M. Repeat at Step 3.
11286
11287 Because M > N and H' and M are relatively prime, the search is guaranteed
11288 to stop at an unused slot or find the match. */
11289
11290 /* Create a hash table to map DWO IDs to their CU/TU entry in
11291 .debug_{info,types}.dwo in DWP_FILE.
11292 Returns NULL if there isn't one.
11293 Note: This function processes DWP files only, not DWO files. */
11294
11295 static struct dwp_hash_table *
11296 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11297 struct dwp_file *dwp_file, int is_debug_types)
11298 {
11299 struct objfile *objfile = dwarf2_per_objfile->objfile;
11300 bfd *dbfd = dwp_file->dbfd.get ();
11301 const gdb_byte *index_ptr, *index_end;
11302 struct dwarf2_section_info *index;
11303 uint32_t version, nr_columns, nr_units, nr_slots;
11304 struct dwp_hash_table *htab;
11305
11306 if (is_debug_types)
11307 index = &dwp_file->sections.tu_index;
11308 else
11309 index = &dwp_file->sections.cu_index;
11310
11311 if (index->empty ())
11312 return NULL;
11313 index->read (objfile);
11314
11315 index_ptr = index->buffer;
11316 index_end = index_ptr + index->size;
11317
11318 version = read_4_bytes (dbfd, index_ptr);
11319 index_ptr += 4;
11320 if (version == 2)
11321 nr_columns = read_4_bytes (dbfd, index_ptr);
11322 else
11323 nr_columns = 0;
11324 index_ptr += 4;
11325 nr_units = read_4_bytes (dbfd, index_ptr);
11326 index_ptr += 4;
11327 nr_slots = read_4_bytes (dbfd, index_ptr);
11328 index_ptr += 4;
11329
11330 if (version != 1 && version != 2)
11331 {
11332 error (_("Dwarf Error: unsupported DWP file version (%s)"
11333 " [in module %s]"),
11334 pulongest (version), dwp_file->name);
11335 }
11336 if (nr_slots != (nr_slots & -nr_slots))
11337 {
11338 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11339 " is not power of 2 [in module %s]"),
11340 pulongest (nr_slots), dwp_file->name);
11341 }
11342
11343 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11344 htab->version = version;
11345 htab->nr_columns = nr_columns;
11346 htab->nr_units = nr_units;
11347 htab->nr_slots = nr_slots;
11348 htab->hash_table = index_ptr;
11349 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11350
11351 /* Exit early if the table is empty. */
11352 if (nr_slots == 0 || nr_units == 0
11353 || (version == 2 && nr_columns == 0))
11354 {
11355 /* All must be zero. */
11356 if (nr_slots != 0 || nr_units != 0
11357 || (version == 2 && nr_columns != 0))
11358 {
11359 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11360 " all zero [in modules %s]"),
11361 dwp_file->name);
11362 }
11363 return htab;
11364 }
11365
11366 if (version == 1)
11367 {
11368 htab->section_pool.v1.indices =
11369 htab->unit_table + sizeof (uint32_t) * nr_slots;
11370 /* It's harder to decide whether the section is too small in v1.
11371 V1 is deprecated anyway so we punt. */
11372 }
11373 else
11374 {
11375 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11376 int *ids = htab->section_pool.v2.section_ids;
11377 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11378 /* Reverse map for error checking. */
11379 int ids_seen[DW_SECT_MAX + 1];
11380 int i;
11381
11382 if (nr_columns < 2)
11383 {
11384 error (_("Dwarf Error: bad DWP hash table, too few columns"
11385 " in section table [in module %s]"),
11386 dwp_file->name);
11387 }
11388 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11389 {
11390 error (_("Dwarf Error: bad DWP hash table, too many columns"
11391 " in section table [in module %s]"),
11392 dwp_file->name);
11393 }
11394 memset (ids, 255, sizeof_ids);
11395 memset (ids_seen, 255, sizeof (ids_seen));
11396 for (i = 0; i < nr_columns; ++i)
11397 {
11398 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11399
11400 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11401 {
11402 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11403 " in section table [in module %s]"),
11404 id, dwp_file->name);
11405 }
11406 if (ids_seen[id] != -1)
11407 {
11408 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11409 " id %d in section table [in module %s]"),
11410 id, dwp_file->name);
11411 }
11412 ids_seen[id] = i;
11413 ids[i] = id;
11414 }
11415 /* Must have exactly one info or types section. */
11416 if (((ids_seen[DW_SECT_INFO] != -1)
11417 + (ids_seen[DW_SECT_TYPES] != -1))
11418 != 1)
11419 {
11420 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11421 " DWO info/types section [in module %s]"),
11422 dwp_file->name);
11423 }
11424 /* Must have an abbrev section. */
11425 if (ids_seen[DW_SECT_ABBREV] == -1)
11426 {
11427 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11428 " section [in module %s]"),
11429 dwp_file->name);
11430 }
11431 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11432 htab->section_pool.v2.sizes =
11433 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11434 * nr_units * nr_columns);
11435 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11436 * nr_units * nr_columns))
11437 > index_end)
11438 {
11439 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11440 " [in module %s]"),
11441 dwp_file->name);
11442 }
11443 }
11444
11445 return htab;
11446 }
11447
11448 /* Update SECTIONS with the data from SECTP.
11449
11450 This function is like the other "locate" section routines that are
11451 passed to bfd_map_over_sections, but in this context the sections to
11452 read comes from the DWP V1 hash table, not the full ELF section table.
11453
11454 The result is non-zero for success, or zero if an error was found. */
11455
11456 static int
11457 locate_v1_virtual_dwo_sections (asection *sectp,
11458 struct virtual_v1_dwo_sections *sections)
11459 {
11460 const struct dwop_section_names *names = &dwop_section_names;
11461
11462 if (section_is_p (sectp->name, &names->abbrev_dwo))
11463 {
11464 /* There can be only one. */
11465 if (sections->abbrev.s.section != NULL)
11466 return 0;
11467 sections->abbrev.s.section = sectp;
11468 sections->abbrev.size = bfd_section_size (sectp);
11469 }
11470 else if (section_is_p (sectp->name, &names->info_dwo)
11471 || section_is_p (sectp->name, &names->types_dwo))
11472 {
11473 /* There can be only one. */
11474 if (sections->info_or_types.s.section != NULL)
11475 return 0;
11476 sections->info_or_types.s.section = sectp;
11477 sections->info_or_types.size = bfd_section_size (sectp);
11478 }
11479 else if (section_is_p (sectp->name, &names->line_dwo))
11480 {
11481 /* There can be only one. */
11482 if (sections->line.s.section != NULL)
11483 return 0;
11484 sections->line.s.section = sectp;
11485 sections->line.size = bfd_section_size (sectp);
11486 }
11487 else if (section_is_p (sectp->name, &names->loc_dwo))
11488 {
11489 /* There can be only one. */
11490 if (sections->loc.s.section != NULL)
11491 return 0;
11492 sections->loc.s.section = sectp;
11493 sections->loc.size = bfd_section_size (sectp);
11494 }
11495 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11496 {
11497 /* There can be only one. */
11498 if (sections->macinfo.s.section != NULL)
11499 return 0;
11500 sections->macinfo.s.section = sectp;
11501 sections->macinfo.size = bfd_section_size (sectp);
11502 }
11503 else if (section_is_p (sectp->name, &names->macro_dwo))
11504 {
11505 /* There can be only one. */
11506 if (sections->macro.s.section != NULL)
11507 return 0;
11508 sections->macro.s.section = sectp;
11509 sections->macro.size = bfd_section_size (sectp);
11510 }
11511 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11512 {
11513 /* There can be only one. */
11514 if (sections->str_offsets.s.section != NULL)
11515 return 0;
11516 sections->str_offsets.s.section = sectp;
11517 sections->str_offsets.size = bfd_section_size (sectp);
11518 }
11519 else
11520 {
11521 /* No other kind of section is valid. */
11522 return 0;
11523 }
11524
11525 return 1;
11526 }
11527
11528 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11529 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11530 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11531 This is for DWP version 1 files. */
11532
11533 static struct dwo_unit *
11534 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11535 struct dwp_file *dwp_file,
11536 uint32_t unit_index,
11537 const char *comp_dir,
11538 ULONGEST signature, int is_debug_types)
11539 {
11540 struct objfile *objfile = dwarf2_per_objfile->objfile;
11541 const struct dwp_hash_table *dwp_htab =
11542 is_debug_types ? dwp_file->tus : dwp_file->cus;
11543 bfd *dbfd = dwp_file->dbfd.get ();
11544 const char *kind = is_debug_types ? "TU" : "CU";
11545 struct dwo_file *dwo_file;
11546 struct dwo_unit *dwo_unit;
11547 struct virtual_v1_dwo_sections sections;
11548 void **dwo_file_slot;
11549 int i;
11550
11551 gdb_assert (dwp_file->version == 1);
11552
11553 if (dwarf_read_debug)
11554 {
11555 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11556 kind,
11557 pulongest (unit_index), hex_string (signature),
11558 dwp_file->name);
11559 }
11560
11561 /* Fetch the sections of this DWO unit.
11562 Put a limit on the number of sections we look for so that bad data
11563 doesn't cause us to loop forever. */
11564
11565 #define MAX_NR_V1_DWO_SECTIONS \
11566 (1 /* .debug_info or .debug_types */ \
11567 + 1 /* .debug_abbrev */ \
11568 + 1 /* .debug_line */ \
11569 + 1 /* .debug_loc */ \
11570 + 1 /* .debug_str_offsets */ \
11571 + 1 /* .debug_macro or .debug_macinfo */ \
11572 + 1 /* trailing zero */)
11573
11574 memset (&sections, 0, sizeof (sections));
11575
11576 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11577 {
11578 asection *sectp;
11579 uint32_t section_nr =
11580 read_4_bytes (dbfd,
11581 dwp_htab->section_pool.v1.indices
11582 + (unit_index + i) * sizeof (uint32_t));
11583
11584 if (section_nr == 0)
11585 break;
11586 if (section_nr >= dwp_file->num_sections)
11587 {
11588 error (_("Dwarf Error: bad DWP hash table, section number too large"
11589 " [in module %s]"),
11590 dwp_file->name);
11591 }
11592
11593 sectp = dwp_file->elf_sections[section_nr];
11594 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11595 {
11596 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11597 " [in module %s]"),
11598 dwp_file->name);
11599 }
11600 }
11601
11602 if (i < 2
11603 || sections.info_or_types.empty ()
11604 || sections.abbrev.empty ())
11605 {
11606 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11607 " [in module %s]"),
11608 dwp_file->name);
11609 }
11610 if (i == MAX_NR_V1_DWO_SECTIONS)
11611 {
11612 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11613 " [in module %s]"),
11614 dwp_file->name);
11615 }
11616
11617 /* It's easier for the rest of the code if we fake a struct dwo_file and
11618 have dwo_unit "live" in that. At least for now.
11619
11620 The DWP file can be made up of a random collection of CUs and TUs.
11621 However, for each CU + set of TUs that came from the same original DWO
11622 file, we can combine them back into a virtual DWO file to save space
11623 (fewer struct dwo_file objects to allocate). Remember that for really
11624 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11625
11626 std::string virtual_dwo_name =
11627 string_printf ("virtual-dwo/%d-%d-%d-%d",
11628 sections.abbrev.get_id (),
11629 sections.line.get_id (),
11630 sections.loc.get_id (),
11631 sections.str_offsets.get_id ());
11632 /* Can we use an existing virtual DWO file? */
11633 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11634 virtual_dwo_name.c_str (),
11635 comp_dir);
11636 /* Create one if necessary. */
11637 if (*dwo_file_slot == NULL)
11638 {
11639 if (dwarf_read_debug)
11640 {
11641 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11642 virtual_dwo_name.c_str ());
11643 }
11644 dwo_file = new struct dwo_file;
11645 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11646 virtual_dwo_name);
11647 dwo_file->comp_dir = comp_dir;
11648 dwo_file->sections.abbrev = sections.abbrev;
11649 dwo_file->sections.line = sections.line;
11650 dwo_file->sections.loc = sections.loc;
11651 dwo_file->sections.macinfo = sections.macinfo;
11652 dwo_file->sections.macro = sections.macro;
11653 dwo_file->sections.str_offsets = sections.str_offsets;
11654 /* The "str" section is global to the entire DWP file. */
11655 dwo_file->sections.str = dwp_file->sections.str;
11656 /* The info or types section is assigned below to dwo_unit,
11657 there's no need to record it in dwo_file.
11658 Also, we can't simply record type sections in dwo_file because
11659 we record a pointer into the vector in dwo_unit. As we collect more
11660 types we'll grow the vector and eventually have to reallocate space
11661 for it, invalidating all copies of pointers into the previous
11662 contents. */
11663 *dwo_file_slot = dwo_file;
11664 }
11665 else
11666 {
11667 if (dwarf_read_debug)
11668 {
11669 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11670 virtual_dwo_name.c_str ());
11671 }
11672 dwo_file = (struct dwo_file *) *dwo_file_slot;
11673 }
11674
11675 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11676 dwo_unit->dwo_file = dwo_file;
11677 dwo_unit->signature = signature;
11678 dwo_unit->section =
11679 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11680 *dwo_unit->section = sections.info_or_types;
11681 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11682
11683 return dwo_unit;
11684 }
11685
11686 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11687 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11688 piece within that section used by a TU/CU, return a virtual section
11689 of just that piece. */
11690
11691 static struct dwarf2_section_info
11692 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11693 struct dwarf2_section_info *section,
11694 bfd_size_type offset, bfd_size_type size)
11695 {
11696 struct dwarf2_section_info result;
11697 asection *sectp;
11698
11699 gdb_assert (section != NULL);
11700 gdb_assert (!section->is_virtual);
11701
11702 memset (&result, 0, sizeof (result));
11703 result.s.containing_section = section;
11704 result.is_virtual = true;
11705
11706 if (size == 0)
11707 return result;
11708
11709 sectp = section->get_bfd_section ();
11710
11711 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11712 bounds of the real section. This is a pretty-rare event, so just
11713 flag an error (easier) instead of a warning and trying to cope. */
11714 if (sectp == NULL
11715 || offset + size > bfd_section_size (sectp))
11716 {
11717 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11718 " in section %s [in module %s]"),
11719 sectp ? bfd_section_name (sectp) : "<unknown>",
11720 objfile_name (dwarf2_per_objfile->objfile));
11721 }
11722
11723 result.virtual_offset = offset;
11724 result.size = size;
11725 return result;
11726 }
11727
11728 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11729 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11730 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11731 This is for DWP version 2 files. */
11732
11733 static struct dwo_unit *
11734 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11735 struct dwp_file *dwp_file,
11736 uint32_t unit_index,
11737 const char *comp_dir,
11738 ULONGEST signature, int is_debug_types)
11739 {
11740 struct objfile *objfile = dwarf2_per_objfile->objfile;
11741 const struct dwp_hash_table *dwp_htab =
11742 is_debug_types ? dwp_file->tus : dwp_file->cus;
11743 bfd *dbfd = dwp_file->dbfd.get ();
11744 const char *kind = is_debug_types ? "TU" : "CU";
11745 struct dwo_file *dwo_file;
11746 struct dwo_unit *dwo_unit;
11747 struct virtual_v2_dwo_sections sections;
11748 void **dwo_file_slot;
11749 int i;
11750
11751 gdb_assert (dwp_file->version == 2);
11752
11753 if (dwarf_read_debug)
11754 {
11755 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11756 kind,
11757 pulongest (unit_index), hex_string (signature),
11758 dwp_file->name);
11759 }
11760
11761 /* Fetch the section offsets of this DWO unit. */
11762
11763 memset (&sections, 0, sizeof (sections));
11764
11765 for (i = 0; i < dwp_htab->nr_columns; ++i)
11766 {
11767 uint32_t offset = read_4_bytes (dbfd,
11768 dwp_htab->section_pool.v2.offsets
11769 + (((unit_index - 1) * dwp_htab->nr_columns
11770 + i)
11771 * sizeof (uint32_t)));
11772 uint32_t size = read_4_bytes (dbfd,
11773 dwp_htab->section_pool.v2.sizes
11774 + (((unit_index - 1) * dwp_htab->nr_columns
11775 + i)
11776 * sizeof (uint32_t)));
11777
11778 switch (dwp_htab->section_pool.v2.section_ids[i])
11779 {
11780 case DW_SECT_INFO:
11781 case DW_SECT_TYPES:
11782 sections.info_or_types_offset = offset;
11783 sections.info_or_types_size = size;
11784 break;
11785 case DW_SECT_ABBREV:
11786 sections.abbrev_offset = offset;
11787 sections.abbrev_size = size;
11788 break;
11789 case DW_SECT_LINE:
11790 sections.line_offset = offset;
11791 sections.line_size = size;
11792 break;
11793 case DW_SECT_LOC:
11794 sections.loc_offset = offset;
11795 sections.loc_size = size;
11796 break;
11797 case DW_SECT_STR_OFFSETS:
11798 sections.str_offsets_offset = offset;
11799 sections.str_offsets_size = size;
11800 break;
11801 case DW_SECT_MACINFO:
11802 sections.macinfo_offset = offset;
11803 sections.macinfo_size = size;
11804 break;
11805 case DW_SECT_MACRO:
11806 sections.macro_offset = offset;
11807 sections.macro_size = size;
11808 break;
11809 }
11810 }
11811
11812 /* It's easier for the rest of the code if we fake a struct dwo_file and
11813 have dwo_unit "live" in that. At least for now.
11814
11815 The DWP file can be made up of a random collection of CUs and TUs.
11816 However, for each CU + set of TUs that came from the same original DWO
11817 file, we can combine them back into a virtual DWO file to save space
11818 (fewer struct dwo_file objects to allocate). Remember that for really
11819 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11820
11821 std::string virtual_dwo_name =
11822 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11823 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11824 (long) (sections.line_size ? sections.line_offset : 0),
11825 (long) (sections.loc_size ? sections.loc_offset : 0),
11826 (long) (sections.str_offsets_size
11827 ? sections.str_offsets_offset : 0));
11828 /* Can we use an existing virtual DWO file? */
11829 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11830 virtual_dwo_name.c_str (),
11831 comp_dir);
11832 /* Create one if necessary. */
11833 if (*dwo_file_slot == NULL)
11834 {
11835 if (dwarf_read_debug)
11836 {
11837 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11838 virtual_dwo_name.c_str ());
11839 }
11840 dwo_file = new struct dwo_file;
11841 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11842 virtual_dwo_name);
11843 dwo_file->comp_dir = comp_dir;
11844 dwo_file->sections.abbrev =
11845 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11846 sections.abbrev_offset, sections.abbrev_size);
11847 dwo_file->sections.line =
11848 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11849 sections.line_offset, sections.line_size);
11850 dwo_file->sections.loc =
11851 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11852 sections.loc_offset, sections.loc_size);
11853 dwo_file->sections.macinfo =
11854 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11855 sections.macinfo_offset, sections.macinfo_size);
11856 dwo_file->sections.macro =
11857 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11858 sections.macro_offset, sections.macro_size);
11859 dwo_file->sections.str_offsets =
11860 create_dwp_v2_section (dwarf2_per_objfile,
11861 &dwp_file->sections.str_offsets,
11862 sections.str_offsets_offset,
11863 sections.str_offsets_size);
11864 /* The "str" section is global to the entire DWP file. */
11865 dwo_file->sections.str = dwp_file->sections.str;
11866 /* The info or types section is assigned below to dwo_unit,
11867 there's no need to record it in dwo_file.
11868 Also, we can't simply record type sections in dwo_file because
11869 we record a pointer into the vector in dwo_unit. As we collect more
11870 types we'll grow the vector and eventually have to reallocate space
11871 for it, invalidating all copies of pointers into the previous
11872 contents. */
11873 *dwo_file_slot = dwo_file;
11874 }
11875 else
11876 {
11877 if (dwarf_read_debug)
11878 {
11879 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11880 virtual_dwo_name.c_str ());
11881 }
11882 dwo_file = (struct dwo_file *) *dwo_file_slot;
11883 }
11884
11885 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11886 dwo_unit->dwo_file = dwo_file;
11887 dwo_unit->signature = signature;
11888 dwo_unit->section =
11889 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11890 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11891 is_debug_types
11892 ? &dwp_file->sections.types
11893 : &dwp_file->sections.info,
11894 sections.info_or_types_offset,
11895 sections.info_or_types_size);
11896 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11897
11898 return dwo_unit;
11899 }
11900
11901 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11902 Returns NULL if the signature isn't found. */
11903
11904 static struct dwo_unit *
11905 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11906 struct dwp_file *dwp_file, const char *comp_dir,
11907 ULONGEST signature, int is_debug_types)
11908 {
11909 const struct dwp_hash_table *dwp_htab =
11910 is_debug_types ? dwp_file->tus : dwp_file->cus;
11911 bfd *dbfd = dwp_file->dbfd.get ();
11912 uint32_t mask = dwp_htab->nr_slots - 1;
11913 uint32_t hash = signature & mask;
11914 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11915 unsigned int i;
11916 void **slot;
11917 struct dwo_unit find_dwo_cu;
11918
11919 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11920 find_dwo_cu.signature = signature;
11921 slot = htab_find_slot (is_debug_types
11922 ? dwp_file->loaded_tus.get ()
11923 : dwp_file->loaded_cus.get (),
11924 &find_dwo_cu, INSERT);
11925
11926 if (*slot != NULL)
11927 return (struct dwo_unit *) *slot;
11928
11929 /* Use a for loop so that we don't loop forever on bad debug info. */
11930 for (i = 0; i < dwp_htab->nr_slots; ++i)
11931 {
11932 ULONGEST signature_in_table;
11933
11934 signature_in_table =
11935 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11936 if (signature_in_table == signature)
11937 {
11938 uint32_t unit_index =
11939 read_4_bytes (dbfd,
11940 dwp_htab->unit_table + hash * sizeof (uint32_t));
11941
11942 if (dwp_file->version == 1)
11943 {
11944 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11945 dwp_file, unit_index,
11946 comp_dir, signature,
11947 is_debug_types);
11948 }
11949 else
11950 {
11951 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11952 dwp_file, unit_index,
11953 comp_dir, signature,
11954 is_debug_types);
11955 }
11956 return (struct dwo_unit *) *slot;
11957 }
11958 if (signature_in_table == 0)
11959 return NULL;
11960 hash = (hash + hash2) & mask;
11961 }
11962
11963 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11964 " [in module %s]"),
11965 dwp_file->name);
11966 }
11967
11968 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11969 Open the file specified by FILE_NAME and hand it off to BFD for
11970 preliminary analysis. Return a newly initialized bfd *, which
11971 includes a canonicalized copy of FILE_NAME.
11972 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11973 SEARCH_CWD is true if the current directory is to be searched.
11974 It will be searched before debug-file-directory.
11975 If successful, the file is added to the bfd include table of the
11976 objfile's bfd (see gdb_bfd_record_inclusion).
11977 If unable to find/open the file, return NULL.
11978 NOTE: This function is derived from symfile_bfd_open. */
11979
11980 static gdb_bfd_ref_ptr
11981 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11982 const char *file_name, int is_dwp, int search_cwd)
11983 {
11984 int desc;
11985 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11986 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11987 to debug_file_directory. */
11988 const char *search_path;
11989 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11990
11991 gdb::unique_xmalloc_ptr<char> search_path_holder;
11992 if (search_cwd)
11993 {
11994 if (*debug_file_directory != '\0')
11995 {
11996 search_path_holder.reset (concat (".", dirname_separator_string,
11997 debug_file_directory,
11998 (char *) NULL));
11999 search_path = search_path_holder.get ();
12000 }
12001 else
12002 search_path = ".";
12003 }
12004 else
12005 search_path = debug_file_directory;
12006
12007 openp_flags flags = OPF_RETURN_REALPATH;
12008 if (is_dwp)
12009 flags |= OPF_SEARCH_IN_PATH;
12010
12011 gdb::unique_xmalloc_ptr<char> absolute_name;
12012 desc = openp (search_path, flags, file_name,
12013 O_RDONLY | O_BINARY, &absolute_name);
12014 if (desc < 0)
12015 return NULL;
12016
12017 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12018 gnutarget, desc));
12019 if (sym_bfd == NULL)
12020 return NULL;
12021 bfd_set_cacheable (sym_bfd.get (), 1);
12022
12023 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12024 return NULL;
12025
12026 /* Success. Record the bfd as having been included by the objfile's bfd.
12027 This is important because things like demangled_names_hash lives in the
12028 objfile's per_bfd space and may have references to things like symbol
12029 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12030 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12031
12032 return sym_bfd;
12033 }
12034
12035 /* Try to open DWO file FILE_NAME.
12036 COMP_DIR is the DW_AT_comp_dir attribute.
12037 The result is the bfd handle of the file.
12038 If there is a problem finding or opening the file, return NULL.
12039 Upon success, the canonicalized path of the file is stored in the bfd,
12040 same as symfile_bfd_open. */
12041
12042 static gdb_bfd_ref_ptr
12043 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12044 const char *file_name, const char *comp_dir)
12045 {
12046 if (IS_ABSOLUTE_PATH (file_name))
12047 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12048 0 /*is_dwp*/, 0 /*search_cwd*/);
12049
12050 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12051
12052 if (comp_dir != NULL)
12053 {
12054 gdb::unique_xmalloc_ptr<char> path_to_try
12055 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12056
12057 /* NOTE: If comp_dir is a relative path, this will also try the
12058 search path, which seems useful. */
12059 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12060 path_to_try.get (),
12061 0 /*is_dwp*/,
12062 1 /*search_cwd*/));
12063 if (abfd != NULL)
12064 return abfd;
12065 }
12066
12067 /* That didn't work, try debug-file-directory, which, despite its name,
12068 is a list of paths. */
12069
12070 if (*debug_file_directory == '\0')
12071 return NULL;
12072
12073 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12074 0 /*is_dwp*/, 1 /*search_cwd*/);
12075 }
12076
12077 /* This function is mapped across the sections and remembers the offset and
12078 size of each of the DWO debugging sections we are interested in. */
12079
12080 static void
12081 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12082 {
12083 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12084 const struct dwop_section_names *names = &dwop_section_names;
12085
12086 if (section_is_p (sectp->name, &names->abbrev_dwo))
12087 {
12088 dwo_sections->abbrev.s.section = sectp;
12089 dwo_sections->abbrev.size = bfd_section_size (sectp);
12090 }
12091 else if (section_is_p (sectp->name, &names->info_dwo))
12092 {
12093 dwo_sections->info.s.section = sectp;
12094 dwo_sections->info.size = bfd_section_size (sectp);
12095 }
12096 else if (section_is_p (sectp->name, &names->line_dwo))
12097 {
12098 dwo_sections->line.s.section = sectp;
12099 dwo_sections->line.size = bfd_section_size (sectp);
12100 }
12101 else if (section_is_p (sectp->name, &names->loc_dwo))
12102 {
12103 dwo_sections->loc.s.section = sectp;
12104 dwo_sections->loc.size = bfd_section_size (sectp);
12105 }
12106 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12107 {
12108 dwo_sections->macinfo.s.section = sectp;
12109 dwo_sections->macinfo.size = bfd_section_size (sectp);
12110 }
12111 else if (section_is_p (sectp->name, &names->macro_dwo))
12112 {
12113 dwo_sections->macro.s.section = sectp;
12114 dwo_sections->macro.size = bfd_section_size (sectp);
12115 }
12116 else if (section_is_p (sectp->name, &names->str_dwo))
12117 {
12118 dwo_sections->str.s.section = sectp;
12119 dwo_sections->str.size = bfd_section_size (sectp);
12120 }
12121 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12122 {
12123 dwo_sections->str_offsets.s.section = sectp;
12124 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12125 }
12126 else if (section_is_p (sectp->name, &names->types_dwo))
12127 {
12128 struct dwarf2_section_info type_section;
12129
12130 memset (&type_section, 0, sizeof (type_section));
12131 type_section.s.section = sectp;
12132 type_section.size = bfd_section_size (sectp);
12133 dwo_sections->types.push_back (type_section);
12134 }
12135 }
12136
12137 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12138 by PER_CU. This is for the non-DWP case.
12139 The result is NULL if DWO_NAME can't be found. */
12140
12141 static struct dwo_file *
12142 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12143 const char *dwo_name, const char *comp_dir)
12144 {
12145 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12146
12147 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12148 if (dbfd == NULL)
12149 {
12150 if (dwarf_read_debug)
12151 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12152 return NULL;
12153 }
12154
12155 dwo_file_up dwo_file (new struct dwo_file);
12156 dwo_file->dwo_name = dwo_name;
12157 dwo_file->comp_dir = comp_dir;
12158 dwo_file->dbfd = std::move (dbfd);
12159
12160 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12161 &dwo_file->sections);
12162
12163 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12164 dwo_file->sections.info, dwo_file->cus);
12165
12166 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12167 dwo_file->sections.types, dwo_file->tus);
12168
12169 if (dwarf_read_debug)
12170 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12171
12172 return dwo_file.release ();
12173 }
12174
12175 /* This function is mapped across the sections and remembers the offset and
12176 size of each of the DWP debugging sections common to version 1 and 2 that
12177 we are interested in. */
12178
12179 static void
12180 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12181 void *dwp_file_ptr)
12182 {
12183 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12184 const struct dwop_section_names *names = &dwop_section_names;
12185 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12186
12187 /* Record the ELF section number for later lookup: this is what the
12188 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12189 gdb_assert (elf_section_nr < dwp_file->num_sections);
12190 dwp_file->elf_sections[elf_section_nr] = sectp;
12191
12192 /* Look for specific sections that we need. */
12193 if (section_is_p (sectp->name, &names->str_dwo))
12194 {
12195 dwp_file->sections.str.s.section = sectp;
12196 dwp_file->sections.str.size = bfd_section_size (sectp);
12197 }
12198 else if (section_is_p (sectp->name, &names->cu_index))
12199 {
12200 dwp_file->sections.cu_index.s.section = sectp;
12201 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12202 }
12203 else if (section_is_p (sectp->name, &names->tu_index))
12204 {
12205 dwp_file->sections.tu_index.s.section = sectp;
12206 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12207 }
12208 }
12209
12210 /* This function is mapped across the sections and remembers the offset and
12211 size of each of the DWP version 2 debugging sections that we are interested
12212 in. This is split into a separate function because we don't know if we
12213 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12214
12215 static void
12216 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12217 {
12218 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12219 const struct dwop_section_names *names = &dwop_section_names;
12220 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12221
12222 /* Record the ELF section number for later lookup: this is what the
12223 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12224 gdb_assert (elf_section_nr < dwp_file->num_sections);
12225 dwp_file->elf_sections[elf_section_nr] = sectp;
12226
12227 /* Look for specific sections that we need. */
12228 if (section_is_p (sectp->name, &names->abbrev_dwo))
12229 {
12230 dwp_file->sections.abbrev.s.section = sectp;
12231 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12232 }
12233 else if (section_is_p (sectp->name, &names->info_dwo))
12234 {
12235 dwp_file->sections.info.s.section = sectp;
12236 dwp_file->sections.info.size = bfd_section_size (sectp);
12237 }
12238 else if (section_is_p (sectp->name, &names->line_dwo))
12239 {
12240 dwp_file->sections.line.s.section = sectp;
12241 dwp_file->sections.line.size = bfd_section_size (sectp);
12242 }
12243 else if (section_is_p (sectp->name, &names->loc_dwo))
12244 {
12245 dwp_file->sections.loc.s.section = sectp;
12246 dwp_file->sections.loc.size = bfd_section_size (sectp);
12247 }
12248 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12249 {
12250 dwp_file->sections.macinfo.s.section = sectp;
12251 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12252 }
12253 else if (section_is_p (sectp->name, &names->macro_dwo))
12254 {
12255 dwp_file->sections.macro.s.section = sectp;
12256 dwp_file->sections.macro.size = bfd_section_size (sectp);
12257 }
12258 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12259 {
12260 dwp_file->sections.str_offsets.s.section = sectp;
12261 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12262 }
12263 else if (section_is_p (sectp->name, &names->types_dwo))
12264 {
12265 dwp_file->sections.types.s.section = sectp;
12266 dwp_file->sections.types.size = bfd_section_size (sectp);
12267 }
12268 }
12269
12270 /* Hash function for dwp_file loaded CUs/TUs. */
12271
12272 static hashval_t
12273 hash_dwp_loaded_cutus (const void *item)
12274 {
12275 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12276
12277 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12278 return dwo_unit->signature;
12279 }
12280
12281 /* Equality function for dwp_file loaded CUs/TUs. */
12282
12283 static int
12284 eq_dwp_loaded_cutus (const void *a, const void *b)
12285 {
12286 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12287 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12288
12289 return dua->signature == dub->signature;
12290 }
12291
12292 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12293
12294 static htab_up
12295 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12296 {
12297 return htab_up (htab_create_alloc (3,
12298 hash_dwp_loaded_cutus,
12299 eq_dwp_loaded_cutus,
12300 NULL, xcalloc, xfree));
12301 }
12302
12303 /* Try to open DWP file FILE_NAME.
12304 The result is the bfd handle of the file.
12305 If there is a problem finding or opening the file, return NULL.
12306 Upon success, the canonicalized path of the file is stored in the bfd,
12307 same as symfile_bfd_open. */
12308
12309 static gdb_bfd_ref_ptr
12310 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12311 const char *file_name)
12312 {
12313 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12314 1 /*is_dwp*/,
12315 1 /*search_cwd*/));
12316 if (abfd != NULL)
12317 return abfd;
12318
12319 /* Work around upstream bug 15652.
12320 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12321 [Whether that's a "bug" is debatable, but it is getting in our way.]
12322 We have no real idea where the dwp file is, because gdb's realpath-ing
12323 of the executable's path may have discarded the needed info.
12324 [IWBN if the dwp file name was recorded in the executable, akin to
12325 .gnu_debuglink, but that doesn't exist yet.]
12326 Strip the directory from FILE_NAME and search again. */
12327 if (*debug_file_directory != '\0')
12328 {
12329 /* Don't implicitly search the current directory here.
12330 If the user wants to search "." to handle this case,
12331 it must be added to debug-file-directory. */
12332 return try_open_dwop_file (dwarf2_per_objfile,
12333 lbasename (file_name), 1 /*is_dwp*/,
12334 0 /*search_cwd*/);
12335 }
12336
12337 return NULL;
12338 }
12339
12340 /* Initialize the use of the DWP file for the current objfile.
12341 By convention the name of the DWP file is ${objfile}.dwp.
12342 The result is NULL if it can't be found. */
12343
12344 static std::unique_ptr<struct dwp_file>
12345 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12346 {
12347 struct objfile *objfile = dwarf2_per_objfile->objfile;
12348
12349 /* Try to find first .dwp for the binary file before any symbolic links
12350 resolving. */
12351
12352 /* If the objfile is a debug file, find the name of the real binary
12353 file and get the name of dwp file from there. */
12354 std::string dwp_name;
12355 if (objfile->separate_debug_objfile_backlink != NULL)
12356 {
12357 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12358 const char *backlink_basename = lbasename (backlink->original_name);
12359
12360 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12361 }
12362 else
12363 dwp_name = objfile->original_name;
12364
12365 dwp_name += ".dwp";
12366
12367 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12368 if (dbfd == NULL
12369 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12370 {
12371 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12372 dwp_name = objfile_name (objfile);
12373 dwp_name += ".dwp";
12374 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12375 }
12376
12377 if (dbfd == NULL)
12378 {
12379 if (dwarf_read_debug)
12380 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12381 return std::unique_ptr<dwp_file> ();
12382 }
12383
12384 const char *name = bfd_get_filename (dbfd.get ());
12385 std::unique_ptr<struct dwp_file> dwp_file
12386 (new struct dwp_file (name, std::move (dbfd)));
12387
12388 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12389 dwp_file->elf_sections =
12390 OBSTACK_CALLOC (&objfile->objfile_obstack,
12391 dwp_file->num_sections, asection *);
12392
12393 bfd_map_over_sections (dwp_file->dbfd.get (),
12394 dwarf2_locate_common_dwp_sections,
12395 dwp_file.get ());
12396
12397 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12398 0);
12399
12400 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12401 1);
12402
12403 /* The DWP file version is stored in the hash table. Oh well. */
12404 if (dwp_file->cus && dwp_file->tus
12405 && dwp_file->cus->version != dwp_file->tus->version)
12406 {
12407 /* Technically speaking, we should try to limp along, but this is
12408 pretty bizarre. We use pulongest here because that's the established
12409 portability solution (e.g, we cannot use %u for uint32_t). */
12410 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12411 " TU version %s [in DWP file %s]"),
12412 pulongest (dwp_file->cus->version),
12413 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12414 }
12415
12416 if (dwp_file->cus)
12417 dwp_file->version = dwp_file->cus->version;
12418 else if (dwp_file->tus)
12419 dwp_file->version = dwp_file->tus->version;
12420 else
12421 dwp_file->version = 2;
12422
12423 if (dwp_file->version == 2)
12424 bfd_map_over_sections (dwp_file->dbfd.get (),
12425 dwarf2_locate_v2_dwp_sections,
12426 dwp_file.get ());
12427
12428 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12429 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12430
12431 if (dwarf_read_debug)
12432 {
12433 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12434 fprintf_unfiltered (gdb_stdlog,
12435 " %s CUs, %s TUs\n",
12436 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12437 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12438 }
12439
12440 return dwp_file;
12441 }
12442
12443 /* Wrapper around open_and_init_dwp_file, only open it once. */
12444
12445 static struct dwp_file *
12446 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12447 {
12448 if (! dwarf2_per_objfile->dwp_checked)
12449 {
12450 dwarf2_per_objfile->dwp_file
12451 = open_and_init_dwp_file (dwarf2_per_objfile);
12452 dwarf2_per_objfile->dwp_checked = 1;
12453 }
12454 return dwarf2_per_objfile->dwp_file.get ();
12455 }
12456
12457 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12458 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12459 or in the DWP file for the objfile, referenced by THIS_UNIT.
12460 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12461 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12462
12463 This is called, for example, when wanting to read a variable with a
12464 complex location. Therefore we don't want to do file i/o for every call.
12465 Therefore we don't want to look for a DWO file on every call.
12466 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12467 then we check if we've already seen DWO_NAME, and only THEN do we check
12468 for a DWO file.
12469
12470 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12471 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12472
12473 static struct dwo_unit *
12474 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12475 const char *dwo_name, const char *comp_dir,
12476 ULONGEST signature, int is_debug_types)
12477 {
12478 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12479 struct objfile *objfile = dwarf2_per_objfile->objfile;
12480 const char *kind = is_debug_types ? "TU" : "CU";
12481 void **dwo_file_slot;
12482 struct dwo_file *dwo_file;
12483 struct dwp_file *dwp_file;
12484
12485 /* First see if there's a DWP file.
12486 If we have a DWP file but didn't find the DWO inside it, don't
12487 look for the original DWO file. It makes gdb behave differently
12488 depending on whether one is debugging in the build tree. */
12489
12490 dwp_file = get_dwp_file (dwarf2_per_objfile);
12491 if (dwp_file != NULL)
12492 {
12493 const struct dwp_hash_table *dwp_htab =
12494 is_debug_types ? dwp_file->tus : dwp_file->cus;
12495
12496 if (dwp_htab != NULL)
12497 {
12498 struct dwo_unit *dwo_cutu =
12499 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12500 signature, is_debug_types);
12501
12502 if (dwo_cutu != NULL)
12503 {
12504 if (dwarf_read_debug)
12505 {
12506 fprintf_unfiltered (gdb_stdlog,
12507 "Virtual DWO %s %s found: @%s\n",
12508 kind, hex_string (signature),
12509 host_address_to_string (dwo_cutu));
12510 }
12511 return dwo_cutu;
12512 }
12513 }
12514 }
12515 else
12516 {
12517 /* No DWP file, look for the DWO file. */
12518
12519 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12520 dwo_name, comp_dir);
12521 if (*dwo_file_slot == NULL)
12522 {
12523 /* Read in the file and build a table of the CUs/TUs it contains. */
12524 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12525 }
12526 /* NOTE: This will be NULL if unable to open the file. */
12527 dwo_file = (struct dwo_file *) *dwo_file_slot;
12528
12529 if (dwo_file != NULL)
12530 {
12531 struct dwo_unit *dwo_cutu = NULL;
12532
12533 if (is_debug_types && dwo_file->tus)
12534 {
12535 struct dwo_unit find_dwo_cutu;
12536
12537 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12538 find_dwo_cutu.signature = signature;
12539 dwo_cutu
12540 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12541 &find_dwo_cutu);
12542 }
12543 else if (!is_debug_types && dwo_file->cus)
12544 {
12545 struct dwo_unit find_dwo_cutu;
12546
12547 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12548 find_dwo_cutu.signature = signature;
12549 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12550 &find_dwo_cutu);
12551 }
12552
12553 if (dwo_cutu != NULL)
12554 {
12555 if (dwarf_read_debug)
12556 {
12557 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12558 kind, dwo_name, hex_string (signature),
12559 host_address_to_string (dwo_cutu));
12560 }
12561 return dwo_cutu;
12562 }
12563 }
12564 }
12565
12566 /* We didn't find it. This could mean a dwo_id mismatch, or
12567 someone deleted the DWO/DWP file, or the search path isn't set up
12568 correctly to find the file. */
12569
12570 if (dwarf_read_debug)
12571 {
12572 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12573 kind, dwo_name, hex_string (signature));
12574 }
12575
12576 /* This is a warning and not a complaint because it can be caused by
12577 pilot error (e.g., user accidentally deleting the DWO). */
12578 {
12579 /* Print the name of the DWP file if we looked there, helps the user
12580 better diagnose the problem. */
12581 std::string dwp_text;
12582
12583 if (dwp_file != NULL)
12584 dwp_text = string_printf (" [in DWP file %s]",
12585 lbasename (dwp_file->name));
12586
12587 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12588 " [in module %s]"),
12589 kind, dwo_name, hex_string (signature),
12590 dwp_text.c_str (),
12591 this_unit->is_debug_types ? "TU" : "CU",
12592 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12593 }
12594 return NULL;
12595 }
12596
12597 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12598 See lookup_dwo_cutu_unit for details. */
12599
12600 static struct dwo_unit *
12601 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12602 const char *dwo_name, const char *comp_dir,
12603 ULONGEST signature)
12604 {
12605 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12606 }
12607
12608 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12609 See lookup_dwo_cutu_unit for details. */
12610
12611 static struct dwo_unit *
12612 lookup_dwo_type_unit (struct signatured_type *this_tu,
12613 const char *dwo_name, const char *comp_dir)
12614 {
12615 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12616 }
12617
12618 /* Traversal function for queue_and_load_all_dwo_tus. */
12619
12620 static int
12621 queue_and_load_dwo_tu (void **slot, void *info)
12622 {
12623 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12624 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12625 ULONGEST signature = dwo_unit->signature;
12626 struct signatured_type *sig_type =
12627 lookup_dwo_signatured_type (per_cu->cu, signature);
12628
12629 if (sig_type != NULL)
12630 {
12631 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12632
12633 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12634 a real dependency of PER_CU on SIG_TYPE. That is detected later
12635 while processing PER_CU. */
12636 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12637 load_full_type_unit (sig_cu);
12638 per_cu->imported_symtabs_push (sig_cu);
12639 }
12640
12641 return 1;
12642 }
12643
12644 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12645 The DWO may have the only definition of the type, though it may not be
12646 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12647 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12648
12649 static void
12650 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12651 {
12652 struct dwo_unit *dwo_unit;
12653 struct dwo_file *dwo_file;
12654
12655 gdb_assert (!per_cu->is_debug_types);
12656 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12657 gdb_assert (per_cu->cu != NULL);
12658
12659 dwo_unit = per_cu->cu->dwo_unit;
12660 gdb_assert (dwo_unit != NULL);
12661
12662 dwo_file = dwo_unit->dwo_file;
12663 if (dwo_file->tus != NULL)
12664 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12665 per_cu);
12666 }
12667
12668 /* Read in various DIEs. */
12669
12670 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12671 Inherit only the children of the DW_AT_abstract_origin DIE not being
12672 already referenced by DW_AT_abstract_origin from the children of the
12673 current DIE. */
12674
12675 static void
12676 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12677 {
12678 struct die_info *child_die;
12679 sect_offset *offsetp;
12680 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12681 struct die_info *origin_die;
12682 /* Iterator of the ORIGIN_DIE children. */
12683 struct die_info *origin_child_die;
12684 struct attribute *attr;
12685 struct dwarf2_cu *origin_cu;
12686 struct pending **origin_previous_list_in_scope;
12687
12688 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12689 if (!attr)
12690 return;
12691
12692 /* Note that following die references may follow to a die in a
12693 different cu. */
12694
12695 origin_cu = cu;
12696 origin_die = follow_die_ref (die, attr, &origin_cu);
12697
12698 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12699 symbols in. */
12700 origin_previous_list_in_scope = origin_cu->list_in_scope;
12701 origin_cu->list_in_scope = cu->list_in_scope;
12702
12703 if (die->tag != origin_die->tag
12704 && !(die->tag == DW_TAG_inlined_subroutine
12705 && origin_die->tag == DW_TAG_subprogram))
12706 complaint (_("DIE %s and its abstract origin %s have different tags"),
12707 sect_offset_str (die->sect_off),
12708 sect_offset_str (origin_die->sect_off));
12709
12710 std::vector<sect_offset> offsets;
12711
12712 for (child_die = die->child;
12713 child_die && child_die->tag;
12714 child_die = sibling_die (child_die))
12715 {
12716 struct die_info *child_origin_die;
12717 struct dwarf2_cu *child_origin_cu;
12718
12719 /* We are trying to process concrete instance entries:
12720 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12721 it's not relevant to our analysis here. i.e. detecting DIEs that are
12722 present in the abstract instance but not referenced in the concrete
12723 one. */
12724 if (child_die->tag == DW_TAG_call_site
12725 || child_die->tag == DW_TAG_GNU_call_site)
12726 continue;
12727
12728 /* For each CHILD_DIE, find the corresponding child of
12729 ORIGIN_DIE. If there is more than one layer of
12730 DW_AT_abstract_origin, follow them all; there shouldn't be,
12731 but GCC versions at least through 4.4 generate this (GCC PR
12732 40573). */
12733 child_origin_die = child_die;
12734 child_origin_cu = cu;
12735 while (1)
12736 {
12737 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12738 child_origin_cu);
12739 if (attr == NULL)
12740 break;
12741 child_origin_die = follow_die_ref (child_origin_die, attr,
12742 &child_origin_cu);
12743 }
12744
12745 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12746 counterpart may exist. */
12747 if (child_origin_die != child_die)
12748 {
12749 if (child_die->tag != child_origin_die->tag
12750 && !(child_die->tag == DW_TAG_inlined_subroutine
12751 && child_origin_die->tag == DW_TAG_subprogram))
12752 complaint (_("Child DIE %s and its abstract origin %s have "
12753 "different tags"),
12754 sect_offset_str (child_die->sect_off),
12755 sect_offset_str (child_origin_die->sect_off));
12756 if (child_origin_die->parent != origin_die)
12757 complaint (_("Child DIE %s and its abstract origin %s have "
12758 "different parents"),
12759 sect_offset_str (child_die->sect_off),
12760 sect_offset_str (child_origin_die->sect_off));
12761 else
12762 offsets.push_back (child_origin_die->sect_off);
12763 }
12764 }
12765 std::sort (offsets.begin (), offsets.end ());
12766 sect_offset *offsets_end = offsets.data () + offsets.size ();
12767 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12768 if (offsetp[-1] == *offsetp)
12769 complaint (_("Multiple children of DIE %s refer "
12770 "to DIE %s as their abstract origin"),
12771 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12772
12773 offsetp = offsets.data ();
12774 origin_child_die = origin_die->child;
12775 while (origin_child_die && origin_child_die->tag)
12776 {
12777 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12778 while (offsetp < offsets_end
12779 && *offsetp < origin_child_die->sect_off)
12780 offsetp++;
12781 if (offsetp >= offsets_end
12782 || *offsetp > origin_child_die->sect_off)
12783 {
12784 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12785 Check whether we're already processing ORIGIN_CHILD_DIE.
12786 This can happen with mutually referenced abstract_origins.
12787 PR 16581. */
12788 if (!origin_child_die->in_process)
12789 process_die (origin_child_die, origin_cu);
12790 }
12791 origin_child_die = sibling_die (origin_child_die);
12792 }
12793 origin_cu->list_in_scope = origin_previous_list_in_scope;
12794
12795 if (cu != origin_cu)
12796 compute_delayed_physnames (origin_cu);
12797 }
12798
12799 static void
12800 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12801 {
12802 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12803 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12804 struct context_stack *newobj;
12805 CORE_ADDR lowpc;
12806 CORE_ADDR highpc;
12807 struct die_info *child_die;
12808 struct attribute *attr, *call_line, *call_file;
12809 const char *name;
12810 CORE_ADDR baseaddr;
12811 struct block *block;
12812 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12813 std::vector<struct symbol *> template_args;
12814 struct template_symbol *templ_func = NULL;
12815
12816 if (inlined_func)
12817 {
12818 /* If we do not have call site information, we can't show the
12819 caller of this inlined function. That's too confusing, so
12820 only use the scope for local variables. */
12821 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12822 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12823 if (call_line == NULL || call_file == NULL)
12824 {
12825 read_lexical_block_scope (die, cu);
12826 return;
12827 }
12828 }
12829
12830 baseaddr = objfile->text_section_offset ();
12831
12832 name = dwarf2_name (die, cu);
12833
12834 /* Ignore functions with missing or empty names. These are actually
12835 illegal according to the DWARF standard. */
12836 if (name == NULL)
12837 {
12838 complaint (_("missing name for subprogram DIE at %s"),
12839 sect_offset_str (die->sect_off));
12840 return;
12841 }
12842
12843 /* Ignore functions with missing or invalid low and high pc attributes. */
12844 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12845 <= PC_BOUNDS_INVALID)
12846 {
12847 attr = dwarf2_attr (die, DW_AT_external, cu);
12848 if (!attr || !DW_UNSND (attr))
12849 complaint (_("cannot get low and high bounds "
12850 "for subprogram DIE at %s"),
12851 sect_offset_str (die->sect_off));
12852 return;
12853 }
12854
12855 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12856 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12857
12858 /* If we have any template arguments, then we must allocate a
12859 different sort of symbol. */
12860 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12861 {
12862 if (child_die->tag == DW_TAG_template_type_param
12863 || child_die->tag == DW_TAG_template_value_param)
12864 {
12865 templ_func = allocate_template_symbol (objfile);
12866 templ_func->subclass = SYMBOL_TEMPLATE;
12867 break;
12868 }
12869 }
12870
12871 newobj = cu->get_builder ()->push_context (0, lowpc);
12872 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12873 (struct symbol *) templ_func);
12874
12875 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12876 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12877 cu->language);
12878
12879 /* If there is a location expression for DW_AT_frame_base, record
12880 it. */
12881 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12882 if (attr != nullptr)
12883 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12884
12885 /* If there is a location for the static link, record it. */
12886 newobj->static_link = NULL;
12887 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12888 if (attr != nullptr)
12889 {
12890 newobj->static_link
12891 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12892 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12893 cu->per_cu->addr_type ());
12894 }
12895
12896 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12897
12898 if (die->child != NULL)
12899 {
12900 child_die = die->child;
12901 while (child_die && child_die->tag)
12902 {
12903 if (child_die->tag == DW_TAG_template_type_param
12904 || child_die->tag == DW_TAG_template_value_param)
12905 {
12906 struct symbol *arg = new_symbol (child_die, NULL, cu);
12907
12908 if (arg != NULL)
12909 template_args.push_back (arg);
12910 }
12911 else
12912 process_die (child_die, cu);
12913 child_die = sibling_die (child_die);
12914 }
12915 }
12916
12917 inherit_abstract_dies (die, cu);
12918
12919 /* If we have a DW_AT_specification, we might need to import using
12920 directives from the context of the specification DIE. See the
12921 comment in determine_prefix. */
12922 if (cu->language == language_cplus
12923 && dwarf2_attr (die, DW_AT_specification, cu))
12924 {
12925 struct dwarf2_cu *spec_cu = cu;
12926 struct die_info *spec_die = die_specification (die, &spec_cu);
12927
12928 while (spec_die)
12929 {
12930 child_die = spec_die->child;
12931 while (child_die && child_die->tag)
12932 {
12933 if (child_die->tag == DW_TAG_imported_module)
12934 process_die (child_die, spec_cu);
12935 child_die = sibling_die (child_die);
12936 }
12937
12938 /* In some cases, GCC generates specification DIEs that
12939 themselves contain DW_AT_specification attributes. */
12940 spec_die = die_specification (spec_die, &spec_cu);
12941 }
12942 }
12943
12944 struct context_stack cstk = cu->get_builder ()->pop_context ();
12945 /* Make a block for the local symbols within. */
12946 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12947 cstk.static_link, lowpc, highpc);
12948
12949 /* For C++, set the block's scope. */
12950 if ((cu->language == language_cplus
12951 || cu->language == language_fortran
12952 || cu->language == language_d
12953 || cu->language == language_rust)
12954 && cu->processing_has_namespace_info)
12955 block_set_scope (block, determine_prefix (die, cu),
12956 &objfile->objfile_obstack);
12957
12958 /* If we have address ranges, record them. */
12959 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12960
12961 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12962
12963 /* Attach template arguments to function. */
12964 if (!template_args.empty ())
12965 {
12966 gdb_assert (templ_func != NULL);
12967
12968 templ_func->n_template_arguments = template_args.size ();
12969 templ_func->template_arguments
12970 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12971 templ_func->n_template_arguments);
12972 memcpy (templ_func->template_arguments,
12973 template_args.data (),
12974 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12975
12976 /* Make sure that the symtab is set on the new symbols. Even
12977 though they don't appear in this symtab directly, other parts
12978 of gdb assume that symbols do, and this is reasonably
12979 true. */
12980 for (symbol *sym : template_args)
12981 symbol_set_symtab (sym, symbol_symtab (templ_func));
12982 }
12983
12984 /* In C++, we can have functions nested inside functions (e.g., when
12985 a function declares a class that has methods). This means that
12986 when we finish processing a function scope, we may need to go
12987 back to building a containing block's symbol lists. */
12988 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12989 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12990
12991 /* If we've finished processing a top-level function, subsequent
12992 symbols go in the file symbol list. */
12993 if (cu->get_builder ()->outermost_context_p ())
12994 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12995 }
12996
12997 /* Process all the DIES contained within a lexical block scope. Start
12998 a new scope, process the dies, and then close the scope. */
12999
13000 static void
13001 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13002 {
13003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13005 CORE_ADDR lowpc, highpc;
13006 struct die_info *child_die;
13007 CORE_ADDR baseaddr;
13008
13009 baseaddr = objfile->text_section_offset ();
13010
13011 /* Ignore blocks with missing or invalid low and high pc attributes. */
13012 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13013 as multiple lexical blocks? Handling children in a sane way would
13014 be nasty. Might be easier to properly extend generic blocks to
13015 describe ranges. */
13016 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13017 {
13018 case PC_BOUNDS_NOT_PRESENT:
13019 /* DW_TAG_lexical_block has no attributes, process its children as if
13020 there was no wrapping by that DW_TAG_lexical_block.
13021 GCC does no longer produces such DWARF since GCC r224161. */
13022 for (child_die = die->child;
13023 child_die != NULL && child_die->tag;
13024 child_die = sibling_die (child_die))
13025 process_die (child_die, cu);
13026 return;
13027 case PC_BOUNDS_INVALID:
13028 return;
13029 }
13030 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13031 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13032
13033 cu->get_builder ()->push_context (0, lowpc);
13034 if (die->child != NULL)
13035 {
13036 child_die = die->child;
13037 while (child_die && child_die->tag)
13038 {
13039 process_die (child_die, cu);
13040 child_die = sibling_die (child_die);
13041 }
13042 }
13043 inherit_abstract_dies (die, cu);
13044 struct context_stack cstk = cu->get_builder ()->pop_context ();
13045
13046 if (*cu->get_builder ()->get_local_symbols () != NULL
13047 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13048 {
13049 struct block *block
13050 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13051 cstk.start_addr, highpc);
13052
13053 /* Note that recording ranges after traversing children, as we
13054 do here, means that recording a parent's ranges entails
13055 walking across all its children's ranges as they appear in
13056 the address map, which is quadratic behavior.
13057
13058 It would be nicer to record the parent's ranges before
13059 traversing its children, simply overriding whatever you find
13060 there. But since we don't even decide whether to create a
13061 block until after we've traversed its children, that's hard
13062 to do. */
13063 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13064 }
13065 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13066 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13067 }
13068
13069 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13070
13071 static void
13072 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13073 {
13074 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13075 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13076 CORE_ADDR pc, baseaddr;
13077 struct attribute *attr;
13078 struct call_site *call_site, call_site_local;
13079 void **slot;
13080 int nparams;
13081 struct die_info *child_die;
13082
13083 baseaddr = objfile->text_section_offset ();
13084
13085 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13086 if (attr == NULL)
13087 {
13088 /* This was a pre-DWARF-5 GNU extension alias
13089 for DW_AT_call_return_pc. */
13090 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13091 }
13092 if (!attr)
13093 {
13094 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13095 "DIE %s [in module %s]"),
13096 sect_offset_str (die->sect_off), objfile_name (objfile));
13097 return;
13098 }
13099 pc = attr->value_as_address () + baseaddr;
13100 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13101
13102 if (cu->call_site_htab == NULL)
13103 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13104 NULL, &objfile->objfile_obstack,
13105 hashtab_obstack_allocate, NULL);
13106 call_site_local.pc = pc;
13107 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13108 if (*slot != NULL)
13109 {
13110 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13111 "DIE %s [in module %s]"),
13112 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13113 objfile_name (objfile));
13114 return;
13115 }
13116
13117 /* Count parameters at the caller. */
13118
13119 nparams = 0;
13120 for (child_die = die->child; child_die && child_die->tag;
13121 child_die = sibling_die (child_die))
13122 {
13123 if (child_die->tag != DW_TAG_call_site_parameter
13124 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13125 {
13126 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13127 "DW_TAG_call_site child DIE %s [in module %s]"),
13128 child_die->tag, sect_offset_str (child_die->sect_off),
13129 objfile_name (objfile));
13130 continue;
13131 }
13132
13133 nparams++;
13134 }
13135
13136 call_site
13137 = ((struct call_site *)
13138 obstack_alloc (&objfile->objfile_obstack,
13139 sizeof (*call_site)
13140 + (sizeof (*call_site->parameter) * (nparams - 1))));
13141 *slot = call_site;
13142 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13143 call_site->pc = pc;
13144
13145 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13146 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13147 {
13148 struct die_info *func_die;
13149
13150 /* Skip also over DW_TAG_inlined_subroutine. */
13151 for (func_die = die->parent;
13152 func_die && func_die->tag != DW_TAG_subprogram
13153 && func_die->tag != DW_TAG_subroutine_type;
13154 func_die = func_die->parent);
13155
13156 /* DW_AT_call_all_calls is a superset
13157 of DW_AT_call_all_tail_calls. */
13158 if (func_die
13159 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13160 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13161 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13162 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13163 {
13164 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13165 not complete. But keep CALL_SITE for look ups via call_site_htab,
13166 both the initial caller containing the real return address PC and
13167 the final callee containing the current PC of a chain of tail
13168 calls do not need to have the tail call list complete. But any
13169 function candidate for a virtual tail call frame searched via
13170 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13171 determined unambiguously. */
13172 }
13173 else
13174 {
13175 struct type *func_type = NULL;
13176
13177 if (func_die)
13178 func_type = get_die_type (func_die, cu);
13179 if (func_type != NULL)
13180 {
13181 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13182
13183 /* Enlist this call site to the function. */
13184 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13185 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13186 }
13187 else
13188 complaint (_("Cannot find function owning DW_TAG_call_site "
13189 "DIE %s [in module %s]"),
13190 sect_offset_str (die->sect_off), objfile_name (objfile));
13191 }
13192 }
13193
13194 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13195 if (attr == NULL)
13196 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13197 if (attr == NULL)
13198 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13199 if (attr == NULL)
13200 {
13201 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13202 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13203 }
13204 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13205 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13206 /* Keep NULL DWARF_BLOCK. */;
13207 else if (attr->form_is_block ())
13208 {
13209 struct dwarf2_locexpr_baton *dlbaton;
13210
13211 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13212 dlbaton->data = DW_BLOCK (attr)->data;
13213 dlbaton->size = DW_BLOCK (attr)->size;
13214 dlbaton->per_cu = cu->per_cu;
13215
13216 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13217 }
13218 else if (attr->form_is_ref ())
13219 {
13220 struct dwarf2_cu *target_cu = cu;
13221 struct die_info *target_die;
13222
13223 target_die = follow_die_ref (die, attr, &target_cu);
13224 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13225 if (die_is_declaration (target_die, target_cu))
13226 {
13227 const char *target_physname;
13228
13229 /* Prefer the mangled name; otherwise compute the demangled one. */
13230 target_physname = dw2_linkage_name (target_die, target_cu);
13231 if (target_physname == NULL)
13232 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13233 if (target_physname == NULL)
13234 complaint (_("DW_AT_call_target target DIE has invalid "
13235 "physname, for referencing DIE %s [in module %s]"),
13236 sect_offset_str (die->sect_off), objfile_name (objfile));
13237 else
13238 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13239 }
13240 else
13241 {
13242 CORE_ADDR lowpc;
13243
13244 /* DW_AT_entry_pc should be preferred. */
13245 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13246 <= PC_BOUNDS_INVALID)
13247 complaint (_("DW_AT_call_target target DIE has invalid "
13248 "low pc, for referencing DIE %s [in module %s]"),
13249 sect_offset_str (die->sect_off), objfile_name (objfile));
13250 else
13251 {
13252 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13253 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13254 }
13255 }
13256 }
13257 else
13258 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13259 "block nor reference, for DIE %s [in module %s]"),
13260 sect_offset_str (die->sect_off), objfile_name (objfile));
13261
13262 call_site->per_cu = cu->per_cu;
13263
13264 for (child_die = die->child;
13265 child_die && child_die->tag;
13266 child_die = sibling_die (child_die))
13267 {
13268 struct call_site_parameter *parameter;
13269 struct attribute *loc, *origin;
13270
13271 if (child_die->tag != DW_TAG_call_site_parameter
13272 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13273 {
13274 /* Already printed the complaint above. */
13275 continue;
13276 }
13277
13278 gdb_assert (call_site->parameter_count < nparams);
13279 parameter = &call_site->parameter[call_site->parameter_count];
13280
13281 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13282 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13283 register is contained in DW_AT_call_value. */
13284
13285 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13286 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13287 if (origin == NULL)
13288 {
13289 /* This was a pre-DWARF-5 GNU extension alias
13290 for DW_AT_call_parameter. */
13291 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13292 }
13293 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13294 {
13295 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13296
13297 sect_offset sect_off
13298 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13299 if (!cu->header.offset_in_cu_p (sect_off))
13300 {
13301 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13302 binding can be done only inside one CU. Such referenced DIE
13303 therefore cannot be even moved to DW_TAG_partial_unit. */
13304 complaint (_("DW_AT_call_parameter offset is not in CU for "
13305 "DW_TAG_call_site child DIE %s [in module %s]"),
13306 sect_offset_str (child_die->sect_off),
13307 objfile_name (objfile));
13308 continue;
13309 }
13310 parameter->u.param_cu_off
13311 = (cu_offset) (sect_off - cu->header.sect_off);
13312 }
13313 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13314 {
13315 complaint (_("No DW_FORM_block* DW_AT_location for "
13316 "DW_TAG_call_site child DIE %s [in module %s]"),
13317 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13318 continue;
13319 }
13320 else
13321 {
13322 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13323 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13324 if (parameter->u.dwarf_reg != -1)
13325 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13326 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13327 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13328 &parameter->u.fb_offset))
13329 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13330 else
13331 {
13332 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13333 "for DW_FORM_block* DW_AT_location is supported for "
13334 "DW_TAG_call_site child DIE %s "
13335 "[in module %s]"),
13336 sect_offset_str (child_die->sect_off),
13337 objfile_name (objfile));
13338 continue;
13339 }
13340 }
13341
13342 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13343 if (attr == NULL)
13344 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13345 if (attr == NULL || !attr->form_is_block ())
13346 {
13347 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13348 "DW_TAG_call_site child DIE %s [in module %s]"),
13349 sect_offset_str (child_die->sect_off),
13350 objfile_name (objfile));
13351 continue;
13352 }
13353 parameter->value = DW_BLOCK (attr)->data;
13354 parameter->value_size = DW_BLOCK (attr)->size;
13355
13356 /* Parameters are not pre-cleared by memset above. */
13357 parameter->data_value = NULL;
13358 parameter->data_value_size = 0;
13359 call_site->parameter_count++;
13360
13361 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13362 if (attr == NULL)
13363 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13364 if (attr != nullptr)
13365 {
13366 if (!attr->form_is_block ())
13367 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13368 "DW_TAG_call_site child DIE %s [in module %s]"),
13369 sect_offset_str (child_die->sect_off),
13370 objfile_name (objfile));
13371 else
13372 {
13373 parameter->data_value = DW_BLOCK (attr)->data;
13374 parameter->data_value_size = DW_BLOCK (attr)->size;
13375 }
13376 }
13377 }
13378 }
13379
13380 /* Helper function for read_variable. If DIE represents a virtual
13381 table, then return the type of the concrete object that is
13382 associated with the virtual table. Otherwise, return NULL. */
13383
13384 static struct type *
13385 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13386 {
13387 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13388 if (attr == NULL)
13389 return NULL;
13390
13391 /* Find the type DIE. */
13392 struct die_info *type_die = NULL;
13393 struct dwarf2_cu *type_cu = cu;
13394
13395 if (attr->form_is_ref ())
13396 type_die = follow_die_ref (die, attr, &type_cu);
13397 if (type_die == NULL)
13398 return NULL;
13399
13400 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13401 return NULL;
13402 return die_containing_type (type_die, type_cu);
13403 }
13404
13405 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13406
13407 static void
13408 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13409 {
13410 struct rust_vtable_symbol *storage = NULL;
13411
13412 if (cu->language == language_rust)
13413 {
13414 struct type *containing_type = rust_containing_type (die, cu);
13415
13416 if (containing_type != NULL)
13417 {
13418 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13419
13420 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13421 initialize_objfile_symbol (storage);
13422 storage->concrete_type = containing_type;
13423 storage->subclass = SYMBOL_RUST_VTABLE;
13424 }
13425 }
13426
13427 struct symbol *res = new_symbol (die, NULL, cu, storage);
13428 struct attribute *abstract_origin
13429 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13430 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13431 if (res == NULL && loc && abstract_origin)
13432 {
13433 /* We have a variable without a name, but with a location and an abstract
13434 origin. This may be a concrete instance of an abstract variable
13435 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13436 later. */
13437 struct dwarf2_cu *origin_cu = cu;
13438 struct die_info *origin_die
13439 = follow_die_ref (die, abstract_origin, &origin_cu);
13440 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13441 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13442 }
13443 }
13444
13445 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13446 reading .debug_rnglists.
13447 Callback's type should be:
13448 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13449 Return true if the attributes are present and valid, otherwise,
13450 return false. */
13451
13452 template <typename Callback>
13453 static bool
13454 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13455 Callback &&callback)
13456 {
13457 struct dwarf2_per_objfile *dwarf2_per_objfile
13458 = cu->per_cu->dwarf2_per_objfile;
13459 struct objfile *objfile = dwarf2_per_objfile->objfile;
13460 bfd *obfd = objfile->obfd;
13461 /* Base address selection entry. */
13462 CORE_ADDR base;
13463 int found_base;
13464 const gdb_byte *buffer;
13465 CORE_ADDR baseaddr;
13466 bool overflow = false;
13467
13468 found_base = cu->base_known;
13469 base = cu->base_address;
13470
13471 dwarf2_per_objfile->rnglists.read (objfile);
13472 if (offset >= dwarf2_per_objfile->rnglists.size)
13473 {
13474 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13475 offset);
13476 return false;
13477 }
13478 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13479
13480 baseaddr = objfile->text_section_offset ();
13481
13482 while (1)
13483 {
13484 /* Initialize it due to a false compiler warning. */
13485 CORE_ADDR range_beginning = 0, range_end = 0;
13486 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13487 + dwarf2_per_objfile->rnglists.size);
13488 unsigned int bytes_read;
13489
13490 if (buffer == buf_end)
13491 {
13492 overflow = true;
13493 break;
13494 }
13495 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13496 switch (rlet)
13497 {
13498 case DW_RLE_end_of_list:
13499 break;
13500 case DW_RLE_base_address:
13501 if (buffer + cu->header.addr_size > buf_end)
13502 {
13503 overflow = true;
13504 break;
13505 }
13506 base = cu->header.read_address (obfd, buffer, &bytes_read);
13507 found_base = 1;
13508 buffer += bytes_read;
13509 break;
13510 case DW_RLE_start_length:
13511 if (buffer + cu->header.addr_size > buf_end)
13512 {
13513 overflow = true;
13514 break;
13515 }
13516 range_beginning = cu->header.read_address (obfd, buffer,
13517 &bytes_read);
13518 buffer += bytes_read;
13519 range_end = (range_beginning
13520 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13521 buffer += bytes_read;
13522 if (buffer > buf_end)
13523 {
13524 overflow = true;
13525 break;
13526 }
13527 break;
13528 case DW_RLE_offset_pair:
13529 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13530 buffer += bytes_read;
13531 if (buffer > buf_end)
13532 {
13533 overflow = true;
13534 break;
13535 }
13536 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13537 buffer += bytes_read;
13538 if (buffer > buf_end)
13539 {
13540 overflow = true;
13541 break;
13542 }
13543 break;
13544 case DW_RLE_start_end:
13545 if (buffer + 2 * cu->header.addr_size > buf_end)
13546 {
13547 overflow = true;
13548 break;
13549 }
13550 range_beginning = cu->header.read_address (obfd, buffer,
13551 &bytes_read);
13552 buffer += bytes_read;
13553 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13554 buffer += bytes_read;
13555 break;
13556 default:
13557 complaint (_("Invalid .debug_rnglists data (no base address)"));
13558 return false;
13559 }
13560 if (rlet == DW_RLE_end_of_list || overflow)
13561 break;
13562 if (rlet == DW_RLE_base_address)
13563 continue;
13564
13565 if (!found_base)
13566 {
13567 /* We have no valid base address for the ranges
13568 data. */
13569 complaint (_("Invalid .debug_rnglists data (no base address)"));
13570 return false;
13571 }
13572
13573 if (range_beginning > range_end)
13574 {
13575 /* Inverted range entries are invalid. */
13576 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13577 return false;
13578 }
13579
13580 /* Empty range entries have no effect. */
13581 if (range_beginning == range_end)
13582 continue;
13583
13584 range_beginning += base;
13585 range_end += base;
13586
13587 /* A not-uncommon case of bad debug info.
13588 Don't pollute the addrmap with bad data. */
13589 if (range_beginning + baseaddr == 0
13590 && !dwarf2_per_objfile->has_section_at_zero)
13591 {
13592 complaint (_(".debug_rnglists entry has start address of zero"
13593 " [in module %s]"), objfile_name (objfile));
13594 continue;
13595 }
13596
13597 callback (range_beginning, range_end);
13598 }
13599
13600 if (overflow)
13601 {
13602 complaint (_("Offset %d is not terminated "
13603 "for DW_AT_ranges attribute"),
13604 offset);
13605 return false;
13606 }
13607
13608 return true;
13609 }
13610
13611 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13612 Callback's type should be:
13613 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13614 Return 1 if the attributes are present and valid, otherwise, return 0. */
13615
13616 template <typename Callback>
13617 static int
13618 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13619 Callback &&callback)
13620 {
13621 struct dwarf2_per_objfile *dwarf2_per_objfile
13622 = cu->per_cu->dwarf2_per_objfile;
13623 struct objfile *objfile = dwarf2_per_objfile->objfile;
13624 struct comp_unit_head *cu_header = &cu->header;
13625 bfd *obfd = objfile->obfd;
13626 unsigned int addr_size = cu_header->addr_size;
13627 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13628 /* Base address selection entry. */
13629 CORE_ADDR base;
13630 int found_base;
13631 unsigned int dummy;
13632 const gdb_byte *buffer;
13633 CORE_ADDR baseaddr;
13634
13635 if (cu_header->version >= 5)
13636 return dwarf2_rnglists_process (offset, cu, callback);
13637
13638 found_base = cu->base_known;
13639 base = cu->base_address;
13640
13641 dwarf2_per_objfile->ranges.read (objfile);
13642 if (offset >= dwarf2_per_objfile->ranges.size)
13643 {
13644 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13645 offset);
13646 return 0;
13647 }
13648 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13649
13650 baseaddr = objfile->text_section_offset ();
13651
13652 while (1)
13653 {
13654 CORE_ADDR range_beginning, range_end;
13655
13656 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13657 buffer += addr_size;
13658 range_end = cu->header.read_address (obfd, buffer, &dummy);
13659 buffer += addr_size;
13660 offset += 2 * addr_size;
13661
13662 /* An end of list marker is a pair of zero addresses. */
13663 if (range_beginning == 0 && range_end == 0)
13664 /* Found the end of list entry. */
13665 break;
13666
13667 /* Each base address selection entry is a pair of 2 values.
13668 The first is the largest possible address, the second is
13669 the base address. Check for a base address here. */
13670 if ((range_beginning & mask) == mask)
13671 {
13672 /* If we found the largest possible address, then we already
13673 have the base address in range_end. */
13674 base = range_end;
13675 found_base = 1;
13676 continue;
13677 }
13678
13679 if (!found_base)
13680 {
13681 /* We have no valid base address for the ranges
13682 data. */
13683 complaint (_("Invalid .debug_ranges data (no base address)"));
13684 return 0;
13685 }
13686
13687 if (range_beginning > range_end)
13688 {
13689 /* Inverted range entries are invalid. */
13690 complaint (_("Invalid .debug_ranges data (inverted range)"));
13691 return 0;
13692 }
13693
13694 /* Empty range entries have no effect. */
13695 if (range_beginning == range_end)
13696 continue;
13697
13698 range_beginning += base;
13699 range_end += base;
13700
13701 /* A not-uncommon case of bad debug info.
13702 Don't pollute the addrmap with bad data. */
13703 if (range_beginning + baseaddr == 0
13704 && !dwarf2_per_objfile->has_section_at_zero)
13705 {
13706 complaint (_(".debug_ranges entry has start address of zero"
13707 " [in module %s]"), objfile_name (objfile));
13708 continue;
13709 }
13710
13711 callback (range_beginning, range_end);
13712 }
13713
13714 return 1;
13715 }
13716
13717 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13718 Return 1 if the attributes are present and valid, otherwise, return 0.
13719 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13720
13721 static int
13722 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13723 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13724 dwarf2_psymtab *ranges_pst)
13725 {
13726 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13727 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13728 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13729 int low_set = 0;
13730 CORE_ADDR low = 0;
13731 CORE_ADDR high = 0;
13732 int retval;
13733
13734 retval = dwarf2_ranges_process (offset, cu,
13735 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13736 {
13737 if (ranges_pst != NULL)
13738 {
13739 CORE_ADDR lowpc;
13740 CORE_ADDR highpc;
13741
13742 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13743 range_beginning + baseaddr)
13744 - baseaddr);
13745 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13746 range_end + baseaddr)
13747 - baseaddr);
13748 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13749 lowpc, highpc - 1, ranges_pst);
13750 }
13751
13752 /* FIXME: This is recording everything as a low-high
13753 segment of consecutive addresses. We should have a
13754 data structure for discontiguous block ranges
13755 instead. */
13756 if (! low_set)
13757 {
13758 low = range_beginning;
13759 high = range_end;
13760 low_set = 1;
13761 }
13762 else
13763 {
13764 if (range_beginning < low)
13765 low = range_beginning;
13766 if (range_end > high)
13767 high = range_end;
13768 }
13769 });
13770 if (!retval)
13771 return 0;
13772
13773 if (! low_set)
13774 /* If the first entry is an end-of-list marker, the range
13775 describes an empty scope, i.e. no instructions. */
13776 return 0;
13777
13778 if (low_return)
13779 *low_return = low;
13780 if (high_return)
13781 *high_return = high;
13782 return 1;
13783 }
13784
13785 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13786 definition for the return value. *LOWPC and *HIGHPC are set iff
13787 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13788
13789 static enum pc_bounds_kind
13790 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13791 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13792 dwarf2_psymtab *pst)
13793 {
13794 struct dwarf2_per_objfile *dwarf2_per_objfile
13795 = cu->per_cu->dwarf2_per_objfile;
13796 struct attribute *attr;
13797 struct attribute *attr_high;
13798 CORE_ADDR low = 0;
13799 CORE_ADDR high = 0;
13800 enum pc_bounds_kind ret;
13801
13802 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13803 if (attr_high)
13804 {
13805 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13806 if (attr != nullptr)
13807 {
13808 low = attr->value_as_address ();
13809 high = attr_high->value_as_address ();
13810 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13811 high += low;
13812 }
13813 else
13814 /* Found high w/o low attribute. */
13815 return PC_BOUNDS_INVALID;
13816
13817 /* Found consecutive range of addresses. */
13818 ret = PC_BOUNDS_HIGH_LOW;
13819 }
13820 else
13821 {
13822 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13823 if (attr != NULL)
13824 {
13825 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13826 We take advantage of the fact that DW_AT_ranges does not appear
13827 in DW_TAG_compile_unit of DWO files. */
13828 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13829 unsigned int ranges_offset = (DW_UNSND (attr)
13830 + (need_ranges_base
13831 ? cu->ranges_base
13832 : 0));
13833
13834 /* Value of the DW_AT_ranges attribute is the offset in the
13835 .debug_ranges section. */
13836 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13837 return PC_BOUNDS_INVALID;
13838 /* Found discontinuous range of addresses. */
13839 ret = PC_BOUNDS_RANGES;
13840 }
13841 else
13842 return PC_BOUNDS_NOT_PRESENT;
13843 }
13844
13845 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13846 if (high <= low)
13847 return PC_BOUNDS_INVALID;
13848
13849 /* When using the GNU linker, .gnu.linkonce. sections are used to
13850 eliminate duplicate copies of functions and vtables and such.
13851 The linker will arbitrarily choose one and discard the others.
13852 The AT_*_pc values for such functions refer to local labels in
13853 these sections. If the section from that file was discarded, the
13854 labels are not in the output, so the relocs get a value of 0.
13855 If this is a discarded function, mark the pc bounds as invalid,
13856 so that GDB will ignore it. */
13857 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13858 return PC_BOUNDS_INVALID;
13859
13860 *lowpc = low;
13861 if (highpc)
13862 *highpc = high;
13863 return ret;
13864 }
13865
13866 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13867 its low and high PC addresses. Do nothing if these addresses could not
13868 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13869 and HIGHPC to the high address if greater than HIGHPC. */
13870
13871 static void
13872 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13873 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13874 struct dwarf2_cu *cu)
13875 {
13876 CORE_ADDR low, high;
13877 struct die_info *child = die->child;
13878
13879 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13880 {
13881 *lowpc = std::min (*lowpc, low);
13882 *highpc = std::max (*highpc, high);
13883 }
13884
13885 /* If the language does not allow nested subprograms (either inside
13886 subprograms or lexical blocks), we're done. */
13887 if (cu->language != language_ada)
13888 return;
13889
13890 /* Check all the children of the given DIE. If it contains nested
13891 subprograms, then check their pc bounds. Likewise, we need to
13892 check lexical blocks as well, as they may also contain subprogram
13893 definitions. */
13894 while (child && child->tag)
13895 {
13896 if (child->tag == DW_TAG_subprogram
13897 || child->tag == DW_TAG_lexical_block)
13898 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13899 child = sibling_die (child);
13900 }
13901 }
13902
13903 /* Get the low and high pc's represented by the scope DIE, and store
13904 them in *LOWPC and *HIGHPC. If the correct values can't be
13905 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13906
13907 static void
13908 get_scope_pc_bounds (struct die_info *die,
13909 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13910 struct dwarf2_cu *cu)
13911 {
13912 CORE_ADDR best_low = (CORE_ADDR) -1;
13913 CORE_ADDR best_high = (CORE_ADDR) 0;
13914 CORE_ADDR current_low, current_high;
13915
13916 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13917 >= PC_BOUNDS_RANGES)
13918 {
13919 best_low = current_low;
13920 best_high = current_high;
13921 }
13922 else
13923 {
13924 struct die_info *child = die->child;
13925
13926 while (child && child->tag)
13927 {
13928 switch (child->tag) {
13929 case DW_TAG_subprogram:
13930 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13931 break;
13932 case DW_TAG_namespace:
13933 case DW_TAG_module:
13934 /* FIXME: carlton/2004-01-16: Should we do this for
13935 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13936 that current GCC's always emit the DIEs corresponding
13937 to definitions of methods of classes as children of a
13938 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13939 the DIEs giving the declarations, which could be
13940 anywhere). But I don't see any reason why the
13941 standards says that they have to be there. */
13942 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13943
13944 if (current_low != ((CORE_ADDR) -1))
13945 {
13946 best_low = std::min (best_low, current_low);
13947 best_high = std::max (best_high, current_high);
13948 }
13949 break;
13950 default:
13951 /* Ignore. */
13952 break;
13953 }
13954
13955 child = sibling_die (child);
13956 }
13957 }
13958
13959 *lowpc = best_low;
13960 *highpc = best_high;
13961 }
13962
13963 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13964 in DIE. */
13965
13966 static void
13967 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13968 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13969 {
13970 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13971 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13972 struct attribute *attr;
13973 struct attribute *attr_high;
13974
13975 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13976 if (attr_high)
13977 {
13978 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13979 if (attr != nullptr)
13980 {
13981 CORE_ADDR low = attr->value_as_address ();
13982 CORE_ADDR high = attr_high->value_as_address ();
13983
13984 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13985 high += low;
13986
13987 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13988 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13989 cu->get_builder ()->record_block_range (block, low, high - 1);
13990 }
13991 }
13992
13993 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13994 if (attr != nullptr)
13995 {
13996 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13997 We take advantage of the fact that DW_AT_ranges does not appear
13998 in DW_TAG_compile_unit of DWO files. */
13999 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14000
14001 /* The value of the DW_AT_ranges attribute is the offset of the
14002 address range list in the .debug_ranges section. */
14003 unsigned long offset = (DW_UNSND (attr)
14004 + (need_ranges_base ? cu->ranges_base : 0));
14005
14006 std::vector<blockrange> blockvec;
14007 dwarf2_ranges_process (offset, cu,
14008 [&] (CORE_ADDR start, CORE_ADDR end)
14009 {
14010 start += baseaddr;
14011 end += baseaddr;
14012 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14013 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14014 cu->get_builder ()->record_block_range (block, start, end - 1);
14015 blockvec.emplace_back (start, end);
14016 });
14017
14018 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14019 }
14020 }
14021
14022 /* Check whether the producer field indicates either of GCC < 4.6, or the
14023 Intel C/C++ compiler, and cache the result in CU. */
14024
14025 static void
14026 check_producer (struct dwarf2_cu *cu)
14027 {
14028 int major, minor;
14029
14030 if (cu->producer == NULL)
14031 {
14032 /* For unknown compilers expect their behavior is DWARF version
14033 compliant.
14034
14035 GCC started to support .debug_types sections by -gdwarf-4 since
14036 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14037 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14038 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14039 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14040 }
14041 else if (producer_is_gcc (cu->producer, &major, &minor))
14042 {
14043 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14044 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14045 }
14046 else if (producer_is_icc (cu->producer, &major, &minor))
14047 {
14048 cu->producer_is_icc = true;
14049 cu->producer_is_icc_lt_14 = major < 14;
14050 }
14051 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14052 cu->producer_is_codewarrior = true;
14053 else
14054 {
14055 /* For other non-GCC compilers, expect their behavior is DWARF version
14056 compliant. */
14057 }
14058
14059 cu->checked_producer = true;
14060 }
14061
14062 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14063 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14064 during 4.6.0 experimental. */
14065
14066 static bool
14067 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14068 {
14069 if (!cu->checked_producer)
14070 check_producer (cu);
14071
14072 return cu->producer_is_gxx_lt_4_6;
14073 }
14074
14075
14076 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14077 with incorrect is_stmt attributes. */
14078
14079 static bool
14080 producer_is_codewarrior (struct dwarf2_cu *cu)
14081 {
14082 if (!cu->checked_producer)
14083 check_producer (cu);
14084
14085 return cu->producer_is_codewarrior;
14086 }
14087
14088 /* Return the default accessibility type if it is not overridden by
14089 DW_AT_accessibility. */
14090
14091 static enum dwarf_access_attribute
14092 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14093 {
14094 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14095 {
14096 /* The default DWARF 2 accessibility for members is public, the default
14097 accessibility for inheritance is private. */
14098
14099 if (die->tag != DW_TAG_inheritance)
14100 return DW_ACCESS_public;
14101 else
14102 return DW_ACCESS_private;
14103 }
14104 else
14105 {
14106 /* DWARF 3+ defines the default accessibility a different way. The same
14107 rules apply now for DW_TAG_inheritance as for the members and it only
14108 depends on the container kind. */
14109
14110 if (die->parent->tag == DW_TAG_class_type)
14111 return DW_ACCESS_private;
14112 else
14113 return DW_ACCESS_public;
14114 }
14115 }
14116
14117 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14118 offset. If the attribute was not found return 0, otherwise return
14119 1. If it was found but could not properly be handled, set *OFFSET
14120 to 0. */
14121
14122 static int
14123 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14124 LONGEST *offset)
14125 {
14126 struct attribute *attr;
14127
14128 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14129 if (attr != NULL)
14130 {
14131 *offset = 0;
14132
14133 /* Note that we do not check for a section offset first here.
14134 This is because DW_AT_data_member_location is new in DWARF 4,
14135 so if we see it, we can assume that a constant form is really
14136 a constant and not a section offset. */
14137 if (attr->form_is_constant ())
14138 *offset = dwarf2_get_attr_constant_value (attr, 0);
14139 else if (attr->form_is_section_offset ())
14140 dwarf2_complex_location_expr_complaint ();
14141 else if (attr->form_is_block ())
14142 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14143 else
14144 dwarf2_complex_location_expr_complaint ();
14145
14146 return 1;
14147 }
14148
14149 return 0;
14150 }
14151
14152 /* Add an aggregate field to the field list. */
14153
14154 static void
14155 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14156 struct dwarf2_cu *cu)
14157 {
14158 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14159 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14160 struct nextfield *new_field;
14161 struct attribute *attr;
14162 struct field *fp;
14163 const char *fieldname = "";
14164
14165 if (die->tag == DW_TAG_inheritance)
14166 {
14167 fip->baseclasses.emplace_back ();
14168 new_field = &fip->baseclasses.back ();
14169 }
14170 else
14171 {
14172 fip->fields.emplace_back ();
14173 new_field = &fip->fields.back ();
14174 }
14175
14176 fip->nfields++;
14177
14178 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14179 if (attr != nullptr)
14180 new_field->accessibility = DW_UNSND (attr);
14181 else
14182 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14183 if (new_field->accessibility != DW_ACCESS_public)
14184 fip->non_public_fields = 1;
14185
14186 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14187 if (attr != nullptr)
14188 new_field->virtuality = DW_UNSND (attr);
14189 else
14190 new_field->virtuality = DW_VIRTUALITY_none;
14191
14192 fp = &new_field->field;
14193
14194 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14195 {
14196 LONGEST offset;
14197
14198 /* Data member other than a C++ static data member. */
14199
14200 /* Get type of field. */
14201 fp->type = die_type (die, cu);
14202
14203 SET_FIELD_BITPOS (*fp, 0);
14204
14205 /* Get bit size of field (zero if none). */
14206 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14207 if (attr != nullptr)
14208 {
14209 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14210 }
14211 else
14212 {
14213 FIELD_BITSIZE (*fp) = 0;
14214 }
14215
14216 /* Get bit offset of field. */
14217 if (handle_data_member_location (die, cu, &offset))
14218 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14219 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14220 if (attr != nullptr)
14221 {
14222 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14223 {
14224 /* For big endian bits, the DW_AT_bit_offset gives the
14225 additional bit offset from the MSB of the containing
14226 anonymous object to the MSB of the field. We don't
14227 have to do anything special since we don't need to
14228 know the size of the anonymous object. */
14229 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14230 }
14231 else
14232 {
14233 /* For little endian bits, compute the bit offset to the
14234 MSB of the anonymous object, subtract off the number of
14235 bits from the MSB of the field to the MSB of the
14236 object, and then subtract off the number of bits of
14237 the field itself. The result is the bit offset of
14238 the LSB of the field. */
14239 int anonymous_size;
14240 int bit_offset = DW_UNSND (attr);
14241
14242 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14243 if (attr != nullptr)
14244 {
14245 /* The size of the anonymous object containing
14246 the bit field is explicit, so use the
14247 indicated size (in bytes). */
14248 anonymous_size = DW_UNSND (attr);
14249 }
14250 else
14251 {
14252 /* The size of the anonymous object containing
14253 the bit field must be inferred from the type
14254 attribute of the data member containing the
14255 bit field. */
14256 anonymous_size = TYPE_LENGTH (fp->type);
14257 }
14258 SET_FIELD_BITPOS (*fp,
14259 (FIELD_BITPOS (*fp)
14260 + anonymous_size * bits_per_byte
14261 - bit_offset - FIELD_BITSIZE (*fp)));
14262 }
14263 }
14264 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14265 if (attr != NULL)
14266 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14267 + dwarf2_get_attr_constant_value (attr, 0)));
14268
14269 /* Get name of field. */
14270 fieldname = dwarf2_name (die, cu);
14271 if (fieldname == NULL)
14272 fieldname = "";
14273
14274 /* The name is already allocated along with this objfile, so we don't
14275 need to duplicate it for the type. */
14276 fp->name = fieldname;
14277
14278 /* Change accessibility for artificial fields (e.g. virtual table
14279 pointer or virtual base class pointer) to private. */
14280 if (dwarf2_attr (die, DW_AT_artificial, cu))
14281 {
14282 FIELD_ARTIFICIAL (*fp) = 1;
14283 new_field->accessibility = DW_ACCESS_private;
14284 fip->non_public_fields = 1;
14285 }
14286 }
14287 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14288 {
14289 /* C++ static member. */
14290
14291 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14292 is a declaration, but all versions of G++ as of this writing
14293 (so through at least 3.2.1) incorrectly generate
14294 DW_TAG_variable tags. */
14295
14296 const char *physname;
14297
14298 /* Get name of field. */
14299 fieldname = dwarf2_name (die, cu);
14300 if (fieldname == NULL)
14301 return;
14302
14303 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14304 if (attr
14305 /* Only create a symbol if this is an external value.
14306 new_symbol checks this and puts the value in the global symbol
14307 table, which we want. If it is not external, new_symbol
14308 will try to put the value in cu->list_in_scope which is wrong. */
14309 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14310 {
14311 /* A static const member, not much different than an enum as far as
14312 we're concerned, except that we can support more types. */
14313 new_symbol (die, NULL, cu);
14314 }
14315
14316 /* Get physical name. */
14317 physname = dwarf2_physname (fieldname, die, cu);
14318
14319 /* The name is already allocated along with this objfile, so we don't
14320 need to duplicate it for the type. */
14321 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14322 FIELD_TYPE (*fp) = die_type (die, cu);
14323 FIELD_NAME (*fp) = fieldname;
14324 }
14325 else if (die->tag == DW_TAG_inheritance)
14326 {
14327 LONGEST offset;
14328
14329 /* C++ base class field. */
14330 if (handle_data_member_location (die, cu, &offset))
14331 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14332 FIELD_BITSIZE (*fp) = 0;
14333 FIELD_TYPE (*fp) = die_type (die, cu);
14334 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14335 }
14336 else if (die->tag == DW_TAG_variant_part)
14337 {
14338 /* process_structure_scope will treat this DIE as a union. */
14339 process_structure_scope (die, cu);
14340
14341 /* The variant part is relative to the start of the enclosing
14342 structure. */
14343 SET_FIELD_BITPOS (*fp, 0);
14344 fp->type = get_die_type (die, cu);
14345 fp->artificial = 1;
14346 fp->name = "<<variant>>";
14347
14348 /* Normally a DW_TAG_variant_part won't have a size, but our
14349 representation requires one, so set it to the maximum of the
14350 child sizes, being sure to account for the offset at which
14351 each child is seen. */
14352 if (TYPE_LENGTH (fp->type) == 0)
14353 {
14354 unsigned max = 0;
14355 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14356 {
14357 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14358 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14359 if (len > max)
14360 max = len;
14361 }
14362 TYPE_LENGTH (fp->type) = max;
14363 }
14364 }
14365 else
14366 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14367 }
14368
14369 /* Can the type given by DIE define another type? */
14370
14371 static bool
14372 type_can_define_types (const struct die_info *die)
14373 {
14374 switch (die->tag)
14375 {
14376 case DW_TAG_typedef:
14377 case DW_TAG_class_type:
14378 case DW_TAG_structure_type:
14379 case DW_TAG_union_type:
14380 case DW_TAG_enumeration_type:
14381 return true;
14382
14383 default:
14384 return false;
14385 }
14386 }
14387
14388 /* Add a type definition defined in the scope of the FIP's class. */
14389
14390 static void
14391 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14392 struct dwarf2_cu *cu)
14393 {
14394 struct decl_field fp;
14395 memset (&fp, 0, sizeof (fp));
14396
14397 gdb_assert (type_can_define_types (die));
14398
14399 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14400 fp.name = dwarf2_name (die, cu);
14401 fp.type = read_type_die (die, cu);
14402
14403 /* Save accessibility. */
14404 enum dwarf_access_attribute accessibility;
14405 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14406 if (attr != NULL)
14407 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14408 else
14409 accessibility = dwarf2_default_access_attribute (die, cu);
14410 switch (accessibility)
14411 {
14412 case DW_ACCESS_public:
14413 /* The assumed value if neither private nor protected. */
14414 break;
14415 case DW_ACCESS_private:
14416 fp.is_private = 1;
14417 break;
14418 case DW_ACCESS_protected:
14419 fp.is_protected = 1;
14420 break;
14421 default:
14422 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14423 }
14424
14425 if (die->tag == DW_TAG_typedef)
14426 fip->typedef_field_list.push_back (fp);
14427 else
14428 fip->nested_types_list.push_back (fp);
14429 }
14430
14431 /* Create the vector of fields, and attach it to the type. */
14432
14433 static void
14434 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14435 struct dwarf2_cu *cu)
14436 {
14437 int nfields = fip->nfields;
14438
14439 /* Record the field count, allocate space for the array of fields,
14440 and create blank accessibility bitfields if necessary. */
14441 TYPE_NFIELDS (type) = nfields;
14442 TYPE_FIELDS (type) = (struct field *)
14443 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14444
14445 if (fip->non_public_fields && cu->language != language_ada)
14446 {
14447 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14448
14449 TYPE_FIELD_PRIVATE_BITS (type) =
14450 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14451 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14452
14453 TYPE_FIELD_PROTECTED_BITS (type) =
14454 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14455 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14456
14457 TYPE_FIELD_IGNORE_BITS (type) =
14458 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14459 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14460 }
14461
14462 /* If the type has baseclasses, allocate and clear a bit vector for
14463 TYPE_FIELD_VIRTUAL_BITS. */
14464 if (!fip->baseclasses.empty () && cu->language != language_ada)
14465 {
14466 int num_bytes = B_BYTES (fip->baseclasses.size ());
14467 unsigned char *pointer;
14468
14469 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14470 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14471 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14472 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14473 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14474 }
14475
14476 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14477 {
14478 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14479
14480 for (int index = 0; index < nfields; ++index)
14481 {
14482 struct nextfield &field = fip->fields[index];
14483
14484 if (field.variant.is_discriminant)
14485 di->discriminant_index = index;
14486 else if (field.variant.default_branch)
14487 di->default_index = index;
14488 else
14489 di->discriminants[index] = field.variant.discriminant_value;
14490 }
14491 }
14492
14493 /* Copy the saved-up fields into the field vector. */
14494 for (int i = 0; i < nfields; ++i)
14495 {
14496 struct nextfield &field
14497 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14498 : fip->fields[i - fip->baseclasses.size ()]);
14499
14500 TYPE_FIELD (type, i) = field.field;
14501 switch (field.accessibility)
14502 {
14503 case DW_ACCESS_private:
14504 if (cu->language != language_ada)
14505 SET_TYPE_FIELD_PRIVATE (type, i);
14506 break;
14507
14508 case DW_ACCESS_protected:
14509 if (cu->language != language_ada)
14510 SET_TYPE_FIELD_PROTECTED (type, i);
14511 break;
14512
14513 case DW_ACCESS_public:
14514 break;
14515
14516 default:
14517 /* Unknown accessibility. Complain and treat it as public. */
14518 {
14519 complaint (_("unsupported accessibility %d"),
14520 field.accessibility);
14521 }
14522 break;
14523 }
14524 if (i < fip->baseclasses.size ())
14525 {
14526 switch (field.virtuality)
14527 {
14528 case DW_VIRTUALITY_virtual:
14529 case DW_VIRTUALITY_pure_virtual:
14530 if (cu->language == language_ada)
14531 error (_("unexpected virtuality in component of Ada type"));
14532 SET_TYPE_FIELD_VIRTUAL (type, i);
14533 break;
14534 }
14535 }
14536 }
14537 }
14538
14539 /* Return true if this member function is a constructor, false
14540 otherwise. */
14541
14542 static int
14543 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14544 {
14545 const char *fieldname;
14546 const char *type_name;
14547 int len;
14548
14549 if (die->parent == NULL)
14550 return 0;
14551
14552 if (die->parent->tag != DW_TAG_structure_type
14553 && die->parent->tag != DW_TAG_union_type
14554 && die->parent->tag != DW_TAG_class_type)
14555 return 0;
14556
14557 fieldname = dwarf2_name (die, cu);
14558 type_name = dwarf2_name (die->parent, cu);
14559 if (fieldname == NULL || type_name == NULL)
14560 return 0;
14561
14562 len = strlen (fieldname);
14563 return (strncmp (fieldname, type_name, len) == 0
14564 && (type_name[len] == '\0' || type_name[len] == '<'));
14565 }
14566
14567 /* Check if the given VALUE is a recognized enum
14568 dwarf_defaulted_attribute constant according to DWARF5 spec,
14569 Table 7.24. */
14570
14571 static bool
14572 is_valid_DW_AT_defaulted (ULONGEST value)
14573 {
14574 switch (value)
14575 {
14576 case DW_DEFAULTED_no:
14577 case DW_DEFAULTED_in_class:
14578 case DW_DEFAULTED_out_of_class:
14579 return true;
14580 }
14581
14582 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14583 return false;
14584 }
14585
14586 /* Add a member function to the proper fieldlist. */
14587
14588 static void
14589 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14590 struct type *type, struct dwarf2_cu *cu)
14591 {
14592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14593 struct attribute *attr;
14594 int i;
14595 struct fnfieldlist *flp = nullptr;
14596 struct fn_field *fnp;
14597 const char *fieldname;
14598 struct type *this_type;
14599 enum dwarf_access_attribute accessibility;
14600
14601 if (cu->language == language_ada)
14602 error (_("unexpected member function in Ada type"));
14603
14604 /* Get name of member function. */
14605 fieldname = dwarf2_name (die, cu);
14606 if (fieldname == NULL)
14607 return;
14608
14609 /* Look up member function name in fieldlist. */
14610 for (i = 0; i < fip->fnfieldlists.size (); i++)
14611 {
14612 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14613 {
14614 flp = &fip->fnfieldlists[i];
14615 break;
14616 }
14617 }
14618
14619 /* Create a new fnfieldlist if necessary. */
14620 if (flp == nullptr)
14621 {
14622 fip->fnfieldlists.emplace_back ();
14623 flp = &fip->fnfieldlists.back ();
14624 flp->name = fieldname;
14625 i = fip->fnfieldlists.size () - 1;
14626 }
14627
14628 /* Create a new member function field and add it to the vector of
14629 fnfieldlists. */
14630 flp->fnfields.emplace_back ();
14631 fnp = &flp->fnfields.back ();
14632
14633 /* Delay processing of the physname until later. */
14634 if (cu->language == language_cplus)
14635 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14636 die, cu);
14637 else
14638 {
14639 const char *physname = dwarf2_physname (fieldname, die, cu);
14640 fnp->physname = physname ? physname : "";
14641 }
14642
14643 fnp->type = alloc_type (objfile);
14644 this_type = read_type_die (die, cu);
14645 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14646 {
14647 int nparams = TYPE_NFIELDS (this_type);
14648
14649 /* TYPE is the domain of this method, and THIS_TYPE is the type
14650 of the method itself (TYPE_CODE_METHOD). */
14651 smash_to_method_type (fnp->type, type,
14652 TYPE_TARGET_TYPE (this_type),
14653 TYPE_FIELDS (this_type),
14654 TYPE_NFIELDS (this_type),
14655 TYPE_VARARGS (this_type));
14656
14657 /* Handle static member functions.
14658 Dwarf2 has no clean way to discern C++ static and non-static
14659 member functions. G++ helps GDB by marking the first
14660 parameter for non-static member functions (which is the this
14661 pointer) as artificial. We obtain this information from
14662 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14663 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14664 fnp->voffset = VOFFSET_STATIC;
14665 }
14666 else
14667 complaint (_("member function type missing for '%s'"),
14668 dwarf2_full_name (fieldname, die, cu));
14669
14670 /* Get fcontext from DW_AT_containing_type if present. */
14671 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14672 fnp->fcontext = die_containing_type (die, cu);
14673
14674 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14675 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14676
14677 /* Get accessibility. */
14678 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14679 if (attr != nullptr)
14680 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14681 else
14682 accessibility = dwarf2_default_access_attribute (die, cu);
14683 switch (accessibility)
14684 {
14685 case DW_ACCESS_private:
14686 fnp->is_private = 1;
14687 break;
14688 case DW_ACCESS_protected:
14689 fnp->is_protected = 1;
14690 break;
14691 }
14692
14693 /* Check for artificial methods. */
14694 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14695 if (attr && DW_UNSND (attr) != 0)
14696 fnp->is_artificial = 1;
14697
14698 /* Check for defaulted methods. */
14699 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14700 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14701 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14702
14703 /* Check for deleted methods. */
14704 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14705 if (attr != nullptr && DW_UNSND (attr) != 0)
14706 fnp->is_deleted = 1;
14707
14708 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14709
14710 /* Get index in virtual function table if it is a virtual member
14711 function. For older versions of GCC, this is an offset in the
14712 appropriate virtual table, as specified by DW_AT_containing_type.
14713 For everyone else, it is an expression to be evaluated relative
14714 to the object address. */
14715
14716 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14717 if (attr != nullptr)
14718 {
14719 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14720 {
14721 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14722 {
14723 /* Old-style GCC. */
14724 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14725 }
14726 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14727 || (DW_BLOCK (attr)->size > 1
14728 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14729 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14730 {
14731 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14732 if ((fnp->voffset % cu->header.addr_size) != 0)
14733 dwarf2_complex_location_expr_complaint ();
14734 else
14735 fnp->voffset /= cu->header.addr_size;
14736 fnp->voffset += 2;
14737 }
14738 else
14739 dwarf2_complex_location_expr_complaint ();
14740
14741 if (!fnp->fcontext)
14742 {
14743 /* If there is no `this' field and no DW_AT_containing_type,
14744 we cannot actually find a base class context for the
14745 vtable! */
14746 if (TYPE_NFIELDS (this_type) == 0
14747 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14748 {
14749 complaint (_("cannot determine context for virtual member "
14750 "function \"%s\" (offset %s)"),
14751 fieldname, sect_offset_str (die->sect_off));
14752 }
14753 else
14754 {
14755 fnp->fcontext
14756 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14757 }
14758 }
14759 }
14760 else if (attr->form_is_section_offset ())
14761 {
14762 dwarf2_complex_location_expr_complaint ();
14763 }
14764 else
14765 {
14766 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14767 fieldname);
14768 }
14769 }
14770 else
14771 {
14772 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14773 if (attr && DW_UNSND (attr))
14774 {
14775 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14776 complaint (_("Member function \"%s\" (offset %s) is virtual "
14777 "but the vtable offset is not specified"),
14778 fieldname, sect_offset_str (die->sect_off));
14779 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14780 TYPE_CPLUS_DYNAMIC (type) = 1;
14781 }
14782 }
14783 }
14784
14785 /* Create the vector of member function fields, and attach it to the type. */
14786
14787 static void
14788 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14789 struct dwarf2_cu *cu)
14790 {
14791 if (cu->language == language_ada)
14792 error (_("unexpected member functions in Ada type"));
14793
14794 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14795 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14796 TYPE_ALLOC (type,
14797 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14798
14799 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14800 {
14801 struct fnfieldlist &nf = fip->fnfieldlists[i];
14802 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14803
14804 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14805 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14806 fn_flp->fn_fields = (struct fn_field *)
14807 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14808
14809 for (int k = 0; k < nf.fnfields.size (); ++k)
14810 fn_flp->fn_fields[k] = nf.fnfields[k];
14811 }
14812
14813 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14814 }
14815
14816 /* Returns non-zero if NAME is the name of a vtable member in CU's
14817 language, zero otherwise. */
14818 static int
14819 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14820 {
14821 static const char vptr[] = "_vptr";
14822
14823 /* Look for the C++ form of the vtable. */
14824 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14825 return 1;
14826
14827 return 0;
14828 }
14829
14830 /* GCC outputs unnamed structures that are really pointers to member
14831 functions, with the ABI-specified layout. If TYPE describes
14832 such a structure, smash it into a member function type.
14833
14834 GCC shouldn't do this; it should just output pointer to member DIEs.
14835 This is GCC PR debug/28767. */
14836
14837 static void
14838 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14839 {
14840 struct type *pfn_type, *self_type, *new_type;
14841
14842 /* Check for a structure with no name and two children. */
14843 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14844 return;
14845
14846 /* Check for __pfn and __delta members. */
14847 if (TYPE_FIELD_NAME (type, 0) == NULL
14848 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14849 || TYPE_FIELD_NAME (type, 1) == NULL
14850 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14851 return;
14852
14853 /* Find the type of the method. */
14854 pfn_type = TYPE_FIELD_TYPE (type, 0);
14855 if (pfn_type == NULL
14856 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14857 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14858 return;
14859
14860 /* Look for the "this" argument. */
14861 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14862 if (TYPE_NFIELDS (pfn_type) == 0
14863 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14864 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14865 return;
14866
14867 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14868 new_type = alloc_type (objfile);
14869 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14870 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14871 TYPE_VARARGS (pfn_type));
14872 smash_to_methodptr_type (type, new_type);
14873 }
14874
14875 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14876 appropriate error checking and issuing complaints if there is a
14877 problem. */
14878
14879 static ULONGEST
14880 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14881 {
14882 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14883
14884 if (attr == nullptr)
14885 return 0;
14886
14887 if (!attr->form_is_constant ())
14888 {
14889 complaint (_("DW_AT_alignment must have constant form"
14890 " - DIE at %s [in module %s]"),
14891 sect_offset_str (die->sect_off),
14892 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14893 return 0;
14894 }
14895
14896 ULONGEST align;
14897 if (attr->form == DW_FORM_sdata)
14898 {
14899 LONGEST val = DW_SND (attr);
14900 if (val < 0)
14901 {
14902 complaint (_("DW_AT_alignment value must not be negative"
14903 " - DIE at %s [in module %s]"),
14904 sect_offset_str (die->sect_off),
14905 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14906 return 0;
14907 }
14908 align = val;
14909 }
14910 else
14911 align = DW_UNSND (attr);
14912
14913 if (align == 0)
14914 {
14915 complaint (_("DW_AT_alignment value must not be zero"
14916 " - DIE at %s [in module %s]"),
14917 sect_offset_str (die->sect_off),
14918 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14919 return 0;
14920 }
14921 if ((align & (align - 1)) != 0)
14922 {
14923 complaint (_("DW_AT_alignment value must be a power of 2"
14924 " - DIE at %s [in module %s]"),
14925 sect_offset_str (die->sect_off),
14926 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14927 return 0;
14928 }
14929
14930 return align;
14931 }
14932
14933 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14934 the alignment for TYPE. */
14935
14936 static void
14937 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14938 struct type *type)
14939 {
14940 if (!set_type_align (type, get_alignment (cu, die)))
14941 complaint (_("DW_AT_alignment value too large"
14942 " - DIE at %s [in module %s]"),
14943 sect_offset_str (die->sect_off),
14944 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14945 }
14946
14947 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14948 constant for a type, according to DWARF5 spec, Table 5.5. */
14949
14950 static bool
14951 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14952 {
14953 switch (value)
14954 {
14955 case DW_CC_normal:
14956 case DW_CC_pass_by_reference:
14957 case DW_CC_pass_by_value:
14958 return true;
14959
14960 default:
14961 complaint (_("unrecognized DW_AT_calling_convention value "
14962 "(%s) for a type"), pulongest (value));
14963 return false;
14964 }
14965 }
14966
14967 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14968 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14969 also according to GNU-specific values (see include/dwarf2.h). */
14970
14971 static bool
14972 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14973 {
14974 switch (value)
14975 {
14976 case DW_CC_normal:
14977 case DW_CC_program:
14978 case DW_CC_nocall:
14979 return true;
14980
14981 case DW_CC_GNU_renesas_sh:
14982 case DW_CC_GNU_borland_fastcall_i386:
14983 case DW_CC_GDB_IBM_OpenCL:
14984 return true;
14985
14986 default:
14987 complaint (_("unrecognized DW_AT_calling_convention value "
14988 "(%s) for a subroutine"), pulongest (value));
14989 return false;
14990 }
14991 }
14992
14993 /* Called when we find the DIE that starts a structure or union scope
14994 (definition) to create a type for the structure or union. Fill in
14995 the type's name and general properties; the members will not be
14996 processed until process_structure_scope. A symbol table entry for
14997 the type will also not be done until process_structure_scope (assuming
14998 the type has a name).
14999
15000 NOTE: we need to call these functions regardless of whether or not the
15001 DIE has a DW_AT_name attribute, since it might be an anonymous
15002 structure or union. This gets the type entered into our set of
15003 user defined types. */
15004
15005 static struct type *
15006 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15007 {
15008 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15009 struct type *type;
15010 struct attribute *attr;
15011 const char *name;
15012
15013 /* If the definition of this type lives in .debug_types, read that type.
15014 Don't follow DW_AT_specification though, that will take us back up
15015 the chain and we want to go down. */
15016 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15017 if (attr != nullptr)
15018 {
15019 type = get_DW_AT_signature_type (die, attr, cu);
15020
15021 /* The type's CU may not be the same as CU.
15022 Ensure TYPE is recorded with CU in die_type_hash. */
15023 return set_die_type (die, type, cu);
15024 }
15025
15026 type = alloc_type (objfile);
15027 INIT_CPLUS_SPECIFIC (type);
15028
15029 name = dwarf2_name (die, cu);
15030 if (name != NULL)
15031 {
15032 if (cu->language == language_cplus
15033 || cu->language == language_d
15034 || cu->language == language_rust)
15035 {
15036 const char *full_name = dwarf2_full_name (name, die, cu);
15037
15038 /* dwarf2_full_name might have already finished building the DIE's
15039 type. If so, there is no need to continue. */
15040 if (get_die_type (die, cu) != NULL)
15041 return get_die_type (die, cu);
15042
15043 TYPE_NAME (type) = full_name;
15044 }
15045 else
15046 {
15047 /* The name is already allocated along with this objfile, so
15048 we don't need to duplicate it for the type. */
15049 TYPE_NAME (type) = name;
15050 }
15051 }
15052
15053 if (die->tag == DW_TAG_structure_type)
15054 {
15055 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15056 }
15057 else if (die->tag == DW_TAG_union_type)
15058 {
15059 TYPE_CODE (type) = TYPE_CODE_UNION;
15060 }
15061 else if (die->tag == DW_TAG_variant_part)
15062 {
15063 TYPE_CODE (type) = TYPE_CODE_UNION;
15064 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15065 }
15066 else
15067 {
15068 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15069 }
15070
15071 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15072 TYPE_DECLARED_CLASS (type) = 1;
15073
15074 /* Store the calling convention in the type if it's available in
15075 the die. Otherwise the calling convention remains set to
15076 the default value DW_CC_normal. */
15077 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15078 if (attr != nullptr
15079 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15080 {
15081 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15082 TYPE_CPLUS_CALLING_CONVENTION (type)
15083 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15084 }
15085
15086 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15087 if (attr != nullptr)
15088 {
15089 if (attr->form_is_constant ())
15090 TYPE_LENGTH (type) = DW_UNSND (attr);
15091 else
15092 {
15093 /* For the moment, dynamic type sizes are not supported
15094 by GDB's struct type. The actual size is determined
15095 on-demand when resolving the type of a given object,
15096 so set the type's length to zero for now. Otherwise,
15097 we record an expression as the length, and that expression
15098 could lead to a very large value, which could eventually
15099 lead to us trying to allocate that much memory when creating
15100 a value of that type. */
15101 TYPE_LENGTH (type) = 0;
15102 }
15103 }
15104 else
15105 {
15106 TYPE_LENGTH (type) = 0;
15107 }
15108
15109 maybe_set_alignment (cu, die, type);
15110
15111 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15112 {
15113 /* ICC<14 does not output the required DW_AT_declaration on
15114 incomplete types, but gives them a size of zero. */
15115 TYPE_STUB (type) = 1;
15116 }
15117 else
15118 TYPE_STUB_SUPPORTED (type) = 1;
15119
15120 if (die_is_declaration (die, cu))
15121 TYPE_STUB (type) = 1;
15122 else if (attr == NULL && die->child == NULL
15123 && producer_is_realview (cu->producer))
15124 /* RealView does not output the required DW_AT_declaration
15125 on incomplete types. */
15126 TYPE_STUB (type) = 1;
15127
15128 /* We need to add the type field to the die immediately so we don't
15129 infinitely recurse when dealing with pointers to the structure
15130 type within the structure itself. */
15131 set_die_type (die, type, cu);
15132
15133 /* set_die_type should be already done. */
15134 set_descriptive_type (type, die, cu);
15135
15136 return type;
15137 }
15138
15139 /* A helper for process_structure_scope that handles a single member
15140 DIE. */
15141
15142 static void
15143 handle_struct_member_die (struct die_info *child_die, struct type *type,
15144 struct field_info *fi,
15145 std::vector<struct symbol *> *template_args,
15146 struct dwarf2_cu *cu)
15147 {
15148 if (child_die->tag == DW_TAG_member
15149 || child_die->tag == DW_TAG_variable
15150 || child_die->tag == DW_TAG_variant_part)
15151 {
15152 /* NOTE: carlton/2002-11-05: A C++ static data member
15153 should be a DW_TAG_member that is a declaration, but
15154 all versions of G++ as of this writing (so through at
15155 least 3.2.1) incorrectly generate DW_TAG_variable
15156 tags for them instead. */
15157 dwarf2_add_field (fi, child_die, cu);
15158 }
15159 else if (child_die->tag == DW_TAG_subprogram)
15160 {
15161 /* Rust doesn't have member functions in the C++ sense.
15162 However, it does emit ordinary functions as children
15163 of a struct DIE. */
15164 if (cu->language == language_rust)
15165 read_func_scope (child_die, cu);
15166 else
15167 {
15168 /* C++ member function. */
15169 dwarf2_add_member_fn (fi, child_die, type, cu);
15170 }
15171 }
15172 else if (child_die->tag == DW_TAG_inheritance)
15173 {
15174 /* C++ base class field. */
15175 dwarf2_add_field (fi, child_die, cu);
15176 }
15177 else if (type_can_define_types (child_die))
15178 dwarf2_add_type_defn (fi, child_die, cu);
15179 else if (child_die->tag == DW_TAG_template_type_param
15180 || child_die->tag == DW_TAG_template_value_param)
15181 {
15182 struct symbol *arg = new_symbol (child_die, NULL, cu);
15183
15184 if (arg != NULL)
15185 template_args->push_back (arg);
15186 }
15187 else if (child_die->tag == DW_TAG_variant)
15188 {
15189 /* In a variant we want to get the discriminant and also add a
15190 field for our sole member child. */
15191 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15192
15193 for (die_info *variant_child = child_die->child;
15194 variant_child != NULL;
15195 variant_child = sibling_die (variant_child))
15196 {
15197 if (variant_child->tag == DW_TAG_member)
15198 {
15199 handle_struct_member_die (variant_child, type, fi,
15200 template_args, cu);
15201 /* Only handle the one. */
15202 break;
15203 }
15204 }
15205
15206 /* We don't handle this but we might as well report it if we see
15207 it. */
15208 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15209 complaint (_("DW_AT_discr_list is not supported yet"
15210 " - DIE at %s [in module %s]"),
15211 sect_offset_str (child_die->sect_off),
15212 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15213
15214 /* The first field was just added, so we can stash the
15215 discriminant there. */
15216 gdb_assert (!fi->fields.empty ());
15217 if (discr == NULL)
15218 fi->fields.back ().variant.default_branch = true;
15219 else
15220 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15221 }
15222 }
15223
15224 /* Finish creating a structure or union type, including filling in
15225 its members and creating a symbol for it. */
15226
15227 static void
15228 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15229 {
15230 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15231 struct die_info *child_die;
15232 struct type *type;
15233
15234 type = get_die_type (die, cu);
15235 if (type == NULL)
15236 type = read_structure_type (die, cu);
15237
15238 /* When reading a DW_TAG_variant_part, we need to notice when we
15239 read the discriminant member, so we can record it later in the
15240 discriminant_info. */
15241 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15242 sect_offset discr_offset {};
15243 bool has_template_parameters = false;
15244
15245 if (is_variant_part)
15246 {
15247 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15248 if (discr == NULL)
15249 {
15250 /* Maybe it's a univariant form, an extension we support.
15251 In this case arrange not to check the offset. */
15252 is_variant_part = false;
15253 }
15254 else if (discr->form_is_ref ())
15255 {
15256 struct dwarf2_cu *target_cu = cu;
15257 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15258
15259 discr_offset = target_die->sect_off;
15260 }
15261 else
15262 {
15263 complaint (_("DW_AT_discr does not have DIE reference form"
15264 " - DIE at %s [in module %s]"),
15265 sect_offset_str (die->sect_off),
15266 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15267 is_variant_part = false;
15268 }
15269 }
15270
15271 if (die->child != NULL && ! die_is_declaration (die, cu))
15272 {
15273 struct field_info fi;
15274 std::vector<struct symbol *> template_args;
15275
15276 child_die = die->child;
15277
15278 while (child_die && child_die->tag)
15279 {
15280 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15281
15282 if (is_variant_part && discr_offset == child_die->sect_off)
15283 fi.fields.back ().variant.is_discriminant = true;
15284
15285 child_die = sibling_die (child_die);
15286 }
15287
15288 /* Attach template arguments to type. */
15289 if (!template_args.empty ())
15290 {
15291 has_template_parameters = true;
15292 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15293 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15294 TYPE_TEMPLATE_ARGUMENTS (type)
15295 = XOBNEWVEC (&objfile->objfile_obstack,
15296 struct symbol *,
15297 TYPE_N_TEMPLATE_ARGUMENTS (type));
15298 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15299 template_args.data (),
15300 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15301 * sizeof (struct symbol *)));
15302 }
15303
15304 /* Attach fields and member functions to the type. */
15305 if (fi.nfields)
15306 dwarf2_attach_fields_to_type (&fi, type, cu);
15307 if (!fi.fnfieldlists.empty ())
15308 {
15309 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15310
15311 /* Get the type which refers to the base class (possibly this
15312 class itself) which contains the vtable pointer for the current
15313 class from the DW_AT_containing_type attribute. This use of
15314 DW_AT_containing_type is a GNU extension. */
15315
15316 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15317 {
15318 struct type *t = die_containing_type (die, cu);
15319
15320 set_type_vptr_basetype (type, t);
15321 if (type == t)
15322 {
15323 int i;
15324
15325 /* Our own class provides vtbl ptr. */
15326 for (i = TYPE_NFIELDS (t) - 1;
15327 i >= TYPE_N_BASECLASSES (t);
15328 --i)
15329 {
15330 const char *fieldname = TYPE_FIELD_NAME (t, i);
15331
15332 if (is_vtable_name (fieldname, cu))
15333 {
15334 set_type_vptr_fieldno (type, i);
15335 break;
15336 }
15337 }
15338
15339 /* Complain if virtual function table field not found. */
15340 if (i < TYPE_N_BASECLASSES (t))
15341 complaint (_("virtual function table pointer "
15342 "not found when defining class '%s'"),
15343 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15344 }
15345 else
15346 {
15347 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15348 }
15349 }
15350 else if (cu->producer
15351 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15352 {
15353 /* The IBM XLC compiler does not provide direct indication
15354 of the containing type, but the vtable pointer is
15355 always named __vfp. */
15356
15357 int i;
15358
15359 for (i = TYPE_NFIELDS (type) - 1;
15360 i >= TYPE_N_BASECLASSES (type);
15361 --i)
15362 {
15363 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15364 {
15365 set_type_vptr_fieldno (type, i);
15366 set_type_vptr_basetype (type, type);
15367 break;
15368 }
15369 }
15370 }
15371 }
15372
15373 /* Copy fi.typedef_field_list linked list elements content into the
15374 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15375 if (!fi.typedef_field_list.empty ())
15376 {
15377 int count = fi.typedef_field_list.size ();
15378
15379 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15380 TYPE_TYPEDEF_FIELD_ARRAY (type)
15381 = ((struct decl_field *)
15382 TYPE_ALLOC (type,
15383 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15384 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15385
15386 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15387 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15388 }
15389
15390 /* Copy fi.nested_types_list linked list elements content into the
15391 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15392 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15393 {
15394 int count = fi.nested_types_list.size ();
15395
15396 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15397 TYPE_NESTED_TYPES_ARRAY (type)
15398 = ((struct decl_field *)
15399 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15400 TYPE_NESTED_TYPES_COUNT (type) = count;
15401
15402 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15403 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15404 }
15405 }
15406
15407 quirk_gcc_member_function_pointer (type, objfile);
15408 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15409 cu->rust_unions.push_back (type);
15410
15411 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15412 snapshots) has been known to create a die giving a declaration
15413 for a class that has, as a child, a die giving a definition for a
15414 nested class. So we have to process our children even if the
15415 current die is a declaration. Normally, of course, a declaration
15416 won't have any children at all. */
15417
15418 child_die = die->child;
15419
15420 while (child_die != NULL && child_die->tag)
15421 {
15422 if (child_die->tag == DW_TAG_member
15423 || child_die->tag == DW_TAG_variable
15424 || child_die->tag == DW_TAG_inheritance
15425 || child_die->tag == DW_TAG_template_value_param
15426 || child_die->tag == DW_TAG_template_type_param)
15427 {
15428 /* Do nothing. */
15429 }
15430 else
15431 process_die (child_die, cu);
15432
15433 child_die = sibling_die (child_die);
15434 }
15435
15436 /* Do not consider external references. According to the DWARF standard,
15437 these DIEs are identified by the fact that they have no byte_size
15438 attribute, and a declaration attribute. */
15439 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15440 || !die_is_declaration (die, cu))
15441 {
15442 struct symbol *sym = new_symbol (die, type, cu);
15443
15444 if (has_template_parameters)
15445 {
15446 struct symtab *symtab;
15447 if (sym != nullptr)
15448 symtab = symbol_symtab (sym);
15449 else if (cu->line_header != nullptr)
15450 {
15451 /* Any related symtab will do. */
15452 symtab
15453 = cu->line_header->file_names ()[0].symtab;
15454 }
15455 else
15456 {
15457 symtab = nullptr;
15458 complaint (_("could not find suitable "
15459 "symtab for template parameter"
15460 " - DIE at %s [in module %s]"),
15461 sect_offset_str (die->sect_off),
15462 objfile_name (objfile));
15463 }
15464
15465 if (symtab != nullptr)
15466 {
15467 /* Make sure that the symtab is set on the new symbols.
15468 Even though they don't appear in this symtab directly,
15469 other parts of gdb assume that symbols do, and this is
15470 reasonably true. */
15471 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15472 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15473 }
15474 }
15475 }
15476 }
15477
15478 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15479 update TYPE using some information only available in DIE's children. */
15480
15481 static void
15482 update_enumeration_type_from_children (struct die_info *die,
15483 struct type *type,
15484 struct dwarf2_cu *cu)
15485 {
15486 struct die_info *child_die;
15487 int unsigned_enum = 1;
15488 int flag_enum = 1;
15489 ULONGEST mask = 0;
15490
15491 auto_obstack obstack;
15492
15493 for (child_die = die->child;
15494 child_die != NULL && child_die->tag;
15495 child_die = sibling_die (child_die))
15496 {
15497 struct attribute *attr;
15498 LONGEST value;
15499 const gdb_byte *bytes;
15500 struct dwarf2_locexpr_baton *baton;
15501 const char *name;
15502
15503 if (child_die->tag != DW_TAG_enumerator)
15504 continue;
15505
15506 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15507 if (attr == NULL)
15508 continue;
15509
15510 name = dwarf2_name (child_die, cu);
15511 if (name == NULL)
15512 name = "<anonymous enumerator>";
15513
15514 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15515 &value, &bytes, &baton);
15516 if (value < 0)
15517 {
15518 unsigned_enum = 0;
15519 flag_enum = 0;
15520 }
15521 else if ((mask & value) != 0)
15522 flag_enum = 0;
15523 else
15524 mask |= value;
15525
15526 /* If we already know that the enum type is neither unsigned, nor
15527 a flag type, no need to look at the rest of the enumerates. */
15528 if (!unsigned_enum && !flag_enum)
15529 break;
15530 }
15531
15532 if (unsigned_enum)
15533 TYPE_UNSIGNED (type) = 1;
15534 if (flag_enum)
15535 TYPE_FLAG_ENUM (type) = 1;
15536 }
15537
15538 /* Given a DW_AT_enumeration_type die, set its type. We do not
15539 complete the type's fields yet, or create any symbols. */
15540
15541 static struct type *
15542 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15543 {
15544 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15545 struct type *type;
15546 struct attribute *attr;
15547 const char *name;
15548
15549 /* If the definition of this type lives in .debug_types, read that type.
15550 Don't follow DW_AT_specification though, that will take us back up
15551 the chain and we want to go down. */
15552 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15553 if (attr != nullptr)
15554 {
15555 type = get_DW_AT_signature_type (die, attr, cu);
15556
15557 /* The type's CU may not be the same as CU.
15558 Ensure TYPE is recorded with CU in die_type_hash. */
15559 return set_die_type (die, type, cu);
15560 }
15561
15562 type = alloc_type (objfile);
15563
15564 TYPE_CODE (type) = TYPE_CODE_ENUM;
15565 name = dwarf2_full_name (NULL, die, cu);
15566 if (name != NULL)
15567 TYPE_NAME (type) = name;
15568
15569 attr = dwarf2_attr (die, DW_AT_type, cu);
15570 if (attr != NULL)
15571 {
15572 struct type *underlying_type = die_type (die, cu);
15573
15574 TYPE_TARGET_TYPE (type) = underlying_type;
15575 }
15576
15577 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15578 if (attr != nullptr)
15579 {
15580 TYPE_LENGTH (type) = DW_UNSND (attr);
15581 }
15582 else
15583 {
15584 TYPE_LENGTH (type) = 0;
15585 }
15586
15587 maybe_set_alignment (cu, die, type);
15588
15589 /* The enumeration DIE can be incomplete. In Ada, any type can be
15590 declared as private in the package spec, and then defined only
15591 inside the package body. Such types are known as Taft Amendment
15592 Types. When another package uses such a type, an incomplete DIE
15593 may be generated by the compiler. */
15594 if (die_is_declaration (die, cu))
15595 TYPE_STUB (type) = 1;
15596
15597 /* Finish the creation of this type by using the enum's children.
15598 We must call this even when the underlying type has been provided
15599 so that we can determine if we're looking at a "flag" enum. */
15600 update_enumeration_type_from_children (die, type, cu);
15601
15602 /* If this type has an underlying type that is not a stub, then we
15603 may use its attributes. We always use the "unsigned" attribute
15604 in this situation, because ordinarily we guess whether the type
15605 is unsigned -- but the guess can be wrong and the underlying type
15606 can tell us the reality. However, we defer to a local size
15607 attribute if one exists, because this lets the compiler override
15608 the underlying type if needed. */
15609 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15610 {
15611 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15612 if (TYPE_LENGTH (type) == 0)
15613 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15614 if (TYPE_RAW_ALIGN (type) == 0
15615 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15616 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15617 }
15618
15619 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15620
15621 return set_die_type (die, type, cu);
15622 }
15623
15624 /* Given a pointer to a die which begins an enumeration, process all
15625 the dies that define the members of the enumeration, and create the
15626 symbol for the enumeration type.
15627
15628 NOTE: We reverse the order of the element list. */
15629
15630 static void
15631 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15632 {
15633 struct type *this_type;
15634
15635 this_type = get_die_type (die, cu);
15636 if (this_type == NULL)
15637 this_type = read_enumeration_type (die, cu);
15638
15639 if (die->child != NULL)
15640 {
15641 struct die_info *child_die;
15642 struct symbol *sym;
15643 std::vector<struct field> fields;
15644 const char *name;
15645
15646 child_die = die->child;
15647 while (child_die && child_die->tag)
15648 {
15649 if (child_die->tag != DW_TAG_enumerator)
15650 {
15651 process_die (child_die, cu);
15652 }
15653 else
15654 {
15655 name = dwarf2_name (child_die, cu);
15656 if (name)
15657 {
15658 sym = new_symbol (child_die, this_type, cu);
15659
15660 fields.emplace_back ();
15661 struct field &field = fields.back ();
15662
15663 FIELD_NAME (field) = sym->linkage_name ();
15664 FIELD_TYPE (field) = NULL;
15665 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15666 FIELD_BITSIZE (field) = 0;
15667 }
15668 }
15669
15670 child_die = sibling_die (child_die);
15671 }
15672
15673 if (!fields.empty ())
15674 {
15675 TYPE_NFIELDS (this_type) = fields.size ();
15676 TYPE_FIELDS (this_type) = (struct field *)
15677 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15678 memcpy (TYPE_FIELDS (this_type), fields.data (),
15679 sizeof (struct field) * fields.size ());
15680 }
15681 }
15682
15683 /* If we are reading an enum from a .debug_types unit, and the enum
15684 is a declaration, and the enum is not the signatured type in the
15685 unit, then we do not want to add a symbol for it. Adding a
15686 symbol would in some cases obscure the true definition of the
15687 enum, giving users an incomplete type when the definition is
15688 actually available. Note that we do not want to do this for all
15689 enums which are just declarations, because C++0x allows forward
15690 enum declarations. */
15691 if (cu->per_cu->is_debug_types
15692 && die_is_declaration (die, cu))
15693 {
15694 struct signatured_type *sig_type;
15695
15696 sig_type = (struct signatured_type *) cu->per_cu;
15697 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15698 if (sig_type->type_offset_in_section != die->sect_off)
15699 return;
15700 }
15701
15702 new_symbol (die, this_type, cu);
15703 }
15704
15705 /* Extract all information from a DW_TAG_array_type DIE and put it in
15706 the DIE's type field. For now, this only handles one dimensional
15707 arrays. */
15708
15709 static struct type *
15710 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15711 {
15712 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15713 struct die_info *child_die;
15714 struct type *type;
15715 struct type *element_type, *range_type, *index_type;
15716 struct attribute *attr;
15717 const char *name;
15718 struct dynamic_prop *byte_stride_prop = NULL;
15719 unsigned int bit_stride = 0;
15720
15721 element_type = die_type (die, cu);
15722
15723 /* The die_type call above may have already set the type for this DIE. */
15724 type = get_die_type (die, cu);
15725 if (type)
15726 return type;
15727
15728 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15729 if (attr != NULL)
15730 {
15731 int stride_ok;
15732 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15733
15734 byte_stride_prop
15735 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15736 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15737 prop_type);
15738 if (!stride_ok)
15739 {
15740 complaint (_("unable to read array DW_AT_byte_stride "
15741 " - DIE at %s [in module %s]"),
15742 sect_offset_str (die->sect_off),
15743 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15744 /* Ignore this attribute. We will likely not be able to print
15745 arrays of this type correctly, but there is little we can do
15746 to help if we cannot read the attribute's value. */
15747 byte_stride_prop = NULL;
15748 }
15749 }
15750
15751 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15752 if (attr != NULL)
15753 bit_stride = DW_UNSND (attr);
15754
15755 /* Irix 6.2 native cc creates array types without children for
15756 arrays with unspecified length. */
15757 if (die->child == NULL)
15758 {
15759 index_type = objfile_type (objfile)->builtin_int;
15760 range_type = create_static_range_type (NULL, index_type, 0, -1);
15761 type = create_array_type_with_stride (NULL, element_type, range_type,
15762 byte_stride_prop, bit_stride);
15763 return set_die_type (die, type, cu);
15764 }
15765
15766 std::vector<struct type *> range_types;
15767 child_die = die->child;
15768 while (child_die && child_die->tag)
15769 {
15770 if (child_die->tag == DW_TAG_subrange_type)
15771 {
15772 struct type *child_type = read_type_die (child_die, cu);
15773
15774 if (child_type != NULL)
15775 {
15776 /* The range type was succesfully read. Save it for the
15777 array type creation. */
15778 range_types.push_back (child_type);
15779 }
15780 }
15781 child_die = sibling_die (child_die);
15782 }
15783
15784 /* Dwarf2 dimensions are output from left to right, create the
15785 necessary array types in backwards order. */
15786
15787 type = element_type;
15788
15789 if (read_array_order (die, cu) == DW_ORD_col_major)
15790 {
15791 int i = 0;
15792
15793 while (i < range_types.size ())
15794 type = create_array_type_with_stride (NULL, type, range_types[i++],
15795 byte_stride_prop, bit_stride);
15796 }
15797 else
15798 {
15799 size_t ndim = range_types.size ();
15800 while (ndim-- > 0)
15801 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15802 byte_stride_prop, bit_stride);
15803 }
15804
15805 /* Understand Dwarf2 support for vector types (like they occur on
15806 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15807 array type. This is not part of the Dwarf2/3 standard yet, but a
15808 custom vendor extension. The main difference between a regular
15809 array and the vector variant is that vectors are passed by value
15810 to functions. */
15811 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15812 if (attr != nullptr)
15813 make_vector_type (type);
15814
15815 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15816 implementation may choose to implement triple vectors using this
15817 attribute. */
15818 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15819 if (attr != nullptr)
15820 {
15821 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15822 TYPE_LENGTH (type) = DW_UNSND (attr);
15823 else
15824 complaint (_("DW_AT_byte_size for array type smaller "
15825 "than the total size of elements"));
15826 }
15827
15828 name = dwarf2_name (die, cu);
15829 if (name)
15830 TYPE_NAME (type) = name;
15831
15832 maybe_set_alignment (cu, die, type);
15833
15834 /* Install the type in the die. */
15835 set_die_type (die, type, cu);
15836
15837 /* set_die_type should be already done. */
15838 set_descriptive_type (type, die, cu);
15839
15840 return type;
15841 }
15842
15843 static enum dwarf_array_dim_ordering
15844 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15845 {
15846 struct attribute *attr;
15847
15848 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15849
15850 if (attr != nullptr)
15851 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15852
15853 /* GNU F77 is a special case, as at 08/2004 array type info is the
15854 opposite order to the dwarf2 specification, but data is still
15855 laid out as per normal fortran.
15856
15857 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15858 version checking. */
15859
15860 if (cu->language == language_fortran
15861 && cu->producer && strstr (cu->producer, "GNU F77"))
15862 {
15863 return DW_ORD_row_major;
15864 }
15865
15866 switch (cu->language_defn->la_array_ordering)
15867 {
15868 case array_column_major:
15869 return DW_ORD_col_major;
15870 case array_row_major:
15871 default:
15872 return DW_ORD_row_major;
15873 };
15874 }
15875
15876 /* Extract all information from a DW_TAG_set_type DIE and put it in
15877 the DIE's type field. */
15878
15879 static struct type *
15880 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15881 {
15882 struct type *domain_type, *set_type;
15883 struct attribute *attr;
15884
15885 domain_type = die_type (die, cu);
15886
15887 /* The die_type call above may have already set the type for this DIE. */
15888 set_type = get_die_type (die, cu);
15889 if (set_type)
15890 return set_type;
15891
15892 set_type = create_set_type (NULL, domain_type);
15893
15894 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15895 if (attr != nullptr)
15896 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15897
15898 maybe_set_alignment (cu, die, set_type);
15899
15900 return set_die_type (die, set_type, cu);
15901 }
15902
15903 /* A helper for read_common_block that creates a locexpr baton.
15904 SYM is the symbol which we are marking as computed.
15905 COMMON_DIE is the DIE for the common block.
15906 COMMON_LOC is the location expression attribute for the common
15907 block itself.
15908 MEMBER_LOC is the location expression attribute for the particular
15909 member of the common block that we are processing.
15910 CU is the CU from which the above come. */
15911
15912 static void
15913 mark_common_block_symbol_computed (struct symbol *sym,
15914 struct die_info *common_die,
15915 struct attribute *common_loc,
15916 struct attribute *member_loc,
15917 struct dwarf2_cu *cu)
15918 {
15919 struct dwarf2_per_objfile *dwarf2_per_objfile
15920 = cu->per_cu->dwarf2_per_objfile;
15921 struct objfile *objfile = dwarf2_per_objfile->objfile;
15922 struct dwarf2_locexpr_baton *baton;
15923 gdb_byte *ptr;
15924 unsigned int cu_off;
15925 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15926 LONGEST offset = 0;
15927
15928 gdb_assert (common_loc && member_loc);
15929 gdb_assert (common_loc->form_is_block ());
15930 gdb_assert (member_loc->form_is_block ()
15931 || member_loc->form_is_constant ());
15932
15933 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15934 baton->per_cu = cu->per_cu;
15935 gdb_assert (baton->per_cu);
15936
15937 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15938
15939 if (member_loc->form_is_constant ())
15940 {
15941 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15942 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15943 }
15944 else
15945 baton->size += DW_BLOCK (member_loc)->size;
15946
15947 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15948 baton->data = ptr;
15949
15950 *ptr++ = DW_OP_call4;
15951 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15952 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15953 ptr += 4;
15954
15955 if (member_loc->form_is_constant ())
15956 {
15957 *ptr++ = DW_OP_addr;
15958 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15959 ptr += cu->header.addr_size;
15960 }
15961 else
15962 {
15963 /* We have to copy the data here, because DW_OP_call4 will only
15964 use a DW_AT_location attribute. */
15965 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15966 ptr += DW_BLOCK (member_loc)->size;
15967 }
15968
15969 *ptr++ = DW_OP_plus;
15970 gdb_assert (ptr - baton->data == baton->size);
15971
15972 SYMBOL_LOCATION_BATON (sym) = baton;
15973 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15974 }
15975
15976 /* Create appropriate locally-scoped variables for all the
15977 DW_TAG_common_block entries. Also create a struct common_block
15978 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15979 is used to separate the common blocks name namespace from regular
15980 variable names. */
15981
15982 static void
15983 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15984 {
15985 struct attribute *attr;
15986
15987 attr = dwarf2_attr (die, DW_AT_location, cu);
15988 if (attr != nullptr)
15989 {
15990 /* Support the .debug_loc offsets. */
15991 if (attr->form_is_block ())
15992 {
15993 /* Ok. */
15994 }
15995 else if (attr->form_is_section_offset ())
15996 {
15997 dwarf2_complex_location_expr_complaint ();
15998 attr = NULL;
15999 }
16000 else
16001 {
16002 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16003 "common block member");
16004 attr = NULL;
16005 }
16006 }
16007
16008 if (die->child != NULL)
16009 {
16010 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16011 struct die_info *child_die;
16012 size_t n_entries = 0, size;
16013 struct common_block *common_block;
16014 struct symbol *sym;
16015
16016 for (child_die = die->child;
16017 child_die && child_die->tag;
16018 child_die = sibling_die (child_die))
16019 ++n_entries;
16020
16021 size = (sizeof (struct common_block)
16022 + (n_entries - 1) * sizeof (struct symbol *));
16023 common_block
16024 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16025 size);
16026 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16027 common_block->n_entries = 0;
16028
16029 for (child_die = die->child;
16030 child_die && child_die->tag;
16031 child_die = sibling_die (child_die))
16032 {
16033 /* Create the symbol in the DW_TAG_common_block block in the current
16034 symbol scope. */
16035 sym = new_symbol (child_die, NULL, cu);
16036 if (sym != NULL)
16037 {
16038 struct attribute *member_loc;
16039
16040 common_block->contents[common_block->n_entries++] = sym;
16041
16042 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16043 cu);
16044 if (member_loc)
16045 {
16046 /* GDB has handled this for a long time, but it is
16047 not specified by DWARF. It seems to have been
16048 emitted by gfortran at least as recently as:
16049 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16050 complaint (_("Variable in common block has "
16051 "DW_AT_data_member_location "
16052 "- DIE at %s [in module %s]"),
16053 sect_offset_str (child_die->sect_off),
16054 objfile_name (objfile));
16055
16056 if (member_loc->form_is_section_offset ())
16057 dwarf2_complex_location_expr_complaint ();
16058 else if (member_loc->form_is_constant ()
16059 || member_loc->form_is_block ())
16060 {
16061 if (attr != nullptr)
16062 mark_common_block_symbol_computed (sym, die, attr,
16063 member_loc, cu);
16064 }
16065 else
16066 dwarf2_complex_location_expr_complaint ();
16067 }
16068 }
16069 }
16070
16071 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16072 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16073 }
16074 }
16075
16076 /* Create a type for a C++ namespace. */
16077
16078 static struct type *
16079 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16080 {
16081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16082 const char *previous_prefix, *name;
16083 int is_anonymous;
16084 struct type *type;
16085
16086 /* For extensions, reuse the type of the original namespace. */
16087 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16088 {
16089 struct die_info *ext_die;
16090 struct dwarf2_cu *ext_cu = cu;
16091
16092 ext_die = dwarf2_extension (die, &ext_cu);
16093 type = read_type_die (ext_die, ext_cu);
16094
16095 /* EXT_CU may not be the same as CU.
16096 Ensure TYPE is recorded with CU in die_type_hash. */
16097 return set_die_type (die, type, cu);
16098 }
16099
16100 name = namespace_name (die, &is_anonymous, cu);
16101
16102 /* Now build the name of the current namespace. */
16103
16104 previous_prefix = determine_prefix (die, cu);
16105 if (previous_prefix[0] != '\0')
16106 name = typename_concat (&objfile->objfile_obstack,
16107 previous_prefix, name, 0, cu);
16108
16109 /* Create the type. */
16110 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16111
16112 return set_die_type (die, type, cu);
16113 }
16114
16115 /* Read a namespace scope. */
16116
16117 static void
16118 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16119 {
16120 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16121 int is_anonymous;
16122
16123 /* Add a symbol associated to this if we haven't seen the namespace
16124 before. Also, add a using directive if it's an anonymous
16125 namespace. */
16126
16127 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16128 {
16129 struct type *type;
16130
16131 type = read_type_die (die, cu);
16132 new_symbol (die, type, cu);
16133
16134 namespace_name (die, &is_anonymous, cu);
16135 if (is_anonymous)
16136 {
16137 const char *previous_prefix = determine_prefix (die, cu);
16138
16139 std::vector<const char *> excludes;
16140 add_using_directive (using_directives (cu),
16141 previous_prefix, TYPE_NAME (type), NULL,
16142 NULL, excludes, 0, &objfile->objfile_obstack);
16143 }
16144 }
16145
16146 if (die->child != NULL)
16147 {
16148 struct die_info *child_die = die->child;
16149
16150 while (child_die && child_die->tag)
16151 {
16152 process_die (child_die, cu);
16153 child_die = sibling_die (child_die);
16154 }
16155 }
16156 }
16157
16158 /* Read a Fortran module as type. This DIE can be only a declaration used for
16159 imported module. Still we need that type as local Fortran "use ... only"
16160 declaration imports depend on the created type in determine_prefix. */
16161
16162 static struct type *
16163 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16164 {
16165 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16166 const char *module_name;
16167 struct type *type;
16168
16169 module_name = dwarf2_name (die, cu);
16170 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16171
16172 return set_die_type (die, type, cu);
16173 }
16174
16175 /* Read a Fortran module. */
16176
16177 static void
16178 read_module (struct die_info *die, struct dwarf2_cu *cu)
16179 {
16180 struct die_info *child_die = die->child;
16181 struct type *type;
16182
16183 type = read_type_die (die, cu);
16184 new_symbol (die, type, cu);
16185
16186 while (child_die && child_die->tag)
16187 {
16188 process_die (child_die, cu);
16189 child_die = sibling_die (child_die);
16190 }
16191 }
16192
16193 /* Return the name of the namespace represented by DIE. Set
16194 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16195 namespace. */
16196
16197 static const char *
16198 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16199 {
16200 struct die_info *current_die;
16201 const char *name = NULL;
16202
16203 /* Loop through the extensions until we find a name. */
16204
16205 for (current_die = die;
16206 current_die != NULL;
16207 current_die = dwarf2_extension (die, &cu))
16208 {
16209 /* We don't use dwarf2_name here so that we can detect the absence
16210 of a name -> anonymous namespace. */
16211 name = dwarf2_string_attr (die, DW_AT_name, cu);
16212
16213 if (name != NULL)
16214 break;
16215 }
16216
16217 /* Is it an anonymous namespace? */
16218
16219 *is_anonymous = (name == NULL);
16220 if (*is_anonymous)
16221 name = CP_ANONYMOUS_NAMESPACE_STR;
16222
16223 return name;
16224 }
16225
16226 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16227 the user defined type vector. */
16228
16229 static struct type *
16230 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16231 {
16232 struct gdbarch *gdbarch
16233 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16234 struct comp_unit_head *cu_header = &cu->header;
16235 struct type *type;
16236 struct attribute *attr_byte_size;
16237 struct attribute *attr_address_class;
16238 int byte_size, addr_class;
16239 struct type *target_type;
16240
16241 target_type = die_type (die, cu);
16242
16243 /* The die_type call above may have already set the type for this DIE. */
16244 type = get_die_type (die, cu);
16245 if (type)
16246 return type;
16247
16248 type = lookup_pointer_type (target_type);
16249
16250 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16251 if (attr_byte_size)
16252 byte_size = DW_UNSND (attr_byte_size);
16253 else
16254 byte_size = cu_header->addr_size;
16255
16256 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16257 if (attr_address_class)
16258 addr_class = DW_UNSND (attr_address_class);
16259 else
16260 addr_class = DW_ADDR_none;
16261
16262 ULONGEST alignment = get_alignment (cu, die);
16263
16264 /* If the pointer size, alignment, or address class is different
16265 than the default, create a type variant marked as such and set
16266 the length accordingly. */
16267 if (TYPE_LENGTH (type) != byte_size
16268 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16269 && alignment != TYPE_RAW_ALIGN (type))
16270 || addr_class != DW_ADDR_none)
16271 {
16272 if (gdbarch_address_class_type_flags_p (gdbarch))
16273 {
16274 int type_flags;
16275
16276 type_flags = gdbarch_address_class_type_flags
16277 (gdbarch, byte_size, addr_class);
16278 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16279 == 0);
16280 type = make_type_with_address_space (type, type_flags);
16281 }
16282 else if (TYPE_LENGTH (type) != byte_size)
16283 {
16284 complaint (_("invalid pointer size %d"), byte_size);
16285 }
16286 else if (TYPE_RAW_ALIGN (type) != alignment)
16287 {
16288 complaint (_("Invalid DW_AT_alignment"
16289 " - DIE at %s [in module %s]"),
16290 sect_offset_str (die->sect_off),
16291 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16292 }
16293 else
16294 {
16295 /* Should we also complain about unhandled address classes? */
16296 }
16297 }
16298
16299 TYPE_LENGTH (type) = byte_size;
16300 set_type_align (type, alignment);
16301 return set_die_type (die, type, cu);
16302 }
16303
16304 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16305 the user defined type vector. */
16306
16307 static struct type *
16308 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16309 {
16310 struct type *type;
16311 struct type *to_type;
16312 struct type *domain;
16313
16314 to_type = die_type (die, cu);
16315 domain = die_containing_type (die, cu);
16316
16317 /* The calls above may have already set the type for this DIE. */
16318 type = get_die_type (die, cu);
16319 if (type)
16320 return type;
16321
16322 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16323 type = lookup_methodptr_type (to_type);
16324 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16325 {
16326 struct type *new_type
16327 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16328
16329 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16330 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16331 TYPE_VARARGS (to_type));
16332 type = lookup_methodptr_type (new_type);
16333 }
16334 else
16335 type = lookup_memberptr_type (to_type, domain);
16336
16337 return set_die_type (die, type, cu);
16338 }
16339
16340 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16341 the user defined type vector. */
16342
16343 static struct type *
16344 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16345 enum type_code refcode)
16346 {
16347 struct comp_unit_head *cu_header = &cu->header;
16348 struct type *type, *target_type;
16349 struct attribute *attr;
16350
16351 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16352
16353 target_type = die_type (die, cu);
16354
16355 /* The die_type call above may have already set the type for this DIE. */
16356 type = get_die_type (die, cu);
16357 if (type)
16358 return type;
16359
16360 type = lookup_reference_type (target_type, refcode);
16361 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16362 if (attr != nullptr)
16363 {
16364 TYPE_LENGTH (type) = DW_UNSND (attr);
16365 }
16366 else
16367 {
16368 TYPE_LENGTH (type) = cu_header->addr_size;
16369 }
16370 maybe_set_alignment (cu, die, type);
16371 return set_die_type (die, type, cu);
16372 }
16373
16374 /* Add the given cv-qualifiers to the element type of the array. GCC
16375 outputs DWARF type qualifiers that apply to an array, not the
16376 element type. But GDB relies on the array element type to carry
16377 the cv-qualifiers. This mimics section 6.7.3 of the C99
16378 specification. */
16379
16380 static struct type *
16381 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16382 struct type *base_type, int cnst, int voltl)
16383 {
16384 struct type *el_type, *inner_array;
16385
16386 base_type = copy_type (base_type);
16387 inner_array = base_type;
16388
16389 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16390 {
16391 TYPE_TARGET_TYPE (inner_array) =
16392 copy_type (TYPE_TARGET_TYPE (inner_array));
16393 inner_array = TYPE_TARGET_TYPE (inner_array);
16394 }
16395
16396 el_type = TYPE_TARGET_TYPE (inner_array);
16397 cnst |= TYPE_CONST (el_type);
16398 voltl |= TYPE_VOLATILE (el_type);
16399 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16400
16401 return set_die_type (die, base_type, cu);
16402 }
16403
16404 static struct type *
16405 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16406 {
16407 struct type *base_type, *cv_type;
16408
16409 base_type = die_type (die, cu);
16410
16411 /* The die_type call above may have already set the type for this DIE. */
16412 cv_type = get_die_type (die, cu);
16413 if (cv_type)
16414 return cv_type;
16415
16416 /* In case the const qualifier is applied to an array type, the element type
16417 is so qualified, not the array type (section 6.7.3 of C99). */
16418 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16419 return add_array_cv_type (die, cu, base_type, 1, 0);
16420
16421 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16422 return set_die_type (die, cv_type, cu);
16423 }
16424
16425 static struct type *
16426 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16427 {
16428 struct type *base_type, *cv_type;
16429
16430 base_type = die_type (die, cu);
16431
16432 /* The die_type call above may have already set the type for this DIE. */
16433 cv_type = get_die_type (die, cu);
16434 if (cv_type)
16435 return cv_type;
16436
16437 /* In case the volatile qualifier is applied to an array type, the
16438 element type is so qualified, not the array type (section 6.7.3
16439 of C99). */
16440 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16441 return add_array_cv_type (die, cu, base_type, 0, 1);
16442
16443 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16444 return set_die_type (die, cv_type, cu);
16445 }
16446
16447 /* Handle DW_TAG_restrict_type. */
16448
16449 static struct type *
16450 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16451 {
16452 struct type *base_type, *cv_type;
16453
16454 base_type = die_type (die, cu);
16455
16456 /* The die_type call above may have already set the type for this DIE. */
16457 cv_type = get_die_type (die, cu);
16458 if (cv_type)
16459 return cv_type;
16460
16461 cv_type = make_restrict_type (base_type);
16462 return set_die_type (die, cv_type, cu);
16463 }
16464
16465 /* Handle DW_TAG_atomic_type. */
16466
16467 static struct type *
16468 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16469 {
16470 struct type *base_type, *cv_type;
16471
16472 base_type = die_type (die, cu);
16473
16474 /* The die_type call above may have already set the type for this DIE. */
16475 cv_type = get_die_type (die, cu);
16476 if (cv_type)
16477 return cv_type;
16478
16479 cv_type = make_atomic_type (base_type);
16480 return set_die_type (die, cv_type, cu);
16481 }
16482
16483 /* Extract all information from a DW_TAG_string_type DIE and add to
16484 the user defined type vector. It isn't really a user defined type,
16485 but it behaves like one, with other DIE's using an AT_user_def_type
16486 attribute to reference it. */
16487
16488 static struct type *
16489 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16490 {
16491 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16492 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16493 struct type *type, *range_type, *index_type, *char_type;
16494 struct attribute *attr;
16495 struct dynamic_prop prop;
16496 bool length_is_constant = true;
16497 LONGEST length;
16498
16499 /* There are a couple of places where bit sizes might be made use of
16500 when parsing a DW_TAG_string_type, however, no producer that we know
16501 of make use of these. Handling bit sizes that are a multiple of the
16502 byte size is easy enough, but what about other bit sizes? Lets deal
16503 with that problem when we have to. Warn about these attributes being
16504 unsupported, then parse the type and ignore them like we always
16505 have. */
16506 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16507 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16508 {
16509 static bool warning_printed = false;
16510 if (!warning_printed)
16511 {
16512 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16513 "currently supported on DW_TAG_string_type."));
16514 warning_printed = true;
16515 }
16516 }
16517
16518 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16519 if (attr != nullptr && !attr->form_is_constant ())
16520 {
16521 /* The string length describes the location at which the length of
16522 the string can be found. The size of the length field can be
16523 specified with one of the attributes below. */
16524 struct type *prop_type;
16525 struct attribute *len
16526 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16527 if (len == nullptr)
16528 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16529 if (len != nullptr && len->form_is_constant ())
16530 {
16531 /* Pass 0 as the default as we know this attribute is constant
16532 and the default value will not be returned. */
16533 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16534 prop_type = cu->per_cu->int_type (sz, true);
16535 }
16536 else
16537 {
16538 /* If the size is not specified then we assume it is the size of
16539 an address on this target. */
16540 prop_type = cu->per_cu->addr_sized_int_type (true);
16541 }
16542
16543 /* Convert the attribute into a dynamic property. */
16544 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16545 length = 1;
16546 else
16547 length_is_constant = false;
16548 }
16549 else if (attr != nullptr)
16550 {
16551 /* This DW_AT_string_length just contains the length with no
16552 indirection. There's no need to create a dynamic property in this
16553 case. Pass 0 for the default value as we know it will not be
16554 returned in this case. */
16555 length = dwarf2_get_attr_constant_value (attr, 0);
16556 }
16557 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16558 {
16559 /* We don't currently support non-constant byte sizes for strings. */
16560 length = dwarf2_get_attr_constant_value (attr, 1);
16561 }
16562 else
16563 {
16564 /* Use 1 as a fallback length if we have nothing else. */
16565 length = 1;
16566 }
16567
16568 index_type = objfile_type (objfile)->builtin_int;
16569 if (length_is_constant)
16570 range_type = create_static_range_type (NULL, index_type, 1, length);
16571 else
16572 {
16573 struct dynamic_prop low_bound;
16574
16575 low_bound.kind = PROP_CONST;
16576 low_bound.data.const_val = 1;
16577 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16578 }
16579 char_type = language_string_char_type (cu->language_defn, gdbarch);
16580 type = create_string_type (NULL, char_type, range_type);
16581
16582 return set_die_type (die, type, cu);
16583 }
16584
16585 /* Assuming that DIE corresponds to a function, returns nonzero
16586 if the function is prototyped. */
16587
16588 static int
16589 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16590 {
16591 struct attribute *attr;
16592
16593 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16594 if (attr && (DW_UNSND (attr) != 0))
16595 return 1;
16596
16597 /* The DWARF standard implies that the DW_AT_prototyped attribute
16598 is only meaningful for C, but the concept also extends to other
16599 languages that allow unprototyped functions (Eg: Objective C).
16600 For all other languages, assume that functions are always
16601 prototyped. */
16602 if (cu->language != language_c
16603 && cu->language != language_objc
16604 && cu->language != language_opencl)
16605 return 1;
16606
16607 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16608 prototyped and unprototyped functions; default to prototyped,
16609 since that is more common in modern code (and RealView warns
16610 about unprototyped functions). */
16611 if (producer_is_realview (cu->producer))
16612 return 1;
16613
16614 return 0;
16615 }
16616
16617 /* Handle DIES due to C code like:
16618
16619 struct foo
16620 {
16621 int (*funcp)(int a, long l);
16622 int b;
16623 };
16624
16625 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16626
16627 static struct type *
16628 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16629 {
16630 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16631 struct type *type; /* Type that this function returns. */
16632 struct type *ftype; /* Function that returns above type. */
16633 struct attribute *attr;
16634
16635 type = die_type (die, cu);
16636
16637 /* The die_type call above may have already set the type for this DIE. */
16638 ftype = get_die_type (die, cu);
16639 if (ftype)
16640 return ftype;
16641
16642 ftype = lookup_function_type (type);
16643
16644 if (prototyped_function_p (die, cu))
16645 TYPE_PROTOTYPED (ftype) = 1;
16646
16647 /* Store the calling convention in the type if it's available in
16648 the subroutine die. Otherwise set the calling convention to
16649 the default value DW_CC_normal. */
16650 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16651 if (attr != nullptr
16652 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16653 TYPE_CALLING_CONVENTION (ftype)
16654 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16655 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16656 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16657 else
16658 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16659
16660 /* Record whether the function returns normally to its caller or not
16661 if the DWARF producer set that information. */
16662 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16663 if (attr && (DW_UNSND (attr) != 0))
16664 TYPE_NO_RETURN (ftype) = 1;
16665
16666 /* We need to add the subroutine type to the die immediately so
16667 we don't infinitely recurse when dealing with parameters
16668 declared as the same subroutine type. */
16669 set_die_type (die, ftype, cu);
16670
16671 if (die->child != NULL)
16672 {
16673 struct type *void_type = objfile_type (objfile)->builtin_void;
16674 struct die_info *child_die;
16675 int nparams, iparams;
16676
16677 /* Count the number of parameters.
16678 FIXME: GDB currently ignores vararg functions, but knows about
16679 vararg member functions. */
16680 nparams = 0;
16681 child_die = die->child;
16682 while (child_die && child_die->tag)
16683 {
16684 if (child_die->tag == DW_TAG_formal_parameter)
16685 nparams++;
16686 else if (child_die->tag == DW_TAG_unspecified_parameters)
16687 TYPE_VARARGS (ftype) = 1;
16688 child_die = sibling_die (child_die);
16689 }
16690
16691 /* Allocate storage for parameters and fill them in. */
16692 TYPE_NFIELDS (ftype) = nparams;
16693 TYPE_FIELDS (ftype) = (struct field *)
16694 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16695
16696 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16697 even if we error out during the parameters reading below. */
16698 for (iparams = 0; iparams < nparams; iparams++)
16699 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16700
16701 iparams = 0;
16702 child_die = die->child;
16703 while (child_die && child_die->tag)
16704 {
16705 if (child_die->tag == DW_TAG_formal_parameter)
16706 {
16707 struct type *arg_type;
16708
16709 /* DWARF version 2 has no clean way to discern C++
16710 static and non-static member functions. G++ helps
16711 GDB by marking the first parameter for non-static
16712 member functions (which is the this pointer) as
16713 artificial. We pass this information to
16714 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16715
16716 DWARF version 3 added DW_AT_object_pointer, which GCC
16717 4.5 does not yet generate. */
16718 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16719 if (attr != nullptr)
16720 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16721 else
16722 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16723 arg_type = die_type (child_die, cu);
16724
16725 /* RealView does not mark THIS as const, which the testsuite
16726 expects. GCC marks THIS as const in method definitions,
16727 but not in the class specifications (GCC PR 43053). */
16728 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16729 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16730 {
16731 int is_this = 0;
16732 struct dwarf2_cu *arg_cu = cu;
16733 const char *name = dwarf2_name (child_die, cu);
16734
16735 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16736 if (attr != nullptr)
16737 {
16738 /* If the compiler emits this, use it. */
16739 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16740 is_this = 1;
16741 }
16742 else if (name && strcmp (name, "this") == 0)
16743 /* Function definitions will have the argument names. */
16744 is_this = 1;
16745 else if (name == NULL && iparams == 0)
16746 /* Declarations may not have the names, so like
16747 elsewhere in GDB, assume an artificial first
16748 argument is "this". */
16749 is_this = 1;
16750
16751 if (is_this)
16752 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16753 arg_type, 0);
16754 }
16755
16756 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16757 iparams++;
16758 }
16759 child_die = sibling_die (child_die);
16760 }
16761 }
16762
16763 return ftype;
16764 }
16765
16766 static struct type *
16767 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16768 {
16769 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16770 const char *name = NULL;
16771 struct type *this_type, *target_type;
16772
16773 name = dwarf2_full_name (NULL, die, cu);
16774 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16775 TYPE_TARGET_STUB (this_type) = 1;
16776 set_die_type (die, this_type, cu);
16777 target_type = die_type (die, cu);
16778 if (target_type != this_type)
16779 TYPE_TARGET_TYPE (this_type) = target_type;
16780 else
16781 {
16782 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16783 spec and cause infinite loops in GDB. */
16784 complaint (_("Self-referential DW_TAG_typedef "
16785 "- DIE at %s [in module %s]"),
16786 sect_offset_str (die->sect_off), objfile_name (objfile));
16787 TYPE_TARGET_TYPE (this_type) = NULL;
16788 }
16789 return this_type;
16790 }
16791
16792 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16793 (which may be different from NAME) to the architecture back-end to allow
16794 it to guess the correct format if necessary. */
16795
16796 static struct type *
16797 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16798 const char *name_hint, enum bfd_endian byte_order)
16799 {
16800 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16801 const struct floatformat **format;
16802 struct type *type;
16803
16804 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16805 if (format)
16806 type = init_float_type (objfile, bits, name, format, byte_order);
16807 else
16808 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16809
16810 return type;
16811 }
16812
16813 /* Allocate an integer type of size BITS and name NAME. */
16814
16815 static struct type *
16816 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16817 int bits, int unsigned_p, const char *name)
16818 {
16819 struct type *type;
16820
16821 /* Versions of Intel's C Compiler generate an integer type called "void"
16822 instead of using DW_TAG_unspecified_type. This has been seen on
16823 at least versions 14, 17, and 18. */
16824 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16825 && strcmp (name, "void") == 0)
16826 type = objfile_type (objfile)->builtin_void;
16827 else
16828 type = init_integer_type (objfile, bits, unsigned_p, name);
16829
16830 return type;
16831 }
16832
16833 /* Initialise and return a floating point type of size BITS suitable for
16834 use as a component of a complex number. The NAME_HINT is passed through
16835 when initialising the floating point type and is the name of the complex
16836 type.
16837
16838 As DWARF doesn't currently provide an explicit name for the components
16839 of a complex number, but it can be helpful to have these components
16840 named, we try to select a suitable name based on the size of the
16841 component. */
16842 static struct type *
16843 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16844 struct objfile *objfile,
16845 int bits, const char *name_hint,
16846 enum bfd_endian byte_order)
16847 {
16848 gdbarch *gdbarch = get_objfile_arch (objfile);
16849 struct type *tt = nullptr;
16850
16851 /* Try to find a suitable floating point builtin type of size BITS.
16852 We're going to use the name of this type as the name for the complex
16853 target type that we are about to create. */
16854 switch (cu->language)
16855 {
16856 case language_fortran:
16857 switch (bits)
16858 {
16859 case 32:
16860 tt = builtin_f_type (gdbarch)->builtin_real;
16861 break;
16862 case 64:
16863 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16864 break;
16865 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16866 case 128:
16867 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16868 break;
16869 }
16870 break;
16871 default:
16872 switch (bits)
16873 {
16874 case 32:
16875 tt = builtin_type (gdbarch)->builtin_float;
16876 break;
16877 case 64:
16878 tt = builtin_type (gdbarch)->builtin_double;
16879 break;
16880 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16881 case 128:
16882 tt = builtin_type (gdbarch)->builtin_long_double;
16883 break;
16884 }
16885 break;
16886 }
16887
16888 /* If the type we found doesn't match the size we were looking for, then
16889 pretend we didn't find a type at all, the complex target type we
16890 create will then be nameless. */
16891 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16892 tt = nullptr;
16893
16894 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16895 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16896 }
16897
16898 /* Find a representation of a given base type and install
16899 it in the TYPE field of the die. */
16900
16901 static struct type *
16902 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16903 {
16904 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16905 struct type *type;
16906 struct attribute *attr;
16907 int encoding = 0, bits = 0;
16908 const char *name;
16909 gdbarch *arch;
16910
16911 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16912 if (attr != nullptr)
16913 encoding = DW_UNSND (attr);
16914 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16915 if (attr != nullptr)
16916 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16917 name = dwarf2_name (die, cu);
16918 if (!name)
16919 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16920
16921 arch = get_objfile_arch (objfile);
16922 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16923
16924 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16925 if (attr)
16926 {
16927 int endianity = DW_UNSND (attr);
16928
16929 switch (endianity)
16930 {
16931 case DW_END_big:
16932 byte_order = BFD_ENDIAN_BIG;
16933 break;
16934 case DW_END_little:
16935 byte_order = BFD_ENDIAN_LITTLE;
16936 break;
16937 default:
16938 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16939 break;
16940 }
16941 }
16942
16943 switch (encoding)
16944 {
16945 case DW_ATE_address:
16946 /* Turn DW_ATE_address into a void * pointer. */
16947 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16948 type = init_pointer_type (objfile, bits, name, type);
16949 break;
16950 case DW_ATE_boolean:
16951 type = init_boolean_type (objfile, bits, 1, name);
16952 break;
16953 case DW_ATE_complex_float:
16954 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16955 byte_order);
16956 type = init_complex_type (objfile, name, type);
16957 break;
16958 case DW_ATE_decimal_float:
16959 type = init_decfloat_type (objfile, bits, name);
16960 break;
16961 case DW_ATE_float:
16962 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16963 break;
16964 case DW_ATE_signed:
16965 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16966 break;
16967 case DW_ATE_unsigned:
16968 if (cu->language == language_fortran
16969 && name
16970 && startswith (name, "character("))
16971 type = init_character_type (objfile, bits, 1, name);
16972 else
16973 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16974 break;
16975 case DW_ATE_signed_char:
16976 if (cu->language == language_ada || cu->language == language_m2
16977 || cu->language == language_pascal
16978 || cu->language == language_fortran)
16979 type = init_character_type (objfile, bits, 0, name);
16980 else
16981 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16982 break;
16983 case DW_ATE_unsigned_char:
16984 if (cu->language == language_ada || cu->language == language_m2
16985 || cu->language == language_pascal
16986 || cu->language == language_fortran
16987 || cu->language == language_rust)
16988 type = init_character_type (objfile, bits, 1, name);
16989 else
16990 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16991 break;
16992 case DW_ATE_UTF:
16993 {
16994 if (bits == 16)
16995 type = builtin_type (arch)->builtin_char16;
16996 else if (bits == 32)
16997 type = builtin_type (arch)->builtin_char32;
16998 else
16999 {
17000 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17001 bits);
17002 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17003 }
17004 return set_die_type (die, type, cu);
17005 }
17006 break;
17007
17008 default:
17009 complaint (_("unsupported DW_AT_encoding: '%s'"),
17010 dwarf_type_encoding_name (encoding));
17011 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17012 break;
17013 }
17014
17015 if (name && strcmp (name, "char") == 0)
17016 TYPE_NOSIGN (type) = 1;
17017
17018 maybe_set_alignment (cu, die, type);
17019
17020 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17021
17022 return set_die_type (die, type, cu);
17023 }
17024
17025 /* Parse dwarf attribute if it's a block, reference or constant and put the
17026 resulting value of the attribute into struct bound_prop.
17027 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17028
17029 static int
17030 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17031 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17032 struct type *default_type)
17033 {
17034 struct dwarf2_property_baton *baton;
17035 struct obstack *obstack
17036 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17037
17038 gdb_assert (default_type != NULL);
17039
17040 if (attr == NULL || prop == NULL)
17041 return 0;
17042
17043 if (attr->form_is_block ())
17044 {
17045 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17046 baton->property_type = default_type;
17047 baton->locexpr.per_cu = cu->per_cu;
17048 baton->locexpr.size = DW_BLOCK (attr)->size;
17049 baton->locexpr.data = DW_BLOCK (attr)->data;
17050 switch (attr->name)
17051 {
17052 case DW_AT_string_length:
17053 baton->locexpr.is_reference = true;
17054 break;
17055 default:
17056 baton->locexpr.is_reference = false;
17057 break;
17058 }
17059 prop->data.baton = baton;
17060 prop->kind = PROP_LOCEXPR;
17061 gdb_assert (prop->data.baton != NULL);
17062 }
17063 else if (attr->form_is_ref ())
17064 {
17065 struct dwarf2_cu *target_cu = cu;
17066 struct die_info *target_die;
17067 struct attribute *target_attr;
17068
17069 target_die = follow_die_ref (die, attr, &target_cu);
17070 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17071 if (target_attr == NULL)
17072 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17073 target_cu);
17074 if (target_attr == NULL)
17075 return 0;
17076
17077 switch (target_attr->name)
17078 {
17079 case DW_AT_location:
17080 if (target_attr->form_is_section_offset ())
17081 {
17082 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17083 baton->property_type = die_type (target_die, target_cu);
17084 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17085 prop->data.baton = baton;
17086 prop->kind = PROP_LOCLIST;
17087 gdb_assert (prop->data.baton != NULL);
17088 }
17089 else if (target_attr->form_is_block ())
17090 {
17091 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17092 baton->property_type = die_type (target_die, target_cu);
17093 baton->locexpr.per_cu = cu->per_cu;
17094 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17095 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17096 baton->locexpr.is_reference = true;
17097 prop->data.baton = baton;
17098 prop->kind = PROP_LOCEXPR;
17099 gdb_assert (prop->data.baton != NULL);
17100 }
17101 else
17102 {
17103 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17104 "dynamic property");
17105 return 0;
17106 }
17107 break;
17108 case DW_AT_data_member_location:
17109 {
17110 LONGEST offset;
17111
17112 if (!handle_data_member_location (target_die, target_cu,
17113 &offset))
17114 return 0;
17115
17116 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17117 baton->property_type = read_type_die (target_die->parent,
17118 target_cu);
17119 baton->offset_info.offset = offset;
17120 baton->offset_info.type = die_type (target_die, target_cu);
17121 prop->data.baton = baton;
17122 prop->kind = PROP_ADDR_OFFSET;
17123 break;
17124 }
17125 }
17126 }
17127 else if (attr->form_is_constant ())
17128 {
17129 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17130 prop->kind = PROP_CONST;
17131 }
17132 else
17133 {
17134 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17135 dwarf2_name (die, cu));
17136 return 0;
17137 }
17138
17139 return 1;
17140 }
17141
17142 /* See read.h. */
17143
17144 struct type *
17145 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17146 {
17147 struct objfile *objfile = dwarf2_per_objfile->objfile;
17148 struct type *int_type;
17149
17150 /* Helper macro to examine the various builtin types. */
17151 #define TRY_TYPE(F) \
17152 int_type = (unsigned_p \
17153 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17154 : objfile_type (objfile)->builtin_ ## F); \
17155 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17156 return int_type
17157
17158 TRY_TYPE (char);
17159 TRY_TYPE (short);
17160 TRY_TYPE (int);
17161 TRY_TYPE (long);
17162 TRY_TYPE (long_long);
17163
17164 #undef TRY_TYPE
17165
17166 gdb_assert_not_reached ("unable to find suitable integer type");
17167 }
17168
17169 /* See read.h. */
17170
17171 struct type *
17172 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17173 {
17174 int addr_size = this->addr_size ();
17175 return int_type (addr_size, unsigned_p);
17176 }
17177
17178 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17179 present (which is valid) then compute the default type based on the
17180 compilation units address size. */
17181
17182 static struct type *
17183 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17184 {
17185 struct type *index_type = die_type (die, cu);
17186
17187 /* Dwarf-2 specifications explicitly allows to create subrange types
17188 without specifying a base type.
17189 In that case, the base type must be set to the type of
17190 the lower bound, upper bound or count, in that order, if any of these
17191 three attributes references an object that has a type.
17192 If no base type is found, the Dwarf-2 specifications say that
17193 a signed integer type of size equal to the size of an address should
17194 be used.
17195 For the following C code: `extern char gdb_int [];'
17196 GCC produces an empty range DIE.
17197 FIXME: muller/2010-05-28: Possible references to object for low bound,
17198 high bound or count are not yet handled by this code. */
17199 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17200 index_type = cu->per_cu->addr_sized_int_type (false);
17201
17202 return index_type;
17203 }
17204
17205 /* Read the given DW_AT_subrange DIE. */
17206
17207 static struct type *
17208 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17209 {
17210 struct type *base_type, *orig_base_type;
17211 struct type *range_type;
17212 struct attribute *attr;
17213 struct dynamic_prop low, high;
17214 int low_default_is_valid;
17215 int high_bound_is_count = 0;
17216 const char *name;
17217 ULONGEST negative_mask;
17218
17219 orig_base_type = read_subrange_index_type (die, cu);
17220
17221 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17222 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17223 creating the range type, but we use the result of check_typedef
17224 when examining properties of the type. */
17225 base_type = check_typedef (orig_base_type);
17226
17227 /* The die_type call above may have already set the type for this DIE. */
17228 range_type = get_die_type (die, cu);
17229 if (range_type)
17230 return range_type;
17231
17232 low.kind = PROP_CONST;
17233 high.kind = PROP_CONST;
17234 high.data.const_val = 0;
17235
17236 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17237 omitting DW_AT_lower_bound. */
17238 switch (cu->language)
17239 {
17240 case language_c:
17241 case language_cplus:
17242 low.data.const_val = 0;
17243 low_default_is_valid = 1;
17244 break;
17245 case language_fortran:
17246 low.data.const_val = 1;
17247 low_default_is_valid = 1;
17248 break;
17249 case language_d:
17250 case language_objc:
17251 case language_rust:
17252 low.data.const_val = 0;
17253 low_default_is_valid = (cu->header.version >= 4);
17254 break;
17255 case language_ada:
17256 case language_m2:
17257 case language_pascal:
17258 low.data.const_val = 1;
17259 low_default_is_valid = (cu->header.version >= 4);
17260 break;
17261 default:
17262 low.data.const_val = 0;
17263 low_default_is_valid = 0;
17264 break;
17265 }
17266
17267 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17268 if (attr != nullptr)
17269 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17270 else if (!low_default_is_valid)
17271 complaint (_("Missing DW_AT_lower_bound "
17272 "- DIE at %s [in module %s]"),
17273 sect_offset_str (die->sect_off),
17274 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17275
17276 struct attribute *attr_ub, *attr_count;
17277 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17278 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17279 {
17280 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17281 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17282 {
17283 /* If bounds are constant do the final calculation here. */
17284 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17285 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17286 else
17287 high_bound_is_count = 1;
17288 }
17289 else
17290 {
17291 if (attr_ub != NULL)
17292 complaint (_("Unresolved DW_AT_upper_bound "
17293 "- DIE at %s [in module %s]"),
17294 sect_offset_str (die->sect_off),
17295 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17296 if (attr_count != NULL)
17297 complaint (_("Unresolved DW_AT_count "
17298 "- DIE at %s [in module %s]"),
17299 sect_offset_str (die->sect_off),
17300 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17301 }
17302 }
17303
17304 LONGEST bias = 0;
17305 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17306 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17307 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17308
17309 /* Normally, the DWARF producers are expected to use a signed
17310 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17311 But this is unfortunately not always the case, as witnessed
17312 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17313 is used instead. To work around that ambiguity, we treat
17314 the bounds as signed, and thus sign-extend their values, when
17315 the base type is signed. */
17316 negative_mask =
17317 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17318 if (low.kind == PROP_CONST
17319 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17320 low.data.const_val |= negative_mask;
17321 if (high.kind == PROP_CONST
17322 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17323 high.data.const_val |= negative_mask;
17324
17325 /* Check for bit and byte strides. */
17326 struct dynamic_prop byte_stride_prop;
17327 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17328 if (attr_byte_stride != nullptr)
17329 {
17330 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17331 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17332 prop_type);
17333 }
17334
17335 struct dynamic_prop bit_stride_prop;
17336 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17337 if (attr_bit_stride != nullptr)
17338 {
17339 /* It only makes sense to have either a bit or byte stride. */
17340 if (attr_byte_stride != nullptr)
17341 {
17342 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17343 "- DIE at %s [in module %s]"),
17344 sect_offset_str (die->sect_off),
17345 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17346 attr_bit_stride = nullptr;
17347 }
17348 else
17349 {
17350 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17351 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17352 prop_type);
17353 }
17354 }
17355
17356 if (attr_byte_stride != nullptr
17357 || attr_bit_stride != nullptr)
17358 {
17359 bool byte_stride_p = (attr_byte_stride != nullptr);
17360 struct dynamic_prop *stride
17361 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17362
17363 range_type
17364 = create_range_type_with_stride (NULL, orig_base_type, &low,
17365 &high, bias, stride, byte_stride_p);
17366 }
17367 else
17368 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17369
17370 if (high_bound_is_count)
17371 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17372
17373 /* Ada expects an empty array on no boundary attributes. */
17374 if (attr == NULL && cu->language != language_ada)
17375 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17376
17377 name = dwarf2_name (die, cu);
17378 if (name)
17379 TYPE_NAME (range_type) = name;
17380
17381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17382 if (attr != nullptr)
17383 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17384
17385 maybe_set_alignment (cu, die, range_type);
17386
17387 set_die_type (die, range_type, cu);
17388
17389 /* set_die_type should be already done. */
17390 set_descriptive_type (range_type, die, cu);
17391
17392 return range_type;
17393 }
17394
17395 static struct type *
17396 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17397 {
17398 struct type *type;
17399
17400 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17401 NULL);
17402 TYPE_NAME (type) = dwarf2_name (die, cu);
17403
17404 /* In Ada, an unspecified type is typically used when the description
17405 of the type is deferred to a different unit. When encountering
17406 such a type, we treat it as a stub, and try to resolve it later on,
17407 when needed. */
17408 if (cu->language == language_ada)
17409 TYPE_STUB (type) = 1;
17410
17411 return set_die_type (die, type, cu);
17412 }
17413
17414 /* Read a single die and all its descendents. Set the die's sibling
17415 field to NULL; set other fields in the die correctly, and set all
17416 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17417 location of the info_ptr after reading all of those dies. PARENT
17418 is the parent of the die in question. */
17419
17420 static struct die_info *
17421 read_die_and_children (const struct die_reader_specs *reader,
17422 const gdb_byte *info_ptr,
17423 const gdb_byte **new_info_ptr,
17424 struct die_info *parent)
17425 {
17426 struct die_info *die;
17427 const gdb_byte *cur_ptr;
17428
17429 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17430 if (die == NULL)
17431 {
17432 *new_info_ptr = cur_ptr;
17433 return NULL;
17434 }
17435 store_in_ref_table (die, reader->cu);
17436
17437 if (die->has_children)
17438 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17439 else
17440 {
17441 die->child = NULL;
17442 *new_info_ptr = cur_ptr;
17443 }
17444
17445 die->sibling = NULL;
17446 die->parent = parent;
17447 return die;
17448 }
17449
17450 /* Read a die, all of its descendents, and all of its siblings; set
17451 all of the fields of all of the dies correctly. Arguments are as
17452 in read_die_and_children. */
17453
17454 static struct die_info *
17455 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17456 const gdb_byte *info_ptr,
17457 const gdb_byte **new_info_ptr,
17458 struct die_info *parent)
17459 {
17460 struct die_info *first_die, *last_sibling;
17461 const gdb_byte *cur_ptr;
17462
17463 cur_ptr = info_ptr;
17464 first_die = last_sibling = NULL;
17465
17466 while (1)
17467 {
17468 struct die_info *die
17469 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17470
17471 if (die == NULL)
17472 {
17473 *new_info_ptr = cur_ptr;
17474 return first_die;
17475 }
17476
17477 if (!first_die)
17478 first_die = die;
17479 else
17480 last_sibling->sibling = die;
17481
17482 last_sibling = die;
17483 }
17484 }
17485
17486 /* Read a die, all of its descendents, and all of its siblings; set
17487 all of the fields of all of the dies correctly. Arguments are as
17488 in read_die_and_children.
17489 This the main entry point for reading a DIE and all its children. */
17490
17491 static struct die_info *
17492 read_die_and_siblings (const struct die_reader_specs *reader,
17493 const gdb_byte *info_ptr,
17494 const gdb_byte **new_info_ptr,
17495 struct die_info *parent)
17496 {
17497 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17498 new_info_ptr, parent);
17499
17500 if (dwarf_die_debug)
17501 {
17502 fprintf_unfiltered (gdb_stdlog,
17503 "Read die from %s@0x%x of %s:\n",
17504 reader->die_section->get_name (),
17505 (unsigned) (info_ptr - reader->die_section->buffer),
17506 bfd_get_filename (reader->abfd));
17507 dump_die (die, dwarf_die_debug);
17508 }
17509
17510 return die;
17511 }
17512
17513 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17514 attributes.
17515 The caller is responsible for filling in the extra attributes
17516 and updating (*DIEP)->num_attrs.
17517 Set DIEP to point to a newly allocated die with its information,
17518 except for its child, sibling, and parent fields. */
17519
17520 static const gdb_byte *
17521 read_full_die_1 (const struct die_reader_specs *reader,
17522 struct die_info **diep, const gdb_byte *info_ptr,
17523 int num_extra_attrs)
17524 {
17525 unsigned int abbrev_number, bytes_read, i;
17526 struct abbrev_info *abbrev;
17527 struct die_info *die;
17528 struct dwarf2_cu *cu = reader->cu;
17529 bfd *abfd = reader->abfd;
17530
17531 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17532 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17533 info_ptr += bytes_read;
17534 if (!abbrev_number)
17535 {
17536 *diep = NULL;
17537 return info_ptr;
17538 }
17539
17540 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17541 if (!abbrev)
17542 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17543 abbrev_number,
17544 bfd_get_filename (abfd));
17545
17546 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17547 die->sect_off = sect_off;
17548 die->tag = abbrev->tag;
17549 die->abbrev = abbrev_number;
17550 die->has_children = abbrev->has_children;
17551
17552 /* Make the result usable.
17553 The caller needs to update num_attrs after adding the extra
17554 attributes. */
17555 die->num_attrs = abbrev->num_attrs;
17556
17557 std::vector<int> indexes_that_need_reprocess;
17558 for (i = 0; i < abbrev->num_attrs; ++i)
17559 {
17560 bool need_reprocess;
17561 info_ptr =
17562 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17563 info_ptr, &need_reprocess);
17564 if (need_reprocess)
17565 indexes_that_need_reprocess.push_back (i);
17566 }
17567
17568 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17569 if (attr != nullptr)
17570 cu->str_offsets_base = DW_UNSND (attr);
17571
17572 auto maybe_addr_base = lookup_addr_base(die);
17573 if (maybe_addr_base.has_value ())
17574 cu->addr_base = *maybe_addr_base;
17575 for (int index : indexes_that_need_reprocess)
17576 read_attribute_reprocess (reader, &die->attrs[index]);
17577 *diep = die;
17578 return info_ptr;
17579 }
17580
17581 /* Read a die and all its attributes.
17582 Set DIEP to point to a newly allocated die with its information,
17583 except for its child, sibling, and parent fields. */
17584
17585 static const gdb_byte *
17586 read_full_die (const struct die_reader_specs *reader,
17587 struct die_info **diep, const gdb_byte *info_ptr)
17588 {
17589 const gdb_byte *result;
17590
17591 result = read_full_die_1 (reader, diep, info_ptr, 0);
17592
17593 if (dwarf_die_debug)
17594 {
17595 fprintf_unfiltered (gdb_stdlog,
17596 "Read die from %s@0x%x of %s:\n",
17597 reader->die_section->get_name (),
17598 (unsigned) (info_ptr - reader->die_section->buffer),
17599 bfd_get_filename (reader->abfd));
17600 dump_die (*diep, dwarf_die_debug);
17601 }
17602
17603 return result;
17604 }
17605 \f
17606
17607 /* Returns nonzero if TAG represents a type that we might generate a partial
17608 symbol for. */
17609
17610 static int
17611 is_type_tag_for_partial (int tag)
17612 {
17613 switch (tag)
17614 {
17615 #if 0
17616 /* Some types that would be reasonable to generate partial symbols for,
17617 that we don't at present. */
17618 case DW_TAG_array_type:
17619 case DW_TAG_file_type:
17620 case DW_TAG_ptr_to_member_type:
17621 case DW_TAG_set_type:
17622 case DW_TAG_string_type:
17623 case DW_TAG_subroutine_type:
17624 #endif
17625 case DW_TAG_base_type:
17626 case DW_TAG_class_type:
17627 case DW_TAG_interface_type:
17628 case DW_TAG_enumeration_type:
17629 case DW_TAG_structure_type:
17630 case DW_TAG_subrange_type:
17631 case DW_TAG_typedef:
17632 case DW_TAG_union_type:
17633 return 1;
17634 default:
17635 return 0;
17636 }
17637 }
17638
17639 /* Load all DIEs that are interesting for partial symbols into memory. */
17640
17641 static struct partial_die_info *
17642 load_partial_dies (const struct die_reader_specs *reader,
17643 const gdb_byte *info_ptr, int building_psymtab)
17644 {
17645 struct dwarf2_cu *cu = reader->cu;
17646 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17647 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17648 unsigned int bytes_read;
17649 unsigned int load_all = 0;
17650 int nesting_level = 1;
17651
17652 parent_die = NULL;
17653 last_die = NULL;
17654
17655 gdb_assert (cu->per_cu != NULL);
17656 if (cu->per_cu->load_all_dies)
17657 load_all = 1;
17658
17659 cu->partial_dies
17660 = htab_create_alloc_ex (cu->header.length / 12,
17661 partial_die_hash,
17662 partial_die_eq,
17663 NULL,
17664 &cu->comp_unit_obstack,
17665 hashtab_obstack_allocate,
17666 dummy_obstack_deallocate);
17667
17668 while (1)
17669 {
17670 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17671
17672 /* A NULL abbrev means the end of a series of children. */
17673 if (abbrev == NULL)
17674 {
17675 if (--nesting_level == 0)
17676 return first_die;
17677
17678 info_ptr += bytes_read;
17679 last_die = parent_die;
17680 parent_die = parent_die->die_parent;
17681 continue;
17682 }
17683
17684 /* Check for template arguments. We never save these; if
17685 they're seen, we just mark the parent, and go on our way. */
17686 if (parent_die != NULL
17687 && cu->language == language_cplus
17688 && (abbrev->tag == DW_TAG_template_type_param
17689 || abbrev->tag == DW_TAG_template_value_param))
17690 {
17691 parent_die->has_template_arguments = 1;
17692
17693 if (!load_all)
17694 {
17695 /* We don't need a partial DIE for the template argument. */
17696 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17697 continue;
17698 }
17699 }
17700
17701 /* We only recurse into c++ subprograms looking for template arguments.
17702 Skip their other children. */
17703 if (!load_all
17704 && cu->language == language_cplus
17705 && parent_die != NULL
17706 && parent_die->tag == DW_TAG_subprogram)
17707 {
17708 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17709 continue;
17710 }
17711
17712 /* Check whether this DIE is interesting enough to save. Normally
17713 we would not be interested in members here, but there may be
17714 later variables referencing them via DW_AT_specification (for
17715 static members). */
17716 if (!load_all
17717 && !is_type_tag_for_partial (abbrev->tag)
17718 && abbrev->tag != DW_TAG_constant
17719 && abbrev->tag != DW_TAG_enumerator
17720 && abbrev->tag != DW_TAG_subprogram
17721 && abbrev->tag != DW_TAG_inlined_subroutine
17722 && abbrev->tag != DW_TAG_lexical_block
17723 && abbrev->tag != DW_TAG_variable
17724 && abbrev->tag != DW_TAG_namespace
17725 && abbrev->tag != DW_TAG_module
17726 && abbrev->tag != DW_TAG_member
17727 && abbrev->tag != DW_TAG_imported_unit
17728 && abbrev->tag != DW_TAG_imported_declaration)
17729 {
17730 /* Otherwise we skip to the next sibling, if any. */
17731 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17732 continue;
17733 }
17734
17735 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17736 abbrev);
17737
17738 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17739
17740 /* This two-pass algorithm for processing partial symbols has a
17741 high cost in cache pressure. Thus, handle some simple cases
17742 here which cover the majority of C partial symbols. DIEs
17743 which neither have specification tags in them, nor could have
17744 specification tags elsewhere pointing at them, can simply be
17745 processed and discarded.
17746
17747 This segment is also optional; scan_partial_symbols and
17748 add_partial_symbol will handle these DIEs if we chain
17749 them in normally. When compilers which do not emit large
17750 quantities of duplicate debug information are more common,
17751 this code can probably be removed. */
17752
17753 /* Any complete simple types at the top level (pretty much all
17754 of them, for a language without namespaces), can be processed
17755 directly. */
17756 if (parent_die == NULL
17757 && pdi.has_specification == 0
17758 && pdi.is_declaration == 0
17759 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17760 || pdi.tag == DW_TAG_base_type
17761 || pdi.tag == DW_TAG_subrange_type))
17762 {
17763 if (building_psymtab && pdi.name != NULL)
17764 add_psymbol_to_list (pdi.name, false,
17765 VAR_DOMAIN, LOC_TYPEDEF, -1,
17766 psymbol_placement::STATIC,
17767 0, cu->language, objfile);
17768 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17769 continue;
17770 }
17771
17772 /* The exception for DW_TAG_typedef with has_children above is
17773 a workaround of GCC PR debug/47510. In the case of this complaint
17774 type_name_or_error will error on such types later.
17775
17776 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17777 it could not find the child DIEs referenced later, this is checked
17778 above. In correct DWARF DW_TAG_typedef should have no children. */
17779
17780 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17781 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17782 "- DIE at %s [in module %s]"),
17783 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17784
17785 /* If we're at the second level, and we're an enumerator, and
17786 our parent has no specification (meaning possibly lives in a
17787 namespace elsewhere), then we can add the partial symbol now
17788 instead of queueing it. */
17789 if (pdi.tag == DW_TAG_enumerator
17790 && parent_die != NULL
17791 && parent_die->die_parent == NULL
17792 && parent_die->tag == DW_TAG_enumeration_type
17793 && parent_die->has_specification == 0)
17794 {
17795 if (pdi.name == NULL)
17796 complaint (_("malformed enumerator DIE ignored"));
17797 else if (building_psymtab)
17798 add_psymbol_to_list (pdi.name, false,
17799 VAR_DOMAIN, LOC_CONST, -1,
17800 cu->language == language_cplus
17801 ? psymbol_placement::GLOBAL
17802 : psymbol_placement::STATIC,
17803 0, cu->language, objfile);
17804
17805 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17806 continue;
17807 }
17808
17809 struct partial_die_info *part_die
17810 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17811
17812 /* We'll save this DIE so link it in. */
17813 part_die->die_parent = parent_die;
17814 part_die->die_sibling = NULL;
17815 part_die->die_child = NULL;
17816
17817 if (last_die && last_die == parent_die)
17818 last_die->die_child = part_die;
17819 else if (last_die)
17820 last_die->die_sibling = part_die;
17821
17822 last_die = part_die;
17823
17824 if (first_die == NULL)
17825 first_die = part_die;
17826
17827 /* Maybe add the DIE to the hash table. Not all DIEs that we
17828 find interesting need to be in the hash table, because we
17829 also have the parent/sibling/child chains; only those that we
17830 might refer to by offset later during partial symbol reading.
17831
17832 For now this means things that might have be the target of a
17833 DW_AT_specification, DW_AT_abstract_origin, or
17834 DW_AT_extension. DW_AT_extension will refer only to
17835 namespaces; DW_AT_abstract_origin refers to functions (and
17836 many things under the function DIE, but we do not recurse
17837 into function DIEs during partial symbol reading) and
17838 possibly variables as well; DW_AT_specification refers to
17839 declarations. Declarations ought to have the DW_AT_declaration
17840 flag. It happens that GCC forgets to put it in sometimes, but
17841 only for functions, not for types.
17842
17843 Adding more things than necessary to the hash table is harmless
17844 except for the performance cost. Adding too few will result in
17845 wasted time in find_partial_die, when we reread the compilation
17846 unit with load_all_dies set. */
17847
17848 if (load_all
17849 || abbrev->tag == DW_TAG_constant
17850 || abbrev->tag == DW_TAG_subprogram
17851 || abbrev->tag == DW_TAG_variable
17852 || abbrev->tag == DW_TAG_namespace
17853 || part_die->is_declaration)
17854 {
17855 void **slot;
17856
17857 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17858 to_underlying (part_die->sect_off),
17859 INSERT);
17860 *slot = part_die;
17861 }
17862
17863 /* For some DIEs we want to follow their children (if any). For C
17864 we have no reason to follow the children of structures; for other
17865 languages we have to, so that we can get at method physnames
17866 to infer fully qualified class names, for DW_AT_specification,
17867 and for C++ template arguments. For C++, we also look one level
17868 inside functions to find template arguments (if the name of the
17869 function does not already contain the template arguments).
17870
17871 For Ada and Fortran, we need to scan the children of subprograms
17872 and lexical blocks as well because these languages allow the
17873 definition of nested entities that could be interesting for the
17874 debugger, such as nested subprograms for instance. */
17875 if (last_die->has_children
17876 && (load_all
17877 || last_die->tag == DW_TAG_namespace
17878 || last_die->tag == DW_TAG_module
17879 || last_die->tag == DW_TAG_enumeration_type
17880 || (cu->language == language_cplus
17881 && last_die->tag == DW_TAG_subprogram
17882 && (last_die->name == NULL
17883 || strchr (last_die->name, '<') == NULL))
17884 || (cu->language != language_c
17885 && (last_die->tag == DW_TAG_class_type
17886 || last_die->tag == DW_TAG_interface_type
17887 || last_die->tag == DW_TAG_structure_type
17888 || last_die->tag == DW_TAG_union_type))
17889 || ((cu->language == language_ada
17890 || cu->language == language_fortran)
17891 && (last_die->tag == DW_TAG_subprogram
17892 || last_die->tag == DW_TAG_lexical_block))))
17893 {
17894 nesting_level++;
17895 parent_die = last_die;
17896 continue;
17897 }
17898
17899 /* Otherwise we skip to the next sibling, if any. */
17900 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17901
17902 /* Back to the top, do it again. */
17903 }
17904 }
17905
17906 partial_die_info::partial_die_info (sect_offset sect_off_,
17907 struct abbrev_info *abbrev)
17908 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17909 {
17910 }
17911
17912 /* Read a minimal amount of information into the minimal die structure.
17913 INFO_PTR should point just after the initial uleb128 of a DIE. */
17914
17915 const gdb_byte *
17916 partial_die_info::read (const struct die_reader_specs *reader,
17917 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17918 {
17919 struct dwarf2_cu *cu = reader->cu;
17920 struct dwarf2_per_objfile *dwarf2_per_objfile
17921 = cu->per_cu->dwarf2_per_objfile;
17922 unsigned int i;
17923 int has_low_pc_attr = 0;
17924 int has_high_pc_attr = 0;
17925 int high_pc_relative = 0;
17926
17927 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17928 for (i = 0; i < abbrev.num_attrs; ++i)
17929 {
17930 bool need_reprocess;
17931 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17932 info_ptr, &need_reprocess);
17933 /* String and address offsets that need to do the reprocessing have
17934 already been read at this point, so there is no need to wait until
17935 the loop terminates to do the reprocessing. */
17936 if (need_reprocess)
17937 read_attribute_reprocess (reader, &attr_vec[i]);
17938 attribute &attr = attr_vec[i];
17939 /* Store the data if it is of an attribute we want to keep in a
17940 partial symbol table. */
17941 switch (attr.name)
17942 {
17943 case DW_AT_name:
17944 switch (tag)
17945 {
17946 case DW_TAG_compile_unit:
17947 case DW_TAG_partial_unit:
17948 case DW_TAG_type_unit:
17949 /* Compilation units have a DW_AT_name that is a filename, not
17950 a source language identifier. */
17951 case DW_TAG_enumeration_type:
17952 case DW_TAG_enumerator:
17953 /* These tags always have simple identifiers already; no need
17954 to canonicalize them. */
17955 name = DW_STRING (&attr);
17956 break;
17957 default:
17958 {
17959 struct objfile *objfile = dwarf2_per_objfile->objfile;
17960
17961 name
17962 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17963 &objfile->per_bfd->storage_obstack);
17964 }
17965 break;
17966 }
17967 break;
17968 case DW_AT_linkage_name:
17969 case DW_AT_MIPS_linkage_name:
17970 /* Note that both forms of linkage name might appear. We
17971 assume they will be the same, and we only store the last
17972 one we see. */
17973 linkage_name = DW_STRING (&attr);
17974 break;
17975 case DW_AT_low_pc:
17976 has_low_pc_attr = 1;
17977 lowpc = attr.value_as_address ();
17978 break;
17979 case DW_AT_high_pc:
17980 has_high_pc_attr = 1;
17981 highpc = attr.value_as_address ();
17982 if (cu->header.version >= 4 && attr.form_is_constant ())
17983 high_pc_relative = 1;
17984 break;
17985 case DW_AT_location:
17986 /* Support the .debug_loc offsets. */
17987 if (attr.form_is_block ())
17988 {
17989 d.locdesc = DW_BLOCK (&attr);
17990 }
17991 else if (attr.form_is_section_offset ())
17992 {
17993 dwarf2_complex_location_expr_complaint ();
17994 }
17995 else
17996 {
17997 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17998 "partial symbol information");
17999 }
18000 break;
18001 case DW_AT_external:
18002 is_external = DW_UNSND (&attr);
18003 break;
18004 case DW_AT_declaration:
18005 is_declaration = DW_UNSND (&attr);
18006 break;
18007 case DW_AT_type:
18008 has_type = 1;
18009 break;
18010 case DW_AT_abstract_origin:
18011 case DW_AT_specification:
18012 case DW_AT_extension:
18013 has_specification = 1;
18014 spec_offset = dwarf2_get_ref_die_offset (&attr);
18015 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18016 || cu->per_cu->is_dwz);
18017 break;
18018 case DW_AT_sibling:
18019 /* Ignore absolute siblings, they might point outside of
18020 the current compile unit. */
18021 if (attr.form == DW_FORM_ref_addr)
18022 complaint (_("ignoring absolute DW_AT_sibling"));
18023 else
18024 {
18025 const gdb_byte *buffer = reader->buffer;
18026 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18027 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18028
18029 if (sibling_ptr < info_ptr)
18030 complaint (_("DW_AT_sibling points backwards"));
18031 else if (sibling_ptr > reader->buffer_end)
18032 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18033 else
18034 sibling = sibling_ptr;
18035 }
18036 break;
18037 case DW_AT_byte_size:
18038 has_byte_size = 1;
18039 break;
18040 case DW_AT_const_value:
18041 has_const_value = 1;
18042 break;
18043 case DW_AT_calling_convention:
18044 /* DWARF doesn't provide a way to identify a program's source-level
18045 entry point. DW_AT_calling_convention attributes are only meant
18046 to describe functions' calling conventions.
18047
18048 However, because it's a necessary piece of information in
18049 Fortran, and before DWARF 4 DW_CC_program was the only
18050 piece of debugging information whose definition refers to
18051 a 'main program' at all, several compilers marked Fortran
18052 main programs with DW_CC_program --- even when those
18053 functions use the standard calling conventions.
18054
18055 Although DWARF now specifies a way to provide this
18056 information, we support this practice for backward
18057 compatibility. */
18058 if (DW_UNSND (&attr) == DW_CC_program
18059 && cu->language == language_fortran)
18060 main_subprogram = 1;
18061 break;
18062 case DW_AT_inline:
18063 if (DW_UNSND (&attr) == DW_INL_inlined
18064 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18065 may_be_inlined = 1;
18066 break;
18067
18068 case DW_AT_import:
18069 if (tag == DW_TAG_imported_unit)
18070 {
18071 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18072 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18073 || cu->per_cu->is_dwz);
18074 }
18075 break;
18076
18077 case DW_AT_main_subprogram:
18078 main_subprogram = DW_UNSND (&attr);
18079 break;
18080
18081 case DW_AT_ranges:
18082 {
18083 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18084 but that requires a full DIE, so instead we just
18085 reimplement it. */
18086 int need_ranges_base = tag != DW_TAG_compile_unit;
18087 unsigned int ranges_offset = (DW_UNSND (&attr)
18088 + (need_ranges_base
18089 ? cu->ranges_base
18090 : 0));
18091
18092 /* Value of the DW_AT_ranges attribute is the offset in the
18093 .debug_ranges section. */
18094 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18095 nullptr))
18096 has_pc_info = 1;
18097 }
18098 break;
18099
18100 default:
18101 break;
18102 }
18103 }
18104
18105 /* For Ada, if both the name and the linkage name appear, we prefer
18106 the latter. This lets "catch exception" work better, regardless
18107 of the order in which the name and linkage name were emitted.
18108 Really, though, this is just a workaround for the fact that gdb
18109 doesn't store both the name and the linkage name. */
18110 if (cu->language == language_ada && linkage_name != nullptr)
18111 name = linkage_name;
18112
18113 if (high_pc_relative)
18114 highpc += lowpc;
18115
18116 if (has_low_pc_attr && has_high_pc_attr)
18117 {
18118 /* When using the GNU linker, .gnu.linkonce. sections are used to
18119 eliminate duplicate copies of functions and vtables and such.
18120 The linker will arbitrarily choose one and discard the others.
18121 The AT_*_pc values for such functions refer to local labels in
18122 these sections. If the section from that file was discarded, the
18123 labels are not in the output, so the relocs get a value of 0.
18124 If this is a discarded function, mark the pc bounds as invalid,
18125 so that GDB will ignore it. */
18126 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18127 {
18128 struct objfile *objfile = dwarf2_per_objfile->objfile;
18129 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18130
18131 complaint (_("DW_AT_low_pc %s is zero "
18132 "for DIE at %s [in module %s]"),
18133 paddress (gdbarch, lowpc),
18134 sect_offset_str (sect_off),
18135 objfile_name (objfile));
18136 }
18137 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18138 else if (lowpc >= highpc)
18139 {
18140 struct objfile *objfile = dwarf2_per_objfile->objfile;
18141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18142
18143 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18144 "for DIE at %s [in module %s]"),
18145 paddress (gdbarch, lowpc),
18146 paddress (gdbarch, highpc),
18147 sect_offset_str (sect_off),
18148 objfile_name (objfile));
18149 }
18150 else
18151 has_pc_info = 1;
18152 }
18153
18154 return info_ptr;
18155 }
18156
18157 /* Find a cached partial DIE at OFFSET in CU. */
18158
18159 struct partial_die_info *
18160 dwarf2_cu::find_partial_die (sect_offset sect_off)
18161 {
18162 struct partial_die_info *lookup_die = NULL;
18163 struct partial_die_info part_die (sect_off);
18164
18165 lookup_die = ((struct partial_die_info *)
18166 htab_find_with_hash (partial_dies, &part_die,
18167 to_underlying (sect_off)));
18168
18169 return lookup_die;
18170 }
18171
18172 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18173 except in the case of .debug_types DIEs which do not reference
18174 outside their CU (they do however referencing other types via
18175 DW_FORM_ref_sig8). */
18176
18177 static const struct cu_partial_die_info
18178 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18179 {
18180 struct dwarf2_per_objfile *dwarf2_per_objfile
18181 = cu->per_cu->dwarf2_per_objfile;
18182 struct objfile *objfile = dwarf2_per_objfile->objfile;
18183 struct dwarf2_per_cu_data *per_cu = NULL;
18184 struct partial_die_info *pd = NULL;
18185
18186 if (offset_in_dwz == cu->per_cu->is_dwz
18187 && cu->header.offset_in_cu_p (sect_off))
18188 {
18189 pd = cu->find_partial_die (sect_off);
18190 if (pd != NULL)
18191 return { cu, pd };
18192 /* We missed recording what we needed.
18193 Load all dies and try again. */
18194 per_cu = cu->per_cu;
18195 }
18196 else
18197 {
18198 /* TUs don't reference other CUs/TUs (except via type signatures). */
18199 if (cu->per_cu->is_debug_types)
18200 {
18201 error (_("Dwarf Error: Type Unit at offset %s contains"
18202 " external reference to offset %s [in module %s].\n"),
18203 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18204 bfd_get_filename (objfile->obfd));
18205 }
18206 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18207 dwarf2_per_objfile);
18208
18209 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18210 load_partial_comp_unit (per_cu);
18211
18212 per_cu->cu->last_used = 0;
18213 pd = per_cu->cu->find_partial_die (sect_off);
18214 }
18215
18216 /* If we didn't find it, and not all dies have been loaded,
18217 load them all and try again. */
18218
18219 if (pd == NULL && per_cu->load_all_dies == 0)
18220 {
18221 per_cu->load_all_dies = 1;
18222
18223 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18224 THIS_CU->cu may already be in use. So we can't just free it and
18225 replace its DIEs with the ones we read in. Instead, we leave those
18226 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18227 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18228 set. */
18229 load_partial_comp_unit (per_cu);
18230
18231 pd = per_cu->cu->find_partial_die (sect_off);
18232 }
18233
18234 if (pd == NULL)
18235 internal_error (__FILE__, __LINE__,
18236 _("could not find partial DIE %s "
18237 "in cache [from module %s]\n"),
18238 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18239 return { per_cu->cu, pd };
18240 }
18241
18242 /* See if we can figure out if the class lives in a namespace. We do
18243 this by looking for a member function; its demangled name will
18244 contain namespace info, if there is any. */
18245
18246 static void
18247 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18248 struct dwarf2_cu *cu)
18249 {
18250 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18251 what template types look like, because the demangler
18252 frequently doesn't give the same name as the debug info. We
18253 could fix this by only using the demangled name to get the
18254 prefix (but see comment in read_structure_type). */
18255
18256 struct partial_die_info *real_pdi;
18257 struct partial_die_info *child_pdi;
18258
18259 /* If this DIE (this DIE's specification, if any) has a parent, then
18260 we should not do this. We'll prepend the parent's fully qualified
18261 name when we create the partial symbol. */
18262
18263 real_pdi = struct_pdi;
18264 while (real_pdi->has_specification)
18265 {
18266 auto res = find_partial_die (real_pdi->spec_offset,
18267 real_pdi->spec_is_dwz, cu);
18268 real_pdi = res.pdi;
18269 cu = res.cu;
18270 }
18271
18272 if (real_pdi->die_parent != NULL)
18273 return;
18274
18275 for (child_pdi = struct_pdi->die_child;
18276 child_pdi != NULL;
18277 child_pdi = child_pdi->die_sibling)
18278 {
18279 if (child_pdi->tag == DW_TAG_subprogram
18280 && child_pdi->linkage_name != NULL)
18281 {
18282 gdb::unique_xmalloc_ptr<char> actual_class_name
18283 (language_class_name_from_physname (cu->language_defn,
18284 child_pdi->linkage_name));
18285 if (actual_class_name != NULL)
18286 {
18287 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18288 struct_pdi->name
18289 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18290 actual_class_name.get ());
18291 }
18292 break;
18293 }
18294 }
18295 }
18296
18297 void
18298 partial_die_info::fixup (struct dwarf2_cu *cu)
18299 {
18300 /* Once we've fixed up a die, there's no point in doing so again.
18301 This also avoids a memory leak if we were to call
18302 guess_partial_die_structure_name multiple times. */
18303 if (fixup_called)
18304 return;
18305
18306 /* If we found a reference attribute and the DIE has no name, try
18307 to find a name in the referred to DIE. */
18308
18309 if (name == NULL && has_specification)
18310 {
18311 struct partial_die_info *spec_die;
18312
18313 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18314 spec_die = res.pdi;
18315 cu = res.cu;
18316
18317 spec_die->fixup (cu);
18318
18319 if (spec_die->name)
18320 {
18321 name = spec_die->name;
18322
18323 /* Copy DW_AT_external attribute if it is set. */
18324 if (spec_die->is_external)
18325 is_external = spec_die->is_external;
18326 }
18327 }
18328
18329 /* Set default names for some unnamed DIEs. */
18330
18331 if (name == NULL && tag == DW_TAG_namespace)
18332 name = CP_ANONYMOUS_NAMESPACE_STR;
18333
18334 /* If there is no parent die to provide a namespace, and there are
18335 children, see if we can determine the namespace from their linkage
18336 name. */
18337 if (cu->language == language_cplus
18338 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18339 && die_parent == NULL
18340 && has_children
18341 && (tag == DW_TAG_class_type
18342 || tag == DW_TAG_structure_type
18343 || tag == DW_TAG_union_type))
18344 guess_partial_die_structure_name (this, cu);
18345
18346 /* GCC might emit a nameless struct or union that has a linkage
18347 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18348 if (name == NULL
18349 && (tag == DW_TAG_class_type
18350 || tag == DW_TAG_interface_type
18351 || tag == DW_TAG_structure_type
18352 || tag == DW_TAG_union_type)
18353 && linkage_name != NULL)
18354 {
18355 gdb::unique_xmalloc_ptr<char> demangled
18356 (gdb_demangle (linkage_name, DMGL_TYPES));
18357 if (demangled != nullptr)
18358 {
18359 const char *base;
18360
18361 /* Strip any leading namespaces/classes, keep only the base name.
18362 DW_AT_name for named DIEs does not contain the prefixes. */
18363 base = strrchr (demangled.get (), ':');
18364 if (base && base > demangled.get () && base[-1] == ':')
18365 base++;
18366 else
18367 base = demangled.get ();
18368
18369 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18370 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18371 }
18372 }
18373
18374 fixup_called = 1;
18375 }
18376
18377 /* Process the attributes that had to be skipped in the first round. These
18378 attributes are the ones that need str_offsets_base or addr_base attributes.
18379 They could not have been processed in the first round, because at the time
18380 the values of str_offsets_base or addr_base may not have been known. */
18381 void read_attribute_reprocess (const struct die_reader_specs *reader,
18382 struct attribute *attr)
18383 {
18384 struct dwarf2_cu *cu = reader->cu;
18385 switch (attr->form)
18386 {
18387 case DW_FORM_addrx:
18388 case DW_FORM_GNU_addr_index:
18389 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18390 break;
18391 case DW_FORM_strx:
18392 case DW_FORM_strx1:
18393 case DW_FORM_strx2:
18394 case DW_FORM_strx3:
18395 case DW_FORM_strx4:
18396 case DW_FORM_GNU_str_index:
18397 {
18398 unsigned int str_index = DW_UNSND (attr);
18399 if (reader->dwo_file != NULL)
18400 {
18401 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18402 DW_STRING_IS_CANONICAL (attr) = 0;
18403 }
18404 else
18405 {
18406 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18407 DW_STRING_IS_CANONICAL (attr) = 0;
18408 }
18409 break;
18410 }
18411 default:
18412 gdb_assert_not_reached (_("Unexpected DWARF form."));
18413 }
18414 }
18415
18416 /* Read an attribute value described by an attribute form. */
18417
18418 static const gdb_byte *
18419 read_attribute_value (const struct die_reader_specs *reader,
18420 struct attribute *attr, unsigned form,
18421 LONGEST implicit_const, const gdb_byte *info_ptr,
18422 bool *need_reprocess)
18423 {
18424 struct dwarf2_cu *cu = reader->cu;
18425 struct dwarf2_per_objfile *dwarf2_per_objfile
18426 = cu->per_cu->dwarf2_per_objfile;
18427 struct objfile *objfile = dwarf2_per_objfile->objfile;
18428 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18429 bfd *abfd = reader->abfd;
18430 struct comp_unit_head *cu_header = &cu->header;
18431 unsigned int bytes_read;
18432 struct dwarf_block *blk;
18433 *need_reprocess = false;
18434
18435 attr->form = (enum dwarf_form) form;
18436 switch (form)
18437 {
18438 case DW_FORM_ref_addr:
18439 if (cu->header.version == 2)
18440 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18441 &bytes_read);
18442 else
18443 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18444 &bytes_read);
18445 info_ptr += bytes_read;
18446 break;
18447 case DW_FORM_GNU_ref_alt:
18448 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18449 info_ptr += bytes_read;
18450 break;
18451 case DW_FORM_addr:
18452 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18453 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18454 info_ptr += bytes_read;
18455 break;
18456 case DW_FORM_block2:
18457 blk = dwarf_alloc_block (cu);
18458 blk->size = read_2_bytes (abfd, info_ptr);
18459 info_ptr += 2;
18460 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18461 info_ptr += blk->size;
18462 DW_BLOCK (attr) = blk;
18463 break;
18464 case DW_FORM_block4:
18465 blk = dwarf_alloc_block (cu);
18466 blk->size = read_4_bytes (abfd, info_ptr);
18467 info_ptr += 4;
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_data2:
18473 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18474 info_ptr += 2;
18475 break;
18476 case DW_FORM_data4:
18477 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18478 info_ptr += 4;
18479 break;
18480 case DW_FORM_data8:
18481 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18482 info_ptr += 8;
18483 break;
18484 case DW_FORM_data16:
18485 blk = dwarf_alloc_block (cu);
18486 blk->size = 16;
18487 blk->data = read_n_bytes (abfd, info_ptr, 16);
18488 info_ptr += 16;
18489 DW_BLOCK (attr) = blk;
18490 break;
18491 case DW_FORM_sec_offset:
18492 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18493 info_ptr += bytes_read;
18494 break;
18495 case DW_FORM_string:
18496 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18497 DW_STRING_IS_CANONICAL (attr) = 0;
18498 info_ptr += bytes_read;
18499 break;
18500 case DW_FORM_strp:
18501 if (!cu->per_cu->is_dwz)
18502 {
18503 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18504 abfd, info_ptr, cu_header,
18505 &bytes_read);
18506 DW_STRING_IS_CANONICAL (attr) = 0;
18507 info_ptr += bytes_read;
18508 break;
18509 }
18510 /* FALLTHROUGH */
18511 case DW_FORM_line_strp:
18512 if (!cu->per_cu->is_dwz)
18513 {
18514 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18515 abfd, info_ptr,
18516 cu_header, &bytes_read);
18517 DW_STRING_IS_CANONICAL (attr) = 0;
18518 info_ptr += bytes_read;
18519 break;
18520 }
18521 /* FALLTHROUGH */
18522 case DW_FORM_GNU_strp_alt:
18523 {
18524 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18525 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18526 &bytes_read);
18527
18528 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18529 dwz, str_offset);
18530 DW_STRING_IS_CANONICAL (attr) = 0;
18531 info_ptr += bytes_read;
18532 }
18533 break;
18534 case DW_FORM_exprloc:
18535 case DW_FORM_block:
18536 blk = dwarf_alloc_block (cu);
18537 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18538 info_ptr += bytes_read;
18539 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18540 info_ptr += blk->size;
18541 DW_BLOCK (attr) = blk;
18542 break;
18543 case DW_FORM_block1:
18544 blk = dwarf_alloc_block (cu);
18545 blk->size = read_1_byte (abfd, info_ptr);
18546 info_ptr += 1;
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_data1:
18552 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18553 info_ptr += 1;
18554 break;
18555 case DW_FORM_flag:
18556 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18557 info_ptr += 1;
18558 break;
18559 case DW_FORM_flag_present:
18560 DW_UNSND (attr) = 1;
18561 break;
18562 case DW_FORM_sdata:
18563 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18564 info_ptr += bytes_read;
18565 break;
18566 case DW_FORM_udata:
18567 case DW_FORM_rnglistx:
18568 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18569 info_ptr += bytes_read;
18570 break;
18571 case DW_FORM_ref1:
18572 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18573 + read_1_byte (abfd, info_ptr));
18574 info_ptr += 1;
18575 break;
18576 case DW_FORM_ref2:
18577 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18578 + read_2_bytes (abfd, info_ptr));
18579 info_ptr += 2;
18580 break;
18581 case DW_FORM_ref4:
18582 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18583 + read_4_bytes (abfd, info_ptr));
18584 info_ptr += 4;
18585 break;
18586 case DW_FORM_ref8:
18587 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18588 + read_8_bytes (abfd, info_ptr));
18589 info_ptr += 8;
18590 break;
18591 case DW_FORM_ref_sig8:
18592 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18593 info_ptr += 8;
18594 break;
18595 case DW_FORM_ref_udata:
18596 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18597 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18598 info_ptr += bytes_read;
18599 break;
18600 case DW_FORM_indirect:
18601 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18602 info_ptr += bytes_read;
18603 if (form == DW_FORM_implicit_const)
18604 {
18605 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18606 info_ptr += bytes_read;
18607 }
18608 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18609 info_ptr, need_reprocess);
18610 break;
18611 case DW_FORM_implicit_const:
18612 DW_SND (attr) = implicit_const;
18613 break;
18614 case DW_FORM_addrx:
18615 case DW_FORM_GNU_addr_index:
18616 *need_reprocess = true;
18617 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18618 info_ptr += bytes_read;
18619 break;
18620 case DW_FORM_strx:
18621 case DW_FORM_strx1:
18622 case DW_FORM_strx2:
18623 case DW_FORM_strx3:
18624 case DW_FORM_strx4:
18625 case DW_FORM_GNU_str_index:
18626 {
18627 ULONGEST str_index;
18628 if (form == DW_FORM_strx1)
18629 {
18630 str_index = read_1_byte (abfd, info_ptr);
18631 info_ptr += 1;
18632 }
18633 else if (form == DW_FORM_strx2)
18634 {
18635 str_index = read_2_bytes (abfd, info_ptr);
18636 info_ptr += 2;
18637 }
18638 else if (form == DW_FORM_strx3)
18639 {
18640 str_index = read_3_bytes (abfd, info_ptr);
18641 info_ptr += 3;
18642 }
18643 else if (form == DW_FORM_strx4)
18644 {
18645 str_index = read_4_bytes (abfd, info_ptr);
18646 info_ptr += 4;
18647 }
18648 else
18649 {
18650 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18651 info_ptr += bytes_read;
18652 }
18653 *need_reprocess = true;
18654 DW_UNSND (attr) = str_index;
18655 }
18656 break;
18657 default:
18658 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18659 dwarf_form_name (form),
18660 bfd_get_filename (abfd));
18661 }
18662
18663 /* Super hack. */
18664 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18665 attr->form = DW_FORM_GNU_ref_alt;
18666
18667 /* We have seen instances where the compiler tried to emit a byte
18668 size attribute of -1 which ended up being encoded as an unsigned
18669 0xffffffff. Although 0xffffffff is technically a valid size value,
18670 an object of this size seems pretty unlikely so we can relatively
18671 safely treat these cases as if the size attribute was invalid and
18672 treat them as zero by default. */
18673 if (attr->name == DW_AT_byte_size
18674 && form == DW_FORM_data4
18675 && DW_UNSND (attr) >= 0xffffffff)
18676 {
18677 complaint
18678 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18679 hex_string (DW_UNSND (attr)));
18680 DW_UNSND (attr) = 0;
18681 }
18682
18683 return info_ptr;
18684 }
18685
18686 /* Read an attribute described by an abbreviated attribute. */
18687
18688 static const gdb_byte *
18689 read_attribute (const struct die_reader_specs *reader,
18690 struct attribute *attr, struct attr_abbrev *abbrev,
18691 const gdb_byte *info_ptr, bool *need_reprocess)
18692 {
18693 attr->name = abbrev->name;
18694 return read_attribute_value (reader, attr, abbrev->form,
18695 abbrev->implicit_const, info_ptr,
18696 need_reprocess);
18697 }
18698
18699 /* Cover function for read_initial_length.
18700 Returns the length of the object at BUF, and stores the size of the
18701 initial length in *BYTES_READ and stores the size that offsets will be in
18702 *OFFSET_SIZE.
18703 If the initial length size is not equivalent to that specified in
18704 CU_HEADER then issue a complaint.
18705 This is useful when reading non-comp-unit headers. */
18706
18707 static LONGEST
18708 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18709 const struct comp_unit_head *cu_header,
18710 unsigned int *bytes_read,
18711 unsigned int *offset_size)
18712 {
18713 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18714
18715 gdb_assert (cu_header->initial_length_size == 4
18716 || cu_header->initial_length_size == 8
18717 || cu_header->initial_length_size == 12);
18718
18719 if (cu_header->initial_length_size != *bytes_read)
18720 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18721
18722 *offset_size = (*bytes_read == 4) ? 4 : 8;
18723 return length;
18724 }
18725
18726 /* Return pointer to string at section SECT offset STR_OFFSET with error
18727 reporting strings FORM_NAME and SECT_NAME. */
18728
18729 static const char *
18730 read_indirect_string_at_offset_from (struct objfile *objfile,
18731 bfd *abfd, LONGEST str_offset,
18732 struct dwarf2_section_info *sect,
18733 const char *form_name,
18734 const char *sect_name)
18735 {
18736 sect->read (objfile);
18737 if (sect->buffer == NULL)
18738 error (_("%s used without %s section [in module %s]"),
18739 form_name, sect_name, bfd_get_filename (abfd));
18740 if (str_offset >= sect->size)
18741 error (_("%s pointing outside of %s section [in module %s]"),
18742 form_name, sect_name, bfd_get_filename (abfd));
18743 gdb_assert (HOST_CHAR_BIT == 8);
18744 if (sect->buffer[str_offset] == '\0')
18745 return NULL;
18746 return (const char *) (sect->buffer + str_offset);
18747 }
18748
18749 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18750
18751 static const char *
18752 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18753 bfd *abfd, LONGEST str_offset)
18754 {
18755 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18756 abfd, str_offset,
18757 &dwarf2_per_objfile->str,
18758 "DW_FORM_strp", ".debug_str");
18759 }
18760
18761 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18762
18763 static const char *
18764 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18765 bfd *abfd, LONGEST str_offset)
18766 {
18767 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18768 abfd, str_offset,
18769 &dwarf2_per_objfile->line_str,
18770 "DW_FORM_line_strp",
18771 ".debug_line_str");
18772 }
18773
18774 /* Read a string at offset STR_OFFSET in the .debug_str section from
18775 the .dwz file DWZ. Throw an error if the offset is too large. If
18776 the string consists of a single NUL byte, return NULL; otherwise
18777 return a pointer to the string. */
18778
18779 static const char *
18780 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18781 LONGEST str_offset)
18782 {
18783 dwz->str.read (objfile);
18784
18785 if (dwz->str.buffer == NULL)
18786 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18787 "section [in module %s]"),
18788 bfd_get_filename (dwz->dwz_bfd.get ()));
18789 if (str_offset >= dwz->str.size)
18790 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18791 ".debug_str section [in module %s]"),
18792 bfd_get_filename (dwz->dwz_bfd.get ()));
18793 gdb_assert (HOST_CHAR_BIT == 8);
18794 if (dwz->str.buffer[str_offset] == '\0')
18795 return NULL;
18796 return (const char *) (dwz->str.buffer + str_offset);
18797 }
18798
18799 /* Return pointer to string at .debug_str offset as read from BUF.
18800 BUF is assumed to be in a compilation unit described by CU_HEADER.
18801 Return *BYTES_READ_PTR count of bytes read from BUF. */
18802
18803 static const char *
18804 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18805 const gdb_byte *buf,
18806 const struct comp_unit_head *cu_header,
18807 unsigned int *bytes_read_ptr)
18808 {
18809 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18810
18811 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18812 }
18813
18814 /* Return pointer to string at .debug_line_str offset as read from BUF.
18815 BUF is assumed to be in a compilation unit described by CU_HEADER.
18816 Return *BYTES_READ_PTR count of bytes read from BUF. */
18817
18818 static const char *
18819 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18820 bfd *abfd, const gdb_byte *buf,
18821 const struct comp_unit_head *cu_header,
18822 unsigned int *bytes_read_ptr)
18823 {
18824 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18825
18826 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18827 str_offset);
18828 }
18829
18830 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18831 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18832 ADDR_SIZE is the size of addresses from the CU header. */
18833
18834 static CORE_ADDR
18835 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18836 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18837 int addr_size)
18838 {
18839 struct objfile *objfile = dwarf2_per_objfile->objfile;
18840 bfd *abfd = objfile->obfd;
18841 const gdb_byte *info_ptr;
18842 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18843
18844 dwarf2_per_objfile->addr.read (objfile);
18845 if (dwarf2_per_objfile->addr.buffer == NULL)
18846 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18847 objfile_name (objfile));
18848 if (addr_base_or_zero + addr_index * addr_size
18849 >= dwarf2_per_objfile->addr.size)
18850 error (_("DW_FORM_addr_index pointing outside of "
18851 ".debug_addr section [in module %s]"),
18852 objfile_name (objfile));
18853 info_ptr = (dwarf2_per_objfile->addr.buffer
18854 + addr_base_or_zero + addr_index * addr_size);
18855 if (addr_size == 4)
18856 return bfd_get_32 (abfd, info_ptr);
18857 else
18858 return bfd_get_64 (abfd, info_ptr);
18859 }
18860
18861 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18862
18863 static CORE_ADDR
18864 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18865 {
18866 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18867 cu->addr_base, cu->header.addr_size);
18868 }
18869
18870 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18871
18872 static CORE_ADDR
18873 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18874 unsigned int *bytes_read)
18875 {
18876 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18877 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18878
18879 return read_addr_index (cu, addr_index);
18880 }
18881
18882 /* Given an index in .debug_addr, fetch the value.
18883 NOTE: This can be called during dwarf expression evaluation,
18884 long after the debug information has been read, and thus per_cu->cu
18885 may no longer exist. */
18886
18887 CORE_ADDR
18888 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18889 unsigned int addr_index)
18890 {
18891 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18892 struct dwarf2_cu *cu = per_cu->cu;
18893 gdb::optional<ULONGEST> addr_base;
18894 int addr_size;
18895
18896 /* We need addr_base and addr_size.
18897 If we don't have PER_CU->cu, we have to get it.
18898 Nasty, but the alternative is storing the needed info in PER_CU,
18899 which at this point doesn't seem justified: it's not clear how frequently
18900 it would get used and it would increase the size of every PER_CU.
18901 Entry points like dwarf2_per_cu_addr_size do a similar thing
18902 so we're not in uncharted territory here.
18903 Alas we need to be a bit more complicated as addr_base is contained
18904 in the DIE.
18905
18906 We don't need to read the entire CU(/TU).
18907 We just need the header and top level die.
18908
18909 IWBN to use the aging mechanism to let us lazily later discard the CU.
18910 For now we skip this optimization. */
18911
18912 if (cu != NULL)
18913 {
18914 addr_base = cu->addr_base;
18915 addr_size = cu->header.addr_size;
18916 }
18917 else
18918 {
18919 cutu_reader reader (per_cu, NULL, 0, 0, false);
18920 addr_base = reader.cu->addr_base;
18921 addr_size = reader.cu->header.addr_size;
18922 }
18923
18924 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18925 addr_size);
18926 }
18927
18928 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18929 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18930 DWO file. */
18931
18932 static const char *
18933 read_str_index (struct dwarf2_cu *cu,
18934 struct dwarf2_section_info *str_section,
18935 struct dwarf2_section_info *str_offsets_section,
18936 ULONGEST str_offsets_base, ULONGEST str_index)
18937 {
18938 struct dwarf2_per_objfile *dwarf2_per_objfile
18939 = cu->per_cu->dwarf2_per_objfile;
18940 struct objfile *objfile = dwarf2_per_objfile->objfile;
18941 const char *objf_name = objfile_name (objfile);
18942 bfd *abfd = objfile->obfd;
18943 const gdb_byte *info_ptr;
18944 ULONGEST str_offset;
18945 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18946
18947 str_section->read (objfile);
18948 str_offsets_section->read (objfile);
18949 if (str_section->buffer == NULL)
18950 error (_("%s used without %s section"
18951 " in CU at offset %s [in module %s]"),
18952 form_name, str_section->get_name (),
18953 sect_offset_str (cu->header.sect_off), objf_name);
18954 if (str_offsets_section->buffer == NULL)
18955 error (_("%s used without %s section"
18956 " in CU at offset %s [in module %s]"),
18957 form_name, str_section->get_name (),
18958 sect_offset_str (cu->header.sect_off), objf_name);
18959 info_ptr = (str_offsets_section->buffer
18960 + str_offsets_base
18961 + str_index * cu->header.offset_size);
18962 if (cu->header.offset_size == 4)
18963 str_offset = bfd_get_32 (abfd, info_ptr);
18964 else
18965 str_offset = bfd_get_64 (abfd, info_ptr);
18966 if (str_offset >= str_section->size)
18967 error (_("Offset from %s pointing outside of"
18968 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18969 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18970 return (const char *) (str_section->buffer + str_offset);
18971 }
18972
18973 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18974
18975 static const char *
18976 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18977 {
18978 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18979 ? reader->cu->header.addr_size : 0;
18980 return read_str_index (reader->cu,
18981 &reader->dwo_file->sections.str,
18982 &reader->dwo_file->sections.str_offsets,
18983 str_offsets_base, str_index);
18984 }
18985
18986 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18987
18988 static const char *
18989 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18990 {
18991 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18992 const char *objf_name = objfile_name (objfile);
18993 static const char form_name[] = "DW_FORM_GNU_str_index";
18994 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18995
18996 if (!cu->str_offsets_base.has_value ())
18997 error (_("%s used in Fission stub without %s"
18998 " in CU at offset 0x%lx [in module %s]"),
18999 form_name, str_offsets_attr_name,
19000 (long) cu->header.offset_size, objf_name);
19001
19002 return read_str_index (cu,
19003 &cu->per_cu->dwarf2_per_objfile->str,
19004 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19005 *cu->str_offsets_base, str_index);
19006 }
19007
19008 /* Return the length of an LEB128 number in BUF. */
19009
19010 static int
19011 leb128_size (const gdb_byte *buf)
19012 {
19013 const gdb_byte *begin = buf;
19014 gdb_byte byte;
19015
19016 while (1)
19017 {
19018 byte = *buf++;
19019 if ((byte & 128) == 0)
19020 return buf - begin;
19021 }
19022 }
19023
19024 static void
19025 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19026 {
19027 switch (lang)
19028 {
19029 case DW_LANG_C89:
19030 case DW_LANG_C99:
19031 case DW_LANG_C11:
19032 case DW_LANG_C:
19033 case DW_LANG_UPC:
19034 cu->language = language_c;
19035 break;
19036 case DW_LANG_Java:
19037 case DW_LANG_C_plus_plus:
19038 case DW_LANG_C_plus_plus_11:
19039 case DW_LANG_C_plus_plus_14:
19040 cu->language = language_cplus;
19041 break;
19042 case DW_LANG_D:
19043 cu->language = language_d;
19044 break;
19045 case DW_LANG_Fortran77:
19046 case DW_LANG_Fortran90:
19047 case DW_LANG_Fortran95:
19048 case DW_LANG_Fortran03:
19049 case DW_LANG_Fortran08:
19050 cu->language = language_fortran;
19051 break;
19052 case DW_LANG_Go:
19053 cu->language = language_go;
19054 break;
19055 case DW_LANG_Mips_Assembler:
19056 cu->language = language_asm;
19057 break;
19058 case DW_LANG_Ada83:
19059 case DW_LANG_Ada95:
19060 cu->language = language_ada;
19061 break;
19062 case DW_LANG_Modula2:
19063 cu->language = language_m2;
19064 break;
19065 case DW_LANG_Pascal83:
19066 cu->language = language_pascal;
19067 break;
19068 case DW_LANG_ObjC:
19069 cu->language = language_objc;
19070 break;
19071 case DW_LANG_Rust:
19072 case DW_LANG_Rust_old:
19073 cu->language = language_rust;
19074 break;
19075 case DW_LANG_Cobol74:
19076 case DW_LANG_Cobol85:
19077 default:
19078 cu->language = language_minimal;
19079 break;
19080 }
19081 cu->language_defn = language_def (cu->language);
19082 }
19083
19084 /* Return the named attribute or NULL if not there. */
19085
19086 static struct attribute *
19087 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19088 {
19089 for (;;)
19090 {
19091 unsigned int i;
19092 struct attribute *spec = NULL;
19093
19094 for (i = 0; i < die->num_attrs; ++i)
19095 {
19096 if (die->attrs[i].name == name)
19097 return &die->attrs[i];
19098 if (die->attrs[i].name == DW_AT_specification
19099 || die->attrs[i].name == DW_AT_abstract_origin)
19100 spec = &die->attrs[i];
19101 }
19102
19103 if (!spec)
19104 break;
19105
19106 die = follow_die_ref (die, spec, &cu);
19107 }
19108
19109 return NULL;
19110 }
19111
19112 /* Return the named attribute or NULL if not there,
19113 but do not follow DW_AT_specification, etc.
19114 This is for use in contexts where we're reading .debug_types dies.
19115 Following DW_AT_specification, DW_AT_abstract_origin will take us
19116 back up the chain, and we want to go down. */
19117
19118 static struct attribute *
19119 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19120 {
19121 unsigned int i;
19122
19123 for (i = 0; i < die->num_attrs; ++i)
19124 if (die->attrs[i].name == name)
19125 return &die->attrs[i];
19126
19127 return NULL;
19128 }
19129
19130 /* Return the string associated with a string-typed attribute, or NULL if it
19131 is either not found or is of an incorrect type. */
19132
19133 static const char *
19134 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19135 {
19136 struct attribute *attr;
19137 const char *str = NULL;
19138
19139 attr = dwarf2_attr (die, name, cu);
19140
19141 if (attr != NULL)
19142 {
19143 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19144 || attr->form == DW_FORM_string
19145 || attr->form == DW_FORM_strx
19146 || attr->form == DW_FORM_strx1
19147 || attr->form == DW_FORM_strx2
19148 || attr->form == DW_FORM_strx3
19149 || attr->form == DW_FORM_strx4
19150 || attr->form == DW_FORM_GNU_str_index
19151 || attr->form == DW_FORM_GNU_strp_alt)
19152 str = DW_STRING (attr);
19153 else
19154 complaint (_("string type expected for attribute %s for "
19155 "DIE at %s in module %s"),
19156 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19157 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19158 }
19159
19160 return str;
19161 }
19162
19163 /* Return the dwo name or NULL if not present. If present, it is in either
19164 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19165 static const char *
19166 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19167 {
19168 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19169 if (dwo_name == nullptr)
19170 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19171 return dwo_name;
19172 }
19173
19174 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19175 and holds a non-zero value. This function should only be used for
19176 DW_FORM_flag or DW_FORM_flag_present attributes. */
19177
19178 static int
19179 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19180 {
19181 struct attribute *attr = dwarf2_attr (die, name, cu);
19182
19183 return (attr && DW_UNSND (attr));
19184 }
19185
19186 static int
19187 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19188 {
19189 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19190 which value is non-zero. However, we have to be careful with
19191 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19192 (via dwarf2_flag_true_p) follows this attribute. So we may
19193 end up accidently finding a declaration attribute that belongs
19194 to a different DIE referenced by the specification attribute,
19195 even though the given DIE does not have a declaration attribute. */
19196 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19197 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19198 }
19199
19200 /* Return the die giving the specification for DIE, if there is
19201 one. *SPEC_CU is the CU containing DIE on input, and the CU
19202 containing the return value on output. If there is no
19203 specification, but there is an abstract origin, that is
19204 returned. */
19205
19206 static struct die_info *
19207 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19208 {
19209 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19210 *spec_cu);
19211
19212 if (spec_attr == NULL)
19213 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19214
19215 if (spec_attr == NULL)
19216 return NULL;
19217 else
19218 return follow_die_ref (die, spec_attr, spec_cu);
19219 }
19220
19221 /* Stub for free_line_header to match void * callback types. */
19222
19223 static void
19224 free_line_header_voidp (void *arg)
19225 {
19226 struct line_header *lh = (struct line_header *) arg;
19227
19228 delete lh;
19229 }
19230
19231 /* A convenience function to find the proper .debug_line section for a CU. */
19232
19233 static struct dwarf2_section_info *
19234 get_debug_line_section (struct dwarf2_cu *cu)
19235 {
19236 struct dwarf2_section_info *section;
19237 struct dwarf2_per_objfile *dwarf2_per_objfile
19238 = cu->per_cu->dwarf2_per_objfile;
19239
19240 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19241 DWO file. */
19242 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19243 section = &cu->dwo_unit->dwo_file->sections.line;
19244 else if (cu->per_cu->is_dwz)
19245 {
19246 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19247
19248 section = &dwz->line;
19249 }
19250 else
19251 section = &dwarf2_per_objfile->line;
19252
19253 return section;
19254 }
19255
19256 /* Read directory or file name entry format, starting with byte of
19257 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19258 entries count and the entries themselves in the described entry
19259 format. */
19260
19261 static void
19262 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19263 bfd *abfd, const gdb_byte **bufp,
19264 struct line_header *lh,
19265 const struct comp_unit_head *cu_header,
19266 void (*callback) (struct line_header *lh,
19267 const char *name,
19268 dir_index d_index,
19269 unsigned int mod_time,
19270 unsigned int length))
19271 {
19272 gdb_byte format_count, formati;
19273 ULONGEST data_count, datai;
19274 const gdb_byte *buf = *bufp;
19275 const gdb_byte *format_header_data;
19276 unsigned int bytes_read;
19277
19278 format_count = read_1_byte (abfd, buf);
19279 buf += 1;
19280 format_header_data = buf;
19281 for (formati = 0; formati < format_count; formati++)
19282 {
19283 read_unsigned_leb128 (abfd, buf, &bytes_read);
19284 buf += bytes_read;
19285 read_unsigned_leb128 (abfd, buf, &bytes_read);
19286 buf += bytes_read;
19287 }
19288
19289 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19290 buf += bytes_read;
19291 for (datai = 0; datai < data_count; datai++)
19292 {
19293 const gdb_byte *format = format_header_data;
19294 struct file_entry fe;
19295
19296 for (formati = 0; formati < format_count; formati++)
19297 {
19298 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19299 format += bytes_read;
19300
19301 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19302 format += bytes_read;
19303
19304 gdb::optional<const char *> string;
19305 gdb::optional<unsigned int> uint;
19306
19307 switch (form)
19308 {
19309 case DW_FORM_string:
19310 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19311 buf += bytes_read;
19312 break;
19313
19314 case DW_FORM_line_strp:
19315 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19316 abfd, buf,
19317 cu_header,
19318 &bytes_read));
19319 buf += bytes_read;
19320 break;
19321
19322 case DW_FORM_data1:
19323 uint.emplace (read_1_byte (abfd, buf));
19324 buf += 1;
19325 break;
19326
19327 case DW_FORM_data2:
19328 uint.emplace (read_2_bytes (abfd, buf));
19329 buf += 2;
19330 break;
19331
19332 case DW_FORM_data4:
19333 uint.emplace (read_4_bytes (abfd, buf));
19334 buf += 4;
19335 break;
19336
19337 case DW_FORM_data8:
19338 uint.emplace (read_8_bytes (abfd, buf));
19339 buf += 8;
19340 break;
19341
19342 case DW_FORM_data16:
19343 /* This is used for MD5, but file_entry does not record MD5s. */
19344 buf += 16;
19345 break;
19346
19347 case DW_FORM_udata:
19348 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19349 buf += bytes_read;
19350 break;
19351
19352 case DW_FORM_block:
19353 /* It is valid only for DW_LNCT_timestamp which is ignored by
19354 current GDB. */
19355 break;
19356 }
19357
19358 switch (content_type)
19359 {
19360 case DW_LNCT_path:
19361 if (string.has_value ())
19362 fe.name = *string;
19363 break;
19364 case DW_LNCT_directory_index:
19365 if (uint.has_value ())
19366 fe.d_index = (dir_index) *uint;
19367 break;
19368 case DW_LNCT_timestamp:
19369 if (uint.has_value ())
19370 fe.mod_time = *uint;
19371 break;
19372 case DW_LNCT_size:
19373 if (uint.has_value ())
19374 fe.length = *uint;
19375 break;
19376 case DW_LNCT_MD5:
19377 break;
19378 default:
19379 complaint (_("Unknown format content type %s"),
19380 pulongest (content_type));
19381 }
19382 }
19383
19384 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19385 }
19386
19387 *bufp = buf;
19388 }
19389
19390 /* Read the statement program header starting at OFFSET in
19391 .debug_line, or .debug_line.dwo. Return a pointer
19392 to a struct line_header, allocated using xmalloc.
19393 Returns NULL if there is a problem reading the header, e.g., if it
19394 has a version we don't understand.
19395
19396 NOTE: the strings in the include directory and file name tables of
19397 the returned object point into the dwarf line section buffer,
19398 and must not be freed. */
19399
19400 static line_header_up
19401 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19402 {
19403 const gdb_byte *line_ptr;
19404 unsigned int bytes_read, offset_size;
19405 int i;
19406 const char *cur_dir, *cur_file;
19407 struct dwarf2_section_info *section;
19408 bfd *abfd;
19409 struct dwarf2_per_objfile *dwarf2_per_objfile
19410 = cu->per_cu->dwarf2_per_objfile;
19411
19412 section = get_debug_line_section (cu);
19413 section->read (dwarf2_per_objfile->objfile);
19414 if (section->buffer == NULL)
19415 {
19416 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19417 complaint (_("missing .debug_line.dwo section"));
19418 else
19419 complaint (_("missing .debug_line section"));
19420 return 0;
19421 }
19422
19423 /* We can't do this until we know the section is non-empty.
19424 Only then do we know we have such a section. */
19425 abfd = section->get_bfd_owner ();
19426
19427 /* Make sure that at least there's room for the total_length field.
19428 That could be 12 bytes long, but we're just going to fudge that. */
19429 if (to_underlying (sect_off) + 4 >= section->size)
19430 {
19431 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19432 return 0;
19433 }
19434
19435 line_header_up lh (new line_header ());
19436
19437 lh->sect_off = sect_off;
19438 lh->offset_in_dwz = cu->per_cu->is_dwz;
19439
19440 line_ptr = section->buffer + to_underlying (sect_off);
19441
19442 /* Read in the header. */
19443 lh->total_length =
19444 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19445 &bytes_read, &offset_size);
19446 line_ptr += bytes_read;
19447
19448 const gdb_byte *start_here = line_ptr;
19449
19450 if (line_ptr + lh->total_length > (section->buffer + section->size))
19451 {
19452 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19453 return 0;
19454 }
19455 lh->statement_program_end = start_here + lh->total_length;
19456 lh->version = read_2_bytes (abfd, line_ptr);
19457 line_ptr += 2;
19458 if (lh->version > 5)
19459 {
19460 /* This is a version we don't understand. The format could have
19461 changed in ways we don't handle properly so just punt. */
19462 complaint (_("unsupported version in .debug_line section"));
19463 return NULL;
19464 }
19465 if (lh->version >= 5)
19466 {
19467 gdb_byte segment_selector_size;
19468
19469 /* Skip address size. */
19470 read_1_byte (abfd, line_ptr);
19471 line_ptr += 1;
19472
19473 segment_selector_size = read_1_byte (abfd, line_ptr);
19474 line_ptr += 1;
19475 if (segment_selector_size != 0)
19476 {
19477 complaint (_("unsupported segment selector size %u "
19478 "in .debug_line section"),
19479 segment_selector_size);
19480 return NULL;
19481 }
19482 }
19483 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19484 line_ptr += offset_size;
19485 lh->statement_program_start = line_ptr + lh->header_length;
19486 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19487 line_ptr += 1;
19488 if (lh->version >= 4)
19489 {
19490 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19491 line_ptr += 1;
19492 }
19493 else
19494 lh->maximum_ops_per_instruction = 1;
19495
19496 if (lh->maximum_ops_per_instruction == 0)
19497 {
19498 lh->maximum_ops_per_instruction = 1;
19499 complaint (_("invalid maximum_ops_per_instruction "
19500 "in `.debug_line' section"));
19501 }
19502
19503 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19504 line_ptr += 1;
19505 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19506 line_ptr += 1;
19507 lh->line_range = read_1_byte (abfd, line_ptr);
19508 line_ptr += 1;
19509 lh->opcode_base = read_1_byte (abfd, line_ptr);
19510 line_ptr += 1;
19511 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19512
19513 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19514 for (i = 1; i < lh->opcode_base; ++i)
19515 {
19516 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19517 line_ptr += 1;
19518 }
19519
19520 if (lh->version >= 5)
19521 {
19522 /* Read directory table. */
19523 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19524 &cu->header,
19525 [] (struct line_header *header, const char *name,
19526 dir_index d_index, unsigned int mod_time,
19527 unsigned int length)
19528 {
19529 header->add_include_dir (name);
19530 });
19531
19532 /* Read file name table. */
19533 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19534 &cu->header,
19535 [] (struct line_header *header, const char *name,
19536 dir_index d_index, unsigned int mod_time,
19537 unsigned int length)
19538 {
19539 header->add_file_name (name, d_index, mod_time, length);
19540 });
19541 }
19542 else
19543 {
19544 /* Read directory table. */
19545 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19546 {
19547 line_ptr += bytes_read;
19548 lh->add_include_dir (cur_dir);
19549 }
19550 line_ptr += bytes_read;
19551
19552 /* Read file name table. */
19553 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19554 {
19555 unsigned int mod_time, length;
19556 dir_index d_index;
19557
19558 line_ptr += bytes_read;
19559 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19560 line_ptr += bytes_read;
19561 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19562 line_ptr += bytes_read;
19563 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19564 line_ptr += bytes_read;
19565
19566 lh->add_file_name (cur_file, d_index, mod_time, length);
19567 }
19568 line_ptr += bytes_read;
19569 }
19570
19571 if (line_ptr > (section->buffer + section->size))
19572 complaint (_("line number info header doesn't "
19573 "fit in `.debug_line' section"));
19574
19575 return lh;
19576 }
19577
19578 /* Subroutine of dwarf_decode_lines to simplify it.
19579 Return the file name of the psymtab for the given file_entry.
19580 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19581 If space for the result is malloc'd, *NAME_HOLDER will be set.
19582 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19583
19584 static const char *
19585 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19586 const dwarf2_psymtab *pst,
19587 const char *comp_dir,
19588 gdb::unique_xmalloc_ptr<char> *name_holder)
19589 {
19590 const char *include_name = fe.name;
19591 const char *include_name_to_compare = include_name;
19592 const char *pst_filename;
19593 int file_is_pst;
19594
19595 const char *dir_name = fe.include_dir (lh);
19596
19597 gdb::unique_xmalloc_ptr<char> hold_compare;
19598 if (!IS_ABSOLUTE_PATH (include_name)
19599 && (dir_name != NULL || comp_dir != NULL))
19600 {
19601 /* Avoid creating a duplicate psymtab for PST.
19602 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19603 Before we do the comparison, however, we need to account
19604 for DIR_NAME and COMP_DIR.
19605 First prepend dir_name (if non-NULL). If we still don't
19606 have an absolute path prepend comp_dir (if non-NULL).
19607 However, the directory we record in the include-file's
19608 psymtab does not contain COMP_DIR (to match the
19609 corresponding symtab(s)).
19610
19611 Example:
19612
19613 bash$ cd /tmp
19614 bash$ gcc -g ./hello.c
19615 include_name = "hello.c"
19616 dir_name = "."
19617 DW_AT_comp_dir = comp_dir = "/tmp"
19618 DW_AT_name = "./hello.c"
19619
19620 */
19621
19622 if (dir_name != NULL)
19623 {
19624 name_holder->reset (concat (dir_name, SLASH_STRING,
19625 include_name, (char *) NULL));
19626 include_name = name_holder->get ();
19627 include_name_to_compare = include_name;
19628 }
19629 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19630 {
19631 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19632 include_name, (char *) NULL));
19633 include_name_to_compare = hold_compare.get ();
19634 }
19635 }
19636
19637 pst_filename = pst->filename;
19638 gdb::unique_xmalloc_ptr<char> copied_name;
19639 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19640 {
19641 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19642 pst_filename, (char *) NULL));
19643 pst_filename = copied_name.get ();
19644 }
19645
19646 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19647
19648 if (file_is_pst)
19649 return NULL;
19650 return include_name;
19651 }
19652
19653 /* State machine to track the state of the line number program. */
19654
19655 class lnp_state_machine
19656 {
19657 public:
19658 /* Initialize a machine state for the start of a line number
19659 program. */
19660 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19661 bool record_lines_p);
19662
19663 file_entry *current_file ()
19664 {
19665 /* lh->file_names is 0-based, but the file name numbers in the
19666 statement program are 1-based. */
19667 return m_line_header->file_name_at (m_file);
19668 }
19669
19670 /* Record the line in the state machine. END_SEQUENCE is true if
19671 we're processing the end of a sequence. */
19672 void record_line (bool end_sequence);
19673
19674 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19675 nop-out rest of the lines in this sequence. */
19676 void check_line_address (struct dwarf2_cu *cu,
19677 const gdb_byte *line_ptr,
19678 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19679
19680 void handle_set_discriminator (unsigned int discriminator)
19681 {
19682 m_discriminator = discriminator;
19683 m_line_has_non_zero_discriminator |= discriminator != 0;
19684 }
19685
19686 /* Handle DW_LNE_set_address. */
19687 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19688 {
19689 m_op_index = 0;
19690 address += baseaddr;
19691 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19692 }
19693
19694 /* Handle DW_LNS_advance_pc. */
19695 void handle_advance_pc (CORE_ADDR adjust);
19696
19697 /* Handle a special opcode. */
19698 void handle_special_opcode (unsigned char op_code);
19699
19700 /* Handle DW_LNS_advance_line. */
19701 void handle_advance_line (int line_delta)
19702 {
19703 advance_line (line_delta);
19704 }
19705
19706 /* Handle DW_LNS_set_file. */
19707 void handle_set_file (file_name_index file);
19708
19709 /* Handle DW_LNS_negate_stmt. */
19710 void handle_negate_stmt ()
19711 {
19712 m_is_stmt = !m_is_stmt;
19713 }
19714
19715 /* Handle DW_LNS_const_add_pc. */
19716 void handle_const_add_pc ();
19717
19718 /* Handle DW_LNS_fixed_advance_pc. */
19719 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19720 {
19721 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19722 m_op_index = 0;
19723 }
19724
19725 /* Handle DW_LNS_copy. */
19726 void handle_copy ()
19727 {
19728 record_line (false);
19729 m_discriminator = 0;
19730 }
19731
19732 /* Handle DW_LNE_end_sequence. */
19733 void handle_end_sequence ()
19734 {
19735 m_currently_recording_lines = true;
19736 }
19737
19738 private:
19739 /* Advance the line by LINE_DELTA. */
19740 void advance_line (int line_delta)
19741 {
19742 m_line += line_delta;
19743
19744 if (line_delta != 0)
19745 m_line_has_non_zero_discriminator = m_discriminator != 0;
19746 }
19747
19748 struct dwarf2_cu *m_cu;
19749
19750 gdbarch *m_gdbarch;
19751
19752 /* True if we're recording lines.
19753 Otherwise we're building partial symtabs and are just interested in
19754 finding include files mentioned by the line number program. */
19755 bool m_record_lines_p;
19756
19757 /* The line number header. */
19758 line_header *m_line_header;
19759
19760 /* These are part of the standard DWARF line number state machine,
19761 and initialized according to the DWARF spec. */
19762
19763 unsigned char m_op_index = 0;
19764 /* The line table index of the current file. */
19765 file_name_index m_file = 1;
19766 unsigned int m_line = 1;
19767
19768 /* These are initialized in the constructor. */
19769
19770 CORE_ADDR m_address;
19771 bool m_is_stmt;
19772 unsigned int m_discriminator;
19773
19774 /* Additional bits of state we need to track. */
19775
19776 /* The last file that we called dwarf2_start_subfile for.
19777 This is only used for TLLs. */
19778 unsigned int m_last_file = 0;
19779 /* The last file a line number was recorded for. */
19780 struct subfile *m_last_subfile = NULL;
19781
19782 /* When true, record the lines we decode. */
19783 bool m_currently_recording_lines = false;
19784
19785 /* The last line number that was recorded, used to coalesce
19786 consecutive entries for the same line. This can happen, for
19787 example, when discriminators are present. PR 17276. */
19788 unsigned int m_last_line = 0;
19789 bool m_line_has_non_zero_discriminator = false;
19790 };
19791
19792 void
19793 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19794 {
19795 CORE_ADDR addr_adj = (((m_op_index + adjust)
19796 / m_line_header->maximum_ops_per_instruction)
19797 * m_line_header->minimum_instruction_length);
19798 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19799 m_op_index = ((m_op_index + adjust)
19800 % m_line_header->maximum_ops_per_instruction);
19801 }
19802
19803 void
19804 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19805 {
19806 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19807 CORE_ADDR addr_adj = (((m_op_index
19808 + (adj_opcode / m_line_header->line_range))
19809 / m_line_header->maximum_ops_per_instruction)
19810 * m_line_header->minimum_instruction_length);
19811 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19812 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19813 % m_line_header->maximum_ops_per_instruction);
19814
19815 int line_delta = (m_line_header->line_base
19816 + (adj_opcode % m_line_header->line_range));
19817 advance_line (line_delta);
19818 record_line (false);
19819 m_discriminator = 0;
19820 }
19821
19822 void
19823 lnp_state_machine::handle_set_file (file_name_index file)
19824 {
19825 m_file = file;
19826
19827 const file_entry *fe = current_file ();
19828 if (fe == NULL)
19829 dwarf2_debug_line_missing_file_complaint ();
19830 else if (m_record_lines_p)
19831 {
19832 const char *dir = fe->include_dir (m_line_header);
19833
19834 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19835 m_line_has_non_zero_discriminator = m_discriminator != 0;
19836 dwarf2_start_subfile (m_cu, fe->name, dir);
19837 }
19838 }
19839
19840 void
19841 lnp_state_machine::handle_const_add_pc ()
19842 {
19843 CORE_ADDR adjust
19844 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19845
19846 CORE_ADDR addr_adj
19847 = (((m_op_index + adjust)
19848 / m_line_header->maximum_ops_per_instruction)
19849 * m_line_header->minimum_instruction_length);
19850
19851 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19852 m_op_index = ((m_op_index + adjust)
19853 % m_line_header->maximum_ops_per_instruction);
19854 }
19855
19856 /* Return non-zero if we should add LINE to the line number table.
19857 LINE is the line to add, LAST_LINE is the last line that was added,
19858 LAST_SUBFILE is the subfile for LAST_LINE.
19859 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19860 had a non-zero discriminator.
19861
19862 We have to be careful in the presence of discriminators.
19863 E.g., for this line:
19864
19865 for (i = 0; i < 100000; i++);
19866
19867 clang can emit four line number entries for that one line,
19868 each with a different discriminator.
19869 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19870
19871 However, we want gdb to coalesce all four entries into one.
19872 Otherwise the user could stepi into the middle of the line and
19873 gdb would get confused about whether the pc really was in the
19874 middle of the line.
19875
19876 Things are further complicated by the fact that two consecutive
19877 line number entries for the same line is a heuristic used by gcc
19878 to denote the end of the prologue. So we can't just discard duplicate
19879 entries, we have to be selective about it. The heuristic we use is
19880 that we only collapse consecutive entries for the same line if at least
19881 one of those entries has a non-zero discriminator. PR 17276.
19882
19883 Note: Addresses in the line number state machine can never go backwards
19884 within one sequence, thus this coalescing is ok. */
19885
19886 static int
19887 dwarf_record_line_p (struct dwarf2_cu *cu,
19888 unsigned int line, unsigned int last_line,
19889 int line_has_non_zero_discriminator,
19890 struct subfile *last_subfile)
19891 {
19892 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19893 return 1;
19894 if (line != last_line)
19895 return 1;
19896 /* Same line for the same file that we've seen already.
19897 As a last check, for pr 17276, only record the line if the line
19898 has never had a non-zero discriminator. */
19899 if (!line_has_non_zero_discriminator)
19900 return 1;
19901 return 0;
19902 }
19903
19904 /* Use the CU's builder to record line number LINE beginning at
19905 address ADDRESS in the line table of subfile SUBFILE. */
19906
19907 static void
19908 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19909 unsigned int line, CORE_ADDR address,
19910 struct dwarf2_cu *cu)
19911 {
19912 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19913
19914 if (dwarf_line_debug)
19915 {
19916 fprintf_unfiltered (gdb_stdlog,
19917 "Recording line %u, file %s, address %s\n",
19918 line, lbasename (subfile->name),
19919 paddress (gdbarch, address));
19920 }
19921
19922 if (cu != nullptr)
19923 cu->get_builder ()->record_line (subfile, line, addr);
19924 }
19925
19926 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19927 Mark the end of a set of line number records.
19928 The arguments are the same as for dwarf_record_line_1.
19929 If SUBFILE is NULL the request is ignored. */
19930
19931 static void
19932 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19933 CORE_ADDR address, struct dwarf2_cu *cu)
19934 {
19935 if (subfile == NULL)
19936 return;
19937
19938 if (dwarf_line_debug)
19939 {
19940 fprintf_unfiltered (gdb_stdlog,
19941 "Finishing current line, file %s, address %s\n",
19942 lbasename (subfile->name),
19943 paddress (gdbarch, address));
19944 }
19945
19946 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
19947 }
19948
19949 void
19950 lnp_state_machine::record_line (bool end_sequence)
19951 {
19952 if (dwarf_line_debug)
19953 {
19954 fprintf_unfiltered (gdb_stdlog,
19955 "Processing actual line %u: file %u,"
19956 " address %s, is_stmt %u, discrim %u%s\n",
19957 m_line, m_file,
19958 paddress (m_gdbarch, m_address),
19959 m_is_stmt, m_discriminator,
19960 (end_sequence ? "\t(end sequence)" : ""));
19961 }
19962
19963 file_entry *fe = current_file ();
19964
19965 if (fe == NULL)
19966 dwarf2_debug_line_missing_file_complaint ();
19967 /* For now we ignore lines not starting on an instruction boundary.
19968 But not when processing end_sequence for compatibility with the
19969 previous version of the code. */
19970 else if (m_op_index == 0 || end_sequence)
19971 {
19972 fe->included_p = 1;
19973 if (m_record_lines_p
19974 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
19975 {
19976 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19977 || end_sequence)
19978 {
19979 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19980 m_currently_recording_lines ? m_cu : nullptr);
19981 }
19982
19983 if (!end_sequence)
19984 {
19985 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19986 m_line_has_non_zero_discriminator,
19987 m_last_subfile))
19988 {
19989 buildsym_compunit *builder = m_cu->get_builder ();
19990 dwarf_record_line_1 (m_gdbarch,
19991 builder->get_current_subfile (),
19992 m_line, m_address,
19993 m_currently_recording_lines ? m_cu : nullptr);
19994 }
19995 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19996 m_last_line = m_line;
19997 }
19998 }
19999 }
20000 }
20001
20002 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20003 line_header *lh, bool record_lines_p)
20004 {
20005 m_cu = cu;
20006 m_gdbarch = arch;
20007 m_record_lines_p = record_lines_p;
20008 m_line_header = lh;
20009
20010 m_currently_recording_lines = true;
20011
20012 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20013 was a line entry for it so that the backend has a chance to adjust it
20014 and also record it in case it needs it. This is currently used by MIPS
20015 code, cf. `mips_adjust_dwarf2_line'. */
20016 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20017 m_is_stmt = lh->default_is_stmt;
20018 m_discriminator = 0;
20019 }
20020
20021 void
20022 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20023 const gdb_byte *line_ptr,
20024 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20025 {
20026 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20027 the pc range of the CU. However, we restrict the test to only ADDRESS
20028 values of zero to preserve GDB's previous behaviour which is to handle
20029 the specific case of a function being GC'd by the linker. */
20030
20031 if (address == 0 && address < unrelocated_lowpc)
20032 {
20033 /* This line table is for a function which has been
20034 GCd by the linker. Ignore it. PR gdb/12528 */
20035
20036 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20037 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20038
20039 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20040 line_offset, objfile_name (objfile));
20041 m_currently_recording_lines = false;
20042 /* Note: m_currently_recording_lines is left as false until we see
20043 DW_LNE_end_sequence. */
20044 }
20045 }
20046
20047 /* Subroutine of dwarf_decode_lines to simplify it.
20048 Process the line number information in LH.
20049 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20050 program in order to set included_p for every referenced header. */
20051
20052 static void
20053 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20054 const int decode_for_pst_p, CORE_ADDR lowpc)
20055 {
20056 const gdb_byte *line_ptr, *extended_end;
20057 const gdb_byte *line_end;
20058 unsigned int bytes_read, extended_len;
20059 unsigned char op_code, extended_op;
20060 CORE_ADDR baseaddr;
20061 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20062 bfd *abfd = objfile->obfd;
20063 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20064 /* True if we're recording line info (as opposed to building partial
20065 symtabs and just interested in finding include files mentioned by
20066 the line number program). */
20067 bool record_lines_p = !decode_for_pst_p;
20068
20069 baseaddr = objfile->text_section_offset ();
20070
20071 line_ptr = lh->statement_program_start;
20072 line_end = lh->statement_program_end;
20073
20074 /* Read the statement sequences until there's nothing left. */
20075 while (line_ptr < line_end)
20076 {
20077 /* The DWARF line number program state machine. Reset the state
20078 machine at the start of each sequence. */
20079 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20080 bool end_sequence = false;
20081
20082 if (record_lines_p)
20083 {
20084 /* Start a subfile for the current file of the state
20085 machine. */
20086 const file_entry *fe = state_machine.current_file ();
20087
20088 if (fe != NULL)
20089 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20090 }
20091
20092 /* Decode the table. */
20093 while (line_ptr < line_end && !end_sequence)
20094 {
20095 op_code = read_1_byte (abfd, line_ptr);
20096 line_ptr += 1;
20097
20098 if (op_code >= lh->opcode_base)
20099 {
20100 /* Special opcode. */
20101 state_machine.handle_special_opcode (op_code);
20102 }
20103 else switch (op_code)
20104 {
20105 case DW_LNS_extended_op:
20106 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20107 &bytes_read);
20108 line_ptr += bytes_read;
20109 extended_end = line_ptr + extended_len;
20110 extended_op = read_1_byte (abfd, line_ptr);
20111 line_ptr += 1;
20112 switch (extended_op)
20113 {
20114 case DW_LNE_end_sequence:
20115 state_machine.handle_end_sequence ();
20116 end_sequence = true;
20117 break;
20118 case DW_LNE_set_address:
20119 {
20120 CORE_ADDR address
20121 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20122 line_ptr += bytes_read;
20123
20124 state_machine.check_line_address (cu, line_ptr,
20125 lowpc - baseaddr, address);
20126 state_machine.handle_set_address (baseaddr, address);
20127 }
20128 break;
20129 case DW_LNE_define_file:
20130 {
20131 const char *cur_file;
20132 unsigned int mod_time, length;
20133 dir_index dindex;
20134
20135 cur_file = read_direct_string (abfd, line_ptr,
20136 &bytes_read);
20137 line_ptr += bytes_read;
20138 dindex = (dir_index)
20139 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20140 line_ptr += bytes_read;
20141 mod_time =
20142 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20143 line_ptr += bytes_read;
20144 length =
20145 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20146 line_ptr += bytes_read;
20147 lh->add_file_name (cur_file, dindex, mod_time, length);
20148 }
20149 break;
20150 case DW_LNE_set_discriminator:
20151 {
20152 /* The discriminator is not interesting to the
20153 debugger; just ignore it. We still need to
20154 check its value though:
20155 if there are consecutive entries for the same
20156 (non-prologue) line we want to coalesce them.
20157 PR 17276. */
20158 unsigned int discr
20159 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20160 line_ptr += bytes_read;
20161
20162 state_machine.handle_set_discriminator (discr);
20163 }
20164 break;
20165 default:
20166 complaint (_("mangled .debug_line section"));
20167 return;
20168 }
20169 /* Make sure that we parsed the extended op correctly. If e.g.
20170 we expected a different address size than the producer used,
20171 we may have read the wrong number of bytes. */
20172 if (line_ptr != extended_end)
20173 {
20174 complaint (_("mangled .debug_line section"));
20175 return;
20176 }
20177 break;
20178 case DW_LNS_copy:
20179 state_machine.handle_copy ();
20180 break;
20181 case DW_LNS_advance_pc:
20182 {
20183 CORE_ADDR adjust
20184 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20185 line_ptr += bytes_read;
20186
20187 state_machine.handle_advance_pc (adjust);
20188 }
20189 break;
20190 case DW_LNS_advance_line:
20191 {
20192 int line_delta
20193 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20194 line_ptr += bytes_read;
20195
20196 state_machine.handle_advance_line (line_delta);
20197 }
20198 break;
20199 case DW_LNS_set_file:
20200 {
20201 file_name_index file
20202 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20203 &bytes_read);
20204 line_ptr += bytes_read;
20205
20206 state_machine.handle_set_file (file);
20207 }
20208 break;
20209 case DW_LNS_set_column:
20210 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20211 line_ptr += bytes_read;
20212 break;
20213 case DW_LNS_negate_stmt:
20214 state_machine.handle_negate_stmt ();
20215 break;
20216 case DW_LNS_set_basic_block:
20217 break;
20218 /* Add to the address register of the state machine the
20219 address increment value corresponding to special opcode
20220 255. I.e., this value is scaled by the minimum
20221 instruction length since special opcode 255 would have
20222 scaled the increment. */
20223 case DW_LNS_const_add_pc:
20224 state_machine.handle_const_add_pc ();
20225 break;
20226 case DW_LNS_fixed_advance_pc:
20227 {
20228 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20229 line_ptr += 2;
20230
20231 state_machine.handle_fixed_advance_pc (addr_adj);
20232 }
20233 break;
20234 default:
20235 {
20236 /* Unknown standard opcode, ignore it. */
20237 int i;
20238
20239 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20240 {
20241 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20242 line_ptr += bytes_read;
20243 }
20244 }
20245 }
20246 }
20247
20248 if (!end_sequence)
20249 dwarf2_debug_line_missing_end_sequence_complaint ();
20250
20251 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20252 in which case we still finish recording the last line). */
20253 state_machine.record_line (true);
20254 }
20255 }
20256
20257 /* Decode the Line Number Program (LNP) for the given line_header
20258 structure and CU. The actual information extracted and the type
20259 of structures created from the LNP depends on the value of PST.
20260
20261 1. If PST is NULL, then this procedure uses the data from the program
20262 to create all necessary symbol tables, and their linetables.
20263
20264 2. If PST is not NULL, this procedure reads the program to determine
20265 the list of files included by the unit represented by PST, and
20266 builds all the associated partial symbol tables.
20267
20268 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20269 It is used for relative paths in the line table.
20270 NOTE: When processing partial symtabs (pst != NULL),
20271 comp_dir == pst->dirname.
20272
20273 NOTE: It is important that psymtabs have the same file name (via strcmp)
20274 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20275 symtab we don't use it in the name of the psymtabs we create.
20276 E.g. expand_line_sal requires this when finding psymtabs to expand.
20277 A good testcase for this is mb-inline.exp.
20278
20279 LOWPC is the lowest address in CU (or 0 if not known).
20280
20281 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20282 for its PC<->lines mapping information. Otherwise only the filename
20283 table is read in. */
20284
20285 static void
20286 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20287 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20288 CORE_ADDR lowpc, int decode_mapping)
20289 {
20290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20291 const int decode_for_pst_p = (pst != NULL);
20292
20293 if (decode_mapping)
20294 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20295
20296 if (decode_for_pst_p)
20297 {
20298 /* Now that we're done scanning the Line Header Program, we can
20299 create the psymtab of each included file. */
20300 for (auto &file_entry : lh->file_names ())
20301 if (file_entry.included_p == 1)
20302 {
20303 gdb::unique_xmalloc_ptr<char> name_holder;
20304 const char *include_name =
20305 psymtab_include_file_name (lh, file_entry, pst,
20306 comp_dir, &name_holder);
20307 if (include_name != NULL)
20308 dwarf2_create_include_psymtab (include_name, pst, objfile);
20309 }
20310 }
20311 else
20312 {
20313 /* Make sure a symtab is created for every file, even files
20314 which contain only variables (i.e. no code with associated
20315 line numbers). */
20316 buildsym_compunit *builder = cu->get_builder ();
20317 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20318
20319 for (auto &fe : lh->file_names ())
20320 {
20321 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20322 if (builder->get_current_subfile ()->symtab == NULL)
20323 {
20324 builder->get_current_subfile ()->symtab
20325 = allocate_symtab (cust,
20326 builder->get_current_subfile ()->name);
20327 }
20328 fe.symtab = builder->get_current_subfile ()->symtab;
20329 }
20330 }
20331 }
20332
20333 /* Start a subfile for DWARF. FILENAME is the name of the file and
20334 DIRNAME the name of the source directory which contains FILENAME
20335 or NULL if not known.
20336 This routine tries to keep line numbers from identical absolute and
20337 relative file names in a common subfile.
20338
20339 Using the `list' example from the GDB testsuite, which resides in
20340 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20341 of /srcdir/list0.c yields the following debugging information for list0.c:
20342
20343 DW_AT_name: /srcdir/list0.c
20344 DW_AT_comp_dir: /compdir
20345 files.files[0].name: list0.h
20346 files.files[0].dir: /srcdir
20347 files.files[1].name: list0.c
20348 files.files[1].dir: /srcdir
20349
20350 The line number information for list0.c has to end up in a single
20351 subfile, so that `break /srcdir/list0.c:1' works as expected.
20352 start_subfile will ensure that this happens provided that we pass the
20353 concatenation of files.files[1].dir and files.files[1].name as the
20354 subfile's name. */
20355
20356 static void
20357 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20358 const char *dirname)
20359 {
20360 gdb::unique_xmalloc_ptr<char> copy;
20361
20362 /* In order not to lose the line information directory,
20363 we concatenate it to the filename when it makes sense.
20364 Note that the Dwarf3 standard says (speaking of filenames in line
20365 information): ``The directory index is ignored for file names
20366 that represent full path names''. Thus ignoring dirname in the
20367 `else' branch below isn't an issue. */
20368
20369 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20370 {
20371 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20372 filename = copy.get ();
20373 }
20374
20375 cu->get_builder ()->start_subfile (filename);
20376 }
20377
20378 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20379 buildsym_compunit constructor. */
20380
20381 struct compunit_symtab *
20382 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20383 CORE_ADDR low_pc)
20384 {
20385 gdb_assert (m_builder == nullptr);
20386
20387 m_builder.reset (new struct buildsym_compunit
20388 (per_cu->dwarf2_per_objfile->objfile,
20389 name, comp_dir, language, low_pc));
20390
20391 list_in_scope = get_builder ()->get_file_symbols ();
20392
20393 get_builder ()->record_debugformat ("DWARF 2");
20394 get_builder ()->record_producer (producer);
20395
20396 processing_has_namespace_info = false;
20397
20398 return get_builder ()->get_compunit_symtab ();
20399 }
20400
20401 static void
20402 var_decode_location (struct attribute *attr, struct symbol *sym,
20403 struct dwarf2_cu *cu)
20404 {
20405 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20406 struct comp_unit_head *cu_header = &cu->header;
20407
20408 /* NOTE drow/2003-01-30: There used to be a comment and some special
20409 code here to turn a symbol with DW_AT_external and a
20410 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20411 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20412 with some versions of binutils) where shared libraries could have
20413 relocations against symbols in their debug information - the
20414 minimal symbol would have the right address, but the debug info
20415 would not. It's no longer necessary, because we will explicitly
20416 apply relocations when we read in the debug information now. */
20417
20418 /* A DW_AT_location attribute with no contents indicates that a
20419 variable has been optimized away. */
20420 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20421 {
20422 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20423 return;
20424 }
20425
20426 /* Handle one degenerate form of location expression specially, to
20427 preserve GDB's previous behavior when section offsets are
20428 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20429 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20430
20431 if (attr->form_is_block ()
20432 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20433 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20434 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20435 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20436 && (DW_BLOCK (attr)->size
20437 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20438 {
20439 unsigned int dummy;
20440
20441 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20442 SET_SYMBOL_VALUE_ADDRESS
20443 (sym, cu->header.read_address (objfile->obfd,
20444 DW_BLOCK (attr)->data + 1,
20445 &dummy));
20446 else
20447 SET_SYMBOL_VALUE_ADDRESS
20448 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20449 &dummy));
20450 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20451 fixup_symbol_section (sym, objfile);
20452 SET_SYMBOL_VALUE_ADDRESS
20453 (sym,
20454 SYMBOL_VALUE_ADDRESS (sym)
20455 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20456 return;
20457 }
20458
20459 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20460 expression evaluator, and use LOC_COMPUTED only when necessary
20461 (i.e. when the value of a register or memory location is
20462 referenced, or a thread-local block, etc.). Then again, it might
20463 not be worthwhile. I'm assuming that it isn't unless performance
20464 or memory numbers show me otherwise. */
20465
20466 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20467
20468 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20469 cu->has_loclist = true;
20470 }
20471
20472 /* Given a pointer to a DWARF information entry, figure out if we need
20473 to make a symbol table entry for it, and if so, create a new entry
20474 and return a pointer to it.
20475 If TYPE is NULL, determine symbol type from the die, otherwise
20476 used the passed type.
20477 If SPACE is not NULL, use it to hold the new symbol. If it is
20478 NULL, allocate a new symbol on the objfile's obstack. */
20479
20480 static struct symbol *
20481 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20482 struct symbol *space)
20483 {
20484 struct dwarf2_per_objfile *dwarf2_per_objfile
20485 = cu->per_cu->dwarf2_per_objfile;
20486 struct objfile *objfile = dwarf2_per_objfile->objfile;
20487 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20488 struct symbol *sym = NULL;
20489 const char *name;
20490 struct attribute *attr = NULL;
20491 struct attribute *attr2 = NULL;
20492 CORE_ADDR baseaddr;
20493 struct pending **list_to_add = NULL;
20494
20495 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20496
20497 baseaddr = objfile->text_section_offset ();
20498
20499 name = dwarf2_name (die, cu);
20500 if (name)
20501 {
20502 const char *linkagename;
20503 int suppress_add = 0;
20504
20505 if (space)
20506 sym = space;
20507 else
20508 sym = allocate_symbol (objfile);
20509 OBJSTAT (objfile, n_syms++);
20510
20511 /* Cache this symbol's name and the name's demangled form (if any). */
20512 sym->set_language (cu->language, &objfile->objfile_obstack);
20513 linkagename = dwarf2_physname (name, die, cu);
20514 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20515
20516 /* Fortran does not have mangling standard and the mangling does differ
20517 between gfortran, iFort etc. */
20518 if (cu->language == language_fortran
20519 && symbol_get_demangled_name (sym) == NULL)
20520 symbol_set_demangled_name (sym,
20521 dwarf2_full_name (name, die, cu),
20522 NULL);
20523
20524 /* Default assumptions.
20525 Use the passed type or decode it from the die. */
20526 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20527 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20528 if (type != NULL)
20529 SYMBOL_TYPE (sym) = type;
20530 else
20531 SYMBOL_TYPE (sym) = die_type (die, cu);
20532 attr = dwarf2_attr (die,
20533 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20534 cu);
20535 if (attr != nullptr)
20536 {
20537 SYMBOL_LINE (sym) = DW_UNSND (attr);
20538 }
20539
20540 attr = dwarf2_attr (die,
20541 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20542 cu);
20543 if (attr != nullptr)
20544 {
20545 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20546 struct file_entry *fe;
20547
20548 if (cu->line_header != NULL)
20549 fe = cu->line_header->file_name_at (file_index);
20550 else
20551 fe = NULL;
20552
20553 if (fe == NULL)
20554 complaint (_("file index out of range"));
20555 else
20556 symbol_set_symtab (sym, fe->symtab);
20557 }
20558
20559 switch (die->tag)
20560 {
20561 case DW_TAG_label:
20562 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20563 if (attr != nullptr)
20564 {
20565 CORE_ADDR addr;
20566
20567 addr = attr->value_as_address ();
20568 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20569 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20570 }
20571 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20572 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20573 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20574 add_symbol_to_list (sym, cu->list_in_scope);
20575 break;
20576 case DW_TAG_subprogram:
20577 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20578 finish_block. */
20579 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20580 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20581 if ((attr2 && (DW_UNSND (attr2) != 0))
20582 || cu->language == language_ada
20583 || cu->language == language_fortran)
20584 {
20585 /* Subprograms marked external are stored as a global symbol.
20586 Ada and Fortran subprograms, whether marked external or
20587 not, are always stored as a global symbol, because we want
20588 to be able to access them globally. For instance, we want
20589 to be able to break on a nested subprogram without having
20590 to specify the context. */
20591 list_to_add = cu->get_builder ()->get_global_symbols ();
20592 }
20593 else
20594 {
20595 list_to_add = cu->list_in_scope;
20596 }
20597 break;
20598 case DW_TAG_inlined_subroutine:
20599 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20600 finish_block. */
20601 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20602 SYMBOL_INLINED (sym) = 1;
20603 list_to_add = cu->list_in_scope;
20604 break;
20605 case DW_TAG_template_value_param:
20606 suppress_add = 1;
20607 /* Fall through. */
20608 case DW_TAG_constant:
20609 case DW_TAG_variable:
20610 case DW_TAG_member:
20611 /* Compilation with minimal debug info may result in
20612 variables with missing type entries. Change the
20613 misleading `void' type to something sensible. */
20614 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20615 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20616
20617 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20618 /* In the case of DW_TAG_member, we should only be called for
20619 static const members. */
20620 if (die->tag == DW_TAG_member)
20621 {
20622 /* dwarf2_add_field uses die_is_declaration,
20623 so we do the same. */
20624 gdb_assert (die_is_declaration (die, cu));
20625 gdb_assert (attr);
20626 }
20627 if (attr != nullptr)
20628 {
20629 dwarf2_const_value (attr, sym, cu);
20630 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20631 if (!suppress_add)
20632 {
20633 if (attr2 && (DW_UNSND (attr2) != 0))
20634 list_to_add = cu->get_builder ()->get_global_symbols ();
20635 else
20636 list_to_add = cu->list_in_scope;
20637 }
20638 break;
20639 }
20640 attr = dwarf2_attr (die, DW_AT_location, cu);
20641 if (attr != nullptr)
20642 {
20643 var_decode_location (attr, sym, cu);
20644 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20645
20646 /* Fortran explicitly imports any global symbols to the local
20647 scope by DW_TAG_common_block. */
20648 if (cu->language == language_fortran && die->parent
20649 && die->parent->tag == DW_TAG_common_block)
20650 attr2 = NULL;
20651
20652 if (SYMBOL_CLASS (sym) == LOC_STATIC
20653 && SYMBOL_VALUE_ADDRESS (sym) == 0
20654 && !dwarf2_per_objfile->has_section_at_zero)
20655 {
20656 /* When a static variable is eliminated by the linker,
20657 the corresponding debug information is not stripped
20658 out, but the variable address is set to null;
20659 do not add such variables into symbol table. */
20660 }
20661 else if (attr2 && (DW_UNSND (attr2) != 0))
20662 {
20663 if (SYMBOL_CLASS (sym) == LOC_STATIC
20664 && (objfile->flags & OBJF_MAINLINE) == 0
20665 && dwarf2_per_objfile->can_copy)
20666 {
20667 /* A global static variable might be subject to
20668 copy relocation. We first check for a local
20669 minsym, though, because maybe the symbol was
20670 marked hidden, in which case this would not
20671 apply. */
20672 bound_minimal_symbol found
20673 = (lookup_minimal_symbol_linkage
20674 (sym->linkage_name (), objfile));
20675 if (found.minsym != nullptr)
20676 sym->maybe_copied = 1;
20677 }
20678
20679 /* A variable with DW_AT_external is never static,
20680 but it may be block-scoped. */
20681 list_to_add
20682 = ((cu->list_in_scope
20683 == cu->get_builder ()->get_file_symbols ())
20684 ? cu->get_builder ()->get_global_symbols ()
20685 : cu->list_in_scope);
20686 }
20687 else
20688 list_to_add = cu->list_in_scope;
20689 }
20690 else
20691 {
20692 /* We do not know the address of this symbol.
20693 If it is an external symbol and we have type information
20694 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20695 The address of the variable will then be determined from
20696 the minimal symbol table whenever the variable is
20697 referenced. */
20698 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20699
20700 /* Fortran explicitly imports any global symbols to the local
20701 scope by DW_TAG_common_block. */
20702 if (cu->language == language_fortran && die->parent
20703 && die->parent->tag == DW_TAG_common_block)
20704 {
20705 /* SYMBOL_CLASS doesn't matter here because
20706 read_common_block is going to reset it. */
20707 if (!suppress_add)
20708 list_to_add = cu->list_in_scope;
20709 }
20710 else if (attr2 && (DW_UNSND (attr2) != 0)
20711 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20712 {
20713 /* A variable with DW_AT_external is never static, but it
20714 may be block-scoped. */
20715 list_to_add
20716 = ((cu->list_in_scope
20717 == cu->get_builder ()->get_file_symbols ())
20718 ? cu->get_builder ()->get_global_symbols ()
20719 : cu->list_in_scope);
20720
20721 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20722 }
20723 else if (!die_is_declaration (die, cu))
20724 {
20725 /* Use the default LOC_OPTIMIZED_OUT class. */
20726 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20727 if (!suppress_add)
20728 list_to_add = cu->list_in_scope;
20729 }
20730 }
20731 break;
20732 case DW_TAG_formal_parameter:
20733 {
20734 /* If we are inside a function, mark this as an argument. If
20735 not, we might be looking at an argument to an inlined function
20736 when we do not have enough information to show inlined frames;
20737 pretend it's a local variable in that case so that the user can
20738 still see it. */
20739 struct context_stack *curr
20740 = cu->get_builder ()->get_current_context_stack ();
20741 if (curr != nullptr && curr->name != nullptr)
20742 SYMBOL_IS_ARGUMENT (sym) = 1;
20743 attr = dwarf2_attr (die, DW_AT_location, cu);
20744 if (attr != nullptr)
20745 {
20746 var_decode_location (attr, sym, cu);
20747 }
20748 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20749 if (attr != nullptr)
20750 {
20751 dwarf2_const_value (attr, sym, cu);
20752 }
20753
20754 list_to_add = cu->list_in_scope;
20755 }
20756 break;
20757 case DW_TAG_unspecified_parameters:
20758 /* From varargs functions; gdb doesn't seem to have any
20759 interest in this information, so just ignore it for now.
20760 (FIXME?) */
20761 break;
20762 case DW_TAG_template_type_param:
20763 suppress_add = 1;
20764 /* Fall through. */
20765 case DW_TAG_class_type:
20766 case DW_TAG_interface_type:
20767 case DW_TAG_structure_type:
20768 case DW_TAG_union_type:
20769 case DW_TAG_set_type:
20770 case DW_TAG_enumeration_type:
20771 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20772 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20773
20774 {
20775 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20776 really ever be static objects: otherwise, if you try
20777 to, say, break of a class's method and you're in a file
20778 which doesn't mention that class, it won't work unless
20779 the check for all static symbols in lookup_symbol_aux
20780 saves you. See the OtherFileClass tests in
20781 gdb.c++/namespace.exp. */
20782
20783 if (!suppress_add)
20784 {
20785 buildsym_compunit *builder = cu->get_builder ();
20786 list_to_add
20787 = (cu->list_in_scope == builder->get_file_symbols ()
20788 && cu->language == language_cplus
20789 ? builder->get_global_symbols ()
20790 : cu->list_in_scope);
20791
20792 /* The semantics of C++ state that "struct foo {
20793 ... }" also defines a typedef for "foo". */
20794 if (cu->language == language_cplus
20795 || cu->language == language_ada
20796 || cu->language == language_d
20797 || cu->language == language_rust)
20798 {
20799 /* The symbol's name is already allocated along
20800 with this objfile, so we don't need to
20801 duplicate it for the type. */
20802 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20803 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20804 }
20805 }
20806 }
20807 break;
20808 case DW_TAG_typedef:
20809 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20810 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20811 list_to_add = cu->list_in_scope;
20812 break;
20813 case DW_TAG_base_type:
20814 case DW_TAG_subrange_type:
20815 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20816 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20817 list_to_add = cu->list_in_scope;
20818 break;
20819 case DW_TAG_enumerator:
20820 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20821 if (attr != nullptr)
20822 {
20823 dwarf2_const_value (attr, sym, cu);
20824 }
20825 {
20826 /* NOTE: carlton/2003-11-10: See comment above in the
20827 DW_TAG_class_type, etc. block. */
20828
20829 list_to_add
20830 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20831 && cu->language == language_cplus
20832 ? cu->get_builder ()->get_global_symbols ()
20833 : cu->list_in_scope);
20834 }
20835 break;
20836 case DW_TAG_imported_declaration:
20837 case DW_TAG_namespace:
20838 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20839 list_to_add = cu->get_builder ()->get_global_symbols ();
20840 break;
20841 case DW_TAG_module:
20842 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20843 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20844 list_to_add = cu->get_builder ()->get_global_symbols ();
20845 break;
20846 case DW_TAG_common_block:
20847 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20848 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20849 add_symbol_to_list (sym, cu->list_in_scope);
20850 break;
20851 default:
20852 /* Not a tag we recognize. Hopefully we aren't processing
20853 trash data, but since we must specifically ignore things
20854 we don't recognize, there is nothing else we should do at
20855 this point. */
20856 complaint (_("unsupported tag: '%s'"),
20857 dwarf_tag_name (die->tag));
20858 break;
20859 }
20860
20861 if (suppress_add)
20862 {
20863 sym->hash_next = objfile->template_symbols;
20864 objfile->template_symbols = sym;
20865 list_to_add = NULL;
20866 }
20867
20868 if (list_to_add != NULL)
20869 add_symbol_to_list (sym, list_to_add);
20870
20871 /* For the benefit of old versions of GCC, check for anonymous
20872 namespaces based on the demangled name. */
20873 if (!cu->processing_has_namespace_info
20874 && cu->language == language_cplus)
20875 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20876 }
20877 return (sym);
20878 }
20879
20880 /* Given an attr with a DW_FORM_dataN value in host byte order,
20881 zero-extend it as appropriate for the symbol's type. The DWARF
20882 standard (v4) is not entirely clear about the meaning of using
20883 DW_FORM_dataN for a constant with a signed type, where the type is
20884 wider than the data. The conclusion of a discussion on the DWARF
20885 list was that this is unspecified. We choose to always zero-extend
20886 because that is the interpretation long in use by GCC. */
20887
20888 static gdb_byte *
20889 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20890 struct dwarf2_cu *cu, LONGEST *value, int bits)
20891 {
20892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20893 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20894 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20895 LONGEST l = DW_UNSND (attr);
20896
20897 if (bits < sizeof (*value) * 8)
20898 {
20899 l &= ((LONGEST) 1 << bits) - 1;
20900 *value = l;
20901 }
20902 else if (bits == sizeof (*value) * 8)
20903 *value = l;
20904 else
20905 {
20906 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20907 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20908 return bytes;
20909 }
20910
20911 return NULL;
20912 }
20913
20914 /* Read a constant value from an attribute. Either set *VALUE, or if
20915 the value does not fit in *VALUE, set *BYTES - either already
20916 allocated on the objfile obstack, or newly allocated on OBSTACK,
20917 or, set *BATON, if we translated the constant to a location
20918 expression. */
20919
20920 static void
20921 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20922 const char *name, struct obstack *obstack,
20923 struct dwarf2_cu *cu,
20924 LONGEST *value, const gdb_byte **bytes,
20925 struct dwarf2_locexpr_baton **baton)
20926 {
20927 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20928 struct comp_unit_head *cu_header = &cu->header;
20929 struct dwarf_block *blk;
20930 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20931 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20932
20933 *value = 0;
20934 *bytes = NULL;
20935 *baton = NULL;
20936
20937 switch (attr->form)
20938 {
20939 case DW_FORM_addr:
20940 case DW_FORM_addrx:
20941 case DW_FORM_GNU_addr_index:
20942 {
20943 gdb_byte *data;
20944
20945 if (TYPE_LENGTH (type) != cu_header->addr_size)
20946 dwarf2_const_value_length_mismatch_complaint (name,
20947 cu_header->addr_size,
20948 TYPE_LENGTH (type));
20949 /* Symbols of this form are reasonably rare, so we just
20950 piggyback on the existing location code rather than writing
20951 a new implementation of symbol_computed_ops. */
20952 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20953 (*baton)->per_cu = cu->per_cu;
20954 gdb_assert ((*baton)->per_cu);
20955
20956 (*baton)->size = 2 + cu_header->addr_size;
20957 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20958 (*baton)->data = data;
20959
20960 data[0] = DW_OP_addr;
20961 store_unsigned_integer (&data[1], cu_header->addr_size,
20962 byte_order, DW_ADDR (attr));
20963 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20964 }
20965 break;
20966 case DW_FORM_string:
20967 case DW_FORM_strp:
20968 case DW_FORM_strx:
20969 case DW_FORM_GNU_str_index:
20970 case DW_FORM_GNU_strp_alt:
20971 /* DW_STRING is already allocated on the objfile obstack, point
20972 directly to it. */
20973 *bytes = (const gdb_byte *) DW_STRING (attr);
20974 break;
20975 case DW_FORM_block1:
20976 case DW_FORM_block2:
20977 case DW_FORM_block4:
20978 case DW_FORM_block:
20979 case DW_FORM_exprloc:
20980 case DW_FORM_data16:
20981 blk = DW_BLOCK (attr);
20982 if (TYPE_LENGTH (type) != blk->size)
20983 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20984 TYPE_LENGTH (type));
20985 *bytes = blk->data;
20986 break;
20987
20988 /* The DW_AT_const_value attributes are supposed to carry the
20989 symbol's value "represented as it would be on the target
20990 architecture." By the time we get here, it's already been
20991 converted to host endianness, so we just need to sign- or
20992 zero-extend it as appropriate. */
20993 case DW_FORM_data1:
20994 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20995 break;
20996 case DW_FORM_data2:
20997 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20998 break;
20999 case DW_FORM_data4:
21000 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21001 break;
21002 case DW_FORM_data8:
21003 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21004 break;
21005
21006 case DW_FORM_sdata:
21007 case DW_FORM_implicit_const:
21008 *value = DW_SND (attr);
21009 break;
21010
21011 case DW_FORM_udata:
21012 *value = DW_UNSND (attr);
21013 break;
21014
21015 default:
21016 complaint (_("unsupported const value attribute form: '%s'"),
21017 dwarf_form_name (attr->form));
21018 *value = 0;
21019 break;
21020 }
21021 }
21022
21023
21024 /* Copy constant value from an attribute to a symbol. */
21025
21026 static void
21027 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21028 struct dwarf2_cu *cu)
21029 {
21030 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21031 LONGEST value;
21032 const gdb_byte *bytes;
21033 struct dwarf2_locexpr_baton *baton;
21034
21035 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21036 sym->print_name (),
21037 &objfile->objfile_obstack, cu,
21038 &value, &bytes, &baton);
21039
21040 if (baton != NULL)
21041 {
21042 SYMBOL_LOCATION_BATON (sym) = baton;
21043 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21044 }
21045 else if (bytes != NULL)
21046 {
21047 SYMBOL_VALUE_BYTES (sym) = bytes;
21048 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21049 }
21050 else
21051 {
21052 SYMBOL_VALUE (sym) = value;
21053 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21054 }
21055 }
21056
21057 /* Return the type of the die in question using its DW_AT_type attribute. */
21058
21059 static struct type *
21060 die_type (struct die_info *die, struct dwarf2_cu *cu)
21061 {
21062 struct attribute *type_attr;
21063
21064 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21065 if (!type_attr)
21066 {
21067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21068 /* A missing DW_AT_type represents a void type. */
21069 return objfile_type (objfile)->builtin_void;
21070 }
21071
21072 return lookup_die_type (die, type_attr, cu);
21073 }
21074
21075 /* True iff CU's producer generates GNAT Ada auxiliary information
21076 that allows to find parallel types through that information instead
21077 of having to do expensive parallel lookups by type name. */
21078
21079 static int
21080 need_gnat_info (struct dwarf2_cu *cu)
21081 {
21082 /* Assume that the Ada compiler was GNAT, which always produces
21083 the auxiliary information. */
21084 return (cu->language == language_ada);
21085 }
21086
21087 /* Return the auxiliary type of the die in question using its
21088 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21089 attribute is not present. */
21090
21091 static struct type *
21092 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21093 {
21094 struct attribute *type_attr;
21095
21096 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21097 if (!type_attr)
21098 return NULL;
21099
21100 return lookup_die_type (die, type_attr, cu);
21101 }
21102
21103 /* If DIE has a descriptive_type attribute, then set the TYPE's
21104 descriptive type accordingly. */
21105
21106 static void
21107 set_descriptive_type (struct type *type, struct die_info *die,
21108 struct dwarf2_cu *cu)
21109 {
21110 struct type *descriptive_type = die_descriptive_type (die, cu);
21111
21112 if (descriptive_type)
21113 {
21114 ALLOCATE_GNAT_AUX_TYPE (type);
21115 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21116 }
21117 }
21118
21119 /* Return the containing type of the die in question using its
21120 DW_AT_containing_type attribute. */
21121
21122 static struct type *
21123 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21124 {
21125 struct attribute *type_attr;
21126 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21127
21128 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21129 if (!type_attr)
21130 error (_("Dwarf Error: Problem turning containing type into gdb type "
21131 "[in module %s]"), objfile_name (objfile));
21132
21133 return lookup_die_type (die, type_attr, cu);
21134 }
21135
21136 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21137
21138 static struct type *
21139 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21140 {
21141 struct dwarf2_per_objfile *dwarf2_per_objfile
21142 = cu->per_cu->dwarf2_per_objfile;
21143 struct objfile *objfile = dwarf2_per_objfile->objfile;
21144 char *saved;
21145
21146 std::string message
21147 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21148 objfile_name (objfile),
21149 sect_offset_str (cu->header.sect_off),
21150 sect_offset_str (die->sect_off));
21151 saved = obstack_strdup (&objfile->objfile_obstack, message);
21152
21153 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21154 }
21155
21156 /* Look up the type of DIE in CU using its type attribute ATTR.
21157 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21158 DW_AT_containing_type.
21159 If there is no type substitute an error marker. */
21160
21161 static struct type *
21162 lookup_die_type (struct die_info *die, const struct attribute *attr,
21163 struct dwarf2_cu *cu)
21164 {
21165 struct dwarf2_per_objfile *dwarf2_per_objfile
21166 = cu->per_cu->dwarf2_per_objfile;
21167 struct objfile *objfile = dwarf2_per_objfile->objfile;
21168 struct type *this_type;
21169
21170 gdb_assert (attr->name == DW_AT_type
21171 || attr->name == DW_AT_GNAT_descriptive_type
21172 || attr->name == DW_AT_containing_type);
21173
21174 /* First see if we have it cached. */
21175
21176 if (attr->form == DW_FORM_GNU_ref_alt)
21177 {
21178 struct dwarf2_per_cu_data *per_cu;
21179 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21180
21181 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21182 dwarf2_per_objfile);
21183 this_type = get_die_type_at_offset (sect_off, per_cu);
21184 }
21185 else if (attr->form_is_ref ())
21186 {
21187 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21188
21189 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21190 }
21191 else if (attr->form == DW_FORM_ref_sig8)
21192 {
21193 ULONGEST signature = DW_SIGNATURE (attr);
21194
21195 return get_signatured_type (die, signature, cu);
21196 }
21197 else
21198 {
21199 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21200 " at %s [in module %s]"),
21201 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21202 objfile_name (objfile));
21203 return build_error_marker_type (cu, die);
21204 }
21205
21206 /* If not cached we need to read it in. */
21207
21208 if (this_type == NULL)
21209 {
21210 struct die_info *type_die = NULL;
21211 struct dwarf2_cu *type_cu = cu;
21212
21213 if (attr->form_is_ref ())
21214 type_die = follow_die_ref (die, attr, &type_cu);
21215 if (type_die == NULL)
21216 return build_error_marker_type (cu, die);
21217 /* If we find the type now, it's probably because the type came
21218 from an inter-CU reference and the type's CU got expanded before
21219 ours. */
21220 this_type = read_type_die (type_die, type_cu);
21221 }
21222
21223 /* If we still don't have a type use an error marker. */
21224
21225 if (this_type == NULL)
21226 return build_error_marker_type (cu, die);
21227
21228 return this_type;
21229 }
21230
21231 /* Return the type in DIE, CU.
21232 Returns NULL for invalid types.
21233
21234 This first does a lookup in die_type_hash,
21235 and only reads the die in if necessary.
21236
21237 NOTE: This can be called when reading in partial or full symbols. */
21238
21239 static struct type *
21240 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21241 {
21242 struct type *this_type;
21243
21244 this_type = get_die_type (die, cu);
21245 if (this_type)
21246 return this_type;
21247
21248 return read_type_die_1 (die, cu);
21249 }
21250
21251 /* Read the type in DIE, CU.
21252 Returns NULL for invalid types. */
21253
21254 static struct type *
21255 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21256 {
21257 struct type *this_type = NULL;
21258
21259 switch (die->tag)
21260 {
21261 case DW_TAG_class_type:
21262 case DW_TAG_interface_type:
21263 case DW_TAG_structure_type:
21264 case DW_TAG_union_type:
21265 this_type = read_structure_type (die, cu);
21266 break;
21267 case DW_TAG_enumeration_type:
21268 this_type = read_enumeration_type (die, cu);
21269 break;
21270 case DW_TAG_subprogram:
21271 case DW_TAG_subroutine_type:
21272 case DW_TAG_inlined_subroutine:
21273 this_type = read_subroutine_type (die, cu);
21274 break;
21275 case DW_TAG_array_type:
21276 this_type = read_array_type (die, cu);
21277 break;
21278 case DW_TAG_set_type:
21279 this_type = read_set_type (die, cu);
21280 break;
21281 case DW_TAG_pointer_type:
21282 this_type = read_tag_pointer_type (die, cu);
21283 break;
21284 case DW_TAG_ptr_to_member_type:
21285 this_type = read_tag_ptr_to_member_type (die, cu);
21286 break;
21287 case DW_TAG_reference_type:
21288 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21289 break;
21290 case DW_TAG_rvalue_reference_type:
21291 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21292 break;
21293 case DW_TAG_const_type:
21294 this_type = read_tag_const_type (die, cu);
21295 break;
21296 case DW_TAG_volatile_type:
21297 this_type = read_tag_volatile_type (die, cu);
21298 break;
21299 case DW_TAG_restrict_type:
21300 this_type = read_tag_restrict_type (die, cu);
21301 break;
21302 case DW_TAG_string_type:
21303 this_type = read_tag_string_type (die, cu);
21304 break;
21305 case DW_TAG_typedef:
21306 this_type = read_typedef (die, cu);
21307 break;
21308 case DW_TAG_subrange_type:
21309 this_type = read_subrange_type (die, cu);
21310 break;
21311 case DW_TAG_base_type:
21312 this_type = read_base_type (die, cu);
21313 break;
21314 case DW_TAG_unspecified_type:
21315 this_type = read_unspecified_type (die, cu);
21316 break;
21317 case DW_TAG_namespace:
21318 this_type = read_namespace_type (die, cu);
21319 break;
21320 case DW_TAG_module:
21321 this_type = read_module_type (die, cu);
21322 break;
21323 case DW_TAG_atomic_type:
21324 this_type = read_tag_atomic_type (die, cu);
21325 break;
21326 default:
21327 complaint (_("unexpected tag in read_type_die: '%s'"),
21328 dwarf_tag_name (die->tag));
21329 break;
21330 }
21331
21332 return this_type;
21333 }
21334
21335 /* See if we can figure out if the class lives in a namespace. We do
21336 this by looking for a member function; its demangled name will
21337 contain namespace info, if there is any.
21338 Return the computed name or NULL.
21339 Space for the result is allocated on the objfile's obstack.
21340 This is the full-die version of guess_partial_die_structure_name.
21341 In this case we know DIE has no useful parent. */
21342
21343 static const char *
21344 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21345 {
21346 struct die_info *spec_die;
21347 struct dwarf2_cu *spec_cu;
21348 struct die_info *child;
21349 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21350
21351 spec_cu = cu;
21352 spec_die = die_specification (die, &spec_cu);
21353 if (spec_die != NULL)
21354 {
21355 die = spec_die;
21356 cu = spec_cu;
21357 }
21358
21359 for (child = die->child;
21360 child != NULL;
21361 child = child->sibling)
21362 {
21363 if (child->tag == DW_TAG_subprogram)
21364 {
21365 const char *linkage_name = dw2_linkage_name (child, cu);
21366
21367 if (linkage_name != NULL)
21368 {
21369 gdb::unique_xmalloc_ptr<char> actual_name
21370 (language_class_name_from_physname (cu->language_defn,
21371 linkage_name));
21372 const char *name = NULL;
21373
21374 if (actual_name != NULL)
21375 {
21376 const char *die_name = dwarf2_name (die, cu);
21377
21378 if (die_name != NULL
21379 && strcmp (die_name, actual_name.get ()) != 0)
21380 {
21381 /* Strip off the class name from the full name.
21382 We want the prefix. */
21383 int die_name_len = strlen (die_name);
21384 int actual_name_len = strlen (actual_name.get ());
21385 const char *ptr = actual_name.get ();
21386
21387 /* Test for '::' as a sanity check. */
21388 if (actual_name_len > die_name_len + 2
21389 && ptr[actual_name_len - die_name_len - 1] == ':')
21390 name = obstack_strndup (
21391 &objfile->per_bfd->storage_obstack,
21392 ptr, actual_name_len - die_name_len - 2);
21393 }
21394 }
21395 return name;
21396 }
21397 }
21398 }
21399
21400 return NULL;
21401 }
21402
21403 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21404 prefix part in such case. See
21405 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21406
21407 static const char *
21408 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21409 {
21410 struct attribute *attr;
21411 const char *base;
21412
21413 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21414 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21415 return NULL;
21416
21417 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21418 return NULL;
21419
21420 attr = dw2_linkage_name_attr (die, cu);
21421 if (attr == NULL || DW_STRING (attr) == NULL)
21422 return NULL;
21423
21424 /* dwarf2_name had to be already called. */
21425 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21426
21427 /* Strip the base name, keep any leading namespaces/classes. */
21428 base = strrchr (DW_STRING (attr), ':');
21429 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21430 return "";
21431
21432 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21433 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21434 DW_STRING (attr),
21435 &base[-1] - DW_STRING (attr));
21436 }
21437
21438 /* Return the name of the namespace/class that DIE is defined within,
21439 or "" if we can't tell. The caller should not xfree the result.
21440
21441 For example, if we're within the method foo() in the following
21442 code:
21443
21444 namespace N {
21445 class C {
21446 void foo () {
21447 }
21448 };
21449 }
21450
21451 then determine_prefix on foo's die will return "N::C". */
21452
21453 static const char *
21454 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21455 {
21456 struct dwarf2_per_objfile *dwarf2_per_objfile
21457 = cu->per_cu->dwarf2_per_objfile;
21458 struct die_info *parent, *spec_die;
21459 struct dwarf2_cu *spec_cu;
21460 struct type *parent_type;
21461 const char *retval;
21462
21463 if (cu->language != language_cplus
21464 && cu->language != language_fortran && cu->language != language_d
21465 && cu->language != language_rust)
21466 return "";
21467
21468 retval = anonymous_struct_prefix (die, cu);
21469 if (retval)
21470 return retval;
21471
21472 /* We have to be careful in the presence of DW_AT_specification.
21473 For example, with GCC 3.4, given the code
21474
21475 namespace N {
21476 void foo() {
21477 // Definition of N::foo.
21478 }
21479 }
21480
21481 then we'll have a tree of DIEs like this:
21482
21483 1: DW_TAG_compile_unit
21484 2: DW_TAG_namespace // N
21485 3: DW_TAG_subprogram // declaration of N::foo
21486 4: DW_TAG_subprogram // definition of N::foo
21487 DW_AT_specification // refers to die #3
21488
21489 Thus, when processing die #4, we have to pretend that we're in
21490 the context of its DW_AT_specification, namely the contex of die
21491 #3. */
21492 spec_cu = cu;
21493 spec_die = die_specification (die, &spec_cu);
21494 if (spec_die == NULL)
21495 parent = die->parent;
21496 else
21497 {
21498 parent = spec_die->parent;
21499 cu = spec_cu;
21500 }
21501
21502 if (parent == NULL)
21503 return "";
21504 else if (parent->building_fullname)
21505 {
21506 const char *name;
21507 const char *parent_name;
21508
21509 /* It has been seen on RealView 2.2 built binaries,
21510 DW_TAG_template_type_param types actually _defined_ as
21511 children of the parent class:
21512
21513 enum E {};
21514 template class <class Enum> Class{};
21515 Class<enum E> class_e;
21516
21517 1: DW_TAG_class_type (Class)
21518 2: DW_TAG_enumeration_type (E)
21519 3: DW_TAG_enumerator (enum1:0)
21520 3: DW_TAG_enumerator (enum2:1)
21521 ...
21522 2: DW_TAG_template_type_param
21523 DW_AT_type DW_FORM_ref_udata (E)
21524
21525 Besides being broken debug info, it can put GDB into an
21526 infinite loop. Consider:
21527
21528 When we're building the full name for Class<E>, we'll start
21529 at Class, and go look over its template type parameters,
21530 finding E. We'll then try to build the full name of E, and
21531 reach here. We're now trying to build the full name of E,
21532 and look over the parent DIE for containing scope. In the
21533 broken case, if we followed the parent DIE of E, we'd again
21534 find Class, and once again go look at its template type
21535 arguments, etc., etc. Simply don't consider such parent die
21536 as source-level parent of this die (it can't be, the language
21537 doesn't allow it), and break the loop here. */
21538 name = dwarf2_name (die, cu);
21539 parent_name = dwarf2_name (parent, cu);
21540 complaint (_("template param type '%s' defined within parent '%s'"),
21541 name ? name : "<unknown>",
21542 parent_name ? parent_name : "<unknown>");
21543 return "";
21544 }
21545 else
21546 switch (parent->tag)
21547 {
21548 case DW_TAG_namespace:
21549 parent_type = read_type_die (parent, cu);
21550 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21551 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21552 Work around this problem here. */
21553 if (cu->language == language_cplus
21554 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21555 return "";
21556 /* We give a name to even anonymous namespaces. */
21557 return TYPE_NAME (parent_type);
21558 case DW_TAG_class_type:
21559 case DW_TAG_interface_type:
21560 case DW_TAG_structure_type:
21561 case DW_TAG_union_type:
21562 case DW_TAG_module:
21563 parent_type = read_type_die (parent, cu);
21564 if (TYPE_NAME (parent_type) != NULL)
21565 return TYPE_NAME (parent_type);
21566 else
21567 /* An anonymous structure is only allowed non-static data
21568 members; no typedefs, no member functions, et cetera.
21569 So it does not need a prefix. */
21570 return "";
21571 case DW_TAG_compile_unit:
21572 case DW_TAG_partial_unit:
21573 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21574 if (cu->language == language_cplus
21575 && !dwarf2_per_objfile->types.empty ()
21576 && die->child != NULL
21577 && (die->tag == DW_TAG_class_type
21578 || die->tag == DW_TAG_structure_type
21579 || die->tag == DW_TAG_union_type))
21580 {
21581 const char *name = guess_full_die_structure_name (die, cu);
21582 if (name != NULL)
21583 return name;
21584 }
21585 return "";
21586 case DW_TAG_subprogram:
21587 /* Nested subroutines in Fortran get a prefix with the name
21588 of the parent's subroutine. */
21589 if (cu->language == language_fortran)
21590 {
21591 if ((die->tag == DW_TAG_subprogram)
21592 && (dwarf2_name (parent, cu) != NULL))
21593 return dwarf2_name (parent, cu);
21594 }
21595 return determine_prefix (parent, cu);
21596 case DW_TAG_enumeration_type:
21597 parent_type = read_type_die (parent, cu);
21598 if (TYPE_DECLARED_CLASS (parent_type))
21599 {
21600 if (TYPE_NAME (parent_type) != NULL)
21601 return TYPE_NAME (parent_type);
21602 return "";
21603 }
21604 /* Fall through. */
21605 default:
21606 return determine_prefix (parent, cu);
21607 }
21608 }
21609
21610 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21611 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21612 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21613 an obconcat, otherwise allocate storage for the result. The CU argument is
21614 used to determine the language and hence, the appropriate separator. */
21615
21616 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21617
21618 static char *
21619 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21620 int physname, struct dwarf2_cu *cu)
21621 {
21622 const char *lead = "";
21623 const char *sep;
21624
21625 if (suffix == NULL || suffix[0] == '\0'
21626 || prefix == NULL || prefix[0] == '\0')
21627 sep = "";
21628 else if (cu->language == language_d)
21629 {
21630 /* For D, the 'main' function could be defined in any module, but it
21631 should never be prefixed. */
21632 if (strcmp (suffix, "D main") == 0)
21633 {
21634 prefix = "";
21635 sep = "";
21636 }
21637 else
21638 sep = ".";
21639 }
21640 else if (cu->language == language_fortran && physname)
21641 {
21642 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21643 DW_AT_MIPS_linkage_name is preferred and used instead. */
21644
21645 lead = "__";
21646 sep = "_MOD_";
21647 }
21648 else
21649 sep = "::";
21650
21651 if (prefix == NULL)
21652 prefix = "";
21653 if (suffix == NULL)
21654 suffix = "";
21655
21656 if (obs == NULL)
21657 {
21658 char *retval
21659 = ((char *)
21660 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21661
21662 strcpy (retval, lead);
21663 strcat (retval, prefix);
21664 strcat (retval, sep);
21665 strcat (retval, suffix);
21666 return retval;
21667 }
21668 else
21669 {
21670 /* We have an obstack. */
21671 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21672 }
21673 }
21674
21675 /* Return sibling of die, NULL if no sibling. */
21676
21677 static struct die_info *
21678 sibling_die (struct die_info *die)
21679 {
21680 return die->sibling;
21681 }
21682
21683 /* Get name of a die, return NULL if not found. */
21684
21685 static const char *
21686 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21687 struct obstack *obstack)
21688 {
21689 if (name && cu->language == language_cplus)
21690 {
21691 std::string canon_name = cp_canonicalize_string (name);
21692
21693 if (!canon_name.empty ())
21694 {
21695 if (canon_name != name)
21696 name = obstack_strdup (obstack, canon_name);
21697 }
21698 }
21699
21700 return name;
21701 }
21702
21703 /* Get name of a die, return NULL if not found.
21704 Anonymous namespaces are converted to their magic string. */
21705
21706 static const char *
21707 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21708 {
21709 struct attribute *attr;
21710 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21711
21712 attr = dwarf2_attr (die, DW_AT_name, cu);
21713 if ((!attr || !DW_STRING (attr))
21714 && die->tag != DW_TAG_namespace
21715 && die->tag != DW_TAG_class_type
21716 && die->tag != DW_TAG_interface_type
21717 && die->tag != DW_TAG_structure_type
21718 && die->tag != DW_TAG_union_type)
21719 return NULL;
21720
21721 switch (die->tag)
21722 {
21723 case DW_TAG_compile_unit:
21724 case DW_TAG_partial_unit:
21725 /* Compilation units have a DW_AT_name that is a filename, not
21726 a source language identifier. */
21727 case DW_TAG_enumeration_type:
21728 case DW_TAG_enumerator:
21729 /* These tags always have simple identifiers already; no need
21730 to canonicalize them. */
21731 return DW_STRING (attr);
21732
21733 case DW_TAG_namespace:
21734 if (attr != NULL && DW_STRING (attr) != NULL)
21735 return DW_STRING (attr);
21736 return CP_ANONYMOUS_NAMESPACE_STR;
21737
21738 case DW_TAG_class_type:
21739 case DW_TAG_interface_type:
21740 case DW_TAG_structure_type:
21741 case DW_TAG_union_type:
21742 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21743 structures or unions. These were of the form "._%d" in GCC 4.1,
21744 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21745 and GCC 4.4. We work around this problem by ignoring these. */
21746 if (attr && DW_STRING (attr)
21747 && (startswith (DW_STRING (attr), "._")
21748 || startswith (DW_STRING (attr), "<anonymous")))
21749 return NULL;
21750
21751 /* GCC might emit a nameless typedef that has a linkage name. See
21752 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21753 if (!attr || DW_STRING (attr) == NULL)
21754 {
21755 attr = dw2_linkage_name_attr (die, cu);
21756 if (attr == NULL || DW_STRING (attr) == NULL)
21757 return NULL;
21758
21759 /* Avoid demangling DW_STRING (attr) the second time on a second
21760 call for the same DIE. */
21761 if (!DW_STRING_IS_CANONICAL (attr))
21762 {
21763 gdb::unique_xmalloc_ptr<char> demangled
21764 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21765
21766 const char *base;
21767
21768 /* FIXME: we already did this for the partial symbol... */
21769 DW_STRING (attr)
21770 = obstack_strdup (&objfile->per_bfd->storage_obstack,
21771 demangled.get ());
21772 DW_STRING_IS_CANONICAL (attr) = 1;
21773
21774 /* Strip any leading namespaces/classes, keep only the base name.
21775 DW_AT_name for named DIEs does not contain the prefixes. */
21776 base = strrchr (DW_STRING (attr), ':');
21777 if (base && base > DW_STRING (attr) && base[-1] == ':')
21778 return &base[1];
21779 else
21780 return DW_STRING (attr);
21781 }
21782 }
21783 break;
21784
21785 default:
21786 break;
21787 }
21788
21789 if (!DW_STRING_IS_CANONICAL (attr))
21790 {
21791 DW_STRING (attr)
21792 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21793 &objfile->per_bfd->storage_obstack);
21794 DW_STRING_IS_CANONICAL (attr) = 1;
21795 }
21796 return DW_STRING (attr);
21797 }
21798
21799 /* Return the die that this die in an extension of, or NULL if there
21800 is none. *EXT_CU is the CU containing DIE on input, and the CU
21801 containing the return value on output. */
21802
21803 static struct die_info *
21804 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21805 {
21806 struct attribute *attr;
21807
21808 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21809 if (attr == NULL)
21810 return NULL;
21811
21812 return follow_die_ref (die, attr, ext_cu);
21813 }
21814
21815 /* A convenience function that returns an "unknown" DWARF name,
21816 including the value of V. STR is the name of the entity being
21817 printed, e.g., "TAG". */
21818
21819 static const char *
21820 dwarf_unknown (const char *str, unsigned v)
21821 {
21822 char *cell = get_print_cell ();
21823 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21824 return cell;
21825 }
21826
21827 /* Convert a DIE tag into its string name. */
21828
21829 static const char *
21830 dwarf_tag_name (unsigned tag)
21831 {
21832 const char *name = get_DW_TAG_name (tag);
21833
21834 if (name == NULL)
21835 return dwarf_unknown ("TAG", tag);
21836
21837 return name;
21838 }
21839
21840 /* Convert a DWARF attribute code into its string name. */
21841
21842 static const char *
21843 dwarf_attr_name (unsigned attr)
21844 {
21845 const char *name;
21846
21847 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21848 if (attr == DW_AT_MIPS_fde)
21849 return "DW_AT_MIPS_fde";
21850 #else
21851 if (attr == DW_AT_HP_block_index)
21852 return "DW_AT_HP_block_index";
21853 #endif
21854
21855 name = get_DW_AT_name (attr);
21856
21857 if (name == NULL)
21858 return dwarf_unknown ("AT", attr);
21859
21860 return name;
21861 }
21862
21863 /* Convert a DWARF value form code into its string name. */
21864
21865 static const char *
21866 dwarf_form_name (unsigned form)
21867 {
21868 const char *name = get_DW_FORM_name (form);
21869
21870 if (name == NULL)
21871 return dwarf_unknown ("FORM", form);
21872
21873 return name;
21874 }
21875
21876 static const char *
21877 dwarf_bool_name (unsigned mybool)
21878 {
21879 if (mybool)
21880 return "TRUE";
21881 else
21882 return "FALSE";
21883 }
21884
21885 /* Convert a DWARF type code into its string name. */
21886
21887 static const char *
21888 dwarf_type_encoding_name (unsigned enc)
21889 {
21890 const char *name = get_DW_ATE_name (enc);
21891
21892 if (name == NULL)
21893 return dwarf_unknown ("ATE", enc);
21894
21895 return name;
21896 }
21897
21898 static void
21899 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21900 {
21901 unsigned int i;
21902
21903 print_spaces (indent, f);
21904 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21905 dwarf_tag_name (die->tag), die->abbrev,
21906 sect_offset_str (die->sect_off));
21907
21908 if (die->parent != NULL)
21909 {
21910 print_spaces (indent, f);
21911 fprintf_unfiltered (f, " parent at offset: %s\n",
21912 sect_offset_str (die->parent->sect_off));
21913 }
21914
21915 print_spaces (indent, f);
21916 fprintf_unfiltered (f, " has children: %s\n",
21917 dwarf_bool_name (die->child != NULL));
21918
21919 print_spaces (indent, f);
21920 fprintf_unfiltered (f, " attributes:\n");
21921
21922 for (i = 0; i < die->num_attrs; ++i)
21923 {
21924 print_spaces (indent, f);
21925 fprintf_unfiltered (f, " %s (%s) ",
21926 dwarf_attr_name (die->attrs[i].name),
21927 dwarf_form_name (die->attrs[i].form));
21928
21929 switch (die->attrs[i].form)
21930 {
21931 case DW_FORM_addr:
21932 case DW_FORM_addrx:
21933 case DW_FORM_GNU_addr_index:
21934 fprintf_unfiltered (f, "address: ");
21935 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21936 break;
21937 case DW_FORM_block2:
21938 case DW_FORM_block4:
21939 case DW_FORM_block:
21940 case DW_FORM_block1:
21941 fprintf_unfiltered (f, "block: size %s",
21942 pulongest (DW_BLOCK (&die->attrs[i])->size));
21943 break;
21944 case DW_FORM_exprloc:
21945 fprintf_unfiltered (f, "expression: size %s",
21946 pulongest (DW_BLOCK (&die->attrs[i])->size));
21947 break;
21948 case DW_FORM_data16:
21949 fprintf_unfiltered (f, "constant of 16 bytes");
21950 break;
21951 case DW_FORM_ref_addr:
21952 fprintf_unfiltered (f, "ref address: ");
21953 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21954 break;
21955 case DW_FORM_GNU_ref_alt:
21956 fprintf_unfiltered (f, "alt ref address: ");
21957 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21958 break;
21959 case DW_FORM_ref1:
21960 case DW_FORM_ref2:
21961 case DW_FORM_ref4:
21962 case DW_FORM_ref8:
21963 case DW_FORM_ref_udata:
21964 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21965 (long) (DW_UNSND (&die->attrs[i])));
21966 break;
21967 case DW_FORM_data1:
21968 case DW_FORM_data2:
21969 case DW_FORM_data4:
21970 case DW_FORM_data8:
21971 case DW_FORM_udata:
21972 case DW_FORM_sdata:
21973 fprintf_unfiltered (f, "constant: %s",
21974 pulongest (DW_UNSND (&die->attrs[i])));
21975 break;
21976 case DW_FORM_sec_offset:
21977 fprintf_unfiltered (f, "section offset: %s",
21978 pulongest (DW_UNSND (&die->attrs[i])));
21979 break;
21980 case DW_FORM_ref_sig8:
21981 fprintf_unfiltered (f, "signature: %s",
21982 hex_string (DW_SIGNATURE (&die->attrs[i])));
21983 break;
21984 case DW_FORM_string:
21985 case DW_FORM_strp:
21986 case DW_FORM_line_strp:
21987 case DW_FORM_strx:
21988 case DW_FORM_GNU_str_index:
21989 case DW_FORM_GNU_strp_alt:
21990 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21991 DW_STRING (&die->attrs[i])
21992 ? DW_STRING (&die->attrs[i]) : "",
21993 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21994 break;
21995 case DW_FORM_flag:
21996 if (DW_UNSND (&die->attrs[i]))
21997 fprintf_unfiltered (f, "flag: TRUE");
21998 else
21999 fprintf_unfiltered (f, "flag: FALSE");
22000 break;
22001 case DW_FORM_flag_present:
22002 fprintf_unfiltered (f, "flag: TRUE");
22003 break;
22004 case DW_FORM_indirect:
22005 /* The reader will have reduced the indirect form to
22006 the "base form" so this form should not occur. */
22007 fprintf_unfiltered (f,
22008 "unexpected attribute form: DW_FORM_indirect");
22009 break;
22010 case DW_FORM_implicit_const:
22011 fprintf_unfiltered (f, "constant: %s",
22012 plongest (DW_SND (&die->attrs[i])));
22013 break;
22014 default:
22015 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22016 die->attrs[i].form);
22017 break;
22018 }
22019 fprintf_unfiltered (f, "\n");
22020 }
22021 }
22022
22023 static void
22024 dump_die_for_error (struct die_info *die)
22025 {
22026 dump_die_shallow (gdb_stderr, 0, die);
22027 }
22028
22029 static void
22030 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22031 {
22032 int indent = level * 4;
22033
22034 gdb_assert (die != NULL);
22035
22036 if (level >= max_level)
22037 return;
22038
22039 dump_die_shallow (f, indent, die);
22040
22041 if (die->child != NULL)
22042 {
22043 print_spaces (indent, f);
22044 fprintf_unfiltered (f, " Children:");
22045 if (level + 1 < max_level)
22046 {
22047 fprintf_unfiltered (f, "\n");
22048 dump_die_1 (f, level + 1, max_level, die->child);
22049 }
22050 else
22051 {
22052 fprintf_unfiltered (f,
22053 " [not printed, max nesting level reached]\n");
22054 }
22055 }
22056
22057 if (die->sibling != NULL && level > 0)
22058 {
22059 dump_die_1 (f, level, max_level, die->sibling);
22060 }
22061 }
22062
22063 /* This is called from the pdie macro in gdbinit.in.
22064 It's not static so gcc will keep a copy callable from gdb. */
22065
22066 void
22067 dump_die (struct die_info *die, int max_level)
22068 {
22069 dump_die_1 (gdb_stdlog, 0, max_level, die);
22070 }
22071
22072 static void
22073 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22074 {
22075 void **slot;
22076
22077 slot = htab_find_slot_with_hash (cu->die_hash, die,
22078 to_underlying (die->sect_off),
22079 INSERT);
22080
22081 *slot = die;
22082 }
22083
22084 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22085 required kind. */
22086
22087 static sect_offset
22088 dwarf2_get_ref_die_offset (const struct attribute *attr)
22089 {
22090 if (attr->form_is_ref ())
22091 return (sect_offset) DW_UNSND (attr);
22092
22093 complaint (_("unsupported die ref attribute form: '%s'"),
22094 dwarf_form_name (attr->form));
22095 return {};
22096 }
22097
22098 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22099 * the value held by the attribute is not constant. */
22100
22101 static LONGEST
22102 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22103 {
22104 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22105 return DW_SND (attr);
22106 else if (attr->form == DW_FORM_udata
22107 || attr->form == DW_FORM_data1
22108 || attr->form == DW_FORM_data2
22109 || attr->form == DW_FORM_data4
22110 || attr->form == DW_FORM_data8)
22111 return DW_UNSND (attr);
22112 else
22113 {
22114 /* For DW_FORM_data16 see attribute::form_is_constant. */
22115 complaint (_("Attribute value is not a constant (%s)"),
22116 dwarf_form_name (attr->form));
22117 return default_value;
22118 }
22119 }
22120
22121 /* Follow reference or signature attribute ATTR of SRC_DIE.
22122 On entry *REF_CU is the CU of SRC_DIE.
22123 On exit *REF_CU is the CU of the result. */
22124
22125 static struct die_info *
22126 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22127 struct dwarf2_cu **ref_cu)
22128 {
22129 struct die_info *die;
22130
22131 if (attr->form_is_ref ())
22132 die = follow_die_ref (src_die, attr, ref_cu);
22133 else if (attr->form == DW_FORM_ref_sig8)
22134 die = follow_die_sig (src_die, attr, ref_cu);
22135 else
22136 {
22137 dump_die_for_error (src_die);
22138 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22139 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22140 }
22141
22142 return die;
22143 }
22144
22145 /* Follow reference OFFSET.
22146 On entry *REF_CU is the CU of the source die referencing OFFSET.
22147 On exit *REF_CU is the CU of the result.
22148 Returns NULL if OFFSET is invalid. */
22149
22150 static struct die_info *
22151 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22152 struct dwarf2_cu **ref_cu)
22153 {
22154 struct die_info temp_die;
22155 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22156 struct dwarf2_per_objfile *dwarf2_per_objfile
22157 = cu->per_cu->dwarf2_per_objfile;
22158
22159 gdb_assert (cu->per_cu != NULL);
22160
22161 target_cu = cu;
22162
22163 if (cu->per_cu->is_debug_types)
22164 {
22165 /* .debug_types CUs cannot reference anything outside their CU.
22166 If they need to, they have to reference a signatured type via
22167 DW_FORM_ref_sig8. */
22168 if (!cu->header.offset_in_cu_p (sect_off))
22169 return NULL;
22170 }
22171 else if (offset_in_dwz != cu->per_cu->is_dwz
22172 || !cu->header.offset_in_cu_p (sect_off))
22173 {
22174 struct dwarf2_per_cu_data *per_cu;
22175
22176 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22177 dwarf2_per_objfile);
22178
22179 /* If necessary, add it to the queue and load its DIEs. */
22180 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22181 load_full_comp_unit (per_cu, false, cu->language);
22182
22183 target_cu = per_cu->cu;
22184 }
22185 else if (cu->dies == NULL)
22186 {
22187 /* We're loading full DIEs during partial symbol reading. */
22188 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22189 load_full_comp_unit (cu->per_cu, false, language_minimal);
22190 }
22191
22192 *ref_cu = target_cu;
22193 temp_die.sect_off = sect_off;
22194
22195 if (target_cu != cu)
22196 target_cu->ancestor = cu;
22197
22198 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22199 &temp_die,
22200 to_underlying (sect_off));
22201 }
22202
22203 /* Follow reference attribute ATTR of SRC_DIE.
22204 On entry *REF_CU is the CU of SRC_DIE.
22205 On exit *REF_CU is the CU of the result. */
22206
22207 static struct die_info *
22208 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22209 struct dwarf2_cu **ref_cu)
22210 {
22211 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22212 struct dwarf2_cu *cu = *ref_cu;
22213 struct die_info *die;
22214
22215 die = follow_die_offset (sect_off,
22216 (attr->form == DW_FORM_GNU_ref_alt
22217 || cu->per_cu->is_dwz),
22218 ref_cu);
22219 if (!die)
22220 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22221 "at %s [in module %s]"),
22222 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22223 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22224
22225 return die;
22226 }
22227
22228 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22229 Returned value is intended for DW_OP_call*. Returned
22230 dwarf2_locexpr_baton->data has lifetime of
22231 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22232
22233 struct dwarf2_locexpr_baton
22234 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22235 struct dwarf2_per_cu_data *per_cu,
22236 CORE_ADDR (*get_frame_pc) (void *baton),
22237 void *baton, bool resolve_abstract_p)
22238 {
22239 struct dwarf2_cu *cu;
22240 struct die_info *die;
22241 struct attribute *attr;
22242 struct dwarf2_locexpr_baton retval;
22243 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22244 struct objfile *objfile = dwarf2_per_objfile->objfile;
22245
22246 if (per_cu->cu == NULL)
22247 load_cu (per_cu, false);
22248 cu = per_cu->cu;
22249 if (cu == NULL)
22250 {
22251 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22252 Instead just throw an error, not much else we can do. */
22253 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22254 sect_offset_str (sect_off), objfile_name (objfile));
22255 }
22256
22257 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22258 if (!die)
22259 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22260 sect_offset_str (sect_off), objfile_name (objfile));
22261
22262 attr = dwarf2_attr (die, DW_AT_location, cu);
22263 if (!attr && resolve_abstract_p
22264 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22265 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22266 {
22267 CORE_ADDR pc = (*get_frame_pc) (baton);
22268 CORE_ADDR baseaddr = objfile->text_section_offset ();
22269 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22270
22271 for (const auto &cand_off
22272 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22273 {
22274 struct dwarf2_cu *cand_cu = cu;
22275 struct die_info *cand
22276 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22277 if (!cand
22278 || !cand->parent
22279 || cand->parent->tag != DW_TAG_subprogram)
22280 continue;
22281
22282 CORE_ADDR pc_low, pc_high;
22283 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22284 if (pc_low == ((CORE_ADDR) -1))
22285 continue;
22286 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22287 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22288 if (!(pc_low <= pc && pc < pc_high))
22289 continue;
22290
22291 die = cand;
22292 attr = dwarf2_attr (die, DW_AT_location, cu);
22293 break;
22294 }
22295 }
22296
22297 if (!attr)
22298 {
22299 /* DWARF: "If there is no such attribute, then there is no effect.".
22300 DATA is ignored if SIZE is 0. */
22301
22302 retval.data = NULL;
22303 retval.size = 0;
22304 }
22305 else if (attr->form_is_section_offset ())
22306 {
22307 struct dwarf2_loclist_baton loclist_baton;
22308 CORE_ADDR pc = (*get_frame_pc) (baton);
22309 size_t size;
22310
22311 fill_in_loclist_baton (cu, &loclist_baton, attr);
22312
22313 retval.data = dwarf2_find_location_expression (&loclist_baton,
22314 &size, pc);
22315 retval.size = size;
22316 }
22317 else
22318 {
22319 if (!attr->form_is_block ())
22320 error (_("Dwarf Error: DIE at %s referenced in module %s "
22321 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22322 sect_offset_str (sect_off), objfile_name (objfile));
22323
22324 retval.data = DW_BLOCK (attr)->data;
22325 retval.size = DW_BLOCK (attr)->size;
22326 }
22327 retval.per_cu = cu->per_cu;
22328
22329 age_cached_comp_units (dwarf2_per_objfile);
22330
22331 return retval;
22332 }
22333
22334 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22335 offset. */
22336
22337 struct dwarf2_locexpr_baton
22338 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22339 struct dwarf2_per_cu_data *per_cu,
22340 CORE_ADDR (*get_frame_pc) (void *baton),
22341 void *baton)
22342 {
22343 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22344
22345 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22346 }
22347
22348 /* Write a constant of a given type as target-ordered bytes into
22349 OBSTACK. */
22350
22351 static const gdb_byte *
22352 write_constant_as_bytes (struct obstack *obstack,
22353 enum bfd_endian byte_order,
22354 struct type *type,
22355 ULONGEST value,
22356 LONGEST *len)
22357 {
22358 gdb_byte *result;
22359
22360 *len = TYPE_LENGTH (type);
22361 result = (gdb_byte *) obstack_alloc (obstack, *len);
22362 store_unsigned_integer (result, *len, byte_order, value);
22363
22364 return result;
22365 }
22366
22367 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22368 pointer to the constant bytes and set LEN to the length of the
22369 data. If memory is needed, allocate it on OBSTACK. If the DIE
22370 does not have a DW_AT_const_value, return NULL. */
22371
22372 const gdb_byte *
22373 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22374 struct dwarf2_per_cu_data *per_cu,
22375 struct obstack *obstack,
22376 LONGEST *len)
22377 {
22378 struct dwarf2_cu *cu;
22379 struct die_info *die;
22380 struct attribute *attr;
22381 const gdb_byte *result = NULL;
22382 struct type *type;
22383 LONGEST value;
22384 enum bfd_endian byte_order;
22385 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22386
22387 if (per_cu->cu == NULL)
22388 load_cu (per_cu, false);
22389 cu = per_cu->cu;
22390 if (cu == NULL)
22391 {
22392 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22393 Instead just throw an error, not much else we can do. */
22394 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22395 sect_offset_str (sect_off), objfile_name (objfile));
22396 }
22397
22398 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22399 if (!die)
22400 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22401 sect_offset_str (sect_off), objfile_name (objfile));
22402
22403 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22404 if (attr == NULL)
22405 return NULL;
22406
22407 byte_order = (bfd_big_endian (objfile->obfd)
22408 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22409
22410 switch (attr->form)
22411 {
22412 case DW_FORM_addr:
22413 case DW_FORM_addrx:
22414 case DW_FORM_GNU_addr_index:
22415 {
22416 gdb_byte *tem;
22417
22418 *len = cu->header.addr_size;
22419 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22420 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22421 result = tem;
22422 }
22423 break;
22424 case DW_FORM_string:
22425 case DW_FORM_strp:
22426 case DW_FORM_strx:
22427 case DW_FORM_GNU_str_index:
22428 case DW_FORM_GNU_strp_alt:
22429 /* DW_STRING is already allocated on the objfile obstack, point
22430 directly to it. */
22431 result = (const gdb_byte *) DW_STRING (attr);
22432 *len = strlen (DW_STRING (attr));
22433 break;
22434 case DW_FORM_block1:
22435 case DW_FORM_block2:
22436 case DW_FORM_block4:
22437 case DW_FORM_block:
22438 case DW_FORM_exprloc:
22439 case DW_FORM_data16:
22440 result = DW_BLOCK (attr)->data;
22441 *len = DW_BLOCK (attr)->size;
22442 break;
22443
22444 /* The DW_AT_const_value attributes are supposed to carry the
22445 symbol's value "represented as it would be on the target
22446 architecture." By the time we get here, it's already been
22447 converted to host endianness, so we just need to sign- or
22448 zero-extend it as appropriate. */
22449 case DW_FORM_data1:
22450 type = die_type (die, cu);
22451 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22452 if (result == NULL)
22453 result = write_constant_as_bytes (obstack, byte_order,
22454 type, value, len);
22455 break;
22456 case DW_FORM_data2:
22457 type = die_type (die, cu);
22458 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22459 if (result == NULL)
22460 result = write_constant_as_bytes (obstack, byte_order,
22461 type, value, len);
22462 break;
22463 case DW_FORM_data4:
22464 type = die_type (die, cu);
22465 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22466 if (result == NULL)
22467 result = write_constant_as_bytes (obstack, byte_order,
22468 type, value, len);
22469 break;
22470 case DW_FORM_data8:
22471 type = die_type (die, cu);
22472 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22473 if (result == NULL)
22474 result = write_constant_as_bytes (obstack, byte_order,
22475 type, value, len);
22476 break;
22477
22478 case DW_FORM_sdata:
22479 case DW_FORM_implicit_const:
22480 type = die_type (die, cu);
22481 result = write_constant_as_bytes (obstack, byte_order,
22482 type, DW_SND (attr), len);
22483 break;
22484
22485 case DW_FORM_udata:
22486 type = die_type (die, cu);
22487 result = write_constant_as_bytes (obstack, byte_order,
22488 type, DW_UNSND (attr), len);
22489 break;
22490
22491 default:
22492 complaint (_("unsupported const value attribute form: '%s'"),
22493 dwarf_form_name (attr->form));
22494 break;
22495 }
22496
22497 return result;
22498 }
22499
22500 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22501 valid type for this die is found. */
22502
22503 struct type *
22504 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22505 struct dwarf2_per_cu_data *per_cu)
22506 {
22507 struct dwarf2_cu *cu;
22508 struct die_info *die;
22509
22510 if (per_cu->cu == NULL)
22511 load_cu (per_cu, false);
22512 cu = per_cu->cu;
22513 if (!cu)
22514 return NULL;
22515
22516 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22517 if (!die)
22518 return NULL;
22519
22520 return die_type (die, cu);
22521 }
22522
22523 /* Return the type of the DIE at DIE_OFFSET in the CU named by
22524 PER_CU. */
22525
22526 struct type *
22527 dwarf2_get_die_type (cu_offset die_offset,
22528 struct dwarf2_per_cu_data *per_cu)
22529 {
22530 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22531 return get_die_type_at_offset (die_offset_sect, per_cu);
22532 }
22533
22534 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22535 On entry *REF_CU is the CU of SRC_DIE.
22536 On exit *REF_CU is the CU of the result.
22537 Returns NULL if the referenced DIE isn't found. */
22538
22539 static struct die_info *
22540 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22541 struct dwarf2_cu **ref_cu)
22542 {
22543 struct die_info temp_die;
22544 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22545 struct die_info *die;
22546
22547 /* While it might be nice to assert sig_type->type == NULL here,
22548 we can get here for DW_AT_imported_declaration where we need
22549 the DIE not the type. */
22550
22551 /* If necessary, add it to the queue and load its DIEs. */
22552
22553 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22554 read_signatured_type (sig_type);
22555
22556 sig_cu = sig_type->per_cu.cu;
22557 gdb_assert (sig_cu != NULL);
22558 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22559 temp_die.sect_off = sig_type->type_offset_in_section;
22560 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22561 to_underlying (temp_die.sect_off));
22562 if (die)
22563 {
22564 struct dwarf2_per_objfile *dwarf2_per_objfile
22565 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22566
22567 /* For .gdb_index version 7 keep track of included TUs.
22568 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22569 if (dwarf2_per_objfile->index_table != NULL
22570 && dwarf2_per_objfile->index_table->version <= 7)
22571 {
22572 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22573 }
22574
22575 *ref_cu = sig_cu;
22576 if (sig_cu != cu)
22577 sig_cu->ancestor = cu;
22578
22579 return die;
22580 }
22581
22582 return NULL;
22583 }
22584
22585 /* Follow signatured type referenced by ATTR in SRC_DIE.
22586 On entry *REF_CU is the CU of SRC_DIE.
22587 On exit *REF_CU is the CU of the result.
22588 The result is the DIE of the type.
22589 If the referenced type cannot be found an error is thrown. */
22590
22591 static struct die_info *
22592 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22593 struct dwarf2_cu **ref_cu)
22594 {
22595 ULONGEST signature = DW_SIGNATURE (attr);
22596 struct signatured_type *sig_type;
22597 struct die_info *die;
22598
22599 gdb_assert (attr->form == DW_FORM_ref_sig8);
22600
22601 sig_type = lookup_signatured_type (*ref_cu, signature);
22602 /* sig_type will be NULL if the signatured type is missing from
22603 the debug info. */
22604 if (sig_type == NULL)
22605 {
22606 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22607 " from DIE at %s [in module %s]"),
22608 hex_string (signature), sect_offset_str (src_die->sect_off),
22609 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22610 }
22611
22612 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22613 if (die == NULL)
22614 {
22615 dump_die_for_error (src_die);
22616 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22617 " from DIE at %s [in module %s]"),
22618 hex_string (signature), sect_offset_str (src_die->sect_off),
22619 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22620 }
22621
22622 return die;
22623 }
22624
22625 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22626 reading in and processing the type unit if necessary. */
22627
22628 static struct type *
22629 get_signatured_type (struct die_info *die, ULONGEST signature,
22630 struct dwarf2_cu *cu)
22631 {
22632 struct dwarf2_per_objfile *dwarf2_per_objfile
22633 = cu->per_cu->dwarf2_per_objfile;
22634 struct signatured_type *sig_type;
22635 struct dwarf2_cu *type_cu;
22636 struct die_info *type_die;
22637 struct type *type;
22638
22639 sig_type = lookup_signatured_type (cu, signature);
22640 /* sig_type will be NULL if the signatured type is missing from
22641 the debug info. */
22642 if (sig_type == NULL)
22643 {
22644 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22645 " from DIE at %s [in module %s]"),
22646 hex_string (signature), sect_offset_str (die->sect_off),
22647 objfile_name (dwarf2_per_objfile->objfile));
22648 return build_error_marker_type (cu, die);
22649 }
22650
22651 /* If we already know the type we're done. */
22652 if (sig_type->type != NULL)
22653 return sig_type->type;
22654
22655 type_cu = cu;
22656 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22657 if (type_die != NULL)
22658 {
22659 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22660 is created. This is important, for example, because for c++ classes
22661 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22662 type = read_type_die (type_die, type_cu);
22663 if (type == NULL)
22664 {
22665 complaint (_("Dwarf Error: Cannot build signatured type %s"
22666 " referenced from DIE at %s [in module %s]"),
22667 hex_string (signature), sect_offset_str (die->sect_off),
22668 objfile_name (dwarf2_per_objfile->objfile));
22669 type = build_error_marker_type (cu, die);
22670 }
22671 }
22672 else
22673 {
22674 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22675 " from DIE at %s [in module %s]"),
22676 hex_string (signature), sect_offset_str (die->sect_off),
22677 objfile_name (dwarf2_per_objfile->objfile));
22678 type = build_error_marker_type (cu, die);
22679 }
22680 sig_type->type = type;
22681
22682 return type;
22683 }
22684
22685 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22686 reading in and processing the type unit if necessary. */
22687
22688 static struct type *
22689 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22690 struct dwarf2_cu *cu) /* ARI: editCase function */
22691 {
22692 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22693 if (attr->form_is_ref ())
22694 {
22695 struct dwarf2_cu *type_cu = cu;
22696 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22697
22698 return read_type_die (type_die, type_cu);
22699 }
22700 else if (attr->form == DW_FORM_ref_sig8)
22701 {
22702 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22703 }
22704 else
22705 {
22706 struct dwarf2_per_objfile *dwarf2_per_objfile
22707 = cu->per_cu->dwarf2_per_objfile;
22708
22709 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22710 " at %s [in module %s]"),
22711 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22712 objfile_name (dwarf2_per_objfile->objfile));
22713 return build_error_marker_type (cu, die);
22714 }
22715 }
22716
22717 /* Load the DIEs associated with type unit PER_CU into memory. */
22718
22719 static void
22720 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22721 {
22722 struct signatured_type *sig_type;
22723
22724 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22725 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22726
22727 /* We have the per_cu, but we need the signatured_type.
22728 Fortunately this is an easy translation. */
22729 gdb_assert (per_cu->is_debug_types);
22730 sig_type = (struct signatured_type *) per_cu;
22731
22732 gdb_assert (per_cu->cu == NULL);
22733
22734 read_signatured_type (sig_type);
22735
22736 gdb_assert (per_cu->cu != NULL);
22737 }
22738
22739 /* Read in a signatured type and build its CU and DIEs.
22740 If the type is a stub for the real type in a DWO file,
22741 read in the real type from the DWO file as well. */
22742
22743 static void
22744 read_signatured_type (struct signatured_type *sig_type)
22745 {
22746 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22747
22748 gdb_assert (per_cu->is_debug_types);
22749 gdb_assert (per_cu->cu == NULL);
22750
22751 cutu_reader reader (per_cu, NULL, 0, 1, false);
22752
22753 if (!reader.dummy_p)
22754 {
22755 struct dwarf2_cu *cu = reader.cu;
22756 const gdb_byte *info_ptr = reader.info_ptr;
22757
22758 gdb_assert (cu->die_hash == NULL);
22759 cu->die_hash =
22760 htab_create_alloc_ex (cu->header.length / 12,
22761 die_hash,
22762 die_eq,
22763 NULL,
22764 &cu->comp_unit_obstack,
22765 hashtab_obstack_allocate,
22766 dummy_obstack_deallocate);
22767
22768 if (reader.comp_unit_die->has_children)
22769 reader.comp_unit_die->child
22770 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22771 reader.comp_unit_die);
22772 cu->dies = reader.comp_unit_die;
22773 /* comp_unit_die is not stored in die_hash, no need. */
22774
22775 /* We try not to read any attributes in this function, because
22776 not all CUs needed for references have been loaded yet, and
22777 symbol table processing isn't initialized. But we have to
22778 set the CU language, or we won't be able to build types
22779 correctly. Similarly, if we do not read the producer, we can
22780 not apply producer-specific interpretation. */
22781 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22782 }
22783
22784 sig_type->per_cu.tu_read = 1;
22785 }
22786
22787 /* Decode simple location descriptions.
22788 Given a pointer to a dwarf block that defines a location, compute
22789 the location and return the value.
22790
22791 NOTE drow/2003-11-18: This function is called in two situations
22792 now: for the address of static or global variables (partial symbols
22793 only) and for offsets into structures which are expected to be
22794 (more or less) constant. The partial symbol case should go away,
22795 and only the constant case should remain. That will let this
22796 function complain more accurately. A few special modes are allowed
22797 without complaint for global variables (for instance, global
22798 register values and thread-local values).
22799
22800 A location description containing no operations indicates that the
22801 object is optimized out. The return value is 0 for that case.
22802 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22803 callers will only want a very basic result and this can become a
22804 complaint.
22805
22806 Note that stack[0] is unused except as a default error return. */
22807
22808 static CORE_ADDR
22809 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22810 {
22811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22812 size_t i;
22813 size_t size = blk->size;
22814 const gdb_byte *data = blk->data;
22815 CORE_ADDR stack[64];
22816 int stacki;
22817 unsigned int bytes_read, unsnd;
22818 gdb_byte op;
22819
22820 i = 0;
22821 stacki = 0;
22822 stack[stacki] = 0;
22823 stack[++stacki] = 0;
22824
22825 while (i < size)
22826 {
22827 op = data[i++];
22828 switch (op)
22829 {
22830 case DW_OP_lit0:
22831 case DW_OP_lit1:
22832 case DW_OP_lit2:
22833 case DW_OP_lit3:
22834 case DW_OP_lit4:
22835 case DW_OP_lit5:
22836 case DW_OP_lit6:
22837 case DW_OP_lit7:
22838 case DW_OP_lit8:
22839 case DW_OP_lit9:
22840 case DW_OP_lit10:
22841 case DW_OP_lit11:
22842 case DW_OP_lit12:
22843 case DW_OP_lit13:
22844 case DW_OP_lit14:
22845 case DW_OP_lit15:
22846 case DW_OP_lit16:
22847 case DW_OP_lit17:
22848 case DW_OP_lit18:
22849 case DW_OP_lit19:
22850 case DW_OP_lit20:
22851 case DW_OP_lit21:
22852 case DW_OP_lit22:
22853 case DW_OP_lit23:
22854 case DW_OP_lit24:
22855 case DW_OP_lit25:
22856 case DW_OP_lit26:
22857 case DW_OP_lit27:
22858 case DW_OP_lit28:
22859 case DW_OP_lit29:
22860 case DW_OP_lit30:
22861 case DW_OP_lit31:
22862 stack[++stacki] = op - DW_OP_lit0;
22863 break;
22864
22865 case DW_OP_reg0:
22866 case DW_OP_reg1:
22867 case DW_OP_reg2:
22868 case DW_OP_reg3:
22869 case DW_OP_reg4:
22870 case DW_OP_reg5:
22871 case DW_OP_reg6:
22872 case DW_OP_reg7:
22873 case DW_OP_reg8:
22874 case DW_OP_reg9:
22875 case DW_OP_reg10:
22876 case DW_OP_reg11:
22877 case DW_OP_reg12:
22878 case DW_OP_reg13:
22879 case DW_OP_reg14:
22880 case DW_OP_reg15:
22881 case DW_OP_reg16:
22882 case DW_OP_reg17:
22883 case DW_OP_reg18:
22884 case DW_OP_reg19:
22885 case DW_OP_reg20:
22886 case DW_OP_reg21:
22887 case DW_OP_reg22:
22888 case DW_OP_reg23:
22889 case DW_OP_reg24:
22890 case DW_OP_reg25:
22891 case DW_OP_reg26:
22892 case DW_OP_reg27:
22893 case DW_OP_reg28:
22894 case DW_OP_reg29:
22895 case DW_OP_reg30:
22896 case DW_OP_reg31:
22897 stack[++stacki] = op - DW_OP_reg0;
22898 if (i < size)
22899 dwarf2_complex_location_expr_complaint ();
22900 break;
22901
22902 case DW_OP_regx:
22903 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22904 i += bytes_read;
22905 stack[++stacki] = unsnd;
22906 if (i < size)
22907 dwarf2_complex_location_expr_complaint ();
22908 break;
22909
22910 case DW_OP_addr:
22911 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22912 &bytes_read);
22913 i += bytes_read;
22914 break;
22915
22916 case DW_OP_const1u:
22917 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22918 i += 1;
22919 break;
22920
22921 case DW_OP_const1s:
22922 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22923 i += 1;
22924 break;
22925
22926 case DW_OP_const2u:
22927 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22928 i += 2;
22929 break;
22930
22931 case DW_OP_const2s:
22932 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22933 i += 2;
22934 break;
22935
22936 case DW_OP_const4u:
22937 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22938 i += 4;
22939 break;
22940
22941 case DW_OP_const4s:
22942 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22943 i += 4;
22944 break;
22945
22946 case DW_OP_const8u:
22947 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22948 i += 8;
22949 break;
22950
22951 case DW_OP_constu:
22952 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22953 &bytes_read);
22954 i += bytes_read;
22955 break;
22956
22957 case DW_OP_consts:
22958 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22959 i += bytes_read;
22960 break;
22961
22962 case DW_OP_dup:
22963 stack[stacki + 1] = stack[stacki];
22964 stacki++;
22965 break;
22966
22967 case DW_OP_plus:
22968 stack[stacki - 1] += stack[stacki];
22969 stacki--;
22970 break;
22971
22972 case DW_OP_plus_uconst:
22973 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22974 &bytes_read);
22975 i += bytes_read;
22976 break;
22977
22978 case DW_OP_minus:
22979 stack[stacki - 1] -= stack[stacki];
22980 stacki--;
22981 break;
22982
22983 case DW_OP_deref:
22984 /* If we're not the last op, then we definitely can't encode
22985 this using GDB's address_class enum. This is valid for partial
22986 global symbols, although the variable's address will be bogus
22987 in the psymtab. */
22988 if (i < size)
22989 dwarf2_complex_location_expr_complaint ();
22990 break;
22991
22992 case DW_OP_GNU_push_tls_address:
22993 case DW_OP_form_tls_address:
22994 /* The top of the stack has the offset from the beginning
22995 of the thread control block at which the variable is located. */
22996 /* Nothing should follow this operator, so the top of stack would
22997 be returned. */
22998 /* This is valid for partial global symbols, but the variable's
22999 address will be bogus in the psymtab. Make it always at least
23000 non-zero to not look as a variable garbage collected by linker
23001 which have DW_OP_addr 0. */
23002 if (i < size)
23003 dwarf2_complex_location_expr_complaint ();
23004 stack[stacki]++;
23005 break;
23006
23007 case DW_OP_GNU_uninit:
23008 break;
23009
23010 case DW_OP_addrx:
23011 case DW_OP_GNU_addr_index:
23012 case DW_OP_GNU_const_index:
23013 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23014 &bytes_read);
23015 i += bytes_read;
23016 break;
23017
23018 default:
23019 {
23020 const char *name = get_DW_OP_name (op);
23021
23022 if (name)
23023 complaint (_("unsupported stack op: '%s'"),
23024 name);
23025 else
23026 complaint (_("unsupported stack op: '%02x'"),
23027 op);
23028 }
23029
23030 return (stack[stacki]);
23031 }
23032
23033 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23034 outside of the allocated space. Also enforce minimum>0. */
23035 if (stacki >= ARRAY_SIZE (stack) - 1)
23036 {
23037 complaint (_("location description stack overflow"));
23038 return 0;
23039 }
23040
23041 if (stacki <= 0)
23042 {
23043 complaint (_("location description stack underflow"));
23044 return 0;
23045 }
23046 }
23047 return (stack[stacki]);
23048 }
23049
23050 /* memory allocation interface */
23051
23052 static struct dwarf_block *
23053 dwarf_alloc_block (struct dwarf2_cu *cu)
23054 {
23055 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23056 }
23057
23058 static struct die_info *
23059 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23060 {
23061 struct die_info *die;
23062 size_t size = sizeof (struct die_info);
23063
23064 if (num_attrs > 1)
23065 size += (num_attrs - 1) * sizeof (struct attribute);
23066
23067 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23068 memset (die, 0, sizeof (struct die_info));
23069 return (die);
23070 }
23071
23072 \f
23073 /* Macro support. */
23074
23075 static struct macro_source_file *
23076 macro_start_file (struct dwarf2_cu *cu,
23077 int file, int line,
23078 struct macro_source_file *current_file,
23079 struct line_header *lh)
23080 {
23081 /* File name relative to the compilation directory of this source file. */
23082 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23083
23084 if (! current_file)
23085 {
23086 /* Note: We don't create a macro table for this compilation unit
23087 at all until we actually get a filename. */
23088 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23089
23090 /* If we have no current file, then this must be the start_file
23091 directive for the compilation unit's main source file. */
23092 current_file = macro_set_main (macro_table, file_name.get ());
23093 macro_define_special (macro_table);
23094 }
23095 else
23096 current_file = macro_include (current_file, line, file_name.get ());
23097
23098 return current_file;
23099 }
23100
23101 static const char *
23102 consume_improper_spaces (const char *p, const char *body)
23103 {
23104 if (*p == ' ')
23105 {
23106 complaint (_("macro definition contains spaces "
23107 "in formal argument list:\n`%s'"),
23108 body);
23109
23110 while (*p == ' ')
23111 p++;
23112 }
23113
23114 return p;
23115 }
23116
23117
23118 static void
23119 parse_macro_definition (struct macro_source_file *file, int line,
23120 const char *body)
23121 {
23122 const char *p;
23123
23124 /* The body string takes one of two forms. For object-like macro
23125 definitions, it should be:
23126
23127 <macro name> " " <definition>
23128
23129 For function-like macro definitions, it should be:
23130
23131 <macro name> "() " <definition>
23132 or
23133 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23134
23135 Spaces may appear only where explicitly indicated, and in the
23136 <definition>.
23137
23138 The Dwarf 2 spec says that an object-like macro's name is always
23139 followed by a space, but versions of GCC around March 2002 omit
23140 the space when the macro's definition is the empty string.
23141
23142 The Dwarf 2 spec says that there should be no spaces between the
23143 formal arguments in a function-like macro's formal argument list,
23144 but versions of GCC around March 2002 include spaces after the
23145 commas. */
23146
23147
23148 /* Find the extent of the macro name. The macro name is terminated
23149 by either a space or null character (for an object-like macro) or
23150 an opening paren (for a function-like macro). */
23151 for (p = body; *p; p++)
23152 if (*p == ' ' || *p == '(')
23153 break;
23154
23155 if (*p == ' ' || *p == '\0')
23156 {
23157 /* It's an object-like macro. */
23158 int name_len = p - body;
23159 std::string name (body, name_len);
23160 const char *replacement;
23161
23162 if (*p == ' ')
23163 replacement = body + name_len + 1;
23164 else
23165 {
23166 dwarf2_macro_malformed_definition_complaint (body);
23167 replacement = body + name_len;
23168 }
23169
23170 macro_define_object (file, line, name.c_str (), replacement);
23171 }
23172 else if (*p == '(')
23173 {
23174 /* It's a function-like macro. */
23175 std::string name (body, p - body);
23176 int argc = 0;
23177 int argv_size = 1;
23178 char **argv = XNEWVEC (char *, argv_size);
23179
23180 p++;
23181
23182 p = consume_improper_spaces (p, body);
23183
23184 /* Parse the formal argument list. */
23185 while (*p && *p != ')')
23186 {
23187 /* Find the extent of the current argument name. */
23188 const char *arg_start = p;
23189
23190 while (*p && *p != ',' && *p != ')' && *p != ' ')
23191 p++;
23192
23193 if (! *p || p == arg_start)
23194 dwarf2_macro_malformed_definition_complaint (body);
23195 else
23196 {
23197 /* Make sure argv has room for the new argument. */
23198 if (argc >= argv_size)
23199 {
23200 argv_size *= 2;
23201 argv = XRESIZEVEC (char *, argv, argv_size);
23202 }
23203
23204 argv[argc++] = savestring (arg_start, p - arg_start);
23205 }
23206
23207 p = consume_improper_spaces (p, body);
23208
23209 /* Consume the comma, if present. */
23210 if (*p == ',')
23211 {
23212 p++;
23213
23214 p = consume_improper_spaces (p, body);
23215 }
23216 }
23217
23218 if (*p == ')')
23219 {
23220 p++;
23221
23222 if (*p == ' ')
23223 /* Perfectly formed definition, no complaints. */
23224 macro_define_function (file, line, name.c_str (),
23225 argc, (const char **) argv,
23226 p + 1);
23227 else if (*p == '\0')
23228 {
23229 /* Complain, but do define it. */
23230 dwarf2_macro_malformed_definition_complaint (body);
23231 macro_define_function (file, line, name.c_str (),
23232 argc, (const char **) argv,
23233 p);
23234 }
23235 else
23236 /* Just complain. */
23237 dwarf2_macro_malformed_definition_complaint (body);
23238 }
23239 else
23240 /* Just complain. */
23241 dwarf2_macro_malformed_definition_complaint (body);
23242
23243 {
23244 int i;
23245
23246 for (i = 0; i < argc; i++)
23247 xfree (argv[i]);
23248 }
23249 xfree (argv);
23250 }
23251 else
23252 dwarf2_macro_malformed_definition_complaint (body);
23253 }
23254
23255 /* Skip some bytes from BYTES according to the form given in FORM.
23256 Returns the new pointer. */
23257
23258 static const gdb_byte *
23259 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23260 enum dwarf_form form,
23261 unsigned int offset_size,
23262 struct dwarf2_section_info *section)
23263 {
23264 unsigned int bytes_read;
23265
23266 switch (form)
23267 {
23268 case DW_FORM_data1:
23269 case DW_FORM_flag:
23270 ++bytes;
23271 break;
23272
23273 case DW_FORM_data2:
23274 bytes += 2;
23275 break;
23276
23277 case DW_FORM_data4:
23278 bytes += 4;
23279 break;
23280
23281 case DW_FORM_data8:
23282 bytes += 8;
23283 break;
23284
23285 case DW_FORM_data16:
23286 bytes += 16;
23287 break;
23288
23289 case DW_FORM_string:
23290 read_direct_string (abfd, bytes, &bytes_read);
23291 bytes += bytes_read;
23292 break;
23293
23294 case DW_FORM_sec_offset:
23295 case DW_FORM_strp:
23296 case DW_FORM_GNU_strp_alt:
23297 bytes += offset_size;
23298 break;
23299
23300 case DW_FORM_block:
23301 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23302 bytes += bytes_read;
23303 break;
23304
23305 case DW_FORM_block1:
23306 bytes += 1 + read_1_byte (abfd, bytes);
23307 break;
23308 case DW_FORM_block2:
23309 bytes += 2 + read_2_bytes (abfd, bytes);
23310 break;
23311 case DW_FORM_block4:
23312 bytes += 4 + read_4_bytes (abfd, bytes);
23313 break;
23314
23315 case DW_FORM_addrx:
23316 case DW_FORM_sdata:
23317 case DW_FORM_strx:
23318 case DW_FORM_udata:
23319 case DW_FORM_GNU_addr_index:
23320 case DW_FORM_GNU_str_index:
23321 bytes = gdb_skip_leb128 (bytes, buffer_end);
23322 if (bytes == NULL)
23323 {
23324 dwarf2_section_buffer_overflow_complaint (section);
23325 return NULL;
23326 }
23327 break;
23328
23329 case DW_FORM_implicit_const:
23330 break;
23331
23332 default:
23333 {
23334 complaint (_("invalid form 0x%x in `%s'"),
23335 form, section->get_name ());
23336 return NULL;
23337 }
23338 }
23339
23340 return bytes;
23341 }
23342
23343 /* A helper for dwarf_decode_macros that handles skipping an unknown
23344 opcode. Returns an updated pointer to the macro data buffer; or,
23345 on error, issues a complaint and returns NULL. */
23346
23347 static const gdb_byte *
23348 skip_unknown_opcode (unsigned int opcode,
23349 const gdb_byte **opcode_definitions,
23350 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23351 bfd *abfd,
23352 unsigned int offset_size,
23353 struct dwarf2_section_info *section)
23354 {
23355 unsigned int bytes_read, i;
23356 unsigned long arg;
23357 const gdb_byte *defn;
23358
23359 if (opcode_definitions[opcode] == NULL)
23360 {
23361 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23362 opcode);
23363 return NULL;
23364 }
23365
23366 defn = opcode_definitions[opcode];
23367 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23368 defn += bytes_read;
23369
23370 for (i = 0; i < arg; ++i)
23371 {
23372 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23373 (enum dwarf_form) defn[i], offset_size,
23374 section);
23375 if (mac_ptr == NULL)
23376 {
23377 /* skip_form_bytes already issued the complaint. */
23378 return NULL;
23379 }
23380 }
23381
23382 return mac_ptr;
23383 }
23384
23385 /* A helper function which parses the header of a macro section.
23386 If the macro section is the extended (for now called "GNU") type,
23387 then this updates *OFFSET_SIZE. Returns a pointer to just after
23388 the header, or issues a complaint and returns NULL on error. */
23389
23390 static const gdb_byte *
23391 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23392 bfd *abfd,
23393 const gdb_byte *mac_ptr,
23394 unsigned int *offset_size,
23395 int section_is_gnu)
23396 {
23397 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23398
23399 if (section_is_gnu)
23400 {
23401 unsigned int version, flags;
23402
23403 version = read_2_bytes (abfd, mac_ptr);
23404 if (version != 4 && version != 5)
23405 {
23406 complaint (_("unrecognized version `%d' in .debug_macro section"),
23407 version);
23408 return NULL;
23409 }
23410 mac_ptr += 2;
23411
23412 flags = read_1_byte (abfd, mac_ptr);
23413 ++mac_ptr;
23414 *offset_size = (flags & 1) ? 8 : 4;
23415
23416 if ((flags & 2) != 0)
23417 /* We don't need the line table offset. */
23418 mac_ptr += *offset_size;
23419
23420 /* Vendor opcode descriptions. */
23421 if ((flags & 4) != 0)
23422 {
23423 unsigned int i, count;
23424
23425 count = read_1_byte (abfd, mac_ptr);
23426 ++mac_ptr;
23427 for (i = 0; i < count; ++i)
23428 {
23429 unsigned int opcode, bytes_read;
23430 unsigned long arg;
23431
23432 opcode = read_1_byte (abfd, mac_ptr);
23433 ++mac_ptr;
23434 opcode_definitions[opcode] = mac_ptr;
23435 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23436 mac_ptr += bytes_read;
23437 mac_ptr += arg;
23438 }
23439 }
23440 }
23441
23442 return mac_ptr;
23443 }
23444
23445 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23446 including DW_MACRO_import. */
23447
23448 static void
23449 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23450 bfd *abfd,
23451 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23452 struct macro_source_file *current_file,
23453 struct line_header *lh,
23454 struct dwarf2_section_info *section,
23455 int section_is_gnu, int section_is_dwz,
23456 unsigned int offset_size,
23457 htab_t include_hash)
23458 {
23459 struct dwarf2_per_objfile *dwarf2_per_objfile
23460 = cu->per_cu->dwarf2_per_objfile;
23461 struct objfile *objfile = dwarf2_per_objfile->objfile;
23462 enum dwarf_macro_record_type macinfo_type;
23463 int at_commandline;
23464 const gdb_byte *opcode_definitions[256];
23465
23466 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23467 &offset_size, section_is_gnu);
23468 if (mac_ptr == NULL)
23469 {
23470 /* We already issued a complaint. */
23471 return;
23472 }
23473
23474 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23475 GDB is still reading the definitions from command line. First
23476 DW_MACINFO_start_file will need to be ignored as it was already executed
23477 to create CURRENT_FILE for the main source holding also the command line
23478 definitions. On first met DW_MACINFO_start_file this flag is reset to
23479 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23480
23481 at_commandline = 1;
23482
23483 do
23484 {
23485 /* Do we at least have room for a macinfo type byte? */
23486 if (mac_ptr >= mac_end)
23487 {
23488 dwarf2_section_buffer_overflow_complaint (section);
23489 break;
23490 }
23491
23492 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23493 mac_ptr++;
23494
23495 /* Note that we rely on the fact that the corresponding GNU and
23496 DWARF constants are the same. */
23497 DIAGNOSTIC_PUSH
23498 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23499 switch (macinfo_type)
23500 {
23501 /* A zero macinfo type indicates the end of the macro
23502 information. */
23503 case 0:
23504 break;
23505
23506 case DW_MACRO_define:
23507 case DW_MACRO_undef:
23508 case DW_MACRO_define_strp:
23509 case DW_MACRO_undef_strp:
23510 case DW_MACRO_define_sup:
23511 case DW_MACRO_undef_sup:
23512 {
23513 unsigned int bytes_read;
23514 int line;
23515 const char *body;
23516 int is_define;
23517
23518 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23519 mac_ptr += bytes_read;
23520
23521 if (macinfo_type == DW_MACRO_define
23522 || macinfo_type == DW_MACRO_undef)
23523 {
23524 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23525 mac_ptr += bytes_read;
23526 }
23527 else
23528 {
23529 LONGEST str_offset;
23530
23531 str_offset = read_offset (abfd, mac_ptr, offset_size);
23532 mac_ptr += offset_size;
23533
23534 if (macinfo_type == DW_MACRO_define_sup
23535 || macinfo_type == DW_MACRO_undef_sup
23536 || section_is_dwz)
23537 {
23538 struct dwz_file *dwz
23539 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23540
23541 body = read_indirect_string_from_dwz (objfile,
23542 dwz, str_offset);
23543 }
23544 else
23545 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23546 abfd, str_offset);
23547 }
23548
23549 is_define = (macinfo_type == DW_MACRO_define
23550 || macinfo_type == DW_MACRO_define_strp
23551 || macinfo_type == DW_MACRO_define_sup);
23552 if (! current_file)
23553 {
23554 /* DWARF violation as no main source is present. */
23555 complaint (_("debug info with no main source gives macro %s "
23556 "on line %d: %s"),
23557 is_define ? _("definition") : _("undefinition"),
23558 line, body);
23559 break;
23560 }
23561 if ((line == 0 && !at_commandline)
23562 || (line != 0 && at_commandline))
23563 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23564 at_commandline ? _("command-line") : _("in-file"),
23565 is_define ? _("definition") : _("undefinition"),
23566 line == 0 ? _("zero") : _("non-zero"), line, body);
23567
23568 if (body == NULL)
23569 {
23570 /* Fedora's rpm-build's "debugedit" binary
23571 corrupted .debug_macro sections.
23572
23573 For more info, see
23574 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23575 complaint (_("debug info gives %s invalid macro %s "
23576 "without body (corrupted?) at line %d "
23577 "on file %s"),
23578 at_commandline ? _("command-line") : _("in-file"),
23579 is_define ? _("definition") : _("undefinition"),
23580 line, current_file->filename);
23581 }
23582 else if (is_define)
23583 parse_macro_definition (current_file, line, body);
23584 else
23585 {
23586 gdb_assert (macinfo_type == DW_MACRO_undef
23587 || macinfo_type == DW_MACRO_undef_strp
23588 || macinfo_type == DW_MACRO_undef_sup);
23589 macro_undef (current_file, line, body);
23590 }
23591 }
23592 break;
23593
23594 case DW_MACRO_start_file:
23595 {
23596 unsigned int bytes_read;
23597 int line, file;
23598
23599 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23600 mac_ptr += bytes_read;
23601 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23602 mac_ptr += bytes_read;
23603
23604 if ((line == 0 && !at_commandline)
23605 || (line != 0 && at_commandline))
23606 complaint (_("debug info gives source %d included "
23607 "from %s at %s line %d"),
23608 file, at_commandline ? _("command-line") : _("file"),
23609 line == 0 ? _("zero") : _("non-zero"), line);
23610
23611 if (at_commandline)
23612 {
23613 /* This DW_MACRO_start_file was executed in the
23614 pass one. */
23615 at_commandline = 0;
23616 }
23617 else
23618 current_file = macro_start_file (cu, file, line, current_file,
23619 lh);
23620 }
23621 break;
23622
23623 case DW_MACRO_end_file:
23624 if (! current_file)
23625 complaint (_("macro debug info has an unmatched "
23626 "`close_file' directive"));
23627 else
23628 {
23629 current_file = current_file->included_by;
23630 if (! current_file)
23631 {
23632 enum dwarf_macro_record_type next_type;
23633
23634 /* GCC circa March 2002 doesn't produce the zero
23635 type byte marking the end of the compilation
23636 unit. Complain if it's not there, but exit no
23637 matter what. */
23638
23639 /* Do we at least have room for a macinfo type byte? */
23640 if (mac_ptr >= mac_end)
23641 {
23642 dwarf2_section_buffer_overflow_complaint (section);
23643 return;
23644 }
23645
23646 /* We don't increment mac_ptr here, so this is just
23647 a look-ahead. */
23648 next_type
23649 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23650 mac_ptr);
23651 if (next_type != 0)
23652 complaint (_("no terminating 0-type entry for "
23653 "macros in `.debug_macinfo' section"));
23654
23655 return;
23656 }
23657 }
23658 break;
23659
23660 case DW_MACRO_import:
23661 case DW_MACRO_import_sup:
23662 {
23663 LONGEST offset;
23664 void **slot;
23665 bfd *include_bfd = abfd;
23666 struct dwarf2_section_info *include_section = section;
23667 const gdb_byte *include_mac_end = mac_end;
23668 int is_dwz = section_is_dwz;
23669 const gdb_byte *new_mac_ptr;
23670
23671 offset = read_offset (abfd, mac_ptr, offset_size);
23672 mac_ptr += offset_size;
23673
23674 if (macinfo_type == DW_MACRO_import_sup)
23675 {
23676 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23677
23678 dwz->macro.read (objfile);
23679
23680 include_section = &dwz->macro;
23681 include_bfd = include_section->get_bfd_owner ();
23682 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23683 is_dwz = 1;
23684 }
23685
23686 new_mac_ptr = include_section->buffer + offset;
23687 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23688
23689 if (*slot != NULL)
23690 {
23691 /* This has actually happened; see
23692 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23693 complaint (_("recursive DW_MACRO_import in "
23694 ".debug_macro section"));
23695 }
23696 else
23697 {
23698 *slot = (void *) new_mac_ptr;
23699
23700 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23701 include_mac_end, current_file, lh,
23702 section, section_is_gnu, is_dwz,
23703 offset_size, include_hash);
23704
23705 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23706 }
23707 }
23708 break;
23709
23710 case DW_MACINFO_vendor_ext:
23711 if (!section_is_gnu)
23712 {
23713 unsigned int bytes_read;
23714
23715 /* This reads the constant, but since we don't recognize
23716 any vendor extensions, we ignore it. */
23717 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23718 mac_ptr += bytes_read;
23719 read_direct_string (abfd, mac_ptr, &bytes_read);
23720 mac_ptr += bytes_read;
23721
23722 /* We don't recognize any vendor extensions. */
23723 break;
23724 }
23725 /* FALLTHROUGH */
23726
23727 default:
23728 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23729 mac_ptr, mac_end, abfd, offset_size,
23730 section);
23731 if (mac_ptr == NULL)
23732 return;
23733 break;
23734 }
23735 DIAGNOSTIC_POP
23736 } while (macinfo_type != 0);
23737 }
23738
23739 static void
23740 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23741 int section_is_gnu)
23742 {
23743 struct dwarf2_per_objfile *dwarf2_per_objfile
23744 = cu->per_cu->dwarf2_per_objfile;
23745 struct objfile *objfile = dwarf2_per_objfile->objfile;
23746 struct line_header *lh = cu->line_header;
23747 bfd *abfd;
23748 const gdb_byte *mac_ptr, *mac_end;
23749 struct macro_source_file *current_file = 0;
23750 enum dwarf_macro_record_type macinfo_type;
23751 unsigned int offset_size = cu->header.offset_size;
23752 const gdb_byte *opcode_definitions[256];
23753 void **slot;
23754 struct dwarf2_section_info *section;
23755 const char *section_name;
23756
23757 if (cu->dwo_unit != NULL)
23758 {
23759 if (section_is_gnu)
23760 {
23761 section = &cu->dwo_unit->dwo_file->sections.macro;
23762 section_name = ".debug_macro.dwo";
23763 }
23764 else
23765 {
23766 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23767 section_name = ".debug_macinfo.dwo";
23768 }
23769 }
23770 else
23771 {
23772 if (section_is_gnu)
23773 {
23774 section = &dwarf2_per_objfile->macro;
23775 section_name = ".debug_macro";
23776 }
23777 else
23778 {
23779 section = &dwarf2_per_objfile->macinfo;
23780 section_name = ".debug_macinfo";
23781 }
23782 }
23783
23784 section->read (objfile);
23785 if (section->buffer == NULL)
23786 {
23787 complaint (_("missing %s section"), section_name);
23788 return;
23789 }
23790 abfd = section->get_bfd_owner ();
23791
23792 /* First pass: Find the name of the base filename.
23793 This filename is needed in order to process all macros whose definition
23794 (or undefinition) comes from the command line. These macros are defined
23795 before the first DW_MACINFO_start_file entry, and yet still need to be
23796 associated to the base file.
23797
23798 To determine the base file name, we scan the macro definitions until we
23799 reach the first DW_MACINFO_start_file entry. We then initialize
23800 CURRENT_FILE accordingly so that any macro definition found before the
23801 first DW_MACINFO_start_file can still be associated to the base file. */
23802
23803 mac_ptr = section->buffer + offset;
23804 mac_end = section->buffer + section->size;
23805
23806 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23807 &offset_size, section_is_gnu);
23808 if (mac_ptr == NULL)
23809 {
23810 /* We already issued a complaint. */
23811 return;
23812 }
23813
23814 do
23815 {
23816 /* Do we at least have room for a macinfo type byte? */
23817 if (mac_ptr >= mac_end)
23818 {
23819 /* Complaint is printed during the second pass as GDB will probably
23820 stop the first pass earlier upon finding
23821 DW_MACINFO_start_file. */
23822 break;
23823 }
23824
23825 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23826 mac_ptr++;
23827
23828 /* Note that we rely on the fact that the corresponding GNU and
23829 DWARF constants are the same. */
23830 DIAGNOSTIC_PUSH
23831 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23832 switch (macinfo_type)
23833 {
23834 /* A zero macinfo type indicates the end of the macro
23835 information. */
23836 case 0:
23837 break;
23838
23839 case DW_MACRO_define:
23840 case DW_MACRO_undef:
23841 /* Only skip the data by MAC_PTR. */
23842 {
23843 unsigned int bytes_read;
23844
23845 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23846 mac_ptr += bytes_read;
23847 read_direct_string (abfd, mac_ptr, &bytes_read);
23848 mac_ptr += bytes_read;
23849 }
23850 break;
23851
23852 case DW_MACRO_start_file:
23853 {
23854 unsigned int bytes_read;
23855 int line, file;
23856
23857 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23858 mac_ptr += bytes_read;
23859 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23860 mac_ptr += bytes_read;
23861
23862 current_file = macro_start_file (cu, file, line, current_file, lh);
23863 }
23864 break;
23865
23866 case DW_MACRO_end_file:
23867 /* No data to skip by MAC_PTR. */
23868 break;
23869
23870 case DW_MACRO_define_strp:
23871 case DW_MACRO_undef_strp:
23872 case DW_MACRO_define_sup:
23873 case DW_MACRO_undef_sup:
23874 {
23875 unsigned int bytes_read;
23876
23877 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23878 mac_ptr += bytes_read;
23879 mac_ptr += offset_size;
23880 }
23881 break;
23882
23883 case DW_MACRO_import:
23884 case DW_MACRO_import_sup:
23885 /* Note that, according to the spec, a transparent include
23886 chain cannot call DW_MACRO_start_file. So, we can just
23887 skip this opcode. */
23888 mac_ptr += offset_size;
23889 break;
23890
23891 case DW_MACINFO_vendor_ext:
23892 /* Only skip the data by MAC_PTR. */
23893 if (!section_is_gnu)
23894 {
23895 unsigned int bytes_read;
23896
23897 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23898 mac_ptr += bytes_read;
23899 read_direct_string (abfd, mac_ptr, &bytes_read);
23900 mac_ptr += bytes_read;
23901 }
23902 /* FALLTHROUGH */
23903
23904 default:
23905 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23906 mac_ptr, mac_end, abfd, offset_size,
23907 section);
23908 if (mac_ptr == NULL)
23909 return;
23910 break;
23911 }
23912 DIAGNOSTIC_POP
23913 } while (macinfo_type != 0 && current_file == NULL);
23914
23915 /* Second pass: Process all entries.
23916
23917 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23918 command-line macro definitions/undefinitions. This flag is unset when we
23919 reach the first DW_MACINFO_start_file entry. */
23920
23921 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23922 htab_eq_pointer,
23923 NULL, xcalloc, xfree));
23924 mac_ptr = section->buffer + offset;
23925 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23926 *slot = (void *) mac_ptr;
23927 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
23928 current_file, lh, section,
23929 section_is_gnu, 0, offset_size,
23930 include_hash.get ());
23931 }
23932
23933 /* Return the .debug_loc section to use for CU.
23934 For DWO files use .debug_loc.dwo. */
23935
23936 static struct dwarf2_section_info *
23937 cu_debug_loc_section (struct dwarf2_cu *cu)
23938 {
23939 struct dwarf2_per_objfile *dwarf2_per_objfile
23940 = cu->per_cu->dwarf2_per_objfile;
23941
23942 if (cu->dwo_unit)
23943 {
23944 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23945
23946 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23947 }
23948 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23949 : &dwarf2_per_objfile->loc);
23950 }
23951
23952 /* A helper function that fills in a dwarf2_loclist_baton. */
23953
23954 static void
23955 fill_in_loclist_baton (struct dwarf2_cu *cu,
23956 struct dwarf2_loclist_baton *baton,
23957 const struct attribute *attr)
23958 {
23959 struct dwarf2_per_objfile *dwarf2_per_objfile
23960 = cu->per_cu->dwarf2_per_objfile;
23961 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23962
23963 section->read (dwarf2_per_objfile->objfile);
23964
23965 baton->per_cu = cu->per_cu;
23966 gdb_assert (baton->per_cu);
23967 /* We don't know how long the location list is, but make sure we
23968 don't run off the edge of the section. */
23969 baton->size = section->size - DW_UNSND (attr);
23970 baton->data = section->buffer + DW_UNSND (attr);
23971 baton->base_address = cu->base_address;
23972 baton->from_dwo = cu->dwo_unit != NULL;
23973 }
23974
23975 static void
23976 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23977 struct dwarf2_cu *cu, int is_block)
23978 {
23979 struct dwarf2_per_objfile *dwarf2_per_objfile
23980 = cu->per_cu->dwarf2_per_objfile;
23981 struct objfile *objfile = dwarf2_per_objfile->objfile;
23982 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23983
23984 if (attr->form_is_section_offset ()
23985 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23986 the section. If so, fall through to the complaint in the
23987 other branch. */
23988 && DW_UNSND (attr) < section->get_size (objfile))
23989 {
23990 struct dwarf2_loclist_baton *baton;
23991
23992 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23993
23994 fill_in_loclist_baton (cu, baton, attr);
23995
23996 if (cu->base_known == 0)
23997 complaint (_("Location list used without "
23998 "specifying the CU base address."));
23999
24000 SYMBOL_ACLASS_INDEX (sym) = (is_block
24001 ? dwarf2_loclist_block_index
24002 : dwarf2_loclist_index);
24003 SYMBOL_LOCATION_BATON (sym) = baton;
24004 }
24005 else
24006 {
24007 struct dwarf2_locexpr_baton *baton;
24008
24009 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24010 baton->per_cu = cu->per_cu;
24011 gdb_assert (baton->per_cu);
24012
24013 if (attr->form_is_block ())
24014 {
24015 /* Note that we're just copying the block's data pointer
24016 here, not the actual data. We're still pointing into the
24017 info_buffer for SYM's objfile; right now we never release
24018 that buffer, but when we do clean up properly this may
24019 need to change. */
24020 baton->size = DW_BLOCK (attr)->size;
24021 baton->data = DW_BLOCK (attr)->data;
24022 }
24023 else
24024 {
24025 dwarf2_invalid_attrib_class_complaint ("location description",
24026 sym->natural_name ());
24027 baton->size = 0;
24028 }
24029
24030 SYMBOL_ACLASS_INDEX (sym) = (is_block
24031 ? dwarf2_locexpr_block_index
24032 : dwarf2_locexpr_index);
24033 SYMBOL_LOCATION_BATON (sym) = baton;
24034 }
24035 }
24036
24037 /* See read.h. */
24038
24039 struct objfile *
24040 dwarf2_per_cu_data::objfile () const
24041 {
24042 struct objfile *objfile = dwarf2_per_objfile->objfile;
24043
24044 /* Return the master objfile, so that we can report and look up the
24045 correct file containing this variable. */
24046 if (objfile->separate_debug_objfile_backlink)
24047 objfile = objfile->separate_debug_objfile_backlink;
24048
24049 return objfile;
24050 }
24051
24052 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24053 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24054 CU_HEADERP first. */
24055
24056 static const struct comp_unit_head *
24057 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24058 const struct dwarf2_per_cu_data *per_cu)
24059 {
24060 const gdb_byte *info_ptr;
24061
24062 if (per_cu->cu)
24063 return &per_cu->cu->header;
24064
24065 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24066
24067 memset (cu_headerp, 0, sizeof (*cu_headerp));
24068 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24069 rcuh_kind::COMPILE);
24070
24071 return cu_headerp;
24072 }
24073
24074 /* See read.h. */
24075
24076 int
24077 dwarf2_per_cu_data::addr_size () const
24078 {
24079 struct comp_unit_head cu_header_local;
24080 const struct comp_unit_head *cu_headerp;
24081
24082 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24083
24084 return cu_headerp->addr_size;
24085 }
24086
24087 /* See read.h. */
24088
24089 int
24090 dwarf2_per_cu_data::offset_size () const
24091 {
24092 struct comp_unit_head cu_header_local;
24093 const struct comp_unit_head *cu_headerp;
24094
24095 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24096
24097 return cu_headerp->offset_size;
24098 }
24099
24100 /* See read.h. */
24101
24102 int
24103 dwarf2_per_cu_data::ref_addr_size () const
24104 {
24105 struct comp_unit_head cu_header_local;
24106 const struct comp_unit_head *cu_headerp;
24107
24108 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24109
24110 if (cu_headerp->version == 2)
24111 return cu_headerp->addr_size;
24112 else
24113 return cu_headerp->offset_size;
24114 }
24115
24116 /* See read.h. */
24117
24118 CORE_ADDR
24119 dwarf2_per_cu_data::text_offset () const
24120 {
24121 struct objfile *objfile = dwarf2_per_objfile->objfile;
24122
24123 return objfile->text_section_offset ();
24124 }
24125
24126 /* See read.h. */
24127
24128 struct type *
24129 dwarf2_per_cu_data::addr_type () const
24130 {
24131 struct objfile *objfile = dwarf2_per_objfile->objfile;
24132 struct type *void_type = objfile_type (objfile)->builtin_void;
24133 struct type *addr_type = lookup_pointer_type (void_type);
24134 int addr_size = this->addr_size ();
24135
24136 if (TYPE_LENGTH (addr_type) == addr_size)
24137 return addr_type;
24138
24139 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24140 return addr_type;
24141 }
24142
24143 /* Locate the .debug_info compilation unit from CU's objfile which contains
24144 the DIE at OFFSET. Raises an error on failure. */
24145
24146 static struct dwarf2_per_cu_data *
24147 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24148 unsigned int offset_in_dwz,
24149 struct dwarf2_per_objfile *dwarf2_per_objfile)
24150 {
24151 struct dwarf2_per_cu_data *this_cu;
24152 int low, high;
24153
24154 low = 0;
24155 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24156 while (high > low)
24157 {
24158 struct dwarf2_per_cu_data *mid_cu;
24159 int mid = low + (high - low) / 2;
24160
24161 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24162 if (mid_cu->is_dwz > offset_in_dwz
24163 || (mid_cu->is_dwz == offset_in_dwz
24164 && mid_cu->sect_off + mid_cu->length >= sect_off))
24165 high = mid;
24166 else
24167 low = mid + 1;
24168 }
24169 gdb_assert (low == high);
24170 this_cu = dwarf2_per_objfile->all_comp_units[low];
24171 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24172 {
24173 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24174 error (_("Dwarf Error: could not find partial DIE containing "
24175 "offset %s [in module %s]"),
24176 sect_offset_str (sect_off),
24177 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24178
24179 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24180 <= sect_off);
24181 return dwarf2_per_objfile->all_comp_units[low-1];
24182 }
24183 else
24184 {
24185 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24186 && sect_off >= this_cu->sect_off + this_cu->length)
24187 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24188 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24189 return this_cu;
24190 }
24191 }
24192
24193 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24194
24195 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24196 : per_cu (per_cu_),
24197 mark (false),
24198 has_loclist (false),
24199 checked_producer (false),
24200 producer_is_gxx_lt_4_6 (false),
24201 producer_is_gcc_lt_4_3 (false),
24202 producer_is_icc (false),
24203 producer_is_icc_lt_14 (false),
24204 producer_is_codewarrior (false),
24205 processing_has_namespace_info (false)
24206 {
24207 per_cu->cu = this;
24208 }
24209
24210 /* Destroy a dwarf2_cu. */
24211
24212 dwarf2_cu::~dwarf2_cu ()
24213 {
24214 per_cu->cu = NULL;
24215 }
24216
24217 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24218
24219 static void
24220 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24221 enum language pretend_language)
24222 {
24223 struct attribute *attr;
24224
24225 /* Set the language we're debugging. */
24226 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24227 if (attr != nullptr)
24228 set_cu_language (DW_UNSND (attr), cu);
24229 else
24230 {
24231 cu->language = pretend_language;
24232 cu->language_defn = language_def (cu->language);
24233 }
24234
24235 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24236 }
24237
24238 /* Increase the age counter on each cached compilation unit, and free
24239 any that are too old. */
24240
24241 static void
24242 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24243 {
24244 struct dwarf2_per_cu_data *per_cu, **last_chain;
24245
24246 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24247 per_cu = dwarf2_per_objfile->read_in_chain;
24248 while (per_cu != NULL)
24249 {
24250 per_cu->cu->last_used ++;
24251 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24252 dwarf2_mark (per_cu->cu);
24253 per_cu = per_cu->cu->read_in_chain;
24254 }
24255
24256 per_cu = dwarf2_per_objfile->read_in_chain;
24257 last_chain = &dwarf2_per_objfile->read_in_chain;
24258 while (per_cu != NULL)
24259 {
24260 struct dwarf2_per_cu_data *next_cu;
24261
24262 next_cu = per_cu->cu->read_in_chain;
24263
24264 if (!per_cu->cu->mark)
24265 {
24266 delete per_cu->cu;
24267 *last_chain = next_cu;
24268 }
24269 else
24270 last_chain = &per_cu->cu->read_in_chain;
24271
24272 per_cu = next_cu;
24273 }
24274 }
24275
24276 /* Remove a single compilation unit from the cache. */
24277
24278 static void
24279 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24280 {
24281 struct dwarf2_per_cu_data *per_cu, **last_chain;
24282 struct dwarf2_per_objfile *dwarf2_per_objfile
24283 = target_per_cu->dwarf2_per_objfile;
24284
24285 per_cu = dwarf2_per_objfile->read_in_chain;
24286 last_chain = &dwarf2_per_objfile->read_in_chain;
24287 while (per_cu != NULL)
24288 {
24289 struct dwarf2_per_cu_data *next_cu;
24290
24291 next_cu = per_cu->cu->read_in_chain;
24292
24293 if (per_cu == target_per_cu)
24294 {
24295 delete per_cu->cu;
24296 per_cu->cu = NULL;
24297 *last_chain = next_cu;
24298 break;
24299 }
24300 else
24301 last_chain = &per_cu->cu->read_in_chain;
24302
24303 per_cu = next_cu;
24304 }
24305 }
24306
24307 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24308 We store these in a hash table separate from the DIEs, and preserve them
24309 when the DIEs are flushed out of cache.
24310
24311 The CU "per_cu" pointer is needed because offset alone is not enough to
24312 uniquely identify the type. A file may have multiple .debug_types sections,
24313 or the type may come from a DWO file. Furthermore, while it's more logical
24314 to use per_cu->section+offset, with Fission the section with the data is in
24315 the DWO file but we don't know that section at the point we need it.
24316 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24317 because we can enter the lookup routine, get_die_type_at_offset, from
24318 outside this file, and thus won't necessarily have PER_CU->cu.
24319 Fortunately, PER_CU is stable for the life of the objfile. */
24320
24321 struct dwarf2_per_cu_offset_and_type
24322 {
24323 const struct dwarf2_per_cu_data *per_cu;
24324 sect_offset sect_off;
24325 struct type *type;
24326 };
24327
24328 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24329
24330 static hashval_t
24331 per_cu_offset_and_type_hash (const void *item)
24332 {
24333 const struct dwarf2_per_cu_offset_and_type *ofs
24334 = (const struct dwarf2_per_cu_offset_and_type *) item;
24335
24336 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24337 }
24338
24339 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24340
24341 static int
24342 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24343 {
24344 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24345 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24346 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24347 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24348
24349 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24350 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24351 }
24352
24353 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24354 table if necessary. For convenience, return TYPE.
24355
24356 The DIEs reading must have careful ordering to:
24357 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24358 reading current DIE.
24359 * Not trying to dereference contents of still incompletely read in types
24360 while reading in other DIEs.
24361 * Enable referencing still incompletely read in types just by a pointer to
24362 the type without accessing its fields.
24363
24364 Therefore caller should follow these rules:
24365 * Try to fetch any prerequisite types we may need to build this DIE type
24366 before building the type and calling set_die_type.
24367 * After building type call set_die_type for current DIE as soon as
24368 possible before fetching more types to complete the current type.
24369 * Make the type as complete as possible before fetching more types. */
24370
24371 static struct type *
24372 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24373 {
24374 struct dwarf2_per_objfile *dwarf2_per_objfile
24375 = cu->per_cu->dwarf2_per_objfile;
24376 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24377 struct objfile *objfile = dwarf2_per_objfile->objfile;
24378 struct attribute *attr;
24379 struct dynamic_prop prop;
24380
24381 /* For Ada types, make sure that the gnat-specific data is always
24382 initialized (if not already set). There are a few types where
24383 we should not be doing so, because the type-specific area is
24384 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24385 where the type-specific area is used to store the floatformat).
24386 But this is not a problem, because the gnat-specific information
24387 is actually not needed for these types. */
24388 if (need_gnat_info (cu)
24389 && TYPE_CODE (type) != TYPE_CODE_FUNC
24390 && TYPE_CODE (type) != TYPE_CODE_FLT
24391 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24392 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24393 && TYPE_CODE (type) != TYPE_CODE_METHOD
24394 && !HAVE_GNAT_AUX_INFO (type))
24395 INIT_GNAT_SPECIFIC (type);
24396
24397 /* Read DW_AT_allocated and set in type. */
24398 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24399 if (attr != NULL && attr->form_is_block ())
24400 {
24401 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24402 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24403 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24404 }
24405 else if (attr != NULL)
24406 {
24407 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24408 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24409 sect_offset_str (die->sect_off));
24410 }
24411
24412 /* Read DW_AT_associated and set in type. */
24413 attr = dwarf2_attr (die, DW_AT_associated, cu);
24414 if (attr != NULL && attr->form_is_block ())
24415 {
24416 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24417 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24418 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24419 }
24420 else if (attr != NULL)
24421 {
24422 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24423 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24424 sect_offset_str (die->sect_off));
24425 }
24426
24427 /* Read DW_AT_data_location and set in type. */
24428 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24429 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24430 cu->per_cu->addr_type ()))
24431 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24432
24433 if (dwarf2_per_objfile->die_type_hash == NULL)
24434 dwarf2_per_objfile->die_type_hash
24435 = htab_up (htab_create_alloc (127,
24436 per_cu_offset_and_type_hash,
24437 per_cu_offset_and_type_eq,
24438 NULL, xcalloc, xfree));
24439
24440 ofs.per_cu = cu->per_cu;
24441 ofs.sect_off = die->sect_off;
24442 ofs.type = type;
24443 slot = (struct dwarf2_per_cu_offset_and_type **)
24444 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24445 if (*slot)
24446 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24447 sect_offset_str (die->sect_off));
24448 *slot = XOBNEW (&objfile->objfile_obstack,
24449 struct dwarf2_per_cu_offset_and_type);
24450 **slot = ofs;
24451 return type;
24452 }
24453
24454 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24455 or return NULL if the die does not have a saved type. */
24456
24457 static struct type *
24458 get_die_type_at_offset (sect_offset sect_off,
24459 struct dwarf2_per_cu_data *per_cu)
24460 {
24461 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24462 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24463
24464 if (dwarf2_per_objfile->die_type_hash == NULL)
24465 return NULL;
24466
24467 ofs.per_cu = per_cu;
24468 ofs.sect_off = sect_off;
24469 slot = ((struct dwarf2_per_cu_offset_and_type *)
24470 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24471 if (slot)
24472 return slot->type;
24473 else
24474 return NULL;
24475 }
24476
24477 /* Look up the type for DIE in CU in die_type_hash,
24478 or return NULL if DIE does not have a saved type. */
24479
24480 static struct type *
24481 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24482 {
24483 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24484 }
24485
24486 /* Add a dependence relationship from CU to REF_PER_CU. */
24487
24488 static void
24489 dwarf2_add_dependence (struct dwarf2_cu *cu,
24490 struct dwarf2_per_cu_data *ref_per_cu)
24491 {
24492 void **slot;
24493
24494 if (cu->dependencies == NULL)
24495 cu->dependencies
24496 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24497 NULL, &cu->comp_unit_obstack,
24498 hashtab_obstack_allocate,
24499 dummy_obstack_deallocate);
24500
24501 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24502 if (*slot == NULL)
24503 *slot = ref_per_cu;
24504 }
24505
24506 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24507 Set the mark field in every compilation unit in the
24508 cache that we must keep because we are keeping CU. */
24509
24510 static int
24511 dwarf2_mark_helper (void **slot, void *data)
24512 {
24513 struct dwarf2_per_cu_data *per_cu;
24514
24515 per_cu = (struct dwarf2_per_cu_data *) *slot;
24516
24517 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24518 reading of the chain. As such dependencies remain valid it is not much
24519 useful to track and undo them during QUIT cleanups. */
24520 if (per_cu->cu == NULL)
24521 return 1;
24522
24523 if (per_cu->cu->mark)
24524 return 1;
24525 per_cu->cu->mark = true;
24526
24527 if (per_cu->cu->dependencies != NULL)
24528 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24529
24530 return 1;
24531 }
24532
24533 /* Set the mark field in CU and in every other compilation unit in the
24534 cache that we must keep because we are keeping CU. */
24535
24536 static void
24537 dwarf2_mark (struct dwarf2_cu *cu)
24538 {
24539 if (cu->mark)
24540 return;
24541 cu->mark = true;
24542 if (cu->dependencies != NULL)
24543 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24544 }
24545
24546 static void
24547 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24548 {
24549 while (per_cu)
24550 {
24551 per_cu->cu->mark = false;
24552 per_cu = per_cu->cu->read_in_chain;
24553 }
24554 }
24555
24556 /* Trivial hash function for partial_die_info: the hash value of a DIE
24557 is its offset in .debug_info for this objfile. */
24558
24559 static hashval_t
24560 partial_die_hash (const void *item)
24561 {
24562 const struct partial_die_info *part_die
24563 = (const struct partial_die_info *) item;
24564
24565 return to_underlying (part_die->sect_off);
24566 }
24567
24568 /* Trivial comparison function for partial_die_info structures: two DIEs
24569 are equal if they have the same offset. */
24570
24571 static int
24572 partial_die_eq (const void *item_lhs, const void *item_rhs)
24573 {
24574 const struct partial_die_info *part_die_lhs
24575 = (const struct partial_die_info *) item_lhs;
24576 const struct partial_die_info *part_die_rhs
24577 = (const struct partial_die_info *) item_rhs;
24578
24579 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24580 }
24581
24582 struct cmd_list_element *set_dwarf_cmdlist;
24583 struct cmd_list_element *show_dwarf_cmdlist;
24584
24585 static void
24586 set_dwarf_cmd (const char *args, int from_tty)
24587 {
24588 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24589 gdb_stdout);
24590 }
24591
24592 static void
24593 show_dwarf_cmd (const char *args, int from_tty)
24594 {
24595 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24596 }
24597
24598 static void
24599 show_check_physname (struct ui_file *file, int from_tty,
24600 struct cmd_list_element *c, const char *value)
24601 {
24602 fprintf_filtered (file,
24603 _("Whether to check \"physname\" is %s.\n"),
24604 value);
24605 }
24606
24607 void _initialize_dwarf2_read ();
24608 void
24609 _initialize_dwarf2_read ()
24610 {
24611 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24612 Set DWARF specific variables.\n\
24613 Configure DWARF variables such as the cache size."),
24614 &set_dwarf_cmdlist, "maintenance set dwarf ",
24615 0/*allow-unknown*/, &maintenance_set_cmdlist);
24616
24617 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24618 Show DWARF specific variables.\n\
24619 Show DWARF variables such as the cache size."),
24620 &show_dwarf_cmdlist, "maintenance show dwarf ",
24621 0/*allow-unknown*/, &maintenance_show_cmdlist);
24622
24623 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24624 &dwarf_max_cache_age, _("\
24625 Set the upper bound on the age of cached DWARF compilation units."), _("\
24626 Show the upper bound on the age of cached DWARF compilation units."), _("\
24627 A higher limit means that cached compilation units will be stored\n\
24628 in memory longer, and more total memory will be used. Zero disables\n\
24629 caching, which can slow down startup."),
24630 NULL,
24631 show_dwarf_max_cache_age,
24632 &set_dwarf_cmdlist,
24633 &show_dwarf_cmdlist);
24634
24635 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24636 Set debugging of the DWARF reader."), _("\
24637 Show debugging of the DWARF reader."), _("\
24638 When enabled (non-zero), debugging messages are printed during DWARF\n\
24639 reading and symtab expansion. A value of 1 (one) provides basic\n\
24640 information. A value greater than 1 provides more verbose information."),
24641 NULL,
24642 NULL,
24643 &setdebuglist, &showdebuglist);
24644
24645 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24646 Set debugging of the DWARF DIE reader."), _("\
24647 Show debugging of the DWARF DIE reader."), _("\
24648 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24649 The value is the maximum depth to print."),
24650 NULL,
24651 NULL,
24652 &setdebuglist, &showdebuglist);
24653
24654 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24655 Set debugging of the dwarf line reader."), _("\
24656 Show debugging of the dwarf line reader."), _("\
24657 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24658 A value of 1 (one) provides basic information.\n\
24659 A value greater than 1 provides more verbose information."),
24660 NULL,
24661 NULL,
24662 &setdebuglist, &showdebuglist);
24663
24664 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24665 Set cross-checking of \"physname\" code against demangler."), _("\
24666 Show cross-checking of \"physname\" code against demangler."), _("\
24667 When enabled, GDB's internal \"physname\" code is checked against\n\
24668 the demangler."),
24669 NULL, show_check_physname,
24670 &setdebuglist, &showdebuglist);
24671
24672 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24673 no_class, &use_deprecated_index_sections, _("\
24674 Set whether to use deprecated gdb_index sections."), _("\
24675 Show whether to use deprecated gdb_index sections."), _("\
24676 When enabled, deprecated .gdb_index sections are used anyway.\n\
24677 Normally they are ignored either because of a missing feature or\n\
24678 performance issue.\n\
24679 Warning: This option must be enabled before gdb reads the file."),
24680 NULL,
24681 NULL,
24682 &setlist, &showlist);
24683
24684 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24685 &dwarf2_locexpr_funcs);
24686 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24687 &dwarf2_loclist_funcs);
24688
24689 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24690 &dwarf2_block_frame_base_locexpr_funcs);
24691 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24692 &dwarf2_block_frame_base_loclist_funcs);
24693
24694 #if GDB_SELF_TEST
24695 selftests::register_test ("dw2_expand_symtabs_matching",
24696 selftests::dw2_expand_symtabs_matching::run_test);
24697 #endif
24698 }
This page took 0.548194 seconds and 5 git commands to generate.